October 11, 2005 at 3:46 am
Hello all,
i have a table called 'users_master' and another 'users_personalinfo'
The primary key for users_master is 'user_id' int identity and there are other fields in it like password etc...
In the users_personalInfo there is field user_id as in the user_master and other fields like name etc..The user_id here is a foreign key which references the user_id of the users_master table.
When i insert a record into the users_master and users_personalinfo (using application like a .net program) ,i need to make sure that personalinfo gets added to the correct user_id.
how can this be done.
Thank u
October 11, 2005 at 5:24 am
I assume what you are asking is how to insert a new row into users_master and in the same transaction insert a row (or many) into users_personalinfo, using the same primary key as was created in users_master. What you need to do is use the function SCOPE_IDENTITY() to fetch the value that was created for the new user.
DECLARE @user_id INT
INSERT INTO users_master VALUES (...)
SET @user_id = SCOPE_IDENTITY()
INSERT INTO users_personal_info (user_id, ...) VALUES (@user_id, ...)
October 11, 2005 at 5:35 am
u got me right chris.
Thanks for ur reply
Viewing 3 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply