SQL Server Using where condition to insert value

bvhaajcl  于 2023-04-04  发布在  其他
关注(0)|答案(2)|浏览(125)
insert into dbo.Repository(STATE_CODE,ZIPCODE) 
values('SA','560064'), 
WHERE JOBDIVA_ID = '23-00871';

output :

Msg 156, Level 15, State 1, Line 3
Incorrect syntax near the keyword 'WHERE'.

SQL command for inserting data into specific row with given column name value condition.

guykilcj

guykilcj1#

Assuming T-SQL, quite a few things wrong here...

insert into 
 dbo.Repository(STATE_CODE,ZIPCODE)  
values
 ('SA','560064'),  
WHERE 
 JOBDIVA_ID = '23-00871';

The correct INSERT command would be (assuming the datatypes are correct):

insert into 
 dbo.Repository(STATE_CODE,ZIPCODE)  
values
 ('SA','560064');

The main issue is the comma followed by the WHERE clause.

If the JOBDIVA_ID column is an attribute of the dbo.Repository table, then it looks like you are wanting an UPDATE command which would be:

UPDATE 
 dbo.Repository
SET
 STATE_CODE = 'SA',
 ZIPCODE = '560064'  
WHERE 
 JOBDIVA_ID = '23-00871';

If the JOBDIVA_ID was an attribute of a different table then you might be looking for:

insert into 
 dbo.Repository(STATE_CODE,ZIPCODE)  
SELECT
 STATE_CODE,ZIPCODE
FROM
 <other table>  
WHERE 
 JOBDIVA_ID = '23-00871';

Or maybe even (this makes no sense):

insert into 
 dbo.Repository(STATE_CODE,ZIPCODE)  
SELECT
 'SA' AS STATE_CODE,
 '560064' AS ZIPCODE
FROM
 <other table>  
WHERE 
 JOBDIVA_ID = '23-00871';
kuuvgm7e

kuuvgm7e2#

In an insert statement you wouldn't have an existing row to do a where claues on. You must use update statement depending on the data conditions

相关问题