Solved

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

Posted on 2014-01-24
4
534 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
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.
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.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

747 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

12 Experts available now in Live!

Get 1:1 Help Now