简体   繁体   English

从SQL Server中的选择查询错误插入

[英]Insert into from select query error in SQL Server

I am using the below query to insert data from one table to another: 我正在使用以下查询将数据从一个表插入到另一个表:

DECLARE @MATNO NVARCHAR(10), @GLOBALREV INT, @LOCALREP INT

SET @MATNO = '7AGME'
SET @GLOBALREV = 11
SET @LOCALREP = 1

INSERT INTO CIGARETTE_HEADER 
VALUES 
    (SELECT * 
     FROM CIGARETTE_HEADER_BK1 
     WHERE MATERIAL_NUMBER = @MATNO 
       AND GLOBAL_REVISION = @GLOBALREV 
       AND LOCAL_REVISION = @LOCALREP)

The column in both the tables are same, but I am getting the following error: 两个表中的列均相同,但出现以下错误:

Msg 156, Level 15, State 1, Line 7 消息156,第15层,州1,第7行
Incorrect syntax near the keyword 'SELECT'. 关键字“ SELECT”附近的语法不正确。

Msg 102, Level 15, State 1, Line 7 Msg 102,第15级,状态1,第7行
Incorrect syntax near ')'. ')'附近的语法不正确。

Can you please let me know the mistake here? 您能告诉我这里的错误吗?

You don't need VALUES keyword: 您不需要VALUES关键字:

INSERT INTO CIGARETTE_HEADER  
SELECT * FROM CIGARETTE_HEADER_BK1 
WHERE MATERIAL_NUMBER = @MATNO AND 
      GLOBAL_REVISION = @GLOBALREV AND 
      LOCAL_REVISION = @LOCALREP

It is also preferable to explicitly cite every field name of both tables participating in the INSERT statement. 还最好显式地引用参与INSERT语句的两个表的每个字段名称。

You don't need to use the VALUES () notation. 您不需要使用VALUES()表示法。 You only use this when you want to insert static values, and only one register. 仅在要插入静态值和仅一个寄存器时才使用此选项。 Example: INSERT INTO Table VALUES('value1',12, newid()); 示例:INSERT INTO Table VALUES('value1',12,newid());

Also i recommend writing the name of the columns you plan to insert into, like this: 另外,我建议编写您打算插入的列的名称,如下所示:

INSERT INTO Table
(String1, Number1, id)
VALUES('value1',12, newid());

In your case, do the same but only with the select: 对于您的情况,请执行以下操作,但只能使用select:

DECLARE @MATNO NVARCHAR(10), @GLOBALREV INT, @LOCALREP INT;

SET @MATNO = '7AGME';
SET @GLOBALREV = 11;
SET @LOCALREP = 1;

INSERT INTO CIGARETTE_HEADER
(ColumnName1, ColumnName2)
SELECT ColumnNameInTable1, ColumnNameInTable2
FROM CIGARETTE_HEADER_BK1 
WHERE MATERIAL_NUMBER = @MATNO 
AND GLOBAL_REVISION = @GLOBALREV 
AND LOCAL_REVISION = @LOCALREP);

To expand a bit on the issue. 在这个问题上扩大一点。

VALUES() is actually a table constructor. VALUES()实际上是一个表构造函数。 When inserting rows into a table you can either use SELECT to fetch rows or VALUE constructor. 将行插入表中时,可以使用SELECT来获取行或使用VALUE构造函数。

Even though INSERT INTO... VALUES is often used in examples to insert a row, you can use it to insert multiple rows, separating them with commas. 即使在示例中经常使用INSERT INTO ... VALUES插入行,也可以使用它插入多行,并用逗号分隔。

Eg. 例如。 INSERT INTO table(col1, col2) VALUES (1, 2) , (3, 4) , (5, 6) 插入表(col1,col2)值(1,2),(3,4),(5,6)

Would insert a set of 3 rows into your table. 将一组3行插入到表中。

VALUES can also be used to make derived tables, allowing some advanced data manipulation. VALUES还可以用于生成派生表,从而允许进行一些高级数据操作。

https://msdn.microsoft.com/en-us/library/dd776382.aspx https://msdn.microsoft.com/zh-CN/library/dd776382.aspx

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

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