If I know the database server name, instance name and the SQL Server job name, how to delete a SQL Server job by its name in a simple way? I am writing scripts which will be called by sqlcmd to delete SQL jobs.
Appreciate if anyone could show me a sample? :-)
thanks in advance,
George
USE msdb;
GO
EXEC sp_delete_job
#job_name = N'NightlyBackups' ;
GO
You're looking for sp_delete_job:
[srv].[master].[dbo].sp_delete_job #job_name = 'MyJob'
So this four part name only works with linked servers. Otherwise, you'll have to connect to the server, and run that command against it (with everything right of [dbo]..
It's worth noting that you can just use SSMS, choose the job, right-click and pick "Delete", and then use the Script button at the top of the dialog box to generate a script like the ones suggested here.
Related
I've got a job in SQL Server Management Studio and I want to back up the schedule that it runs on so that the schedule can be applied to other jobs that I add. I know that I can get what I assume is the data I need to copy from using the following:
-- lists all aspects of the information for the job NightlyBackups.
USE msdb ;
GO
EXEC dbo.sp_help_job
#job_name = N'NightlyBackups',
#job_aspect = N'SCHEDULES' ;
GO
I'm just wondering how I can store the results of this stored procedure in a way that will allow me to add it to other jobs on the system. Preferably in T-SQL .
The GUI method:
Right-click the job in SSMS and script it as CREATE; alter parameters to suit.
The T-SQL method:
I don't have that on-hand, but try opening Profiler, look for SQL:Completed and RPC:Completed, and then do the GUI method - you should capture the T-SQL that SSMS is executing! Alter to suit.
My SQL Server Agent job seems to execute the 1st SP cmd but not the second. Does anybody know how i get multiple commands into a single Server Agent job?
Use the GO command after executing each stored procedure.
Do you reference any FilteredViews in your sproc code? Then check this post and see if you can implement any of these solutions.
If I want to run this sort of query in SQL Server, how can I do the same query from one server I am connected to to another?
I tried adding "[ServerName1]." before "[DatabaseName1].[dbo]..." and "[ServerName2]." before "[DatabaseName2].[dbo]..." but that didn't seem to work.
INSERT INTO [DatabaseName1].[dbo].[TableName]
([FieldName])
SELECT [FieldName] FROM [DatabaseName2].[dbo].[TableName]
Is this possible?
Yes you would use the server-name before the whole rest of object-name like:
myserver.mydatabase.dbo.mytable
However you first have to set up linked servers. Look up linked servers in BOL.
If you have adhoc distributed queries enabled you can use OPENDATASOURCE. Setting up a linked server is another option. Not sure of the pros and cons of each approach.
INSERT INTO [DatabaseName1].[dbo].[TableName]
SELECT FieldName
FROM OPENDATASOURCE('SQLNCLI',
'Data Source=Server\InstanceName;Integrated Security=SSPI')
.DatabaseName2.dbo.TableName
The best way to do this would be to create a "linked server" between the two. You will need appropriate permissions to do this.
Then it's just a matter of accessing the databases using your linkedserver name.
Ex: [linkedserver].databasename.dbo.tablename
To create a linkedserver, go to server objects->right click on linked servers->click on 'new linked server'.
In SSMS, Go to Query -> 'SQLCMD Mode'
DECLARE #VERSION VARCHAR(1000)
:CONNECT Soruce_Server_Name
SELECT ##VERSION AS [SQL_VERSION]
INTO
:CONNECT Destination_Server_Name
[MSSQLTips].[dbo].[TEST]
Now on the Destination Server, execute your select command to check your output. For E.g.
SELECT * FROM [CloverInfotech_DB].[dbo].[TEST]
I need to update the SQL SERVER stored procedure on three different servers. I do not like to perform this manually. What are my options?
You can use the SQLCMD utility to connect to the three different servers / databases and run the stored procedure script. The control script may look something like this:
:connect server1
use DatabaseName
GO
:r StoredProcedure.sql
GO
:connect server2
use DatabaseName
GO
:r StoredProcedure.sql
GO
:connect server3
use DatabaseName
GO
:r StoredProcedure.sql
GO
SQL Compare is a great tool, especially for large or complex updates. However, you do have to pay for it. Using a utility like SQLCMD is not quite so elegant, but it is quick and free.
Use a tool like Red-Gate SQL Compare to create a script and then use their Multi-Script tool to execute it on multiple servers at one time.
www.red-gate.com
You could use a SQL Server synchronization tool, such as Red Gate SQL Compare. Or you could write a small script / application to connect to each server and execute the update statement, using OSQL.
You can set up some replication between the servers...have 1 main server that you make the update on, and then send that update out to each other server by use of a publication to the other servers. That'd be an easy way to do this.
Check out Migrator.NET, this combined with a builder like Hudson that runs on a check-in should do the trick. Plus you get versioning and rollbacks along with it.
With "Central Management Servers" feature of SQL Server 2008, what you can do is to add those three servers into one group and then run a single alter procedure script against these three servers.
I am using SQL Server 2008 and I want to test the execution correctness of my SQL Server Agent job immediately to ignore the schedule. Any ideas?
thanks in advance,
George
In SSMS, under the "SQL Server Agent" node, open the "Jobs" subnode and find your job, right click on it and select "Start Job" - that'll start and run the job right away.
Marc
Create a stored procedure that encapsulates all the aspects of your job and then use that in SQL Agent. You can then just call the procedure from the command line to test it eg. exec dbo.MyProcedure #param1 = 'foo'
Change the schedule to have it run five minutes in the future.
Then get off the server so it runs in exactly the environment you want to test.