2009-10-29 103 views
0
SQL Server上

工作2000年使用SQL Server工作Schedular.If我查詢分析器中運行的波紋管光標的語法比我沒有得到任何錯誤。但是當我設置SqlServer的試劑 - 這句法要自動電子郵件通知>喬布斯然後它是創建錯誤。爲什麼光標顯示錯誤

-- Script generated on 29-Oct-09 11:57 AM 
-- By: sa 
-- Server: (LOCAL) 

BEGIN TRANSACTION    
    DECLARE @JobID BINARY(16) 
    DECLARE @ReturnCode INT  
    SELECT @ReturnCode = 0  
IF (SELECT COUNT(*) FROM msdb.dbo.syscategories WHERE name = N'[Uncategorized (Local)]') < 1 
    EXECUTE msdb.dbo.sp_add_category @name = N'[Uncategorized (Local)]' 

    -- Delete the job with the same name (if it exists) 
    SELECT @JobID = job_id  
    FROM msdb.dbo.sysjobs  
    WHERE (name = N'Check4')  
    IF (@JobID IS NOT NULL)  
    BEGIN 
    -- Check if the job is a multi-server job 
    IF (EXISTS (SELECT * 
       FROM msdb.dbo.sysjobservers 
       WHERE (job_id = @JobID) AND (server_id <> 0))) 
    BEGIN 
    -- There is, so abort the script 
    RAISERROR (N'Unable to import job ''Check4'' since there is already a multi-server job with this name.', 16, 1) 
    GOTO QuitWithRollback 
    END 
    ELSE 
    -- Delete the [local] job 
    EXECUTE msdb.dbo.sp_delete_job @job_name = N'Check4' 
    SELECT @JobID = NULL 
    END 

BEGIN 

    -- Add the job 
    EXECUTE @ReturnCode = msdb.dbo.sp_add_job @job_id = @JobID OUTPUT , @job_name = N'Check4', @owner_login_name = N'sa', @description = N'No description available.', @category_name = N'[Uncategorized (Local)]', @enabled = 1, @notify_level_email = 0, @notify_level_page = 0, @notify_level_netsend = 0, @notify_level_eventlog = 2, @delete_level= 0 
    IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback 

    -- Add the job steps 
    EXECUTE @ReturnCode = msdb.dbo.sp_add_jobstep @job_id = @JobID, @step_id = 1, @step_name = N'Step4', @command = N'DECLARE 
@out_desc VARCHAR(1000), 
@out_mesg VARCHAR(10) 

DECLARE @name VARCHAR(20), 
@birthdate datetime, 
@email NVARCHAR(50) 



DECLARE @body NVARCHAR(1000) 

DECLARE C1 CURSOR READ_ONLY 
FOR 
SELECT [Name],[BirthDate],[Email] 
FROM Customers3 

OPEN C1 
FETCH NEXT FROM C1 INTO 
@name, @birthdate, @email 
WHILE @@FETCH_STATUS = 0 
BEGIN 


