?
Solved

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

Posted on 2014-01-24
4
Medium Priority
?
560 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 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

765 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