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 2: Tools of the Trade Figure 2-12 Results to Grid This option divides the columns and rows into a grid arrangement. Following is a list of specific things that this option gives you that the Results to Text doesn’t: ❑ You can resize the column by hovering your mouse pointer on the right border of the column header, then clicking and dragging the column border to its new size. Double-clicking the right border results in the autofit for the column. ❑ If you select several cells, then cut and paste them into another grid (say, Microsoft Excel), they will be treated as individual cells. (Under the Results to Text option, the cut data is pasted all into one cell.) ❑ You can select just one or two columns of multiple rows. (Under Results to Text, if you select several rows, all the inner rows have every column selected; you can select only in the middle of the row for the first and last row selected.) I use this option for almost everything because I find that I usually want one of the benefits I just listed. Results to File Think of this one as largely the same as Results to Text, but instead of to screen, it routes the output directly to a file. I use this one to generate files I intend to parse using some utility or that I want to easily e-mail. sqlcmd Mode 36 We will discuss sqlcmd a bit more shortly. For now, suffice it to say that it is a tool that helps us run queries from a Windows command line. It has some special scripting abilities that are meant specifically for

command-line scripting. By default, these special script commands are not available in the Query window. Turning sqlcmd mode on activates the special sqlcmd scripting options even in the Query window. Show Execution Plan Every time you run a query, SQL Server parses your query into its component parts and then sends it to the query optimizer. The query optimizer is the part of SQL Server that figures out the best way to run your query to balance fast results with minimum impact to other users. When you use the Show Estimated Execution Plan option, you receive a graphical representation and additional information about how SQL Server plans to run your query. Similarly, you can turn on the Include Actual Execution Plan option. Most of the time, this will be the same as the estimated execution plan, but you will occasionally see differences here due to changes that the optimizer decides to make while running the query, as well as changes in the actual cost of running the query versus what the optimizer thinks is going to happen. Let’s see what a query plan looks like in our simple query. Click the Include Actual Execution Plan option, and execute the query again, as shown in Figure 2-13. Figure 2-13 Chapter 2: Tools of the Trade Be aware that the Query window always utilizes the SQLNativeClient connection method (even when operating in sqlcmd mode), whereas the actual sqlcmd utility will use an OLE DB connection. The result is that you may see slight differences in behavior between running a script using sqlcmd versus using sqlcmd from the Query window. These tend to be corner case differences, and are rarely seen and generally innocuous. 37

command-line scripting. By default, these special script commands are not available in the Query window.<br />

Turning sqlcmd mode on activates the special sqlcmd scripting options even in the Query window.<br />

Show Execution Plan<br />

Every time you run a query, <strong>SQL</strong> <strong>Server</strong> parses your query into its component parts and then sends it to<br />

the query optimizer. The query optimizer is the part of <strong>SQL</strong> <strong>Server</strong> that figures out the best way to run<br />

your query to balance fast results with minimum impact to other users. When you use the Show Estimated<br />

Execution Plan option, you receive a graphical representation and additional information about how<br />

<strong>SQL</strong> <strong>Server</strong> plans to run your query. Similarly, you can turn on the Include Actual Execution Plan option.<br />

Most of the time, this will be the same as the estimated execution plan, but you will occasionally see<br />

differences here due to changes that the optimizer decides to make while running the query, as well as<br />

changes in the actual cost of running the query versus what the optimizer thinks is going to happen.<br />

Let’s see what a query plan looks like in our simple query. Click the Include Actual Execution Plan<br />

option, and execute the query again, as shown in Figure 2-13.<br />

Figure 2-13<br />

Chapter 2: Tools of the Trade<br />

Be aware that the Query window always utilizes the <strong>SQL</strong>NativeClient connection<br />

method (even when operating in sqlcmd mode), whereas the actual sqlcmd utility<br />

will use an OLE DB connection. The result is that you may see slight differences<br />

in behavior between running a script using sqlcmd versus using sqlcmd from the<br />

Query window. These tend to be corner case differences, and are rarely seen and<br />

generally innocuous.<br />

37

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

Saved successfully!

Ooh no, something went wrong!