シノニムは、スキーマ スコープ オブジェクトの代替名です。 SMO では、シノニムは Synonym オブジェクトによって表されます。 Synonym オブジェクトは、Database オブジェクトの子です。 これは、シノニムが定義されているデータベースのスコープ内でのみ有効であることを意味します。 ただし、シノニムは、別のデータベースまたは SQL Server のリモート インスタンス上のオブジェクトを参照できます。
別名が付けられたオブジェクトは、基本オブジェクトと呼ばれます。 Synonym オブジェクトの name プロパティは、基本オブジェクトに指定された代替名です。
例
次のコード例では、アプリケーションを作成するために、プログラミング環境、プログラミング テンプレート、およびプログラミング言語を選択する必要があります。 詳細については、「 Visual Studio .NET で Visual Basic SMO プロジェクトを作成する」および「Visual Studio .NETで Visual C# SMO プロジェクトを作成する」を参照してください。
Visual Basic でのシノニムの作成
このコード例では、スキーマ スコープ オブジェクトのシノニムまたは代替名を作成する方法を示します。 クライアント アプリケーションでは、複数のパーツ名を使用してベース オブジェクトを参照する代わりに、シノニムを介してベース オブジェクトに対して 1 つの参照を使用できます。
Visual C でのシノニムの作成#
このコード例では、スキーマ スコープ オブジェクトのシノニムまたは代替名を作成する方法を示します。 クライアント アプリケーションでは、複数のパーツ名を使用してベース オブジェクトを参照する代わりに、シノニムを介してベース オブジェクトに対して 1 つの参照を使用できます。
{
//Connect to the local, default instance of SQL Server.
Server srv = new Server();
//Reference the AdventureWorks2012 database.
Database db = srv.Databases["AdventureWorks2012"];
//Define a Synonym object variable by supplying the
//parent database, name, and schema arguments in the constructor.
//The name is also a synonym of the name of the base object.
Synonym syn = new Synonym(db, "Shop", "Sales");
//Specify the base object, which is the object on which
//the synonym is based.
syn.BaseDatabase = "AdventureWorks2012";
syn.BaseSchema = "Sales";
syn.BaseObject = "Store";
syn.BaseServer = srv.Name;
//Create the synonym on the instance of SQL Server.
syn.Create();
}
PowerShell でのシノニムの作成
このコード例では、スキーマ スコープ オブジェクトのシノニムまたは代替名を作成する方法を示します。 クライアント アプリケーションでは、複数のパーツ名を使用してベース オブジェクトを参照する代わりに、シノニムを介してベース オブジェクトに対して 1 つの参照を使用できます。
#Get a server object which corresponds to the default instance
$srv = New-Object -TypeName Microsoft.SqlServer.Management.SMO.Server
#And the database object corresponding to Adventureworks
$db = $srv.Databases["AdventureWorks2012"]
$syn = New-Object -TypeName Microsoft.SqlServer.Management.SMO.Synonym -ArgumentList $db, "Shop", "Sales"
#Specify the base object, which is the object on which the synonym is based.
$syn.BaseDatabase = "AdventureWorks2012"
$syn.BaseSchema = "Sales"
$syn.BaseObject = "Store"
$syn.BaseServer = $srv.Name
#Create the synonym on the instance of SQL Server.
$syn.Create()