光标仅返回空值。

huangapple go评论68阅读模式
英文:

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;

huangapple
  • 本文由 发表于 2023年4月17日 17:56:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76033875.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定