뷰 생성, 변경 및 제거

SMO(SQL Server Management Objects)에서 SQL Server 뷰는 View 개체로 표시됩니다.

View 개체의 TextBody 속성이 뷰를 정의합니다. 이 속성은 뷰를 만드는 Transact-SQL SELECT 문에 해당합니다.

제공된 코드 예제를 사용하려면 응용 프로그램을 만들 프로그래밍 환경, 프로그래밍 템플릿 및 프로그래밍 언어를 선택해야 합니다. 자세한 내용은 Visual Studio .NET에서 Visual Basic SMO 프로젝트 만들기 또는 Visual Studio .NET에서 Visual C# SMO 프로젝트 만들기를 참조하십시오.

Visual Basic에서 뷰 생성, 변경 및 제거

이 코드 예제는 내부 조인을 사용하여 두 테이블을 조인한 뷰를 만드는 방법을 보여 줍니다. 텍스트 모드를 사용하여 뷰를 작성하므로 TextHeader 속성을 설정해야 합니다.

'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 View object variable by supplying the parent database, view name and schema in the constructor.
Dim myview As View
myview = New View(db, "Test_View", "Sales")
'Set the TextHeader and TextBody property to define the view.
myview.TextHeader = "CREATE VIEW [Sales].[Test_View] AS"
myview.TextBody = "SELECT h.SalesOrderID, d.OrderQty FROM Sales.SalesOrderHeader AS h INNER JOIN Sales.SalesOrderDetail AS d ON h.SalesOrderID = d.SalesOrderID"
'Create the view on the instance of SQL Server.
myview.Create()
'Remove the view.
myview.Drop()

Visual C#에서 뷰 생성, 변경 및 제거

이 코드 예제는 내부 조인을 사용하여 두 테이블을 조인한 뷰를 만드는 방법을 보여 줍니다. 텍스트 모드를 사용하여 뷰를 작성하므로 TextHeader 속성을 설정해야 합니다.

{
        //Connect to the local, default instance of SQL Server. 
        Server srv; 
        srv = new Server(); 
        //Reference the AdventureWorks2012 database. 
        Database db; 
        db = srv.Databases["AdventureWorks2012"]; 
        //Define a View object variable by supplying the parent database, view name and schema in the constructor. 
        View myview; 
        myview = new View(db, "Test_View", "Sales"); 
        //Set the TextHeader and TextBody property to define the view. 
        myview.TextHeader = "CREATE VIEW [Sales].[Test_View] AS"; 
        myview.TextBody = "SELECT h.SalesOrderID, d.OrderQty FROM Sales.SalesOrderHeader AS h INNER JOIN Sales.SalesOrderDetail AS d ON h.SalesOrderID = d.SalesOrderID"; 
        //Create the view on the instance of SQL Server. 
        myview.Create(); 
        //Remove the view. 
        myview.Drop(); 
        }

PowerShell에서 뷰 생성, 변경 및 제거

이 코드 예제는 내부 조인을 사용하여 두 테이블을 조인한 뷰를 만드는 방법을 보여 줍니다. 텍스트 모드를 사용하여 뷰를 작성하므로 TextHeader 속성을 설정해야 합니다.

# Set the path context to the local, default instance of SQL Server and get a reference to AdventureWorks2012
CD \sql\localhost\default\databases
$db = get-item Adventureworks2012

# Define a View object variable by supplying the parent database, view name and schema in the constructor. 
$myview  = New-Object -TypeName Microsoft.SqlServer.Management.SMO.View `
-argumentlist $db, "Test_View", "Sales"
      
# Set the TextHeader and TextBody property to define the view. 
$myview.TextHeader = "CREATE VIEW [Sales].[Test_View] AS"
$myview.TextBody ="SELECT h.SalesOrderID, d.OrderQty FROM Sales.SalesOrderHeader AS h INNER JOIN Sales.SalesOrderDetail AS d ON h.SalesOrderID = d.SalesOrderID"
        
# Create the view on the instance of SQL Server. 
$myview.Create()

# Remove the view. 
$myview.Drop();

참고 항목

참조

View