《mysql存储过程编程》之一些杂碎知识

本文介绍了如何使用存储程序中的非SELECT SQL语句进行数据增删改,并演示了如何通过SELECT...INTO语句将表中的数据读入到本地变量中。

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

 XML Code 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

 

1、在存储程序中使用非 SELECT SQL 语句(可以向表中添加/修改/删除数据)
create table t1(id int ,name varchar(20));

create procedure insert_data(in count int)
begin
    declare i int default 0;
    repeat 
        set i=i+1;
        insert into t1(id,name) values(i,concat('name-',i)); 
    until i=10
    end repeat;
end$$


2、把表中的数据读入本地变量(使用select-into语法结构)
create procedure use_select_into()
begin
    declare count int default 0;
    select count(1) into count from t1;
    select concat('==>',count);
end$$


create procedure use_select_into2()
begin
    declare row_id int default 0;   
    declare row_name varchar(20) default '';
    declare i int default 0;
    
    while i<=10 do 
        set i=i+1;
        select id,name into row_id,row_name from t1 where id = i;
        select concat('[',i,'] id=',row_id,' name=',row_name);
    end while;
end$$

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值