Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to reference other scripts in a TSQL script file?

like C file, add reference in a SQL script file at beginning?


#include "somefile.sql"

-- other code...

like image 504
Cooper.Wu Avatar asked Jan 12 '12 07:01

Cooper.Wu


People also ask

How do I run a SQL script from another SQL script?

If your scripts are . sql (or any kind of text) file, as @Abe Miesller says (upvoted) you can run them from within SSMS via the :r command, when SQLCMD mode is enabled. You would have to know and script the exact file path and name. This cannot be done from within a stored procedure.

How do I compare two .SQL files?

ApexSQL Compare offers integration for SSMS and Visual Studio, so you can right-click a SQL object in Object Explorer, select the Compare scripts in the right-click context menu, then Set as left/right, and the New comparison query window will be shown.


1 Answers

If you are running them with sqlcmd you can use r: to include other scripts

:r C:\Files\selectFromTable.sql 

Editing SQLCMD Scripts in the Transact-SQL Editor

like image 115
Mitch Wheat Avatar answered Oct 11 '22 14:10

Mitch Wheat