0

I am trying to update a table from another database using joins and having a hard time. This is what I am trying to do in pseudo:

UPDATE [Database1].[dbo].[Sessions]
   SET [SpeakerID] = ?STATEMENT1?
 WHERE ?STATEMENT2?

For "Statement1", this would be coming from another database and table that has columns: SessionID and SpeakerID. How can this be achieved?

TruMan1
  • 33,665
  • 59
  • 184
  • 335

3 Answers3

2
UPDATE  a
SET     a.SpeakerID = b.colName          -- SET valoue here
FROM    Database1.dbo.Sessions a
        INNER JOIN Database2.dbo.Sessions b
            ON a.SessionID = b.SessionID -- assumes that their 
                                         -- relationship column is SessionID, 
                                         -- change it in your original columnName
WHERE   ....

a and b are called alias. They are useful when you have longer source name.

John Woo
  • 258,903
  • 69
  • 498
  • 492
0
UPDATE L
  SET SpeakerID = R.SpeakerID
  FROM dbo.LocalTable AS L
  INNER JOIN RemoteDatabase.dbo.RemoteTable AS R
  ON L.SomeValue = R.SomeValue;

This really is no different from this problem except you have to add a database prefix to one of the tables in the join.

Community
  • 1
  • 1
Aaron Bertrand
  • 272,866
  • 37
  • 466
  • 490
0

try

UPDATE [Database1].[dbo].[Sessions]
SET
    Sessions.col1 = other_table.col1
FROM
    [Database1].[dbo].[Sessions] Sessions
INNER JOIN
    [Database2].[dbo].other_table AS other_table
ON
    Sessions.id = other_table.id
WHERE Sessions.id = ??

Note if the database is on another server you will need to create a linked server first

http://msdn.microsoft.com/en-us/library/ff772782.aspx

john
  • 581
  • 4
  • 9