I'm assuming I'm missing something really basic, I just can't seem to figure it out. I am doing an INSERT into a table from an OPENXML recordset, and I need to populate a link table with IDs just entered into the first table. Examples seem to work best, so here goesINSERT INTO EmailAddress(EmailTypeID, Address)SELECT xEmailTypeID, xAddressFROM OPENXML(@hDoc, 'Persons/Person/Emails/Email[@DBAction="A"]')WITH (xEmailTypeID int '@EmailTypeID', xAddress varchar(255) '@Address')
Just a really basic INSERT..SELECT, but now I need to go and associate those emails just added with the Person in a PersonEmailAddress table. Since there can be more than one email address, I can't just use @@IDENTITY because that will only give me the last. My attempt at a solution doesn't seem to work right:DECLARE @RowCount int...Do The Insert Here...SET @RowCount = @@RowCountSET ROWCOUNT @RowCount INSERT INTO PersonEmail (PersonID, EmailAddressID) SELECT @PersonID, EmailAddressID FROM EmailAddress ORDER BY EmailAddressID DESCSET ROWCOUNT 0
The thinking was that I could get just the last EmailAddressIDs from the insert by limiting my next INSERT..SELECT using SET ROWCOUNT to the rowcount of the first insert and then selecting the IDs from EmailAddress in descending order.Unfortunatly what I get is the first @RowCount number of IDs from EmailAddress in backwards order (eg: 3, 2, 1 instead of 403, 402, 401), as if the SET ROWCOUNT is limiting the SELECT before the ORDER BY has a chance to take effect.Is there some other way of getting at this problem that I missed? I'd really rather not have to cursor through the XML simply for speed reasons, though it'd sure make the development easier. I would very much appreciate any insight :)Thanks!Edited by - ailuro on 06/02/2002 08:42:03