Solved

TSQL SQL to query table using a list of keys from an excel column

Posted on 2014-03-12
4
957 Views
Last Modified: 2014-04-23
I know I have done this in the past but am drawing a complete blank... this should be SO easy.

What I want to do is write TSQL which will read a SQL 2008 table using a list of values in an Excel s/s as the key/where clause.

Example.  I want to query aaa_auto_table where aaa_auto_id = (list of values from excel column).. so this would be a loop, I assume.
0
Comment
Question by:66chawger
  • 2
4 Comments
 
LVL 33

Expert Comment

by:knightEknight
ID: 39924764
How many values are in the Excel column?

You can use the in clause to query multiple values:

select * from aaa_auto_table where aaa_auto_id in (2,4,6,8,10)
0
 
LVL 1

Expert Comment

by:DB-aha
ID: 39924860
0
 

Author Comment

by:66chawger
ID: 39926232
KnightEknight,

This is the simplest, however, there are a large number of values in the column, possibly many hundreds or thousands... I know I can do find and replace to put the SQL syntax to us the values in a SQL query, but this is cumbersome.  I know in the past I was able to write or use existing TSQL that would perform the query looping through the list of values.. it may have been a UDF or Stored Procedure also, I don't remember.  

DB-aha, this link appears more of querying within Excel, I need to use a list of values from an excel column to query a SQL table.
0
 
LVL 1

Accepted Solution

by:
DB-aha earned 500 total points
ID: 39926369
The information in the link I provided is only partial for what you need. It involves some creativity in querying the excel sheet as a table and then referring to the column you want to search where ColumnName IN(excelSheet.ColumnName). You can make this easier by importing the excel sheet into a table itself but the IN operator is definitely what you would use in either case. By importing the excel sheet into a table, you can index the column you want to search by which should speed up performance of the query if you are looking for hundreds of thousands of values.
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

The System Center Operations Manager 2012, known as SCOM, is a part of the Microsoft system center product that provides the user with infrastructure monitoring and application performance monitoring. SCOM monitors:   Windows or UNIX/LinuxNetwo…
User Beware!  This is a rather permanent solution to removing your email from an exchange server.  The only way to truly go back is to have your exchange administrator restore your mailbox from backups.  This is usually the option of last resort.  A…
Viewers will learn a basic relationship technique in Power Pivot for Excel 2013.
Viewers will learn the basics of slicers and timelines for both PivotTables and standard Excel tables in Excel 2013.

803 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