We help IT Professionals succeed at work.
Get Started

Setting Variables in TSQL

dec789 asked
Last Modified: 2012-05-09
I've pasted the code I'm working with to the bottom of this question.

I need some help setting some variables in the attached script.  I don't know anything about TSQL  SQL Server generated the code for me.  I use the code to set up jobs for the new databases we create.  Currently I do a search and replace to change the variables its sort of tedious.   I would prefer, if possible, to be able to change a couple parameters at the very beginning of the script and have them flow down to the body of the script.  

In the example script the text "AHEF_SV6" (the database name) and "AEHF_SV6_Friday" (the name and description of the job) are two pieces of text I woul like to set at the beginning of the script.

Also in the script below:
Can the line " @active_start_date = 20100617 " be changed to always have the current date?  So I don't have to remember to change it.

Can the line " @freq_type = 8 " be changed so I can set the day number (8) at the begining of the script?  This would help me to not forget to set it and not accidently screw up the body of the text.

Thank you for your time.

  DECLARE @ReturnCode INT    
  SELECT @ReturnCode = 0    
IF (SELECT COUNT(*) FROM msdb.dbo.syscategories WHERE name = N'Database Maintenance') < 1
  EXECUTE msdb.dbo.sp_add_category @name = N'Database Maintenance'


  -- Add the job
  EXECUTE @ReturnCode = msdb.dbo.sp_add_job @job_id = @JobID OUTPUT , @job_name = N'AEHF_SV6_Friday', @owner_login_name = N'sa', @description = N'AEHF_SV6_Friday', @category_name = N'Database Maintenance', @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'AEHF_SV6_Friday', @command = N'EXEC AEHF_SV6_Friday', @database_name = N'AEHF_SV6', @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'AEHF_SV6_Friday', @enabled = 1, @freq_type = 8, @active_start_date = 20100617, @active_start_time = 233500, @freq_interval = 32, @freq_subday_type = 1, @freq_subday_interval = 0, @freq_relative_interval = 0, @freq_recurrence_factor = 1, @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

GOTO   EndSave              

Watch Question
Expert of the Quarter 2010
Expert of the Year 2010
This problem has been solved!
Unlock 3 Answers and 5 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE