Я использую следующий сценарий powershell, чтобы сгенерировать сценарий базы данных и импортировать его в файл. Я использую библиотеку Microsoft.SqlServer.SMO.
function generate_data_base_script
{
param([string]$DirectoryToSaveTo,
[string]$servername,
[string]$Database,
[string]$SQLUsername,
[string]$SQLPassword,
[string]$FileName_Specification='')
set-psdebug -strict
$ErrorActionPreference = "stop" # you can opt to stagger on, bleeding, if an error occurs
Trap {
# Handle the error
$err = $_.Exception
write-host $err.Message
while( $err.InnerException ) {
$err = $err.InnerException
write-host $err.Message
};
# End the script.
break
}
# Load SMO assembly, and if we're running SQL 2008 DLLs load the SMOExtended and SQLWMIManagement libraries
$v = [System.Reflection.Assembly]::LoadWithPartialName( 'Microsoft.SqlServer.SMO')
$c = [System.Reflection.Assembly]::LoadWithPartialName( 'Microsoft.SqlServer.ConnectionInfo')
if ((($v.FullName.Split(','))[1].Split('='))[1].Split('.')[0] -ne '9') {
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMOExtended') | out-null
}
$My='Microsoft.SqlServer.Management.Smo'
$ServerConnection = new-object Microsoft.SqlServer.Management.Common.ServerConnection ($ServerName , $SQLUsername, $SQLPassword)
$s = new-object ("$My.Server") $ServerConnection
$Server=$s.netname -replace '[\\\/\:\.]',' ' # remove characters that can cause problems
$instance = $s.instanceName -replace '[\\\/\:\.]',' ' # ditto
$DatabaseName =$database -replace '[\\\/\:\.]',' ' # ditto
if (!( Test-Path -path "$DirectoryToSaveTo" )) # create it if not existing
{$progress ="attempting to create directory $DirectoryToSaveTo"
Try { New-Item "$DirectoryToSaveTo" -type directory | out-null }
Catch [system.exception]{
Write-Error "error while $progress. $_"
return
}
}
<# now we will use the canteen system of SMO to specify what we want from the script. It is best to have a list of the defaults to hand and just override the defaults where necessary, but there is a chance that a later revision of SMO could change the defaults, so beware! #>
$CreationScriptOptions = new-object ("$My.ScriptingOptions")
$CreationScriptOptions.ExtendedProperties= $true # yes, we want these
$CreationScriptOptions.DRIAll= $true # and all the constraints
$CreationScriptOptions.Indexes= $true # Yup, these would be nice
$CreationScriptOptions.Triggers= $true # This should be included when scripting a database
$CreationScriptOptions.ScriptBatchTerminator = $true # this only goes to the file
$CreationScriptOptions.Filename = "$($DirectoryToSaveTo)\$($DatabaseName)$($FileName_Specification).sql";
# we have to write to a file to get the GOs
$CreationScriptOptions.IncludeHeaders = $true; # of course
$CreationScriptOptions.ToFileOnly = $true # no need of string output as well
#$CreationScriptOptions.IncludeIfNotExists = $true # not necessary but it means the script can be more versatile
$transfer = new-object ("$My.Transfer") $s.Databases[$Database]
$transfer.options=$CreationScriptOptions # tell the transfer object of our preferences
$scripter = new-object ("$My.Scripter") $s # script out the database creation
$scripter.options=$CreationScriptOptions # with the same options
$scripter.Script($s.Databases[$Database]) # do it
"USE $Database" | Out-File -Append -FilePath "$($DirectoryToSaveTo)\$($DatabaseName)$($FileName_Specification).sql"
"GO" | Out-File -Append -FilePath "$($DirectoryToSaveTo)\$($DatabaseName)$($FileName_Specification).sql"
# add the database object build script
$transfer.options.AppendToFile=$true
#$transfer.options.ScriptDrops=$true
$transfer.options.NoCollation=$true
$transfer.options.IncludeDatabaseRoleMemberships=$true
$transfer.options.SchemaQualify=$true
$transfer.options.ScriptSchema=$true
$transfer.options.SchemaQualifyForeignKeysReferences=$true
$transfer.EnumScriptTransfer()
"All written to $($DirectoryToSaveTo)\$($DatabaseName)$($FileName_Specification).sql"
}
$Destination_Folder_Partially_Databases = "C:\Users\djin\Documents\Backups\DATABASE_BACK_UPS\$((Get-Date).ToString('yyyyMMdd'))"
generate_data_base_script -DirectoryToSaveTo $Destination_Folder_Partially_Databases -servername 'some ip' -Database 'some database' -SQLUsername 'some username' -SQLPassword 'some password' -FileName_Specification 'some filename specification'
Я установил для параметра SchemaQualify значение true. Поэтому все объекты базы данных, имеющие схему, должны иметь префикс схемы в имени.
Но когда я просматриваю файл, я вижу следующее Отсутствие префикса схемы
Все другие объекты, имеющие схему, имеют префикс схемы в имени.
Это дефект библиотеки 'Microsoft.SqlServer.SMO' или как? Спасибо!