Solved

MSSQL query to return results based on a list of inputs

Posted on 2014-11-06
3
137 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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
A couple of weeks ago, my client requested me to implement a SSIS package that allows them to download their files from a FTP server and archives them. Microsoft SSIS is the powerful tool which allows us to proceed multiple files at same time even w…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

895 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