reference_manual

1266
Sybase SQL Server™ Reference Manual Volume 1: Commands, Functions, and Topics Sybase SQL Server Release 11.0.x Document ID: 32401-01-1100-03 Last Revised: January 24, 1996

Upload: api-3772730

Post on 10-Apr-2015

215 views

Category:

Documents


5 download

TRANSCRIPT

Sybase SQL Server Reference Manual Volume 1: Commands, Functions, and Topics

Sybase SQL Server Release 11.0.x Document ID: 32401-01-1100-03 Last Revised: January 24, 1996

Principal author: Server Publications Group Document ID: 32401-01-1100 This publication pertains to Sybase SQL Server Release 11.0.x of the Sybase database management software and to any subsequent release until otherwise indicated in new editions or technical notes. Information in this document is subject to change without notice. The software described herein is furnished under a license agreement, and it may be used or copied only in accordance with the terms of that agreement.

Document OrdersTo order additional documents, U.S. and Canadian customers should call Customer Fulllment at (800) 685-8225, fax (617) 229-9845. Customers in other countries with a U.S. license agreement may contact Customer Fulllment via the above fax number. All other international customers should contact their Sybase subsidiary or local distributor. Upgrades are provided only at regularly scheduled software release dates. Copyright 19891995 by Sybase, Inc. All rights reserved. No part of this publication may be reproduced, transmitted, or translated in any form or by any means, electronic, mechanical, manual, optical, or otherwise, without the prior written permission of Sybase, Inc.

Sybase TrademarksAPT-FORMS, Data Workbench, DBA Companion, Deft, GainExposure, Gain Momentum, Navigation Server, PowerBuilder, Powersoft, Replication Server, SA Companion, SQL Advantage, SQL Debug, SQL Monitor, SQL SMART, SQL Solutions, SQR, SYBASE, the Sybase logo, Transact-SQL, and VQL are registered trademarks of Sybase, Inc. Adaptable Windowing Environment, ADA Workbench, AnswerBase, Application Manager, APT-Build, APT-Edit, APT-Execute, APT-Library, APT-Translator, APT Workbench, Backup Server, Bit-Wise, Client-Library, Client/Server Architecture for the Online Enterprise, Client/Server for the Real World, Client Services, Congurator, Connection Manager, Database Analyzer, DBA Companion Application Manager, DBA Companion Resource Manager, DB-Library, Deft Analyst, Deft Designer, Deft Educational, Deft Professional, Deft Trial, Developers Workbench, DirectCONNECT, Easy SQR, Embedded SQL, EMS, Enterprise Builder, Enterprise Client/Server, Enterprise CONNECT, Enterprise Manager, Enterprise SQL Server Manager, Enterprise Work Architecture, Enterprise Work Designer, Enterprise Work Modeler, EWA, Gain Interplay, Gateway Manager, InfoMaker, Interactive Quality Accelerator, Intermedia Server, IQ Accelerator, Maintenance Express, MAP, MDI, MDI Access Server, MDI Database Gateway, MethodSet, Movedb, Navigation Server Manager, Net-Gateway, Net-Library, New Media Studio, OmniCONNECT, OmniSQL Access Module, OmniSQL Gateway, OmniSQL Server, OmniSQL Toolkit, Open Client, Open Client/Server, Open Client/Server Interfaces, Open Gateway, Open Server, Open Solutions, PC APT-Execute,

PC DB-Net, PC Net Library, Powersoft Portfolio, Replication Agent, Replication Driver, Replication Server Manager, Report-Execute, Report Workbench, Resource Manager, RW-DisplayLib, RW-Library, SAFE, SDF, Secure SQL Server, Secure SQL Toolset, SKILS, SQL Anywhere, SQL Code Checker, SQL Edit, SQL Edit/TPU, SQL Server, SQL Server/CFT, SQL Server/DBM, SQL Server Manager, SQL Server Monitor, SQL Station, SQL Toolset, SQR Developers Kit, SQR Execute, SQR Toolkit, SQR Workbench, Sybase Client/Server Interfaces, Sybase Gateways, Sybase Intermedia, Sybase Interplay, Sybase IQ, Sybase MPP, Sybase SQL Desktop, Sybase SQL Lifecycle, Sybase SQL Workgroup, Sybase Synergy Program, Sybase Virtual Server Architecture, Sybase User Workbench, SyBooks, System 10, System 11, the System XI logo, Tabular Data Stream, The Enterprise Client/Server Company, The Online Information Center, Warehouse WORKS, Watcom SQL, WebSights, WorkGroup SQL Server, XA-Library, and XA-Server are trademarks of Sybase, Inc. All other company and product names used herein may be trademarks or registered trademarks of their respective companies.

Restricted RightsUse, duplication, or disclosure by the government is subject to the restrictions set forth in subparagraph (c)(1)(ii) of DFARS 52.227-7013 for the DOD and as set forth in FAR 52.227-19(a)-(d) for civilian agencies. Sybase, Inc., 6475 Christie Avenue, Emeryville, CA 94608.

Table of ContentsAudience. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii How to Use This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii Related Documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xviii Conventions Used in This Manual . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix If You Need Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxii

1. SQL Server RoadmapStarting, Stopping, and Conguring SQL Server. . . . . . . . . . . . . . . . . . . . . . . . . . 1-3 Creating and Maintaining Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-9 Setting Database-Wide Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-12 Backing Up and Restoring Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-14 Managing Logins, Users, Groups, and Roles . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-17 Dening and Using Datatypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-21 Creating and Maintaining Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-23 Auditing Server Activity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-31 Retrieving and Manipulating Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-33 Using Defaults and Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-38 Error Handling and Messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-40 Using Global Variables to Get Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-42 Using System and Catalog Stored Procedures to Get Help . . . . . . . . . . . . . . . . . . 1-45 Managing Languages, Character Sets, and Sort Orders . . . . . . . . . . . . . . . . . . . . 1-49 Managing Database Devices, Segments, and Thresholds . . . . . . . . . . . . . . . . . . . 1-52 Setting Session-Wide Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-56 Using Stored Procedures and Triggers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-59 Managing Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-64 Creating and Using Views. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-66

SQL Server Reference Manual

v

Sybase SQL Server Release 11.0.x

2. System and User-Dened DatatypesFunction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-1 Datatype Categories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-1 Range and Storage Size . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-2 Declaring the Datatype of a Column, Variable, or Parameter . . . . . . . . . . . . . . . . . . . 2-3 Datatype of Mixed-Mode Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-5 Converting One Datatype to Another . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-7 Standards and Compliance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-9 Exact Numeric Datatypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-10 Approximate Numeric Datatypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-14 Money Datatypes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-16 timestamp Datatype. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-18 Date/time Datatypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-20 Character Datatypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-25 Binary Datatypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-29 bit Datatype . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-32 sysname Datatype . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-33 text and image Datatypes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-34 User-Dened Datatypes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-40

3. Transact-SQL Commandsalter database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-6 alter table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-10 begin...end . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-21 begin transaction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-23 break . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-24 checkpoint. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-26 close . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-29 commit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-30 compute Clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-32 continue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-41 create database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-43 create default . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-48 create index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-51 create procedure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-59 create rule. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-70 create schema . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-74 create table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-76

vi

Sybase SQL Server Release 11.0.x

create trigger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-96 create view . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-106 dbcc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-114 deallocate cursor. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-120 declare . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-121 declare cursor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-123 delete . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-129 disk init . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-135 disk mirror . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-139 disk ret . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-143 disk reinit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-144 disk remirror. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-146 disk unmirror . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-149 drop database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-152 drop default . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-154 drop index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-156 drop procedure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-158 drop rule . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-160 drop table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-161 drop trigger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-164 drop view . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-165 dump database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-166 dump transaction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-179 execute . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-194 fetch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-199 goto Label . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-202 grant. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-203 group by and having Clauses. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-214 if...else. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-227 insert . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-230 kill . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-239 load database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-242 load transaction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-251 online database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-260 open . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-262 order by Clause. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-264 prepare transaction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-268 print . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-269 raiserror . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-273

