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
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;