sql server - Using current database name in T-SQL has Using statement -


I have run a T-SQL statement to update more than one database. The problem is the use of the following T-SQL doing

  use [msdb] announced @jobId binary GO (16) EXEC msdb.dbo.sp_add_job @ job_name = N'test2 ', @ enabled = 1, @ start_step_id = 1, @ notify_level_eventlog = 0 @ notify_level_email = 2, @ notify_level_netsend = 2, @ notify_level_page = 2, @ delete_level = 0, @ description = N ', @ CATEGORY_NAME = N' [unclassified (local)] ', @ owner_login_name = N'sa', @ Notify_email_operator_name = 'N', @ notify_netsend_operator_name = N ', @ notify_page_operator_name = n', @job_id = @jobId selection of output @jobId GO EXEC msdb .dbo.sp_add_jobserver @ job_name = N'test2 ', @server_name = N'AMR PC \ SQL2008' Go use [msdb] GO EXEC msdb.dbo.sp_add_jobstep @ job_name = N'test2 ', @ STEP_NAME = N'test' , @ step_id = 1, @ cmdexec_success_code = 0, @o n_success_action = 1, @ on_fail_action = 2, @ retry_attempts = 0, @ retry_interval = 0, @ os_run_priority = 0, @ subsystem = N'TSQL ', @ command = N' EXEC sp_MSforeachdb '' EXEC sp_MSforeachtable @ command1 = '' '' DBCC DBREINDEX ('' '' '' '' '' '' '' ', @replacechar =' '' '' '' ' '' * ', @ Database_Name = N'Client5281', @ output_file_name = N'C: \ documents and Settings \ Amr \ Desktop \ Scheduel report \ report ", @ flags = 2 GO use [msdb] GO msdb.dbo.sp_add_jobschedule @ Job_name = N'test2 announcement @schedule_id integer EXEC ', @ name = N'test', @ enabled = 1, @ freq_ Type = 8, @ freq_subday_type = 1, @ freq_subday_interval = 0, @ freq_relative_interval = 0, @ freq_recurrence_factor = 1, @ active_start_date = 20,100,517, @active_end_date = 99991231, @active_start_time = 0, @ active_end_time = 235,959, @ Schedule_id = @schedule_id output @schedule_id GO selection  

and I am using (use [msdb]) before any block and I get the database name to change this I want to use @ Database_Name = N '** Client5281 **', with the existing database name ([MSDB]).

I hope I explained what I want.

remove all use [msdb] , they do not need the script Completely passes the process name with the name DB and schema name, so there is no need to use before implementing the process. You can just use @ Database_Name = DB_NAME ()

Another solution is to use the SQLCMD variable, which recognizes SSMS:.

 : setVar dbname Client5281 ... use [msdb] GO EXEC msdb.dbo.sp_add_jobstep @ job_name = N'test2 ', @ STEP_NAME = N'test', ... @ Database_Name = N '$ (dbname)', ... go  

Comments