SQL Server Reference Manual

vii

Sybase SQL Server Release 11.0.x

readtext. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . recongure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . return . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . revoke . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . rollback . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . rollback trigger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . save transaction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . select. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . setuser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . shutdown . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . truncate table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . union Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . update. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . update statistics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . use . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . waitfor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . where Clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . while . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . writetext . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

3-279 3-282 3-283 3-287 3-294 3-296 3-298 3-300 3-313 3-327 3-329 3-332 3-334 3-338 3-346 3-348 3-349 3-352 3-359 3-362

4. Transact-SQL FunctionsAggregate Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-2 Datatype Conversion Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-9 Date Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-19 Mathematical Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-24 Row Aggregate Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-29 String Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-33 System Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-40 text and image Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-48

5. Transact-SQL TopicsAuditing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-3 Batch Queries. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-6 Browse Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-8 Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-10 Control-of-Flow Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-12 Cursors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-14

viii

Sybase SQL Server Release 11.0.x

Disk Mirroring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-27 Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-32 Identiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-41 IDENTITY Columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-47 Joins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-61 Login Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-67 Null Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-70 Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-78 Roles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-81 Search Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-87 Subqueries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-92 Temporary Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-98 Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-102 Variables (Local and Global) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-122 Wildcard Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-129

Index

SQL Server Reference Manual

ix

Sybase SQL Server Release 11.0.x

x

List of FiguresFigure 3-1: Figure 3-2: Figure 3-3: Figure 3-4: Figure 5-1: File naming convention for database dumps ........................................................3-175 Dumping several databases to the same volume..................................................3-177 File naming convention for transaction log dumps..............................................3-190 Dumping three transaction logs to a single volume.............................................3-192 Nesting transaction statements................................................................................5-114

SQL Server Reference Manual

xi

Sybase SQL Server Release 11.0.x

xii

List of TablesTable 1: Table 2: Table 2-1: Table 2-2: Table 2-3: Table 2-4: Table 2-5: Table 2-6: Table 2-7: Table 2-8: Table 2-9: Table 2-10: Table 2-11: Table 2-12: Table 2-13: Table 2-14: Table 2-15: Table 2-16: Table 2-17: Table 3-1: Table 3-2: Table 3-3: Table 3-4: Table 3-5: Table 3-6: Table 3-7: Table 3-8: Table 3-9: Table 3-10: Table 3-11: Table 3-12: Table 3-13: Table 3-14: Table 3-15: Table 3-16: Table 3-17: Table 3-18: Table 3-19: Syntax statement conventions .....................................................................................xix Types of expressions used in syntax statements ..................................................... xxii Datatype categories .......................................................................................................2-1 Range and storage size for SQL Server system datatypes.......................................2-2 Precision and scale after arithmetic operations.........................................................2-7 Automatic conversion of xed-length datatypes......................................................2-7 Integer datatypes .........................................................................................................2-10 Valid integer values .....................................................................................................2-10 Invalid integer values..................................................................................................2-11 Valid decimal values ...................................................................................................2-12 Invalid decimal values ................................................................................................2-12 Approximate numeric datatypes...............................................................................2-14 Money datatypes .........................................................................................................2-16 Transact-SQL datatypes for storing dates and times..............................................2-20 Date formats for datetime and smalldatetime datatypes ......................................2-21 Examples of datetime entries .....................................................................................2-23 Character datatypes.....................................................................................................2-25 Storage of text and image data ..................................................................................2-36 text and image global variables .................................................................................2-38 Transact-SQL commands..............................................................................................3-1 Information stored about referential integrity constraints ....................................3-17 Row aggregate functions used with the compute clause ......................................3-32 compute by clauses and detail rows .........................................................................3-38 Relationship between nulls and column defaults...................................................3-50 Duplicate row options.................................................................................................3-55 Index options................................................................................................................3-55 Rule binding precedence ............................................................................................3-71 Variable-length datatypes used to store nulls .........................................................3-85 Methods of integrity enforcement.............................................................................3-87 Information stored about cross-database referential integrity constraints .........3-92 Information stored about referential integrity constraints ..................................3-162 Commands used to back up databases and logs ..................................................3-171 Commands used to back up databases ..................................................................3-184 @@sqlstatus values ....................................................................................................3-200 Object access permissions.........................................................................................3-204 Command and object permissions..........................................................................3-207 Status values reported by sp_who ..........................................................................3-240 Commands used to restore databases from dumps .............................................3-245

SQL Server Reference Manual

xiii

Sybase SQL Server Release 11.0.x

Table 3-20: Table 3-21: Table 3-22: Table 3-23: Table 3-24: Table 3-25: Table 3-26: Table 3-27: Table 3-28: Table 3-29: Table 3-30: Table 3-31: Table 4-1: Table 4-2: Table 4-3: Table 4-4: Table 4-5: Table 4-6: Table 4-7: Table 4-8: Table 4-9: Table 4-10: Table 4-11: Table 4-12: Table 4-13: Table 4-14: Table 4-15: Table 5-1: Table 5-2: Table 5-3: Table 5-4: Table 5-5: Table 5-6: Table 5-7: Table 5-8: Table 5-9: Table 5-10: Table 5-11: Table 5-12: Table 5-13: Table 5-14:

Commands used to restore databases ....................................................................3-254 Effect of sort order choices .......................................................................................3-266 SQL Server error return values................................................................................3-285 Object access permissions.........................................................................................3-288 Results of using aggregates with group by............................................................3-304 Permissions required for update and delete..........................................................3-314 Options to set for entry level SQL92 compliance..................................................3-325 Comparing datatypes in union operations ............................................................3-336 Comparison operators ..............................................................................................3-353 Arithmetic operators .................................................................................................3-354 Bitwise operators .......................................................................................................3-354 Wildcard characters...................................................................................................3-356 Transact-SQL functions.................................................................................................4-1 Aggregate functions ......................................................................................................4-2 Datatype conversion functions ....................................................................................4-9 Display formats for date/time information.............................................................4-10 Explicit, implicit, and unsupported datatype conversions ...................................4-12 Date functions ..............................................................................................................4-19 Date parts and their values ........................................................................................4-21 Arguments used in mathematical functions............................................................4-24 Mathematical functions ..............................................................................................4-25 Row aggregate functions ............................................................................................4-29 Arguments used in string functions .........................................................................4-33 Function names, arguments, and results..................................................................4-34 Arguments used in system functions .......................................................................4-40 System functions, arguments, and results ...............................................................4-41 text and image functions, arguments, and results ..................................................4-48 Transact-SQL topics.......................................................................................................5-1 System procedures used to manage auditing options .............................................5-4 Control-of-ow keywords ..........................................................................................5-12 @@sqlstatus values ......................................................................................................5-22 Arithmetic operators ...................................................................................................5-33 Truth tables for bitwise operations............................................................................5-34 Examples of bitwise operations.................................................................................5-34 Comparison operators ................................................................................................5-35 Wildcard characters used with like...........................................................................5-37 Truth tables for logical expressions...........................................................................5-38 Comparison operators ................................................................................................5-62 System procedures for login account management................................................5-67 Conversion of xed-length to variable-length datatypes ......................................5-71 Column denition and null defaults ........................................................................5-75

xiv

List of Tables

Sybase SQL Server Release 11.0.x

Table 5-15: Table 5-16: Table 5-17: Table 5-18: Table 5-19: Table 5-20: Table 5-21: Table 5-22: Table 5-23: Table 5-24: Table 5-25: Table 5-26: Table 5-27:

