vbs做的sql脚本执行工具.docx
- 文档编号:1806625
- 上传时间:2023-05-01
- 格式:DOCX
- 页数:9
- 大小:17.10KB
vbs做的sql脚本执行工具.docx
《vbs做的sql脚本执行工具.docx》由会员分享,可在线阅读,更多相关《vbs做的sql脚本执行工具.docx(9页珍藏版)》请在冰点文库上搜索。
vbs做的sql脚本执行工具
vbs做的sql脚本执行工具
vbs做的sql脚本执行工具
##################执行多目录下的所有sql脚本的工具#################################'#可以方便的执行指定的目录或文件到多个服务器多个数据库'#只会执行以
“.sql”后缀的脚本文件'#执行返回结果会生成报…
##################执行多目录下的所有sql脚本的工具
#################################
'#可以方便的执行指定的目录或文件到多个服务器多个数据
库
'#只会执行以“.sql”后缀的脚本文件
'#执行返回结果会生成报告以文本文件打开
'【请在此过程中填写运行参数】
Subtodo
DimexecTool
SetexecTool=newMySqlScriptExecTool
'使用步骤:
'一、指定需要执行脚本的服务器,分别为:
目标服务器的ip、数据库名、数据库用户名、数据库密码execTool.addDatabaseLink
"192.168.1.116","dbname","username","pwd"
'如果需要添加多个不同的服务器或者数据库,可以模仿如下
方法继续添加
'execTool.addDatabaseLink"192.168.1.116","dbname2","username","pwd"'二、指定需要执行的脚本所在的目录:
这样在最终执行前会搜索出具体的需要执行的文件,可以写多条类似语句添加多个目录
execTool.addFold"F:
\exec\sql"
'execTool.addFold"D:
\drp\DEV\db\SQLServer\report"'三、如果同时需要执行目录下面有子目录的脚本则需要去掉下面这句话前面的“'”
'execTool.searchChildFolds()'迭代找出所有子目录'找出指定目录里面的sql脚本execTool.searchFiles()
'四、也可以单独需要执行的文件
'execTool.addFile("D:
\drp\DEV\db\SQLServer\common\init.sql")
'开始执行脚本到服务器execTool.execSqlScript()EndSub
ClassMySqlScriptExecTool
'属性
Publicfolds()'sql语句所在目录
Publicfiles()'文件
PubliclogoutFile'日志输出文件
Privatefso
PrivateWshShell
'PrivatetempRstFileName
PrivatecurrentFolder'当前目录
PrivateDBserver()'指定数组大小则后面不能该大小了,要改大小的时候报错:
该数组为定长的或临时被锁定
PrivatedatabaseName()
Privateusername()
Privatepassword()
'初始化
PrivateSubClass_Initialize
ReDimfolds(0)
ReDimfiles(0)
ReDimDBserver(0)
ReDimdatabaseName(0)
ReDimusername(0)
ReDimpassword(0)
Setfso=
Wscript.CreateObject("Scripting.FileSystemObject")
setWshShell=WScript.CreateObject("WScript.Shell")strPath=Wscript.ScriptFullNameobjFile=fso.GetFile(strPath)
currentFolder=fso.GetParentFolderName(objFile)
logoutFile=currentFolder+"\SqlScriptExecTool_Log.txt"EndSub'添加目标数据库
PublicfunctionaddDatabaseLink(DBserverip,dbname,usenme2,pwd)
length=UBound(DBserver)
DBserver(length)=DBserverip:
ReDimPreserve
DBserver(length+1)databaseName(length)=dbname:
ReDimPreserve
databaseName(length+1)
username(length)=usenme2:
ReDimPreserveusername(length+1)
password(length)=pwd:
ReDimPreservepassword(length+1)
endFunction'创建文件,存在则覆盖
PublicfunctioncreateNewFile(dscFile,msgs)
ConstForReading=1,ForWriting=2,ForAppending=8Dimf
Setf=fso.OpenTextFile(dscFile,ForWriting,True)f.Writemsgs
f.Close
EndFunction
'追加文件
PublicfunctionappendToFile(dscFile,msgs)
ConstForReading=1,ForWriting=2,ForAppending=8Dimf
Setf=fso.OpenTextFile(dscFile,ForAppending,True)f.Writemsgs
f.Close
EndFunction
'读运文本文件内容
PrivateFunctionreadTxtFile(file_path)
ConstForReading=1,ForWriting=2
DimMyFile
SetMyFile=fso.OpenTextFile(file_path,ForReading)readTxtFile=MyFile.ReadAll()
MyFile.Close
EndFunction
'添加目录,有避免重复的检查
PublicfunctionaddFold(fold)
Iffso.FolderExists(fold)=FalseThen
MsgBox"文件夹"+fold+"不存在!
"
ExitFunction
EndIf
length=UBound(folds)
Fori=0Tolength-1
Iffold=folds(i)ThenExitFunction
Next
folds(length)=fold:
ReDimPreservefolds(length+1)endFunction
'添加文件,有避免重复的检查
PublicfunctionaddFile(file)
Iffso.FileExists(file)=FalseThen
MsgBox"文件"+file+"不存在!
"
ExitFunction
EndIf
'IfisSqlScriptFile(file)=falseThenExitFunctionlength=UBound(files)
Fori=0Tolength-1
Iffile=files(i)ThenExitFunction
Next
files(length)=file:
ReDimPreservefiles(length+1)endFunction
PublicfunctionisSqlScriptFile(file)
IfLen(file)>4AndUCase(Right(file,4))=".SQL"ThenisSqlScriptFile=true
ExitFunction
EndIf
isSqlScriptFile=false
endFunction'找出目录下的sql文件
PublicfunctionsearchFiles()
length=UBound(folds)
Fori=0Tolength-1
file_path=folds(i)
'MsgBox"分析文件"+CStr(i)+"="+folds(i)Iffso.FolderExists(file_path)=trueThen
'MsgBox"存在的!
"
Dimfold
Setfold=fso.GetFolder(file_path)
'Iffold.files.Count>0Then
ForEachfldeInfold.files
IfisSqlScriptFile(flde.Path)Then'MsgBoxflde.Path'NamePathaddFileflde.Path
EndIf
Next
'EndIf
Else
MsgBox"目录"+file_path+"不存在!
"
EndIf
Next
endFunction
'找出所有目录
PublicfunctionsearchChildFolds()
'IfsearchChildFold=FalseThenExitFunctionlength=UBound(folds)+1
Fori=length-1To0Step-1
file_path=folds(i)
'MsgBox"分析文件"+CStr(i)+"="+folds(i)
Iffso.FolderExists(file_path)=trueThen
'MsgBox"存在!
"
Dimfold
Setfold=fso.GetFolder(file_path)
ListAllChildFoldsfold
EndIf
Next
endFunction
'迭代找出所有子文件夹
PublicfunctionListAllChildFolds(fold)
Iffold.SubFolders.Count=0ThenExitFunction
ForEachsubFolderInfold.SubFolders'fold.Files'将子层目录加入待处理目录
'MsgBoxsubFolder.Path
addFoldsubFolder.Path
DimchildFold
SetchildFold=fso.GetFolder(subFolder.Path)
ListAllChildFoldschildFold
NextendFunctionPrivateFunctiongetTempRstFileName(index,scriptName,serverName,databaseName)
'getTempRstFileName=currentFolder+""+CStr(index)+"@"+serverName+"@"+databaseName+"@"+scriptName+".txt"
getTempRstFileName=currentFolder+""+serverName+
"@"+databaseName+"@"+scriptName+".txt"
EndFunction
PublicfunctionexecSqlScript()
'创建日志文件
createNewFilelogoutFile,"执行sql脚本,开始于:
"+CStr(Date())+""+CStr(Time())+vbCrLf+"=============================================================="+vbCrLf
DimtmprstfilepathIdx,tmpFileName
'1.执行
tmprstfilepathIdx=0
ForsIndex=0ToUBound(DBserver)-1
Fori=0ToUBound(files)-1
tmprstfilepathIdx=tmprstfilepathIdx+1tmpFileName=getTempRstFileName(tmprstfilepathIdx,fso.GetBaseName(files(i)),DBserver(sIndex),databaseName(sIndex))
runsql="isqlw-S"+DBserver(sIndex)+"-d"+databaseName(sIndex)+"-U"+username(sIndex)+"-P"+password(sIndex)+"-i"+files(i)+"-o"+tmpFileName
'MsgBoxrunsql
WshShell.Runrunsql
WScript.Sleep1000'预留的执行空闲等待
Next
Next
'2.log合并
tmprstfilepathIdx=0
Dimnewmsg
ForsIndex=0ToUBound(DBserver)-1
Fori=0ToUBound(files)-1
tmprstfilepathIdx=tmprstfilepathIdx+1tmpFileName=getTempRstFileName(tmprstfilepathIdx,fso.GetBaseName(files(i)),DBserver(sIndex),databaseName(sIndex))
DimrunRst
runRst=readTxtFile(tmpFileName)
WScript.Sleep450
newmsg=""
Ifi<>0AndsIndex<>0Then
newmsg=vbCrLf+vbCrLf
EndIf
newmsg=newmsg+"执行序号:
"+
CStr(tmprstfilepathIdx)
newmsg=newmsg+vbCrLf+"目标:
databaseName(sIndex)+"@"+DBserver(sIndex)
newmsg=newmsg+vbCrLf+"执行脚本:
fso.GetBaseName(files(i))+".sql"
+vbCrLf+
newmsg=newmsg+vbCrLf+"执行结果:
runRst+vbCrLf
appendToFilelogoutFile,newmsg
WScript.Sleep500fso.DeleteFile(tmpFileName)
Next
Next
'日志记录结束时间appendToFilelogoutFile,vbCrLf+
"+CStr(Date())+""+CStr(Time())
'用记事本打开日志文件
WshShell.Run"notepad"+logoutFileendFunction
PublicfunctionshowFolds()
Fori=0ToUBound(folds)-1
MsgBoxCStr(i)+"="+folds(i)
Next
endFunction
PublicfunctionshowFiles()
Fori=0ToUBound(files)-1
MsgBoxCStr(i)+"="+files(i)
Next
endfunction
EndClasstodo
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- vbs sql 脚本 执行 工具