MySQL存储过程,触发器,游标
程序员文章站
2022-06-04 08:42:00
...
语法:
1.存储过程:
create PROCEDURE name(argment_list)
begin
sql_statement;
end
2.触发器:
create trigger name (before|after) (insert | update | delete) on table_name
for each row
begin
sql_statement;
end
3.游标:
declare cursor_name cursor for select statement;
4.其他语法
declare var_name int; 声明变量
set var_name = 5; 赋值
-- 或者
set @var_name = 5
例子:
CREATE PROCEDURE `procedure_name`()
BEGIN
Declare found boolean default true;
Declare name varchar(20);
Declare names_cursor cursor for SELECT name FROM user;
DECLARE CONTINUE HANDLER FOR NOT FOUND set found=false;
open names_cursor;
name_loop:LOOP fetch names_cursor into name;
if found then
sql_statement;
else
leave name_loop;
end if;
end LOOP name_loop;
close names_cursor;
END
create trigger trigger_name before insert on user
for each row
begin
set @userid = NEW.userid;
set @name = NEW.name;
set @count= (select count(userid) from user where [email protected] and name = @name);
if @count=1 then
-- this user already exists,throw exception
insert into Error_message_ChannelIdToClientMustBeUnique values(1);
end if;
end
推荐阅读
-
mysql 导入导出数据库以及函数、存储过程的介绍_MySQL
-
怎么现在写MYSQL数据库 都在用存储过程了。ecshop phpcms等等 这些好像都没有用给
-
读书笔记-MySQL存储过程-存储函数_MySQL
-
MySQL简略存储过程示例(持续追加)
-
mysql-MySQL:为什么这样的小改动会造成存储过程错误呢?
-
在编写存储过程时使用 Set NoCount On_MySQL
-
cursor-mysql中存储过程无法创建成功
-
mysql利用存储过程批量插入数据_MySQL
-
Oracle 查看表存储过程触发器函数等对象定义语句的方法
-
在编写存储过程时使用 Set NoCount On_MySQL