OReilly - SQL.In Nutshell
OReilly - SQL.In Nutshell
SOLL
SO
IN A NUTSHELL
A Desktop Quick Reference
sql_titlepg.qxd 10/13/00 5:27 PM Page 2
sql_titlepg.qxd 10/13/00 5:27 PM Page 3
SOLL
SO
IN A NUTSHELL
A Desktop Quick Reference
CHAPTER 3
Statements
This chapter is the heart of : it is an alphabetical listing of SQL
commands with detailed explanations and examples. Each command and function
is identified in a master table as being “supported,” “supported with variations,”
“supported with limitations,” or “not supported,” for each of the four SQL dialects
covered in this book: SQL Server, MySQL, Oracle, and PostgreSQL. After a brief
description of the SQL99 standard, each vendor application is discussed briefly but
thoroughly, with supporting examples and sample coding.
When researching a command in this chapter, first read the introductory para-
graph, vendor support table, and the section on SQL99 syntax and description.
The reason for this is that any common features between all the implementations
of the command are discussed once under the SQL99 topic. Thus, reading directly
about a vendor’s implementation of a particular command may not describe every
aspect of that command, since some of its details may be covered in the opening
comments.
The following list offers useful tips for reading Table 3-1, as well as what each
abbreviation stands for. The sections that follow describe the table’s commands in
detail:
1. The first column contains the alphabetized SQL commands.
2. The SQL statement class for each command is indicated in the second left-
hand column.
3. The command’s implementation in SQL99 is indicated in the next column.
27
4. The subsequent columns list the vendor’s level of support:
The vendor supports the SQL99 standard for the particular command.
The vendor supports the SQL99 standard for the particular command,
using vendor-specific code or syntax.
The vendor supports some but not all of the functions specified by the
SQL99 standard for the particular command.
The vendor does not support the particular command according to the
SQL99 standard.
5. Remember that even if a specific SQL99 command is listed as “Not
Supported,” the vendor usually has alternative coding or syntax to enact the
same command or function. Therefore, be sure read the discussion and exam-
ples for each command later in this chapter.
Statements
SQL-data Yes S NS S S
Statements
ALTER PROC[EDURE] procedure_name [;number]
[ {@parameter datatype } [VARYING] [= default] [OUTPUT] ][,...n]
[WITH { RECOMPILE | ENCRYPTION | RECOMPILE , ENCRYPTION } ]
[FOR REPLICATION]
AS
T-SQL Block
In SQL Server, this command allows the change of any existing parameters for the
previously created stored procedure. In effect, this command is just a shortcut
around issuing a statement, followed by a modified
statement. Such grants or permissions to the stored procedure do not
have to be reestablished. Review the command for a full
explanation of the syntax. This command may be executed on SQL Server by the
owner of the stored procedure or a member of the db_owner and ddl_admin fixed
database roles.
This example using Microsoft SQL Server creates a procedure called get_next_br
that generates a unique CHAR(22) output string. Then, when the procedure must
be changed to retrieve unique INT output value, is used to
redefine the stored procedure:
-- A Microsoft SQL Server stored procedure
CREATE PROCEDURE get_next_nbr
@next_nbr CHAR(22) OUTPUT
AS
BEGIN
DECLARE @random_nbr INT
SELECT @random_nbr = RAND() * 1000000
SELECT @next_nbr =
RIGHT('000000' + CAST(ROUND(RAND(@random_nbr)*1000000,0))AS CHAR(6), 6) +
RIGHT('0000' + CAST(DATEPART (yy, GETDATE() ) AS CHAR(4)), 2) +
RIGHT('000' + CAST(DATEPART (dy, GETDATE() ) AS CHAR(3)), 3) +
RIGHT('00' + CAST(DATEPART (hh, GETDATE() ) AS CHAR(2)), 2) +
RIGHT('00' + CAST(DATEPART (mi, GETDATE() ) AS CHAR(2)), 2) +
RIGHT('00' + CAST(DATEPART (ss, GETDATE() ) AS CHAR(2)), 2) +
RIGHT('000' + CAST(DATEPART (ms, GETDATE() ) AS CHAR(3)), 3)
END
GO
SELECT @convert_to_nbr =
RIGHT('000000' + CAST(ROUND(RAND(@random_nbr)*1000000,0))AS CHAR(6), 6) +
RIGHT('0000' + CAST(DATEPART (yy, GETDATE() ) AS CHAR(4)), 2) +
RIGHT('000' + CAST(DATEPART (dy, GETDATE() ) AS CHAR(3)), 3) +
RIGHT('00' + CAST(DATEPART (hh, GETDATE() ) AS CHAR(2)), 2) +
RIGHT('00' + CAST(DATEPART (mi, GETDATE() ) AS CHAR(2)), 2) +
RIGHT('00' + CAST(DATEPART (ss, GETDATE() ) AS CHAR(2)), 2) +
RIGHT('000' + CAST(DATEPART (ms, GETDATE() ) AS CHAR(3)), 3)
END
GO