Solved

enter data in 2 table at the same time

Posted on 2011-09-08
2
230 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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Suggested Solutions

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

735 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