SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Sql Script Help
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

ahmeds08
Aged Yak Warrior

India
656 Posts

Posted - 04/24/2013 :  02:01:05  Show Profile  Send ahmeds08 a Yahoo! Message  Reply with Quote
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
Flowing Fount of Yak Knowledge

India
2218 Posts

Posted - 04/24/2013 :  02:34:19  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

India
2218 Posts

Posted - 04/24/2013 :  02:40:03  Show Profile  Reply with Quote
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)


--
Chandu
Go to Top of Page

ahmeds08
Aged Yak Warrior

India
656 Posts

Posted - 04/24/2013 :  03:04:55  Show Profile  Send ahmeds08 a Yahoo! Message  Reply with Quote
Thanks Chandu,
I will give it a try.

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

bandi
Flowing Fount of Yak Knowledge

India
2218 Posts

Posted - 04/24/2013 :  03:07:08  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000