enter data in 2 table at the same time

I want to enter data in two table at the same time and I am writing stored procedure for that.

Like you can consider customer table

For Payment table
Primarykey,UerID(Primarykey of customer tbale),Credictcardnumber,CVVcode,Expirationdate

I want to first enter data in custoemr table at the same time primarykey of custoemr tbale I want to enter in payment tbale.
I want to write stored procedure but I really don't know how can I enter custoemr table primary key into payment table at the same time.

Please help me out.I am not that much good in SQL
Who is Participating?
binaryevoConnect With a Mentor Commented:
So write your insert into Customers query first and from that you do Select SCOPE_IDENTITY and that will return you the ID / PK field from your Customers table, from there you use that value in your second insert.

			INSERT INTO Customers(Col1,

			Declare @ScopeID int
			Set @ScopeID = (Select SCOPE_IDENTITY())
			INSERT INTO Table2(CustomerID,

Open in new window

One way you can do it is to create the customer ID and not rely the auto ID to get it for you, then use that ID to enter in both tables
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.