Solved

enter data in 2 table at the same time

Posted on 2011-09-08
2
231 Views
Last Modified: 2012-05-12
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
PrimaryKey,FirstName,LastName,Age,Gender

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
0
Comment
Question by:Itgirl16
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 6

Expert Comment

by:billfusion
ID: 36505825
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
0
 
LVL 14

Accepted Solution

by:
binaryevo earned 500 total points
ID: 36505848
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,
									Col2,
									Col3)
			VALUES(@Col1,
				   @Col2,
				   @Col3)

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

Open in new window

0

Featured Post

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question