Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 INSERT INTO

Author  Topic 

VeryNew2SQL
Starting Member

2 Posts

Posted - 2015-01-14 : 21:30:56
I am trying to come up with one statement that will INSERT a row if the primary key does not exist but APPEND if it does

Would the following statement be correct?

INSERT INTO Products (a,b,c) VALUES (2, Cotton Socks, $12) ON DUPLICATE KEY (b=Cotton Socks), (C=$12)

where a is the primary key?

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2015-01-15 : 01:03:23
thats not valid sql server syntax. There is no on duplicate key clause in SQL.
the INSERT statement does both insert and append.
Go to Top of Page

VeryNew2SQL
Starting Member

2 Posts

Posted - 2015-01-15 : 02:11:19
quote:
Originally posted by gbritton

thats not valid sql server syntax. There is no on duplicate key clause in SQL.
the INSERT statement does both insert and append.



Thanks. I am a bit confused. You are saying if a primary key value is repeated it will append the rest of columns and not create a duplicate key?

You are also saying there is no option for the ON DUPLICATE Clause but the MySQL docs show it

http://dev.mysql.com/doc/refman/5.0/en/insert.html
Go to Top of Page

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2015-01-15 : 07:01:00
you're asking a question about mysql? This a SQL server forum.
for what it's worth append makes no sense in a relational database. Tables are set and sets have no sequence. Append implies add to the end but since there is no sequence there is no end.new rows would be added in order of the primary key if there is a clustered index on the PK. Otherwise they will simply be added wherever they fit.
Go to Top of Page
   

- Advertisement -