[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Adding rows to SQL table depending on key values from another table

Posted on 2014-08-18
4
Medium Priority
?
157 Views
Last Modified: 2014-08-20
I have three tables.

Table A - Vendor Contracts Table
Table B - Vendor Agreement Details
Table C - Vendor Agreement codes and descriptions

What I need to do is for every vendor in table A,  add rows to table B for each of the rows in table C.

So for instance I have Vendor 1234567890 in Table A, I want to add rows to table B for that vendor for every "Vnn" row in table C.   "Vnn" is just a serious of codes from V1 - V9.  

There are currently a total of 9 "Vnn" codes and there respective descriptions in Table C.   So in Table B I would end up with the following:

Vendor                           Code
123456789                      V01
123456789                      V02
"
"
"
123456789                      V09

and of course for each of the other Vendors in table A.


No need to worry to much about what other columns will be selected/added.  I just need to know what the structure/syntax would look like to do this.
0
Comment
Question by:66chawger
  • 2
  • 2
4 Comments
 
LVL 1

Accepted Solution

by:
Robert Lind earned 2000 total points
ID: 40268407
I think this is what you are looking for.  Insert from select?
DECLARE @a TABLE (ID int)

DECLARE @b TABLE ( tableAID int, details varchar(10))

DECLARE @c TABLE (tableAID INT ,details VARCHAR(10))

INSERT INTO @a (id) VALUES (1)
INSERT INTO @a (id) VALUES (2)
INSERT INTO @a (id) VALUES (3)

INSERT INTO @c (tableAID,Details) VALUES (3,'Text 3')
INSERT INTO @c (tableAID,Details) VALUES (5,'Text 5')
INSERT INTO @c (tableAID,Details) VALUES (1,'Text 1')
INSERT INTO @c (tableAID,Details) VALUES (1,'Text 11')
INSERT INTO @c (tableAID,Details) VALUES (1,'Text 111')

INSERT INTO @B (tableaid,details)
SELECT  c.tableaid,c.details FROM @c  c INNER JOIN @a a ON a.id = c.tableaid

SELECT * FROM @b

tableAID      details
3      Text 3
1      Text 1
1      Text 11
1      Text 111
0
 

Author Comment

by:66chawger
ID: 40268716
So in the example above, you are using "tableAID" to select all the information from the various tables, then using this for the resulting insert, correct?
0
 
LVL 1

Expert Comment

by:Robert Lind
ID: 40270511
Yes.  In your case, you would have to match the ID's for your tables, from your example that would be the vendor column in tables a and c.
0
 

Author Comment

by:66chawger
ID: 40274454
I was making this waaaaay to difficult.. LOL.  My mindset has been tied up so much in SP's and TSQL that I forgot to think simple!

Thanks for the example.
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…

873 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