Subject | Re: [firebird-support] IF ELSE in Select Statement |
---|---|
Author | Helen Borrie |
Post date | 2005-02-28T06:04:15Z |
At 03:35 AM 28/02/2005 +0000, you wrote:
arguments to a SP in order to condition the structure of a query, and it's
total guesswork what you want to happen in the SP...but...this might help
you to get thinking about it.
create procedure blah(i integer)
as
declare field_1 varchar(3);
declare field_2 varchar(3);
declare....variables for all outputs from the select...
begin
if (i = 1) then
select Field_1, Field_2, ...<others>
from aTable
where Field_1 = 'AAA"
into :field_1, field_2, .... ;
else if (1 = 2) then
select Field_1, Field_2, ...<others>
from aTable
where Field_2= 'BBB'
into :field_1, field_2, .... ;
...
end
Or, depending on what you want to do, EXECUTE STATEMENT might come in useful:
create procedure blah(i integer)
as
declare aStatement varchar(200) = '';
declare field_1 varchar(3);
declare field_2 varchar(3);
declare blahblah integer;
begin
aStatement = aStatement || 'select Field_1, Field_2, blahblah from
aTable where ';
if (i = 1) then
aStatement = aStatement || 'Field_1 = ' || ''AAA'' || ' ; /* doubled
single quotes, not double-quotes */
else if (i = 2) then
aStatement = aStatement || 'Field_2 = ' || ''BBB'' || ' ;
execute statement aStatement into :field_1, :field_2, :blahblah;
...
end
./hb
>hi,Well, metadata objects can't be arguments to a SP, so you can't pass
>I want to do something like this:
>
>Select * From TEST_TBT
>If (i=1) Then
> Where Field_1 = 'AAA'
>Else If (i=2) Then
> Where Field_2 = 'BBB'
>
>Note = i is an Input Parameter in procedure. And this Select
>statement is used inside procedure.
arguments to a SP in order to condition the structure of a query, and it's
total guesswork what you want to happen in the SP...but...this might help
you to get thinking about it.
create procedure blah(i integer)
as
declare field_1 varchar(3);
declare field_2 varchar(3);
declare....variables for all outputs from the select...
begin
if (i = 1) then
select Field_1, Field_2, ...<others>
from aTable
where Field_1 = 'AAA"
into :field_1, field_2, .... ;
else if (1 = 2) then
select Field_1, Field_2, ...<others>
from aTable
where Field_2= 'BBB'
into :field_1, field_2, .... ;
...
end
Or, depending on what you want to do, EXECUTE STATEMENT might come in useful:
create procedure blah(i integer)
as
declare aStatement varchar(200) = '';
declare field_1 varchar(3);
declare field_2 varchar(3);
declare blahblah integer;
begin
aStatement = aStatement || 'select Field_1, Field_2, blahblah from
aTable where ';
if (i = 1) then
aStatement = aStatement || 'Field_1 = ' || ''AAA'' || ' ; /* doubled
single quotes, not double-quotes */
else if (i = 2) then
aStatement = aStatement || 'Field_2 = ' || ''BBB'' || ' ;
execute statement aStatement into :field_1, :field_2, :blahblah;
...
end
./hb