HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Restore SQL Server database from .sql files

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
sqldatabasefilesserverfromrestore

Problem

So we had an encrypted database that puked and killed our whole SQL Server setup. Sucks about our data, but we were smart enough to have our data structures / stored procedures / functions in Git

The problem is they're saved as .sql files.

Is there anyway we can batch restore our schema from directories full of these files?

I've looked around and I can only find tutorials for restoring from .bak files or .mdf's. This isn't the lazy man's way out- I just need to find a solution ASAP. Any advice or resources/ anything at all would be greatly appreciated.

Thanks Interwebs,

Dylan

Solution

So you just need to execute the collection of schema scripts to recreate your database? You could execute them one by one or create a batch file and use SQLCMD to execute all script files in directory

for /r "%ScriptPath%" %%X in (*.sql) do ( 
   sqlcmd -S%ServerName% -d%DBName% -b -i "%%X"
)

Code Snippets

for /r "%ScriptPath%" %%X in (*.sql) do ( 
   sqlcmd -S%ServerName% -d%DBName% -b -i "%%X"
)

Context

StackExchange Database Administrators Q#34902, answer score: 3

Revisions (0)

No revisions yet.