x
login about faq Site discussion (meta-askssc)

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 '09 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 '09 at 04:13 PM

RBarryYoung gravatar image

RBarryYoung
762 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.

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



Facebook logo Follow Ask SSC on Facebook
Find Ask SSC on Google+
linkedin logo Find us on LinkedIn

Topics:

x912
x91

asked: Oct 18 '09 at 03:43 PM

Seen: 902 times

Last Updated: Oct 19 '09 at 05:51 AM

Copyright © 2002-2012 Simple Talk Publishing. All Rights Reserved. If you have any queries, please contact the site administrators.
Ask SQL Server Central is a community service provided by Red Gate.