Solved

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

Posted on 2014-01-24
4
549 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
  • 2
  • 2
4 Comments
 
LVL 59

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 59

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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

777 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