Subject RE: [firebird-support] changing "join" for "left join" inside a stored procedure
Author Svein Erling Tysvær
You could do something like

select * from customers
left join sel on sel.id = customers.id
where (sel.id is not null or :MyParam = 1)

This should return the selected records unless MyParam = 1.

In case your next question is that this is a solution that is slow (it can be if customers is a huge table and you return all of it): The optimizer needs to know at prepare time whether it should use an index or not and indexes slow down things if everything is to be returned, whereas they're extremely useful if only parts should be returned. If you want fast response times, then you need to have two separate statements.

HTH,
Set

-----Original Message-----
From: firebird-support@yahoogroups.com [mailto:firebird-support@yahoogroups.com] On Behalf Of Sergio H. Gonzalez
Sent: 11. juni 2010 16:02
To: firebird-support@yahoogroups.com
Subject: [firebird-support] changing "join" for "left join" inside a stored procedure

Hello, is there any way to change some part of a select sentence inside a stored
procedure depending on a parameter?

This is what I need: I have a SP which should return all records or just the
selected records (I select records saving the IDs in a temp table). So I'd like
to change:

select * from customers join sel on sel.id = customers.id

for

select * from customers left join sel on sel.id = customers.id


depending on a parameter I send to the SP. The actual select is bigger than the
example. That's why I'm trying to avoid to write the sentence twice inside an if
statement.

Thanks!!

s