Solved

Creating a variable list from an input file to use in TSQL select statement  - SQL Server

Posted on 2014-01-24
4
551 Views
Last Modified: 2014-01-24
Experts,

I have an input file that has a list of four character variables.  The number of rows on the file can vary.  This list has the codes that I would like to select from a Sql Server table.

Unfortunately the way our system is set up you cannot read an external file into the select statement which reads the table directly.
i.e.  Select * from Table A where code in (Select code from input_file)

Does anyone know of an efficient way to pull only the codes on the list without hard coding them in the select statement (since the codes can change) or reading the entire file into a temporary table and then selecting only the codes on the input file.
0
Comment
Question by:morinia
[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
  • 2
4 Comments
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 39806504
Unfortunately the way our system is set up you cannot read an external file into the select statement which reads the table directly.
Can you please clarify what this means?  If you are not allowed to read an external file, how are you currently getting the information?  

If you could read the file using SQL, you could store the data in delimited or XML format.  That way, you can query the contents of the file.
http://technet.microsoft.com/en-us/library/ms190312.aspx

Again, though, I am not sure this is an option based on your comment.
0
 

Author Comment

by:morinia
ID: 39806579
What I mean is that I cannot query against the SQL Server and use a where clause which is accessing a table and/or an external file within the query that is directly accessing the table.

i.e.  Select * from Table_A where code in (Select code from input_file)

In the statement above table_a being the table from SQL Server and input_file being the list of codes I want to use.

If hardcoded the statement might read:
Select * from Table_A where code in ('ABCD', 'DEFG', 'GHIJ', 'KLMN');

The input file is:
ABCD
DEFG
GHIJ
KLMN

I am working on your suggestion above  to store the data in delimited or xml format.

I will respond as to how I make out

Thanks
0
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 500 total points
ID: 39806878
Here is an example.

input_file.txt contains:
ABCD
DEFG
GHIJ
KLMN

Open in new window


input_fmt.xml contains:
<?xml version="1.0"?>
<BCPFORMAT xmlns="http://schemas.microsoft.com/sqlserver/2004/bulkload/format" 
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <RECORD>
  <FIELD ID="1" 
         xsi:type="CharTerm" 
         TERMINATOR="\r\n" 
         MAX_LENGTH="255" 
         COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
 </RECORD>
 <ROW>
   <COLUMN SOURCE="1" NAME="Value" xsi:type="SQLNVARCHAR"/>
 </ROW>
</BCPFORMAT>

Open in new window


Sample SQL:
DECLARE @ValueList TABLE([Value] VARCHAR(255) PRIMARY KEY);

INSERT INTO @ValueList([Value])
SELECT [Value]
FROM OPENROWSET(
    BULK 'c:\temp\input_file.txt', 
    FORMATFILE = 'c:\temp\input_fmt.xml'
) AS input_file
;

SELECT [Value]
FROM @ValueList
;

/* USAGE: 
SELECT /* columns you want. */
FROM Table_A
WHERE WorkOrder. IN (
    SELECT [Value]
    FROM @ValueList
)
;
*/

Open in new window


Results:
Value
--------------
ABCD
DEFG
GHIJ
KLMN

Open in new window


I hope that helps!
0
 

Author Closing Comment

by:morinia
ID: 39807229
THANKS!!! IT WORKED GREAT!!

This definitely helped.  From the example you gave me I was able to translate the process into SAS and create a macro and it worked fine.

Thanks for all your help.
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to shrink a transaction log file down to a reasonable size.

739 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