简体   繁体   English

SQL插入命令歧义

[英]Sql insert command ambiguity

I'm trying to use a sql command like this but it's not working for some obvious reasons.Can you tell me what's the way out for this type of commands ? 我正在尝试使用这样的sql命令,但是由于某些明显的原因它无法正常工作。您能告诉我这种命令的出路是什么吗?

   INSERT INTO ContentToGroup(ContentId,ClassSectionId)  
   VALUES (16, Select ClassSectionId from ClassSectionMaster where ClassId=1)
INSERT INTO ContentToGroup (ContentId,ClassSectionId)
Select 16, ClassSectionId
from ClassSectionMaster
where classid = 1

You cannot mix the INSERT .... VALUES.... with "inline" select statements. 您不能将INSERT .... VALUES....与“内联”选择语句混合使用。 Using the VALUES keyword, you must provide all values as literals or SQL variables. 使用VALUES关键字,必须提供所有值作为文字或SQL变量。

Either you need to select the value first and assign it to a variable: 您需要先选择该值,然后将其分配给变量:

DECLARE @ClassSectionID INT

SELECT @ClassSectionID = ClassSectionID 
FROM dbo.ClassSectionMaster WHERE ClassId = 1

INSERT INTO ContentToGroup(ContentId,ClassSectionId)  
VALUES (16, @ClassSectionID)

or then use the SELECT statement others have shown to provide all values (and then you omit the VALUES keyword) 或使用SELECT语句显示其他人提供的所有值(然后省略VALUES关键字)

INSERT INTO ContentToGroup(ContentId,ClassSectionId) Select 16, ClassSectionId from ClassSectionMaster where ClassId=1

First you need to remove the VALUES when you are using an nested query. 首先,在使用嵌套查询时,您需要删除VALUES。 Also try specifying the table names in front of the field name in case that is causing your ambiguity. 另外,请尝试在字段名称前指定表名称,以免引起歧义。

INSERT INTO ContentToGroup(ContentId,ClassSectionId) 
    SELECT 16, ClassSectionMaster.ClassSectionId 
    FROM ClassSectionMaster 
    WHERE ClassSectionMaster.ClassId=1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM