tags:

views:

13

answers:

1

I am preparing stored procedure with oracle .I m using multiple inner joins with the same table where an input parameter value is checked within each inner join. I want to eliminate particular inner join if the input parameter is null

A: 

You would need to use dynamic SQL to construct the appropriate query for the parameters like this:

PROCEDURE myproc (p1 VARCHAR2) IS
   l_sql LONG;
   l_cursor SYS_REFCURSOR;
BEGIN
   l_sql := 'SELECT a, b, c FROM table1';
   IF p1 IS NOT NULL THEN
      l_sql := l_sql || ' JOIN table2 ON table2.x = table1.x';
   END IF;
   l_sql := l_sql || ' WHERE table1.y = :bind1';
   OPEN l_cursor FOR l_sql USING 123;
   ...
END;
Tony Andrews