搜索
首页数据库mysql教程Mysql 存储过程_MySQL

转换结束符:

delimiter /

存储过程:

 

create procedure test 

()#或者是(a int,b int,out b int) 

begin 

#这里写你的操作 

end / 

 

调用:call test()/

存储函数:

 

create function test 

()#或者是(a int,b int) 

returns int #函数体必须包含一个RETURN value语句 

begin 

#这里写你的操作 

return 1; 

end/

 

调用:select test()/

备注:存储函数内不能查表(不能使用select 语句)储过程可以

语法:

  1. 存储过程中的局部变量前面无@符号用declare定义,且只能在begin end块中,全局变量为@var,前面带有@符号

     

    create procedure test(out a int) 

    begin 

    declare ab int default 1; 

    set a=100; 

    set a=ab; 

    end 

     

    declare 定义的变量的优先级最高,所有当out a 等变量和局部变量相同名的时候,该变量只在该begin块中有效

    SELECT id,data INTO x,y FROM test.t1 通过查询赋值变量

    begin不能并列使用

2.返回值通过OUT参数得到

3.判断

if条件 then

操作

end if;

case 值 (可选)

when 条件 then 操作

when 条件 then 操作

end

4.循环

while 条件 do … end while

loop … end loop

repeat … end repeat

goto

while … end while 例

 

CREATE PROCEDURE p14 () 

BEGIN 

DECLARE v INT; 

SET v = 0; 

WHILE v 
INSERT INTO t VALUES (v); 

SET v = v + 1; 

END WHILE; 

END; // 

 

 

labels 使用

lab1 :begin

操作

end lab1;

 

lab2:while 条件 do

#操作

#可以使用leave lab2;跳出循环

#可以使用iterate lab2;跳过当次循环

end while lab2

 

GOTO 使用

 

b1:begin 

declare i int; 

set i=1; 

 

label lab1; 

select 'hi'; 

set i=i+1; 

if(i
goto lab1; 



end b1;   

     

5.错误处理

declare continue handler for sqlstate 'error number' 操作 end;

declare exit handler for sqlstate 'error number' 操作 end;

sqlstate 'error number' 还可以是

not found #空行

sqlexception #发生错误

sqlwarning #发生警告

以上语句均在发送错误的时候才触发

6.光标

只读的及不滚动, 声明处理程序之前被声明, SELECT语句不能有INTO子句。

例:

 

CREATE PROCEDURE curdemo() 

BEGIN 

declare a,b,done int; 

DECLARE cur1 CURSOR FOR SELECT id,data FROM test.t1; 

DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = 1;#声明错误处理 

OPEN cur1;#打开光标 

while done!=1 do 

fetch cur1 into a,b; 

if a>b then select 'a'; 

else select 'b'; 

end if ; 

end while; 

close cur1; 

end ; 

 

 

备注:取得最后插入的ID 函数为last_insert_id();所有的存储过程等信息都在INFORMATION_SCHEMA库中

 

 

查询存储过程

 

show create procedure test/ #查询存储过程详细 

show create function test/ 

 

 

触发器: TRIGGER

类型:

INSERT:将新行插入表时激活触发程序,例如,通过INSERT、LOAD DATA和REPLACE语句。

UPDATE:更改某一行时激活触发程序,例如,通过UPDATE语句。

DELETE:从表中删除某一行时激活触发程序,例如,通过DELETE和REPLACE语句。

使用OLD.col_name来引用更新前的某一行的列,也能使用NEW.col_name来引用更新后的行中的列

时间: BEFORE AFTER

例:

 

CREATE TRIGGER testtrio BEFORE INSERT ON test1 

  FOR EACH ROW BEGIN 

   #操作 

  END

 

 

动态SQL:

语法:

PREPARE stmt_name FROM preparable_stmt;
EXECUTE stmt_name [USING @var_name [, @var_name] ...];
{DEALLOCATE | DROP} PREPARE stmt_name;

实例:

 

mysql> SET @a=1;
mysql> PREPARE STMT FROM "SELECT * FROM tbl LIMIT ?"; 
mysql> EXECUTE STMT USING @a; 
mysql> SET @skip=1; SET @numrows=5; 
mysql> PREPARE STMT FROM "SELECT * FROM tbl LIMIT ?, ?"; 
mysql> EXECUTE STMT USING @skip, @numrows;

 

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
在MySQL中使用视图的局限性是什么?在MySQL中使用视图的局限性是什么?May 14, 2025 am 12:10 AM

mysqlviewshavelimitations:1)他们不使用Supportallsqloperations,限制DatamanipulationThroughViewSwithJoinSorsubqueries.2)他们canimpactperformance,尤其是withcomplexcomplexclexeriesorlargedatasets.3)