Roles required for SQL commands ...........................................................................5-84 Roles required for system procedures ......................................................................5-85 Wildcard characters used in match strings ..............................................................5-88 Comparison operators ................................................................................................5-94 @@transtate values ....................................................................................................5-103 DDL commands allowed in transactions ...............................................................5-106 DDL commands not allowed in transactions ........................................................5-107 How rollbacks affect processing..............................................................................5-115 How rollbacks from errors affect processing.........................................................5-119 Rollbacks caused by duplicate key errors or rules violations .............................5-120 Global variables .........................................................................................................5-124 Using square brackets to search for wildcard characters.....................................5-133 Using the escape Clause ...........................................................................................5-134

SQL Server Reference Manual

xv

Sybase SQL Server Release 11.0.x

xvi

List of Tables

PrefaceThe SQL Server Reference Manual is a two-volume guide to Sybase SQL Server and the Transact-SQL language. This volume, Volume 1, includes information about datatypes, Transact-SQL commands, built-in functions, and topics of general interest to Transact-SQL users. Volume 2 contains information about system procedures and catalog stored procedures.

AudienceThis manual is intended as a reference tool for Transact-SQL users of all levels. It provides basic syntax and usage information for every command, function, system procedure, and catalog stored procedure.

How to Use This BookThis manual consists of the following chapters: Chapter 1, SQL Server Roadmap, contains lists of tasks you can do with SQL Server and tells you which commands, system procedures, catalog stored procedures, built-in functions, and global variables you can use to accomplish them and which sections of this manual to read to get more information. Chapter 2, System and User-Dened Datatypes, describes the system and user-dened datatypes that are supplied with SQL Server and indicates how to use them to create user-dened datatypes. Chapter 3, Transact-SQL Commands, contains reference information for every Transact-SQL command. Particularly complex commands, such as select, are divided into subsections. For example, there are reference pages on the compute clause and on the group by and having clauses of the select command. Chapter 4, Transact-SQL Functions, contains reference information for the SQL Server aggregate functions, datatype conversion functions, date functions, mathematical functions, row aggregate functions, string functions, system functions, and text and image functions.

SQL Server Reference Manual

xvii

Related Documents

Sybase SQL Server Release 11.0.x

Chapter 5, Transact-SQL Topics, contains information about topics of general interest. Each topic describes the commands, system procedures, and functions that provide a particular type of functionality in Transact-SQL. The Index contains entries for both volumes of the SQL Server Reference Manual.

Related DocumentsOther manuals that you may nd useful are: SQL Server installation and conguration guide, which describes the installation procedures for SQL Server and documents operating system-specic system administration, security administration, and tuning tasks. SQL Server Performance and Tuning Guide, which explains how to tune SQL Server for maximum performance. The book includes information about database design issues that affect performance, query optimization, how to tune SQL Server for very large databases, disk and cache issues, and the effects of locking and cursors on performance. SQL Server Reference Supplement, which contains a list of TransactSQL reserved words, denitions of system tables, a description of the pubs2 sample database, a list of SQL Server error messages, and other reference information that is common to all the SQL Server manuals. SQL Server Security Administration Guide, which explains how to use the security features provided by SQL Server to control user access to data. The manual includes information about how to add users to the server, give them controlled access to database objects and procedures, and manage remote servers. SQL Server Security Features Users Guide, which explains how to use the security features of SQL Server. SQL Server System Administration Guide, which provides in-depth information about administering servers and databases. The manual includes instructions and guidelines for managing physical resources and user and system databases, and specifying character conversion, international language, and sort order settings.

xviii

Sybase SQL Server Release 11.0.x

Conventions Used in This Manual

SQL Server utility programs manual, which documents the Sybase utility programs, such as isql and bcp that are executed at the operating system level. Transact-SQL Users Guide, which documents Transact-SQL, Sybases enhanced version of the relational database language. It serves as a textbook for beginning users of the database management system. Whats New in Sybase SQL Server Release 11.0?, which describes the new features in SQL Server release 11.0.

Conventions Used in This ManualFormatting SQL StatementsSQL is a free-form language: there are no rules about the number of words you can put on a line or where you must break a line. However, for readability, all examples and syntax statements in this manual are formatted so that each clause of a statement begins on a new line. Clauses that have more than one part extend to additional lines, which are indented.

SQL Syntax ConventionsThe conventions for syntax statements in this manual are as follows:Table 1: Syntax statement conventions Keycommand

DenitionCommand names, command option names, utility names, utility ags, and other keywords are in bold Courier in syntax statements, and in bold Helvetica in paragraph text. Variables, or words that stand for values that you ll in, are in italics. Curly braces indicate that you choose at least one of the enclosed options. Do not include braces in your option. Brackets mean choosing one or more of the enclosed options is optional. Do not include brackets in your option. Parentheses are to be typed as part of the command.

variable

{ } [ ] ( )

SQL Server Reference Manual

xix

Conventions Used in This Manual

Sybase SQL Server Release 11.0.x

Table 1: Syntax statement conventions (continued) Key | , DenitionThe vertical bar means you may select only one of the options shown. The comma means you may choose as many of the options shown as you like, separating your choices with commas to be typed as part of the command.

Syntax statements (displaying the syntax and all options for a command) are printed like this:sp_dropdevice [device_name]

or, for a command with more options:select column_name from table_name where search_conditions

In syntax statements, keywords (commands) are in normal font and identiers are in lowercase: normal font for keywords, italics for user-supplied words. Examples showing the use of Transact-SQL commands are printed like this:select * from publishers

Examples of output from the computer are printed like this:pub_id ------0736 0877 1389 pub_name ------------------New Age Books Binnet & Hardley Algodata Infosystems city ----------Boston Washington Berkeley state ----MA DC CA

(3 rows affected)

Case You can disregard case when you type keywords:SELECT is the same as Select is the same as select

SQL Servers sensitivity to the case (upper or lower) of database objects, such as table names, and data depends on the sort order installed on your SQL Server. Case sensitivity can be changed for single-byte character sets by reconguring SQL Servers sort order. (See the System Administration Guide for more information.)

xx

Sybase SQL Server Release 11.0.x

Conventions Used in This Manual

Obligatory Options {You Must Choose At Least One} Curly Braces and Vertical Bars: Choose one and only one option.{die_on_your_feet | live_on_your_knees | live_on_your_feet}

Curly Braces and Commas: Choose one or more options. If you choose more than one, separate your choices with commas.{cash, check, credit}

Optional Options [You Dont Have to Choose Any] One Item in Square Brackets: You dont have to choose it.[anchovies]

Square Brackets and Vertical Bars: Choose none or only one.[beans | rice | sweet_potatoes]

Square Brackets and Commas: Choose none, one, or more than one option. If you choose more than one, separate your choices with commas.[extra_cheese, avocados, sour_cream]

Ellipsis: Do It Again (and Again)... An ellipsis (...) means that you can repeat the last unit as many times as you like. In this syntax statement, buy is a required keyword:buy thing = price [cash | check | credit] [, thing = price [cash | check | credit]]...

You must buy at least one thing and give its price. You may choose a method of payment: one of the items enclosed in square brackets. You may also choose to buy additional things: as many of them as you like. For each thing you buy, give its name, its price, and (optionally) a method of payment.

SQL Server Reference Manual

xxi

If You Need Help

Sybase SQL Server Release 11.0.x

Expressions Several different types of expressions are used in SQL Server syntax statements.Table 2: Types of expressions used in syntax statements Usageexpression logical expression constant expression oat_expr integer_expr numeric_expr char_expr binary_expression

