使用单个insert语句插入多行

时间:2017-11-24 17:25:09

标签: sql insert

我正在尝试使用以下查询插入表变量。    但它抛出一个错误。    请使用单个插入语句帮助插入多个选择。

      DECLARE @AddressRecordsToPurge TABLE  
    (  
      RowID INT NOT NULL PRIMARY KEY IDENTITY(1,1),  
        GUIDValue Nvarchar(max) ,  
     GuidColumn Nvarchar(max) ,  
      GuidTable Nvarchar(max)  
     )    

    Insert Into @AddressRecordsToPurge values ( (Select            
    EMPLOYMENTSEQUENCENUMBER FROM ACCOUNTANTSREFERENCE  WHERE  
    CustomerNumber =  @CustomerNumber AND Customerversionnumber =  
    @CustomerVersionNumber AND EMPLOYMENTSEQUENCENUMBER IS NOT 
     NULL), 'EMPLOYMENTSEQUENC ENUMBER', 'ACCOUNTANTSREFERENCE'); 

我的select语句返回多个值,我想只以这种方式使用它。请帮忙!

1 个答案:

答案 0 :(得分:4)

您的语法略有偏差:

Insert Into @AddressRecordsToPurge (GuidValue, GuidColumn, GuidTable)
SELECT EMPLOYMENTSEQUENCENUMBER, 'EMPLOYMENTSEQUENCENUMBER', 'ACCOUNTANTSREFERENCE'
FROM ACCOUNTANTSREFERENCE
WHERE CustomerNumber =  @CustomerNumber
    AND Customerversionnumber = @CustomerVersionNumber
    AND EMPLOYMENTSEQUENCENUMBER IS NOT NULL;