I need to execute a script in 2 different servers having different db names but same tables and data. For this, I tried using the following statement.
if (@@servername= 'srvrA')
begin
use dbA
end
else
begin
use dbB
end
But for srvrB, it says Database dbA does not exist.
Could someone help me achieving this?
One more options
Add linked servers sp_addlinkedserver
Mapping between a login local instance and remote server sp_addlinkedsrvlogin
Something like:
sp_addlinkedserver
@server= N'srvrA',
@srvproduct= N'',
@provider= N'SQLNCLI',
@datasrc= N'srvrA';
sp_addlinkedsrvlogin
@rmtsrvname = 'srvrA' ,
@useself = 'FALSE' ,
@locallogin = 'your_local_login' ,
@rmtuser = 'your_remote_login' ,
@rmtpassword = 'your_password'
And then your script will look like
DECLARE @@srvname nvarchar(10) = 'srvrA'
IF (@@servername = @@srvname)
BEGIN
SELECT *
FROM srvrA.dbA.your_schema.your_table
END
ELSE
BEGIN
SELECT *
FROM srvrB.dbB.your_schema.your_table
END
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