home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft_Programmers_Library.7z / MPL / net / sqllang.txt < prev    next >
Encoding:
Text File  |  2013-11-08  |  674.8 KB  |  21,330 lines

  1.  Microsoft  SQL Server - Language Reference
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  ────────────────────────────────────────────────────────────────────────────
  11.                 Microsoft (R) SQL Server - Language Reference
  12.  
  13.               The SYBASE (R) SQL Server database for PC networks
  14.                                  VERSION 1.1
  15.  ────────────────────────────────────────────────────────────────────────────
  16.  
  17.  
  18.                      for the MS (R) OS/2 Operating System
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  Microsoft Corporation
  28.  
  29.  Information in this document is subject to change without notice and does
  30.  not represent a commitment on the part of Microsoft Corporation. The
  31.  software described in this document is furnished under a license agreement
  32.  or nondisclosure agreement. The software may be used or copied only in
  33.  accordance with the terms of the agreement. It is against the law to copy
  34.  the software on any medium except as specifically allowed in the license or
  35.  nondisclosure agreement. No part of this manual may be reproduced or
  36.  transmitted in any form or by any means, electronic or mechanical, including
  37.  photocopying and recording, for any purpose without the express written
  38.  permission of Microsoft Corporation.
  39.  (C) 1990 Microsoft Corporation and SYBASE, Inc. All rights reserved.
  40.  
  41.  
  42.  Printed in the USA.
  43.  
  44.  
  45.   1 = Microsoft, MS, MS-DOS, and the Microsoft logo are registered trademarks
  46.  of Microsoft Corporation.  IBM is a registered trademark of International
  47.  Business
  48.  Machines Corporation.
  49.  
  50.  
  51.  SYBASE is a registered trademark of SYBASE, Inc. TRANSACT-SQL
  52.  and DB-LIBRARY are trademarks of SYBASE, Inc.
  53.  
  54.  Document Number: SY10231-0290
  55.   OEM-D/0788-1Z
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  Table of Contents
  63.  ────────────────────────────────────────────────────────────────────────────
  64.  
  65.  
  66.  
  67.  Before You Begin
  68.       Manual Overview
  69.       How to Use This Guide
  70.       Notational Conventions
  71.       Finding Further Information
  72.  
  73.  Chapter 1  TRANSACT-SQL Statements
  74.  
  75.              Aggregate Functions
  76.              ALTER DATABASE
  77.              ALTER TABLE
  78.              Batch Queries
  79.              BEGIN...END
  80.              BEGIN TRANSACTION
  81.              BREAK
  82.              Browse Mode
  83.              CHECKPOINT
  84.              Comments
  85.              COMMIT TRANSACTION
  86.              COMPUTE Clause
  87.              CONTINUE
  88.              Control-of-Flow Language
  89.              Conversion Function
  90.              CREATE DATABASE
  91.              CREATE DEFAULT
  92.              CREATE INDEX
  93.              CREATE PROCEDURE
  94.              CREATE RULE
  95.              CREATE TABLE
  96.              CREATE TRIGGER
  97.              CREATE VIEW
  98.              Datatypes
  99.              Date Functions
  100.              DBCC
  101.              DECLARE
  102.              DELETE
  103.              DISK INIT
  104.              DISK REFIT
  105.              DISK REINIT
  106.              DROP DATABASE
  107.              DROP DEFAULT
  108.              DROP INDEX
  109.              DROP PROCEDURE
  110.              DROP RULE
  111.              DROP TABLE
  112.              DROP TRIGGER
  113.              DROP VIEW
  114.              DUMP DATABASE
  115.              DUMP TRANSACTION
  116.              EXECUTE
  117.              Expressions
  118.              Functions
  119.              GOTO
  120.              GRANT
  121.              GROUP BY and HAVING Clauses
  122.              Identifiers
  123.              IF...ELSE
  124.              INSERT
  125.              Joins
  126.              KILL
  127.              LOAD DATABASE
  128.              LOAD TRANSACTION
  129.              Mathematical Functions
  130.              Null Values
  131.              ORDER BY Clause
  132.              Parameters
  133.              PRINT
  134.              RAISERROR
  135.              READTEXT
  136.              RECONFIGURE
  137.              RETURN
  138.              REVOKE
  139.              ROLLBACK TRANSACTION
  140.              Row Aggregate Functions
  141.              SAVE TRANSACTION
  142.              Search Conditions
  143.              SELECT
  144.              SET
  145.              SETUSER
  146.              SHUTDOWN
  147.              String Functions
  148.              Subqueries
  149.              System Functions
  150.              System Procedures
  151.              Text/Image Datatypes
  152.              Text/Image Functions
  153.              TRUNCATE TABLE
  154.              UPDATE
  155.              UPDATE STATISTICS
  156.              USE
  157.              Variables (Local and Global)
  158.              Views
  159.              WAITFOR
  160.              WHERE Clause
  161.              WHILE
  162.              Wildcard Characters
  163.              WRITETEXT
  164.  
  165.  Chapter 2  System Procedures
  166.  
  167.              sp_addalias
  168.              sp_addgroup
  169.              sp_addlogin
  170.              sp_addtype
  171.              sp_addumpdevice
  172.              sp_adduser
  173.              sp_bindefault
  174.              sp_bindrule
  175.              sp_changedbowner
  176.              sp_changegroup
  177.              sp_commonkey
  178.              sp_configure
  179.              sp_dboption
  180.              sp_defaultdb
  181.              sp_depends
  182.              sp_diskdefault
  183.              sp_dropalias
  184.              sp_dropdevice
  185.              sp_dropgroup
  186.              sp_dropkey
  187.              sp_droplogin
  188.              sp_droptype
  189.              sp_dropuser
  190.              sp_foreignkey
  191.              sp_help
  192.              sp_helpdb
  193.              sp_helpdevice
  194.              sp_helpgroup
  195.              sp_helpindex
  196.              sp_helpjoins
  197.              sp_helpkey
  198.              sp_helprotect
  199.              sp_helpsql
  200.              sp_helptext
  201.              sp_helpuser
  202.              sp_lock
  203.              sp_logdevice
  204.              sp_monitor
  205.              sp_password
  206.              sp_primarykey
  207.              sp_rename
  208.              sp_renamedb
  209.              sp_spaceused
  210.              sp_unbindefault
  211.              sp_unbindrule
  212.              sp_who
  213.  
  214.  Chapter 3  Utility Programs
  215.  
  216.              bcp
  217.              bldmastr
  218.              console
  219.              defncopy
  220.              isql
  221.              sqlservr
  222.  
  223.  Appendix A  System Tables
  224.  
  225.              sysalternates (all databases)
  226.              syscolumns (all databases)
  227.              syscomments (all databases)
  228.              sysconfigures (master database only)
  229.              syscurconfigs (master database only)
  230.              sysdatabases (master database only)
  231.              sysdepends (all databases)
  232.              sysdevices (master database only)
  233.              sysindexes (all databases)
  234.              syskeys (all databases)
  235.              syslocks (master database only)
  236.              syslogins (master database only)
  237.              syslogs (all databases)
  238.              sysmessages (master database only)
  239.              sysobjects (all databases)
  240.              sysprocedures (all databases)
  241.              sysprocesses (master database only)
  242.              sysprotects (all databases)
  243.              syssegments (all databases)
  244.              systypes (all databases)
  245.              sysusages (master database only)
  246.              sysusers (all databases)
  247.  
  248.  Appendix B  The pubs Sample Database
  249.  
  250.              B.1.1
  251.              Rules
  252.              Trigger
  253.              Stored Procedure
  254.              View
  255.              Defaults
  256.  
  257.  Appendix C  TRANSACT-SQL Keywords
  258.  
  259.  
  260.  Index
  261.  
  262.  
  263.  
  264.  
  265.  Before You Begin
  266.  ────────────────────────────────────────────────────────────────────────────
  267.  
  268.  
  269.  Manual Overview
  270.  
  271.  This reference manual describes each statement and command of
  272.  TRANSACT-SQL(tm), an enhanced version of the SQL relational database
  273.  language. It also describes SQL Server system procedures and utility
  274.  programs.
  275.  
  276.  Before using this manual, you should have read SQL Server Administrator's
  277.  Guide for information on using the SQL Server Administration Facility (SAF).
  278.  You should also be familiar with TRANSACT-SQL or another version of SQL. To
  279.  learn TRANSACT-SQL, see SQL Server Learning TRANSACT-SQL.
  280.  
  281.  
  282.  How to Use This Guide
  283.  
  284.  Topics in each chapter are listed in alphabetical order. The topics include
  285.  each statement, or command, with complete syntax as well as general topics.
  286.  For example, to find the complete syntax of the SELECT statement, look under
  287.  "SELECT" in Chapter 1. To read about identifiers, look under "Identifiers"
  288.  in Chapter 1. This manual covers the following topics:
  289.  
  290.  ────────────────────────────────────────────────────────────────────────────
  291.  Chapter 1
  292.    TRANSACT-SQL, SQL Server's enhanced version of the SQL database language
  293.  
  294.  Chapter 2
  295.    SQL Server system procedures, which are procedures supplied with SQL
  296.    Server and executed from the SQL Server Administration Facility (SAF) or
  297.    isql
  298.  
  299.  Chapter 3
  300.    SQL Server utility programs, which are supplied with SQL Server and
  301.    executed directly from the operating system
  302.  
  303.  Appendix A
  304.    SQL Server system tables, which are supplied with SQL Server or created
  305.    when the CREATE DATABASE statement is executed
  306.  
  307.  Appendix B
  308.    The sample database, pubs (used in examples throughout the manual)
  309.  
  310.  Appendix C
  311.    TRANSACT-SQL keywords
  312.  
  313.  ────────────────────────────────────────────────────────────────────────────
  314.  The SQL Server Language Reference is part of the SQL Server documentation
  315.  set. Several manuals in this set are fully illustrated with examples from
  316.  the online sample database, pubs. (See Appendix B, "The pubs Sample
  317.  Database.") Ask your System Administrator how to get a clean copy of the
  318.  pubs database for use in learning TRANSACT-SQL and the system procedures.
  319.  
  320.  
  321.  Notational Conventions
  322.  
  323.  Throughout this manual, the following conventions are used to distinguish
  324.  elements of text:
  325.  
  326. ╓┌─────────────────────────────────┌─────────────────────────────────────────╖
  327.  Convention                        Purpose
  328.  ────────────────────────────────────────────────────────────────────────────
  329.  UPPERCASE                         Represents statement and clause names,
  330.                                    functions, macros, and any other
  331.                                    portions of syntax that must appear
  332.                                    exactly as shown.
  333.  
  334.  MIXEDcase                         Shows abbreviations for keywords in
  335.                                    syntax. The uppercase letters show the
  336.                                    required portion of the keyword; the
  337.                                    lowercase letters show the optional
  338.  Convention                        Purpose
  339.  ────────────────────────────────────────────────────────────────────────────
  340.                                   lowercase letters show the optional
  341.                                    portion.
  342.  
  343.  SMALL CAPS                        Represent key names such as CTRL.
  344.  
  345.  bold                              Represents stored procedures, system
  346.                                    procedures, triggers, defaults, rules,
  347.                                    utility programs, and commands.
  348.  
  349.  italic                            Represents database names, table names,
  350.                                    view names, column names, datatypes,
  351.                                    index names, pathnames, filenames, and
  352.                                    variables that appear in text.
  353.  
  354.  monospace                         Represents examples, screen output,
  355.                                    program code, and error messages.
  356.  
  357.  [brackets]                        Enclose optional items. Type only the
  358.                                    information within the brackets, not the
  359.  Convention                        Purpose
  360.  ────────────────────────────────────────────────────────────────────────────
  361.                                   information within the brackets, not the
  362.                                    brackets themselves.
  363.  
  364.  {braces}                          Enclose required items. Type only the
  365.                                    information within the braces, not the
  366.                                    braces themselves.
  367.  
  368.  | (vertical bar)                  Separates items inside a set of braces
  369.                                    or brackets. The vertical bar means you
  370.                                    must choose one and only one item.
  371.  
  372.  ... (ellipsis)                    Means that you can repeat the previous
  373.                                    item as many times as you like.
  374.  
  375.  <execute>                         Executes one or more SQL statements. (In
  376.                                    the SQL Server Administration facility,
  377.                                    SQL statements are executed by pressing
  378.                                    the CONTROL+E keys. In the isql program,
  379.                                    SQL statements are executed with the go
  380.  Convention                        Purpose
  381.  ────────────────────────────────────────────────────────────────────────────
  382.                                   SQL statements are executed with the go
  383.                                    command.)
  384.  
  385.  
  386.  
  387.  
  388.  Finding Further Information
  389.  
  390.  The following manuals describe SQL Server and are included as part of the
  391.  standard documentation set:
  392.  
  393.  ────────────────────────────────────────────────────────────────────────────
  394.  SQL Server Installation Guide
  395.    A guide to installing and setting up SQL Server
  396.  
  397.  SQL Server Learning TRANSACT-SQL
  398.    A guide to learning and using TRANSACT-SQL
  399.  
  400.  SQL Server System Administrator's Guide
  401.    A guide for SQL Server System Administrators and for using the SQL Server
  402.    Administration Facility for database queries
  403.  
  404.  SQL Server Programmer's Reference
  405.    A reference to DB-LIBRARY(tm), which is a set of C routines and macros
  406.    that allow your application to interact with SQL Server
  407.  
  408.  SQL Server Quick Reference
  409.    A quick reference guide to TRANSACT-SQL
  410.  
  411.  ────────────────────────────────────────────────────────────────────────────
  412.  
  413.  
  414.  
  415.  
  416.  Chapter 1  TRANSACT-SQL Statements
  417.  ────────────────────────────────────────────────────────────────────────────
  418.  
  419.  This chapter consists of alphabetized reference pages, one for each
  420.  TRANSACT-SQL statement plus special topics such as datatypes, identifiers,
  421.  and functions.
  422.  
  423.  Some SQL statements that are particularly complex (such as SELECT) are
  424.  broken down into individual subsections. For example, there are separate
  425.  reference pages on the COMPUTE, GROUP BY, and HAVING clauses of the SELECT
  426.  statement.
  427.  
  428.  The following statements, commands, and topics are covered in this chapter:
  429.  
  430.  
  431.  ────────────────────────────────────────────────────────────────────────────
  432.  Aggregate Functions
  433.    Return summary values.
  434.  
  435.  ALTER DATABASE
  436.    Increases the amount of disk space allocated to a database.
  437.  
  438.  ALTER TABLE
  439.    Adds new columns to an existing table.
  440.  
  441.  Batch Queries
  442.    A set of SQL statements submitted together and executed as a group, one
  443.    after the other.
  444.  
  445.  BEGIN...END
  446.    Encloses a series of SQL statements so that control-of-flow language, such
  447.    as IF...ELSE, affects the performance of the whole group.
  448.  
  449.  BEGIN TRANSACTION
  450.    Marks the starting point of a user-specified transaction.
  451.  
  452.  BREAK
  453.    Controls operation of statements in a WHILE loop.
  454.  
  455.  Browse Mode
  456.    Supports the ability to perform updates while viewing data.
  457.  
  458.  CHECKPOINT
  459.    Forces all dirty pages in the current database to be written to the disk.
  460.  
  461.  Comments
  462.    Provide information about SQL statements, statement blocks, and stored
  463.    procedures.
  464.  
  465.  COMMIT TRANSACTION
  466.    Marks the ending point of a user-defined transaction.
  467.  
  468.  COMPUTE Clause
  469.    Generates summary values in a SELECT statement with row aggregate
  470.    functions.
  471.  
  472.  CONTINUE
  473.    Controls operation of statements in a WHILE loop.
  474.  
  475.  Control-of-Flow Language
  476.    Controls the flow of execution of SQL statements, statement blocks, and
  477.    stored procedures.
  478.  
  479.  Conversion Function
  480.    Converts expressions of one datatype to another datatype.
  481.  
  482.  CREATE DATABASE
  483.    Creates a new database.
  484.  
  485.  CREATE DEFAULT
  486.    Specifies a value that will be inserted in a column if no value is
  487.    explicitly supplied at insert time.
  488.  
  489.  CREATE INDEX
  490.    Creates indexes.
  491.  
  492.  CREATE PROCEDURE
  493.    Creates a stored procedure that can take one or more user-supplied
  494.    parameters.
  495.  
  496.  CREATE RULE
  497.    Specifies the domain of acceptable values for a particular column or for
  498.    any column of a specified user datatype.
  499.  
  500.  CREATE TABLE
  501.    Creates new tables.
  502.  
  503.  CREATE TRIGGER
  504.    Creates a trigger, which is a special kind of stored procedure often used
  505.    for enforcing integrity constraints.
  506.  
  507.  CREATE VIEW
  508.    Creates views.
  509.  
  510.  Datatypes
  511.    Specify data characteristics of columns, stored procedure parameters, and
  512.    local variables.
  513.  
  514.  Date Functions
  515.    Manipulate datetime values.
  516.  
  517.  DBCC
  518.    Checks the logical and physical consistency of a database.
  519.  
  520.  DECLARE
  521.    Declares the name and type of local variables for a batch or procedure.
  522.  
  523.  DELETE
  524.    Removes rows from a table.
  525.  
  526.  DISK INIT
  527.    Reserves and formats physical storage for database devices.
  528.  
  529.  DISK REFIT
  530.    Restores a damaged master database.
  531.  
  532.  DISK REINIT
  533.    Part of the procedure that restores a damaged master database.
  534.  
  535.  DROP DATABASE
  536.    Removes one or more databases from SQL Server.
  537.  
  538.  DROP DEFAULT
  539.    Removes a user-specified default.
  540.  
  541.  DROP INDEX
  542.    Removes an index from the database.
  543.  
  544.  DROP PROCEDURE
  545.    Removes user-created stored procedures from the current database.
  546.  
  547.  DROP RULE
  548.    Removes a user-specified rule.
  549.  
  550.  DROP TABLE
  551.    Removes a table definition and all data, indexes, triggers, and permission
  552.    specifications for a table from the database.
  553.  
  554.  DROP TRIGGER
  555.    Removes a trigger.
  556.  
  557.  DROP VIEW
  558.    Removes views from the database.
  559.  
  560.  DUMP DATABASE
  561.    Makes a backup copy of the database and the transaction log.
  562.  
  563.  DUMP TRANSACTION
  564.    Removes the inactive part of the transaction log and makes a backup copy
  565.    of it.
  566.  
  567.  EXECUTE
  568.    Runs a system procedure or a user-defined stored procedure.
  569.  
  570.  Expressions
  571.    Used as variables and constants in many SQL statements.
  572.  
  573.  Functions
  574.    Return special information from the database.
  575.  
  576.  GOTO
  577.    Causes an unconditional branching to a user-defined label.
  578.  
  579.  GRANT
  580.    Assigns permissions to users.
  581.  
  582.  GROUP BY and HAVING Clauses
  583.    Divide a table into groups.
  584.  
  585.  Identifiers
  586.    Name database objects─databases, tables, views, columns, indexes,
  587.    triggers, procedures, defaults, rules, and so on.
  588.  
  589.  IF...ELSE
  590.    Impose conditions on the execution of an SQL statement.
  591.  
  592.  INSERT
  593.    Adds new rows to a table or view.
  594.  
  595.  Joins
  596.    Compare two or more tables (or views).
  597.  
  598.  KILL
  599.    Kills a process.
  600.  
  601.  LOAD DATABASE
  602.    Loads a backup copy of a user database and its transaction log that was
  603.    created with DUMP DATABASE.
  604.  
  605.  LOAD TRANSACTION
  606.    Loads a backup copy of the transaction log.
  607.  
  608.  Mathematical Functions
  609.    Return values commonly needed for operations on mathematical data.
  610.  
  611.  Null Values
  612.    Mark columns having an unknown value.
  613.  
  614.  ORDER BY Clause
  615.    Returns query results in the specified column(s) in sorted order.
  616.  
  617.  Parameters
  618.    Values supplied to a stored procedure.
  619.  
  620.  PRINT
  621.    Prints a user-defined message on the user's screen.
  622.  
  623.  RAISERROR
  624.    Prints a user-defined error message on the user's screen and sets a system
  625.    flag to record that an error has occurred.
  626.  
  627.  READTEXT
  628.    Reads text and image values, starting from a specified offset and reading
  629.    a specified number of bytes.
  630.  
  631.  RECONFIGURE
  632.    Part of the procedure that sets configuration options, which control
  633.    various aspects of SQL Server's memory allocation and performance.
  634.  
  635.  RETURN
  636.    Exits unconditionally from a query or procedure.
  637.  
  638.  REVOKE
  639.    Revokes permissions from users.
  640.  
  641.  ROLLBACK TRANSACTION
  642.    Rolls back a user-specified transaction to the last savepoint inside a
  643.    transaction or to the beginning of a transaction.
  644.  
  645.  Row Aggregate Functions
  646.    Generate summary values that appear as additional rows in the query
  647.    results.
  648.  
  649.  SAVE TRANSACTION
  650.    Sets a savepoint within a transaction.
  651.  
  652.  Search Conditions
  653.    Set the conditions in a WHERE or HAVING clause.
  654.  
  655.  SELECT
  656.    Retrieves rows from the database.
  657.  
  658.  SET
  659.    Sets SQL Server query-processing options for the duration of the user's
  660.    work session, or inside a trigger or stored procedure.
  661.  
  662.  SETUSER
  663.    Impersonates another user.
  664.  
  665.  SHUTDOWN
  666.    Brings the system to a halt.
  667.  
  668.  String Functions
  669.    Perform various operations on binary data, character strings, or
  670.    expressions, including concatenation.
  671.  
  672.  Subqueries
  673.    Nest a SELECT statement inside a SELECT, INSERT, UPDATE, or DELETE
  674.    statement, another subquery, or anywhere an expression is allowed (if it
  675.    returns a single value).
  676.  
  677.  System Functions
  678.    Return special information from the database.
  679.  
  680.  System Procedures
  681.    Stored procedures that SQL Server supplies to update and report from the
  682.    system tables.
  683.  
  684.  Text/Image Datatypes
  685.    The text datatype stores extremely long (up to 231) byte strings of
  686.    printable characters.
  687.  
  688.    The image datatype stores extremely long (up to 231) bytes of
  689.    hexadecimal-encoded binary data.
  690.  
  691.  Text/Image Functions
  692.    Return values commonly needed for operations on text and image data.
  693.  
  694.  TRUNCATE TABLE
  695.    Removes all rows in a table as quickly as possible.
  696.  
  697.  UPDATE
  698.    Changes data in existing rows, either by adding new data or modifying
  699.    existing data.
  700.  
  701.  UPDATE STATISTICS
  702.    Updates information about the distribution of key values in specified
  703.    indexes.
  704.  
  705.  USE
  706.    Changes the current database.
  707.  
  708.  Variables (Local and Global)
  709.    Defined entities that are assigned values.
  710.  
  711.  Views
  712.    Provide an alternate way of looking at data in one or more tables.
  713.  
  714.  WAITFOR
  715.    Specifies a specific time, a time interval, or an event for the execution
  716.    of a statement block, stored procedure, or transaction.
  717.  
  718.  WHERE Clause
  719.    Sets the conditions in a WHERE clause.
  720.  
  721.  WHILE
  722.    Sets a condition for the repeated execution of a statement or statement
  723.    block.
  724.  
  725.  Wildcard Characters
  726.    Used with the LIKE keyword to represent any character in a string when
  727.    searching for a char, varchar, or datetime value.
  728.  
  729.  WRITETEXT
  730.    Permits nonlogged, interactive updating of an existing text field.
  731.  
  732.  ────────────────────────────────────────────────────────────────────────────
  733.  
  734.  
  735.  
  736.  Aggregate Functions
  737.  ────────────────────────────────────────────────────────────────────────────
  738.  
  739.  
  740.  Function
  741.  
  742.  Returns summary values. Aggregate functions are SUM, AVG, COUNT, COUNT(*),
  743.  MAX, and MIN. They can be used in the select list or the HAVING clause of a
  744.  SELECT statement or subquery, and often appear in a statement that includes
  745.  a GROUP BY clause. (A similar type of aggregate function, called a row
  746.  aggregate function, is used in the COMPUTE clause.)
  747.  
  748.  
  749.  
  750.  Syntax
  751.  
  752.  Aggregate functions have the following syntax:
  753.  
  754.    aggregate_function ([DISTINCT] expression)
  755.  
  756.  Aggregate functions, their individual syntax, and the results they produce
  757.  are shown in the following table. Note that expression is usually a column
  758.  name. DISTINCT is optional for SUM, AVG, and COUNT but must not be used with
  759.  COUNT(*). COUNT(*) takes no parameters. The numeric datatypes are int,
  760.  smallint, tinyint, float, and money.
  761.  
  762. ╓┌───────────────────┌────────────────────────┌──────────────────────────────╖
  763.  Aggregate Function  Parameters               Result
  764.  ────────────────────────────────────────────────────────────────────────────
  765.  AVG                 ([DISTINCT] expression)  The average of the [distinct]
  766.                                               values in the numeric column
  767.  
  768.  COUNT               ([DISTINCT] expression)  The number of [distinct]
  769.                                               non-null values in the column
  770.  
  771.  COUNT               (*)                      The number of selected rows
  772.  
  773.  MAX                 (expression)             The highest value in the
  774.                                               expression
  775.  
  776.  MIN                 (expression)             The lowest value in the
  777.                                               expression
  778.  
  779.  Aggregate Function  Parameters               Result
  780.  ────────────────────────────────────────────────────────────────────────────
  781.  AVG                 ([DISTINCT] expression)  The average of the [distinct]
  782. 
  783.  SUM                 ([DISTINCT]expression)   The total of the [distinct]
  784.                                               values in the numeric column
  785.  
  786.  ────────────────────────────────────────────────────────────────────────────
  787.  
  788.  
  789.  
  790.  
  791.  Examples
  792.  
  793.    A. select avg(advance), sum(ytd_sales)
  794.     from titles
  795.     where type = "business"
  796.  
  797.  Example A calculates the average advance and the sum of year-to-date sales
  798.  for all business books. Each of these aggregate functions produces a single
  799.  summary value for all of the retrieved rows.
  800.  
  801.    B. select type, avg(advance), sum(ytd_sales)
  802.     from titles
  803.     group by type
  804.  
  805.  When used with a GROUP BY clause, aggregate functions produce single values
  806.  for each group, rather than for the whole table. The statement in example B
  807.  produces summary values for each type of book.
  808.  
  809.    C. select count(distinct city)
  810.     from authors
  811.  
  812.  Example C finds the number of different cities in which authors live.
  813.  
  814.    D. select type
  815.     from titles
  816.     group by type
  817.     having count(*) > 1
  818.  
  819.  Example D lists the types in the titles table but eliminates the types that
  820.  include only one book or none.
  821.  
  822.    E. select pub_id, sum(advance), avg(price)
  823.     from titles
  824.     group by pub_id
  825.     having sum(advance) > $25000 and avg(price) > $15
  826.  
  827.  Example E groups the titles table by publisher, and includes only those
  828.  groups of publishers who have paid more than $25,000 in total advances and
  829.  whose books average more than $15 in price.
  830.  
  831.  
  832.  Options
  833.  
  834.  DISTINCT
  835.    Eliminates duplicate values before an aggregate function is applied.
  836.    DISTINCT is optional with SUM, AVG, and COUNT.
  837.  
  838.  column_name
  839.    The name of a column.
  840.  
  841.  expression
  842.    A column name, a constant, a function, any combination of column names,
  843.    constants, and functions connected by arithmetic or bitwise operators, or
  844.    a subquery. In the context of the aggregate functions, an expression is
  845.    usually a column name. (See "Expressions" for more information.)
  846.  
  847.  SUM
  848.    Finds the sum of all values in the column. SUM can be used with numeric
  849.    columns only. Null values are ignored.
  850.  
  851.  AVG
  852.    Finds the average of the values in the column. AVG can be used with
  853.    numeric columns only. Null values are ignored.
  854.  
  855.  COUNT
  856.    Finds the number of non-null values in the column. When DISTINCT is
  857.    specified, COUNT finds the number of unique non-null values. COUNT can be
  858.    used with both numeric and character columns. Null values are ignored.
  859.  
  860.  COUNT(*)
  861.    Finds the number of rows. COUNT(*) does not take any parameters and cannot
  862.    be used with DISTINCT. All rows are counted, regardless of the presence of
  863.    null values.
  864.  
  865.  MAX
  866.    Finds the maximum value in the column. MAX can be used with numeric,
  867.    character, and datetime columns. With character columns, MAX finds the
  868.    value that is highest in the collating sequence. MAX ignores any null
  869.    values. MAX cannot be used with bit columns. DISTINCT is not available
  870.    since it is not meaningful with MAX.
  871.  
  872.  MIN
  873.    Finds the minimum value in the column. MIN can be used with numeric,
  874.    character, and datetime columns. With character columns, MIN finds the
  875.    value that is lowest in the sort sequence. MIN ignores any null values.
  876.    MIN cannot be used with bit columns. DISTINCT is not available since it is
  877.    not meaningful with MIN.
  878.  
  879.  
  880.  Comments
  881.  
  882.  Aggregate functions can be used in the select list or in the HAVING clause
  883.  of a SELECT statement. They cannot be used in a WHERE clause.
  884.  
  885.  Aggregate functions, which calculate summary values from the non-null values
  886.  in a particular column, can be applied to all rows in a table, in which case
  887.  they produce a single value, called a scalar aggregate function.
  888.  Alternatively, they can be applied to all rows that have the same value in a
  889.  specified column or expression (with the GROUP BY and, optionally, the
  890.  HAVING clause), in which case they produce a value for each group, called a
  891.  vector aggregate function. The results of the aggregate functions are shown
  892.  as new columns.
  893.  
  894.  You can nest a vector aggregate function inside a scalar aggregate function.
  895.  For example:
  896.  
  897. ╓┌─┌─────────────────────────────────────────┌─────────────┌─────────────────╖
  898.  ────────────────────────────────────────────────────────────────────────────
  899.    select type, avg(price), avg(avg(price))
  900.    from titles
  901.    group by type
  902.  
  903.    type
  904.    ------------                              ------------  ------------
  905.  ────────────────────────────────────────────────────────────────────────────
  906.   ------------                              ------------  ------------
  907.    UNDECIDED                                 NULL          NULL
  908.    business                                  13.73         15.23
  909.    mod_cook                                  11.49         15.23
  910.    popular_comp                              21.48         15.23
  911.    psychology                                13.50         15.23
  912.    trad_cook                                 15.96         15.23
  913.  
  914.    (6 rows affected)
  915.  
  916.  
  917.  The GROUP BY clause applies to the vector aggregate function─in this case,
  918.  AVG(price).
  919.  
  920.  The scalar aggregate function, AVG(AVG(price)), returns the average price of
  921.  all books in the titles table.
  922.  
  923.  In standard SQL, all the columns in a select list that includes an aggregate
  924.  function must either have aggregate functions applied to them or be in the
  925.  GROUP BY list. TRANSACT-SQL has no such restrictions.
  926.  
  927.  Example A shows a SELECT statement with the standard restrictions; example B
  928.  shows the same statement with another item (title_id) added to the select
  929.  list to illustrate the difference in displays. These extra columns can also
  930.  be referenced in a HAVING clause.
  931.  
  932.    A. select type, avg(price), avg(advance)
  933.     from titles
  934.  
  935.     group by type
  936.  
  937. ╓┌─┌──────────────────┌─────────────┌────────────────────────────────────────╖
  938.  ────────────────────────────────────────────────────────────────────────────
  939.    type
  940.    ------------       ------------  ------------
  941.    UNDECIDED          NULL          NULL
  942.    business           13.73         6,281.25
  943.    mod_cook           11.49         7,500.00
  944.    popular_comp       21.48         7,500.00
  945.    psychology         13.50         4,255.00
  946.    trad_cook          15.96         6,333.33
  947.  ────────────────────────────────────────────────────────────────────────────
  948.   trad_cook          15.96         6,333.33
  949.    (6 rows affected)
  950.  
  951.  
  952.    B. select type, title_id, avg(price), avg(advance)
  953.     from titles
  954.     group by type
  955.     order by type
  956.  
  957. ╓┌─┌───────────────────┌───────────┌───────────┌─────────────────────────────╖
  958.  ────────────────────────────────────────────────────────────────────────────
  959.    type                title_id
  960.    ------------        ----------  ----------  -----------
  961.    UNDECIDED           MC3026      NULL        NULL
  962.    business            BU1032      13.73       6,281.25
  963.    business            BU1111      13.73       6,281.25
  964.    business            BU2075      13.73       6,281.25
  965.    business            BU7832      13.73       6,281.25
  966.    mod_cook            MC2222      11.49       7,500.00
  967.    mod_cook            MC3021      11.49       7,500.00
  968.  ────────────────────────────────────────────────────────────────────────────
  969.   mod_cook            MC3021      11.49       7,500.00
  970.    popular_comp        PC1035      21.48       7,500.00
  971.    popular_comp        PC8888      21.48       7,500.00
  972.    popular_comp        PC9999      21.48       7,500.00
  973.    psychology          PS1372      13.50       4,255.00
  974.    psychology          PS2091      13.50       4,255.00
  975.    psychology          PS2106      13.50       4,255.00
  976.    psychology          PS3333      13.50       4,255.00
  977.    psychology          PS7777      13.50       4,255.00
  978.    trad_cook           TC3218      15.96       6,333.33
  979.    trad_cook           TC4203      15.96       6,333.33
  980.    trad_cook           TC7777      15.96       6,333.33
  981.  
  982.    (18 rows affected)
  983.  
  984.  
  985.  You can use either a column name or any other expression (except a column
  986.  heading or alias) after GROUP BY.
  987.  
  988.  Null values in the GROUP BY column are put into a single group.
  989.  
  990.  The COMPUTE clause in a SELECT statement uses row aggregate functions to
  991.  produce summary values. The row aggregate functions make it possible to
  992.  retrieve detail and summary rows with one statement. The following example
  993.  illustrates this feature:
  994.  
  995.    select type, title_id, price, advance
  996.     from titles
  997.     where type = "psychology"
  998.     order by type
  999.     compute sum(price), sum(advance) by type
  1000.  
  1001. ╓┌───────┌───────────────────┌─────────────┌───────────────────┌─────────────
  1002.  ─────────────────────────────────────────────────────────────────────────────
  1003.          type                title_id      price               advance
  1004.  
  1005.          ------------        ------------  ----------          -----------
  1006.  
  1007.          psychology          PS1372        21.59               7,000.00
  1008.  
  1009.          psychology          PS2091        10.95               2,275.00
  1010.  ─────────────────────────────────────────────────────────────────────────────
  1011.         psychology          PS2091        10.95               2,275.00
  1012.  
  1013.          psychology          PS2106        7.00                6,000.00
  1014.  
  1015.          psychology          PS3333        19.99               2,000.00
  1016.  
  1017.          psychology          PS7777        7.99                4,000.00
  1018.  
  1019.                                            sum                 sum
  1020.  
  1021.                                            -----------67.52    -------------21
  1022.  
  1023.  
  1024.  
  1025.          (6 rows affected)
  1026.  
  1027.  
  1028.  
  1029.  Note the difference in display between this example and the earlier examples
  1030.  without COMPUTE.
  1031.  
  1032.  You can use a HAVING clause without a GROUP BY clause.
  1033.  
  1034.  Because each aggregate function in a query requires its own work table, a
  1035.  query using aggregate functions can exceed the maximum number of tables
  1036.  allowed in a query (16).
  1037.  
  1038.  If there are columns in the select list that neither have aggregate
  1039.  functions applied to them nor are included in the query's GROUP BY clause
  1040.  (illegal in standard SQL), the meanings of HAVING and WHERE are somewhat
  1041.  different. In this situation, a WHERE clause restricts the rows included in
  1042.  the calculation of the aggregate function but does not restrict the rows
  1043.  returned by the query. Conversely, a HAVING clause restricts the rows
  1044.  returned by the query but does not affect the calculation of the aggregate
  1045.  function. See "GROUP BY and HAVING Clauses" for examples.
  1046.  
  1047.  When you sum or average integer data, TRANSACT-SQL treats the result as an
  1048.  int value, even if the datatype of the column is smallint or tinyint. To
  1049.  avoid overflow errors in DB-LIBRARY programs, declare all variables for
  1050.  results of averages or sums as type int.
  1051.  
  1052.  
  1053.  See Also
  1054.  
  1055.  COMPUTE Clause, Functions, GROUP BY and HAVING Clauses, Row Aggregate
  1056.  Functions, Search Conditions, SELECT, WHERE Clause
  1057.  
  1058.  
  1059.  ALTER DATABASE
  1060.  ────────────────────────────────────────────────────────────────────────────
  1061.  
  1062.  
  1063.  Function
  1064.  
  1065.  Increases the amount of disk space allocated to a database.
  1066.  
  1067.  
  1068.  Syntax
  1069.  
  1070.    ALTER DATABASE database_name
  1071.      [ON {DEFAULT | database_device} [=size] [, database_device
  1072.      [= size]]...]
  1073.  
  1074.  
  1075.  Examples
  1076.  
  1077.    A. alter database pubs
  1078.  
  1079.    B. alter database pubs
  1080.     on file1 = 3
  1081.  
  1082.  
  1083.  Options
  1084.  
  1085.  database_name
  1086.    The name of the database whose storage size is to be changed.
  1087.  
  1088.  ON
  1089.    Indicates a size and/or location for the database extension. The default
  1090.    extension size is 1 megabyte.
  1091.  
  1092.  DEFAULT
  1093.    Indicates that ALTER DATABASE can put the database extension on any
  1094.    default database device(s) (as shown in sysdevices.status). To specify a
  1095.    size for the database extension without specifying the location, use "ON
  1096.    DEFAULT = size." (The default size is 1 megabyte.) To change a database
  1097.    device's status to the default, use the sp_diskdefault system procedure.
  1098.  
  1099.  database_device
  1100.    The logical name of the database device on which you want to locate the
  1101.    database extension. The default size of database_device is 2 megabytes. A
  1102.    database can occupy more than one database device with different amounts
  1103.    of space on each.
  1104.  
  1105.  size
  1106.    The amount of space allocated to the database extension, in megabytes. The
  1107.    minimum extension is 1 megabyte. The default value is 1 megabyte for a
  1108.    default database device and 2 megabytes for a nondefault database device.
  1109.    Legal values range from 1 to 215. If SQL Server can't allocate the
  1110.    requested size, it will do as much as it can in half-megabyte units with a
  1111.    minimum of 1 megabyte.
  1112.  
  1113.  
  1114.  Comments
  1115.  
  1116.  The Database Owner or System Administrator must be using the master database
  1117.  to execute the ALTER DATABASE statement.
  1118.  
  1119.  If you don't specify a location or size, the default location is any default
  1120.  database device indicated in sysdevices and the default size is 1 megabyte.
  1121.  
  1122.  
  1123.  If SQL Server can't handle your request, it allocates as much space as
  1124.  possible on each database device and prints a message telling how much space
  1125.  has been allocated on each database device.
  1126.  
  1127.  It is important to back up the master database with the DUMP DATABASE or
  1128.  DUMP TRANSACTION statement after each use of ALTER DATABASE. This makes
  1129.  recovery easier and safer if the master database is damaged. (If you use
  1130.  ALTER DATABASE and fail to back up the master database, you may subsequently
  1131.  be able to recover the changes with DISK REFIT.)
  1132.  
  1133.  To increase the amount of storage space allocated for the transaction log,
  1134.  give the name of the log's database device in the ON clause when you execute
  1135.  the ALTER DATABASE statement. Then execute sp_logdevice to make the newly
  1136.  allocated space available for the log.
  1137.  
  1138.  To rename a database, execute the sp_renamedb system procedure, which takes
  1139.  the old name of the database and its new name as parameters. For information
  1140.  on a database, execute sp_helpdb. To get a report on the amount of space
  1141.  used in a database, execute sp_spaceused.
  1142.  
  1143.  
  1144.  Permissions
  1145.  
  1146.  ALTER DATABASE permission defaults to the Database Owner if he or she has
  1147.  CREATE DATABASE permission. It cannot be transferred. This prevents
  1148.  unauthorized users from allocating too much disk space. The System
  1149.  Administrator can also alter databases.
  1150.  
  1151.  
  1152.  See Also
  1153.  
  1154.  CREATE DATABASE, DROP DATABASE, sp_diskdefault, sp_helpdb, sp_logdevice,
  1155.  sp_renamedb, sp_spaceused
  1156.  
  1157.  
  1158.  ALTER TABLE
  1159.  ────────────────────────────────────────────────────────────────────────────
  1160.  
  1161.  Function
  1162.  
  1163.  Adds new columns to an existing table.
  1164.  
  1165.  
  1166.  Syntax
  1167.  
  1168.    ALTER TABLE [[database.]owner.]table_name
  1169.      ADD column_name datatype NULL [, column_name datatype NULL...]
  1170.  
  1171.  
  1172.  Examples
  1173.  
  1174.    alter table publishers
  1175.     add manager_name varchar(40) null
  1176.  
  1177.  
  1178.  Options
  1179.  
  1180.  datatype
  1181.    Any of the system datatypes except bit and any user-defined datatype
  1182.    except those based on bit.
  1183.  
  1184.  NULL
  1185.    A required parameter because the initial values in the new column must be
  1186.    set to NULL in already existing rows.
  1187.  
  1188.  
  1189.  Comments
  1190.  
  1191.  Columns of type bit cannot be added to an existing table.
  1192.  
  1193.  The number of columns in a table cannot exceed 250.
  1194.  
  1195.  To rename a table, execute the sp_rename system procedure. For information
  1196.  on a table and its columns, use sp_help.
  1197.  
  1198.  
  1199.  Permissions
  1200.  
  1201.  ALTER TABLE permission defaults to the table owner. It cannot be
  1202.  transferred. The System Administrator can alter users' tables.
  1203.  
  1204.  
  1205.  See Also
  1206.  
  1207.  CREATE TABLE, DROP TABLE, sp_help, sp_rename
  1208.  
  1209.  
  1210.  Batch Queries
  1211.  ────────────────────────────────────────────────────────────────────────────
  1212.  
  1213.  
  1214.  Function
  1215.  
  1216.  A batch or batch file is a set of SQL statements submitted together and
  1217.  executed as a group, one after the other. A batch is terminated by an
  1218.  end-of-batch signal. With the isql program, the signal is the go command on
  1219.  a line by itself. For details on the isql program, see Chapter 3, "Utility
  1220.  Programs."
  1221.  
  1222.  
  1223.  Examples
  1224.  
  1225.    A. select count(*) from titles
  1226.     select count(*) from authors
  1227.     go
  1228.  
  1229.    B. create table test
  1230.      (column1 char(10), column2 int)
  1231.     insert test
  1232.      values ("hello", 598)
  1233.     select * from test
  1234.     go
  1235.  
  1236.    C. use master
  1237.     go
  1238.     select count(*)
  1239.     from sysdatabases
  1240.     go
  1241.  
  1242.  
  1243.  Comments
  1244.  
  1245.  CREATE PROCEDURE, CREATE RULE, CREATE DEFAULT, CREATE TRIGGER, and CREATE
  1246.  VIEW statements cannot be combined with other statements in a batch. They
  1247.  must be submitted singularly. CREATE DATABASE, CREATE TABLE, and CREATE
  1248.  INDEX can be combined with other statements in a single batch.
  1249.  
  1250.  USE must be submitted in a batch before you can reference objects in that
  1251.  database.
  1252.  
  1253.  You cannot drop an object and then reference or re-create it in the same
  1254.  batch.
  1255.  
  1256.  Any options set with a SET statement take effect at the end of the batch.
  1257.  You can combine SET statements and queries in the same batch, but the SET
  1258.  options won't apply to the queries in that batch.
  1259.  
  1260.  You can submit batches through the SQL Server Administration Facility or
  1261.  from a file with the isql program. A file submitted to isql can include more
  1262.  than one batch of SQL statements if each batch is terminated by the go
  1263.  command.
  1264.  
  1265.  If there is an error anywhere in a batch, none of its statements are
  1266.  executed. A batch is considered a transaction.
  1267.  
  1268.  
  1269.  See Also
  1270.  
  1271.  CREATE DEFAULT, CREATE PROCEDURE, CREATE RULE, CREATE TRIGGER, CREATE VIEW,
  1272.  SET, USE
  1273.  
  1274.  
  1275.  BEGIN...END
  1276.  ────────────────────────────────────────────────────────────────────────────
  1277.  
  1278.  
  1279.  Function
  1280.  
  1281.  Enclose a series of SQL statements so that control-of-flow language, such as
  1282.  IF...ELSE, affects the performance of the whole group.
  1283.  
  1284.  
  1285.  Syntax
  1286.  
  1287.    BEGIN
  1288.     statement_block
  1289.     END
  1290.  
  1291.  
  1292.  Examples
  1293.  
  1294.    A. if (select avg(price) from titles) < $15
  1295.     begin
  1296.      update titles
  1297.      set price = price * $2
  1298.      select title, price
  1299.      from titles
  1300.      where price > $28
  1301.     end
  1302.  
  1303.  Without BEGIN and END, the IF condition would cause execution of only one
  1304.  SQL statement.
  1305.  
  1306.    B. create trigger deltitle
  1307.     on titles
  1308.     for delete
  1309.     as
  1310.     if
  1311.      (select count(*) from deleted, sales
  1312.      where sales.title_id = deleted.title_id) > 0
  1313.      begin
  1314.      rollback transaction
  1315.      print "You can't delete a title with sales."
  1316.      end
  1317.     else
  1318.      print "Deletion successful─no sales for this title."
  1319.  
  1320.  
  1321.  Options
  1322.  
  1323.  statement_block
  1324.    A series of statements enclosed by BEGIN and END.
  1325.  
  1326.  
  1327.  Comments
  1328.  
  1329.  BEGIN...END blocks can be nested within other BEGIN...END blocks.
  1330.  
  1331.  
  1332.  See Also
  1333.  
  1334.  Control-of-Flow Language
  1335.  
  1336.  
  1337.  BEGIN TRANSACTION
  1338.  ────────────────────────────────────────────────────────────────────────────
  1339.  
  1340.  
  1341.  Function
  1342.  
  1343.  Marks the starting point of a user-specified transaction.
  1344.  
  1345.  
  1346.  Syntax
  1347.  
  1348.    BEGIN TRANsaction [transaction_name]
  1349.  
  1350.  
  1351.  Examples
  1352.  
  1353.    begin transaction royaltychange
  1354.      /* A user sets out to change the royalty split for the
  1355.      two authors of The Gourmet Microwave. Since the
  1356.      database would be inconsistent between the two updates,
  1357.      they must be grouped into a user-defined transaction. */
  1358.  
  1359.     update titleauthor
  1360.     set royaltyper = 65
  1361.     from titleauthor, titles
  1362.     where royaltyper = 75
  1363.     and titleauthor.title_id = titles.title_id
  1364.     and title = "The Gourmet Microwave"
  1365.  
  1366.     update titleauthor
  1367.     set royaltyper = 35
  1368.     from titleauthor, titles
  1369.     where royaltyper = 25
  1370.     and titleauthor.title_id = titles.title_id
  1371.     and title = "The Gourmet Microwave"
  1372.  
  1373.     save transaction percentchanged
  1374.      /* After having updated the royaltyper entries
  1375.      for the two authors, the user inserts the savepoint
  1376.      percentchanged and then determines how
  1377.      a 10% increase in the book's price would affect the
  1378.      authors' royalty earnings. */
  1379.  
  1380.     update titles
  1381.     set price = price * $1.1
  1382.     where title = "The Gourmet Microwave"
  1383.  
  1384.     select (price * royalty * ytd_sales) * royaltyper
  1385.     from titles, titleauthor
  1386.     where title = "The Gourmet Microwave"
  1387.     and titles.title_id = titleauthor.title_id
  1388.      /* The transaction is rolled back to the savepoint
  1389.      with the ROLLBACK TRANSACTION statement. */
  1390.  
  1391.    rollback transaction percentchanged
  1392.  
  1393.     commit transaction
  1394.  
  1395.      /* End of royaltychange. */
  1396.  
  1397.  
  1398.  Options
  1399.  
  1400.  transaction_name
  1401.    The name assigned to this transaction. It must conform to the rules for
  1402.    identifiers.
  1403.  
  1404.  
  1405.  Comments
  1406.  
  1407.  Define a transaction by enclosing SQL statements and/or stored procedures
  1408.  within the statements BEGIN TRANSACTION and COMMIT TRANSACTION.
  1409.  
  1410.  To cancel an entire transaction, use the statement ROLLBACK TRANSACTION
  1411.  transaction_name. All of the transaction's statements or procedures that
  1412.  have been completed are undone.
  1413.  
  1414.  To cancel part of a transaction, use the statement ROLLBACK TRANSACTION
  1415.  savepoint_name. (The savepoint was inserted inside the transaction earlier
  1416.  with a SAVE TRANSACTION statement.) All of the transaction's statements or
  1417.  procedures between the savepoint and the ROLLBACK TRANSACTION statement are
  1418.  undone.
  1419.  
  1420.  After a transaction is rolled back to a savepoint, it must proceed to
  1421.  completion (with more SQL statements if desired and a COMMIT TRANSACTION
  1422.  statement), or it must be canceled altogether (by rolling it back to its
  1423.  beginning).
  1424.  
  1425.  The ROLLBACK TRANSACTION statement must appear within a transaction: you
  1426.  can't rollback a transaction after COMMIT TRANSACTION has been entered.
  1427.  
  1428.  If no savepoint_name or transaction_name is given with the ROLLBACK
  1429.  TRANSACTION statement, the transaction is rolled back to the previous BEGIN
  1430.  TRANSACTION.
  1431.  
  1432.  The following statements cannot be used inside a user-defined transaction:
  1433.  CREATE DATABASE, CREATE TABLE, CREATE INDEX, all DROP statements, SELECT
  1434.  INTO (because it creates a table), GRANT, REVOKE, ALTER DATABASE, ALTER
  1435.  TABLE, TRUNCATE TABLE, RECONFIGURE, LOAD DATABASE, LOAD TRANSACTION, and
  1436.  DISK INIT. In addition, some of the system procedures cannot be used inside
  1437.  user-defined transactions because they create temporary tables.
  1438.  
  1439.  Transactions can be nested inside each other.
  1440.  
  1441.  
  1442.  Permissions
  1443.  
  1444.  BEGIN TRANSACTION permission defaults to all users. No permission is
  1445.  required to use it.
  1446.  
  1447.  
  1448.  See Also
  1449.  
  1450.  COMMIT TRANSACTION, ROLLBACK TRANSACTION, SAVE TRANSACTION
  1451.  
  1452.  
  1453.  BREAK
  1454.  ────────────────────────────────────────────────────────────────────────────
  1455.  
  1456.  
  1457.  Function
  1458.  
  1459.  Controls operation of statements in a WHILE loop. BREAK is often (but not
  1460.  always) activated by an IF test. BREAK causes an exit from the WHILE loop.
  1461.  
  1462.  
  1463.  Syntax
  1464.  
  1465.    WHILE
  1466.     boolean expression
  1467.     statement
  1468.     BREAK
  1469.     statement
  1470.     CONTINUE
  1471.  
  1472.  
  1473.  Examples
  1474.  
  1475.    while (select avg(price) from titles) < $30
  1476.     begin
  1477.      update titles
  1478.      set price = price * $2
  1479.      select max(price) from titles
  1480.      if (select max(price) from titles) > $50
  1481.      break
  1482.      else
  1483.      continue
  1484.     end
  1485.     begin
  1486.     print "Too much for the market to bear"
  1487.     end
  1488.  
  1489.  If the average price is less than $30, double the prices. Then select the
  1490.  maximum price. If it is less than or equal to $50, restart the WHILE loop
  1491.  and double the prices again. If the maximum price is more than $50, exit the
  1492.  WHILE loop and print a message.
  1493.  
  1494.  
  1495.  Comments
  1496.  
  1497.  BREAK causes an exit from the WHILE loop. Any statements that appear after
  1498.  the END keyword that marks the end of the loop are executed.
  1499.  
  1500.  If two or more WHILE loops are nested, the inner BREAK exits to the next
  1501.  outermost loop. First, all the statements after the end of the inner loop
  1502.  run, and then the next outermost loop restarts.
  1503.  
  1504.  
  1505.  See Also
  1506.  
  1507.  CONTINUE, Control-of-Flow Language, Expressions, WHILE
  1508.  
  1509.  
  1510.  Browse Mode
  1511.  ────────────────────────────────────────────────────────────────────────────
  1512.  
  1513.  
  1514.  Function
  1515.  
  1516.  Supports the ability to perform updates while viewing data. Browse mode is
  1517.  used in an application program using DB-LIBRARY. The FOR BROWSE clause is
  1518.  used in a SELECT statement.
  1519.  
  1520.  
  1521.  Syntax
  1522.  
  1523.    FOR BROWSE
  1524.  
  1525.  
  1526.  Examples
  1527.  
  1528.  See the SQL Server Programmer's Reference for an example of a DB-LIBRARY
  1529.  application that uses browse mode.
  1530.  
  1531.  
  1532.  Comments
  1533.  
  1534.  A table can be browsed in an application program if
  1535.  
  1536.  
  1537.    ■   Its rows have been timestamped by including a column named timestamp
  1538.        in the table definition or by adding the timestamp column with ALTER
  1539.        TABLE:
  1540.  
  1541.  
  1542.                CREATE TABLE browsetest (col1 int, timestamp, col3 char(7))
  1543.  
  1544.  
  1545.        or
  1546.  
  1547.        ALTER TABLE table_name
  1548.                ADD timestamp
  1549.  
  1550.        A column named timestamp automatically has the system datatype
  1551.        timestamp and is automatically updated.
  1552.  
  1553.    ■   It has a unique key.
  1554.  
  1555.    ■   The keywords FOR BROWSE are at the end of the SELECT statement sent to
  1556.        SQL Server.
  1557.  
  1558.  
  1559.  The use of the keyword HOLDLOCK is forbidden in a SELECT statement that
  1560.  includes the FOR BROWSE clause.
  1561.  
  1562.  
  1563.  See Also
  1564.  
  1565.  Datatypes, SELECT, sp_primarykey
  1566.  
  1567.  
  1568.  
  1569.  
  1570.  
  1571.  CHECKPOINT
  1572.  ────────────────────────────────────────────────────────────────────────────
  1573.  
  1574.  
  1575.  Function
  1576.  
  1577.  Forces all dirty pages (pages on which there have been updates since the
  1578.  last checkpoint) in the current database to be written to the disk.
  1579.  Checkpoints caused by the CHECKPOINT statement supplement automatic
  1580.  checkpoints, which occur at intervals calculated by SQL Server on the basis
  1581.  of the configurable value for maximum acceptable recovery time.
  1582.  
  1583.  
  1584.  Syntax
  1585.  
  1586.    CHECKPOINT
  1587.  
  1588.  
  1589.  Examples
  1590.  
  1591.    checkpoint
  1592.  
  1593.  All dirty pages in the current database are written to the disk, regardless
  1594.  of the system checkpoint schedule.
  1595.  
  1596.  
  1597.  Comments
  1598.  
  1599.  The CHECKPOINT statement saves work in recovery by identifying a point at
  1600.  which all completed transactions are guaranteed to have been written to the
  1601.  disk.
  1602.  
  1603.  A typical checkpoint takes one second, though this figure varies depending
  1604.  on the amount of activity on SQL Server.
  1605.  
  1606.  The automatic checkpoint interval is calculated by SQL Server on the basis
  1607.  of system activity and the recovery interval value in the system table
  1608.  syscurconfigs. The recovery interval determines checkpoint frequency by
  1609.  specifying the amount of time it should take the system to recover. Reset
  1610.  this value by executing the sp_configure system procedure and the
  1611.  RECONFIGURE statement. For more information, see the SQL Server System
  1612.  Administrator's Guide.
  1613.  
  1614.  Use the CHECKPOINT statement as a precautionary measure only in special
  1615.  circumstances. For example, if the automatic checkpoint is infrequent
  1616.  because the recovery interval is set high, you might want to use the
  1617.  CHECKPOINT statement after inserting rows with bulk copy.
  1618.  
  1619.  Use the CHECKPOINT statement after changing a database option with the
  1620.  sp_dboption system procedure.
  1621.  
  1622.  If you change a database option with sp_dboption inside a user-defined
  1623.  transaction and then roll back that transaction you must use another
  1624.  CHECKPOINT statement to make the rollback take effect on the option change.
  1625.  Here's an example:
  1626.  
  1627.    begin transaction
  1628.     use master
  1629.     <execute>
  1630.     sp_dboption 'pubs', 'single', 'true'
  1631.     <execute>
  1632.     use pubs
  1633.     <execute>
  1634.     checkpoint
  1635.     <execute>
  1636.     rollback transaction
  1637.     <execute>
  1638.     /*
  1639.     ** If the following checkpoint is not used, the
  1640.     ** "pubs" database remains single-user.
  1641.     */
  1642.     checkpoint
  1643.     <execute>
  1644.  
  1645.  
  1646.  Permissions
  1647.  
  1648.  Permission to use the CHECKPOINT statement defaults to the Database Owner.
  1649.  It cannot be transferred.
  1650.  
  1651.  
  1652.  See Also
  1653.  
  1654.  RECONFIGURE, sp_dboption
  1655.  
  1656.  
  1657.  Comments
  1658.  ────────────────────────────────────────────────────────────────────────────
  1659.  
  1660.  
  1661.  Function
  1662.  
  1663.  Provide information about SQL statements, statement blocks, and stored
  1664.  procedures.
  1665.  
  1666.  
  1667.  Syntax
  1668.  
  1669.    /* text of comment */
  1670.  
  1671.  
  1672.  Examples
  1673.  
  1674.    /* this procedure finds rules by user name */
  1675.     create procedure findmyrule @nm varchar(30) = null
  1676.     as
  1677.     if @nm is null
  1678.     begin
  1679.      print "You must give a user name"
  1680.      return
  1681.      print "I have returned"
  1682.     /* this statement follows RETURN,
  1683.     ** so won't be executed */
  1684.     end
  1685.     else /* print the rule names and IDs, and the user ID */
  1686.      select sysobjects.name, sysobjects.id, sysobjects.uid
  1687.      from sysobjects, master..syslogins
  1688.      where master..syslogins.name = @nm
  1689.      and sysobjects.uid = master..syslogins.suid
  1690.      and sysobjects.type = "R"
  1691.  
  1692.  
  1693.  Comments
  1694.  
  1695.  Comments can be inserted on a line by themselves or at the end of a command
  1696.  line. Multiple-line comments are allowed as long as they are surrounded by
  1697.  /* and */. A stylistic convention often used for multiple-line comments is
  1698.  to begin the first line with /* and subsequent lines with **. The comment is
  1699.  ended with */.
  1700.  
  1701.  There is no maximum length for comments.
  1702.  
  1703.  Comments can be nested.
  1704.  
  1705.  ────────────────────────────────────────────────────────────────────────────
  1706.  NOTE
  1707.  Do not include a go command within a comment because it will produce an
  1708.  error message.
  1709.  ────────────────────────────────────────────────────────────────────────────
  1710.  
  1711.  
  1712.  COMMIT TRANSACTION
  1713.  ────────────────────────────────────────────────────────────────────────────
  1714.  
  1715.  
  1716.  Function
  1717.  
  1718.  Marks the ending point of a user-defined transaction.
  1719.  
  1720.  
  1721.  Syntax
  1722.  
  1723.    COMMIT TRANsaction [transaction_name]
  1724.  
  1725.  
  1726.  Examples
  1727.  
  1728.    begin transaction royaltychange
  1729.      /* A user sets out to change the royalty split for the
  1730.      two authors of The Gourmet Microwave. Since the
  1731.      database would be inconsistent between the two updates,
  1732.      they must be grouped into a user-defined transaction. */
  1733.  
  1734.     update titleauthor
  1735.     set royaltyper = 65
  1736.     from titleauthor, titles
  1737.     where royaltyper = 75
  1738.     and titleauthor.title_id = titles.title_id
  1739.     and title = "The Gourmet Microwave"
  1740.  
  1741.     update titleauthor
  1742.     set royaltyper = 35
  1743.     from titleauthor, titles
  1744.     where royaltyper = 25
  1745.     and titleauthor.title_id = titles.title_id
  1746.     and title = "The Gourmet Microwave"
  1747.  
  1748.     save transaction percentchanged
  1749.      /* After having updated the royaltyper entries
  1750.      for the two authors, the user inserts the savepoint
  1751.      percentchanged and then experiments to see how
  1752.      a 10% increase in the book's price would affect the
  1753.      authors' royalty earnings. */
  1754.  
  1755.     update titles
  1756.     set price = price * $1.1
  1757.     where title = "The Gourmet Microwave"
  1758.  
  1759.     select (price * royalty * ytd_sales) * royaltyper
  1760.     from titles, titleauthor
  1761.     where title = "The Gourmet Microwave"
  1762.     and titles.title_id = titleauthor.title_id
  1763.      /* The transaction is rolled back to the savepoint
  1764.      with the ROLLBACK TRANSACTION statement. */
  1765.  
  1766.    rollback transaction percentchanged
  1767.  
  1768.     commit transaction
  1769.  
  1770.      /* End of royaltychange. */
  1771.  
  1772.  
  1773.  Options
  1774.  
  1775.  transaction_name
  1776.    The name assigned to the transaction. It must conform to the rules for
  1777.    identifiers. (See "Identifiers" for details.)
  1778.  
  1779.  
  1780.  Comments
  1781.  
  1782.  Define a transaction by enclosing SQL statements and/or stored procedures
  1783.  with the BEGIN TRANSACTION and COMMIT TRANSACTION statements.
  1784.  
  1785.  A savepoint is a marker set by the SAVE TRANSACTION savepoint_name statement
  1786.  within a transaction. You can cancel part of a transaction with the
  1787.  following statement:
  1788.  
  1789.    rollback tran savepoint_name
  1790.  
  1791.  All of the statements or procedures between the savepoint and ROLLBACK
  1792.  TRANSACTION are undone.
  1793.  
  1794.  To cancel an entire transaction, use the ROLLBACK TRANSACTION statement. All
  1795.  of the transaction's statements or procedures are undone.
  1796.  
  1797.  If no savepoint name or transaction name is given with the ROLLBACK
  1798.  TRANSACTION statement, the transaction is rolled back to the previous BEGIN
  1799.  TRANSACTION.
  1800.  
  1801.  The ROLLBACK TRANSACTION statement must appear within a transaction: you
  1802.  can't rollback a transaction after COMMIT TRANSACTION has been entered.
  1803.  
  1804.  After a transaction is rolled back to a savepoint, it must proceed to
  1805.  completion (with more SQL statements if desired and a COMMIT TRANSACTION
  1806.  statement), or it must be canceled altogether (by rolling it back to its
  1807.  beginning). Until you execute COMMIT TRANSACTION, SQL Server will consider
  1808.  all subsequent statements to be part of the transaction, until it encounters
  1809.  another BEGIN TRANSACTION statement. At that point, SQL Server rolls the
  1810.  original transaction back and displays an error message.
  1811.  
  1812.  You may want to insert a comment giving the name of the transaction being
  1813.  ended, as in the example.
  1814.  
  1815.  
  1816.  Permissions
  1817.  
  1818.  COMMIT TRANSACTION permission defaults to all users. No permission is
  1819.  required to use it.
  1820.  
  1821.  
  1822.  See Also
  1823.  
  1824.  BEGIN TRANSACTION, ROLLBACK TRANSACTION, SAVE TRANSACTION
  1825.  
  1826.  
  1827.  COMPUTE Clause
  1828.  ────────────────────────────────────────────────────────────────────────────
  1829.  
  1830.  
  1831.  Function
  1832.  
  1833.  Generates summary values in a SELECT statement with row aggregate functions
  1834.  (SUM, AVG, MIN, MAX, and COUNT). The summary values appear as additional
  1835.  rows in the query results (unlike the aggregate function results, which
  1836.  appear as new columns), which allows you to see the detail and summary rows
  1837.  in one set of results. You can calculate summary values for subgroups, and
  1838.  you can calculate more than one aggregate function for the same group.
  1839.  
  1840.  
  1841.  Syntax
  1842.  
  1843.    COMPUTE row_aggregate(column_name)
  1844.      [, row_aggregate(column_name)...]
  1845.      [BY column_name [, column_name]...]
  1846.  
  1847.  
  1848.  Examples
  1849.  
  1850.    A. select type, price
  1851.     from titles
  1852.     where price > $12
  1853.     and type like "%cook"
  1854.     order by type, price
  1855.     compute sum(price) by type
  1856.  
  1857. ╓┌─┌──────────────────┌──────────────────────────────────────────────────────╖
  1858.  ────────────────────────────────────────────────────────────────────────────
  1859.    type               price
  1860.    ---------          ------------
  1861.    mod_cook           19.99
  1862.                       sum
  1863.                       ------------
  1864.                       19.99
  1865.    type               price
  1866.    ---------          ------------
  1867.    trad_cook          14.99
  1868.    trad_cook          20.95
  1869.                       sum
  1870.                       ------------
  1871.  ────────────────────────────────────────────────────────────────────────────
  1872.                      ------------
  1873.                       35.94
  1874.  
  1875.    (5 rows affected)
  1876.  
  1877.  
  1878.  Example A calculates the sum of the prices of each type of cookbook that
  1879.  costs more than $12.
  1880.  
  1881.    B. select type, price, advance
  1882.     from titles titles
  1883.     where price > $12
  1884.     and type like "%cook"
  1885.     order by type, price
  1886.     compute sum(price), sum(advance) by type
  1887.  
  1888. ╓┌─┌──────────────────┌──────────┌───────────────────────────────────────────╖
  1889.  ────────────────────────────────────────────────────────────────────────────
  1890.    type               price      advance
  1891.    ---------          ---------  ------------
  1892.  ────────────────────────────────────────────────────────────────────────────
  1893.   ---------          ---------  ------------
  1894.    mod_cook           19.99      0.00
  1895.                       sum        sum
  1896.                       ---------  ------------
  1897.                       19.99      0.00
  1898.    type               price      advance
  1899.    ---------          ---------  ------------
  1900.    trad_cook          14.99      8,000.00
  1901.    trad_cook          20.95      7,000.00
  1902.                       sum        sum
  1903.                       ---------  ------------
  1904.                       35.94      15,000.00
  1905.  
  1906.    (5 rows affected)
  1907.  
  1908.  
  1909.  Example B calculates the sum of the prices and advances for each type of
  1910.  cookbook that costs more than $12.
  1911.  
  1912.    C. select type, price, advance
  1913.     from titles
  1914.     where price > $12
  1915.     and type like "%cook"
  1916.     order by type, price
  1917.     compute sum(price), max(advance) by type
  1918.  
  1919. ╓┌─┌──────────────────┌─────────────┌────────────────────────────────────────╖
  1920.  ────────────────────────────────────────────────────────────────────────────
  1921.    type               price         advance
  1922.    ---------          ------------  ------------
  1923.    mod_cook           19.99         0.00
  1924.                       sum
  1925.                       ------------
  1926.                       19.99
  1927.                                     max
  1928.                                     ------------
  1929.                                     0.00
  1930.    type               price         advance
  1931.    ----------         ------------  ------------
  1932.    trad_cook          14.99         8,000.00
  1933.    trad_cook          20.95         7,000.00
  1934.  ────────────────────────────────────────────────────────────────────────────
  1935.   trad_cook          20.95         7,000.00
  1936.                       sum
  1937.                       ------------
  1938.                       35.94
  1939.                                     max
  1940.                                     ------------
  1941.                                     8,000.00
  1942.    (5 rows affected)
  1943.  
  1944.  
  1945.  Example C calculates the sum of the prices and maximum advance of each type
  1946.  of cookbook that costs more than $12.
  1947.  
  1948.    D. select type, pub_id, price
  1949.     from titles
  1950.     where price > $10
  1951.     and type = "psychology"
  1952.     order by type, pub_id, price
  1953.     compute sum(price) by type, pub_id
  1954.  
  1955. ╓┌─┌──────────────────┌────────────┌─────────────────────────────────────────╖
  1956.  ────────────────────────────────────────────────────────────────────────────
  1957.    type               pub_id       price
  1958.    ------------       ---------    ------------
  1959.    psychology         0736         10.95
  1960.    psychology         0736         19.99
  1961.                                    sum
  1962.                                    ------------
  1963.                                    30.94
  1964.    type               pub_id       price
  1965.    ------------       ----------   ------------
  1966.    psychology         0877         21.59
  1967.                                    sum
  1968.                                    ------------
  1969.                                    21.59
  1970.    (5 rows affected)
  1971.  
  1972.  
  1973.  Example D breaks on type and pub_id and calculates the sum of the prices of
  1974.  psychology books by type-publisher ID combination.
  1975.  
  1976.    E. select type, pub_id, price
  1977.     from titles
  1978.     where price > $10
  1979.     and type = "psychology"
  1980.     order by type, pub_id, price
  1981.     compute sum(price) by type, pub_id
  1982.     compute sum(price) by type
  1983.  
  1984. ╓┌─┌──────────────────┌───────────┌──────────────────────────────────────────╖
  1985.  ────────────────────────────────────────────────────────────────────────────
  1986.    type               pub_id      price
  1987.    -----------        ---------   ------------
  1988.    psychology         0736        10.95
  1989.    psychology         0736        19.99
  1990.                                   sum
  1991.                                   ------------
  1992.                                   30.94
  1993.    type               pub_id      price
  1994.    ----------         ---------   ------------
  1995.    psychology         0877        21.59
  1996.                                   sum
  1997.  ────────────────────────────────────────────────────────────────────────────
  1998.                                  sum
  1999.                                   ------------
  2000.                                   21.59
  2001.                                   sum
  2002.                                   ------------
  2003.                                   52.53
  2004.    (6 rows affected)
  2005.  
  2006.  
  2007.  Example E calculates the grand total of the prices of psychology books that
  2008.  cost more than $10, in addition to sums by type and pub_id.
  2009.  
  2010.    F. select type, price, advance
  2011.     from titles
  2012.     where price > $10
  2013.     and type like "%cook"
  2014.     compute sum(price), sum(advance)
  2015.  
  2016. ╓┌─┌──────────────────┌─────────────┌────────────────────────────────────────╖
  2017.  ────────────────────────────────────────────────────────────────────────────
  2018.  ────────────────────────────────────────────────────────────────────────────
  2019.    type               price         advance
  2020.    ---------          -----------   ------------
  2021.    mod_cook           19.99         0.00
  2022.    trad_cook          20.95         8,000.00
  2023.    trad_cook          11.95         4,000.00
  2024.    trad_cook          14.99         7,000.00
  2025.                       sum           sum
  2026.                       ------------  -----------
  2027.                       67.88         19,000.00
  2028.    (5 rows affected)
  2029.  
  2030.  
  2031.  Example F calculates the grand totals of the prices and advances of cooking
  2032.  books that cost over $10.
  2033.  
  2034.  
  2035.  Options
  2036.  
  2037.  row_aggregate
  2038.    One of the following:
  2039.  
  2040. ╓┌─────────────────────────────────┌─────────────────────────────────────────╖
  2041.  Function                          Definition
  2042.  ────────────────────────────────────────────────────────────────────────────
  2043.  SUM                               Total of values in the (numeric) column
  2044.  
  2045.  AVG                               Average of values in the (numeric)
  2046.                                    column
  2047.  
  2048.  MIN                               Lowest value in the column
  2049.  
  2050.  MAX                               Highest value in the column
  2051.  
  2052.  COUNT                             Number of values in the column
  2053.  
  2054.  column_name                       The name of a column, which must be
  2055.                                    enclosed in parentheses. Only numeric
  2056.                                    columns can be used with SUM and AVG.
  2057.                                    One COMPUTE clause can apply the same
  2058.                                    function to several columns. (See
  2059.                                    example B.) When using more than one
  2060.  Function                          Definition
  2061.  ────────────────────────────────────────────────────────────────────────────
  2062.                                   example B.) When using more than one
  2063.                                    function, use more than one COMPUTE
  2064.                                    clause. (See example E.)
  2065.  
  2066.  BY                                Indicates that row aggregate function
  2067.                                    values are to be calculated for
  2068.                                    subgroups. Whenever the value of the BY
  2069.                                    item changes, row aggregate function
  2070.                                    values are generated. If you use BY, you
  2071.                                    must use ORDER BY. Listing more than one
  2072.                                    item after BY breaks a group into
  2073.                                    subgroups and applies a function at each
  2074.                                    level of grouping.
  2075.  
  2076.  ────────────────────────────────────────────────────────────────────────────
  2077.  
  2078.  
  2079.  
  2080.  
  2081.  Comments
  2082.  
  2083.  Row aggregate functions make it possible to retrieve detail and summary rows
  2084.  with one statement. Aggregate functions, on the other hand, ordinarily
  2085.  produce a single value for all selected rows in the table or for each group,
  2086.  and these summary values are shown as new columns. The following examples
  2087.  illustrate the differences:
  2088.  
  2089.    A. select type, sum(price), sum(advance)
  2090.     from titles
  2091.     where type like "%cook"
  2092.     group by type
  2093.  
  2094.  ────────────────────────────────────────────────────────────────────────────
  2095.    type
  2096.    ------------       ---------  ----------
  2097.    mod_cook           22.98      15,000.00
  2098.    trad_cook          47.89      19,000.00
  2099.    (2 rows affected)
  2100.  
  2101.    B. select type, price, advance
  2102.     from titles
  2103.     where type like "%cook"
  2104.     order by type
  2105.     compute sum(price), sum(advance) by type
  2106.  
  2107. ╓┌─┌──────────────────┌─────────────┌────────────────────────────────────────╖
  2108.  ────────────────────────────────────────────────────────────────────────────
  2109.    type               price         advance
  2110.    ---------          ------------  ------------
  2111.    mod_cook           2.99          15,000.00
  2112.    mod_cook           19.99         0.00
  2113.                       sum           sum
  2114.                       ------------  ------------
  2115.                       22.98         15,000.00
  2116.    type               price         advance
  2117.    ---------          ------------  ------------
  2118.    trad_cook          11.95         4,000.00
  2119.    trad_cook          14.99         8,000.00
  2120.    trad_cook          20.95         7,000.00
  2121.                       sum           sum
  2122.                       -----------   ------------
  2123.  ────────────────────────────────────────────────────────────────────────────
  2124.                      -----------   ------------
  2125.                       47.89         19,000.00
  2126.    (7 rows affected)
  2127.  
  2128.  
  2129.  The columns in the COMPUTE clause must appear in the select list.
  2130.  
  2131.  You can't use SELECT INTO in the same statement with a COMPUTE clause
  2132.  because statements that include COMPUTE do not generate normal tables.
  2133.  
  2134.  If you use COMPUTE BY, you must also use an ORDER BY clause. The columns
  2135.  listed after COMPUTE BY must be identical to or a subset of those listed
  2136.  after ORDER BY, and must be in the same left-to-right order, start with the
  2137.  same expression, and not skip any expressions. For example, if the ORDER BY
  2138.  clause is
  2139.  
  2140.    order by a, b, c
  2141.  
  2142.  the COMPUTE BY clause can be any of these:
  2143.  
  2144.    compute by a, b, c
  2145.     compute by a, b
  2146.     compute by a
  2147.  
  2148.  The COMPUTE keyword can be used without BY to generate grand totals, grand
  2149.  counts, and so on. ORDER BY is optional if you use the COMPUTE keyword
  2150.  without BY. See example F.
  2151.  
  2152.  In a SELECT statement with a COMPUTE clause, the order of columns in the
  2153.  select list overrides the order of the aggregate functions in the COMPUTE
  2154.  clause. DB-LIBRARY programmers must be aware of this to put the aggregate
  2155.  function results in the right place. See "Row Aggregate Functions" for an
  2156.  example.
  2157.  
  2158.  
  2159.  See Also
  2160.  
  2161.  Aggregate Functions, GROUP BY and HAVING Clauses, Row Aggregate Functions,
  2162.  SELECT
  2163.  
  2164.  
  2165.  CONTINUE
  2166.  ────────────────────────────────────────────────────────────────────────────
  2167.  
  2168.  
  2169.  Function
  2170.  
  2171.  Controls operation of statements in a WHILE loop. CONTINUE is often (but not
  2172.  always) activated by an IF test. CONTINUE causes the WHILE loop to restart.
  2173.  
  2174.  
  2175.  
  2176.  Syntax
  2177.  
  2178.    WHILE
  2179.     boolean expression
  2180.     statement
  2181.     BREAK
  2182.     statement
  2183.     CONTINUE
  2184.  
  2185.  
  2186.  Examples
  2187.  
  2188.    while (select avg(price) from titles) < $30
  2189.     begin
  2190.      update titles
  2191.      set price = price * $2
  2192.      select max(price) from titles
  2193.      if (select max(price) from titles) > $50
  2194.      break
  2195.      else
  2196.      continue
  2197.     end
  2198.     begin
  2199.     print "Too much for the market to bear"
  2200.     end
  2201.  
  2202.  If the average price is less than $30, double the prices. Then select the
  2203.  maximum price. If it is less than or equal to $50, restart the WHILE loop
  2204.  and double the prices again. If the maximum price is more than $50, exit the
  2205.  WHILE loop and print a message.
  2206.  
  2207.  
  2208.  Comments
  2209.  
  2210.  CONTINUE causes the WHILE loop to restart, skipping any statements after
  2211.  CONTINUE.
  2212.  
  2213.  
  2214.  See Also
  2215.  
  2216.  BREAK, Expressions, WHILE
  2217.  
  2218.  
  2219.  Control-of-Flow Language
  2220.  ────────────────────────────────────────────────────────────────────────────
  2221.  
  2222.  
  2223.  Function
  2224.  
  2225.  Controls the flow of execution of SQL statements, statement blocks, and
  2226.  stored procedures.
  2227.  
  2228.  
  2229.  Statements
  2230.  
  2231.  BEGIN
  2232.    Begins a statement block.
  2233.  
  2234.  ...END
  2235.    Ends a statement block.
  2236.  
  2237.  BREAK
  2238.    Exits the innermost WHILE loop.
  2239.  
  2240.  ...CONTINUE
  2241.    Restarts a WHILE loop.
  2242.  
  2243.  DECLARE
  2244.    Declares local variables.
  2245.  
  2246.  GOTO label:
  2247.    Goes to label:, a position in a statement block.
  2248.  
  2249.  IF
  2250.    Defines conditional execution.
  2251.  
  2252.  ...ELSE
  2253.    Defines alternate execution when a condition is false.
  2254.  
  2255.  PRINT
  2256.    Prints a user-defined message on the user's screen.
  2257.  
  2258.  RAISERROR
  2259.    Prints a user-defined message on the user's screen and sets a system flag
  2260.    (in the global variable @ERROR) to record the fact that an error condition
  2261.    has occurred.
  2262.  
  2263.  RETURN
  2264.    Exits unconditionally.
  2265.  
  2266.  WAITFOR
  2267.    Sets a delay for statement execution.
  2268.  
  2269.  WHILE
  2270.    Repeats statements while condition is true.
  2271.  
  2272.  /* comment */
  2273.    Inserts a comment anywhere in an SQL statement.
  2274.  
  2275.  
  2276.  Comments
  2277.  
  2278.  Control-of-flow language can be used with interactive SQL statements, in
  2279.  batches, and in stored procedures.
  2280.  
  2281.  
  2282.  See Also
  2283.  
  2284.  Batch Queries, BEGIN...END, BREAK, Comments, CONTINUE, CREATE PROCEDURE,
  2285.  CREATE TRIGGER, DECLARE, GOTO, IF...ELSE, Parameters, PRINT, RAISERROR,
  2286.  RETURN, Variables (Local and Global), WAITFOR, WHILE
  2287.  
  2288.  
  2289.  Conversion Function
  2290.  ────────────────────────────────────────────────────────────────────────────
  2291.  
  2292.  
  2293.  Function
  2294.  
  2295.  Converts expressions of one datatype to another datatype. Also obtains a
  2296.  variety of special date formats.
  2297.  
  2298.  
  2299.  Syntax
  2300.  
  2301.    CONVERT(datatype, expression [, style])
  2302.  
  2303.  
  2304.  Examples
  2305.  
  2306.    A. select title, convert(char(12), ytd_sales)
  2307.     from titles
  2308.  
  2309.    B. select title, ytd_sales
  2310.     from titles
  2311.     where convert(char(20), ytd_sales) like "1%"
  2312.  
  2313.    C. select convert(char(12), getdate(), 3)
  2314.  
  2315.  Example C converts the current date to style "3", dd/mm/yy.
  2316.  
  2317.  
  2318.  Options
  2319.  
  2320.  datatype
  2321.    Any system datatype (for example, char(10), varbinary(50), int) into which
  2322.    the expression is to be converted. User-defined datatypes cannot be used.
  2323.    If no length is specified when converting to char, varchar, binary, or
  2324.    varbinary datatypes, the data adapts to any size necessary. The maximum
  2325.    length you can specify when converting to these types is 255.
  2326.  
  2327.  expression
  2328.    The value to be converted.
  2329.  
  2330.  style
  2331.    The style of date representation you desire when converting to char or
  2332.    varchar from datetime data. Add 100 to a style value to get a four-place
  2333.    year that includes the century (yyyy).
  2334.  
  2335. ╓┌───────────────────┌──────────────────┌──────────────────┌─────────────────
  2336.  Without             With               Standard           Output
  2337.  Century             Century
  2338.  (yy)                (yyyy)
  2339.  ─────────────────────────────────────────────────────────────────────────────
  2340.  -                   0 or 100           default            mon dd yyyy
  2341.                                                            hh:miAM (or PM)
  2342.  
  2343.  1                   101                USA                mm/dd/yy
  2344.  
  2345.  2                   102                ANSI               yy.mm.dd
  2346.  
  2347.  3                   103                British/French     dd/mm/yy
  2348.  
  2349.  4                   104                German             dd.mm.yy
  2350.  
  2351.  5                   105                Italian            dd-mm-yy
  2352.  
  2353.  6                   106                -                  dd mon yy
  2354.  Without             With               Standard           Output
  2355.  Century             Century
  2356.  (yy)                (yyyy)
  2357.  ─────────────────────────────────────────────────────────────────────────────
  2358. 6                   106                -                  dd mon yy
  2359.  
  2360.  7                   107                -                  mon dd yy
  2361.  
  2362.  8                   108                -                  hh:mi:ss
  2363.  
  2364.  -                   9 or 109           default +          mon dd, yyyy
  2365.                                         milliseconds       hh:mi:sssAM (or
  2366.                                                            PM)
  2367.  
  2368.  10                  110                USA                mm-dd-yy
  2369.  
  2370.  11                  111                JAPAN              yy/mm/dd
  2371.  
  2372.  12                  112                ISO                yymmdd
  2373.  
  2374.  ─────────────────────────────────────────────────────────────────────────────
  2375.  Without             With               Standard           Output
  2376.  Century             Century
  2377.  (yy)                (yyyy)
  2378.  ─────────────────────────────────────────────────────────────────────────────
  2379. ─────────────────────────────────────────────────────────────────────────────
  2380.  
  2381.  
  2382.  
  2383.  The default values (style 0 or 100 and 9 or 109) always return the century
  2384.  (yyyy).
  2385.  
  2386.  
  2387.  
  2388.  Comments
  2389.  
  2390.  Table  shows the possibilities for converting datatypes.
  2391.  
  2392.  Table   1.1 Datatype Conversion Chart
  2393.  
  2394. ╓┌───────────────┌───────┌──────────┌──────────────┌───────────────┌─────────
  2395.  ─────────────────────────────────────────────────────────────────────────────
  2396.  ─────────────────────────────────────────────────────────────────────────────
  2397.  To:             binary  varbinary  tinyint(INT1)  smallint(INT2)  int(INT4)
  2398.  
  2399.  From:
  2400.  
  2401.  binary          -       I          I              I               I
  2402.  
  2403.  varbinary       I       -          I              I               I
  2404.  
  2405.  tinyint(INT1)   E       I          -              I               I
  2406.  
  2407.  smallint(INT2)  E       I          I              -               I
  2408.  
  2409.  int(INT4)       E       E          I              I               -
  2410.  
  2411.  float           N       N          I              I               I
  2412.  
  2413.  char            E       E          E              E               E
  2414.  
  2415.  varchar         E       E          E              E               E
  2416.  
  2417.  ─────────────────────────────────────────────────────────────────────────────
  2418. 
  2419.  money           I       I          I              I               I
  2420.  
  2421.  bit             I       I          I              I               I
  2422.  
  2423.  datetime        E       E          N              N               N
  2424.  
  2425.  text            N       N          N              N               N
  2426.  
  2427.  image           E       E          N              N               N
  2428.  
  2429.  ─────────────────────────────────────────────────────────────────────────────
  2430.  
  2431.  
  2432.  
  2433.  Key:
  2434.  I Implicit conversion
  2435.  E Explicit conversion, CONVERT function must be used
  2436.  N Conversion not allowed
  2437.  - Conversion of a datatype to itself; allowed but meaningless
  2438.  
  2439.  
  2440.  SQL Server automatically handles certain datatype conversions. For example,
  2441.  if you compare a char expression and a datetime expression, or a smallint
  2442.  expression and an int expression, or char expressions of different lengths,
  2443.  SQL Server makes the conversion automatically for the comparison. You need
  2444.  not use the CONVERT function.
  2445.  
  2446.  It is never wrong to use the CONVERT function, even when you are comparing
  2447.  two expressions of exactly the same datatype.
  2448.  
  2449.  Automatic conversion is not supported for the text and image datatypes. You
  2450.  can explicitly convert text data to char or varchar, and image data to
  2451.  binary or varbinary, but the maximum length you can specify is 255. If you
  2452.  attempt a conversion that is not possible (for example, if you try to
  2453.  convert a char expression that includes letters to an int), SQL Server
  2454.  generates an error message.
  2455.  
  2456.  If you do not specify a length for the char, varchar, binary, or varbinary
  2457.  datatype to which the expression is to be converted, the data adapts to any
  2458.  size necessary.
  2459.  
  2460.  When converting to datetime, SQL Server rejects all values it cannot
  2461.  recognize as dates (including dates earlier than January 1, 1753).
  2462.  
  2463.  Converting to bit changes any non-zero value to a 1.
  2464.  
  2465.  When converting to money from integer datatypes, units are assumed to be
  2466.  dollars. For example, the integer value of 4 would be converted to the money
  2467.  equivalent of 4 dollars.
  2468.  
  2469.  Expressions of type char or varchar being converted to an integer datatype
  2470.  must consist only of digits and an optional plus (+) or minus (-) sign.
  2471.  Leading blanks are ignored.
  2472.  
  2473.  Expressions of type char or varchar being converted to money can also
  2474.  include an optional decimal point and dollar sign ($).
  2475.  
  2476.  Expressions of type char or varchar being converted to float can also
  2477.  include optional exponential notation (a lowercase "e" or uppercase "E"
  2478.  followed by an optional + or - and then a number).
  2479.  
  2480.  When char or varchar expressions are converted to a character datatype of a
  2481.  different size, values too long for the new datatype are truncated.
  2482.  
  2483.  Converting to binary is used to display the internal representation of a
  2484.  value. If lengths differ, the binary value is padded with zeros on the
  2485.  right.
  2486.  
  2487.  You can convert int, smallint, and tinyint to binary or varbinary, but if
  2488.  you convert the binary value back to an integer value, it will be different.
  2489.  In addition, if the integer is longer than the specified binary length, it
  2490.  is truncated without notice.
  2491.  
  2492.  When converting between types with a different number of decimal points, the
  2493.  value is truncated. For example, the result of the following is "10":
  2494.  
  2495.    select convert(int, 10.672)
  2496.  
  2497.  You can convert any datatype to binary or varbinary, but if the binary field
  2498.  is too short and the value being converted is truncated, the data may be
  2499.  meaningless.
  2500.  
  2501.  The type conversion function can be used in the select list, in the WHERE
  2502.  clause, and anywhere an expression is allowed.
  2503.  
  2504.  
  2505.  See Also
  2506.  
  2507.  Datatypes, Date Functions, Functions, Mathematical Functions, String
  2508.  Functions, Text/Image Functions
  2509.  
  2510.  
  2511.  CREATE DATABASE
  2512.  ────────────────────────────────────────────────────────────────────────────
  2513.  
  2514.  
  2515.  Function
  2516.  
  2517.  Creates a new database. You must be in the master database to create a new
  2518.  database.
  2519.  
  2520.  
  2521.  Syntax
  2522.  
  2523.    CREATE DATABASE database_name
  2524.      [ON {DEFAULT | database_device} [= size] [, database_device
  2525.      [= size]]...]
  2526.  
  2527.  
  2528.  Examples
  2529.  
  2530.    A. create database pubs
  2531.  
  2532.    B. create database pubs
  2533.     on default = 4
  2534.  
  2535.    C. create database pubs
  2536.     on file1 = 3, file2 = 2
  2537.  
  2538.  
  2539.  Options
  2540.  
  2541.  database_name
  2542.    The name of the new database. It must conform to the rules for
  2543.    identifiers.
  2544.  
  2545.  ON
  2546.    Indicates that you want to specify a location and (optionally) a size for
  2547.    the database.
  2548.  
  2549.  DEFAULT
  2550.    Indicates that CREATE DATABASE can put the new database on any default
  2551.    database device(s) (as shown in sysdevices.status). To specify a size for
  2552.    the database without specifying a location, use "ON DEFAULT = size". To
  2553.    change a database device's status to the default, use the sp_diskdefault
  2554.    system procedure.
  2555.  
  2556.  database_device
  2557.    The logical name of the database device on which you want to locate the
  2558.    database. A database can occupy different amounts of space on each of
  2559.    several database devices. DISK INIT is used to allocate space for a
  2560.    database device.
  2561.  
  2562.  size
  2563.    The amount of space allocated to the database, in megabytes. The
  2564.    SQL-Server-supplied default size is 2 megabytes. The System Administrator
  2565.    can change the default size by editing the system table
  2566.    master..sysconfigures, executing the RECONFIGURE statement and restarting
  2567.    SQL Server. The default size is usually the same size as the model
  2568.    database and must be at least as large as the model database. The legal
  2569.    values for a database size range from 2 to 215.
  2570.  
  2571.  
  2572.  Comments
  2573.  
  2574.  You must be using the master database to create a new user database.
  2575.  
  2576.  SQL Server can manage up to 32,767 databases.
  2577.  
  2578.  When the CREATE DATABASE statement is executed, SQL Server makes a copy of
  2579.  the model database, which initially contains the system tables. You can
  2580.  update the model database just like any other database to add your own
  2581.  tables, stored procedures, user datatypes, system variables, and so on. The
  2582.  default database size in master..sysconfigures must be at least as large as
  2583.  the model database.
  2584.  
  2585.  Only one database can be created at a time. If two database creation
  2586.  requests collide, one user will get the message "MODEL database in use:
  2587.  cannot create new database."
  2588.  
  2589.  If you don't specify a location and size for a database, the default
  2590.  location is any default database device indicated in master..sysdevices, and
  2591.  the default size is the size of the model database.
  2592.  
  2593.  If SQL Server can't give you as much space as you want where you've
  2594.  requested it, it creates the database anyway, giving you as much space as
  2595.  possible on each database device and prints a message telling how much space
  2596.  was allocated where.
  2597.  
  2598.  Each new database inherits its database option settings from the model
  2599.  database. For example, the database option select into/bulkcopy is
  2600.  originally set to "off". You can change that default with the sp_dboption
  2601.  system procedure.
  2602.  
  2603.  Since the CREATE DATABASE statement must clear every page (to guarantee the
  2604.  database can be recovered), it takes some number of seconds per megabyte to
  2605.  complete.
  2606.  
  2607.  It is important to back up the master database with the DUMP DATABASE or
  2608.  DUMP TRANSACTION statement after each use of CREATE DATABASE. This makes
  2609.  recovery easier and safer in case the master database is damaged. (If you
  2610.  create a database and fail to back up master, you may be able to recover the
  2611.  changes with DISK REFIT.)
  2612.  
  2613.  Whenever you create a database larger than about 4 megabytes, you should
  2614.  assign it to at least two separate database devices with the CREATE DATABASE
  2615.  statement and then use sp_logdevice to put the database's syslogs table on
  2616.  one of these database devices. This is essential to guarantee that SQL
  2617.  Server's roll-forward mechanism works correctly. Putting the transaction log
  2618.  and the rest of the database on different database devices also improves
  2619.  performance.
  2620.  
  2621.  ────────────────────────────────────────────────────────────────────────────
  2622.  NOTE
  2623.  
  2624.  A smaller database can be created on a single database device and the
  2625.  transaction log stored together with the rest of the database, but you must
  2626.  rely on the DUMP DATABASE statement for backups.
  2627.  ────────────────────────────────────────────────────────────────────────────
  2628.  
  2629.  The size of the database device required for the transaction log varies
  2630.  according to the amount of update activity and the frequency of transaction
  2631.  log dumps. As a rule, allocate to the log database device 10% to 25% of the
  2632.  space you allocate to the database itself. It is best to start small, since
  2633.  space allocated to a transaction log database device cannot be reclaimed and
  2634.  cannot be used for storage of data.
  2635.  
  2636.  To display a report on a database or on all databases on SQL Server, execute
  2637.  the sp_helpdb system procedure. For a report on the space used in a
  2638.  database, use sp_spaceused.
  2639.  
  2640.  
  2641.  Permissions
  2642.  
  2643.  CREATE DATABASE permission defaults to the System Administrator, who can
  2644.  transfer it to other users who are listed in the sysusers table of the
  2645.  current database. However, CREATE DATABASE permission is often centralized
  2646.  to maintain control over disk allocation. See the SQL Server System
  2647.  Administrator's Guide for details. CREATE DATABASE permission is not
  2648.  included when you use the GRANT ALL statement.
  2649.  
  2650.  
  2651.  See Also
  2652.  
  2653.  ALTER DATABASE, DISK INIT, DROP DATABASE, sp_changedbowner, sp_diskdefault,
  2654.  sp_helpdb, sp_logdevice, sp_renamedb, sp_spaceused
  2655.  
  2656.  
  2657.  CREATE DEFAULT
  2658.  ────────────────────────────────────────────────────────────────────────────
  2659.  
  2660.  
  2661.  Function
  2662.  
  2663.  Specifies a value that will be inserted in a column (or in all columns of a
  2664.  user-defined datatype) if no value is explicitly supplied at insert time.
  2665.  
  2666.  
  2667.  Syntax
  2668.  
  2669.    CREATE DEFAULT [owner.]default_name
  2670.      AS constant_expression
  2671.  
  2672.  
  2673.  Examples
  2674.  
  2675.    create default namedefault as "unknown"
  2676.  
  2677.  A default value has been defined. Now you need to bind it to the appropriate
  2678.  column or user datatype with the sp_bindefault system procedure.
  2679.  
  2680.  
  2681.  Options
  2682.  
  2683.  default_name
  2684.    The name of the default. It must conform to the rules for identifiers.
  2685.  
  2686.  constant_expression
  2687.    An expression that contains only constant values (that is, it does not
  2688.    include the names of any columns or other database objects). Built-in
  2689.    functions that do not reference database objects can be included.
  2690.  
  2691.  
  2692.  Comments
  2693.  
  2694.  After you've created a default, use the sp_bindefault system procedure to
  2695.  bind it to a column or user-defined datatype.
  2696.  
  2697.  Defaults cannot be bound to SQL-Server-supplied datatypes.
  2698.  
  2699.  The default must be compatible with the datatype of the column. You cannot
  2700.  use "N/A", for example, as a default for a numeric column. If the default is
  2701.  not compatible with the column to which you've bound it, SQL Server will
  2702.  generate an error message when it tries to insert the default value (not
  2703.  when you bind it).
  2704.  
  2705.  Be sure to enclose character and date constants in quotation marks and to
  2706.  precede binary constants with "0x".
  2707.  
  2708.  If the default value is too long for the column to which you've bound it,
  2709.  the value will be truncated.
  2710.  
  2711.  CREATE DEFAULT statements cannot be combined with other SQL statements in a
  2712.  single batch.
  2713.  
  2714.  You can create a default in the current database only.
  2715.  
  2716.  Default definitions are stored in syscomments. After a default is bound to a
  2717.  particular column or user datatype, its object ID number is stored in the
  2718.  syscolumns or systypes system table. To display definitions and binding
  2719.  information, execute the sp_helptext system procedure.
  2720.  
  2721.  You must drop a default before you create a new one of the same name and you
  2722.  must unbind a default (with the sp_unbindefault system procedure) before you
  2723.  drop it.
  2724.  
  2725.  You can bind a new default to a column or datatype without unbinding the old
  2726.  one; the new default overrides the old one.
  2727.  
  2728.  You can bind one default to a user datatype and another default to a column
  2729.  of that datatype. The default most recently bound takes precedence.
  2730.  
  2731.  If a column has both a default and a rule associated with it, the default
  2732.  value must not violate the rule. A default that conflicts with a rule will
  2733.  never be inserted. SQL Server will generate an error message each time it
  2734.  attempts to insert the default.
  2735.  
  2736.  If you specify NOT NULL when you create a column and do not create a default
  2737.  for it, an error message will be generated whenever a user fails to make an
  2738.  entry in that column. The following table illustrates the relationship
  2739.  between the existence of a default and the definition of a column as NULL or
  2740.  NOT NULL. The entries in the table show the result:
  2741.  
  2742.        No entry
  2743.  ────────────────────────────────────────────────────────────────────────────
  2744.  NULL  null      default  null   null
  2745.  NOT   error     default  error  error
  2746.  NULL
  2747.  
  2748.  ────────────────────────────────────────────────────────────────────────────
  2749.  
  2750.  
  2751.  You can find the value of a default by using the sp_helptext system
  2752.  procedure with the default name as the parameter.
  2753.  
  2754.  To rename a default, use sp_rename. For a report on a default, use sp_help.
  2755.  
  2756.  
  2757.  
  2758.  Permissions
  2759.  
  2760.  CREATE DEFAULT permission defaults to the Database Owner, who can transfer
  2761.  it to other users.
  2762.  
  2763.  
  2764.  See Also
  2765.  
  2766.  Batch Queries, CREATE RULE, DROP DEFAULT, DROP RULE, sp_bindefault, sp_help,
  2767.  sp_helptext, sp_rename, sp_unbindefault
  2768.  
  2769.  
  2770.  CREATE INDEX
  2771.  ────────────────────────────────────────────────────────────────────────────
  2772.  
  2773.  
  2774.  Function
  2775.  
  2776.  Creates indexes.
  2777.  
  2778.  
  2779.  Syntax
  2780.  
  2781.    CREATE [UNIQUE] [CLUSTERED | NONCLUSTERED] INDEX index_name
  2782.      ON [[database.]owner.]table_name (column_name [, column_name]...)
  2783.      [WITH {FILLFACTOR = x , IGNORE_DUP_KEY ,
  2784.      [IGNORE_DUP_ROW | ALLOW_DUP_ROW]}]
  2785.  
  2786.  
  2787.  Examples
  2788.  
  2789.    A. create index au_id_ind
  2790.     on authors (au_id)
  2791.  
  2792.    B. create unique clustered index au_id_ind
  2793.     on authors (au_id)
  2794.  
  2795.    C. create index ind1
  2796.     on titleauthor (au_id, title_id)
  2797.  
  2798.    D. create nonclustered index zip_ind
  2799.     on authors (zip)
  2800.     with fillfactor = 25
  2801.  
  2802.  
  2803.  Options
  2804.  
  2805.  UNIQUE
  2806.    Prohibits duplicate index, or key, values. The system checks for duplicate
  2807.    key values when the index is created (if data already exists) and checks
  2808.    each time data is added with an INSERT or UPDATE. You cannot create a
  2809.    unique index if there are duplicate key values: the statement is aborted
  2810.    and an error message giving the first duplicate is printed. UPDATE or
  2811.    INSERT statements that would generate duplicate key values are rolled
  2812.    back, and SQL Server displays an error message. This is true even if the
  2813.    UPDATE or INSERT statement would have changed many rows but caused only
  2814.    one duplicate. Composite indexes (indexes in which the key value is
  2815.    composed of more than one column) can be unique.
  2816.  
  2817.  CLUSTERED
  2818.    Indicates that the physical order of rows is the same as the indexed order
  2819.    of the rows, and the bottom, or leaf, level of the clustered index
  2820.    contains the actual data pages. A clustered index almost always retrieves
  2821.    data faster than a nonclustered index. By definition, only one clustered
  2822.    index is permitted per table. The clustered index is often created on the
  2823.    table's primary key─the column or columns that uniquely identify the row.
  2824.    The primary key is declared with the sp_primarykey system procedure.
  2825.  
  2826.  NONCLUSTERED
  2827.    Indicates that there is an extra level between the index structure and the
  2828.    data itself. You can have up to 250 nonclustered indexes per table;
  2829.    however, you cannot exceed a total of 250 indexes, both clustered and
  2830.    nonclustered. If CLUSTERED is not specified, NONCLUSTERED is assumed.
  2831.  
  2832.  index_name
  2833.    The name of the index. Index names must be unique within a table but need
  2834.    not be unique within a database.
  2835.  
  2836.  table_name
  2837.    The name of the table in which the indexed column or columns are located.
  2838.  
  2839.  column_name
  2840.    The column or columns to which the index applies. Composite indexes are
  2841.    based on the combined values in up to 16 columns. The sum of the maximum
  2842.    lengths of all the columns used in a composite index cannot exceed 256
  2843.    bytes. List the columns to be included in the composite index (in
  2844.    sort-priority order) inside the parentheses after table_name.
  2845.  
  2846.  FILLFACTOR
  2847.    Specifies how full SQL Server will make each page when it is creating a
  2848.    new index on existing data. The fillfactor percentage affects performance,
  2849.    because SQL Server must take the time to split pages when they fill up.
  2850.    The fillfactor percentage is relevant only at the time the index is
  2851.    created and becomes less important as the data changes. The pages are not
  2852.    maintained at any particular level of fullness.
  2853.  
  2854.    The default for FILLFACTOR is 0. Legal values are between 0 and 100. A
  2855.    fillfactor of 0 does not mean that pages are 0% full. Rather, it is like
  2856.    100 in that SQL Server creates clustered indexes with completely full
  2857.    pages and nonclustered indexes with completely full leaf pages. It is
  2858.    different from 100 in that SQL Server leaves a comfortable amount of space
  2859.    within the index B-tree in both the clustered and nonclustered case. There
  2860.    is seldom a reason to change the fillfactor variable, especially since you
  2861.    can override it in the CREATE INDEX statement.
  2862.  
  2863.    If FILLFACTOR is 100, SQL Server creates both clustered and nonclustered
  2864.    indexes with each page 100% full. A fillfactor of 100 makes sense only for
  2865.    read-only tables─tables to which no additional data will ever be added.
  2866.    Smaller fillfactor values (except 0, which is a special case) cause SQL
  2867.    Server to create new indexes with pages that are not completely full. For
  2868.    example, a fill factor of 10 might be a reasonable choice if you are
  2869.    creating an index on a table that you know contains a small portion of the
  2870.    data it will eventually hold.
  2871.  
  2872.    Smaller fillfactor values cause each index to take more storage space.
  2873.  
  2874.    The default FILLFACTOR value can be altered by updating sysconfigures.
  2875.  
  2876.  IGNORE_DUP_KEY
  2877.    An infrequently used keyword. Its most common function is to sort through
  2878.    a batch of data that includes duplicate rows you wish to eliminate.
  2879.  
  2880.    IGNORE_DUP_KEY controls what happens when you attempt to enter a duplicate
  2881.    key in a unique clustered index. It is meaningful only when the UPDATE or
  2882.    INSERT statement affects multiple rows.
  2883.  
  2884.    If IGNORE_DUP_KEY is not set and an attempt is made to enter data that
  2885.    would create duplicate keys, the entire data modification statement fails.
  2886.    (If there are other statements in the same transaction, they execute
  2887.    normally.)
  2888.  
  2889.    If IGNORE_DUP_KEY is set and you give an UPDATE or INSERT statement that
  2890.    would create duplicate keys, the row that would cause the duplicates is
  2891.    not added or changed. In fact, in the case of UPDATE, the row is
  2892.    discarded. (See the following warning.)
  2893.  
  2894.    Other changes to the database caused by the UPDATE or INSERT attempt (for
  2895.    example, changes to index pages) are also backed out. However, if the
  2896.    UPDATE or INSERT affects multiple rows, the other rows are added or
  2897.    changed as usual.
  2898.  
  2899.  ────────────────────────────────────────────────────────────────────────────
  2900.  WARNING
  2901.  
  2902.  With IGNORE_DUP_KEY set, if you try to update a row in such a way that a
  2903.  duplicate key would be created, that row is discarded. Neither the new value
  2904.  nor the original value of the row that would have produced the duplicate
  2905.  exists in the updated table. For example, if you try to update "Smith" to
  2906.  "Jones" and "Jones" already exists, you will have one "Jones" and no
  2907.  "Smith." The disappearance of the row for "Smith" is distressing, but
  2908.  unavoidable, because an UPDATE statement is actually a DELETE followed by an
  2909.  INSERT. SQL Server has no way to know about the disallowed duplicate when it
  2910.  deletes the row, and the whole transaction can't be rolled back because the
  2911.  purpose of IGNORE_DUP_KEY (and of the IGNORE_DUP_ROW option) is to allow a
  2912.  transaction to proceed in spite of the presence of duplicates.
  2913.  ────────────────────────────────────────────────────────────────────────────
  2914.  
  2915.    You cannot create a unique index on a column that includes duplicate
  2916.    values, whether or not IGNORE_DUP_KEY is set. If you attempt to do so, SQL
  2917.    Server prints an error message that gives the first of the duplicate
  2918.    values. You must eliminate duplicates before you create a unique index on
  2919.    the column.
  2920.  
  2921.  IGNORE_DUP_ROW and ALLOW_DUP_ROW
  2922.    Keywords allowed only with a nonunique clustered index. (From the point of
  2923.    view of a nonclustered index, there are never any duplicate rows─even for
  2924.    identical data values─because nonclustered indexes include a unique row
  2925.    identification number along with the index value.) Only one of the
  2926.    IGNORE_DUP_ROW and ALLOW_DUP_ROW options can be set.
  2927.  
  2928.    The ALLOW_DUP_ROW option allows duplicate rows in a table. If
  2929.    ALLOW_DUP_ROW is set, you can create a new clustered index on a table that
  2930.    includes duplicate rows, and you can subsequently create duplicate rows
  2931.    with INSERT or UPDATE.
  2932.  
  2933.    If ALLOW_DUP_ROW is not set, the handling of duplicate rows depends on
  2934.    whether IGNORE_DUP_ROW is set. You cannot set ALLOW_DUP_ROW  if
  2935.    IGNORE_DUP_ROW is set.
  2936.  
  2937.    If any index in the table is unique, the requirement for uniqueness─the
  2938.    most stringent requirement─takes precedence over the ALLOW_DUP_ROW option.
  2939.    Thus, ALLOW_DUP_ROW applies only to tables with nonunique indexes: you
  2940.    cannot use this keyword if a unique clustered index exists on any column
  2941.    in the table.
  2942.  
  2943.    (However, you might create a table with a nonunique clustered index and
  2944.    the ALLOW_DUP_ROW option, and then create a unique nonclustered index on
  2945.    the same table. If at that time the existing data does not include any
  2946.    duplicate rows, the CREATE UNIQUE NONCLUSTERED INDEX statement will
  2947.    succeed, but subsequent attempts to enter duplicate rows will fail because
  2948.    of the requirement for uniqueness on the new nonclustered index. If
  2949.    duplicate rows do exist when you attempt to create a unique nonclustered
  2950.    index, the statement fails.)
  2951.  
  2952.    The IGNORE_DUP_ROW option, like IGNORE_DUP_KEY, is used infrequently. Its
  2953.    most common function is to sort through a batch of data that includes
  2954.    errors you wish to eliminate.
  2955.  
  2956.    IGNORE_DUP_ROW controls what happens when you attempt to enter duplicate
  2957.    rows into a table in which they're not allowed. IGNORE_DUP_ROW applies
  2958.    only to tables with nonunique indexes: you cannot use this keyword if a
  2959.    unique index exists on any column in the table. If ALLOW_DUP_ROW is set,
  2960.    IGNORE_DUP_ROW is meaningless. If IGNORE_DUP_ROW is not set (and
  2961.    ALLOW_DUP_ROW is not set), the entire statement fails. (If there are other
  2962.    statements in the same transaction, they are executed normally.)
  2963.  
  2964.    If IGNORE_DUP_ROW is set, the row that would duplicate another row is not
  2965.    added or changed, and you get an error message. Changes to the database
  2966.    caused by the attempt to duplicate a row (for example, changes to index
  2967.    pages) are also backed out. Other rows are added or changed, or the rest
  2968.    of the index is created, as usual.
  2969.  
  2970.  ────────────────────────────────────────────────────────────────────────────
  2971.  WARNING
  2972.  
  2973.  With IGNORE_DUP_ROW set, if you try to update a row in such a way that a
  2974.  duplicate row would be created, that row is discarded. Neither the new value
  2975.  nor the original value of the row that would have produced the duplicate
  2976.  exists in the updated table. For example, if you try to update "Smith" to
  2977.  "Jones" and "Jones" already exists, you will have one "Jones" and no
  2978.  "Smith." The disappearance of the row for "Smith" is distressing, but
  2979.  unavoidable, because an UPDATE statement is actually a DELETE followed by an
  2980.  INSERT. SQL Server has no way to know about the disallowed duplicate when it
  2981.  deletes the row, and the whole transaction can't be rolled back because the
  2982.  purpose of IGNORE_DUP_ROW (and of the IGNORE_DUP_KEY option) is to allow a
  2983.  transaction to proceed in spite of the presence of duplicates.
  2984.  ────────────────────────────────────────────────────────────────────────────
  2985.  
  2986.    In the default situation, neither ALLOW_DUP_ROW nor IGNORE_DUP_ROW is set.
  2987.    Attempting to insert duplicate rows or create an index on a table with
  2988.    duplicate rows causes the entire statement to fail.
  2989.  
  2990.    The following table illustrates how ALLOW_DUP_ROW and IGNORE_DUP_ROW
  2991.    affect attempts to create a nonunique clustered index on a table that
  2992.    includes duplicate rows, and to enter duplicate rows into a table.
  2993.  
  2994.                      Create index             Insert duplicate rows
  2995.                      (table has duplicate
  2996.                      rows)
  2997.  ────────────────────────────────────────────────────────────────────────────
  2998.  Neither Option Set  Command fails.           Command fails.
  2999.  
  3000.  ALLOW_DUP_ROW Set   Command is completed.    Command is completed.
  3001.  
  3002.  IGNORE_DUP_ROW Set  Index created but        All rows accepted except
  3003.                      duplicate rows thrown    duplicates; error message.
  3004.                      out; error message.      See the previous warning.
  3005.  
  3006.  ────────────────────────────────────────────────────────────────────────────
  3007.  
  3008.  
  3009.  
  3010.  Comments
  3011.  
  3012.  Indexes speed data retrieval but can slow data update.
  3013.  
  3014.  Columns regularly used in joins should always be indexed.
  3015.  
  3016.  Creating a clustered index requires work-table space that is 1.21 times the
  3017.  size of the data.
  3018.  
  3019.  Create the clustered index before creating any nonclustered indexes, since
  3020.  nonclustered indexes are automatically rebuilt when a clustered index is
  3021.  created.
  3022.  
  3023.  Columns of bit, text, and image types cannot be indexed.
  3024.  
  3025.  You can create an index on a temporary table. It disappears when the table
  3026.  disappears.
  3027.  
  3028.  As the syntax signifies, you can create an index on a table in another
  3029.  database as long as you are the owner of that table.
  3030.  
  3031.  If there is no data in the table when an index is created, you should run
  3032.  UPDATE STATISTICS after data is added.
  3033.  
  3034.  A composite index, like any other index, is represented by one row in
  3035.  sysindexes.
  3036.  
  3037.  To display a report on an object's indexes, execute the sp_helpindex system
  3038.  procedure.
  3039.  
  3040.  Space is allocated to tables and indexes in increments of one extent, or 8
  3041.  pages, at a time. In other words, an extent is allocated when the table or
  3042.  index is created, and another extent is allocated each time the previous
  3043.  extent becomes full. For a report on the amount of space allocated and used
  3044.  by an index, use sp_spaceused.
  3045.  
  3046.  Indexes cannot be created on columns containing text or image values.
  3047.  
  3048.  
  3049.  Permissions
  3050.  
  3051.  CREATE INDEX permission defaults to the table owner and is not transferable.
  3052.  
  3053.  
  3054.  
  3055.  See Also
  3056.  
  3057.  CREATE TABLE, DROP INDEX, INSERT, RECONFIGURE, SET, UPDATE, UPDATE
  3058.  STATISTICS, sp_helpindex, sp_spaceused
  3059.  
  3060.  
  3061.  CREATE PROCEDURE
  3062.  ────────────────────────────────────────────────────────────────────────────
  3063.  
  3064.  
  3065.  Function
  3066.  
  3067.  Creates a stored procedure (a precompiled collection of SQL statements,
  3068.  often including control-of-flow language) that can take one or more
  3069.  user-supplied parameters.
  3070.  
  3071.  
  3072.  Syntax
  3073.  
  3074.    CREATE PROCedure [owner.]procedure_name [;number]
  3075.      [[(]@parameter_name datatype [= default]
  3076.      [, @parameter_name datatype [= default]]...[)]]
  3077.      [WITH RECOMPILE]
  3078.      AS SQL_statements
  3079.  
  3080.  
  3081.  Examples
  3082.  
  3083.    A. create procedure showind @tabname varchar(30)
  3084.     as
  3085.      select sysobjects.name, sysindexes.name, indid
  3086.      from sysindexes, sysobjects
  3087.      where sysobjects.name = @tabname
  3088.      and sysobjects.id = sysindexes.id
  3089.  
  3090.  Given a table name, the procedure showind (example A) displays its name and
  3091.  the names and identification numbers of any indexes on any of its columns.
  3092.  
  3093.  The following are the acceptable syntax forms for executing showind:
  3094.  
  3095.    execute showind titles
  3096.  
  3097.     execute showind @tabname = "titles"
  3098.  
  3099.  Or, if this is the first statement in a file or batch:
  3100.  
  3101.    showind titles
  3102.  
  3103.    B. create procedure showsysind @table varchar(30) = "sys%"
  3104.     as
  3105.      select sysobjects.name, sysindexes.name, indid
  3106.      from sysindexes, sysobjects
  3107.      where sysobjects.name like @table
  3108.      and sysobjects.id = sysindexes.id
  3109.  
  3110.  The procedure in example B displays information about the system tables if
  3111.  the user does not supply a parameter.
  3112.  
  3113.    C. create procedure showindnew @table varchar(30) = null
  3114.     as
  3115.      if @table is null
  3116.      print "Please give a table name"
  3117.      else
  3118.      select sysobjects.name, sysindexes.name, indid
  3119.      from sysindexes, sysobjects
  3120.       where sysobjects.name = @table
  3121.      and sysobjects.id = sysindexes.id
  3122.  
  3123.  The procedure in example C specifies an action to be taken if the parameter
  3124.  is NULL (that is, if the user does not give a parameter).
  3125.  
  3126.  
  3127.  Options
  3128.  
  3129.  procedure_name
  3130.    The name of the procedure. It must conform to the rules for identifiers.
  3131.  
  3132.  ;number
  3133.    An optional integer used to group procedures of the same name so that they
  3134.    can be dropped together with a single DROP PROCEDURE statement. Procedures
  3135.    used in the same application are often grouped this way. For example, the
  3136.    procedures used with the application orders might be named orderproc;1,
  3137.    orderproc;2, and so on. The statement DROP PROCEDURE orderproc would drop
  3138.    the entire group. Once procedures have been grouped, individual procedures
  3139.    within the group cannot be dropped. For example, the statement DROP
  3140.    PROCEDURE orderproc;2 is not allowed.
  3141.  
  3142.  parameter_name
  3143.    The name of a parameter to the procedure. The value of each parameter is
  3144.    supplied when the procedure is executed. (Parameter names are optional in
  3145.    CREATE PROCEDURE statements─a procedure need not take any parameters.)
  3146.  
  3147.    Parameter names must be preceded by the "at" symbol (@). The parameter
  3148.    name conforms to the rules for identifiers except that it can contain from
  3149.    1 to 29 characters. Parameters are local to the procedure; the same
  3150.    parameter names can be used in other procedures. Parameters can only take
  3151.    the place of constants; they cannot be used in place of table names,
  3152.    column names, or the names of other database objects. However, the value
  3153.    of a parameter given at execution time can be an object name.
  3154.  
  3155.    If the value of a parameter is an object name and the object name is
  3156.    qualified by a database name or owner name, the entire name must be
  3157.    enclosed in single or double quotation marks.
  3158.  
  3159.  datatype
  3160.    The datatype of the parameter. A length in parentheses after the datatype
  3161.    entry is required for some datatypes. See the "Datatypes" section for a
  3162.    list of SQL-Server-supplied datatypes and their syntax.
  3163.  
  3164.  default
  3165.    A default parameter value for the procedure. If a default is defined, a
  3166.    user can execute the procedure without giving a parameter. The default
  3167.    must be a constant. It can include the wildcard characters (%, _, [ ], and
  3168.    [^]) if the procedure uses the parameter name with the LIKE keyword. (See
  3169.    example B.)
  3170.  
  3171.    The default can be NULL. The procedure definition can specify that some
  3172.    action be taken if the parameter value is NULL. (See example C.)
  3173.  
  3174.  WITH RECOMPILE
  3175.    Indicates that SQL Server will never save a plan for this procedure; the
  3176.    procedure will be recompiled each time it is executed. Use this optional
  3177.    clause when you expect that the parameters you supply to the procedure
  3178.    won't be typical, that is, they would not result in the same optimal plan.
  3179.  
  3180.  SQL_statements
  3181.    Statements that specify the actions the procedure is to take. Any number
  3182.    and kind of SQL statements can be included with the exception of CREATE
  3183.    statements. The SQL statements in a CREATE PROCEDURE statement can also
  3184.    include parameters.
  3185.  
  3186.    CREATE PROCEDURE statements often include control-of-flow language,
  3187.    including one or more of the following: DECLARE; IF...ELSE; WHILE; BREAK;
  3188.    CONTINUE; BEGIN...END; GOTO; RETURN; WAITFOR; /* comment  */.
  3189.  
  3190.    The SQL statements can reference objects in another database as long as
  3191.    they are properly qualified.
  3192.  
  3193.  
  3194.  Comments
  3195.  
  3196.  Since stored procedures are compiled the first time they are executed,
  3197.  subsequent run time is much shorter than for the equivalent set of
  3198.  stand-alone statements.
  3199.  
  3200.  Most SQL Server database administration tasks are accomplished with
  3201.  predefined system procedures. Each of the system procedures' names begins
  3202.  with sp_.
  3203.  
  3204.  CREATE PROCEDURE statements cannot be combined with other SQL statements in
  3205.  a single batch.
  3206.  
  3207.  You can create a stored procedure in the current database only.
  3208.  
  3209.  A CREATE PROCEDURE statement cannot include any CREATE VIEW, CREATE DEFAULT,
  3210.  CREATE RULE, CREATE TRIGGER, or other CREATE PROCEDURE statements.
  3211.  
  3212.  The maximum number for each parameter, local variable, and global variable
  3213.  in a procedure is 40. (See "Variables (Local and Global)".)
  3214.  
  3215.  The maximum amount of text in a stored procedure is 65,280 characters. This
  3216.  limit is imposed by the fact that the text is stored in syscomments, where
  3217.  each procedure can occupy 255 rows of 256 bytes each.
  3218.  
  3219.  Once a procedure is created, you can run it by executing the EXECUTE
  3220.  statement along with the procedure's name and any parameters. Or, if a
  3221.  procedure is the first statement in a batch, you can give its name without
  3222.  the keyword EXECUTE.
  3223.  
  3224.  Any objects referenced in a procedure must already exist. You can create an
  3225.  object within a procedure and then reference it as long as the object is
  3226.  created before it is referenced.
  3227.  
  3228.  You can create a procedure that references a temporary object if the
  3229.  temporary object exists at the time the procedure is created or if the
  3230.  procedure itself creates the temporary object. A temporary object created in
  3231.  a procedure disappears when the procedure exits. If a procedure uses a
  3232.  temporary object that it does not create, you must re-create the temporary
  3233.  object and the stored procedure before you invoke that procedure. For this
  3234.  reason, system procedures (like sp_help) do not work on temporary objects.
  3235.  If you have a procedure that uses a temporary table and is not going to call
  3236.  other procedures, then it makes sense to use a temporary table. The table
  3237.  goes away when you exit the procedure.
  3238.  
  3239.  If you execute a procedure that calls another procedure, the called
  3240.  procedure can access objects created by the calling procedure.
  3241.  
  3242.  You can use the SET statement inside a stored procedure. The SET option you
  3243.  choose remains in effect during the execution of the procedure and then
  3244.  reverts to its former setting.
  3245.  
  3246.  Within a stored procedure, you cannot create an object (including a
  3247.  temporary one), drop it, and then create a new object with the same name.
  3248.  
  3249.  Inside a stored procedure, object names used with certain statements (the
  3250.  so-called utility programs) must be qualified with the object owner's name
  3251.  if other users are to use the stored procedure. For example, user mary, who
  3252.  owns table marytab, should qualify the name of her table inside a stored
  3253.  procedure (when it is used with these utility commands) if she wants other
  3254.  users to be able to execute it, like this:
  3255.  
  3256.    create procedure p1
  3257.     as
  3258.     create index marytab_ind
  3259.     on mary.marytab(col1)
  3260.  
  3261.  This is because the object names are resolved when the procedure is run. In
  3262.  the example, if user john tried to execute procedure p1, SQL Server would
  3263.  look for a table called marytab owned by John. The utility statements are
  3264.  ALTER TABLE, CREATE TABLE, DROP TABLE, TRUNCATE TABLE, CREATE INDEX, DROP
  3265.  INDEX, UPDATE STATISTICS, and DBCC. Object names used with other statements
  3266.  (for example, SELECT or INSERT) inside a stored procedure need not be
  3267.  qualified, because the names are resolved when the procedure is compiled.
  3268.  
  3269.  The text of the CREATE PROCEDURE statement is stored in syscomments. To
  3270.  display it, execute the sp_helptext system procedure with the procedure name
  3271.  as the parameter. To display the text of a system procedure, execute
  3272.  sp_helptext from the master database.
  3273.  
  3274.  To rename a procedure, use sp_rename. For a report on the objects referenced
  3275.  by a procedure, use sp_depends.
  3276.  
  3277.  
  3278.  Permissions
  3279.  
  3280.  CREATE PROCEDURE permission defaults to the Database Owner, who can transfer
  3281.  it to other users.
  3282.  
  3283.  
  3284.  See Also
  3285.  
  3286.  Batch Queries, BEGIN...END, BREAK, Comments, CONTINUE, DECLARE, DROP
  3287.  PROCEDURE, EXECUTE, GOTO, GRANT, IF...ELSE, Parameters, RETURN, SELECT,
  3288.  System Procedures, Variables (Local and Global), WAITFOR, WHILE, Wildcard
  3289.  Characters, sp_depends, sp_helptext, sp_rename
  3290.  
  3291.  
  3292.  CREATE RULE
  3293.  ────────────────────────────────────────────────────────────────────────────
  3294.  
  3295.  
  3296.  Function
  3297.  
  3298.  Specifies the domain of acceptable values for a particular column or for any
  3299.  column of a user-specified datatype.
  3300.  
  3301.  
  3302.  Syntax
  3303.  
  3304.    CREATE RULE [owner.]rule_name
  3305.      AS condition_expression
  3306.  
  3307.  
  3308.  Examples
  3309.  
  3310.    A. create rule limit
  3311.     as @advance < $1000
  3312.  
  3313.    B. create rule pubid_rule
  3314.     as @pub_id in ('1389', '0736', '0877')
  3315.  
  3316.    C. create rule picture
  3317.     as @value like '_ _-%[0-9]'
  3318.  
  3319.  
  3320.  Options
  3321.  
  3322.  rule_name
  3323.    The name of the new rule. It must conform to the rules for identifiers.
  3324.  
  3325.  condition_expression
  3326.    The conditions that define the rule. It can be any expression that is
  3327.    valid in a WHERE clause and can include arithmetic operators, relational
  3328.    operators, IN, LIKE, BETWEEN, and so on. However, it cannot reference any
  3329.    column or other database object. Built-in functions that do not reference
  3330.    database objects can be included.
  3331.  
  3332.    A condition_expression takes one parameter. The parameter is prefixed by
  3333.    the "at" symbol (@) and refers to the value that is entered with the
  3334.    UPDATE or INSERT statement. You can use any name or symbol to represent
  3335.    the value when you write the rule, but the first character must be "@".
  3336.  
  3337.  
  3338.  Comments
  3339.  
  3340.  Rules do not apply to the data already existing in the database at the time
  3341.  the rules are created.
  3342.  
  3343.  CREATE RULE statements cannot be combined with other SQL statements in a
  3344.  single batch.
  3345.  
  3346.  You can create a rule in the current database only.
  3347.  
  3348.  After you've created a rule, use the sp_bindrule system procedure to link
  3349.  the rule to a column or user datatype.
  3350.  
  3351.  Rules cannot be bound to SQL-Server-supplied datatypes.
  3352.  
  3353.  The rule must be compatible with the datatype of the column. You cannot use
  3354.  "@value like A%", for example, as a rule for a numeric column.
  3355.  
  3356.  ────────────────────────────────────────────────────────────────────────────
  3357.  NOTE
  3358.  
  3359.  If the rule is not compatible with the column to which you've bound it, SQL
  3360.  Server generates an error message when it tries to insert a value (not when
  3361.  you bind it).
  3362.  ────────────────────────────────────────────────────────────────────────────
  3363.  
  3364.  Be sure to enclose character and date constants in quotation marks and to
  3365.  precede binary constants with "0x".
  3366.  
  3367.  Rule definitions and binding information are stored in system tables. The
  3368.  text of CREATE RULE statements is stored in the syscomments system table. To
  3369.  display it, execute the sp_helptext system procedure with the rule name as
  3370.  the parameter. After a rule is bound to a particular column or user
  3371.  datatype, its ID is stored in the syscolumns or systypes system tables.
  3372.  
  3373.  To get a report on a rule, use sp_help. To rename a rule, use sp_rename.
  3374.  
  3375.  You must drop a rule before you can create a new one of the same name, and
  3376.  you must unbind a rule (with the sp_unbindrule system procedure) before you
  3377.  drop it.
  3378.  
  3379.  You can bind a new rule to a column or datatype without unbinding the old
  3380.  one; the new rule overrides the old one.
  3381.  
  3382.  When unbinding a rule from a column, use sp_unbindrule.
  3383.  
  3384.  You can bind one rule to a user datatype and another rule to a column of
  3385.  that datatype. The rule most recently bound takes precedence.
  3386.  
  3387.  If a column has both a default and a rule associated with it, the default
  3388.  must fall within the domain defined by the rule. A default that conflicts
  3389.  with a rule will never be inserted. SQL Server generates an error message
  3390.  each time it attempts to insert the default.
  3391.  
  3392.  
  3393.  Permissions
  3394.  
  3395.  CREATE RULE permission defaults to the Database Owner, who can transfer it
  3396.  to other users.
  3397.  
  3398.  
  3399.  See Also
  3400.  
  3401.  Batch Queries, CREATE DEFAULT, DROP DEFAULT, DROP RULE, sp_bindrule,
  3402.  sp_help, sp_helptext, sp_rename, sp_unbindrule
  3403.  
  3404.  
  3405.  CREATE TABLE
  3406.  ────────────────────────────────────────────────────────────────────────────
  3407.  
  3408.  
  3409.  Function
  3410.  
  3411.  Creates new tables.
  3412.  
  3413.  
  3414.  Syntax
  3415.  
  3416.    CREATE TABLE [[database.]owner.]table_name
  3417.      (column_name datatype [NOT NULL | NULL]
  3418.      [, column_name datatype [NOT NULL | NULL]]...)
  3419.  
  3420.  
  3421.  Examples
  3422.  
  3423.    create table titles
  3424.      (title_id tid,
  3425.       title varchar(80),
  3426.       type char(12),
  3427.       pub_id char(4),
  3428.       price money null,
  3429.       advance money null,
  3430.       royalty float null,
  3431.       ytd_sales money null,
  3432.       notes varchar(200),
  3433.       pubdate datetime)
  3434.  
  3435.  
  3436.  Options
  3437.  
  3438.  table_name
  3439.    The name of the new table. It must conform to the rules for identifiers
  3440.    and must be unique within this database and owner.
  3441.  
  3442.    You can create temporary tables by preceding the table name with a pound
  3443.    sign (#). If you wanted the table in example A to be a temporary table,
  3444.    you would call it #titles. Temporary tables are stored in
  3445.    tempdb..sysobjects by their names plus a system-supplied numeric suffix.
  3446.    Temporary tables disappear at the end of the current session or when they
  3447.    are explicitly dropped.
  3448.  
  3449.    You can create a table in a different database as long as you are listed
  3450.    in the sysusers table of the other database and have CREATE TABLE
  3451.    permission in that database. For example, to create a table called
  3452.    newtable in the database otherdb, use the following syntax:
  3453.  
  3454.    A. create table otherdb..newtable
  3455.  
  3456.      B. create table otherdb.yourname.newtable
  3457.  
  3458.  column_name
  3459.    The name of the column in the table. It must conform to the rules for
  3460.    identifiers and must be unique in the table.
  3461.  
  3462.  datatype
  3463.    The datatype of the column. System or user-defined datatypes are
  3464.    acceptable. Some datatypes require a length in the form datatype(n) where
  3465.    n is the length. To assign a datatype, use the format column_name
  3466.    datatype. (See "Datatypes" for more information.)
  3467.  
  3468.  NOT NULL
  3469.    Indicates that if there is no default for this column, an error message
  3470.    will be produced if no entry is made at insert time. Furthermore, the user
  3471.    cannot assign the value NULL in this column. If neither NULL nor NOT NULL
  3472.    is specified, NOT NULL is assumed. This SQL Server enhancement speeds
  3473.    retrievals and updates.
  3474.  
  3475.  NULL
  3476.    Indicates that if the user does not make an entry at insert time and there
  3477.    is no default entry for this column, SQL Server will assign the value
  3478.    NULL.
  3479.  
  3480.  
  3481.  Comments
  3482.  
  3483.  There can be up to 2 billion tables per database and 250 columns per table.
  3484.  
  3485.  
  3486.  The table is created in the currently open database unless a different
  3487.  database is explicitly specified in the CREATE TABLE statement with the
  3488.  optional database and owner names. (Cross-database creation of tables and
  3489.  indexes is allowed as long as the creator is listed in the sysusers table of
  3490.  the other database and has CREATE TABLE permission in that database.
  3491.  However, cross-database creation of views, rules, defaults, stored
  3492.  procedures, and triggers is not allowed.)
  3493.  
  3494.  User-defined datatypes are defined in terms of system datatypes. User
  3495.  datatypes permit frequently used type information to be accessed by a name
  3496.  you choose with a specified rule, default, and display format attached to
  3497.  it. User-defined datatypes are created with the sp_addtype system procedure
  3498.  before they are used in a table definition.
  3499.  
  3500.  You cannot create a row that has more than 1962 bytes of data.
  3501.  
  3502.  When you create user-defined datatypes with sp_addtype, there are three
  3503.  parameters: the name you assign the datatype; the base datatype (one of the
  3504.  SQL-Server-supplied datatypes─include length for char, varchar, binary, and
  3505.  varbinary); and either NULL or NOT NULL. The last parameter is optional.
  3506.  
  3507.  If you don't type NULL or NOT NULL, NOT NULL is assumed. The first two
  3508.  examples here are NOT NULL; the third is NULL. (Quotation marks are used
  3509.  when parameters contain blanks or punctuation marks.)
  3510.  
  3511.    A. sp_addtype tid1, "char(6)"
  3512.  
  3513.    B. sp_addtype tid2, "char(6)", "not null"
  3514.  
  3515.    C. sp_addtype tid3, "char(6)", null
  3516.  
  3517.  The NULL/NOT NULL assignment of a user datatype can be changed when you use
  3518.  it in a CREATE TABLE statement. However, the length specification cannot be
  3519.  changed: SQL Server will not allow you to specify a new length for a
  3520.  user-defined datatype in a CREATE TABLE statement.
  3521.  
  3522.  User-defined datatypes are removed with the sp_droptype system procedure.
  3523.  
  3524.  All of the datatypes that handle numbers (int, smallint, tinyint, money, and
  3525.  float) support arithmetic operations and aggregate functions (except that
  3526.  you cannot use modulo with float or money). Relational operators can be used
  3527.  on any of the datatypes except text and image.
  3528.  
  3529.  You can use the LIKE keyword and wildcard characters with char, varchar,
  3530.  text, and datetime data (but not to search for seconds or milliseconds).
  3531.  
  3532.  Built-in functions for concatenation and for finding substrings are provided
  3533.  for char, varchar, binary, and varbinary datatypes. (See "String
  3534.  Functions.")
  3535.  
  3536.  Conversion from one datatype to another is available with the CONVERT
  3537.  statement. (See "Conversion Function.")
  3538.  
  3539.  You can convert text data to varchar, and image data to varbinary, but it
  3540.  will be truncated to 255 bytes.
  3541.  
  3542.  When you create NULL columns with certain datatypes, SQL Server
  3543.  automatically converts them to a different internal datatype to allow the
  3544.  storage of null values. The char datatype is automatically converted to
  3545.  varchar; binary to varbinary; datetime to datetimn; float to floatn; int,
  3546.  smallint, and tinyint to intn; and money to moneyn.
  3547.  
  3548.  SQL Server does not inform the user of the type change, and the user need be
  3549.  concerned about it only if querying the system tables.
  3550.  
  3551.  Temporary tables are stored in the temporary database (tempdb), and
  3552.  permanent ones in the current database.
  3553.  
  3554.  You cannot create a view on a temporary table, nor can you associate rules,
  3555.  defaults, or triggers with temporary tables. You can use a user-defined
  3556.  datatype when creating a temporary table only if that type is in
  3557.  tempdb..systypes.
  3558.  
  3559.  (There are two ways to add a user-defined datatype (or any other object) to
  3560.  tempdb. To add an object for the current session only, execute sp_addtype
  3561.  while using the tempdb database. To add an object permanently, execute
  3562.  sp_addtype while using the model database and then restart SQL Server so
  3563.  that model is copied to tempdb.)
  3564.  
  3565.  Within a stored procedure, you cannot create a table, drop it, and then
  3566.  create a new table with the same name.
  3567.  
  3568.  For a report on a table and its columns, execute the sp_help system
  3569.  procedure. To rename a table, use sp_rename.
  3570.  
  3571.  ────────────────────────────────────────────────────────────────────────────
  3572.  WARNING
  3573.  
  3574.  Procedures, triggers, and views that depend on an object whose name has been
  3575.  changed work properly until they are recompiled. However, recompilation
  3576.  takes place for many reasons and without notification to the user. When the
  3577.  procedure, trigger, or view is recompiled by SQL Server, it will no longer
  3578.  work. The user must change the procedure, trigger, or view text to reflect
  3579.  the new name of the dependent object. The safest course is to change the
  3580.  definitions of any dependent objects when you execute sp_rename.
  3581.  ────────────────────────────────────────────────────────────────────────────
  3582.  
  3583.  Space is allocated to tables and indexes in increments of one extent, or 8
  3584.  pages, at a time. In other words, an extent is allocated when the table or
  3585.  index is created, and another extent is allocated each time the previous
  3586.  extent becomes full. For a report on the amount of space allocated and used
  3587.  by a table, use sp_spaceused.
  3588.  
  3589.  For a report on the views and stored procedures that depend on a table, use
  3590.  sp_depends.
  3591.  
  3592.  You can define primary, foreign, and common keys on a table with the
  3593.  sp_primarykey, sp_foreignkey, and sp_commonkey system procedures. For a
  3594.  report on the keys that have been defined, use sp_helpkey. For a report on
  3595.  frequently used joins, use sp_helpjoins.
  3596.  
  3597.  
  3598.  Permissions
  3599.  
  3600.  CREATE TABLE permission defaults to the Database Owner, who can transfer it
  3601.  to other users. Any user can create temporary tables.
  3602.  
  3603.  
  3604.  See Also
  3605.  
  3606.  CREATE INDEX, CREATE RULE, CREATE VIEW, Datatypes, DROP INDEX, DROP RULE,
  3607.  DROP TABLE, sp_addtype, sp_commonkey, sp_depends, sp_foreignkey, sp_help,
  3608.  sp_helpjoins, sp_primarykey, sp_rename, sp_spaceused
  3609.  
  3610.  
  3611.  CREATE TRIGGER
  3612.  ────────────────────────────────────────────────────────────────────────────
  3613.  
  3614.  
  3615.  Function
  3616.  
  3617.  Creates a trigger, which is a special kind of stored procedure often used
  3618.  for enforcing integrity constraints. A trigger is executed automatically
  3619.  when a user attempts the specified data modification statement on the
  3620.  specified table.
  3621.  
  3622.  
  3623.  Syntax
  3624.  
  3625.    CREATE TRIGGER [owner.]trigger_name
  3626.      ON [owner.]table_name
  3627.      FOR {INSERT | UPDATE | DELETE}
  3628.      [,{INSERT | UPDATE | DELETE}...]
  3629.      AS SQL_statements |
  3630.      IF UPDATE (column_name)
  3631.      [{AND | OR} UPDATE (column_name)...]
  3632.  
  3633.  
  3634.  Examples
  3635.  
  3636.    A. create trigger reminder
  3637.     on titles
  3638.     for insert, update as
  3639.     print "Don't forget to print a report for accounting."
  3640.  
  3641.  Example A prints a message every time anyone tries to add data or change
  3642.  data in the titles table.
  3643.  
  3644.    B. create trigger t1
  3645.     on titleauthor
  3646.     for insert as
  3647.     if (select count(*)
  3648.      from titles, inserted
  3649.      where titles.title_id = inserted.title_id) = 0
  3650.     begin
  3651.      print "Please put the book's title_id in the titles
  3652.      table first."
  3653.      rollback transaction
  3654.     end
  3655.  
  3656.  Example B prevents insertion of a new row into titleauthor if there is no
  3657.  corresponding title_id in the titles table.
  3658.  
  3659.    C. create trigger t2
  3660.     on publishers
  3661.     for update as
  3662.     if update (pub_id)
  3663.     begin
  3664.      update titles
  3665.      set titles.pub_id = inserted.pub_id
  3666.      from titles, deleted, inserted
  3667.      where deleted.pub_id = titles.pub_id
  3668.     end
  3669.  
  3670.  If the pub_id column of the publishers table is changed, example C makes the
  3671.  corresponding change in the titles table.
  3672.  
  3673.    D. create trigger t3
  3674.     on titleauthor
  3675.     for delete as
  3676.     begin
  3677.      delete titles
  3678.      from titles, deleted
  3679.      where deleted.title_id = titles.title_id
  3680.      delete titleauthor
  3681.      from titleauthor, deleted
  3682.      where deleted.title_id = titleauthor.title_id
  3683.      print "All references to this title have been deleted
  3684.      from titles and titleauthor."
  3685.     end
  3686.  
  3687.  If any row is deleted from titleauthor, that title is also deleted from the
  3688.  titles table (example D). If the book was written by more than one author,
  3689.  other references to it in titleauthor are also deleted.
  3690.  
  3691.  
  3692.  Options
  3693.  
  3694.  trigger_name
  3695.    The name of the trigger. It must conform to the rules for identifiers and
  3696.    must be unique in the database.
  3697.  
  3698.  INSERT, DELETE, UPDATE
  3699.    Keywords that can be included in any combination.
  3700.  
  3701.  SQL_statements
  3702.    Statements that specify trigger conditions and trigger actions. Trigger
  3703.    conditions specify additional criteria that determine whether the
  3704.    attempted INSERT, DELETE, or UPDATE will cause the trigger action(s) to be
  3705.    carried out. Trigger conditions often include a subquery preceded by the
  3706.    keyword IF.
  3707.  
  3708.    The trigger actions specified in the SQL statements go into effect when
  3709.    the user action (UPDATE, INSERT, or DELETE) is attempted. If multiple
  3710.    trigger actions are specified, they are grouped with BEGIN and END.
  3711.  
  3712.    Triggers can include any number and kind of SQL statements except SELECT
  3713.    statements; a trigger cannot return data to the user. The SQL statements
  3714.    in a trigger are similar to those in a stored procedure in that they often
  3715.    include control-of-flow language. A few special names are used in CREATE
  3716.    TRIGGER statements:
  3717.  
  3718.  
  3719.    ■   Deleted and inserted are logical (conceptual) tables. They are
  3720.        structurally like the table on which the trigger is defined─that is,
  3721.        the table on which the user action is attempted─and hold the old
  3722.        values or new values of the rows that would be changed by the user
  3723.        action. Deleted and inserted tables can be examined by the trigger to
  3724.        determine whether or how the trigger action(s) should be carried out.
  3725.        Deleted tables are used with DELETE and UPDATE; inserted with INSERT
  3726.        and UPDATE. See examples.
  3727.  
  3728.    ■   IF UPDATE is used to test if the specified column has been modified in
  3729.        any way. This allows specified trigger actions to be associated with
  3730.        updates to specified columns. See example B. More than one column can
  3731.        be specified. Use IF UPDATE like this:
  3732.  
  3733.        IF UPDATE (column_name) [{AND | OR} UPDATE (column_name)...]
  3734.  
  3735.  
  3736.  
  3737.  
  3738.  Comments
  3739.  
  3740.  Triggers are commonly used to enforce referential integrity (integrity rules
  3741.  about relationships between the primary and foreign keys of tables or
  3742.  views), to supply cascading deletes, and to supply cascading updates. (See
  3743.  examples B, C, and D, respectively.)
  3744.  
  3745.  You can define primary, foreign, and common keys on a table or view with the
  3746.  sp_primarykey, sp_foreignkey, and sp_commonkey system procedures. For a
  3747.  report on the keys that have been defined, use sp_helpkey.
  3748.  
  3749.  Trigger conditions specify additional criteria that determine whether the
  3750.  attempted INSERT, DELETE, or UPDATE will cause the trigger action(s) to be
  3751.  carried out. In example B (and in most triggers with trigger conditions),
  3752.  the subquery that follows the keyword IF is the trigger condition.
  3753.  
  3754.  Trigger actions specify the action(s) taken by SQL Server when the user
  3755.  action (that meets the trigger condition, if one is included) is attempted.
  3756.  Multiple statements are grouped with BEGIN and END.
  3757.  
  3758.  You can create a trigger in the current database only. If you use an owner
  3759.  name to qualify a trigger, you must explicitly qualify the table name the
  3760.  same way.
  3761.  
  3762.  A trigger may reference objects outside the current database.
  3763.  
  3764.  Triggers cannot be nested. That is, one trigger can't call another. If a
  3765.  trigger changes a table on which there is another trigger, the second
  3766.  trigger won't be executed.
  3767.  
  3768.  If you need a trigger to cause two actions, try to put both of the actions
  3769.  into a single trigger. (Or consider using a stored procedure instead.)
  3770.  
  3771.  For example, you might create an INSERT trigger on the sales table that
  3772.  updates titles.ytd_sales. You'd also like a trigger on updates to
  3773.  titles.ytd_sales that checks the royalty column in titles and changes it if
  3774.  necessary.
  3775.  
  3776.  The correct method is to write a single trigger that takes both actions─the
  3777.  update of titles.ytd_sales and the (possible) update of titles.royalty. This
  3778.  is because triggers cannot be nested─that is, one trigger cannot cause the
  3779.  execution of a second trigger.
  3780.  
  3781.  In performance terms, trigger overhead is usually very low. The time
  3782.  involved in running a trigger is spent mostly in referencing other tables,
  3783.  which may be either in memory or on the disk.
  3784.  
  3785.  The deleted and inserted tables, often referenced by triggers, are always in
  3786.  memory rather than on the disk because they are logical tables. The location
  3787.  of other tables referenced by the trigger determines the amount of time the
  3788.  operation takes.
  3789.  
  3790.  When an INSERT or UPDATE statement is executed, rows are added to the
  3791.  trigger table and to inserted at the same time. So the rows in inserted are
  3792.  always duplicates of one or more rows in the trigger table.
  3793.  
  3794.  Once a trigger is defined, the user action it specifies on the table to
  3795.  which it applies is always implicitly part of a transaction, along with the
  3796.  trigger itself. Triggers are often used to roll back that transaction if an
  3797.  error is detected.
  3798.  
  3799.  An UPDATE or INSERT trigger can make use of the IF UPDATE statement. It is
  3800.  used to test if the UPDATE or INSERT changed a particular column.
  3801.  
  3802.  IF UPDATE(column_name) is true for an INSERT statement whenever the column
  3803.  is assigned a value in the select list or in the VALUES clause. An explicit
  3804.  NULL or a default assigns a value to a column and thus activates the
  3805.  trigger. An implicit NULL, however, does not. Here are some examples:
  3806.  
  3807.    CREATE TABLE junk
  3808.     (a int null,
  3809.     b int not null)
  3810.  
  3811.    A. INSERT junk (a, b)
  3812.     VALUES (1, 2)
  3813.  
  3814.  IF UPDATE is true for either column
  3815.  
  3816.    B. INSERT junk
  3817.     VALUES (1, 2)
  3818.  
  3819.  IF UPDATE is true for either column
  3820.  
  3821.    C. INSERT junk
  3822.     VALUES (NULL, 2)
  3823.  
  3824.  Explicit NULL IF UPDATE is true for either column
  3825.  
  3826.    D. INSERT junk (b)
  3827.     VALUES (2)
  3828.  
  3829.  With a default for column a, IF UPDATE is true for either column
  3830.  
  3831.    E. INSERT junk (b)
  3832.     VALUES (2)
  3833.  
  3834.  With no default for column a, IF UPDATE is not true for column a
  3835.  
  3836.  IF UPDATE is never true for a DELETE statement.
  3837.  
  3838.  The text of the CREATE TRIGGER statement is stored in syscomments. To
  3839.  display it, execute the sp_helptext system procedure.
  3840.  
  3841.  The execution plan for the trigger is stored in sysprocedures. Each trigger
  3842.  is assigned an identification number, which is stored as a new row in
  3843.  sysobjects and as an entry in the sysobjects row for the table to which it
  3844.  applies.
  3845.  
  3846.  You can use the SET statement inside a trigger. The SET option you invoke
  3847.  remains in effect during the execution of the trigger and then reverts to
  3848.  its former setting.
  3849.  
  3850.  It is recommended that a trigger not include SELECT statements that return
  3851.  results to the user, since special handling for these returned results would
  3852.  have to be written into every application program in which modifications to
  3853.  the trigger table are allowed.
  3854.  
  3855.  You cannot create a trigger on a view or on a temporary object.
  3856.  
  3857.  A TRUNCATE TABLE statement is not caught by a DELETE trigger. Although a
  3858.  TRUNCATE TABLE statement is, in effect, like a DELETE without a WHERE clause
  3859.  (it removes all rows), it is not logged, and so cannot execute a trigger.
  3860.  
  3861.  Since permission for the TRUNCATE TABLE statement defaults to the table
  3862.  owner and is not transferable, only the table owner should be concerned
  3863.  about inadvertently circumventing a DELETE trigger with a TRUNCATE TABLE
  3864.  statement.
  3865.  
  3866.  When you drop a table, any triggers associated with it are also dropped.
  3867.  
  3868.  A trigger cannot apply to more than one table. However, the same trigger
  3869.  action can be defined for all three user actions in the same CREATE TRIGGER
  3870.  statement. A table can have a maximum of three different triggers─one each
  3871.  for INSERT, UPDATE, and DELETE.
  3872.  
  3873.  For a report on a trigger, execute the sp_help system procedure. For a
  3874.  report on the tables and views that are referenced by a trigger, use
  3875.  sp_depends.
  3876.  
  3877.  You can rename a trigger with sp_rename.
  3878.  
  3879.  
  3880.  Permissions
  3881.  
  3882.  CREATE TRIGGER permission defaults to the table owner and is not
  3883.  transferable.
  3884.  
  3885.  
  3886.  See Also
  3887.  
  3888.  CREATE PROCEDURE, DROP TRIGGER, sp_commonkey, sp_depends, sp_foreignkey,
  3889.  sp_help, sp_helptext, sp_primarykey, sp_rename, sp_spaceused
  3890.  
  3891.  
  3892.  CREATE VIEW
  3893.  ────────────────────────────────────────────────────────────────────────────
  3894.  
  3895.  
  3896.  Function
  3897.  
  3898.  Creates views (alternative ways of looking at the data in one or more
  3899.  tables).
  3900.  
  3901.  
  3902.  Syntax
  3903.  
  3904.    CREATE VIEW [owner.]view_name
  3905.      [(column_name [, column_name]...)]
  3906.     AS select_statement
  3907.  
  3908.  
  3909.  Examples
  3910.  
  3911.    A. create view titles_view
  3912.     as select title, type, price, pubdate
  3913.     from titles
  3914.  
  3915.    B. create view accounts (title, advance, amt_due)
  3916.     as select title, advance, price * royalty * ytd_sales
  3917.     from titles
  3918.     where price > $5
  3919.  
  3920.    C. create view cities (authorname, acity, publishername, pcity)
  3921.     as select au_lname, authors.city, pub_name, publishers.city
  3922.     from authors, publishers
  3923.     where authors.city = publishers.city
  3924.  
  3925.    D. create view cities2
  3926.     as select authorname = au_lname, acity = authors.city,
  3927.      publishername = pub_name, pcity = publishers.city
  3928.     from authors, publishers
  3929.     where authors.city = publishers.city
  3930.  
  3931.  
  3932.  Options
  3933.  
  3934.  view_name
  3935.    The name of the new view. It must be a legal identifier.
  3936.  
  3937.  column_name
  3938.    The names to be used for the columns in the view. This is always legal but
  3939.    only necessary when a column is derived from an arithmetic expression, a
  3940.    function, or a constant; when two or more columns could otherwise have the
  3941.    same name (usually because of a join); or when you want to give a column
  3942.    in a view a different name than the column from which it is derived. (See
  3943.    example C.) Column names can also be assigned in the SELECT statement.
  3944.    (See example D.)
  3945.  
  3946.    If no column names are specified, the view columns acquire the same names
  3947.    as the columns in the SELECT statement.
  3948.  
  3949.  select_statement
  3950.    The SELECT statement that defines the view. It can use more than one table
  3951.    and other views.
  3952.  
  3953.  
  3954.  Comments
  3955.  
  3956.  You cannot include ORDER BY or COMPUTE clauses or the DISTINCT or INTO
  3957.  keywords in the SELECT statements that define views.
  3958.  
  3959.  You cannot create a trigger on a view.
  3960.  
  3961.  You can create a view in the current database only.
  3962.  
  3963.  Currently there are no temporary views.
  3964.  
  3965.  If you alter the structure of a view's underlying table(s) by adding
  3966.  columns, the new columns will not appear in a view defined with a "SELECT *"
  3967.  clause unless the view is deleted and redefined. The asterisk shorthand is
  3968.  interpreted and expanded when the view is first created.
  3969.  
  3970.  If a view depends on a table (or view) that has been dropped, SQL Server
  3971.  produces an error message if anyone tries to use the view. If a new table
  3972.  (or view) is created to replace the one that has been dropped, the view
  3973.  again becomes usable.
  3974.  
  3975.  You cannot create a view on a temporary table.
  3976.  
  3977.  You can redefine a view without redefining other views that depend on it
  3978.  unless the redefinition makes it impossible for SQL Server to translate the
  3979.  dependent view.
  3980.  
  3981.  CREATE VIEW statements cannot be combined with other SQL statements in a
  3982.  single batch.
  3983.  
  3984.  There are no restrictions on querying through views, but there are some
  3985.  restrictions on data update through views.
  3986.  
  3987.  Data update statements may not change any column in a view that is a
  3988.  computation.
  3989.  
  3990.  Data update statements may not change a view that includes aggregate
  3991.  functions (built-in functions and a GROUP BY or COMPUTE BY clause).
  3992.  
  3993.  INSERT statements are not allowed unless all NOT NULL columns in the
  3994.  underlying table or view are included in the view through which you are
  3995.  inserting new rows. (SQL Server has no way to supply values for NOT NULL
  3996.  columns in the underlying table or view.)
  3997.  
  3998.  INSERT and UPDATE statements are not allowed unless the columns being
  3999.  updated all belong to the same base table. (It's illegal in SQL to use data
  4000.  update statements on more than one table in a single statement.) However, if
  4001.  a view contains columns from more than one table but the data update
  4002.  statement references columns from only one of those tables, the statement is
  4003.  legal.
  4004.  
  4005.  When you query through a view, SQL Server checks to make sure that all the
  4006.  database objects referenced anywhere in the statement exist, that they are
  4007.  valid in the context of the statement, and that data update statements do
  4008.  not violate data integrity rules. If any of these checks fail, you get an
  4009.  error message. If the checks are successful, it translates the view into an
  4010.  action on the underlying table(s).
  4011.  
  4012.  You can use views as security mechanisms by granting permission on a view
  4013.  but not on underlying tables.
  4014.  
  4015.  The text of the CREATE VIEW statement is stored in syscomments. To display
  4016.  it, execute the sp_helptext system procedure with the view name as the
  4017.  parameter.
  4018.  
  4019.  You can rename a view with sp_rename. To get a report of the tables or views
  4020.  on which a view depends, and of objects that depend on a view, execute the
  4021.  sp_depends system procedure.
  4022.  
  4023.  See SQL Server Learning TRANSACT-SQL for more information and examples on
  4024.  views.
  4025.  
  4026.  
  4027.  Permissions
  4028.  
  4029.  CREATE VIEW permission defaults to the Database Owner, who can transfer it
  4030.  to other users. The creator of a view must have SELECT permission on any
  4031.  objects referenced in the view definition.
  4032.  
  4033.  
  4034.  See Also
  4035.  
  4036.  DROP VIEW, Views, sp_depends, sp_help, sp_helptext, sp_rename
  4037.  
  4038.  
  4039.  
  4040.  
  4041.  
  4042.  Datatypes
  4043.  ────────────────────────────────────────────────────────────────────────────
  4044.  
  4045.  
  4046.  Function
  4047.  
  4048.  Specify data characteristics of columns, stored procedure parameters, and
  4049.  local variables. For text and image datatypes used to store long string of
  4050.  data, see "Text/Image Datatypes."
  4051.  
  4052.  
  4053.  Syntax
  4054.  
  4055.    column_name text
  4056.     column_name image
  4057.     column_name timestamp
  4058.     {param_name | variable_name | column_name} int
  4059.     {param_name | variable_name | column_name} smallint
  4060.     {param_name | variable_name | column_name} tinyint
  4061.     {param_name | variable_name | column_name} float
  4062.     {param_name | variable_name | column_name} char(n)
  4063.     {param_name | variable_name | column_name} varchar(n)
  4064.     {param_name | variable_name | column_name} binary(n)
  4065.     {param_name | variable_name | column_name} varbinary(n)
  4066.     {param_name | variable_name | column_name} bit
  4067.     {param_name | variable_name | column_name} money
  4068.     {param_name | variable_name | column_name} datetime
  4069.     {param_name | variable_name | column_name} sysname
  4070.     {param_name | variable_name | column_name} user_type_name
  4071.  
  4072.  Datatypes include system datatypes and user-defined datatypes. SQL Server
  4073.  supplies the sysname user datatype. The pubs sample database also supplies
  4074.  user datatypes. You can also create your own user datatypes. Some datatypes
  4075.  require a length.
  4076.  
  4077.  
  4078.  Examples
  4079.  
  4080.    A. create table food
  4081.      (fname varchar(20),
  4082.      fcals int,
  4083.      frate int)
  4084.  
  4085.    B. create procedure auname_sp @auname varchar(40)
  4086.     as
  4087.      select au_lname, title, au_ord
  4088.      from authors, titles, titleauthor
  4089.      where @auname = au_lname
  4090.      and authors.au_id = titleauthor.au_id
  4091.      and titles.title_id = titleauthor.title_id
  4092.  
  4093.    C. declare @hope money
  4094.  
  4095.  
  4096.  Options
  4097.  
  4098.  param_name
  4099.    The name of a stored procedure parameter. It must conform to the rules for
  4100.    identifiers.
  4101.  
  4102.  variable_name
  4103.    The name of a local variable. It must conform to the rules for
  4104.    identifiers.
  4105.  
  4106.  column_name
  4107.    The name of a database column. It must conform to the rules for
  4108.    identifiers and must be unique within its table.
  4109.  
  4110.  int
  4111.    An integer column that holds whole numbers between 231- 1 (that is,
  4112.    2,147,483,647) and -231+ 1 (that is, -2,147,483,648), inclusive. However,
  4113.    you cannot enter -2,147,483,648, but you can enter -2,147,483,647 - 1. You
  4114.    can store this number or it can be the result of a calculation. Storage
  4115.    size is 4 bytes.
  4116.  
  4117.  smallint
  4118.    A small integer column that holds whole numbers between 215- 1 (32,767)
  4119.    and -215 (-32,768), inclusive. Storage size is 2 bytes.
  4120.  
  4121.  tinyint
  4122.    A tiny integer column that holds whole numbers between 0 and 255,
  4123.    inclusive. Storage size is 1 byte.
  4124.  
  4125.  float
  4126.    A floating-point column that holds positive or negative floating-point
  4127.    numbers. This column has 15-digit precision. The range of values is
  4128.    approximately 1.7E - 308 to 1.7E + 308. Storage size is 8 bytes. All the
  4129.    arithmetic operations except modulo are available with float.
  4130.  
  4131.    Data of type float can include an optional exponent. For float data with
  4132.    an exponential component, enter a number (with or without a decimal point
  4133.    and a positive or negative sign), followed by "e" or "E", followed by an
  4134.    optionally signed integer. The value represented by a float is the product
  4135.    of the first number and the power of 10 of the second number.
  4136.  
  4137.  char(n)
  4138.    A character column that any combination of up to 255 letters, symbols, and
  4139.    numbers. Specify the maximum size of the column with n. A char column can
  4140.    contain 0 characters, but n must be between 1 and 255. Storage size is n,
  4141.    no matter what the actual entry length.
  4142.  
  4143.    Columns of type char are accessed a little faster than varchar columns and
  4144.    use more storage space. Choose char when you know the size of character
  4145.    data (as in a social security number or a zip code). Data of type char
  4146.    must be enclosed in single or double quotation marks when it's input.
  4147.    Numbers stored in char columns must be converted to a numeric data type
  4148.    before you can do arithmetic on them.
  4149.  
  4150.    You can use the LIKE keyword and wildcard characters with char, varchar,
  4151.    and datetime data (but not to search for seconds or milliseconds).
  4152.  
  4153.    If you enter strings that are too long for the specified length, char and
  4154.    varchar entries are truncated.
  4155.  
  4156.  varchar(n)
  4157.    A column of variable-length characters that holds any combination of up to
  4158.    255 letters, symbols, and numbers. Specify the maximum size of the column
  4159.    with n. A varchar column can contain 0 characters, but n must be between 1
  4160.    and 255. Storage size is the actual size of the data values entered, not
  4161.    n.
  4162.  
  4163.    Because of the way it uses space, varchar is best suited for data-like
  4164.    names where the length of each entry can vary considerably.
  4165.  
  4166.    Data of type varchar must be enclosed in single or double quotation marks
  4167.    when it's input. You can use the LIKE keyword and wildcard characters with
  4168.    char, varchar, and datetime data (but not to search for seconds or
  4169.    milliseconds).
  4170.  
  4171.    If you enter strings that are too long for the specified length, char and
  4172.    varchar entries are truncated.
  4173.  
  4174.  text
  4175.    Variable-length columns that can hold up to 2,147,483,647 (231- 1)
  4176.    characters. The text datatype may not be used for variables or parameters
  4177.    in stored procedures. (See "Text/Image Datatypes.")
  4178.  
  4179.  binary(n)
  4180.    A column that holds up to 255 bytes of fixed-length binary data. The
  4181.    binary datatypes are not for hexadecimal data, but rather for bit
  4182.    patterns. They are used for storing programming code or pictures, not for
  4183.    hexadecimal numbers.
  4184.  
  4185.    Specify the maximum byte length of the column with n. A binary column can
  4186.    contain 0 bytes, but n must be between 1 and 255. Storage size is n no
  4187.    matter what the actual entry length. The maximum binary value is 255
  4188.    bytes, each of which is "ff".
  4189.  
  4190.    Columns of type binary are accessed a little faster than varbinary columns
  4191.    and use more storage space. Choose binary when you think the data entries
  4192.    in the column will be close to the same size. Data of type binary consists
  4193.    of the characters 0 to 9 and A to F (or a to f), and the first binary
  4194.    string must be preceded with "0x" when it's input. For example, to input
  4195.    "FF", type 0xFF. A length of 10 for a binary or varbinary column means 20
  4196.    characters. If you enter strings that are too long for the specified
  4197.    length, binary and varbinary entries are truncated.
  4198.  
  4199.  varbinary(n)
  4200.    A variable-length binary column that holds up to 255 bytes of
  4201.    variable-length binary data. The binary datatypes are not for hexadecimal
  4202.    data, but rather for bit patterns. They are used for storing programming
  4203.    code or pictures, not for hexadecimal numbers. Specify the maximum size of
  4204.    the column with n. A varbinary column can contain 0 bytes, but n must be
  4205.    between 1 and 255. Storage size is the actual size of the data values
  4206.    entered, not n.
  4207.  
  4208.    Because of the way it uses space, choose varbinary when you expect
  4209.    variation in the size of the data.
  4210.  
  4211.    Data of type varbinary consists of the characters 0 to 9 and A to F (or a
  4212.    to f), and the first varbinary string must be preceded with "0x" when it's
  4213.    input.
  4214.  
  4215.    A length of 10 for a binary or varbinary column means 20 characters.
  4216.  
  4217.    If you enter strings that are too long for the specified length, binary
  4218.    and varbinary entries are truncated.
  4219.  
  4220.  image
  4221.    A variable-length column that holds between 0 and 2,147,483,647 bytes of
  4222.    binary data. The image datatype may not be used for variables or
  4223.    parameters in stored procedures. (See "Text/Image Datatypes" for details.)
  4224.  
  4225.  bit
  4226.    A column that holds either a 1 or a 0. Integer values other than 1 or 0
  4227.    are accepted but are always interpreted as 1. Storage size is 1 bit.
  4228.    Multiple bit types in a table can be collected into bytes. For example, 7
  4229.    bit columns fit into 1 byte; 9 bit columns take 2 bytes. The status column
  4230.    in the syscolumns system table indicates the unique offset position for
  4231.    bit columns.
  4232.  
  4233.    Columns of type bit cannot be NULL and cannot have indexes on them. Use
  4234.    bit for true/false or yes/no types of data.
  4235.  
  4236.  money
  4237.    A column that stores dollar and cent values between
  4238.    ±922,337,203,685,477.5807 dollars with accuracy to a ten-thousandth of a
  4239.    dollar. Money values are represented as double-precision integers. Storage
  4240.    size is 8 bytes.
  4241.  
  4242.    Use money for all decimal currency entries. Data of type money should be
  4243.    preceded with a dollar sign ($) when it's input. If there is no dollar
  4244.    sign, SQL Server treats the value as a float. Depending on the exact
  4245.    input, SQL Server may not accept it, or may lose some of the value's
  4246.    precision.
  4247.  
  4248.    To represent negative money values, a minus sign (-) must be placed after
  4249.    the dollar sign. You cannot enter money values with commas, although the
  4250.    default print format for money data places a comma after every three
  4251.    digits.
  4252.  
  4253.    When money values are displayed, they are rounded up to the nearest cent.
  4254.    All the arithmetic operations except modulo are available with money.
  4255.  
  4256.  datetime
  4257.    A column that holds dates and times of day. Storage size is two 4-byte
  4258.    integers: 4 bytes for the number of days before or after the base date of
  4259.    January 1, 1900, and 4 bytes for the number of milliseconds after
  4260.    midnight. The date segment of datetime values representing dates before
  4261.    the base date are stored as negative values.
  4262.  
  4263.    Data values for datetime range from January 1, 1753, to December 31, 9999.
  4264.    The maximum datetime value is the last day of the year 9999. SQL Server
  4265.    will reject all values it cannot recognize as dates between 1753 and 9999.
  4266.  
  4267.    SQL Server stores datetime data to an accuracy of .003 milliseconds. The
  4268.    entries .001, .002, and .003 are stored as .000, while an entry of .004 is
  4269.    stored as .003.
  4270.  
  4271.    The default display format for dates is "mon dd yyyy hh:miAM" (or PM),
  4272.    such as "Apr 15 1988 10:23AM".
  4273.  
  4274.    When you enter datetime values, always enclose them in single or double
  4275.    quotation marks. Case is always ignored, and spaces can occur anywhere
  4276.    between date parts. SQL Server recognizes a wide variety of data entry
  4277.    formats for date and time values.
  4278.  
  4279.    When time data is entered, the order of time components is significant:
  4280.    hours; then minutes; then seconds; then milliseconds; then AM, am, PM, or
  4281.    pm (12AM is midnight, 12PM is noon). To be recognized as time, a value
  4282.    must contain either a colon or an AM/PM signifier.
  4283.  
  4284.    Milliseconds can be preceded either with a colon or a period. If preceded
  4285.    by a colon, the number means thousandths of a second. If preceded by a
  4286.    period, a single digit means tenths of a second, two digits mean
  4287.    hundredths of a second, and three digits mean thousandths of a second. For
  4288.    example, "12:30:20:1" means twenty and one-thousandth second past 12:30;
  4289.    "12:30:20.1" means twenty and one-tenth second past 12:30.
  4290.  
  4291.    The following formats for time data are acceptable:
  4292.  
  4293.    14:30
  4294.      14:30[:20:999]
  4295.      14:30[:20.9]
  4296.      4am
  4297.      4 PM
  4298.      [0]4[:30:20:500]AM
  4299.  
  4300.    When date data is entered, the order of components can vary. Months can be
  4301.    in alphabetic or numeric format. Alphabetic format is either the full name
  4302.    of the month or its first three letters. Case is not significant.
  4303.  
  4304.    If the month is given in numeric format, you can use any of the following
  4305.    separators: slash (/), hyphen (-), period (.), or space ( ). You can also
  4306.    give a date without separators: you can enter January 1, 1998 as
  4307.    "19980101" or "980101".
  4308.  
  4309.    If the month is given in alphabetic format, the day and year can be in any
  4310.    location, either before or after the month. In addition to the previously
  4311.    mentioned separators, the day can optionally be followed by a comma (,).
  4312.    When the month is alphabetic and the values for day and year are grouped
  4313.    together, the first value is assumed to be the day. For example, 2 3 mar
  4314.    is stored as "Mar 2 2003 12:00:00AM".
  4315.  
  4316.    If the year is given with two digits, <50 is next century and >=50 is this
  4317.    century. So "25" is "2025" while "50" is "1950".
  4318.  
  4319.    The following formats for date data are acceptable:
  4320.  
  4321.    Apr[il] 15[,] [19]88
  4322.      [15] Apr[il][,] [19]88
  4323.      1988 APR[IL]
  4324.      4/15/1988
  4325.        4-15-88
  4326.        [19]88[0415]
  4327.  
  4328.    A datetime value can be either time then date or date then time, or the
  4329.    year can be separated from the rest of the date:
  4330.  
  4331.    10:23PM Apr 15 1988
  4332.      Apr 15 1988 10:23PM
  4333.      Apr 15 10:23PM 1988
  4334.  
  4335.    If time is missing, it defaults to midnight. For example, if you insert
  4336.    "Apr 15 1988", SQL Server stores this as "Apr 15 1988 12:00AM".
  4337.  
  4338.    If the day of the month is missing, it defaults to the first day of the
  4339.    month. If the month is missing, it defaults to January. If the entire date
  4340.    is missing, it defaults to the base date, January 1, 1900. For example, if
  4341.    you insert "4:33", SQL Server stores it as "Jan 1 1900 4:33AM".
  4342.  
  4343.    Use the CONVERT function's style parameter to obtain many additional date
  4344.    styles. (See "Conversion Function.")
  4345.  
  4346.    You can do some arithmetic calculations on datetime values with the
  4347.    built-in date functions. (See "Date Functions.")
  4348.  
  4349.    When you use LIKE with datetime values, SQL Server converts the dates to
  4350.    the standard datetime format and then to varchar. Since the standard
  4351.    storage format doesn't include seconds or milliseconds, you cannot search
  4352.    for seconds or milliseconds with LIKE and a match pattern.
  4353.  
  4354.    It is a good idea to use LIKE when you search for datetime values, since
  4355.    datetime entries may contain a variety of date parts. For example, if you
  4356.    insert the value "9:20" into a column named arrival_time, the clause WHERE
  4357.    arrival_time = "9:20 " would not find it because SQL Server converts the
  4358.    entry into "Jan 1, 1900 9:20AM". However, the clause WHERE arrival_time
  4359.    LIKE "%9:20%" would find it.
  4360.  
  4361.  sysname
  4362.    A user-defined datatype supplied by SQL Server and used in the system
  4363.    tables. Its definition is varchar(30) "NOT NULL".
  4364.  
  4365.  timestamp
  4366.    A column that is automatically updated when you use browse mode in a
  4367.    DB-LIBRARY application. (See "Browse Mode.") Every time a row containing a
  4368.    timestamp column is inserted or updated, the timestamp column is
  4369.    automatically updated. A table can have only one column of timestamp type.
  4370.    Its definition is varbinary (8) null.
  4371.  
  4372.    When a table is created that contains timestamp data, both the column and
  4373.    the datatype must be named "timestamp".
  4374.  
  4375.  user_type_name
  4376.    The name of a user-defined datatype created with the sp_addtype system
  4377.    procedure. For example, to add a type called nm, you might execute the
  4378.    following system procedure:
  4379.  
  4380.    sp_addtype nm, "varchar(30)", null
  4381.  
  4382.    The name of the user-defined datatype must conform to the rules for
  4383.    identifiers.
  4384.  
  4385.  
  4386.  Comments
  4387.  
  4388.  All system datatypes and user-defined datatypes default to NOT NULL in
  4389.  CREATE TABLE statements. When you include a user-defined datatype in a
  4390.  CREATE TABLE statement, you can override its nulltype specification.
  4391.  However, you cannot override the length specification.
  4392.  
  4393.  Datatypes are database objects, and their names must conform to the rules
  4394.  for identifiers. The names of the SQL-Server-supplied datatypes are not
  4395.  reserved words and can be used to name other objects.
  4396.  
  4397.  User-defined datatypes are defined in terms of system datatypes. Creating
  4398.  user-defined datatypes permits frequently used datatype and length
  4399.  information to be accessed under a name you choose. User-defined datatypes
  4400.  are created with the sp_addtype system procedure and dropped with
  4401.  sp_droptype. Rules and defaults can be bound to user datatypes with the
  4402.  sp_bindrule and sp_bindefault system procedures. See Chapter 2, "System
  4403.  Procedures," for details.
  4404.  
  4405.  When you create user-defined datatypes with sp_addtype, there are three
  4406.  parameters: the name you assign the datatype; the base datatype (one of the
  4407.  SQL-Server-supplied datatypes, which include length for char, varchar,
  4408.  binary, and varbinary); and either NULL or NOT NULL. The last parameter is
  4409.  optional.
  4410.  
  4411.  ────────────────────────────────────────────────────────────────────────────
  4412.  WARNING
  4413.  
  4414.  Whenever you create a table containing timestamp data, both the column and
  4415.  the datatype must be named literally "timestamp". For example, the following
  4416.  table has one timestamp column   create table dates  (timestamp timestamp)
  4417.  Do not change the name of a timestamp column using the sp_addtype system
  4418.  procedure. If you do not use "timestamp" as the column name and datatype
  4419.  name, the timestamp is not set when rows are inserted into a table.
  4420.  ────────────────────────────────────────────────────────────────────────────
  4421.  
  4422.  If you don't specify NULL or NOT NULL, NOT NULL is assumed. In the following
  4423.  examples, the first two examples are NOT NULL; the third is NULL. (Quotation
  4424.  marks are used when a parameter contains blanks or punctuation marks.)
  4425.  
  4426.    sp_addtype tid1, "char(6)"
  4427.     sp_addtype tid2, "char(6)", "not null"
  4428.     sp_addtype tid3, "char(6)", null
  4429.  
  4430.  The NULL/NOT NULL assignment for a user-defined datatype can be changed in a
  4431.  CREATE TABLE statement. However, the length specification cannot be changed;
  4432.  SQL Server will not allow you to specify a length for a user-defined
  4433.  datatype in a CREATE TABLE statement.
  4434.  
  4435.  User-defined datatypes cannot be used in temporary tables unless the
  4436.  datatypes exist in the tempdb database, that is, unless they have been
  4437.  explicitly created in tempdb during the current session or unless they have
  4438.  been permanently added to the model database by changing the instmodl.sql
  4439.  script. See the SQL Server Installation Guide for the location of this
  4440.  script on your system.
  4441.  
  4442.  All of the datatypes that handle numbers─int, smallint, tinyint, money, and
  4443.  float─support arithmetic operations and aggregate functions (except that you
  4444.  cannot use modulo with float or money). Computations that result in overflow
  4445.  conditions are handled as errors.
  4446.  
  4447.  Relational operators can be used on any of the datatypes except text and
  4448.  image.
  4449.  
  4450.  There are two ways to specify literal quotation marks within a char or
  4451.  varchar entry. The first method is to use two quotation marks. For example,
  4452.  if you have begun a character entry with a single quotation mark and wish to
  4453.  include a single quotation mark as part of the entry, use two single
  4454.  quotation marks:
  4455.  
  4456.    'I don"t understand.'
  4457.  
  4458.  With double quotation marks:
  4459.  
  4460.    "He said, ""It's not really confusing."""
  4461.  
  4462.  The second method is to enclose one quotation mark in the opposite kind of
  4463.  quotation mark. In other words, surround an entry containing double
  4464.  quotation marks with single quotation marks (or vice versa). Here are some
  4465.  examples:
  4466.  
  4467.    'George said, "There must be a better way."'
  4468.     "Isn't there a better way?"
  4469.     'George asked, "Isn"t there a better way?"'
  4470.  
  4471.  To continue a character string onto the next line of your screen, enter a
  4472.  backslash (\) before going to the next line.
  4473.  
  4474.  Built-in functions are provided for character, numeric, and date datatypes.
  4475.  (See "Date Functions," "Mathematical Functions," "String Functions," and
  4476.  "Text Functions" for details.)
  4477.  
  4478.  Many conversions from one datatype to another are handled automatically by
  4479.  SQL Server. Others are available with the CONVERT function. However, there
  4480.  are some type conversions that can never be done. For example, you cannot
  4481.  convert datetime to tinyint. (See "Conversion Function" for more
  4482.  information.) Text and image data can be converted to char or varchar and
  4483.  binary or varbinary, respectively.
  4484.  
  4485.  Use the style parameter of the CONVERT function to obtain a wide variety of
  4486.  date display formats. (See "Conversion Function" for more information.)
  4487.  
  4488.  When you create NULL columns with certain datatypes, SQL Server
  4489.  automatically converts them to a different internal datatype to allow the
  4490.  storage of null values. The char datatype is automatically converted to
  4491.  varchar; binary to varbinary; datetime to datetimn; float to floatn; int,
  4492.  smallint, and tinyint to intn; and money to moneyn.
  4493.  
  4494.  SQL Server does not inform the user of the type change, and the user should
  4495.  be concerned about it only if querying the system tables.
  4496.  
  4497.  For a report on a user-defined datatype, execute sp_help. You can rename a
  4498.  user-defined datatype with sp_rename.
  4499.  
  4500.  When you perform mixed-mode arithmetic─arithmetic operations on values of
  4501.  different datatypes, for example int + smallint ─the resultant datatype is
  4502.  determined by the hierarchy of datatypes. The hierarchy of datatypes is
  4503.  created from the rank ordering of the hexadecimal values of the datatype
  4504.  codes. In a mixed-mode expression, the lower type is converted internally to
  4505.  the higher type.
  4506.  
  4507.  In the following example, the qty from the sales table is multiplied by
  4508.  royalty from the roysched table. The qty is a smallint (INT2), and the
  4509.  royalty is an int (INT4). INT2 has a hexadecimal value of 34; INT4 has a
  4510.  hexadecimal value of 38. Therefore, the resultant datatype is an int (INT4).
  4511.  
  4512.  
  4513.    smallint(qty) * int(royalty) = int
  4514.  
  4515.  The hierarchy of datatypes is shown in the systypes system table. (Also see
  4516.  the SQL Server System Administrator's Guide and the "Conversion Function.")
  4517.  
  4518.  
  4519.  You can use the following query to obtain a list of datatypes with their
  4520.  decimal and hexadecimal values. Select the name of the datatype and its
  4521.  decimal type code, then use CONVERT to compute the hexadecimal value of each
  4522.  type.
  4523.  
  4524.    select name, type, hex=convert(varbinary(2),type)
  4525.      from systypes
  4526.     order by type
  4527.     <execute>
  4528.  
  4529. ╓┌─┌───────────────────┌────────┌────────────────────────────────────────────╖
  4530.  ────────────────────────────────────────────────────────────────────────────
  4531.    name                type     hex
  4532.    ------------        -------  -------
  4533.    text                35       0x23
  4534.    timestamp           37       0x25
  4535.    varbinary           37       0x25
  4536.    intn                38       0x26
  4537.    sysname             39       0x27
  4538.  ────────────────────────────────────────────────────────────────────────────
  4539.   sysname             39       0x27
  4540.    varchar             39       0x27
  4541.    binary              45       0x2d
  4542.    image               46       0x2e
  4543.    char                47       0x2f
  4544.    tinyint             48       0x30
  4545.    bit                 50       0x32
  4546.    smallint            52       0x34
  4547.    int                 56       0x38
  4548.    money               60       0x3c
  4549.    datetime            61       0x3d
  4550.    float               62       0x3e
  4551.    floatn              109      0x6d
  4552.    moneyn              110      0x6e
  4553.    datetimn            111      0x6f
  4554.  
  4555.    (19 rows affected)
  4556.  
  4557.  
  4558.  The results of this query on one of your databases will be slightly
  4559.  different if you have installed any user-defined datatypes. User types take
  4560.  on the hexadecimal value of their base types.
  4561.  
  4562.  
  4563.  See Also
  4564.  
  4565.  Aggregate Functions, Conversion Function, CREATE PROCEDURE, CREATE TABLE,
  4566.  Date Functions, EXECUTE, Parameters, Text/Image Datatypes, Text/Image
  4567.  Functions, Variables (Local and Global), Wildcard Characters, sp_addtype,
  4568.  sp_bindefault, sp_bindrule, sp_droptype, sp_help, sp_rename,
  4569.  sp_unbindefault, sp_unbindrule
  4570.  
  4571.  
  4572.  Date Functions
  4573.  ────────────────────────────────────────────────────────────────────────────
  4574.  
  4575.  
  4576.  Function
  4577.  
  4578.  Manipulate datetime values.
  4579.  
  4580.  
  4581.  Syntax
  4582.  
  4583.  All date functions take parameters except GETDATE. Function names,
  4584.  parameters, and results are as follows:
  4585.  
  4586. ╓┌─────────┌────────────────────────────────┌────────────────────────────────╖
  4587.  Function  Parameter                        Result
  4588.  ────────────────────────────────────────────────────────────────────────────
  4589.  DATEADD   (datepart, number, date)         A date produced by adding an
  4590.                                             interval to a date you specify.
  4591.                                             The result is a datetime value
  4592.                                             equal to the date plus the
  4593.                                             number of date parts.
  4594.  
  4595.  DATEDIFF  (datepart, date1, date2)         The number of dateparts between
  4596.                                             two specified dates. The result
  4597.                                             is a signed integer value equal
  4598.                                             to date2 minus date1 in date
  4599.                                             parts.
  4600.  
  4601.  Function  Parameter                        Result
  4602.  ────────────────────────────────────────────────────────────────────────────
  4603. 
  4604.  DATENAME  (datepart, date)                 The specified datepart (the
  4605.                                             first parameter) of the
  4606.                                             specified date (the second
  4607.                                             parameter) as a character
  4608.                                             string.
  4609.  
  4610.  DATEPART  (datepart, date)                 The specified datepart (the
  4611.                                             first parameter) of the
  4612.                                             specified date (the second
  4613.                                             parameter) as an integer.
  4614.  
  4615.  GETDATE   ( )                              The current date and time in
  4616.                                             SQL Server's standard internal
  4617.                                             format for datetime values.
  4618.                                             GETDATE takes the NULL
  4619.                                             parameter, ( ).
  4620.  
  4621.  ────────────────────────────────────────────────────────────────────────────
  4622.  Function  Parameter                        Result
  4623.  ────────────────────────────────────────────────────────────────────────────
  4624. ────────────────────────────────────────────────────────────────────────────
  4625.  
  4626.  
  4627.  
  4628.  
  4629.  Examples
  4630.  
  4631.    A. Statement        Result
  4632.  
  4633.     select rightnow = getdate( )   Nov 25 1995 10:32AM
  4634.     select datepart(month, getdate( ))  11
  4635.     select datename(month, getdate( ))  November
  4636.  
  4637.  Example A assumes a current date of November 25, 1995, 10:32 am.
  4638.  
  4639.    B. select interval = dateadd(day, 21, pubdate)
  4640.     from titles
  4641.  
  4642.  Example B displays the new publication dates when the publication dates of
  4643.  all the books in the titles table slip 21 days.
  4644.  
  4645.    C. select newdate = datediff(day, pubdate, getdate())
  4646.     from titles
  4647.  
  4648.  Example C finds the number of days elapsed between pubdate and the current
  4649.  date (found with the GETDATE function).
  4650.  
  4651.  
  4652.  Options
  4653.  
  4654.  date
  4655.    A parameter used with DATEADD, DATEDIFF, DATENAME, and DATEPART. The date
  4656.    option can be either the function GETDATE, a character string in one of
  4657.    the acceptable date formats (see "Datatypes"), or the name of a datetime
  4658.    column.
  4659.  
  4660.  datepart
  4661.    A parameter used with DATEADD, DATEDIFF, DATENAME, and DATEPART. The
  4662.    following table lists the dateparts, the abbreviations recognized by SQL
  4663.    Server, and the acceptable values:
  4664.  
  4665. ╓┌────────────┌─────────────┌────────────────────────────────────────────────╖
  4666.  Date Part    Abbreviation  Values
  4667.  ────────────────────────────────────────────────────────────────────────────
  4668.  year         yy            1753-9999
  4669.  quarter      qq            1-4
  4670.  month        mm            1-12
  4671.  day of year  dy            1-366
  4672.  day          dd            1-31
  4673.  week         wk            1-53
  4674.  weekday      dw            1-7 (Sun.-Sat.)
  4675.  hour         hh            0-23
  4676.  minute       mi            0-59
  4677.  second       ss            0-59
  4678.  millisecond  ms            0-999
  4679.  ────────────────────────────────────────────────────────────────────────────
  4680.  
  4681.  
  4682.    If the year is given with two digits, <50 is the next century and >=50 is
  4683.    this century. So "25" is "2025", while "50" is "1950".
  4684.  
  4685.    When you use week with DATEDIFF, you get the number of Sundays between the
  4686.    two dates, including the first date but not the second. For example, the
  4687.    difference in weeks between Sunday, January 4, and Sunday, January 11, is
  4688.    one.
  4689.  
  4690.    Milliseconds can be preceded either with a colon or a period. If preceded
  4691.    by a colon, the number means thousandths of a second. If preceded by a
  4692.    period, a single digit means tenths of a second, two digits mean
  4693.    hundredths of a second, and three digits mean thousandths of a second. For
  4694.    example, "12:30:20:1" means twenty and one-thousandth second past 12:30;
  4695.    "12:30:20.1" means twenty and one-tenth second past 12:30.
  4696.  
  4697.  
  4698.  Comments
  4699.  
  4700.  Date functions can be used in the select list or WHERE clause of a query.
  4701.  
  4702.  Use the datetime datatype only for dates after January 1, 1753.
  4703.  
  4704.  When you enter datetime values, always enclose them in single or double
  4705.  quotation marks.
  4706.  
  4707.  Values of type datetime are stored in a special internal format. They are
  4708.  displayed in a default format and can be entered in a variety of formats.
  4709.  (See "Datatypes" for more information.)
  4710.  
  4711.  SQL Server automatically converts between character and datetime values when
  4712.  necessary (for example, when you compare a character value to a datetime
  4713.  value).
  4714.  
  4715.  Use the style parameter of the CONVERT function to obtain a wide variety of
  4716.  date display formats. (See "Conversion Function" for more information.)
  4717.  
  4718.  The DATEDIFF function does not produce reliable results if your result in
  4719.  milliseconds is greater than 2 billion. For example, if you try to find the
  4720.  difference in milliseconds between two dates more than a year apart, your
  4721.  result will not be accurate.
  4722.  
  4723.  
  4724.  See Also
  4725.  
  4726.  Conversion Function, Expressions, Functions, SELECT, WHERE Clause
  4727.  
  4728.  
  4729.  DBCC
  4730.  ────────────────────────────────────────────────────────────────────────────
  4731.  
  4732.  
  4733.  Function
  4734.  
  4735.  Checks the logical and physical consistency of a database, ordinarily either
  4736.  because damage is suspected or as part of a periodic check.
  4737.  
  4738.  
  4739.  Syntax
  4740.  
  4741.    DBCC
  4742.      {CHECKTABLE (table_name) | CHECKDB [(database_name)] |
  4743.      CHECKALLOC [(database_name)] | CHECKCATALOG [(database_name)] |
  4744.      DBREPAIR (database_name, DROPDB) }
  4745.  
  4746.  
  4747.  Examples
  4748.  
  4749.    A. dbcc checktable(titleauthor)
  4750.  
  4751.    B. dbcc checkalloc(pubs)
  4752.  
  4753.    C. dbcc dbrepair(pubs, dropdb)
  4754.  
  4755.  
  4756.  Options
  4757.  
  4758.  CHECKTABLE
  4759.    Checks the specified table to see that index and data pages are correctly
  4760.    linked, that indexes are in properly sorted order, that all pointers are
  4761.    consistent, that the data information on each page is reasonable, and that
  4762.    page offsets are reasonable.
  4763.  
  4764.  CHECKDB
  4765.    Runs the same checks as CHECKTABLE, but on each table in the specified
  4766.    database. If no database name is given, CHECKDB checks the current
  4767.    database.
  4768.  
  4769.  CHECKALLOC
  4770.    Checks the specified database to see that all pages are correctly
  4771.    allocated and that no page is allocated that isn't used. If no database
  4772.    name is given, CHECKALLOC checks the current database. CHECKALLOC reports
  4773.    on the amount of space allocated and used. For example, here's the report
  4774.    on pubs:
  4775.  
  4776.    Checking pubs Alloc page 0 (# of extent=32 used pages=65 ref
  4777.    pages=65)
  4778.      Alloc page 256 (# of extent=32 used pages=140 ref pages=140)
  4779.      Alloc page 512 (# of extent=32 used pages=186 ref pages=186)
  4780.      Alloc page 768 (# of extent=10 used pages=40 ref pages=40)
  4781.      Total (# of extent=106 used pages=431 ref pages=431) in this database
  4782.      DBCC execution completed. If DBCC printed error messages,
  4783.      see your system administrator.
  4784.  
  4785.    This database contains four allocation units, 512K each, or 2 megabytes in
  4786.    all (the default size of a database).
  4787.  
  4788.    There are 32 extents (required for the creation of new database objects)
  4789.    per allocation page; the number of extents tells whether or not you can
  4790.    create new objects in the database. There are 8 pages per extent; the
  4791.    number of used pages tells whether you can add data to existing objects.
  4792.  
  4793.    To get an approximation of the space used, multiply the total number of
  4794.    used pages by 2K.
  4795.  
  4796.  CHECKCATALOG
  4797.    Checks for consistency in and between system tables. For example, it makes
  4798.    sure that every type in syscolumns has a matching entry in systypes, that
  4799.    every table and view in sysobjects has at least one column in syscolumns,
  4800.    and that the last checkpoint in syslogs is valid. CHECKCATALOG also
  4801.    reports on any segments that have been defined. If no database name is
  4802.    given, CHECKCATALOG checks the current database.
  4803.  
  4804.  DBREPAIR (database_name, DROPDB)
  4805.    Drops a damaged database. The DROP DATABASE statement does not work on a
  4806.    damaged database.  No user, including the person executing the statement,
  4807.    can be using the database that is being dropped when this DBCC statement
  4808.    is executed.
  4809.  
  4810.  
  4811.  Comments
  4812.  
  4813.  DBCC (Database Consistency Checker) can be run while the database is active
  4814.  except for the DBREPAIR(database_name, DROPDB) option.
  4815.  
  4816.  A warning message may be issued by DBCC, either during CHECKDB or
  4817.  CHECKTABLE, stating that the value in the dpages column of the sysindexes
  4818.  row is not the same as DBCC's count of data pages for some table. To correct
  4819.  this inconsistency, follow this procedure:
  4820.  
  4821.  
  4822.    1.  Use the sp_configure stored procedure to enable updates to system
  4823.        catalogs.
  4824.  
  4825.    2.  Execute the RECONFIGURE WITH OVERRIDE statement for that change in
  4826.        configuration to take effect.
  4827.  
  4828.    3.  When using the database where the DBCC check was done, execute the
  4829.        following statement:
  4830.  
  4831.        update sysindexes set dpages=<count from DBCC>
  4832.               where id=<id of table> and indid=<0 or 1>
  4833.  
  4834.  
  4835.    4.  Execute the CHECKPOINT statement in the database where the DBCC check
  4836.        was done.
  4837.  
  4838.    5.  Disable updates to system catalogs with sp_configure.
  4839.  
  4840.    6.  Execute the RECONFIGURE statement.
  4841.  
  4842.  
  4843.  Because of the special status of sysindexes as a system table, it is
  4844.  necessary to qualify the update and restrict it to a single row as described
  4845.  above. The value for the qualification's comparison to indid will be 1 if
  4846.  there is a clustered index on that table and 0 if there is none.
  4847.  
  4848.  
  4849.  Permissions
  4850.  
  4851.  DBCC permission defaults to the table owner for CHECKTABLE and to the
  4852.  Database Owner for CHECKDB, CHECKALLOC, CHECKCATALOG, and DBREPAIR. It is
  4853.  not transferable.
  4854.  
  4855.  
  4856.  See Also
  4857.  
  4858.  DROP DATABASE, sp_helpdb
  4859.  
  4860.  
  4861.  DECLARE
  4862.  ────────────────────────────────────────────────────────────────────────────
  4863.  
  4864.  
  4865.  Function
  4866.  
  4867.  Declares the name and type of local variables for a batch or procedure.
  4868.  Local variables are assigned values with a SELECT statement.
  4869.  
  4870.  
  4871.  Syntax
  4872.  
  4873.  Variable declaration:
  4874.  
  4875.    DECLARE @variable_name datatype
  4876.      [, @variable_name datatype...]
  4877.  
  4878.  Variable assignment:
  4879.  
  4880.    SELECT @variable = expression [, @variable = expression...]
  4881.      [FROM table_list]
  4882.      [WHERE search_conditions]
  4883.      [GROUP BY group_by_list] [HAVING search_conditions]
  4884.      [ORDER BY order_by_list]
  4885.      [COMPUTE function_list [BY by_list]]
  4886.  
  4887.  
  4888.  Examples
  4889.  
  4890.    A. declare @one varchar(18), @two varchar(18)
  4891.     select @one = "this is one", @two = "this is two"
  4892.     if @one = "this is one"
  4893.       print "you got one"
  4894.     if @two = "this is two"
  4895.       print "you got two"
  4896.     else print "nope"
  4897.  
  4898.    B. declare @veryhigh money
  4899.     select @veryhigh = max(price)
  4900.       from titles
  4901.     if @veryhigh > $20
  4902.       print "Ouch!"
  4903.  
  4904.  
  4905.  Options
  4906.  
  4907.  variable_name
  4908.    The name of the variable. It must conform to the rules for identifiers.
  4909.    Variables can be used only in place of constants. They cannot be used in
  4910.    place of the names of tables, columns, other database objects, or
  4911.    keywords.
  4912.  
  4913.  datatype
  4914.    A system datatype or a user datatype.
  4915.  
  4916.  
  4917.  Comments
  4918.  
  4919.  The "at" sign (@) denotes a variable name.
  4920.  
  4921.  Local variables must be used within the batch or procedure in which they are
  4922.  declared.
  4923.  
  4924.  Local variables are often used in a batch or procedure as counters for WHILE
  4925.  loops or IF...ELSE blocks.
  4926.  
  4927.  When they are used in stored procedures, local variables are declared for
  4928.  automatic, noninteractive use by the procedure when it executes.
  4929.  
  4930.  The SELECT statement that assigns a value to the local variable usually
  4931.  returns a single value. If the SELECT variable assignment statement returns
  4932.  more than one value, the variable is assigned the last value returned.
  4933.  
  4934.  The PRINT and RAISERROR statements can take local variables as parameters.
  4935.  
  4936.  The SELECT statement that assigns values to variables cannot be used to
  4937.  retrieve data in the same statement.
  4938.  
  4939.  The maximum number for each parameter and global variable in a procedure is
  4940.  255. See "Variables (Local and Global)." There is no limit on the number of
  4941.  local variables.
  4942.  
  4943.  Users cannot declare global variables.
  4944.  
  4945.  
  4946.  See Also
  4947.  
  4948.  Parameters, PRINT, RAISERROR, Variables (Local and Global), WHILE
  4949.  
  4950.  
  4951.  DELETE
  4952.  ────────────────────────────────────────────────────────────────────────────
  4953.  
  4954.  
  4955.  Function
  4956.  
  4957.  Removes rows from a table.
  4958.  
  4959.  
  4960.  Syntax
  4961.  
  4962.    A. DELETE [[database.]owner.]{table_name | view_name}
  4963.     [FROM [[database.]owner.]{table_name | view_name}
  4964.     [, [[database.]owner.]{table_name | view_name}]...]
  4965.     [WHERE search_conditions]
  4966.  
  4967.    B. DELETE [FROM] [[database.]owner.]{table_name | view_name}
  4968.     [WHERE search_conditions]
  4969.  
  4970.  
  4971.  Examples
  4972.  
  4973.    A. delete authors
  4974.  
  4975.  Example A deletes all rows from the authors table.
  4976.  
  4977.    B. delete from authors
  4978.     where au_lname = "McBadden"
  4979.  
  4980.  Example B deletes one row from the authors table.
  4981.  
  4982.    C. delete titles
  4983.     from titles, authors, titleauthor
  4984.     where authors.au_lname = 'Bennet'
  4985.      and authors.au_id = titleauthor.au_id
  4986.      and titleauthor.title_id = titles.title_id
  4987.  
  4988.  Example C deletes rows for books written by Bennet from the titles table.
  4989.  (The pubs database includes a trigger (deltitle) that prevents the deletion
  4990.  of titles that are recorded in the sales table; you must drop this trigger
  4991.  to make the last example work.)
  4992.  
  4993.  
  4994.  Options
  4995.  
  4996.  FROM (see syntax A)
  4997.    The additional FROM optionally lets you name more than one table or view
  4998.    to use with a WHERE clause to specify which rows to delete. The additional
  4999.    FROM clause allows you to delete rows from one table based on data stored
  5000.    in other tables, giving you much of the power of an embedded SELECT
  5001.    statement.
  5002.  
  5003.  FROM (see syntax B)
  5004.    An optional keyword for compatibility with other versions of SQL. Follow
  5005.    it with the name of the table or view from which you want to remove rows.
  5006.  
  5007.  WHERE
  5008.    A standard WHERE clause. (See "WHERE Clause" for more information.)
  5009.  
  5010.  
  5011.  Comments
  5012.  
  5013.  If you don't use a WHERE clause, all rows in the table named after DELETE
  5014.  [FROM] are removed. The table, though empty of data, continues to exist
  5015.  until you use a DROP TABLE statement.
  5016.  
  5017.  TRUNCATE TABLE and DELETE without specifying any rows are functionally
  5018.  equivalent, but TRUNCATE TABLE is faster. DELETE removes rows one at a time
  5019.  and logs these transactions; TRUNCATE TABLE removes whole data pages and is
  5020.  not logged.
  5021.  
  5022.  Both DELETE and TRUNCATE TABLE reclaim the space occupied by the data and
  5023.  its associated indexes.
  5024.  
  5025.  The second FROM clause allows you to delete rows from one table based on
  5026.  data stored in other tables.
  5027.  
  5028.  You can define a trigger that takes a specified action when a DELETE
  5029.  statement is executed on a specified table.
  5030.  
  5031.  
  5032.  Permissions
  5033.  
  5034.  DELETE permission defaults to the table or view owner, who can transfer it
  5035.  to other users.
  5036.  
  5037.  
  5038.  See Also
  5039.  
  5040.  CREATE TRIGGER, DROP TABLE, TRUNCATE TABLE
  5041.  
  5042.  
  5043.  DISK INIT
  5044.  ────────────────────────────────────────────────────────────────────────────
  5045.  
  5046.  
  5047.  Function
  5048.  
  5049.  Reserves and formats physical storage for database devices. Database devices
  5050.  contain databases and transaction logs. DISK INIT also provides a physical
  5051.  filename and a logical filename for the database file. (You need not use
  5052.  DISK INIT on the master database device; it is initialized when SQL Server
  5053.  is installed.)
  5054.  
  5055.  
  5056.  Syntax
  5057.  
  5058.    DISK INIT
  5059.      NAME = "logical_name" ,
  5060.      PHYSNAME = "physical_name" ,
  5061.      VDEVNO = virtual_device_number ,
  5062.      SIZE = number_of_blocks
  5063.      [, VSTART = virtual_address ,
  5064.      CNTRLTYPE = controller_number]
  5065.  
  5066.  
  5067.  Examples
  5068.  
  5069.    disk init
  5070.      name = "user_file",
  5071.      physname = "f:\sqlservr\data\userdisk.dat",
  5072.      vdevno = 2, size = 5120
  5073.  
  5074.  
  5075.  Options
  5076.  
  5077.  NAME
  5078.    The logical name of the database device. It must correspond to the rules
  5079.    for identifiers and must be enclosed in single or double quotation marks.
  5080.    This name is used in the CREATE DATABASE and ALTER DATABASE statements.
  5081.    Although a name longer than 30 characters is allowed, the name will be
  5082.    truncated to 30 characters.
  5083.  
  5084.  PHYSNAME
  5085.    The full pathname of the database device. It must follow the rules for the
  5086.    operating system pathnames. It must be enclosed in single or double
  5087.    quotation marks.
  5088.  
  5089.  VDEVNO
  5090.    The virtual device number. It must be unique among database devices
  5091.    associated with SQL Server. The device number 0 is reserved for the master
  5092.    database device. Legal numbers are between 1 and 9. To determine the
  5093.    virtual device number, look at the device_number column of sysdevices with
  5094.    sp_helpdevice, and use the next integer.
  5095.  
  5096.  SIZE
  5097.    The size of the database device in 2K blocks.
  5098.  
  5099.    If you are planning to use the new database device for the creation of a
  5100.    new database, the minimum SIZE is the size of the model database, 1024 2K
  5101.    blocks (2 megabytes). If you are initializing a database device that will
  5102.    contain a transaction log, SIZE can be as small as 512 2K blocks (1
  5103.    megabyte).
  5104.  
  5105.  VSTART
  5106.    The starting virtual address or the starting offset in 2K blocks. The
  5107.    value for VSTART should be 0 (the default). Reset VSTART only if
  5108.    instructed to do so.
  5109.  
  5110.  CNTRLTYPE
  5111.    The disk controller. Its default value is zero. Reset CNTRLTYPE only if
  5112.    instructed to do so.
  5113.  
  5114.  
  5115.  Comments
  5116.  
  5117.  Execute DISK INIT once for each new database device. Each time DISK INIT is
  5118.  executed, a row is added to master..sysdevices.
  5119.  
  5120.  It is important to back up the master database with the DUMP DATABASE or
  5121.  DUMP TRANSACTION statement after each use of DISK INIT. This makes recovery
  5122.  easier and safer in case master is damaged. (If you add a database device
  5123.  with DISK INIT and fail to back up master, you may be able to recover the
  5124.  changes by using DISK REINIT and then stopping and restarting SQL Server.)
  5125.  
  5126.  User databases are assigned to database devices with the optional "ON
  5127.  database_file" clause of the CREATE DATABASE or ALTER DATABASE statement.
  5128.  
  5129.  A new database device does not automatically become part of the pool of
  5130.  default database storage. Assign default status to a database device with
  5131.  the sp_diskdefault system procedure.
  5132.  
  5133.  To put a database's transaction log (that is, the system table syslogs) on a
  5134.  different database device than the one on which the rest of the database is
  5135.  stored, name at least two database devices when you create the database, and
  5136.  then execute the sp_logdevice system procedure. You can also alter the
  5137.  database to a second database device and then run sp_logdevice.
  5138.  
  5139.  For a report on all database devices and dump devices on your system,
  5140.  execute the sp_helpdevice system procedure.
  5141.  
  5142.  Remove a database device with the sp_dropdevice system procedure. You must
  5143.  first drop all existing databases on that database device.
  5144.  
  5145.  After dropping a database device, you can create a new one with the same
  5146.  logical name (using DISK INIT) as long as you give it a different physical
  5147.  name and virtual device number. If you wish to use the same physical name
  5148.  and virtual device number, you must restart SQL Server.
  5149.  
  5150.  
  5151.  Permissions
  5152.  
  5153.  DISK INIT permission defaults to the System Administrator and is not
  5154.  transferable. The System Administrator must be using the master database to
  5155.  use DISK INIT.
  5156.  
  5157.  
  5158.  See Also
  5159.  
  5160.  CREATE DATABASE, DISK REFIT, DISK REINIT, DUMP DATABASE, DUMP TRANSACTION,
  5161.  LOAD DATABASE, LOAD TRANSACTION, sp_diskdefault, sp_helpdevice, sp_logdevice
  5162.  
  5163.  
  5164.  
  5165.  DISK REFIT
  5166.  ────────────────────────────────────────────────────────────────────────────
  5167.  
  5168.  
  5169.  Function
  5170.  
  5171.  Restores a damaged master database if databases have been created or
  5172.  database space allocated since the last database or transaction log dump.
  5173.  This statement is used after DISK REINIT.
  5174.  
  5175.  
  5176.  Syntax
  5177.  
  5178.    DISK REFIT
  5179.  
  5180.  
  5181.  Examples
  5182.  
  5183.    disk refit
  5184.  
  5185.  
  5186.  Comments
  5187.  
  5188.  DISK REFIT is used after DISK REINIT. It uses information in sysdevices to
  5189.  rebuild sysusages and sysdatabases.
  5190.  
  5191.  Stop and then restart SQL Server after using DISK REFIT.
  5192.  
  5193.  For more information, see the SQL Server System Administrator's Guide.
  5194.  
  5195.  
  5196.  Permissions
  5197.  
  5198.  DISK REFIT permission defaults to the System Administrator and is not
  5199.  transferable. The System Administrator must be in the master database to use
  5200.  DISK REFIT.
  5201.  
  5202.  
  5203.  See Also
  5204.  
  5205.  DISK INIT, DISK REINIT, sp_addumpdevice, sp_helpdevice
  5206.  
  5207.  
  5208.  DISK REINIT
  5209.  ────────────────────────────────────────────────────────────────────────────
  5210.  
  5211.  
  5212.  Function
  5213.  
  5214.  Part of the procedure that restores a damaged master database if database
  5215.  devices have been added since the last database or transaction log dump.
  5216.  
  5217.  
  5218.  Syntax
  5219.  
  5220.    DISK REINIT
  5221.      NAME = "logical_name",
  5222.      PHYSNAME = "physical_name" ,
  5223.      VDEVNO = virtual_device_number ,
  5224.      SIZE = number_of_blocks
  5225.      [, VSTART = virtual_address ,
  5226.      CNTRLTYPE = controller_number]
  5227.  
  5228.  
  5229.  Examples
  5230.  
  5231.    disk reinit
  5232.      name = "user_disk",
  5233.      physname = "f:\sqlservr\data\userdisk.dat",
  5234.      vdevno = 2, size = 5120
  5235.  
  5236.  
  5237.  Options
  5238.  
  5239.  NAME
  5240.    The logical name of the database device. It must correspond to the rules
  5241.    for identifiers and must be enclosed in single or double quotation marks.
  5242.    This name is used in the CREATE DATABASE and ALTER DATABASE statements.
  5243.    Although a name longer than 30 characters is allowed, the name will be
  5244.    truncated to 30 characters.
  5245.  
  5246.  PHYSNAME
  5247.    The full pathname of the database device. It must follow the rules for the
  5248.    operating system pathnames. It must be enclosed in single or double
  5249.    quotation marks.
  5250.  
  5251.  VDEVNO
  5252.    The virtual device number. It must be unique among database devices used
  5253.    by SQL Server. The device number 0 is reserved for the master database
  5254.    device. Legal numbers are between 1 and 49.
  5255.  
  5256.  SIZE
  5257.    The size of the database device in 2K blocks. The minimum usable size is
  5258.    1024 2K blocks (2 megabytes).
  5259.  
  5260.  VSTART
  5261.    The starting virtual address or the starting offset in 2K blocks. The
  5262.    value for VSTART should be 0 (the default). Reset it only if instructed to
  5263.    do so.
  5264.  
  5265.  CNTRLTYPE
  5266.    The disk controller. Its default value is 0. Reset it only if instructed
  5267.    to do so.
  5268.  
  5269.  
  5270.  Comments
  5271.  
  5272.  DISK REINIT insures that master..sysdevices is correct if a master database
  5273.  has been damaged and database devices have been added since the last dump of
  5274.  master.
  5275.  
  5276.  DISK REINIT is very similar to DISK INIT but does not initialize the disk
  5277.  space.
  5278.  
  5279.  See the SQL Server System Administrator's Guide for more complete
  5280.  information on using DISK REINIT.
  5281.  
  5282.  
  5283.  Permissions
  5284.  
  5285.  DISK REINIT permission defaults to the System Administrator and is not
  5286.  transferable. The System Administrator must be in the master database to use
  5287.  DISK REINIT.
  5288.  
  5289.  
  5290.  See Also
  5291.  
  5292.  DISK INIT, DISK REFIT, sp_addumpdevice, sp_helpdevice
  5293.  
  5294.  
  5295.  DROP DATABASE
  5296.  ────────────────────────────────────────────────────────────────────────────
  5297.  
  5298.  
  5299.  Function
  5300.  
  5301.  Removes one or more databases from SQL Server.
  5302.  
  5303.  
  5304.  Syntax
  5305.  
  5306.    DROP DATABASE database_name [, database_name...]
  5307.  
  5308.  
  5309.  Examples
  5310.  
  5311.    A. drop database publishing
  5312.  
  5313.    B. drop database publishing, newpubs
  5314.  
  5315.  Examples A and B remove databases and their contents.
  5316.  
  5317.  
  5318.  Comments
  5319.  
  5320.  Removing a database deletes the database and all its objects, frees its
  5321.  storage allocation, and erases references to it in the master database.
  5322.  
  5323.  You cannot drop a database that is in use (open for reading or writing by
  5324.  any user).
  5325.  
  5326.  A damaged database cannot be removed with DROP DATABASE. Use the DBCC
  5327.  DBREPAIR statement:
  5328.  
  5329.    dbcc dbrepair (database_name, dropdb)
  5330.  
  5331.  
  5332.  Permissions
  5333.  
  5334.  DROP DATABASE permission defaults to the Database Owner and cannot be
  5335.  transferred. The master database must be current at the time. (A database is
  5336.  made current with the USE statement.)
  5337.  
  5338.  
  5339.  See Also
  5340.  
  5341.  ALTER DATABASE, CREATE DATABASE, DBCC, USE, sp_changedbowner, sp_helpdb,
  5342.  sp_renamedb, sp_spaceused
  5343.  
  5344.  
  5345.  DROP DEFAULT
  5346.  ────────────────────────────────────────────────────────────────────────────
  5347.  
  5348.  
  5349.  Function
  5350.  
  5351.  Removes a user-specified default.
  5352.  
  5353.  
  5354.  Syntax
  5355.  
  5356.    DROP DEFAULT [owner.]default_name
  5357.      [, [owner.]default_name...]
  5358.  
  5359.  
  5360.  Examples
  5361.  
  5362.    drop default datedefault
  5363.  
  5364.  This example removes user-created default datedefault.
  5365.  
  5366.  
  5367.  Options
  5368.  
  5369.  default_name
  5370.    The name of an existing default. You can find out what defaults exist by
  5371.    executing the sp_help system procedure.
  5372.  
  5373.  
  5374.  Comments
  5375.  
  5376.  A default cannot be dropped if it is currently bound to a column or user
  5377.  datatype. Use the sp_unbindefault system procedure to unbind the default
  5378.  before you drop it. See Chapter 2, "System Procedures," for details.
  5379.  
  5380.  You can bind a new default to a column or user datatype without unbinding
  5381.  its current default. The new default will override the old one.
  5382.  
  5383.  When you drop a default for a NULL column, NULL will from then on be
  5384.  inserted in that position when rows are added but no value for that column
  5385.  is entered. When you drop a default for a NOT NULL column, you'll get an
  5386.  error message when rows are added but no value for that column is explicitly
  5387.  entered.
  5388.  
  5389.  
  5390.  Permissions
  5391.  
  5392.  DROP DEFAULT permission defaults to the owner of the default and is not
  5393.  transferable.
  5394.  
  5395.  
  5396.  See Also
  5397.  
  5398.  CREATE DEFAULT, CREATE RULE, DROP RULE, sp_bindefault, sp_help, sp_helptext,
  5399.  sp_rename, sp_unbindefault
  5400.  
  5401.  
  5402.  DROP INDEX
  5403.  ────────────────────────────────────────────────────────────────────────────
  5404.  
  5405.  
  5406.  Function
  5407.  
  5408.  Removes an index from the database.
  5409.  
  5410.  
  5411.  Syntax
  5412.  
  5413.    DROP INDEX table_name.index_name [, table_name.index_name...]
  5414.  
  5415.  
  5416.  Examples
  5417.  
  5418.    drop index authors.au_id_ind
  5419.  
  5420.  This example removes the index au_id_ind in the authors table.
  5421.  
  5422.  
  5423.  Options
  5424.  
  5425.  table_name
  5426.    The table in which the indexed column is located.
  5427.  
  5428.  index_name
  5429.    The index to be dropped. In TRANSACT-SQL, index names need not be unique
  5430.    in a database, though they must be unique within a table.
  5431.  
  5432.  
  5433.  Comments
  5434.  
  5435.  Once the statement is executed, you regain all of the space occupied by the
  5436.  index. This space can be used for any database objects.
  5437.  
  5438.  You can't use DROP INDEX on any of the system tables that exist in the
  5439.  master database and in user databases.
  5440.  
  5441.  To get information about what indexes exist on a table or view, use the
  5442.  sp_helpindex system procedure with the table name as parameter.
  5443.  
  5444.  
  5445.  Permissions
  5446.  
  5447.  DROP INDEX permission defaults to the index owner and is not transferable.
  5448.  
  5449.  
  5450.  See Also
  5451.  
  5452.  CREATE INDEX, CREATE TABLE, sp_helpindex, sp_spaceused
  5453.  
  5454.  
  5455.  DROP PROCEDURE
  5456.  ────────────────────────────────────────────────────────────────────────────
  5457.  
  5458.  
  5459.  Function
  5460.  
  5461.  Removes user-created stored procedures from the current database.
  5462.  
  5463.  
  5464.  Syntax
  5465.  
  5466.    DROP PROCedure procedure_name [procedure_name...]
  5467.  
  5468.  
  5469.  Examples
  5470.  
  5471.    use pubs
  5472.     <execute>
  5473.     drop procedure byroyalty
  5474.  
  5475.  This example removes the stored procedure byroyalty.
  5476.  
  5477.  
  5478.  Comments
  5479.  
  5480.  You can drop stored procedures only from the current database. The existence
  5481.  of a stored procedure is checked on each call to that procedure.
  5482.  
  5483.  A procedure group (more than one procedure with the same name but with
  5484.  different ;number suffixes) can be dropped with a single DROP PROCEDURE
  5485.  statement. For example, if the procedures used with the application orders
  5486.  were named orderproc;1, orderproc;2, and so on, the following statement
  5487.  would drop the entire group:
  5488.  
  5489.    drop proc orderproc
  5490.  
  5491.  Once procedures have been grouped, individual procedures within the group
  5492.  cannot be dropped. For example, the following statement is not allowed:
  5493.  
  5494.    drop procedure orderproc;2
  5495.  
  5496.  The sp_helptext system procedure displays the procedure definition (which is
  5497.  stored in syscomments). See Chapter 2, "System Procedures," for details.
  5498.  
  5499.  
  5500.  Permissions
  5501.  
  5502.  DROP PROCEDURE permission defaults to the procedure owner and is not
  5503.  transferable.
  5504.  
  5505.  
  5506.  See Also
  5507.  
  5508.  CREATE PROCEDURE, EXECUTE, sp_depends, sp_helptext, sp_rename
  5509.  
  5510.  
  5511.  DROP RULE
  5512.  ────────────────────────────────────────────────────────────────────────────
  5513.  
  5514.  
  5515.  Function
  5516.  
  5517.  Removes a user-specified rule.
  5518.  
  5519.  
  5520.  Syntax
  5521.  
  5522.    DROP RULE [owner.]rule_name
  5523.      [, [owner.]rule_name...]
  5524.  
  5525.  
  5526.  Examples
  5527.  
  5528.    drop rule pubid_rule
  5529.  
  5530.  This example removes the pubid_rule rule.
  5531.  
  5532.  
  5533.  Comments
  5534.  
  5535.  Use the sp_unbindrule system procedure to unbind the rule before dropping
  5536.  it. If it is still in effect, an error message will be displayed and the
  5537.  DROP RULE statement will be aborted. See Chapter 2, "System Procedures," for
  5538.  details.
  5539.  
  5540.  You can bind a new rule to a column or user datatype without unbinding its
  5541.  current rule. The new rule overrides the old one.
  5542.  
  5543.  After you drop a rule, new data entered into the columns that previously
  5544.  were governed by it goes in without these constraints. Already existing data
  5545.  is not affected in any way.
  5546.  
  5547.  
  5548.  Permissions
  5549.  
  5550.  DROP RULE permission defaults to the rule owner and is not transferable.
  5551.  
  5552.  
  5553.  See Also
  5554.  
  5555.  CREATE DEFAULT, CREATE RULE, DROP DEFAULT, sp_bindrule, sp_help,
  5556.  sp_helptext, sp_rename, sp_unbindrule
  5557.  
  5558.  
  5559.  DROP TABLE
  5560.  ────────────────────────────────────────────────────────────────────────────
  5561.  
  5562.  
  5563.  Function
  5564.  
  5565.  Removes a table definition and all data, indexes, triggers, and permission
  5566.  specifications for a table from the database.
  5567.  
  5568.  
  5569.  Syntax
  5570.  
  5571.    DROP TABLE [[database.]owner.]table_name
  5572.      [, [[database.]owner.]table_name...]
  5573.  
  5574.  
  5575.  Examples
  5576.  
  5577.    drop table roysched
  5578.  
  5579.  This example removes the roysched table and its data and indexes.
  5580.  
  5581.  
  5582.  Comments
  5583.  
  5584.  When you drop a table, any rules or defaults on it lose their binding, and
  5585.  any triggers associated with it are automatically dropped. If you re-create
  5586.  a table, you must rebind the appropriate rules and defaults, and re-create
  5587.  any triggers.
  5588.  
  5589.  The system tables affected when a table is dropped are sysobjects,
  5590.  syscolumns, sysindexes, sysprotects, and syscomments.
  5591.  
  5592.  You can't use the DROP TABLE statement on any of the system tables, either
  5593.  in the master database or in a user database.
  5594.  
  5595.  You can drop a table in any database if you are the table owner. The
  5596.  following examples drop newtable in the otherdb database:
  5597.  
  5598.    drop table otherdb..newtable
  5599.  
  5600.     drop table otherdb.yourname.newtable
  5601.  
  5602.  If you delete all the rows in a table or give the TRUNCATE TABLE statement,
  5603.  the table still exists until you drop it.
  5604.  
  5605.  
  5606.  Permissions
  5607.  
  5608.  DROP TABLE permission defaults to the table owner and is not transferable.
  5609.  
  5610.  
  5611.  See Also
  5612.  
  5613.  ALTER TABLE, CREATE TABLE, DELETE, TRUNCATE TABLE, sp_depends, sp_help,
  5614.  sp_rename, sp_spaceused
  5615.  
  5616.  
  5617.  DROP TRIGGER
  5618.  ────────────────────────────────────────────────────────────────────────────
  5619.  
  5620.  
  5621.  Function
  5622.  
  5623.  Removes a trigger.
  5624.  
  5625.  
  5626.  Syntax
  5627.  
  5628.    DROP TRIGGER [owner.]trigger_name [, [owner.]trigger_name...]
  5629.  
  5630.  
  5631.  Examples
  5632.  
  5633.    drop trigger trigger1
  5634.  
  5635.  
  5636.  Permissions
  5637.  
  5638.  DROP TRIGGER permission defaults to the trigger owner and is not
  5639.  transferable.
  5640.  
  5641.  
  5642.  See Also
  5643.  
  5644.  CREATE TRIGGER, sp_depends, sp_help, sp_helptext, sp_rename
  5645.  
  5646.  
  5647.  DROP VIEW
  5648.  ────────────────────────────────────────────────────────────────────────────
  5649.  
  5650.  
  5651.  Function
  5652.  
  5653.  Removes views from the database.
  5654.  
  5655.  
  5656.  Syntax
  5657.  
  5658.    DROP VIEW [owner.]view_name [, [owner.]view_name...]
  5659.  
  5660.  
  5661.  Examples
  5662.  
  5663.    drop view new_pricey
  5664.  
  5665.  This example removes the new_pricey view.
  5666.  
  5667.  
  5668.  Comments
  5669.  
  5670.  When you use DROP VIEW, the definition of the view and other information
  5671.  about it are deleted from the system tables sysobjects, syscolumns,
  5672.  syscomments, sysprocedures, and sysprotects. All permissions for the view
  5673.  are deleted, too.
  5674.  
  5675.  Existence of a view is checked each time the view is referenced by another
  5676.  view or by a stored procedure.
  5677.  
  5678.  
  5679.  Permissions
  5680.  
  5681.  DROP VIEW permission defaults to the view owner and is not transferable.
  5682.  
  5683.  
  5684.  See Also
  5685.  
  5686.  CREATE VIEW, Views, sp_depends, sp_help, sp_helptext, sp_rename
  5687.  
  5688.  
  5689.  DUMP DATABASE
  5690.  ────────────────────────────────────────────────────────────────────────────
  5691.  
  5692.  
  5693.  Function
  5694.  
  5695.  Makes a backup copy of the database and the transaction log in a form that
  5696.  can be read in with LOAD DATABASE.
  5697.  
  5698.  
  5699.  Syntax
  5700.  
  5701.    DUMP DATABASE database_name
  5702.      TO dump_device
  5703.  
  5704.  
  5705.  Examples
  5706.  
  5707.    dump database pubs
  5708.      to diskdump1
  5709.  
  5710.  This example dumps the pubs database to the diskdump1 diskette.
  5711.  
  5712.  
  5713.  Options
  5714.  
  5715.  database_name
  5716.    The name of the database from which you are copying data.
  5717.  
  5718.  TO dump_device
  5719.    The logical name of the dump device to which you are dumping the specified
  5720.    database. Logical dump device names are stored in master..sysdevices.name.
  5721.    A report on dump devices is displayed when you execute sp_helpdevice.
  5722.    (Logical dump device names are in the report column device_name.)
  5723.  
  5724.    You add dump devices with the sp_addumpdevice system procedure. See
  5725.    Chapter 2, "System Procedures," or the SQL Server System Administrator's
  5726.    Guide for details.
  5727.  
  5728.  
  5729.  Comments
  5730.  
  5731.  SQL Server database dump is a dynamic dump─it can take place while the
  5732.  database is active. It slows the system down minimally, so you may want to
  5733.  run it when the database is not being heavily updated.
  5734.  
  5735.  The dump device is on the machine on which SQL Server is running.
  5736.  
  5737.  When the DUMP DATABASE statement is executed for dumping to a diskette, the
  5738.  console program prompts the operator and waits for responses. (See Chapter
  5739.  3, "Utility Programs," for more information on console.) If console is not
  5740.  running or if the operator does not answer its questions, the dump cannot
  5741.  proceed.
  5742.  
  5743.  The dump captures the state of the database as it was at the moment the
  5744.  statement was executed. Any changes made after the dump begins are not
  5745.  reflected in the dumped database.
  5746.  
  5747.  To recover a database, load the most recent database dump and load all the
  5748.  transaction log dumps made since that database dump. The database into which
  5749.  you are loading the data is replaced by this reconstruction.
  5750.  
  5751.  DUMP DATABASE backs up the database and the log but does not remove the
  5752.  inactive portion of the log. DUMP TRANSACTION backs up the log and removes
  5753.  the inactive portion of it.
  5754.  
  5755.  The DUMP DATABASE statement must be used to back up both the database and
  5756.  its transaction log for small databases. Databases smaller than about 4
  5757.  megabytes can store their transaction logs on the same database device as
  5758.  the rest of the database. In this situation, DUMP DATABASE is used to create
  5759.  backups, and DUMP TRANSACTION WITH TRUNCATE_ONLY can be used to purge the
  5760.  log of completed transactions. Databases larger than 4 megabytes should
  5761.  store their transaction logs on a different database device, which improves
  5762.  performance and enables unrestricted use of both the DUMP DATABASE and DUMP
  5763.  TRANSACTION statements.
  5764.  
  5765.  You can run more than one dump (or load) at the same time, but only if the
  5766.  cntrltype values of the devices to which you are dumping or loading are
  5767.  different. For example, only one dump or load through the disk byte stream
  5768.  interface can be active at a time.
  5769.  
  5770.  For more information on SQL Server's backup and recovery facilities, see the
  5771.  SQL Server System Administrator's Guide.
  5772.  
  5773.  
  5774.  Permissions
  5775.  
  5776.  DUMP DATABASE permission defaults to the Database Owner, who can transfer it
  5777.  to other users.
  5778.  
  5779.  
  5780.  See Also
  5781.  
  5782.  DUMP TRANSACTION, LOAD DATABASE, LOAD TRANSACTION, sp_addumpdevice,
  5783.  sp_dropdevice, sp_helpdb, sp_helpdevice, sp_logdevice, sp_spaceused
  5784.  
  5785.  
  5786.  DUMP TRANSACTION
  5787.  ────────────────────────────────────────────────────────────────────────────
  5788.  
  5789.  
  5790.  Function
  5791.  
  5792.  Removes the inactive part of the transaction log and makes a backup copy of
  5793.  it in a form that can be read in with LOAD TRANSACTION. The DUMP TRANSACTION
  5794.  and LOAD TRANSACTION statements should be used only when the transaction log
  5795.  is stored on a different database device than the rest of the database.
  5796.  
  5797.  
  5798.  Syntax
  5799.  
  5800.    DUMP TRANsaction database_name
  5801.      [TO dump_device]
  5802.      [WITH TRUNCATE_ONLY]
  5803.      [WITH NO_LOG]
  5804.  
  5805.  
  5806.  Examples
  5807.  
  5808.    dump transaction pubs
  5809.     to diskdump1
  5810.  
  5811.  This example dumps the transaction log to the diskdump1 dump device.
  5812.  
  5813.  
  5814.  Options
  5815.  
  5816.  TO dump_device
  5817.    The logical name of the dump device to which you are dumping the specified
  5818.    transaction log. Logical dump device names are available in
  5819.    master..sysdevices.name. To add a dump device, use the sp_addumpdevice
  5820.    system procedure.
  5821.  
  5822.    The dump device name is optional if you use the WITH TRUNCATE_ONLY or WITH
  5823.    NO_LOG clause.
  5824.  
  5825.  WITH TRUNCATE_ONLY
  5826.    Removes the inactive part of the log without making a backup copy of it.
  5827.    This optional clause frees disk space used by the transaction log.
  5828.  
  5829.    WITH TRUNCATE_ONLY is used after you have backed up the entire database
  5830.    with DUMP DATABASE. The DUMP DATABASE statement backs up the log but does
  5831.    not remove the inactive portion of it. If you use WITH TRUNCATE_ONLY and
  5832.    do not have a backup created by DUMP DATABASE, the changes that had been
  5833.    recorded in the log are not recoverable.
  5834.  
  5835.    Since the WITH TRUNCATE_ONLY option does not perform a dump, you can use
  5836.    any dump device name.
  5837.  
  5838.    You cannot use both WITH TRUNCATE_ONLY and WITH NO_LOG in the same
  5839.    statement.
  5840.  
  5841.  WITH NO_LOG
  5842.    Is used only when you have run out of space in the database so that you
  5843.    cannot even run DUMP TRANSACTION WITH TRUNCATE_ONLY to retrieve some space
  5844.    from the log. Like WITH TRUNCATE_ONLY, WITH NO_LOG removes the inactive
  5845.    part of the log without making a backup copy of it. In addition, WITH
  5846.    NO_LOG saves space by not recording this procedure in the transaction log.
  5847.  
  5848.    After the transaction log has been dumped using WITH NO_LOG, the changes
  5849.    that had been recorded in the log are not recoverable. You should
  5850.    immediately run DUMP DATABASE and then enlarge the database with ALTER
  5851.    DATABASE.
  5852.  
  5853.    You cannot use both WITH TRUNCATE_ONLY and WITH NO_LOG in the same
  5854.    statement.
  5855.  
  5856.  
  5857.  Comments
  5858.  
  5859.  A transaction log dump can take place while the database is active.
  5860.  
  5861.  The DUMP TRANSACTION (and LOAD TRANSACTION) statement should be used only
  5862.  when the transaction log is stored on a different database file than the
  5863.  rest of the database. This is recommended for databases larger than about 4
  5864.  megabytes. The transaction log is transferred to a separate database device
  5865.  with the sp_logdevice system procedure.
  5866.  
  5867.  With smaller databases, the transaction log is usually stored on the same
  5868.  database device as the rest of the database. In this situation, you use DUMP
  5869.  DATABASE to back up both the database and the transaction log, after which
  5870.  you can use DUMP TRANSACTION WITH TRUNCATE_ONLY to remove committed
  5871.  transactions from the log.
  5872.  
  5873.  DUMP TRANSACTION uses much less storage and takes much less time than DUMP
  5874.  DATABASE, which backs up both the transaction log and the rest of the
  5875.  database.
  5876.  
  5877.  When the DUMP TRANSACTION statement is executed for dumping to a diskette,
  5878.  the console program prompts the operator and waits for a response. If
  5879.  console is not running or if the operator does not answer its questions, the
  5880.  dump cannot proceed.
  5881.  
  5882.  Dumps of the transaction log are typically coordinated with DUMP DATABASE as
  5883.  part of an overall backup procedure. Transaction log dumps are made more
  5884.  frequently than database dumps.
  5885.  
  5886.  The dump captures the state of the transaction log as it was at the moment
  5887.  the statement was executed. Any changes made after the dump begins are not
  5888.  reflected in the dumped log.
  5889.  
  5890.  To recover a database, load the most recent database dump and load all the
  5891.  transaction log dumps (in order) made since the database dump. The database
  5892.  is restored to its state at the time of the last transaction log dump.
  5893.  
  5894.  ────────────────────────────────────────────────────────────────────────────
  5895.  WARNING
  5896.  
  5897.  Do not attempt to modify the transaction log table syslogs with a DELETE,
  5898.  UPDATE, or INSERT statement. Doing so makes it impossible for SQL Server to
  5899.  recover correctly in case of a system failure. In addition, an attempt to
  5900.  delete all rows from syslogs causes SQL Server to get into an infinite loop
  5901.  that eventually fills up the entire database.
  5902.  ────────────────────────────────────────────────────────────────────────────
  5903.  
  5904.  You can run more than one dump (or load) at the same time, but only if the
  5905.  cntrltype values of the devices to which you are dumping or loading are
  5906.  different. For example, only one dump or load through the disk byte stream
  5907.  interface can be active at a time.
  5908.  
  5909.  You cannot execute the DUMP TRANSACTION statement while the select
  5910.  into/bulkcopy or the trunc. log on chkpt. database options are enabled. Use
  5911.  DUMP DATABASE instead, or disable the option with sp_dboption.
  5912.  
  5913.  
  5914.  Permissions
  5915.  
  5916.  DUMP TRANSACTION permission defaults to the Database Owner, who can transfer
  5917.  it to other users. For more information on SQL Server's backup and recovery
  5918.  facilities, see the SQL Server System Administrator's Guide.
  5919.  
  5920.  
  5921.  See Also
  5922.  
  5923.  DUMP DATABASE, LOAD DATABASE, LOAD TRANSACTION, sp_addumpdevice,
  5924.  sp_dboption, sp_dropdevice, sp_helpdevice, sp_logdevice
  5925.  
  5926.  
  5927.  
  5928.  
  5929.  
  5930.  EXECUTE
  5931.  ────────────────────────────────────────────────────────────────────────────
  5932.  
  5933.  
  5934.  Function
  5935.  
  5936.  Runs a system procedure or a user-defined stored procedure. This statement
  5937.  is only necessary if other statements are submitted in the same batch.
  5938.  
  5939.  
  5940.  Syntax
  5941.  
  5942.    [EXECute] [[database.]owner.]procedure_name[;number]
  5943.      [[@parameter_name =] value [,[@parameter_name =] value]...]
  5944.      [WITH RECOMPILE]
  5945.  
  5946.  
  5947.  Examples
  5948.  
  5949.  Supply parameters in either of two ways: by value or with "parameter_name =
  5950.  value". If you use the "parameter_name = value" form, you need not supply
  5951.  the parameters in the order defined in the CREATE PROCEDURE statement.
  5952.  
  5953.    A. execute showind titles
  5954.  
  5955.    B. exec showind @tabname = titles
  5956.  
  5957.  If this is the only statement in a batch or file, you can use the following
  5958.  form:
  5959.  
  5960.    C. showind titles
  5961.  
  5962.  The showind stored procedure with a parameter value titles has been
  5963.  executed.
  5964.  
  5965.  
  5966.  Options
  5967.  
  5968.  procedure_name
  5969.    The name of a procedure that has been defined with a CREATE PROCEDURE
  5970.    statement. You can execute a procedure that has been created in another
  5971.    database as long as you are its owner or have permission to execute it in
  5972.    that database.
  5973.  
  5974.  ;number
  5975.    An optional integer used to group procedures of the same name so that they
  5976.    can be dropped together with a single DROP PROCEDURE statement. Procedures
  5977.    used in the same application are often grouped this way. For example, the
  5978.    procedures used with the application orders might be named orderproc;1,
  5979.    orderproc;2, and so on. The statement DROP PROCEDURE orderproc would drop
  5980.    the entire group. Once procedures have been grouped, individual procedures
  5981.    within the group cannot be dropped. For example, the statement DROP
  5982.    PROCEDURE orderproc;2 is not allowed.
  5983.  
  5984.  parameter_name
  5985.    The name of a parameter to the procedure, as defined in the CREATE
  5986.    PROCEDURE statement. Parameter names must be preceded by the "at" symbol
  5987.    (value" form, parameter names and constants need not be supplied in the
  5988.    order defined in the CREATE PROCEDURE statement.
  5989.  
  5990.  value
  5991.    The value of the parameter or parameter to the procedure. If you do not
  5992.    use parameter names, parameter values must be supplied in the order
  5993.    defined in the CREATE PROCEDURE statement.
  5994.  
  5995.    If a default is defined in the CREATE PROCEDURE statement, a user can
  5996.    execute the procedure without giving a parameter. The default must be a
  5997.    constant. It can include the wildcard characters (%, _, [ ], and [^]) if
  5998.    the procedure uses the parameter name with the LIKE keyword. See example B
  5999.    in the "CREATE PROCEDURE" section.
  6000.  
  6001.    The default can be NULL. Usually, the procedure definition specifies what
  6002.    action should be taken if a parameter value is NULL.
  6003.  
  6004.  WITH RECOMPILE
  6005.    Forces compilation of a new plan only for this execution of the procedure.
  6006.    Use this option if the parameter you're supplying for this execution is
  6007.    atypical.
  6008.  
  6009.    The original plan is used on subsequent executions.
  6010.  
  6011.  
  6012.  Comments
  6013.  
  6014.  If the user does not supply expected parameters, the stored procedure error
  6015.  message lists the names of expected parameters in reverse order.
  6016.  
  6017.  You don't need to use the EXECUTE keyword if the statement is the first one
  6018.  in a batch. A batch is a segment of an input file terminated by the go
  6019.  command on a line by itself.
  6020.  
  6021.  Since stored procedures are compiled the first time they are executed,
  6022.  subsequent run time is much shorter than for the equivalent set of
  6023.  stand-alone statements.
  6024.  
  6025.  System procedures (for example, sp_help) do not work on temporary tables
  6026.  because a procedure cannot access a temporary object unless it created the
  6027.  object itself during the current session.
  6028.  
  6029.  See "System Procedures," later in this chapter, and Chapter 2, "System
  6030.  Procedures," for details.
  6031.  
  6032.  
  6033.  Permissions
  6034.  
  6035.  EXECUTE permission defaults to the owner of the stored procedure, who can
  6036.  transfer it to other users.
  6037.  
  6038.  
  6039.  See Also
  6040.  
  6041.  CREATE PROCEDURE, DROP PROCEDURE, Parameters, System Procedures, Variables
  6042.  (Local and Global), Wildcard Characters, sp_depends, sp_helptext
  6043.  
  6044.  
  6045.  Expressions
  6046.  ────────────────────────────────────────────────────────────────────────────
  6047.  
  6048.  
  6049.  Function
  6050.  
  6051.  Used as variables and constants in many SQL statements.
  6052.  
  6053.  
  6054.  Syntax
  6055.  
  6056.  An expression returns values. Expressions have the following syntax:
  6057.  
  6058.    {constant | column_name | function | (subquery)}
  6059.     [{arithmetic_operator | bitwise_operator | string_operator}
  6060.     {constant | column_name | function | (subquery)}...]
  6061.  
  6062.  A boolean expression returns true or false. It has the following syntax:
  6063.  
  6064.    - expression comparison_operator [ANY | ALL] expression
  6065.     - expression [NOT] IN expression
  6066.     - [NOT] EXISTS expression
  6067.     - expression [NOT] BETWEEN expression AND expression
  6068.     - expression [NOT] LIKE expression
  6069.     - NOT expression LIKE expression
  6070.     - expression IS [NOT] NULL
  6071.     - NOT boolean_expression
  6072.     - boolean_expression {AND | OR} boolean_expression
  6073.     - [NOT] boolean_function
  6074.  
  6075.  
  6076.  Options
  6077.  
  6078.  function
  6079.    An aggregate function or a built-in function. See "Functions" for more
  6080.    information.
  6081.  
  6082.  subquery
  6083.    A nested SELECT statement with some restrictions. A subquery can be used
  6084.    in place of an expression if it returns a single value. See "Subqueries"
  6085.    for more information.
  6086.  
  6087.  arithmetic_operator
  6088.    One of the following:
  6089.  
  6090. ╓┌───────────────────────┌───────────────────────────────────────────────────╖
  6091.  Symbol                  Meaning
  6092.  Symbol                  Meaning
  6093.  ────────────────────────────────────────────────────────────────────────────
  6094.  ++                      Addition
  6095.  -                       Subtraction
  6096.  *                       Multiplication
  6097.  /                       Division
  6098.  %                       Modulo
  6099.  ────────────────────────────────────────────────────────────────────────────
  6100.  
  6101.  
  6102.    Addition, subtraction, division, and multiplication can be used on int,
  6103.    smallint, tinyint, float, and money columns. The modulo operator cannot be
  6104.    used on money or float columns. Modulo finds the integer remainder after
  6105.    dividing two whole numbers. For example, 21 % 11 = 10, because 21 divided
  6106.    by 11 equals 1 with a remainder of 10.
  6107.  
  6108.  bitwise_operator
  6109.    One of the following:
  6110.  
  6111.  Symbol           Meaning
  6112.  ────────────────────────────────────────────────────────────────────────────
  6113.  &                Bitwise AND (two operands)
  6114.  |                Bitwise OR (two operands)
  6115.                   Bitwise exclusive OR (two operands)
  6116.  ~                Bitwise NOT (one operand)
  6117.  ────────────────────────────────────────────────────────────────────────────
  6118.  
  6119.    The bitwise operators that require two operands (&, |, and ^) can be used
  6120.    on columns with the datatypes shown below:
  6121.  
  6122. ╓┌─────────────────┌─────────────────────────────────────────────────────────╖
  6123.  Left Operand      Right Operand
  6124.  ────────────────────────────────────────────────────────────────────────────
  6125.  int               int, smallint, tinyint, binary, or varbinary
  6126.  smallint          int, smallint, tinyint, binary, or varbinary
  6127.  tinyint           int, smallint, tinyint, binary, or varbinary
  6128.  varbinary         int, smallint, or tinyint
  6129.  bit               int, smallint, tinyint, or bit
  6130.  float             none
  6131.  binary            int, smallint, or tinyint
  6132.  ────────────────────────────────────────────────────────────────────────────
  6133.  
  6134.  
  6135.    The ~ bitwise operator can be used on columns with a datatype of int,
  6136.    smallint, tinyint, or bit.
  6137.  
  6138.    All the bitwise operators translate the integer parameters into binary
  6139.    representations before evaluating them. In the following examples, A =
  6140.    10101010 and B = 01001011:
  6141.  
  6142.        (A & B)  10101010
  6143.            01001011
  6144.            --------
  6145.            00001010
  6146.  
  6147.    The result is 1 where both A and B are 1.
  6148.  
  6149.        (A | B)  10101010
  6150.            01001011
  6151.            --------
  6152.            11101011
  6153.  
  6154.    The result is 1 where A, B, or A and B are 1.
  6155.  
  6156.        (A ^ B)  10101010
  6157.            01001011
  6158.            --------
  6159.            11100001
  6160.  
  6161.    The result is 1 where A or B, but not both, are 1.
  6162.  
  6163.        (~A)  10101010
  6164.            --------
  6165.            01010101
  6166.  
  6167.    All 1s are changed to 0s and all 0s to 1s.
  6168.  
  6169.  string_operator
  6170.    The string operator + can be used to concatenate expressions. See "String
  6171.    Functions" for examples.
  6172.  
  6173.  comparison_operator
  6174.    One of the following:
  6175.  
  6176. ╓┌────────────────────┌──────────────────────────────────────────────────────╖
  6177.  Symbol               Meaning
  6178.  ────────────────────────────────────────────────────────────────────────────
  6179.  =                    Equal to
  6180.  >                    Greater than
  6181.  <                    Less than
  6182.  >=                   Greater than or equal to
  6183.  <=                   Less than or equal to
  6184.  !=                   Not equal to
  6185.  !>                   Not greater than
  6186.  !<                   Not less than
  6187.  ────────────────────────────────────────────────────────────────────────────
  6188.  
  6189.  
  6190.    In comparing char and varchar data, < means closer to the beginning of the
  6191.    alphabet and > means closer to the end of the alphabet. In the ASCII
  6192.    collating sequence, lowercase letters are greater than uppercase letters,
  6193.    and uppercase letters are greater than numbers. In comparing dates, <
  6194.    means earlier and > means later. Trailing blanks are ignored for
  6195.    comparison purposes. For example, "Dirk" is the same as "Dirk  ".
  6196.  
  6197.    Put single or double quotation marks around all character and datetime
  6198.    data used with a comparison operator (= "Bennet", > "94609").
  6199.  
  6200.  ANY
  6201.    Is used with <, >, or = and a subquery. It returns results when any value
  6202.    retrieved in the subquery matches the value in the WHERE or HAVING clause
  6203.    of the outer statement. (See "Subqueries" for more information.)
  6204.  
  6205.  ALL
  6206.    Is used with < or > and a subquery. It returns results when all values
  6207.    retrieved in the subquery match the value in the WHERE or HAVING clause of
  6208.    the outer statement. (See "Subqueries" for more information.)
  6209.  
  6210.  NOT
  6211.    Negates the meaning of a keyword, a boolean function, or a boolean
  6212.    expression.
  6213.  
  6214.  IN
  6215.    Returns results when any value returned by the second expression matches
  6216.    the value in the first expression. The second expression must be a
  6217.    subquery or a list of values enclosed in parentheses. IN is equivalent to
  6218.    = ANY.
  6219.  
  6220.    See "WHERE Clause" for details.
  6221.  
  6222.  EXISTS
  6223.    Is used to test for the existence of some result. The expression that
  6224.    follows must be a subquery.
  6225.  
  6226.  BETWEEN
  6227.    The range-start keyword. Use AND for the range-end keyword. A range of
  6228.    between x and y is inclusive. A range of > x < y is not inclusive.
  6229.  
  6230.  LIKE
  6231.    Indicates that the following expression is a matching pattern. With LIKE,
  6232.    the second expression must always be a character string. LIKE is only
  6233.    available for char, varchar, and datetime columns (but not to search for
  6234.    seconds or milliseconds). It can be used with these wildcards:
  6235.  
  6236.  Wildcard                          Meaning
  6237.  ────────────────────────────────────────────────────────────────────────────
  6238.  %                                 Any string of 0 or more characters
  6239.  
  6240.  _ (underscore)                    Any single character
  6241.  
  6242.  [ ]                               Any single character within the
  6243.                                    specified range ([a-f]) or set ([abcdef])
  6244.  
  6245.  [^]                               Any single character not within the
  6246.                                    specified range ([^a-f]) or set
  6247.                                    ([^abcdef])
  6248.  
  6249.  ────────────────────────────────────────────────────────────────────────────
  6250.  
  6251.  
  6252.    The wildcard and the string must be enclosed in single or double quotation
  6253.    marks (LIKE "[dD]eFr_nce"). For complete information, see "Wildcard
  6254.    Characters."
  6255.  
  6256.  NULL
  6257.    Indicates the value is "unknown" to the system. Use IS NULL or IS NOT NULL
  6258.    in queries on columns defined as NULL. If you use IS NULL or IS NOT NULL
  6259.    to query columns defined as NOT NULL, SQL Server displays an error
  6260.    message.
  6261.  
  6262.    An expression with a bitwise or arithmetic operator evaluates to NULL if
  6263.    any of the operands is null.
  6264.  
  6265.  AND
  6266.    Connects two expressions and returns results when both are true.
  6267.  
  6268.    When more than one logical operator is used in a statement, AND is handled
  6269.    first. However, you can change the order of execution with parentheses.
  6270.  
  6271.  OR
  6272.    Connects two or more conditions and returns results when either of the
  6273.    conditions is true.
  6274.  
  6275.    When more than one logical operator is used in a statement, OR is
  6276.    evaluated after AND. However, you can change the order of execution with
  6277.    parentheses.
  6278.  
  6279.  boolean_function
  6280.    A function that returns true or false.
  6281.  
  6282.  
  6283.  Comments
  6284.  
  6285.  Parentheses can be used to group the elements in an expression or a boolean
  6286.  expression.
  6287.  
  6288.  When "expression" is given as a variable in a syntax statement, a simple
  6289.  expression is assumed. "Boolean expression" is specified when only a boolean
  6290.  expression is acceptable.
  6291.  
  6292.  Operators have the following precedence levels, where 1 is the highest level
  6293.  and 6 is the lowest level:
  6294.  
  6295.  
  6296.    1.  (unary) ~ - +
  6297.  
  6298.    2.  * / %
  6299.  
  6300.    3.  + - & | ^
  6301.  
  6302.    4.  NOT
  6303.  
  6304.    5.  AND
  6305.  
  6306.    6.  OR
  6307.  
  6308.  
  6309.  When all operators in an expression are of the same level, the order of
  6310.  execution is left to right. You can change the order of execution with
  6311.  parentheses─the most deeply nested expression is evaluated first.
  6312.  
  6313.  There are two ways to specify literal quotation marks within a char or
  6314.  varchar entry. The first method is to use two quotation marks. For example,
  6315.  if you have begun a character entry with a single quotation mark and wish to
  6316.  include a single quotation mark as part of the entry, use two single
  6317.  quotation marks:
  6318.  
  6319.    'I don"t understand.'
  6320.  
  6321.  With double quotation marks:
  6322.  
  6323.    "He said, ""It's not really confusing."""
  6324.  
  6325.  The second method is to enclose a quote in the opposite kind of quotation
  6326.  mark. In other words, surround an entry containing a double quotation mark
  6327.  with single quotation marks (or vice versa). Here are some examples:
  6328.  
  6329.    'George said, "There must be a better way."'
  6330.     "Isn't there a better way?"
  6331.     'George asked, "Isn"t there a better way?"'
  6332.  
  6333.  To continue a character string to the next line on your screen, enter a
  6334.  backslash (\) before going to the next line.
  6335.  
  6336.  
  6337.  See Also
  6338.  
  6339.  Functions, Joins, Search Conditions, SELECT, Subqueries, WHERE Clause,
  6340.  Wildcard Characters, sp_helpjoins
  6341.  
  6342.  
  6343.  Functions
  6344.  ────────────────────────────────────────────────────────────────────────────
  6345.  
  6346.  
  6347.  Function
  6348.  
  6349.  Return special information from the database. Functions are used in SELECT
  6350.  statements and are divided into aggregate functions (SUM, AVG, COUNT,
  6351.  COUNT(*), MAX, and MIN), date functions, mathematical functions, string
  6352.  functions, system functions, text/image functions, and type conversion
  6353.  functions. Each of these groups is discussed in its own section of this
  6354.  manual.
  6355.  
  6356.  
  6357.  Syntax
  6358.  
  6359.  Aggregate functions return summary values. See "Aggregate Functions" for
  6360.  syntax and other information.
  6361.  
  6362.  Date functions compute datetime values and their components, dateparts. See
  6363.  "Date Functions" for syntax and other information.
  6364.  
  6365.  Mathematical functions perform operations on numeric data. See "Mathematical
  6366.  Functions" for syntax and other information.
  6367.  
  6368.  String functions perform operations on character and binary data. See
  6369.  "String Functions" for syntax and other information.
  6370.  
  6371.  System functions return special information from the database. See "System
  6372.  Functions" for syntax and other information.
  6373.  
  6374.  Text/image functions perform operations on text and image data. See
  6375.  "Text/Image Functions" for syntax and other information.
  6376.  
  6377.  Conversion functions convert expressions of one datatype to another
  6378.  datatype. See "Conversion Function" for syntax and other information.
  6379.  
  6380.  
  6381.  Comments
  6382.  
  6383.  Unlike the aggregate functions, the other built-in functions are often used
  6384.  on their own or as part of a stored procedure or program. When they are used
  6385.  in SQL statements, they are allowed in the select list, in the WHERE clause,
  6386.  and anywhere an expression is allowed.
  6387.  
  6388.  Many of the miscellaneous built-in functions are SQL Server extensions to
  6389.  SQL.
  6390.  
  6391.  
  6392.  See Also
  6393.  
  6394.  Aggregate Functions, COMPUTE Clause, Conversion Function, Date Functions,
  6395.  Mathematical Functions, Search Conditions, SELECT, String Functions, System
  6396.  Functions, Text/Image Functions
  6397.  
  6398.  
  6399.  GOTO
  6400.  ────────────────────────────────────────────────────────────────────────────
  6401.  
  6402.  
  6403.  Function
  6404.  
  6405.  Causes an unconditional branching to a user-defined label.
  6406.  
  6407.  
  6408.  Syntax
  6409.  
  6410.    label:
  6411.  
  6412.     GOTO label
  6413.  
  6414.  
  6415.  Examples
  6416.  
  6417.    declare @count smallint
  6418.     select @count = 1
  6419.     restart:
  6420.      print "yes"
  6421.     select @count = @count + 1
  6422.     while @count <= 4
  6423.      goto restart
  6424.  
  6425.  
  6426.  Comments
  6427.  
  6428.  The label name must follow the rules for identifiers and must be followed by
  6429.  a colon (:) when it is declared. It is not followed by a colon when it is
  6430.  used with GOTO.
  6431.  
  6432.  GOTO is usually made dependent on an IF or WHILE test, or some other
  6433.  condition to avoid an endless loop between GOTO and the label.
  6434.  
  6435.  
  6436.  See Also
  6437.  
  6438.  Control-of-Flow Language, IF...ELSE, WHILE
  6439.  
  6440.  
  6441.  GRANT
  6442.  ────────────────────────────────────────────────────────────────────────────
  6443.  
  6444.  
  6445.  Function
  6446.  
  6447.  Assigns permissions to users.
  6448.  
  6449.  
  6450.  Syntax
  6451.  
  6452.  Object permissions:
  6453.  
  6454.    GRANT {ALL | permission_list}
  6455.     ON {table_name [(column_list)] | view_name [(column_list)] |
  6456.      stored_procedure_name}
  6457.     TO {PUBLIC | name_list}
  6458.  
  6459.  Statement permissions:
  6460.  
  6461.    GRANT {ALL | statement_list}
  6462.      TO {PUBLIC | name_list}
  6463.  
  6464.  
  6465.  Examples
  6466.  
  6467.    A. use pubs /* Permissions can be granted on objects in
  6468.    the
  6469.     ** current database only. */
  6470.     grant insert, delete
  6471.     on titles
  6472.     to mary, sales
  6473.  
  6474.    B. grant update
  6475.     on titles (royalty, advance)
  6476.     to public
  6477.  
  6478.    C. grant create database, create table
  6479.     to mary, john
  6480.  
  6481.    D. grant all on titles
  6482.     to public
  6483.  
  6484.    E. grant all
  6485.     to public
  6486.  
  6487.  
  6488.  Options
  6489.  
  6490.  ALL
  6491.    When used to assign object permissions (first syntax format), this keyword
  6492.    specifies that all permissions applicable to the specified object are
  6493.    granted or revoked. Only the System Administrator can use ALL to assign
  6494.    statement permissions (second syntax format), since only the System
  6495.    Administrator can grant or revoke CREATE DATABASE permission.
  6496.  
  6497.  permission_list
  6498.    A list of permissions granted. When permissions are granted on a table or
  6499.    a view, the permission list can include one or more of the following
  6500.    items: SELECT, INSERT, DELETE, and UPDATE.
  6501.  
  6502.    When permissions are granted on columns, the permission list can include
  6503.    one or both of the following items: SELECT and UPDATE.
  6504.  
  6505.    When permissions are granted on stored procedures, the permission list can
  6506.    include EXECUTE only.
  6507.  
  6508.    If more than one permission is listed, separate them with commas.
  6509.  
  6510.  statement_list
  6511.    A list of statements granted. The statement list can include CREATE
  6512.    DATABASE (if the user executing the statement is the System
  6513.    Administrator), CREATE DEFAULT, CREATE PROCEDURE, CREATE RULE, CREATE
  6514.    TABLE, CREATE VIEW, DUMP DATABASE, and DUMP TRANSACTION.
  6515.  
  6516.    If more than one statement is listed, separate them with commas.
  6517.  
  6518.  table_name
  6519.    The name of a table in the current database. Only one table can be listed
  6520.    for each GRANT statement.
  6521.  
  6522.  column_list
  6523.    A list of columns, separated by commas, to which the permissions apply. If
  6524.    columns are specified, only SELECT and UPDATE permissions can be granted.
  6525.  
  6526.  view_name
  6527.    The name of a view in the current database. Only one view can be listed
  6528.    for each GRANT statement.
  6529.  
  6530.  stored_procedure_name
  6531.    The name of a stored procedure in the current database. Only one stored
  6532.    procedure can be listed for each GRANT statement.
  6533.  
  6534.  PUBLIC
  6535.    All users in the public group, which includes all users not explicitly in
  6536.    another group.
  6537.  
  6538.  name_list
  6539.    A list of users' database names and/or groupnames, separated by commas.
  6540.    These names are stored in the database's sysusers table. Users are added
  6541.    with sp_adduser; groups are added with sp_addgroup. New users are made
  6542.    members of groups with sp_adduser; group membership is modified with
  6543.    sp_changegroup.
  6544.  
  6545.  
  6546.  Comments
  6547.  
  6548.  You can substitute the word "FROM" for "TO" in the GRANT syntax.
  6549.  
  6550.  Table  summarizes the SQL Server protection system. Permission to use each
  6551.  statement defaults to the System Administrator, the Database Owner, the
  6552.  table owner, or the default group public. This user can grant or revoke the
  6553.  permission if it is transferable. Users at higher levels are either
  6554.  automatically assigned the permission or (in the case of Database Owners)
  6555.  can get it via the SETUSER statement.
  6556.  
  6557.  For example, the owner of a database does not automatically receive
  6558.  permissions on objects owned by other users. A Database Owner can always
  6559.  take any permission by assuming the identity of the object owner with the
  6560.  SETUSER statement and then executing the appropriate GRANT or REVOKE
  6561.  statements. The System Administrator has permission to do anything at any
  6562.  time.
  6563.  
  6564.  For permissions that default to PUBLIC, no permission is required─that is,
  6565.  no GRANT or REVOKE statements must be written.
  6566.  
  6567.  Table   Summary of Permissions System
  6568.  
  6569. ╓┌────────────┌──────────┌──────────┌──────────┌───────┌──────────┌───┌──────╖
  6570.                                                        Can Be
  6571.               Defaults                                 Granted /
  6572.               to                                       Revoked
  6573.               System     Db Owner   Table      Public  Yes        No  N/A
  6574.               Admin                 Owner
  6575.                                                        Can Be
  6576.               Defaults                                 Granted /
  6577.               to                                       Revoked
  6578.               System     Db Owner   Table      Public  Yes        No  N/A
  6579.               Admin                 Owner
  6580.  Statement:
  6581.  ────────────────────────────────────────────────────────────────────────────
  6582.  ALTER        X          ─          ─          ─       (1)        ─   ─
  6583.  DATABASE
  6584.  
  6585.  ALTER TABLE  ─          ─          X          ─       ─          X   ─
  6586.  
  6587.  BEGIN        ─          ─          ─          X       ─          ─   X
  6588.  TRANSACTION
  6589.  
  6590.  CHECKPOINT   ─          X          ─          ─       ─          X   ─
  6591.  
  6592.  COMMIT       ─          ─          ─          X       ─          ─   X
  6593.  TRANSACTION
  6594.  
  6595.  CREATE       X          ─          ─          ─       X          ─   ─
  6596.                                                        Can Be
  6597.               Defaults                                 Granted /
  6598.               to                                       Revoked
  6599.               System     Db Owner   Table      Public  Yes        No  N/A
  6600.               Admin                 Owner
  6601.  Statement:
  6602. CREATE       X          ─          ─          ─       X          ─   ─
  6603.  DATABASE
  6604.  
  6605.  CREATE       ─          X          ─          ─       X          ─   ─
  6606.  DEFAULT
  6607.  
  6608.  CREATE       ─          ─          X          ─       ─          X   ─
  6609.  INDEX
  6610.  
  6611.  CREATE       ─          X          ─          ─       X          ─   ─
  6612.  PROCEDURE
  6613.  
  6614.  CREATE RULE  ─          X          ─          ─       X          ─   ─
  6615.  
  6616.  CREATE       ─          X          ─          (2)     X(2)       ─   ─
  6617.                                                        Can Be
  6618.               Defaults                                 Granted /
  6619.               to                                       Revoked
  6620.               System     Db Owner   Table      Public  Yes        No  N/A
  6621.               Admin                 Owner
  6622.  Statement:
  6623. CREATE       ─          X          ─          (2)     X(2)       ─   ─
  6624.  TABLE
  6625.  
  6626.  CREATE TRIG  ─          ─          X          ─       ─          X   ─
  6627.  GER
  6628.  
  6629.  CREATE VIEW  ─          X          ─          ─       X          ─   ─
  6630.  
  6631.  DBCC         ─          X          ─          ─       ─          ─   X
  6632.  
  6633.  DELETE       ─          ─          X(3)       ─       X          ─   ─
  6634.  
  6635.  DISK INIT    X          ─          ─          ─       ─          X   ─
  6636.  
  6637.  DISK REFIT   X          ─          ─          ─       ─          X   ─
  6638.                                                        Can Be
  6639.               Defaults                                 Granted /
  6640.               to                                       Revoked
  6641.               System     Db Owner   Table      Public  Yes        No  N/A
  6642.               Admin                 Owner
  6643.  Statement:
  6644. DISK REFIT   X          ─          ─          ─       ─          X   ─
  6645.  
  6646.  DISK REINIT  X          ─          ─          ─       ─          X   ─
  6647.  
  6648.  DROP (any    ─          ─          ─          ─       ─          X   ─
  6649.  object)(4)
  6650.  
  6651.  DUMP         ─          X          ─          ─       X          ─   ─
  6652.  DATABASE
  6653.  
  6654.  DUMP         ─          X          ─          ─       X          ─   ─
  6655.  TRANSACTION
  6656.  
  6657.  EXECUTE (5)  ─          ─          ─          ─       X          ─   ─
  6658.  
  6659.                                                        Can Be
  6660.               Defaults                                 Granted /
  6661.               to                                       Revoked
  6662.               System     Db Owner   Table      Public  Yes        No  N/A
  6663.               Admin                 Owner
  6664.  Statement:
  6665. 
  6666.  GRANT        ─          X          ─          ─       ─          X   ─
  6667.  
  6668.  GRANT on     ─          ─          ─          ─       ─          X   ─
  6669.  object (4)
  6670.  
  6671.  INSERT       ─          ─          X(3)       ─       X          ─   ─
  6672.  
  6673.  KILL         X          ─          ─          ─       ─          X   ─
  6674.  
  6675.  LOAD         ─          X          ─          ─       ─          X   ─
  6676.  DATABASE
  6677.  
  6678.  LOAD         ─          X          ─          ─       ─          X   ─
  6679.  TRANSACTION
  6680.                                                        Can Be
  6681.               Defaults                                 Granted /
  6682.               to                                       Revoked
  6683.               System     Db Owner   Table      Public  Yes        No  N/A
  6684.               Admin                 Owner
  6685.  Statement:
  6686. TRANSACTION
  6687.  
  6688.  PRINT        ─          ─          ─          X       ─          ─   X
  6689.  
  6690.  RAISERROR    ─          ─          ─          X       ─          ─   X
  6691.  
  6692.  READTEXT     ─          ─          X(3)       ─       X          ─   ─
  6693.  
  6694.  RECONFIGURE  X          ─          ─          ─       ─          X   ─
  6695.  
  6696.  REVOKE       ─          X          ─          ─       ─          X   ─
  6697.  
  6698.  REVOKE on    ─          ─          ─          ─       ─          X   ─
  6699.  object (4)
  6700.  
  6701.                                                        Can Be
  6702.               Defaults                                 Granted /
  6703.               to                                       Revoked
  6704.               System     Db Owner   Table      Public  Yes        No  N/A
  6705.               Admin                 Owner
  6706.  Statement:
  6707. 
  6708.  ROLLBACK     ─          ─          ─          X       ─          ─   X
  6709.  TRANSACTION
  6710.  
  6711.  SAVE         ─          ─          ─          X       ─          ─   X
  6712.  TRANSACTION
  6713.  
  6714.  SELECT       ─          ─          X(3)       ─       X          ─   ─
  6715.  
  6716.  SET          ─          ─          ─          X       ─          ─   X
  6717.  
  6718.  SETUSER      ─          X          ─          ─       ─          X   ─
  6719.  
  6720.  TRUNCATE     ─          ─          X          ─       ─          X   ─
  6721.  TABLE
  6722.                                                        Can Be
  6723.               Defaults                                 Granted /
  6724.               to                                       Revoked
  6725.               System     Db Owner   Table      Public  Yes        No  N/A
  6726.               Admin                 Owner
  6727.  Statement:
  6728. TABLE
  6729.  
  6730.  UPDATE       ─          ─          X(3)       ─       X          ─   ─
  6731.  
  6732.  UPDATE       ─          ─          X          ─       ─          X   ─
  6733.  STATISTICS
  6734.  
  6735.  WRITETEXT    ─          ─          X(3)       ─       X          ─   ─
  6736.  
  6737.  ────────────────────────────────────────────────────────────────────────────
  6738.  
  6739.  
  6740.  
  6741.  (1) Transferred with CREATE DATABASE permission
  6742.  (2) Public can create temporary tables, no permission required
  6743.  (3) If a view, permission defaults to view owner
  6744.  (4) Defaults to object owner
  6745.  (5) Defaults to stored procedure owner
  6746.  
  6747.  
  6748.  You can grant or revoke permissions on objects in the current database only.
  6749.  
  6750.  
  6751.  The statement permissions include CREATE DATABASE (which can be granted only
  6752.  by the System Administrator), CREATE DEFAULT, CREATE PROCEDURE, CREATE RULE,
  6753.  CREATE TABLE, CREATE VIEW, DUMP DATABASE, and DUMP TRANSACTION.
  6754.  
  6755.  User groups allow you to grant or revoke permissions to more than one user
  6756.  with a single statement. Each user can be a member of at most one group.
  6757.  
  6758.  The Database Owner or System Administrator can add new users with sp_adduser
  6759.  and create groups with sp_addgroup.
  6760.  
  6761.  A guest user can be added with sp_adduser to allow users with login IDs on
  6762.  SQL Server to use the database with limited permissions.
  6763.  
  6764.  By default, all users are a member of the group public if they have not
  6765.  explicitly been added to another group. To add a new user to a group other
  6766.  than public, use the sp_adduser system procedure. To change an established
  6767.  user's group, use the sp_changegroup system procedure.
  6768.  
  6769.  To remove a user, use the sp_dropuser system procedure. To remove a group,
  6770.  use sp_dropgroup.
  6771.  
  6772.  To display the members of a group, use sp_helpgroup. Its optional parameter
  6773.  is the groupname. If you do not give a groupname, sp_helpgroup displays all
  6774.  the groups that exist and lists the members of each.
  6775.  
  6776.  The sp_helprotect system procedure reports permissions on a database object
  6777.  or on a user.
  6778.  
  6779.  See Chapter 2, "System Procedures," for details on the procedures for adding
  6780.  and dropping groups and users.
  6781.  
  6782.  The SQL Server setup program assigns a set of permissions to the default
  6783.  group, public.
  6784.  
  6785.  GRANT and REVOKE statements are order sensitive. The statement that takes
  6786.  effect when there is a conflict is the most recently executed one.
  6787.  
  6788.  Permission to grant and revoke defaults to particular users and cannot be
  6789.  transferred.
  6790.  
  6791.  
  6792.  See Also
  6793.  
  6794.  REVOKE, SETUSER, sp_addgroup, sp_adduser, sp_changedbowner, sp_changegroup,
  6795.  sp_dropgroup, sp_dropuser, sp_helpgroup, sp_helprotect, sp_helpuser
  6796.  
  6797.  
  6798.  GROUP BY and HAVING Clauses
  6799.  ────────────────────────────────────────────────────────────────────────────
  6800.  
  6801.  
  6802.  Function
  6803.  
  6804.  Divide a table into groups. These clauses are used in SELECT statements.
  6805.  
  6806.  
  6807.  Syntax
  6808.  
  6809.    GROUP BY [ALL] aggregate_free_expression
  6810.      [, aggregate_free_expression...]
  6811.     [HAVING search_conditions]
  6812.  
  6813.  
  6814.  Examples
  6815.  
  6816.    A. select type, avg(advance), sum(ytd_sales)
  6817.     from titles
  6818.     group by type
  6819.  
  6820.  Example A calculates the average advance and the sum of the sales for each
  6821.  type of book.
  6822.  
  6823.    B. select type, pub_id, avg(advance), sum(ytd_sales)
  6824.     from titles
  6825.     group by type, pub_id
  6826.  
  6827.  Example B groups the results by type and then by pub_id within each type.
  6828.  
  6829.    C. select type, avg(price)
  6830.     from titles
  6831.     group by type
  6832.     having type like 'p%'
  6833.  
  6834.  Example C displays results for groups having a type beginning with "p" only.
  6835.  
  6836.  
  6837.    D. select pub_id, sum(advance), avg(price)
  6838.     from titles
  6839.     group by pub_id
  6840.     having sum(advance) > $15000
  6841.     and avg(price) < $10
  6842.     and pub_id > "0800"
  6843.  
  6844.  Example D displays results for groups matching the conditions in the HAVING
  6845.  clause.
  6846.  
  6847.  
  6848.  Options
  6849.  
  6850.  GROUP BY
  6851.    Specifies the groups into which the table will be divided and, if
  6852.    aggregate functions are included in the select list, finds a summary value
  6853.    for each group. These summary values appear as new columns in the results,
  6854.    rather than as new rows. When GROUP BY is used with standard SQL, each
  6855.    item in the select list must either have a fixed value in every row in the
  6856.    group or be used with aggregate functions that produce a single value for
  6857.    each group. TRANSACT-SQL has no such restrictions on the items in the
  6858.    select list. Also, TRANSACT-SQL allows you to group by a column name or
  6859.    any expression (except a column heading or alias), while with standard SQL
  6860.    you can only group by a column name. You can use these aggregate functions
  6861.    with GROUP BY (the expression in the syntax is usually a column name):
  6862.  
  6863. ╓┌─────────────────────────────────┌─────────────────────────────────────────╖
  6864.  Aggregate Function                Result
  6865.  ────────────────────────────────────────────────────────────────────────────
  6866.  SUM([DISTINCT] expression)        Returns the total of the [distinct]
  6867.                                    values in the numeric column
  6868.  
  6869.  Aggregate Function                Result
  6870.  ────────────────────────────────────────────────────────────────────────────
  6871. 
  6872.  AVG([DISTINCT] expression)        Returns the average of the [distinct]
  6873.                                    values in the numeric column
  6874.  
  6875.  COUNT([DISTINCT] expression)      Returns the number of [distinct]
  6876.                                    non-null values in the expression
  6877.  
  6878.  COUNT(*)                          Returns the number of selected rows
  6879.  
  6880.  MAX(expression)                   Returns the highest value in the
  6881.                                    expression
  6882.  
  6883.  MIN(expression)                   Returns the lowest value in the
  6884.                                    expression
  6885.  
  6886.  ────────────────────────────────────────────────────────────────────────────
  6887.  
  6888.  
  6889.  
  6890.    A table can be grouped by any combination of columns─that is, groups can
  6891.    be nested within each other.
  6892.  
  6893.  ALL
  6894.    Includes all groups in the results, even those that don't have any rows
  6895.    that meet the search conditions. For example:
  6896.  
  6897.    select type, avg(price)
  6898.      from titles
  6899.      where royalty = 10
  6900.      group by all type
  6901.  
  6902. ╓┌─┌──────────────────┌──────────────────────────────────────────────────────╖
  6903.  ────────────────────────────────────────────────────────────────────────────
  6904.    type
  6905.    ------------       ----------
  6906.    UNDECIDED          NULL
  6907.    business           17.31
  6908.    mod_cook           NULL
  6909.    popular_comp       20.00
  6910.    psychology         14.14
  6911.  ────────────────────────────────────────────────────────────────────────────
  6912.   psychology         14.14
  6913.    trad_cook          17.97
  6914.    (6 rows affected)
  6915.  
  6916.  
  6917.    ALL is meaningful only if the SELECT statement includes a WHERE clause.
  6918.  
  6919.  aggregate_free_expression
  6920.    An expression that includes no aggregate functions. TRANSACT-SQL allows
  6921.    you to group by such an expression as well as by a column name.
  6922.  
  6923.    Note that you cannot group by a column heading or alias. This example is
  6924.    correct:
  6925.  
  6926.    select Price=avg(price), Pay=avg(advance), Total=price * $1.15
  6927.      from titles
  6928.      group by price * $1.15
  6929.  
  6930.  HAVING
  6931.    Sets conditions for the GROUP BY clause, similar to the way that WHERE
  6932.    sets conditions for the SELECT clause. HAVING search conditions are
  6933.    identical to WHERE search conditions with one exception: WHERE search
  6934.    conditions cannot include aggregate functions and HAVING search conditions
  6935.    can. Here's an example of a HAVING clause with aggregate functions:
  6936.  
  6937.        select pub_id, total = sum(ytd_sales)
  6938.         from titles
  6939.         where ytd_sales is not null
  6940.         group by pub_id
  6941.         having count(*)>5
  6942.  
  6943.    There is no limit on the number of conditions that can be included in a
  6944.    HAVING clause.
  6945.  
  6946.    HAVING can be used without GROUP BY.
  6947.  
  6948.    If there are columns in the select list that neither have aggregate
  6949.    functions applied to them nor are included in the query's GROUP BY clause
  6950.    (illegal in standard SQL), the meanings of HAVING and WHERE are somewhat
  6951.    different. For examples, see the next section, "Comments."
  6952.  
  6953.  
  6954.  Comments
  6955.  
  6956.  You can use a column name or any expression (except a column heading) after
  6957.  GROUP BY.
  6958.  
  6959.  Null values in the GROUP BY column are put into a single group.
  6960.  
  6961.  Columns of type text or image are invalid in GROUP BY and HAVING clauses.
  6962.  
  6963.  Aggregate functions can be used in the select list or in the HAVING clause
  6964.  of a SELECT statement. They cannot be used in a WHERE clause.
  6965.  
  6966.  The aggregate functions, which calculate summary values from the non-null
  6967.  values in a column, can be divided into two groups. If they are applied to
  6968.  all the rows in a table (producing a single value per function), they are
  6969.  called scalar aggregate functions. If they are applied to all the rows that
  6970.  have the same value in a specified column or expression with the GROUP BY
  6971.  and, optionally, the HAVING clause (producing a value for each group per
  6972.  function), they are called vector aggregate functions. In either case, the
  6973.  results of the aggregate functions are shown as new columns.
  6974.  
  6975.  You can nest a vector aggregate function inside a scalar aggregate function.
  6976.  (See "Aggregate Functions" for more information and an example.)
  6977.  
  6978.  In standard SQL, columns in a select list that include aggregate functions
  6979.  must either have aggregate functions applied to them or be in the GROUP BY
  6980.  list. TRANSACT-SQL has no such restrictions. The first example shows a
  6981.  SELECT statement with the standard restrictions; the second one shows the
  6982.  same statement with another item (title_id) added to the select list to
  6983.  illustrate the difference in displays. The "extra" columns can also be
  6984.  referenced in the HAVING clause. For example:
  6985.  
  6986.    A. select type, avg(price)
  6987.     from titles
  6988.     group by type
  6989.  
  6990. ╓┌─┌──────────────────┌──────────────────────────────────────────────────────╖
  6991.  ────────────────────────────────────────────────────────────────────────────
  6992.    type
  6993.    ---------------    -----------
  6994.    UNDECIDED          NULL
  6995.  ────────────────────────────────────────────────────────────────────────────
  6996.   UNDECIDED          NULL
  6997.    business           13.73
  6998.    mod_cook           11.49
  6999.    popular_comp       21.48
  7000.    psychology         13.50
  7001.    trad_cook          15.96
  7002.    (6 rows affected)
  7003.  
  7004.  
  7005.    B. select type, title_id, avg(price)
  7006.     from titles
  7007.     group by type
  7008.  
  7009. ╓┌─┌───────────────────┌─────────┌───────────────────────────────────────────╖
  7010.  ────────────────────────────────────────────────────────────────────────────
  7011.    type                title_id
  7012.    ---------------     --------  ---------
  7013.    business            BU1032    13.73
  7014.    business            BU1111    13.73
  7015.    business            BU2075    13.73
  7016.  ────────────────────────────────────────────────────────────────────────────
  7017.   business            BU2075    13.73
  7018.    business            BU7832    13.73
  7019.    mod_cook            MC2222    11.49
  7020.    mod_cook            MC3021    11.49
  7021.    UNDECIDED           MC3026    NULL
  7022.    popular_comp        PC1035    21.48
  7023.    popular_comp        PC8888    21.48
  7024.    popular_comp        PC9999    21.48
  7025.    psychology          PS1372    13.50
  7026.    psychology          PS2091    13.50
  7027.    psychology          PS2106    13.50
  7028.    psychology          PS3333    13.50
  7029.    psychology          PS7777    13.50
  7030.    trad_cook           TC3218    15.96
  7031.    trad_cook           TC4203    15.96
  7032.    trad_cook           TC7777    15.96
  7033.    (18 rows affected)
  7034.  
  7035.  
  7036.  If there are columns in the select list that neither have aggregate
  7037.  functions applied to them nor are included in the query's GROUP BY clause
  7038.  (illegal in standard SQL), the meanings of HAVING and WHERE are somewhat
  7039.  different.
  7040.  
  7041.  In this situation, a WHERE clause restricts the rows that are included in
  7042.  the calculation of the aggregate function but does not restrict the rows
  7043.  returned by the query. Conversely, a HAVING clause restricts the rows
  7044.  returned by the query but does not affect the calculation of the aggregate
  7045.  function.
  7046.  
  7047.  The following examples illustrate the situation:
  7048.  
  7049.    A. select pub_id, count(pub_id)
  7050.     from publishers
  7051.  
  7052. ╓┌─┌──────────────────┌──────────────────────────────────────────────────────╖
  7053.  ────────────────────────────────────────────────────────────────────────────
  7054.    pub_id
  7055.    -----------        ---------
  7056.    0736               3
  7057.    0877               3
  7058.  ────────────────────────────────────────────────────────────────────────────
  7059.   0877               3
  7060.    1389               3
  7061.    (3 rows affected)
  7062.  
  7063.  
  7064.    B. select pub_id, count(pub_id)
  7065.     from publishers
  7066.     where pub_id < "1000"
  7067.  
  7068. ╓┌─┌──────────────────┌──────────────────────────────────────────────────────╖
  7069.  ────────────────────────────────────────────────────────────────────────────
  7070.    pub_id
  7071.    ------------       -----------
  7072.    0736               2
  7073.    0877               2
  7074.    1389               2
  7075.  
  7076.    (3 rows affected)
  7077.  
  7078.  
  7079.  Since the select list includes the pub_id column, which has no aggregate
  7080.  function applied and is not grouped, the WHERE clause returns all the values
  7081.  for pub_id, although it counts only rows where pub_id is less than 1000.
  7082.  
  7083.    C. select pub_id, count(pub_id)
  7084.     from publishers
  7085.     having pub_id < "1000"
  7086.  
  7087. ╓┌─┌──────────────────┌──────────────────────────────────────────────────────╖
  7088.  ────────────────────────────────────────────────────────────────────────────
  7089.    pub_id
  7090.    ------------       ---------
  7091.    0736               3
  7092.    0877               3
  7093.  
  7094.    (2 rows affected)
  7095.  
  7096.  
  7097.  In example C, the HAVING clause means that only rows where pub_id is less
  7098.  than 1000 are returned. All the rows in the table are counted for the
  7099.  purposes of calculating the aggregate function.
  7100.  
  7101.    D. select pub_id, count(pub_id)
  7102.     from publishers
  7103.     where pub_id < "1000"
  7104.     having pub_id < "1000"
  7105.  
  7106.  ────────────────────────────────────────────────────────────────────────────
  7107.    pub_id
  7108.    ------------       --------
  7109.    0736               2
  7110.    0877               2
  7111.    (2 rows affected)
  7112.  
  7113.  Since both HAVING and WHERE clauses are included (example D), only rows
  7114.  where pub_id is less than 1000 are returned, and only these rows are
  7115.  included in the calculation of the COUNT aggregate function.
  7116.  
  7117.  
  7118.  See Also
  7119.  
  7120.  Aggregate Functions, COMPUTE Clause, Functions, Row Aggregate Functions,
  7121.  Search Conditions, SELECT, WHERE Clause
  7122.  
  7123.  
  7124.  Identifiers
  7125.  ────────────────────────────────────────────────────────────────────────────
  7126.  
  7127.  
  7128.  Function
  7129.  
  7130.  Name database objects─databases, tables, views, columns, indexes, triggers,
  7131.  procedures, defaults, rules, and so on.
  7132.  
  7133.  
  7134.  Syntax
  7135.  
  7136.  Identifiers are 1 to 30 characters long. The first character must be a
  7137.  letter or the symbols #, or _. (A table name beginning with # denotes a
  7138.  temporary table.)
  7139.  
  7140.  Characters following the first character can include letters, digits, or the
  7141.  symbols #, $, or _. No embedded spaces are allowed in identifiers.
  7142.  
  7143.  ────────────────────────────────────────────────────────────────────────────
  7144.  NOTE
  7145.  When an identifier is preceded by the "at" symbol (@), the identifier is
  7146.  from 1 to 29 characters long. (For example, variable names and stored
  7147.  procedure parameter names are preceded by "@".)
  7148.  ────────────────────────────────────────────────────────────────────────────
  7149.  
  7150.  By default, SQL Server is not sensitive to the case (upper or lower) of
  7151.  identifiers and data. If required, a System Administrator can build a
  7152.  case-sensitive SQL Server with the bldmastr program. The case of switches
  7153.  for utility programs is significant.
  7154.  
  7155.  Object names need not be unique in a database. However, column names and
  7156.  index names must be unique within a table, and other object names must be
  7157.  unique for each owner within a database. Database names must be unique on
  7158.  SQL Server. You can uniquely identify a table or column by adding other
  7159.  names that qualify it─the database name, owner's name, and (for a column)
  7160.  the table or view name. Each of these qualifiers is separated from the next
  7161.  by a period:
  7162.  
  7163.    database.owner.table_name.column_name
  7164.     database.owner.view_name.column_name
  7165.  
  7166.  The naming conventions are indicated in the syntax as follows:
  7167.  
  7168.    [[database.]owner.]table_name
  7169.     [[database.]owner.]view_name
  7170.  
  7171.  The default value for owner is the current user, and the default value for
  7172.  database is the current database.
  7173.  
  7174.  If desired, intermediate elements in a name can be omitted and their
  7175.  positions indicated by periods, as long as the system is given enough
  7176.  information to identify the object:
  7177.  
  7178.    database..table_name
  7179.     database..view_name
  7180.  
  7181.  
  7182.  Comments
  7183.  
  7184.  When qualifying a column name and a table name in the same statement, be
  7185.  sure to use the same qualifying expressions for each; they are evaluated as
  7186.  strings and must match or an error is returned.
  7187.  
  7188.  Two examples are shown, with different entries for the column name. Example
  7189.  B is not correct because the syntax for the column name does not match the
  7190.  syntax for the table name.
  7191.  
  7192.    A. select demo.mary.publishers.city
  7193.     from demo.mary.publishers
  7194.  
  7195.     city
  7196.     -----------------------
  7197.     Boston
  7198.     Washington
  7199.     Berkeley
  7200.  
  7201.    B. select demo.mary.publishers.city
  7202.     from demo..publishers
  7203.  
  7204.     The column prefix "demo.mary.publishers" does not match
  7205.     a table name or alias name used in the query.
  7206.  
  7207.  The names of system datatypes and built-in functions are not reserved words
  7208.  and may be used to name database objects.
  7209.  
  7210.  When you reference an object without qualifying it with the database name
  7211.  and owner name, SQL Server tries to find the object in the current database
  7212.  among the objects you own. You need not qualify references to your objects
  7213.  in the current database, nor do you need to qualify objects owned by the
  7214.  Database Owner. However, you must qualify objects owned by other users by
  7215.  using the user's name.
  7216.  
  7217.  You can rename user objects (including user-defined datatypes) with
  7218.  sp_rename.
  7219.  
  7220.  
  7221.  See Also
  7222.  
  7223.  CREATE DATABASE, CREATE DEFAULT, CREATE PROCEDURE, CREATE RULE, CREATE
  7224.  TABLE, CREATE TRIGGER, CREATE VIEW, Datatypes, SELECT, sp_rename
  7225.  
  7226.  
  7227.  IF...ELSE
  7228.  ────────────────────────────────────────────────────────────────────────────
  7229.  
  7230.  
  7231.  Function
  7232.  
  7233.  Impose conditions on the execution of an SQL statement. The SQL statement
  7234.  following an IF keyword and its condition executes if the condition is
  7235.  satisfied (when the boolean expression returns "true"). The optional ELSE
  7236.  keyword introduces an alternate SQL statement that executes when the IF
  7237.  condition is not satisfied (when the boolean expression returns "false").
  7238.  
  7239.  
  7240.  Syntax
  7241.  
  7242.    IF
  7243.      boolean_expression
  7244.      statement
  7245.     [ELSE
  7246.      [boolean_expression]
  7247.      statement]
  7248.  
  7249.  
  7250.  Examples
  7251.  
  7252.    A. if 3 > 2
  7253.     print "yes"
  7254.  
  7255.    B. if exists (select zip from authors
  7256.     where zip = "94705")
  7257.     print "Berkeley author"
  7258.  
  7259.    C. if (select max(id) from sysobjects) < 50
  7260.     print "No user-created objects in this database"
  7261.     else
  7262.     begin
  7263.     print "These are the user-created objects"
  7264.     select name, type, id
  7265.     from sysobjects
  7266.     where id > 50
  7267.     end
  7268.  
  7269.  The IF...ELSE condition tests for the presence of user-created objects (all
  7270.  of which have ID numbers that are larger than 50) in a database. Where user
  7271.  tables exist, the ELSE clause prints a message and selects their names,
  7272.  types, and ID numbers.
  7273.  
  7274.  
  7275.  Options
  7276.  
  7277.  boolean_expression
  7278.    An expression (a column name, a constant, any combination of column names
  7279.    and constants connected by arithmetic or bitwise operators, or a subquery)
  7280.    that returns "true" or "false." If the boolean expression contains a
  7281.    SELECT statement, the SELECT statement must be enclosed in parentheses.
  7282.  
  7283.  
  7284.  Comments
  7285.  
  7286.  The IF or ELSE condition can affect the performance of only a single SQL
  7287.  statement unless statements are grouped into a block between the keywords
  7288.  BEGIN and END, as in the last example.
  7289.  
  7290.  If a SELECT statement is used as part of the boolean expression, it must
  7291.  return a single value.
  7292.  
  7293.  The statement clause could be an EXECUTE statement or any other legal SQL
  7294.  statement or statement block.
  7295.  
  7296.  IF...ELSE constructs can be used either in a stored procedure (where they
  7297.  are often used to test for the existence of some parameter) or in ad hoc
  7298.  queries as in the earlier examples.
  7299.  
  7300.  IF tests can be nested, either within another IF or following an ELSE. There
  7301.  is no limit on the number of levels of nesting.
  7302.  
  7303.  
  7304.  See Also
  7305.  
  7306.  BEGIN...END, Control-of-Flow Language, CREATE PROCEDURE, Expressions,
  7307.  Subqueries
  7308.  
  7309.  
  7310.  INSERT
  7311.  ────────────────────────────────────────────────────────────────────────────
  7312.  
  7313.  
  7314.  Function
  7315.  
  7316.  Adds new rows to a table or view.
  7317.  
  7318.  
  7319.  Syntax
  7320.  
  7321.    INSERT [INTO]
  7322.      [[database.]owner.]{table_name | view_name} [(column_list)]
  7323.      {VALUES (constant_expression [, constant_expression]...) |
  7324.      select_statement}
  7325.  
  7326.  
  7327.  Examples
  7328.  
  7329.    A. insert titles
  7330.     values("BU2222", "Faster!", "business", "1389",
  7331.      null, null, null, null, "ok", "06/17/87")
  7332.  
  7333.    B. insert titles(title_id, title, type, pub_id, notes, pubdate)
  7334.      values ('BU1237', 'Get Going!', 'business', '1389',
  7335.      'great', '06/18/86')
  7336.  
  7337.    C. insert authors
  7338.      select *
  7339.      from newauthors
  7340.      where city = "San Francisco"
  7341.  
  7342.    D. insert test
  7343.      select *
  7344.      from test
  7345.      where city = "San Francisco"
  7346.  
  7347.  
  7348.  Options
  7349.  
  7350.  INTO
  7351.    An optional keyword.
  7352.  
  7353.  column_list
  7354.    A list of one or more columns to which data is to be added. The columns
  7355.    can be in any order, but the incoming data (whether in a VALUES clause or
  7356.    a SELECT clause) must be in the same order.
  7357.  
  7358.    The column_list is only necessary when some, but not all, of the columns
  7359.    in the table are to receive data. Enclose the items in column_list in
  7360.    parentheses. If no column_list is given, all of the columns in the
  7361.    receiving table (in CREATE TABLE order) is assumed.
  7362.  
  7363.    The column_list determines the order in which values are entered. In the
  7364.    first example, the columns in the column_list of the receiving table match
  7365.    the columns in the select list of the contributing table. Given a name
  7366.    like Euphemia Briggs, Euphemia (the first name) will be stored in the
  7367.    au_fname column and Briggs (the last name) will be stored in the au_lname
  7368.    column.
  7369.  
  7370.    In the second example, the order of the columns in the select list is not
  7371.    the same as the order of the columns in column_list. Euphemia (the first
  7372.    name) is stored in the au_lname column and Briggs (the last name) is
  7373.    stored in the au_fname column.
  7374.  
  7375.    A. insert authors (au_lname, au_fname)
  7376.      select au_lname, au_fname
  7377.      from newauthors
  7378.  
  7379.    B. insert authors (au_lname, au_fname)
  7380.      select au_fname, au_lname
  7381.      from newauthors
  7382.  
  7383.  VALUES
  7384.    Introduces a list of constant expressions.
  7385.  
  7386.  constant_expression
  7387.    Constant or null values for the indicated columns. The values list must be
  7388.    enclosed in parentheses and must match the explicit or implicit columns
  7389.    list. Enclose char, varchar, and datetime constants in single or double
  7390.    quotation marks.
  7391.  
  7392.  select_statement
  7393.    A standard SELECT statement used to retrieve the values to be inserted.
  7394.  
  7395.  
  7396.  Comments
  7397.  
  7398.  INSERT adds new rows only. Use UPDATE to modify any column values in a row
  7399.  you've already inserted.
  7400.  
  7401.  You can leave out items in the column_list and VALUES list as long as the
  7402.  omitted columns are defined to allow null values. See example B.
  7403.  
  7404.  You can select rows from a table and insert them into the same table in a
  7405.  single statement. See example D.
  7406.  
  7407.  If you have two tables identical in structure except that one has NULL
  7408.  fields and some NULL data and the other has NOT NULL fields, this difference
  7409.  makes it impossible to insert the data from the NULL table into the NOT NULL
  7410.  table with a SELECT statement.
  7411.  
  7412.  INSERT interacts with the IGNORE_DUP_KEY, IGNORE_DUP_ROW, and ALLOW_DUP_ROW
  7413.  options set with the CREATE INDEX statement. (See "CREATE INDEX" for more
  7414.  information.)
  7415.  
  7416.  A rule can be created and bound to a column to restrict the domain of legal
  7417.  values that can be entered into it. Rules are created with CREATE RULE and
  7418.  bound with the sp_bindrule system procedure.
  7419.  
  7420.  A default can be created and bound to a column in order to supply a value if
  7421.  a user does not explicitly enter one. Defaults are created with CREATE
  7422.  DEFAULT and bound with the sp_bindefault system procedure.
  7423.  
  7424.  You can define a trigger that takes a specified action when an INSERT
  7425.  statement is executed on a specified table.
  7426.  
  7427.  If an INSERT statement violates domain or integrity rules (see "CREATE RULE"
  7428.  and "CREATE TRIGGER") or if it is the wrong datatype (see "CREATE TABLE" and
  7429.  "Datatypes"), the statement fails, and SQL Server displays an error message.
  7430.  
  7431.  
  7432.  
  7433.  Permissions
  7434.  
  7435.  INSERT permission defaults to the table or view owner, who can transfer it
  7436.  to other users.
  7437.  
  7438.  
  7439.  See Also
  7440.  
  7441.  CREATE DEFAULT, CREATE INDEX, CREATE RULE, CREATE TABLE, CREATE TRIGGER,
  7442.  Datatypes, DELETE, SELECT, UPDATE, sp_bindefault, sp_bindrule,
  7443.  sp_unbindefault, sp_unbindrule
  7444.  
  7445.  
  7446.  Joins
  7447.  ────────────────────────────────────────────────────────────────────────────
  7448.  
  7449.  
  7450.  Function
  7451.  
  7452.  Compare two or more tables (or views) by specifying a column from each,
  7453.  comparing the values in those columns row by row, and concatenating rows
  7454.  that have matching values.
  7455.  
  7456.  
  7457.  Syntax
  7458.  
  7459.    FROM {table_list | view_list}
  7460.     WHERE [NOT] [table_name. | view_name.] column_name
  7461.     join_operator [table_name. | view_name.] column_name
  7462.     [{AND | OR} [NOT] [table_name. | view_name.] column_name
  7463.     join_operator [table_name. | view_name.] column_name...]
  7464.  
  7465.  
  7466.  Examples
  7467.  
  7468.    A. select au_fname, au_lname, pub_name
  7469.     from authors, publishers
  7470.     where authors.city = publishers.city
  7471.  
  7472.    B. select au_fname, au_lname, pub_name
  7473.     from authors, publishers
  7474.     where authors.city *= publishers.city
  7475.  
  7476.    C. update titles
  7477.     set price = price * 2
  7478.     from titles, publishers
  7479.     where titles.pub_id = publishers.pub_id
  7480.     and publishers.state = "CA"
  7481.  
  7482.    D. select type, au_lname, au_fname, title
  7483.     from authors, titles, titleauthor
  7484.     where authors.au_id = titleauthor.au_id
  7485.     and titles.title_id = titleauthor.title_id
  7486.     order by type
  7487.  
  7488.    E. select au_fname, au_lname, pub_name
  7489.     from authors, publishers
  7490.     where authors.city = publishers.city
  7491.     and authors.state = publishers.state
  7492.  
  7493.    F. select au1.au_fname, au1.au_lname, au2.au_fname, au2.au_lname
  7494.     from authors au1, authors au2
  7495.     where au1.zip = au2.zip
  7496.     and (au1.au_lname + au1.au_fname) != (au2.au_lname + au2.au_fname)
  7497.  
  7498.  Example F finds the authors that live in the same zip code area as other
  7499.  authors, eliminating the rows where the author's name matches itself.
  7500.  
  7501.    G. select titles.type, t1.title_id, t1.price, t2.title_id,
  7502.    t2.price
  7503.     from titles t1, titles t2
  7504.     where t1.type = t2.type
  7505.     and t1.price != t2.price
  7506.  
  7507.  
  7508.  Options
  7509.  
  7510.  FROM table_list, view_list
  7511.    The table(s) and view(s) that are being joined. All tables and/or views
  7512.    referenced elsewhere in the statement must be included in the FROM clause.
  7513.  
  7514.  WHERE clause
  7515.    The connection between the table(s) and view(s) named in the FROM clause,
  7516.    restricting the rows to be included in the results. Qualify column names
  7517.    if there is ambiguity about the table or view to which they belong.
  7518.    Columns being joined must be join compatible─they must both be numeric or
  7519.    both be character columns (or SQL Server must be instructed to treat them
  7520.    that way with the CONVERT function).
  7521.  
  7522.  join_operator
  7523.    One of the following:
  7524.  
  7525. ╓┌─────────────────────────────────┌─────────────────────────────────────────╖
  7526.  Symbol                            Meaning
  7527.  ────────────────────────────────────────────────────────────────────────────
  7528.  =                                 Equal to.
  7529.  
  7530.  >                                 Greater than.
  7531.  
  7532.  <                                 Less than.
  7533.  
  7534.  >=                                Greater than or equal to.
  7535.  
  7536.  <=                                Less than or equal to.
  7537.  
  7538.  !=                                Not equal to.
  7539.  
  7540.  !>                                Not greater than.
  7541.  Symbol                            Meaning
  7542.  ────────────────────────────────────────────────────────────────────────────
  7543. !>                                Not greater than.
  7544.  
  7545.  !<                                Not less than.
  7546.  
  7547.  *=                                Includes all rows from the first table
  7548.                                    that meet the statement's restrictions.
  7549.                                    The second table generates values if
  7550.                                    there is a match on the join condition.
  7551.                                    Otherwise, the second table generates
  7552.                                    null values.
  7553.  
  7554.  =*                                Includes in the results all rows from
  7555.                                    the second table that meet the
  7556.                                    statement's restrictions. The first
  7557.                                    table generates values if there is a
  7558.                                    match on the join condition. Otherwise,
  7559.                                    the first table generates null values.
  7560.  
  7561.  ────────────────────────────────────────────────────────────────────────────
  7562.  Symbol                            Meaning
  7563.  ────────────────────────────────────────────────────────────────────────────
  7564. ────────────────────────────────────────────────────────────────────────────
  7565.  
  7566.  
  7567.  
  7568.  
  7569.  Comments
  7570.  
  7571.  If a join is to have meaningful results, the columns being joined must have
  7572.  similar values─values drawn from the same domain.
  7573.  
  7574.  You can use more than one join operator either to join more than two tables
  7575.  or to join more than two pairs of columns. (See examples D and E.) These
  7576.  "join expressions" are almost always connected with AND, although OR is also
  7577.  legal.
  7578.  
  7579.  The expression NOT column_name = column_name is equivalent to column_name !=
  7580.  column_name.
  7581.  
  7582.  Joins based on equality (=) are called equijoins. A natural join is an
  7583.  equijoin with only one of the matching columns displayed in the results.
  7584.  
  7585.  Joins that compare values within the same column of one table are called
  7586.  self-joins. To distinguish the two roles in which the table appears, use
  7587.  aliases. See example F.
  7588.  
  7589.  Joins based on a comparison of scalar values (=, > , >= , < , <= , !=, !<,
  7590.  !>) are called theta joins.
  7591.  
  7592.  The "not equal join" is meaningful only with a self-join. See example G.
  7593.  Otherwise, it is rarely used, except by mistake. For example, it would be
  7594.  easy to think you could find the authors that do not live in a city in which
  7595.  a publisher is located with a not equal join:
  7596.  
  7597.    /* INCORRECT STATEMENT */
  7598.     select distinct au_lname, authors.city
  7599.     from publishers, authors
  7600.     where publishers.city != authors.city
  7601.  
  7602.  However, this query actually finds the authors who live in a state where
  7603.  some publisher is not located, which is all of them. The correct SQL
  7604.  statement is as follows:
  7605.  
  7606.    select distinct au_lname, authors.city
  7607.     from publishers, authors
  7608.     where authors.city not in
  7609.     (select city from publishers
  7610.     where authors.city = publishers.city)
  7611.  
  7612.  Joins that include all rows regardless of whether there is a matching row
  7613.  (*= and =*) are called outer joins. See example B. If a table is an inner
  7614.  member of an outer join (the second table is the inner table if the join
  7615.  operator is *= ; the first table is the inner table if the join operator is
  7616.  =*), you can't place any restrictions on it except outer join restrictions.
  7617.  
  7618.  
  7619.  Null values in tables or views being joined will never match each other.
  7620.  
  7621.  Joins can also be stated as subqueries. (See the section on "Subqueries" for
  7622.  more information.)
  7623.  
  7624.  Joins may not be used for columns containing text or image values.
  7625.  
  7626.  
  7627.  See Also
  7628.  
  7629.  DELETE, INSERT, SELECT, Subqueries, WHERE Clause, UPDATE, sp_helpjoins
  7630.  
  7631.  
  7632.  KILL
  7633.  ────────────────────────────────────────────────────────────────────────────
  7634.  
  7635.  
  7636.  Function
  7637.  
  7638.  Kills a process.
  7639.  
  7640.  
  7641.  Syntax
  7642.  
  7643.    KILL spid
  7644.  
  7645.  
  7646.  Examples
  7647.  
  7648.    kill 1378
  7649.  
  7650.  
  7651.  Options
  7652.  
  7653.  spid
  7654.    The identification number of the process you want to kill, stored in
  7655.    sysprocesses.spid. The sysprocesses table also stores other process
  7656.    information, like status (runnable, waiting for a lock, infected, and so
  7657.    on), the uid (the ID of the user who executed the statement), program_name
  7658.    (the name of the application program), and so on.
  7659.  
  7660.  
  7661.  Comments
  7662.  
  7663.  You can get a report on the current processes with the sp_who system
  7664.  procedure. Here's a typical report:
  7665.  
  7666. ╓┌───────┌───────────┌───────────┌───────────┌───────────┌───────┌───────┌───
  7667.  ─────────────────────────────────────────────────────────────────────────────
  7668.          spid        status      login_id    hostname    blk     dbname  cmd
  7669.  
  7670.          ----        ---------   --------    ---------   ---     -----   -----
  7671.  
  7672.          1           sleeping    sa          PC Client   0       pubs    AWAIT
  7673.  
  7674.          2           sleeping    sa                      0       master  NETWO
  7675.  
  7676.          3           sleeping    sa                      0       master  CHECK
  7677.  
  7678.          4           sleeping    karenp      PC Client   1       pubs    SELEC
  7679.  
  7680.          5           runnable    karenp      PC Client   0       pubs    SELEC
  7681.  
  7682.          (5 rows
  7683.          affected)
  7684.  
  7685.  
  7686.  
  7687.  The spid column contains process identification numbers used in the
  7688.  TRANSACT-SQL KILL statement. The blk column contains the process ID of a
  7689.  blocking process, if there is one. A blocking process (which may have an
  7690.  exclusive lock) is one that is holding resources that another process needs.
  7691.  In the earlier example, process 4 (a SELECT on a table) is blocked by
  7692.  process 1 (a BEGIN TRANSACTION followed by an INSERT on the same table).
  7693.  
  7694.  Server processes cannot be killed. If you try to kill a server process, it
  7695.  is not killed and is still listed when you execute the sp_who system
  7696.  procedure.
  7697.  
  7698.  Sleeping processes that are killed don't die until they wake up, so you may
  7699.  still see them listed after you execute the KILL statement.
  7700.  
  7701.  You can get a report on the current locks and the spids of the processes
  7702.  holding them with the sp_lock system procedure.
  7703.  
  7704. ╓┌─┌──────┌─────────────┌────────────┌──────┌────────────────────────────────╖
  7705.  ────────────────────────────────────────────────────────────────────────────
  7706.    spid   locktype      table_id     page   dbname
  7707.    -----  ------------  -----------  -----  --------
  7708.    1      Sh_intent     16003088     0      master
  7709.  ────────────────────────────────────────────────────────────────────────────
  7710.   1      Sh_intent     16003088     0      master
  7711.    4      Ex_extent     0            440    pubs
  7712.    4      Ex_extent     0            504    pubs
  7713.    4      Sh_table      112003430    0      pubs
  7714.    4      Ex_table      240003886    0      pubs
  7715.  
  7716.  
  7717.  
  7718.  Permissions
  7719.  
  7720.  KILL permission defaults to the System Administrator and is not
  7721.  transferable.
  7722.  
  7723.  
  7724.  See Also
  7725.  
  7726.  sp_lock, sp_who
  7727.  
  7728.  
  7729.  LOAD DATABASE
  7730.  ────────────────────────────────────────────────────────────────────────────
  7731.  
  7732.  
  7733.  Function
  7734.  
  7735.  Loads a backup copy of a user database and its transaction log that was
  7736.  created with DUMP DATABASE.
  7737.  
  7738.  
  7739.  Syntax
  7740.  
  7741.    LOAD DATABASE database_name
  7742.      FROM dump_device
  7743.  
  7744.  
  7745.  Examples
  7746.  
  7747.    load database pubs
  7748.     from diskdump1
  7749.  
  7750.  The pubs database has been reloaded from the diskdump1 device.
  7751.  
  7752.  
  7753.  Options
  7754.  
  7755.  database_name
  7756.    The name of the database that has been created to receive data from a
  7757.    dumped backup copy. It can be either a newly created database with no data
  7758.    or an existing database. Loading dumped data to an existing database
  7759.    replaces existing data with the loaded data. The database into which you
  7760.    load a dumped database must be large enough to hold the dumped data.
  7761.  
  7762.  dump_device
  7763.    The logical name of the dump device from which you are loading the
  7764.    database. Logical dump device names are available in
  7765.    master..sysdevices.name or with the sp_helpdevice system procedure.
  7766.  
  7767.  
  7768.  Comments
  7769.  
  7770.  The specified database must not be in use during the load. Any data in the
  7771.  specified database is replaced by the loaded data.
  7772.  
  7773.  When the LOAD DATABASE statement is executed for loading from a diskette,
  7774.  the console program prompts the operator and waits for responses. If the
  7775.  console program is not running or if the operator does not answer its
  7776.  questions, the load cannot proceed. For more information, see Chapter 3,
  7777.  "Utility Programs."
  7778.  
  7779.  The recipient database must be large enough for dumped data: it must be
  7780.  large enough to hold the highest logical page that was used by the dumped
  7781.  database. If the recipient database is too small, SQL Server displays an
  7782.  error message that gives the required size.
  7783.  
  7784.  To recover a database, load the most recent database dump and load all the
  7785.  transaction log dumps made since that database dump. The database into which
  7786.  you are loading the data is replaced by this reconstruction.
  7787.  
  7788.  Loading a master database that has been damaged is done with a different
  7789.  procedure. See "DISK REFIT," "DISK REINIT," or the SQL Server System
  7790.  Administator's Guide for details.
  7791.  
  7792.  For more information on SQL Server's backup and recovery facilities, see the
  7793.  SQL Server System Administrator's Guide.
  7794.  
  7795.  
  7796.  Permissions
  7797.  
  7798.  LOAD DATABASE permission defaults to the Database Owner and is not
  7799.  transferable.
  7800.  
  7801.  
  7802.  See Also
  7803.  
  7804.  DBCC, DUMP DATABASE, DUMP TRANSACTION, LOAD TRANSACTION, sp_helpdevice
  7805.  
  7806.  
  7807.  LOAD TRANSACTION
  7808.  ────────────────────────────────────────────────────────────────────────────
  7809.  
  7810.  
  7811.  Function
  7812.  
  7813.  Loads a backup copy of the transaction log. The DUMP TRANSACTION and LOAD
  7814.  TRANSACTION statements should be used only when the transaction log is
  7815.  stored on a different dump device than the rest of the database.
  7816.  
  7817.  
  7818.  Syntax
  7819.  
  7820.    LOAD TRANsaction database_name
  7821.      FROM dump_device
  7822.  
  7823.  
  7824.  Examples
  7825.  
  7826.    load transaction pubs
  7827.     from diskdump1
  7828.  
  7829.     The transaction log for the pubs database,
  7830.     previously dumped to diskdump1, has been reloaded.
  7831.  
  7832.  
  7833.  Options
  7834.  
  7835.  database_name
  7836.    The name of the destination database.
  7837.  
  7838.  dump_device
  7839.    The logical name of the dump device from which the transaction log is
  7840.    being loaded. Logical dump device names are available in
  7841.    master..sysdevices.name or with the sp_helpdevice system procedure.
  7842.  
  7843.  
  7844.  Comments
  7845.  
  7846.  LOAD TRANSACTION loads a backup copy of the transaction log made with DUMP
  7847.  TRANSACTION.
  7848.  
  7849.  To recover a database, load the most recent database dump and then load (in
  7850.  order) all the transaction log dumps made since the database dump. The
  7851.  database is restored to its state at the time of the last transaction log
  7852.  dump.
  7853.  
  7854.  When the LOAD TRANSACTION statement is executed for loading from a diskette,
  7855.  the console program prompts the operator and waits for a response. If the
  7856.  console program is not running or if the operator does not answer its
  7857.  questions, the load cannot proceed. For more information, see Chapter 3,
  7858.  "Utility Programs."
  7859.  
  7860.  SQL Server checks timestamps on the dumped transaction log to make sure that
  7861.  the transactions are being loaded into the correct database and in the
  7862.  correct sequence.
  7863.  
  7864.  Do not attempt to modify syslogs with a DELETE, UPDATE, or INSERT statement.
  7865.  Doing so makes it impossible for SQL Server to recover correctly in case of
  7866.  a system failure. In addition, an attempt to delete all rows from syslogs
  7867.  causes SQL Server to get into an infinite loop that eventually fills up the
  7868.  entire database.
  7869.  
  7870.  For more information on SQL Server's backup and recovery facilities, see the
  7871.  SQL Server System Administrator's Guide.
  7872.  
  7873.  
  7874.  Permissions
  7875.  
  7876.  LOAD DATABASE permission defaults to the Database Owner and is not
  7877.  transferable.
  7878.  
  7879.  
  7880.  See Also
  7881.  
  7882.  DUMP DATABASE, DUMP TRANSACTION, LOAD DATABASE, sp_helpdevice
  7883.  
  7884.  
  7885.  Mathematical Functions
  7886.  ────────────────────────────────────────────────────────────────────────────
  7887.  
  7888.  
  7889.  Function
  7890.  
  7891.  Return values commonly needed for operations on mathematical data.
  7892.  Mathematical function names are not keywords.
  7893.  
  7894.  
  7895.  Syntax
  7896.  
  7897.  Mathematical functions have the following syntax:
  7898.  
  7899.    function_name(parameters)
  7900.  
  7901.  Function names, parameters, and results are listed in the following table:
  7902.  
  7903.  
  7904.  
  7905.  
  7906. ╓┌─────────┌────────────────────────────────┌────────────────────────────────╖
  7907.  Function  Parameters                       Result
  7908.  ────────────────────────────────────────────────────────────────────────────
  7909.  ABS       (numeric_expr)                   An absolute value of a given
  7910.                                             expression. The expression can
  7911.                                             be of integer, float, or money
  7912.                                             type. Results are of the same
  7913.                                             type as the numeric expression.
  7914.  
  7915.  ACOS      (float_expr)                     An angle (in radians) whose
  7916.                                             cosine is the specified
  7917.                                             floating-point value.
  7918.  
  7919.  Function  Parameters                       Result
  7920.  ────────────────────────────────────────────────────────────────────────────
  7921. 
  7922.  ASIN      (float_expr)                     An angle (in radians) whose
  7923.                                             sine is the specified
  7924.                                             floating-point value.
  7925.  
  7926.  ATAN      (float_expr)                     An angle (in radians) whose
  7927.                                             tangent is the specified
  7928.                                             floating-point value.
  7929.  
  7930.  ATN2      (float_expr1, float_expr2)       An angle (in radians) whose
  7931.                                             tangent is (
  7932.                                             float_expr1/float_expr2).
  7933.  
  7934.  CEILING   (numeric_expr)                   The smallest integer greater
  7935.                                             than or equal to the specified
  7936.                                             value. The expression can be of
  7937.                                             integer, float, or money type.
  7938.                                             Results are of the same type as
  7939.                                             the numeric expression.
  7940.  Function  Parameters                       Result
  7941.  ────────────────────────────────────────────────────────────────────────────
  7942.                                            the numeric expression.
  7943.  
  7944.  COS       (float_expr)                     The trigonometric cosine of the
  7945.                                             specified angle (in radians).
  7946.  
  7947.  COT       (float_expr)                     The trigonometric cotangent of
  7948.                                             the specified angle (in
  7949.                                             radians).
  7950.  
  7951.  DEGREES   (numeric_expr)                   Degrees converted from radians.
  7952.                                             The expression can be of
  7953.                                             integer, float, or money type.
  7954.                                             Results are of the same type as
  7955.                                             the numeric expression.
  7956.  
  7957.  EXP       (float_expr)                     The exponential value of the
  7958.                                             specified value.
  7959.  
  7960.  FLOOR     (numeric_expr)                   The largest integer less than
  7961.  Function  Parameters                       Result
  7962.  ────────────────────────────────────────────────────────────────────────────
  7963. FLOOR     (numeric_expr)                   The largest integer less than
  7964.                                             or equal to the specified value.
  7965.                                             The expression can be of
  7966.                                             integer, float, or money type.
  7967.                                             Results are of the same type as
  7968.                                             the numeric expression.
  7969.  
  7970.  LOG       (float_expr)                     The natural logarithm of the
  7971.                                             specified value.
  7972.  
  7973.  LOG10     (float_expr)                     The base 10 logarithm of the
  7974.                                             specified value.
  7975.  
  7976.  PI        (  )                             The constant value of
  7977.                                             3.1415926535897936.
  7978.  
  7979.  POWER     (numeric_expr, y)                The value of numeric_expr to
  7980.                                             the power of y. The expression
  7981.                                             and y can be of integer, float,
  7982.  Function  Parameters                       Result
  7983.  ────────────────────────────────────────────────────────────────────────────
  7984.                                            and y can be of integer, float,
  7985.                                             or money type. Results are of
  7986.                                             the same type as numeric_expr.
  7987.  
  7988.  RADIANS   (numeric_expr)                   Radians converted from degrees.
  7989.                                             The expression can be of
  7990.                                             integer, float, or money type.
  7991.                                             Results are of the same type as
  7992.                                             numeric_expr.
  7993.  
  7994.  RAND      ([int_expr])                     A random float number using
  7995.                                             int_expr as the optional seed.
  7996.  
  7997.  ROUND     (numeric_expr, int_expr)         A numeric expression rounded
  7998.                                             off to the precision specified
  7999.                                             in int_expr. The expression can
  8000.                                             be of integer, float, or money
  8001.                                             type. Results are of the same
  8002.                                             type as numeric_expr.
  8003.  Function  Parameters                       Result
  8004.  ────────────────────────────────────────────────────────────────────────────
  8005.                                            type as numeric_expr.
  8006.  
  8007.  SIGN      (numeric_expr)                   Positive (+1), zero (0), or
  8008.                                             negative (-1). The expression
  8009.                                             can be of integer, float, or
  8010.                                             money type. Results are of the
  8011.                                             same type as numeric_expr.
  8012.  
  8013.  SIN       (float_expr)                     The trigonometric sine of the
  8014.                                             specified angle (measured in
  8015.                                             radians).
  8016.  
  8017.  SQRT      (float_expr)                     The square root of the
  8018.                                             specified value.
  8019.  
  8020.  TAN       (float_expr)                     The trigonometric tangent of
  8021.                                             the specified angle (measured
  8022.                                             in radians).
  8023.  
  8024.  Function  Parameters                       Result
  8025.  ────────────────────────────────────────────────────────────────────────────
  8026. 
  8027.  ────────────────────────────────────────────────────────────────────────────
  8028.  
  8029.  
  8030.  
  8031.  
  8032.  Examples
  8033.  
  8034.    Statement     Result
  8035.  
  8036.    A. select floor(123.45)  123.000000
  8037.     select floor(-123.45)  -124.000000
  8038.     select floor($123.45)  $123.00
  8039.  
  8040.    B. select ceiling(123.45)  124.000000
  8041.     select ceiling(-123.45)  -123.000000
  8042.     select ceiling($123.45)  $124.00
  8043.  
  8044.    C. select round(123.4545, 2) 123.450000
  8045.     select round(123.45, -2) 100.000000
  8046.  
  8047.  The ROUND function always returns a value even if the length is illegal. If
  8048.  the specified length is positive and longer than the digits after the
  8049.  decimal point, 0 is added after the fraction digits.
  8050.  
  8051.  If the length is negative and larger than or equal to the digits before the
  8052.  decimal point, ROUND returns 0.00.
  8053.  
  8054.  The last digit in a floating-point number is always an estimate:
  8055.  
  8056.    round(123.9995, 3) = 123.999
  8057.  
  8058.  while
  8059.  
  8060.    round(123.9996, 3) = 124.000
  8061.  
  8062.  
  8063.  Comments
  8064.  
  8065.  Error traps are provided to handle domain or range errors of these
  8066.  functions. A user can set the options ARITHABORT or ARITHIGNORE, which
  8067.  respectively abort the query or return NULL when a domain error occurs. No
  8068.  warning message is displayed. If neither of these options is set, the system
  8069.  returns NULL and prints a warning message after the query is executed.
  8070.  
  8071.  
  8072.  See Also
  8073.  
  8074.  Conversion Function, Functions, SET, String Functions, Text/Image Functions
  8075.  
  8076.  
  8077.  
  8078.  
  8079.  
  8080.  
  8081.  Null Values
  8082.  ────────────────────────────────────────────────────────────────────────────
  8083.  
  8084.  
  8085.  Function
  8086.  
  8087.  Mark columns having an unknown value (as opposed to those that have 0 or a
  8088.  blank as a value). NULL allows you to distinguish between a deliberate entry
  8089.  of zero (for numerical columns) or a blank (for character columns) and a
  8090.  nonentry (NULL for both numerical and character columns).
  8091.  
  8092.  
  8093.  Syntax
  8094.  
  8095.  In CREATE TABLE statements:
  8096.  
  8097.    column_name datatype [NULL | NOT NULL]
  8098.  
  8099.  In SELECT statements:
  8100.  
  8101.    WHERE column_name IS [NOT] NULL
  8102.  
  8103.  In INSERT statements:
  8104.  
  8105.    VALUES({constant | NULL} [, {constant | NULL}]...)
  8106.  
  8107.  In UPDATE statements:
  8108.  
  8109.    SET column_name = {expression | NULL}
  8110.     [, column_name = {expression | NULL}...]
  8111.  
  8112.  The ISNULL built-in function:
  8113.  
  8114.    isnull(expression, value)
  8115.  
  8116.  
  8117.  Examples
  8118.  
  8119.    A. create table test
  8120.     (t1 char(10) null,
  8121.     t2 char(10) not null)
  8122.     insert test
  8123.     values (null, "stuff")
  8124.     insert test (t2)
  8125.     values ("stuff")
  8126.  
  8127.  Because column t1 of table test is defined as accepting NULL in the CREATE
  8128.  TABLE statement (example A), these two INSERT statements are equivalent to
  8129.  each other. The user can explicitly insert NULL or allow the system to do
  8130.  it. NULL is inserted in the t1 column whenever the user inserts rows but
  8131.  does not specify a value for the column.
  8132.  
  8133.    B. select isnull(t1, "unknown")
  8134.     from test
  8135.  
  8136.  Example B selects all the rows from test and displays all the null values in
  8137.  column t1 with the value unknown.
  8138.  
  8139.    C. update titles
  8140.     set advance = null
  8141.     where title_id = "TC3218"
  8142.  
  8143.    D. select title_id, advance
  8144.     from titles
  8145.     where advance < $5000 or advance is null
  8146.  
  8147.  
  8148.  Options
  8149.  
  8150.  NULL
  8151.    Indicates that the user (or a program) has made no entry. The value is
  8152.    unknown rather than blank or 0. In CREATE TABLE statements, NULL after a
  8153.    column name means that if there is no default for this column, SQL Server
  8154.    assigns a null value whenever the user does not make an entry for this
  8155.    column at insert time.
  8156.  
  8157.    In SELECT statements, use IS [NOT] NULL to retrieve null values (allowed
  8158.    only if the column has been defined to allow null values in the CREATE
  8159.    TABLE statement).
  8160.  
  8161.    An expression with a bitwise or arithmetic operator evaluates to NULL if
  8162.    any of the operands is null.
  8163.  
  8164.  NOT NULL
  8165.    Is the default in CREATE TABLE statements. It can also be used in SELECT
  8166.    statements to retrieve all values except those that are NULL.
  8167.  
  8168.  
  8169.  Comments
  8170.  
  8171.  Only columns for which NULL was specified in the CREATE TABLE statement, and
  8172.  into which you have explicitly entered NULL or into which no data has been
  8173.  entered contain null values. (Avoid entering the character string "NULL" as
  8174.  data for a character column. Use "N/A" or "none" instead. When you wish to
  8175.  enter the value NULL explicitly, do not use single or double quotation
  8176.  marks.)
  8177.  
  8178.  NOT NULL (the default) in a CREATE TABLE statement means that NULL is not a
  8179.  legal value for this column. If there is no default for this column, an
  8180.  error message will be produced whenever the user does not make an entry for
  8181.  this column at insert time. In addition, the user cannot assign NULL as a
  8182.  value with an INSERT or UPDATE statement.
  8183.  
  8184.  Columns added to an existing table with the ALTER TABLE statement (except
  8185.  bit columns, which cannot be added to an existing table) must allow NULL
  8186.  because the initial values of the new columns in existing rows are set to
  8187.  NULL.
  8188.  
  8189.  When you create NULL columns with certain datatypes, SQL Server
  8190.  automatically converts them to a different internal datatype to allow the
  8191.  storage of null values. SQL Server does not inform the user of the type
  8192.  change, and the user should be concerned about it only if querying the
  8193.  system tables.
  8194.  
  8195.  The char datatype is automatically converted to varchar; binary to
  8196.  varbinary; datetime to datetimn; float to floatn; int, smallint, and tinyint
  8197.  to intn; and money to moneyn.
  8198.  
  8199.  An expression with an arithmetic or bitwise operator evaluates to NULL if
  8200.  any of the operands is NULL. For example, 1 + column1 evaluates to NULL if
  8201.  column1 is NULL.
  8202.  
  8203.  An expression with a comparison operator evaluates to false if any of the
  8204.  operands is NULL. This means that null values will never match another value
  8205.  (not even another NULL) when used with a comparison operator. Use IS NULL to
  8206.  find null values in queries (when the columns being searched are defined as
  8207.  allowing null values). If you try to find null values in columns defined as
  8208.  NOT NULL, SQL Server generates an error message.
  8209.  
  8210.  The result of a subquery that returns no values is NULL.
  8211.  
  8212.  The result of a divide-by-zero is NULL.
  8213.  
  8214.  Aggregate functions ignore null values, except COUNT(*), which includes
  8215.  them. For example, in the calculation avg(advance), null values in the
  8216.  advance column are not counted, either in calculating the total amount or
  8217.  the number of values.
  8218.  
  8219.  When the GROUP BY clause is used, null values form their own group.
  8220.  
  8221.  With ORDER BY, null values come before all others.
  8222.  
  8223.  In a SELECT clause with the DISTINCT keyword, which selects unique rows
  8224.  only, null values are considered duplicates of each other. Only one NULL is
  8225.  selected, no matter how many are encountered.
  8226.  
  8227.  Null values are never joined, not even to other null values.
  8228.  
  8229.  If you have two tables identical in structure except that one has NULL
  8230.  fields and some null values, while the other has NOT NULL fields, this
  8231.  difference makes it impossible to insert the data from the NULL table into
  8232.  the NOT NULL table with SELECT.
  8233.  
  8234.  If a subquery returns NULL, the statement results will not be what you
  8235.  expect.
  8236.  
  8237.  If you specify NOT NULL when you create a column and do not create a default
  8238.  for it, an error message is produced whenever a user fails to make an entry
  8239.  in that column. The following table illustrates the relationship between the
  8240.  existence of a default and the definition of a column as NULL or NOT NULL.
  8241.  The entries in the table show the result:
  8242.  
  8243.            No entry
  8244.  ────────────────────────────────────────────────────────────────────────────
  8245.  NULL      null      default  null   null
  8246.  
  8247.  NOT NULL  error     default  error  error
  8248.  
  8249.  ────────────────────────────────────────────────────────────────────────────
  8250.  
  8251.  
  8252.  For information on NULL as the default parameter for a procedure, see
  8253.  "CREATE PROCEDURE."
  8254.  
  8255.  The sp_help system procedure reports the nulltype of a column (whether or
  8256.  not the column accepts null values).
  8257.  
  8258.  
  8259.  See Also
  8260.  
  8261.  Aggregate Functions, CREATE PROCEDURE, Expressions, GROUP BY and HAVING
  8262.  Clauses, INSERT, Search Conditions, SELECT, Subqueries, UPDATE, sp_help
  8263.  
  8264.  
  8265.  ORDER BY Clause
  8266.  ────────────────────────────────────────────────────────────────────────────
  8267.  
  8268.  
  8269.  Function
  8270.  
  8271.  Returns query results in the specified column(s) in sorted order.
  8272.  
  8273.  
  8274.  Syntax
  8275.  
  8276.    [ORDER BY {[table_name. | view_name.] column_name |
  8277.     select_list_number | expression} [ASC | DESC]
  8278.     [,{[table_name. | view_name.] column_name |
  8279.     select_list_number | expression} [ASC | DESC]]...]
  8280.  
  8281.  
  8282.  Examples
  8283.  
  8284.    A. select title, type, price
  8285.     from titles
  8286.     where price > $9.99
  8287.     order by title
  8288.  
  8289.    B. select type, price, advance
  8290.     from titles
  8291.     order by type desc
  8292.     compute avg(price), avg(advance) by type
  8293.  
  8294.  
  8295.  Options
  8296.  
  8297.  ORDER BY
  8298.    Sorts the results by columns. You can sort up to 16 columns. In
  8299.    TRANSACT-SQL, you can use this statement for items that do not appear in
  8300.    the select list. You can sort by a column heading, a column name, an
  8301.    expression, or a number representing the position of the item in the
  8302.    select list (the select_list_number).
  8303.  
  8304.  ASC
  8305.    Indicates that results are to be sorted in ascending order. ASC is the
  8306.    default; if you do not specify ASC or DESC, ASC is assumed.
  8307.  
  8308.  DESC
  8309.    Indicates that results are to be sorted in descending order.
  8310.  
  8311.  
  8312.  Comments
  8313.  
  8314.  ORDER BY cannot be specified on text or image datatype columns.
  8315.  
  8316.  With ORDER BY, null values come before all others.
  8317.  
  8318.  If you use COMPUTE BY, you must also use an ORDER BY clause. The columns
  8319.  listed after COMPUTE BY must be identical to or a subset of those listed
  8320.  after ORDER BY, and must be in the same left-to-right order, start with the
  8321.  same expression, and not skip any expressions. For example, say the ORDER BY
  8322.  clause is
  8323.  
  8324.    order by a, b, c
  8325.  
  8326.  In that case, the COMPUTE BY clause can be any (or all) of these:
  8327.  
  8328.    compute by a, b, c
  8329.     compute by a, b
  8330.     compute by a
  8331.  
  8332.  You can use a column name, an expression, or a column heading (or alias) in
  8333.  the ORDER BY clause.
  8334.  
  8335.  The COMPUTE keyword can be used without BY to generate grand totals, grand
  8336.  counts, and so on. ORDER BY is optional if you use the COMPUTE keyword
  8337.  without BY.
  8338.  
  8339.  Subqueries cannot include an ORDER BY clause, a COMPUTE clause, or the INTO
  8340.  keyword.
  8341.  
  8342.  View definitions cannot include an ORDER BY clause, a COMPUTE CLAUSE, or the
  8343.  INTO keyword.
  8344.  
  8345.  The ORDER BY clause cannot be specified on columns containing text or image
  8346.  datatypes.
  8347.  
  8348.  
  8349.  See Also
  8350.  
  8351.  COMPUTE Clause, Expressions, GROUP BY and HAVING Clauses, SELECT, WHERE
  8352.  Clause
  8353.  
  8354.  
  8355.  Parameters
  8356.  ────────────────────────────────────────────────────────────────────────────
  8357.  
  8358.  
  8359.  Function
  8360.  
  8361.  Values supplied to a stored procedure. Parameters are defined (optionally)
  8362.  when a stored procedure is created, and their values are supplied by the
  8363.  user when a procedure is executed.
  8364.  
  8365.  
  8366.  Syntax
  8367.  
  8368.    CREATE PROCEDURE [[database.]owner]procedure_name[;number]
  8369.      [[(]@parameter_name datatype [= default]
  8370.      [,@parameter_name datatype [= default]]...[)]]
  8371.     [WITH RECOMPILE]
  8372.     AS SQL_statements
  8373.  
  8374.     [EXECute] procedure_name [value [, value]...] or
  8375.  
  8376.     [EXECute] procedure_name [@parameter_name = value
  8377.      [,@parameter_name = value]...]
  8378.  
  8379.  
  8380.  Examples
  8381.  
  8382.    create procedure showtype @tabname varchar(18), @colname varchar(18)
  8383.     as
  8384.     select syscolumns.name, syscolumns.length, systypes.name
  8385.     from syscolumns, systypes, sysobjects
  8386.     where sysobjects.id = syscolumns.id
  8387.      and @tabname = sysobjects.name
  8388.      and @colname = syscolumns.name
  8389.      and syscolumns.type = systypes.type
  8390.  
  8391.     exec showtype sales, qty
  8392.  
  8393.  
  8394.  Options
  8395.  
  8396.  @parameter_name
  8397.    The name of the parameter or parameter to the stored procedure. Parameter
  8398.    names must be preceded by the "at" symbol (@) and conform to the rules for
  8399.    identifiers.
  8400.  
  8401.  datatype
  8402.    The datatype of the parameter. A length in parentheses after the datatype
  8403.    entry is required for some datatypes. See the "Datatypes" section for a
  8404.    list of SQL-Server-supplied datatypes and their syntax.
  8405.  
  8406.  default
  8407.    A default parameter value for the procedure. If a default is defined, a
  8408.    user can execute the procedure without giving a parameter. The default
  8409.    must be a constant. It can include the wildcard characters (%, _, [ ], and
  8410.    [^]) if the procedure uses the parameter name with the LIKE keyword.
  8411.  
  8412.    The default can be NULL. The procedure definition can specify that some
  8413.    action be taken if the parameter value is NULL. See "CREATE PROCEDURE" for
  8414.    examples.
  8415.  
  8416.  
  8417.  Comments
  8418.  
  8419.  Parameter names are optional in CREATE PROCEDURE statements; a procedure
  8420.  need not take any parameters.
  8421.  
  8422.  Parameter names are local to the procedure; that is, the same parameter
  8423.  names can be used in other procedures.
  8424.  
  8425.  Parameter names can take the place of constants only; they cannot be used in
  8426.  place of table names, column names, or the names of other database objects.
  8427.  However, the value of a parameter given at execution time can be an object
  8428.  name. See "EXECUTE" for examples.
  8429.  
  8430.  Enclose any parameter value that includes punctuation (such as an object
  8431.  name qualified by a database name or owner name) in single or double
  8432.  quotation marks.
  8433.  
  8434.  Parameters cannot be of text or image datatype.
  8435.  
  8436.  
  8437.  See Also
  8438.  
  8439.  CREATE PROCEDURE, DECLARE, Datatypes, DROP PROCEDURE, EXECUTE, Variables
  8440.  (Local and Global)
  8441.  
  8442.  
  8443.  PRINT
  8444.  ────────────────────────────────────────────────────────────────────────────
  8445.  
  8446.  
  8447.  Function
  8448.  
  8449.  Prints a user-defined message on the user's screen.
  8450.  
  8451.  
  8452.  Syntax
  8453.  
  8454.    PRINT {"any ASCII text" | local variable |
  8455.      @version global variable}
  8456.  
  8457.  
  8458.  Examples
  8459.  
  8460.    A. if exists (select zip from authors
  8461.     where zip = '94705')
  8462.     print "Berkeley author"
  8463.  
  8464.    B. declare @msg char(50)
  8465.     select @msg = "What's up, doc?"
  8466.     print @msg
  8467.  
  8468.     What's up, doc?
  8469.  
  8470.    C. declare @tabname varchar(30)
  8471.     select @tabname = "titles"
  8472.     declare @username varchar(30)
  8473.     select @username = "ezekiel"
  8474.     declare @message varchar(255)
  8475.     select @message = "The table '" + @tabname + "' is not
  8476.    owned
  8477.      by the user '" + @username + ".'"
  8478.     print @message
  8479.  
  8480.     The table 'titles' is not owned
  8481.     by the user 'ezekiel.'
  8482.  
  8483.  
  8484.  Comments
  8485.  
  8486.  The message can be up to 255 characters long.
  8487.  
  8488.  The local variable must be of type char or varchar and must be declared
  8489.  within the batch or procedure in which it is used.
  8490.  
  8491.  The global variable must be of type char or varchar or automatically
  8492.  convertible to these types, such as @VERSION.
  8493.  
  8494.  You can generate a "formatted" message with more than one parameter by using
  8495.  the concatenation operator, as shown in example C.
  8496.  
  8497.  
  8498.  See Also
  8499.  
  8500.  DECLARE, RAISERROR, Variables (Local and Global)
  8501.  
  8502.  
  8503.  RAISERROR
  8504.  ────────────────────────────────────────────────────────────────────────────
  8505.  
  8506.  
  8507.  Function
  8508.  
  8509.  Prints a user-defined error message on the user's screen and sets a system
  8510.  flag to record that an error has occurred.
  8511.  
  8512.  
  8513.  Syntax
  8514.  
  8515.    RAISERROR number {"text of message" | local variable}
  8516.  
  8517.  
  8518.  Examples
  8519.  
  8520.    create procedure showtable_sp @tabname varchar(18)
  8521.     as
  8522.     if @tabname is null
  8523.      begin
  8524.      raiserror 99999 "You must give a table name"
  8525.      end
  8526.     else
  8527.      begin
  8528.      select sysobjects.name, type, crdate, indid
  8529.      from sysindexes, sysobjects
  8530.      where sysobjects.name = @tabname
  8531.      and sysobjects.id = sysindexes.id
  8532.      end
  8533.  
  8534.  
  8535.  Comments
  8536.  
  8537.  The error message can be up to 255 characters long.
  8538.  
  8539.  Error numbers for user-defined error messages must be greater than 20,000.
  8540.  
  8541.  When RAISERROR is executed, the error number is placed in the global
  8542.  variable @ERROR, which stores the error number most recently generated by
  8543.  the system.
  8544.  
  8545.  The local variable must be of type char or varchar, and must be declared
  8546.  within the batch or procedure in which it is used.
  8547.  
  8548.  Use RAISERROR instead of PRINT when you want an error number stored in
  8549.  @ERROR.
  8550.  
  8551.  
  8552.  See Also
  8553.  
  8554.  DECLARE, PRINT, Variables (Local and Global)
  8555.  
  8556.  
  8557.  READTEXT
  8558.  ────────────────────────────────────────────────────────────────────────────
  8559.  
  8560.  
  8561.  Function
  8562.  
  8563.  Reads text and image values, starting from a specified offset and reading a
  8564.  specified number of bytes.
  8565.  
  8566.  
  8567.  Syntax
  8568.  
  8569.    READTEXT [[database.]owner.]table_name.column_name
  8570.      text_ptr offset size [HOLDLOCK]
  8571.  
  8572.  
  8573.  Examples
  8574.  
  8575.    declare @val varbinary(30)
  8576.     select @val = textptr(blurb) from texttest
  8577.     where title_id = "BU7832"
  8578.     readtext texttest.blurb @val 1 5
  8579.  
  8580.  This example selects the second through sixth characters of the blurb column
  8581.  from the texttest table.
  8582.  
  8583.  
  8584.  Options
  8585.  
  8586.  table_name.column_name
  8587.    The table name and column name. The table name must be included, but the
  8588.    database name and owner name are optional.
  8589.  
  8590.  text_ptr
  8591.    A valid text pointer.
  8592.  
  8593.  offset
  8594.    The number of bytes to skip before starting to read the text or image
  8595.    data.
  8596.  
  8597.  size
  8598.    The number of bytes of data to read. If size = 0, 4K bytes of data are
  8599.    read.
  8600.  
  8601.  HOLDLOCK
  8602.    Causes the text value to be locked for reads until the end of the
  8603.    transaction. Other users can read the value, but they cannot modify it.
  8604.  
  8605.  
  8606.  Comments
  8607.  
  8608.  Since the TEXTPTR function returns a long binary string, it is best to
  8609.  declare a local variable to hold the text pointer and then use the variable
  8610.  with READTEXT.
  8611.  
  8612.  The HOLDLOCK option causes the text value to be locked for reads until the
  8613.  end of the transaction. Other users can read the value, but they cannot
  8614.  modify it.
  8615.  
  8616.  The TEXTPTR function returns a pointer to the text or image column in the
  8617.  specified row or to the text or image column in the last row returned by the
  8618.  query if more than one row is returned.
  8619.  
  8620.  The value in the global variable @TEXTSIZE, which is the limit on the number
  8621.  of bytes of data to be returned, supersedes the size specified for READTEXT
  8622.  if it is less than the specified size for READTEXT. Use SET TEXTSIZE to
  8623.  change this limit.
  8624.  
  8625.  
  8626.  Permissions
  8627.  
  8628.  Users must have select permission on the table to use READTEXT.
  8629.  
  8630.  
  8631.  See Also
  8632.  
  8633.  Text/Image Datatypes, Text/Image Functions, WRITETEXT
  8634.  
  8635.  
  8636.  RECONFIGURE
  8637.  ────────────────────────────────────────────────────────────────────────────
  8638.  
  8639.  
  8640.  Function
  8641.  
  8642.  Part of the procedure that sets configuration options, which control various
  8643.  aspects of SQL Server's memory allocation and performance. The System
  8644.  Administrator uses RECONFIGURE.
  8645.  
  8646.  
  8647.  Syntax
  8648.  
  8649.    RECONFIGURE [WITH OVERRIDE]
  8650.  
  8651.  
  8652.  Examples
  8653.  
  8654.    reconfigure
  8655.  
  8656.  The dynamic values in the master..sysconfigures system table are put into
  8657.  effect immediately. The static values take effect the next time SQL Server
  8658.  is started.
  8659.  
  8660.  
  8661.  Options
  8662.  
  8663.  WITH OVERRIDE
  8664.    Instructs SQL Server to accept the values for system variables supplied by
  8665.    the user (with sp_configure). User-supplied values that are unacceptable
  8666.    to SQL Server cause the RECONFIGURE statement to abort.
  8667.  
  8668.    If the user supplies values that SQL Server cannot accept, an error
  8669.    message telling the user to re-execute sp_configure is displayed.
  8670.  
  8671.    The WITH OVERRIDE clause must always be used when you set the allow
  8672.    updates variable to 1.
  8673.  
  8674.  
  8675.  Comments
  8676.  
  8677.  To display a list of the configuration options with their current values and
  8678.  the range of permissible values, execute sp_configure without parameters.
  8679.  The report looks like this:
  8680.  
  8681. ╓┌───────┌─────────────────────┌────────┌────────┌─────────────────────┌─────
  8682.  ─────────────────────────────────────────────────────────────────────────────
  8683.          name                  minimum  maximum  config_value          run_val
  8684.  
  8685.          -----------------     -------  -------  ------------          -------
  8686.  
  8687.          recovery interval     1        32767    4                     3
  8688.  
  8689.          allow updates         0        1        0                     0
  8690.  
  8691.          user connections *    5        35       0                     25
  8692.  
  8693.          memory                1000     14000    0                     1700
  8694.  
  8695.          open databases        5        100      0                     10
  8696.  ─────────────────────────────────────────────────────────────────────────────
  8697.         open databases        5        100      0                     10
  8698.  
  8699.          locks                 5000     50000    0                     5000
  8700.  
  8701.          open objects          100      10000    0                     500
  8702.  
  8703.          procedure cache       1        99       0                     20
  8704.  
  8705.          fill factor           0        100      0                     0
  8706.  
  8707.          time slice            50       1000     0                     100
  8708.  
  8709.          database size         2        10000    0                     2
  8710.  
  8711.          media retention       0        365      0                     0
  8712.  
  8713.          recovery flags        0        1        0                     0
  8714.  
  8715.          serial number         1        999999   0                     0
  8716.  
  8717.  ─────────────────────────────────────────────────────────────────────────────
  8718. 
  8719.          (14 rows affected)
  8720.  
  8721.          * The SQL Server
  8722.          maximum is 250; the
  8723.          effective number is
  8724.          based upon your
  8725.          system configuration
  8726.          and is stored in the
  8727.          global variable
  8728.          @MAX_CONNECTIONS.
  8729.  
  8730.  
  8731.  
  8732.  The config_value column of the report contains the value to which the
  8733.  configuration option was set with sp_configure (the value in
  8734.  sysconfigures.value).
  8735.  
  8736.  The run_value column contains the value SQL Server is using. This value
  8737.  changes for dynamic configuration options (recovery interval and allow
  8738.  updates) after you run the RECONFIGURE statement. This value changes for
  8739.  static configuration options after you shut down and restart SQL Server.
  8740.  This is the value in syscurconfigs.value.
  8741.  
  8742.  To change a configuration option, execute sp_configure with its parameters
  8743.  optname (the name of the configuration option) and optvalue (a legal value).
  8744.  Then execute the RECONFIGURE statement. (Include the WITH OVERRIDE clause to
  8745.  turn the allow updates variable on or to override a warning that the value
  8746.  you have specified is considered less than optimal by SQL Server.)
  8747.  
  8748.  If you have changed any of the static configuration options (all of them
  8749.  except allow updates and recovery interval), you must restart SQL Server
  8750.  before the new values take effect.
  8751.  
  8752.  SQL Server checks to make sure that the new values in sysconfigures are
  8753.  acceptable (SQL Server will run) and reasonable (SQL Server will run
  8754.  optimally).
  8755.  
  8756.  The values in sysconfigures.value and in the config_value column of the
  8757.  sp_configure report do not necessarily reflect the values currently being
  8758.  used by SQL Server for two reasons. First, the System Administrator may have
  8759.  updated sysconfigures.value but not executed the RECONFIGURE statement
  8760.  and/or not restarted SQL Server. Second, the default for most values in
  8761.  sysconfigures is 0, which indicates that SQL Server calculates optimum
  8762.  values for that parameter.
  8763.  
  8764.  Here are the contents of sysconfigures:
  8765.  
  8766. ╓┌───────┌───────┌──────┌────────────────────────────────────────────────────
  8767.  ─────────────────────────────────────────────────────────────────────────────
  8768.          config  value  comment
  8769.  
  8770.          ------  -----  ------------------------------------------------------
  8771.  
  8772.          101     4      Maximum recovery interval in minutes
  8773.  
  8774.          102     0      Allow updates to system tables
  8775.  
  8776.          103     0      Number of user connections allowed
  8777.  
  8778.          104     0      Size of available physical memory in 2K pages
  8779.  
  8780.  ─────────────────────────────────────────────────────────────────────────────
  8781. 
  8782.          105     0      Number of open databases allowed among all users
  8783.  
  8784.          106     0      Number of locks for all users
  8785.  
  8786.          107     0      Number of open database objects
  8787.  
  8788.          108     0      Percentage of remaining memory used for procedure
  8789.                         cache
  8790.  
  8791.          109     0      Default fill factor percentage
  8792.  
  8793.          110     0      Average time slice per process in milliseconds
  8794.  
  8795.          111     0      Default database size in megabytes
  8796.  
  8797.          112     0      Backup media retention period in days
  8798.  
  8799.          113     0      Recovery flags
  8800.  
  8801.  ─────────────────────────────────────────────────────────────────────────────
  8802. 
  8803.          114     0      Serial Number
  8804.  
  8805.  
  8806.  
  8807.  The value in the value column is the value that has been set with
  8808.  sp_configure. The value in the status column is 1 if the configuration
  8809.  option is dynamic and 0 if it is not.
  8810.  
  8811.  The overhead of each additional user connection (103) is 34K.
  8812.  
  8813.  The total size of available memory (104) must be changed when you add or
  8814.  remove memory boards. The total available physical memory minus the memory
  8815.  required for the operating system is available for SQL Server.
  8816.  
  8817.  The percentage of memory allocated to the procedure cache (108) and, by
  8818.  implication, to the buffer cache, can affect performance noticeably.
  8819.  
  8820.  You may have to change the database size (111) if the size of your model
  8821.  database increases substantially (for example, if you add a lot of your own
  8822.  stored procedures to model). You may want to change the default database
  8823.  size if most of your databases are larger than the value in syscurconfigs,
  8824.  which is 2 megabytes.
  8825.  
  8826.  For more information, see the SQL Server System Administrator's Guide.
  8827.  
  8828.  
  8829.  Permissions
  8830.  
  8831.  RECONFIGURE permission defaults to the System Administrator and is not
  8832.  transferable.
  8833.  
  8834.  
  8835.  See Also
  8836.  
  8837.  sp_configure
  8838.  
  8839.  
  8840.  RETURN
  8841.  ────────────────────────────────────────────────────────────────────────────
  8842.  
  8843.  
  8844.  Function
  8845.  
  8846.  Exits unconditionally from a query or procedure. Statements following RETURN
  8847.  are not executed.
  8848.  
  8849.  
  8850.  Syntax
  8851.  
  8852.    RETURN
  8853.  
  8854.  
  8855.  Examples
  8856.  
  8857.    create procedure findrules @nm varchar(30) = null as
  8858.     if @nm is null
  8859.      begin
  8860.      print "You must give a user name"
  8861.      return
  8862.     end
  8863.     else
  8864.     begin
  8865.      select sysobjects.name, sysobjects.id, sysobjects.uid
  8866.      from sysobjects, master..syslogins
  8867.      where master..syslogins.name = @nm
  8868.      and sysobjects.uid = master..syslogins.suid
  8869.      and sysobjects.type = "R"
  8870.     end
  8871.  
  8872.  If no username is given as a parameter, the RETURN keyword causes the
  8873.  procedure to exit after a message has been sent to the user's screen. If a
  8874.  username is given, the names of the rules created by this user in the
  8875.  current database are retrieved from the appropriate system tables.
  8876.  
  8877.  
  8878.  Comments
  8879.  
  8880.  The RETURN keyword can be used at any point where you want to exit from a
  8881.  procedure.
  8882.  
  8883.  RETURN is immediate and complete: statements after RETURN are not executed.
  8884.  
  8885.  
  8886.  
  8887.  See Also
  8888.  
  8889.  BEGIN...END, IF...ELSE, WHILE
  8890.  
  8891.  
  8892.  REVOKE
  8893.  ────────────────────────────────────────────────────────────────────────────
  8894.  
  8895.  
  8896.  Function
  8897.  
  8898.  Revokes permissions from users.
  8899.  
  8900.  
  8901.  Syntax
  8902.  
  8903.  Object permissions:
  8904.  
  8905.    REVOKE {ALL | permission_list}
  8906.      ON {table_name [(column_list)] | view_name [(column_list)] |
  8907.      stored_procedure_name}
  8908.      FROM {PUBLIC | name_list}
  8909.  
  8910.  Statement permissions:
  8911.  
  8912.    REVOKE {ALL | statement_list}
  8913.      FROM {PUBLIC | name_list}
  8914.  
  8915.  
  8916.  Examples
  8917.  
  8918.    A. use pubs /* Permissions can be revoked on objects
  8919.     ** in the current database only. */
  8920.     revoke insert, delete
  8921.     on titles
  8922.     from mary, sales
  8923.  
  8924.    B. revoke update
  8925.     on titles (royalty, advance)
  8926.     from public
  8927.  
  8928.    C. revoke create database, create table
  8929.     from mary, john
  8930.  
  8931.    D. revoke all
  8932.     from mary
  8933.  
  8934.    E. revoke all
  8935.     on titles
  8936.     from mary
  8937.  
  8938.  
  8939.  Options
  8940.  
  8941.  ALL
  8942.    When used to revoke object permissions (first syntax format), this keyword
  8943.    specifies that all permissions applicable to the specified object are
  8944.    revoked. When used to revoke statement permissions (second syntax format),
  8945.    this keyword specifies that all statement permissions are revoked. (CREATE
  8946.    DATABASE permission can be granted only by the System Administrator.)
  8947.  
  8948.  permission_list
  8949.    A list of granted permissions. When permissions are revoked on a table or
  8950.    a view, the permission list can include one or more of the following:
  8951.    SELECT, INSERT, DELETE, and UPDATE. When permissions are revoked on
  8952.    columns, the permission list can include one or both of the following:
  8953.    SELECT and UPDATE. When permissions are revoked on stored procedures, the
  8954.    permission list can include EXECUTE only. If more than one permission is
  8955.    listed, separate them with commas.
  8956.  
  8957.  statement_list
  8958.    A list of granted statements. The statement list can include CREATE
  8959.    DATABASE (which can be granted only by System Administrator), CREATE
  8960.    DEFAULT, CREATE PROCEDURE, CREATE RULE, CREATE TABLE, CREATE VIEW, DUMP
  8961.    DATABASE, and DUMP TRANSACTION. If more than one statement is listed,
  8962.    separate them with commas.
  8963.  
  8964.  table_name
  8965.    The name of the table in the current database. Only one object can be
  8966.    listed for each REVOKE statement.
  8967.  
  8968.  column_list
  8969.    A list of columns, separated by commas, to which the permissions apply. If
  8970.    columns are specified, only SELECT and UPDATE permissions can be granted.
  8971.  
  8972.  view_name
  8973.    The name of the view in the current database. Only one object can be
  8974.    listed for each REVOKE statement.
  8975.  
  8976.  stored_procedure_name
  8977.    The name of the stored procedure in the current database. Only one object
  8978.    can be listed for each REVOKE statement.
  8979.  
  8980.  PUBLIC
  8981.    All users.
  8982.  
  8983.  name_list
  8984.    A list of usernames and/or groupnames, separated by commas.
  8985.  
  8986.  
  8987.  Comments
  8988.  
  8989.  Permissions can be granted or revoked on objects in the current database
  8990.  only.
  8991.  
  8992.  You can substitute the word "TO" for the word "FROM" in the REVOKE statement
  8993.  if you wish.
  8994.  
  8995.  User groups allow you to grant or revoke permissions to more than one user
  8996.  at a time. Each user can be a member of only one group.
  8997.  
  8998.  Groups are created with the sp_addgroup system procedure, which takes the
  8999.  groupname as its parameter.
  9000.  
  9001.  New users can be added to a group (other than public, the default group)
  9002.  with the sp_adduser system procedure. Users' groups can be changed with
  9003.  sp_changegroup.
  9004.  
  9005.  To remove a group, use the sp_dropgroup system procedure. Its parameter is
  9006.  the groupname.
  9007.  
  9008.  To display the members of a group, use the sp_helpgroup system procedure.
  9009.  Its optional parameter is the groupname. If you do not give a groupname,
  9010.  sp_helpgroup displays all the groups that exist and lists the members of
  9011.  each.
  9012.  
  9013.  GRANT and REVOKE statements are order sensitive. The statement that takes
  9014.  effect when there is a conflict is the most recently executed one.
  9015.  
  9016.  
  9017.  See Also
  9018.  
  9019.  GRANT, SETUSER, sp_addgroup, sp_adduser, sp_changedbowner, sp_changegroup,
  9020.  sp_dropgroup, sp_dropuser, sp_helpgroup, sp_helprotect, sp_helpuser
  9021.  
  9022.  
  9023.  ROLLBACK TRANSACTION
  9024.  ────────────────────────────────────────────────────────────────────────────
  9025.  
  9026.  
  9027.  Function
  9028.  
  9029.  Rolls back a user-specified transaction to the last savepoint inside a
  9030.  transaction or to the beginning of a transaction.
  9031.  
  9032.  
  9033.  Syntax
  9034.  
  9035.    ROLLBACK TRANsaction [transaction_name | savepoint_name]
  9036.  
  9037.  
  9038.  Examples
  9039.  
  9040.    begin transaction royaltychange
  9041.      /* A user sets out to change the royalty split for the
  9042.      two authors of The Gourmet Microwave. Since the
  9043.      database would be inconsistent between the two updates,
  9044.      they must be grouped into a user-defined transaction. */
  9045.  
  9046.     update titleauthor
  9047.      set royaltyper = 65
  9048.      from titleauthor, titles
  9049.      where royaltyper = 75
  9050.      and titleauthor.title_id = titles.title_id
  9051.      and title = "The Gourmet Microwave"
  9052.  
  9053.     update titleauthor
  9054.      set royaltyper = 35
  9055.      from titleauthor, titles
  9056.      where royaltyper = 25
  9057.      and titleauthor.title_id = titles.title_id
  9058.      and title = "The Gourmet Microwave"
  9059.  
  9060.     save transaction percentchanged
  9061.      /* After having updated the royaltyper entries
  9062.      for the two authors, the user inserts the savepoint
  9063.      percentchanged, and then experiments to see how
  9064.      a 10% increase in the book's price would affect the
  9065.      authors' royalty earnings. */
  9066.  
  9067.     update titles
  9068.      set price = price * $1.1
  9069.      where title = "The Gourmet Microwave"
  9070.  
  9071.     select (price * royalty * ytd_sales) * royaltyper
  9072.      from titles, titleauthor
  9073.      where title = "The Gourmet Microwave"
  9074.      and titles.title_id = titleauthor.title_id
  9075.      /* The transaction is rolled back to the savepoint
  9076.      with the ROLLBACK TRANSACTION statement.*/
  9077.  
  9078.    rollback transaction percentchanged
  9079.  
  9080.     commit transaction
  9081.  
  9082.      /* End of royaltychange. */
  9083.  
  9084.  
  9085.  Options
  9086.  
  9087.  transaction_name
  9088.    The name assigned to the transaction. It must conform to the rules for
  9089.    identifiers.
  9090.  
  9091.  savepoint_name
  9092.    The name assigned to the savepoint. It must conform to the rules for
  9093.    identifiers.
  9094.  
  9095.  
  9096.  Comments
  9097.  
  9098.  Use the form ROLLBACK TRANSACTION transaction_name. When you rollback a
  9099.  transaction, all of the transaction's statements or procedures are undone.
  9100.  It is as though you had never performed any of those statements on
  9101.  procedures.
  9102.  
  9103.  If no savepoint_name or transaction_name is given with the ROLLBACK
  9104.  TRANSACTION statement, the transaction is rolled back to the previous BEGIN
  9105.  TRANSACTION.
  9106.  
  9107.  A savepoint is a marker set by a user within a transaction. It is used to
  9108.  cancel part of a transaction with the statement ROLLBACK TRANSACTION
  9109.  savepoint_name. All of the statements or procedures between the savepoint
  9110.  and the ROLLBACK TRANSACTION are undone. After a transaction is rolled back
  9111.  to a savepoint, it must proceed to completion (with more SQL statements if
  9112.  desired and a COMMIT TRANSACTION statement), or it must be canceled
  9113.  altogether (by rolling it back to its beginning).
  9114.  
  9115.  There is no limit on the number of savepoints within a transaction.
  9116.  
  9117.  The ROLLBACK TRANSACTION statement must appear within a transaction; you
  9118.  can't roll back a transaction after COMMIT TRANSACTION has been entered.
  9119.  
  9120.  Define a transaction by enclosing SQL statements and/or stored procedures
  9121.  with BEGIN TRANSACTION and COMMIT TRANSACTION.
  9122.  
  9123.  Within a transaction, duplicate savepoint_names are allowed, but only the
  9124.  first instance of a savepoint_name is used.
  9125.  
  9126.  
  9127.  Permissions
  9128.  
  9129.  ROLLBACK TRANSACTION permission defaults to public; no permission is
  9130.  required to use it.
  9131.  
  9132.  
  9133.  See Also
  9134.  
  9135.  BEGIN TRANSACTION, COMMIT TRANSACTION, SAVE TRANSACTION
  9136.  
  9137.  
  9138.  Row Aggregate Functions
  9139.  ────────────────────────────────────────────────────────────────────────────
  9140.  
  9141.  
  9142.  Function
  9143.  
  9144.  Generate summary values that appear as additional rows in the query results
  9145.  (unlike the aggregate function results, which appear as new columns). They
  9146.  allow you to see detail and summary rows in one set of results. Row
  9147.  aggregate functions (SUM, AVG, MIN, MAX, and COUNT) are used in a SELECT
  9148.  statement with the COMPUTE clause. You can calculate summary values for
  9149.  subgroups, and you can calculate more than one aggregate function for the
  9150.  same group. COMPUTE and the row aggregate functions are SQL Server
  9151.  enhancements to standard SQL.
  9152.  
  9153.  
  9154.  Syntax
  9155.  
  9156.    COMPUTE row_aggregate(column_name) [, row_aggregate(column_name)...]
  9157.      [BY column_name [, column_name]...]
  9158.  
  9159.  
  9160.  Examples
  9161.  
  9162.    select type, price
  9163.     from titles
  9164.     where price > $10
  9165.     and type like "%cook"
  9166.     order by type, price
  9167.     compute sum(price) by type
  9168.  
  9169. ╓┌─┌──────────────────┌──────────────────────────────────────────────────────╖
  9170.  ────────────────────────────────────────────────────────────────────────────
  9171.    type               price
  9172.    -----------        ----------
  9173.    mod_cook           19.99
  9174.                       sum
  9175.                       ----------
  9176.                       19.99
  9177.    type               price
  9178.    ------------       ----------
  9179.  ────────────────────────────────────────────────────────────────────────────
  9180.   ------------       ----------
  9181.    trad_cook          11.95
  9182.    trad_cook          14.99
  9183.    trad_cook          20.95
  9184.                       sum
  9185.                       ----------
  9186.                       47.89
  9187.    (6 rows affected)
  9188.  
  9189.  
  9190.  This example calculates the sum of the price of each type of cookbook over
  9191.  $10. (See "COMPUTE Clause" for more examples and information.)
  9192.  
  9193.  
  9194.  Options
  9195.  
  9196.  row_aggregate
  9197.    One of the following:
  9198.  
  9199. ╓┌─────────────────────────────────┌─────────────────────────────────────────╖
  9200.  Aggregate Function                Result
  9201.  ────────────────────────────────────────────────────────────────────────────
  9202.  SUM([DISTINCT] expression)        Returns the total of the [distinct]
  9203.                                    values in the numeric column
  9204.  
  9205.  AVG([DISTINCT] expression)        Returns the average of the [distinct]
  9206.                                    values in the numeric column
  9207.  
  9208.  COUNT(*)                          Returns the number of non-null values in
  9209.                                    the column
  9210.  
  9211.  MAX(expression)                   Returns the highest value in the column
  9212.  
  9213.  MIN(expression)                   Returns the lowest value in the column
  9214.  
  9215.  ────────────────────────────────────────────────────────────────────────────
  9216.  
  9217.  
  9218.  
  9219.  column_name
  9220.    The name of a column. It must be enclosed in parentheses. Only numeric
  9221.    columns can be used with SUM and AVG. One COMPUTE clause can apply the
  9222.    same function to several columns. When using more than one function, use
  9223.    more than one COMPUTE clause.
  9224.  
  9225.  BY
  9226.    Indicates that row aggregate function values are to be calculated for
  9227.    subgroups. Whenever the value of the BY item changes, row aggregate
  9228.    function values are generated. If you use BY, you must use ORDER BY.
  9229.    Listing more than one item after BY breaks a group into subgroups and
  9230.    applies a function at each level of grouping.
  9231.  
  9232.  
  9233.  Comments
  9234.  
  9235.  The row aggregate functions make it possible to retrieve detail and summary
  9236.  rows with one command. The aggregate functions, on the other hand,
  9237.  ordinarily produce a single value for all the selected rows in the table or
  9238.  for each group, and these summary values are shown as new columns. The
  9239.  following examples illustrate the differences:
  9240.  
  9241.    A. select type, sum(price), sum(advance)
  9242.     from titles
  9243.     where type like "%cook"
  9244.     group by type
  9245.  
  9246. ╓┌───┌─────────────────────────────────────────┌─────────────┌───────────────╖
  9247.  ────────────────────────────────────────────────────────────────────────────
  9248.      type
  9249.      ----------                                ----------    ------------
  9250.      mod_cook                                  22.98         15,000.00
  9251.      trad_cook                                 47.89         19,000.00
  9252.      (2 rows affected)
  9253.  B.  select type, price, advance
  9254.      from titles
  9255.      where type like "%cook"
  9256.      order by type
  9257.      compute sum(price), sum(advance) by type
  9258.      type                                      price         advance
  9259.      ----------                                ------------  ------------
  9260.      mod_cook                                  2.99          15,000.00
  9261.      mod_cook                                  19.99         0.00
  9262.                                                sum           sum
  9263.  ────────────────────────────────────────────────────────────────────────────
  9264.                                               sum           sum
  9265.                                                ------------  ------------
  9266.                                                22.98         15,000.00
  9267.      type                                      price         advance
  9268.      ----------                                ------------  ------------
  9269.      trad_cook                                 11.95         4,000.00
  9270.      trad_cook                                 14.99         8,000.00
  9271.      trad_cook                                 20.95         7,000.00
  9272.                                                sum           sum
  9273.                                                ------------  ------------
  9274.                                                47.89         19,000.00
  9275.      (7 rows affected)
  9276.  
  9277.  
  9278.  The columns in the COMPUTE clause must appear in the select list.
  9279.  
  9280.  You can't use SELECT INTO in the same statement as a COMPUTE clause because
  9281.  statements that include COMPUTE generate tables that include the summary
  9282.  results, which are not stored in the database.
  9283.  
  9284.  If you use COMPUTE BY, you must also use an ORDER BY clause. The columns
  9285.  listed after COMPUTE BY must be identical to or a subset of those listed
  9286.  after ORDER BY, and must be in the same left-to-right order, start with the
  9287.  same expression, and not skip any expressions. For example, say the ORDER BY
  9288.  clause is
  9289.  
  9290.    order by a, b, c
  9291.  
  9292.  the COMPUTE BY clause can be any or all of these:
  9293.  
  9294.    compute by a, b, c
  9295.     compute by a, b
  9296.     compute by a
  9297.  
  9298.  You must use a column name or an expression in the ORDER BY clause; you
  9299.  cannot sort by a column heading.
  9300.  
  9301.  The COMPUTE keyword can be used without BY to generate grand totals, grand
  9302.  counts, and so on. ORDER BY is optional if you use the COMPUTE keyword
  9303.  without BY.
  9304.  
  9305.  When you sum or average integer data, SQL Server treats the result as an int
  9306.  value, even if the datatype of the column is smallint or tinyint.
  9307.  
  9308.  ────────────────────────────────────────────────────────────────────────────
  9309.  WARNING
  9310.  
  9311.  To avoid overflow errors in DB-LIBRARY programs, make all variable
  9312.  declarations for results of averages or sums type int.
  9313.  ────────────────────────────────────────────────────────────────────────────
  9314.  
  9315.  In a SELECT statement with a COMPUTE clause, the order of columns in the
  9316.  select list overrides the order of the aggregate functions in the COMPUTE
  9317.  clause. For example:
  9318.  
  9319. ╓┌─┌───────────────────────────────┌───────────┌─────────────────────────────╖
  9320.  ────────────────────────────────────────────────────────────────────────────
  9321.    select a, b, c
  9322.    from test
  9323.    compute sum(c), max(b), min(a)
  9324.    a                               b           c
  9325.    ---------                       ----------  ----------
  9326.  ────────────────────────────────────────────────────────────────────────────
  9327.   ---------                       ----------  ----------
  9328.    1                               2           3
  9329.    3                               2           1
  9330.                                                sum
  9331.                                                =======
  9332.                                                4
  9333.                                    max
  9334.                                    =======
  9335.                                    2
  9336.    min
  9337.    =========
  9338.    1
  9339.    (3 rows affected)
  9340.  
  9341.  
  9342.  DB-LIBRARY programmers must be aware of this to put the aggregate function
  9343.  results in the right place.
  9344.  
  9345.  
  9346.  See Also
  9347.  
  9348.  Aggregate Functions, COMPUTE Clause, Functions, GROUP BY and HAVING Clauses,
  9349.  SELECT
  9350.  
  9351.  
  9352.  SAVE TRANSACTION
  9353.  ────────────────────────────────────────────────────────────────────────────
  9354.  
  9355.  
  9356.  Function
  9357.  
  9358.  Sets a savepoint within a transaction.
  9359.  
  9360.  
  9361.  Syntax
  9362.  
  9363.    SAVE TRANsaction savepoint_name
  9364.  
  9365.  
  9366.  Examples
  9367.  
  9368.    begin transaction royaltychange
  9369.      /* A user sets out to change the royalty split for the
  9370.      two authors of The Gourmet Microwave. Since the
  9371.      database would be inconsistent between the two updates,
  9372.      they must be grouped into a user-defined transaction. */
  9373.  
  9374.     update titleauthor
  9375.      set royaltyper = 65
  9376.      from titleauthor, titles
  9377.      where royaltyper = 75
  9378.      and titleauthor.title_id = titles.title_id
  9379.      and title = "The Gourmet Microwave"
  9380.  
  9381.     update titleauthor
  9382.      set royaltyper = 35
  9383.      from titleauthor, titles
  9384.      where royaltyper = 25
  9385.      and titleauthor.title_id = titles.title_id
  9386.      and title = "The Gourmet Microwave"
  9387.  
  9388.     save transaction percentchanged
  9389.      /* After having updated the royaltyper entries
  9390.      for the two authors, the user inserts the savepoint
  9391.      percentchanged and then experiments to see how
  9392.      a 10% increase in the book's price would affect the
  9393.      authors' royalty earnings. */
  9394.  
  9395.     update titles
  9396.      set price = price * $1.1
  9397.      where title = "The Gourmet Microwave"
  9398.  
  9399.     select (price * royalty * ytd_sales) * royaltyper
  9400.      from titles, titleauthor
  9401.      where title = "The Gourmet Microwave"
  9402.      and titles.title_id = titleauthor.title_id
  9403.      /* The transaction is rolled back to the savepoint
  9404.      with the ROLLBACK TRANSACTION statement. */
  9405.  
  9406.    rollback transaction percentchanged
  9407.  
  9408.     commit transaction
  9409.  
  9410.      /* End of royaltychange. */
  9411.  
  9412.  
  9413.  Options
  9414.  
  9415.  savepoint_name
  9416.    The name assigned to the savepoint. It must conform to the rules for
  9417.    identifiers.
  9418.  
  9419.  
  9420.  Comments
  9421.  
  9422.  A savepoint is a marker set by a user within a transaction. It is used to
  9423.  cancel part of a transaction with the statement ROLLBACK TRANSACTION
  9424.  savepoint_name. All of the statements or procedures after the savepoint are
  9425.  undone. After a transaction is rolled back to a savepoint, it must proceed
  9426.  to completion (with more SQL statements if desired and a COMMIT TRANSACTION
  9427.  statement), or it must be canceled altogether (by rolling it back to its
  9428.  beginning).
  9429.  
  9430.  To cancel an entire transaction, use the form ROLLBACK TRANSACTION
  9431.  transaction_name. All of the transaction's statements or procedures are
  9432.  undone.
  9433.  
  9434.  If no savepoint_name or transaction_name is given with the ROLLBACK
  9435.  TRANSACTION statement, the transaction is rolled back to the previous BEGIN
  9436.  TRANSACTION.
  9437.  
  9438.  The ROLLBACK TRANSACTION statement must appear within a transaction; you
  9439.  can't roll back a transaction after COMMIT TRANSACTION has been entered.
  9440.  
  9441.  Define a transaction by enclosing SQL statements and/or stored procedures
  9442.  with BEGIN TRANSACTION and COMMIT TRANSACTION. Within a transaction,
  9443.  duplicate savepoint_names are allowed, but only the first instance of a
  9444.  savepoint_name is used.
  9445.  
  9446.  
  9447.  Permissions
  9448.  
  9449.  SAVE TRANSACTION permission defaults to public; no permissions are required
  9450.  to use it.
  9451.  
  9452.  
  9453.  See Also
  9454.  
  9455.  BEGIN TRANSACTION, COMMIT TRANSACTION, ROLLBACK TRANSACTION
  9456.  
  9457.  
  9458.  Search Conditions
  9459.  ────────────────────────────────────────────────────────────────────────────
  9460.  
  9461.  
  9462.  Function
  9463.  
  9464.  Set the conditions in a WHERE or HAVING clause. (Joins and subqueries are
  9465.  specified in the search conditions; see the "Joins" and "Subqueries"
  9466.  sections for full details.)
  9467.  
  9468.  
  9469.  Syntax
  9470.  
  9471.  Search conditions immediately follow the keywords WHERE or HAVING in a
  9472.  SELECT, INSERT, UPDATE, or DELETE statement. (HAVING search conditions
  9473.  differ from WHERE search conditions only in that aggregate functions are not
  9474.  allowed in WHERE conditions.) If you use more than one of the search
  9475.  conditions in a single statement, connect the conditions with AND or OR.
  9476.  
  9477.    {WHERE|HAVING}[NOT]expression comparison_operator expression
  9478.     {WHERE|HAVING}[NOT]column_name [NOT] LIKE "match_string"
  9479.     {WHERE|HAVING}[NOT]expression [NOT] BETWEEN expression AND expression
  9480.     {WHERE|HAVING}[NOT]expression [NOT] IN ({value_list|subquery})
  9481.     {WHERE|HAVING}[NOT]EXISTS (subquery)
  9482.     {WHERE|HAVING}[NOT]expression comparison_operator{ANY|ALL}(subquery)
  9483.     {WHERE|HAVING}[NOT]column_name join_operator column_name
  9484.     {WHERE|HAVING}[NOT]boolean_expression
  9485.     {WHERE|HAVING}[NOT]boolean_expression {AND|OR} boolean_expression
  9486.     {WHERE|HAVING}[NOT]column_name IS [NOT] NULL
  9487.  
  9488.  (The last clause is allowed only where column_name has been defined as
  9489.  allowing NULL in the CREATE TABLE statement.)
  9490.  
  9491.  
  9492.  Examples
  9493.  
  9494.    A. where advance * $2 > ytd_sales * price
  9495.  
  9496.    B. where phone not like '415%'
  9497.  
  9498.  Example B finds all rows in which the phone number does not begin with 415.
  9499.  
  9500.  
  9501.    C. where au_lname like "[CK]ars[eo]n"
  9502.  
  9503.  Example C finds the rows for authors named Carson, Carsen, Karsen, and
  9504.  Karson.
  9505.  
  9506.    D. where advance < $5000 or advance is null
  9507.  
  9508.    E. where (type = "business" or type = "psychology")
  9509.    and
  9510.     advance > $5500
  9511.  
  9512.    F. where ytd_sales between 4095 and 12000
  9513.  
  9514.    G. where state in ('CA', 'IN', 'MD')
  9515.  
  9516.  Examples D-G find the rows in which the state is one of the three in the
  9517.  list.
  9518.  
  9519.  
  9520.  Options
  9521.  
  9522.  expression
  9523.    A column name, a constant, a function, any combination of column names,
  9524.    constants, and functions connected by arithmetic or bitwise operators, or
  9525.    a subquery. The arithmetic operators are as follows:
  9526.  
  9527. ╓┌───────────────────────┌───────────────────────────────────────────────────╖
  9528.  Symbol                  Meaning
  9529.  ────────────────────────────────────────────────────────────────────────────
  9530.  ++                      Addition
  9531.  -                       Subtraction
  9532.  *                       Multiplication
  9533.  /                       Division
  9534.  %                       Modulo
  9535.  ────────────────────────────────────────────────────────────────────────────
  9536.  Symbol                  Meaning
  9537.  ────────────────────────────────────────────────────────────────────────────
  9538. ────────────────────────────────────────────────────────────────────────────
  9539.  
  9540.  
  9541.    Addition, subtraction, division, and multiplication can be used on int,
  9542.    smallint, tinyint, float, and money columns. The modulo operator cannot be
  9543.    used with money or float columns. A modulo is the integer remainder after
  9544.    a division involving two integers. For example, 21 % 9 = 3 because 21
  9545.    divided by 9 equals 2 with a remainder of 3. The bitwise operators are as
  9546.    follows:
  9547.  
  9548.  Symbol           Meaning
  9549.  ────────────────────────────────────────────────────────────────────────────
  9550.  &                Bitwise and (two operands)
  9551.  |                Bitwise or (two operands)
  9552.                   Bitwise exclusive or (two operands)
  9553.  ~                Bitwise not (one operand)
  9554.  ────────────────────────────────────────────────────────────────────────────
  9555.  
  9556.    The bitwise operators can be used on int, smallint, and tinyint columns
  9557.    only. All the bitwise operators translate the integer parameters into
  9558.    binary representation before evaluating them. (See "Expressions" for more
  9559.    information.)
  9560.  
  9561.  comparison_operator
  9562.    One of the following:
  9563.  
  9564. ╓┌────────────────────┌──────────────────────────────────────────────────────╖
  9565.  Symbol               Meaning
  9566.  ────────────────────────────────────────────────────────────────────────────
  9567.  =                    Equal to
  9568.  >                    Greater than
  9569.  <                    Less than
  9570.  >=                   Greater than or equal to
  9571.  <=                   Less than or equal to
  9572.  !=                   Not equal to
  9573.  !>                   Not greater than
  9574.  !<                   Not less than
  9575.  ────────────────────────────────────────────────────────────────────────────
  9576.  
  9577.  
  9578.    In comparing char and varchar data, < means closer to the beginning of the
  9579.    alphabet, and > means closer to the end of the alphabet. Case and special
  9580.    character evaluations depend on the sorting sequence used by the operating
  9581.    system on the machine on which SQL Server is located. On ASCII machines,
  9582.    for example, lowercase letters are greater than uppercase letters, and
  9583.    uppercase letters are greater than numbers. In comparing dates, < means
  9584.    earlier and > means later. Trailing blanks are ignored for the purposes of
  9585.    comparison. "Dirk" is the same as "Dirk  ". Put quotation marks around all
  9586.    character and datetime data used with a comparison operator (= "Bennet", >
  9587.    "94609").
  9588.  
  9589.  column_name
  9590.    The name of a column used in the comparison. Qualify the column name with
  9591.    its table or view name if there is any ambiguity.
  9592.  
  9593.  NOT
  9594.    Negates any boolean expression and keywords, such as LIKE, NULL, BETWEEN,
  9595.    IN, and EXISTS.
  9596.  
  9597.  LIKE
  9598.    Indicates that the following character string (enclosed by single or
  9599.    double quotation marks) is a matching pattern. LIKE is available for char,
  9600.    varchar, and datetime columns (but not to search for seconds or
  9601.    milliseconds). You can use the LIKE keyword and wildcard characters with
  9602.    datetime data as well as with char and varchar. When you use LIKE with
  9603.    datetime values, SQL Server converts the dates to the standard datetime
  9604.    format and then to varchar. Since the standard storage format doesn't
  9605.    include seconds or milliseconds, you cannot search for seconds or
  9606.    milliseconds with LIKE and a pattern. It is a good idea to use LIKE when
  9607.    you search for datetime values, since datetime entries can contain a
  9608.    variety of date parts.
  9609.  
  9610.    For example, if you insert the value "9:20" into a column named
  9611.    arrival_time, the clause WHERE arrival_time = "9:20" would not find it
  9612.    because SQL Server converts the entry into "Jan 1, 1900 9:20AM". However,
  9613.    the clause WHERE arrival_time LIKE "%9:20%" would find it.
  9614.  
  9615.  match_string
  9616.    A string of characters and wildcards enclosed in quotation marks. The
  9617.    wildcards are as follows:
  9618.  
  9619.  Symbol                            Meaning
  9620.  ────────────────────────────────────────────────────────────────────────────
  9621.  %                                 Any string of 0 or more characters
  9622.  
  9623.  _ (underscore)                    Any single character
  9624.  
  9625.  [ ]                               Any single character within the
  9626.                                    specified range ([a-f]) or set ([abcdef])
  9627.  
  9628.  [^]                               Any single character not within the
  9629.                                    specified range ([^a-f]) or set
  9630.                                    ([^abcdef])
  9631.  
  9632.  ────────────────────────────────────────────────────────────────────────────
  9633.  
  9634.  
  9635.    The wildcard and the string must be enclosed in single or double quotation
  9636.    marks. For complete information, see "Wildcard Characters."
  9637.  
  9638.  IS [NOT] NULL
  9639.    Keywords that search for null values (or all values except null values).
  9640.    It is allowed only if the column has been defined to allow null values in
  9641.    the CREATE TABLE statement. An expression with a bitwise or arithmetic
  9642.    operator evaluates to NULL if any of the operands is null.
  9643.  
  9644.  BETWEEN
  9645.    The range-start keyword. Use AND for the range-end value. A range of
  9646.    BETWEEN x and y, unlike a range of > x < y, is inclusive.
  9647.  
  9648.  IN
  9649.    Allows you to select values that match any one of a list of values. The
  9650.    comparator can be a constant or a column name, and the list can be a set
  9651.    of constants or, more commonly, a subquery. (See "Subqueries" for
  9652.    information on using IN with a subquery.) Enclose the list of values in
  9653.    parentheses.
  9654.  
  9655.  value_list
  9656.    A list of values. Put single or double quotation marks around char,
  9657.    varchar, and datetime values, and separate each value from the following
  9658.    one with a comma. (See example E, earlier in this section.)
  9659.  
  9660.  EXISTS
  9661.    Is used with a subquery to test for the existence of some result from the
  9662.    subquery. (See "Subqueries" for more information.)
  9663.  
  9664.  subquery
  9665.    A restricted SELECT statement (ORDER BY and COMPUTE clauses and the INTO
  9666.    keyword are not allowed) in the WHERE or HAVING clause of a SELECT,
  9667.    INSERT, DELETE, or UPDATE statement, or in a subquery or expression. (See
  9668.    "Subqueries" for more information.)
  9669.  
  9670.  ANY
  9671.    Is used with <, >, or = and a subquery. It returns results when any value
  9672.    retrieved in the subquery matches the value in the WHERE or HAVING clause
  9673.    of the outer statement. (See "Subqueries" for more information.)
  9674.  
  9675.  ALL
  9676.    Is used with < or > and a subquery. It returns results when all values
  9677.    retrieved in the subquery match the value in the WHERE or HAVING clause of
  9678.    the outer statement. (See "Subqueries" for more information.)
  9679.  
  9680.  join_operator
  9681.    A comparison operator, =* or *=. (See "Joins" for more information.)
  9682.  
  9683.  boolean_expression
  9684.    An expression that returns true or false. (See "Expressions" for a full
  9685.    definition.)
  9686.  
  9687.  AND
  9688.    Joins two conditions and returns results when both of the conditions are
  9689.    true. When more than one logical operator is used in a statement, AND
  9690.    operators are normally evaluated first. However, you can change the order
  9691.    of execution with parentheses.
  9692.  
  9693.  OR
  9694.    Joins two conditions and returns results when either of the conditions is
  9695.    true. When more than one logical operator is used in a statement, OR
  9696.    operators are normally evaluated after AND operators. However, you can
  9697.    change the order of execution with parentheses.
  9698.  
  9699.  
  9700.  Comments
  9701.  
  9702.  WHERE and HAVING search conditions are identical, except that aggregate
  9703.  functions are not permitted in WHERE clauses (HAVING avg(price) > $20 is
  9704.  legal; WHERE avg(price) > $20 is not legal). See "Aggregate Functions" for
  9705.  information on the use of aggregate functions.
  9706.  
  9707.  You can use a HAVING clause without a GROUP BY clause.
  9708.  
  9709.  If there are columns in the select list that neither have aggregate
  9710.  functions applied to them nor are included in the query's GROUP BY clause
  9711.  (illegal in standard SQL), the meanings of HAVING and WHERE are somewhat
  9712.  different.
  9713.  
  9714.  In this situation, a WHERE clause restricts the rows that are included in
  9715.  the calculation of the aggregate function but does not restrict the rows
  9716.  returned by the query. Conversely, a HAVING clause restricts the rows
  9717.  returned by the query but does not affect the calculation of the aggregate
  9718.  function. See "GROUP BY and HAVING Clauses" for examples.
  9719.  
  9720.  
  9721.  See Also
  9722.  
  9723.  Aggregate Functions, DELETE, EXECUTE, Expressions, INSERT, Joins, SELECT,
  9724.  Subqueries, UPDATE, Wildcard Characters, sp_helpjoins
  9725.  
  9726.  
  9727.  SELECT
  9728.  ────────────────────────────────────────────────────────────────────────────
  9729.  
  9730.  
  9731.  Function
  9732.  
  9733.  Retrieves rows from the database.
  9734.  
  9735.  
  9736.  Syntax
  9737.  
  9738.    SELECT [ALL | DISTINCT] select_list
  9739.      [INTO [[database.]owner.]table_name]
  9740.      [FROM [[database.]owner.]{table_name | view_name} [HOLDLOCK]
  9741.      [,[[database.]owner.]{table_name | view_name} [HOLDLOCK]...]
  9742.      [WHERE search_conditions]
  9743.      [GROUP BY [ALL]
  9744.      aggregate_free_expression
  9745.      [, aggregate_free_expression...]
  9746.      [HAVING search_conditions]
  9747.      [ORDER BY {[[[database.]owner.]{table_name. | view_name.}]
  9748.      column_name |
  9749.      select_list_number | expression} [ASC | DESC]
  9750.      [,{[[[database.]owner.]{table_name | view_name.}]
  9751.      column_name |
  9752.      select_list_number | expression} [ASC | DESC]]...]
  9753.      [COMPUTE row_aggregate(column_name)
  9754.      [, row_aggregate(column_name)...]
  9755.      [BY column_name [, column_name]...]]
  9756.      [FOR BROWSE]
  9757.  
  9758.  The keywords in the SELECT statement, as in all other SQL statements, must
  9759.  be used in the order shown in the syntax statement.
  9760.  
  9761.  
  9762.  Examples
  9763.  
  9764.    A. select *
  9765.     from publishers
  9766.  
  9767.    B. select pub_id, pub_name, city, state
  9768.     from publishers
  9769.  
  9770.    C. select "The publisher's name is", Publisher = pub_name,
  9771.    pub_id
  9772.     from publishers
  9773.  
  9774.    D. select pub_id, total = sum (ytd_sales)
  9775.     from titles
  9776.     where advance < $10000
  9777.     and ytd_sales is not null
  9778.     group by pub_id
  9779.     having count(*) >1
  9780.  
  9781.  
  9782.  
  9783.    E. select type, price, advance
  9784.     from titles
  9785.     order by type desc
  9786.     compute avg(price), sum(advance) by type
  9787.     compute sum(price), sum(advance)
  9788.  
  9789.    F. select type, price, advance
  9790.     from titles
  9791.     compute sum(price), sum(advance)
  9792.  
  9793.    G. select *
  9794.     into coffeetabletitles
  9795.     from titles
  9796.     where price > $20
  9797.  
  9798.    H. select *
  9799.     into newtitles
  9800.     from titles
  9801.     where price > $25
  9802.     and price < $20
  9803.  
  9804.  
  9805.  Options
  9806.  
  9807.  ALL
  9808.    Includes all rows in the results. This is the default.
  9809.  
  9810.  DISTINCT
  9811.    Includes only unique rows in the results. Null values are considered equal
  9812.    for the purposes of the DISTINCT keyword; only one NULL is selected no
  9813.    matter how many are encountered.
  9814.  
  9815.  select_list
  9816.    One or more of the following:
  9817.  
  9818.  
  9819.    ■   Asterisk (*), representing all columns in create-table order
  9820.  
  9821.    ■   A list of column names in the order in which you want to see them
  9822.  
  9823.    ■   A column heading to replace the default column heading (the column
  9824.        name) in the following form:
  9825.  
  9826.            column_heading = column_name
  9827.                    or
  9828.                    column_name column_heading
  9829.  
  9830.  
  9831.    ■   An expression (a column name, constant, function, or any combination
  9832.        of column names, constants, and functions connected by arithmetic or
  9833.        bitwise operators, or a subquery)
  9834.  
  9835.    ■   A built-in function or an aggregate function
  9836.  
  9837.    ■   Any combination of these items
  9838.  
  9839.          See "Expressions" for information on arithmetic and bitwise
  9840.          operators and a more detailed definition of expressions. See
  9841.          "Functions" for information on functions. See "Subqueries" for
  9842.          information on subqueries.
  9843.  
  9844.        INTO
  9845.    Creates a new table based on the columns specified in the select list and
  9846.    the rows chosen in the WHERE clause. The new table must not already exist.
  9847.    Its name must be unique in the database and must conform to the rules for
  9848.    identifiers. See example G.
  9849.  
  9850.          You can use SELECT INTO to create a duplicate table with no data by
  9851.          having a false condition in the WHERE clause. See example H.
  9852.  
  9853.          You cannot use SELECT INTO inside a user-defined transaction.
  9854.  
  9855.          SELECT INTO is a two-step operation. The first step creates the new
  9856.          table. The second step inserts the specified rows into the new
  9857.          table. If the second step fails for any reason (hardware failure,
  9858.          exceeding a system resource, and so on), the new table will exist
  9859.          with no rows in it.
  9860.  
  9861.          The select into/bulkcopy option must be on (by executing
  9862.          sp_dboption) to be able to select into a permanent table. You do not
  9863.          have to have the select into/bulkcopy option on to select into a
  9864.          temporary table, since the tempdb temporary database is never
  9865.          recovered.
  9866.  
  9867.          While the select into/bulkcopy option is on, you are not allowed to
  9868.          dump the transaction log because these operations are not logged and
  9869.          changes are therefore not recoverable from transaction logs. In this
  9870.          situation, executing the DUMP TRANSACTION statement produces an
  9871.          error message instructing you to use DUMP DATABASE instead.
  9872.  
  9873.          By default, the select into/bulkcopy option is off in newly created
  9874.          databases. To change the default, turn this option on in the model
  9875.          database.
  9876.  
  9877.        FROM
  9878.    Indicates that particular tables and views are used in the SELECT
  9879.    statement. It is always required except when the select list contains no
  9880.    column names (that is, constants and arithmetic expressions only):
  9881.  
  9882.        select 5 x, 2 y, "the product is", 5*2 Result
  9883.  
  9884.        table_name, view_name
  9885.    Tables and views used in the SELECT statement. If there is more than one
  9886.    table or view in the list, separate their names with commas. The order of
  9887.    the tables and views after the FROM keyword makes no difference in the
  9888.    results.
  9889.  
  9890.          As the syntax indicates, you can query tables in different databases
  9891.          in the same statement.
  9892.  
  9893.          Table names and view names can be given aliases, either for
  9894.          convenience or to distinguish the different roles that a table or
  9895.          view play in a self-join or subquery. Give the table or view name,
  9896.          then a space, then the alias name, like this:
  9897.  
  9898.        select *
  9899.                from publishers pu, authors au
  9900.  
  9901.        HOLDLOCK
  9902.    Restricts a shared lock on a specified table or view by holding it until
  9903.    the completion of a transaction (instead of releasing the shared lock as
  9904.    soon as the required table or data page is no longer needed, whether or
  9905.    not the transaction has been completed). The HOLDLOCK statement applies
  9906.    only to the table or view for which it is specified and only for the
  9907.    duration of the transaction defined by the statement in which it is used.
  9908.  
  9909.        search_conditions
  9910.    The conditions for the rows that are retrieved. A search condition can
  9911.    include column names; expressions; arithmetic operators; comparison
  9912.    operators; the keywords NOT, LIKE, IS NULL, AND, OR, BETWEEN, IN, EXISTS,
  9913.    ANY, or ALL; subqueries; or any combination of these items. There is no
  9914.    limit on the number of search conditions that can be included in an SQL
  9915.    statement. WHERE search conditions and HAVING search conditions are
  9916.    identical with one exception: WHERE clauses cannot contain aggregate
  9917.    functions and HAVING clauses can. See the sections "Search Conditions,"
  9918.    "WHERE Clause," "Expressions," and "Subqueries" for more information.
  9919.  
  9920.        GROUP BY
  9921.    Keywords that find a value for each group. These values appear as new
  9922.    columns in the results, rather than as new rows.
  9923.  
  9924.          When GROUP BY is used with standard SQL, each item in the select
  9925.          list must either have a fixed value in every row in the group or be
  9926.          used with aggregate functions that produce a single value for each
  9927.          group. TRANSACT-SQL has no such restrictions on the items in the
  9928.          select list.
  9929.  
  9930.          Also, with TRANSACT-SQL, you can group by any expression (though not
  9931.          by a column alias), while with standard SQL you can only group by a
  9932.          column.
  9933.  
  9934.          You can use the following aggregate functions with GROUP BY
  9935.          (expression is almost always a column name):
  9936. ╓┌─────────────────────────────────┌─────────────────────────────────────────╖
  9937.  Aggregate Function                Result
  9938.  ────────────────────────────────────────────────────────────────────────────
  9939.  SUM([DISTINCT] expression)        Returns the total of the [distinct]
  9940.                                    values in the numeric column
  9941.  
  9942.  AVG([DISTINCT] expression)        Returns the average of the [distinct]
  9943.                                    values in the numeric column
  9944.  
  9945.  COUNT([DISTINCT] expression)      Returns the number of [distinct]
  9946.                                    non-null values in the expression
  9947.  
  9948.  COUNT(*)                          Returns the number of selected rows
  9949.  
  9950.  MAX(expression)                   Returns the highest value in the
  9951.                                    expression
  9952.  
  9953.  MIN(expression)                   Returns the lowest value in the
  9954.                                    expression
  9955.  
  9956.  Aggregate Function                Result
  9957.  ────────────────────────────────────────────────────────────────────────────
  9958. 
  9959.  ────────────────────────────────────────────────────────────────────────────
  9960.  
  9961.  
  9962.  
  9963.    (See "GROUP BY" and "Aggregate Functions" for more information.)
  9964.  
  9965.          A table can be grouped by any combination of columns─that is, groups
  9966.          can be nested within each other. Note that you cannot group by a
  9967.          column heading; you must use a column name, an expression, or a
  9968.          number representing the position of the item in the select list.
  9969.  
  9970.        ALL
  9971.    All groups in the results, even those that don't have any rows that meet
  9972.    the search conditions. (See "GROUP BY and HAVING Clauses" for an example.)
  9973.  
  9974.        aggregate_free_expression
  9975.    An expression that includes no aggregate functions.
  9976.  
  9977.        HAVING
  9978.    Sets conditions for the GROUP BY clause, similar to the way that WHERE
  9979.    sets conditions for the SELECT clause. There is no limit on the number of
  9980.    conditions that can be included.
  9981.  
  9982.          You can use a HAVING clause without a GROUP BY clause.
  9983.  
  9984.          If there are columns in the select list that neither have aggregate
  9985.          functions applied to them nor are included in the query's GROUP BY
  9986.          clause (illegal in standard SQL), the meanings of HAVING and WHERE
  9987.          are somewhat different.
  9988.  
  9989.          In this situation, a WHERE clause restricts the rows that are
  9990.          included in the calculation of the aggregate function but does not
  9991.          restrict the rows returned by the query. Conversely, a HAVING clause
  9992.          restricts the rows returned by the query but does not affect the
  9993.          calculation of the aggregate function. See "GROUP BY and HAVING
  9994.          Clauses" for examples.
  9995.  
  9996.        ORDER BY
  9997.    Sorts the results by columns. You can sort up to 16 columns. In
  9998.    TRANSACT-SQL, you can use ORDER BY for items that do not appear in the
  9999.    select list. You can sort by a column name, a column heading (or alias),
  10000.    an expression, or a number representing the position of the item in the
  10001.    select list (the select_list_number). If you sort by select list number,
  10002.    the columns to which the ORDER BY clause refers must be included in the
  10003.    select list, and the select list cannot be *.
  10004.  
  10005.        ASC
  10006.    Indicates that results are to be sorted in ascending order, which is the
  10007.    default.
  10008.  
  10009.        DESC
  10010.    Indicates that results are to be sorted in descending order.
  10011.  
  10012.        COMPUTE
  10013.    Used with row aggregate functions (SUM, AVG, MIN, MAX, and COUNT) to
  10014.    generate control break summary values. The summary values appear as
  10015.    additional rows in the query results, allowing you to see detail and
  10016.    summary rows with one statement.
  10017.  
  10018.          You cannot use a SELECT INTO clause with COMPUTE.
  10019.  
  10020.          If you use COMPUTE BY, you must also use an ORDER BY clause. The
  10021.          columns listed after COMPUTE BY must be identical to or a subset of
  10022.          those listed after ORDER BY, and must be in the same left-to-right
  10023.          order, start with the same expression, and not skip any expressions.
  10024.  
  10025.          For example, say the ORDER BY clause is
  10026.  
  10027.        order by a, b, c
  10028.  
  10029.          Then the COMPUTE BY clause can be any (or all) of these:
  10030.  
  10031.        compute by a, b, c
  10032.                compute by a, b
  10033.                compute by a
  10034.  
  10035.          The COMPUTE keyword can be used without BY to generate grand totals,
  10036.        grand counts, and so on. ORDER BY is optional if you use the COMPUTE
  10037.        keyword without BY. If more than one item is listed after COMPUTE BY,
  10038.        the items must be a subset of those listed after ORDER BY and must be
  10039.        in the same order. (See "COMPUTE Clause" for details and examples.)
  10040.  
  10041.        FOR BROWSE
  10042.    DB-LIBRARY only. Allows you to perform updates while viewing data. FOR
  10043.    BROWSE is used only in an application program using DB-LIBRARY.
  10044.  
  10045.  
  10046.  
  10047.  Comments
  10048.  
  10049.  The keywords in the SELECT statement, as in all other SQL statements, must
  10050.  be used in the order shown in the syntax statement.
  10051.  
  10052.  Although it is not shown in the syntax, the keyword ALL can be used after
  10053.  SELECT for compatibility with other implementations of SQL. ALL is the
  10054.  default. Used in this context, ALL is the opposite of DISTINCT: all
  10055.  retrieved rows are included in the results, whether or not some are
  10056.  duplicates.
  10057.  
  10058.  Column headings and table aliases must conform to the rules for identifiers.
  10059.  
  10060.  
  10061.  If you have two tables identical in structure except that one has NULL
  10062.  fields and the other has NOT NULL fields, this difference makes it
  10063.  impossible to insert the data from the NULL table into the NOT NULL table
  10064.  with a SELECT, whether or not there is any NULL data. The length of returned
  10065.  text defaults to the size of the text. To change the length of returned
  10066.  text, use the SET TEXTSIZE statement.
  10067.  
  10068.  The limit on the length of text data returned with a SELECT statement
  10069.  defaults to 32K, or to the size set with SET TEXTSIZE.
  10070.  
  10071.  
  10072.  Permissions
  10073.  
  10074.  SELECT permission defaults to the owner of the table or view, who can
  10075.  transfer it to other users.
  10076.  
  10077.  
  10078.  See Also
  10079.  
  10080.  Aggregate Functions, COMPUTE Clause, CREATE TRIGGER, DELETE, Expressions,
  10081.  Functions, GROUP BY and HAVING Clauses, INSERT, Row Aggregate Functions,
  10082.  Search Conditions, UPDATE, WHERE Clause, Wildcard Characters, sp_dboption
  10083.  
  10084.  
  10085.  SET
  10086.  ────────────────────────────────────────────────────────────────────────────
  10087.  
  10088.  
  10089.  Function
  10090.  
  10091.  Sets SQL Server query-processing options for the duration of the user's work
  10092.  session, or inside a trigger or stored procedure.
  10093.  
  10094.  
  10095.  Syntax
  10096.  
  10097.    SET
  10098.     {{ ARITHABORT | ARITHIGNORE |
  10099.      BACKGROUND | NOCOUNT | NOEXEC |
  10100.      OFFSETS {keyword_list} | PARSEONLY | PROCID |
  10101.      SHOWPLAN | STATISTICS IO | STATISTICS TIME }
  10102.     {ON | OFF} |
  10103.     ROWCOUNT number | TEXTSIZE n}
  10104.  
  10105.  
  10106.  Examples
  10107.  
  10108.    A. set showplan on
  10109.     go
  10110.     set noexec on
  10111.     go
  10112.     select * from publishers
  10113.     go
  10114.  
  10115.  For each query, SQL Server returns a description of the processing plan but
  10116.  does not execute it (example A).
  10117.  
  10118.    B. set rowcount 10
  10119.  
  10120.  For each query, SQL Server stops processing the query after it returns the
  10121.  first ten rows (example B).
  10122.  
  10123.  
  10124.  Options
  10125.  
  10126.  ARITHABORT
  10127.    Aborts a query when an overflow or divide-by-zero error occurs during
  10128.    query execution.
  10129.  
  10130.  ARITHIGNORE
  10131.    Returns NULL when an overflow or divide-by-zero error occurs during a
  10132.    query. No warning message is displayed. If neither ARITHABORT nor
  10133.    ARITHIGNORE is set, SQL Server returns NULL and prints a warning message
  10134.    after the query is executed.
  10135.  
  10136.  BACKGROUND
  10137.    Puts processes generated by subsequent statements in the background and
  10138.    breaks the connection to the workstation. Once BACKGROUND is on, you can't
  10139.    do anything else. Your session is over; you must interrupt it and log in
  10140.    again.
  10141.  
  10142.    If you execute a stored procedure that sets the background option, your
  10143.    session becomes disconnected from your workstation. The connection is not
  10144.    re-established even when the procedure finishes executing.
  10145.  
  10146.  NOCOUNT
  10147.    Turns off the message returned at the end of each statement that tells you
  10148.    how many rows were affected by the statement. The global variable
  10149.    @ROWCOUNT is updated even when NOCOUNT is on.
  10150.  
  10151.  NOEXEC
  10152.    Compiles each query but does not execute it. NOEXEC is often used with
  10153.    SHOWPLAN. Once NOEXEC is turned on, no subsequent statements will be
  10154.    executed (including other SET statements) until NOEXEC is turned off.
  10155.  
  10156.  OFFSETS
  10157.    DB-LIBRARY only. Returns the offset (position in relation to the beginning
  10158.    of the query) of specified keywords in SQL statements. The keyword list is
  10159.    a list, separated with commas, that can include any of the following SQL
  10160.    constructs: SELECT, FROM, ORDER, COMPUTE, TABLE, PROCEDURE, STATEMENT,
  10161.    PARAM, and EXECUTE.
  10162.  
  10163.    The OFFSETS option is used only in an application program using
  10164.    DB-LIBRARY.
  10165.  
  10166.  PARSEONLY
  10167.    Checks the syntax of each query and returns any error messages without
  10168.    generating a sequence tree, compiling, or executing the query. Do not use
  10169.    PARSEONLY in a stored procedure. PARSEONLY returns offsets if the OFFSETS
  10170.    option is on and there are no errors (however, OFFSETS is used only in an
  10171.    application program using DB-LIBRARY).
  10172.  
  10173.  PROCID
  10174.    Returns the ID number of the stored procedure to DB-LIBRARY (not to the
  10175.    user) before sending rows generated by that stored procedure.
  10176.  
  10177.  ROWCOUNT number
  10178.    Causes SQL Server to stop processing the query after the specified number
  10179.    of rows are returned. To turn this option off, so that all rows are
  10180.    returned, use "SET ROWCOUNT 0".
  10181.  
  10182.  SHOWPLAN
  10183.    Generates a description of the processing plan for the query and
  10184.    immediately processes it unless NOEXEC is set. Do not use SHOWPLAN in a
  10185.    stored procedure.
  10186.  
  10187.  STATISTICS IO
  10188.    Displays the number of scans, the number of logical reads (pages
  10189.    accessed), and the number of physical reads (disk accesses) for each table
  10190.    referenced in the statement; STATISTICS IO displays the number of pages
  10191.    written for each statement.
  10192.  
  10193.  STATISTICS TIME
  10194.    Displays the time it took to parse and compile each command and the time
  10195.    it took to execute each step of the command. Times are given in timeticks,
  10196.    the exact value of which is machine-dependent.
  10197.  
  10198.  TEXTSIZE n
  10199.    Specifies the size in bytes of text type data to be returned with a SELECT
  10200.    statement. If you specify a textsize of 0, 32K bytes of data are returned
  10201.    with a SELECT statement. The @TEXTSIZE global variable stores the current
  10202.    setting.
  10203.  
  10204.  
  10205.  Comments
  10206.  
  10207.  If you use the SET statement inside a trigger or stored procedure, the
  10208.  option reverts to its former setting after the trigger or procedure
  10209.  executes.
  10210.  
  10211.  Do not use the SHOWPLAN or PARSEONLY option within a stored procedure.
  10212.  
  10213.  The SET options can be divided into four categories:
  10214.  
  10215.  
  10216.    ■   PARSEONLY, NOEXEC, SHOWPLAN, BACKGROUND, ROWCOUNT, and NOCOUNT control
  10217.        the way a query is executed. It doesn't make sense to set both
  10218.        PARSEONLY and NOEXEC to ON. The default setting for ROWCOUNT is 0
  10219.        (return all rows); the default for the others is OFF.
  10220.  
  10221.    ■   The STATISTICS options display performance statistics after each
  10222.        query. The default setting for these options is OFF.
  10223.  
  10224.    ■   ARITHABORT and ARITHIGNORE handle exceptional cases during query
  10225.        execution. You cannot set both ARITHABORT and ARITHIGNORE to ON. The
  10226.        default setting for these options is OFF.
  10227.  
  10228.    ■   OFFSETS and PROCID are used by application programs to interpret
  10229.        results from SQL Server. The default setting for these options is ON.
  10230.  
  10231.  
  10232.  Any options set with a SET statement take effect at the end of the batch.
  10233.  You can combine SET statements and queries in the same batch, but the SET
  10234.  options won't apply to the queries in that batch. In the first example, the
  10235.  SET statement is combined with the query in the same batch; therefore, the
  10236.  SET option doesn't apply to the queries in that batch. In the second
  10237.  example, the SET statement is executed before the batch; therefore, the SET
  10238.  option does apply to the queries in that batch.
  10239.  
  10240.    A. set showplan on
  10241.     select * from publishers
  10242.     go
  10243.  
  10244.     pub_id  pub_name     city  state
  10245.     -------  ----------------------  -------- ------
  10246.     0736  New Age Books    Boston  MA
  10247.     0877  Binnet & Hardley   Washington DC
  10248.     1389  Algodata Infosystems  Berkeley CA
  10249.  
  10250.     (3 rows affected)
  10251.  
  10252.    B. set showplan on
  10253.     go
  10254.     select * from publishers
  10255.     go
  10256.     STEP 1
  10257.     The type of query is SELECT
  10258.     FROM TABLE
  10259.     publishers
  10260.     Nested iteration
  10261.     Table Scan
  10262.  
  10263.     pub_id  pub_name     city   state
  10264.     -------  ----------------------  ------------ ------
  10265.     0736  New Age Books    Boston   MA
  10266.     0877  Binnet & Hardley   Washington  DC
  10267.     1389  Algodata Infosystems  Berkeley  CA
  10268.  
  10269.     (3 rows affected)
  10270.  
  10271.  
  10272.  See Also
  10273.  
  10274.  Batch Queries, Stored Procedures, Triggers
  10275.  
  10276.  
  10277.  SETUSER
  10278.  ────────────────────────────────────────────────────────────────────────────
  10279.  
  10280.  
  10281.  Function
  10282.  
  10283.  Impersonates another user. This statement is used by a Database Owner.
  10284.  
  10285.  
  10286.  Syntax
  10287.  
  10288.    SETUSER ["username"]
  10289.  
  10290.  
  10291.  Examples
  10292.  
  10293.    setuser "mary"
  10294.     go
  10295.     grant select on authors to joe
  10296.     setuser
  10297.     go
  10298.  
  10299.  The Database Owner temporarily adopts Mary's identity in the database to
  10300.  grant permissions on authors, a table owned by Mary.
  10301.  
  10302.  
  10303.  Comments
  10304.  
  10305.  When the Database Owner uses the SETUSER statement, SQL Server checks the
  10306.  permissions of the user being impersonated instead of the permissions of the
  10307.  Database Owner.
  10308.  
  10309.  When the System Administrator uses the SETUSER statement, he or she retains
  10310.  full System Administrator permissions.
  10311.  
  10312.  The user being impersonated must be listed in the sysusers table of the
  10313.  database.
  10314.  
  10315.  The SETUSER statement remains in effect until another SETUSER statement is
  10316.  given or until the current database is changed with the USE statement.
  10317.  
  10318.  The SETUSER statement with no username re-establishes the Database Owner's
  10319.  original identity.
  10320.  
  10321.  SETUSER is used by the Database Owner to adopt the identity of another user,
  10322.  to use another user's database object, to grant permissions, to create an
  10323.  object, or for some other reason.
  10324.  
  10325.  The System Administrator can use SETUSER to create objects that will be
  10326.  owned by another user. However, since the System Administrator operates
  10327.  outside the permissions system, she or he cannot use SETUSER to acquire
  10328.  another user's permissions. No matter what SETUSER statements have been
  10329.  executed, the System Administrator always has permission to do everything.
  10330.  
  10331.  
  10332.  Permissions
  10333.  
  10334.  SETUSER permission defaults to the Database Owner and is not transferable.
  10335.  
  10336.  
  10337.  See Also
  10338.  
  10339.  Batch Queries, GRANT, REVOKE
  10340.  
  10341.  
  10342.  SHUTDOWN
  10343.  ────────────────────────────────────────────────────────────────────────────
  10344.  
  10345.  
  10346.  Function
  10347.  
  10348.  Brings the system to a halt. This statement can only be executed by the
  10349.  System Administrator.
  10350.  
  10351.  
  10352.  Syntax
  10353.  
  10354.    SHUTDOWN [WITH NOWAIT]
  10355.  
  10356.  
  10357.  Options
  10358.  
  10359.  WITH NOWAIT
  10360.    Keywords that shut SQL Server down immediately, without performing
  10361.    checkpoints in every database. SQL Server exits after attempting to
  10362.    terminate all user processes and roll back any active transactions.
  10363.  
  10364.  
  10365.  Comments
  10366.  
  10367.  Unless the System Administrator uses the NOWAIT option, SHUTDOWN waits for
  10368.  currently executing SQL statements or stored procedures to finish and then
  10369.  performs a checkpoint in every database.
  10370.  
  10371.  SHUTDOWN minimizes the amount of work that automatic recovery has to do when
  10372.  the System Administrator restarts SQL Server.
  10373.  
  10374.  
  10375.  Permissions
  10376.  
  10377.  SHUTDOWN permission defaults to the System Administrator and cannot be
  10378.  transferred.
  10379.  
  10380.  
  10381.  String Functions
  10382.  ────────────────────────────────────────────────────────────────────────────
  10383.  
  10384.  
  10385.  Function
  10386.  
  10387.  Perform various operations on binary data, character strings, or
  10388.  expressions, including concatenation.
  10389.  
  10390.  
  10391.  Syntax
  10392.  
  10393.  Built-in string functions return values commonly needed for operations on
  10394.  character data. String function names are not keywords. String functions
  10395.  have the following syntax:
  10396.  
  10397.    function_name(parameters)
  10398.  
  10399.  You can concatenate binary or character expressions like this:
  10400.  
  10401.    (expression + expression [+ expression} . . .)
  10402.  
  10403.  Function names, parameters, and results are as follows:
  10404.  
  10405.  
  10406.  
  10407. ╓┌───────────┌───────────────────────────────┌───────────────────────────────╖
  10408.  Function    Parameters                      Result
  10409.  ────────────────────────────────────────────────────────────────────────────
  10410.  ASCII       (char_expr)                     The ASCII code value of the
  10411.                                              leftmost character of a
  10412.                                              character expression.
  10413.  
  10414.  CHAR        (int_expr)                      A character converted from an
  10415.                                              ASCII code. The ASCII code
  10416.                                              should be between 0 and 255;
  10417.                                              otherwise, NULL is returned.
  10418.  Function    Parameters                      Result
  10419.  ────────────────────────────────────────────────────────────────────────────
  10420.                                             otherwise, NULL is returned.
  10421.  
  10422.  CHARINDEX   ("Pattern", expression)         The starting position of the
  10423.                                              specified pattern. The first
  10424.                                              parameter is the pattern. The
  10425.                                              second parameter is an
  10426.                                              expression, usually a column
  10427.                                              name, in which SQL Server
  10428.                                              searches for the pattern. Not
  10429.                                              implemented for text and image
  10430.                                              data.
  10431.  
  10432.  DATALENGTH  (char_expr)                     The length of any type of data
  10433.                                              value or text field. A NULL
  10434.                                              string returns a length of 1.
  10435.  
  10436.  DIFFERENCE  (char_expr1, char_expr2)        The difference between the
  10437.                                              values of two character
  10438.                                              expressions as returned by the
  10439.  Function    Parameters                      Result
  10440.  ────────────────────────────────────────────────────────────────────────────
  10441.                                             expressions as returned by the
  10442.                                              SOUNDEX function.
  10443.  
  10444.  LOWER       (char_expr)                     Lowercase converted from
  10445.                                              uppercase.
  10446.  
  10447.  LTRIM       (char_expr)                     Data without leading blanks.
  10448.  
  10449.  REPLICATE   (char_expr, int_expr)           A character expression
  10450.                                              repeated a specified number of
  10451.                                              times. If int_expr is negative,
  10452.                                              a NULL string is returned.
  10453.  
  10454.  RIGHT       (char_expr, int_expr)           Part of a character string
  10455.                                              starting int_expr characters
  10456.                                              from the right. If int_expr is
  10457.                                              negative, a NULL string is
  10458.                                              returned.
  10459.  
  10460.  Function    Parameters                      Result
  10461.  ────────────────────────────────────────────────────────────────────────────
  10462. 
  10463.  RTRIM       (char_expr)                     Data without trailing blanks.
  10464.  
  10465.  SOUNDEX     (char_expr)                     A four-digit (SOUNDEX) code
  10466.                                              for use in evaluating the
  10467.                                              similarity of two strings.
  10468.  
  10469.  SPACE       (int_expr)                      A string of repeated spaces.
  10470.                                              The number of spaces is equal
  10471.                                              to int_expr. If int_expr is
  10472.                                              negative, a NULL string is
  10473.                                              returned.
  10474.  
  10475.  STR         (float_expr [, length [,        Character data converted from
  10476.              decimal]])                      numeric data. The length is
  10477.                                              the total length, including
  10478.                                              decimal point, sign, digits,
  10479.                                              and spaces. The decimal value
  10480.                                              is the number of spaces to the
  10481.  Function    Parameters                      Result
  10482.  ────────────────────────────────────────────────────────────────────────────
  10483.                                             is the number of spaces to the
  10484.                                              right of the decimal point.
  10485.  
  10486.  STUFF       (char_expr1, start, length,     Data where length characters
  10487.              char_expr2)                     have been deleted from
  10488.                                              char_expr1 at start and where
  10489.                                              char_expr2 has been inserted
  10490.                                              into char_expr1 at start. Not
  10491.                                              implemented for text data.
  10492.  
  10493.  SUBSTRING   (expression, start, length)     Part of a character or binary
  10494.                                              string. The first parameter
  10495.                                              can be a character or binary
  10496.                                              string, a column name, or an
  10497.                                              expression that includes a
  10498.                                              column name. (Do not use
  10499.                                              expressions that include
  10500.                                              functions or subqueries.) The
  10501.                                              second parameter specifies the
  10502.  Function    Parameters                      Result
  10503.  ────────────────────────────────────────────────────────────────────────────
  10504.                                             second parameter specifies the
  10505.                                              position at which the
  10506.                                              substring begins. The third
  10507.                                              parameter specifies the number
  10508.                                              of characters in the substring.
  10509.                                              Not implemented for text data.
  10510.  
  10511.  UPPER       (char_expr)                     Uppercase converted from
  10512.                                              lowercase.
  10513.  
  10514.  ++                                          The + option indicates two or
  10515.                                              more concatenated character
  10516.                                              strings, binary strings,
  10517.                                              column names, or a combination
  10518.                                              of them. Enclose character
  10519.                                              strings in single or double
  10520.                                              quotation marks.
  10521.  
  10522.  ────────────────────────────────────────────────────────────────────────────
  10523.  Function    Parameters                      Result
  10524.  ────────────────────────────────────────────────────────────────────────────
  10525. ────────────────────────────────────────────────────────────────────────────
  10526.  
  10527.  
  10528.  
  10529.  
  10530.  Examples
  10531.  
  10532.    A. select Name = (au_lname + ", " + au_fname)
  10533.     from authors
  10534.  
  10535.  Example A displays author names under the column heading Name in last-name,
  10536.  first-name order with a comma after the last name, that is, Bennet, Abraham.
  10537.  
  10538.  
  10539.    B. select au_lname, substring(au_fname, 1, 1)
  10540.     from authors
  10541.  
  10542.  Example B displays the last name and first initial of each author, that is,
  10543.  Bennet A.
  10544.  
  10545.    C. select (au_lname + ", " + substring(au_fname,
  10546.    1, 1) + ".")
  10547.     from authors
  10548.  
  10549.  Example C displays the last name and first initial of each author, that is,
  10550.  Bennet, A.
  10551.  
  10552.    D. select substring ((pub_id + title_id), 1, 6)
  10553.     from titles
  10554.  
  10555.  Example D displays the first six characters of the concatenation of each
  10556.  pub_id and title_id. In other words, it displays all four characters of the
  10557.  pub_id and the first two characters of the title_id.
  10558.  
  10559.    E. select charindex("wonderful", notes)
  10560.     from titles
  10561.     where title_id = "TC3218"
  10562.  
  10563.  Example E returns the position at which the pattern "wonderful" begins in
  10564.  the notes column of titles.
  10565.  
  10566.     Statement       Result
  10567.  
  10568.    F. select right("abcde", 3)   cde
  10569.     select right("abcde", 6)   abcde
  10570.  
  10571.    G. select str(123.45, 6, 2)   123.45
  10572.  
  10573.    H. select stuff("abc", 2, 1, "xyz") axyzc
  10574.  
  10575.  
  10576.  Comments
  10577.  
  10578.  All string functions and concatenation can be used on char, varchar, binary,
  10579.  and varbinary datatypes. In addition, the REPLICATE and RIGHT string
  10580.  functions can be used on datetime datatypes.
  10581.  
  10582.  String functions can be nested. See examples C and D.
  10583.  
  10584.  When you use constants with string functions, enclose them in single or
  10585.  double quotation marks.
  10586.  
  10587.  String functions can be used in the select list, in the WHERE clause, and
  10588.  anywhere an expression is allowed.
  10589.  
  10590.  Length and decimal parameters to STR (if supplied) should be non-negative.
  10591.  The default length is 10. The number is rounded to an integer by default or
  10592.  if the decimal parameter is 0. The specified length should be at least equal
  10593.  to or greater than the part of the number before the decimal point plus the
  10594.  number's sign (if any). If float_expr exceeds the specified length, the
  10595.  string returns "**" of the specified length:
  10596.  
  10597.    select str(123.45, 2, 2)
  10598.     --
  10599.     **
  10600.     (1 row affected)
  10601.  
  10602.  Do not use a function or subquery as the float_expr in the STR function. A
  10603.  short float_expr is right justified in the specified length, and a long
  10604.  float_expr is truncated to the specified number of decimal places.
  10605.  
  10606.  The STUFF function inserts a string into another string. It deletes a
  10607.  specified length of characters from char_expr1 at the start position. It
  10608.  then inserts char_expr2 into char_expr1 at the start position.
  10609.  
  10610.  If the start position or the length is negative, a NULL string is returned.
  10611.  
  10612.  
  10613.  If the start position is longer than char_expr1, a NULL string is returned.
  10614.  
  10615.  
  10616.  If the length to delete is longer than char_expr1, it is deleted to the
  10617.  first character in char_expr1:
  10618.  
  10619.    select stuff("abc", 2, 3, "xyz")
  10620.     ──
  10621.     axyz
  10622.  
  10623.     (1 row affected)
  10624.  
  10625.  The SOUNDEX function converts an alpha string to a four-digit code for
  10626.  comparison use. All characters that are nonalpha will be used to terminate
  10627.  the compression. This function always returns some value.
  10628.  
  10629.  The DIFFERENCE function compares two strings and evaluates the similarity
  10630.  between them, returning a value from 0 to 4. Four is the best match. For
  10631.  example:
  10632.  
  10633. ╓┌──────────────┌────────────────────────────────────────────────────────────╖
  10634.  ────────────────────────────────────────────────────────────────────────────
  10635.                 select difference("abc", "abc")
  10636.                 ---------
  10637.                 4
  10638.                 (1 row affected)
  10639.                 select difference("abc", "abd")
  10640.                 ---------
  10641.                 3
  10642.                 (1 row affected)
  10643.  
  10644.  
  10645.  
  10646.  See Also
  10647.  
  10648.  Conversion Function, Functions, Mathematical Functions, SELECT, Text/Image
  10649.  Functions
  10650.  
  10651.  
  10652.  Subqueries
  10653.  ────────────────────────────────────────────────────────────────────────────
  10654.  
  10655.  
  10656.  Function
  10657.  
  10658.  Nest a SELECT statement inside a SELECT, INSERT, UPDATE, or DELETE
  10659.  statement, another subquery, or anywhere an expression is allowed (if it
  10660.  returns a single value). A subquery is always enclosed in parentheses.
  10661.  
  10662.  Many SQL statements that include subqueries can be alternatively formulated
  10663.  as joins. (See "Joins" for more information on joining; see "SELECT" for
  10664.  more information on the full SELECT statement; see "WHERE Clause" for more
  10665.  information on search conditions and WHERE clause syntax.)
  10666.  
  10667.  
  10668.  Syntax
  10669.  
  10670.  A subquery can appear anywhere an expression can be used, as long as it
  10671.  returns a single value. It is usually nested inside the WHERE or HAVING
  10672.  clause of an outer SELECT, INSERT, UPDATE, DELETE, or another subquery. A
  10673.  subquery can be used in the following contexts:
  10674.  
  10675.    A. expression comparison_operator [ANY | ALL] (subquery)
  10676.  
  10677.    B. expression [NOT] IN (subquery)
  10678.  
  10679.    C. [NOT] EXISTS (subquery)
  10680.  
  10681.  A subquery has the following restricted SELECT syntax:
  10682.  
  10683.    ( SELECT [DISTINCT] subquery_select_list
  10684.      [FROM [[database.]owner.]{table_name | view_name} [HOLDLOCK]
  10685.      [, [[database.]owner.]{table_name | view_name} [HOLDLOCK]]...]
  10686.      [WHERE search_conditions]
  10687.      [GROUP BY aggregate_free_expression
  10688.      [, aggregate_free_expression]...]
  10689.      [HAVING search_conditions] )
  10690.  
  10691.  
  10692.  Examples
  10693.  
  10694.    A. select distinct pub_name
  10695.     from publishers
  10696.     where pub_id in
  10697.      (select pub_id
  10698.      from titles
  10699.      where type = "business")
  10700.  
  10701.    B. select distinct pub_name
  10702.     from publishers
  10703.     where "business" in
  10704.      (select type
  10705.      from titles
  10706.      where pub_id = publishers.pub_id)
  10707.  
  10708.    C. select distinct pub_name
  10709.     from publishers
  10710.     where exists
  10711.      (select *
  10712.      from titles
  10713.      where pub_id = publishers.pub_id
  10714.      and type = "business")
  10715.  
  10716.  Examples A, B, and C find the names of publishers who have published
  10717.  business books.
  10718.  
  10719.    D. select au_lname, au_fname
  10720.     from authors
  10721.     where au_id in
  10722.      (select au_id
  10723.      from titleauthor
  10724.      where title_id in
  10725.       (select title_id
  10726.       from titles
  10727.       where type = "popular_comp"))
  10728.  
  10729.  Example D finds the names of authors who have participated in writing at
  10730.  least one popular computing book.
  10731.  
  10732.    E. select title
  10733.     from titles
  10734.     where advance > all
  10735.      (select advance
  10736.      from titles, publishers
  10737.      where titles.pub_id = publishers.pub_id
  10738.      and pub_name = "Algodata Infosystems")
  10739.  
  10740.  Example E finds the books that commanded an advance greater than the largest
  10741.  advance paid by Algodata Infosystems.
  10742.  
  10743.    F. select au_lname, au_fname
  10744.     from authors
  10745.     where city = any
  10746.      (select city
  10747.      from publishers)
  10748.  
  10749.  Example F finds the authors that live in the same city as some publisher.
  10750.  
  10751.  
  10752.  Options
  10753.  
  10754.  DISTINCT
  10755.    Cannot be used with subqueries that include a GROUP BY clause.
  10756.  
  10757.  subquery_select_list
  10758.    A restricted select list for use with subqueries introduced with IN or a
  10759.    comparison operator. It consists of one expression. If a column name is
  10760.    used in the WHERE clause of the outer statement, a column name in the
  10761.    subquery select list must be join compatible with it.
  10762.  
  10763.    The select list must consist of only one column name except for the EXISTS
  10764.    subquery, in which case the asterisk (*) is usually used in place of the
  10765.    single column name. Do not specify more than one column name.
  10766.  
  10767.  expression
  10768.    A column name; a constant; a function; any combination of column names,
  10769.    constants, and functions connected by arithmetic or bitwise operators; or
  10770.    a subquery. (The three formats for subqueries in the "Syntax" section are
  10771.    all considered expressions and can be substituted wherever the word
  10772.    "expression" appears as long as the subquery returns a single value.) The
  10773.    arithmetic operators are as follows:
  10774.  
  10775. ╓┌───────────────────────┌───────────────────────────────────────────────────╖
  10776.  Symbol                  Meaning
  10777.  ────────────────────────────────────────────────────────────────────────────
  10778.  ++                      Addition
  10779.  -                       Subtraction
  10780.  *                       Multiplication
  10781.  /                       Division
  10782.  %                       Modulo
  10783.  ────────────────────────────────────────────────────────────────────────────
  10784.  
  10785.  
  10786.    Addition, subtraction, division, and multiplication can be used on int,
  10787.    smallint, tinyint, float, and money columns. The modulo operator can be
  10788.    used with all integer datatypes but cannot be used with money or float
  10789.    columns. A modulo is the integer remainder after dividing two integers.
  10790.    For example, 21 % 9 = 3 because 21 divided by 9 equals 2 with a remainder
  10791.    of 3. The bitwise operators are as follows:
  10792.  
  10793.  Symbol           Meaning
  10794.  ────────────────────────────────────────────────────────────────────────────
  10795.  &                Bitwise and (two operands)
  10796.  |                Bitwise or (two operands)
  10797.                   Bitwise exclusive or (two operands)
  10798.  ~                Bitwise not (one operand)
  10799.  ────────────────────────────────────────────────────────────────────────────
  10800.  
  10801.    The bitwise operators can be used on int, smallint, and tinyint columns
  10802.    only. All the bitwise operators translate the integer parameters into
  10803.    binary representation before evaluating them. (See "Expressions" for more
  10804.    information.)
  10805.  
  10806.  comparison_operator
  10807.    One of the following:
  10808.  
  10809. ╓┌────────────────────┌──────────────────────────────────────────────────────╖
  10810.  Symbol               Meaning
  10811.  ────────────────────────────────────────────────────────────────────────────
  10812.  =                    Equal to
  10813.  >                    Greater than
  10814.  <                    Less than
  10815.  >=                   Greater than or equal to
  10816.  <=                   Less than or equal to
  10817.  Symbol               Meaning
  10818.  ────────────────────────────────────────────────────────────────────────────
  10819. <=                   Less than or equal to
  10820.  !=                   Not equal to
  10821.  !>                   Not greater than
  10822.  !<                   Not less than
  10823.  ────────────────────────────────────────────────────────────────────────────
  10824.  
  10825.  
  10826.    In comparing char and varchar data, < means closer to the beginning of the
  10827.    alphabet, and > means closer to the end of the alphabet. Case and special
  10828.    character evaluations depend on the collating sequence of the machine on
  10829.    which SQL Server is located. On ASCII machines, for example, lowercase
  10830.    letters are greater than uppercase letters, and uppercase letters are
  10831.    greater than numbers. In comparing dates, < means earlier and > means
  10832.    later. Trailing blanks are ignored for comparison purposes. So, for
  10833.    example, "Dirk" is the same as "Dirk  ". Put quotation marks around all
  10834.    character and datetime data used with a comparison operator (= "Bennet", >
  10835.    "94609").
  10836.  
  10837.  ANY
  10838.    Is used with >, <, or = and a subquery. > ANY is true (and the subquery
  10839.    returns results) when the value in the outer query is greater than any
  10840.    value in the list (that is, greater than the minimum value in the list). <
  10841.    ANY is true (and the subquery returns results) when the value in the outer
  10842.    query is less than any value in the list (that is, less than the maximum
  10843.    value in the list).
  10844.  
  10845.  ALL
  10846.    Is used with > or < and a subquery. > ALL means that the value being
  10847.    compared must be greater than all values in the list (that is, greater
  10848.    than the maximum value). < ALL means that the value being compared must be
  10849.    less than all values in the list (that is, less than the minimum value).
  10850.  
  10851.  IN
  10852.    Returns results when any value in the subquery matches the value in the
  10853.    WHERE clause of the outer statement. (IN is equivalent to = ANY.) IN can
  10854.    also be used with a list of values enclosed in parentheses. See "WHERE
  10855.    Clause" for details.
  10856.  
  10857.  subquery
  10858.    A query with the restricted SELECT syntax shown in the "Examples" section.
  10859.    It cannot include ORDER BY or COMPUTE clauses or the INTO keyword. If it
  10860.    is introduced by an unmodified comparison operator (and therefore returns
  10861.    a single value), it cannot include GROUP BY and HAVING clauses. The select
  10862.    list must consist of only one column name, except for the EXISTS subquery,
  10863.    in which case the asterisk (*) is almost always used. (SQL Server does not
  10864.    check the select list of a subquery introduced with EXISTS.) A subquery
  10865.    can contain another subquery. A subquery is legal in any TRANSACT-SQL
  10866.    syntax that allows an expression as long as it returns a single value.
  10867.  
  10868.  EXISTS
  10869.    Is used with a subquery to test for the existence of some result from the
  10870.    subquery. EXISTS is not preceded by an expression. The subquery that
  10871.    follows the EXISTS keyword is enclosed in parentheses. If the NOT keyword
  10872.    is used, the WHERE clause is satisfied if no rows are returned by the
  10873.    subquery. Since EXISTS tests only for the existence of a condition, the
  10874.    asterisk (*) is almost always used as the select list.
  10875.  
  10876.  
  10877.  Comments
  10878.  
  10879.  A subquery can be used anywhere an expression can be used if it returns a
  10880.  single value.
  10881.  
  10882.  Note that the syntax for EXISTS is somewhat different than the syntax for
  10883.  the other keywords; it does not take an expression between WHERE and itself.
  10884.  The two queries following, which are semantically equivalent, illustrate the
  10885.  difference:
  10886.  
  10887.    A. select distinct pub_name
  10888.     from publishers
  10889.     where exists
  10890.      (select *
  10891.      from titles
  10892.      where pub_id = publishers.pub_id
  10893.      and type = "business")
  10894.  
  10895.    B. select distinct pub_name
  10896.     from publishers
  10897.     where pub_id in
  10898.      (select pub_id
  10899.      from titles
  10900.      where type = "business")
  10901.  
  10902.  A correlated (or repeating) subquery depends on the outer query for its
  10903.  values. It is executed repeatedly, once for each row that might be selected
  10904.  by the outer query. Here's an example:
  10905.  
  10906.    select distinct au_lname, au_fname
  10907.     from authors
  10908.     where 100 in
  10909.      (select royaltyper
  10910.      from titleauthor
  10911.      where titleauthor.au_id = authors.au_id)
  10912.  
  10913.  The subquery in this statement cannot be evaluated independently of the
  10914.  outer query. It needs a value for authors.au_id, but this value changes as
  10915.  SQL Server examines different rows in authors.
  10916.  
  10917.  A correlated subquery can also be used in the HAVING clause of an outer
  10918.  query. This example finds the types of books for which the maximum advance
  10919.  is more than twice the average for the group:
  10920.  
  10921.    select t1.type
  10922.     from titles t1
  10923.     group by t1.type
  10924.     having max(t1.advance) >=all
  10925.      (select 2 * avg(t2.advance)
  10926.      from titles t2
  10927.      where t1.type = t1.type)
  10928.  
  10929.  
  10930.  See Also
  10931.  
  10932.  DELETE, EXECUTE, Expressions, Functions, GROUP BY and HAVING Clauses,
  10933.  INSERT, Joins, SELECT, UPDATE, WHERE Clause
  10934.  
  10935.  
  10936.  System Functions
  10937.  ────────────────────────────────────────────────────────────────────────────
  10938.  
  10939.  
  10940.  Function
  10941.  
  10942.  Return special information from the database.
  10943.  
  10944.  
  10945.  Syntax
  10946.  
  10947.    function_name(parameters)
  10948.  
  10949.  Function names, parameters, and results are listed in the following table.
  10950.  Where the parameter is optional, the function returns the current value.
  10951.  
  10952.  
  10953.  
  10954. ╓┌────────────┌──────────────────────────────┌───────────────────────────────╖
  10955.  Function     Parameters                     Result
  10956.  ────────────────────────────────────────────────────────────────────────────
  10957.  COL_LENGTH   ("object name", "column name")  The length of column
  10958.  
  10959.  COL_NAME     (object ID #, column ID #)     The column name
  10960.  
  10961.  DB_ID        (["database name"])            The database ID #
  10962.  
  10963.  DB_NAME      ([database ID #])              The database name
  10964.  Function     Parameters                     Result
  10965.  ────────────────────────────────────────────────────────────────────────────
  10966. DB_NAME      ([database ID #])              The database name
  10967.  
  10968.  HOST_ID      ( )                            The workstation ID #
  10969.  
  10970.  HOST_NAME    ( )                            The workstation name
  10971.  
  10972.  INDEX_COL    ("object name", index ID #,    The indexed column name
  10973.               key #)
  10974.  
  10975.  ISNULL       (expression, value)            NULL entries with the
  10976.                                              specified value
  10977.  
  10978.  OBJECT_ID    ("database object name")       The database object ID #
  10979.  
  10980.  OBJECT_NAME  (database object ID #)         The database object name
  10981.  
  10982.  SUSER_ID     (["server username"])          The user's login ID #
  10983.  
  10984.  SUSER_NAME   ([server user ID #])           The user's login ID
  10985.  Function     Parameters                     Result
  10986.  ────────────────────────────────────────────────────────────────────────────
  10987. SUSER_NAME   ([server user ID #])           The user's login ID
  10988.  
  10989.  USER_ID      (["username"])                 The user's database ID number
  10990.  
  10991.  USER_NAME    ([user ID number])             The username
  10992.  
  10993.  ────────────────────────────────────────────────────────────────────────────
  10994.  
  10995.  
  10996.  
  10997.  
  10998.  Examples
  10999.  
  11000.    A. select user_id("harold")
  11001.  
  11002.  Example A returns harold's ID #, 13.
  11003.  
  11004.    B. select user_name(13)
  11005.  
  11006.  Example B returns the login ID for user #13, harold
  11007.  
  11008.    C. select avg(isnull(price, $10.00))
  11009.     from titles
  11010.  
  11011.  Example C finds the average of the prices of all titles, substituting the
  11012.  value $10.00 for all NULL entries in price.
  11013.  
  11014.    D. select isnull(price, 0)
  11015.     from titles
  11016.  
  11017.  Example D finds all rows with null values in price and displays the null
  11018.  value as "0".
  11019.  
  11020.  
  11021.  Comments
  11022.  
  11023.  When the parameter to a system function is optional, the current database,
  11024.  host computer, server user, or database user is assumed. Built-in functions
  11025.  must always be followed by parentheses.
  11026.  
  11027.  The system functions can be used in the select list, in the WHERE clause,
  11028.  and anywhere an expression is allowed.
  11029.  
  11030.  The System Administrator's server user ID is always 1.
  11031.  
  11032.  Server user ID -1 is always reserved for a guest account.
  11033.  
  11034.  The user ID of the Database Owner is always 1.
  11035.  
  11036.  The USER_NAME function returns the name of the current user if the parameter
  11037.  is less than zero.
  11038.  
  11039.  
  11040.  See Also
  11041.  
  11042.  Functions, SELECT
  11043.  
  11044.  
  11045.  System Procedures
  11046.  ────────────────────────────────────────────────────────────────────────────
  11047.  
  11048.  
  11049.  Function
  11050.  
  11051.  Stored procedures that SQL Server supplies to update and report from the
  11052.  system tables.
  11053.  
  11054.  
  11055.  Comments
  11056.  
  11057.  Chapter 2 of this manual contains a complete explanation of each system
  11058.  procedure.
  11059.  
  11060.  System procedures are created in the master database. They are owned by the
  11061.  master Database Owner.
  11062.  
  11063.  Since system procedures are located in the master database, their
  11064.  permissions are also set there. In other words, the System Administrator or
  11065.  master Database Owner must set up permissions for the system procedures in
  11066.  the master database, not from user databases.
  11067.  
  11068.  You can run system procedures from any database. If a system procedure is
  11069.  executed from a database other than master, any references to system tables
  11070.  are mapped to the database from which the procedure is being run. For
  11071.  example, if the Database Owner of pubs runs sp_adduser from pubs, the new
  11072.  user is added to pubs..sysusers.
  11073.  
  11074.  The names of all system procedures begin with "sp_".
  11075.  
  11076.  You can create your own system procedures that can be executed from any
  11077.  database. Simply create a stored procedure in master and give it a name that
  11078.  begins with "sp_".
  11079.  
  11080.  System procedures (for example, sp_help) do not work on temporary tables
  11081.  because a procedure cannot access a temporary object unless it created the
  11082.  object itself during the current session.
  11083.  
  11084.  Some of the system procedures can be run only by Database Owners. These
  11085.  procedures make sure that the user executing the procedure is the owner of
  11086.  the database from which they are being executed.
  11087.  
  11088.  Other system procedures can be executed by any user who has been granted
  11089.  EXECUTE permission on them, but this permission must be granted in master.
  11090.  Therefore, a user can either have permission to execute a system procedure
  11091.  in all databases or in none of them.
  11092.  
  11093.  Authorized SQL Server users (those who have a login ID) who are not listed
  11094.  in master..sysusers are treated as guest in master. The guest user inherits
  11095.  the permissions granted to public (in the setup program), including
  11096.  permission to execute certain system procedures (for example, all the
  11097.  sp_help procedures).
  11098.  
  11099.  To deny a user permission on a system procedure, the System Administrator or
  11100.  master Database Owner must add him or her to master..sysusers and write a
  11101.  REVOKE statement on that procedure. Thus, the owner of a user database
  11102.  cannot directly control permissions on the system procedures within his or
  11103.  her own database.
  11104.  
  11105.  When the parameter value for a system procedure contains punctuation marks
  11106.  or embedded blanks, it must be enclosed in single or double quotation marks.
  11107.  When the parameter is an object name and the object name is qualified by a
  11108.  database name or owner name, the entire name must be enclosed in single or
  11109.  double quotation marks.
  11110.  
  11111.  Several system procedure tables in the master database are used by the
  11112.  system procedures to convert internal system values (for example, status
  11113.  bits) into readable English. One of them, spt_values, is used by a wide
  11114.  variety of system procedures: sp_configure, sp_dboption, sp_depends,
  11115.  sp_help, sp_helpdb, sp_helpdevice, sp_helpindex, sp_helpkey, sp_helprotect,
  11116.  and sp_lock. The table spt_values is never updated. To see how it is used,
  11117.  execute sp_helptext to look at the text for one of the system procedures
  11118.  that references it. The other system procedure tables are spt_monitor and
  11119.  spt_committab. In addition, several of the system procedures create and then
  11120.  drop temporary tables: sp_helpdb creates #spdbdesc, sp_helpdevice creates
  11121.  #spdevtab, and sp_helpindex creates #spindtab.
  11122.  
  11123.  See Chapter 2, "System Procedures," for a complete description of the system
  11124.  procedures.
  11125.  
  11126.  
  11127.  See Also
  11128.  
  11129.  CREATE PROCEDURE, EXECUTE
  11130.  
  11131.  
  11132.  
  11133.  
  11134.  
  11135.  Text/Image Datatypes
  11136.  ────────────────────────────────────────────────────────────────────────────
  11137.  
  11138.  
  11139.  Function
  11140.  
  11141.  The text datatype stores extremely long (up to 231- 1 or 2,147,483,647) byte
  11142.  strings of printable characters.
  11143.  
  11144.  The image datatype stores extremely long (up to 231- 1 or 2,147,483,647)
  11145.  bytes of hexadecimal-encoded binary data.
  11146.  
  11147.  
  11148.  Syntax
  11149.  
  11150.  The data values for a column of type text or image are stored and managed as
  11151.  a linked list of data pages, but they appear to the user as if they were
  11152.  stored in a table row. A text or image datatype definition does not include
  11153.  a length.
  11154.  
  11155.    column_name {text | image}
  11156.  
  11157.  
  11158.  Examples
  11159.  
  11160.    A. create table texttest
  11161.     (title_id varchar(6), blurb text, pub_id char(4))
  11162.  
  11163.    B. insert texttest values
  11164.     ("BU7832", "Straight Talk About Computers is an annotated
  11165.     analysis of what computers can do for you: a no-hype guide
  11166.     for the critical user.", "1389")
  11167.  
  11168.    C. create table imagetest
  11169.     (image_id varchar(6), imagecol image, graphic_id char(4))
  11170.  
  11171.    D. insert imagetest values
  11172.     ("94732", 0xFFFF1111222223333444445555566666FFFFF, "1389")
  11173.  
  11174.    E. select blurb from texttest
  11175.     where blurb like "Straight Talk%"
  11176.  
  11177.  
  11178.  Comments
  11179.  
  11180.  When you initialize a text or image column with an INSERT, UPDATE, or
  11181.  WRITETEXT, SQL Server assigns a text pointer and allocates an entire data
  11182.  page, 2K bytes, to hold the text or image value.
  11183.  
  11184.  Text values, like char and varchar values, require quotation marks in an
  11185.  INSERT statement.
  11186.  
  11187.  SELECT statements returning text and image values return all the data, up to
  11188.  the limit specified in the global variable @TEXTSIZE. The SET TEXTSIZE
  11189.  statement is used to change this value. The initial value of @TEXTSIZE is
  11190.  32K; the maximum value for @TEXTSIZE is 231- 1 (that is, 2,147,483,647).
  11191.  
  11192.  You can explicitly convert text values to char or varchar with the CONVERT
  11193.  function, but you are limited to the maximum length of these datatypes, 255
  11194.  bytes. If you do not specify a length, the converted value has a default
  11195.  length of 30 characters. Implicit conversion is not supported.
  11196.  
  11197.  Text and image values are stored on a linked list of data pages. A text or
  11198.  image column in a table contains a pointer to the first data page or value
  11199.  fragment for that entry, and thus to the entire text or image value.
  11200.  
  11201.  Image values, like binary and varbinary values, must be preceded by "0x" for
  11202.  an INSERT statement.
  11203.  
  11204.  You can explicitly convert image values to binary or varbinary with the
  11205.  CONVERT function, but you are limited to the maximum length of these
  11206.  datatypes, 255 bytes. If you do not specify a length, the converted value
  11207.  has a default length of 30 bytes. Implicit conversion is not supported.
  11208.  
  11209.  There are a number of restrictions on the use of text and image columns.
  11210.  They cannot be used under the following conditions:
  11211.  
  11212.  
  11213.    ■   As parameters to stored procedures. It is also illegal to pass text or
  11214.        image values to stored procedures.
  11215.  
  11216.    ■   As local variables.
  11217.  
  11218.    ■   In an ORDER BY clause.
  11219.  
  11220.    ■   In a COMPUTE clause.
  11221.  
  11222.    ■   In a GROUP BY clause.
  11223.  
  11224.    ■   In an index.
  11225.  
  11226.    ■   In a search clause (WHERE clause) except with the LIKE expression.
  11227.  
  11228.    ■   In a join clause.
  11229.  
  11230.  
  11231.  Use the LIKE expression to search for a text pattern.
  11232.  
  11233.  Use the PATINDEX function to search for the starting position of the first
  11234.  occurrence of a specified pattern in a text column.
  11235.  
  11236.  
  11237.  See Also
  11238.  
  11239.  Datatypes, Functions, READTEXT, Text/Image Functions, WRITETEXT
  11240.  
  11241.  
  11242.  Text/Image Functions
  11243.  ────────────────────────────────────────────────────────────────────────────
  11244.  
  11245.  
  11246.  Function
  11247.  
  11248.  Return values commonly needed for operations on text and image data. Text
  11249.  and image built-in function names are not keywords.
  11250.  
  11251.  
  11252.  Syntax
  11253.  
  11254.    function_name(parameter)
  11255.  
  11256.  Function names, parameters, and results are listed in the following table.
  11257.  
  11258.  
  11259. ╓┌─────────────┌──────────────────────────────┌──────────────────────────────╖
  11260.  Function      Parameters                     Result
  11261.  ────────────────────────────────────────────────────────────────────────────
  11262.  PATINDEX      ("pattern", column_name)       The starting position of the
  11263.                                               first occurrence of pattern
  11264.                                               in the specified column. You
  11265.                                               can use wildcard characters
  11266.                                               in pattern.
  11267.  
  11268.  SET TEXTSIZE  {n 0}                          The limit, in bytes, of the
  11269.                                               text or image data to be
  11270.                                               returned with a SELECT
  11271.                                               statement. The current
  11272.                                               setting is stored in the
  11273.                                               @TEXTSIZE global variable. n
  11274.                                               is an integer that specifies
  11275.                                               the limit on the number of
  11276.                                               bytes to be returned. 0
  11277.                                               restores the default limit
  11278.                                               (32K bytes.)
  11279.  Function      Parameters                     Result
  11280.  ────────────────────────────────────────────────────────────────────────────
  11281.                                              (32K bytes.)
  11282.  
  11283.  
  11284.  
  11285.  TEXTPTR       (column_name)                  The text pointer value in
  11286.                                               varbinary format. The text
  11287.                                               pointer is checked to ensure
  11288.                                               that it points to the first
  11289.                                               text page.
  11290.  
  11291.  TEXTVALID     ("table_name.column_name",     Checks if a given text
  11292.                text_ptr)                      pointer is valid and returns
  11293.                                               1 if the pointer is valid and
  11294.                                               0 if the pointer is invalid.
  11295.                                               Note that the identifier for
  11296.                                               the text column must include
  11297.                                               the table name.
  11298.  
  11299.  ────────────────────────────────────────────────────────────────────────────
  11300.  Function      Parameters                     Result
  11301.  ────────────────────────────────────────────────────────────────────────────
  11302. ────────────────────────────────────────────────────────────────────────────
  11303.  
  11304.  
  11305.  
  11306.  
  11307.  Examples
  11308.  
  11309.    A. declare @val varbinary(30)
  11310.     select @val = textptr(blurb) from texttest
  11311.     where title_id = "BU7832"
  11312.     readtext texttest.blurb @val 1 5
  11313.  
  11314.  Example A uses the TEXTPTR function to locate the text column, blurb,
  11315.  associated with title_id BU7832 in the texttest table. The text pointer is
  11316.  put into a local variable @val and supplied as a parameter to the READTEXT
  11317.  statement, which returns 5 bytes starting at the second byte (offset of 1).
  11318.  
  11319.  
  11320.    B. set textsize 100
  11321.  
  11322.    C. select title_id, textptr(blurb) from texttest
  11323.  
  11324.    D. select textvalid ("texttest.blurb", textptr(blurb))
  11325.      from texttest
  11326.  
  11327.  
  11328.  Comments
  11329.  
  11330.  "SELECT textptr(column_name) from my_table" returns a long binary string. It
  11331.  is a good idea to put this string into a local variable, as in the first
  11332.  example, and use it by reference.
  11333.  
  11334.  Explicit conversion is supported from text to char, and image to binary and
  11335.  varbinary, but you are limited to the maximum length for these datatypes,
  11336.  255 bytes. If you do not specify a length, the default value of 30 bytes is
  11337.  used. Implicit conversion of text or image to another datatype is not
  11338.  supported.
  11339.  
  11340.  You cannot create an index on a text or image column.
  11341.  
  11342.  A text value cannot appear in a search clause or join clause, except in
  11343.  WHERE clauses using LIKE.
  11344.  
  11345.  A text value cannot be a local variable or a parameter to a stored
  11346.  procedure. It is also illegal to pass text values to stored procedures.
  11347.  
  11348.  ORDER BY cannot be specified on text type columns.
  11349.  
  11350.  
  11351.  See Also
  11352.  
  11353.  Conversion Function, Functions, Mathematical Functions, READTEXT, String
  11354.  Functions, Text/Image Datatypes
  11355.  
  11356.  
  11357.  TRUNCATE TABLE
  11358.  ────────────────────────────────────────────────────────────────────────────
  11359.  
  11360.  
  11361.  Function
  11362.  
  11363.  This example removes all rows in a table as quickly as possible.
  11364.  
  11365.  
  11366.  Syntax
  11367.  
  11368.    TRUNCATE TABLE [[database.]owner.]table_name
  11369.  
  11370.  
  11371.  Examples
  11372.  
  11373.    truncate table authors
  11374.  
  11375.  This example removes all data in the authors table.
  11376.  
  11377.  
  11378.  Comments
  11379.  
  11380.  TRUNCATE TABLE restores the database to the same state it would be in if the
  11381.  CREATE TABLE and CREATE INDEX statements had been run on the table.
  11382.  
  11383.  The table, though empty of data, continues to exist until you give a DROP
  11384.  TABLE statement.
  11385.  
  11386.  TRUNCATE TABLE and DELETE without any conditions are functionally
  11387.  equivalent, but TRUNCATE TABLE is faster. TRUNCATE TABLE removes whole data
  11388.  pages and is not logged; DELETE removes rows one at a time and logs these
  11389.  transactions. Both DELETE and TRUNCATE TABLE reclaim the space occupied by
  11390.  the data and its associated indexes.
  11391.  
  11392.  A TRUNCATE TABLE statement is not caught by a DELETE trigger. Although a
  11393.  TRUNCATE TABLE statement is, in effect, like a DELETE without a WHERE clause
  11394.  (it removes all rows), it is not logged, and therefore cannot "fire" a
  11395.  trigger.
  11396.  
  11397.  Since permission for the TRUNCATE TABLE statement defaults to the table
  11398.  owner and is not transferable, only the table owner need worry about
  11399.  inadvertently circumventing a DELETE trigger with a TRUNCATE TABLE
  11400.  statement.
  11401.  
  11402.  TRUNCATE TABLE permission defaults to the table owner and is not
  11403.  transferable.
  11404.  
  11405.  
  11406.  See Also
  11407.  
  11408.  DELETE, DROP TABLE
  11409.  
  11410.  
  11411.  UPDATE
  11412.  ────────────────────────────────────────────────────────────────────────────
  11413.  
  11414.  
  11415.  Function
  11416.  
  11417.  Changes data in existing rows, either by adding new data or modifying
  11418.  existing data.
  11419.  
  11420.  
  11421.  Syntax
  11422.  
  11423.    UPDATE [[database.]owner.]{table_name | view_name}
  11424.      SET [[[database.]owner.]{table_name. | view_name.}]
  11425.      column_name1 = {expression1 | NULL}
  11426.      [, column_name2 = {expression2 | NULL}...]
  11427.      [WHERE search_conditions]
  11428.  
  11429.     UPDATE [[database.]owner.]{table_name | view_name}
  11430.      SET [[[database.]owner.]{table_name. | view_name.}]
  11431.      column_name1 = {expression1 | NULL}
  11432.      [, column_name2 = {expression2 | NULL}...]
  11433.      [FROM [[database.]owner.]{table_name | view_name}
  11434.      [, [[database.]owner.]{table_name | view_name}]...]
  11435.      [WHERE search_conditions]
  11436.  
  11437.  
  11438.  Examples
  11439.  
  11440.    A. update authors
  11441.     set au_lname = "MacBadden"
  11442.     where au_lname = "McBadden"
  11443.  
  11444.  Example A changes all the McBaddens in the authors table to MacBaddens.
  11445.  
  11446.    B. update titles
  11447.     set ytd_sales = ytd_sales + qty
  11448.     from titles, sales
  11449.     where titles.title_id = sales.title_id
  11450.     and sales.date in (select max(sales.date) from sales)
  11451.  
  11452.  Example B modifies ytd_sales column to reflect the most recent sales
  11453.  recorded in the sales table. This assumes that only one set of sales is
  11454.  recorded for a given title on a given date and that updates are up-to-date.
  11455.  
  11456.  
  11457.  
  11458.  Options
  11459.  
  11460.  SET
  11461.    Specifies the value for a particular column name. The value can be an
  11462.    expression or a null value. When more than one column name and value pair
  11463.    is listed, they must be separated by commas.
  11464.  
  11465.  FROM
  11466.    Is required to pull data from other tables or views to modify rows in the
  11467.    table or view specified in the UPDATE clause. A standard FROM clause lists
  11468.    the tables and views referenced in the WHERE clause.
  11469.  
  11470.  WHERE
  11471.    A standard WHERE clause.
  11472.  
  11473.  
  11474.  Comments
  11475.  
  11476.  Use UPDATE to change values in rows that have already been inserted. Use
  11477.  INSERT to add full or partial new rows.
  11478.  
  11479.  UPDATE interacts with the IGNORE_DUP_KEY, IGNORE_DUP_ROW, and ALLOW_DUP_ROW
  11480.  statements set with the CREATE INDEX statement. (See "CREATE INDEX" for more
  11481.  information.)
  11482.  
  11483.  A rule can be created and bound to a column to restrict the domain of legal
  11484.  values that can be entered into it. Rules are created with CREATE RULE and
  11485.  bound with the sp_bindrule system procedure.
  11486.  
  11487.  You can define a trigger that takes a specified action when an UPDATE
  11488.  statement is executed on a specified table or on a specified column of a
  11489.  table.
  11490.  
  11491.  
  11492.  Permissions
  11493.  
  11494.  UPDATE permission defaults to the table or view owner, who can transfer it
  11495.  to other users.
  11496.  
  11497.  
  11498.  See Also
  11499.  
  11500.  CREATE INDEX, CREATE RULE, CREATE TRIGGER, INSERT, WHERE Clause,
  11501.  sp_bindefault, sp_bindrule, sp_unbindefault, sp_unbindrule
  11502.  
  11503.  
  11504.  UPDATE STATISTICS
  11505.  ────────────────────────────────────────────────────────────────────────────
  11506.  
  11507.  
  11508.  Function
  11509.  
  11510.  Updates information about the distribution of key values in specified
  11511.  indexes.
  11512.  
  11513.  
  11514.  Syntax
  11515.  
  11516.    UPDATE STATISTICS table_name [[database.]owner.][index_name]
  11517.  
  11518.  
  11519.  Examples
  11520.  
  11521.    A. update statistics authors
  11522.  
  11523.    B. update statistics authors au_id_ind
  11524.  
  11525.  
  11526.  Comments
  11527.  
  11528.  SQL Server keeps statistics about the distribution of the key values in each
  11529.  index and uses these statistics in its decisions about which index(es) to
  11530.  use in query processing. Use the UPDATE STATISTICS statement when you create
  11531.  an index on already existing data or if a great deal of data in an indexed
  11532.  column has been added, changed, or removed (that is, if you suspect that the
  11533.  distribution of key values has changed).
  11534.  
  11535.  If you do not specify an index name, the statement updates the distribution
  11536.  statistics for all the indexes in the specified table.
  11537.  
  11538.  Since TRANSACT-SQL does not require index names to be unique in a database,
  11539.  you must give the name of the table with which the index is associated.
  11540.  
  11541.  
  11542.  Permissions
  11543.  
  11544.  UPDATE STATISTICS permission defaults to the table owner and is not
  11545.  transferable.
  11546.  
  11547.  
  11548.  See Also
  11549.  
  11550.  CREATE INDEX, sp_helpindex
  11551.  
  11552.  
  11553.  USE
  11554.  ────────────────────────────────────────────────────────────────────────────
  11555.  
  11556.  
  11557.  Function
  11558.  
  11559.  Changes the current database.
  11560.  
  11561.  
  11562.  Syntax
  11563.  
  11564.    USE database_name
  11565.  
  11566.  
  11567.  Examples
  11568.  
  11569.    use pubs
  11570.  
  11571.  This example changes the current database to pubs.
  11572.  
  11573.  
  11574.  Comments
  11575.  
  11576.  USE must be in a batch by itself for you to reference objects in that
  11577.  database.
  11578.  
  11579.  An alias permits a user to use a database by an alias name. Use the
  11580.  sp_addalias system procedure.
  11581.  
  11582.  
  11583.  Permissions
  11584.  
  11585.  Permission for the USE statement is assigned when the Database Owner assigns
  11586.  permission by executing the sp_adduser system procedure.
  11587.  
  11588.  
  11589.  See Also
  11590.  
  11591.  CREATE DATABASE, DROP DATABASE, sp_addalias, sp_adduser, sp_defaultdb
  11592.  
  11593.  
  11594.  Variables (Local and Global)
  11595.  ────────────────────────────────────────────────────────────────────────────
  11596.  
  11597.  
  11598.  Function
  11599.  
  11600.  Defined entities that are assigned values. A local variable is defined with
  11601.  a DECLARE statement and assigned an initial value within the statement batch
  11602.  where it is declared with a SELECT statement. Global variables are
  11603.  predefined.
  11604.  
  11605.  
  11606.  Syntax
  11607.  
  11608.  Local variable declaration:
  11609.  
  11610.    DECLARE @variable_name datatype
  11611.      [, @variable_name datatype...]
  11612.  
  11613.  Assignment of values to a local variable:
  11614.  
  11615.    SELECT @variable_name = expression [, @variable_name = expression...]
  11616.      [FROM clause] [WHERE clause]
  11617.      [GROUP BY clause] [HAVING clause]
  11618.      [ORDER BY clause] [COMPUTE clause]
  11619.  
  11620.  
  11621.  Examples
  11622.  
  11623.    A. declare @veryhigh money
  11624.     select @veryhigh = max(price)
  11625.      from titles
  11626.     if @veryhigh > $20
  11627.      print "Ouch!"
  11628.  
  11629.    B. declare @one varchar(18), @two varchar(18)
  11630.     select @one = "this is one", @two = "this is two"
  11631.     if @one = "this is one"
  11632.      print "you got one"
  11633.     if @two = "this is two"
  11634.      print "you got two"
  11635.     else print "nope"
  11636.  
  11637.  
  11638.  Options
  11639.  
  11640.  @variable_name
  11641.    The name of a variable. Variable names must be preceded by the "at" symbol
  11642.    (@). The variable name conforms to the rules for identifiers except that
  11643.    it can contain only 29 characters. Variables can be used only in place of
  11644.    constants. They cannot be used in place of the names of tables, columns,
  11645.    other database objects, or keywords.
  11646.  
  11647.  datatype
  11648.    A system datatype or a user datatype.
  11649.  
  11650.  
  11651.  Comments
  11652.  
  11653.  Variables cannot be of the text or image datatype.
  11654.  
  11655.  Local variables are declared in the body of a procedure or query with the
  11656.  DECLARE keyword and given values with a SELECT statement.
  11657.  
  11658.  The "@" denotes a local variable name.
  11659.  
  11660.  Local variables are often used in a batch or procedure as counters for WHILE
  11661.  loops or IF...ELSE blocks.
  11662.  
  11663.  The SELECT statement that assigns a value to the local variable usually
  11664.  returns a single value. If the SELECT variable assignment statement returns
  11665.  more than one value, the variable is assigned the last value returned.
  11666.  
  11667.  The SELECT statement that assigns values to variables cannot be used to
  11668.  retrieve data. For example, the following is not legal:
  11669.  
  11670.    /* ILLEGAL STATEMENT
  11671.     ** declare @veryhigh money
  11672.     ** select @veryhigh = max(price), title_id
  11673.     ** from titles */
  11674.  
  11675.  In addition to local variables, there are predefined global variables that
  11676.  can be used without being declared. Global variables are distinguished from
  11677.  local variables by having two @ signs preceding their names, for example,
  11678.  @ERROR, @ROWCOUNT. Many of the global variables report on system activity.
  11679.  Their reports refer to activity since the last time SQL Server was started.
  11680.  The global variables are as follows:
  11681.  
  11682.  @@CONNECTIONS
  11683.    The number of logins or attempted logins since SQL Server was last
  11684.    started.
  11685.  
  11686.  @@CPU_BUSY
  11687.    The amount of time, in ticks, that the CPU has spent doing SQL Server work
  11688.    since the last time SQL Server was started.
  11689.  
  11690.  @@ERROR
  11691.    The last error number generated by the system.
  11692.  
  11693.  @@IDLE
  11694.    The amount of time, in ticks, that SQL Server has been idle since it was
  11695.    last started.
  11696.  
  11697.  @@IO_BUSY
  11698.    The amount of time, in ticks, that SQL Server has spent doing input and
  11699.    output operations since it was last started.
  11700.  
  11701.  @@MAX_CONNECTIONS
  11702.    The maximum number of simultaneous connections that can be made with SQL
  11703.    Server in this computer environment. The user can configure SQL Server for
  11704.    fewer connections with sp_configure.
  11705.  
  11706.  @@NESTLEVEL
  11707.    The nesting level of the current execution (initially zero). Each time a
  11708.    stored procedure calls another stored procedure, the nesting level is
  11709.    incremented. If the maximum of 32 is exceeded, the transaction aborts.
  11710.  
  11711.  @@PACK_RECEIVED
  11712.    The number of input packets read by SQL Server since it was last started.
  11713.  
  11714.  @@PACKET_ERRORS
  11715.    The number of errors that have occurred while SQL Server was sending and
  11716.    receiving packets.
  11717.  
  11718.  @@PACK_SENT
  11719.    The number of output packets written by SQL Server since it was last
  11720.    started.
  11721.  
  11722.  @@PROCID
  11723.    The stored procedure ID of the currently executing procedure.
  11724.  
  11725.  @@ROWCOUNT
  11726.    The number of rows affected by the last statement (includes SELECT,
  11727.    INSERT, UPDATE, DELETE, the CREATE statements, and so on).
  11728.  
  11729.  @@TEXTSIZE
  11730.    The current value of the TEXTSIZE option of the SET statement, which
  11731.    specifies the length in bytes of text to be returned with a SELECT
  11732.    statement.
  11733.  
  11734.  @@TIMETICKS
  11735.    The number of microseconds per tick. The amount of time per tick is
  11736.    machine dependent. Each tick on the operating system is 21.25 milliseconds
  11737.    (1/32 second).
  11738.  
  11739.  @@TOTAL_ERRORS
  11740.    The number of errors that have occurred while SQL Server was reading or
  11741.    writing.
  11742.  
  11743.  @@TOTAL_READ
  11744.    The number of disk reads by SQL Server since it was last started (this
  11745.    includes disk reads only, not cache reads).
  11746.  
  11747.  @@TOTAL_WRITE
  11748.    The number of writes by SQL Server since it was last started.
  11749.  
  11750.  @@TRANCOUNT
  11751.    The number of currently active transactions for the current user.
  11752.  
  11753.  @@VERSION
  11754.    The date of the current version of SQL Server.
  11755.  
  11756.  The @ERROR global variable is commonly used to check the error status
  11757.  (succeeded or failed) of the most recently executed statement. A statement
  11758.  such as "if @ERROR != 0 return" causes an exit if an error had occurred.
  11759.  
  11760.  For information on the contents of many of these global variables, execute
  11761.  the sp_monitor system procedure. See Chapter 2, "System Procedures," for
  11762.  details.
  11763.  
  11764.  
  11765.  See Also
  11766.  
  11767.  DECLARE, Datatypes, PRINT, RAISERROR, sp_monitor
  11768.  
  11769.  
  11770.  Views
  11771.  ────────────────────────────────────────────────────────────────────────────
  11772.  
  11773.  
  11774.  Function
  11775.  
  11776.  Provide an alternate way of looking at data in one or more tables.
  11777.  
  11778.  
  11779.  Syntax
  11780.  
  11781.    CREATE VIEW [owner.]view_name
  11782.      [(column_name [, column_name]...)]
  11783.     AS SELECT_statement
  11784.  
  11785.  
  11786.  Comments
  11787.  
  11788.  See "CREATE VIEW" for syntax, examples, explanations of keywords, and
  11789.  comments on creating a view.
  11790.  
  11791.  You cannot create a trigger on a view.
  11792.  
  11793.  If a view depends on a table (or view) that has been dropped, SQL Server
  11794.  produces an error message if anyone tries to use the view. If a new table
  11795.  (or view) is created to replace the one that has been dropped, the view
  11796.  becomes usable again.
  11797.  
  11798.  There are no restrictions on querying through views, but there are some
  11799.  restrictions on data update through views.
  11800.  
  11801.  Data update statements may not change any column in the view that is a
  11802.  computation.
  11803.  
  11804.  Data update statements referring to a view that includes aggregate functions
  11805.  (a built-in function and a GROUP BY or COMPUTE BY clause) are not allowed.
  11806.  INSERT statements are not allowed unless all NOT NULL columns in the
  11807.  underlying table or view are included in the view through which you are
  11808.  inserting new rows. (SQL Server has no way to supply values for NOT NULL
  11809.  columns in the underlying table or view.) INSERT and UPDATE statements are
  11810.  not allowed unless the columns being updated all belong to the same base
  11811.  table. (It's illegal in SQL to use data update statements on more than one
  11812.  table in a single statement.) However, if a view contains columns from more
  11813.  than one table but the data update statement references columns from only
  11814.  one of the tables, the statement is legal.
  11815.  
  11816.  When you query through a view, SQL Server checks to make sure that all the
  11817.  database objects referenced anywhere in the statement exist, that they are
  11818.  valid in the context of the statement, and that data update statements do
  11819.  not violate data integrity rules. If any of these checks fail, you get an
  11820.  error message. If the checks are successful, it "translates" the view into
  11821.  an action on the underlying table(s).
  11822.  
  11823.  
  11824.  Permissions
  11825.  
  11826.  You can use views as security mechanisms by granting permission on a view
  11827.  but not on one or more of its underlying tables.
  11828.  
  11829.  
  11830.  See Also
  11831.  
  11832.  CREATE VIEW, DROP VIEW, sp_depends, sp_help, sp_helptext, sp_rename
  11833.  
  11834.  
  11835.  WAITFOR
  11836.  ────────────────────────────────────────────────────────────────────────────
  11837.  
  11838.  
  11839.  Function
  11840.  
  11841.  Specifies a specific time, a time interval, or an event for the execution of
  11842.  a statement block, stored procedure, or transaction.
  11843.  
  11844.  
  11845.  Syntax
  11846.  
  11847.    WAITFOR {DELAY "time" | TIME "time" | ERROREXIT | PROCESSEXIT}
  11848.  
  11849.  
  11850.  Examples
  11851.  
  11852.    A. begin
  11853.      waitfor time "14:20"
  11854.      insert chess(next_move)
  11855.      values('Q-KR5')
  11856.      execute sendmail 'judy'
  11857.     end
  11858.  
  11859.  At 2:20 p.m., the chess table will be updated with my next move, and a
  11860.  procedure called sendmail will insert a row in a table owned by Judy and
  11861.  notifies her that a new move now exists in the chess table.
  11862.  
  11863.    B. begin
  11864.      waitfor delay "00:00:10"
  11865.      print "Ten seconds have passed. Your time is up."
  11866.     end
  11867.  
  11868.  
  11869.  Options
  11870.  
  11871.  DELAY
  11872.    Instructs SQL Server to wait until the specified amount of time has
  11873.    passed, up to a maximum of 24 hours.
  11874.  
  11875.  TIME
  11876.    Instructs SQL Server to wait until the specified time.
  11877.  
  11878.  time
  11879.    A time in one of the acceptable formats for datetime data. You cannot
  11880.    specify dates─the date portion of the datetime value is not allowed.
  11881.  
  11882.  ERROREXIT
  11883.    Instructs SQL Server to wait until a kernel or user process terminates
  11884.    abnormally.
  11885.  
  11886.  PROCESSEXIT
  11887.    Instructs SQL Server to wait until a kernel or user process terminates for
  11888.    any reason.
  11889.  
  11890.  
  11891.  Comments
  11892.  
  11893.  After executing the WAITFOR statement, you cannot use your connection to SQL
  11894.  Server until the time or event that you specified occurs.
  11895.  
  11896.  You can use WAITFOR ERROREXIT with a procedure that kills the abnormally
  11897.  terminated process to free system resources that would otherwise be taken up
  11898.  by an infected process.
  11899.  
  11900.  To find out which process terminated, check the sysprocesses table with the
  11901.  sp_who system procedure.
  11902.  
  11903.  The time you specify with WAITFOR TIME or WAITFOR DELAY can include hours,
  11904.  minutes, and seconds. Use the format "hh:mi:ss", as described in
  11905.  "Datatypes."
  11906.  
  11907.  For example, WAITFOR TIME "16:23" instructs SQL Server to wait for 4:23 pm.
  11908.  The statement WAITFOR DELAY "01:30" instructs SQL Server to wait 1 hour and
  11909.  30 minutes.
  11910.  
  11911.  For details on acceptable time formats, see "Datatypes."
  11912.  
  11913.  
  11914.  See Also
  11915.  
  11916.  BEGIN...END, Datatypes, sp_who
  11917.  
  11918.  
  11919.  WHERE Clause
  11920.  ────────────────────────────────────────────────────────────────────────────
  11921.  
  11922.  
  11923.  Function
  11924.  
  11925.  Sets the conditions in a WHERE clause. (Joins and subqueries are specified
  11926.  in the search conditions; see the "Joins" and "Subqueries" sections for full
  11927.  details.)
  11928.  
  11929.  
  11930.  Syntax
  11931.  
  11932.  Search conditions immediately follow the keywords WHERE or HAVING in a
  11933.  SELECT, INSERT, UPDATE, or DELETE statement. (HAVING search conditions
  11934.  differ from WHERE search conditions only in that aggregate functions are not
  11935.  allowed in WHERE conditions.) If you use more than one of the search
  11936.  conditions in a single statement, connect the conditions with AND or OR.
  11937.  
  11938.    WHERE [NOT] expression comparison_operator expression
  11939.     WHERE [NOT] column_name [NOT] LIKE "match_string"
  11940.     WHERE [NOT] column_name IS [NOT] NULL
  11941.     WHERE [NOT] expression [NOT] BETWEEN expression AND expression
  11942.     WHERE [NOT] expression [NOT] IN ({value_list | subquery})
  11943.     WHERE [NOT] EXISTS (subquery)
  11944.     WHERE [NOT] expression comparison_operator {ANY | ALL} (subquery)
  11945.     WHERE [NOT] column_name join_operator column_name
  11946.     WHERE [NOT] boolean_expression
  11947.     WHERE [NOT] expression {AND | OR} [NOT] expression
  11948.  
  11949.  
  11950.  Examples
  11951.  
  11952.    A. where advance * $2 > ytd_sales * price
  11953.  
  11954.    B. where phone not like '415%'
  11955.  
  11956.  Example B finds all the rows in which the phone number does not begin with
  11957.  415.
  11958.  
  11959.    C. where au_lname like "[CK]ars[eo]n"
  11960.  
  11961.  Example C finds the rows for authors named Carson, Carsen, Karsen, and
  11962.  Karson.
  11963.  
  11964.    D. where advance < $5000 or advance is null
  11965.  
  11966.    E. where (type = "business" or type = "psychology")
  11967.     and advance > $5500
  11968.  
  11969.    F. where ytd_sales between 4095 and 12000
  11970.  
  11971.    G. where state in ('CA', 'IN', 'MD')
  11972.  
  11973.  Example G finds the rows in which the state is one of the three in the list.
  11974.  
  11975.  
  11976.  
  11977.  Options
  11978.  
  11979.  expression
  11980.    A column name; a constant; a function; any combination of column names,
  11981.    constants, and functions connected by arithmetic or bitwise operators; or
  11982.    a subquery. The arithmetic operators are as follows:
  11983.  
  11984.  Symbol  Meaning
  11985.  ────────────────────────────────────────────────────────────────────────────
  11986.  +       Addition
  11987.  -       Subtraction
  11988.  *       Multiplication
  11989.  /       Division
  11990.  %       Modulo
  11991.  
  11992.  ────────────────────────────────────────────────────────────────────────────
  11993.  
  11994.  
  11995.    Addition, subtraction, division, and multiplication can be used on int,
  11996.    smallint, tinyint, float, and money columns. The modulo operator cannot be
  11997.    used with money or float columns. A modulo is the integer remainder after
  11998.    a division involving two integers. For example, 21 % 9 = 3 because 21
  11999.    divided by 9 equals 2 with a remainder of 3. The bitwise operators are as
  12000.    follows:
  12001.  
  12002.  Symbol  Meaning
  12003.  ────────────────────────────────────────────────────────────────────────────
  12004.  &       Bitwise and (two operands)
  12005.  |       Bitwise or (two operands)
  12006.  ^       Bitwise exclusive or (two operands)
  12007.   ~      Bitwise not (one operand)
  12008.  
  12009.  ────────────────────────────────────────────────────────────────────────────
  12010.  
  12011.  
  12012.    The bitwise operators can be used on int, smallint, and tinyint columns
  12013.    only. All the bitwise operators translate the integer parameters into
  12014.    binary representation before evaluating them. (See "Expressions" for more
  12015.    information.)
  12016.  
  12017.  comparison_operator
  12018.    One of the following:
  12019.  
  12020.  Symbol  Meaning
  12021.  ────────────────────────────────────────────────────────────────────────────
  12022.  =       Equal to
  12023.  >       Greater than
  12024.  <       Less than
  12025.  >=      Greater than or equal to
  12026.  <=      Less than or equal to
  12027.  !=      Not equal to
  12028.  !>      Not greater than
  12029.  !<      Not less than
  12030.  
  12031.  ────────────────────────────────────────────────────────────────────────────
  12032.  
  12033.  
  12034.    In comparing char and varchar data, < means closer to the beginning of the
  12035.    alphabet and > means closer to the end of the alphabet. Case and special
  12036.    character evaluations depend on the collating sequence of the operating
  12037.    system on the machine on which SQL Server is located. On ASCII machines,
  12038.    for example, lowercase letters are greater than uppercase letters, and
  12039.    uppercase letters are greater than numbers. Trailing blanks are ignored
  12040.    for the purposes of comparison. So, for example, "Dirk" is the same as
  12041.    "Dirk  ". In comparing dates, < means earlier and > means later. Put
  12042.    quotation marks around all character and datetime data used with a
  12043.    comparison operator. For example, = "Bennet", > "94609". (See
  12044.    "Datatypes.")
  12045.  
  12046.  column_name
  12047.    The name of a column used in the comparison. Qualify the column name with
  12048.    its table or view name if there is any ambiguity.
  12049.  
  12050.  NOT
  12051.    Negates any boolean expression and keywords, such as LIKE, NULL, BETWEEN,
  12052.    IN, and EXISTS.
  12053.  
  12054.  LIKE
  12055.    Indicates that the following character string (enclosed by single or
  12056.    double quotation marks) is a matching pattern. LIKE is available for char,
  12057.    varchar, and datetime columns (but not to search for seconds or
  12058.    milliseconds). You can use the LIKE keyword and wildcard characters with
  12059.    datetime data as well as with char and varchar. When you use LIKE with
  12060.    datetime values, SQL Server converts the dates to the standard datetime
  12061.    format and then to varchar. Since the standard storage format doesn't
  12062.    include seconds or milliseconds, you cannot search for seconds or
  12063.    milliseconds with LIKE and a pattern. It is a good idea to use LIKE when
  12064.    you search for datetime values, since datetime entries can contain a
  12065.    variety of date parts. For example, if you insert the value "9:20" into a
  12066.    column named arrival_time, the clause WHERE arrival_time = "9:20" would
  12067.    not find it because SQL Server converts the entry into "Jan 1, 1900
  12068.    9:20AM." However, the clause WHERE arrival_time LIKE "%9:20%" would find
  12069.    it.
  12070.  
  12071.  match_string
  12072.    A string of characters and wildcards enclosed in quotation marks. For
  12073.    complete information, see "Wildcard Characters." The wildcards are as
  12074.    follows:
  12075.  
  12076.  Symbol                            Meaning
  12077.  ────────────────────────────────────────────────────────────────────────────
  12078.  %                                 Any string of 0 or more characters
  12079.  
  12080.  _ (underscore)                    Any single character
  12081.  
  12082.  [ ]                               Any single character within the
  12083.                                    specified range ([a-f]) or set ([abcdef])
  12084.  
  12085.  [^]                               Any single character not within the
  12086.                                    specified range ([^a-f]) or set
  12087.                                    ([^abcdef])
  12088.  
  12089.  ────────────────────────────────────────────────────────────────────────────
  12090.  
  12091.  
  12092.  IS NULL
  12093.    Searches for null values. (=NULL is allowed in UPDATE statements. Outside
  12094.    of this one exception, NULL returns only NULL when used with arithmetic,
  12095.    bitwise, or comparison operators. NULL is essentially useless as an
  12096.    operand with any of these operators.)
  12097.  
  12098.  BETWEEN
  12099.    The range-start keyword. Use AND for the range-end value. A range of
  12100.    BETWEEN x AND y, unlike a range of > x < y, is inclusive.
  12101.  
  12102.  IN
  12103.    Allows you to select values that match any one of a list of values. The
  12104.    comparator can be a constant or a column name, and the list can be a set
  12105.    of constants or, more commonly, a subquery. (See "Subqueries" for
  12106.    information on using IN with a subquery.)  Enclose the list of values in
  12107.    parentheses.
  12108.  
  12109.  value_list
  12110.    A list of values. Put single or double quotation marks around character
  12111.    values, and separate each value from the following one with a comma. (See
  12112.    example E, earlier in this section.)
  12113.  
  12114.  EXISTS
  12115.    Is used with a subquery to test for the existence of some result from the
  12116.    subquery. (See "Subqueries" for more information.)
  12117.  
  12118.  subquery
  12119.    A restricted SELECT statement (ORDER BY and COMPUTE clauses and the INTO
  12120.    keyword are not allowed) inside the WHERE or HAVING clause of a SELECT,
  12121.    INSERT, DELETE, UPDATE, or subquery. (See "Subqueries" for more
  12122.    information.)
  12123.  
  12124.  ANY
  12125.    Is used with >, <, or = and a subquery. It returns results when any value
  12126.    retrieved in the subquery matches the value in the WHERE or HAVING clause
  12127.    of the outer statement. (See "Subqueries" for more information.)
  12128.  
  12129.  ALL
  12130.    Is used with > or < and a subquery. It returns results when all values
  12131.    retrieved in the subquery match the value in the WHERE or HAVING clause of
  12132.    the outer statement. (See "Subqueries" for more information.)
  12133.  
  12134.  join_operator
  12135.    A comparison operator, =*, or *= . (See "Joins" for more information.)
  12136.  
  12137.  boolean_expression
  12138.    An expression that returns true or false. (See "Expressions" for a full
  12139.    definition.)
  12140.  
  12141.  AND
  12142.    Joins two conditions and returns results when both of the conditions are
  12143.    true. When more than one logical operator is used in a statement, AND
  12144.    operators are normally evaluated first. However, you can change the order
  12145.    of execution with parentheses.
  12146.  
  12147.  OR
  12148.    Joins two conditions and returns results when either of the conditions is
  12149.    true. When more than one logical operator is used in a statement, OR
  12150.    operators are normally evaluated after AND operators. However, you can
  12151.    change the order of execution with parentheses.
  12152.  
  12153.  
  12154.  Comments
  12155.  
  12156.  WHERE and HAVING search conditions are identical, except that aggregate
  12157.  functions are not permitted in WHERE clauses ("HAVING avg(price) > 20" is
  12158.  legal; "WHERE avg(price) > 20" is not). See "Aggregate Functions" for
  12159.  information on the use of aggregate functions.
  12160.  
  12161.  If there are columns in the select list that neither have aggregate
  12162.  functions applied to them nor are included in the query's GROUP BY clause
  12163.  (illegal in standard SQL), the meanings of HAVING and WHERE are somewhat
  12164.  different.
  12165.  
  12166.  In this situation, a WHERE clause restricts the rows that are included in
  12167.  the calculation of the aggregate function but does not restrict the rows
  12168.  returned by the query. Conversely, a HAVING clause restricts the rows
  12169.  returned by the query but does not affect the calculation of the aggregate
  12170.  function. See "GROUP BY and HAVING Clauses" for examples.
  12171.  
  12172.  There are two ways to specify literal quotation marks within a char or
  12173.  varchar entry. The first method is to use two quotation marks. For example,
  12174.  if you have begun a character entry with a single quotation mark and wish to
  12175.  include a single quotation mark as part of the entry, use two single
  12176.  quotation marks:
  12177.  
  12178.    'I don"t understand.'
  12179.  
  12180.  With double quotation marks:
  12181.  
  12182.    "He said, ""It's not really confusing."""
  12183.  
  12184.  The second method is to enclose a quote in the opposite kind of quotation
  12185.  mark. In other words, surround an entry containing a double quotation mark
  12186.  with single quotation marks (or vice versa). Here are some examples:
  12187.  
  12188.    'George said, "There must be a better way."'
  12189.     "Isn't there a better way?"
  12190.     'George asked, "Isn"t there a better way?"'
  12191.  
  12192.  To enter a character string longer than the width of your screen, enter a
  12193.  backslash (\) before going to the next line.
  12194.  
  12195.  
  12196.  See Also
  12197.  
  12198.  Datatypes, DELETE, EXECUTE, Expressions, Functions, INSERT, Joins, Search
  12199.  Conditions, SELECT, Subqueries, UPDATE, Wildcard Characters, sp_helpjoins
  12200.  
  12201.  
  12202.  WHILE
  12203.  ────────────────────────────────────────────────────────────────────────────
  12204.  
  12205.  
  12206.  Function
  12207.  
  12208.  Sets a condition for the repeated execution of a statement or statement
  12209.  block. The statement(s) is executed repeatedly as long as the specified
  12210.  condition is true.
  12211.  
  12212.  
  12213.  Syntax
  12214.  
  12215.    WHILE
  12216.     boolean_expression
  12217.     statement
  12218.  
  12219.  
  12220.  Examples
  12221.  
  12222.    while (select avg(price) from titles) < $30
  12223.     begin
  12224.      update titles
  12225.      set price = price * $2
  12226.      select max(price) from titles
  12227.      if (select max(price) from titles) > $50
  12228.      break
  12229.      else
  12230.      continue
  12231.     end
  12232.     begin
  12233.     select title_id, price from titles
  12234.     print "Too much for the market to bear"
  12235.     end
  12236.  
  12237.  If the average price is less than $30, double the prices. Then select the
  12238.  maximum price. If it is less than or equal to $50, restart the WHILE loop
  12239.  and double the prices again. If the maximum price is more than $50, exit the
  12240.  WHILE loop, select the title_ids and the too-high prices, and print a
  12241.  message.
  12242.  
  12243.  
  12244.  Comments
  12245.  
  12246.  The execution of statements in the WHILE loop can be controlled from inside
  12247.  the loop with the BREAK and CONTINUE statements.
  12248.  
  12249.  CONTINUE causes the WHILE loop to restart, skipping any statements after
  12250.  CONTINUE. BREAK causes an exit from the WHILE loop, and any statements that
  12251.  appear after the END keyword that marks the end of the loop are executed.
  12252.  BREAK and CONTINUE are often (but not always) activated by IF tests.
  12253.  
  12254.  If two or more WHILE loops are nested, BREAK exits to the next outermost
  12255.  loop. First all the statements after the end of the inner loop run, and then
  12256.  the next outermost loop restarts.
  12257.  
  12258.  
  12259.  See Also
  12260.  
  12261.  BEGIN...END, BREAK, CONTINUE, CREATE PROCEDURE, GOTO
  12262.  
  12263.  
  12264.  Wildcard Characters
  12265.  ────────────────────────────────────────────────────────────────────────────
  12266.  
  12267.  
  12268.  Function
  12269.  
  12270.  Used with the LIKE keyword to represent any character in a string when
  12271.  searching for a char, varchar, or datetime value.
  12272.  
  12273.  
  12274.  Syntax
  12275.  
  12276.    {WHERE | HAVING} [NOT] expression [NOT] LIKE "string"
  12277.  
  12278.  The string can include these wildcard characters:
  12279.  
  12280.  Wildcard                          Meaning
  12281.  ────────────────────────────────────────────────────────────────────────────
  12282.  %                                 Any string of 0 or more characters
  12283.  
  12284.  _ (underscore)                    Any single  character
  12285.  
  12286.  [ ]                               Any single character within the
  12287.                                    specified range ([a-f]) or set ([abcdef])
  12288.  
  12289.  [^]                               Any single character not within the
  12290.                                    specified range ([^a-f]) or set
  12291.                                    ([^abcdef])
  12292.  
  12293.  ────────────────────────────────────────────────────────────────────────────
  12294.  
  12295.  
  12296.  
  12297.  Examples
  12298.  
  12299.    A. select phone
  12300.     from authors
  12301.     where phone like "415%"
  12302.  
  12303.  Example A finds all the phone numbers in the authors table that begin with
  12304.  the 415 area code.
  12305.  
  12306.    B. select phone
  12307.     from authors
  12308.     where phone not like "415%"
  12309.  
  12310.     select phone
  12311.     from authors
  12312.     where not phone like "415%"
  12313.  
  12314.  These two queries are equivalent: they find all the phone numbers in the
  12315.  authors table that do not begin with the 415 area code.
  12316.  
  12317.    C. select au_lname
  12318.     from authors
  12319.     where au_lname like "%en%"
  12320.  
  12321.  Example C finds names that have the characters "en" in them (Bennet, Green,
  12322.  McBadden).
  12323.  
  12324.    D. select au_fname
  12325.     from authors
  12326.     where au_fname like "_heryl"
  12327.  
  12328.  Example D finds six-letter names that end with "heryl" (Cheryl).
  12329.  
  12330.    E. select au_lname
  12331.     from authors
  12332.     where au_lname like "[M-Z]inger"
  12333.  
  12334.  Example E finds names ending with "inger" and beginning with any single
  12335.  character between "M" and "Z".
  12336.  
  12337.    F. select au_lname
  12338.     from authors
  12339.     where au_lname like "[dD]eFrance"
  12340.  
  12341.  Example F finds both DeFrance and deFrance.
  12342.  
  12343.    G. select au_lname
  12344.     from authors
  12345.     where au_lname like "M[^c]%"
  12346.  
  12347.  Example G finds names beginning with "M" whose second letter is not "c".
  12348.  
  12349.  
  12350.  Comments
  12351.  
  12352.  The string with wildcard characters must always be enclosed by single or
  12353.  double quotation marks.
  12354.  
  12355.  LIKE and the wildcard characters can be used for searching char, varchar,
  12356.  and datetime columns (but not to search for seconds or milliseconds).
  12357.  
  12358.  Wildcards used without LIKE have no special meaning. For example, this query
  12359.  finds any phone numbers that start with the four characters "415%":
  12360.  
  12361.    select phone
  12362.     from authors
  12363.     where phone = "415%"
  12364.  
  12365.  You can use the LIKE keyword and wildcard characters with datetime data as
  12366.  well as with char and varchar. When you use LIKE with datetime values, SQL
  12367.  Server converts the dates to the standard datetime format and then to
  12368.  varchar. Since the standard storage format doesn't include seconds or
  12369.  milliseconds, you cannot search for seconds or milliseconds with LIKE and a
  12370.  pattern. It is a good idea to use LIKE when you search for datetime values,
  12371.  since datetime entries can contain a variety of date parts. For example, if
  12372.  you insert the value "9:20" into a column named arrival_time, the clause
  12373.  WHERE arrival_time = "9:20" would not find it because SQL Server converts
  12374.  the entry into "Jan 1, 1900 9:20AM". However, the clause WHERE arrival_time
  12375.  LIKE "%9:20%" would find it.
  12376.  
  12377.  To use %, _, [ ], or [^ ] as literal characters in a LIKE string rather than
  12378.  as wildcards, use square brackets around the percent sign, the underscore,
  12379.  and the open bracket. Use the close bracket by itself. Use the dash as the
  12380.  first character inside a set of square brackets.
  12381.  
  12382. ╓┌─────────────────┌─────────────────────────────────────────────────────────╖
  12383.  Symbol            Meaning
  12384.  ────────────────────────────────────────────────────────────────────────────
  12385.  LIKE "5%"         5 followed by any string of 0 or more characters
  12386.  LIKE "5[%]"       5%
  12387.  LIKE "_n"         an, in, on, and so on
  12388.  LIKE "[_]n"       _n
  12389.  LIKE "[a-cdf]"    a, b, c, d, or f
  12390.  LIKE "[-acdf]"    -, a, c, d, or f
  12391.  LIKE "[[]"        [
  12392.  Symbol            Meaning
  12393.  ────────────────────────────────────────────────────────────────────────────
  12394. LIKE "[[]"        [
  12395.  LIKE "]"          ]
  12396.  ────────────────────────────────────────────────────────────────────────────
  12397.  
  12398.  
  12399.  You can't always duplicate NOT LIKE patterns with LIKE and the negative
  12400.  wildcard [^] because NOT LIKE finds the items that do not match the entire
  12401.  LIKE pattern, while LIKE with negative wildcards is evaluated one character
  12402.  at a time.
  12403.  
  12404.  For example, to see all the system tables in a database, use this query
  12405.  since all system tables begin with these three letters:
  12406.  
  12407.    select name
  12408.     from sysobjects
  12409.     where name LIKE "sys%"
  12410.  
  12411.  To see all the objects that are not system tables, use NOT LIKE "sys%". If
  12412.  you have a total of 32 objects and the LIKE finds 13 names that match the
  12413.  pattern, the NOT LIKE will find exactly the 19 objects that do not match the
  12414.  LIKE pattern.
  12415.  
  12416.  You won't always find the same names with a pattern such as LIKE
  12417.  "[^s][^y][^s]%". Instead of 19, you might get only 14, with all the names
  12418.  that begin with "s" or have "y" as the second letter or have "s" as the
  12419.  third letter eliminated from the results as well as the system table names.
  12420.  This is because matchstrings with negative wildcards are evaluated in steps,
  12421.  one wildcard at a time. If the match fails at any point in the evaluation,
  12422.  it is eliminated.
  12423.  
  12424.  
  12425.  See Also
  12426.  
  12427.  CREATE PROCEDURE, EXECUTE, Expressions, Search Conditions, WHERE Clause
  12428.  
  12429.  
  12430.  WRITETEXT
  12431.  ────────────────────────────────────────────────────────────────────────────
  12432.  
  12433.  
  12434.  Function
  12435.  
  12436.  Permits nonlogged, interactive updating of an existing text field.
  12437.  
  12438.  
  12439.  Syntax
  12440.  
  12441.    WRITETEXT [[database.]owner.]table_name.column_name text_ptr
  12442.      [WITH LOG] data
  12443.  
  12444.  
  12445.  Examples
  12446.  
  12447.    declare @val varbinary(30)
  12448.     select @val = textptr(blurb) from texttest
  12449.     where title_id = "BU7832"
  12450.     writetext texttest.blurb @val "hello world"
  12451.  
  12452.  
  12453.  Options
  12454.  
  12455.  table_name.column_name
  12456.    The name of the text column and the table name, which must be included.
  12457.    The database name and owner name are optional.
  12458.  
  12459.  WITH LOG
  12460.    Logs the inserted text or image data. This option aids media recovery, but
  12461.    text data quickly increases the size of the transaction log. Make sure
  12462.    that the transaction log resides in a separate database device. (See
  12463.    "sp_logdevice" in Chapter 2 and the SQL Server Administrator's Guide for
  12464.    details.)
  12465.  
  12466.  
  12467.  Comments
  12468.  
  12469.  By default, WRITETEXT is a nonlogged operation. This means that text or
  12470.  image data is not logged when it is written into the database. To use
  12471.  WRITETEXT in its default, nonlogged state, the System Administrator must use
  12472.  sp_dboption to set select into/bulkcopy. This permits the insertion of
  12473.  nonlogged data.
  12474.  
  12475.  WRITETEXT updates text data in an existing row. The update completely
  12476.  replaces existing text. For a valid text pointer to exist, a text column
  12477.  must contain either text data or a null value that has been explicitly
  12478.  entered with INSERT or UPDATE. Given a table textnull with columns key and
  12479.  x, where x is a text column that permits nulls:
  12480.  
  12481.  This insert assigns a valid text pointer:
  12482.  
  12483.    insert textnull
  12484.     values (1, null)
  12485.     go
  12486.  
  12487.  This insert does not assign a pointer:
  12488.  
  12489.    insert textnull (key)
  12490.     values (2)
  12491.     go
  12492.  
  12493.  You can update a null text column to get a valid text pointer or insert
  12494.  entirely new rows with INSERT (which is a logged operation) or bcp.
  12495.  
  12496.  The maximum length of text that can be inserted interactively with WRITETEXT
  12497.  is approximately 120K bytes for text data and 60K bytes for image data.
  12498.  
  12499.  You cannot use WRITETEXT on text and image columns in views.
  12500.  
  12501.  
  12502.  See Also
  12503.  
  12504.  READTEXT, Text/Image Datatypes, Text/Image Functions
  12505.  
  12506.  
  12507.  
  12508.  
  12509.  
  12510.  
  12511.  Chapter 2  System Procedures
  12512.  ────────────────────────────────────────────────────────────────────────────
  12513.  
  12514.  This chapter consists of reference pages, in alphabetical order, for system
  12515.  procedures (SQL-Server-supplied stored procedures). Note that system
  12516.  procedure names always begin with "sp_".
  12517.  
  12518.  Each system procedure and its function is listed below:
  12519.  
  12520.  ────────────────────────────────────────────────────────────────────────────
  12521.  sp_addalias
  12522.    Maps one user to another in a database.
  12523.  
  12524.  sp_addgroup
  12525.    Adds a group to a database.
  12526.  
  12527.  sp_addlogin
  12528.    Authorizes a new SQL Server user by adding an entry to
  12529.    master.dbo.syslogins.
  12530.  
  12531.  sp_addtype
  12532.    Creates a user-defined datatype.
  12533.  
  12534.  sp_addumpdevice
  12535.    Adds a dump device to SQL Server.
  12536.  
  12537.  sp_adduser
  12538.    Adds a new user to the current database.
  12539.  
  12540.  sp_bindefault
  12541.    Binds a default to a column or to a user-defined datatype.
  12542.  
  12543.  sp_bindrule
  12544.    Binds a rule to a column or user datatype.
  12545.  
  12546.  sp_changedbowner
  12547.    Changes the owner of a database.
  12548.  
  12549.  sp_changegroup
  12550.    Changes a user's group.
  12551.  
  12552.  sp_commonkey
  12553.    Defines a common key between two tables or views.
  12554.  
  12555.  sp_configure
  12556.    Displays or changes configuration options.
  12557.  
  12558.  sp_dboption
  12559.    Displays or changes database options.
  12560.  
  12561.  sp_defaultdb
  12562.    Changes a user's default database.
  12563.  
  12564.  sp_depends
  12565.    Displays information about database object dependencies.
  12566.  
  12567.  sp_diskdefault
  12568.    Sets a database device's status to defaulton or defaultoff.
  12569.  
  12570.  sp_dropalias
  12571.    Removes the alias username identity that had been established with
  12572.    sp_addalias.
  12573.  
  12574.  sp_dropdevice
  12575.    Drops an SQL Server database device or dump device.
  12576.  
  12577.  sp_dropgroup
  12578.    Drops a group from a database.
  12579.  
  12580.  sp_dropkey
  12581.    Removes a key from the syskeys table that had been defined using
  12582.    sp_primarykey, sp_foreignkey, or sp_commonkey.
  12583.  
  12584.  sp_droplogin
  12585.    Drops an SQL Server user by deleting his or her entry in
  12586.    master.dbo.syslogins.
  12587.  
  12588.  sp_droptype
  12589.    Drops a user-defined datatype by deleting the type from systypes.
  12590.  
  12591.  sp_dropuser
  12592.    Drops a user from the current database by deleting the entry from
  12593.    sysusers.
  12594.  
  12595.  sp_foreignkey
  12596.    Defines a foreign key on a table or view.
  12597.  
  12598.  sp_help
  12599.    Reports information about a database object (any object listed in
  12600.    sysobjects) or about an SQL Server-supplied or user-defined datatype.
  12601.  
  12602.  sp_helpdb
  12603.    Reports information about a database or about all databases.
  12604.  
  12605.  sp_helpdevice
  12606.    Reports information about SQL Server's database devices and dump devices.
  12607.  
  12608.  sp_helpgroup
  12609.    Reports information on a group or on all groups in the current database.
  12610.  
  12611.  sp_helpindex
  12612.    Reports index information on a table.
  12613.  
  12614.  sp_helpjoins
  12615.    Lists the columns in two tables or views that are likely to be joined.
  12616.  
  12617.  sp_helpkey
  12618.    Reports information on primary, foreign, and common keys.
  12619.  
  12620.  sp_helprotect
  12621.    Reports permissions by database object and optionally by user for that
  12622.    object.
  12623.  
  12624.  sp_helpsql
  12625.    Provides syntax for SQL statements and system procedures.
  12626.  
  12627.  sp_helptext
  12628.    Prints the text of a stored procedure, trigger, view, default, or rule.
  12629.  
  12630.  sp_helpuser
  12631.    Reports information on users of a database.
  12632.  
  12633.  sp_lock
  12634.    Reports information on locks.
  12635.  
  12636.  sp_logdevice
  12637.    Puts the syslogs system table, which contains the transaction log, on a
  12638.    separate database device.
  12639.  
  12640.  sp_monitor
  12641.    Displays statistics about SQL Server.
  12642.  
  12643.  sp_password
  12644.    Adds or changes a password for an SQL Server login ID.
  12645.  
  12646.  sp_primarykey
  12647.    Defines a primary key on a table or view.
  12648.  
  12649.  sp_rename
  12650.    Changes the name of a user-created object in the current database.
  12651.  
  12652.  sp_renamedb
  12653.    Changes the name of a database.
  12654.  
  12655.  sp_spaceused
  12656.    Displays the number of rows, the number of data pages, and the disk space
  12657.    used by an object or by each object in a database.
  12658.  
  12659.  sp_unbindefault
  12660.    Unbinds a default value from a column or from a user-defined datatype.
  12661.  
  12662.  sp_unbindrule
  12663.    Unbinds a rule from a column or user-defined datatype.
  12664.  
  12665.  sp_who
  12666.    Reports information on current SQL Server users and processes.
  12667.  
  12668.  ────────────────────────────────────────────────────────────────────────────
  12669.  There are additional system procedures that are called by other system
  12670.  procedures; users cannot call them directly.
  12671.  
  12672.  
  12673.  
  12674.  
  12675.  
  12676.  sp_addalias
  12677.  ────────────────────────────────────────────────────────────────────────────
  12678.  
  12679.  
  12680.  Function
  12681.  
  12682.  Maps one user to another in a database.
  12683.  
  12684.  
  12685.  Syntax
  12686.  
  12687.    sp_addalias login_id, username
  12688.  
  12689.  
  12690.  Examples
  12691.  
  12692.    sp_addalias victoria, albert
  12693.  
  12694.  There is a user named albert in the database's sysusers table and a login ID
  12695.  for a user named victoria in master.dbo.syslogins. This system procedure
  12696.  allows victoria to use the current database and to be known as albert in
  12697.  this database.
  12698.  
  12699.  
  12700.  Options
  12701.  
  12702.  login_id
  12703.    The master.dbo.syslogins name of the user who wants an alternate identity
  12704.    in the current database. An alias allows an SQL Server user to be known in
  12705.    a database as another user. If the user with login_id exists in the
  12706.    database's sysusers table, SQL Server won't find the user's alias
  12707.    identity, since it checks sysusers before checking sysalternates.
  12708.  
  12709.  username
  12710.    The name of the database user to whom the first user wishes to be linked.
  12711.    The name must exist in both master.dbo.syslogins and in the sysusers table
  12712.    of the current database
  12713.  
  12714.  
  12715.  Comments
  12716.  
  12717.  Executing sp_addalias maps one user (login_id) to another (username) in the
  12718.  current database. The mapping is shown in sysalternates, where the two
  12719.  users' suids are connected.
  12720.  
  12721.  A report on any users mapped to a specified user can be generated with
  12722.  sp_helpuser if you give the specified user's name as a parameter.
  12723.  
  12724.  When a user tries to use a database, SQL Server checks sysusers to see if
  12725.  the username is listed there. If the username is not there, it then checks
  12726.  sysalternates. If an entry (the user's suid) is found there, mapped to a
  12727.  database user's suid (altsuid in sysalternates table), the first user is
  12728.  treated as the second user while using the database.
  12729.  
  12730.  There are three ways that you can be authorized to use a database (assuming
  12731.  you are listed in master.dbo.syslogins). First, you can be listed in the
  12732.  sysusers table of that database. (New database users are added by the
  12733.  Database Owner with the sp_adduser system procedure.)
  12734.  
  12735.  Second, you can have an alias in the database, which has been added by the
  12736.  Database Owner with the sp_addalias system procedure.
  12737.  
  12738.  Third, you can use the database as guest if there is a guest entry in the
  12739.  database's sysusers table.
  12740.  
  12741.  
  12742.  Messages
  12743.  
  12744.    Alias user added.
  12745.  
  12746.  The procedure was successful. Now the user with login_id can use the current
  12747.  database. When doing so, he or she will be known as username.
  12748.  
  12749.    No login exists with the specified login name.
  12750.  
  12751.  There is no entry in master.dbo.syslogins for login_id. Everyone using SQL
  12752.  Server, whether aliased or not, must have a login ID.
  12753.  
  12754.    No user with the specified name exists in the current database.
  12755.  
  12756.  Since username is not a user in the database, login_id can't be aliased to
  12757.  it.
  12758.  
  12759.    The specified user name is already aliased.
  12760.  
  12761.  The login_id is already aliased to a user in the current database. A
  12762.  login_id may be aliased to only one database user at a time. To change an
  12763.  alias, first drop the current alias using sp_dropalias, then add the new
  12764.  alias.
  12765.  
  12766.  
  12767.  Permissions
  12768.  
  12769.  Execute permission defaults to the Database Owner.
  12770.  
  12771.  
  12772.  Tables Used
  12773.  
  12774.  master.dbo.syslogins, sysalternates, sysusers
  12775.  
  12776.  
  12777.  See Also
  12778.  
  12779.  sp_addlogin, sp_adduser, sp_dropalias, sp_helpuser, USE
  12780.  
  12781.  
  12782.  sp_addgroup
  12783.  ────────────────────────────────────────────────────────────────────────────
  12784.  
  12785.  
  12786.  Function
  12787.  
  12788.  Adds a group to a database. Groups are used as collective names in granting
  12789.  and revoking permissions.
  12790.  
  12791.  
  12792.  Syntax
  12793.  
  12794.    sp_addgroup grpname
  12795.  
  12796.  
  12797.  Examples
  12798.  
  12799.    sp_addgroup accounting
  12800.  
  12801.  This example creates an accounting group.
  12802.  
  12803.  
  12804.  Options
  12805.  
  12806.  grpname
  12807.    The name of the group. Groupnames must follow the rules for identifiers.
  12808.  
  12809.  
  12810.  Comments
  12811.  
  12812.  Groups are used as collective names for granting and revoking permissions.
  12813.  
  12814.  The sp_addgroup system procedure adds an entry for the new group to a
  12815.  database's sysusers table. Each group's uid is a number greater than or
  12816.  equal to 16,384 (except the uid of the public group, which is always 0).
  12817.  
  12818.  Once a group has been created, you can use the groupname as an optional
  12819.  parameter to sp_adduser to add the new user to a group at the same time. To
  12820.  add an already existing user to a group, use sp_changegroup.
  12821.  
  12822.  A user can be a member of only one group.
  12823.  
  12824.  Every database is created with a public group. If a user is not explicitly
  12825.  added to another group, he or she is a member of the public group.
  12826.  
  12827.  
  12828.  Messages
  12829.  
  12830.    New group added.
  12831.  
  12832.  An entry for the group has been added to the current database's sysusers
  12833.  table. Users may now be made members of the group with the sp_adduser or
  12834.  sp_changegroup system procedures.
  12835.  
  12836.    A user with the specified group name already exists.
  12837.  
  12838.  The groupname you supplied is already being used as a username. Choose
  12839.  another name to use as the grpname parameter.
  12840.  
  12841.    A group with the specified name already exists.
  12842.  
  12843.  The groupname you supplied is already being used as a groupname. Choose
  12844.  another name to use as the grpname parameter.
  12845.  
  12846.  
  12847.  Permissions
  12848.  
  12849.  Execute permission defaults to the Database Owner.
  12850.  
  12851.  
  12852.  Tables Used
  12853.  
  12854.  sysusers
  12855.  
  12856.  
  12857.  See Also
  12858.  
  12859.  sp_adduser, sp_changegroup, sp_dropgroup, sp_helpgroup, GRANT, REVOKE
  12860.  
  12861.  
  12862.  sp_addlogin
  12863.  ────────────────────────────────────────────────────────────────────────────
  12864.  
  12865.  
  12866.  Function
  12867.  
  12868.  Authorizes a new SQL Server user by adding an entry to master.dbo.syslogins.
  12869.  
  12870.  
  12871.  
  12872.  Syntax
  12873.  
  12874.    sp_addlogin login_id [, passwd [, defdb]]
  12875.  
  12876.  
  12877.  Examples
  12878.  
  12879.    A. sp_addlogin victoria
  12880.  
  12881.  Example A creates an SQL Server login for the user victoria. Since the
  12882.  passwd and defdb parameters are not included, victoria's password is the
  12883.  null value and her default database is master.
  12884.  
  12885.    B. sp_addlogin albert, food, corporate
  12886.  
  12887.  Example B creates an SQL Server login ID for the user albert. His password
  12888.  is food and his default database is corporate.
  12889.  
  12890.  
  12891.  Options
  12892.  
  12893.  login_id
  12894.    The login ID of the user. Login IDs normally follow the rules for
  12895.    identifiers. (However, numeric login IDs of up to 30 digits are also
  12896.    allowed.)
  12897.  
  12898.  passwd
  12899.    The user's password. If no password is given, the default password is the
  12900.    null value.
  12901.  
  12902.  defdb
  12903.    The name of the user's default, or "home," database─the database to which
  12904.    the user is connected when he or she logs in. If no defdb is given, the
  12905.    default is master.
  12906.  
  12907.  
  12908.  Comments
  12909.  
  12910.  After sp_addlogin is executed, the user can log in to SQL Server.
  12911.  
  12912.  The user can change his or her own password at any time with sp_password.
  12913.  The System Administrator can change any user's password with sp_password.
  12914.  
  12915.  If a third parameter (the name of a default database) is supplied, the user
  12916.  is connected to the specified database without the USE statement and without
  12917.  passing through master. However, the user cannot use the default database
  12918.  until he or she is given access to that database by its Database Owner
  12919.  (using the sp_adduser or sp_addalias system procedures).
  12920.  
  12921.  The user can change his or her own default database at any time with
  12922.  sp_defaultdb. The System Administrator can change any user's default
  12923.  database with sp_defaultdb.
  12924.  
  12925.  
  12926.  Messages
  12927.  
  12928.    New login created.
  12929.  
  12930.  An SQL Server login ID for login_id has been created successfully.
  12931.  
  12932.    Only the System Administrator may execute this procedure.
  12933.  
  12934.  Someone other than the System Administrator tried to execute the system
  12935.  procedure. Only the System Administrator can add login IDs to SQL Server.
  12936.  
  12937.    A user with the specified login name already exists.
  12938.  
  12939.  A user with that login ID has already been added. Choose another login_id.
  12940.  
  12941.    Database name not valid -- login not added.
  12942.  
  12943.  The specified default database does not exist. Create the database first or
  12944.  choose a database that already exists as defdb.
  12945.  
  12946.  
  12947.  Permissions
  12948.  
  12949.  Execute permission defaults to the System Administrator.
  12950.  
  12951.  
  12952.  Tables Used
  12953.  
  12954.  master.dbo.sysdatabases, master.dbo.syslogins
  12955.  
  12956.  
  12957.  See Also
  12958.  
  12959.  sp_addalias, sp_adduser, sp_defaultdb, sp_droplogin, sp_password, USE
  12960.  
  12961.  
  12962.  sp_addtype
  12963.  ────────────────────────────────────────────────────────────────────────────
  12964.  
  12965.  
  12966.  Function
  12967.  
  12968.  Creates a user-defined datatype.
  12969.  
  12970.  
  12971.  Syntax
  12972.  
  12973.    sp_addtype typename, phystype[(length)] [, nulltype]
  12974.  
  12975.  
  12976.  Examples
  12977.  
  12978.    A. sp_addtype ssn, "varchar(11)"
  12979.  
  12980.  Example A creates a user-defined datatype called ssn to be used for columns
  12981.  that hold social security numbers. Since no nulltype parameter is specified,
  12982.  no null values are allowed. Notice that "varchar(11)" is enclosed in
  12983.  quotation marks because it contains punctuation (parentheses).
  12984.  
  12985.    B. sp_addtype birthday, datetime, null
  12986.  
  12987.  Example B creates a user-defined datatype called birthday that allows null
  12988.  values.
  12989.  
  12990.  
  12991.  Options
  12992.  
  12993.  typename
  12994.    The name of the user datatype. Type names must follow the rules for
  12995.    identifiers and must be unique for each owner in each database.
  12996.  
  12997.  phystype[(length)]
  12998.    The physical or SQL-Server-supplied type (char, int, and so on) on which
  12999.    the user datatype is based. Length must be specified for char, varchar,
  13000.    binary, and varbinary. If a length is given, the phystype parameter must
  13001.    be enclosed in single or double quotation marks. (Quotation marks are
  13002.    required around all parameters with embedded blank spaces or punctuation
  13003.    marks.)
  13004.  
  13005.  nulltype
  13006.    A variable that indicates how the user datatype handles null value
  13007.    entries. The default (if this optional parameter is omitted) is NOT NULL
  13008.    (disallow null values and require an explicit entry). Acceptable values
  13009.    for this parameter are NULL, NONULL, and NOT NULL. You can override the
  13010.    user datatype's nulltype condition in a CREATE TABLE statement.
  13011.  
  13012.  
  13013.  Comments
  13014.  
  13015.  Executing sp_addtype creates a user-defined datatype and adds it to the
  13016.  systypes system table. Once a user datatype is created, you can use it in
  13017.  CREATE TABLE statements and bind defaults and rules to it.
  13018.  
  13019.  Define each user datatype in terms of one of the physical
  13020.  (SQL-Server-supplied) datatypes, optionally specifying NULL (allow null
  13021.  entries) or NONULL (disallow them). The default, if you give no third
  13022.  parameter, is nonull.
  13023.  
  13024.  A user-defined datatype name must be unique in the database, but
  13025.  user-defined datatypes with different names can have the same definitions.
  13026.  
  13027.  
  13028.  Messages
  13029.  
  13030.    Type added.
  13031.  
  13032.  The new user datatype was created and can now be used in CREATE TABLE
  13033.  statements or to bind rules and defaults.
  13034.  
  13035.    Usage: sp_addtype name, 'datatype' [,null | nonull]
  13036.  
  13037.  Syntax summary. The nulltype parameter can be null, NULL, nonull, NONULL,
  13038.  not null, or NOT NULL.
  13039.  
  13040.    A type with the specified name already exists.
  13041.  
  13042.  The typename parameter is already a user-defined datatype. Choose a
  13043.  different name.
  13044.  
  13045.    Illegal length specified -- must be between 1 and 255.
  13046.  
  13047.  The length of a datatype must be between 1 and 255.
  13048.  
  13049.    Physical datatype does not exist.
  13050.  
  13051.  The phystype you gave is not an SQL Server datatype.
  13052.  
  13053.    Physical datatype does not allow null values.
  13054.  
  13055.  The bit datatype doesn't allow null values, and you specified with the
  13056.  nulltype parameter that you wanted to allow null values.
  13057.  
  13058.    Physical type is fixed length. You cannot specify the length.
  13059.  
  13060.  The physical datatypes that take length specifications are char, varchar,
  13061.  binary, and varbinary. Other physical datatypes have fixed lengths that
  13062.  cannot be changed.
  13063.  
  13064.    You must specify a length with this physical type.
  13065.  
  13066.  You used a phystype ─char, varchar, binary, or varbinary─that requires a
  13067.  length. For example, "char(10)" is acceptable, but char is not.
  13068.  
  13069.  
  13070.  Permissions
  13071.  
  13072.  Execute permission defaults to the public group.
  13073.  
  13074.  
  13075.  Tables Used
  13076.  
  13077.  systypes
  13078.  
  13079.  
  13080.  See Also
  13081.  
  13082.  sp_bindefault, sp_bindrule, sp_droptype, sp_rename, sp_unbindefault,
  13083.  sp_unbindrule, CREATE DEFAULT, CREATE RULE, CREATE TABLE
  13084.  
  13085.  
  13086.  sp_addumpdevice
  13087.  ────────────────────────────────────────────────────────────────────────────
  13088.  
  13089.  
  13090.  Function
  13091.  
  13092.  Adds a dump device to SQL Server.
  13093.  
  13094.  
  13095.  Syntax
  13096.  
  13097.    sp_addumpdevice {"disk | diskette"}, logical_name,
  13098.      physical_name, cntrltype [, noskip | skip [, media_capacity]]
  13099.  
  13100.  
  13101.  Examples
  13102.  
  13103.    sp_addumpdevice "diskette", "diskettedumpf",
  13104.      "f:\sqlserver\dump\dsk.dat", 3, noskip, .36
  13105.  
  13106.  
  13107.  Options
  13108.  
  13109.  disk", "diskette"
  13110.    The type of dump device. Use "disk" for adding a hard disk file as a dump
  13111.    device. Use "diskette" for adding a 5-1/4 inch or 3-1/2 inch diskette
  13112.    drive file as a dump device. These options must be enclosed in quotation
  13113.    marks.
  13114.  
  13115.  logical_name
  13116.    The logical name of the dump device, which is used in the LOAD and DUMP
  13117.    SQL statements. This name must follow the rules for identifiers.
  13118.  
  13119.    When you add a dump device with a logical name longer than 30 characters,
  13120.    the dump device is created but the name is truncated to 30 characters.
  13121.  
  13122.  physical_name
  13123.    The physical name of the dump device. This name must follow the rules for
  13124.    operating system filenames and must include a full pathname.
  13125.  
  13126.  cntrltype
  13127.    The controller number of the new dump device. The same controller number
  13128.    can be used for more than one dump device. Different numbers are necessary
  13129.    only if you want to use more than one device simultaneously. For hard disk
  13130.    dump devices, the controller number parameter must be 2. For diskette dump
  13131.    devices, the controller number must be 3 or 4. If there are two diskette
  13132.    dump devices, use controller number 3 for one and controller number 4 for
  13133.    the other.
  13134.  
  13135.  skip, noskip
  13136.    Indicates whether ANSI tape labels are read (noskip) or ignored (skip).
  13137.    Use noskip for diskette dump devices. (The skip option is reserved for
  13138.    future tape support.)
  13139.  
  13140.  media_capacity
  13141.    Diskette size in megabytes. For diskette devices, 1.2 megabytes is the
  13142.    default. If you specify media_capacity, you must also specify the noskip
  13143.    option. The console program prompts the operator to change diskettes when
  13144.    a database dump reaches the specified capacity of the diskette.
  13145.  
  13146.  
  13147.  Comments
  13148.  
  13149.  The sp_addumpdevice system procedure adds a dump device to the
  13150.  master.dbo.sysdevices table. It can then be referred to logically in the
  13151.  DUMP DATABASE, DUMP TRANSACTION, LOAD DATABASE, and LOAD TRANSACTION
  13152.  statements.
  13153.  
  13154.  To add database devices to sysdevices, use the DISK INIT statement.
  13155.  
  13156.  ────────────────────────────────────────────────────────────────────────────
  13157.  Warning
  13158.  
  13159.  If you add a dump device with a physical name of NULL and any logical name,
  13160.  the dump device will be added. However, its physical name will be changed to
  13161.  nul and its logical name changed to diskdump.
  13162.  ────────────────────────────────────────────────────────────────────────────
  13163.  
  13164.  
  13165.  Messages
  13166.  
  13167.    Unknown device type. Use 'disk' or 'diskette'.
  13168.  
  13169.  The value supplied for the first parameter isn't a known dump device type.
  13170.  
  13171.    Device with same logical name already exists.
  13172.  
  13173.  All SQL Server dump devices must have unique logical names. There is already
  13174.  a dump device with the name supplied for the logical_name parameter.
  13175.  
  13176.    Diskette device added.
  13177.  
  13178.  The diskette dump device was added successfully.
  13179.  
  13180.    Disk device added.
  13181.  
  13182.  The disk dump device was added successfully.
  13183.  
  13184.    For disk devices, controller number parameter must be 2.
  13185.  
  13186.  When you are adding a disk dump device, the value for the fourth parameter,
  13187.  cntrltype, must be 2.
  13188.  
  13189.    For diskette devices, controller number parameter must be 3
  13190.    or 4.
  13191.  
  13192.  When you are adding a diskette dump device, the value for the fourth
  13193.  parameter, cntrltype, must be 3 or 4.
  13194.  
  13195.    Please specify media capacity in megabytes (1 megabytes minimum).
  13196.  
  13197.  For diskette dump devices, enter the size in megabytes. The default is 1.2
  13198.  megabytes. The console program prompts the operator to change diskettes when
  13199.  a database dump reaches the specified capacity of the diskette.
  13200.  
  13201.  
  13202.  Permissions
  13203.  
  13204.  Execute permission defaults to the System Administrator.
  13205.  
  13206.  
  13207.  Tables Used
  13208.  
  13209.  master.dbo.sysdevices
  13210.  
  13211.  
  13212.  See Also
  13213.  
  13214.  sp_helpdevice, sp_dropdevice, DISK INIT, DUMP DATABASE, DUMP TRANSACTION,
  13215.  LOAD DATABASE, LOAD TRANSACTION, console
  13216.  
  13217.  
  13218.  sp_adduser
  13219.  ────────────────────────────────────────────────────────────────────────────
  13220.  
  13221.  
  13222.  Function
  13223.  
  13224.  Adds a new user to the current database.
  13225.  
  13226.  
  13227.  Syntax
  13228.  
  13229.    sp_adduser login_id [, username [, grpname]]
  13230.  
  13231.  
  13232.  Examples
  13233.  
  13234.    A. sp_adduser victoria, victoria, fort_mudge
  13235.  
  13236.  SQL Server login ID victoria has already been added to SQL Server with
  13237.  sp_addlogin. In example A we add her to the database. Victoria will be known
  13238.  in the current database by the username victoria and belong to the group
  13239.  fort_mudge (which has previously been created with sp_addgroup).
  13240.  
  13241.    B. sp_adduser margaret
  13242.  
  13243.  Example B adds margaret to the database. Her database username is the same
  13244.  as her SQL Server login ID, and she belongs to the default group public.
  13245.  
  13246.    C. sp_adduser haroldq, harold, fort_mudge
  13247.  
  13248.  Example C adds haroldq to the database. When haroldq uses the current
  13249.  database, his name is harold. He belongs to the fort_mudge group.
  13250.  
  13251.  
  13252.  Options
  13253.  
  13254.  login_id
  13255.    The user's login ID as found in master.dbo.syslogins.
  13256.  
  13257.  username
  13258.    A name for this user in the current database. Omitting this optional
  13259.    parameter causes the username to default to the user's login ID.
  13260.  
  13261.  grpname
  13262.    The name of an existing group in the database. If you don't specify a
  13263.    group, the user becomes a member of the default group public. If you want
  13264.    to add a user to a group, you must supply a value for the username
  13265.    parameter. It can be the same as login_id.
  13266.  
  13267.  
  13268.  Comments
  13269.  
  13270.  The Database Owner executes sp_adduser to add a username to the sysusers
  13271.  table of the current database, enabling the user to access the current
  13272.  database under his or her own name.
  13273.  
  13274.  Specifying a username parameter gives the new user a name in the database
  13275.  different from his or her login ID on SQL Server. The ability to assign a
  13276.  user a different name on SQL Server and in a database is provided as a
  13277.  convenience. This is not an alias as provided by sp_addalias: the user is
  13278.  simply given a different name and is not mapped to the identity and
  13279.  permissions of another user.
  13280.  
  13281.  You can also supply a grpname for the user. The group must already exist in
  13282.  the database.
  13283.  
  13284.  A user can be a member of only one group. Every user is a member of the
  13285.  default group, public, if he or she has not been explicitly added to some
  13286.  other group with sp_adduser or sp_changegroup.
  13287.  
  13288.  To access a database, a user must be listed in sysusers (with sp_adduser) or
  13289.  mapped to another user in sysalternates (with sp_addalias), or there must be
  13290.  a guest entry in sysusers.
  13291.  
  13292.  
  13293.  Messages
  13294.  
  13295.    New user added.
  13296.  
  13297.  The system procedure was successful. The user is now known in the current
  13298.  database.
  13299.  
  13300.    No login with the specified name exists.
  13301.  
  13302.  The login_id you gave is unknown to SQL Server. Each user must have a login
  13303.  ID on SQL Server before he or she can be added to a database.
  13304.  
  13305.    A user with the same name already exists in the database.
  13306.  
  13307.  The username is already a user in the database. Choose another name.
  13308.  
  13309.    User already has alias access to the database.
  13310.  
  13311.  The login_id is already known to the database by an alias. If you still want
  13312.  to add the user, drop the alias with the sp_dropalias system procedure and
  13313.  then re-execute sp_adduser.
  13314.  
  13315.    User already has a login under a different name.
  13316.  
  13317.  The user with the login_id you supplied is listed in the current database's
  13318.  sysusers table with a name different from the one supplied as the username
  13319.  parameter.
  13320.  
  13321.    No group with the specified name exists.
  13322.  
  13323.  The groupname you supplied does not exist in this database. Either omit the
  13324.  grpname parameter or create the group with sp_addgroup first.
  13325.  
  13326.  
  13327.  Permissions
  13328.  
  13329.  Execute permission defaults to the Database Owner.
  13330.  
  13331.  
  13332.  Tables Used
  13333.  
  13334.  master.dbo.syslogins, sysalternates, sysusers,
  13335.  
  13336.  
  13337.  See Also
  13338.  
  13339.  sp_addalias, sp_addgroup, sp_changegroup, sp_dropalias, sp_dropgroup,
  13340.  sp_helpuser, GRANT, REVOKE, USE
  13341.  
  13342.  
  13343.  sp_bindefault
  13344.  ────────────────────────────────────────────────────────────────────────────
  13345.  
  13346.  
  13347.  Function
  13348.  
  13349.  Binds a default to a column or to a user-defined datatype.
  13350.  
  13351.  
  13352.  Syntax
  13353.  
  13354.    sp_bindefault defname, objname [, futureonly]
  13355.  
  13356.  
  13357.  Examples
  13358.  
  13359.    A. sp_bindefault today, "employees.startdate"
  13360.  
  13361.  Assuming that a default named today has been defined in the current database
  13362.  with CREATE DEFAULT, the system procedure in example A binds it to the
  13363.  startdate column of the employees table. Whenever a row is added to the
  13364.  employees table and data for the startdate column is not supplied, the
  13365.  column gets the value of the default today.
  13366.  
  13367.    B. sp_bindefault def_ssn, ssn
  13368.  
  13369.  Assuming that a default named def_ssn and a user-defined datatype named ssn
  13370.  exist, the system procedure in example B binds def_ssn to ssn. The default
  13371.  is inherited by all columns that are assigned the user datatype ssn when a
  13372.  table is created. Existing columns of type ssn also inherit the default
  13373.  def_ssn unless you use the optional third parameter, futureonly (which
  13374.  prevents existing columns of that user datatype from inheriting the
  13375.  default), or unless the column's default has previously been changed (in
  13376.  which case the changed default is maintained).
  13377.  
  13378.    C. sp_bindefault def_ssn, ssn, futureonly
  13379.  
  13380.  Example C binds the default def_ssn to the user datatype ssn. Because the
  13381.  futureonly parameter is included, no existing columns of type ssn are
  13382.  affected.
  13383.  
  13384.  
  13385.  Options
  13386.  
  13387.  defname
  13388.    The name of a default. Defaults are created with CREATE DEFAULT statements
  13389.    and then bound to specific columns or user-defined datatypes with
  13390.    sp_bindefault.
  13391.  
  13392.  objname
  13393.    The name of the table and column or user-defined datatype to which the
  13394.    default is to be bound. If the objname is not of the form "table.column",
  13395.    it is assumed to be a user datatype. (Quotation marks are required around
  13396.    all system procedure parameters that have embedded blanks or punctuation.)
  13397.    By default, existing columns of the user datatype inherit the default
  13398.    defname unless the column's default has previously been changed.
  13399.  
  13400.  futureonly
  13401.    Prevents existing columns of a user datatype from inheriting the new
  13402.    default. This parameter is optional when binding a default to a
  13403.    user-defined datatype. It is never used when binding a default to a
  13404.    column.
  13405.  
  13406.  
  13407.  Comments
  13408.  
  13409.  First use the CREATE DEFAULT statement to create a default. Then execute
  13410.  sp_bindefault to bind it to a column or user datatype in the current
  13411.  database.
  13412.  
  13413.  You cannot bind a default to an SQL-Server-supplied datatype.
  13414.  
  13415.  If binding to a column, the objname parameter must be of the form
  13416.  "table.column". Any other format is taken to be the name of a user datatype.
  13417.  
  13418.  
  13419.  You can bind a new default to a column or user datatype using sp_bindefault
  13420.  without unbinding an existing default. The old default is overridden.
  13421.  
  13422.  Existing columns of the user-defined datatype inherit the new default unless
  13423.  their default has previously been changed or the value of the optional third
  13424.  parameter is futureonly. New columns of the user-defined datatype always
  13425.  inherit the default.
  13426.  
  13427.  The default must be compatible with the datatype of the column. You cannot
  13428.  use "N/A", for example, as a default for a numeric column. If the default is
  13429.  not compatible with the column to which you've bound it, SQL Server will
  13430.  generate an error message when it tries to insert the default value (not
  13431.  when you bind it).
  13432.  
  13433.  
  13434.  Messages
  13435.  
  13436.    Default bound to column.
  13437.  
  13438.  The default was successfully bound to the specified column in the specified
  13439.  table.
  13440.  
  13441.    Default bound to datatype.
  13442.  
  13443.  The default was successfully bound to the specified user-defined datatype.
  13444.  
  13445.    Default and table or usertype must be in 'current' database.
  13446.  
  13447.  The objname parameter supplied with the system procedure contained a
  13448.  reference to another database. Defaults can be bound to objects in the
  13449.  current database only.
  13450.  
  13451.    No such default exists. You must create the default first.
  13452.  
  13453.  First create the default in the current database with CREATE DEFAULT. Then
  13454.  execute sp_bindefault.
  13455.  
  13456.    You do not own a column of that name.
  13457.  
  13458.  Only the owner of a table can bind a default to any of its columns. You are
  13459.  not the owner.
  13460.  
  13461.    You do not own a datatype of that name.
  13462.  
  13463.  Only the owner of a user-defined datatype can bind a default to it. You are
  13464.  not the owner.
  13465.  
  13466.    The new default has been bound to column(s) of the specified
  13467.    user datatype.
  13468.  
  13469.  Existing columns of the user-defined datatype specified now have the new
  13470.  default bound to them (unless their defaults were previously changed).
  13471.  
  13472.  
  13473.  Permissions
  13474.  
  13475.  Execute permission defaults to the object owner.
  13476.  
  13477.  
  13478.  Tables Used
  13479.  
  13480.  syscolumns, sysobjects, systypes
  13481.  
  13482.  
  13483.  See Also
  13484.  
  13485.  sp_bindrule, sp_unbindefault, sp_unbindrule, CREATE DEFAULT, DROP DEFAULT
  13486.  
  13487.  
  13488.  sp_bindrule
  13489.  ────────────────────────────────────────────────────────────────────────────
  13490.  
  13491.  
  13492.  Function
  13493.  
  13494.  Binds a rule to a column or user datatype.
  13495.  
  13496.  
  13497.  Syntax
  13498.  
  13499.    sp_bindrule rulename, objname [, futureonly]
  13500.  
  13501.  
  13502.  Examples
  13503.  
  13504.    A. sp_bindrule today, "employees.startdate"
  13505.  
  13506.  Assuming that a rule named today has been created in the current database
  13507.  with CREATE RULE, the system procedure in example A binds it to the
  13508.  startdate column of the employees table. When a row is added to employees,
  13509.  the data for the startdate column is checked against the today rule.
  13510.  
  13511.    B. sp_bindrule rule_ssn, ssn
  13512.  
  13513.  Assuming the existence of a rule named rule_ssn and a user datatype named
  13514.  ssn, the system procedure in example B binds rule_ssn to ssn. In a CREATE
  13515.  TABLE statement, columns of type ssn inherit the rule_ssn rule. Existing
  13516.  columns of type ssn also inherit the rule_ssn rule unless you use the
  13517.  optional third parameter, futureonly (which prevents existing columns of
  13518.  type ssn from inheriting the rule) or ssn's rule has previously been changed
  13519.  (in which case the changed rule is maintained).
  13520.  
  13521.    C. sp_bindrule rule_ssn, ssn, futureonly
  13522.  
  13523.  The rule_ssn rule is bound to the user type ssn, but no existing columns of
  13524.  type ssn are affected (example C).
  13525.  
  13526.  
  13527.  Options
  13528.  
  13529.  rulename
  13530.    The name of a rule. Rules are created with CREATE RULE statements and then
  13531.    bound to specific columns or user datatypes with sp_bindrule.
  13532.  
  13533.  objname
  13534.    The name of the table and column or user datatype to which the rule is to
  13535.    be bound. If the parameter is not of the form "table.column", it is
  13536.    assumed to be a user datatype. (Quotation marks are required around all
  13537.    system procedure parameters that have embedded blanks or punctuation.)
  13538.  
  13539.  futureonly
  13540.    Prevents existing columns of a user datatype from inheriting the new rule.
  13541.    This parameter is optional when binding a rule to a user datatype. It is
  13542.    never used when binding a rule to a column.
  13543.  
  13544.  
  13545.  Comments
  13546.  
  13547.  The rule is enforced when an INSERT is attempted, not at binding. You can
  13548.  bind a character rule to a column of numeric datatype, even though such an
  13549.  INSERT is illegal.
  13550.  
  13551.  First use the CREATE RULE statement to create a rule. Then execute
  13552.  sp_bindrule to bind it to a column or user datatype in the current database.
  13553.  
  13554.  
  13555.  You cannot bind a rule to an SQL-Server-supplied datatype.
  13556.  
  13557.  If binding to a column, the objname parameter must be of the form
  13558.  "table.column". Any other format is taken to be the name of a user datatype.
  13559.  
  13560.  
  13561.  You can bind a new rule to a column or user datatype using sp_bindrule
  13562.  without unbinding an existing rule. The old rule is overridden.
  13563.  
  13564.  Existing columns of the user datatype inherit the new rule unless their rule
  13565.  has previously been changed or the value of the optional third parameter is
  13566.  futureonly. New columns of the user datatype always inherit the rule.
  13567.  
  13568.  
  13569.  Messages
  13570.  
  13571.    Rule bound to table column.
  13572.  
  13573.  The rule was successfully bound to the specified column in the specified
  13574.  table.
  13575.  
  13576.    Rule bound to datatype.
  13577.  
  13578.  The rule was successfully bound to the specified user datatype.
  13579.  
  13580.    Rule and table or usertype must be in 'current' database.
  13581.  
  13582.  The objname parameter supplied with the procedure contained a reference to
  13583.  another database. Rules can be bound to objects in the current database
  13584.  only.
  13585.  
  13586.    No such rule exists. You must create the rule first.
  13587.  
  13588.  First create the rule in the current database with CREATE RULE. Then execute
  13589.  sp_bindrule.
  13590.  
  13591.    You do not own a column of that name.
  13592.  
  13593.  Only the owner of a table can bind a rule to any of its columns. You are not
  13594.  the owner.
  13595.  
  13596.    You do not own a datatype of that name.
  13597.  
  13598.  Only the owner of a user datatype can bind a rule to it. You are not the
  13599.  owner.
  13600.  
  13601.    The new rule has been bound to column(s) of the specified
  13602.     user datatype.
  13603.  
  13604.  Existing columns of the specified user datatype now have the new rule bound
  13605.  to them (unless their rules were previously changed).
  13606.  
  13607.  
  13608.  Permissions
  13609.  
  13610.  Execute permission defaults to the object owner.
  13611.  
  13612.  
  13613.  Tables Used
  13614.  
  13615.  syscolumns, sysobjects, systypes
  13616.  
  13617.  
  13618.  See Also
  13619.  
  13620.  sp_bindefault, sp_unbindefault, sp_unbindrule, CREATE RULE, DROP RULE
  13621.  
  13622.  
  13623.  sp_changedbowner
  13624.  ────────────────────────────────────────────────────────────────────────────
  13625.  
  13626.  
  13627.  Function
  13628.  
  13629.  Changes the owner of a database.
  13630.  
  13631.  
  13632.  Syntax
  13633.  
  13634.    sp_changedbowner login_id
  13635.  
  13636.  
  13637.  Examples
  13638.  
  13639.    sp_changedbowner albert
  13640.  
  13641.  This example makes the user albert the owner of the current database.
  13642.  
  13643.  
  13644.  Options
  13645.  
  13646.  login_id
  13647.    The login ID of the new owner of the current database. The new owner must
  13648.    not already be known as either a user or alias (that is, the new owner
  13649.    must not already be listed in sysusers or sysalternates).
  13650.  
  13651.  
  13652.  Comments
  13653.  
  13654.  The sp_changedbowner system procedure makes login_id the owner of the
  13655.  current database.
  13656.  
  13657.  The new owner must already have a login ID on SQL Server but must not have a
  13658.  database name or alias name in the database. If you want to assign database
  13659.  ownership to such a user, drop his or her database name and alias entries
  13660.  before executing sp_changedbowner.
  13661.  
  13662.  After the sp_changedbowner system procedure is executed, the new owner is
  13663.  known as Database Owner inside the database.
  13664.  
  13665.  If you (as System Administrator) want to grant permissions to this new
  13666.  owner, grant them to Database Owner because the user is no longer known
  13667.  inside the database under any other name.
  13668.  
  13669.  
  13670.  Messages
  13671.  
  13672.    Only the System Administrator can change the owner of a database.
  13673.  
  13674.  You are not the System Administrator, so you can't change the owner of the
  13675.  current database.
  13676.  
  13677.    You can't change the owner of the Master Database.
  13678.  
  13679.  The owner of the master database cannot be changed by anyone.
  13680.  
  13681.    No login with the specified name exists.
  13682.  
  13683.  The proposed new Database Owner must have a login ID on SQL Server.
  13684.  
  13685.    The proposed new db owner already is a user in the database.
  13686.  
  13687.  The specified login_id is already a user in the current database. To make
  13688.  him or her the Database Owner, first drop his or her user entry from the
  13689.  current database's sysusers table.
  13690.  
  13691.    The proposed new db owner already is aliased in the database.
  13692.  
  13693.  The specified login_id is already aliased in the current database. To make
  13694.  him or her the Database Owner, first drop his or her user alias entry from
  13695.  the current database's sysalternates table.
  13696.  
  13697.    Database owner changed.
  13698.  
  13699.  The Database Owner was successfully changed.
  13700.  
  13701.  
  13702.  Permissions
  13703.  
  13704.  Execute permission defaults to the Database Owner.
  13705.  
  13706.  
  13707.  Tables Used
  13708.  
  13709.  master.dbo.sysdatabases, master.dbo.syslogins, sysalternates, sysusers
  13710.  
  13711.  
  13712.  See Also
  13713.  
  13714.  sp_addlogin, sp_dropalias, sp_dropuser, sp_helpdb, CREATE DATABASE
  13715.  
  13716.  
  13717.  sp_changegroup
  13718.  ────────────────────────────────────────────────────────────────────────────
  13719.  
  13720.  
  13721.  Function
  13722.  
  13723.  Changes a user's group.
  13724.  
  13725.  
  13726.  Syntax
  13727.  
  13728.    sp_changegroup grpname, username
  13729.  
  13730.  
  13731.  Examples
  13732.  
  13733.    sp_changegroup fort_mudge, albert
  13734.  
  13735.  The user albert is now a member of the fort_mudge group. It doesn't matter
  13736.  what group albert belonged to before.
  13737.  
  13738.  
  13739.  Options
  13740.  
  13741.  grpname
  13742.    The name of the group. The group must already exist in the current
  13743.    database.
  13744.  
  13745.  username
  13746.    The name of the user to add to the group. The user must already exist in
  13747.    the current database.
  13748.  
  13749.  
  13750.  Comments
  13751.  
  13752.  Executing sp_changegroup adds the specified user to the specified group. The
  13753.  user is dropped from the group he or she belongs to and is added to the one
  13754.  specified by grpname.
  13755.  
  13756.  New database users can be added to groups at the same time they are given
  13757.  access to the database with sp_adduser.
  13758.  
  13759.  Every user is a member of the default group, public, if he or she has not
  13760.  been explicitly added to some other group with sp_adduser or sp_changegroup.
  13761.  
  13762.  
  13763.  A user can be a member of only one group.
  13764.  
  13765.  Groups are used as a collective name for granting and revoking permissions.
  13766.  
  13767.  
  13768.  If you use public as the grpname parameter, it must be enclosed in quotation
  13769.  marks because it is an SQL keyword.
  13770.  
  13771.  
  13772.  Messages
  13773.  
  13774.    Group changed.
  13775.  
  13776.  The user now belongs to the specified group.
  13777.  
  13778.    No such group exists.
  13779.  
  13780.  The specified group doesn't exist in the current database.
  13781.  
  13782.    No such user exists.
  13783.  
  13784.  The specified user doesn't exist in the current database.
  13785.  
  13786.  
  13787.  Permissions
  13788.  
  13789.  Execute permission defaults to the Database Owner.
  13790.  
  13791.  
  13792.  Tables Used
  13793.  
  13794.  sysusers
  13795.  
  13796.  
  13797.  See Also
  13798.  
  13799.  sp_addgroup, sp_adduser, sp_dropgroup, sp_helpgroup, GRANT, REVOKE
  13800.  
  13801.  
  13802.  sp_commonkey
  13803.  ────────────────────────────────────────────────────────────────────────────
  13804.  
  13805.  
  13806.  Function
  13807.  
  13808.  Defines a common key between two tables or views.
  13809.  
  13810.  
  13811.  Syntax
  13812.  
  13813.    sp_commonkey tabaname, tabbname, col1a, col1b
  13814.      [, col2a, col2b, ..., col8a, col8b]
  13815.  
  13816.  
  13817.  Examples
  13818.  
  13819.    sp_commonkey projects, departments, empid, empid
  13820.  
  13821.  Assume two tables, projects and departments, each with a column named empid.
  13822.  This statement defines a frequently used join on the two columns.
  13823.  
  13824.  
  13825.  Options
  13826.  
  13827.  tabaname
  13828.    The name of the first table or view to be joined.
  13829.  
  13830.  tabbname
  13831.    The name of the second table or view to be joined.
  13832.  
  13833.  col1a
  13834.    The name of the first column in the tabaname table or view that makes up
  13835.    the common key. At least one pair of columns (one column from the first
  13836.    table or view and one from the second table or view) must be specified.
  13837.  
  13838.  col1b
  13839.    The name of the partner column in the tabbname table or view that is
  13840.    joined with col1a in the tabaname table or view.
  13841.  
  13842.  
  13843.  Comments
  13844.  
  13845.  Common keys are created to make explicit a logical relationship that is
  13846.  implicit in your database design. The information can be used by an
  13847.  application.
  13848.  
  13849.  Executing sp_commonkey adds the key to the syskeys system table. To display
  13850.  a report on the common keys that have been defined, execute sp_helpkey.
  13851.  
  13852.  You must be the owner of at least one of the two tables or views to define a
  13853.  common key between them.
  13854.  
  13855.  The number of columns from the first table or view must be the same as the
  13856.  number of columns from the second table or view. Up to eight columns from
  13857.  each table or view can participate in the common key.
  13858.  
  13859.  The datatypes of the common columns must agree, but the length
  13860.  specification, if applicable, and nulltypes need not agree. Common keys have
  13861.  been assigned within the master and model databases.
  13862.  
  13863.  
  13864.  Messages
  13865.  
  13866.    New common key added.
  13867.  
  13868.  The common key between the specified tables or views has been added to
  13869.  syskeys.
  13870.  
  13871.    First table in the common key doesn't exist.
  13872.  
  13873.  The table or view you gave as tabaname doesn't exist in the current
  13874.  database.
  13875.  
  13876.    Second table in the common key doesn't exist.
  13877.  
  13878.  The table or view you gave as tabbname doesn't exist in the current
  13879.  database.
  13880.  
  13881.    Only the table owner may define its common key.
  13882.  
  13883.  You aren't the owner of the tabaname table or view.
  13884.  
  13885.    The tables or views have no such first column or the columns
  13886.  
  13887.     are of different types.
  13888.  
  13889.  Two cases generate this message: either the column pair that you specified
  13890.  doesn't exist or the columns in the pair are different types.
  13891.  
  13892.  
  13893.  Permissions
  13894.  
  13895.  Execute permission defaults to the owner of tabaname or tabbname.
  13896.  
  13897.  
  13898.  Tables Used
  13899.  
  13900.  syscolumns, syskeys, sysobjects
  13901.  
  13902.  
  13903.  See Also
  13904.  
  13905.  sp_dropkey, sp_foreignkey, sp_helpjoins, sp_helpkey, sp_primarykey, Joins
  13906.  
  13907.  
  13908.  sp_configure
  13909.  ────────────────────────────────────────────────────────────────────────────
  13910.  
  13911.  
  13912.  Function
  13913.  
  13914.  Displays or changes configuration options.
  13915.  
  13916.  
  13917.  Syntax
  13918.  
  13919.    sp_configure [configname [, configvalue]]
  13920.  
  13921.  
  13922.  Examples
  13923.  
  13924.    A. sp_configure
  13925.  
  13926.  Example A displays a list of all the configuration options with their
  13927.  current and permissible range of values, similar to the following:
  13928.  
  13929. ╓┌─┌───────────────────┌─────────┌─────────┌─────────────┌───────────────────╖
  13930.  ────────────────────────────────────────────────────────────────────────────
  13931.    name                minimum   maximum   config_value  run_value
  13932.    -----------------   --------  --------  ------------  ---------
  13933.    recovery interval   1         32767     4             3
  13934.    allow updates       0         1         0             0
  13935.    user connections*   5         35        0             25
  13936.    memory              1000      14000     0             1700
  13937.    open databases      5         100       0             10
  13938.    locks               5000      50000     0             5000
  13939.    open objects        100       10000     0             500
  13940.    procedure cache     1         99        0             20
  13941.    fill factor         0         100       0             0
  13942.    time slice          50        1000      0             100
  13943.    database size       2         10000     0             2
  13944.    media retention     0         365       0             0
  13945.    recovery flags      0         1         0             0
  13946.  ────────────────────────────────────────────────────────────────────────────
  13947.   recovery flags      0         1         0             0
  13948.    serial number       1         999999    0             0
  13949.    (14 rows affected)
  13950.  
  13951.  
  13952.  * SQL Server maximum is 250; the effective number is based upon your system
  13953.  configuration. The maximum number of connections in your computer
  13954.  environment is stored in the @MAX_CONNECTIONS global variable. The
  13955.  config_value column of the report contains the value to which the
  13956.  configuration option has been set with sp_configure. It changes after you
  13957.  execute sp_configure. (This is the value in sysconfigures.value.) The
  13958.  run_value column contains the value SQL Server is using. It changes after
  13959.  you execute the RECONFIGURE statement. (This is the value in
  13960.  syscurconfigs.value.)
  13961.  
  13962.  
  13963.    B. sp_configure "recovery interval", 3
  13964.  
  13965.  Example B sets the system recovery interval to 3 minutes.
  13966.  
  13967.  
  13968.  Options
  13969.  
  13970.  configname
  13971.    The name of the configuration option. SQL Server understands any unique
  13972.    string that is part of the configuration name.
  13973.  
  13974.  configvalue
  13975.    The value for the configuration option.
  13976.  
  13977.  
  13978.  Comments
  13979.  
  13980.  All users can execute sp_configure with no parameters (SQL Server displays a
  13981.  list of all the configuration options and their current values) or with one
  13982.  parameter, the name of the configuration options (SQL Server displays the
  13983.  value for that variable).
  13984.  
  13985.  The System Administrator can execute sp_configure with both parameters to
  13986.  change the value of one of the configuration options. The second parameter
  13987.  is the new config_value. After sp_configure has been executed, the System
  13988.  Administrator must execute the RECONFIGURE statement to install the changed
  13989.  value. Then, for all variables except allow updates and recovery interval,
  13990.  the System Administrator must restart SQL Server.
  13991.  
  13992.  Use RECONFIGURE WITH OVERRIDE when you set allow updates on or when you set
  13993.  a configuration option to a value that SQL Server considers less than
  13994.  optimal.
  13995.  
  13996.  To instruct SQL Server to supply a default configuration option, give the
  13997.  value 0 as the config_value.
  13998.  
  13999.  See the SQL Server System Administrator's Guide for details.
  14000.  
  14001.  
  14002.  Messages
  14003.  
  14004.    Configuration option changed. Run the RECONFIGURE statement
  14005.  
  14006.     to install.
  14007.  
  14008.  After changing a configuration option with sp_configure, the change does not
  14009.  take effect until the RECONFIGURE statement is executed and (for all but
  14010.  allow updates and recovery interval) SQL Server is restarted.
  14011.  
  14012.    Configuration option doesn't exist.
  14013.  
  14014.  The name supplied as the config_name parameter is unknown.
  14015.  
  14016.    Configuration option is not unique.
  14017.  
  14018.  The name supplied as the config_name parameter is not unique. No
  14019.  configuration option was changed. For example, two of the configuration
  14020.  options are recovery interval and recovery flags. Using recovery for the
  14021.  config_name parameter generates this message because it matches both names.
  14022.  The complete names that match the string supplied are printed out so that
  14023.  you can see how to make the config_name more specific.
  14024.  
  14025.    Only the System Administrator may change configuration parameters.
  14026.  
  14027.  Although any user can examine the state of the configuration options, only
  14028.  the System Administrator can change them.
  14029.  
  14030.    Configuration option value is not legal.
  14031.  
  14032.  The config_value supplied is not in the range of permissible values for the
  14033.  specified configuration option. For a display of the range of permissible
  14034.  values, rerun sp_configure with the name of the configuration option as the
  14035.  only parameter. A config_value of 0 is always legal. It instructs SQL Server
  14036.  to set the configuration value to its default.
  14037.  
  14038.  
  14039.  Permissions
  14040.  
  14041.  Execute permission for no parameters or the first parameter defaults to all
  14042.  users. Execute permission for both parameters defaults to the System
  14043.  Administrator.
  14044.  
  14045.  
  14046.  Tables Used
  14047.  
  14048.  sysconfigures, syscurconfigs, spt_values
  14049.  
  14050.  
  14051.  See Also
  14052.  
  14053.  sp_dboption, RECONFIGURE
  14054.  
  14055.  
  14056.  
  14057.  
  14058.  
  14059.  sp_dboption
  14060.  ────────────────────────────────────────────────────────────────────────────
  14061.  
  14062.  
  14063.  Function
  14064.  
  14065.  Displays or changes database options.
  14066.  
  14067.  
  14068.  Syntax
  14069.  
  14070.    sp_dboption [dbname, optname, {true | false}]
  14071.  
  14072.  
  14073.  Examples
  14074.  
  14075.    A. sp_dboption
  14076.  
  14077.  Example A displays a list of the database options:
  14078.  
  14079.    Settable database options.
  14080.  
  14081.     database_options
  14082.     -----------------------
  14083.     ALL SETTABLE OPTIONS
  14084.     dbo use only
  14085.     no chkpt on recovery
  14086.     read only
  14087.     select into/bulkcopy
  14088.     single user
  14089.     trunc. log on chkpt.
  14090.  
  14091.     (7 rows affected)
  14092.  
  14093.    B. use master
  14094.     go
  14095.     sp_dboption pubs, read, true
  14096.     use pubs
  14097.     go
  14098.     checkpoint
  14099.  
  14100.  Example B makes the pubs database read only.
  14101.  
  14102.    C. use master
  14103.     go
  14104.     sp_dboption pubs, read, false
  14105.     use pubs
  14106.     go
  14107.     checkpoint
  14108.  
  14109.  Example C makes the pubs database writable again.
  14110.  
  14111.  
  14112.  Options
  14113.  
  14114.  dbname
  14115.    The name of the database in which you want to set the option. You must be
  14116.    using master to execute sp_dboption with parameters (that is, to change a
  14117.    database option). However, the database name cannot be master; you cannot
  14118.    change the master database option settings.
  14119.  
  14120.  optname
  14121.    The name of the option you want to set or unset. SQL Server understands
  14122.    any unique string that is part of the option name. Use quotation marks
  14123.    around the option name if it includes embedded blanks.
  14124.  
  14125.  true, false
  14126.    Use true if you want to set the option and false if you want to turn off
  14127.    the option.
  14128.  
  14129.  
  14130.  Comments
  14131.  
  14132.  You cannot change any of the database option settings for the master
  14133.  database.
  14134.  
  14135.  To display a list of the database options the user can set, execute
  14136.  sp_dboption with no parameters from inside the master database.
  14137.  
  14138.  For a report on which database options are set in a particular database,
  14139.  execute sp_helpdb.
  14140.  
  14141.  The Database Owner or System Administrator can set or unset particular
  14142.  database options for all new databases by executing sp_dboption on the model
  14143.  database.
  14144.  
  14145.  After sp_dboption has been executed, the change does not take effect until
  14146.  the CHECKPOINT statement is executed in the database for which the option
  14147.  was changed.
  14148.  
  14149.  If you change a database option with sp_dboption inside a user-defined
  14150.  transaction and then roll back that transaction, you must execute a
  14151.  CHECKPOINT statement for the change to take effect. Here's an example:
  14152.  
  14153.    begin tran
  14154.     use master
  14155.     go
  14156.     sp_dboption orderentry, single, true
  14157.     go
  14158.     use orderentry
  14159.     go
  14160.     checkpoint
  14161.     go
  14162.     rollback tran
  14163.     go
  14164.     /*
  14165.     ** If the following CHECKPOINT is not executed,
  14166.     ** the orderentry
  14167.     ** database remains single-user.
  14168.     */
  14169.     checkpoint
  14170.     go
  14171.  
  14172.  While the dbo use only option is set (or true), only the Database Owner can
  14173.  use the database.
  14174.  
  14175.  The read only option means that users can retrieve data from the database
  14176.  but can't modify anything.
  14177.  
  14178.  When single user is set to true, only one user at a time can access the
  14179.  database.
  14180.  
  14181.  The trunc. log on chkpt. option means that the transaction log is truncated
  14182.  (committed transactions are removed) every time the CHECKPOINT checking
  14183.  process occurs (usually more than once per minute). It may be useful to turn
  14184.  this option on while doing development work, to prevent the log from
  14185.  growing. While the trunc. log on chkpt. option is set, you cannot use DUMP
  14186.  TRANSACTION, since dumps from the truncated transaction log dumps cannot be
  14187.  used to recover from a media failure. Issuing the DUMP TRANSACTION statement
  14188.  produces an error message instructing you to use DUMP DATABASE instead.
  14189.  
  14190.  The select into/bulkcopy option must be set if you want to use SELECT INTO
  14191.  on a permanent table or do a "fast" bulk copy into a table that has no
  14192.  indexes. The fast version of bulk copy (using bcp) copies data into tables
  14193.  that have no indexes. Since these operations are not logged, DUMP
  14194.  TRANSACTION is prohibited when this option is set. The user is instructed to
  14195.  use DUMP DATABASE instead. You do not have to set the select into/bulkcopy
  14196.  option to use SELECT INTO on a temporary table since the temporary database
  14197.  is never recovered. The option need not be set to run bcp on a table that
  14198.  has indexes because tables with indexes are always copied with the slower
  14199.  version and are logged. By default, the select into/bulkcopy option is off
  14200.  in newly created databases. To change the default situation, set this option
  14201.  in the model database.
  14202.  
  14203.  The no chkpt on recovery option is set (true) when an up-to-date copy of a
  14204.  database is kept. In these situations, there is a "primary" and a
  14205.  "secondary" database. Initially, the primary database is dumped and loaded
  14206.  into the secondary database. Then, at intervals, the transaction log of the
  14207.  primary database is dumped and loaded into the secondary database.
  14208.  
  14209.  If this option is off (false), the default condition, a checkpoint record is
  14210.  added to a database after it is recovered due to restarting SQL Server. This
  14211.  checkpoint, which insures that the recovery mechanism won't unnecessarily be
  14212.  rerun, changes the sequence number and causes a subsequent load of the
  14213.  transaction log from the primary database to fail.
  14214.  
  14215.  If this option is on (true), no checkpoint record is added to a database
  14216.  after it is recovered, so that subsequent transaction log dumps from the
  14217.  primary database can be loaded into it.
  14218.  
  14219.  See the SQL Server System Administrator's Guide for additional details on
  14220.  database options.
  14221.  
  14222.  
  14223.  Messages
  14224.  
  14225.    No such database -- run sp_helpdb to list databases.
  14226.  
  14227.  No database with the supplied name exists. Run sp_helpdb to get a list of
  14228.  databases.
  14229.  
  14230.    The `master' database's options cannot be changed.
  14231.  
  14232.  No one can change any of master database option settings.
  14233.  
  14234.    Settable database options.
  14235.  
  14236.  Executing sp_dboption with no parameters displays a list of the options the
  14237.  user can set.
  14238.  
  14239.    Database option doesn't exist or can't be set by user.
  14240.  
  14241.  Run sp_dboption with no parameters to see options. This message is generated
  14242.  in two cases: either the option doesn't exist or the user does not have
  14243.  permission to set or unset it. Run the system procedure with no parameters
  14244.  to display a list of the options the user can set.
  14245.  
  14246.    Database option is not unique.
  14247.  
  14248.  The name supplied as the optname parameter is not unique. No database option
  14249.  value was changed. For example, two of the database options are dbo use only
  14250.  and read only. Using only for the optname parameter generates this message
  14251.  because it matches both names. The complete names that match the supplied
  14252.  string are printed out so that you can see how to make the optname more
  14253.  specific.
  14254.  
  14255.    Only the System Administrator or the owner of the database
  14256.  
  14257.     may set db options.
  14258.  
  14259.  Although all users can look at a list of the settable database options, only
  14260.  the System Administrator or Database Owner can set or unset them. You aren't
  14261.  the System Administrator or the Database Owner.
  14262.  
  14263.    You must be in the `master' database to change database options.
  14264.  
  14265.  To change a database option (of any database other than master), execute the
  14266.  sp_dboption system procedure with the appropriate parameters, while using
  14267.  master.
  14268.  
  14269.    usage: sp_dboption [dbname, optname, {true | false}]
  14270.  
  14271.  Either the optname parameter was omitted or the optvalue parameter was
  14272.  something other than true or false.
  14273.  
  14274.    Run the CHECKPOINT command in the database that was changed.
  14275.  
  14276.  The change in the database option takes effect after the CHECKPOINT
  14277.  statement is run.
  14278.  
  14279.  
  14280.  Permissions
  14281.  
  14282.  Execute permission for no parameters (display options only) defaults to all
  14283.  users. Execute permission for parameters (change an option) defaults to the
  14284.  System Administrator and Database Owner of the database for which the option
  14285.  is to be changed.
  14286.  
  14287.  
  14288.  Tables Used
  14289.  
  14290.  master.dbo.spt_values, master.dbo.sysdatabases
  14291.  
  14292.  
  14293.  See Also
  14294.  
  14295.  sp_configure, sp_helpdb, sp_helpjoins, CHECKPOINT, SELECT
  14296.  
  14297.  
  14298.  sp_defaultdb
  14299.  ────────────────────────────────────────────────────────────────────────────
  14300.  
  14301.  
  14302.  Function
  14303.  
  14304.  Changes a user's default database.
  14305.  
  14306.  
  14307.  Syntax
  14308.  
  14309.    sp_defaultdb login_id, defdb
  14310.  
  14311.  
  14312.  Examples
  14313.  
  14314.    sp_defaultdb victoria, pubs
  14315.  
  14316.  This example sets the default database for victoria to pubs.
  14317.  
  14318.  
  14319.  Options
  14320.  
  14321.  login_id
  14322.    The login ID of the user.
  14323.  
  14324.  defdb
  14325.    The name of the user's default, or "home," database (the database to which
  14326.    the user is connected when he or she logs in).
  14327.  
  14328.  
  14329.  Comments
  14330.  
  14331.  A default database can be set either with sp_defaultdb or with sp_addlogin
  14332.  when the user's login ID is first added to SQL Server.
  14333.  
  14334.  The database to which users are connected if no default database has been
  14335.  specified, either with sp_defaultdb or sp_addlogin, is master.
  14336.  
  14337.  After sp_defaultdb is executed, the user is connected to the new defdb the
  14338.  next time he or she logs in. However, this system procedure does not
  14339.  automatically give the user access to that database. The Database Owner must
  14340.  give the user database access through sp_adduser or sp_addalias, or there
  14341.  must be a guest user in the database's sysusers table. If the user does not
  14342.  have access to the database by any of these means, she or he is connected to
  14343.  master, and an error message is displayed.
  14344.  
  14345.  If a user's default database is dropped, the user is connected to master on
  14346.  his or her next login, and an error message is displayed.
  14347.  
  14348.  The System Administrator can change anyone's default database with this
  14349.  system procedure. All others can change only their own default database.
  14350.  
  14351.  
  14352.  Messages
  14353.  
  14354.    Default database changed.
  14355.  
  14356.  The default database for login_id has been changed successfully.
  14357.  
  14358.    No such account exists.
  14359.  
  14360.  There is no login ID for login_id. Nothing was changed.
  14361.  
  14362.    Database name not valid -- default not changed.
  14363.  
  14364.  There is no database with the specified name.
  14365.  
  14366.    You can't change someone else's default database.
  14367.  
  14368.  Every user can change his or her own default database, but only the System
  14369.  Administrator can change other users' default databases. You aren't the
  14370.  System Administrator.
  14371.  
  14372.  
  14373.  Permissions
  14374.  
  14375.  Execute permission defaults to public for his or her own login ID only.
  14376.  Execute permission defaults to the System Administrator for any login ID.
  14377.  
  14378.  
  14379.  Tables Used
  14380.  
  14381.  master.dbo.sysdatabases, master.dbo.syslogins
  14382.  
  14383.  
  14384.  See Also
  14385.  
  14386.  sp_addlogin, sp_droplogin, sp_password, USE
  14387.  
  14388.  
  14389.  sp_depends
  14390.  ────────────────────────────────────────────────────────────────────────────
  14391.  
  14392.  
  14393.  Function
  14394.  
  14395.  Displays information about database object dependencies─the views and
  14396.  procedures that depend on the table or view specified, and the tables and
  14397.  views that are depended on by the view or procedure specified.
  14398.  
  14399.  
  14400.  Syntax
  14401.  
  14402.    sp_depends objname
  14403.  
  14404.  
  14405.  Examples
  14406.  
  14407.    A. sp_depends sysobjects
  14408.  
  14409.  Example A lists the database objects that depend on the sysobjects table.
  14410.  
  14411.    B. sp_depends titleview
  14412.  
  14413.     Things the object references in the current database.
  14414.  
  14415.     object     type    updated  selected
  14416.     -------------------- ---------------- -------  --------
  14417.     dbo.authors    user table   no   no
  14418.     dbo.titleauthor   user table   no   no
  14419.     dbo.titles    user table   no   no
  14420.  
  14421.     Things inside the current database that reference the object.
  14422.  
  14423.     object     type
  14424.     ------------------  ---------------
  14425.     dbo.tview2    view
  14426.  
  14427.  
  14428.  Options
  14429.  
  14430.  objname
  14431.    The name of the database object that you want to examine for dependencies.
  14432.    It can be a table, view, stored procedure, or trigger.
  14433.  
  14434.  
  14435.  Comments
  14436.  
  14437.  Executing sp_depends lists all the objects, if any, that depend on objname
  14438.  and all the objects, if any, that objname depends on. For example, views
  14439.  depend on one or more tables and can have procedures or other views that
  14440.  depend on them.
  14441.  
  14442.  The sp_depends system procedure determines the dependencies by looking at
  14443.  sysdepends.
  14444.  
  14445.  An object that references another object is considered dependent on that
  14446.  object.
  14447.  
  14448.  The updated and selected columns in the report from sp_depends are
  14449.  meaningful if the object being reported on is a stored procedure or trigger.
  14450.  The values in these columns indicate whether the stored procedure or trigger
  14451.  updates or selects from that object.
  14452.  
  14453.  References to objects outside the current database are not reported.
  14454.  
  14455.  
  14456.  Messages
  14457.  
  14458.    No such object in the current database.
  14459.  
  14460.  The object name supplied for the objname parameter does not exist in the
  14461.  current database.
  14462.  
  14463.    Things the object references in the current database.
  14464.  
  14465.  These are the objects in the current database that objname depends on.
  14466.  
  14467.    Things inside the current database that reference the object.
  14468.  
  14469.  These are the objects in the current database that reference objname.
  14470.  
  14471.    Object doesn't reference any object and no objects reference
  14472.    it.
  14473.  
  14474.  Nothing depends upon objname, and objname doesn't reference any objects.
  14475.  
  14476.  
  14477.  Permissions
  14478.  
  14479.  Execute permission defaults to the public group.
  14480.  
  14481.  
  14482.  Tables Used
  14483.  
  14484.  master.dbo.spt_values, master.dbo.sysdatabases, sysdepends, sysobjects
  14485.  
  14486.  
  14487.  See Also
  14488.  
  14489.  sp_help, CREATE PROCEDURE, CREATE TABLE, CREATE VIEW, EXECUTE, Views
  14490.  
  14491.  
  14492.  sp_diskdefault
  14493.  ────────────────────────────────────────────────────────────────────────────
  14494.  
  14495.  
  14496.  Function
  14497.  
  14498.  Sets a database device's status to defaulton or defaultoff. This status
  14499.  indicates whether a database device can be used for database storage when
  14500.  the user does not specify a database device, or specifies DEFAULT with
  14501.  CREATE DATABASE or ALTER DATABASE statements.
  14502.  
  14503.  
  14504.  Syntax
  14505.  
  14506.    sp_diskdefault database_device, {defaulton | defaultoff}
  14507.  
  14508.  
  14509.  Examples
  14510.  
  14511.    sp_diskdefault master, defaultoff
  14512.  
  14513.  The master database device is no longer used by CREATE DATABASE or ALTER
  14514.  DATABASE for default storage of a database.
  14515.  
  14516.  
  14517.  Options
  14518.  
  14519.  database_device
  14520.    The logical name of the database device. It must be a database device
  14521.    rather than a dump device.
  14522.  
  14523.  defaulton, defaultoff
  14524.    Use defaulton if the specified database device is to be designated a
  14525.    default database device. Use defaultoff if the specified database device
  14526.    is not to be designated a default database device. The defaulton option
  14527.    will most frequently be used after a database device has been added to the
  14528.    system with DISK INIT. The defaultoff option will most frequently be used
  14529.    to change the default status of the master database device (which is on
  14530.    when SQL Server is first installed).
  14531.  
  14532.  
  14533.  Comments
  14534.  
  14535.  A default database device is one that is used for database storage by CREATE
  14536.  DATABASE or ALTER DATABASE if the user does not specify a database
  14537.  devicename or if the user gives the DEFAULT keyword.
  14538.  
  14539.  When you first install SQL Server, the master database device is the only
  14540.  default database device.
  14541.  
  14542.  To find out which database devices are default database devices, execute the
  14543.  sp_helpdevice system procedure.
  14544.  
  14545.  
  14546.  Messages
  14547.  
  14548.    No such device exists.
  14549.  
  14550.  The database devicename supplied for the database_device doesn't exist on
  14551.  SQL Server. Run the sp_helpdevice system procedure without a parameter to
  14552.  see a list of all database devices. To add a new database device to the
  14553.  system, use the DISK INIT statement.
  14554.  
  14555.    The device name supplied is not a database disk.
  14556.  
  14557.  The database devicename supplied is in sysdevices, but it is a dump device
  14558.  rather than a database device. Run the sp_helpdevice system procedure
  14559.  without a parameter to see a list of all database devices. To add a new
  14560.  database device to the system, use the DISK INIT statement.
  14561.  
  14562.    Only the System Administrator may execute this procedure.
  14563.  
  14564.  Only the System Administrator can change the default status of database
  14565.  devices.
  14566.  
  14567.    Usage: sp_diskdefault logical_name {defaulton | defaultoff}.
  14568.  
  14569.  The second parameter must be either defaulton or defaultoff.
  14570.  
  14571.  
  14572.  Permissions
  14573.  
  14574.  Execute permission defaults to the System Administrator.
  14575.  
  14576.  
  14577.  Tables Used
  14578.  
  14579.  master.dbo.sysdevices
  14580.  
  14581.  
  14582.  See Also
  14583.  
  14584.  sp_helpdevice, ALTER DATABASE, CREATE DATABASE, DISK INIT
  14585.  
  14586.  
  14587.  sp_dropalias
  14588.  ────────────────────────────────────────────────────────────────────────────
  14589.  
  14590.  
  14591.  Function
  14592.  
  14593.  Removes the alias username identity that had been established with
  14594.  sp_addalias.
  14595.  
  14596.  
  14597.  Syntax
  14598.  
  14599.    sp_dropalias login_id
  14600.  
  14601.  
  14602.  Examples
  14603.  
  14604.    sp_dropalias victoria
  14605.  
  14606.  Assuming that victoria had an alias (for example, to the Database Owner) in
  14607.  the current database, this statement drops victoria's alias from the
  14608.  database.
  14609.  
  14610.  
  14611.  Options
  14612.  
  14613.  login_id
  14614.    The name (in master.dbo.syslogins) of the user who has an alias.
  14615.  
  14616.  
  14617.  Comments
  14618.  
  14619.  Executing the sp_dropalias system procedure deletes an alternate suid
  14620.  mapping for a user from the sysalternates table.
  14621.  
  14622.  When a user's alias is dropped, he or she no longer has access to the
  14623.  database for which the alias was created.
  14624.  
  14625.  
  14626.  Messages
  14627.  
  14628.    Alias user dropped.
  14629.  
  14630.  The user no longer has an alias in the current database. He or she cannot
  14631.  use the database until the Database Owner reinstates him or her using
  14632.  sp_adduser or sp_addalias.
  14633.  
  14634.    No alias for specified user exists.
  14635.  
  14636.  The named user doesn't have an alias in the current database.
  14637.  
  14638.    No login with the specified name exists.
  14639.  
  14640.  The login_id you supplied has no account on SQL Server. No action was taken.
  14641.  
  14642.  
  14643.  
  14644.  Permissions
  14645.  
  14646.  Execute permission defaults to the Database Owner.
  14647.  
  14648.  
  14649.  Tables Used
  14650.  
  14651.  sysalternates
  14652.  
  14653.  
  14654.  See Also
  14655.  
  14656.  sp_addalias, sp_adduser, sp_changedbowner, sp_droplogin, sp_dropuser,
  14657.  sp_helpuser, USE
  14658.  
  14659.  
  14660.  sp_dropdevice
  14661.  ────────────────────────────────────────────────────────────────────────────
  14662.  
  14663.  
  14664.  Function
  14665.  
  14666.  Drops an SQL Server database device or dump device.
  14667.  
  14668.  
  14669.  Syntax
  14670.  
  14671.    sp_dropdevice logical_name
  14672.  
  14673.  
  14674.  Examples
  14675.  
  14676.    A. sp_dropdevice disk5
  14677.  
  14678.  Example A drops the disk5 dump device from SQL Server.
  14679.  
  14680.    B. sp_dropdevice fredsdata
  14681.  
  14682.  Example B drops the fredsdata database device from SQL Server. This database
  14683.  device must not be in use by any databases.
  14684.  
  14685.  
  14686.  Options
  14687.  
  14688.  logical_name
  14689.    The logical name of the database device or dump device as listed in
  14690.    master.dbo.sysdevices.name.
  14691.  
  14692.  
  14693.  Comments
  14694.  
  14695.  The sp_dropdevice system procedure drops a database device or dump device
  14696.  from SQL Server, deleting the entry from master.dbo.sysdevices.
  14697.  
  14698.  ────────────────────────────────────────────────────────────────────────────
  14699.  WARNING
  14700.  
  14701.  After executing sp_dropdevice, you must restart SQL Server because the
  14702.  kernel has a process that is accessing the dropped database device; there is
  14703.  no way to kill the process. Failure to restart SQL Server can cause errors.
  14704.  Also, restarting SQL Server frees up the database device or dump device.
  14705.  ────────────────────────────────────────────────────────────────────────────
  14706.  
  14707.  
  14708.  Messages
  14709.  
  14710.    Device dropped.
  14711.  
  14712.  The database device or dump device has been dropped from the
  14713.  master.dbo.sysdevices table.
  14714.  
  14715.    No device with specified logical name exists.
  14716.  
  14717.  You tried to drop a database device or dump device that doesn't exist on SQL
  14718.  Server.
  14719.  
  14720.    Only the System Administrator may execute this procedure.
  14721.  
  14722.  Only the System Administrator can add or drop database devices or dump
  14723.  devices.
  14724.  
  14725.    Device is being used by a database. You can't drop it.
  14726.  
  14727.  Only database devices that are not in use can be dropped. You must drop all
  14728.  the databases associated with the database device before dropping the
  14729.  database device.
  14730.  
  14731.  
  14732.  Permissions
  14733.  
  14734.  Execute permission defaults to the System Administrator.
  14735.  
  14736.  
  14737.  Tables Used
  14738.  
  14739.  master.dbo.sysdevices
  14740.  
  14741.  
  14742.  See Also
  14743.  
  14744.  sp_addumpdevice, sp_helpdb, sp_helpdevice, DROP DATABASE
  14745.  
  14746.  
  14747.  sp_dropgroup
  14748.  ────────────────────────────────────────────────────────────────────────────
  14749.  
  14750.  
  14751.  Function
  14752.  
  14753.  Drops a group from a database.
  14754.  
  14755.  
  14756.  Syntax
  14757.  
  14758.    sp_dropgroup grpname
  14759.  
  14760.  
  14761.  Examples
  14762.  
  14763.    sp_changegroup accounting, martha
  14764.     sp_changegroup "public", george
  14765.     sp_dropgroup purchasing
  14766.  
  14767.  The purchasing group has merged with the accounting group. Members of the
  14768.  purchasing group, martha and george, are moved to the accounting group and
  14769.  the public group, respectively. Then the purchasing group is dropped. The
  14770.  groupname "public" is in quotation marks because public is a reserved word.
  14771.  
  14772.  
  14773.  
  14774.  Options
  14775.  
  14776.  grpname
  14777.    The name of a group in the current database.
  14778.  
  14779.  
  14780.  Comments
  14781.  
  14782.  Executing sp_dropgroup drops a groupname from a database's sysusers table.
  14783.  
  14784.  You can't drop a group if it has members. You must execute sp_changegroup
  14785.  for each member before you can drop the group.
  14786.  
  14787.  
  14788.  Messages
  14789.  
  14790.    Group has been dropped.
  14791.  
  14792.  The group no longer exists in the current database.
  14793.  
  14794.    No group with the specified name exists.
  14795.  
  14796.  The specified group doesn't exist.
  14797.  
  14798.    Can't drop the group 'public'.
  14799.  
  14800.  The public group exists in every database. It is the group that all users
  14801.  belong to by default and cannot be dropped.
  14802.  
  14803.    Group has members. It must be empty before it can be dropped.
  14804.  
  14805.  Groups with members can't be dropped. Reassign the members of the group to
  14806.  another group using sp_changegroup. When this error message is generated, a
  14807.  list of the group members is displayed.
  14808.  
  14809.  
  14810.  Permissions
  14811.  
  14812.  Execute permission defaults to the Database Owner.
  14813.  
  14814.  
  14815.  Tables Used
  14816.  
  14817.  sysobjects, sysusers
  14818.  
  14819.  
  14820.  See Also
  14821.  
  14822.  sp_addgroup, sp_adduser, sp_changegroup, sp_dropuser, sp_helpgroup, GRANT,
  14823.  REVOKE, USE
  14824.  
  14825.  
  14826.  sp_dropkey
  14827.  ────────────────────────────────────────────────────────────────────────────
  14828.  
  14829.  
  14830.  Function
  14831.  
  14832.  Removes a key from the syskeys table that had been defined using
  14833.  sp_primarykey, sp_foreignkey, or sp_commonkey.
  14834.  
  14835.  
  14836.  Syntax
  14837.  
  14838.    sp_dropkey keytype, tabname [, deptabname]
  14839.  
  14840.  
  14841.  Examples
  14842.  
  14843.    A. sp_dropkey primary, employees
  14844.  
  14845.  Example A drops the primary key for the employees table. Any foreign keys
  14846.  that were dependent on the primary key on employees are also dropped.
  14847.  
  14848.    B. sp_dropkey common, employees, projects
  14849.  
  14850.  Example B drops the common keys between the employees and projects tables.
  14851.  
  14852.  
  14853.  Options
  14854.  
  14855.  keytype
  14856.    The type of key to be dropped. The keytype must be primary, foreign, or
  14857.    common.
  14858.  
  14859.  tabname
  14860.    The name of the table that contains the key to be dropped.
  14861.  
  14862.  deptabname
  14863.    The name of the dependent table in the key to be dropped. Use this option
  14864.    only if the keytype is foreign. The deptabname is the table that contains
  14865.    the foreign key. If the keytype is primary, this parameter is ignored
  14866.    since primary keys have no dependent table.
  14867.  
  14868.  
  14869.  Comments
  14870.  
  14871.  Executing sp_dropkey deletes the specified key from syskeys.
  14872.  
  14873.  Only the owner of a table can drop a key on that table.
  14874.  
  14875.  Dropping a primary key automatically drops any foreign keys associated with
  14876.  it, but dropping a foreign key has no effect on a primary key specified on
  14877.  that table.
  14878.  
  14879.  Keys are created to make explicit a logical relationship that is implicit in
  14880.  your database design. This information can be used by an application
  14881.  program.
  14882.  
  14883.  Executing sp_commonkey, sp_primarykey, or sp_foreignkey adds the key to the
  14884.  syskeys system table. To display a report on the keys that have been
  14885.  defined, execute sp_helpkey.
  14886.  
  14887.  
  14888.  Messages
  14889.  
  14890.    Usage: sp_dropkey {primary | foreign | common}, tabname
  14891.     [,deptabname]. Type must be 'primary', 'foreign', or 'common'.
  14892.  
  14893.  The keytype should specify the type of key to drop.
  14894.  
  14895.    Table or view name must be in 'current' database and owned
  14896.    by you.
  14897.  
  14898.  You can't drop keys on tables in other databases.
  14899.  
  14900.    The table named doesn't exist in the current database.
  14901.  
  14902.  The tabname supplied isn't a table or view in the current database.
  14903.  
  14904.    You must be the owner of the table to drop its key.
  14905.  
  14906.  You aren't the owner of tabname, so you can't drop the key.
  14907.  
  14908.    No primary key for the table exists.
  14909.  
  14910.  The tabname has no primary key defined.
  14911.  
  14912.    Primary key for the table dropped.
  14913.  
  14914.  The primary key was successfully dropped. It has been deleted from syskeys.
  14915.  
  14916.  
  14917.    Dependent foreign keys were also dropped.
  14918.  
  14919.  When a primary key is dropped, any foreign keys that depend on it are also
  14920.  dropped.
  14921.  
  14922.    You need to supply the dependent table as the third parameter.
  14923.  
  14924.  When dropping a foreign or common key, both the tabname and deptabname
  14925.  tables must be named.
  14926.  
  14927.    The dependent table doesn't exist in the current database.
  14928.  
  14929.  The name supplied for the deptabname parameter isn't a table or view in the
  14930.  current database.
  14931.  
  14932.    No foreign key for the table exists.
  14933.  
  14934.  The tabname has no foreign key defined.
  14935.  
  14936.    Foreign key dropped.
  14937.  
  14938.  The foreign key has been successfully dropped. It has been deleted from
  14939.  syskeys.
  14940.  
  14941.    No common keys exist between the two tables supplied.
  14942.  
  14943.  There are no common keys between the tabname and deptabname tables. No
  14944.  action was taken.
  14945.  
  14946.    Common keys dropped.
  14947.  
  14948.  The common keys were successfully dropped. They have been deleted from
  14949.  syskeys.
  14950.  
  14951.  
  14952.  Permissions
  14953.  
  14954.  Execute permission defaults to the owner of tabname.
  14955.  
  14956.  
  14957.  Tables Used
  14958.  
  14959.  syskeys, sysobjects
  14960.  
  14961.  
  14962.  See Also
  14963.  
  14964.  sp_commonkey, sp_foreignkey, sp_helpkey, sp_primarykey
  14965.  
  14966.  
  14967.  sp_droplogin
  14968.  ────────────────────────────────────────────────────────────────────────────
  14969.  
  14970.  
  14971.  Function
  14972.  
  14973.  Drops an SQL Server user by deleting the user's entry in
  14974.  master.dbo.syslogins.
  14975.  
  14976.  
  14977.  Syntax
  14978.  
  14979.    sp_droplogin login_id
  14980.  
  14981.  
  14982.  Examples
  14983.  
  14984.    sp_droplogin victoria
  14985.  
  14986.  The user victoria has been dropped from SQL Server.
  14987.  
  14988.  
  14989.  Options
  14990.  
  14991.  login_id
  14992.    The name of the user as listed in master.dbo.syslogins.
  14993.  
  14994.  
  14995.  Comments
  14996.  
  14997.  Executing sp_droplogin drops a user login ID from SQL Server, deleting the
  14998.  user's entry from master.dbo.syslogins.
  14999.  
  15000.  The sp_droplogin procedure checks the current database (but no others) to
  15001.  see if the login ID to be dropped is a user there. You cannot execute
  15002.  sp_droplogin if the user exists in the current database.
  15003.  
  15004.  
  15005.  Messages
  15006.  
  15007.    Login dropped.
  15008.  
  15009.  The user's entry in master.dbo.syslogins has been deleted. The user no
  15010.  longer has access to SQL Server.
  15011.  
  15012.    No login exists with supplied name.
  15013.  
  15014.  The specified login ID does not exist.
  15015.  
  15016.    Only the System Administrator may execute this procedure.
  15017.  
  15018.  Only the System Administrator can add or drop login IDs. You aren't the
  15019.  System Administrator.
  15020.  
  15021.    User exists in current database. Drop user before dropping
  15022.    login.
  15023.  
  15024.  The specified login ID is a user in the current database. Drop the user from
  15025.  the current database with the sp_dropuser procedure. When this error message
  15026.  is generated, the database name, user's login ID, and user's name in the
  15027.  database are displayed.
  15028.  
  15029.  
  15030.  Permissions
  15031.  
  15032.  Execute permission defaults to the System Administrator.
  15033.  
  15034.  
  15035.  Tables Used
  15036.  
  15037.  master.dbo.syslogins, sysusers
  15038.  
  15039.  
  15040.  See Also
  15041.  
  15042.  sp_addlogin, sp_changedbowner, sp_dropuser, sp_helpuser
  15043.  
  15044.  
  15045.  sp_droptype
  15046.  ────────────────────────────────────────────────────────────────────────────
  15047.  
  15048.  
  15049.  Function
  15050.  
  15051.  Drops a user-defined datatype by deleting the type from systypes.
  15052.  
  15053.  
  15054.  Syntax
  15055.  
  15056.    sp_droptype typename
  15057.  
  15058.  
  15059.  Examples
  15060.  
  15061.    sp_droptype birthday
  15062.  
  15063.  This example drops the user-defined datatype named birthday.
  15064.  
  15065.  
  15066.  Options
  15067.  
  15068.  typename
  15069.    The name of a user-defined datatype that you own.
  15070.  
  15071.  
  15072.  Comments
  15073.  
  15074.  Executing sp_droptype deletes a user-defined datatype from systypes.
  15075.  
  15076.  A user datatype cannot be dropped if tables or other database objects
  15077.  reference it.
  15078.  
  15079.  
  15080.  Messages
  15081.  
  15082.    Type has been dropped.
  15083.  
  15084.  The user-defined type no longer exists in the current database.
  15085.  
  15086.    The type doesn't exist or you don't own it.
  15087.  
  15088.  You do not own a user datatype with that name.
  15089.  
  15090.    Type is being used. You cannot drop it.
  15091.  
  15092.  A user datatype referenced by a table or other database object cannot be
  15093.  dropped. Drop the tables and/or procedures first.
  15094.  
  15095.  
  15096.  Permissions
  15097.  
  15098.  Execute permission defaults to the Database Owner and datatype owner.
  15099.  
  15100.  
  15101.  Tables Used
  15102.  
  15103.  syscolumns, sysobjects, systypes, sysusers
  15104.  
  15105.  
  15106.  See Also
  15107.  
  15108.  sp_addtype, sp_rename
  15109.  
  15110.  
  15111.  sp_dropuser
  15112.  ────────────────────────────────────────────────────────────────────────────
  15113.  
  15114.  
  15115.  Function
  15116.  
  15117.  Drops a user from the current database by deleting the entry from sysusers.
  15118.  
  15119.  
  15120.  
  15121.  Syntax
  15122.  
  15123.    sp_dropuser username
  15124.  
  15125.  
  15126.  Examples
  15127.  
  15128.    sp_dropuser albert
  15129.  
  15130.  This statement drops the user albert from the current database. The user
  15131.  albert may no longer use the database.
  15132.  
  15133.  
  15134.  Options
  15135.  
  15136.  username
  15137.    The user's name in the current database's sysusers table.
  15138.  
  15139.  
  15140.  Comments
  15141.  
  15142.  Executing sp_dropuser deletes a user from the current database by deleting
  15143.  the user's row from sysusers.
  15144.  
  15145.  ────────────────────────────────────────────────────────────────────────────
  15146.  WARNING
  15147.  
  15148.  Do not drop users who own database objects. Before dropping a user, first
  15149.  check to see if that user owns any database objects. If the user does own
  15150.  database objects, drop the objects or change their ownership before dropping
  15151.  the user.
  15152.  ────────────────────────────────────────────────────────────────────────────
  15153.  
  15154.  The Database Owner of a database cannot be dropped.
  15155.  
  15156.  If other users have an alias to the user being dropped, their aliases are
  15157.  dropped. They are no longer able to access the database.
  15158.  
  15159.  
  15160.  Messages
  15161.  
  15162.    User has been dropped from current database.
  15163.  
  15164.  The specified user is no longer known to the database.
  15165.  
  15166.    Dependent aliases were also dropped.
  15167.  
  15168.  Other users had an alias to the user being dropped. Their aliases have been
  15169.  dropped, and they can no longer access the database.
  15170.  
  15171.    No user exists with the supplied name.
  15172.  
  15173.  The specified user doesn't exist in the current database.
  15174.  
  15175.    You cannot drop the 'Database Owner'.
  15176.  
  15177.  The Database Owner cannot be dropped, even by the System Administrator.
  15178.  
  15179.    You cannot drop user because he or she owns objects in database.
  15180.  
  15181.  Users who own objects in the current database cannot be dropped. Drop the
  15182.  owned objects first. When this error message is generated, a list of objects
  15183.  and their owners is displayed.
  15184.  
  15185.    You cannot drop user because he or she owns types in database.
  15186.  
  15187.  Users who own user datatypes in the current database cannot be dropped. Drop
  15188.  the owned datatypes first. When this error message is generated, a list of
  15189.  datatypes and their owners is displayed.
  15190.  
  15191.  
  15192.  Permissions
  15193.  
  15194.  Execute permission defaults to the Database Owner.
  15195.  
  15196.  
  15197.  Tables Used
  15198.  
  15199.  sysalternates, sysobjects, systypes, sysusers
  15200.  
  15201.  
  15202.  See Also
  15203.  
  15204.  sp_adduser, sp_droplogin, GRANT, REVOKE, USE
  15205.  
  15206.  
  15207.  sp_foreignkey
  15208.  ────────────────────────────────────────────────────────────────────────────
  15209.  
  15210.  
  15211.  Function
  15212.  
  15213.  Defines a foreign key on a table or view.
  15214.  
  15215.  
  15216.  Syntax
  15217.  
  15218.    sp_foreignkey tabname, pktabname, col1 [, col2, col3, ...,
  15219.    col8]
  15220.  
  15221.  
  15222.  Examples
  15223.  
  15224.    A. sp_foreignkey titles, publishers, pub_id
  15225.  
  15226.  The primary key of the publishers table is the pub_id column. The titles
  15227.  table also contains a pub_id column, which is a foreign key of publishers.
  15228.  
  15229.    B. sp_foreignkey orders, parts, part, subpart
  15230.  
  15231.  The primary key of the parts table has been defined with sp_primarykey as
  15232.  the partnumber and subpartnumber columns. The orders table contains the
  15233.  columns part and subpart, which make up a foreign key of parts.
  15234.  
  15235.  
  15236.  Options
  15237.  
  15238.  tabname
  15239.    The name of the table or view that contains the foreign key.
  15240.  
  15241.  pktabname
  15242.    The name of the table or view that has the primary key to which the
  15243.    foreign key applies.
  15244.  
  15245.  col1
  15246.    The name of the first column that makes up the foreign key. The foreign
  15247.    key must consist of at least one column and can have a maximum of eight
  15248.    columns.
  15249.  
  15250.  
  15251.  Comments
  15252.  
  15253.  Executing sp_foreignkey adds the key to the syskeys table.
  15254.  
  15255.  Keys are created to make explicit a logical relationship that is implicit in
  15256.  your database design.
  15257.  
  15258.  To display a report on the keys that have been defined, execute sp_helpkey.
  15259.  
  15260.  
  15261.  You must be the owner of the table or view to define its foreign key.
  15262.  
  15263.  The primary key of pktabname table or view must already be defined.
  15264.  
  15265.  The number and order of columns that make up the foreign key must be the
  15266.  same as the number and order of columns that make up the primary key. The
  15267.  datatypes of the primary and foreign keys must agree, but the lengths and
  15268.  nulltypes need not agree.
  15269.  
  15270.  When you define a foreign key, a common key is automatically defined between
  15271.  the pktabname and tabname tables or views, and added to syskeys.
  15272.  
  15273.  Foreign keys have been assigned within the master and model databases.
  15274.  
  15275.  
  15276.  Messages
  15277.  
  15278.    Foreign key table doesn't exist.
  15279.  
  15280.  The table or view specified with the tabname parameter doesn't exist in the
  15281.  current database.
  15282.  
  15283.    Primary key table doesn't exist.
  15284.  
  15285.  The table or view specified with the pktabname parameter doesn't exist in
  15286.  the current database or doesn't have a primary key defined.
  15287.  
  15288.    Only the owner of the table may define a foreign key.
  15289.  
  15290.  You are not the owner of the table or view.
  15291.  
  15292.    The table has no such first column.
  15293.  
  15294.  The table or view specified with the tabname parameter does not have a
  15295.  column of the specified name.
  15296.  
  15297.    Primary key does not exist with the same number of columns
  15298.  
  15299.     as the foreign key.
  15300.  
  15301.  The number of columns in the foreign key of tabname must be the same as the
  15302.  number of columns in the primary key of pktabname.
  15303.  
  15304.    Datatypes of the first column in the keys are different.
  15305.  
  15306.  The datatypes of the columns of the foreign key of tabname and the primary
  15307.  key of pktabname must be the same.
  15308.  
  15309.    New foreign key and common join added.
  15310.  
  15311.  The foreign key has been defined and added to syskeys. A common key has also
  15312.  been added to syskeys.
  15313.  
  15314.  
  15315.  Permissions
  15316.  
  15317.  Execute permission defaults to the tabname owner.
  15318.  
  15319.  
  15320.  Tables Used
  15321.  
  15322.  syscolumns, syskeys, sysobjects
  15323.  
  15324.  
  15325.  See Also
  15326.  
  15327.  sp_commonkey, sp_dropkey, sp_helpjoins, sp_helpkey, sp_primarykey, CREATE
  15328.  TRIGGER
  15329.  
  15330.  
  15331.  sp_help
  15332.  ────────────────────────────────────────────────────────────────────────────
  15333.  
  15334.  
  15335.  Function
  15336.  
  15337.  Reports information about a database object (any object listed in
  15338.  sysobjects) or about an SQL-Server-supplied or user-defined datatype.
  15339.  
  15340.  
  15341.  Syntax
  15342.  
  15343.    sp_help [objname]
  15344.  
  15345.  
  15346.  Examples
  15347.  
  15348.    A. sp_help
  15349.  
  15350.  Example A displays a brief listing of each object in sysobjects, giving its
  15351.  name, owner, and object type, and of each user-defined datatype in systypes,
  15352.  giving its name, storage type, case sensitivity, length, nulltype, default
  15353.  name, and rule name. The nulltype is 0 if null values are not allowed and 1
  15354.  if null values are allowed.
  15355.  
  15356.    B. sp_help publishers
  15357.  
  15358.  Example B displays information about the publishers table:
  15359.  
  15360.    Name   Owner   Type
  15361.     -----------  ----------- - ------------
  15362.     publishers  dbo    user table
  15363.  
  15364.     (0 rows affected)
  15365.  
  15366.     Data_located_on_segment  When_created
  15367.     -----------------------  --------------------
  15368.     default       Sep 15 1987 2:03PM
  15369.  
  15370.     (0 rows affected)
  15371.  
  15372. ╓┌───────┌────────────┌────────────────┌───────┌────────────┌─────────────┌──
  15373.  ─────────────────────────────────────────────────────────────────────────────
  15374.  ─────────────────────────────────────────────────────────────────────────────
  15375.          Column_name  Type             Length  Null values                Rule
  15376.                                                             Default_name
  15377.  
  15378.          -----------  ---------------  ------  -----------                 ---
  15379.                                                             ------------
  15380.  
  15381.          pub_id       char nocase      4       0             NULL         pub_
  15382.  
  15383.          pub_name     varchar nocase   40      1             NULL         NULL
  15384.  
  15385.          city         varchar nocase   20      1             NULL         NULL
  15386.  
  15387.          state        char nocase      2       1             NULL         NULL
  15388.  
  15389.          (9 rows
  15390.          affected)
  15391.  
  15392.  
  15393.  
  15394.    index_name   index_description   index_keys
  15395.     ---------------- --------------------  -----------
  15396.     pubind    clustered, unique   pub_id
  15397.  
  15398.     (1 row affected)
  15399.  
  15400.     No defined keys for this object.
  15401.  
  15402.  
  15403.  
  15404.  
  15405.  Options
  15406.  
  15407.  objname
  15408.    The name of any object in sysobjects or any user-defined datatype in
  15409.    systypes. Database names are not acceptable.
  15410.  
  15411.  
  15412.  Comments
  15413.  
  15414.  The procedure looks for an object in the current database only.
  15415.  
  15416.  System procedures do not work on temporary tables because a procedure cannot
  15417.  access a temporary object unless it created the object itself during the
  15418.  current session.
  15419.  
  15420.  If sp_help prints "nocase" after char and varchar datatypes, the server is
  15421.  not case sensitive.
  15422.  
  15423.  
  15424.  Messages
  15425.  
  15426.    Object must be in your current database.
  15427.  
  15428.  The procedure gives information about objects in the current database only.
  15429.  You have included the database name in the objname parameter.
  15430.  
  15431.    Object does not exist in this database.
  15432.  
  15433.  The specified object does not exist in the current database.
  15434.  
  15435.    No defined keys for this object.
  15436.  
  15437.  The table or view has no defined keys.
  15438.  
  15439.  
  15440.  Tables Used
  15441.  
  15442.  master.dbo.spt_values, syscolumns, sysobjects, systypes
  15443.  
  15444.  
  15445.  Permissions
  15446.  
  15447.  Execute permission defaults to the public group.
  15448.  
  15449.  
  15450.  See Also
  15451.  
  15452.  sp_helpgroup, sp_helpindex, sp_helprotect, sp_helpuser
  15453.  
  15454.  
  15455.  sp_helpdb
  15456.  ────────────────────────────────────────────────────────────────────────────
  15457.  
  15458.  
  15459.  Function
  15460.  
  15461.  Reports information about a database or about all databases.
  15462.  
  15463.  
  15464.  Syntax
  15465.  
  15466.    sp_helpdb [dbname]
  15467.  
  15468.  
  15469.  Examples
  15470.  
  15471.    A. sp_helpdb pubs
  15472.  
  15473.  Example A displays information about the pubs database:
  15474.  
  15475.    name db_size  owner  dbid created   status
  15476.     ------ -------- -------- ------ -----------  ----------------
  15477.     pubs 2 MB  sa   4  Sep 15 1987  no options set
  15478.  
  15479.     (1 row affected)
  15480.  
  15481.     device     size   usage
  15482.     --------------------- ------------ ----------------
  15483.     master     2 MB   data and log
  15484.  
  15485.     (1 row affected)
  15486.  
  15487.    B. sp_helpdb
  15488.  
  15489.  Example B displays information about all the databases on SQL Server.
  15490.  
  15491.  
  15492.  Options
  15493.  
  15494.  dbname
  15495.    The name of the database you want information on. Omitting this optional
  15496.    parameter results in a report on all databases.
  15497.  
  15498.  
  15499.  Comments
  15500.  
  15501.  Executing sp_helpdb reports on the specified database when the dbname is
  15502.  supplied or on all databases in master.dbo.sysdatabases when no parameter is
  15503.  supplied.
  15504.  
  15505.  
  15506.  Messages
  15507.  
  15508.    No such database exists.
  15509.  
  15510.  The specified database doesn't exist on SQL Server. Run the procedure
  15511.  without the dbname parameter to see a list of all the databases.
  15512.  
  15513.  
  15514.  Permissions
  15515.  
  15516.  Execute permission defaults to the public group.
  15517.  
  15518.  
  15519.  Tables Used
  15520.  
  15521.  #spdbdesc, master.dbo.spt_values, master.dbo.sysdatabases,
  15522.  master.dbo.sysdevices, master.dbo.syslogins, master.dbo.sysusages
  15523.  
  15524.  
  15525.  See Also
  15526.  
  15527.  sp_configure, sp_dboption, sp_renamedb, ALTER DATABASE, CREATE DATABASE
  15528.  
  15529.  
  15530.  sp_helpdevice
  15531.  ────────────────────────────────────────────────────────────────────────────
  15532.  
  15533.  
  15534.  Function
  15535.  
  15536.  Reports information about SQL Server's database devices and dump devices.
  15537.  
  15538.  
  15539.  Syntax
  15540.  
  15541.    sp_helpdevice [logical_name]
  15542.  
  15543.  
  15544.  Examples
  15545.  
  15546.    A. sp_helpdevice
  15547.  
  15548.     device_name   physical_name
  15549.      description
  15550.      status   cntrltype  device_number low  high
  15551.     ---------------  -----------------------------
  15552.      ------------------------------
  15553.      -----------  ------------- ------------- ----- ------
  15554.     diskdump   nul
  15555.      disk dump device
  15556.      16    2    0    0  20000
  15557.     diskettedumpa  a:sqltable.dat
  15558.      diskette, 1.2 Mb, dump device
  15559.      16    3    0    0  19
  15560.     diskettedumpb  b:sqltable.dat
  15561.      diskette, 1.2 Mb, dump device
  15562.      16    4    0    0  19
  15563.     master    c:\sql\data\master.dat
  15564.      special, default disk, physical disk, 10 Mb
  15565.      3    0    0    0  5119
  15566.  
  15567.     (4 rows affected)
  15568.  
  15569.  Example A reports information about all database devices and dump devices on
  15570.  SQL Server.
  15571.  
  15572.    B. sp_helpdevice diskdump
  15573.  
  15574.  Example B reports information about the device named diskdump.
  15575.  
  15576.  
  15577.  Options
  15578.  
  15579.  logical_name
  15580.    The logical name of the database device or dump device you want
  15581.    information on. If you omit this parameter, information on all the
  15582.    database files and dump devices is displayed.
  15583.  
  15584.  
  15585.  Comments
  15586.  
  15587.  Executing sp_helpdevice displays information on the specified database
  15588.  device or dump device when logical_name is given or on all database devices
  15589.  and dump devices in master.dbo.sysdevices when no parameter is given.
  15590.  
  15591.  The sysdevices table contains database devices and dump devices. Database
  15592.  devices can be designated default database devices, which means that they
  15593.  can be used for database storage when a user executes the CREATE DATABASE or
  15594.  ALTER DATABASE statement and does not specify a database devicename, or
  15595.  gives the keyword DEFAULT. To make a database device a default database
  15596.  device, execute the sp_diskdefault system procedure.
  15597.  
  15598.  Database devices are added to the system with the DISK INIT statement. Dump
  15599.  devices are added to the system with the sp_addumpdevice system procedure.
  15600.  
  15601.  The status column in the report from sp_helpdevice contains a number that
  15602.  corresponds to the status description in the description column.
  15603.  
  15604.  The cntrltype column specifies the controller number of the device. For hard
  15605.  disk dump devices, the controller number is 2; for diskette dump devices,
  15606.  the controller number is 3 or 4. For database devices, it is almost always 0
  15607.  (unless your installation has a special type of disk controller).
  15608.  
  15609.  The device_number column is 0 for dump devices, 0 for the master database
  15610.  device, and between 1 and 9 for other database devices.
  15611.  
  15612.  The low and high columns represent virtual page numbers for database devices
  15613.  devices and disk dump devices.
  15614.  
  15615.  The high and low columns represent media capacity for diskette dump devices
  15616.  (high - low = number of 62K blocks).
  15617.  
  15618.  
  15619.  Messages
  15620.  
  15621.    No such i/o device exists.
  15622.  
  15623.  The database device or dump device name supplied for the logical_name
  15624.  parameter doesn't exist on SQL Server. Run the procedure without the
  15625.  logical_name parameter to see a list of all devices.
  15626.  
  15627.  
  15628.  Permissions
  15629.  
  15630.  Execute permission defaults to the public group.
  15631.  
  15632.  
  15633.  Tables Used
  15634.  
  15635.  #spdevtab, master.dbo.spt_values, master.dbo.sysdevices
  15636.  
  15637.  
  15638.  See Also
  15639.  
  15640.  sp_addumpdevice, sp_configure, sp_diskdefault, sp_dropdevice, sp_helpdb,
  15641.  sp_logdevice, sp_who, DISK INIT, DUMP DATABASE, DUMP TRANSACTION, LOAD
  15642.  DATABASE, LOAD TRANSACTION
  15643.  
  15644.  
  15645.  sp_helpgroup
  15646.  ────────────────────────────────────────────────────────────────────────────
  15647.  
  15648.  
  15649.  Function
  15650.  
  15651.  Reports information on a group or on all groups in the current database.
  15652.  
  15653.  
  15654.  Syntax
  15655.  
  15656.    sp_helpgroup [grpname]
  15657.  
  15658.  
  15659.  Examples
  15660.  
  15661.    A. sp_helpgroup hackers
  15662.  
  15663.  Example A displays information about the hackers group:
  15664.  
  15665.    Group_name   Group_id Users_in_group  Userid
  15666.     --------------  ---------- -------------  ------
  15667.     hackers    16384  ann     4
  15668.     hackers    16384  judy    3
  15669.  
  15670.     (3 rows affected)
  15671.  
  15672.    B. sp_helpgroup
  15673.  
  15674.  Example B displays information about all the groups in the current database:
  15675.  
  15676.  
  15677.  ────────────────────────────────────────────────────────────────────────────
  15678.    Group_name         Group_id
  15679.    ------------       --------
  15680.    hackers            16384
  15681.    public             0
  15682.    (2 rows affected)
  15683.  
  15684.  
  15685.  Options
  15686.  
  15687.  grpname
  15688.    The name of a group in the database that has been created with
  15689.    sp_addgroup.
  15690.  
  15691.  
  15692.  Comments
  15693.  
  15694.  Executing sp_helpgroup reports on the specified group or, if no parameter is
  15695.  supplied, on all groups in the database.
  15696.  
  15697.  
  15698.  Messages
  15699.  
  15700.    No such group exists in the current database.
  15701.  
  15702.  The specified group does not exist in the current database. Execute the
  15703.  procedure without the grpname parameter to see a list of all the groups in
  15704.  the database.
  15705.  
  15706.  
  15707.  Permissions
  15708.  
  15709.  Execute permission defaults to the public group.
  15710.  
  15711.  
  15712.  Tables Used
  15713.  
  15714.  sysusers
  15715.  
  15716.  
  15717.  See Also
  15718.  
  15719.  sp_addgroup, sp_changegroup, sp_dropgroup, sp_helprotect, sp_helpuser,
  15720.  GRANT, REVOKE
  15721.  
  15722.  
  15723.  sp_helpindex
  15724.  ────────────────────────────────────────────────────────────────────────────
  15725.  
  15726.  
  15727.  Function
  15728.  
  15729.  Reports index information on a table.
  15730.  
  15731.  
  15732.  Syntax
  15733.  
  15734.    sp_helpindex tabname
  15735.  
  15736.  
  15737.  Examples
  15738.  
  15739.    sp_helpindex sysobjects
  15740.  
  15741.  This statement reports on the types of indexes on the sysobjects table:
  15742.  
  15743.    index_name    index_description  index_keys
  15744.     -------------------- -------------------- ---------------
  15745.     sysobjects    clustered, unique  id
  15746.     ncsysobjects   nonclustered, unique name, uid
  15747.  
  15748.     (2 rows affected)
  15749.  
  15750.  
  15751.  Options
  15752.  
  15753.  tabname
  15754.    The name of a table in the current database.
  15755.  
  15756.  
  15757.  Comments
  15758.  
  15759.  Executing sp_helpindex lists up to eight indexes on a table.
  15760.  
  15761.  ────────────────────────────────────────────────────────────────────────────
  15762.  WARNING
  15763.  
  15764.  Only eight indexes are displayed when you execute sp_helpindex. The
  15765.  sysindexes system table contains information on all indexes.
  15766.  ────────────────────────────────────────────────────────────────────────────
  15767.  
  15768.  
  15769.  Messages
  15770.  
  15771.    Table does not exist.
  15772.  
  15773.  The name you gave for the tabname parameter does not exist in the current
  15774.  database.
  15775.  
  15776.    Table does not have any indexes.
  15777.  
  15778.  The table you named has no indexes.
  15779.  
  15780.    Table name must be in 'current' database.
  15781.  
  15782.  The name you gave for the tabname parameter includes a database reference.
  15783.  Name references must be local to the current database.
  15784.  
  15785.  
  15786.  Permissions
  15787.  
  15788.  Execute permission defaults to the public group.
  15789.  
  15790.  
  15791.  Tables Used
  15792.  
  15793.  #spindtab, master.dbo.spt_values, sysindexes
  15794.  
  15795.  
  15796.  See Also
  15797.  
  15798.  sp_help, sp_helpkey, CREATE INDEX, DROP INDEX, UPDATE STATISTICS
  15799.  
  15800.  
  15801.  sp_helpjoins
  15802.  ────────────────────────────────────────────────────────────────────────────
  15803.  
  15804.  
  15805.  Function
  15806.  
  15807.  Lists the columns in two tables or views that are likely to be joined.
  15808.  
  15809.  
  15810.  Syntax
  15811.  
  15812.    sp_helpjoins lefttab, righttab
  15813.  
  15814.  
  15815.  Examples
  15816.  
  15817.    sp_helpjoins sysobjects, syscolumns
  15818.  
  15819.  This statement displays a list of columns that are likely to be joined in
  15820.  the sysobjects and syscolumns tables:
  15821.  
  15822.    a1      a2      b1
  15823.     b2      c1      c2
  15824.     d1      d2      e1
  15825.     -------- -------- -------- -------- -------- -------
  15826.     -------- -------- -------- -------- -------- -------
  15827.     -------- -------- -------- -------- -------- -------
  15828.     id   id   NULL  NULL  NULL  NULL
  15829.     NULL  NULL  NULL  NULL  NULL  NULL
  15830.     NULL  NULL  NULL  NULL  NULL  NULL
  15831.  
  15832.     (1 row affected)
  15833.  
  15834.  
  15835.  Options
  15836.  
  15837.  lefttab
  15838.    The first table or view.
  15839.  
  15840.  righttab
  15841.    The second table or view. The order of the parameters does not matter.
  15842.  
  15843.  
  15844.  Comments
  15845.  
  15846.  The column pairs that sp_helpjoins displays come from two sources. First,
  15847.  sp_helpjoins checks the syskeys table in the current database to see if any
  15848.  common keys have been defined on the two tables with sp_commonkey. If it
  15849.  doesn't find any common keys there, the procedure applies less restrictive
  15850.  criteria to come up with any keys that may be reasonably joined: it checks
  15851.  for keys with the same user datatypes and if that fails, for columns with
  15852.  the same name and datatype.
  15853.  
  15854.  The sp_helpjoins procedure does not create any joins.
  15855.  
  15856.  
  15857.  Messages
  15858.  
  15859.    First table doesn't exist.
  15860.  
  15861.  The table specified as the lefttab parameter is not a table or view in the
  15862.  current database.
  15863.  
  15864.    Second table doesn't exist.
  15865.  
  15866.  The table specified as the righttab parameter is not a table or view in the
  15867.  current database.
  15868.  
  15869.  
  15870.  Permissions
  15871.  
  15872.  Execute permission defaults to the public group.
  15873.  
  15874.  
  15875.  Tables Used
  15876.  
  15877.  syscolumns, syskeys, sysobjects
  15878.  
  15879.  
  15880.  See Also
  15881.  
  15882.  sp_commonkey, sp_foreignkey, sp_help, sp_helpkey, sp_primarykey, Joins
  15883.  
  15884.  
  15885.  sp_helpkey
  15886.  ────────────────────────────────────────────────────────────────────────────
  15887.  
  15888.  
  15889.  Function
  15890.  
  15891.  Reports information on primary, foreign, and common keys.
  15892.  
  15893.  
  15894.  Syntax
  15895.  
  15896.    sp_helpkey [objname]
  15897.  
  15898.  
  15899.  Examples
  15900.  
  15901.    sp_helpkey
  15902.  
  15903.  This statement displays information on the keys that have been defined in
  15904.  the database:
  15905.  
  15906.    keytype object   related_object object_keys  related_keys
  15907.    ------- -------   -------------- ----------  -----------------
  15908.    primary authors   none   au_id, *, *  *, *, *, *, *, *
  15909.    foreign titleauthor  authors   au_id, *, *  au_id, *, *, *, *
  15910.  
  15911.    (2 rows affected)
  15912.  
  15913.  
  15914.  Options
  15915.  
  15916.  objname
  15917.    The name of a table or view in the current database. If you don't specify
  15918.    a name, the procedure reports on all keys that have been defined in the
  15919.    current database.
  15920.  
  15921.  
  15922.  Comments
  15923.  
  15924.  Executing sp_helpkey lists information about all the primary, foreign, or
  15925.  common key definitions that reference the objname table, or about all the
  15926.  keys in the database. The object_keys and related_keys columns in the report
  15927.  refer to the names of the columns that make up the key.
  15928.  
  15929.  Keys are created to make explicit a logical relationship that is implicit in
  15930.  your database design. The information can be used by an application program.
  15931.  
  15932.  
  15933.  
  15934.  Messages
  15935.  
  15936.    Table or view name must be in 'current' database.
  15937.  
  15938.  The name supplied for the objname parameter included a database reference.
  15939.  You can get key information about local tables or views only.
  15940.  
  15941.    No such table or view in the current database.
  15942.  
  15943.  The name supplied for the objname parameter is not a table or view in the
  15944.  current database.
  15945.  
  15946.  
  15947.  Permissions
  15948.  
  15949.  Execute permission defaults to the public group.
  15950.  
  15951.  
  15952.  Tables Used
  15953.  
  15954.  master.dbo.spt_values, syskeys
  15955.  
  15956.  
  15957.  See Also
  15958.  
  15959.  sp_commonkey, sp_foreignkey, sp_help, sp_primarykey, CREATE TRIGGER
  15960.  
  15961.  
  15962.  sp_helprotect
  15963.  ────────────────────────────────────────────────────────────────────────────
  15964.  
  15965.  
  15966.  Function
  15967.  
  15968.  Reports permissions by database object and, optionally, by user for that
  15969.  object.
  15970.  
  15971.  
  15972.  Syntax
  15973.  
  15974.    sp_helprotect name [, username]
  15975.  
  15976.  
  15977.  Examples
  15978.  
  15979.    A. grant select on titles to judy
  15980.     grant update on titles to judy
  15981.     revoke update on titles(price) from judy
  15982.     sp_helprotect titles
  15983.  
  15984.  After the series of GRANT and REVOKE statements in example A, executing
  15985.  sp_helprotect titles results in this display:
  15986.  
  15987.    type  action   user  column
  15988.     --------- -----------  -------  ----------
  15989.     Grant  Select   public  All
  15990.     Grant  Select   guest  All
  15991.     Grant  Update   guest  All
  15992.     Grant  Select   judy  All
  15993.     Grant  Update   judy  title_id
  15994.     Grant  Update   judy  title
  15995.     Grant  Update   judy  type
  15996.     Grant  Update   judy  pub_id
  15997.     Grant  Update   judy  advance
  15998.     Grant  Update   judy  royalty
  15999.     Grant  Update   judy  notes
  16000.     Revoke  Update   judy  price
  16001.  
  16002.     (12 rows affected)
  16003.  
  16004.    B. sp_helprotect judy
  16005.  
  16006.  Example B displays all the permissions that judy has in the database.
  16007.  
  16008.  
  16009.  Options
  16010.  
  16011.  name
  16012.    The name of a table, view, or stored procedure; or the name of a user or
  16013.    group in the current database.
  16014.  
  16015.  username
  16016.    A user's name in the current database.
  16017.  
  16018.  
  16019.  Comments
  16020.  
  16021.  Executing sp_helprotect reports permissions on a database object. If the
  16022.  username parameter is supplied, only those users' permissions on the
  16023.  database object are reported. If name is not an object and username is also
  16024.  supplied, the permissions for the user or group are listed.
  16025.  
  16026.  The procedure looks for objects and users in the current database only.
  16027.  
  16028.  
  16029.  Messages
  16030.  
  16031.    Name must be in 'current' database.
  16032.  
  16033.  The name supplied for the name parameter included a reference to a database.
  16034.  The name must be local to the database.
  16035.  
  16036.    No such user exists in the database.
  16037.  
  16038.  The name supplied for username is not a user or group in the current
  16039.  database.
  16040.  
  16041.    No such object or user exists in the database.
  16042.  
  16043.  The name supplied for the name parameter is not an object, user, or group in
  16044.  the current database.
  16045.  
  16046.  
  16047.  Permissions
  16048.  
  16049.  Execute permission defaults to the public group.
  16050.  
  16051.  
  16052.  Tables Used
  16053.  
  16054.  master.dbo.spt_values, syscolumns, sysobjects, sysprotects, sysusers
  16055.  
  16056.  
  16057.  See Also
  16058.  
  16059.  sp_help, GRANT, REVOKE
  16060.  
  16061.  
  16062.  sp_helpsql
  16063.  ────────────────────────────────────────────────────────────────────────────
  16064.  
  16065.  
  16066.  Function
  16067.  
  16068.  Provides syntax for SQL statements, system procedures, and other special
  16069.  topics.
  16070.  
  16071.  
  16072.  Syntax
  16073.  
  16074.    sp_helpsql ["topic"]
  16075.  
  16076.  
  16077.  Examples
  16078.  
  16079.    A. sp_helpsql "disk init"
  16080.  
  16081.  Example A displays a brief definition of the DISK INIT statement along with
  16082.  its syntax.
  16083.  
  16084.    B. sp_helpsql functions
  16085.  
  16086.  Example B displays information on SQL Server functions.
  16087.  
  16088.    C. sp_helpsql "cre"
  16089.  
  16090.  Example C displays a list of all statements starting with the letters "CRE"
  16091.  (such as CREATE DATABASE, CREATE DEFAULT, and so on).
  16092.  
  16093.  
  16094.  Options
  16095.  
  16096.  topic
  16097.    The topic on which you want help. Help is available for all SQL
  16098.    statements, all system procedures, and the following special topics:
  16099.    datatypes, expressions, functions, wildcard characters, mathematical
  16100.    functions, and text functions. Be sure to include quotation marks around
  16101.    "topic".
  16102.  
  16103.  
  16104.  Comments
  16105.  
  16106.  You can enter just the first few letters of a topic. If these letters are
  16107.  enough to identify the topic, then help on the topic is displayed. For
  16108.  example, to get help on LOAD DATABASE, you could execute
  16109.  
  16110.    sp_helpsql "load d"
  16111.  
  16112.  To get help on all topics starting with "LOAD," execute
  16113.  
  16114.    sp_helpsql "load"
  16115.  
  16116.  
  16117.  Messages
  16118.  
  16119.    sp_helpsql supplies help on SQL statements, system procedures,
  16120.    and
  16121.     special topics:
  16122.  
  16123.    Syntax:  sp_helpsql [topic]
  16124.  
  16125.  You executed sp_helpsql without specifying a topic. Supply a topic and
  16126.  re-execute sp_helpsql.
  16127.  
  16128.    You have entered a non-unique topic. More information can
  16129.     be obtained for the following topics:
  16130.  
  16131.  You have not entered enough letters to identify a unique topic. Supply the
  16132.  complete topic name. For example, enter "DISK INIT" rather than just "DISK".
  16133.  
  16134.  
  16135.    No help available for topic.
  16136.  
  16137.  There is no help available for the topic you entered.
  16138.  
  16139.  
  16140.  Permissions
  16141.  
  16142.  Execute permission defaults to the public group.
  16143.  
  16144.  
  16145.  sp_helptext
  16146.  ────────────────────────────────────────────────────────────────────────────
  16147.  
  16148.  
  16149.  Function
  16150.  
  16151.  Prints the text of a stored procedure, trigger, view, default, or rule.
  16152.  
  16153.  
  16154.  Syntax
  16155.  
  16156.    sp_helptext objname
  16157.  
  16158.  
  16159.  Examples
  16160.  
  16161.    A. sp_helptext ziprule
  16162.  
  16163.     ---------
  16164.       1
  16165.  
  16166.     (1 row affected)
  16167.  
  16168.     text
  16169.     -------------------------------------------------
  16170.     create rule ziprule
  16171.     as @zip like "[0-9][0-9][0-9][0-9][0-9]"
  16172.  
  16173.     (1 row affected)
  16174.  
  16175.  Example A displays the text of ziprule. Since this rule is in the pubs
  16176.  database, this system procedure must be executed from pubs.
  16177.  
  16178.    B. sp_helptext sp_helptext
  16179.  
  16180.     ---------
  16181.       3
  16182.  
  16183.     (1 row affected)
  16184.  
  16185.     text
  16186.     -------------------------------------------------------
  16187.  
  16188.     /* helptext 1.5 5/4/87 */
  16189.     create procedure sp_helptext
  16190.     @OBJNAME varchar(92)
  16191.     as
  16192.     /*
  16193.     **, Make sure the @objname is local to the current database.  */
  16194.     if @objname like "%.%.%" and
  16195.     substring(@objname,, 1,, charindex(".",, @objname) - 1) != db_name()
  16196.  
  16197.     begin
  16198.     print
  16199.  
  16200.    "Object must be in your current database."
  16201.     return
  16202.     end
  16203.  
  16204.     /*
  16205.     **, Find out how many lines of text are coming back.  */
  16206.     select count(*)
  16207.     from syscomments
  16208.     where id = object_id(@objname)
  16209.  
  16210.     /*
  16211.     **, Now get the text.  */
  16212.     select text
  16213.     from syscomments
  16214.     where id = object_id(@objname)
  16215.  
  16216.     return
  16217.     (3 rows affected)
  16218.  
  16219.  Example B displays the text of sp_helptext. Since system procedures are
  16220.  stored in master, this system procedure must be executed from master.
  16221.  
  16222.  
  16223.  Options
  16224.  
  16225.  objname
  16226.    The name of the object for which you wish to see the CREATE text. It must
  16227.    be in the current database.
  16228.  
  16229.  
  16230.  Comments
  16231.  
  16232.  Executing sp_helptext prints out the number of rows in syscomments (255
  16233.  characters long each) that the object occupies, followed by the CREATE text
  16234.  of the object.
  16235.  
  16236.  The system procedure looks for the text in the syscomments table of the
  16237.  current database only.
  16238.  
  16239.  
  16240.  Messages
  16241.  
  16242.    Object must be in your current database.
  16243.  
  16244.  The objname parameter included a database name reference. The objname must
  16245.  be in the current database.
  16246.  
  16247.  
  16248.  Permissions
  16249.  
  16250.  Execute permission defaults to the public group.
  16251.  
  16252.  
  16253.  Tables Used
  16254.  
  16255.  syscomments
  16256.  
  16257.  
  16258.  See Also
  16259.  
  16260.  sp_help, CREATE DEFAULT, CREATE PROCEDURE, CREATE RULE, CREATE TRIGGER,
  16261.  CREATE VIEW
  16262.  
  16263.  
  16264.  sp_helpuser
  16265.  ────────────────────────────────────────────────────────────────────────────
  16266.  
  16267.  
  16268.  Function
  16269.  
  16270.  Reports information on users of a database.
  16271.  
  16272.  
  16273.  Syntax
  16274.  
  16275.    sp_helpuser [username]
  16276.  
  16277.  
  16278.  Examples
  16279.  
  16280.    A. sp_helpuser
  16281.  
  16282.  Example A displays information about all users in the current database:
  16283.  
  16284.    Users_name  ID_in_db Group_name  Login_id Default_db
  16285.     -----------  -------- ------------ ---------- ------------
  16286.     ann    4   hackers   ann   master
  16287.     dbo    1   public   sa   master
  16288.     guest   2   public   NULL  NULL
  16289.     judy   3   hackers   judy  master
  16290.  
  16291.     (4 rows affected)
  16292.  
  16293.    B. sp_helpuser dbo
  16294.  
  16295.  Example B displays information about the user Database Owner:
  16296.  
  16297.    Users_name  ID_in_db Group_name  Login_id Default_db
  16298.     ----------  -------- ----------  -------- ------------
  16299.     dbo    1   public   sa   master
  16300.  
  16301.     Users aliased to user.
  16302.     Login_id
  16303.     -------------------------------
  16304.     andy
  16305.     christa
  16306.     howard
  16307.     linda
  16308.  
  16309.  
  16310.  Options
  16311.  
  16312.  username
  16313.    The user's name in the current database. If you don't specify a name, the
  16314.    system procedure reports on all users of the current database.
  16315.  
  16316.  
  16317.  Comments
  16318.  
  16319.  Executing sp_helpuser reports information on a specified user or on all
  16320.  users of the database.
  16321.  
  16322.  If the specified user is not listed in the current database's sysusers
  16323.  table, the procedure checks to see if the user has an alias to another user
  16324.  or is a groupname.
  16325.  
  16326.  
  16327.  Messages
  16328.  
  16329.    The name supplied is a group name.
  16330.  
  16331.  The name specified for the username parameter is a groupname.
  16332.  
  16333.    Users aliased to user.
  16334.  
  16335.  If the user has other users that have an alias to him or her, the names of
  16336.  the other users are listed.
  16337.  
  16338.    The name supplied is aliased to another user.
  16339.  
  16340.  The name supplied is not a user in the database, but has an alias to a user
  16341.  in the database.
  16342.  
  16343.    The name supplied is not a user, group, or aliased.
  16344.  
  16345.  The name supplied is unknown in the database as a login ID, user, or group.
  16346.  
  16347.  
  16348.  
  16349.  Permissions
  16350.  
  16351.  Execute permission defaults to the public group.
  16352.  
  16353.  
  16354.  Tables Used
  16355.  
  16356.  master.dbo.syslogins, sysalternates, sysusers
  16357.  
  16358.  
  16359.  See Also
  16360.  
  16361.  sp_adduser, sp_dropuser, sp_help, sp_helpgroup, GRANT, REVOKE, USE
  16362.  
  16363.  
  16364.  
  16365.  
  16366.  
  16367.  sp_lock
  16368.  ────────────────────────────────────────────────────────────────────────────
  16369.  
  16370.  
  16371.  Function
  16372.  
  16373.  Reports information on locks.
  16374.  
  16375.  
  16376.  Syntax
  16377.  
  16378.    sp_lock [spid1 [, spid2]]
  16379.  
  16380.  
  16381.  Examples
  16382.  
  16383.    A. sp_lock
  16384.  
  16385. ╓┌─┌──────────────────┌──────────┌──────────┌──────┌─────────────────────────╖
  16386.  ────────────────────────────────────────────────────────────────────────────
  16387.    spid               locktype   table_id   page   dbname
  16388.    -----              ---------  --------   -----  -----------
  16389.    1                  Sh_intent  16003088   0      master
  16390.    4                  Ex_extent  0          440    pubs
  16391.    4                  Ex_extent  0          504    pubs
  16392.    4                  Sh_table   112003430  0      pubs
  16393.    4                  Ex_table   240003886  0      pubs
  16394.    (5 rows affected)
  16395.  
  16396.  
  16397.  Example A displays information on all locks currently held in SQL Server.
  16398.  
  16399.    B. sp_lock 1
  16400.  
  16401.  ────────────────────────────────────────────────────────────────────────────
  16402.    spid   locktype   table_id   page   dbname
  16403.    -----  ---------  ---------  -----  ------
  16404.    1      Sh_intent  16003088   0      master
  16405.  
  16406.  Example B displays information on locks currently held on spid 1.
  16407.  
  16408.  
  16409.  Options
  16410.  
  16411.  spid1
  16412.    The SQL Server process ID number from master.dbo.sysprocesses. Execute
  16413.    sp_who to get the spid of the lock. The parameter is optional. If it is
  16414.    not supplied, information on all locks is displayed.
  16415.  
  16416.  spid2
  16417.    Another SQL Server process ID number to check for locks. The parameter is
  16418.    optional.
  16419.  
  16420.  
  16421.  Comments
  16422.  
  16423.  Executing sp_lock reports information on processes that currently hold
  16424.  locks.
  16425.  
  16426.  Use the OBJECT_ID function to get a table's name from its ID number.
  16427.  
  16428.  If the system procedure is executed without parameters, it displays a list
  16429.  of the current locks.
  16430.  
  16431.  The locktype column indicates not only whether the lock is a shared lock
  16432.  (Sh) or an exclusive lock (Ex), but also whether it is held on a table or a
  16433.  page, and whether it is an intent lock, an extent lock, or a demand lock. An
  16434.  intent lock indicates the intention to acquire a shared or exclusive lock on
  16435.  a data page. Setting an intent lock prevents another transaction from
  16436.  acquiring an exclusive lock on the table that contains that page. An extent
  16437.  lock is a lock held on a group of eight database pages while they are being
  16438.  allocated or freed.
  16439.  
  16440.  Extent locks are set while a CREATE or DROP statement is running, or while
  16441.  an INSERT statement that requires new data or index pages is running. A
  16442.  demand lock prevents any more shared locks from being set. It indicates that
  16443.  a transaction is next in line to lock a table or page. Demand locks are
  16444.  necessary because shared locks can overlap so that read transactions keep
  16445.  monopolizing a table or page, forcing a write transaction to wait
  16446.  indefinitely. After waiting on four different read transactions, a write
  16447.  transaction is given a demand lock. As soon as the existing read
  16448.  transactions finish, the write transaction is allowed to proceed. Any new
  16449.  read transactions then have to wait for the write transaction to finish.
  16450.  
  16451.  
  16452.  Permissions
  16453.  
  16454.  Execute permission defaults to the public group.
  16455.  
  16456.  
  16457.  Tables Used
  16458.  
  16459.  master.dbo.spt_values, master.dbo.syslocks
  16460.  
  16461.  
  16462.  See Also
  16463.  
  16464.  sp_who, KILL
  16465.  
  16466.  
  16467.  sp_logdevice
  16468.  ────────────────────────────────────────────────────────────────────────────
  16469.  
  16470.  
  16471.  Function
  16472.  
  16473.  Puts the syslogs system table, which contains the transaction log, on a
  16474.  separate database device.
  16475.  
  16476.  
  16477.  Syntax
  16478.  
  16479.    sp_logdevice dbname, database_device
  16480.  
  16481.  
  16482.  Examples
  16483.  
  16484.    create database products on default = 10, logs = 2
  16485.     <execute>
  16486.     sp_logdevice products, logs
  16487.     <execute>
  16488.  
  16489.  
  16490.  Options
  16491.  
  16492.  dbname
  16493.    The name of the database whose syslogs table you want to put on a specific
  16494.    logical device.
  16495.  
  16496.  database_device
  16497.    The logical name of the database device you want to put the syslogs table
  16498.    on. This database device must be listed in sysdevices (run sp_helpdevice
  16499.    for a report on the existing database devices) and have been named in the
  16500.    CREATE DATABASE statement that the database dbname was defined in.
  16501.  
  16502.  
  16503.  Comments
  16504.  
  16505.  Whenever you create a database larger than about 4 megabytes, you must
  16506.  assign it to at least two separate logical database devices with the CREATE
  16507.  DATABASE statement and then use sp_logdevice to put the database's syslogs
  16508.  table on one of these database devices. This is essential to guarantee that
  16509.  SQL Server's automatic recovery mechanism works correctly. Putting the
  16510.  transaction log on a database device different from the rest of the database
  16511.  also improves performance.
  16512.  
  16513.  A smaller database can be created on a single database device, and the
  16514.  transaction log can be stored together with the rest of the database if you
  16515.  use only the DUMP DATABASE statement and never DUMP TRANSACTION for backups.
  16516.  
  16517.  
  16518.  The size of the database device required for the transaction log varies
  16519.  according to the amount of update activity and the frequency of transaction
  16520.  log dumps. As a rule of thumb, allocate to the log device 10 percent to 25
  16521.  percent of the space you allocate to the database itself. It is best to
  16522.  start small, since space allocated to a transaction log device cannot be
  16523.  reclaimed.
  16524.  
  16525.  To increase the amount of allocated storage space on the transaction log
  16526.  database device, give the logical name of the log database device in the ON
  16527.  clause when you execute the ALTER DATABASE statement. Then execute
  16528.  sp_logdevice to cause the newly allocated space to be made available for the
  16529.  log.
  16530.  
  16531.  The database device on which you put syslogs is used only for the syslogs
  16532.  table. If you want to increase the amount of storage space allocated for the
  16533.  rest of the database, specify any database device other than the log
  16534.  database device when you execute the ALTER DATABASE statement.
  16535.  
  16536.  Use the DISK INIT statement to format a new physical database device to
  16537.  store databases and transaction logs on.
  16538.  
  16539.  If you run the DBCC CHECKALLOC statement after executing sp_logdevice, you
  16540.  will still see some pages for syslogs allocated on the database device.
  16541.  These pages are released for general database use only after they have been
  16542.  used by the transaction log and then dumped with DUMP TRANSACTION. After
  16543.  that, the transaction log will have been completely transferred to the
  16544.  database device named when you executed sp_logdevice.
  16545.  
  16546.  Only the owner of the database affected by sp_logdevice can execute it, but
  16547.  it can be executed in any database.
  16548.  
  16549.  See the SQL Server System Administrator's Guide for details.
  16550.  
  16551.  
  16552.  Messages
  16553.  
  16554.    No such database -- run sp_helpdb to list databases.
  16555.  
  16556.  No database with the supplied name exists. Run sp_helpdb to get a list of
  16557.  databases.
  16558.  
  16559.    No such device -- run sp_helpdevice to list SQL Server devices.
  16560.  
  16561.  The logical_name database device doesn't exist on SQL Server.
  16562.  
  16563.    Only the owner of the database may move the syslogs table.
  16564.  
  16565.  You aren't the owner of the dbname database.
  16566.  
  16567.    The specified device is not used by the database.
  16568.  
  16569.  The dbname database has no space allocated on the specified database device.
  16570.  
  16571.  
  16572.    Syslogs moved.
  16573.  
  16574.  The system procedure was successful, and the syslogs table is now located on
  16575.  the devname device.
  16576.  
  16577.  
  16578.  Permissions
  16579.  
  16580.  Execute permission defaults to the Database Owner of the named database.
  16581.  
  16582.  
  16583.  Tables Used
  16584.  
  16585.  master.dbo.sysdatabases, master.dbo.sysdevices, master.dbo.sysusages
  16586.  
  16587.  
  16588.  See Also
  16589.  
  16590.  sp_helpdevice, ALTER DATABASE, CREATE DATABASE, DISK INIT, DUMP DATABASE,
  16591.  DUMP TRANSACTION
  16592.  
  16593.  
  16594.  sp_monitor
  16595.  ────────────────────────────────────────────────────────────────────────────
  16596.  
  16597.  
  16598.  Function
  16599.  
  16600.  Displays statistics about SQL Server.
  16601.  
  16602.  
  16603.  Syntax
  16604.  
  16605.    sp_monitor
  16606.  
  16607.  
  16608.  Examples
  16609.  
  16610.    sp_monitor
  16611.  
  16612.  This statement reports information about how busy SQL Server has been:
  16613.  
  16614.    last_run   current_run    seconds
  16615.     ------------------ --------------------  -----------
  16616.     Sep 16 1987 4:52PM Sep 16 1987 4:53PM  55
  16617.  
  16618.     (0 rows affected)
  16619.  
  16620.     cpu_busy   io_busy    idle
  16621.     ----------------- ---------------  -------------------
  16622.     138(0)-0%   4(0)-0%    0(0)-0%
  16623.  
  16624.     (0 rows affected)
  16625.  
  16626. ╓┌─┌──────────────────┌──────────────────┌──────────────────┌────────────────╖
  16627.  ────────────────────────────────────────────────────────────────────────────
  16628.    packets_received   packets_sent       packet_errors
  16629.    -----------------  -----------------  -----------------
  16630.    720(3)             806(4)             3(0)
  16631.    (0 rows affected)
  16632.    total_read         total_write        total_errors       connections
  16633.    -----------------  -----------------  -----------------  ------------
  16634.  ────────────────────────────────────────────────────────────────────────────
  16635.   -----------------  -----------------  -----------------  ------------
  16636.    702(1)             23275(6)           0(0)               74(2)
  16637.    (0 rows affected)
  16638.  
  16639.  
  16640.  
  16641.  Options
  16642.  
  16643.  This system procedure has no options.
  16644.  
  16645.  
  16646.  Comments
  16647.  
  16648.  SQL Server keeps track of how much work it has done in a series of global
  16649.  variables. Executing sp_monitor displays the current values of these global
  16650.  variables and how much they have changed since the last time the procedure
  16651.  was run.
  16652.  
  16653.  For each column, the statistic is printed in the form number(number)-number%
  16654.  or number(number). The first number refers to the number of seconds (for
  16655.  cpu_busy, io_busy, and idle) or the total number (for the other variables)
  16656.  since SQL Server was restarted. The number in parentheses refers to the
  16657.  number of seconds or total number since the last time sp_monitor was run.
  16658.  The percentage is the percent of time since sp_monitor was last run. For
  16659.  example, if the report showed cpu_busy as 4250(215)-68%, this would mean
  16660.  that the CPU had been busy 4250 seconds since SQL Server was last started
  16661.  up, 215 seconds since sp_monitor was last run, and 68% of the total time
  16662.  since sp_monitor was last run.
  16663.  
  16664.  The following are the columns in the sp_monitor report and their meanings:
  16665.  
  16666.  last_run
  16667.    The time the sp_monitor system procedure was last run.
  16668.  
  16669.  current_run
  16670.    The time the sp_monitor system procedure is being run.
  16671.  
  16672.  seconds
  16673.    The number of elapsed seconds since the sp_monitor system procedure was
  16674.    run.
  16675.  
  16676.  cpu_busy
  16677.    The number of seconds that SQL Server's CPU was doing SQL Server work.
  16678.  
  16679.  io_busy
  16680.    The number of seconds that SQL Server has spent doing input and output
  16681.    operations.
  16682.  
  16683.  idle
  16684.    The number of seconds that SQL Server has been idle.
  16685.  
  16686.  pack_received
  16687.    The number of input packets read by SQL Server.
  16688.  
  16689.  pack_sent
  16690.    The number of output packets written by SQL Server.
  16691.  
  16692.  packet_errors
  16693.    The number of errors encountered by SQL Server while reading and writing
  16694.    packets.
  16695.  
  16696.  total_read
  16697.    The number of reads by SQL Server.
  16698.  
  16699.  total_write
  16700.    The number of writes by SQL Server.
  16701.  
  16702.  rdwr_errors
  16703.    The number of errors encountered by SQL Server while reading and writing.
  16704.  
  16705.  connections
  16706.    The number of logins or attempted logins to SQL Server.
  16707.  
  16708.  
  16709.  Permissions
  16710.  
  16711.  Execute permission defaults to the System Administrator.
  16712.  
  16713.  
  16714.  Tables Used
  16715.  
  16716.  master.dbo.spt_monitor
  16717.  
  16718.  
  16719.  See Also
  16720.  
  16721.  sp_who, Variables (Local and Global)
  16722.  
  16723.  
  16724.  sp_password
  16725.  ────────────────────────────────────────────────────────────────────────────
  16726.  
  16727.  
  16728.  Function
  16729.  
  16730.  Adds or changes a password for an SQL Server login ID.
  16731.  
  16732.  
  16733.  Syntax
  16734.  
  16735.    sp_password old, new [, login_id]
  16736.  
  16737.  
  16738.  Examples
  16739.  
  16740.    A. sp_password null, ok, victoria
  16741.  
  16742.  The System Administrator has changed victoria's password from the default
  16743.  (NULL) to ok. Notice that NULL is not enclosed in quotation marks.
  16744.  
  16745.    B. sp_password ok, mediumhot
  16746.  
  16747.  The user victoria has changed her password from ok to mediumhot.
  16748.  
  16749.  
  16750.  Options
  16751.  
  16752.  old
  16753.    The user's old password, which is listed in master.dbo.syslogins. In the
  16754.    usual installation, only the System Administrator has SELECT permission on
  16755.    this field. The default password for all users is NULL. The System
  16756.    Administrator can give NULL for this parameter no matter what the user's
  16757.    old password was. However, if the System Administrator gives any value
  16758.    besides NULL, SQL Server checks it against master.dbo.syslogins and
  16759.    rejects it if it is wrong.
  16760.  
  16761.  new
  16762.    The user's new password.
  16763.  
  16764.  login_id
  16765.    An option that only the System Administrator can use to change any user's
  16766.    password.
  16767.  
  16768.  
  16769.  Comments
  16770.  
  16771.  Any user can change his or her password with sp_password.
  16772.  
  16773.  Only the System Administrator can use the login_id parameter and change
  16774.  someone else's password.
  16775.  
  16776.  The old parameter is checked against the existing password for the user
  16777.  login_id (unless the user executing the procedure is the System
  16778.  Administrator and the old parameter is NULL) and must match for the password
  16779.  to be changed. Passwords are listed in master.dbo.syslogins.password. As
  16780.  permissions are set up on the SQL Server distribution media, only the System
  16781.  Administrator has SELECT permission on this field.
  16782.  
  16783.  To give the NULL password in response to the Password: prompt, press ENTER.
  16784.  To give the NULL password with isql and the /P option, use isql /P.
  16785.  
  16786.  
  16787.  Messages
  16788.  
  16789.    Password changed.
  16790.  
  16791.  The password was successfully changed. The new password will be required the
  16792.  next time the user logs in to SQL Server.
  16793.  
  16794.    Only System Administrator can use loginame option
  16795.     -- password not changed.
  16796.  
  16797.  Any user can change his or her own password, but only the System
  16798.  Administrator can change another user's password. You aren't the System
  16799.  Administrator.
  16800.  
  16801.    No such login -- no password changed.
  16802.  
  16803.  The name supplied for the login_id parameter does not exist on SQL Server.
  16804.  
  16805.    Old (current) password incorrect for user -- password not changed.
  16806.  
  16807.  The password supplied as the old parameter is not the current password.
  16808.  
  16809.  
  16810.  Permissions
  16811.  
  16812.  Execute permission defaults to the public group. Each member of the public
  16813.  group has permission to change only his or her own password. The System
  16814.  Administrator can change any user's password.
  16815.  
  16816.  
  16817.  Tables Used
  16818.  
  16819.  master.dbo.syslogins
  16820.  
  16821.  
  16822.  See Also
  16823.  
  16824.  sp_addlogin, sp_adduser, sp_defaultdb
  16825.  
  16826.  
  16827.  sp_primarykey
  16828.  ────────────────────────────────────────────────────────────────────────────
  16829.  
  16830.  
  16831.  Function
  16832.  
  16833.  Defines a primary key on a table or view.
  16834.  
  16835.  
  16836.  Syntax
  16837.  
  16838.    sp_primarykey tabname, col1 [, col2, col3, ..., col8]
  16839.  
  16840.  
  16841.  Examples
  16842.  
  16843.    A. sp_primarykey authors, au_id
  16844.  
  16845.  Example A makes the au_id field the primary key of authors.
  16846.  
  16847.    B. sp_primarykey employees, lastname, firstname
  16848.  
  16849.  Example B makes the combination of the fields lastname and firstname the
  16850.  primary key of the employees table.
  16851.  
  16852.  
  16853.  Options
  16854.  
  16855.  tabname
  16856.    The name of the table or view on which to define the primary key.
  16857.  
  16858.  col1
  16859.    The name of the first column that makes up the primary key. The primary
  16860.    key can consist of between one and eight columns.
  16861.  
  16862.  
  16863.  Comments
  16864.  
  16865.  Executing sp_primarykey adds the key to the syskeys table.
  16866.  
  16867.  Keys are created to make explicit a logical relationship that is implicit in
  16868.  your database design. The information can be used by an application program.
  16869.  
  16870.  
  16871.  To display a report on the keys that have been defined, execute sp_helpkey.
  16872.  
  16873.  
  16874.  Only the owner of a table or view can define its primary key.
  16875.  
  16876.  A table or view can have only one primary key.
  16877.  
  16878.  Primary keys have been assigned within the master and model databases.
  16879.  
  16880.  
  16881.  Messages
  16882.  
  16883.    No table exists with the supplied name.
  16884.  
  16885.  The table or view supplied as the tabname parameter doesn't exist in the
  16886.  current database.
  16887.  
  16888.    Only the owner of the table may define a primary key.
  16889.  
  16890.  You aren't the owner of the table or view and therefore cannot define its
  16891.  primary key.
  16892.  
  16893.    Primary key already exists on table -- drop key first.
  16894.  
  16895.  A table or view can have only have one primary key; one already exists on
  16896.  the table or view supplied as the tabname parameter.
  16897.  
  16898.    The table has no such first column.
  16899.  
  16900.  The column name supplied as one of the column names isn't a column in
  16901.  tabname.
  16902.  
  16903.    New primary key added.
  16904.  
  16905.  The primary key has been added to the syskeys table.
  16906.  
  16907.  
  16908.  Permissions
  16909.  
  16910.  Execute permission defaults to the tabname owner.
  16911.  
  16912.  
  16913.  Tables Used
  16914.  
  16915.  syscolumns, syskeys, sysobjects
  16916.  
  16917.  
  16918.  See Also
  16919.  
  16920.  sp_commonkey, sp_dropkey, sp_foreignkey, sp_helpjoins, sp_helpkey, CREATE
  16921.  TRIGGER
  16922.  
  16923.  
  16924.  sp_rename
  16925.  ────────────────────────────────────────────────────────────────────────────
  16926.  
  16927.  
  16928.  Function
  16929.  
  16930.  Changes the name of a user-created object in the current database.
  16931.  
  16932.  
  16933.  Syntax
  16934.  
  16935.    sp_rename objname, newname
  16936.  
  16937.  
  16938.  Examples
  16939.  
  16940.    A. sp_rename titles, books
  16941.  
  16942.  Example A renames the titles table to books.
  16943.  
  16944.    B. sp_rename "books.title", name
  16945.  
  16946.  Example B renames the title column in the books table to name.
  16947.  
  16948.    C. sp_rename tid, bookid
  16949.  
  16950.  Example C renames the user-defined datatype tid to bookid.
  16951.  
  16952.  
  16953.  Options
  16954.  
  16955.  objname
  16956.    The original name of the user object (table, view, column, stored
  16957.    procedure, trigger, default, or rule) or datatype. If the object to be
  16958.    renamed is a column in a table, objname must be in the form
  16959.    "table.column". You can rename an object in the current database only, and
  16960.    only if you own it. This rule holds for the Database Owner and System
  16961.    Administrator as well as for other users.
  16962.  
  16963.  newname
  16964.    The new name of the object or datatype. Names of objects and datatypes
  16965.    must follow the rules for identifiers.
  16966.  
  16967.  
  16968.  Comments
  16969.  
  16970.  Executing sp_rename changes the name of a user-created object or datatype.
  16971.  
  16972.  The names of most system datatypes and system objects cannot be changed.
  16973.  
  16974.  ────────────────────────────────────────────────────────────────────────────
  16975.  WARNING
  16976.  
  16977.  Although it is possible to rename system procedures and the model and master
  16978.  databases, doing so will cause serious system problems.
  16979.  ────────────────────────────────────────────────────────────────────────────
  16980.  
  16981.  You can change the name of an object or datatype in the current database
  16982.  only.
  16983.  
  16984.  A user can change the names only of those objects he or she owns. The
  16985.  Database Owner can change the name of any user's objects.
  16986.  
  16987.  ────────────────────────────────────────────────────────────────────────────
  16988.  WARNING
  16989.  
  16990.  Procedures, triggers, and views that depend on an object whose name has been
  16991.  changed work fine until they are recompiled. However, recompilation takes
  16992.  place for many reasons and without notification to the user. When the
  16993.  procedure, trigger, or view is recompiled by SQL Server, it will no longer
  16994.  work. The user must change its text to reflect the new name of the object on
  16995.  which it depends. The safest course is to change the definitions of any
  16996.  dependent objects when you execute sp_rename. You can find dependent objects
  16997.  with sp_depends.
  16998.  ────────────────────────────────────────────────────────────────────────────
  16999.  
  17000.  
  17001.  Messages
  17002.  
  17003.    Object must be in the current database.
  17004.  
  17005.  The name supplied for the objname parameter included a reference to a
  17006.  database. The object must be in the current database.
  17007.  
  17008.    Object name cannot be changed either because it does not
  17009.     exist in this database, or you don't own it, or it is a system name.
  17010.  
  17011.  The specified object is not an object or user-defined datatype, or you don't
  17012.  own the object.
  17013.  
  17014.    You do not own a table or column of that name in the
  17015.     current database.
  17016.  
  17017.  No column of the specified name exists in the specified table, or you don't
  17018.  own the table.
  17019.  
  17020.    Column name has been changed.
  17021.  
  17022.  The specified column name has been renamed to newname.
  17023.  
  17024.    Name of user-defined type name changed.
  17025.  
  17026.  The specified user-defined datatype has been renamed to newname.
  17027.  
  17028.    Newname already exists in sysobjects.
  17029.  
  17030.  The newname you specified is an existing name in the sysobjects table. Use a
  17031.  different newname.
  17032.  
  17033.    Object name has been changed.
  17034.  
  17035.  The specified object has been renamed to newname.
  17036.  
  17037.  
  17038.  Permissions
  17039.  
  17040.  Execute permission defaults to the object owners for their own objects.
  17041.  Execute permission defaults to the Database Owner for all objects.
  17042.  
  17043.  
  17044.  Tables Used
  17045.  
  17046.  syscolumns, sysobjects, systypes
  17047.  
  17048.  
  17049.  See Also
  17050.  
  17051.  sp_addtype, sp_depends, sp_renamedb, ALTER TABLE, CREATE DEFAULT, CREATE
  17052.  PROCEDURE, CREATE RULE, CREATE TABLE, CREATE TRIGGER, CREATE VIEW
  17053.  
  17054.  
  17055.  sp_renamedb
  17056.  ────────────────────────────────────────────────────────────────────────────
  17057.  
  17058.  
  17059.  Function
  17060.  
  17061.  Changes the name of a database.
  17062.  
  17063.  
  17064.  Syntax
  17065.  
  17066.    sp_renamedb oldname, newname
  17067.  
  17068.  
  17069.  Examples
  17070.  
  17071.    sp_renamedb accounting, financial
  17072.  
  17073.  Renames the accounting database to financial.
  17074.  
  17075.  
  17076.  Options
  17077.  
  17078.  oldname
  17079.    The original name of the database.
  17080.  
  17081.  newname
  17082.    The new name of the database. Names of objects and datatypes must follow
  17083.    the rules for identifiers.
  17084.  
  17085.  
  17086.  Comments
  17087.  
  17088.  Executing sp_renamedb changes the name of a database.
  17089.  
  17090.  Only the System Administrator can change the name of a database.
  17091.  
  17092.  ────────────────────────────────────────────────────────────────────────────
  17093.  WARNING
  17094.  
  17095.  Procedures, triggers, and views that depend on a database whose name has
  17096.  been changed work fine until they are recompiled. However, recompilation
  17097.  takes place for many reasons and without notification to the user. When the
  17098.  procedure, trigger, or view is recompiled by SQL Server, it will no longer
  17099.  work. The user must change its text to reflect the new name of the database
  17100.  on which it depends. The safest course is to change the definitions of any
  17101.  dependent objects when you execute sp_renamedb.
  17102.  ────────────────────────────────────────────────────────────────────────────
  17103.  
  17104.  
  17105.  Messages
  17106.  
  17107.    Only the System Administrator can change the name of a database.
  17108.  
  17109.  Only the System Administrator can change database names, and you aren't the
  17110.  System Administrator.
  17111.  
  17112.    No specified database currently exists.
  17113.  
  17114.  The database you specified for the oldname parameter is not currently a
  17115.  database.
  17116.  
  17117.    A database with the new name already exists.
  17118.  
  17119.  The database you specified for the newname parameter is already a database.
  17120.  Database names must be unique, so you must use another name.
  17121.  
  17122.    Database name changed.
  17123.  
  17124.  The database name was successfully changed.
  17125.  
  17126.  
  17127.  Permissions
  17128.  
  17129.  Execute permission defaults to the object owners for their own objects.
  17130.  Execute permission defaults to the Database Owner for all objects.
  17131.  
  17132.  
  17133.  Tables Used
  17134.  
  17135.  master.dbo.sysdatabases
  17136.  
  17137.  
  17138.  See Also
  17139.  
  17140.  sp_changedbowner, sp_depends, sp_helpdb, sp_rename, CREATE DATABASE
  17141.  
  17142.  
  17143.  sp_spaceused
  17144.  ────────────────────────────────────────────────────────────────────────────
  17145.  
  17146.  
  17147.  Function
  17148.  
  17149.  Displays the number of rows, the number of data pages, and the disk space
  17150.  used by an object or by each object in a database.
  17151.  
  17152.  
  17153.  Syntax
  17154.  
  17155.    sp_spaceused [objname]
  17156.  
  17157.  
  17158.  Examples
  17159.  
  17160.    A. sp_spaceused titles
  17161.  
  17162.  Example A reports on the amount of space allocated (reserved) for the titles
  17163.  table, the amount used for data, the amount used for index(es), and the
  17164.  available (unused) space:
  17165.  
  17166.    name  rows reserved data index_size unused
  17167.     ------  ---- -------- ---- ---------- -------
  17168.     titles  18  48 KB  6 KB 4 KB  38 KB
  17169.  
  17170.    B. sp_spaceused
  17171.  
  17172.  Example B prints a summary of space used in the database:
  17173.  
  17174.    database_name  database_size
  17175.      -------------  --------------------
  17176.      master    5 MB
  17177.  
  17178.      reserved   data  index_size  unused
  17179.      ---------   -------- ----------  -------
  17180.      2176 KB    1374 KB  72 KB   730 KB
  17181.  
  17182.  
  17183.  Options
  17184.  
  17185.  objname
  17186.    The name of the table or index under being examined. If the parameter is
  17187.    omitted, a summary of space used in the database is displayed.
  17188.  
  17189.  
  17190.  Comments
  17191.  
  17192.  Executing sp_spaceused computes the number of rows, the number of data
  17193.  pages, and the disk space used by the object or by each object in the
  17194.  database.
  17195.  
  17196.  The system procedure looks for an object in the current database.
  17197.  
  17198.  After you drop an index, sp_spaceused does not report accurate information.
  17199.  This is a known problem.
  17200.  
  17201.  
  17202.  Messages
  17203.  
  17204.    Object does not exist.
  17205.  
  17206.  The name supplied for the objname parameter is not an object in the current
  17207.  database.
  17208.  
  17209.  
  17210.  Permissions
  17211.  
  17212.  Execute permission defaults to the public group.
  17213.  
  17214.  
  17215.  Tables Used
  17216.  
  17217.  master..sysusages, sysindexes
  17218.  
  17219.  
  17220.  See Also
  17221.  
  17222.  sp_help, CREATE INDEX, CREATE TABLE, DROP INDEX, DROP TABLE
  17223.  
  17224.  
  17225.  sp_unbindefault
  17226.  ────────────────────────────────────────────────────────────────────────────
  17227.  
  17228.  
  17229.  Function
  17230.  
  17231.  Unbinds a default value from a column or from a user-defined datatype.
  17232.  
  17233.  
  17234.  Syntax
  17235.  
  17236.    sp_unbindefault objname [, futureonly]
  17237.  
  17238.  
  17239.  Examples
  17240.  
  17241.    A. sp_unbindefault "employees.startdate"
  17242.  
  17243.  Example A unbinds the default from the startdate column of the employees
  17244.  table.
  17245.  
  17246.    B. sp_unbindefault ssn
  17247.  
  17248.  Example B unbinds the default from the user-defined datatype named ssn and
  17249.  all columns of that type.
  17250.  
  17251.    C. sp_unbindefault ssn, futureonly
  17252.  
  17253.  If you don't want to unbind defaults from existing ssn columns, use the
  17254.  string futureonly as an optional second parameter. Now the user datatype
  17255.  named ssn no longer has a default, but existing columns of type ssn are
  17256.  unaffected.
  17257.  
  17258.  
  17259.  Options
  17260.  
  17261.  objname
  17262.    The name of the table and column or datatype from which the default is to
  17263.    be unbound. If the parameter is not of the form "table.column", then
  17264.    objname is taken to be a usertype. When unbinding a default from a user
  17265.    datatype, any columns of that type that have the same default as the user
  17266.    datatype are also unbound. Columns of that type whose default has been
  17267.    changed are unaffected.
  17268.  
  17269.  futureonly
  17270.    Prevents existing columns of the specified user datatype from losing their
  17271.    defaults.
  17272.  
  17273.  
  17274.  Comments
  17275.  
  17276.  Executing sp_unbindefault removes a default from a column or from a user
  17277.  datatype in the current database.
  17278.  
  17279.  Columns of the user-defined datatype lose their current default unless their
  17280.  default has previously been changed or the value of the optional second
  17281.  parameter is futureonly.
  17282.  
  17283.  
  17284.  Messages
  17285.  
  17286.    Default unbound from table column.
  17287.  
  17288.  The table column supplied for the objname parameter no longer has any
  17289.  default.
  17290.  
  17291.    Default unbound from datatype.
  17292.  
  17293.  The user-defined datatype supplied for the objname parameter no longer has
  17294.  any default.
  17295.  
  17296.    You do not own a table with a column of that name.
  17297.  
  17298.  The table name supplied for the objname parameter either doesn't exist in
  17299.  the database or you don't own it. You can only bind or unbind defaults from
  17300.  columns in tables that you own.
  17301.  
  17302.    You do not own a user datatype of that name.
  17303.  
  17304.  The user-defined datatype supplied for the objname parameter either doesn't
  17305.  exist in the database or you don't own it. You can only bind or unbind
  17306.  defaults from datatypes that you own.
  17307.  
  17308.    Columns of the user datatype specified had their defaults unbound.
  17309.  
  17310.  Defaults on other columns of the user datatype specified were unbound unless
  17311.  their defaults had previously been changed.
  17312.  
  17313.    Column or user datatype must be in 'current' database.
  17314.  
  17315.  The objname parameter cannot include a database reference.
  17316.  
  17317.  
  17318.  Permissions
  17319.  
  17320.  Execute permission defaults to the object owner.
  17321.  
  17322.  
  17323.  Tables Used
  17324.  
  17325.  syscolumns, sysobjects, systypes
  17326.  
  17327.  
  17328.  See Also
  17329.  
  17330.  sp_bindefault, sp_bindrule, sp_unbindrule, CREATE DEFAULT, DROP DEFAULT
  17331.  
  17332.  
  17333.  sp_unbindrule
  17334.  ────────────────────────────────────────────────────────────────────────────
  17335.  
  17336.  
  17337.  Function
  17338.  
  17339.  Unbinds a rule from a column or user-defined datatype.
  17340.  
  17341.  
  17342.  Syntax
  17343.  
  17344.    sp_unbindrule objname [, futureonly]
  17345.  
  17346.  
  17347.  Examples
  17348.  
  17349.    A. sp_unbindrule "employees.startdate"
  17350.  
  17351.  Example A unbinds the rule from the startdate column of the employees table.
  17352.  
  17353.  
  17354.    B. sp_unbindrule ssn
  17355.  
  17356.  Example B unbinds the rule from the user-defined datatype named ssn. If you
  17357.  don't want to unbind the rule from existing ssn columns, use the string
  17358.  futureonly as an optional second parameter.
  17359.  
  17360.    C. sp_unbindrule ssn, futureonly
  17361.  
  17362.  The user datatype ssn no longer has a rule, but no existing ssn columns are
  17363.  affected.
  17364.  
  17365.  
  17366.  Options
  17367.  
  17368.  objname
  17369.    The name of the table and column or user-defined datatype from which the
  17370.    rule is to be unbound. If the parameter is not of the form "table.column",
  17371.    then objname is taken to be a usertype. When unbinding a rule from a user
  17372.    datatype, any columns of that type that have the same rule as the user
  17373.    datatype are also unbound. Columns of that type whose rule has been
  17374.    changed are unaffected.
  17375.  
  17376.  futureonly
  17377.    Prevents existing columns of the specified user datatype from losing their
  17378.    rule.
  17379.  
  17380.  
  17381.  Comments
  17382.  
  17383.  Executing sp_unbindrule removes a rule from a column or user datatype in the
  17384.  current database.
  17385.  
  17386.  If unbinding a rule from a table column, the objname parameter must be of
  17387.  the form "table.column".
  17388.  
  17389.  The rule is unbound from all existing columns of the user-defined datatype
  17390.  unless their default has previously been changed or the value of the
  17391.  optional second parameter is futureonly.
  17392.  
  17393.  
  17394.  Messages
  17395.  
  17396.    Rule unbound from table column.
  17397.  
  17398.  The table column supplied for the objname parameter no longer has any rule.
  17399.  
  17400.  
  17401.    Rule unbound from datatype.
  17402.  
  17403.  The user-defined datatype supplied for the objname parameter no longer has
  17404.  any rule.
  17405.  
  17406.    You do not own a table with a column of that name.
  17407.  
  17408.  The table name supplied for the objname parameter either doesn't exist in
  17409.  the database or you don't own it. You can only bind or unbind rules on
  17410.  tables that you own.
  17411.  
  17412.    You do not own a user datatype of that name.
  17413.  
  17414.  The user-defined datatype supplied for the objname parameter either doesn't
  17415.  exist in the database or you don't own it. You can only bind or unbind rules
  17416.  from datatypes that you own.
  17417.  
  17418.    Columns of the user datatype specified had their rules unbound.
  17419.  
  17420.  Rules on other columns of the user datatype specified were unbound unless
  17421.  their rules had previously been changed.
  17422.  
  17423.    Column or user datatype must be in 'current' database.
  17424.  
  17425.  The objname parameter cannot include a database reference.
  17426.  
  17427.  
  17428.  Permissions
  17429.  
  17430.  Execute permission defaults to the object owner.
  17431.  
  17432.  
  17433.  Tables Used
  17434.  
  17435.  syscolumns, sysobjects, systypes
  17436.  
  17437.  
  17438.  See Also
  17439.  
  17440.  sp_bindefault, sp_bindrule, sp_unbindefault, CREATE RULE, DROP RULE
  17441.  
  17442.  
  17443.  sp_who
  17444.  ────────────────────────────────────────────────────────────────────────────
  17445.  
  17446.  
  17447.  Function
  17448.  
  17449.  Reports information on current SQL Server users and processes.
  17450.  
  17451.  
  17452.  Syntax
  17453.  
  17454.    sp_who [login_id | "spid"]
  17455.  
  17456.  
  17457.  Examples
  17458.  
  17459.    A. sp_who
  17460.  
  17461.  Example A reports on the processes that are running on SQL Server:
  17462.  
  17463. ╓┌───────┌──────────┌──────────┌──────────┌──────────┌───────┌──────────┌────
  17464.  ─────────────────────────────────────────────────────────────────────────────
  17465.          spid       status     login_id   hostname   blk     dbname     cmd
  17466.  
  17467.          -----      ---------  --------   --------   ---     -------    ------
  17468.  
  17469.          1          sleeping   sa         plum       0       pubs       AWAITI
  17470.  
  17471.          2          sleeping   sa                    0       master     NETWOR
  17472.  
  17473.          3          sleeping   sa                    0       master     CHECKP
  17474.  ─────────────────────────────────────────────────────────────────────────────
  17475.         3          sleeping   sa                    0       master     CHECKP
  17476.  
  17477.          4          sleeping   karenp     plum       1       pubs       SELECT
  17478.  
  17479.          5          runnable   karenp     plum       0       pubs       SELECT
  17480.  
  17481.          (5 rows
  17482.          affected)
  17483.  
  17484.  
  17485.  
  17486.  The spid column contains process identification numbers used in the
  17487.  TRANSACT-SQL KILL statement. The blk column contains the process IDs of the
  17488.  blocking process, if there is one. A blocking process (which can be infected
  17489.  or have an exclusive lock) is one that is holding resources that another
  17490.  process needs. In the previous example, process 4 (a SELECT on a table) is
  17491.  blocked by process 1 (a BEGIN TRANSACTION followed by an INSERT on the same
  17492.  table).
  17493.  
  17494.    B. sp_who victoria
  17495.  
  17496.  Example B reports on the processes the user victoria is running.
  17497.  
  17498.    C. sp_who "17"
  17499.  
  17500.  Example C reports on what SQL Server process number 17 is doing.
  17501.  
  17502.  
  17503.  Options
  17504.  
  17505.  login_id
  17506.    The user's login ID on SQL Server. If no name is specified, the procedure
  17507.    reports on all active users of SQL Server.
  17508.  
  17509.  spid
  17510.    The number of a specific process. You can supply a process number as a
  17511.    parameter if you enclose it in quotation marks (SQL Server is expecting a
  17512.    char type).
  17513.  
  17514.  
  17515.  Comments
  17516.  
  17517.  Executing sp_who reports information on a specified user, on a specified SQL
  17518.  Server process, or on all users currently running a process on SQL Server.
  17519.  
  17520.  Without parameters, the procedure reports which users are running what
  17521.  processes in all databases.
  17522.  
  17523.  The System Administrator can remove infected processes with the KILL
  17524.  statement.
  17525.  
  17526.  
  17527.  Messages
  17528.  
  17529.    No login exists with the supplied name.
  17530.  
  17531.  The name supplied for the login_id parameter does not exist on SQL Server.
  17532.  
  17533.  
  17534.  Permissions
  17535.  
  17536.  Execute permission defaults to the public group.
  17537.  
  17538.  
  17539.  Tables Used
  17540.  
  17541.  sysprocesses
  17542.  
  17543.  
  17544.  See Also
  17545.  
  17546.  sp_lock, KILL
  17547.  
  17548.  
  17549.  
  17550.  
  17551.  
  17552.  
  17553.  Chapter 3  Utility Programs
  17554.  ────────────────────────────────────────────────────────────────────────────
  17555.  
  17556.  This chapter consists of reference pages, in alphabetical order, for the SQL
  17557.  Server utility programs.
  17558.  
  17559.  Each utility program and its function is listed below:
  17560.  
  17561.  ────────────────────────────────────────────────────────────────────────────
  17562.  bcp
  17563.    Copies a database table to or from an operating system file in a
  17564.    user-specified format.
  17565.  
  17566.  bldmastr
  17567.    Builds the master database.
  17568.  
  17569.  console
  17570.    Dumps (or backs up) or loads (or restores) from a diskette drive.
  17571.  
  17572.  defncopy
  17573.    Copies definitions for specified views, rules, defaults, triggers,
  17574.    procedures, or reports from a database to an operating system file or from
  17575.    an operating system file to a database.
  17576.  
  17577.  isql
  17578.    Allows you to enter SQL statements and system procedures.
  17579.  
  17580.  sqlservr
  17581.    Starts SQL Server.
  17582.  
  17583.  ────────────────────────────────────────────────────────────────────────────
  17584.  Also, the startsql utility program is called by DB-LIBRARY; users cannot
  17585.  call it directly.
  17586.  
  17587.  
  17588.  
  17589.  
  17590.  
  17591.  bcp
  17592.  ────────────────────────────────────────────────────────────────────────────
  17593.  
  17594.  
  17595.  Function
  17596.  
  17597.  Copies a database table to or from an operating system file in a
  17598.  user-specified format.
  17599.  
  17600.  
  17601.  Syntax
  17602.  
  17603.    bcp /U login_id [[database_name.]owner.]table_name {in | out}
  17604.    datafile
  17605.     [/m maxerrors]  [/f formatfile] [/e errfile]
  17606.     [/F firstrow]  [/L lastrow] [/b batchsize]
  17607.     [/n] [/c]
  17608.     [/t field_term] [/r row_term]
  17609.     [/i inputfile] [/o outputfile]
  17610.     [/P password] [/S servername] [/v]
  17611.  
  17612.  
  17613.  Options
  17614.  
  17615.  /U login_id
  17616.    Allows the user to specify a login ID.
  17617.  
  17618.  database_name
  17619.    The database name, which is optional if the table being copied is in your
  17620.    default database. Otherwise, you must specify a database name.
  17621.  
  17622.  owner
  17623.    The owner's name, which is optional only if you own the table being
  17624.    copied. If no owner is specified and you do not own a table of that name,
  17625.    the program will not execute.
  17626.  
  17627.  table_name
  17628.    The name of the database table you want to copy.
  17629.  
  17630.  in, out
  17631.    The direction of the copy. The in option is a copy from a file into the
  17632.    database table, while the out option is a copy to a file from the database
  17633.    table.
  17634.  
  17635.  datafile
  17636.    The full pathname of an operating system file when copying a table to or
  17637.    from a hard disk file or a single diskette. The pathname can be from 1 to
  17638.    255 characters in length. To copy a table to or from multiple diskettes,
  17639.    datafile is a drive specifier only (such as a:).
  17640.  
  17641.  /m maxerrors
  17642.    The maximum number of errors before the copy is aborted. Each row that
  17643.    can't be rebuilt by bcp is thrown out and counted as one error. The
  17644.    default used if this option is not included is 10.
  17645.  
  17646.  /f formatfile
  17647.    The full pathname of a file with stored responses from a previous use of
  17648.    bcp on the same table; creation of the format file is optional. Use this
  17649.    option when you have already created a format file that you want to use
  17650.    for a copy in or out. After you answer bcp's format questions, it will ask
  17651.    you if you want to save your answers in a format file. The default
  17652.    filename is bcp.fmt. The bcp program can refer to a format file when
  17653.    copying data so that you do not have to duplicate your previous format
  17654.    responses interactively. If this option is not used, bcp queries you for
  17655.    format information interactively.
  17656.  
  17657.  /e errfile
  17658.    The full pathname of an error file where bcp stores any rows that it was
  17659.    unable to transfer from the file to the database. Error messages from the
  17660.    bcp program go to the user's workstation. If this option is not used, no
  17661.    error file is created.
  17662.  
  17663.  /F firstrow
  17664.    The number of the first row to copy (the default is the first row).
  17665.  
  17666.  /L lastrow
  17667.    The number of the last row to copy (the default is the last row).
  17668.  
  17669.  /b batchsize
  17670.    The number of rows per batch of data copied (the default copies all the
  17671.    rows in one batch).
  17672.  
  17673.  /n
  17674.    Performs the copy operation using the data's native (database) datatypes
  17675.    as the default. This option does not prompt for each field; it uses the
  17676.    default values.
  17677.  
  17678.  /c
  17679.    Performs the copy operation with character type as the default. This
  17680.    option does not prompt for each field; it uses char as the default storage
  17681.    type, no prefixes, \t as the default field separator, and \n as the
  17682.    default row terminator.
  17683.  
  17684.  /t field_term
  17685.    Specifies the default field terminator.
  17686.  
  17687.  /r row_term
  17688.    Specifies the default row terminator.
  17689.  
  17690.  /i inputfile
  17691.    Allows the user to specify the name of a file that redirects input to bcp.
  17692.  
  17693.  /o outputfile
  17694.    Allows the user to specify the name of a file that receives output
  17695.    redirected from bcp.
  17696.  
  17697.  /P password
  17698.    Allows the user to specify a password. If the /P option is not used, bcp
  17699.    prompts for a password. If the /P option is used at the end of the command
  17700.    line without any password, bcp uses the default password (NULL).
  17701.  
  17702.  /S servername
  17703.    Allows the user to specify the name of the SQL Server to connect to. The
  17704.    servername is the name of the server computer on the network. This option
  17705.    is required when you are executing bcp from a remote computer on the
  17706.    network.
  17707.  
  17708.  /v
  17709.    Reports the current version of the bcp program.
  17710.  
  17711.  For more information and examples of how to use bcp see the SQL Server
  17712.  System Administrator's Guide.
  17713.  
  17714.  
  17715.  bldmastr
  17716.  ────────────────────────────────────────────────────────────────────────────
  17717.  
  17718.  
  17719.  Function
  17720.  
  17721.  Builds the master database.
  17722.  
  17723.  
  17724.  Syntax
  17725.  
  17726.    bldmastr [ /d database_device ] [ /c cntrltype ]
  17727.     [ /s size ] [ /C ] [ /r ] [ /m ]
  17728.  
  17729.  
  17730.  Options
  17731.  
  17732.  /d database_device
  17733.    The master database device to be configured.
  17734.  
  17735.  /c cntrltype
  17736.    The controller number for the master database device. Together, cntrltype
  17737.    and disk specify the device. By default, a master database device always
  17738.    sets the cntrltype to 0.
  17739.  
  17740.  /s size
  17741.    The size of the master database device in 2K blocks. The bldmastr program
  17742.    verifies that the master database device has at least 2K blocks unless the
  17743.    /m or /r option is used. The default size is 10 megabytes (or 5120 2K
  17744.    blocks).
  17745.  
  17746.  /C
  17747.    Tells bldmastr to build a master database device that is not case
  17748.    sensitive. The default is case sensitive. See "Identifiers" in Chapter 1
  17749.    for more information.
  17750.  
  17751.  /r
  17752.    Rewrites the configuration block that contains the system startup
  17753.    parameters with the default values without disturbing anything else
  17754.    contained on the disk. This option increments the version number without
  17755.    rebuilding the master database.
  17756.  
  17757.  /m
  17758.    Rewrites the master database only without changing the configuration block
  17759.    or reinitializing the master database device. If the /r and /m options are
  17760.    both used, then both the configuration block that contains the system
  17761.    start parameters (as derived from the system tables sysconfigures and
  17762.    syscurconfigs) and the master database are rewritten without initializing
  17763.    the rest of the disk.
  17764.  
  17765.  
  17766.  Comments
  17767.  
  17768.  The bldmastr program initializes the specified database device as an SQL
  17769.  Server master database device and builds the master and model databases on
  17770.  it. The bldmastr program also provides options for recovery from
  17771.  configuration errors and other disasters.
  17772.  
  17773.  The bldmastr program prompts for options that are not provided on the
  17774.  command line. If you don't supply any options, bldmastr prompts for each
  17775.  option as follows:
  17776.  
  17777.    master database name?
  17778.     master device controller number?
  17779.     master database size?
  17780.     master database starting offset?
  17781.     configuration only? (y or n) (same as /r)
  17782.     databases only? (y or n) (same as /m)
  17783.     case insensitive database? (y or n)
  17784.  
  17785.  In most cases, it is best to specify all bldmastr options on the command
  17786.  line rather than using bldmastr prompts. If you do not specify all options
  17787.  on the command line, be aware of the following limitations:
  17788.  
  17789.  
  17790.    ■   If you use the /d option only, the bldmastr program does not prompt
  17791.        you for other options, and it uses default values for /c, /s, and /C.
  17792.  
  17793.    ■   If you omit the /s option, the default size value is used, and you are
  17794.        not prompted for size.
  17795.  
  17796.  
  17797.  
  17798.  Files
  17799.  
  17800.  The bldmastr program contains all of the data needed to build the master and
  17801.  model databases. However, it does not include stored procedures,
  17802.  permissions, and so on. These are loaded during installation.
  17803.  
  17804.  
  17805.  console
  17806.  ────────────────────────────────────────────────────────────────────────────
  17807.  
  17808.  
  17809.  Function
  17810.  
  17811.  Dumps or loads from a diskette drive. The console program is used by the
  17812.  person responsible for dumping and loading a database.
  17813.  
  17814.  ────────────────────────────────────────────────────────────────────────────
  17815.  NOTE
  17816.  The console utility program is not available under Microsoft(R) Disk
  17817.  Operating System (MS-DOS(R)).
  17818.  ────────────────────────────────────────────────────────────────────────────
  17819.  
  17820.  
  17821.  Syntax
  17822.  
  17823.    console [/S servername]
  17824.  
  17825.  The console program must be running before a DUMP DATABASE or LOAD DATABASE
  17826.  statement can proceed. See the "Comments" section.
  17827.  
  17828.  
  17829.  Options
  17830.  
  17831.  /S servername
  17832.    Allows the user to specify the name of the SQL Server to connect to. The
  17833.    servername is the name of the server computer on the network. This option
  17834.    is required when you are executing console from a remote computer on the
  17835.    network.
  17836.  
  17837.  
  17838.  Messages
  17839.  
  17840.  The following are the console's prompts when writing to a diskette (dumping
  17841.  a database or transaction log) and their explanations:
  17842.  
  17843.  
  17844.    ■   Prompt to mount diskette when writing:
  17845.  
  17846.        Mount first diskette: mount diskette on drive device_name.
  17847.                Type n to quit, press ENTER when mounted.
  17848.  
  17849.  
  17850.        device_name Physical name of the diskette dump device
  17851.  
  17852.    ■   Prompt to change diskette when writing (only when a diskette has been
  17853.        filled):
  17854.  
  17855.        Change diskette: mount diskette on
  17856.                drive device_name.
  17857.                Type n to quit, otherwise press ENTER when mounted.
  17858.  
  17859.  
  17860.        device_name Physical name of the diskette dump device
  17861.  
  17862.    ■   Prompt to remove the last diskette:
  17863.  
  17864.        Remove the diskette - press ENTER when done.
  17865.  
  17866.  
  17867.  
  17868.  The following are console's prompts when reading from diskette (loading a
  17869.  database or transaction log) and their explanations:
  17870.  
  17871.  
  17872.    ■   Prompt to mount diskette when reading:
  17873.  
  17874.        Mount first diskette: mount diskette_name volume_number on
  17875.                drive device_name.
  17876.                Type n if no diskettes are available, otherwise press ENTER
  17877.                when mounted.
  17878.  
  17879.  
  17880.    ■   Prompt to change diskette when reading:
  17881.  
  17882.        Change diskette: mount database_name volume_number
  17883.                on drive device_name.
  17884.                Type n if no diskettes are available, otherwise press
  17885.                ENTER when mounted.
  17886.  
  17887.  
  17888.        diskette_name Usually name of the dump, first 17 characters of
  17889.        database_name
  17890.  
  17891.        volume_number Diskette volume sequence number
  17892.  
  17893.        device_name Physical diskette dump device name, for example,
  17894.        a:\sqltable.dat
  17895.  
  17896.    ■   Prompt for operator override when reading:
  17897.  
  17898.        Diskette diskette_nameM volume_number mounted instead of
  17899.                diskette_name volume_number.
  17900.                Type n if you want to change diskette, otherwise
  17901.                press ENTER to use this one.
  17902.  
  17903.  
  17904.  
  17905.  The first diskette name and volume number are read from the diskette that
  17906.  was mounted. The second diskette name and volume number refer to the
  17907.  diskette that was expected to be mounted. Mounting a diskette out of
  17908.  sequence or with a different diskette name is usually an error, but an
  17909.  operator might have a good reason to override this error and use the
  17910.  diskettes in the order mounted.
  17911.  
  17912.  Mounting diskettes that were made on different days as part of the same load
  17913.  is usually an error, but an operator might have a good reason to do so.
  17914.  
  17915.  
  17916.  Comments
  17917.  
  17918.  The console program is a process that supports DUMP and LOAD for diskette
  17919.  drives. Relevant statements include DUMP DATABASE, DUMP TRANSACTION, LOAD
  17920.  DATABASE, and LOAD TRANSACTION.
  17921.  
  17922.  The console program prompts the operator for information relating to
  17923.  mounting and unmounting diskettes.
  17924.  
  17925.  The console program can be started each time SQL Server is started and left
  17926.  running in another screen group continuously.
  17927.  
  17928.  If the console program stops for any reason, the operator is prompted to
  17929.  restart it. Do not attempt to stop the console program with CONTROL+C or any
  17930.  other interrupt signal from the operating system. If SQL Server stops,
  17931.  console automatically stops.
  17932.  
  17933.  Since the console workstation (which can be any screen group) prints prompts
  17934.  and messages for the operator, you may want to run it in a window on a
  17935.  Microsoft Operating System/2 (MS(R) OS/2) workstation located close to the
  17936.  diskette unit.
  17937.  
  17938.  Most of the console program's messages are self-explanatory. To stop a dump
  17939.  or load to a diskette, type n in response to any prompt to mount a new
  17940.  diskette.
  17941.  
  17942.  If two diskettes (that is, two dumps/loads) are being used at once, only one
  17943.  console is needed. The program prints messages one at a time, waiting for a
  17944.  response before going to the next prompt. For this reason, the messages
  17945.  include the name of the physical drive to which they pertain.
  17946.  
  17947.  Make sure that the diskette you are using is formatted and not
  17948.  write-protected.
  17949.  
  17950.  The console program is not available under MS-DOS.
  17951.  
  17952.  ────────────────────────────────────────────────────────────────────────────
  17953.  WARNING
  17954.  
  17955.  If the console utility program has been started and appears to be unusable,
  17956.  wait for up to one minute and try to use it again. (SQL Server may be in the
  17957.  process of fixing a broken console connection.)
  17958.  ────────────────────────────────────────────────────────────────────────────
  17959.  
  17960.  
  17961.  defncopy
  17962.  ────────────────────────────────────────────────────────────────────────────
  17963.  
  17964.  
  17965.  Function
  17966.  
  17967.  Copies definitions for specified views, rules, defaults, triggers, or
  17968.  procedures from a database to an operating system file or from an operating
  17969.  system file to a database.
  17970.  
  17971.  ────────────────────────────────────────────────────────────────────────────
  17972.  NOTE
  17973.  
  17974.  The defncopy program cannot copy table definitions.
  17975.  ────────────────────────────────────────────────────────────────────────────
  17976.  
  17977.  
  17978.  Syntax
  17979.  
  17980.    defncopy /U login_id [ /P password ] [ /S servername]
  17981.     { in filename dbname |
  17982.     out filename dbname [owner.]objectname [[owner.]objectname]...}
  17983.  
  17984.  
  17985.  Options
  17986.  
  17987.  /U login_id
  17988.  
  17989.    Allows the user to specify a login ID.
  17990.  
  17991.  /P password
  17992.    Allows the user to specify a password. If /P has no parameter, defncopy
  17993.    uses the default password (NULL); put /P by itself at the end of the
  17994.    command line. If /P is not used, the user is prompted for a password.
  17995.  
  17996.  /S servername
  17997.    Allows the user to specify the name of SQL Server to connect to. The
  17998.    servername is the name of the server computer on the network. This option
  17999.    is required if you are executing defncopy from a remote computer on the
  18000.    network.
  18001.  
  18002.  in, out
  18003.    Options that specify the direction of definition copy.
  18004.  
  18005.  filename
  18006.    The name of the operating system file destination or source for the
  18007.    definition copy. The copy out will overwrite an existing file.
  18008.  
  18009.  dbname
  18010.    The name of the database from or to which the definitions are being
  18011.    copied.
  18012.  
  18013.  objectname
  18014.    The name(s) of the database object(s) for defncopy to copy out. This
  18015.    parameter is not used when definitions are copied in.
  18016.  
  18017.  
  18018.  Comments
  18019.  
  18020.  The defncopy program is invoked directly from the operating system. It
  18021.  provides a noninteractive way of copying out definitions (CREATE statements)
  18022.  for views, rules, defaults, triggers, or procedures from a database to an
  18023.  operating system file. Alternatively, it will copy in all the definitions in
  18024.  a specified file.
  18025.  
  18026.  The infile or outfile name and the database name are required and must be
  18027.  unambiguous. For copying out, use filenames that reflect both the object's
  18028.  name and its owner.
  18029.  
  18030.  The defncopy program cannot be used to copy table definitions.
  18031.  
  18032.  You can copy out objects' definitions even if you do not have SELECT
  18033.  permission on these objects. (However, you do need SELECT permission on the
  18034.  sysobjects and syscomments tables for the relevant database.)
  18035.  
  18036.  You must have the appropriate CREATE permission for the type of object you
  18037.  are copying in. Objects copied in belong to the copier. A System
  18038.  Administrator copying in definitions on behalf of a user should be logged in
  18039.  as that user for that user to have proper access to the reconstructed
  18040.  database objects.
  18041.  
  18042.  A comment with more than 125 characters placed before a CREATE statement can
  18043.  cause defncopy to fail.
  18044.  
  18045.  
  18046.  isql
  18047.  ────────────────────────────────────────────────────────────────────────────
  18048.  
  18049.  
  18050.  Function
  18051.  
  18052.  Allows you to enter SQL statements and system procedures.
  18053.  
  18054.  
  18055.  Syntax
  18056.  
  18057.    isql /U login_id [/e] [/p] [/i] [/n]
  18058.     [ /c cmdend] [ /h headers] [ /w columnwidth] [ /s colseparator]
  18059.     [ /t timeout] [ /m errorlevel]
  18060.     [ /H wksta_name] [ /P password]
  18061.     [/S servername] [/i inputfile] [/o outputfile]
  18062.  
  18063.  When using isql, you can use the following commands:
  18064.  
  18065.  go
  18066.  
  18067.    Terminates a command.
  18068.  
  18069.  reset
  18070.    Clears any statements you have entered.
  18071.  
  18072.  ed
  18073.    Calls the editor.
  18074.  
  18075.  !! command
  18076.    Executes an operating system command.
  18077.  
  18078.  quit, exit, or CONTROL+C
  18079.    Exits from isql.
  18080.  
  18081.  
  18082.  Options
  18083.  
  18084.  /U login_id
  18085.  
  18086.    Allows the user to specify a login ID. Login IDs are case sensitive.
  18087.  
  18088.  /e
  18089.    Echos input.
  18090.  
  18091.  /p
  18092.    Prints out performance statistics.
  18093.  
  18094.  /n
  18095.    Removes numbering and the prompt symbol (>) from input lines.
  18096.  
  18097.  /c cmdend
  18098.    Allows the user to reset the command terminator. By default, commands are
  18099.    terminated and sent to SQL Server by entering go on a line by itself. When
  18100.    you reset the command terminator, do not use SQL reserved words or
  18101.    characters that have special meaning to the operating system, whether
  18102.    preceded by a backslash or not.
  18103.  
  18104.  /h headers
  18105.    Allows the user to specify how many rows to print between column headings.
  18106.    The default is to print headings only once for each set of query results.
  18107.  
  18108.  /w columnwidth
  18109.    Allows the user to set the screen width for output. The default is 80
  18110.    characters. When an output line has reached its maximum screen width, it
  18111.    is broken into multiple lines.
  18112.  
  18113.  /s colseparator
  18114.    Allows the user to reset the column separator character, which is blank by
  18115.    default. To use characters that have special meaning to the operating
  18116.    system (for example, |, ;, &, <, >), precede them with a backslash (\).
  18117.  
  18118.  /t timeout
  18119.    Allows the user to specify the number of seconds before a command times
  18120.    out. If no timeout is specified, a command runs indefinitely; however, the
  18121.    default timeout for logging in to isql is 60 seconds.
  18122.  
  18123.  /m errorlevel
  18124.    Allows the user to customize the display of error messages. The message
  18125.    number, state, and error level are displayed for errors of the specified
  18126.    severity level or higher. Nothing is displayed for errors of levels lower
  18127.    than the specified level.
  18128.  
  18129.  /H wksta_name
  18130.    Allows the user to specify a workstation name, changing the value in the
  18131.    dynamic system table sysprocesses, if he or she logs in from a different
  18132.    computer than usual. If no workstation name is specified, the current
  18133.    computer name is assumed.
  18134.  
  18135.  /P password
  18136.    Allows the user to specify a password. If the /P option is not used, isql
  18137.    prompts for a password. If the /P option is used at the end of the command
  18138.    line without any password, isql uses the default password (NULL).
  18139.    Passwords are case sensitive.
  18140.  
  18141.  /S servername
  18142.    Allows the user to specify the name of the SQL Server to connect to. The
  18143.    servername is the name of the server computer on the network. This option
  18144.    is required if you are executing isql from a remote computer on the
  18145.    network.
  18146.  
  18147.  /i inputfile
  18148.    Allows the user to specify the name of the file that contains a batch of
  18149.    SQL statements or stored procedures. The less-than symbol (<) can be used
  18150.    in place of /I.
  18151.  
  18152.  /o outputfile
  18153.    Allows the user to specify the name of the file that receives output from
  18154.    isql. The greater-than symbol (>) can be used in place of /o.
  18155.  
  18156.  
  18157.  Examples
  18158.  
  18159.    A. isql /p /s#
  18160.  
  18161.    B. isql /U joe
  18162.  
  18163.     Password:
  18164.  
  18165.     1>select *
  18166.     2>from authors
  18167.     3>where city = "Oakland"
  18168.     4>go
  18169.  
  18170.  Examples A and B execute the command.
  18171.  
  18172.    C. isql /U joe /P abracadabra
  18173.  
  18174.     1>select *
  18175.     2>from authors
  18176.     3>where city = "Oakland"
  18177.     4>ed
  18178.  
  18179.  Example C puts you in a text file where you can edit the query. When you
  18180.  save the file, you are returned to isql. The query is displayed. Type go to
  18181.  execute it.
  18182.  
  18183.    D. isql /U alma /P
  18184.  
  18185.     1>select *
  18186.     2>from authors
  18187.     3>where city = "Oakland"
  18188.     4>reset
  18189.     1>quit
  18190.     %
  18191.  
  18192.  In example D, the reset command clears any statements you have entered. The
  18193.  quit command returns you to the operating system.
  18194.  
  18195.  
  18196.  Comments
  18197.  
  18198.  The isql program is started directly from the operating system. It accepts
  18199.  SQL commands and sends them to SQL Server. The results are formatted and
  18200.  printed on standard output. The isql program exits with quit or exit.
  18201.  
  18202.  Terminate a command by typing a line beginning with the command terminator.
  18203.  The default command terminator is go. You can follow the command terminator
  18204.  with an integer to specify how many times the command should be run. For
  18205.  example, to cause this command to be executed 100 times, type
  18206.  
  18207.    select x = 1
  18208.     go 100
  18209.  
  18210.  The results are printed once, at the end of execution.
  18211.  
  18212.  The user can call an editor on the current query buffer by entering ed as
  18213.  the first word on a line. Operating system commands can also be executed by
  18214.  starting a line with two exclamation points (!!) followed by the command.
  18215.  The editor is defined in the EDITOR environment variable. The default is
  18216.  "edlin".
  18217.  
  18218.  The existing query buffer can be cleared by typing reset on a line by
  18219.  itself. It causes any pending input to be discarded.
  18220.  
  18221.  The command terminator (go by default), reset, ed, !!, exit, quit, and
  18222.  CONTROL+C are recognized only if they appear at the very beginning of a
  18223.  line, immediately following the isql prompt. Anything entered on that line
  18224.  after these keywords is disregarded by isql.
  18225.  
  18226.  To use isql interactively, give the isql command (and any of the options) at
  18227.  your operating system prompt.
  18228.  
  18229.  You can read in a file containing a query for execution by isql by typing a
  18230.  command similar to the one shown below:
  18231.  
  18232.    isql /U alma /P /i stores.qry
  18233.  
  18234.  The file must include a command terminator. The results are displayed on the
  18235.  user's workstation.
  18236.  
  18237.  You can read in a file containing a query and direct the results to another
  18238.  file by typing a command similar to the one shown below:
  18239.  
  18240.    isql /U alma /P /i titles.qry /o titles.res
  18241.  
  18242.  Case is significant for isql options.
  18243.  
  18244.  When using isql interactively, you can read an operating system file into
  18245.  the command buffer with :r filename. Do not include a command terminator in
  18246.  the file; enter the terminator interactively once you have finished editing.
  18247.  
  18248.  
  18249.  You can include comments in a TRANSACT-SQL statement submitted to SQL Server
  18250.  by isql. Open a comment with "/*". Close it with "*/". Comments can be
  18251.  nested.
  18252.  
  18253.  For example:
  18254.  
  18255.    select au_lname, au_fname
  18256.     /*retrieve authors' last and first names*/
  18257.     from authors, titles, titleauthor
  18258.     where authors.au_id = titleauthor.au_id
  18259.     and titles.title_id = titleauthor.title_id
  18260.     /*this is a three-way join that links authors
  18261.     **to the books they have written.*/
  18262.  
  18263.  ────────────────────────────────────────────────────────────────────────────
  18264.  NOTE
  18265.  
  18266.  Do not include a go command within a comment. A go command within a comment
  18267.  produces an error message.
  18268.  ────────────────────────────────────────────────────────────────────────────
  18269.  
  18270.  
  18271.  sqlservr
  18272.  ────────────────────────────────────────────────────────────────────────────
  18273.  
  18274.  
  18275.  Function
  18276.  
  18277.  Starts SQL Server.
  18278.  
  18279.  
  18280.  Syntax
  18281.  
  18282.    sqlservr /dmaster_phys_name [/eerror_log_phys_name] [/m]
  18283.  
  18284.  
  18285.  Options
  18286.  
  18287.  /dmaster_phys_name
  18288.    The full pathname of the master database device.
  18289.  
  18290.  /eerror_log_phys_name
  18291.    The full pathname of the error log file for SQL Server system-level error
  18292.    messages.
  18293.  
  18294.  /m
  18295.    Starts SQL Server in single user mode.
  18296.  
  18297.  
  18298.  Comments
  18299.  
  18300.  Note that the names of the master database device and the error log file
  18301.  cannot be preceded with a blank.
  18302.  
  18303.  This program should only be used to start SQL Server in single-user mode.
  18304.  
  18305.  
  18306.  
  18307.  
  18308.  
  18309.  
  18310.  Appendix A  System Tables
  18311.  ────────────────────────────────────────────────────────────────────────────
  18312.  
  18313.  System tables define the structure of the database. All system tables are
  18314.  found in the master database. Some system tables are found in all databases;
  18315.  they are automatically created when the CREATE DATABASE statement is
  18316.  executed.
  18317.  
  18318.  The following system tables exist in all databases:
  18319.  
  18320.  ────────────────────────────────────────────────────────────────────────────
  18321.  sysalternates
  18322.    One row for each SQL Server user mapped to a database user
  18323.  
  18324.  syscolumns
  18325.    One row for each column in a table or view, and for each parameter in a
  18326.    stored procedure
  18327.  
  18328.  syscomments
  18329.    One or more rows for each view, rule, default, trigger, and stored
  18330.    procedure, giving an SQL definition statement
  18331.  
  18332.  sysdepends
  18333.    One row for each procedure, view, or table that is referenced by a
  18334.    procedure, view, or trigger
  18335.  
  18336.  sysindexes
  18337.    One row for each clustered index, nonclustered index, and table with no
  18338.    indexes
  18339.  
  18340.  syskeys
  18341.    One row for each foreign or primary key; set by user (not maintained by
  18342.    SQL Server)
  18343.  
  18344.  syslogs
  18345.    The transaction log
  18346.  
  18347.  sysobjects
  18348.    One row for each table, view, stored procedure, rule, trigger default,
  18349.    log, and (in tempdb only) temporary object
  18350.  
  18351.  sysprocedures
  18352.    One row for each view, rule, default, trigger, and stored procedure,
  18353.    giving an internal definition
  18354.  
  18355.  sysprotects
  18356.    User permissions information
  18357.  
  18358.  syssegments
  18359.    One row for each segment (named collection of disk pieces)
  18360.  
  18361.  systypes
  18362.    One row for each system-supplied and user-defined datatype
  18363.  
  18364.  sysusers
  18365.    One row for each user allowed in the database
  18366.  
  18367.  ────────────────────────────────────────────────────────────────────────────
  18368.  The following system tables exist in the master database only:
  18369.  
  18370.  ────────────────────────────────────────────────────────────────────────────
  18371.  sysconfigures
  18372.    One row for each user-settable configuration option
  18373.  
  18374.  syscurconfigs
  18375.    Information about configuration options currently being used by SQL Server
  18376.  
  18377.  sysdatabases
  18378.    One row for each database on SQL Server
  18379.  
  18380.  sysdevices
  18381.    One row for each disk dump device, disk for database device, and disk
  18382.    partition for databases
  18383.  
  18384.  syslocks
  18385.    Information about active locks
  18386.  
  18387.  syslogins
  18388.    One row for each valid SQL Server user account
  18389.  
  18390.  sysmessages
  18391.    One row for each system error or warning
  18392.  
  18393.  sysprocesses
  18394.    Information about server processes
  18395.  
  18396.  sysusages
  18397.    One row for each disk piece allocated to a database
  18398.  
  18399.  ────────────────────────────────────────────────────────────────────────────
  18400.  In the pages that follow, each system table is described in more detail.
  18401.  Each table's columns are listed and described, and their datatypes are
  18402.  given. Finally, each table's indexes and the system procedures that
  18403.  reference it are listed.
  18404.  
  18405.  The word reserved in the column description means that the column is not
  18406.  currently used by SQL Server.
  18407.  
  18408.  Permissions for use of the system tables can be controlled by the Database
  18409.  Owner, just like permissions on any other tables.
  18410.  
  18411.  The SQL Server setup program sets up permissions so that all users can read
  18412.  the system tables, except for a few fields, like syslogins.passwd.
  18413.  
  18414.  All direct updates on system tables are disallowed by default, however, even
  18415.  for the Database Owner, because SQL Server has supplied system procedures to
  18416.  make any updates and additions to system tables that are normally needed.
  18417.  
  18418.  You can change the default and allow direct updates to the system tables if
  18419.  for some reason it becomes necessary to modify them in a way that has not
  18420.  been anticipated by SQL Server (and that therefore cannot be accomplished
  18421.  with a system procedure). This is accomplished by resetting the
  18422.  configuration option called allow updates with the sp_configure system
  18423.  procedure and the RECONFIGURE statement. For information on sp_configure,
  18424.  see the SQL Server Language Reference.
  18425.  
  18426.  ────────────────────────────────────────────────────────────────────────────
  18427.  WARNING
  18428.  
  18429.  Some entries in some master database tables should not be altered by any
  18430.  user under any circumstances. For example, do not access syslogs in any way.
  18431.  Doing so may make it impossible for SQL Server to recover correctly in case
  18432.  of a system failure. In addition, an attempt to delete all rows from syslogs
  18433.  puts SQL Server into an infinite loop that eventually fills up the entire
  18434.  database.
  18435.  ────────────────────────────────────────────────────────────────────────────
  18436.  
  18437.  
  18438.  sysalternates (all databases)
  18439.  
  18440.  
  18441.  Description
  18442.  
  18443.  Contains one row for each SQL Server user mapped (or with an alias) to a
  18444.  user of the current database. When a user tries to access a database, SQL
  18445.  Server looks for a valid uid entry in sysusers. If none is found, it looks
  18446.  in sysalternates.suid. If the user's suid is found there, he or she is
  18447.  treated as the database user whose suid is listed in sysalternates.altsuid.
  18448.  
  18449.  
  18450.  When SQL Server is installed, there are no entries in sysalternates.
  18451.  
  18452.  Column   Datatype  Description
  18453.  ────────────────────────────────────────────────────────────────────────────
  18454.  suid     smallint  Server user ID of user being mapped
  18455.  altsuid  smallint  Server user ID of user to whom another user is mapped
  18456.  ────────────────────────────────────────────────────────────────────────────
  18457.  
  18458.  
  18459.  Indexes
  18460.  
  18461.  Unique clustered index on suid
  18462.  
  18463.  
  18464.  Referenced by System Procedures
  18465.  
  18466.  sp_addalias, sp_adduser, sp_changedbowner, sp_dropalias, sp_dropuser,
  18467.  sp_helpuser
  18468.  
  18469.  
  18470.  syscolumns (all databases)
  18471.  
  18472.  
  18473.  Description
  18474.  
  18475.  Contains one row for every column in every table and view, and a row for
  18476.  each parameter in a stored procedure.
  18477.  
  18478. ╓┌─────────┌─────────────┌───────────────────────────────────────────────────╖
  18479.  Column    Datatype      Description
  18480.  ────────────────────────────────────────────────────────────────────────────
  18481.  id        int           ID of the table to which this column belongs or of
  18482.  Column    Datatype      Description
  18483.  ────────────────────────────────────────────────────────────────────────────
  18484.  id        int           ID of the table to which this column belongs or of
  18485.                          the stored procedure with which this parameter is
  18486.                          associated
  18487.  
  18488.  number    smallint      Subprocedure number when the procedure is grouped
  18489.                          (0 for nonprocedure entries)
  18490.  
  18491.  colid     tinyint       Column ID
  18492.  
  18493.  status    tinyint       Indicates the unique position for bit columns and
  18494.                          whether null values are legal in this column
  18495.  
  18496.  type      tinyint       Physical storage type; copied from systypes
  18497.  
  18498.  length    tinyint       Physical length of data; copied from systypes or
  18499.                          supplied by user
  18500.  
  18501.  offset    smallint      Offset into the row where this column appears; if
  18502.                          negative, variable-length column
  18503.  Column    Datatype      Description
  18504.  ────────────────────────────────────────────────────────────────────────────
  18505.                         negative, variable-length column
  18506.  
  18507.  usertype  smallint      User type ID; copied from systypes
  18508.  
  18509.  cdefault  int           ID of the stored procedure that generates default
  18510.                          value for this column
  18511.  
  18512.  domain    int           ID of the stored procedure that contains the rule
  18513.                          for this column
  18514.  
  18515.  name      sysname       Column name
  18516.  
  18517.  printfmt  varchar(255)  reserved
  18518.  
  18519.  ────────────────────────────────────────────────────────────────────────────
  18520.  
  18521.  
  18522.  
  18523.  
  18524.  Indexes
  18525.  
  18526.  Unique clustered index on id, number, and colid
  18527.  
  18528.  
  18529.  Referenced by System Procedures
  18530.  
  18531.  sp_bindefault, sp_bindrule, sp_commonkey, sp_droptype, sp_foreignkey,
  18532.  sp_help, sp_helpjoins, sp_helprotect, sp_primarykey, sp_rename,
  18533.  sp_unbindefault, sp_unbindrule
  18534.  
  18535.  
  18536.  syscomments (all databases)
  18537.  
  18538.  
  18539.  Description
  18540.  
  18541.  Contains entries for each view, rule, default, trigger, and stored
  18542.  procedure. The text field contains the original SQL definition statements.
  18543.  Since text is often longer than 255 characters, entries often span rows.
  18544.  Each object can occupy up to 255 rows.
  18545.  
  18546. ╓┌─────────┌─────────────┌───────────────────────────────────────────────────╖
  18547.  Column    Datatype      Description
  18548.  ────────────────────────────────────────────────────────────────────────────
  18549.  id        int           Object ID to which this text applies
  18550.  
  18551.  number    smallint      Grouped (0 for nonprocedure entries)
  18552.  
  18553.  colid     tinyint       Column ID if this entry is a column; 0 otherwise
  18554.  
  18555.  texttype  smallint      0 System-supplied comment (for views, rules,
  18556.                          defaults, triggers, and stored procedures)
  18557.                          1 User-supplied comment (users can add entries
  18558.                          that describe an object or column)
  18559.  
  18560.  language  smallint      reserved
  18561.  
  18562.  text      varchar(255)  Actual text of SQL definition statement
  18563.  
  18564.  ────────────────────────────────────────────────────────────────────────────
  18565.  
  18566.  Column    Datatype      Description
  18567.  ────────────────────────────────────────────────────────────────────────────
  18568. 
  18569.  
  18570.  
  18571.  
  18572.  Indexes
  18573.  
  18574.  Unique clustered index on id, number, colid, and texttype
  18575.  
  18576.  
  18577.  Referenced by System Procedures
  18578.  
  18579.  sp_helptext
  18580.  
  18581.  
  18582.  sysconfigures (master database only)
  18583.  
  18584.  
  18585.  Description
  18586.  
  18587.  Contains one row for each user-settable configuration option. It contains
  18588.  the configuration options that were defined before the latest SQL Server
  18589.  startup plus any dynamic configuration options that were set since the
  18590.  latest SQL Server startup.
  18591.  
  18592.  The contents of sysconfigures is as follows:
  18593.  
  18594. ╓┌───────┌───────────┌───────┌────────────────────────────┌──────────────────╖
  18595.  ────────────────────────────────────────────────────────────────────────────
  18596.          config      value   comment                      status
  18597.  
  18598.          101         5       Maximum recovery interval    1
  18599.                              in minutes
  18600.  
  18601.          102         0       Allow updates to system      1
  18602.                              tables
  18603.  
  18604.          103         10      Number of user connections   0
  18605.                              allowed
  18606.  
  18607.          104         1518    Size of available physical   0
  18608.  ────────────────────────────────────────────────────────────────────────────
  18609.         104         1518    Size of available physical   0
  18610.                              memory in 2k pages
  18611.  
  18612.          105         10      Number of open databases     0
  18613.                              allowed among all users
  18614.  
  18615.          106         5000    Number of locks for all      0
  18616.                              users
  18617.  
  18618.          107         500     Number of open database      0
  18619.                              objects
  18620.  
  18621.          108         20      Percent of remaining memory  0
  18622.                              used for procedure cache
  18623.  
  18624.          109         0       Default fill factor          0
  18625.                              percentage
  18626.  
  18627.          110         100     Average time slice per       0
  18628.                              process in milliseconds
  18629.  ────────────────────────────────────────────────────────────────────────────
  18630.                             process in milliseconds
  18631.  
  18632.          111         2       Default database size in     0
  18633.                              megabytes
  18634.  
  18635.          112         0       Media retention period in    0
  18636.                              days
  18637.  
  18638.          113         0       Recovery flags               0
  18639.  
  18640.          114         0       Serial Number                0
  18641.  
  18642.  
  18643.          (14 rows
  18644.          affected)
  18645.  
  18646.  
  18647.  
  18648.  Column   Datatype      Description
  18649.  ────────────────────────────────────────────────────────────────────────────
  18650.  config   smallint      Configuration variable number
  18651.  
  18652.  value    int           User-modifiable value for the variable (being used
  18653.                         by SQL Server only if RECONFIGURE has been executed)
  18654.  
  18655.  comment  varchar(255)  Explanation of the configuration option
  18656.  
  18657.  status   varchar(2)    1 dynamic, the variable takes effect when
  18658.                         RECONFIGURE is executed
  18659.                         0 The variable takes effect when SQL Server is
  18660.                         restarted
  18661.  
  18662.  ────────────────────────────────────────────────────────────────────────────
  18663.  
  18664.  
  18665.  ────────────────────────────────────────────────────────────────────────────
  18666.  NOTE
  18667.  
  18668.  The values shown above are created when you install SQL Server with the
  18669.  setup program. If you install SQL Server with the bldmastr utility program,
  18670.  the values column will be different. bldmastr creates a 0 in the values
  18671.  column to indicate that the default value is in effect.
  18672.  ────────────────────────────────────────────────────────────────────────────
  18673.  
  18674.  
  18675.  Indexes
  18676.  
  18677.  Unique clustered index on config
  18678.  
  18679.  
  18680.  Referenced by System Procedures
  18681.  
  18682.  sp_configure
  18683.  
  18684.  
  18685.  syscurconfigs (master database only)
  18686.  
  18687.  
  18688.  Description
  18689.  
  18690.  Is built dynamically when queried. It contains an entry for each of the
  18691.  configuration options, as does sysconfigures, but with the current values.
  18692.  In addition, it contains four entries that describe the configuration
  18693.  structure.
  18694.  
  18695.  The contents of syscurconfigs is as follows:
  18696.  
  18697. ╓┌───────┌───────────┌───────┌────────────────────────────┌──────────────────╖
  18698.  ────────────────────────────────────────────────────────────────────────────
  18699.          config      value   comment                      status
  18700.  
  18701.          1           1       Major revision number of     0
  18702.                              config data
  18703.  
  18704.          2           0       Minor revision number of     0
  18705.                              config data
  18706.  
  18707.          3           1       Reconfigure revision number  0
  18708.                              of config data
  18709.  
  18710.          4           2       Configuration boot source    0
  18711.  
  18712.          101         5       Maximum recovery interval
  18713.  ────────────────────────────────────────────────────────────────────────────
  18714.         101         5       Maximum recovery interval
  18715.                              in minutes 1
  18716.  
  18717.          102         0       Allow updates to system      1
  18718.                              tables
  18719.  
  18720.          103         10      Number of user connections   0
  18721.                              allowed
  18722.  
  18723.          104         1518    Size of available physical   0
  18724.                              memory in 2k pages
  18725.  
  18726.          105         10      Number of open databases     0
  18727.                              allowed among all users
  18728.  
  18729.          106         5000    Number of locks for all      0
  18730.                              users
  18731.  
  18732.          107         500     Number of open database      0
  18733.                              objects
  18734.  ────────────────────────────────────────────────────────────────────────────
  18735.                             objects
  18736.  
  18737.          108         20      Percent of remaining memory  0
  18738.                              used for procedure cache
  18739.  
  18740.          109         0       Default fill factor          0
  18741.                              percentage
  18742.  
  18743.          110         100     Average time slice per       0
  18744.                              process in milliseconds
  18745.  
  18746.          111         2       Default database size in     0
  18747.                              megabytes
  18748.  
  18749.          112         0       Media retention period in    0
  18750.                              days
  18751.  
  18752.          113         0       Recovery flags               0
  18753.  
  18754.          114         666666  Serial Number                0
  18755.  ────────────────────────────────────────────────────────────────────────────
  18756.         114         666666  Serial Number                0
  18757.  
  18758.  
  18759.          (18 rows
  18760.          affected)
  18761.  
  18762.  
  18763.  
  18764.  
  18765.  Referenced by System Procedures
  18766.  
  18767.  sp_configure
  18768.  
  18769.  
  18770.  sysdatabases (master database only)
  18771.  
  18772.  
  18773.  Description
  18774.  
  18775.  Contains one row for each database on SQL Server. When SQL Server is
  18776.  installed, sysdatabases contains entries for the master database, the model
  18777.  database, and the tempdb temporary database.
  18778.  
  18779. ╓┌────────┌─────────┌────────────────────────────────────────────────────────╖
  18780.  Column   Datatype  Description
  18781.  ────────────────────────────────────────────────────────────────────────────
  18782.  name     sysname   Name of the database
  18783.  
  18784.  dbid     smallint  Database ID
  18785.  
  18786.  suid     smallint  Server user ID of database creator
  18787.  
  18788.  mode     smallint  Used internally for locking a database while it is
  18789.                     being created
  18790.  
  18791.  status   smallint  Control bits, some of which can be set by the user with
  18792.                     sp_dboption (read only, dbo only, single user, and so
  18793.                     on):
  18794.                     0x40  select into/bulkcopy; set with sp_dboption
  18795.                     0x08  truncate log on chkpt; set with sp_dboption
  18796.                     0x10  no chkpt on recovery; set with sp_dboption
  18797.  Column   Datatype  Description
  18798.  ────────────────────────────────────────────────────────────────────────────
  18799.                    0x10  no chkpt on recovery; set with sp_dboption
  18800.                     0x20  Crashed while database was being loaded;
  18801.                       instructs recovery not to proceed
  18802.                     0x100  Database is suspect; cannot be opened or used in
  18803.                     its present state and can be dropped only with
  18804.                       DBCC   DBREPAIR
  18805.                     0x400  Read only; set with sp_dboption
  18806.                     0x800  dbo use only; set with sp_dboption
  18807.                     0x1000 Single user; set with sp_dboption
  18808.                     0x4000  dbname has changed
  18809.  
  18810.  version  smallint  Version of SQL Server code under which database was
  18811.                     created
  18812.  
  18813.  crdate   datetime  Creation date
  18814.  
  18815.  ────────────────────────────────────────────────────────────────────────────
  18816.  
  18817.  
  18818.  
  18819.  
  18820.  Indexes
  18821.  
  18822.  Unique clustered index on name Unique nonclustered index on dbid
  18823.  
  18824.  
  18825.  Referenced by System Procedures
  18826.  
  18827.  sp_addlogin, sp_changedbowner, sp_dboption, sp_defaultdb, sp_depends,
  18828.  sp_helpdb, sp_logdevice, sp_renamedb
  18829.  
  18830.  
  18831.  sysdepends (all databases)
  18832.  
  18833.  
  18834.  Description
  18835.  
  18836.  Contains one row for each procedure, view, or table that is referenced by a
  18837.  procedure, view, or trigger.
  18838.  
  18839. ╓┌──────────┌─────────┌──────────────────────────────────────────────────────╖
  18840.  Column     Datatype  Description
  18841.  ────────────────────────────────────────────────────────────────────────────
  18842.  id         int       Object ID
  18843.  number     smallint  Procedure number
  18844.  depid      int       Dependent object ID
  18845.  depnumber  smallint  Dependent procedure number
  18846.  depdbid              Reserved for future use
  18847.  depsiteid            Reserved for future use
  18848.  status     smallint  Internal status information
  18849.  selall     bit       On if object is used in SELECT * statement
  18850.  resultobj  bit       On if object is being updated
  18851.  readobj    bit       On if object is being read
  18852.  ────────────────────────────────────────────────────────────────────────────
  18853.  
  18854.  
  18855.  
  18856.  Indexes
  18857.  
  18858.  Clustered index on id, number, depid, depnumber, dpedbid, and depsiteid
  18859.  
  18860.  
  18861.  Referenced by System Procedures
  18862.  
  18863.  sp_depends
  18864.  
  18865.  
  18866.  sysdevices (master database only)
  18867.  
  18868.  
  18869.  Description
  18870.  
  18871.  Contains one row for each disk dump device, diskette dump device, and
  18872.  database device. When SQL Server is installed, there are four entries in
  18873.  sysdevices: one for the master database device (for databases), one for a
  18874.  disk dump device, and two for diskette dump devices.
  18875.  
  18876. ╓┌──────────┌─────────────┌──────────────────────────────────────────────────╖
  18877.  Column     Datatype      Description
  18878.  ────────────────────────────────────────────────────────────────────────────
  18879.  low        int           First virtual page number on database device (not
  18880.                           used for dump devices)
  18881.  Column     Datatype      Description
  18882.  ────────────────────────────────────────────────────────────────────────────
  18883.                          used for dump devices)
  18884.  
  18885.  high       int           Last virtual page number on database device (not
  18886.                           used for dump devices)
  18887.  
  18888.  status     smallint      Indicates whether it is to be used as a default:
  18889.                            2 Physical database device for database storage
  18890.                            3 Physical database device for default database
  18891.                             storage
  18892.                           16 Dump device (either disk or diskette)
  18893.                           24 Dump device (either disk or diskette);
  18894.                             skip ANSI labels
  18895.  
  18896.  cntrltype  smallint      Controller type (0 if database device, 2 if hard
  18897.                           disk dump device, 3-4 if diskette dump device)
  18898.  
  18899.  name       sysname       Logical name of dump device or database device
  18900.  
  18901.  phyname    varchar(127)  Name of physical database device or dump device
  18902.  Column     Datatype      Description
  18903.  ────────────────────────────────────────────────────────────────────────────
  18904. phyname    varchar(127)  Name of physical database device or dump device
  18905.  
  18906.  ────────────────────────────────────────────────────────────────────────────
  18907.  
  18908.  
  18909.  
  18910.  
  18911.  Indexes
  18912.  
  18913.  Unique clustered index on name
  18914.  
  18915.  
  18916.  Referenced by System Procedures
  18917.  
  18918.  sp_addumpdevice, sp_diskdefault, sp_dropdumpdevice, sp_helpdevice,
  18919.  sp_logdevice
  18920.  
  18921.  
  18922.  sysindexes (all databases)
  18923.  
  18924.  
  18925.  Description
  18926.  
  18927.  Contains one row for each clustered index, one row for each nonclustered
  18928.  index, and one row for each table that has no clustered index.
  18929.  
  18930.  The columns dpages, reserved, used, and rows are maintained only if the row
  18931.  describes a table or clustered index.
  18932.  
  18933. ╓┌─────────────┌───────────────┌─────────────────────────────────────────────╖
  18934.  Column        Datatype        Description
  18935.  ────────────────────────────────────────────────────────────────────────────
  18936.  name          sysname         Index or table name
  18937.  
  18938.  id            int             ID of table or ID of table to which index
  18939.                                belongs
  18940.  
  18941.  indid         smallint        0 if table, 1 if clustered index, >1 if
  18942.                                nonclustered
  18943.  
  18944.  Column        Datatype        Description
  18945.  ────────────────────────────────────────────────────────────────────────────
  18946. 
  18947.  dpages        int             Number of data pages used (if entry is a
  18948.                                table or clustered index)
  18949.  
  18950.  reserved      int             Number of pages allocated (if entry is a
  18951.                                table or clustered index)
  18952.  
  18953.  used          int             Number of data and index pages (if entry is
  18954.                                a clustered index)
  18955.  
  18956.  rows          int             Number of rows (if entry is a table or
  18957.                                clustered index)
  18958.  
  18959.  first         int             Pointer to first data or leaf page
  18960.  
  18961.  root          int             Pointer to root page if entry is an index;
  18962.                                pointer to last page if entry is a table
  18963.  
  18964.  distribution  int             Pointer to distribution page (if entry is an
  18965.  Column        Datatype        Description
  18966.  ────────────────────────────────────────────────────────────────────────────
  18967. distribution  int             Pointer to distribution page (if entry is an
  18968.                                index)
  18969.  
  18970.  usagecnt      int             reserved
  18971.  
  18972.  segment       smallint        Number of segment in which this object
  18973.                                resides
  18974.  
  18975.  status        smallint        Internal system status information:
  18976.                                0x1 Abort command if attempt to insert
  18977.                                 duplicate key
  18978.                                0x2 Unique index
  18979.                                0x4 Abort command if attempt to insert
  18980.                                 duplicate row
  18981.                                0x10 Clustered index
  18982.                                0x40 Index allows duplicate rows
  18983.  
  18984.  rowpage       smallint        Maximum number of rows per page
  18985.  
  18986.  Column        Datatype        Description
  18987.  ────────────────────────────────────────────────────────────────────────────
  18988. 
  18989.  minlen        smallint        Minimum size of a row
  18990.  
  18991.  maxlen        smallint        Maximum size of a row
  18992.  
  18993.  maxirow       smallint        Maximum size of a nonleaf index row
  18994.  
  18995.  keycnt        smallint        Number of keys
  18996.  
  18997.  keys1         varbinary(255)  Description of key columns (if entry is an
  18998.                                index)
  18999.  
  19000.  keys2         varbinary(255)  Description of key columns (if entry is an
  19001.                                index)
  19002.  
  19003.  ────────────────────────────────────────────────────────────────────────────
  19004.  
  19005.  
  19006.  
  19007.  
  19008.  Indexes
  19009.  
  19010.  Unique clustered index on id and indid
  19011.  
  19012.  
  19013.  Referenced by System Procedures
  19014.  
  19015.  sp_helpindex, sp_spaceused
  19016.  
  19017.  
  19018.  syskeys (all databases)
  19019.  
  19020.  
  19021.  Description
  19022.  
  19023.  Contains one row for each foreign or primary key.
  19024.  
  19025. ╓┌────────┌─────────┌────────────────────────────────────────────────────────╖
  19026.  Column   Datatype  Description
  19027.  ────────────────────────────────────────────────────────────────────────────
  19028.  Column   Datatype  Description
  19029.  ────────────────────────────────────────────────────────────────────────────
  19030.  id       int       int null
  19031.  type     smallint  Record type
  19032.  depid    int null  Dependent object ID
  19033.  keycnt   int       Number of non-null keys
  19034.  size     int null  reserved
  19035.  key1     int null  Column ID
  19036.  key2     int null  Column ID
  19037.  key3     int null  Column ID
  19038.  key4     int null  Column ID
  19039.  key5     int null  Column ID
  19040.  key6     int null  Column ID
  19041.  key7     int null  Column ID
  19042.  key8     int null  Column ID
  19043.  depkey1  int null  Column ID
  19044.  depkey2  int null  Column ID
  19045.  depkey3  int null  Column ID
  19046.  depkey4  int null  Column ID
  19047.  depkey5  int null  Column ID
  19048.  depkey6  int null  Column ID
  19049.  Column   Datatype  Description
  19050.  ────────────────────────────────────────────────────────────────────────────
  19051. depkey6  int null  Column ID
  19052.  depkey7  int null  Column ID
  19053.  depkey8  int null  Column ID
  19054.  ────────────────────────────────────────────────────────────────────────────
  19055.  
  19056.  
  19057.  
  19058.  Indexes
  19059.  
  19060.  Clustered index on id
  19061.  
  19062.  
  19063.  Referenced by System Procedures
  19064.  
  19065.  sp_commonkey, sp_dropkey, sp_foreignkey, sp_helpkey, sp_helpjoins,
  19066.  sp_primarykey
  19067.  
  19068.  
  19069.  syslocks (master database only)
  19070.  
  19071.  
  19072.  Description
  19073.  
  19074.  Contains information about active locks. The syslocks system table is not a
  19075.  standard table. Rather, it is built dynamically when queried by a user. No
  19076.  updates to syslocks are allowed.
  19077.  
  19078. ╓┌───────┌─────────┌─────────────────────────────────────────────────────────╖
  19079.  Column  Datatype  Description
  19080.  ────────────────────────────────────────────────────────────────────────────
  19081.  id      int       Table ID
  19082.  
  19083.  dbid    smallint  Database ID
  19084.  
  19085.  page    int       Page number
  19086.  
  19087.  type    smallint  Type of lock:
  19088.                    1 Exclusive table lock
  19089.                    2 Shared table lock
  19090.                    3 Exclusive intent lock (will do page locking on
  19091.  Column  Datatype  Description
  19092.  ────────────────────────────────────────────────────────────────────────────
  19093.                   3 Exclusive intent lock (will do page locking on
  19094.                     indicated pages)
  19095.                    4 Shared intent lock
  19096.                    5 Exclusive page lock
  19097.                    6 Shared page lock
  19098.                    7 Update page lock (changes to exclusive if page is
  19099.                     actually modified)
  19100.                    8 Exclusive extent lock
  19101.                    9 Shared extent lock
  19102.                    0x100 Lock is blocking another process
  19103.                    0x200 Demand lock
  19104.  
  19105.  spid    smallint  ID of process that holds the lock
  19106.  
  19107.  ────────────────────────────────────────────────────────────────────────────
  19108.  
  19109.  
  19110.  
  19111.  
  19112.  Indexes
  19113.  
  19114.  None
  19115.  
  19116.  
  19117.  Referenced by System Procedures
  19118.  
  19119.  sp_lock
  19120.  
  19121.  
  19122.  syslogins (master database only)
  19123.  
  19124.  
  19125.  Description
  19126.  
  19127.  Contains one row for each valid SQL Server user account. When SQL Server is
  19128.  installed, syslogins contains one entry, in which the name is sa, the suid
  19129.  is 1, and the password is null.
  19130.  
  19131. ╓┌────────────┌─────────────┌────────────────────────────────────────────────╖
  19132.  Column       Datatype      Description
  19133.  Column       Datatype      Description
  19134.  ────────────────────────────────────────────────────────────────────────────
  19135.  suid         smallint      Server user ID
  19136.  
  19137.  status       smallint      reserved
  19138.  
  19139.  accdate      datetime      reserved
  19140.  
  19141.  totcpu       int           reserved
  19142.  
  19143.  totio        int           reserved
  19144.  
  19145.  spacelimit   int           reserved
  19146.  
  19147.  timelimit    int           reserved
  19148.  
  19149.  resultlimit  int           reserved
  19150.  
  19151.  dbname       sysname       Name of database to put user into when
  19152.                             connection is established
  19153.  
  19154.  Column       Datatype      Description
  19155.  ────────────────────────────────────────────────────────────────────────────
  19156. 
  19157.  name         sysname       Login ID of user
  19158.  
  19159.  password     sysname null  Password of user (may be null)
  19160.  
  19161.  ────────────────────────────────────────────────────────────────────────────
  19162.  
  19163.  
  19164.  
  19165.  
  19166.  Indexes
  19167.  
  19168.  Unique clustered index on suid Unique nonclustered index on name
  19169.  
  19170.  
  19171.  Referenced by System Procedures
  19172.  
  19173.  sp_addalias, sp_addlogin, sp_adduser, sp_changedbowner, sp_defaultdb,
  19174.  sp_defdb, sp_droplogin, sp_helpdb, sp_helpuser, sp_password
  19175.  
  19176.  
  19177.  syslogs (all databases)
  19178.  
  19179.  
  19180.  Description
  19181.  
  19182.  Contains the transaction log. It is used by SQL Server for recovery and
  19183.  roll-forward; it is not used by system users.
  19184.  
  19185.  You cannot delete from, insert into, or update syslogs. Every data
  19186.  modification operation is logged, so before you can change syslogs, the
  19187.  change must be logged. This means that a change operation on syslogs adds a
  19188.  row to syslogs, which then must be logged, adding another row to syslogs,
  19189.  and so on, producing an infinite loop. The loop continues until the database
  19190.  becomes full.
  19191.  
  19192.  Column  Datatype   Description
  19193.  ────────────────────────────────────────────────────────────────────────────
  19194.  xactid  binary(6)  Transaction ID
  19195.  op      tinyint    Update operation number
  19196.  ────────────────────────────────────────────────────────────────────────────
  19197.  
  19198.  
  19199.  Indexes
  19200.  
  19201.  None
  19202.  
  19203.  
  19204.  sysmessages (master database only)
  19205.  
  19206.  
  19207.  Description
  19208.  
  19209.  Contains one row for each system error or warning that can be returned by
  19210.  SQL Server. SQL Server displays the error description on the user's screen.
  19211.  
  19212.  
  19213.  Column       Datatype      Description
  19214.  ────────────────────────────────────────────────────────────────────────────
  19215.  error        int           Unique error number
  19216.  
  19217.  severity     smallint      Severity level of error
  19218.  
  19219.  dlevel       smallint      Reserved for number of descriptive level of
  19220.                             this message:  terse, short, or long
  19221.  
  19222.  description  varchar(255)  Explanation of error with place holders for
  19223.                             parameters
  19224.  
  19225.  ────────────────────────────────────────────────────────────────────────────
  19226.  
  19227.  
  19228.  
  19229.  Indexes
  19230.  
  19231.  Unique clustered index on error and dlevel
  19232.  
  19233.  
  19234.  sysobjects (all databases)
  19235.  
  19236.  
  19237.  Description
  19238.  
  19239.  Contains one row for each table, view, stored procedure, log, rule, default,
  19240.  trigger, and (in tempdb only) temporary object.
  19241.  
  19242. ╓┌─────────┌─────────┌───────────────────────────────────────────────────────╖
  19243.  Column    Datatype  Description
  19244.  ────────────────────────────────────────────────────────────────────────────
  19245.  name      sysname   Object name
  19246.  
  19247.  id        int       Object ID
  19248.  
  19249.  uid       smallint  User ID of owner object
  19250.  
  19251.  type      char      One of the following object types:
  19252.                      S System table
  19253.                      U User table
  19254.                      V View
  19255.                      L Log
  19256.                      P Stored procedure
  19257.                      R Rule
  19258.                      D Default
  19259.  Column    Datatype  Description
  19260.  ────────────────────────────────────────────────────────────────────────────
  19261.                     D Default
  19262.                      TR Trigger
  19263.  
  19264.  userstat  smallint  Application-dependent type information
  19265.  
  19266.  sysstat   smallint  Internal status information
  19267.  
  19268.  indexdel  smallint  Index delete count (incremented if an index is deleted)
  19269.  
  19270.  schema    smallint  Count of changes in schema of a given object
  19271.                      (incremented if a rule or default is added)
  19272.  
  19273.  refdate   datetime  reserved
  19274.  
  19275.  crdate    datetime  Date object was created
  19276.  
  19277.  expdate   datetime  reserved
  19278.  
  19279.  deltrig   int       Stored procedure ID of a delete trigger
  19280.  Column    Datatype  Description
  19281.  ────────────────────────────────────────────────────────────────────────────
  19282. deltrig   int       Stored procedure ID of a delete trigger
  19283.  
  19284.  instrig   int       Stored procedure ID of an insert trigger
  19285.  
  19286.  updtrig   int       Stored procedure ID of an update trigger
  19287.  
  19288.  seltrig   int       reserved
  19289.  
  19290.  category  int       reserved
  19291.  
  19292.  cache     smallint  reserved
  19293.  
  19294.  ────────────────────────────────────────────────────────────────────────────
  19295.  
  19296.  
  19297.  
  19298.  
  19299.  Indexes
  19300.  
  19301.  Unique clustered index on id Unique nonclustered index on name and uid
  19302.  
  19303.  
  19304.  Referenced by System Procedures
  19305.  
  19306.  sp_bindefault, sp_bindrule, sp_commonkey, sp_depends, sp_dropgroup,
  19307.  sp_dropkey, sp_droptype, sp_dropuser, sp_foreignkey, sp_help, sp_helpjoins,
  19308.  sp_helprotect, sp_primarykey, sp_rename, sp_unbindefault, sp_unbindrule
  19309.  
  19310.  
  19311.  sysprocedures (all databases)
  19312.  
  19313.  
  19314.  Description
  19315.  
  19316.  Contains entries for each view, default, rule, trigger, and stored
  19317.  procedure. The plan or sequence tree for each object is stored in binary
  19318.  form. If the sequence tree doesn't fit in one entry, it is broken into more
  19319.  than one row. The sequence field identifies the sub-rows.
  19320.  
  19321. ╓┌─────────┌─────────┌───────────────────────────────────────────────────────╖
  19322.  Column    Datatype  Description
  19323.  ────────────────────────────────────────────────────────────────────────────
  19324.  type      smallint  Object type:
  19325.                      0x1 Entry describes a plan (reserved)
  19326.                      0x2 Entry describes a tree
  19327.  
  19328.  id        int       Object ID
  19329.  
  19330.  sequence  smallint  Sequence number if more than one row is used to
  19331.                      describe this object
  19332.  
  19333.  status    smallint  Internal system status
  19334.  
  19335.  number    smallint  Subprocedure number when the procedure is grouped (0
  19336.                      for nonprocedure entries)
  19337.  
  19338.  ────────────────────────────────────────────────────────────────────────────
  19339.  
  19340.  
  19341.  
  19342.  
  19343.  Indexes
  19344.  
  19345.  Unique clustered index on id, number, type, and sequence
  19346.  
  19347.  
  19348.  sysprocesses (master database only)
  19349.  
  19350.  
  19351.  Description
  19352.  
  19353.  Contains information about SQL Server processes. The sysprocesses system
  19354.  table is not a standard table. Rather, it is built dynamically when queried
  19355.  by a user. No updates to sysprocesses are allowed. Use the KILL statement to
  19356.  kill a process.
  19357.  
  19358. ╓┌─────────────┌─────────┌───────────────────────────────────────────────────╖
  19359.  Column        Datatype  Description
  19360.  ────────────────────────────────────────────────────────────────────────────
  19361.  spid          smallint  Process ID
  19362.  
  19363.  kpid          smallint  Kernel process ID
  19364.  Column        Datatype  Description
  19365.  ────────────────────────────────────────────────────────────────────────────
  19366. kpid          smallint  Kernel process ID
  19367.  
  19368.  status        char(10)  Process ID status (for example, runnable, sleeping,
  19369.                          and so on)
  19370.  
  19371.  suid          smallint  Server user ID of user who executed command
  19372.  
  19373.  hostname      char(10)  Name of workstation
  19374.  
  19375.  program_name  char(16)  Name of application program
  19376.  
  19377.  hostprocess   char(8)   Workstation process ID number
  19378.  
  19379.  cmd           char(16)  Command currently being executed
  19380.  
  19381.  cpu           int       Cumulative CPU time for process
  19382.  
  19383.  physical_io   int       Number of disk reads and writes for current
  19384.                          command
  19385.  Column        Datatype  Description
  19386.  ────────────────────────────────────────────────────────────────────────────
  19387.                         command
  19388.  
  19389.  memusage      int       Amount of memory allocated to process
  19390.  
  19391.  blocked       smallint  Process ID of blocking process, if any
  19392.  
  19393.  dbid          smallint  Database ID
  19394.  
  19395.  uid           smallint  ID of user who executed command
  19396.  
  19397.  gid           smallint  Group ID of user who executed command
  19398.  
  19399.  ────────────────────────────────────────────────────────────────────────────
  19400.  
  19401.  
  19402.  
  19403.  
  19404.  Indexes
  19405.  
  19406.  None
  19407.  
  19408.  
  19409.  Referenced by System Procedures
  19410.  
  19411.  sp_who
  19412.  
  19413.  
  19414.  sysprotects (all databases)
  19415.  
  19416.  
  19417.  Description
  19418.  
  19419.  Contains user permissions information─entries for each GRANT and REVOKE
  19420.  statement that has been executed.
  19421.  
  19422. ╓┌────────────┌──────────────┌───────────────────────────────────────────────╖
  19423.  Column       Datatype       Description
  19424.  ────────────────────────────────────────────────────────────────────────────
  19425.  id           int            ID of object to which this permission applies
  19426.  
  19427.  Column       Datatype       Description
  19428.  ────────────────────────────────────────────────────────────────────────────
  19429. 
  19430.  uid          smallint       ID of user or group to which this permission
  19431.                              applies
  19432.  
  19433.  action       tinyint        One of the following permissions:
  19434.                              SELECT = 193
  19435.                              INSERT = 195
  19436.                              DELETE = 196
  19437.                              UPDATE = 197
  19438.                              EXECUTE = 224
  19439.                              CREATE DATABASE = 203
  19440.                              CREATE DEFAULT = 233
  19441.                              CREATE PROCEDURE = 222
  19442.                              CREATE RULE = 236
  19443.                              CREATE TABLE = 198
  19444.                              CREATE VIEW = 207
  19445.                              DUMP DATABASE = 228
  19446.                              DUMP TRANSACTION = 235
  19447.  
  19448.  Column       Datatype       Description
  19449.  ────────────────────────────────────────────────────────────────────────────
  19450. 
  19451.  protecttype  tinyint        Either 205 (grant) or 206 (revoke)
  19452.  
  19453.  columns      varbinary(32)  Bit map of columns to which this SELECT or
  19454.                              UPDATE permission applies. Bit 0 indicates all
  19455.                              columns; bit 1 means permission applies to
  19456.                              that column; NULL means no information.
  19457.  
  19458.  ────────────────────────────────────────────────────────────────────────────
  19459.  
  19460.  
  19461.  
  19462.  
  19463.  Indexes
  19464.  
  19465.  Clustered index on id, uid, and action
  19466.  
  19467.  
  19468.  Referenced by System Procedures
  19469.  
  19470.  sp_helprotect
  19471.  
  19472.  
  19473.  syssegments (all databases)
  19474.  
  19475.  
  19476.  Description
  19477.  
  19478.  Contains one row for each segment (named collection of disk pieces). When
  19479.  SQL Server is installed, this table contains two entries: segment 0 (system)
  19480.  for system tables and segment 1 (default) for other objects. All entries in
  19481.  sysusages contain both these segments in their maps.
  19482.  
  19483.  Column   Datatype  Description
  19484.  ────────────────────────────────────────────────────────────────────────────
  19485.  segment  smallint  Segment number
  19486.  name     sysname   Segment name, used internally
  19487.  status   int null  Indicates which is the default segment
  19488.  ────────────────────────────────────────────────────────────────────────────
  19489.  
  19490.  
  19491.  Indexes
  19492.  
  19493.  None
  19494.  
  19495.  
  19496.  systypes (all databases)
  19497.  
  19498.  
  19499.  Description
  19500.  
  19501.  Contains one row for each system-supplied and each user-defined datatype.
  19502.  Domains (defined by rules) and defaults are given if they exist. The rows
  19503.  that describe system-supplied datatypes cannot be altered. The
  19504.  system-supplied datatypes and their ID numbers (the contents of the name and
  19505.  type fields, respectively) are as follows:
  19506.  
  19507. ╓┌───────────────────────────────┌───────────────────────────────────────────╖
  19508.  System Datatype                 ID Number
  19509.  ────────────────────────────────────────────────────────────────────────────
  19510.  binary                          45
  19511.  System Datatype                 ID Number
  19512.  ────────────────────────────────────────────────────────────────────────────
  19513.  binary                          45
  19514.  bit                             50
  19515.  char                            4
  19516.  datetime                        61
  19517.  datetimn                        111
  19518.  float                           62
  19519.  floatn                          109
  19520.  image                           46
  19521.  int                             56
  19522.  intn                            38
  19523.  money                           60
  19524.  moneyn                          110
  19525.  smallint                        52
  19526.  sysname                         39
  19527.  text                            35
  19528.  timestamp                       37
  19529.  tinyint                         48
  19530.  varbinary                       37
  19531.  varchar                         39
  19532.  System Datatype                 ID Number
  19533.  ────────────────────────────────────────────────────────────────────────────
  19534. varchar                         39
  19535.  ────────────────────────────────────────────────────────────────────────────
  19536.  
  19537.  
  19538.  The following table shows the datatype and description for each column in
  19539.  the systypes table.
  19540.  
  19541. ╓┌───────────┌─────────────┌─────────────────────────────────────────────────╖
  19542.  Column      Datatype      Description
  19543.  ────────────────────────────────────────────────────────────────────────────
  19544.  uid         smallint      User ID of datatype creator
  19545.  
  19546.  usertype    smallint      User type ID
  19547.  
  19548.  variable    bit           1 if datatype is variable length; 0 otherwise
  19549.  
  19550.  allownulls  bit           Indicates whether NULLs are allowed for this
  19551.                            datatype
  19552.  
  19553.  Column      Datatype      Description
  19554.  ────────────────────────────────────────────────────────────────────────────
  19555. 
  19556.  type        tinyint       Physical storage datatype
  19557.  
  19558.  length      tinyint       Physical length of datatype
  19559.  
  19560.  tdefault    int           ID of stored procedure that generates default
  19561.                            for this datatype
  19562.  
  19563.  domain      int           ID of stored procedure that contains integrity
  19564.                            checks for this datatype
  19565.  
  19566.  name        sysname       Datatype name
  19567.  
  19568.  printfmt    varchar(255)  reserved
  19569.  
  19570.  ────────────────────────────────────────────────────────────────────────────
  19571.  
  19572.  
  19573.  
  19574.  
  19575.  Indexes
  19576.  
  19577.  Unique clustered index on name Unique nonclustered index on usertype
  19578.  
  19579.  
  19580.  Referenced by System Procedures
  19581.  
  19582.  sp_addtype, sp_bindefault, sp_bindrule, sp_droptype, sp_dropuser, sp_help,
  19583.  sp_rename, sp_unbindefault, sp_unbindrule
  19584.  
  19585.  
  19586.  sysusages (master database only)
  19587.  
  19588.  
  19589.  Description
  19590.  
  19591.  Contains one row for each disk allocation piece assigned to a database. Each
  19592.  database contains a specified number of database (logical) page numbers.
  19593.  Each disk piece includes segments 0 and 1.
  19594.  
  19595.  When the CREATE DATABASE statement is executed, SQL Server scans the disk(s)
  19596.  to find available disk allocation pieces. One or more contiguous disk
  19597.  allocation pieces is assigned to the database, and the mapping is recorded
  19598.  in sysusages.
  19599.  
  19600. ╓┌───────┌─────────┌─────────────────────────────────────────────────────────╖
  19601.  Column  Datatype  Description
  19602.  ────────────────────────────────────────────────────────────────────────────
  19603.  dbid    smallint  Database ID
  19604.  segmap  int       Bit map of possible segment assignments
  19605.  lstart  int       First database (logical) page number
  19606.  size    int       Number of contiguous database (logical) pages
  19607.  vstart  int       Starting virtual page number
  19608.  ────────────────────────────────────────────────────────────────────────────
  19609.  
  19610.  
  19611.  
  19612.  Indexes
  19613.  
  19614.  Unique clustered index on dbid and lstart Unique nonclustered index on
  19615.  vstart
  19616.  
  19617.  
  19618.  Referenced by System Procedures
  19619.  
  19620.  sp_logdevice
  19621.  
  19622.  
  19623.  sysusers (all databases)
  19624.  
  19625.  
  19626.  Description
  19627.  
  19628.  Contains one row for each user allowed in the database and one row for each
  19629.  group.
  19630.  
  19631.  On the SQL Server distribution diskette, master..sysusers contains three
  19632.  entries: dbo, whose suid is 1 and uid is 1; guest, whose suid is -1 and uid
  19633.  is 2; and public, whose suid is -2 and uid is 0. The sysusers table in the
  19634.  model database (and thus in all user databases) initially contains two
  19635.  entries: dbo and public.
  19636.  
  19637.  The user guest provides a mechanism for giving access to the database to
  19638.  users not explicitly listed in sysusers, with a restricted set of
  19639.  permissions. The guest entry in master means that any user with an account
  19640.  on SQL Server (that is, with an entry in syslogins) can access master.
  19641.  
  19642.  The user public refers to all users. The keyword PUBLIC is used with the
  19643.  GRANT and REVOKE statements to signify that the permission is being given to
  19644.  or taken away from all users.
  19645.  
  19646. ╓┌────────┌─────────────┌────────────────────────────────────────────────────╖
  19647.  Column   Datatype      Description
  19648.  ────────────────────────────────────────────────────────────────────────────
  19649.  uid      smallint      Server user ID, copied from syslogins. suid 1 is
  19650.                         the System Administrator; -1 is a guest account.
  19651.  
  19652.  uid      smallint      User ID, unique in this database, used for granting
  19653.                         and revoking permissions. uid 1 is the dbo.
  19654.  
  19655.  gid      smallint      Group ID to which this user belongs. If uid = gid,
  19656.                         this entry defines a group, and suid for this entry
  19657.                         is 0.
  19658.  Column   Datatype      Description
  19659.  ────────────────────────────────────────────────────────────────────────────
  19660.                        is 0.
  19661.  
  19662.  name     sysname       Username or groupname, unique in this database.
  19663.  
  19664.  environ  varchar(255)  reserved
  19665.  
  19666.  ────────────────────────────────────────────────────────────────────────────
  19667.  
  19668.  
  19669.  
  19670.  
  19671.  Indexes
  19672.  
  19673.  Unique clustered index on suid Unique nonclustered index on name Unique
  19674.  nonclustered index on uid
  19675.  
  19676.  
  19677.  Referenced by System Procedures
  19678.  
  19679.  sp_addalias, sp_adduser, sp_addgroup, sp_changedbowner, sp_changegroup,
  19680.  sp_dropgroup, sp_droplogin, sp_droptype, sp_dropuser, sp_helpgroup,
  19681.  sp_helprotect, sp_helpuser
  19682.  
  19683.  Figure A.1: System Tables Relationship Chart -
  19684.  
  19685.  (This figure may be found in the printed book.)
  19686.  
  19687.  
  19688.  
  19689.  
  19690.  
  19691.  
  19692.  Appendix B  The pubs Sample Database
  19693.  ────────────────────────────────────────────────────────────────────────────
  19694.  
  19695.  This appendix describes the sample database, pubs. The pubs database has
  19696.  eight tables:
  19697.  
  19698.  
  19699.    ■   publishers
  19700.  
  19701.    ■   titleauthor
  19702.  
  19703.    ■   sales
  19704.  
  19705.    ■   stores
  19706.  
  19707.    ■   discounts
  19708.  
  19709.    ■   roysched
  19710.  
  19711.    ■   authors
  19712.  
  19713.    ■   titles
  19714.  
  19715.  
  19716.  Each database table is described by two figures. The first figure documents
  19717.  the structure of the table; for each table field, the figure lists its
  19718.  datatype, its NULL/NOT NULL status, and any defaults, rules, triggers, and
  19719.  indexes. The second figure lists the table contents.
  19720.  
  19721.  Table   publishers ─ Structure
  19722.  
  19723.            pub_id         pub_name     city         state
  19724.  ────────────────────────────────────────────────────────────────────────────
  19725.  Datatype  char(4)        varchar(40)  varchar(20)  char(2)
  19726.  Null      not null       null         null         null
  19727.  Rule      pub_idrule(1)  ─            ─            ─
  19728.  Index     clust, uniq    ─            ─            ─
  19729.  ────────────────────────────────────────────────────────────────────────────
  19730.  
  19731.  (1) The pub_idrule states that the data must be 1389, 0736, 0877, 1622, or
  19732.  1756, or must match the pattern  99[0-9][0-9].
  19733.  
  19734.  
  19735.  pub_id                            pub_name    city           state
  19736.  ────────────────────────────────────────────────────────────────────────────
  19737.  1389                              Algodata    Berkeley       CA
  19738.                                    Infosystem
  19739.                                    s
  19740.  
  19741.  0736                              New Age     Boston         MA
  19742.                                    Books
  19743.  
  19744.  0877                              Binnet &    Washington     DC
  19745.                                    Hardley
  19746.  
  19747.  Table   titleauthor ─ Structure
  19748.  
  19749.            au_id            title_id  au_ord    royaltyper
  19750.  ────────────────────────────────────────────────────────────────────────────
  19751.  Datatype  id               tid       smallint  int
  19752.  
  19753.  Null      not null         not null  null      null
  19754.  
  19755.  Index     nonclust         nonclust  ─         ─
  19756.  
  19757.            uniq, nonclust,
  19758.            composite
  19759.  
  19760.  ────────────────────────────────────────────────────────────────────────────
  19761.  
  19762.  
  19763.  Table   titleauthor ─ Contents
  19764.  
  19765. ╓┌────────────┌─────────┌───────┌────────────────────────────────────────────╖
  19766.  au_id        title_id  au_ord  royaltyper
  19767.  ────────────────────────────────────────────────────────────────────────────
  19768.  409-56-7008  BU1032    1       60
  19769.  213-46-8915  BU1032    2       40
  19770.  238-95-7766  PC1035    1       100
  19771.  213-46-8915  BU2075    1       100
  19772.  998-72-3567  PS2091    1       50
  19773.  899-46-2035  PS2091    2       50
  19774.  998-72-3567  PS2106    1       100
  19775.  722-51-5454  MC3021    1       75
  19776.  899-46-2035  MC3021    2       25
  19777.  807-91-6654  TC3218    1       100
  19778.  486-29-1786  PS7777    1       100
  19779.  486-29-1786  PC9999    1       100
  19780.  712-45-1867  MC2222    1       100
  19781.  172-32-1176  PS3333    1       100
  19782.  274-80-9391  BU7832    1       100
  19783.  427-17-2319  PC8888    1       50
  19784.  au_id        title_id  au_ord  royaltyper
  19785.  ────────────────────────────────────────────────────────────────────────────
  19786. 427-17-2319  PC8888    1       50
  19787.  846-92-7186  PC8888    2       50
  19788.  756-30-7391  PS1372    1       75
  19789.  724-80-9391  PS1372    2       25
  19790.  724-80-9391  BU1111    1       60
  19791.  267-41-2394  BU1111    2       40
  19792.  672-71-3249  TC7777    1       40
  19793.  267-41-2394  TC7777    2       30
  19794.  472-27-2349  TC7777    3       30
  19795.  648-92-1872  TC4203    1       100
  19796.  ────────────────────────────────────────────────────────────────────────────
  19797.  
  19798.  
  19799.  Table   sales ─ Structure
  19800.  
  19801.            stor_id   ord_num      date      qty       payterms     title_id
  19802.  ────────────────────────────────────────────────────────────────────────────
  19803.  Datatype  char(4)   varchar(20)  datetime  smallint  varchar(12)  tid
  19804.  Null      not null  not null     not null  not null  not null     not null
  19805.  Index     ─         ─            ─         ─         ─            nonclust
  19806.  ────────────────────────────────────────────────────────────────────────────
  19807.  
  19808.  Table   sales ─ Contents
  19809.  
  19810. ╓┌────────┌─────────┌─────────┌────┌───────────┌─────────────────────────────╖
  19811.  stor_id  ord_num   date      qty  payterms    title_id
  19812.  ────────────────────────────────────────────────────────────────────────────
  19813.  7066     QA7442.3  09/13/85  75   On invoice  PS2091
  19814.  7067     D4482     09/14/85  10   Net 60      PS2091
  19815.  7131     N914008   09/14/85  20   Net 30      PS2091
  19816.  7131     N914014   09/14/85  25   Net 30      MC3021
  19817.  8042     423LL922  09/14/85  15   On invoice  MC3021
  19818.  8042     423LL930  09/14/85  10   On invoice  BU1032
  19819.  6380     722a      09/13/85  03   Net 60      PS2091
  19820.  6380     6871      09/14/85  05   Net 60      BU1032
  19821.  8042     P723      03/11/88  25   Net 30      BU1111
  19822.  7896     X999      02/21/88  35   On invoice  BU2075
  19823.  7896     QQ2299    10/28/87  15   Net 60      BU7832
  19824.  7896     TQ456     12/12/87  10   Net 60      MC2222
  19825.  8042     QA879.1   05/22/87  30   Net 30      PC1035
  19826.  stor_id  ord_num   date      qty  payterms    title_id
  19827.  ────────────────────────────────────────────────────────────────────────────
  19828. 8042     QA879.1   05/22/87  30   Net 30      PC1035
  19829.  7066     A2976     05/24/87  50   Net 30      PC8888
  19830.  7131     P3087a    05/29/87  20   Net 60      PS1372
  19831.  7131     P3087a    05/29/87  25   Net 60      PS2106
  19832.  7131     P3087a    05/29/87  15   Net 60      PS3333
  19833.  7131     P3087a    05/29/87  25   Net 60      PS7777
  19834.  7067     P2121     05/15/87  40   Net 30      TC3218
  19835.  7067     P2121     05/15/87  20   Net 30      TC4203
  19836.  7067     P2121     05/15/87  20   Net 30      TC7777
  19837.  ────────────────────────────────────────────────────────────────────────────
  19838.  
  19839.  
  19840.  Table   stores ─ Structure
  19841.  
  19842.             stor_id    stor_name    stor_addres  city         state    zip
  19843.                                     s
  19844.  ─────────────────────────────────────────────────────────────────────────────
  19845.  Datatype   char(4)    varchar(40)  varchar(40)  varchar(20)  char(2)  char(5)
  19846.  
  19847.  Null       not null   null         null         null         null     null
  19848.  
  19849.  ─────────────────────────────────────────────────────────────────────────────
  19850.  
  19851.  
  19852.  Table   stores ─ Contents
  19853.  
  19854. ╓┌────────┌────────────────────┌────────────────────┌──────────┌──────┌──────╖
  19855.  stor_id  stor_name            stor_address         city       state  zip
  19856.  ────────────────────────────────────────────────────────────────────────────
  19857.  7066     Barnum's             567 Pasadena Ave.    Tustin     CA     92789
  19858.  
  19859.  7067     News & Brews         577 First St.        Los Gatos  CA     96745
  19860.  
  19861.  7131     Doc-U-Mat: Quality   24-A Avrogado Way    Remulade   WA     98014
  19862.           Laundry and Books
  19863.  
  19864.  8042     Bookbeat             679 Carson St.       Portland   OR     89076
  19865.  
  19866.  6380     Eric the Read Books  788 Catamaugus Ave.  Seattle    WA     98056
  19867.  
  19868.  stor_id  stor_name            stor_address         city       state  zip
  19869.  ────────────────────────────────────────────────────────────────────────────
  19870. 
  19871.  7896     Fricative Bookshop   89 Madison St.       Fremont    CA     90019
  19872.  
  19873.  ────────────────────────────────────────────────────────────────────────────
  19874.  
  19875.  
  19876.  
  19877.  
  19878.  B.1.1
  19879.  
  19880.  
  19881.  discounts ─ Structure
  19882.  
  19883.  -         discounttype  stor_id  lowqty    highqty   discount
  19884.  ────────────────────────────────────────────────────────────────────────────
  19885.  Datatype  varchar(40)   char(4)  smallint  smallint  float
  19886.  
  19887.  Null      not null      null     null      null      not null
  19888.  
  19889.  
  19890.  discounts ─ Contents
  19891.  
  19892.  discounttype                      stor_id   lowqty  highqty  discount
  19893.  ────────────────────────────────────────────────────────────────────────────
  19894.  Initial Customer                  ─         ─       ─        10.5
  19895.  
  19896.  Volume Discount                   ─         100     1000     6.7
  19897.  
  19898.  Customer Discount                 8042      ─       ─        5.0
  19899.  
  19900.  
  19901.  roysched ─ Structure
  19902.  
  19903.  -                                 title_id  lorange  hirange  royalty
  19904.  ────────────────────────────────────────────────────────────────────────────
  19905.  Datatype                          tid        int      int      int
  19906.  
  19907.  Null                              not null   null     null     null
  19908.  
  19909.  Index                             nonclust   ─        ─        ─
  19910.  
  19911.  Table   roysched ─ Contents
  19912.  
  19913. ╓┌─────────┌────────┌─┌────────┌─┌───────────────────────────────────────────╖
  19914.  title_id  lorange    hirange    royalty
  19915.  ────────────────────────────────────────────────────────────────────────────
  19916.  BU1032    0          5000       10
  19917.  BU1032    5001       50000      12
  19918.  PC1035    0          2000       10
  19919.  PC1035    2001       3000       12
  19920.  PC1035    3001       4000       14
  19921.  PC1035    4001       10000      16
  19922.  PC1035    10001      50000      18
  19923.  BU2075    0          1000       10
  19924.  BU2075    1001       3000       12
  19925.  BU2075    3001       5000       14
  19926.  BU2075    5001       7000       16
  19927.  BU2075    7001       10000      18
  19928.  BU2075    10001      12000      20
  19929.  BU2075    12001      14000      22
  19930.  BU2075    14001      50000      24
  19931.  title_id  lorange    hirange    royalty
  19932.  ────────────────────────────────────────────────────────────────────────────
  19933. BU2075    14001      50000      24
  19934.  PS2091    0          1000       10
  19935.  PS2091    1001       5000       12
  19936.  PS2091    5001       10000      14
  19937.  PS2091    10001      50000      16
  19938.  PS2106    0          2000       10
  19939.  PS2106    2001       5000       12
  19940.  PS2106    5001       10000      14
  19941.  PS2106    10001      50000      16
  19942.  MC3021    0          1000       10
  19943.  MC3021    1001       2000       12
  19944.  MC3021    2001       4000       14
  19945.  MC3021    4001       6000       16
  19946.  MC3021    6001       8000       18
  19947.  MC3021    8001       10000      20
  19948.  MC3021    10001      12000      22
  19949.  MC3021    12001      50000      24
  19950.  TC3218    0          2000       10
  19951.  TC3218    2001       4000       12
  19952.  title_id  lorange    hirange    royalty
  19953.  ────────────────────────────────────────────────────────────────────────────
  19954. TC3218    2001       4000       12
  19955.  TC3218    4001       6000       14
  19956.  TC3218    6001       8000       16
  19957.  TC3218    8001       10000      18
  19958.  TC3218    10001      12000      20
  19959.  TC3218    12001      14000      22
  19960.  TC3218    14001      50000      24
  19961.  TC3218    0          2000       10
  19962.  TC3218    2001       4000       12
  19963.  TC3218    4001       6000       14
  19964.  TC3218    6001       8000       16
  19965.  TC3218    8001       10000      18
  19966.  TC3218    10001      12000      20
  19967.  TC3218    12001      14000      22
  19968.  TC3218    14001      50000      24
  19969.  PC8888    0          5000       10
  19970.  PC8888    5001       10000      12
  19971.  PC8888    10001      15000      14
  19972.  PC8888    15001      50000      16
  19973.  title_id  lorange    hirange    royalty
  19974.  ────────────────────────────────────────────────────────────────────────────
  19975. PC8888    15001      50000      16
  19976.  PS7777    0          5000       10
  19977.  PS7777    5001       50000      12
  19978.  PS3333    0          5000       10
  19979.  PS3333    5001       10000      12
  19980.  PS3333    10001      15000      14
  19981.  PS3333    15001      50000      16
  19982.  BU1111    0          4000       10
  19983.  BU1111    4001       8000       12
  19984.  BU1111    8001       10000      14
  19985.  BU1111    12001      16000      16
  19986.  BU1111    16001      20000      18
  19987.  BU1111    20001      24000      20
  19988.  BU1111    24001      28000      22
  19989.  BU1111    28001      50000      24
  19990.  MC2222    0          2000       10
  19991.  MC2222    2001       4000       12
  19992.  MC2222    4001       8000       14
  19993.  MC2222    8001       12000      16
  19994.  title_id  lorange    hirange    royalty
  19995.  ────────────────────────────────────────────────────────────────────────────
  19996. MC2222    8001       12000      16
  19997.  MC2222    8001       12000      16
  19998.  MC2222    12001      20000      18
  19999.  MC2222    20001      50000      20
  20000.  TC7777    0          5000       10
  20001.  TC7777    5001       15000      02
  20002.  TC7777    15001      50000      14
  20003.  TC4203    0          2000       10
  20004.  TC4203    2001       8000       12
  20005.  TC4203    8001       16000      14
  20006.  TC4203    16001      24000      16
  20007.  TC4203    24001      32000      18
  20008.  TC4203    32001      40000      20
  20009.  TC4203    40001      50000      22
  20010.  BU7832    0          5000       10
  20011.  BU7832    5001       10000      12
  20012.  BU7832    10001      15000      14
  20013.  BU7832    15001      20000      16
  20014.  BU7832    20001      25000      18
  20015.  title_id  lorange    hirange    royalty
  20016.  ────────────────────────────────────────────────────────────────────────────
  20017. BU7832    20001      25000      18
  20018.  BU7832    25001      30000      20
  20019.  BU7832    30001      35000      22
  20020.  BU7832    35001      50000      24
  20021.  PS1372    0          10000      10
  20022.  PS1372    10001      20000      12
  20023.  PS1372    20001      30000      14
  20024.  PS1372    30001      40000      16
  20025.  PS1372    40001      50000      18
  20026.  ────────────────────────────────────────────────────────────────────────────
  20027.  
  20028.  
  20029.  Table   authors ─ Structure
  20030.  
  20031.            au_id    au_lname     au_fname    phone     address      city
  20032.  
  20033.  ─────────────────────────────────────────────────────────────────────────────
  20034.  Datatype  id       varchar(40)  varchar(20)  char(12)  varchar(40)  varchar(2
  20035.  
  20036.  Null      not      not null     not null    not null  null         null
  20037.            null
  20038.  
  20039.  Default   ─        ─            ─           UNKNOWN1  ─            ─
  20040.  
  20041.  
  20042.  Index     clust,   nonclust,                ─         ─            ─
  20043.            uniq     composite
  20044.  
  20045.  ─────────────────────────────────────────────────────────────────────────────
  20046.  
  20047.  
  20048.  (1) The default UNKNOWN is inserted if no data is entered.
  20049.  (2) The rule ziprule states that the zip code must match the pattern
  20050.  [0-9][0-9][0-9][0-9][0-9].
  20051.  
  20052.  
  20053.  Table   authors ─ Contents
  20054.  
  20055. ╓┌────────────┌───────────────┌────────────┌─────────┌─────────────┌─────────
  20056.  au_id        au_lname        au_fname     phone     address       city
  20057.  au_id        au_lname        au_fname     phone     address       city
  20058.  
  20059.  ─────────────────────────────────────────────────────────────────────────────
  20060.  409-56-7008  Bennet          Abraham      415       6223 Bateman  Berkeley
  20061.                                            658-9932  St.
  20062.  
  20063.  213-46-8915  Green           Marjorie     415       309 63rd St.  Oakland
  20064.                                            986-7020  #411
  20065.  
  20066.  238-95-7766  Carson          Cheryl       415       589 Darwin    Berkeley
  20067.                                            548-7723  Ln.
  20068.  
  20069.  998-72-3567  Ringer          Albert       801       67 Seventh    Salt Lake
  20070.                                            826-0752  Av.           City
  20071.  
  20072.  899-46-2035  Ringer          Anne         801       67 Seventh    Salt Lake
  20073.                                            826-0752  Av.           City
  20074.  
  20075.  722-51-5454  DeFrance        Michel       219       3 Balding Pl.  Gary
  20076.                                            547-9982
  20077.  
  20078.  au_id        au_lname        au_fname     phone     address       city
  20079.  
  20080. 
  20081.  807-91-6654  Panteley        Sylvia       301       1956          Rockville
  20082.                                            946-8853  Arlington Dr.
  20083.  
  20084.  893-72-1158  McBadden        Heather      707       301 Putnam    Vacaville
  20085.                                            448-4982
  20086.  
  20087.  724-08-9931  Stringer        Dirk         415       5420          Oakland
  20088.                                            843-2991  Telegraph Av.
  20089.  
  20090.  274-80-9391  Straight        Dick         415       5420 College  Oakland
  20091.                                            834-2919  Av.
  20092.  
  20093.  756-30-7391  Karsen          Livia        415       5720 McAuley  Oakland
  20094.                                            534-9219  St.
  20095.  
  20096.  724-80-9391  MacFeather      Stearns      415       44 Upland     Oakland
  20097.                                            354-7128  Hts.
  20098.  
  20099.  au_id        au_lname        au_fname     phone     address       city
  20100.  
  20101. 
  20102.  427-17-2319  Dull            Ann          415       3410 Blonde   Palo Alto
  20103.                                            836-7128  St.
  20104.  
  20105.  672-71-3249  Yokomoto        Akiko        415       3 Silver Ct.  Walnut
  20106.                                            935-4228                Creek
  20107.  
  20108.  267-41-2394  O'Leary         Michael      408       22 Cleveland  San Jose
  20109.                                            286-2428  Av. #14
  20110.  
  20111.  472-27-2349  Gringlesby      Burt         707       PO Box 792    Covelo
  20112.                                            938-6445
  20113.  
  20114.  527-72-3246  Greene          Morningstar  615       22 Graybar    Nashville
  20115.                                            297-2723  House Rd.
  20116.  
  20117.  172-32-1176  White           Johnson      408       10932 Bigge   Menlo
  20118.                                            496-7223  Rd.           Park
  20119.  
  20120.  au_id        au_lname        au_fname     phone     address       city
  20121.  
  20122. 
  20123.  712-45-1867  del Castillo    Innes        615       2286 Cram Pl.  Ann Arbor
  20124.                                            996-8275  #86
  20125.  
  20126.  846-92-7186  Hunter          Sheryl       415       3410 Blonde   Palo Alto
  20127.                                            836-7128  St.
  20128.  
  20129.  486-29-1786  Locksley        Chastity     415       18 Broadway   San
  20130.                                            585-4620  Av.           Francisco
  20131.  
  20132.  648-92-1872  Blotchet-Halls  Reginald     503       55 Hillsdale  Corvallis
  20133.                                            745-6402  Bl.
  20134.  
  20135.  341-22-1782  Smith           Meander      913       10            Lawrence
  20136.                                            843-0462  Mississippi
  20137.                                                      Dr.
  20138.  
  20139.  ─────────────────────────────────────────────────────────────────────────────
  20140.  
  20141.  au_id        au_lname        au_fname     phone     address       city
  20142.  
  20143. 
  20144.  
  20145.  
  20146.  Table   titles ─ Structure
  20147.  
  20148. ╓┌─────────┌──────────┌────────────┌───────────┌────────┌──────┌──────┌─────┌
  20149.            title_id   title        type        pub_id   price  advan  roya  yt
  20150.                                                                ce     lty   sa
  20151.                                                                             s
  20152.  ─────────────────────────────────────────────────────────────────────────────
  20153.  Datatype  tid        varchar(80)  char(12)    char(4)  money  money  int   in
  20154.  
  20155.  Null      not null   not null     not null    null     null   null   null  nu
  20156.  
  20157.  Default   ─          ─            UNDECIDED1  ─        ─      ─      ─     ─
  20158.  
  20159.  Rule      ─          ─            ─           ─        ─      ─      ─     ─
  20160.  
  20161.  Trigger   deltitle3  ─            ─           ─        ─      ─      ─     ─
  20162.            title_id   title        type        pub_id   price  advan  roya  yt
  20163.                                                                ce     lty   sa
  20164.                                                                             s
  20165. Trigger   deltitle3  ─            ─           ─        ─      ─      ─     ─
  20166.  
  20167.  Index     clust,     nonclust     ─           ─        ─      ─      ─     ─
  20168.            uniq
  20169.  
  20170.  ─────────────────────────────────────────────────────────────────────────────
  20171.  
  20172.  
  20173.  
  20174.  (1) The default UNDECIDED is inserted if no data is entered in the column.
  20175.  (2) The getdate() function inserts the current date as the default if no
  20176.  data is entered in the column.
  20177.  (3) The deltitle trigger prohibits deleting a title if the title_id is
  20178.  listed in the sales table.
  20179.  
  20180.  
  20181.  Table   titles ─ Contents
  20182.  
  20183. ╓┌───────┌───────────────┌─────────────┌──────┌───────┌──────────┌──────┌────
  20184.  title_  title           type          pub_i  price   advance    royal  ytd_s
  20185.  id                                    d                         ty     ales
  20186.  ─────────────────────────────────────────────────────────────────────────────
  20187.  BU1032  The Busy        business      1389   $19.99  $5000.00   10     4095
  20188.          Executive's
  20189.          Database Guide
  20190.  
  20191.  
  20192.  
  20193.  
  20194.  PC1035  But Is It User  popular_comp  1389   $22.95  $7000.00   16     8780
  20195.          Friendly?
  20196.  
  20197.  
  20198.  
  20199.  
  20200.  BU2075  You Can Combat  business      0736   $2.99   $10125.00  24     18722
  20201.          Computer
  20202.          Stress!
  20203.  
  20204.  title_  title           type          pub_i  price   advance    royal  ytd_s
  20205.  id                                    d                         ty     ales
  20206.  ─────────────────────────────────────────────────────────────────────────────
  20207. 
  20208.  
  20209.  
  20210.  
  20211.  
  20212.  PS2091  Is Anger the    psychology    0736   10.95   $2275.00   12     2045
  20213.          Enemy?
  20214.  
  20215.  
  20216.  
  20217.  
  20218.  PS2106  Life Without    psychology    0736   $7.00   $6000.00   10     111
  20219.          Fear
  20220.  
  20221.  
  20222.  
  20223.  
  20224.  
  20225.  title_  title           type          pub_i  price   advance    royal  ytd_s
  20226.  id                                    d                         ty     ales
  20227.  ─────────────────────────────────────────────────────────────────────────────
  20228. 
  20229.  
  20230.  
  20231.  
  20232.  
  20233.  MC3021  The Gourmet     mod_cook      0877   $2.99   $15000.00  24     22246
  20234.          Microwave
  20235.  
  20236.  
  20237.  
  20238.  TC3218  Onions, Leeks,  trad_cook     0877   $20.95  $7000.00   10     375
  20239.          and Garlic:
  20240.          Cooking
  20241.          Secrets of the
  20242.          Mediterranean
  20243.  
  20244.  
  20245.  MC3026  The Psychology  UNDECIDED     0877   NULL    NULL       NULL   NULL
  20246.  title_  title           type          pub_i  price   advance    royal  ytd_s
  20247.  id                                    d                         ty     ales
  20248.  ─────────────────────────────────────────────────────────────────────────────
  20249. MC3026  The Psychology  UNDECIDED     0877   NULL    NULL       NULL   NULL
  20250.          of Computer
  20251.          Cooking
  20252.  
  20253.  PC8888  Secrets of      popular_comp  1389   $20.00  $8000.00   10     4095
  20254.          Silicon
  20255.          Valley
  20256.  
  20257.  
  20258.  
  20259.  
  20260.  PS7777  Emotional       psychology    0736   $7.99   $4000.00   10     3336
  20261.          Security:
  20262.          A New
  20263.          Algorithm
  20264.  
  20265.  
  20266.  
  20267.  title_  title           type          pub_i  price   advance    royal  ytd_s
  20268.  id                                    d                         ty     ales
  20269.  ─────────────────────────────────────────────────────────────────────────────
  20270. 
  20271.  
  20272.  
  20273.  PS3333  Prolonged Data  psychology    0736   $19.99  $2000.00   10     4072
  20274.          Deprivation:
  20275.          Four Case
  20276.          Studies
  20277.  
  20278.  
  20279.  
  20280.  
  20281.  BU1111  Cooking with    business      1389   $11.95  $5000.00   10     3876
  20282.          Computers:
  20283.          Surreptitious
  20284.          Balance Sheets
  20285.  
  20286.  MC2222  Silicon Valley  mod_cook      0877   $19.99  $0.00      12     2032
  20287.          Gastronomic
  20288.  title_  title           type          pub_i  price   advance    royal  ytd_s
  20289.  id                                    d                         ty     ales
  20290.  ─────────────────────────────────────────────────────────────────────────────
  20291.         Gastronomic
  20292.          Treats
  20293.  
  20294.  
  20295.  
  20296.  
  20297.  
  20298.  TC7777  Sushi, Anyone?  trad_cook     0877   $14.99  $8000.00   10     4095
  20299.  
  20300.  
  20301.  
  20302.  
  20303.  
  20304.  
  20305.  
  20306.  
  20307.  
  20308.  
  20309.  title_  title           type          pub_i  price   advance    royal  ytd_s
  20310.  id                                    d                         ty     ales
  20311.  ─────────────────────────────────────────────────────────────────────────────
  20312. 
  20313.  TC4203  Fifty Years in  trad_cook     0877   $11.95  $4000.00   14     15096
  20314.          Buckingham
  20315.          Palace
  20316.          Kitchens
  20317.  
  20318.  
  20319.  
  20320.  BU7832  Straight Talk   business      1389   $19.99  $5000.00   10     4095
  20321.          About
  20322.          Computers
  20323.  
  20324.  
  20325.  
  20326.  PS1372  Computer        psychology    0877   $21.59  $7000.00   10     375
  20327.          Phobic and
  20328.          Non-Phobic
  20329.          Individuals:
  20330.  title_  title           type          pub_i  price   advance    royal  ytd_s
  20331.  id                                    d                         ty     ales
  20332.  ─────────────────────────────────────────────────────────────────────────────
  20333.         Individuals:
  20334.          Behavior
  20335.          Variations
  20336.  
  20337.  
  20338.  
  20339.  PC9999  Net Etiquette   popular_comp  1389   NULL    NULL       NULL   NULL
  20340.  
  20341.  
  20342.  
  20343.  ─────────────────────────────────────────────────────────────────────────────
  20344.  
  20345.  
  20346.  
  20347.  The pubs database contains the following database objects:
  20348.  
  20349.  
  20350.  Rules
  20351.  
  20352.  
  20353.  pub_idrule
  20354.  
  20355.    create rule pub_idrule
  20356.     as @pub_id in ("1389", "0736", "0877", "1622", "1756")
  20357.     or @pub_id like "99[0-9][0-9]"
  20358.  
  20359.  
  20360.  ziprule
  20361.  
  20362.    create rule ziprule
  20363.     as @zip like "[0-9][0-9][0-9][0-9][0-9]"
  20364.  
  20365.  
  20366.  Trigger
  20367.  
  20368.  
  20369.  deltitle
  20370.  
  20371.    create trigger deltitle
  20372.     on titles
  20373.     for delete
  20374.     as
  20375.     if (select count(*) from deleted, sales
  20376.     where sales.title_id = deleted.title_id) > 0
  20377.      begin
  20378.      rollback transaction
  20379.      print "You can't delete a title with sales."
  20380.      end
  20381.  
  20382.  
  20383.  Stored Procedure
  20384.  
  20385.  
  20386.  byroyalty
  20387.  
  20388.    create procedure byroyalty percentage int
  20389.     as
  20390.     select au_id from titleauthor
  20391.     where titleauthor.royaltyper = percentage
  20392.  
  20393.  
  20394.  View
  20395.  
  20396.  
  20397.  titleview
  20398.  
  20399.    create view titleview
  20400.     as
  20401.     select title, au_ord, au_lname, price, ytd_sales, pub_id
  20402.     from authors, titles, titleauthor
  20403.     where authors.au_id = titleauthor.au_id
  20404.     and titles.title_id = titleauthor.title_id
  20405.  
  20406.  
  20407.  Defaults
  20408.  
  20409.  
  20410.  datedflt
  20411.  
  20412.    create default datedflt
  20413.     as getdate( )
  20414.  
  20415.  
  20416.  phonedflt
  20417.  
  20418.    create default phonedflt
  20419.     as "UNKNOWN"
  20420.  
  20421.  
  20422.  typedflt
  20423.  
  20424.    create default typedflt  as "UNDECIDED"
  20425.  
  20426.  Figure B.1 shows the structure of the pubs sample database.
  20427.  
  20428.  (This figure may be found in the printed book.)
  20429.  
  20430.  
  20431.  
  20432.  
  20433.  
  20434.  
  20435.  Appendix C  TRANSACT-SQL Keywords
  20436.  ────────────────────────────────────────────────────────────────────────────
  20437.  
  20438.  TRANSACT-SQL statements contain keywords listed in this appendix. Keywords
  20439.  cannot be used for the names of database objects, such as databases, tables,
  20440.  rules, defaults, and so on, but they can be used for the names of local
  20441.  variables and stored procedure parameter names.
  20442.  
  20443.  ADD                DESC                                  ROLLBACK
  20444.  ALL                DISK               KILL               ROWCOUNT
  20445.  ALTER              DISTINCT                              RULE
  20446.  AND                DROP               LIKE
  20447.  ANY                DUMMY              LINENO             SAVE
  20448.  AS                 DUMP               LOAD               SELECT
  20449.  ASC                                                      SET
  20450.  AVG                ELSE               MAX                SETUSER
  20451.                     END                MIN                STATISTICS
  20452.  BEGIN              ERRLVL                                SUM
  20453.  BETWEEN            ERROREXIT, EXEC    NONCLUSTERED
  20454.  BREAK              EXECUTE            NOT                TABLE
  20455.  BROWSE             EXISTS             NULL               TAPE
  20456.  BULK               EXIT                                  TEXTSIZE
  20457.  BY                                    OFF                TO
  20458.                     FILLFACTOR         OFFSETS            TRAN
  20459.  CHECKPOINT         FOR                ON                 TRANSACTION
  20460.  CLUSTERED          FROM               ONCE               TRIGGER
  20461.  COMMIT                                OR                 TRUNCATE
  20462.  COMPUTE            GETDEFAULT         ORDER
  20463.  CONFIRM            GOTO               OVER, PREPARE      UNIQUE
  20464.  CONTINUE           GRANT              PRINT              UPDATE
  20465.  CONTROLROW         GROUP              PROC               USE
  20466.  CONVERT                               PROCEDURE
  20467.  COUNT              HAVING             PROCESSEXIT        VALUES
  20468.  CREATE             HOLDLOCK           PUBLIC             VIEW
  20469.  
  20470.  DATABASE           IF                 RAISERROR          WAITFOR
  20471.  DBCC               IN                 READTEXT           WHERE
  20472.  DEBUG              INDEX              RECONFIGURE        WHILE
  20473.  DECLARE            INSERT             RETURN             WITH
  20474.  DEFAULT            INTO               REVOKE             WRITETEXT
  20475.  DELETE             IS
  20476.  
  20477.  
  20478.  
  20479.  
  20480.  INDEX
  20481.  ──────────────────────────────────────────────────────────────────────────
  20482.  
  20483.  
  20484.  
  20485.  + string function
  20486.  /*
  20487.  */
  20488.  **
  20489.  
  20490.      A
  20491.  ABS mathematical function
  20492.  ACOS mathematical function
  20493.  Aggregate functions
  20494.    AVG
  20495.    COUNT
  20496.    COUNT(*)
  20497.    MAX
  20498.    MIN
  20499.    SUM
  20500.  Alias
  20501.    add
  20502.    remove
  20503.  ALTER DATABASE
  20504.  ALTER TABLE
  20505.  ASCII string function
  20506.  ASIN mathematical function
  20507.  ATAN mathematical function
  20508.  ATN2 mathematical function
  20509.  AVG aggregate function
  20510.  
  20511.      B
  20512.  Batch queries
  20513.  bcp utility program
  20514.  BEGIN TRANSACTION
  20515.  BEGIN...END
  20516.  binary datatype
  20517.  bit datatype
  20518.  bldmastr utility program
  20519.  Boolean expression
  20520.  BREAK
  20521.  Browse mode
  20522.  
  20523.      C
  20524.  Case sensitivity
  20525.  CEILING mathematical function
  20526.  char datatype
  20527.  CHAR string function
  20528.  Characters
  20529.    wildcard
  20530.  CHARINDEX string function
  20531.  CHECKPOINT
  20532.  Clauses
  20533.    COMPUTE
  20534.    FOR BROWSE
  20535.    GROUP BY
  20536.    HAVING
  20537.    ORDER BY
  20538.    WHERE
  20539.  Clustered index
  20540.  Column
  20541.    add
  20542.    bind default value to
  20543.    bind rule to
  20544.    unbind default value from
  20545.    unbind rule from
  20546.  COL_LENGTH system function
  20547.  COL_NAME system function
  20548.  Comments
  20549.  COMMIT TRANSACTION
  20550.  Common key
  20551.  COMPUTE clause
  20552.  Configuration variable
  20553.    display or change
  20554.  console utility program
  20555.  CONTINUE
  20556.  Control-of-flow language
  20557.  Conversion function
  20558.  COS mathematical function
  20559.  COT mathematical function
  20560.  COUNT aggregate function
  20561.  COUNT(*) aggregate function
  20562.  CREATE DATABASE
  20563.  CREATE DEFAULT
  20564.  CREATE INDEX
  20565.  CREATE PROCEDURE
  20566.  CREATE RULE
  20567.  CREATE TABLE
  20568.  CREATE TRIGGER
  20569.  CREATE VIEW
  20570.  
  20571.      D
  20572.  Data
  20573.    change
  20574.  Database device
  20575.    help
  20576.    remove
  20577.    set defaults
  20578.    storage space for
  20579.  Database object
  20580.    help
  20581.    rename
  20582.    space used by
  20583.  Database options
  20584.    dbo use only
  20585.    no chkpt on recovery
  20586.    read only
  20587.    select into/bulkcopy
  20588.    single user
  20589.    trunc. log on chkpt.
  20590.  Database Owner
  20591.    change
  20592.  Database
  20593.    add group
  20594.    add new user
  20595.    back up
  20596.    change current
  20597.    change default
  20598.    change owner
  20599.    copy definitions to and from file
  20600.    create
  20601.    default size
  20602.    display object dependency
  20603.    display or change options
  20604.    dump
  20605.    help
  20606.    load
  20607.    master
  20608.    model
  20609.    pubs
  20610.    remove group from
  20611.    remove user
  20612.    remove
  20613.    rename
  20614.    restore damaged
  20615.    sample
  20616.    select row
  20617.    size
  20618.    space on disk
  20619.  DATALENGTH string function
  20620.  Datatype
  20621.    bit
  20622.    char
  20623.  Datatypes
  20624.    binary
  20625.    bind default
  20626.    bind rule
  20627.    create user-defined
  20628.    datetime
  20629.    float
  20630.    help
  20631.    image
  20632.    int
  20633.    money
  20634.    remove user-defined
  20635.    smallint
  20636.    sysname
  20637.    text
  20638.    text/image
  20639.    timestamp
  20640.    tinyint
  20641.    unbind default value from
  20642.    unbind rule from
  20643.    user-defined
  20644.    user_type_name
  20645.    varbinary
  20646.    varchar
  20647.  Date functions
  20648.    DATEDIFF
  20649.    DATENAME
  20650.    DATEPART
  20651.    GETDATE
  20652.  DATEDIFF date function
  20653.  DATENAME date function
  20654.  DATEPART date function
  20655.  datetime datatype
  20656.  DB-LIBRARY
  20657.  DBCC (Database Consistency Checker)
  20658.  DBCC (database consistency checker)
  20659.  dbo use only database option
  20660.  DB_ID system function
  20661.  DB_NAME system function
  20662.  DECLARE
  20663.  Default database
  20664.    change
  20665.  Default value
  20666.    bind to column or datatype
  20667.    create
  20668.    help
  20669.    remove
  20670.    unbind from column or datatype
  20671.  defncopy utility program
  20672.  DEGREES mathematical function
  20673.  DELETE
  20674.  DIFFERENCE string function
  20675.  DISK INIT
  20676.  DISK REFIT
  20677.  DISK REINIT
  20678.  DROP DATABASE
  20679.  DROP DEFAULT
  20680.  DROP INDEX
  20681.  DROP PROCEDURE
  20682.  DROP RULE
  20683.  DROP TABLE
  20684.  DROP TRIGGER
  20685.  DROP VIEW
  20686.  DUMP DATABASE
  20687.  Dump database
  20688.  Dump device
  20689.    add
  20690.    help
  20691.    remove
  20692.  DUMP TRANSACTION
  20693.  
  20694.      E
  20695.  ELSE
  20696.  END
  20697.  Error message
  20698.  EXECUTE
  20699.  Exit
  20700.    from query or procedure
  20701.  EXP mathematical function
  20702.  Expressions
  20703.    Boolean
  20704.  
  20705.      F
  20706.  float datatype
  20707.  FLOOR mathematical function
  20708.  FOR BROWSE clause
  20709.  Foreign key
  20710.  Functions
  20711.    aggregate
  20712.    conversion
  20713.    date
  20714.    mathematical
  20715.    row aggregate
  20716.    scalar aggregate
  20717.    string
  20718.    system
  20719.    text/image
  20720.    vector aggregate
  20721.  
  20722.      G
  20723.  GETDATE date function
  20724.  Global variables
  20725.  
  20726.      @CPU_BUSY
  20727.      @ERROR
  20728.      @IDLE
  20729.      @IO_BUSY
  20730.      @MAX_CONNECTIONS
  20731.      @NESTLEVEL
  20732.      @PACKET_ERRORS
  20733.      @PACK_RECEIVED
  20734.      @PACK_SENT
  20735.      @PROCID
  20736.      @ROWCOUNT
  20737.      @TEXTSIZE
  20738.      @TIMETICKS
  20739.      @TOTAL_ERRORS
  20740.      @TOTAL_READ
  20741.      @TOTAL_WRITE
  20742.      @TRANCOUNT
  20743.      @VERSION
  20744.  go command
  20745.  GOTO label
  20746.  GRANT
  20747.  GROUP BY and HAVING clauses
  20748.  Group
  20749.    add to database
  20750.    change user
  20751.    help
  20752.    remove from database
  20753.  
  20754.      H
  20755.  Help
  20756.    database device
  20757.    database objects
  20758.    databases
  20759.    datatypes
  20760.    default text
  20761.    dump device
  20762.    group
  20763.    index
  20764.    joins
  20765.    key
  20766.    permissions
  20767.    rule text
  20768.    statements
  20769.    stored procedure text
  20770.    syntax
  20771.    trigger text
  20772.    user
  20773.    view text
  20774.  HOST_ID system function
  20775.  HOST_NAME system function
  20776.  
  20777.      I
  20778.  Identifiers
  20779.  IF...ELSE
  20780.  image datatype
  20781.  Image functions
  20782.    PATINDEX
  20783.    SET TEXTSIZE
  20784.    TEXTPTR
  20785.    TEXTVALID
  20786.  Index
  20787.    clustered
  20788.    create
  20789.    help
  20790.    nonclustered
  20791.    remove
  20792.  INDEX_COL system function
  20793.  INSERT
  20794.  int datatype
  20795.  ISNULL system function
  20796.  isql utility program
  20797.  Isql utility program
  20798.  
  20799.      J
  20800.  Joins
  20801.    help
  20802.  
  20803.      K
  20804.  Key
  20805.    common
  20806.    foreign
  20807.    help
  20808.    primary
  20809.    remove
  20810.  Keywords
  20811.  KILL
  20812.  
  20813.      L
  20814.  Label
  20815.    user-defined
  20816.  LOAD DATABASE
  20817.  Load database
  20818.  LOAD TRANSACTION
  20819.  Local variables
  20820.  Locks
  20821.    report on
  20822.  LOG mathematical function
  20823.  LOG10 mathematical function
  20824.  Login authorization
  20825.    add
  20826.    remove
  20827.  LOWER string function
  20828.  LTRIM string function
  20829.  
  20830.      M
  20831.  master database
  20832.  Mathematical functions
  20833.    ABS
  20834.    ACOS
  20835.    ASIN
  20836.    ATAN
  20837.    ATN2
  20838.    CEILING
  20839.    COS
  20840.    COT
  20841.    DEGREES
  20842.    EXP
  20843.    FLOOR
  20844.    LOG
  20845.    LOG10
  20846.    PI
  20847.    POWER
  20848.    RADIANS
  20849.    RAND
  20850.    ROUND
  20851.    SIGN
  20852.    SIN
  20853.    SQRT
  20854.    TAN
  20855.  MAX aggregate function
  20856.  Message
  20857.    print on screen
  20858.  MIN aggregate function
  20859.  model database
  20860.  money datatype
  20861.  
  20862.      N
  20863.  Nest statements
  20864.  no chkpt on recovery database option
  20865.  Nonclustered index
  20866.  Notational conventions
  20867.  Null values
  20868.  
  20869.      O
  20870.  OBJECT_ID system function
  20871.  OBJECT_NAME system function
  20872.  Operating system file
  20873.    copy definitions to and from database
  20874.  Option
  20875.    display or change database
  20876.    set
  20877.  ORDER BY clause
  20878.  
  20879.      P
  20880.  Parameters
  20881.  Password
  20882.    add or change
  20883.  PATINDEX text/image function
  20884.  Permissions
  20885.    assign
  20886.    help
  20887.    revoke
  20888.  PI mathematical function
  20889.  POWER mathematical function
  20890.  Primary key
  20891.  PRINT
  20892.  Procedure
  20893.    create
  20894.    exit
  20895.    remove
  20896.    stored
  20897.    system
  20898.    system, enter with isql
  20899.  Process
  20900.    current
  20901.  pubs sample database
  20902.  
  20903.      Q
  20904.  Query
  20905.    batch
  20906.  
  20907.      R
  20908.  RADIANS mathematical function
  20909.  RAISERROR
  20910.  RAND mathematical function
  20911.  read only database option
  20912.  READTEXT
  20913.  RECONFIGURE
  20914.  Rename
  20915.    database object
  20916.    database
  20917.    table
  20918.  REPLICATE string function
  20919.  Reserved words
  20920.  RETURN
  20921.  REVOKE
  20922.  RIGHT string function
  20923.  ROLLBACK TRANSACTION
  20924.  ROUND mathematical function
  20925.  Row aggregate functions
  20926.  Row
  20927.    delete from table
  20928.    insert to table or view
  20929.    remove
  20930.    retrieve from database
  20931.  RTRIM string function
  20932.  Rule
  20933.    bind to column or datatype
  20934.    create
  20935.    help
  20936.    remove
  20937.    unbind from column or datatype
  20938.  
  20939.      S
  20940.  SAF
  20941.  Sample database, pubs
  20942.  SAVE TRANSACTION
  20943.  Savepoint
  20944.  Scalar aggregate function
  20945.  Search conditions
  20946.  select into/bulkcopy database option
  20947.  SELECT
  20948.  Set options
  20949.  SET TEXTSIZE text/image function
  20950.  SET
  20951.  Setup program
  20952.  SETUSER
  20953.  Shutdown system
  20954.  SHUTDOWN
  20955.  SIGN mathematical function
  20956.  SIN mathematical function
  20957.  single user database option
  20958.  smallint datatype
  20959.  SOUNDEX string function
  20960.  SPACE string function
  20961.  Space used by database object
  20962.  sp_addalias
  20963.  sp_addgroup
  20964.  sp_addlogin
  20965.  sp_addtype
  20966.  sp_addumpdevice
  20967.  sp_adduser
  20968.  sp_bindefault
  20969.  sp_bindrule
  20970.  sp_changedbowner
  20971.  sp_changegroup
  20972.  sp_commonkey
  20973.  sp_configure
  20974.  sp_dboption
  20975.  sp_defaultdb
  20976.  sp_depends
  20977.  sp_diskdefault
  20978.  sp_dropalias
  20979.  sp_dropdevice
  20980.  sp_dropgroup
  20981.  sp_dropkey
  20982.  sp_droplogin
  20983.  sp_droptype
  20984.  sp_dropuser
  20985.  sp_foreignkey
  20986.  sp_help
  20987.  sp_helpdb
  20988.  sp_helpdevice
  20989.  sp_helpgroup
  20990.  sp_helpindex
  20991.  sp_helpjoins
  20992.  sp_helpkey
  20993.  sp_helprotect
  20994.  sp_helpsql
  20995.  sp_helptext
  20996.  sp_helpuser
  20997.  sp_lock
  20998.  sp_logdevice
  20999.  sp_monitor
  21000.  sp_password
  21001.  sp_primarykey
  21002.  sp_rename
  21003.  sp_renamedb
  21004.  sp_spaceused
  21005.  sp_unbindefault
  21006.  sp_unbindrule
  21007.  sp_who
  21008.  SQL Server Administration Facility (SAF)
  21009.  SQL Server
  21010.    shutdown system
  21011.    start
  21012.  sqlservr utility program
  21013.  Sqlservr utility program
  21014.  SQRT mathematical function
  21015.  Statements
  21016.    ALTER DATABASE
  21017.    ALTER TABLE
  21018.    BEGIN TRANSACTION
  21019.    BEGIN
  21020.    BREAK
  21021.    CHECKPOINT
  21022.    COMMIT TRANSACTION
  21023.    CONTINUE
  21024.    CREATE DATABASE
  21025.    CREATE DEFAULT
  21026.    CREATE INDEX
  21027.    CREATE PROCEDURE
  21028.    CREATE RULE
  21029.    CREATE TABLE
  21030.    CREATE TRIGGER
  21031.    CREATE VIEW
  21032.    DBCC
  21033.    DECLARE
  21034.    DELETE
  21035.    DISK INIT
  21036.    DISK REFIT
  21037.    DISK REINIT
  21038.    DROP DATABASE
  21039.    DROP DEFAULT
  21040.    DROP INDEX
  21041.    DROP PROCEDURE
  21042.    DROP RULE
  21043.    DROP TABLE
  21044.    DROP TRIGGER
  21045.    DROP VIEW
  21046.    DUMP DATABASE
  21047.    DUMP TRANSACTION
  21048.    ELSE
  21049.    END
  21050.    enter with isql
  21051.    EXECUTE
  21052.    GOTO
  21053.    GRANT
  21054.    help
  21055.    IF
  21056.    IF...ELSE
  21057.    INSERT
  21058.    KILL
  21059.    LOAD DATABASE
  21060.    LOAD TRANSACTION
  21061.    nest
  21062.    PRINT
  21063.    RAISERROR
  21064.    READTEXT
  21065.    RECONFIGURE
  21066.    RETURN
  21067.    REVOKE
  21068.    ROLLBACK TRANSACTION
  21069.    SAVE TRANSACTION
  21070.    SELECT
  21071.    SET
  21072.    SETUSER
  21073.    SHUTDOWN
  21074.    TRUNCATE TABLE
  21075.    UPDATE STATISTICS
  21076.    UPDATE
  21077.    USE
  21078.    WAITFOR
  21079.    WHILE
  21080.    WRITETEXT
  21081.  Statistics
  21082.    display
  21083.    update
  21084.  Stored procedure
  21085.    execute
  21086.    help
  21087.    remove
  21088.  STR string function
  21089.  String functions
  21090.    +
  21091.    CHAR
  21092.    CHARINDEX
  21093.    DATALENGTH
  21094.    DIFFERENCE
  21095.    LOWER
  21096.    LTRIM
  21097.    REPLICATE
  21098.    RIGHT
  21099.    RTRIM
  21100.    SOUNDEX
  21101.    SPACE
  21102.    STR
  21103.    STUFF
  21104.    SUBSTRING
  21105.    UPPER
  21106.  STUFF string function
  21107.  Subqueries
  21108.  SUBSTRING string function
  21109.  SUM aggregate function
  21110.  Summary value
  21111.  SUSER_ID system function
  21112.  SUSER_NAME system function
  21113.  Syntax
  21114.    help
  21115.  sysalternates
  21116.  syscolumns
  21117.  syscomments
  21118.  sysconfigures
  21119.  syscurconfigs
  21120.  sysdatabases
  21121.  sysdepends
  21122.  sysdevices
  21123.  sysindexes
  21124.  syskeys
  21125.  syslocks
  21126.  syslogins
  21127.  syslogs
  21128.  sysmessages
  21129.  sysname datatype
  21130.  sysobjects
  21131.  sysprocedures
  21132.  sysprocesses system
  21133.  sysprotects
  21134.  syssegments
  21135.  System functions
  21136.    COL_LENGTH
  21137.    COL_NAME
  21138.    DB_ID
  21139.    DB_NAME
  21140.    HOST_ID
  21141.    HOST_NAME
  21142.    INDEX_COL
  21143.    ISNULL
  21144.    OBJECT_ID
  21145.    OBJECT_NAME
  21146.    SUSER_ID
  21147.    SUSER_NAME
  21148.    USER_ID
  21149.    USER_NAME
  21150.  System procedures
  21151.    enter with isql
  21152.    execute
  21153.    list
  21154.    sp_
  21155.    sp_addalias
  21156.    sp_addgroup
  21157.    sp_addlogin
  21158.    sp_addtype
  21159.    sp_addumpdevice
  21160.    sp_adduser
  21161.    sp_bindefault
  21162.    sp_bindrule
  21163.    sp_changedbowner
  21164.    sp_changegroup
  21165.    sp_commonkey
  21166.    sp_configure
  21167.    sp_dboption
  21168.    sp_defaultdb
  21169.    sp_depends
  21170.    sp_diskdefault
  21171.    sp_dropalias
  21172.    sp_dropdevice
  21173.    sp_dropgroup
  21174.    sp_dropkey
  21175.    sp_droplogin
  21176.    sp_droptype
  21177.    sp_dropuser
  21178.    sp_foreignkey
  21179.    sp_help
  21180.    sp_helpdb
  21181.    sp_helpdevice
  21182.    sp_helpgroup
  21183.    sp_helpindex
  21184.    sp_helpjoins
  21185.    sp_helpkey
  21186.    sp_helprotect
  21187.    sp_helpsql
  21188.    sp_helptext
  21189.    sp_helpuser
  21190.    sp_lock
  21191.    sp_logdevice
  21192.    sp_monitor
  21193.    sp_password
  21194.    sp_primarykey
  21195.    sp_rename
  21196.    sp_renamedb
  21197.    sp_spaceused
  21198.    sp_unbindefault
  21199.    sp_unbindrule
  21200.    sp_who
  21201.  System tables
  21202.    list
  21203.    relationship chart
  21204.    syscolumns
  21205.    syscomments
  21206.    sysconfigures
  21207.    syscurconfigs
  21208.    sysdatabases
  21209.    sysdepends
  21210.    sysdevices
  21211.    sysindexes
  21212.    syskeys
  21213.    syslaternates
  21214.    syslocks
  21215.    syslogins
  21216.    syslogs
  21217.    sysmessages
  21218.    sysobjects
  21219.    sysprocedures
  21220.    sysprocesses
  21221.    sysprotects
  21222.    syssegments
  21223.    systypes
  21224.    sysusages
  21225.    sysusers
  21226.  System, shutdown
  21227.  systypes
  21228.  sysusages
  21229.  sysusers
  21230.  
  21231.      T
  21232.  Table
  21233.    add columns
  21234.    browsing
  21235.    copy to and from operating system file
  21236.    create
  21237.    define common key
  21238.    define foreign key
  21239.    define primary key
  21240.    delete row from
  21241.    divide into groups
  21242.    remove rows
  21243.    remove
  21244.    rename
  21245.    truncate
  21246.    view
  21247.  TAN mathematical function
  21248.  text datatype
  21249.  Text functions
  21250.    PATINDEX
  21251.    SET TEXTSIZE
  21252.    TEXTPTR
  21253.    TEXTVALID
  21254.  Text
  21255.    read from database
  21256.    write into database
  21257.  TEXTPTR text/image function
  21258.  TEXTVALID text/image function
  21259.  timestamp datatype
  21260.  tinyint datatype
  21261.  TRANSACT-SQL
  21262.  Transaction log
  21263.    back up
  21264.    dump
  21265.    load
  21266.    place in separate database device
  21267.    size of database device required
  21268.    storage space
  21269.  Transaction
  21270.    begin
  21271.    end
  21272.    rollback
  21273.    save
  21274.  Trigger
  21275.    create
  21276.    help
  21277.    remove
  21278.  trunc. log on chkpt. database option
  21279.  TRUNCATE TABLE
  21280.  
  21281.      U
  21282.  UPDATE STATISTICS
  21283.  UPDATE
  21284.  UPPER string function
  21285.  USE
  21286.  User
  21287.    add login authorization
  21288.    add new
  21289.    change group
  21290.    current
  21291.    help
  21292.    impersonate another
  21293.    remove from database
  21294.    remove login authorization
  21295.  USER_ID system function
  21296.  USER_NAME system function
  21297.  user_type_name datatype
  21298.  Utility programs
  21299.    bcp
  21300.    bldmastr
  21301.    console
  21302.    defncopy
  21303.    isql
  21304.    sqlservr
  21305.  
  21306.      V
  21307.  Value
  21308.    null
  21309.    summary
  21310.  varbinary datatype
  21311.  varchar datatype
  21312.  Variables
  21313.    global. See Global variables
  21314.    local
  21315.  Vector aggregate function
  21316.  Views
  21317.    create
  21318.    define common key
  21319.    define foreign key
  21320.    define primary key
  21321.    remove
  21322.  
  21323.      W
  21324.  WAITFOR
  21325.  WHERE clause
  21326.  WHILE
  21327.  Wildcard characters
  21328.  WRITETEXT
  21329.  
  21330.