MySQL_Procedure_Function_Cursor

本文详细介绍了SQL中的几种关键操作:声明、设置、选择赋值、游标、条件判断、循环与迭代、重复执行及循环控制,通过创建多个存储过程展示了如何在数据库中执行复杂的数据操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    1. Declare

delimiter ||
crate procedure usePro()
begin
declare a, b varchar(20);
insert into TesFun2 values(null, a, b);
end ||
delimiter;

211313_VSsV_2363350.png

call usePro();

211405_GbWt_2363350.png

   

    2. Set

delimiter ||
create procedure usePro2()
begin
declare a, b varchar(20);
set a='Linux C编程', b='123456';
insert into TesFun2 values(null, a, b);
end ||
delimiter;

211700_u5S3_2363350.png

call usePro2();

211725_dXOw_2363350.png


    3. Select into

211822_V3wf_2363350.png

delimiter ||
create procedure usePro3()
begin
declare a, b varchar(20);
select useName2, password2 into a, b from TesFun3 where id2=1;
insert into TesFun2 values(null, a, b);
end ||
delimiter;

212127_Hyj2_2363350.png

call usePro3();

212206_mkOk_2363350.png


    4. Cursor

212312_lfb3_2363350.png

212533_sBM5_2363350.png

delimiter||
create procedure usePro4()
begin
declare a, b varchar(20);
declare cur4 cursor for select useName2, password2 from TesFun3;
open cur4;
fetch cur4 into a, b;
insert into TesFun2 values(null, a, b);
close cur4;
end ||
delimiter;

212921_04t4_2363350.png

call usePro4();

212958_bzJL_2363350.png


    5. If

213411_Nggx_2363350.png

delimiter ||
create procedure tesPro5(in bookId int)
begin
select count(*) into @num from TesFun2 where id=bookId;
if @num>0 then update TesFun2 set useNam='Java12345' where id=bookId;
else
insert into TesFun2 values(null, '禾博士', 'heboshi');
end if;
end ||
delimiter;

213911_Iu9B_2363350.png

call tesPro5(4);

214007_2aia_2363350.png

call tesPro5(5);

214047_aSLs_2363350.png


    6. Case

214203_boJT_2363350.png

delimiter ||
create procedure tesPro6(in bookId int)
begin
select count(*) into @num from TesFun2 where id=bookId;
case @num
when 1 then update TesFun2 set useNam='Java1234567' where id=bookId;
when 0 then insert into TesFun2 values(null, '禾博士2', 'heboshi2');
else insert into TesFun2 values(null, '禾博士3', 'heboshi3');
end case;
end ||
delimiter;

214713_Kgfh_2363350.png

call tesPro6(3);

214746_BKMj_2363350.png

call tesPro6(8);

214837_dOVW_2363350.png


    7. Loop

214928_bQDd_2363350.png

delimiter ||
create procedure tesPro7(in totNum int)
begin
loo: loop
set totNum=totNum-1;
if totNum=0 then leave loo;
else
insert into TesFun3 values(totNum, '禾博士', 'heboshi');
end if;
end loop loo;
end ||
delimiter;

215430_uAIQ_2363350.png

call tesPro7(10);

215520_w10i_2363350.png


    8. Iterate

215608_QIWf_2363350.png

delimiter ||
create procedure tesPro8(in totNum int)
begin
loo: loop
set totNum=totNum-1;
if totNum=0 then leave loo;
elseif totNum=3 then iterate loo;
else
insert into TesFun3 values(totNum, '好玩的Java', 'mima');
end if;
end loop loo;
end ||
delimiter;

215941_s6Jq_2363350.png

call tesPro8(11);

220040_jvQD_2363350.png


    9. Repeat

220134_NejM_2363350.png

delimiter ||
create procedure tesPro9(in totNum int)
begin
rea: repeat
set totNum=totNum-1;
insert into TesFun3 values(totNum, '加油', 'jiayou');
until totNum=1;
end repeat rea;
end ||
delimiter;

220432_4i3j_2363350.png

call tesPro9(11);

220522_bn68_2363350.png



    10. While

220558_jhed_2363350.png

delimiter ||
create procedure tesPro10(in totNum int)
begin
whi: while totNum>0 do
insert into TesFun3 values(totNum, 'while', 'pwhile');
set totNum=totNum-1;
end while whi;
end ||
delimiter;

221144_XDfO_2363350.png

call tesPro10(5);

221233_iBNZ_2363350.png

转载于:https://my.oschina.net/u/2363350/blog/672476

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值