使用WHERE条件来插入数值

huangapple go评论66阅读模式
英文:

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
 (&#39;SA&#39;,&#39;560064&#39;),  
WHERE 
 JOBDIVA_ID = &#39;23-00871&#39;;

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

insert into 
 dbo.Repository(STATE_CODE,ZIPCODE)  
values
 (&#39;SA&#39;,&#39;560064&#39;);

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 = &#39;SA&#39;,
 ZIPCODE = &#39;560064&#39;  
WHERE 
 JOBDIVA_ID = &#39;23-00871&#39;;

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
 &lt;other table&gt;  
WHERE 
 JOBDIVA_ID = &#39;23-00871&#39;;

Or maybe even (this makes no sense):

insert into 
 dbo.Repository(STATE_CODE,ZIPCODE)  
SELECT
 &#39;SA&#39; AS STATE_CODE,
 &#39;560064&#39; AS ZIPCODE
FROM
 &lt;other table&gt;  
WHERE 
 JOBDIVA_ID = &#39;23-00871&#39;;

答案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

huangapple
  • 本文由 发表于 2023年3月31日 18:22:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75897422.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定