Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute SQL Server scripts

How can I automate process of running all SQL scripts from given folder ?

like image 319
gruber Avatar asked Aug 19 '10 15:08

gruber


People also ask

How do I run a SQL script in SQL Server Management Studio?

Open SQL Server Management Studio > File > Open > File > Choose your . sql file (the one that contains your script) > Press Open > the file will be opened within SQL Server Management Studio, Now all what you need to do is to press Execute button.

How do I run a SQL script in terminal?

To run SQL files from the terminal, you can use the source or the backslash and dot command ( \. ) Next, enter the password for your root user. The path /Users/nsebhastian/Desktop/test/main. sql above needs to be changed to the SQL file path on your computer.


3 Answers

Write a Windows script, use the FOR construct to loop through your files and use the SQLCMD utility to execute each file.

for %f in (c:\MySQLScripts\*.sql) do sqlcmd -i %f
like image 93
Joe Stefanelli Avatar answered Nov 08 '22 18:11

Joe Stefanelli


You can run an SQL script file with the sqlcmd command line tool that comes with SQL Server. The syntax is like this:

sqlcmd -i c:\MyScript.sql

So basically, you just need to find all files in your folder, loop through them and execute sqlcmd for each one, like shown above.

like image 36
Christian Specht Avatar answered Nov 08 '22 20:11

Christian Specht


I have created the following script for one of my projects:

SET NOCOUNT ON

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

--Create the Folder- en filetables.
DECLARE @SQLFolders TABLE ( SQLFolderName VARCHAR(MAX))
DECLARE @SQLFiles TABLE ( SQLFileName VARCHAR(MAX))
DECLARE @MainFolder VARCHAR(MAX)
DECLARE @FileName VARCHAR(MAX)
DECLARE @FolderName VARCHAR(MAX)
DECLARE @SQLStatement VARCHAR(2000)

SET @MainFolder = 'C:\ProjectName\'

--Fill the foldertable and loop through them.
INSERT INTO @SQLFolders VALUES ('CreateScripts\')
INSERT INTO @SQLFolders VALUES ('ChangeScripts\')

DECLARE cFolders CURSOR LOCAL FOR
    SELECT [SQLFolderName]
    FROM @SQLFolders
OPEN cFolders
FETCH NEXT FROM cFolders INTO @FolderName
WHILE @@FETCH_STATUS = 0
BEGIN
    --Fill the file-table and loop through.
    SET @SQLStatement = 'dir /b "' + @MainFolder + @FolderName + '*.sql"'
    INSERT INTO @SQLFiles
    EXECUTE master.dbo.xp_cmdshell @SQLStatement

    DECLARE cFiles CURSOR LOCAL FOR
        SELECT DISTINCT [SQLFileName]
        FROM @SQLFiles
        WHERE [SQLFileName] IS NOT NULL AND
              [SQLFileName] != 'NULL' AND
              [SQLFileName] != 'File Not Found'
        ORDER BY [SQLFileName]
    OPEN cFiles
    FETCH NEXT FROM cFiles INTO @FileName
    WHILE @@FETCH_STATUS = 0
    BEGIN
        SET @SQLStatement = 'SQLCMD -d hantisdb -i' +  @MainFolder + @FolderName + @FileName
        EXECUTE master.dbo.xp_cmdshell @SQLStatement

        FETCH NEXT FROM cFiles INTO @FileName
    END

    DELETE FROM @SQLFiles

    CLOSE cFiles
    DEALLOCATE cFiles
    FETCH NEXT FROM cFolders INTO @FolderName
END

CLOSE cFolders
DEALLOCATE cFolders
GO

EXEC master.dbo.sp_configure 'xp_cmdshell', 0
RECONFIGURE
EXEC master.dbo.sp_configure 'show advanced options', 0
RECONFIGURE

SET NOCOUNT OFF

I use this to recreate my database and start with a fresh database everyday. Mind you that it will execute the files in alfabetical order!

like image 28
Geert Immerzeel Avatar answered Nov 08 '22 20:11

Geert Immerzeel