SQL Server : openquery insert linked server

2019-06-04 19:39发布

问题:

How do I insert data into a linked server (oracle) with a condition that a row does not exist?

I want to insert into employee table if employeecode does not exist yet in that table

INSERT INTO OPENQUERY(ORACLEX,
  'SELECT EMPCODE, EMPNAME FROM AX.EMPLOYEE') -- I want a where clause here

Select EID, ENAME FROM EMPDATA

回答1:

You might actually have to read from the table twice

   INSERT INTO OPENQUERY(ORACLEX,
  'SELECT EMPCODE, EMPNAME FROM AX.EMPLOYEE') -- I want a where clause here
   Select D.EID, D.ENAME
     FROM EMPDATA D
LEFT JOIN OPENQUERY(ORACLEX,
  'SELECT EMPCODE, EMPNAME FROM AX.EMPLOYEE') OQ ON OQ.EMPCODE = D.EID
    WHERE QQ.EMPCODE IS NULL;