在我的 MySQL 工作台中,我尝试用 350 个随机值填充表,但它停在 109 个值,这是我的代码:
drop procedure if exists in_vino;
delimiter $$
create procedure in_vino()
begin
declare c int;
declare var int;
set c = 1;
set var = (select count(codice)
from bottiglia
where bottiglia.tipo_bottiglia = 'vino');
start transaction;
while c <= var do
update vino
set codice = (select codice
from bottiglia
where bottiglia.tipo_bottiglia = 'vino'
order by rand()
limit 1);
while c <= 175 do
insert into vino(codice, tipo_vino, vitigno)
select
codice,
'rosso' as tipo_vino,
elt(floor(1 + rand() * 10), 'barbera', 'dolcetto', 'nebbiolo', 'corvina', 'rondinella', 'sangiovese', 'nero d avola',
'syrah', 'cerasuolo di vittoria', 'nerello mascalese') as vitigno;
end while;
while c >175 and c <= 350 do
insert into vino(codice, tipo_vino, vitigno)
select
codice,
'bianco' as tipo_vino,
elt(floor(1 + rand() * 9), 'timorasso', 'pinot bianco', 'pinot grigio', 'soavignon blanc', 'friuliano', 'vermentino', 'pecorino',
'malvasia del lazio', 'grillo') as vitigno;
end while;
set c = c + 1;
end while;
end $$
delimiter ;
select * from vino;
我检查了 var 变量,它的值为 350。
这是我的输出:返回 109 行
我无法告诉你为什么你的代码只插入 109 行,除非你提供来自 bottiglia 的示例数据(最好与表定义一起使用)。
您的代码在许多地方看起来不正确,并且应该超时,因为内部循环永远不会终止,并且应该因 insert..select 语句中的未知列名代码而失败。另外,我不明白为什么当看起来设置变量是合适的时候你试图更新 vino 。您还应该在退出程序之前提交事务。
以下可能是(或接近)您需要的
create table vino(id int auto_increment primary key,codice int, tipo_vino varchar(10), vitigno varchar(30));
create table bottiglia(codice int);
drop procedure if exists p;
delimiter $$
create procedure p()
begin
declare c int;
declare var int;
declare vcodice int;
set c = 1;
while c<=350 do
insert into bottiglia values (c);
set c=c+1;
end while;
set var := (select count(codice)
from bottiglia
#where bottiglia.tipo_bottiglia = 'vino'
);
set c = 1;
start transaction;
select c,var;
while c <= var do
#update vino
set vcodice = (select codice
from bottiglia
#where bottiglia.tipo_bottiglia = 'vino'
order by rand()
limit 1);
if c <= 175 then
insert into vino(codice, tipo_vino, vitigno)
select
vcodice,
'rosso' as tipo_vino,
elt(floor(1 + rand() * 10), 'barbera', 'dolcetto', 'nebbiolo', 'corvina', 'rondinella', 'sangiovese', 'nero d avola',
'syrah', 'cerasuolo di vittoria', 'nerello mascalese') as vitigno;
end if;
if c >175 and c <= 350 then
insert into vino(codice, tipo_vino, vitigno)
select
vcodice,
'bianco' as tipo_vino,
elt(floor(1 + rand() * 9), 'timorasso', 'pinot bianco', 'pinot grigio', 'soavignon blanc', 'friuliano', 'vermentino', 'pecorino',
'malvasia del lazio', 'grillo') as vitigno;
end if;
set c = c + 1;
end while;
select c;
commit;
end $$
delimiter ;
truncate table bottiglia;
call p();
注意:-小提琴不需要设置分隔符..