mysql資料庫存儲過程
創建存儲過程
mysql> delimiter $ -- delimiter $是設置 $為命令終止符號,代替默認的分號,因為分號有其他用處.
mysql> create procere sp_test(IN pi_id int, OUT po_name varchar(10))
-> begin
-> select * from test.tb_test;
-> select tb_test.name into po_name from test.tb_test where tb_test.id = pi_id;
-> end
-> $
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter ; -- 恢復分號作為分隔終止符號
5.調用存儲過程
mysql> set @po_name='';
Query OK, 0 rows affected (0.00 sec)
mysql> call sp_test(1,@po_name);
❷ mysql存儲過程用分號還是雙刀
mysql存儲過程用雙刀。
存儲過程(StoredProcere)是一種在資料庫中存儲復雜程序,以便外部程序調用的一種資料庫對象。
❸ 怎麼在mysql中查詢已建立的存儲過程
1、首先先創建一個存儲過程,代碼如圖,存儲過程主要的功能是為表JingYan插入新的數據。
❹ mysql 存儲過程 DDL 參數
MySQL8.0 開始支持原⼦ DDL(atomic DDL),數據字典的更新,存儲引擎操作,寫⼆進制日誌結合成了一個事務。在沒有原⼦DDL之前,DROP TABLE test1,test2;如遇到server crash,可能會有test1被drop了,test2沒有被drop掉。下面來看下在MySQL8.0之前和MySQL8.0 數據字典的區別
在MySQL8.0 之前,Data Dictionary除了存在與.FRM, .TRG, .OPT ⽂件外,還存在於系統表中(MyISAM ⾮事務引擎表中),在MySQL8.0 ,Data Dictionary 全部存在於Data Dictionary Storage Engine(即 InnoDB表中),這使crash recovery 維持原⼦性成為了可能
存儲引擎⽀持
目前,只有InnoDB存儲引擎⽀持原子DDL,為了實現原子DDL,Innodb要寫DDL logs 到 mysql.innodb_ddl_log 表,這是⼀個隱藏在mysql.ibd 數據字典表空間⾥的數據字典表。要看mysql.innodb_ddl_log 中的內容,需要
SET GLOBALLOG_ERROR_VERBOSITY=3;(MySQL 8.0 默認為2,error log 記錄Errors and
warnings,不不記錄notes)
SET GLOBAL innodb_print_ddl_logs=1;
CREATE TABLEt1 (c1 INT)ENGINE=InnoDB;
查看error log
[Note] [MY-011066] InnoDB: DDL loginsert: [DDLrecord:DELETE SPACE,id=30,
thread_id=25, space_id=9, old_file_path=./test/t1.ibd]
[Note] [MY-011066]InnoDB:DDL logdelete:by id30
[Note] [MY-011066]InnoDB:DDL loginsert: [DDLrecord: REMOVECACHE,id=31,
thread_id=25, table_id=1066, new_file_path=test/t1]
[Note] [MY-011066]InnoDB:DDL logdelete:by id31
[Note] [MY-011066]InnoDB:DDL loginsert: [DDLrecord: FREE,id=32, thread_
id=25, space_id=9, index_id=143, page_no=4]
[Note] [MY-011066]InnoDB:DDL log delete:by id32
[Note] [MY-011066]InnoDB:DDL logpost ddl :begin for thread id: 25
[Note] [MY-011066]InnoDB:DDL logpost ddl :end for thread id: 25
原子DDL 操作步驟
准備:創建所需的對象並將DDL⽇志寫入 mysql.innodb_ddl_log表中。DDL日誌定義了如何前滾和回滾DDL操作。
執行:執⾏DDL操作。例如,為CREATE TABLE操作執⾏創建。
提交:更新數據字典並提交數據字典事務。
Post-DDL:重播並從mysql.innodb_ddl_log表格中刪除DDL⽇志。為確保回滾可以安全執⾏⽽不引⼊不⼀致性,在此最後階段執⾏⽂件操作(如重命名或刪除數據文件)。這一階段還從 mysql.innodb_dynamic_metadata的數據字典表刪除的動態元數據為了DROP TABLE,TRUNCATE和其它重建表的DDL操作。
⽆論事務是提交還是回滾,DDL日誌都會mysql.innodb_ddl_log在Post-DDL階段重播並從表中刪除 。mysql.innodb_ddl_log如果伺服器在DDL操作期間暫停,DDL⽇志應該只保留在表中。在這種情況下,DDL⽇志會在恢復後重播並刪除。
在恢復情況下,當伺服器重新啟動時,可能會提交或回退DDL事務。如果在重做⽇志和⼆進制日誌中存在DDL操作的提交階段期間執⾏的數據字典事務,則該操作被認為是成功的並且被前滾。否則,在InnoDB重放數據字典重做日誌時回滾不完整的數據字典事務 ,並且回滾DDL事務。
原⼦DDL ⽀持類型
• DROP TABLES , all tables dropped or none
• DROP SCHEMA, all entities in the schema are dropped, or none
• Note that atomic DDL statements will be rolled back or committed even in case of crash, e.g. RENAME TABLES
• CREATE TABLE would be successfully committed or rolled back (no orphan ibd left)
• TRUNCATE TABLE (including InnoDB tables with FTS AUX tables) would be successfully committed or rolled back
• RENAME TABLES, all or none
• ALTER TABLE successful or not done
示例
結論
在MySQL8.0之前,alter table 操作在server crash的情況下,會遺留.frm,.ibd文件。MySQL8.0 能實現原⼦DDL(包括 DROP TABLE, DROP SCHEMA, CREATE TABLE, TRUNCATE TABLE, ALTER TABLE),alter table 操作,在server crash的情況下,不會遺留.frm,.ibd臨時文件。讓我們⼀起期待MySQL8.0 GA的到來吧!
❺ 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存儲過程知識點難學嗎
MySQL存儲過程 一、存儲過程 1.1 什麼是存儲過程 存儲過程(Stored Procere)是在大型資料庫系統中,一組為了完成特定功能的SQL 語句集,它存儲在資料庫中,一次編譯後永久有效,用戶通...,確實有一定的難度!
❼ mysql存儲過程的基本用法有哪些
mysql存儲過程的基本用法有哪些
在外部程序訪問資料庫時(例如 PHP),要組織很多 SQL 語句。
特別是業務邏輯復雜的時候,一大堆的 SQL 和條件夾雜在 PHP 代碼中,讓人不寒而慄。現在有了 MySQL 存儲過程,業務邏輯可以封裝存儲過程中,這樣不僅容易維護,而且執行效率也高。
一、MySQL 創建存儲過程
"pr_add" 是個簡單的 MySQL 存儲過程,這個MySQL 存儲過程有兩個 int 類型的輸入參數 "a"、"b",返回這兩個參數的和。
復制代碼 代碼如下:
drop procere if exists pr_add;
計算兩個數之和
復制代碼 代碼如下:
create procere pr_add
(
a int,
b int
)
begin
declare c int;
if a is null then
set a = 0;
end if;
if b is null then
set b = 0;
end if;
set c = a + b;
select c as sum;
/*
return c;
不能在 MySQL 存儲過程中使用。return 只能出現在函數中。
*/
end;
二、調用 MySQL 存儲過程
復制代碼 代碼如下:
call pr_add(10, 20);
執行 MySQL 存儲過程,存儲過程參數為 MySQL 用戶變數。
復制代碼 代碼如下:
set @a = 10;
set @b = 20;
call pr_add(@a, @b);
三、MySQL 存儲過程特點
創建 MySQL 存儲過程的簡單語法為:
復制代碼 代碼如下:
create procere 存儲過程名字()
(
[in|out|inout] 參數 datatype
)
begin
MySQL 語句;
end;
MySQL 存儲過程參數如果不顯式指定"in"、"out"、"inout",則默認為"in"。習慣上,對於是"in" 的參數,我們都不會顯式指定。
1. MySQL 存儲過程名字後面的"()"是必須的,即使沒有一個參數,也需要"()"
2. MySQL 存儲過程參數,不能在參數名稱前加"@",如:"@a int"。下面的創建存儲過程語法在 MySQL 中是錯誤的(在 SQL Server 中是正確的)。 MySQL 存儲過程中的變數,不需要在變數名字前加"@",雖然 MySQL 客戶端用戶變數要加個"@"。
復制代碼 代碼如下:
create procere pr_add
(
@a int, -- 錯誤
b int -- 正確
)
3. MySQL 存儲過程的參數不能指定默認值。
4. MySQL 存儲過程不需要在 procere body 前面加 "as"。而 SQL Server 存儲過程必須加 "as" 關鍵字。
復制代碼 代碼如下:
create procere pr_add
(
a int,
b int
)
as -- 錯誤,MySQL 不需要 "as"
begin
mysql statement ...;
end;
5. 如果 MySQL 存儲過程中包含多條 MySQL 語句,則需要 begin end 關鍵字。
復制代碼 代碼如下:
create procere pr_add
(
a int,
b int
)
begin
mysql statement 1 ...;
mysql statement 2 ...;
end;
6. MySQL 存儲過程中的每條語句的末尾,都要加上分號 ";"
復制代碼 代碼如下:
...
declare c int;
if a is null then
set a = 0;
end if;
...
end;
7. MySQL 存儲過程中的注釋。
復制代碼 代碼如下:
/*
這是個
多行 MySQL 注釋。
*/
declare c int; -- 這是單行 MySQL 注釋 (注意 -- 後至少要有一個空格)
if a is null then # 這也是個單行 MySQL 注釋
set a = 0;
end if;
❽ 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裡面sql語句調用存儲過程,該如何寫
這樣:
CREATEPROCEDUREsp_add(a int, b int,outc int)
begin
set c=a+ b;
end;
調用過程:
call sp_add (1,2,@a);
select @a;
(9)mysql資料庫存儲過程擴展閱讀:
注意事項
存儲過程(stored procere)是一組為了完成特定功能的SQL語句集合,經編譯後存儲在伺服器端的資料庫中,利用存儲過程可以加速SQL語句的執行。
存儲過程分為系統存儲過程和自定義存儲過程。
系統存儲過程在master資料庫中,但是在其他的資料庫中可以直接調用,並且在調用時不必在存儲過程前加上資料庫名,因為在創建一個新資料庫時,系統存儲過程在新的資料庫中會自動創建。
自定義存儲過程,由用戶創建並能完成某一特定功能的存儲過程,存儲過程既可以有參數又有返回值,但是它與函數不同,存儲過程的返回值只是指明執行是否成功,並不能像函數那樣被直接調用,只能利用execute來執行存儲過程。
創建存儲過程
SQL Server創建存儲過程:
create procere 過程名
@parameter 參數類型
@parameter 參數類型
。。。
as
begin
end
執行存儲過程:execute 過程名
❿ mysql中的存儲過程是什麼意思啊
直白的講就是把SQL語句進行封裝,然後留個介面,使用的時候直接調用介面。