Montar cláusula SQL em SP do Firebird

Delphi

05/12/2007

Galera,
Estou trocando uma metologia de querys que, ao invés de fazer direto no delphi, sql.add... estou passando tudo para stored procedure.

Porém não consigo ´concatenar´ a cláusula sql. Por exemplo: se eu passar um código, esse código faz parte do where. Caso contrário não tem where.

Coloquei um pedaço do código da SP, que já dá erro no IBExpert de cara, na linha do [b:c7860c4f99]promocao where 1 = 1 + xwhere + xordem[/b:c7860c4f99]

Se alguém puder ajudar !!!

CREATE PROCEDURE SALL_PROMOCAO (
    pcodprm integer,
    pordem varchar(100))
returns (
    codprm integer,
    desprm blob sub_type 1 segment size 80)
as
declare variable xwhere varchar(100);
declare variable xordem varchar(100);
begin
  xwhere = ´´;
  xordem = ´´;

  if (:pcodprm is not null) then
    xwhere = ´ and codprm = ´+cast(:pcodprm as varchar(10));

  if (:pordem is not null) then
    xordem = ´ order by ´+pordem;

  for select codprm,
             desprm
      from promocao where 1 = 1 + xwhere + xordem
      into :codprm,
           :desprm
  do
  begin
    suspend;
  end
end



Powerlog Tecnologia

Powerlog Tecnologia

Curtidas 0

Respostas

Powerlog Tecnologia

Powerlog Tecnologia

05/12/2007

Galera, achei uma outra forma e agora funcionou ...

CREATE PROCEDURE SALL_PROMOCAO (
    PCODPRM INTEGER,
    PORDEM VARCHAR(100))
RETURNS (
    CODPRM INTEGER,
    DESPRM BLOB SUB_TYPE 1 SEGMENT SIZE 80)
AS
DECLARE VARIABLE SQLTEXT VARCHAR(8192);
begin
  sqltext = ´ select codprm, desprm ´;
  sqltext = sqltext || ´ from promocao ´;
  sqltext = sqltext || ´ where 1 = 1 ´;

  if ((:pcodprm is not null) and (:pcodprm > 0)) then
    sqltext = sqltext || ´ and codprm = ´|| :pcodprm;

  if ((:pordem is not null) and (:pordem > ´´)) then
    sqltext = sqltext || ´ order by = ´|| :pordem;

  for execute statement sqltext
      into :codprm,
           :desprm
  do
  begin
    suspend;
  end
end


Bem, pelo menos serve de exemplo aqui no fórum :wink:


GOSTEI 0
POSTAR