mysql語句存儲過程-ag真人国际官网
• create procere用來創建 存儲過程 ,create function用來創建 函數
• delimiter命令是改變語句的結束符 ,mysql默認的結束符為;號,由於procere和function中的;號並不代表創建的結束,所以要替換成另外的結束符以便表示創建的結束
• rontine_body子句可以包含一個簡單的sql語句,也可以包含多個sql語句, 通過begin…end將這多個sql語句 包含在一起
• mysql存儲過程和函數中也可以包含類似create和drop等ddl語句
• comment子句用來寫入對存儲過程和函數的注釋
• language子句用來表示此存儲過程和函數的創建語言
• 存儲過程和函數被標注為deterministic表明當輸入相同的參數是會返回相同的結果,反之如果是not deterministic則表示相同參數不會是相同結果,默認是not deterministic
• 相關屬性短語只有咨詢含義,並不是強制性的約束
• drop procere/function語句用來 刪除指定名稱的存儲過程或函數
• begin…end語句通常出現在存儲過程、函數和觸發器中,其中 可以包含一個或多個語句 ,每個語句用;號隔開
• 標簽label可以加在begin…end語句以及loop, repeat和while語句
• 語句中通過iterate和leave來控制流程,iterate表示返回指定標簽位置,leave表示跳出標簽
• declare語句通常用來聲明本地變數、游標、條件或者handler
• declare語句只允許出現在begin … end語句中而且必須出現在第一行
• declare的順序也有要求,通常是先聲明本地變數,再是游標,然後是條件和handler
• 本地變數可以通過declare語句進行聲明
• 聲明後的變數可以通過select … into var_list進行賦值,或者通過set語句賦值,或者通過定義游標並使用fetch … into var_list賦值
• 通過declare聲明變數方法:
• mysql支持if,case,iterate,leave,loop,while,repeat語句作為存儲過程和函數中的 流程式控制制語句 ,另外return語句也是函數中的特定流程式控制制語句
• case語句在存儲過程或函數中表明了 復雜的條件選擇語句
• if語句在存儲過程或函數中表明了 基礎的條件選擇語句
其中在 function 裡面,只有 deterministic, no sql 和 reads sql data 被支持。如果我們開啟了 bin-log, 我們就必須為我們的 function 指定一個參數。
在 mysql 中創建函數時出現這種錯誤的解決方法:
set global log_bin_trust_function_creators=true;
• iterate語句 僅出現在loop,repeat,while循環語句中,其含義表示重新開始此循環
• leave語句表明 退出指定標簽的流程式控制制語句塊
• 通常會用在begin…end,以及loop,repeat,while的循環語句中
• loop語句是存儲過程或函數中表達 循環執行 的一種方式
• repeat語句是存儲過程或函數中表達 循環執行 的一種方式
• while語句是存儲過程或函數中表達 循環執行 的一種方式
• return語句用在 函數中,用來終結函數的執行並將指定值返回給調用者
• cursor游標用來 聲明一個數據集
• 游標的聲明必須在變數和條件聲明之後,在handler聲明之前
• cursor close語句用來 關閉之前打開的游標
• cursor declare語句用來聲明一個游標和指定游標對應的數據集合, 通常數據集合是一個select語句
• cursor fetch語句用來獲取游標指定數據集的 下一行數據 並將各個欄位值賦予後面的變數
• open cursor語句用來打開一個之前已經 聲明好的游標
• declare condition語句命名 特定的錯誤條件 ,而該特定錯誤可以在declare…handler中指定 處理方法
• 比如在mysql中1051error code表示的是unknown table的錯誤,如果要對這
個錯誤做特殊處理,可以用三種方法:
• declare handler語句用來聲明一個handler來處理一個或多個特殊條件,當其中的某個條件滿足時則觸發其中的statement語句執行
• statement可以是一個簡單sql語句,也可以是begin…end組成的多個語句
• handler_action子句聲明當執行完statement語句之後應該怎麼辦
condition_value的值有以下幾種:
• 當condition發生但沒有聲明handler時,則存儲過程和函數依照如下規則處理
• create trigger語句用來創建一個觸發器,觸發器的作用是當表上有對應sql語句發生時,則觸發執行
• 觸發器創建時需要 指定對應的表名 tbl_name
• definer關鍵詞用來指定trigger的安全環境
• trigger_time指定觸發器的執行時間,before和after指定觸發器在表中的 每行數據修改前或者後 執行
• trigger_event指定觸發該觸發器的具體 事件
• insert當新的一行數據插入表中時觸發,比如通過執行insert,load data,replace語句插入新數據
• update當表的一行數據被修改時觸發,比如執行update語句時
• delete當表的一行數據被刪除時觸發,比如執行delete,replace語句時
• 當執行insert into … on plicate key update語句時,當碰到重復行執行update時,則觸發update下的觸發器
• 從5.7.2版本開始,可以創建具有相同trigger_time和trigger_event的同一個表上的多個觸發器,默認情況下按照創建的時間依次執行,通過 指定follows/precedes改變執行順序 ,即follows時表示新創建的觸發器後執行,precedes則表示新觸發器先執行
• trigger_body表示觸發器觸發之後要執行的一個或多個語句,在內部可以引用涉及表的欄位, old.col_name表示行數據被修改或刪除之前的欄位數據,new.col_name表示行數據被插入或修改之後的欄位數據
• drop trigger語句用來 刪除一個觸發器
• if exists短語用來避免刪除不存在的觸發器時引發報錯
• 當你執行drop table時,表上的觸發器也被drop掉了
ⅱ mysql 存儲過程總結(一)
1、存儲過程定義:
存儲過程是事先經過編譯並存儲在資料庫中的一段 sql 語句的集合,調用存儲過程可以簡化應用開發 人員的很多工作,減少數據在資料庫和應用伺服器之間的傳輸,對於提高數據處理的效率是有好處的。 存儲過程思想上很簡單,就是資料庫 sql 語言層面的代碼封裝與重用。
2、特點:
封裝,復用 : 可以把某一業務sql封裝在存儲過程中,需要用到 的時候直接調用即可。
可以接收參數,也可以返回數據 :再存儲過程中,可以傳遞參數,也可以接收返回 值。
減少網路交互,效率提升 : 如果涉及到多條sql,每執行一次都是一次網路傳 輸。 而如果封裝在存儲過程中,我們只需要網路交互一次可能就可以了。
3、基本語法
(1)創建:
(2)調用:
(3)查看:
(4)刪除
注意: 在命令行中,執行創建存儲過程的sql時,需要通過關鍵字 delimiter 指定sql語句的 結束符。
ⅲ mysql 存儲過程 中怎麼捕獲異常
declare處理程序的使用:
declare handler_type handler for condition_value[,...] sp_statement
其中,
handler_type的取值范圍:continue | exit | undo
condition_value的取值范圍:sqlstate [value] sqlstate_value | condition_name | sqlwarning | not found | sqlexception | mysql_error_code
這個語句指定每個可以處理一個或多個條件的處理程序。如果產生一個或多個條件,指定的語句被執行。對一個continue處理程序,當前子程序的執行在執行處理程序語句之後繼續。對於exit處理程序,當前begin...end復合語句的執行被終止。undo 處理程序類型語句還不被支持。
· sqlwarning是對所有以01開頭的sqlstate代碼的速記。
· not found是對所有以02開頭的sqlstate代碼的速記。
· sqlexception是對所有沒有被sqlwarning或not found捕獲的sqlstate代碼的速記。
註:除了sqlstate值,mysql錯誤代碼也不被支持。
例:
[sql]viewplainprint?
delimiter$$
createtable`_t1`(
`id`int(11)notnullauto_increment,
`val1`varchar(20)defaultnull,
`val2`int(11)defaultnull,
primarykey(`id`)
)engine=innodbauto_increment=113defaultcharset=latin1$$
[sql]viewplainprint?
delimiter$$
createdefiner=`abandonship`@`%`procedure`p_testexception`()
begin
declare_var,_errintdefault0;
,sqlwarning,notfoundset_err=1;
insertinto_t1(val1,val2)value(2012,'abandonship');
if_err=1then
set_var=2;
endif;
selectcasewhen_var=2then'出錯了'else_varend;
調用該存儲過程將返回:出錯了
ⅳ mysql 存儲過程能返回結果集嗎
可以,存儲過程只是把你的查詢語句形成一個固定的格式,這樣你要查詢的時候就不用每次都執行查詢語句了,sql在執行的時候都會將你的sql語句轉換為位元組碼,然後機器才能運行,存儲過程就節省了這一步,已經形成了位元組碼,所以速度才會快,特別是執行次數比較多的時候,可以節省很多時間
ⅳ mysql 存儲過程
你應該在做統計吧,估計你不會的就是mysql存儲過程的語法 我之前也寫過 很是郁悶 我給你一段代碼 是我用mysql寫過的一個存儲過程 你看看 主要是了解裡面的語法 看懂了 你所說的需求並不難
有看不懂的地方一起討論 :
begin
declare tikk datetime ;
declare done int default 0;
declare userid int default 0;
declare moleid int default 0;
declare couid int default 0;
declare mname varchar(255) ;
declare opsid int default 0;
declare c1 cursor for select i_userid,i_operationid from space_operation_record where status<>0 group by i_userid,i_operationid order by createtime desc;
declare continue handler for sqlstate '02000' set done =1;
set tikk = now();
open c1;
repeat
fetch c1 into userid, opsid;
if not done then
select i_moleid from space_operation where status<>0 and id=opsid into moleid;
if moleid <> '' then
select nvc_identification from space_operation where status<>0 and id=opsid into @identiftion;
if moleid > 0 then
select nvc_ename from space_mole where status<>0 and id=moleid into mname;
else
set mname = 'space';
end if;
create temporary table if not exists sp_tab1(id bigint(20),nvc_content mediumtext,i_obyuid bigint(20),i_tid bigint(20),createtime datetime);
insert into sp_tab1 select id,nvc_content,i_objectid,i_tmid,createtime from space_operation_record where status<>0 and i_operationid=opsid and i_userid=userid ;
select count(*) from sp_tab1 into couid;
set @ihod = 0;
set @listp = '';
set @listpp = '';
set @content0p = '';
set @content0 = '';
while couid > 0 do
select id,nvc_content,i_obyuid,createtime,i_tid into @iok,@conuiy,@objiplk,@crtimhr,@tmids from sp_tab1 where id > @ihod order by id asc limit 0,1;
if @iok <> '' then
if mname = 'blog' then
insert into space_operation_stat(i_operationid,i_userid,nvc_content,d_stattime,createtime) values (@iok,userid,@conuiy,@crtimhr,tikk);
elseif mname = 'team' then
if(@identiftion = 'addblog' || @identiftion = 'mdyblog') then
insert into space_operation_stat(i_operationid,i_userid,nvc_content,d_stattime,i_tmid,createtime) values (@iok,userid,@conuiy,@crtimhr,@tmids,tikk);
else
set @listpp = concat(@listpp,concat(@objiplk,','));
set @operarry1p = substring_index(@conuiy,'|',1);
set @operarry2p = substring_index(@conuiy,'|',-1);
set @content0p = concat(@content0p,concat(@operarry2p,space(1)));
set @objlistp = substring(@listpp,1,length(@listpp)-1);
end if;
elseif mname = 'space' then
if(@identiftion = 'headphoto' || @identiftion = 'status') then
insert into space_operation_stat(i_operationid,i_userid,nvc_content,d_stattime,i_tmid,createtime) values (@iok,userid,@conuiy,@crtimhr,@tmids,tikk);
else
set @listppr = concat(@listppr,concat(@objiplk,','));
set @operarry1pr = substring_index(@conuiy,'|',1);
set @operarry2pr = substring_index(@conuiy,'|',-1);
set @content0pr = concat(@content0pr,concat(@operarry2pr,space(1)));
set @objlistpr = substring(@listppr,1,length(@listppr)-1);
end if;
else
set @listp = concat(@listp,concat(@objiplk,','));
set @operarry1 = substring_index(@conuiy,'|',1);
set @operarry2 = substring_index(@conuiy,'|',-1);
set @content0 = concat(@content0,concat(@operarry2,space(1)));
set @objlist = substring(@listp,1,length(@listp)-1);
end if;
set @ihod = @iok;
end if;
set couid = couid -1;
end while;
if @content0 <> '' then
set @contentp = concat(@operarry1,concat('|',@content0));
select createtime,id into @uitimej,@idjok from space_operation_record where status<>0 and i_operationid=opsid order by createtime desc limit 0,1;
if @uitimej <> '' then
insert into space_operation_stat(i_operationid,i_userid,nvc_content,d_stattime,createtime,nvc_objlist) values(@iok,userid,@contentp,@crtimhr,tikk,@objlist);
end if;
end if;
if @content0p <> '' then
if @identiftion = 'addphoto' then
set @contentp = concat(@operarry1p,concat('|',@content0p));
else
set @contentp = concat(@operarry1p,concat(@content0p,'|'));
end if;
select createtime,id into @uitimej,@idjok from space_operation_record where status<>0 and i_operationid=opsid order by createtime desc limit 0,1;
if @uitimej <> '' then
insert into space_operation_stat(i_operationid,i_userid,nvc_content,d_stattime,createtime,nvc_objlist,i_tmid) values(@iok,userid,@contentp,@crtimhr,tikk,@objlistp,@tmids);
end if;
end if;
if @content0pr <> '' then
set @contentp = concat(@operarry1p,concat('|',@content0pr));
select createtime,id into @uitimej,@idjok from space_operation_record where status<>0 and i_operationid=opsid order by createtime desc limit 0,1;
if @uitimej <> '' then
insert into space_operation_stat(i_operationid,i_userid,nvc_content,d_stattime,createtime,nvc_objlist,i_tmid) values(@iok,userid,@contentp,@crtimhr,tikk,@objlistp,@tmids);
end if;
end if;
delete from sp_tab1;
end if;
end if;
until done end repeat;
close c1;
drop temporary table if exists sp_tab1 ;
update space_operation_play set status=0;
update space_operation_display set status=0;
select createtime into @ptimes from space_operation_stat where status<>0 order by createtime desc limit 0,1;
if @ptimes <>'' then
create temporary table if not exists sp_tab2(id bigint(20),nvc_content mediumtext,i_userid bigint(20),i_lyuid bigint(20),d_stattime datetime);
insert into sp_tab2 select id,nvc_content,i_userid,i_tmid,d_stattime from space_operation_stat where status<>0 and createtime=@ptimes order by d_stattime desc limit 0,30;
select count(*) from sp_tab2 into @cou1id;
set @uoj = 0;
while @cou1id > 0 do
select id,nvc_content,i_userid,d_stattime,i_lyuid into @io1k,@conui1y,@objipl1k,@crtimh1r,@unlpa from sp_tab2 where id > @uoj order by id asc limit 0,1;
if @io1k <> '' then
insert into space_operation_play(i_statid,nvc_content,d_stattime,i_userid,createtime,i_tmid) values (@io1k,@conui1y,@crtimh1r,@objipl1k,now(),@unlpa);
set @uoj = @io1k;
end if;
set @cou1id = @cou1id -1;
end while;
drop temporary table if exists sp_tab2 ;
end if;
end
ⅵ mysql中的存儲過程能返回數組么
mysql中要獲得存儲過程的返回值,可以增加一個out參數,用來返回。
mysql中存儲過程的例子:
create procedure addvoucher (
in userid int,
in voucherid int,
outresult int
)
begin
select
@endate_a := endate ,@batch_a := batch ,@c_count_a := c_count,
@isdead_a := isdead
from
t_voucher
where
id = voucherid;
set autocommit = 0;
if exists (
select
*
from
t_user_voucher tuv,
t_voucher tv
where
tv.id = tuv.voucherid
and tv.batch =@batch_a
) then
set result = 1;-- 已存在
select
result;
else
if @c_count_a > 0 then
if (
to_days(@endate_a) - to_days(now())
) > 0 then
if @isdead_a = 1 then
insert into t_user_voucher (userid, voucherid, isdead)
values
(userid, voucherid, 1);
update t_voucher set c_count = c_count-1 where id = voucherid;
set result = 0;-- 成功
end;
下面是調用並返回結果:
ⅶ mysql資料庫新特性之存儲過程入門教程
在mysql 中 終於引入了存儲過程這一新特性 這將大大增強mysql 的資料庫處理能力 在本文中 將指導讀者快速掌握mysql 的存儲過程的基本知識 帶領用戶入門
存儲過程介紹
存儲過程是一組為了完成特定功能的sql語句集 經編譯後存儲在資料庫中 用戶通過指定存儲過程的名字並給出參數(如果該存儲過程帶有參數)來執行它 存儲過程可由應用程序通過一個調用來執行 而且允許用戶聲明變數 同時 存儲過程可以接收和輸出參數 返回執行存儲過程的狀態值 也可以嵌套調用
存儲過程的優點
作為存儲過程 有以下這些優點
( )減少網路通信量 調用一個行數不多的存儲過程與直接調用sql語句的網路通信量可能不會有很大的差別 可是如果存儲過程包含上百行sql語句 那麼其性能絕對比一條一條的調用sql語句要高得多
( )執行速度更快 存儲過程創建的時候 資料庫已經對其進行了一次解析和優化 其次 存儲過程一旦執行 在內存中就會保留一份這個存儲過程 這樣下次再執行同樣的存儲過程時 可以從內存中直接中讀取
( )更強的安全性 存儲過程是通過向用戶授予許可權(而不是基於表) 它們可以提供對特定數據的訪問 提高代碼安全 比如防止 sql注入
( ) 業務邏輯可以封裝存儲過程中 這樣不僅容易維護 而且執行效率也高
當然存儲過程也有一些缺點 比如
可移植性方面 當從一種資料庫遷移到另外一種資料庫時 不少的存儲過程的編寫要進行部分修改
存儲過程需要花費一定的學習時間去學習 比如學習其語法等
在mysql中 推薦使用mysql query browswer()這個工具去進行存儲過程的開發和管理 下面分步驟來學習mysql中的存儲過程
定義存儲過程的結束符
在存儲過程中 通常要輸入很多sql語句 而sql語句中每個語句以分號來結束 因此要告訴存儲過程 什麼位置是意味著整個存儲過程結束 所以我們在編寫存儲過程前 先定義分隔符 我們這里定義 // 為分隔符 我們使用delimiter //這樣的語法 就可以定義結束符了 當然你可以自己定義其他喜歡的符號
如何創建存儲過程
下面先看下一個簡單的例子 代碼如下
delimiter//createprocedure`p ` ()language sqldeterministicsql security definerment a procere beginselect hello world ! ;end//
下面講解下存儲過程的組成部分
)首先在定義好終結符後 使用create procedure 存儲過程名的方法創建存儲過程 language選項指定了使用的語言 這里默認是使用sql
)deterministic關鍵詞的作用是 當確定每次的存儲過程的輸入和輸出都是相同的內容時 可以使用該關鍵詞 否則默認為not deterministic
) sql security關鍵詞 是表示調用時檢查用戶的許可權 當值為invoker時 表示是用戶調用該存儲過程時檢查 默認為definer 即創建存儲過程時檢查
) ment部分是存儲過程的注釋說明部分
lishixin/article/program/mysql/201404/30557ⅷ mysql 存儲過程 是什麼意思
用select...into語句
下面是mysql 5.0的幫助文檔的:
這個select語法把選定的列直接存儲到變數。因此,只有單一的行可以被取回。
select id,data into x,y from test.t1 limit 1;
注意,用戶變數名在mysql 5.1中是對大小寫不敏感的。請參閱9.3節,「用戶變數」。
重要: sql變數名不能和列名一樣。如果select ... into這樣的sql語句包含一個對列的參考,並包含一個與列相同名字的局部變數,mysql當前把參考解釋為一個變數的名字。例如,在下面的語句中,xname 被解釋為到xname variable 的參考而不是到xname column的:
create procedure sp1 (x varchar(5))
begin
declare xname varchar(5) default 'bob';
declare newname varchar(5);
declare xid int;
select xname,id into newname,xid
from table1 where xname = xname;
select newname;
end;
當這個程序被調用的時候,無論table.xname列的值是什麼,變數newname將返回值『bob』。
ⅸ 關於mysql存儲過程
create table mytb
(tid int,
subject varchar(100),
content varchar(100))
eg:測試數據:
20 計算機應用 應用軟體開發
21 計算機操作系統 操作系統詳解
22 c# c#開發實戰
=======================================
說明一點:tid編號是連續的,如(1,2,3,4···)
存儲過程如下,測試通過:
create proc my_search
@beg_id int ,
@end_id int
as
declare @names varchar(200),
@bcp varchar(8000)
if(@beg_id<@end_id)
begin
while @beg_id<=@end_id
begin
select @names=subject from mytb where tid=@beg_id
set @bcp = 'bcp "select content from 資料庫名.dbo.mytb where tid = ' rtrim(@beg_id) '" queryout "e:\11\' rtrim(@names) '.txt" -c -u"sa" -p"sa"'
exec master..xp_cmdshell @bcp
set @beg_id=@beg_id 1
end
end
==================加路徑後過程==============
--exec my_search 20,21,'e:\11'
alter proc my_search
@beg_id int ,
@end_id int ,
@path varchar(200)
as
if(right(@path,1)='\')
begin
set @path=left(@path,len(@path)-1)
end
declare @names varchar(200),
@bcp varchar(8000)
if(@beg_id<@end_id)
begin
while @beg_id<=@end_id
begin
select @names=subject from mytb where tid=@beg_id
set @bcp = 'bcp "select content from cdma.dbo.mytb where tid = ' rtrim(@beg_id) '" queryout "' @path '\' rtrim(@names) '.txt" -c -u"sa" -p"sa"'
exec master..xp_cmdshell @bcp
set @beg_id=@beg_id 1
end
end
=======
呵呵,很抱歉啊 樓主,沒能實現你的要求,
mssql我不熟,我想語法大概也是差不多吧,