Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

cdn.s3techtraining.com
from cdn.s3techtraining.com More from this publisher
17.06.2013 Views

Chapter 10: Views 314 There are four panes to the View Builder — each of which can be independently turned on or off: ❑ The Diagram pane ❑ The Criteria pane ❑ The SQL pane ❑ The Results pane For those of you who have worked with Access at all, the Diagram pane works much as it does in Access queries. You can add and remove tables, and even define relationships. Each of those added tables, checked columns, and defined relationships will automatically be reflected in the SQL pane in the form of the SQL required to match the diagram. To identify each of the icons on the toolbar, just hover your mouse pointer over them for a moment or two, and you will get a ToolTip that indicates the purpose of each button. You can add tables either by right-clicking in the Diagram pane (the top one in Figure 10-2) and choosing Add Table or by clicking on the Add Table toolbar button (the one with an arrow pointing right in the very top left of the icon). Now select some columns, as shown in Figure 10-3. Figure 10-3 Note that I am just showing the diagram pane here to save space. If you have the Grid pane up while you check the above, then you would see each column appear in the Grid pane as you select it. With the SQL pane up, you will also see it appear in the SQL code. In case you haven’t recognized it yet, we’re building the same view that we built as our first complex view (CustomerOrders_vw). The only thing that’s tricky at all is the computed column (TotalDiscount). To do that one, either we have to manually type the equation into the SQL pane, or we can type it into the Column column in the Grid pane along with its alias (see Figure 10-4).

Figure 10-4 When all is said and done, the View Builder gives us the following SQL code: Chapter 10: Views SELECT Sales.Customer.AccountNumber, Sales.SalesOrderHeader.SalesOrderID, Sales.SalesOrderHeader.OrderDate, Sales.SalesOrderDetail.ProductID, Production.Product.Name, Sales.SalesOrderDetail.OrderQty, Sales.SalesOrderDetail.UnitPrice, Sales.SalesOrderDetail.UnitPriceDiscount * Sales.SalesOrderDetail.UnitPrice * Sales.SalesOrderDetail.OrderQty AS TotalDiscount, Sales.SalesOrderDetail.LineTotal FROM Sales.SalesOrderDetail INNER JOIN Production.Product ON Sales.SalesOrderDetail.ProductID = Production.Product.ProductID INNER JOIN Sales.SalesOrderHeader ON Sales.SalesOrderDetail.SalesOrderID = Sales.SalesOrderHeader.SalesOrderID AND Sales.SalesOrderDetail.SalesOrderID = Sales.SalesOrderHeader.SalesOrderID INNER JOIN Sales.Customer ON Sales.SalesOrderHeader.CustomerID = Sales.Customer.CustomerID AND Sales.SalesOrderHeader.CustomerID = Sales.Customer.CustomerID While it’s not formatted the same, if you look it over, you’ll find that it’s basically the same code we wrote by hand! If you’ve been struggling with learning your T-SQL query syntax, you can use this tool to play around with the syntax of a query. Just drag and drop some tables into the Diagram pane, select the column you want from each table, and, for the most part, SQL Server will build you a query — you can then use the syntax from the View Builder to learn how to build it yourself next time. Now go ahead and save it (the disk icon in the toolbar is how I do it) as CustomerOrders2_vw and close the View Builder. 315

Chapter 10: Views<br />

314<br />

There are four panes to the View Builder — each of which can be independently turned on or off:<br />

❑ The Diagram pane<br />

❑ The Criteria pane<br />

❑ The <strong>SQL</strong> pane<br />

❑ The Results pane<br />

For those of you who have worked with Access at all, the Diagram pane works much as it does in Access<br />

queries. You can add and remove tables, and even define relationships. Each of those added tables, checked<br />

columns, and defined relationships will automatically be reflected in the <strong>SQL</strong> pane in the form of the <strong>SQL</strong><br />

required to match the diagram. To identify each of the icons on the toolbar, just hover your mouse pointer<br />

over them for a moment or two, and you will get a ToolTip that indicates the purpose of each button.<br />

You can add tables either by right-clicking in the Diagram pane (the top one in<br />

Figure 10-2) and choosing Add Table or by clicking on the Add Table toolbar button<br />

(the one with an arrow pointing right in the very top left of the icon).<br />

Now select some columns, as shown in Figure 10-3.<br />

Figure 10-3<br />

Note that I am just showing the diagram pane here to save space. If you have the Grid pane up while<br />

you check the above, then you would see each column appear in the Grid pane as you select it. With the<br />

<strong>SQL</strong> pane up, you will also see it appear in the <strong>SQL</strong> code.<br />

In case you haven’t recognized it yet, we’re building the same view that we built as our first complex<br />

view (CustomerOrders_vw). The only thing that’s tricky at all is the computed column (TotalDiscount).<br />

To do that one, either we have to manually type the equation into the <strong>SQL</strong> pane, or we can type<br />

it into the Column column in the Grid pane along with its alias (see Figure 10-4).

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!