sql - Creating a table and populating with a merge -


i have sql 2008 r2 table consists of (col1(char), col2(int, prim key), col3(char)).

i populating table with...

merge [dbo].[data] target using  ( select 01, 1, 'data1' union  select 03, 2, 'data2' union  select d2, 3, 'data3' )as source ([col1], [col2], [col3]) on (target.[col2] = source.[col2]) when matched      update set [col1] = source.[col1],                [col3] = source.[col3] when not matched        insert ([col1], [col2], [col3])     values (source.[col1],source.[col2], source.[col3]); 

it populates fine, aside "select d2, 3, 'data3'" statement, , throws invalid column name. ideas why or how fix it?

thanks

you need add little char col1: '

merge [dbo].[data] target using  ( select '01' col1, 1 col2, 'data1' col3 union  select '03', 2, 'data2' union  select 'd2', 3, 'data3' )as source ([col1], [col2], [col3]) on (target.[col2] = source.[col2]) when matched      update set [col1] = source.[col1],                [col3] = source.[col3] when not matched        insert ([col1], [col2], [col3])     values (source.[col1],source.[col2], source.[col3]); 

Comments

Popular posts from this blog

delphi - How to convert bitmaps to video? -

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -