Tuesday, March 27, 2012

Executing large SQL-scripts through DMO

Hi there!

I'd like to execute some large filebased scripts through the DMO (in javascript).

Any thoughts about the best way to do this?

Regards

Jesper

This code works for me:

Dim oServer
Set FSO = CreateObject("Scripting.FileSystemObject")
Set SQLScript = FSO.OpenTextFile("C:\SQLTricks\SqlScript.sql")

dim sSQL
sSQL = ""
do until SQLScript.atendofstream = True
sSQL = sSQL + SQLScript.readline
loop

Set oServer=CreateObject("SQLDMO.Sqlserver")
oServer.LoginSecure=True
oServer.Connect("MyServer")

set oDatabases=oServer.Databases
for each db in oDatabases
if not (db.Name = "master" or db.Name = "model" or db.Name = "msdb" or db.Name = "tempdb") then
db.ExecuteImmediate sSQL, SQLDMOExec_ContinueOnError
end if
next
oServer.Disconnect

set oServer=Nothing

Note that it'll run the script against every database that isn't a system database. Modify to your own needs.

No comments:

Post a Comment