Solved

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

Posted on 2014-03-12
4
986 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
Having trouble getting your hands on Dynamics 365 Field Service or Project Service trial? Worry No More!!!
Viewers will learn the basics about Excel 2013’s new Flash Fill feature.
Viewers will learn the basics of printing in Excel 2013 and how to adjust some common settings.

690 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