确保您的MySQL数据库:添加用户并授予特权确保您的MySQL数据库:添加用户并授予特权May 14, 2025 am 12:09 AM

porthusermanagementInmysqliscialforenhancingsEcurityAndsingsmenting效率databaseoperation.1)usecReateusertoAddusers,指定connectionsourcewith@'localhost'or@'%'。

哪些因素会影响我可以在MySQL中使用的触发器数量?哪些因素会影响我可以在MySQL中使用的触发器数量?May 14, 2025 am 12:08 AM

mysqldoes notimposeahardlimitontriggers,butacticalfactorsdeterminetheireffactective:1)serverConfiguration impactactStriggerGermanagement; 2)复杂的TriggerSincreaseSySystemsystem load; 3)largertablesslowtriggerperfermance; 4)highConconcConcrencerCancancancancanceTigrignecentign; 5); 5)

mysql:存储斑点安全吗?mysql:存储斑点安全吗?May 14, 2025 am 12:07 AM

Yes,it'ssafetostoreBLOBdatainMySQL,butconsiderthesefactors:1)StorageSpace:BLOBscanconsumesignificantspace,potentiallyincreasingcostsandslowingperformance.2)Performance:LargerrowsizesduetoBLOBsmayslowdownqueries.3)BackupandRecovery:Theseprocessescanbe

mySQL:通过PHP Web界面添加用户mySQL:通过PHP Web界面添加用户May 14, 2025 am 12:04 AM

通过PHP网页界面添加MySQL用户可以使用MySQLi扩展。步骤如下:1.连接MySQL数据库,使用MySQLi扩展。2.创建用户,使用CREATEUSER语句,并使用PASSWORD()函数加密密码。3.防止SQL注入,使用mysqli_real_escape_string()函数处理用户输入。4.为新用户分配权限,使用GRANT语句。

mysql:blob和其他无-SQL存储,有什么区别?mysql:blob和其他无-SQL存储,有什么区别?May 13, 2025 am 12:14 AM

mysql'sblobissuitableForStoringBinaryDataWithInareLationalDatabase,而alenosqloptionslikemongodb,redis和calablesolutionsoluntionsoluntionsoluntionsolundortionsolunsolunsstructureddata.blobobobsimplobissimplobisslowderperformandperformanceperformancewithlararengelitiate;

mySQL添加用户:语法,选项和安全性最佳实践mySQL添加用户:语法,选项和安全性最佳实践May 13, 2025 am 12:12 AM

toaddauserinmysql,使用:createUser'username'@'host'Indessify'password'; there'showtodoitsecurely:1)choosethehostcarecarefullytocon trolaccess.2)setResourcelimitswithoptionslikemax_queries_per_hour.3)usestrong,iniquepasswords.4)Enforcessl/tlsconnectionswith

MySQL:如何避免字符串数据类型常见错误?MySQL:如何避免字符串数据类型常见错误?May 13, 2025 am 12:09 AM

toAvoidCommonMistakeswithStringDatatatPesInMysQl,CloseStringTypenuances,chosethirtightType,andManageEngencodingAndCollat​​ionsEttingsefectery.1)usecharforfixed lengengters lengengtings,varchar forbariaible lengength,varchariable length,andtext/blobforlabforlargerdata.2 seterters seterters seterters seterters

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SecLists

SecLists

SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

螳螂BT

螳螂BT

Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

功能强大的PHP集成开发环境

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用