Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server Sql插入命令歧义_Sql Server_Insert_Select Insert - Fatal编程技术网

Sql server Sql插入命令歧义

Sql server Sql插入命令歧义,sql-server,insert,select-insert,Sql Server,Insert,Select Insert,我试着使用像这样的sql命令,但是由于一些明显的原因它不起作用。你能告诉我这种命令的解决方法吗 INSERT INTO ContentToGroup(ContentId,ClassSectionId) VALUES (16, Select ClassSectionId from ClassSectionMaster where ClassId=1) 您不能混合使用INSERT。。。。值…带有“内联”select语句。使用VALUES关键字,必须将所有值作为文本或SQL变量提供

我试着使用像这样的sql命令,但是由于一些明显的原因它不起作用。你能告诉我这种命令的解决方法吗

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

您不能混合使用
INSERT。。。。值…
带有“内联”select语句。使用
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, ClassSectionMaster.ClassSectionId 
    FROM ClassSectionMaster 
    WHERE ClassSectionMaster.ClassId=1
我认为应该在某处显示“x人正在回答”。因为我们都发布了几乎相同的答案
INSERT INTO ContentToGroup(ContentId,ClassSectionId) Select 16, ClassSectionId from ClassSectionMaster where ClassId=1
INSERT INTO ContentToGroup(ContentId,ClassSectionId) 
    SELECT 16, ClassSectionMaster.ClassSectionId 
    FROM ClassSectionMaster 
    WHERE ClassSectionMaster.ClassId=1