반응형
반응형



'***************************************************************************************************
' SQL DMO, File SystemObject, shell을 이용 Database Object별 Secript 생성
' Argument 설명
' arg 0 : saveRoot dir(local 또는 Unc)
' arg 1 : database Login Id
' arg 2 : database Login Pwd
' arg 3 : server name, server IP
' arg 4 : scripting을 할 데이터베이스 네임
'
' Run Example ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
' prompt:\>cscript script.vbs  "c:\script\" "<id>" "<pwd>" "<Server>" "<databasename>"
' 확인 : c:\script
'***************************************************************************************************
'변수들에 대한 개체 정의
Dim objArgs, objSql, fso, WshShell, objDb, dbName
Dim ScriptDb, saveDir
Set objSQL = CreateObject("SQLDMO.SQLServer")   
Set fso = CreateObject ("Scripting.FileSystemObject")
Set WshShell = CreateObject("WScript.Shell")
Set objArgs = Wscript.Arguments
saveDir = objArgs(0)
objSQL.LoginSecure = False 'Trust된 곳이라면 당연히 True가 되면서 Login pwd불필요
objSQL.Login = objArgs(1)
objSQL.Password = objArgs(2)
'Server Connect
objSQL.Connect objArgs(3)
Set objDB = objSQL.Databases
If Not(fso.FolderExists(saveDir)) Then
    createFolder saveDir
End If
scriptDb = objArgs(4)
If NOT(fso.FolderExists(saveDir & scriptDb)) Then
    createFolder saveDir & scriptDb
End If
''''여기서 부터 개체 콜하기 시작
For each dbName in objDB
   If LCase(dbName.name) = LCase(scriptDb) Then
        
     ScriptOut dbName.tables, "Tables"    
    
     ScriptOut dbName.StoredProcedures, "StoredProcs"
     ScriptOut dbName.Views, "Views"
     ScriptOut dbName.Users, "Users"
     ScriptOut dbName.Rules, "Rules"
     ScriptOut dbName.UserDefinedDatatypes, "UserDefDataType"
     ScriptOut dbName.UserDefinedFunctions, "UserDefFunc" 
   End If
Next
''''이놈은 파일을 만들 sub proc
Sub ScriptOut(object, foldername)
    For each item in object
        If item.SystemObject = False Then
            createFolder saveDir & scriptDb & "\" & foldername     
            sFileName = saveDir & scriptDb & "\" & foldername & "\" & Replace(item.Name, "\", "-") & ".sql"
           ' MsgBox item.Script
            If IsNull(item.script) Or IsEmpty(item.script) Then
           Else  
            If IsNull(sFileName) Or IsEmpty(sFileName) Then
            Else
             item.Script 4, sFileName
            End If
           End If
        End If
    Next
End Sub
''''이놈은 폴더를 만들  sub proc 'fso의 약간의 버그로 인한..
Sub createFolder(fName)
    If NOT(fso.FolderExists(fName)) Then
        WshShell.Run "cmd /c md " & fName     
        WScript.Sleep 1000
    End If 
End Sub
반응형

+ Recent posts

반응형