473,385 Members | 1,821 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Inserting into multiple related tables using table valued parameters.

How would I insert into multiple related tables using a table valued parameter? Is there any way to do this without using a WHILE loop?

I know how to insert from a table valued parameter into one table:
INSERT INTO dbo.Table
SELECT * FROM @TVP

But what do I do when I want to insert all of someone's information into multiple tables, and have those records related to each other by foreign keys? For example, I want to insert their address info into the dbo.Address table, their phone info into the dbo.Phone table, and then the PKs for the Phone record and the Address record should be inserted in the dbo.Person table, along with FirstName and LastName. How do I get those PKs and insert them into the Person table, all as part of inserting with a table valued parameter that has multiple records in it?
Jan 16 '10 #1
3 4941
Delerna
1,134 Expert 1GB
You will need to write multiple insert statements (one for each table to be inserted to) and select the appropriate fields from @TVP for each different insert.

You will need to join @TVP to the previously inserted table to get the primary key to insert into the next table.

for example, something like this
Expand|Select|Wrap|Line Numbers
  1. INSERT INTO dbo.Person
  2. SELECT Name,Surname FROM @TVP
  3.  
  4. INSERT INTO dbo.Phone
  5. SELECT Person.PersonID,Phone 
  6. FROM @TVP
  7. JOIN dbo.Person on Person.Name=@TVP.name 
  8.   and  Person.SurName=@TVP.SurName 
  9. WHERE Phone is not null and Phone<>''
  10. etc
  11. etc
  12.  
Jan 18 '10 #2
Delerna
1,134 Expert 1GB
I generally try not to give advise that was not asked for because your design is your choice . However, you may want to give further consideration to normalizing your tables.
Jan 18 '10 #3
ck9663
2,878 Expert 2GB
Or you can create a VIEW then use that view to cascade your update on the underlying tables.

Happy Coding!!!

~~ CK
Jan 20 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Greg Ofiesh | last post by:
Anyone who can help, I have two tables T1 and T2. T1 has fields K1 and F2 and T2 has fields K2 and F1. F1 is the foreign key relating to K1 and F2 is the foreign key relating to K2. My...
2
by: a | last post by:
NEW Post Here's my best guess at how to insert this dataset.... the code runs, but no new records are added to the sql table. I've read and split a delimited text file into a dataset. It...
0
by: Sandy | last post by:
I have four related tables. I need to do an insert on three of them. I created a stored procedure that handles the insert on two of the tables correctly . . . then I get to a tough part. The...
6
by: James Radke | last post by:
Hello, I have a multithreaded windows NT service application (vb.net 2003) that I am working on (my first one), which reads a message queue and creates multiple threads to perform the processing...
11
by: c676228 | last post by:
Hi everyone, I am just wodering in asp program, if there is anybody writing store procedure for inserting data into database since there are so many parameters need to be passed into store...
0
by: kolalakitty | last post by:
Hopefully someone here can help me/point me in the right direction. I've found tons of references towards making relations, creating rows, saving said rows, using datagrids, databinding objects,...
3
by: rcoco | last post by:
Hi, I want to share this problem. I have a datagrid that will help me Insert data into sql database. So I made a button On my form so that when I press the button a new row on datagrid should be...
11
by: shriil | last post by:
Hi I have this database that calculates and stores the incentive amount earned by employees of a particular department. Each record is entered by entering the Date, Shift (morn, eve, or night)...
2
by: cluce | last post by:
I am trying to read a csv file with user info (username, password, email, address, city, zip, state, etc.) I am inserting the username, password, email into the aspnet_memberhsip table using the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.