views:

51

answers:

1

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.

A: 

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
Peter
thank you very much.To show me the errorthanks again .
HelloBD