[英]Sql insert command ambiguity
我正在尝试使用这样的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
您不能将INSERT .... VALUES....
与“内联”选择语句混合使用。 使用VALUES
关键字,必须提供所有值作为文字或SQL变量。
您需要先选择该值,然后将其分配给变量:
DECLARE @ClassSectionID INT
SELECT @ClassSectionID = ClassSectionID
FROM dbo.ClassSectionMaster WHERE ClassId = 1
INSERT INTO ContentToGroup(ContentId,ClassSectionId)
VALUES (16, @ClassSectionID)
或使用SELECT
语句显示其他人提供的所有值(然后省略VALUES
关键字)
INSERT INTO ContentToGroup(ContentId,ClassSectionId) Select 16, ClassSectionId from ClassSectionMaster where ClassId=1
首先,在使用嵌套查询时,您需要删除VALUES。 另外,请尝试在字段名称前指定表名称,以免引起歧义。
INSERT INTO ContentToGroup(ContentId,ClassSectionId)
SELECT 16, ClassSectionMaster.ClassSectionId
FROM ClassSectionMaster
WHERE ClassSectionMaster.ClassId=1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.