0

I wrote a query in SQL Server that select from linked table (using OPENQUERY) and inserting data into the local table. I need to insert only the new data that have been added to the linked table.So every time that I run the query doesn’t insert the previous data that have already inserted. I used this query:

 SET IDENTITY_INSERT DocumentCenter.dbo.tbl_Webhook ON;

insert into dbo.tbl_Webhook ( 
id,
event,
ip,
city,
domain,
campaignId,
deviceType,
clientType,
region,
clientName,
userAgent,
clientOS,
country,
messageId,
recipient,
campaignName,
timestamp,
token,
signature,
tag,
url,
mailingList,
my_var_1,
my_var_2,
messageHeaders,
attachmentX,
code,
error,
notification,
reason,
description,
CurrentTimestamp)
SELECT *  FROM OPENQUERY([162.241.181.144], 'SELECT * from tbl_Webhook') 
Where id > (Select max(id) From DocumentCenter.dbo.tbl_Webhook)

the part of SELECT * FROM OPENQUERY([162.241.181.144], 'SELECT * from tbl_Webhook') is working as I can see data but when I run the insert statment it give me (0 row(s) affected) and no error. I have already created same table in my local database. that the name of local database is DocumentCenter and table name is tbl_Webhook.

What did I miss?

I found the answer: I found id is null (Select max(id) From DocumentCenter.dbo.tbl_Webhook) because table is empty as the beginning. So I have added one row and then execute query and it works perfect.

3
  • 2
    does it return any rows when you execute only the select statement without insert statement ??? Commented Dec 14, 2013 at 1:26
  • 1
    I would avoid using select * in your select statements when this becomes production code as any future changes to the remote table schema are likely to cause problems. I would list the column names again. Commented Dec 14, 2013 at 2:08
  • 1
    What does Select max(id) From DocumentCenter.dbo.tbl_Webhook on its own yield? Commented Dec 14, 2013 at 3:25

1 Answer 1

1

ok I found the answer: I found id is null )(Select max(id) From DocumentCenter.dbo.tbl_Webhook) because table is empty as the beginning. So I have added one row and then execute query and it works perfect.

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

Comments

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.