Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
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
?
562 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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

597 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