sql - Insert output IDs into another table -
i have status table, , table containing additional data. object ids pk in status table, need insert additional data table each new row.
i need insert new row statustable each new listing, containing constants.
declare @temp table(listingid int) insert statustable(status, date) output inserted.listingid @temp select 1, getdate() anotherimportedtable
this gets me enough new listing ids use.
i need insert actual listing data table, , map each row 1 of listingids -
insert listingextradata(listingid, data) select t.listingid, a.data @temp t, anotherimportedtable
now doesn't work, because otherdatatable , ids in @temp unrelated... far many rows inserted.
how can insert each row anotherimportedtable
listingextradata
along unique newly created listingid
? possibly trigger more sql @ point output
in first block of sql?
edit: input far, here's tables like:
anotherimportedtable:
data
statustable:
listingid (pk), status, date
listingextradata:
data, listingid
you see want create 1 entry statustable
per row in anotherimportedtable
, put 1 listingid
row anotherimportedtable
listingextradata
... i'm thinking might have resort cursor perhaps?
ok, here's how can (if i'm right want do):
insert listingextradata(listingid, data) select q1.listingid, q2.data (select listingid, row_number() on (order listingid) rn @temp t) q1 inner join (select data, row_number() on (order data) rn anotherimportedtable) q2 on q1.rn = q2.rn
in case matching logic differs need change sorting of anotherimportedtable. in case match order can not achieved ordering anotherimporttable [in 1 way or another] you're out of luck.
Comments
Post a Comment