Solved

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

Posted on 2014-03-12
4
946 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Article by: Leon
Software Metering within our group of companies has always been an afterthought until auditing of software and licensing became a pain point. Orchestrator and SCCM metering gave us the answer and it was an exciting process.
: Microsoft Office Collaborate for free and online versions of Microsoft  Word, Excel, Powerpoint, OneNote, Onedrive , Email, Calendar etc. In short we can say that Microsoft office is a suite of servers, applications and services developed by  Micr…
Viewers will learn how to find and create templates in Excel 2013.
Viewers will learn the basics of printing in Excel 2013 and how to adjust some common settings.

863 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

28 Experts available now in Live!

Get 1:1 Help Now