?? ch19.htm
字號:
<TD ALIGN="LEFT">2</TD>
<TD ALIGN="LEFT">.305</TD>
<TD ALIGN="LEFT">0</TD>
<TD ALIGN="LEFT">28</TD>
</TR>
</TABLE>
<H4><FONT COLOR="#000077">The PITCHERS Table</FONT></H4>
<P>The <TT>PITCHERS</TT> table can be created using the following Transact-SQL statement:</P>
<H5>INPUT:</H5>
<PRE><FONT COLOR="#0066FF">1> <B>use BASEBALL</B>
2> <B>go</B>
1> <B>create table PITCHERS (</B>
2> <B>NAME char(30),</B>
3> <B>TEAM int,</B>
4> <B>WON int,</B>
5><B> LOST int,</B>
6> <B>ERA float)</B>
7> <B>go</B>
</FONT></PRE>
<P>Enter the data in Table 19.2 into the <TT>PITCHERS</TT> table.
<H4><FONT COLOR="#000077">Table 19.2. Data for the PITCHERS table.</FONT></H4>
<P>
<TABLE BORDER="1">
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><B>Name</B></TD>
<TD ALIGN="LEFT"><B>Team</B></TD>
<TD ALIGN="LEFT"><B>Won</B></TD>
<TD ALIGN="LEFT"><B>Lost</B></TD>
<TD ALIGN="LEFT"><B>Era</B></TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">Tom Madden</TD>
<TD ALIGN="LEFT">1</TD>
<TD ALIGN="LEFT">7</TD>
<TD ALIGN="LEFT">5</TD>
<TD ALIGN="LEFT">3.46</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">Bill Witter</TD>
<TD ALIGN="LEFT">1</TD>
<TD ALIGN="LEFT">8</TD>
<TD ALIGN="LEFT">2</TD>
<TD ALIGN="LEFT">2.75</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">Jeff Knox</TD>
<TD ALIGN="LEFT">2</TD>
<TD ALIGN="LEFT">2</TD>
<TD ALIGN="LEFT">8</TD>
<TD ALIGN="LEFT">4.82</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">Hank Arnold</TD>
<TD ALIGN="LEFT">2</TD>
<TD ALIGN="LEFT">13</TD>
<TD ALIGN="LEFT">1</TD>
<TD ALIGN="LEFT">1.93</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">Tim Smythe</TD>
<TD ALIGN="LEFT">3</TD>
<TD ALIGN="LEFT">4</TD>
<TD ALIGN="LEFT">2</TD>
<TD ALIGN="LEFT">2.76</TD>
</TR>
</TABLE>
<H4><FONT COLOR="#000077">The TEAMS Table</FONT></H4>
<P>The <TT>TEAMS</TT> table can be created using the following Transact-SQL statement:</P>
<H5>INPUT:</H5>
<PRE><FONT COLOR="#0066FF">1> <B>use BASEBALL</B>
2> <B>go</B>
1> <B>create table TEAMS (</B>
2> <B>TEAM_ID int,</B>
3> <B>CITY char(30),</B>
4> <B>NAME char(30),</B>
5> <B>WON int,</B>
6> <B>LOST int,</B>
7> <B>TOTAL_HOME_ATTENDANCE int,</B>
8> <B>AVG_HOME_ATTENDANCE int)</B>
9> <B>go</B>
</FONT></PRE>
<P>Enter the data in Table 19.3 into the <TT>TEAMS</TT> table.
<H4><FONT COLOR="#000077">Table 19.3. Data for the TEAMS table.</FONT></H4>
<P>
<TABLE BORDER="1">
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP">
<P ALIGN="CENTER"><B>Team_ID</B>
</TD>
<TD ALIGN="LEFT" VALIGN="TOP"><B>City</B></TD>
<TD ALIGN="LEFT" VALIGN="TOP"><B>Name</B></TD>
<TD ALIGN="LEFT" VALIGN="TOP"><B>Won</B></TD>
<TD ALIGN="LEFT" VALIGN="TOP"><B>Lost</B></TD>
<TD ALIGN="LEFT" VALIGN="TOP"><B>Total_Home_Attendance</B></TD>
<TD ALIGN="LEFT" VALIGN="TOP"><B>Avg_Home_Attendance</B></TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP">1</TD>
<TD ALIGN="LEFT" VALIGN="TOP">Portland</TD>
<TD ALIGN="LEFT" VALIGN="TOP">Beavers</TD>
<TD ALIGN="LEFT" VALIGN="TOP">72</TD>
<TD ALIGN="LEFT" VALIGN="TOP">63</TD>
<TD ALIGN="LEFT" VALIGN="TOP">1,226,843</TD>
<TD ALIGN="LEFT" VALIGN="TOP">19,473</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP">2</TD>
<TD ALIGN="LEFT" VALIGN="TOP">Washington</TD>
<TD ALIGN="LEFT" VALIGN="TOP">Representatives</TD>
<TD ALIGN="LEFT" VALIGN="TOP">50</TD>
<TD ALIGN="LEFT" VALIGN="TOP">85</TD>
<TD ALIGN="LEFT" VALIGN="TOP">941,228</TD>
<TD ALIGN="LEFT" VALIGN="TOP">14,048</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP">3</TD>
<TD ALIGN="LEFT" VALIGN="TOP">Tampa</TD>
<TD ALIGN="LEFT" VALIGN="TOP">Sharks</TD>
<TD ALIGN="LEFT" VALIGN="TOP">99</TD>
<TD ALIGN="LEFT" VALIGN="TOP">36</TD>
<TD ALIGN="LEFT" VALIGN="TOP">2,028,652</TD>
<TD ALIGN="LEFT" VALIGN="TOP">30,278</TD>
</TR>
</TABLE>
<H3><FONT COLOR="#000077">Declaring Local Variables</FONT></H3>
<P>Every programming language enables some method for declaring local (or global)
variables that can be used to store data. Transact-SQL is no exception. Declaring
a variable using Transact-SQL is an extremely simple procedure. The keyword that
must be used is the <TT>DECLARE</TT> keyword. The syntax looks like this:</P>
<H5>SYNTAX:</H5>
<P><TT>declare @variable_name data_type</TT></P>
<P>To declare a character string variable to store players' names, use the following
statement:</P>
<PRE><FONT COLOR="#0066FF">1> <B>declare @name char(30)</B>
2> <B>go</B>
</FONT></PRE>
<P>Note the <TT>@</TT> symbol before the variable's name. This symbol is required
and is used by the query processor to identify variables.
<H3><FONT COLOR="#000077">Declaring Global Variables</FONT></H3>
<P>If you delve further into the Transact-SQL documentation, you will notice that
the <TT>@@</TT> symbol precedes the names of some system-level variables. This syntax
denotes SQL Server global variables that store information.</P>
<P>Declaring your own global variables is particularly useful when using stored procedures.
SQL Server also maintains several system global variables that contain information
that might be useful to the database system user. Table 19.4 contains the complete
list of these variables. The source for this list is the Sybase SQL Server System
10 documentation.
<H4><FONT COLOR="#000077">Table 19.4. SQL Server global variables.</FONT></H4>
<P>
<TABLE BORDER="1">
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><B>Variable Name</B></TD>
<TD ALIGN="LEFT"><B>Purpose</B></TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>@@char_convert</TT></TD>
<TD ALIGN="LEFT"><TT>0</TT> if character set conversion is in effect.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>@@client_csid</TT></TD>
<TD ALIGN="LEFT">Client's character set ID.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>@@client_csname</TT></TD>
<TD ALIGN="LEFT">Client's character set name.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>@@connections</TT></TD>
<TD ALIGN="LEFT">Number of logons since SQL Server was started.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>@@cpu_busy</TT></TD>
<TD ALIGN="LEFT">Amount of time, in ticks, the CPU has been busy since SQL Server was started.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>@@error</TT></TD>
<TD ALIGN="LEFT">Contains error status.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>@@identity</TT></TD>
<TD ALIGN="LEFT">Last value inserted into an identity column.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>@@idle</TT></TD>
<TD ALIGN="LEFT">Amount of time, in ticks, that SQL Server has been idle since started.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>@@io_busy</TT></TD>
<TD ALIGN="LEFT">Amount of time, in ticks, that SQL Server has spent doing I/O.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>@@isolation</TT></TD>
<TD ALIGN="LEFT">Current isolation level of the Transact-SQL program.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>@@langid</TT></TD>
<TD ALIGN="LEFT">Defines local language ID.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>@@language</TT></TD>
<TD ALIGN="LEFT">Defines the name of the local language.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>@@maxcharlen</TT></TD>
<TD ALIGN="LEFT">Maximum length of a character.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>@@max_connections</TT></TD>
<TD ALIGN="LEFT">Maximum number of connections that can be made with SQL Server.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>@@ncharsize</TT></TD>
<TD ALIGN="LEFT">Average length of a national character.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>@@nestlevel</TT></TD>
<TD ALIGN="LEFT">Nesting level of current execution.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>@@pack_received</TT></TD>
<TD ALIGN="LEFT">Number of input packets read by SQL Server since it was started.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>@@pack_sent</TT></TD>
<TD ALIGN="LEFT">Number of output packets sent by SQL Server since it was started.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>@@packet_errors</TT></TD>
<TD ALIGN="LEFT">Number of errors that have occurred since SQL Server was started.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>@@procid</TT></TD>
<TD ALIGN="LEFT">ID of the currently executing stored procedure.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>@@rowcount</TT></TD>
<TD ALIGN="LEFT">Number of rows affected by the last command.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>@@servername</TT></TD>
<TD ALIGN="LEFT">Name of the local SQL Server.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>@@spid</TT></TD>
<TD ALIGN="LEFT">Process ID number of the current process.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>@@sqlstatus</TT></TD>
<TD ALIGN="LEFT">Contains status information.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>@@textsize</TT></TD>
<TD ALIGN="LEFT">Maximum length of text or image data returned with <TT>SELECT</TT> statement.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>@@thresh_hysteresis</TT></TD>
<TD ALIGN="LEFT">Change in free space required to activate a threshold.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>@@timeticks</TT></TD>
<TD ALIGN="LEFT">Number of microseconds per tick.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>@@total_errors</TT></TD>
<TD ALIGN="LEFT">Number of errors that have occurred while reading or writing.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>@@total_read</TT></TD>
<TD ALIGN="LEFT">Number of disk reads since SQL Server was started.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>@@total_write</TT></TD>
<TD ALIGN="LEFT">Number of disk writes since SQL Server was started.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>@@tranchained</TT></TD>
<TD ALIGN="LEFT">Current transaction mode of the Transact-SQL program.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>@@trancount</TT></TD>
<TD ALIGN="LEFT">Nesting level of transactions.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>@@transtate</TT></TD>
<TD ALIGN="LEFT">Current state of a transaction after a statement executes.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>@@version</TT></TD>
<TD ALIGN="LEFT">Date of the current version of SQL Server.</TD>
</TR>
</TABLE>
<H3><FONT COLOR="#000077">Using Variables</FONT></H3>
<P>The <TT>DECLARE</TT> keyword enables you to declare several variables with a single
statement (although this device can sometimes look confusing when you look at your
code later). An example of this type of statement appears here:</P>
<PRE><FONT COLOR="#0066FF">1> <B>declare @batter_name char(30), @team int, @average float</B>
2> <B>go</B>
</FONT></PRE>
<P>The next section explains how to use variables it to perform useful programming
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -