2010-08-15 8 views
5

मैं एसएसएमएस 2008 में स्क्रिप्ट पीढ़ी (एसएसएमएस -> कार्य -> ​​स्क्रिप्ट जेनरेट) में स्वचालित करना चाहता हूं। मैंने पढ़ा है कि SQL Server 2008 डेटाबेस प्रकाशन विज़ार्ड (SQLPUBWIZ SCRIPT सहित) का समर्थन नहीं करता है लेकिन यह स्वचालन किया जा सकता है एसक्यूएल सर्वर 2008 में एसएमओ का उपयोग करना। मुझे एसएमओ के बारे में कोई जानकारी नहीं है और एसएमओ का उपयोग करके इसे कैसे किया जाए, तो क्या आप मुझे कुछ सलाह (संसाधन इत्यादि) कैसे शुरू कर सकते हैं?एसक्यूएल सर्वर में एसएमओ का उपयोग कर स्क्रिप्ट पीढ़ी को स्वचालित कैसे करें?

+0

की [स्वचालित स्क्रिप्ट SQL2008 में जादूगर उत्पन्न] (http://stackoverflow.com/questions/3380195/automate-generate-scripts-wizard-in-sql2008) या यह भी संभव http डुप्लिकेट: // stackoverflow .com/प्रश्न/3384649 – gbn

उत्तर

10

एसएमओ स्क्रिप्टिंग की कुंजी Scripter कक्षा है। अन्य सभी टूल्स (जैसे एसएसएमएस) इस वर्ग का उपयोग जेनरेट ऑब्जेक्ट सृजन स्क्रिप्ट पर करते हैं।

{ 
    //Connect to the local, default instance of SQL Server. 
    Server srv = new Server(); 

    //Reference the AdventureWorks2008R2 database. 
    Database db = srv.Databases["AdventureWorks2008R2"]; 

    //Define a Scripter object and set the required scripting options. 
    Scripter scrp = new Scripter(srv); 
    scrp.Options.ScriptDrops = false; 
    scrp.Options.WithDependencies = true; 

    //Iterate through the tables in database and script each one. Display the script. 
    //Note that the StringCollection type needs the System.Collections.Specialized namespace to be included. 
    Microsoft.SqlServer.Management.Sdk.Sfc.Urn[] smoObjects = new Microsoft.SqlServer.Management.Sdk.Sfc.Urn[1] ; 
    foreach (Table tb in db.Tables) { 
     smoObjects[0] = tb.Urn; 
     if (tb.IsSystemObject == false) { 
     System.Collections.Specialized.StringCollection sc; 
     sc = scrp.Script(smoObjects); 
     foreach (string st in sc) { 
      Console.WriteLine(st); 
     } 
     } 
    } 
} 
4

डेटाबेस में सभी वस्तुओं को स्क्रिप्ट के कुछ तरीके हैं, लेकिन सबसे आसान में से एक आप के लिए एसएमओ tranfer वर्ग है: वहाँ MSDN पर एक उदाहरण उपयोग है। यहाँ स्क्रिप्ट बाहर करने के लिए कुछ PowerShell कोड सभी वस्तुओं है:

add-type -AssemblyName "Microsoft.SqlServer.ConnectionInfo, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" 
add-type -AssemblyName "Microsoft.SqlServer.Smo, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" 
add-type -AssemblyName "Microsoft.SqlServer.SMOExtended, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" 

$sourceSrv = "$env:computername\sql2k8" 
$sourceDb = "Northwind" 

$server = new-object ("Microsoft.SqlServer.Management.Smo.Server") $sourceSrv 
$db = $server.Databases[$sourceDb] 

$transfer = new-object ("Microsoft.SqlServer.Management.Smo.Transfer") $db 
$transfer.CopyAllObjects = $true 
$transfer.DropDestinationObjectsFirst = $true 
$transfer.CopySchema = $true 
$transfer.Options.IncludeIfNotExists = $true 

$transfer.ScriptTransfer() 
7

हालांकि सवाल सही ढंग से जवाब दिया गया है, मैं कुछ दिनों के एक साथ एक स्क्रिप्ट है कि सभी वस्तुओं को मैं एक डाटाबेस सर्वर पर के बारे में परवाह बाहर पटकथा डाल करने के लिए संघर्ष किया। यहां मेरा कोड है अगर यह किसी बिंदु पर किसी और के लिए उपयोगी है।

# Load needed assemblies 
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") | out-null 
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMOExtended")| Out-Null; 

#Specify target server and databases. 
$sql_server = "SomeServerName" 
$SMOserver = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Server -ArgumentList "$sql_server" 
$databases = $SMOserver.Databases 
$BaseSavePath = "T:\SomeFilePath\" + $sql_server + "\" 

#Remove existing objects. 
Remove-Item $BaseSavePath -Recurse 

#Script server-level objects. 
$ServerSavePath = $BaseSavePath 
$ServerObjects = $SMOserver.BackupDevices 
$ServerObjects += $SMOserver.Endpoints 
$ServerObjects += $SMOserver.JobServer.Jobs 
$ServerObjects += $SMOserver.LinkedServers 
$ServerObjects += $SMOserver.Triggers 

foreach ($ScriptThis in $ServerObjects | where {!($_.IsSystemObject)}) 
{ 
    #Need to Add Some mkDirs for the different $Fldr=$ScriptThis.GetType().Name 
    $scriptr = new-object ('Microsoft.SqlServer.Management.Smo.Scripter') ($SMOserver) 
    $scriptr.Options.AppendToFile = $True 
    $scriptr.Options.AllowSystemObjects = $False 
    $scriptr.Options.ClusteredIndexes = $True 
    $scriptr.Options.DriAll = $True 
    $scriptr.Options.ScriptDrops = $False 
    $scriptr.Options.IncludeHeaders = $False 
    $scriptr.Options.ToFileOnly = $True 
    $scriptr.Options.Indexes = $True 
    $scriptr.Options.Permissions = $True 
    $scriptr.Options.WithDependencies = $False 

    <#Script the Drop too#> 
    $ScriptDrop = new-object ('Microsoft.SqlServer.Management.Smo.Scripter') ($SMOserver) 
    $ScriptDrop.Options.AppendToFile = $True 
    $ScriptDrop.Options.AllowSystemObjects = $False 
    $ScriptDrop.Options.ClusteredIndexes = $True 
    $ScriptDrop.Options.DriAll = $True 
    $ScriptDrop.Options.ScriptDrops = $True 
    $ScriptDrop.Options.IncludeHeaders = $False 
    $ScriptDrop.Options.ToFileOnly = $True 
    $ScriptDrop.Options.Indexes = $True 
    $ScriptDrop.Options.WithDependencies = $False 

    <#This section builds folder structures. Remove the date folder if you want to overwrite#> 
    $TypeFolder=$ScriptThis.GetType().Name 
    if ((Test-Path -Path "$ServerSavePath\$TypeFolder") -eq "true") ` 
      {"Scripting Out $TypeFolder $ScriptThis"} ` 
     else {new-item -type directory -name "$TypeFolder"-path "$ServerSavePath"} 
    $ScriptFile = $ScriptThis -replace ":", "-" -replace "\\", "-" 
    $ScriptDrop.Options.FileName = $ServerSavePath + "\" + $TypeFolder + "\" + $ScriptFile.Replace("]", "").Replace("[", "") + ".sql" 
    $scriptr.Options.FileName = $ServerSavePath + "\" + $TypeFolder + "\" + $ScriptFile.Replace("]", "").Replace("[", "") + ".sql" 

    #This is where each object actually gets scripted one at a time. 
    $ScriptDrop.Script($ScriptThis) 
    $scriptr.Script($ScriptThis) 
} #This ends the object scripting loop at the server level. 


#Script database-level objects. 
foreach ($db in $databases) 
{ 
    $DatabaseObjects = $db.ApplicationRoles 
    $DatabaseObjects += $db.Assemblies 
    $DatabaseObjects += $db.ExtendedStoredProcedures 
    $DatabaseObjects += $db.ExtendedProperties 
    $DatabaseObjects += $db.PartitionFunctions 
    $DatabaseObjects += $db.PartitionSchemes 
    $DatabaseObjects += $db.Roles 
    $DatabaseObjects += $db.Rules 
    $DatabaseObjects += $db.Schemas 
    $DatabaseObjects += $db.StoredProcedures 
    $DatabaseObjects += $db.Synonyms 
    $DatabaseObjects += $db.Tables 
    $DatabaseObjects += $db.Triggers 
    $DatabaseObjects += $db.UserDefinedAggregates 
    $DatabaseObjects += $db.UserDefinedDataTypes 
    $DatabaseObjects += $db.UserDefinedFunctions 
    $DatabaseObjects += $db.UserDefinedTableTypes 
    $DatabaseObjects += $db.UserDefinedTypes 
    $DatabaseObjects += $db.Users 
    $DatabaseObjects += $db.Views 

    #Build this portion of the directory structure out here. Remove the existing directory and its contents first. 
    $DatabaseSavePath = $BaseSavePath + "Databases\" + $db.Name 

    new-item -type directory -path "$DatabaseSavePath" 

    foreach ($ScriptThis in $DatabaseObjects | where {!($_.IsSystemObject)}) 
    { 
     #Need to Add Some mkDirs for the different $Fldr=$ScriptThis.GetType().Name 
     $scriptr = new-object ('Microsoft.SqlServer.Management.Smo.Scripter') ($SMOserver) 
     $scriptr.Options.AppendToFile = $True 
     $scriptr.Options.AllowSystemObjects = $False 
     $scriptr.Options.ClusteredIndexes = $True 
     $scriptr.Options.DriAll = $True 
     $scriptr.Options.ScriptDrops = $False 
     $scriptr.Options.IncludeHeaders = $False 
     $scriptr.Options.ToFileOnly = $True 
     $scriptr.Options.Indexes = $True 
     $scriptr.Options.Permissions = $True 
     $scriptr.Options.WithDependencies = $False 

     <#Script the Drop too#> 
     $ScriptDrop = new-object ('Microsoft.SqlServer.Management.Smo.Scripter') ($SMOserver) 
     $ScriptDrop.Options.AppendToFile = $True 
     $ScriptDrop.Options.AllowSystemObjects = $False 
     $ScriptDrop.Options.ClusteredIndexes = $True 
     $ScriptDrop.Options.DriAll = $True 
     $ScriptDrop.Options.ScriptDrops = $True 
     $ScriptDrop.Options.IncludeHeaders = $False 
     $ScriptDrop.Options.ToFileOnly = $True 
     $ScriptDrop.Options.Indexes = $True 
     $ScriptDrop.Options.WithDependencies = $False 

     <#This section builds folder structures. Remove the date folder if you want to overwrite#> 
     $TypeFolder=$ScriptThis.GetType().Name 
     if ((Test-Path -Path "$DatabaseSavePath\$TypeFolder") -eq "true") ` 
       {"Scripting Out $TypeFolder $ScriptThis"} ` 
      else {new-item -type directory -name "$TypeFolder"-path "$DatabaseSavePath"} 
     $ScriptFile = $ScriptThis -replace ":", "-" -replace "\\", "-" 
     $ScriptDrop.Options.FileName = $DatabaseSavePath + "\" + $TypeFolder + "\" + $ScriptFile.Replace("]", "").Replace("[", "") + ".sql" 
     $scriptr.Options.FileName = $DatabaseSavePath + "\" + $TypeFolder + "\" + $ScriptFile.Replace("]", "").Replace("[", "") + ".sql" 

     #This is where each object actually gets scripted one at a time. 
     $ScriptDrop.Script($ScriptThis) 
     $scriptr.Script($ScriptThis) 

    } #This ends the object scripting loop. 
} #This ends the database loop. 
संबंधित मुद्दे