Link to home
Start Free TrialLog in
Avatar of Morpheus7
Morpheus7

asked on

Inserting data using column numbers

Hi,

Would it be possible to insert data from another table using column numbers. We have a reference table that holds the column numbers together with the corresponding column header value.  The reference table is as follows:
Name VARCHAR (30)
ColumnNumber INT

We would also like to be able to insert the corresponding Name value into the target table together with the other info.

Target Table is as follows:
Name VARCHAR(30)
Results VARCHAR (50).

It would probably mean an UPDATE statement, as the target table is already populated with IDs, dates, etc from the source table. Unless there is a way to INSERT the whole line at once.

Any assistance would be appreciated.
Thanks.
Avatar of ste5an
ste5an
Flag of Germany image

You know, that you cannot rely on column order?
Avatar of Morpheus7
Morpheus7

ASKER

Hi,
Yes I understand that but our customer will only supply the data in this format. We receive a file from them, then plan to load it into a work table and split out the info that we need using the column numbers. So we really have no choice about this.

Thanks
Please rephrase your question and explain your concrete problem.

It sounds like a common file import. But this has nothing to do with "Inserting data using column numbers" on T-SQL level. This kind of transformation is normally done in the front-end.

Give us a concise and complete example. Add some sample import file and your DDL sofar.
Can you give a little bit of:

1. current table,
2. sample data and
3, resultant table.
Hi,

If there is a way to insert the column heading in one table as a data item in another.
The way we receive the data is as follows:

ProdID   Barcode      TestName1 TestName2 TestName3 TestName4
AB1256 12345678      3.67              90.89            7.789            56.0

In the table above, we have one row per ProdID. We need to be able to move the data to another table where we have one row per test. So in the case above we would then have 4 entries in the new table.  So we would need to be able to insert  TestName1 TestName2 TestName3 TestName4 as data items in the new table together with the values for that test like so:

AB1256       TestName1     3.67
AB1256       TestName2    90.89
AB1256       TestName3    7.789
AB1256       TestName4    56.0

Thanks
ASKER CERTIFIED SOLUTION
Avatar of Phillip Burton
Phillip Burton

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Alternatively, you can UNPIVOT:

Select ProdID, FieldName, myValue
from (Select ProdID, TestName1, TestName2, TestName3, TestName4
From myTable) as pvt
UNPIVOT
(myValue FOR FieldName IN (TestName1, TestName2, TestName3, TestName4)) as unpvt;
That's great, thank you.