SQL Server 2008 R2 - Running a Stored Procedure against Linked Servers - sql-server

I am trying to create a SQL Job that sits on what I like to call a Maintenance Server where details about all the other server databases sit. So I have a stored procedure to gather all the required data I have and this stored procedure is now in MaintenanceServer.MaintenanceDB.pr_MyProcedure.
Now what i'd like to do this is. I am creating Linked Servers on my MaintenanceServer which points to all the other Servers I'd like to maintain. Next on my list is to be able to run this procedure pr_MyProcedure against all those LinkedServers. I have over 25 servers to maintain and what i want to do is to run the procedure against them and insert the result to my local MaintenanceDB.
Can someone point me to a good way to do this? Also i'd like to hear the drawbacks of this method.
Thanks and Cheers!
EDIT:
I was also thinking about storing the SP on all the master databases of my other Servers and running them remotely on my Maintenance Server. Then i'll use OPENQUERY() to gather all the results that I need. However, I don't think I want to touch all the master databases on my server list. Please tell me if this would be better than the once I stated above.

The nice way to do it is run the SP on the target server, as that's nicely modular. If you want to query the remote server, it's going to be more painful. I'm not sure whether the undocumented sp_foreachdb deals with linked databases, if not you are down to dynamic sql.
As in build a string and call it with Exec(#somesql), that will need to deal with the results itself, as any variables will only be in scope inside the exec.
Personally I'd consider inverting all this. Have each server call it's own SP and the call an SP on the maintenance box to store the results.

EXEC [RemoteServer] .DatabaseName.DatabaseOwner.StoredProcedureName
‘Params’
Example:
EXEC [RemoteServer] .DatabaseName.DatabaseOwner.StoredProcedureName ’PramValue’

Related

Update Linked Server Tables Using Synonyms

I have a server in a remote location and Its now connected to my local server through Linked Server. How can I update a table at remote location without using stored procedures.
Would SQL Synonyms be any kind of help?
Does user need to have Write permission on the Remote Server?
Thanks in advance
You would use an update statement. Not sure why the requirement to not use a stored procedure comes from. I am concerned that you believe that somehow a stored procedure would make this easier in some way.
1) Synonyms are not going to help or hinder this process. They are simply a way to shorthand an object name.
2) Of course the user for the linked server connection must have write permission to write data.

Running Stored Procs on Different Databases through Agent Job

I have a list of three stored procedures that need to be ran daily on a number of separate SQL databases. Each database is named differently, but the stored procedures on each are the same.
I have extremely limited knowledge on anything other than basic queries, but was thinking I could have a SQL Server Agent job on a database I set up as Master. Then I have that server push that job to the other databases once I configure those as Targets. My issue is that in thinking through this, the database names are different and within the SQL Server Agent wizard I can only set the database to what's currently on master instance.
What would be the best approach to executing this looping through servers to run the stored procs?
What you're looking for is a linked-server. They allow servers to be linked so that you can call objects from one server to another. They are very easy to setup. In your case, you'll need to create 3 linked-servers on the main server, which will be used to schedule the job. A linked-server will allow the main server to link to listed servers. Here are tutorials of how you can create a linked-server.
Once you've created a linked-server you'll simply create a job that will execute all 3 sprocs, something like this:
EXEC [server1].[database].[schema].[sp_name];
EXEC [server2].[database].[schema].[sp_name];
EXEC [server3].[database].[schema].[sp_name];

Is it possible to run a SQL Server view from a stored procedure?

I have a TON of views that are rather long and dependent/depended on other views. I also have some stored procedures that need to be run weekly and these procedures need to use some of the SQL statements in the views so instead of putting the statements in the procedures I was hoping I could just run the view.
Is that possible? I'll be running these from SQL Server 2008 btw
SQL 101:
A Select statement can be run on a table or view.
So, yes, any code that can access at able can also access a view. Given the necessary permissions.
As I said - this is beginner SQL knowledge. I will add it to my list of interiew questions for junior developers.
A stored procedure is, in essence, nothing more than a canned ad-hoc statement. Yes, it offers many other features but the point is anything you can type in an interactive query window can also be run in a stored procedure.
Views are not restricted to being run in a stored procedure.
Yes you can query tables and views inside your stored procedure.

Function like USE to point to a SQL database on a different server?

In SQL Server, you can apply the use function to point a query to another database. For example:
USE databasename
GO;
Is there a function that allows you to point to a different database server and use a database on that server? I would expect this to work, but no luck:
USE [servername].databasename
GO;
I know I could just point the query to the database on the other server, but when I am dealing with production versus staging environments, it's more efficient to declare the server and database in the beginning of the query.
Thanks
USE does not span across to another server, you need to define a linked server on your local instance and then you can access data from that server.
I use Linked Servers to accomplish this. I don't know if this will meet your needs, however.
http://msdn.microsoft.com/en-us/library/ms188279.aspx
In Management Studio, this is available under Database/Server Objects/Linked Servers.
You can refer to objects on this server like this:
[Server].database.schema.object
I just realized this isn't what you want. JonH has it right, you can't specify a dabase on another server at the beginning of your query.

Migration of the same data in several directions

I need to move data between PROD and DEV SQL Server databases using stored procedure. Obviously I have to use linked servers to perform this task. But I need the same functionality in both directions.
For example something like:
CREATE PROCEDURE dbo.spMoveData(
)
AS
INSERT INTO Target.dbo.TestTable
SELECT * FROM Source.dbo.TestTable
And just pass proper connection strings to linked servers. I think it is better then creation of two identical stored procedures. Is it possible? If not, are there any best practices to do such tasks?
Maybe it makes sense to drop and create required linked servers before procedure execution?

Resources