Creazione, modifica e rimozione di viste
In SMO (SQL Server Management Objects), le viste di SQL Server sono rappresentate dall'oggetto View.
La proprietà TextBody dell'oggetto View definisce la vista. Equivale all'istruzione SELECT di Transact-SQL per la creazione di una vista.
Esempio
Per utilizzare qualsiasi esempio di codice fornito, è necessario scegliere l'ambiente di programmazione, il modello di programmazione e il linguaggio di programmazione per la creazione dell'applicazione. Per ulteriori informazioni, vedere Creare un progetto SMO per Visual Basic in Visual Studio .NET o Creare un progetto SMO per Visual C# in Visual Studio .NET.
Creazione, modifica e rimozione di una vista in Visual Basic
In questo esempio di codice viene illustrato come creare una vista di due tabelle tramite un inner join. La vista viene creata tramite la modalità testo, di conseguenza è necessario impostare la proprietà 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()
Creazione, modifica e rimozione di una vista in Visual C#
In questo esempio di codice viene illustrato come creare una vista di due tabelle tramite un inner join. La vista viene creata tramite la modalità testo, di conseguenza è necessario impostare la proprietà 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();
}
Creazione, modifica e rimozione di una vista in PowerShell
In questo esempio di codice viene illustrato come creare una vista di due tabelle tramite un inner join. La vista viene creata tramite la modalità testo, di conseguenza è necessario impostare la proprietà 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();