DenitionCan include constants, literals, functions, column identiers, variables or parameters An expression that returns TRUE, FALSE, or UNKNOWN An expression that always returns the same value, such as 5+3 or ABCDE Any oating-point expression or expression that implicitly converts to a oating value Any integer expression, or an expression that implicitly converts to an integer value Any numeric expression that returns a single value Any expression that returns a single character-type value An expression that returns a single binary or varbinary value

If You Need HelpHelp with your Sybase software is available in the form of documentation and Sybase Technical Support. Each Sybase installation has a designated person who may contact Technical Support. If you cannot resolve your problem using the manuals, ask the designated person at your site to contact Sybase Technical Support.

xxii

SQL Server Roadmap

1

SQL Server Roadmap

1.

This chapter helps you gure out how to use SQL Server to accomplish particular tasks. It tells you which commands, functions, global variables, system and catalog stored procedures, utility programs, and global variables to use and which sections of the documentation to read. This chapter contains information about the following topics: Starting, Stopping, and Conguring SQL Server 1-3 Creating and Maintaining Databases 1-9 Setting Database-Wide Options 1-12 Backing Up and Restoring Databases 1-14 Managing Logins, Users, Groups, and Roles 1-17 Dening and Using Datatypes 1-21 Creating and Maintaining Tables 1-23 Auditing Server Activity 1-31 Retrieving and Manipulating Data 1-33 Using Defaults and Rules 1-38 Error Handling and Messages 1-40 Using Global Variables to Get Information 1-42 Using System and Catalog Stored Procedures to Get Help 1-45 Managing Languages, Character Sets, and Sort Orders 1-49 Managing Database Devices, Segments, and Thresholds 1-52 Setting Session-Wide Options 1-56 Using Stored Procedures and Triggers 1-59 Managing Transactions 1-64 Creating and Using Views 1-66 Each topic consists of a list of related tasks. For example, the topic Starting, Stopping, and Conguring SQL Server includes such tasks as Starting and Stopping SQL Server, Displaying Current Conguration Parameter Values, and Conguring Backup and Recovery.

SQL Server Reference Manual

1-1

Sybase SQL Server Release 11.0.x

Each task lists one or more related activities, plus the name of the command or other syntactical element used to accomplish each activity. For detailed information about syntax or usage, click on the name of the command, function or procedure, or look in the appropriate section of this manual. For detailed information about utility commands, refer to your SQL Server utility programs manual.

1-2

SQL Server Roadmap

Sybase SQL Server Release 11.0.x

Starting, Stopping, and Configuring SQL Server

Starting, Stopping, and Conguring SQL ServerThis section contains information about the following tasks: Starting and Stopping SQL Server 1-3 Displaying Current Conguration Parameter Values 1-3 Conguring Backup and Recovery 1-3 Conguring Cache Management 1-4 Conguring Disk I/O 1-4 Conguring Languages, Character Sets, and Sort Orders 1-4 Conguring the Lock Manager 1-4 Conguring Memory Use 1-5 Conguring Network Communications 1-5 Conguring Operating System Resources 1-5 Conguring Physical Memory Resources 1-6 Conguring SMP Processors 1-6 Conguring Server Administration 1-6 Conguring User Environments 1-7

Starting and Stopping SQL ServerStart a SQL Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . dataserver, startserver Stop a SQL Server after any executing statements complete . . . shutdown Stop a SQL Server immediately. . . . . . . . . . . . . . . . . . . . . . . . . . . . shutdown with nowait

Displaying Current Conguration Parameter ValuesDisplay current values for conguration parameters . . . . . . . . . sp_congure Determine which parameters are displayed by sp_congure. . . . sp_displaylevel

Conguring Backup and RecoverySpecify how many days tapes are protected from overwrite . . . sp_congure tape retention in days Specify recovery speed in minutes . . . . . . . . . . . . . . . . . . . . . . . . . sp_congure recovery interval in minutes

SQL Server Reference Manual

1-3

Starting, Stopping, and Configuring SQL Server

Sybase SQL Server Release 11.0.x

Specify whether verbose recovery messages are displayed . . . . sp_congure print recovery information

Conguring Cache ManagementDetermine boundary on which data caches are aligned . . . . . . . sp_congure memory alignment boundary Specify how long to keep index pages in cache . . . . . . . . . . . . . . sp_congure number of index trips Specify how long to keep OAM pages in cache . . . . . . . . . . . . . . sp_congure number of oam trips Specify memory allocated to procedure cache . . . . . . . . . . . . . . . sp_congure procedure cache size Determine memory available for data, index, and log pages . . . sp_congure total data cache size

Conguring Disk I/ODetermine whether SQL Server runs with asynchronous I/O . . sp_congure allow sql server async i/o Specify number of disk I/O control blocks allocated. . . . . . . . . . sp_congure disk i/o structures Specify maximum number of database devices . . . . . . . . . . . . . . sp_congure number of devices Determine whether server searches for unused pages . . . . . . . . sp_congure page utilization percent

Conguring Languages, Character Sets, and Sort OrdersSpecify the default character set . . . . . . . . . . . . . . . . . . . . . . . . . . . sp_congure default character set id Specify the default language for system messages. . . . . . . . . . . . sp_congure default language id Specify the default sort order. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . sp_congure default sortorder id Specify how many languages can be held in cache . . . . . . . . . . . sp_congure number of languages in cache

Conguring the Lock ManagerSpecify how many address locks are protected by a spinlock . . sp_congure address lock spinlock ratio

1-4

SQL Server Roadmap

Sybase SQL Server Release 11.0.x

Starting, Stopping, and Configuring SQL Server

Specify delay in milliseconds before deadlock checking. . . . . . . sp_congure deadlock checking period Specify number locks transferred between server and engine . . sp_congure freelock transfer block size Specify maximum number of locks available in an engine . . . . . sp_congure max engine freelocks Specify number of locks available for all users and engines. . . . sp_congure number of locks Specify number of page lock structures protected per spinlock. sp_congure page spinlock ratio Specify number table lock structures protected per spinlock . . . sp_congure table lock spinlock ratio

Conguring Memory UseDetermine size of SQL Server executable . . . . . . . . . . . . . . . . . . . sp_congure executable code size

Conguring Network CommunicationsEnable connections to remote servers . . . . . . . . . . . . . . . . . . . . . . sp_congure allow remote access Specify default network packet size, in bytes, for all users . . . . . sp_congure default network packet size Specify maximum network packet size in bytes. . . . . . . . . . . . . . sp_congure max network packet size Specify how many network listeners can be open at one time . . sp_congure max number network listeners Specify the maximum number of remote connections. . . . . . . . . sp_congure number of remote connections Specify the maximum number of remote logins. . . . . . . . . . . . . . sp_congure number of remote logins Specify how many remote sites can access server at one time . . sp_congure number of remote sites Specify the number of pre-read packets. . . . . . . . . . . . . . . . . . . . . sp_congure remote server preread packets Enable/disable TCP packet batching . . . . . . . . . . . . . . . . . . . . . . . sp_congure tcp no delay

Conguring Operating System ResourcesLimit outstanding asynchronous disk I/O requests per engine. sp_congure max async i/os per engine

SQL Server Reference Manual

1-5

Starting, Stopping, and Configuring SQL Server

Sybase SQL Server Release 11.0.x

Limit outstanding asynchronous disk I/O requests per server . sp_congure max async i/os per server Specify starting address for servers shared memory region . . . sp_congure shared memory starting address

Conguring Physical Memory ResourcesSpecify maximum additional memory for large packets. . . . . . . sp_congure additional network memory Prevent swapping of SQL Server pages to disk . . . . . . . . . . . . . . sp_congure lock shared memory Specify the memory, in 2K units, allocated for SQL Server. . . . . sp_congure total memory

