Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL query: Incorrect Syntax error

Tags:

syntax

c#

sql

I'm getting an error

incorrect syntax near the keyword WHERE

with the following SQL statement:

SqlCommand scInsertCostSpilt = new SqlCommand("INSERT INTO [ASSETS_CC] ([DEPT], [CC], [PER_CENT]) WHERE [ASSET_NO] = @AssetNumber)" +
"Values (@AssetNumber, @Dept, @CC, @PerCent)" , DataAccess.AssetConnection);

What's wrong?

like image 412
CHRISTOPHER MCCONVILLE Avatar asked Jul 13 '12 11:07

CHRISTOPHER MCCONVILLE


2 Answers

In SQL insert statements do not have a WHERE clause (which makes sense, because the record is not there yet). You put the IDs together with all other values if you would like to insert a new record, or use an UPDATE statement if you would like to change an existing record.

INSERT INTO [ASSETS_CC] ([ASSET_NO], [DEPT], [CC], [PER_CENT]) 
VALUES (@AssetNumber, @Dept, @CC, @PerCent)

or

UPDATE [ASSETS_CC]
SET [DEPT] =  @Dept, [CC] = @CC, [PER_CENT] = @PerCent
WHERE [ASSET_NO] = @AssetNumber
like image 96
Sergey Kalinichenko Avatar answered Sep 21 '22 20:09

Sergey Kalinichenko


I think you have written wrong query. Update as below given query:

INSERT INTO [ASSETS_CC] ([DEPT], [CC], [PER_CENT]) Values ( @Dept, @CC, @PerCent) 
like image 36
Hiren Visavadiya Avatar answered Sep 22 '22 20:09

Hiren Visavadiya