小编典典

如何从插入的表中获取所有ID

sql

我正在插入大量记录,如何从插入的表中获取所有这些ID。

我想将所有这些ID用作伪造密钥并插入到另一个表中

INSERT INTO [dbo].[BudCustomers]
           ([LegalName]
           ,[EffectiveDate]
           ,[LawsonCustomerNumber]
           ,[ChangeReason]
           ,[ImportedRecord]
           ,[VersionID]
           ,[StatusID]
           ,[CreatedDate]
           ,[CreatedUserID]
           ,[LastModifiedDate]
           ,[LastModifiedUserID]
           ,[CustomerGroupID])
SELECT CustomerName
         ,'1900-01-01 00:00:00.000'
         , CASE WHEN PATINDEX('%[0-9]%', CustomerName) > 0 
            THEN REPLACE(SUBSTRING(CustomerName, PATINDEX('%[0-9]%', CustomerName),
            LEN(CustomerName)), ')', '') 
            ELSE 0 END
         ,''
         ,1
         ,1
         ,1
         ,GETDATE()
         ,'Import'
         ,GETDATE()
         ,'Import'
         ,NULL
FROM External_Blk_Itm_Contracts
WHERE TerminalName NOT IN (SELECT MBFTERMINALNAME FROM budterminals)

阅读 151

收藏
2021-04-15

共1个答案

小编典典

使用 OUTPUT 条款:

CREATE TABLE #temp (CustomerId <datatype> );

INSERT INTO [dbo].[BudCustomers]
           ([LegalName]
           ,[EffectiveDate]
           ,[LawsonCustomerNumber]
           ,[ChangeReason]
           ,[ImportedRecord]
           ,[VersionID]
           ,[StatusID]
           ,[CreatedDate]
           ,[CreatedUserID]
           ,[LastModifiedDate]
           ,[LastModifiedUserID]
           ,[CustomerGroupID])
OUTPUT inserted.CustomerId
INTO #temp
SELECT CustomerName
         ,'1900-01-01 00:00:00.000'
         , CASE 
           WHEN PATINDEX('%[0-9]%', CustomerName) > 0 
            THEN REPLACE(SUBSTRING(CustomerName, PATINDEX('%[0-9]%', CustomerName),
            LEN(CustomerName)), ')', '')  ELSE 0 END
         ,''
         ,1
         ,1
         ,1
         ,GETDATE()
         ,'Import'
         ,GETDATE()
         ,'Import'
         ,NULL
FROM External_Blk_Itm_Contracts
WHERE TerminalName NOT IN (SELECT MBFTERMINALNAME FROM budterminals)

SELECT *
FROM #temp;
2021-04-15