Invoke LARGE SQL Script from a SQL Job

  • Need to invoke a long (1500 line) SQL Script from a SQL Job.  The script contains all sorts of stuff including "If Exists..  drop Procs.. ", "Create Procs ..." , "GO" statements galore etc.

    I am familiar w/ the invocation of a stored proc from a Job via the command:

    EXECUTE master..xp_cmdshell 'osql -E -Qadministrative..spr_Updatestats_all_db -oC:\CERSAsql\SQLReports\UpdateStatsAllDB.rpt'

    How do I build a SQL JOB to invoke a HUGE SQL Script that will not terminate when it encounters GO's etc.  (I can't build the Script into a SQL Proc because the script itself CREATEs dozens of Procs)

    thx in advance.

    BT
  • Try Saving the sql script to file, having the job call osql and osql runs the script.

     

  • Bill, Ray :

    Not really sure OSQL allows GO. The parameter description of OSQL Utility in BOL says:

    ......(Note that the query statement should not include GO).....

    Did you try a job with many steps? Tell us more what you are trying to accomplish, maybe we will have other ideas

    Yelena

    Regards,Yelena Varsha

  • Yelena - it worked for me.  (w/ the dozens of GO statements embedded throughout the script)  I too was surprised that it completed successfully.

    BT
  • Hello,

    The SQL agent will also support batch separators (GO) in the T-SQL type of job step.

    JG

  • try this :

    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)]'

    IF (SELECT COUNT(*) FROM msdb.dbo.sysjobs WHERE name = N'Run_OSQL') > 0

      PRINT N'The job "Run_OSQL" already exists so will not be replaced.'

    ELSE

    BEGIN

      -- Add the job

      EXECUTE @ReturnCode = msdb.dbo.sp_add_job @job_id = @JobID OUTPUT , @job_name = N'Run_OSQL', @owner_login_name = N'sa', @description = N'I hope i m not the onlyone using these comments', @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'mystem', @command = N'osql -E -S MYSERVER -i "\\unc\fileshare\mySQLscript.sql" -n', @database_name = N'', @server = N'', @database_user_name = N'', @subsystem = N'CmdExec', @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'Every17u', @enabled = 1, @freq_type = 4, @active_start_date = 20040301, @active_start_time = 170000, @freq_interval = 1, @freq_subday_type = 1, @freq_subday_interval = 0, @freq_relative_interval = 0, @freq_recurrence_factor = 0, @active_end_date = 99991231, @active_end_time = 235959

      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:

     

    Johan

    Learn to play, play to learn !

    Dont drive faster than your guardian angel can fly ...
    but keeping both feet on the ground wont get you anywhere :w00t:

    - How to post Performance Problems
    - How to post data/code to get the best help[/url]

    - How to prevent a sore throat after hours of presenting ppt

    press F1 for solution, press shift+F1 for urgent solution 😀

    Need a bit of Powershell? How about this

    Who am I ? Sometimes this is me but most of the time this is me

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply