您是否可以使用python中的Microsoft.SqlServer.Management.Smo.Scripter URN对象,或者是否有与之等效的对象?

2024-06-28 20:28:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我的任务是将powershell脚本转换为python,除此之外,我已经完成了所有的工作。该脚本使用Microsoft.SqlServer.Management.Smo.Scripter类的URN对象创建droptable和createtable.sql。有没有办法在python中模仿这一点? 动力壳

$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 = "$bcpOutputDir\schema\DropTables.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 = "$bcpOutputDir\schema\CreateTables.sql"
        $createscript.Options.AppendToFile = $True
        $dropscript.Script($allUrns) + "GO `r" | Out-String
        $createscript.Script($allUrns) + "GO `r" | Out-String

Tags: 脚本truesqlschemamanagementmicrosoftoptionsscripter