Solved

How to insert data into SQL table where a column name equals a different column namefrom another table.

Posted on 2008-10-27
2
936 Views
Last Modified: 2012-05-05
I am a fairly new beginner with SQL and I having a couple of issues.

I want to insert data in a pre-existing table from a separate table. For instance, table A has a column entitled prod_id and table B has a column titled sold_prod_id. These values are the same, they are just named differently within the two tables (ITable B was prodived and I created table A based on a provided ERD). I have created a column within table A entitled sold_date. Table B contains the sold_date information, but I need it within table A. I have tried a WHERE clause and an INNER JOIN.

WHERE clause:

insert into a (sold_date)
select sold_date
from b
where a.prod_id = b.sold_prod_id

This is where I enounter the "The multi-part identifier 'a.prod_id' could not be bound."

INNER JOIN:

insert into a (sold_date)
select sold_date
from b
inner join a on b.sold_prod_id = a.prod_id

The inner join query inputs sold_date, but after all of the data within table a. It doesn't put the sold_date on the same row associated with the prod_id. I hope this isn't too confusing and any help is greatly appreciated.
0
Comment
Question by:MISKid08
2 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 125 total points
ID: 22817217
I think you want a UPDATE:
UPDATE a
  SET sold_date = b.sold_date
from b
inner join a 
  on b.sold_prod_id = a.prod_id

Open in new window

0
 

Author Closing Comment

by:MISKid08
ID: 31510539
You're amazing. Thanks for helping a beginner.
0

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

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.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
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.

743 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

13 Experts available now in Live!

Get 1:1 Help Now