Solved

Inserting data using column numbers

Posted on 2015-02-11
8
80 Views
Last Modified: 2015-02-11
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.
0
Comment
Question by:Morpheus7
  • 3
  • 3
  • 2
8 Comments
 
LVL 33

Expert Comment

by:ste5an
ID: 40603207
You know, that you cannot rely on column order?
0
 

Author Comment

by:Morpheus7
ID: 40603247
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
0
 
LVL 33

Expert Comment

by:ste5an
ID: 40603331
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.
0
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40603417
Can you give a little bit of:

1. current table,
2. sample data and
3, resultant table.
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

Author Comment

by:Morpheus7
ID: 40603577
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
0
 
LVL 24

Accepted Solution

by:
Phillip Burton earned 500 total points
ID: 40603604
Sounds like you want:

Select ProdID, 'TestName1' as FieldName, TestName1 as myValue
From MyTable
Where TestName1 is not null
UNION ALL
Select ProdID, 'TestName2', TestName2
From MyTable
Where TestName2 is not null
UNION ALL
Select ProdID, 'TestName3', TestName3
From MyTable
Where TestName2 is not null
UNION ALL
Select ProdID, 'TestName4', TestName4
From MyTable
Where TestName4 is not null
0
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40603609
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;
0
 

Author Closing Comment

by:Morpheus7
ID: 40603638
That's great, thank you.
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL Server DatePart HOUR 6 39
How to replace WHITESPACE (1 or more spaces) in a field with a SINGLE DASH? 4 30
SQL Query 2 59
CROSS APPLY 4 43
Recently, when I was asked to create a new SQL 2005 cluster, Microsoft released a new service pack for MS SQL 2005 what is Service Pack 3. When I finished the installation of MS SQL 2005 I found myself troubled why the installation of SP3 failed …
by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

932 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

10 Experts available now in Live!

Get 1:1 Help Now