Conguring SMP ProcessorsSpecify the maximum number of online engines . . . . . . . . . . . . . sp_congure max online engines Specify the minimum number of online engines . . . . . . . . . . . . . sp_congure min online engines

Conguring Server AdministrationAllow triggers to re other triggers . . . . . . . . . . . . . . . . . . . . . . . . sp_congure allow nested triggers Allow updates to system tables. . . . . . . . . . . . . . . . . . . . . . . . . . . . sp_congure allow updates to system tables Specify the maximum number of auditing records . . . . . . . . . . . sp_congure audit queue size Specify the number of server clock ticks before ushing . . . . . . sp_congure cpu accounting ush interval Limit milliseconds a task can run before time slice error . . . . . . sp_congure cpu grace time Specify the default database size. . . . . . . . . . . . . . . . . . . . . . . . . . . sp_congure default database size Specify the initial fullness of index pages . . . . . . . . . . . . . . . . . . . sp_congure default ll factor percent Limit percent increase in database writes by housekeeper task. sp_congure housekeeper free write percent Specify percentage of IDENTITY column values held in cache . sp_congure identity burning set factor Specify number of sequential values reserved per process. . . . . sp_congure identity grab size Specify the number of disk I/Os before ushing . . . . . . . . . . . . . sp_congure i/o accounting ush interval

1-6

SQL Server Roadmap

Sybase SQL Server Release 11.0.x

Starting, Stopping, and Configuring SQL Server

Limit server tasks run before checking for I/O completions . . . sp_congure i/o polling process count Specify threshold for number of page locks per command. . . . . sp_congure lock promotion hwm Limit alarm structures allocated for waitfor commands . . . . . . . sp_congure number of alarms Limit number of extents allocated as create index work buffers . sp_congure number of extent i/o buffers Limit mailbox structures for intertask communications . . . . . . . sp_congure number of mailboxes Limit message structures for intertask communications. . . . . . . sp_congure number of messages Specify the maximum number of open databases . . . . . . . . . . . . sp_congure number of open databases Specify the maximum number of open objects . . . . . . . . . . . . . . . sp_congure number of open objects Specify number of extents allocated per trip to page manager . sp_congure number of preallocated extents Specify how many buffers can hold pages from input tables . . . sp_congure number of sort buffers Include or exclude deadlock information from error log . . . . . . sp_congure print deadlock information Limit engine loops before relinquishing CPU. . . . . . . . . . . . . . . . sp_congure runnable process search count Limit number of partition groups allocated . . . . . . . . . . . . . . . . . sp_congure partition groups Specify ratio of spinlocks to internal partition caches . . . . . . . . . sp_congure partition spinlock ratio Specify size of automatically created IDENTITY columns . . . . . sp_congure size of auto identity Limit amount of memory available for sort operations. . . . . . . . sp_congure sort page count Specify length of clock tick in microseconds . . . . . . . . . . . . . . . . . sp_congure sql server clock tick length Specify number of milliseconds a task can run. . . . . . . . . . . . . . . sp_congure time slice Determine the SQL Server version number. . . . . . . . . . . . . . . . . . sp_congure upgrade version

Conguring User EnvironmentsSpecify the maximum number of local connections. . . . . . . . . . . sp_congure number of user connections Specify number of cache protectors per task. . . . . . . . . . . . . . . . . sp_congure permission cache entries Specify size of stack guard area in bytes . . . . . . . . . . . . . . . . . . . . sp_congure stack guard size Specify size of stack in bytes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . sp_congure stack size

SQL Server Reference Manual

1-7

Starting, Stopping, and Configuring SQL Server

Sybase SQL Server Release 11.0.x

Specify maximum duration of passwords . . . . . . . . . . . . . . . . . . . sp_congure systemwide password expiration Specify number of bytes in each users user log cache . . . . . . . . sp_congure user log cache size Specify number of user log caches per user log spinlock . . . . . . sp_congure user log cache spinlock ratio

1-8

SQL Server Roadmap

Sybase SQL Server Release 11.0.x

Creating and Maintaining Databases

Creating and Maintaining DatabasesThis section contains information about the following tasks: Building the System Databases 1-9 Creating a User Database 1-9 Adding Space to a Database 1-9 Specifying the Current Database 1-10 Changing the Database Owner (dbo) 1-10 Checking Database Consistency 1-10 Getting Help on Databases 1-10 Moving the Transaction Log to Its Own Device 1-10 Renaming a Database 1-10 Removing a Database from a Server 1-11

Building the System DatabasesBuild the system databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . buildmaster, installmaster

Creating a User DatabaseChoose a valid database name

Choose a valid name for the database . . . . . . . . . . . . . . . . . . . . . . Identiers Check a potential database name for validity . . . . . . . . . . . . . . . . . valid_name()Specify default database devices

Specify default database devices for the server . . . . . . . . . . . . . . sp_diskdefaultCreate the database

Create a database with a separate log segment. . . . . . . . . . . . . . . create database...log on Determine the default size for databases in megabytes. . . . . . . . sp_congure default database size

Adding Space to a DatabaseAllocate additional space to a database . . . . . . . . . . . . . . . . . . . . . alter database, sp_dbremap Find out how much space is used by database objects . . . . . . . . sp_spaceused

SQL Server Reference Manual

1-9

Creating and Maintaining Databases

Sybase SQL Server Release 11.0.x

Specifying the Current DatabaseIdentify the current database

Find a databases ID number . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . db_id() Find the name of the current database . . . . . . . . . . . . . . . . . . . . . . db_name()Use a different database

Find out whether the database is available for public use . . . . . sp_helpdb Make the database available for public use . . . . . . . . . . . . . . . . . . online database Use a different database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . use

Changing the Database Owner (dbo)Find out who owns the database . . . . . . . . . . . . . . . . . . . . . . . . . . sp_helpuser dbo Specify a new database owner. . . . . . . . . . . . . . . . . . . . . . . . . . . . . sp_changedbowner

Checking Database ConsistencyCheck consistency of indexes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . sp_indsuspect Check consistency of page allocations . . . . . . . . . . . . . . . . . . . . . . dbcc checkalloc Check consistency of system tables . . . . . . . . . . . . . . . . . . . . . . . . dbcc checkcatalog Check consistency of all tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . dbcc checkdb

Getting Help on DatabasesGet help on databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . sp_helpdb List the databases on a SQL Server . . . . . . . . . . . . . . . . . . . . . . . . . sp_databases

Moving the Transaction Log to Its Own DeviceMove the transaction log to a separate device . . . . . . . . . . . . . . . sp_logdevice

Renaming a DatabaseDetermine which databases need new names

Find out whether a database name is a reserved word . . . . . . . . sp_checkreswordsChoose a valid database name

Choose a valid name for the database . . . . . . . . . . . . . . . . . . . . . . Identiers Check a potential database name for validity . . . . . . . . . . . . . . . . . valid_name()

1-10

SQL Server Roadmap

Sybase SQL Server Release 11.0.x

Creating and Maintaining Databases

Rename the database

Rename a database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . sp_renamedb

Removing a Database from a ServerRemove a damaged database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . dbcc dbrepair Remove an intact database. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . drop database

SQL Server Reference Manual

1-11

Setting Database-Wide Options

Sybase SQL Server Release 11.0.x

Setting Database-Wide OptionsThis section contains information about the following tasks: Listing Current Database Option Settings 1-12 Putting New Settings into Effect 1-12 Setting Options That Affect Backup and Recovery 1-12 Setting Options That Affect bcp, select into, and writetext 1-12 Setting Options That Affect Checkpoints 1-13 Setting Options That Affect IDENTITY Columns 1-13 Specifying the Default Null Type for a Database 1-13 Specifying Whether Users Can Update the Database 1-13 Setting Options That Affect Thresholds 1-13 Setting Options That Affect Transaction Management 1-13

