Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call a SQL script from within another SQL script?

i want to call a series of .sql scripts to create the initial database structure

  1. script1.sql
  2. script2.sql etc.

is there any way of doing this without sqlcmd or stored procedures or any other kind of code that is not sql ? just inside a .sql file.

like image 437
keikkeik Avatar asked Oct 15 '08 20:10

keikkeik


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.


1 Answers

you could try this:

exec master..xp_cmdshell 'osql -E -ix:\path\filename.sql'

osql must be in the path, the full filename must be known, and logins have to be set up correctly (options -E or -U)

like image 98
devio Avatar answered Sep 25 '22 00:09

devio