为什么光标显示错误

时间:2009-10-29 05:01:36

标签: sql sql-server tsql sql-server-2000 cursor

在SQL Server 2000上工作。想要使用SQL Server Job Schedular进行自动电子邮件通知。如果我在查询分析器中运行波纹管游标语法而不是没有错误。但是当我在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''+@a+''''
        + ''<br /><br />Customer Relationship Department''
        EXEC sp_send_mail 
        ''nahid10@gmail.com'', --- 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: 

希望发送生日祝福,并希望告知今天有多少用户有生日。

1 个答案:

答案 0 :(得分:0)

如果没有错误消息,则很难诊断问题。

老实说,代码一团糟。这是内部文本的第一个剪切。我修复了光标,你的迭代习语,以及你对出生日期的过滤。我不知道你想用@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 
    'nahid10@gmail.com', --- 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