簡體   English   中英

SQL插入命令歧義

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM