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
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…