Possible Duplicate:
Constants and Include files in TSQL
I’ve been looking for a T-SQL equivalent of the C “#include” directive. As I haven’t found, I wonder
- Is it possible that MS did not include any sort of SQL files inclusion / joined execution
- If not, why
- If not, is there any better way for me to execute several SQL files at once without copy-paste or manual precompilation
For those curious of my motivations, I am writing a set of similar queries, and I find it convenient to externalize constants (from enum tables), pre-computations (making some test input tables) and parameter variables (which subsets I’m working on).
As far as I know, I can’t really use stored procedures or functions for that (And I don’t like the idea of polluting my DB with those).
You could look into executing in SQLCMD mode, either in SSMS or via sqlcmd utility (command line). There is a
:r <file name>command that allows you to include external files in the current execution session. So if you included a:r my_init_script.sqlnear the top of your larger script it may achieve what you are after.Note, this is for SQL 2005 and up.