Listing Current Database Option SettingsList database options and their current settings . . . . . . . . . . . . . . sp_helpdb

Putting New Settings into EffectPut new option settings into effect . . . . . . . . . . . . . . . . . . . . . . . . . checkpoint

Setting Options That Affect Backup and RecoveryAbort transactions when the log becomes full . . . . . . . . . . . . . . . sp_dboption abort tran on log full Checkpoint database automatically after recovery . . . . . . . . . . . sp_dboption no chkpt on recovery, false Truncate the log automatically after checkpoints . . . . . . . . . . . . . sp_dboption trunc log on chkpt

Setting Options That Affect bcp, select into, and writetextAllow fast bcp in a database. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . sp_dboption select into/bulkcopy Allow select into on permanent tables. . . . . . . . . . . . . . . . . . . . . . . sp_dboption select into/bulkcopy

1-12

SQL Server Roadmap

Sybase SQL Server Release 11.0.x

Setting Database-Wide Options

Allow writetext in a database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . sp_dboption select into/bulkcopy

Setting Options That Affect CheckpointsClear the transaction log after automatic checkpoints. . . . . . . . . sp_dboption trunc log on chkpt Perform automatic checkpoints after recovery . . . . . . . . . . . . . . . sp_dboption no chkpt on recovery

Setting Options That Affect IDENTITY ColumnsAutomatically create an IDENTITY column in new tables . . . . . sp_dboption auto identity Include IDENTITY columns in indexes for all tables. . . . . . . . . . sp_dboption identity in nonunique index, true

Specifying the Default Null Type for a DatabaseSet the default null type for columns . . . . . . . . . . . . . . . . . . . . . . . sp_dboption allow nulls by default

Specifying Whether Users Can Update the DatabaseLock users out of a database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . sp_dboption dbo use only Set single-user mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . sp_dboption single user Prohibit updates to a database . . . . . . . . . . . . . . . . . . . . . . . . . . . . sp_dboption read only

Setting Options That Affect ThresholdsDisable nonlog thresholds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . sp_dboption no free space acctg List database options and their current settings . . . . . . . . . . . . . . sp_helpdb

Setting Options That Affect Transaction ManagementAbort transactions when the log is full . . . . . . . . . . . . . . . . . . . . . sp_dboption abort tran on log full Allow data denition language in transactions . . . . . . . . . . . . . . sp_dboption ddl in tran

SQL Server Reference Manual

1-13

Backing Up and Restoring Databases

Sybase SQL Server Release 11.0.x

Backing Up and Restoring DatabasesThis section contains information about the following tasks: Adding a Backup Server to a SQL Server 1-14 Getting Help on Backup Servers 1-14 Starting and Stopping Backup Servers 1-14 Backing Up a Database After a Device Failure 1-14 Backing Up a Database When Its Transaction Log Is Full 1-15 Performing Routine Backups of User Databases 1-15 Restoring User Databases from Backups 1-15 Restoring Backups of Release 10.x Databases 1-16 Trimming a Databases Transaction Log 1-16

Adding a Backup Server to a SQL ServerChoose a valid name for the Backup Server . . . . . . . . . . . . . . . . . Identiers Add or change a Backup Server . . . . . . . . . . . . . . . . . . . . . . . . . . . sp_addserver

Getting Help on Backup ServersGet help on Backup Servers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . sp_helpserver List the Backup Servers running on the local machine . . . . . . . . . showserver

Starting and Stopping Backup ServersStart a Backup Server

Start a Backup Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . backupserver, startserverStop a Backup Server

Find out the name of the Backup Server . . . . . . . . . . . . . . . . . . . . sp_helpserver Stop a Backup Server after active dumps and loads complete . . shutdown Stop a Backup Server immediately . . . . . . . . . . . . . . . . . . . . . . . . . shutdown with nowait

Backing Up a Database After a Device FailureBack up a database after its database device fails. . . . . . . . . . . . . dump transaction with no_truncate

1-14

SQL Server Roadmap

Sybase SQL Server Release 11.0.x

Backing Up and Restoring Databases

Backing Up a Database When Its Transaction Log Is FullCopy and trim log without recording transaction . . . . . . . . . . . . dump transaction with no_log Make backup after trimming log . . . . . . . . . . . . . . . . . . . . . . . . . . dump database

Performing Routine Backups of User DatabasesCreate a device to be used for backup and recovery

Add a dump device to a SQL Server . . . . . . . . . . . . . . . . . . . . . . . sp_addumpdeviceAllow users other than the owner to back up a database

Allow a user to back up and restore the database . . . . . . . . . . . . sp_role grant oper_roleMake regular backups of the database

Copy the entire database, including transaction log . . . . . . . . . . dump databaseBack up the transaction log in between full database backups Copy the transaction log then trim it . . . . . . . . . . . . . . . . . . . . . . . . . dump transaction Reply to Backup Server prompts while backing up the database

Reply to Backup Server prompts . . . . . . . . . . . . . . . . . . . . . . . . . . sp_volchanged

Restoring User Databases from BackupsCreate an empty database into which you will load the backup

Create an empty database to receive the backup . . . . . . . . . . . . . create database for load, alter database for loadMake sure you have the right tape

Find out which backup les are on the tape . . . . . . . . . . . . . . . . . load database with listonly, load transaction with listonlyRestore the most recent backup of the database

Restore the database from the most recent backup . . . . . . . . . . . load databaseReply to Backup Server prompts while restoring the database

Reply to Backup Server prompts . . . . . . . . . . . . . . . . . . . . . . . . . . sp_volchangedRestore the transaction logs in the order made

Restore transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . load transactionMake the restored database available for use

Find out whether the database is available for public use . . . . . sp_helpdb Make the database available for public use . . . . . . . . . . . . . . . . . . online database

SQL Server Reference Manual

1-15

Backing Up and Restoring Databases

Sybase SQL Server Release 11.0.x

Restoring Backups of Release 10.x DatabasesCreate a database to load the backup into

Create an empty database to receive the backup . . . . . . . . . . . . . create database for load Add additional space to the new database . . . . . . . . . . . . . . . . . . . . alter database for loadLoad the release 10.x database

Load the release 10.x database. . . . . . . . . . . . . . . . . . . . . . . . . . . . . load databaseUpgrade the database

Upgrade the database to current SQL Server version . . . . . . . . . online databaseBack up the database

Back up the newly upgraded database . . . . . . . . . . . . . . . . . . . . . dump databaseDetermine which object names are currently reserved words

Identify object names that are reserved words . . . . . . . . . . . . . . . sp_checkreswordsFind procedures, triggers, and views that depend on these objects

Identify dependent procedures, views, triggers . . . . . . . . . . . . . . sp_dependsStore definitions of dependent objects

Copy denitions of dependent objects to le . . . . . . . . . . . . . . . . . . defncopy Print denitions of dependent objects . . . . . . . . . . . . . . . . . . . . . . sp_helptextDrop the dependent objects

Remove dependent objects from database . . . . . . . . . . . . . . . . . . drop procedure, drop trigger, drop viewChoose new, valid names for the objects

Change object names that are reserved words . . . . . . . . . . . . . . . sp_renameRe-create the dependent objects

Re-create dependent objects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . create procedure, create trigger, create viewBack up the database

Back up the database after re-creating dependent objects. . . . . . dump database

Trimming a Databases Transaction LogMove the transaction log to its own device

Move the transaction log to a separate device . . . . . . . . . . . . . . . sp_logdeviceTrim the transaction log

Trim the transaction log without making a copy . . . . . . . . . . . . . dump transaction with truncate_only Trim a transaction log after with truncate_only option fails . . . . . . dump transaction with no_log Back up the database after trimming the transaction log . . . . . . dump database

