Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Insert data from 2 Tables

Author  Topic 

h_abhijith
Starting Member

2 Posts

Posted - 2011-10-31 : 15:29:13
Hi,
I need to insert data into a table. But the data for that table comes in one table. And the column names are in a reference table. I somehow need to map the column names from the reference table to the actual data table .(Both these share a Key). Any idea, how this can be done?

example:

TableA(First Name,Last Name,City) -- The table where the final data will go to

TableB(ID,Field1,Field2,Field3) -- The table which contains the data with generic column names

TableC(ID,Field1,Field2,Field3) -- The ref table which for a given ID from the TableB..identifies what's the column name for TableB in that particular Field based column.

Let's say TableB has this data:

ID Field1 Field2 Field3

1 John Smith LA

2 Denver David Parker

TableC will have this mapping:

ID Field1 Field2 Field3

1 first name Last Name City

2 City First Name Last Name

The tableA should have the data with the proper columns formatting.

I hope you get what I'm trying to say. Please let me know how do I achieve this.

Thank you

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-11-01 : 04:41:54
what kind of database design is this? why do you need table to determine which columns and what order you want them in resultset. its a presentation requirement which has to be done at your front end application. no need of storing and retrieving these details in db.
In above design, if you want to determine columns like this, you need to make use dynamic sql. Also you need to make sure code you write is well checked to avoid injection attack

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -