Solved

enter data in 2 table at the same time

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
always on switch back after failover 2 34
C# Gridview 1 31
Increment column based of a FK 8 21
VB.NET 2008 Winforms Signing 13 17
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
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…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

770 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