0

I have a table11 (Id, status). It's created in a test environment and I have full rights on this table. sometimes I need to update it using the status'es from a different environment - lets call it table2. On this table/server I only have read rights We have the data in different servers which cannot communicate due to security rules...so I have extract data from one environment and store it locally before updating. I have MS SQL server

if I want to update 1 row I'll use

update Table set status = status1 where id = ID1

but I have a LOT of rows :) so far the best I can think of is using excel to generate a long list of update statements like the above one, but isn't there an easier/ more elegant way?

I only have read right on the table/server with the "correct data"

10
  • Is it always the same status for every ID or does the status change for different IDs? Commented Jan 25, 2018 at 8:54
  • 1
    To make things clearer, add some sample table data and the expected result. (As formatted text, not images.) Commented Jan 25, 2018 at 8:56
  • It's different status and different iD's Commented Jan 25, 2018 at 9:23
  • for example: id: 1,2,3,4,5 today may have id's 2,6,7,1,1 and needs to be updated to 1,1,7,7,4 Commented Jan 25, 2018 at 9:24
  • @MrsDahl still unclear example, do you mean that row with id=1 have status=2 today and you want to update status to 1? and id=2 to status 6->1 and so on? Commented Jan 25, 2018 at 10:01

1 Answer 1

1

You can do what you suggesting without using Excel, here's a SQL Server example where @tbl1 is on server 1 and @tbl2 is on server 2:

DECLARE @tbl1 TABLE ( id INT, [status] NVARCHAR(10) )
DECLARE @tbl2 TABLE ( id INT, [status] NVARCHAR(10) )

-- @tbl1 data is different and needs to be sync'd to @tbl2
INSERT INTO @tbl1 ( id , [status] ) VALUES ( 1, 'd' ), ( 2, 'e' ), ( 3, 'f' )
INSERT INTO @tbl2 ( id , [status] ) VALUES ( 1, 'a' ), ( 2, 'b' ), ( 3, 'c' )

-- show current data
SELECT * FROM @tbl1 AS t
SELECT * FROM @tbl2 AS t

-- run a command like this on @tbl1 to generate UPDATE statements
SELECT 'UPDATE @tbl2 SET [status] = ''' + [status] + 
       ''' WHERE id = ' + CAST(t.id AS NVARCHAR(5))
FROM @tbl1 AS t

-- the above will generate this sql, which you can copy across and run on server 2
UPDATE @tbl2 SET [status] = 'd' WHERE id = 1
UPDATE @tbl2 SET [status] = 'e' WHERE id = 2
UPDATE @tbl2 SET [status] = 'f' WHERE id = 3

-- running the generated SQL on server 2 would then give you matching results
SELECT * FROM @tbl1 AS t
SELECT * FROM @tbl2 AS t

Instead of the standard Results to grid output, you can either display results as text or export to a file, to make copying/running the generated SQL easier, and possibly creating the first step of an automated process.

You'll find Query Result output options via the highlighted shortcuts below of via Tools > Options in the menu:

enter image description here

Summary

The actual SQL you will want to run on your source server is something like this:

-- run a command like this on @tbl1 to generate UPDATE statements
SELECT 'UPDATE @tbl2 SET [status] = ''' + [status] + 
       ''' WHERE id = ' + CAST(t.id AS NVARCHAR(5))
FROM @tbl1 AS t

Then you would take the results away to run on the target server.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks - but I see I forgot to add that I only have read rights on table2/server2
then how do you perform an update query if you only have read rights? please add that to the question so that other users can see this information, although make it clear what permissions you have, as with read rights, you can't update?!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.