Question : INSERT INTO SELECT FROM OPENQUERY question

Hi,

We would like to use the regular       :-

insert into MyTable(FirstName, LastName, Salary)
select FirstName, SecondName, Salary
from OPENQUERY(LEGACY_DATABASE,'select FirstName, SecondName, Salary from Legacy_Table')

command which works if there is nothing in the target "MyTable" - so the question is how do we use this in conjuction with IF EXIST to test if the record already exists and perhaps do an UPDATE instead of an INSERT.

Thanks.

James.

Answer : INSERT INTO SELECT FROM OPENQUERY question

Try it this way:

-- Update all the common rows.
Update      MyTable
Set      FirstName = l.FirstName,
      LastName = l.LastName
      Salary = l.Salary
From      MyTable
      Inner Join OPENQUERY(LEGACY_DATABASE,'select FirstName, SecondName, Salary from Legacy_Table') l On MyTable.Here> = l.

-- Add all the missing rows.
Insert      MyTable(FirstName, LastName, Salary)
Select      FirstName, SecondName, Salary
From      OPENQUERY(LEGACY_DATABASE,'select FirstName, SecondName, Salary from Legacy_Table') l
Where      NOT EXISTS (Select 1 FROM MyTable Where MyTable.Here> = l.)

       

Random Solutions  
 
programming4us programming4us