July 7, 2005 at 9:04 am
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.
July 7, 2005 at 9:11 am
Try Saving the sql script to file, having the job call osql and osql runs the script.
July 7, 2005 at 3:00 pm
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
July 8, 2005 at 6:51 am
Yelena - it worked for me. (w/ the dozens of GO statements embedded throughout the script) I too was surprised that it completed successfully.
July 8, 2005 at 7:37 am
Hello,
The SQL agent will also support batch separators (GO) in the T-SQL type of job step.
JG
July 8, 2005 at 8:04 am
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