barnesco
asked on
Inserting values into a table
How do you insert an @@Identity value with values from another table. I tried:
DECLARE @NewStoreID int
SELECT @NewStoreID = @@Identity
INSERT INTO PromoList
VALUES (StoreID, PromoID)
SELECT @NewStoreID, PromoID FROM SourceList
Obviously the @NewStoreID isn't in the SourceList, so how do you get @NewStoreID in with the PromoID?
Thanks.
DECLARE @NewStoreID int
SELECT @NewStoreID = @@Identity
INSERT INTO PromoList
VALUES (StoreID, PromoID)
SELECT @NewStoreID, PromoID FROM SourceList
Obviously the @NewStoreID isn't in the SourceList, so how do you get @NewStoreID in with the PromoID?
Thanks.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
You guys are fast!
I did figure it out, and I can't believe it's this simple, but it works:
DECLARE @NewStoreID int
SELECT @NewStoreID = @@Identity
INSERT INTO KScoop_PromoList (StoreID, PromoID)
SELECT @NewStoreID, PromoID FROM SourceList
I did figure it out, and I can't believe it's this simple, but it works:
DECLARE @NewStoreID int
SELECT @NewStoreID = @@Identity
INSERT INTO KScoop_PromoList (StoreID, PromoID)
SELECT @NewStoreID, PromoID FROM SourceList
just a quick note, use Scope_Identity() rather than @@identity. If you have triggers on your table, @@identity could be way way off.
VALUES (StoreID, PromoID)
SELECT getID(), PromoID FROM SourceList