1 mysql
在操作子程序时,由于需要使用分号";",所以要使用delimiter先重新定义分界符为//。以下/**/包含的内容表示注释
delimiter // /*使用delimiter // 把定界符由“;”设置为“//” 。 注意“delimiter”和“//”之间的空格。*/
/* 1 创建子程序(存储过程和存储函数的统称)*/
create procedure p1( out cnt int)
begin
select count(*) into cnrfrom baseinfo_tbl;
end
// /* 提交以上语句 */
create function f1( s char(20)) returns char(50)
begin
return concat('Hello, ',s,'!');
end
// /* 提交以上语句 */delimiter ; /* 把定界符重新设置为 “;” “delimiter”和“;”之间的空格*/
/* 2 调用子程序*/
select f1('world'); /* 调用函数*/
call p1(@a); /* 调用存储过程*/
select @a;
3 显示指定子程序的结构
show create function f1;
show create procedure p1;
4 显示所有的子程序
show function status;
show procedure status;
注:关于参数的in,out,inout。in表示仅作为输入,out表示仅作为输出,inout表示作为输入和输出。
一般的存储过程的参数默认是in方式。存储函数只能使用in方式。
2 sqlserver
(1)创建存储过程
--本过程使用了pubs数据库,所以开头要加如下语句
use pubs
go
CREATE procedure get_sales_for_title
@title varchar(80) = NULL, --this is the input paramater.
@ytd_sales int output
as
if @title is NULL
begin
print 'error: Your must specify a title value.'
return
end
-- Get the sales for the specified title
select "YTD_SALES" = ytd_sales
from titles
where title = @title
set @ytd_sales = @@rowcount
return
GO
--调用存储过程
declare @ytd int
execute get_sales_for_title @title='Sushi, Anyone?', @ytd_sales = @ytd output
select @ytd as ytd
显示结果
YTD_SALES
4095
ytd
1