Multi Server Queries - sql

Currently we are having 5 servers and they will be expanded to more servers in coming months. Now we are facing problem of handling them and syncing all servers. So we are using SQL Compare 10. But is there any procedure by which we can connect to these server and update those Stored Procedures at one go.
Suppose we are having 3 servers, with their IPs and Username and Passwords
Now if i change one procedure in one database in one of the server and reflect it to all the db on different server how to accomplish the same.
I was thinking of creating a procedure for which the parameters needs to be passed including the stored procedure name and then in that SP it will connect to databases and either ALTER or DELETE followed by CREATE.

Related

How can i creat a stored procedure on mirroring database using SQL Server?

I have two servers: server1 includes the primary_DB and server2 includes the mirror_DB, I created a database in another server3, then I created two servers linked to the servers 1 and 2.
The objectif is to create a stored procedure to collect data from server 1 or 2 (depending on the active databse) and insert data into server 3 using the likned server.
I wanted create two jobs using two stored procedure, one with linked1 and second with the linked2 in case of server failover, but creation is not allowed when the database acts as a mirror database.
My question is how to retrieve data using a stored procedure from a database that can become a mirror database?
i found the solution, i used Dynamic SQL on creation the stored procedure

Passing params to stored procedure located on another server

I have a stored procedure on tempdb database (under System Databases) on server A. The stored procedure has 3 parameters, Param1, Param2, and Param3 which all accept varchars.
I would like to execute this stored procedure on server B for a database called SomeDB.
With the stored procedure, I'd like to pull data from different tables on SomeDB, and then put the results in a new table that will be created called SomeNewTable, which again will be located on SomeDb
Let's assume that the servers are linked.
How should I approach this?
Write the stored procedure on ServerB . Test the stored proc so that it can run completely on ServerB when you are connected to server B. So everything is happening in Server B.
Now just call the Server B stored proc from Server A.
On Server B exec SomeDb.dbo.myStoredProc(p1,p2,p3)
On Server A exec LinkedServerB.SomeDb.dbo.myStoredProc(p1,p2,p3)
Do not use tempDb for anything - you can not reliably put a stored proc in TempDb*
The fact that it will work in temp db for awhile is misleading.
If you are doing a coding exercise they may have you use tempDb but that is a special situation.
You will have to deal with the security jump from ServerA to ServerB depending on how the linked server was setup.
*(yes I know how to do it permanently - but I doubt it works in azure)

Creating an SQL Server Error Log using sp_readerror from multiple servers SQL Server 2012

I am attempting to create one single database to store all login errors.
insert into [dbo].[SQL_ErrorLog]
exec sp_readerrorlog 0, 1, 'error'
The above code gets me the information that I need for the current long and I understand that changing the 0 to a 1,2....etc will get me the previous days logs.
I have 4 different environments and instead of setting this same job up on each environment, I would like to control it all from 1 single job. I intend to add a field to determine which environment the log information is coming from.
I know that I could also set up staging tables on each environment and then run a select statement to pull in data from each staging table to the final table, however again I am trying to complete all the work from one environment if possible.
I have linked the other environments using the linked servers and can select data from any of them without a problem.
My question is more related on how I can run the exec sp_readerror stored procedure on the other server and insert that data into my master table.
An example would be:
Env0 - This is where the master table would be and where I would like to set everything up
Env1
Env2
Env3
I would like to be able to pull sp_readerror 0, 1, 'error' information from Env1, Env2, and Env3 and populate it on Env0 without using staging tables on each individual environment if possible.
Please let me know if this is not 100% clear. It makes sense in my head, however that does not always come out in text form. :)
Thanks in Advance.
If you are using linked servers it seems like you could link together multiple calls using go from the main source server. This will work assuming your linked servers are linked off one server.
INSERT INTO [Linked Server Name]. [some database name].[dbo].[SQL_ErrorLog]
EXEC [Linked Server Name].[some database name].[dbo].sp_readerrorlog
GO
INSERT INTO [Linked Server Name2]. [some database name].[dbo].[SQL_ErrorLog]
EXEC [Linked Server Name2].[some database name].[dbo].sp_readerrorlog
GO
INSERT INTO [Linked Server Name3]. [some database name].[dbo].[SQL_ErrorLog]
EXEC [Linked Server Name3].[some database name].[dbo].sp_readerrorlog
GO
INSERT INTO [Linked Server Name4]. [some database name].[dbo].[SQL_ErrorLog]
EXEC [Linked Server Name4].[some database name].[dbo].sp_readerrorlog
I think this will be your best bet. You can use the agent and then put all of these into the agent job and run the job. They will need to be fully qualified in order to run on the correct linked server.

Check database / server before executing query

I am frequently testing certain areas on a development server and so running a pre-defined SQL statement to truncate the tables in question before testing again. It would only be a slip of a key to switch to the live server.
I'm looking for an IF statement or similar to prevent that.
Either to check the server name, database name, or even that a certain record in a different table exists before running the query.
Any help appreciated
For such cases I use stored procedures. I'd call them TestTruncateTables, etc.
Then instead of calling TRUNCATE TABLE you should CALL TestTruncateTables.
Just make sure that the procedures are not created on the live server. If by any chance you happen to run CALL TestTruncateTables on the live server you only get an error about non-existing proc.

Running synchronous commands to between two sql servers

I'm running a stored procedure on server1 from my application. The stored procedure does a bunch of stuff and populate a table on server2 with the result from the procedure.
I'm using linked server to accomplish this.
When the stored procedure is done running the application continues and tries to do some manipulation of the result from the stored procedure.
My problem is that the results from the stored procedure has not been completely inserted into the tables yet, so the manipulation of the tables fails.
So my question is. Is it possible to ensure the insert into on the linked server is done synchronous? I would like to have the stored procedure not return until the tables on the linked server actually is done.
You can use an output parameter of the first procedure. When the table is create on the second server the output parameter value will be return to your application and indicates the operation is ready.
If the things are difficult then this you can try setting a different isolation level of your store procedure:
http://msdn.microsoft.com/en-us/library/ms173763.aspx
I found the reason for this strange behavior. There was a line of code in my stored procedure added during debug that did a select on a temporary mem table before the data in the same table was written to the linked server.
When the select statement was run, the control was given back to my application and at the same time the stored procedure continued running. I guess the stored procedure was running synchronously from the start.