如果能够在Firebird存储过程中正确识别空字符串

时间:2017-03-15 19:18:27

标签: sql stored-procedures firebird firebird2.5

我正在尝试创建一个程序来自动化某些查询,这些查询是我正在创建的搜索工具的一部分。

create or alter procedure buscaExactPaisSS
    (palavra varchar(500))
returns
    (seriePK integer)
as
begin
    if (:palavra is null or :palavra = '') then
    begin
        for
            select seriesetorial.codserie
                from seriesetorial
                where codserie not in (select codserie from localidaderelset)
            into :seriePK
        do
        begin
            suspend;
        end
    end
    else
    begin
        for
            select seriesetorial.codserie
                from seriesetorial, localidade, localidaderelset
                where lower(localidade.nome) = lower(:palavra)
                    and localidaderelset.codlocalidade = localidade.codlocalidade
                    and seriesetorial.codserie = localidaderelset.codserie
                into :seriePK
        do
        begin
            suspend;
        end
    end
end!

但是,该过程始终返回第一种情况的结果,即palavra为空或“#”时的结果。这是为什么?我该如何解决这个问题?

编辑 - 2017年3月16日:

这个功能实际上是对的。希望这至少可以作为FB的PSQL可变使用的工作程序的一个例子。

1 个答案:

答案 0 :(得分:2)

问题出在您的代码存储过程之外。如果你稍微简化它,你会发现两个条件都有机会正确执行:

Item.name = "thisCouldBeAnything"

尝试#1:

CREATE OR ALTER PROCEDURE BUSCAEXACTPAISSS (PALAVRA VARCHAR(500))
RETURNS (
    SERIEPK INTEGER
)
AS
begin
    if (:palavra is null or :palavra = '') then
    begin
        for
            select 0 FROM someTable
            into :seriePK
        do
        begin
            suspend;
        end
    end
    else
    begin
        for
            select 1 FROM someTable
                into :seriePK
        do
        begin
            suspend;
        end
    end
end;
  

1

尝试#2:

EXECUTE PROCEDURE buscaExactPaisSS '77'
  

0