为什么光标显示错误

发布于 2024-08-08 20:13:51 字数 4603 浏览 6 评论 0原文

在 SQL Server 2000 上工作。想要使用 SQL Server 作业计划自动发送电子邮件通知。如果我在查询分析器中运行以下游标语法,则不会出现错误。但是当我在 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 
        ''[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: 

想要发送生日祝福,还想告知有多少用户今天生日。

work on SQL Server 2000. want to Automated Email Notifications using SQL Server Job Schedular.If i run the bellow cursor syntax in query analyzer than i get no error .But when i set this syntax on SqlServer agent->Jobs then it's create error.

-- 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 
        ''[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: 

want to send birthday wish and also want to inform how many users have birth day today.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

调妓 2024-08-15 20:13:51

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

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

Without an error message it's hard to diagnose the problem.

Honestly, the code's a mess. Here's a first cut of the inner text. I fixed the cursor, your iteration idiom, and your filtering for birth date. I don't know what you are trying to do w/ @a, but it is wrong.

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文