Solved

MSSQL query to return results based on a list of inputs

Posted on 2014-11-06
3
139 Views
Last Modified: 2016-02-11
I have the need to run a query based on the data that is saved in an excel spreadsheet.  The spreadsheet has two columns and about 100 rows.  The two values in each row will be plugged into the sql query.  The query should loop through the list.  Honestly, I can put the list in whatever format is best for this process.

My guess is that SSIS may server me best, but I have never do the looping needed to make the query run each row.  Below is the very simple query with Column1 and Column2 as the variables.  The Column1 data is formatted as 0000 and the Column2 data is formatted as 00000.

Please be detailed in your response because my query knowledge is somewhat limited.

select * from MHGROUP.DOCMASTER
where C1ALIAS = 'Column1' and C2ALIAS = 'Column2'
0
Comment
Question by:TPBPIT
  • 2
3 Comments
 
LVL 65

Accepted Solution

by:
Jim Horn earned 500 total points
ID: 40426669
>Please be detailed in your response because my query knowledge is somewhat limited.
The extremely low-tech solution would be...

1.

>The Column1 data is formatted as 0000 and the Column2 data is formatted as 00000.
Eyeball the data types of DOCMASTER.C1ALIAS and .C2ALIAS, and write them down.

2.

In SSMS, click on your database, then do a right-click > Tasks > Import Data... and follow the prompts to connect to your Excel doc and create a new table, let's call it Table1, with columns Column1 and Column2, with the data types noted in Step 1.   Using this wizard avoids the step of having to create an SSIS package.

3.

Execute the below query in SSMS:
SELECT dm.* 
FROM MHGROUP.DOCMASTER dm
   JOIN Table1 t1 ON dm.C1ALIAS = t1.Column1 AND dm.C2ALIAS = t1.Column2

Open in new window

0
 

Author Closing Comment

by:TPBPIT
ID: 40427220
I had to convert the file to a tab delimited text file, but it worked like a champ.  Thanks for the help!
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 40427235
Thanks for the grade.  Good luck with your project.  -Jim
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

My client sends data in an Excel file to me to load them into Staging database. The file contains many sheets that they have same structure. In this article, I would like to share the simple way to load data of multiple sheets by using SSIS.
My client sends a request to me that they want me to load data, which will be returned by Web Service APIs, and do some transformation before importing to database. In this article, I will provide an approach to load data with Web Service Task and X…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

813 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

11 Experts available now in Live!

Get 1:1 Help Now