0

我有一个INSERT查询来插入多条记录并在插入时返回一个集合CompanyID,并且该查询工作正常。我想将返回的那些存储CompanyIDs到一个表变量中,这样我就可以在之后使用那些运行另一个查询CompanyIDs. 我试过了,但卡住了。

请帮忙。

-- Create a table variable
DECLARE @CompanyIDs TABLE
(
    CompanyID INT NOT NULL
)

INSERT INTO @CompanyIDs  -- STUCK right here and it failed

-- This INSERT query is working fine, and will return the CompanyIDs that inserted.
INSERT INTO [Apps].[dbo].[ERP_Company]
(
   [Name]
   ,[Type]
   ,[ImportFrom]
   ,[InActiveFlag]
   ,[CreatedDate]
   ,[CreatedBy]
   ,[CompanyOwnerID]
   ,[ModifiedDate]
   ,[HQAddress]
   ,[HQCity]
   ,[HQState]
   ,[HQZip]
   ,[Type2]
   ,[Segment]
   ,[Industry]   
)
    output inserted.CompanyID
    SELECT DISTINCT(Company) AS Company
    , 'End-User'
    , 'MarketingUpload'
    , '0'
    , GETDATE()
    , '1581'
    , '1581'
    , GETDATE()
    , [Address]
    , City
    , [State]
    , ZipCode
    , 'Customer'
    , Segment
    , Industry
    FROM dbo.Import_CompanyContact icc 
    WHERE RefNum = 14
    AND MatchFlag = 3
    AND NOT Exists (SELECT * FROM dbo.ERP_Company
    WHERE REPLACE(Name, '''', '') = REPLACE(icc.Company, '''', '') 
    )
4

1 回答 1

0
OUTPUT inserted.CompanyID INTO @YourTable
于 2013-10-03T17:04:09.633 回答