Include Statement

INCLUDE Statement

 Executes the SQL statements in the supplied file.

Syntax

INCLUDE 'filename'

Arguments

'filename'

The path of a text file containing SQL statements. The path becomes the current working directory for any further files to be included. If the path is not absolute, and if the @@SCRIPTURI is set, then the result is the combination of 'filename' as relative to @@SCRIPTURI.

Remarks

Nesting of include files is supported.  Variables are visible to a nested include file. 

Recursion is possible, but limited to 100 files.

Examples

1) Conditional include

IF version = '1.0'
  INCLUDE 'v1.sql'
ELSE
  INCLUDE 'v1.1.sql'

2) Variable is used across included files

-- startup.sql
DECLARE @log bit = 0
INCLUDE 'C:\Documents and Settings\All Users\Documents\stage1.sql'
INCLUDE 'stage2.sql'
-- stage1.sql
IF @log = 1 THEN PRINT 'starting stage1...'
-- ...
IF @log = 1 THEN PRINT stage1 complete'
-- stage2.sql
IF @log = 1 THEN PRINT 'starting stage2...'
-- ...
IF @log = 1 THEN PRINT stage2 complete'