December 3, 2013 at 11:26 pm
Hi,
I am pulling records from source table and inserting into target table. this is incremental load. If new records found then i am inserting, if update happens in source then i am updating in target. I am facing issue to maintain delete flag in target. If record is deleted from source then how could i manage in target that this record is deleted from source?
please suggest idea.
Thanks
Abhas.
December 4, 2013 at 12:12 am
If both source and target are on the same server, you could use the TSQL MERGE statement.
Need an answer? No, you need a question
My blog at https://sqlkover.com.
MCSE Business Intelligence - Microsoft Data Platform MVP
December 4, 2013 at 12:16 am
Thanks Koen,
But in the target table there is one extra column ISdelete. If record is deleted from source then i need toset value of this column is D. If i use merge then updating is not allowed under if not match
Thnks
December 4, 2013 at 12:21 am
abhas (12/4/2013)
Thanks Koen,But in the target table there is one extra column ISdelete. If record is deleted from source then i need toset value of this column is D. If i use merge then updating is not allowed under if not match
Thnks
... when not matched by source then
update
set IsDelete = 'D'
...
The absence of evidence is not evidence of absence.
Martin Rees
You can lead a horse to water, but a pencil must be lead.
Stan Laurel
December 4, 2013 at 12:43 am
Thanks Phil,
I am using below query but getting an error
MERGE dbo.tblcustomerSource AS d -- this is source
USING dbo.tblcustomerTarget AS dd --this is target
ON (d.custid = dd.custid )
when not matched by source then
update
set dd.IsDelete = 'D';
error is below
Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "dd.IsDelete" could not be bound.
Can you please help?
Thanks
Abhas.
December 4, 2013 at 12:54 am
You have to switch the source and the target in your query.
Need an answer? No, you need a question
My blog at https://sqlkover.com.
MCSE Business Intelligence - Microsoft Data Platform MVP
December 4, 2013 at 4:19 am
Yes,Thanks. the query is working fine but its taking time to execute a package. After lookup i am using conitionalsplit transformation and comparing sourceid with targetid and fetching records but its not working.
example i am using below expression.
isnull(sourceid) && !isnull(targetid) but its not working.
Thanks for your help.
December 4, 2013 at 5:18 am
abhas (12/4/2013)
Yes,Thanks. the query is working fine but its taking time to execute a package. After lookup i am using conitionalsplit transformation and comparing sourceid with targetid and fetching records but its not working.example i am using below expression.
isnull(sourceid) && !isnull(targetid) but its not working.
Thanks for your help.
Awesome. Not only have you used my personal favourite ("It's not working"), but neither have you asked a question.
The absence of evidence is not evidence of absence.
Martin Rees
You can lead a horse to water, but a pencil must be lead.
Stan Laurel
December 4, 2013 at 5:24 am
Hi Phil,
by comparing source and target hoe to update the target isdelete flag if record is deleted from source?
Thanks
abhas
December 4, 2013 at 5:36 am
can you use this
Update dd
set dd.IsDelete = 'D'
From dbo.tblcustomerTarget dd
Left join dbo.tblcustomerSource d on d.custid = dd.custid
Where d.custid is null
December 4, 2013 at 5:56 am
abhas (12/4/2013)
Hi Phil,by comparing source and target hoe to update the target isdelete flag if record is deleted from source?
Thanks
abhas
You'd already written 99% of the required query:
MERGE dbo.tblcustomerTarget AS dd --this is target
USING dbo.tblcustomerSource AS d -- this is source
ON (d.custid = dd.custid )
when not matched by source then
update
set dd.IsDelete = 'D';
Note that this assumes that dbo.tblcustomerSource contains all of your customer source rows.
The absence of evidence is not evidence of absence.
Martin Rees
You can lead a horse to water, but a pencil must be lead.
Stan Laurel
December 4, 2013 at 7:24 am
Thanks twin ,
I am using the same logic and it working fine. but my source is linked sqlserver. The above query is working fine if source and target is the same server.
could you help me same for linked server?
Thanks
Abhas.
December 4, 2013 at 7:54 am
Add the Destination Server through "sp_addlinkedserver" For more detail refer to the following http://technet.microsoft.com/en-us/library/ms190479.aspx
December 4, 2013 at 8:02 am
Koen Verbeeck (12/4/2013)
If both source and target are on the same server, you could use the TSQL MERGE statement.
Unless the table is absolutely huge, why not just copy the whole table to a staging table on the target and do a cascade-rename to put it into service? Depending on index and FK requirements, it's a bit more complicated but any contention is relegated to the milliseconds it takes for sp_Rename to execute.
--Jeff Moden
Change is inevitable... Change for the better is not.
December 4, 2013 at 8:02 am
Thanks twin,
but again interrupting.
linked server is already defined and i am able to select records using the linked server. Facing issue with merge statement only.
please help.
thank you
Viewing 15 posts - 1 through 15 (of 17 total)
You must be logged in to reply to this topic. Login to reply