Zoals EzLo al zei, is output je vriend voor het ophalen van ingevoerde identiteitswaarden:
-- use a table _temp_org_records for output
if object_id('_temp_org_records') is not null drop table _temp_org_records;
-- create table with correct column datatypes
select top 0 UserID
into _temp_org_records
from UserProductMapping
INSERT INTO User (userlogin, Organisationid, emailaddress, username, userpassword)
OUTPUT inserted.UserID INTO _temp_org_records --all USerIDs will be saved into _temp_org_records
SELECT 'AGT' + Code, organisationid, '[email protected]', 'User' + Code, '123'
FROM organisation;
INSERT INTO UserProductMapping (UserID, ProductID)
SELECT t.UserID, productid.value
FROM
_temp_org_records t
cross join (values ('11'),('22'),('33'),('44'),('55')) as productid(value)
INSERT UserGroups
SELECT t.UserID, UserGroup.value
FROM
_temp_org_records t
cross join (values ('1'),('3')) as UserGroup(value)