Monday, 3 March 2014

SQL to reference different servers

I hit an requirement where I had to update the one of the field in the table for about couple of hundred records. I had the excel file which listed the records that needs updating.

We are not allowed to create temporary database/tables in the LI VE server, then I had two options either to read the excel file in SQL or create the temp database in dev envrnoment for the records that needs to be updated in live. I opted to go with the second option of creating the temp database with the records to update in live.

I used the import export option available in the SQL management studio to import all the records from excel to local database table.  and then used something similar to following logic

In environment where I have acess to LIVE I created a link to dev server to refer the table

To create a link server.

Exec sp_addlinkserver 'TempDevBox', '<space>', 'SQLNCLI', '<servername>', '<database name>'

--just to check if following select works fine
select * from TempDevBox.<database name>.dbo.Tablename


-- To update records in live table where record detail matched the dev table records.

Update livetable
set field = 1
where field = 0 and
recordnumber in
(
select recordnumber from TempDevBox.<database name>.dbo.Tablename
)
 

No comments:

Post a Comment