[thelist] SQL: insert into multiple tables help

Ryan Finley RyanF at SonicFoundry.com
Tue Apr 10 16:53:11 CDT 2001

How can I insert a new email address into Subscribers and a Subscriber
foreign key to Subscriptions at the same time? Since INSERT INTO works with
only one table, is it possible? Or do I need to insert the email, run
another query to get the ID of the new Subscriber, and yet another SQL
statement to insert the SubscriberFK to Subscriptions?

Just use the @@IDENTITY field:

INSERT INTO Subscribers ...
INSERT INTO Subscriptions(SubscriberID,...) VALUES (SELECT @@IDENTITY,...)

I think this should work.

	Ryan Finley
	President - SurveyMonkey.com (http://www.surveymonkey.com)

More information about the thelist mailing list