Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to execute a text file from SQL query?

I have a number of generated .sql files that I want to run in succession. I'd like to run them from a SQL statement in a query (i.e. Query Analyzer/Server Management Studio).
Is it possible to do something like this and if so what is the syntax for doing this?

I'm hoping for something like:

exec 'c:\temp\file01.sql'  exec 'c:\temp\file02.sql' 

I am using SQL Server 2005 and running queries in management studio.

like image 251
ChrisHDog Avatar asked Oct 28 '08 01:10

ChrisHDog


People also ask

How do I run a text file in SQL?

command: mysql> source file_name mysql> \. The statement shown outputs <info_to_display> . You can also invoke mysql with the --verbose option, which causes each statement to be displayed before the result that it produces. mysql ignores Unicode byte order mark (BOM) characters at the beginning of input files.

How do I run a text file in mysql?

use the MySQL command line client: mysql -h hostname -u user database < path/to/test. sql. Install the MySQL GUI tools and open your SQL file, then execute it.


2 Answers

use xp_cmdshell and sqlcmd

EXEC xp_cmdshell  'sqlcmd -S ' + @DBServerName + ' -d  ' + @DBName + ' -i ' + @FilePathName 
like image 153
Gulzar Nazim Avatar answered Sep 29 '22 17:09

Gulzar Nazim


Very helpful thanks, see also this link: Execute SQL Server scripts for a similar example. To turn xp_cmdshell on and off see below:

On

SET NOCOUNT ON   EXEC master.dbo.sp_configure 'show advanced options', 1  RECONFIGURE  EXEC master.dbo.sp_configure 'xp_cmdshell', 1  RECONFIGURE  

Off

EXEC master.dbo.sp_configure 'xp_cmdshell', 0  RECONFIGURE  EXEC master.dbo.sp_configure 'show advanced options', 0  RECONFIGURE   SET NOCOUNT OFF  
like image 45
Archi Moore Avatar answered Sep 29 '22 18:09

Archi Moore