I have a few SQL statements stored in text files. How do I pull these files into a string variable in SSIS so that I can use the same query in multiple places?
Answer to questions:
The queries are long and complex, something I’d prefer to edit in a real text editor, not inside the SSIS text boxes. I’d also like the queries to be editable by people who don’t have access to SSIS or don’t know how to use it. Finally, each of the queries is used in a number of different data flows. Correct me if I’m wrong, but if I use the same query in multiple spots I believe I have to either use a variable or re-write the code for each data flow.
Here’s how I did this very thing (after searching everywhere for an answer and finding none.)
I started with a package that the Export Data Wizard created, so my instructions relate to that. Doing it that way sets up the column mappings. If you didn’t use the Export Wizard to create the package you may have to add columns by hand.
the path of your .sql file.
file specified by SqlFileName into
SQLCommand.
edit the properties using the
properties window. If you use the
fancy Edit window you’ll get an
error about the command text not
being set. It’s because SQLCommand
is blank at design-time.
because the SQLCommand is blank. To
prevent the red X, change
ValidateExternalMetaData to False.
That’s about it. I hope I remembered all the bits. The script is the key part so that you can get the SQL into a variable and then use the variable in the Data Flow.