英文:
Cursor only returning null values
问题
我有以下的代码(目前还没有完成,我只是在测试光标是否工作)。每当我从光标中选择内容时,我只会得到null值,即使表中有值。
drop procedure if exists get_hr_bp;
delimiter //
create procedure get_hr_bp()
begin
declare done int default false;
declare hr, sbp, dbp, new_hr, new_sbp, new_dbp numeric(3,0);
declare cur cursor for select hr, sbp, dbp from patient;
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = true;
open cur;
read_loop: loop
fetch cur into hr, sbp, dbp;
select hr, sbp, dbp;
if done then
leave read_loop;
end if;
end loop;
close cur;
end//
delimiter ;
英文:
I have the following code (it is not finished yet I am only testing if the cursor is working for now). whenever I select the contents from the cursor, I only get null-values even though there are values in the table.
delimiter //
create procedure get_hr_bp()
begin
declare done int default false;
declare hr, sbp, dbp, new_hr, new_sbp, new_dbp numeric(3,0);
declare cur cursor for select hr, sbp, dbp from patient;
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = true;
open cur;
read_loop: loop
fetch cur into hr, sbp, dbp;
select hr, sbp, dbp;
if done then
leave read_loop;
end if;
end loop;
close cur;
end//
delimiter ;
答案1
得分: 0
您正在使用相同的变量名hr、sbp、dbp
,这些变量名也是您的列名。
create procedure get_hr_bp()
begin
declare done int default 0;
declare new_hr, new_sbp, new_dbp numeric(3,0);
declare cur cursor for select hr, sbp, dbp from patient;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
open cur;
read_loop: loop
fetch cur into new_hr, new_sbp, new_dbp;
select new_hr, new_sbp, new_dbp;
if done then
leave read_loop;
end if;
end loop;
close cur;
end;
如果您需要进一步的帮助,请告诉我。
英文:
You are using same variables names hr, sbp, dbp
which are also your column names.
create procedure get_hr_bp()
begin
declare done int default 0;
declare new_hr, new_sbp, new_dbp numeric(3,0);
declare cur cursor for select hr, sbp, dbp from patient;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
open cur;
read_loop: loop
fetch cur into new_hr, new_sbp, new_dbp;
select new_hr, new_sbp, new_dbp;
if done then
leave read_loop;
end if;
end loop;
close cur;
end;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论