Declare @a int 
SELECT @a=count(*) FROM Customers where name like ''%s%'' 

    IF DATEPART(DAY,@birthdate) = DATEPART(DAY,GETDATE()) 
    AND DATEPART(MONTH,@birthdate) = DATEPART(MONTH,GETDATE()) 
     AND DATEPART(hour,@birthdate) = DATEPART(hour,GETDATE()) 
     AND DATEPART(minute,@birthdate) = DATEPART(minute,GETDATE()) 
    BEGIN 
     SET @body = ''<b>HappyBirthday '' + @name + ''</b><br />Many happy returns of the day''[email protected]+'''' 
     + ''<br /><br />Customer Relationship Department'' 
     EXEC sp_send_mail 
     ''[email protected]'', --- add your Email Address here 
     ''n7n10u'',  ----add your Password here 
     @email, 
     ''Birthday Wishes'', 
     @body, 
     ''htmlbody'', @output_mesg = @out_mesg output, @output_desc = @out_desc output 

     PRINT @out_mesg 
     PRINT @out_desc 
    END 
    FETCH NEXT FROM C1 INTO 
    @name, @birthdate, @email 
END 
CLOSE C1 
DEALLOCATE C1 
', @database_name = N'CustomerDetails', @server = N'', @database_user_name = N'', @subsystem = N'TSQL', @cmdexec_success_code = 0, @flags = 0, @retry_attempts = 0, @retry_interval = 1, @output_file_name = N'', @on_success_step_id = 0, @on_success_action = 1, @on_fail_step_id = 0, @on_fail_action = 2 
    IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback 
    EXECUTE @ReturnCode = msdb.dbo.sp_update_job @job_id = @JobID, @start_step_id = 1 

    IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback 

    -- Add the job schedules 
    EXECUTE @ReturnCode = msdb.dbo.sp_add_jobschedule @job_id = @JobID, @name = N'Schedule4', @enabled = 1, @freq_type = 8, @active_start_date = 20091029, @active_start_time = 115200, @freq_interval = 16, @freq_subday_type = 4, @freq_subday_interval = 5, @freq_relative_interval = 0, @freq_recurrence_factor = 1, @active_end_date = 99991231, @active_end_time = 145959 
    IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback 

    -- Add the Target Servers 
    EXECUTE @ReturnCode = msdb.dbo.sp_add_jobserver @job_id = @JobID, @server_name = N'(local)' 
    IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback 

END 
COMMIT TRANSACTION   
GOTO EndSave    
QuitWithRollback: 
    IF (@@TRANCOUNT > 0) ROLLBACK TRANSACTION 
EndSave: 

想發送生日祝願,並且還想通知有多少用戶今天有生日。

+0

有什麼錯誤訊息? – 2009-10-29 13:48:24

回答

0

沒有錯誤消息,很難診斷問題。

老實說,代碼亂七八糟。這是內部文本的第一部分。我修正了遊標,你的迭代習慣用法,以及你的出生日期過濾。我不知道你想要做什麼w/@a,但它是錯誤的。

DECLARE 
    @out_desc VARCHAR(1000) 
, @out_mesg VARCHAR(10) 
, @name VARCHAR(20) 
, @birthdate datetime 
, @email NVARCHAR(50) 
, @body NVARCHAR(1000) 

-- Use a cursor variable for easier scoping: 
DECLARE @C1 CURSOR 

-- Since Birthdate = Today should be very selective, we 
-- use the STATIC keyword to materialize the results in tempdb 
-- before iteration. 
SET @C1 = CURSOR STATIC FOR 
    SELECT [Name],[BirthDate],[Email] 
    FROM Customers3 
    WHERE BirthDate = DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0) 


-- Since we used the STATIC keywork, calling OPEN will execute the SELECT statement above 
-- and materialize the results in tempdb. The cursor will then iterate over the materialized 
-- results in tempdb, rather than Customers3. 
OPEN @C1 

-- this is a better idiom for cursor iteration: one FETCH NEXT, rather then two 
-- and therefore fewer places you need to touch the code to make changes 
WHILE 1=1 BEGIN 
    FETCH NEXT FROM @C1 INTO @name, @birthdate, @email 
    IF @@FETCH_STATUS <> 0 BREAK 

    -- this is wrong, whatever you are trying to do. 
    Declare @a int 
    SELECT @a=count(*) FROM Customers where name like '%s%' 

    -- moved to the cursor WHERE clause. Why were you filtering on hour and minute? 
    -- IF DATEPART(DAY,@birthdate) = DATEPART(DAY,GETDATE()) 
    -- AND DATEPART(MONTH,@birthdate) = DATEPART(MONTH,GETDATE()) 
    --  AND DATEPART(hour,@birthdate) = DATEPART(hour,GETDATE()) 
    --  AND DATEPART(minute,@birthdate) = DATEPART(minute,GETDATE()) 
    -- BEGIN 

    -- to concatenant an INT, you must first convert it to a varchar 
    SET @body = '<b>HappyBirthday ' + @name + '</b><br />Many happy returns of the day'+CONVERT(VARCAR(30),@a)+'' 
    + '<br /><br />Customer Relationship Department' 

    EXEC sp_send_mail 
    '[email protected]', --- add your Email Address here 
    'n7n10u',  ----add your Password here 
    @email, 
    'Birthday Wishes', 
    @body, 
    'htmlbody', @output_mesg = @out_mesg output, @output_desc = @out_desc output 

    PRINT @out_mesg 
    PRINT @out_desc 

END 
CLOSE @C1 
DEALLOCATE @C1 
+0

非常感謝。給我看錯誤 再次感謝。 – HelloBD 2009-10-31 16:09:21