mysql function cursor_MySQL_Procedure_Function_Cursor

1. Declare

delimiter ||

crate procedure usePro()

begin

declare a, b varchar(20);

insert into TesFun2 values(null, a, b);

end ||

delimiter;

905d11ed64ec2efb891e5caed460d132.png

call usePro();

9746655c363d6052efe93b69f91c1118.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;

09cb68c600d0da59b6945f2f27f69835.png

call usePro2();

8c6659becf98819bb4fd1bd76808b94d.png

3. Select into

8480617ed3ce9ee59fa6f45acdcc53df.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;

f322336b4de94ea4a5ab36adeab7ebd9.png

call usePro3();

3937085ec8709950711677aab0dc40b4.png

4. Cursor

6f7a74e28e33e5f0e3b68040e6e3c488.png

f1bc0df7b792ccfdf8ee0d107120d871.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;

9bb0db6a738aa880bf8ad1a748e2216d.png

call usePro4();

f47258d9826e411858a252b3998ea0e2.png

5. If

463b8aebd953d75784c14762e5f5ad07.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;

49721e7a40346edc0c99c38887245a93.png

call tesPro5(4);

d0fdfd027e9893b948e52497917820c4.png

call tesPro5(5);

0cdea36ba31a594b614829e4ab5b839d.png

6. Case

2c50209020900e537eaa99c89f74eca3.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;

54cd05659e647f82299f24aa9daaeac9.png

call tesPro6(3);

af9d59aacf65872e20d8546227303c48.png

call tesPro6(8);

ef060acf7f703c41e50c2f0fe96c198c.png

7. Loop

de6c025876b6f37a9bd93845fe3d0688.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;

202ab41d2ba44eba47d17532c7e86446.png

call tesPro7(10);

05334a46f15e5eee54ee2acfbdac12df.png

8. Iterate

71976dbfb74b43f005673430c7243ca7.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;

8229722381d774b0797422a101624be3.png

call tesPro8(11);

858fe7fdf1246d61648fc595e65ff7c7.png

9. Repeat

0830bfd5e8bf4373e82631ea5823bbf1.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;

67f1e364c9ab6f579dbde0a1424c3bfb.png

call tesPro9(11);

d7057f0103abd6f311665b1306be6792.png

10. While

2136605939a9a266dcee1763baa530dd.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;

244f3e6124c3f76cca857b1266026442.png

call tesPro10(5);

8a40579efe57b4a3a31891a1c6cbe6ac.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值