英文:
Using where condition to insert value
问题
插入数据到具体行的SQL命令,根据给定的列名和数值条件。
英文:
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.
答案1
得分: 1
在T-SQL中,这里有一些问题...
正确的INSERT命令应该是(假设数据类型正确):
insert into
dbo.Repository(STATE_CODE,ZIPCODE)
values
('SA','560064');
主要问题是逗号后面跟着WHERE子句。
如果JOBDIVA_ID列是dbo.Repository表的属性,那么您似乎想要一个UPDATE命令,如下所示:
UPDATE
dbo.Repository
SET
STATE_CODE = 'SA',
ZIPCODE = '560064'
WHERE
JOBDIVA_ID = '23-00871';
如果JOBDIVA_ID是不同表的属性,那么您可能正在寻找:
insert into
dbo.Repository(STATE_CODE,ZIPCODE)
SELECT
STATE_CODE,ZIPCODE
FROM
<other table>
WHERE
JOBDIVA_ID = '23-00871';
或者甚至可能是(这没有意义):
insert into
dbo.Repository(STATE_CODE,ZIPCODE)
SELECT
'SA' AS STATE_CODE,
'560064' AS ZIPCODE
FROM
<other table>
WHERE
JOBDIVA_ID = '23-00871';
以上是修复了问题的SQL命令。
英文:
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';
答案2
得分: -1
在插入语句中,你没有现有的行可以使用where子句。
根据数据条件,你必须使用更新语句。
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论