Solved

enter data in 2 table at the same time

Posted on 2011-09-08
2
226 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
Comment Utility
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
Comment Utility
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

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how the fundamental information of how to create a table.

772 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now