I am trying to get data from different sever, and the sever name might change. So I set the server name as a parameter. The idea of my sql store procedure is something like this
CREATE PROCEDURE [dbo].[GetData]
@ServerName
AS
BEGIN
SELECT * FROM @ServerName.ClientDataBase.dbo.Client
END
Does anyone know how to achieve this?
The database version is sql server 2005
Use dynamic SQL, however evil this may be:
exec('select * from ' + @ServerName + '.ClientDatabase.dbo.Client')
Look at using Synonym(s)
Create syntax (from MSDN):
CREATE SYNONYM [ schema_name_1. ] synonym_name FOR < object >
< object > :: =
{
[ server_name.[ database_name ] . [ schema_name_2 ].|
database_name . [ schema_name_2 ].| schema_name_2. ] object_name
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With