Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Sql Script Help

Author  Topic 

ahmeds08
Aged Yak Warrior

737 Posts

Posted - 2013-04-24 : 02:01:05
Dear Experts,
Need your help in writing some script.

I need to find a record in every table of one databases on each server.
for eg:
I have a table called 'Table1' in the database called 'Database1' on Server called 'Server1'

In the same way I have around 200 servers which have same table name and database name.

I am trying to insert a new record in these tables on each server,so before that i want to check if the same account number already exists in the table on each server.
The table has Account Number column which should be unique.

Thanks,
Javeed.

mohammad.javeed.ahmed@gmail.com

bandi
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2013-04-24 : 02:34:19
create one table for storing all linked server names...
use SP_LINKEDSERVERS procedure to return all linked server names in the instance
then loop through the script by passing ServerName

--
Chandu
Go to Top of Page

bandi
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2013-04-24 : 02:40:03
[code]DECLARE @sql VARCHAR(MAX) =
'IF NOT EXISTS (SELECT 1 FROM ' + @ServerName + '.Database1..Table1 WHERE AccountNumber = ' + @InputForAccNum + ' )
INSERT INTO ' + @ServerName + '.Database1..Table1 VALUES( ' + @InputValue + ' );'

EXEC (@sql)[/code]

--
Chandu
Go to Top of Page

ahmeds08
Aged Yak Warrior

737 Posts

Posted - 2013-04-24 : 03:04:55
Thanks Chandu,
I will give it a try.

mohammad.javeed.ahmed@gmail.com
Go to Top of Page

bandi
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2013-04-24 : 03:07:08
quote:
Originally posted by ahmeds08

Thanks Chandu,
I will give it a try.

mohammad.javeed.ahmed@gmail.com


ok. Let us know the full version of script

--
Chandu
Go to Top of Page
   

- Advertisement -