How to get all id's from inserted table

后端 未结 1 1087
隐瞒了意图╮
隐瞒了意图╮ 2021-01-28 03:47

I am insert bulk of records how to get all those Id\'s from inserted tables.

I want to use all those ids as forgein keys and insert into another table

IN         


        
相关标签:
1条回答
  • 2021-01-28 03:55

    Use OUTPUT clause:

    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;
    
    0 讨论(0)
提交回复
热议问题