3 4
Numerous additions and enhancements were made to T-SQL in Microsoft SQL Server 7, including new stored procedures, system tables, functions, data types, statements, and options for existing statements. These remain the same in SQL Server 2000, so we will review them here (in case you are not yet familiar with them from SQL Server 7). There are far too many features to describe them all in detail here, however, so we'll look at just a few examples in each category.
MORE INFO
For a complete list of features, refer to the topic "New Features in Transact-SQL" in SQL Server Books Online. To display this topic, on the Contents tab, click Transact-SQL Reference, and then click New Features In Transact-SQL.
System stored procedures are provided by SQL Server to perform administrative and other tasks that involve updating system tables and to retrieve information from system tables. System stored procedures are installed with SQL Server; their names begin with sp_ (for "stored procedure") or xp_ (for "extended stored procedure"). These procedures are stored in the master database and are owned by the system administrator, but many of them can be executed from any user-defined database to retrieve information from the system tables in that particular database. When you execute a system stored procedure, it operates on the system tables in the current database.
MORE INFO
See the topic "Extended Stored Procedures" in Books Online for more information about this type of procedure.
Many system stored procedures were added in SQL Server 7, and they are now available in SQL Server 2000 as well. Table 13-1 describes a few of these system stored procedures that you might find helpful.
Table 13-1. Procedures introduced in SQL Server 7 and included in SQL Server 2000
System stored procedure | Description |
---|---|
sp_cycle_errorlog | Closes the current error log file, renames it errorlog.1 (and, if necessary, renames the old errorlog.1 as errorlog.2, and so on), and starts a new error log file |
sp_helpfile | Returns the physical names and attributes of the files associated with the current database |
sp_helpfilegroup | Returns the names and attributes of the filegroups associated with the current database |
sp_helprole | Returns information about the roles in the current database |
sp_help_alert | Reports information about the alerts defined for the server |
sp_start_job | Instructs the SQL Server Agent to begin execution of a job |
Not only do some of these stored procedures provide immediate information, but they can also be used to save important information about your user databases for later use. For example, the procedures that return information about a user database might prove useful when they are run as T-SQL scripts and the output is saved to a file. You could run and save the output from sp_helpfile, sp_helpfilegroup, and sp_helpdb (this latter stored procedure is an oldie) in a particular database, in case you ever need to rebuild that database and want to know how the files, filegroups, and database options were originally created and configured. A listing of the remainder of the new system stored procedures (introduced in SQL Server 7) can be found in the "New Features in Transact-SQL" topic in Books Online.
System tables are used to store SQL Server configuration information and definitions of objects, users, and permissions for all databases. Each user database has its own system tables, which hold information for that database. System tables that hold server-level configuration information are found in only the master database. You should use system stored procedures to access system tables rather than access the tables directly. A list of the new system tables first featured in SQL Server 7 can be found in the "New Features in Transact-SQL" topic in Books Online. Some interesting new system tables include the following:
CAUTION
Always use system stored procedures to access system tables. The system stored procedures provide a layer of insulation that can keep you from altering data you should not alter. If you access system tables by hand, you run the risk of rendering your database useless by inadvertently altering important system information.
SQL Server's built-in functions provide a quick and easy way to accomplish certain tasks. Several new T-SQL functions were made available in SQL Server 7 and are included in SQL Server 2000. Knowing which functions are available to you can make SQL Server application programming a little easier. A complete list of the new functions can be found in the "New Transact-SQL Functions" topic in Books Online. Here are just a few new functions that you might find useful:
Several new data types were added with SQL Server 7, and a size extension was added to some existing data types. In addition to including these changes, three more new data types were added for SQL Server 2000. Most of these data types were discussed in Chapter 10. Here is a list of the data type changes in SQL Server 7 that are also included in SQL Server 2000:
And the three new data types for SQL Server 2000 are as follows:
SQL Server 7 included many new T-SQL statements and new options added to existing statements, which, again, are also part of SQL Server 2000. These statements correspond to some of the features introduced in SQL Server 7. For example, the ALTER DATABASE statement includes the following new options for files and filegroups: MODIFY FILE, ADD FILEGROUP, MODIFY FILEGROUP, REMOVE FILE, and REMOVE FILEGROUP. Also related to filegroups, the new statement DBCC CHECKFILEGROUP checks the allocation and structural integrity of all tables in a given filegroup.
SQL Server 7 and SQL Server 2000 include two additional DBCC statements: DBCC SHRINKFILE and DBCC SHRINKDATABASE. The former shrinks the size of a given data file, and the latter shrinks all data files in a given database, freeing up unused disk space.
SQL Server 7 and SQL Server 2000 have an improved backup-and-restore architecture. The new BACKUP statement allows full or partial database backups and log backups, and the new RESTORE statement allows full or partial database backups and log backups to be restored. These take the place of the DUMP and LOAD statements from previous versions of SQL Server. For a complete list of the new statements and options available for SQL Server 7 and SQL Server 2000, see the "New Features in Transact-SQL" topic in Books Online. See Chapters 32 and 33 for details about backing up and restoring databases.