x

Mapping values from other tables while inserting

I have 3 Table say TableA(holding data) ,TableB(mapping data) and TableC(data to be inserted).

TableA

Col1                     
----                     
High                     
High                     
Low                      
Right                    
Low                      
Left

TableB

Col1      Col2          
-----------------        
1          High         
2          Low          
3          Left         
4          Right

TableC

Col1                        
----                         
1
1
2
4
2
3

My Dataset is like 3 tables shown above. TableA consists of source data uploaded using bcp.

TableB is a mapping table with static reference values as you can see.

TableC is the table to which now data has to be inserted which is col1 of TableB and equivalent numeric value for Col1 in TableA.

So my doubt is how I write my insert script for TableC and resolve corresponding values for Col1 in TableA. in short I want numeric equivalent of Col1 in TableA to be inserted into TableC by checking on mapping values from TableB.

Thanks in advance

more ▼

asked Oct 18, 2009 at 03:43 PM in Default

vijitk gravatar image

vijitk
11 1 1 1

(comments are locked)
10|1200 characters needed characters left

1 answer: sort voted first

Try this:

INSERT INTO TableC( Col1 )  SELECT TableB.Col1  FROM TableA  JOIN TableB ON TableA.Col1 = TableB.Col2 
more ▼

answered Oct 18, 2009 at 04:13 PM

RBarryYoung gravatar image

RBarryYoung
782 5 5 8

(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

New code box

There's a new way to format code on the site - the red speech bubble logo will automatically format T-SQL for you. The original code box is still there for XML, etc. More details here.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

SQL Server Central

Need long-form SQL discussion? SQLserverCentral.com is the place.

Topics:

x985
x103

asked: Oct 18, 2009 at 03:43 PM

Seen: 1252 times

Last Updated: Oct 19, 2009 at 05:51 AM