適用対象:SQL Server
Azure SQL Database
Azure SQL Managed Instance
Azure Synapse Analytics
Microsoft Fabric プレビューの SQL データベース
SMO でのスクリプトは、 Scripter オブジェクトとその子オブジェクト、または個々のオブジェクトの Script メソッドによって制御されます。 Scripter オブジェクトは、Microsoft SQL Server のインスタンス上のオブジェクトの依存関係からマッピングを制御します。
Scripter オブジェクト、およびその子オブジェクトを使用する高度なスクリプティング プロセスには、次の 3 つのフェーズがあります。
Discovery
List generation
Script generation
検索フェーズでは、DependencyWalker オブジェクトが使用されます。 オブジェクトの URN リストが指定されている場合、DiscoverDependencies オブジェクトの DependencyWalker メソッドは、URN リスト内のオブジェクトに対応する DependencyTree オブジェクトを返します。 Boolean fParents パラメーターは、指定したオブジェクトの親と子のどちらを検出するかを選択するために使用されます。 依存関係ツリーはこの段階で変更することができます。
リスト生成フェーズでは、このツリーが渡され、結果リストが返されます。 このオブジェクト リストは記述順であり、変更することもできます。
リスト生成フェーズでは、WalkDependencies メソッドを使用して DependencyTree を返します。 DependencyTree はこの段階で変更することができます。
3 番目の最後のフェーズでは、指定されたリストとスクリプティング オプションを使用してスクリプトが生成されます。 結果は StringCollection システム オブジェクトとして返されます。 このフェーズで、DependencyTree オブジェクトの Items コレクションおよび NumberOfSiblings や FirstChild などのプロパティから、依存オブジェクト名が抽出されます。
Example
提供されているコード例を使用するには、アプリケーションを作成するプログラミング環境、プログラミング テンプレート、およびプログラミング言語を選択する必要があります。 詳細については、「 Visual Studio .NET で Visual C# SMO プロジェクトを作成するを参照してください。
このコード例では、System.Collections.Specialized 名前空間に Imports ステートメントが必要です。 アプリケーションの宣言の前、かつ他の Imports ステートメントの後に、次のステートメントを挿入します。
Imports Microsoft.SqlServer.Management.Smo
Imports Microsoft.SqlServer.Management.Common
Imports System.Collections.Specialized
Visual Basic でデータベースの依存関係のスクリプトを作成する
このコード例では、依存関係を検出する方法と、リストを反復処理して結果を表示する方法を示します。
' compile with:
' /r:Microsoft.SqlServer.Smo.dll
' /r:Microsoft.SqlServer.ConnectionInfo.dll
' /r:Microsoft.SqlServer.Management.Sdk.Sfc.dll
Imports Microsoft.SqlServer.Management.Smo
Imports Microsoft.SqlServer.Management.Sdk.Sfc
Public Class A
Public Shared Sub Main()
' database name
Dim dbName As [String] = "AdventureWorksLT2012" ' database name
' Connect to the local, default instance of SQL Server.
Dim srv As New Server()
' Reference the database.
Dim db As Database = srv.Databases(dbName)
' Define a Scripter object and set the required scripting options.
Dim scrp As New Scripter(srv)
scrp.Options.ScriptDrops = False
scrp.Options.WithDependencies = True
scrp.Options.Indexes = True ' To include indexes
scrp.Options.DriAllConstraints = True ' to include referential constraints in the script
' Iterate through the tables in database and script each one. Display the script.
For Each tb As Table In db.Tables
' check if the table is not a system table
If tb.IsSystemObject = False Then
Console.WriteLine("-- Scripting for table " + tb.Name)
' Generating script for table tb
Dim sc As System.Collections.Specialized.StringCollection = scrp.Script(New Urn() {tb.Urn})
For Each st As String In sc
Console.WriteLine(st)
Next
Console.WriteLine("--")
End If
Next
End Sub
End Class
Visual C# でデータベースの依存関係のスクリプトを作成する
このコード例では、依存関係を検出する方法と、リストを反復処理して結果を表示する方法を示します。
// compile with:
// /r:Microsoft.SqlServer.Smo.dll
// /r:Microsoft.SqlServer.ConnectionInfo.dll
// /r:Microsoft.SqlServer.Management.Sdk.Sfc.dll
using System;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Sdk.Sfc;
public class A {
public static void Main() {
String dbName = "AdventureWorksLT2012"; // database name
// Connect to the local, default instance of SQL Server.
Server srv = new Server();
// Reference the database.
Database db = srv.Databases[dbName];
// Define a Scripter object and set the required scripting options.
Scripter scrp = new Scripter(srv);
scrp.Options.ScriptDrops = false;
scrp.Options.WithDependencies = true;
scrp.Options.Indexes = true; // To include indexes
scrp.Options.DriAllConstraints = true; // to include referential constraints in the script
// Iterate through the tables in database and script each one. Display the script.
foreach (Table tb in db.Tables) {
// check if the table is not a system table
if (tb.IsSystemObject == false) {
Console.WriteLine("-- Scripting for table " + tb.Name);
// Generating script for table tb
System.Collections.Specialized.StringCollection sc = scrp.Script(new Urn[]{tb.Urn});
foreach (string st in sc) {
Console.WriteLine(st);
}
Console.WriteLine("--");
}
}
}
}
PowerShell でデータベースの依存関係のスクリプトを作成する
このコード例では、依存関係を検出する方法と、リストを反復処理して結果を表示する方法を示します。
# Set the path context to the local, default instance of SQL Server.
CD \sql\localhost\default
# Create a Scripter object and set the required scripting options.
$scrp = New-Object -TypeName Microsoft.SqlServer.Management.SMO.Scripter -ArgumentList (Get-Item .)
$scrp.Options.ScriptDrops = $false
$scrp.Options.WithDependencies = $true
$scrp.Options.IncludeIfNotExists = $true
# Set the path context to the tables in AdventureWorks2022.
CD Databases\AdventureWorks2022\Tables
foreach ($Item in Get-ChildItem)
{
$scrp.Script($Item)
}