SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


MERGE with ANSI


MERGE with ANSI

Author
Message
Koen Verbeeck
Koen Verbeeck
SSC Guru
SSC Guru (145K reputation)SSC Guru (145K reputation)SSC Guru (145K reputation)SSC Guru (145K reputation)SSC Guru (145K reputation)SSC Guru (145K reputation)SSC Guru (145K reputation)SSC Guru (145K reputation)

Group: General Forum Members
Points: 145032 Visits: 13349
Tough one, thanks.


How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?
My blog at SQLKover.

MCSE Business Intelligence - Microsoft Data Platform MVP
Anipaul
Anipaul
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14547 Visits: 1407
A very tough question....



Revenant
Revenant
SSC-Insane
SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)

Group: General Forum Members
Points: 22356 Visits: 5132
I have unbfair advantage: MERGE is my daily bread. ;-)

Thanks for this interesting question!
SQLRNNR
SQLRNNR
SSC Guru
SSC Guru (143K reputation)SSC Guru (143K reputation)SSC Guru (143K reputation)SSC Guru (143K reputation)SSC Guru (143K reputation)SSC Guru (143K reputation)SSC Guru (143K reputation)SSC Guru (143K reputation)

Group: General Forum Members
Points: 143713 Visits: 18651
Thanks Hugo

Jason...AKA CirqueDeSQLeil
_______________________________________________
I have given a name to my pain...MCM SQL Server, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw
Learn Extended Events

OzYbOi d(-_-)b
OzYbOi d(-_-)b
SSCrazy
SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)

Group: General Forum Members
Points: 2370 Visits: 778
thanks Hugo!
Britt Cluff
Britt Cluff
Hall of Fame
Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)

Group: General Forum Members
Points: 3291 Visits: 253
Challenging question. Thanks for submitting.

http://brittcluff.blogspot.com/
sbamaty
sbamaty
SSC Eights!
SSC Eights! (826 reputation)SSC Eights! (826 reputation)SSC Eights! (826 reputation)SSC Eights! (826 reputation)SSC Eights! (826 reputation)SSC Eights! (826 reputation)SSC Eights! (826 reputation)SSC Eights! (826 reputation)

Group: General Forum Members
Points: 826 Visits: 198
My brain almost exploded..lol!! Pretty hard question. I actually have to open my book and read more about MERGE statement. I was under impression that this MERGE functionality came in SQL2008 only since I start using it in SQL2008 only. I didn't get lots of opportunities to play with newer functionality in 2005.

One thing I would like to point out is that MERGE statement does allows BY SOURCE and BY TARGET in the statement. Here's the snippet of code where I use both:

MERGE dbo.column_collection AS cc
USING dbo.staging_column_collection AS scc
ON (
scc.table_name = cc.table_name
AND scc.database_name = cc.database_name
AND scc.ordinal_position = cc.ordinal_position
AND scc.record_status = cc.record_status
AND scc.schema_name = cc.schema_name
AND cc.record_status = 'A'
AND scc.platform_type_code = cc.platform_type_code
AND scc.database_name NOT LIKE '_broken%'
)
WHEN MATCHED AND cc.physical_column_name <> scc.physical_column_name
OR cc.data_type <> scc.data_type
OR cc.column_length <> scc.column_length
THEN UPDATE
SET cc.physical_column_name = scc.physical_column_name
, cc.column_name = scc.column_name
, cc.data_type = scc.data_type
, cc.column_length = scc.column_length

WHEN NOT MATCHED BY TARGET THEN
INSERT
(
column_name
, physical_column_name
, data_type
, column_length
, column_definition
, record_status, insert_date
, table_name, database_name
, ordinal_position
, schema_name
, platform_type_code
)
VALUES
(
scc.column_name
, scc.physical_column_name
, scc.data_type
, scc.column_length
, scc.column_definition
, scc.record_status
, scc.insert_date
, scc.table_name
, scc.database_name
, scc.ordinal_position
, schema_name
, platform_type_code
)

WHEN NOT MATCHED BY SOURCE
THEN UPDATE
SET cc.record_status = 'D'
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search