Click here to Skip to main content
15,890,506 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#
using (SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True"))
{
    SqlCommand CmdSql = new SqlCommand("INSERT INTO bookList (ISBN,Book,Price,Link) VALUES (@ISBN,@Book,@Price,@Link) ON DUPLICATE KEY UPDATE Price = Price", conn);
    conn.Open();
    CmdSql.Parameters.AddWithValue("@ISBN", isbnCode.Text);
    CmdSql.Parameters.AddWithValue("@Book", bookEntry.Text);
    CmdSql.Parameters.AddWithValue("@Price", priceConvert);
    CmdSql.Parameters.AddWithValue("@Link", getUrl);
    CmdSql.ExecuteNonQuery();
    conn.Close();
}


The following is the table I have:

http://i622.photobucket.com/albums/tt301/kidown/table.jpg

The program gives me "Incorrect syntax near the keyword 'ON'."

Is it because the data value the same?

basically when the data is the same I want to update the Price (if there is a difference in price).

Link is set as primary key since its the only one in the table that has to be unique.

Thanks
Posted
Updated 7-Jan-14 9:12am
v2

The correct solution in SQL Server is merge[^]. This allows you to check if something exists, and do either an update, or in insert, on that basis.
 
Share this answer
 
Comments
Maciej Los 7-Jan-14 17:11pm    
As far as i know, MS SQL Server 2005 does not support Merge clause.
Christian Graus 7-Jan-14 17:24pm    
Where did he say it was SS2005 ? I can't see that anywhere in his question, is it obvious in the screenshot ?
Maciej Los 7-Jan-14 17:26pm    
Nowhere ;) It's an additional information.
Christian Graus 7-Jan-14 17:27pm    
Fair enough. I assume if people are using such an old version, they should specify.
Maciej Los 7-Jan-14 17:29pm    
Agree!
I think
ON DUPLICATE KEY UPDATE
is only available in MySQL, at the very least it doesn't appear to be available for SQLExpress at this time. So you'll have to do it a different way.
 
Share this answer
 
Comments
slayasty 7-Jan-14 15:11pm    
I see. Any idea on what it might be?
bowlturner 7-Jan-14 16:37pm    
To the best of my knowledge I think you still have to do an update or an insert. You can try an update and if there are zero rows affected you can then do an insert. That is what I tend to do.
slayasty 7-Jan-14 16:42pm    
i see. i will try to elaborate on that!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900