Beginning Microsoft SQL Server 2008 ... - S3 Tech Training
Beginning Microsoft SQL Server 2008 ... - S3 Tech Training Beginning Microsoft SQL Server 2008 ... - S3 Tech Training
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
- Page 301 and 302: Page Splits When a page becomes ful
- Page 303 and 304: The point here is that what happens
- Page 305 and 306: Page Splits — A First Look All of
- Page 307 and 308: You may hear lots of bad things abo
- Page 309 and 310: Navigating the Tree Figure 9-4 As I
- Page 311 and 312: there was no link between the data.
- Page 313 and 314: Root Non-Leaf Level Leaf Level Figu
- Page 315 and 316: The CREATE INDEX Statement The CREA
- Page 317 and 318: FILLFACTOR When SQL Server first cr
- Page 319 and 320: works only if tempdb is on a separa
- Page 321 and 322: Secondary XML Indexes Chapter 9: SQ
- Page 323 and 324: occur, and that one or more non-lea
- Page 325 and 326: isn’t room on the page, SQL Serve
- Page 327 and 328: more administrator oriented and usu
- Page 329 and 330: The Database Engine Tuning Advisor
- Page 331 and 332: The output is far more self-describ
- Page 333 and 334: We use a FILLFACTOR when we need to
- Page 335: Chapter 9: SQL Server Storage and I
- Page 338 and 339: Chapter 10: Views The preceding syn
- Page 340 and 341: Chapter 10: Views 302 columns to a
- Page 342 and 343: Chapter 10: Views Try It Out Using
- Page 344 and 345: Chapter 10: Views 306 soh.SalesOrde
- Page 346 and 347: Chapter 10: Views AW00000676 43659
- Page 348 and 349: Chapter 10: Views NULL values will
- Page 350 and 351: Chapter 10: Views Editing V iews wi
- Page 354 and 355: Chapter 10: Views Editing Views in
- Page 356 and 357: Chapter 10: Views 318 In addition,
- Page 358 and 359: Chapter 10: Views 320 from the firs
- Page 360 and 361: Chapter 10: Views You can get the y
- Page 363 and 364: 11 Writing Scripts and Batches Whet
- Page 365 and 366: Next we have a DECLARE statement to
- Page 367 and 368: I’m not going to pick any bones a
- Page 369 and 370: Using @@IDENTITY @@IDENTITY is one
- Page 371 and 372: How It Works What we’re doing in
- Page 373 and 374: DECLARE @RowCount int; --Notice the
- Page 375 and 376: When the editing tool encounters a
- Page 377 and 378: When you think about it, this seems
- Page 379 and 380: So, let’s try a quick query direc
- Page 381 and 382: We now have our text file source fo
- Page 383 and 384: Let’s build an example in the Adv
- Page 385 and 386: DECLARE @InVar varchar(50); DECLARE
- Page 387 and 388: -- This won’t work DECLARE @Numbe
- Page 389 and 390: -- Now we’re run our conditional
- Page 391 and 392: Out of the condition from inner con
- Page 393 and 394: A Simple CASE A simple CASE takes a
- Page 395 and 396: 3 8 More Than One Apart 2 2 Ends Wi
- Page 397 and 398: Now, I don’t know about you, but
- Page 399 and 400: The WAITFOR statement does exactly
- Page 401 and 402: IF @ErrorNo = 2714 -- Object exists
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).