存储过程
6.1. 创建无参数存储过程
IF EXISTS (SELECT name FROM sysobjects
WHERE name = 'au_info_all' AND type = 'P')
DROP PROCEDURE au_info_all –判断如果存在XXX过程就删除
CREATE PROCEDURE au_info_all –创建存储过程
AS
SELECT pub_name FROM authors
SQLSERVER 调用方法:
EXECUTE au_info_all/ EXEC au_info_all
ORACLE 调用方法:
6.2. 创建有参数存储过程
IF EXISTS (SELECT name FROM sysobjects
WHERE name = 'au_info' AND type = 'P')
DROP PROCEDURE au_info
CREATE PROCEDURE au_info
@lastname varchar(40),
@firstname varchar(20)
AS
SELECT au_lname, au_fname, title, pub_name
FROM authors
WHERE au_fname = @firstname
AND au_lname = @lastname
SQLSERVER 调用方法:
EXECUTE au_info 'Dull', 'Ann'
EXECUTE au_info @lastname = 'Dull', @firstname = 'Ann'
6.3. 创建有通配符参数存储过程
从四个表的联接中只返回指定的作者(提供了姓名)、出版的书籍以及出版社。该存储过程对传递的参数进行模式匹配,如果没有提供参数,则使用预设的默认值.
IF EXISTS (SELECT name FROM sysobjects
WHERE name = 'au_info2' AND type = 'P')
DROP PROCEDURE au_info2
CREATE PROCEDURE au_info2
@lastname varchar(30) = 'D%',
@firstname varchar(18) = '%'
AS
SELECT au_lname, au_fname, title, pub_name
FROM authors
WHERE au_fname LIKE @firstname
AND au_lname LIKE @lastname
SQLSERVER 调用方法:
EXECUTE au_info2
EXECUTE au_info2 'Wh%'
EXECUTE au_info2 @firstname = 'A%'
EXECUTE au_info2 'Hunter', 'Sheryl'
EXECUTE au_info2 'H%', 'S%'
6.4. 创建有Output参数存储过程
IF EXISTS(SELECT name FROM sysobjects
WHERE name = 'titles_sum' AND type = 'P')
DROP PROCEDURE titles_sum
CREATE PROCEDURE
titles_sum
@@TITLE varchar(40) = '%',
@@SUM money OUTPUT
AS
SELECT 'Title Name' = title
FROM titles
WHERE title LIKE @@TITLE
SELECT @@SUM = SUM(price)
FROM titles WHERE title LIKE @@TITLE
SQLSERVER 调用方法:
DECLARE @@TOTALCOST money
EXECUTE titles_sum 'The%', @@TOTALCOST OUTPUT
IF @@TOTALCOST < 200
BEGIN
PRINT 'All of these titles can be purchased for less than $200.'
END
ELSE
SELECT 'The total cost of these titles is $' + RTRIM(CAST(@@TOTALCOST AS varchar(20)))
6.5. 创建有Output游标参数存储过程
IF EXISTS (SELECT name FROM sysobjects
WHERE name = 'titles_cursor' and type = 'P')
DROP PROCEDURE titles_cursor
CREATE PROCEDURE titles_cursor @titles_cursor CURSOR VARYING OUTPUT
AS
SET @titles_cursor = CURSOR
FORWARD_ONLY STATIC FOR
SELECT *
FROM titles
OPEN @titles_cursor
SQLSERVER 调用方法:
DECLARE @MyCursor CURSOR
EXEC titles_cursor @titles_cursor = @MyCursor OUTPUT
WHILE (@@FETCH_STATUS = 0)
BEGIN
FETCH NEXT FROM @MyCursor
END
CLOSE @MyCursor
DEALLOCATE @MyCursor
6.6. 创建有WITH RECOMPILE存储过程
当每次调用存储过程时都重新编译存储过程,目前尚未发现这个参数的用处
CREATE PROCEDURE titles_by_author
@@LNAME_PATTERN varchar(30) = '%'
WITH RECOMPILE
AS
SELECT RTRIM(au_fname) + ' ' + RTRIM(au_lname) AS 'Authors full name',
title AS Title
FROM authors a INNER JOIN titleauthor ta
ON a.au_id = ta.au_id INNER JOIN titles t
ON ta.title_id = t.title_id
WHERE au_lname LIKE @@LNAME_PATTERN
6.7. 创建有WITH ENCRYPTION存储过程
对存储过程进行加密,用于隐藏存储过程的文本,有编程私心的很重要噢!
CREATE PROCEDURE encrypt_this
WITH ENCRYPTION
AS SELECT * FROM authors
6.8. 调用用户定义的系统存储过程
6.9. 删除存储过程
DROP PROCEDURE procecureName
6.10. 在SQL中存储过程的调用
? 无参数存储过程调用
EXECUTE au_info_all
? 有参数存储过程调用
EXECUTE au_info 'Dull', 'Ann'
EXECUTE au_info @lastname = 'Dull', @firstname = 'Ann'
? 通配符存储过程调用
EXECUTE au_info2
EXECUTE au_info2 'Wh%'
EXECUTE au_info2 @firstname = 'A%'
EXECUTE au_info2 'Hunter', 'Sheryl'
EXECUTE au_info2 'H%', 'S%'
? Output参数存储过程调用
DECLARE @@TOTALCOST money
EXECUTE titles_sum 'The%', @@TOTALCOST OUTPUT
IF @@TOTALCOST < 200
BEGIN
PRINT ' '
END
ELSE
SELECT 'The total cost of these titles is $' + RTRIM(CAST(@@TOTALCOST AS varchar(20)))
? Output游标参数存储过程调用
DECLARE @MyCursor CURSOR
EXEC titles_cursor @titles_cursor = @MyCursor OUTPUT
WHILE (@@FETCH_STATUS = 0)
BEGIN
FETCH NEXT FROM @MyCursor
END
CLOSE @MyCursor
DEALLOCATE @MyCursor