当前位置:首页 » 存储配置 » mysql数据库存储过程

mysql数据库存储过程

发布时间: 2023-01-13 20:39:25

❶ 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语句进行封装,然后留个接口,使用的时候直接调用接口。

热点内容
java返回this 发布:2025-10-20 08:28:16 浏览:595
制作脚本网站 发布:2025-10-20 08:17:34 浏览:889
python中的init方法 发布:2025-10-20 08:17:33 浏览:583
图案密码什么意思 发布:2025-10-20 08:16:56 浏览:766
怎么清理微信视频缓存 发布:2025-10-20 08:12:37 浏览:686
c语言编译器怎么看执行过程 发布:2025-10-20 08:00:32 浏览:1015
邮箱如何填写发信服务器 发布:2025-10-20 07:45:27 浏览:258
shell脚本入门案例 发布:2025-10-20 07:44:45 浏览:117
怎么上传照片浏览上传 发布:2025-10-20 07:44:03 浏览:808
python股票数据获取 发布:2025-10-20 07:39:44 浏览:714