Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically Specify Linked Server and DB Names in Stored Procedure

I have a query in a stored procedure that needs to be executed on different servers and databases according to some parameters.

How can I do this without using neither exec, nor sp_executesql?

I'm using SQL Server 2008.

Thank you.

UPDATE

I've found some links

http://www.eggheadcafe.com/software/aspnet/29397800/dynamically-specify-serve.aspx

http://www.sommarskog.se/dynamic_sql.html

Is using SYNONYM possible solution? If yes, than how?

UPDATE 2

I forgot to mention that all this servers are linked to the server where stored procedure is stored.

UPDATE 3

OPENROWSET or OPENDATASOURCE are not accessible either. I need a solution without building query string concating server name, schema name, db name.

It surely can be done by using if or case in stored procedure, but if we have 37 variations, then it's not a good solution.

Any other suggestions?

like image 629
hgulyan Avatar asked Dec 01 '25 04:12

hgulyan


1 Answers

Nobody wants to answer, so I'll do it myself, just to have accepted answer.

There's isn't any way to do this. You need to use one of specified suggestions, anyway the query must be generating by concatenating.

like image 80
hgulyan Avatar answered Dec 02 '25 19:12

hgulyan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!