1-16

SQL Server Roadmap

Sybase SQL Server Release 11.0.x

Managing Logins, Users, Groups, and Roles

Managing Logins, Users, Groups, and RolesThis section contains informtion about the following tasks: Creating New Users 1-17 Getting Help on Users 1-17 Managing Passwords 1-17 Using Groups 1-18 Using Roles 1-18 Using Aliases 1-19 Changing the Characteristics of a Login 1-19 Impersonating Another User 1-19 Managing User Permissions 1-19 Managing Remote Users 1-19 Monitoring User Activity 1-19 Preventing a User from Accessing the Server 1-20

Creating New UsersRead about SQL Server logins . . . . . . . . . . . . . . . . . . . . . . . . . . Login Management Create a SQL Server login . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . sp_addlogin Add a SQL Server login as a user in the current database . . . . . sp_adduser

Getting Help on UsersGet help on a database user or all users . . . . . . . . . . . . . . . . . . . . . sp_helpuser Conrm that a login is a database user or alias. . . . . . . . . . . . . . . valid_user() Get help on local users . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . sp_displaylogin, sp_who Find out a database users ID number . . . . . . . . . . . . . . . . . . . . . . user_id() Find out the current users name . . . . . . . . . . . . . . . . . . . . . . . . . . user, user_name() Find out the name of a SQL Server login . . . . . . . . . . . . . . . . . . . . suser_name() Find out the server user ID of a SQL Server login . . . . . . . . . . . . suser_id()

Managing PasswordsAdd a password for a SQL Server login. . . . . . . . . . . . . . . . . . . . . sp_password

SQL Server Reference Manual

1-17

Managing Logins, Users, Groups, and Roles

Sybase SQL Server Release 11.0.x

Change a password for a SQL Server login . . . . . . . . . . . . . . . . . . sp_password

Using GroupsManage permissions through groups

Find out what permissions are assigned to a group. . . . . . . . . . . sp_helprotect Remove permissions from a group. . . . . . . . . . . . . . . . . . . . . . . . . revoke Grant permissions to a group . . . . . . . . . . . . . . . . . . . . . . . . . . . . . grantCreate a group

Create a group in the current database . . . . . . . . . . . . . . . . . . . . . sp_addgroupList the groups in the current database

List the groups in the current database . . . . . . . . . . . . . . . . . . . . . sp_helpgroupList the members of a group

List the members of a group . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . sp_helpgroupRemove a user from a group or change a users group

Change a users group . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . sp_changegroupRemove a group from a database

List the members of the group. . . . . . . . . . . . . . . . . . . . . . . . . . . . . sp_helpgroup Remove each member from the group . . . . . . . . . . . . . . . . . . . . . . sp_changegroup Remove the group from the current database. . . . . . . . . . . . . . . . sp_dropgroup

Using RolesGet general information about roles

Read about roles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . RolesManage permissions through roles

Confer permissions on a role . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . grant Find out what permissions are granted to a role . . . . . . . . . . . . . sp_helprotect Get help on permissions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . sp_helprotect Revoke permissions from a role . . . . . . . . . . . . . . . . . . . . . . . . . . . revokeVerify that a user has a particular role

Verify that a user has a required role . . . . . . . . . . . . . . . . . . . . . . . proc_role()Assign a role to a user

Assign a role to a SQL Server login. . . . . . . . . . . . . . . . . . . . . . . . . sp_roleTurn a role on or off for a session

Activate or deactivate a role for a session . . . . . . . . . . . . . . . . . . . set role Display a users currently active roles . . . . . . . . . . . . . . . . . . . . . . show_role()

1-18

SQL Server Roadmap

Sybase SQL Server Release 11.0.x

Managing Logins, Users, Groups, and Roles

Revoke a role from a user

Revoke a role from a SQL Server login. . . . . . . . . . . . . . . . . . . . . . sp_role

Using AliasesCreate an alias in the current database for a server login . . . . . . sp_addalias List the SQL Server logins mapped to an alias . . . . . . . . . . . . . . . sp_helpuser Remove an alias from the current database. . . . . . . . . . . . . . . . . . sp_dropalias

Changing the Characteristics of a LoginChange a logins default database or language . . . . . . . . . . . . . . sp_modifylogin Rename a login . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . sp_modifylogin

Impersonating Another UserTemporarily impersonate another database user . . . . . . . . . . . . . setuser

Managing User PermissionsGrant permissions to a user, group, or role . . . . . . . . . . . . . . . . . . grant Get help on permissions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . sp_helprotect Revoke permissions from a user, group, or role . . . . . . . . . . . . . . revoke

Managing Remote UsersAllow a remote login to execute local stored procedures . . . . . . sp_addremotelogin Display or change remote server option settings . . . . . . . . . . . . . sp_remoteoption Get help on remote server logins . . . . . . . . . . . . . . . . . . . . . . . . . . sp_helpremotelogin Remove a remote login from the local server . . . . . . . . . . . . . . . . sp_dropremotelogin Require password verication from remote logins . . . . . . . . . . . sp_remoteoption trusted Remove a remote server and its logins. . . . . . . . . . . . . . . . . . . . . . sp_dropserver...droplogin

Monitoring User ActivityAudit a user

Audit a logins object access and command batches . . . . . . . . . . sp_auditlogin

SQL Server Reference Manual

1-19

Managing Logins, Users, Groups, and Roles

Sybase SQL Server Release 11.0.x

Report a logins system usage

Close the accounting period and report system usage . . . . . . . . sp_clearstats Report system usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . sp_reportstats List the processes run by a SQL Server login . . . . . . . . . . . . . . . . sp_who

Preventing a User from Accessing the ServerRemove a user from a database

Remove a user from the current database . . . . . . . . . . . . . . . . . . . sp_dropuser Remove an alias from the current database. . . . . . . . . . . . . . . . . . sp_dropaliasLock a login or remove it from the server

List locked logins. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . sp_locklogin Lock or unlock a SQL Server login . . . . . . . . . . . . . . . . . . . . . . . . . sp_locklogin Remove a login from SQL Server . . . . . . . . . . . . . . . . . . . . . . . . . . sp_droplogin Remove a remote login from the local server . . . . . . . . . . . . . . . . sp_dropremotelogin

1-20

SQL Server Roadmap

Sybase SQL Server Release 11.0.x

Defining and Using Datatypes

Dening and Using DatatypesThis section contains information about the following tasks: Getting Information About Datatypes 1-21 Converting Data to Another Datatype or Format 1-21 Creating User-Dened Datatypes 1-22 Finding a Columns Datatype 1-22 Renaming a User-Dened Datatype 1-22 Removing a User-Dened Datatype from a Database 1-22

Getting Information About DatatypesGet help on system datatypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . sp_datatype_info, sp_help Read about system datatypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . System and User-Dened Datatypes Read about exact numeric types . . . . . . . . . . . . . . . . . . . . . . . . . . . Exact Numeric Datatypes Read about approximate numeric types . . . . . . . . . . . . . . . . . . . . Approximate Numeric Datatypes Read about datatypes for monetary values . . . . . . . . . . . . . . . . . . Money Datatypes Read about datatypes for dates and times. . . . . . . . . . . . . . . . . . . Date/time Datatypes Read about the timestamp type. . . . . . . . . . . . . . . . . . . . . . . . . . . . . timestamp Datatype Read about character types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Character Datatypes Read about datatypes for binary values. . . . . . . . . . . . . . . . . . . . . Binary Datatypes Read about the bit type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . bit Datatype Read about the sysname type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . sysname Datatype Read about text and image types . . . . . . . . . . . . . . . . . . . . . . . . . . . text and image Datatypes Read about user-dened datatypes . . . . . . . . . . . . . . . . . . . . . . . . User-Dened Datatypes

