Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
492 views
in Technique[技术] by (71.8m points)

sql server - Can you use or is there an equivalent to the Microsoft.SqlServer.Management.Smo.Scripter URN object in python?

I was tasked with converting a powershell script into python, and i have all of it complete except this part. The script creates a droptable and createtable .sql using an URN object of the Microsoft.SqlServer.Management.Smo.Scripter class. Is there a way to mimic this in python? Powershell

$allTablesCollection = $mySQLSMOConnection.Databases[$Database].Tables
    foreach ($table in $ExtractTables) {
        $foundTable = $false
        foreach ($allTable in $allTablesCollection) {
            if ($table -eq $allTable.Name) {
                [Array]$allUrns += $allTable.Urn
                $tableHash = @{
                    "dbName" = $allTable.Parent
                    "schema" = $allTable.Schema
                    "tableName" = $allTable.Name
                }
                [Array]$allTables += $tableHash
                $foundTable = $true
            } 
        }
        $dropscript = new-object("Microsoft.SqlServer.Management.Smo.Scripter")
        $dropscript.Server=$mySQLSMOConnection
        $dropscript.Options.ScriptDrops = $True
        $dropscript.Options.IncludeIfNotExists = $True
        $dropscript.Options.FileName = "$bcpOutputDirschemaDropTables.sql"
        $dropscript.Options.AppendToFile = $True
        $createscript = new-object("Microsoft.SqlServer.Management.Smo.Scripter")
        $createscript.Server=$mySQLSMOConnection
        $createscript.Options.NoIdentities = $True
        $createscript.Options.ScriptDrops = $False
        $createscript.Options.NoFileGroup = $true
        $createscript.Options.IncludeIfNotExists = $False
        $createscript.Options.FileName = "$bcpOutputDirschemaCreateTables.sql"
        $createscript.Options.AppendToFile = $True
        $dropscript.Script($allUrns) + "GO `r" | Out-String
        $createscript.Script($allUrns) + "GO `r" | Out-String

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...