동의어 사용

적용 대상:SQL ServerAzure SQL DatabaseAzure SQL Managed InstanceAzure Synapse Analytics

동의어는 스키마 범위 개체의 대체 이름입니다. SMO에서 동의어는 개체로 Synonym 표시됩니다. Synonym 개체는 Database 개체의 자식입니다. 즉, 동의어는 정의된 데이터베이스 범위 내에서만 유효합니다. 그러나 동의어는 다른 데이터베이스 또는 SQL Server의 원격 인스턴스에 있는 개체를 참조할 수 있습니다.

대체 이름이 지정된 개체를 기본 개체라고 합니다. 개체의 Synonym 이름 속성은 기본 개체에 지정된 대체 이름입니다.

다음 코드 예제를 사용하려면 애플리케이션을 만들 프로그래밍 환경, 프로그래밍 템플릿 및 프로그래밍 언어를 선택해야 합니다. 자세한 내용은 Visual Studio .NET에서 Visual C# SMO 프로젝트 만들기를 참조하세요.

Visual C에서 동의어 만들기#

코드 예제에서는 스키마 범위 개체의 동의어 또는 대체 이름을 만드는 방법을 보여 있습니다. 클라이언트 애플리케이션은 여러 파트 이름을 사용하여 기본 개체를 참조하는 대신 동의어를 통해 기본 개체에 대한 단일 참조를 사용할 수 있습니다.

{  
            //Connect to the local, default instance of SQL Server.   
            Server srv = new Server();  
  
            //Reference the AdventureWorks2022 database.   
            Database db = srv.Databases["AdventureWorks2022"];  
  
            //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 = "AdventureWorks2022";  
            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["AdventureWorks2022"]  
  
$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 = "AdventureWorks2022"  
$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)