Solved

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

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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL trigger 5 34
storing csv file in table variable in Python 2 64
Index and Stats Management-Specific tables 8 39
SQL - Curser to do an insert based on a select 2 26
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
: 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.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…

749 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