Converting Data to Another Datatype or FormatConvert one datatype to another . . . . . . . . . . . . . . . . . . . . . . . . . . convert() Convert a date to another date format . . . . . . . . . . . . . . . . . . . . . . convert() Convert an angle from degrees to radians. . . . . . . . . . . . . . . . . . . radians() Convert an angle from radians to degrees. . . . . . . . . . . . . . . . . . . degrees() Find the ASCII code for the rst character in an expression. . . . ascii() Find the character with a specied ASCII code . . . . . . . . . . . . . . char() Convert a oating point number to a character string. . . . . . . . . str()

SQL Server Reference Manual

1-21

Defining and Using Datatypes

Sybase SQL Server Release 11.0.x

Find the integer equivalent of a hexadecimal string . . . . . . . . . . hextoint() Find the hexadecimal equivalent of an integer . . . . . . . . . . . . . . . inttohex() Convert a string from uppercase to lowercase . . . . . . . . . . . . . . . lower() Convert a string from lowercase to uppercase . . . . . . . . . . . . . . . upper()

Creating User-Dened DatatypesRead about user-defined datatypes

Get information about user-dened datatypes . . . . . . . . . . . . . User-Dened Datatypes Get information about columns with system-generated values. IDENTITY Columns Get information about column null types . . . . . . . . . . . . . . . . . . . Null ValuesChoose a name for the user-defined datatype

Choose a valid name for the datatype . . . . . . . . . . . . . . . . . . . . . . Identiers Check a potential datatype name for validity . . . . . . . . . . . . . . . . . valid_name()Create the datatype

Create a user-dened datatype . . . . . . . . . . . . . . . . . . . . . . . . . . . . sp_addtypeSpecify a default value for columns of the datatype

Create a default . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . create default Bind the default to the user-dened datatype. . . . . . . . . . . . . . . . sp_bindefault Unbind the default from the user-dened datatype . . . . . . . . . . sp_unbindefaultSpecify rules for valid values

Create a rule . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . create rule Bind the rule to the user-dened datatype . . . . . . . . . . . . . . . . . . sp_bindrule Unbind the rule from the user-dened datatype . . . . . . . . . . . . . sp_unbindrule

Finding a Columns DatatypeFind a columns datatype . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . sp_columns

Renaming a User-Dened DatatypeChoose a valid datatype name . . . . . . . . . . . . . . . . . . . . . . . . . . . . Identiers, valid_name() Identify datatype names that are reserved words . . . . . . . . . . . . sp_checkreswords Rename a datatype . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . sp_rename

Removing a User-Dened Datatype from a DatabaseRemove a user-dened datatype from the current database . . . sp_droptype

1-22

SQL Server Roadmap

Sybase SQL Server Release 11.0.x

Creating and Maintaining Tables

Creating and Maintaining TablesThis section contains information about the following tasks: Using Temporary Tables 1-23 Using IDENTITY Columns 1-23 Creating a Table from Scratch 1-24 Using an Existing Table to Create a New Table 1-24 Adding Constraints, Defaults, Rules, and Keys to a Table 1-25 Adding a Column to a Table 1-26 Renaming a Column 1-26 Renaming a Table 1-27 Checking Table Consistency 1-27 Determining Column Datatype, Length, and Value 1-27 Giving Others Permission to Use a Table 1-28 Identifying Tables 1-28 Creating and Maintaining a Tables Indexes 1-28 Manipulating Data 1-29 Monitoring Table Size 1-29 Limiting the Number of Rows per Page 1-30 Moving a Table or Index to Another Segment 1-30 Partitioning a Table for Better Insert Performance 1-30 Removing a Table from a Database 1-30

Using Temporary TablesRead about temporary tables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Temporary Tables

Using IDENTITY ColumnsGet general information about IDENTITY columns

Read about IDENTITY columns . . . . . . . . . . . . . . . . . . . . . . . . . . . IDENTITY ColumnsConfigure server parameters for IDENTITY columns

Specify the percentage of column values held in cache . . . . . . . . sp_congure identity set burning factor

SQL Server Reference Manual

1-23

Creating and Maintaining Tables

Sybase SQL Server Release 11.0.x

Specify number of sequential values reserved per process. . . . . sp_congure identity grab size Specify size of automatically created IDENTITY columns . . . . . sp_congure size of auto identitySet database options for IDENTITY columns

Include IDENTITY columns in indexes for all tables. . . . . . . . . . sp_dboption identity in nonunique index, trueSet session options for IDENTITY columns

Allow explicit inserts into an IDENTITY column. . . . . . . . . . . . . set identity_insert on Automatically create an IDENTITY column in new tables . . . . . set auto identity onRetrieve IDENTITY column values

Find the last value inserted into an IDENTITY column . . . . . . . @@identity Pseudonym for a tables IDENTITY column . . . . . . . . . . . . . . . . . . syb_identity

Creating a Table from ScratchCreate a schema

Create a schema in the current database . . . . . . . . . . . . . . . . . . . . create schemaChoose valid table and column names or allow nonstandard names

Choose a valid name for the table and its columns . . . . . . . . . . . Identiers Check potential names for validity. . . . . . . . . . . . . . . . . . . . . . . . . valid_name() Allow delimited identiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . set quoted_identier onCreate the table

Create a new table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . create tableBack up the database

Back up the database that contains the new table . . . . . . . . . . . . dump database

Using an Existing Table to Create a New TableChoose a valid table name or allow nonstandard names

Choose a valid name for the table. . . . . . . . . . . . . . . . . . . . . . . . . . Identiers Check a potential table name for validity . . . . . . . . . . . . . . . . . . . valid_name() Allow delimited identiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . set quoted_identier onSet database option allowing use of the select into command

Allow the use of the select into command in the database . . . . . . sp_dboption select into/bulkcopy Put new option setting into effect . . . . . . . . . . . . . . . . . . . . . . . . . . checkpointCreate the new table

Create a table and populate with existing data. . . . . . . . . . . . . . . select into

1-24

SQL Server Roadmap

Sybase SQL Server Release 11.0.x

Creating and Maintaining Tables

Disallow select into command

Allow the use of the select into command in the database . . . . . . sp_dboption select into/bulkcopy Put new option setting into effect . . . . . . . . . . . . . . . . . . . . . . . . . . checkpointBack up the database

Back up the database that contains the new table . . . . . . . . . . . . dump database

Adding Constraints, Defaults, Rules, and Keys to a TableUse SQL92 primary key, foreign key, and unique constraints

Add a named constraint to an existing table . . . . . . . . . . . . . . . . . alter table List the denition of the constraint. . . . . . . . . . . . . . . . . . . . . . . . . sp_helptext Change a constraint or remove it from a table . . . . . . . . . . . . . . . alter tableCreate a message that displays whenever the SQL92 constraint is violated

Add a user message to a database . . . . . . . . . . . . . . . . . . . . . . . . . sp_addmessage Associate the message with a named constraint. . . . . . . . . . . . . . sp_bindmsg Find out what message is associated with a constraint . . . . . . . . sp_helpconstraint Associate a different message with a constraint . . . . . . . . . . . . . . sp_unbindmsg, sp_bindmsgSpecify a default value for a column

Create a default . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . create default Bind the default to a column . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . sp_bindefault Unbind the default from a column . . . . . . . . . . . . . . . . . . . . . . . . . sp_unbindefaultSpecify a rule that determines valid column values

Create a rule that determines acceptable values . . . . . . . . . . . . . . create rule Bind the rule to a column . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . sp_bindrule Unbind the rule from a column. . . . . . . . . . . . . . . . . . . . . . . . . . . . sp_unbindruleUse common keys

List potential common keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . sp_helpjoins Dene a common key . .