次の方法で共有


シノニムの使用

シノニムは、スキーマ スコープ オブジェクトの別名です。 SMO では、シノニムは Synonym オブジェクトで表現します。 Synonym オブジェクトは、Database オブジェクトの子です。 これは、シノニムは、そのシノニムが定義されているデータベースのスコープ内でのみ有効であることを意味しています。 ただし、シノニムは、別のデータベース上または SQL Server のリモート インスタンス上のオブジェクトを参照することができます。

別名を持つオブジェクトは、ベース オブジェクトと呼ばれます。 Synonym オブジェクトの名前プロパティは、ベース オブジェクトの別名です。

次のコード例では、アプリケーションを作成するプログラミング環境、プログラミング テンプレート、およびプログラミング言語を選択する必要があります。 詳細については、「Visual Studio .NET での Visual Basic SMO プロジェクトの作成」および「Visual Studio .NET での Visual C# SMO プロジェクトの作成」を参照してください。

Visual Basic でのシノニムの作成

このコード例では、シノニム、またはスキーマ スコープ オブジェクトの別名を作成する方法を示します。 クライアント アプリケーションは、マルチパート名を使用してベース オブジェクトを参照するのではなく、シノニムを使用することによって、ベース オブジェクトの単一参照を使用することができます。

'Connect to the local, default instance of SQL Server.
Dim srv As Server
srv = New Server()
'Reference the AdventureWorks2012 2008R2 database.
Dim db As 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.
Dim syn As 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()

Visual C# でのシノニムの作成

このコード例では、シノニム、またはスキーマ スコープ オブジェクトの別名を作成する方法を示します。 クライアント アプリケーションは、マルチパート名を使用してベース オブジェクトを参照するのではなく、シノニムを使用することによって、ベース オブジェクトの単一参照を使用することができます。

{
            //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 でのシノニムの作成

このコード例では、シノニム、またはスキーマ スコープ オブジェクトの別名を作成する方法を示します。 クライアント アプリケーションは、マルチパート名を使用してベース オブジェクトを参照するのではなく、シノニムを使用することによって、ベース オブジェクトの単一参照を使用することができます。

#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()

関連項目

参照

CREATE SYNONYM (Transact-SQL)