Link to home
Start Free TrialLog in
Avatar of dec789
dec789

asked on

Setting Variables in TSQL

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.


BEGIN TRANSACTION            
  DECLARE @JobID BINARY(16)  
  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'



BEGIN

  -- 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

END
COMMIT TRANSACTION          
GOTO   EndSave              
QuitWithRollback:
  IF (@@TRANCOUNT > 0) ROLLBACK TRANSACTION
EndSave:


SOLUTION
Avatar of Adam Menkes
Adam Menkes
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
create procedure myproc (@myjobname nvarchar(25)) as begin

...
 EXECUTE   ... @job_name = @myjobname, @description =  @myjobname ...
...

end

and you would call it with

exec myproc  N'AEHF_SV6_Friday'

of course, you might want more parameters, and output parameters, but this is the basic idea.
ASKER CERTIFIED SOLUTION
Avatar of cyberkiwi
cyberkiwi
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dec789
dec789

ASKER

Thank you all for your time between the three responses I will be able get this thing going.  amenkes sorry I didn't emphasize enough how much I don't know about TSQL.  You showed me how to  declare set up text variable and the date variable but the stored procedure thing is way over my head at this point.  CyberKiwi and MapleMale thank you for showing me how to declare the other variables and how to actually place the variable down in the body of the code.  Thanks again for your time.