Solved

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

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
Viewers will learn the basics of slicers and timelines for both PivotTables and standard Excel tables in Excel 2013.
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…

747 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

14 Experts available now in Live!

Get 1:1 Help Now