存儲過程和存儲函數


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


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章