Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8067733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T12:25:19+00:00 2026-06-05T12:25:19+00:00

I have a stored procedure that accepts multiple parameters (i.e. pName, pHeight, pTeam) I

  • 0

I have a stored procedure that accepts multiple parameters (i.e. pName, pHeight, pTeam)

I have the query built up like this:

SQLQuery VARCHAR2(6000);
TestCursor T_CURSOR;

SQLQuery := 'SELECT ID, Name, Height, Team FROM MyTable WHERE ID IS NOT NULL ';


-- Build the query based on the parameters passed.
IF pName IS NOT NULL
  SQLQuery := SQLQuery || 'AND Name LIKE :pName ';
END IF;

IF pHeight IS > 0
  SQLQuery := SQLQuery || 'AND Height = :pHeight ';
END IF;

IF pTeam IS NOT NULL
  SQLQuery := SQLQuery || 'AND Team LIKE :pTeam ';
END IF;


OPEN TestCursor FOR SQLQuery USING pName, pHeight, pTeam;

If I execute the procedure passing all parameters, it runs properly.

But if I only passed one or two of the parameters, then the procedure errors out:

ORA-01006: bind variable does not exist

How do I selectively bind the variable with the parameters based on where the parameter value was used? For example, if only pName was passed, then I would only execute the query:

OPEN TestCursor FOR SQLQuery USING pName;

Or if both pName and pTeam was passed, then:

OPEN TestCursor FOR SQLQuery USING pName, pTeam;

Hope someone can shed more ways to resolve this. Thanks.

Edit:
I could actually use the following:

— Build the query based on the parameters passed.
IF pName IS NOT NULL
SQLQuery := SQLQuery || ‘AND Name LIKE ”’ || pName || ”’ ‘;
END IF;

IF pHeight IS > 0
  SQLQuery := SQLQuery || 'AND Height = pHeight ';
END IF;

IF pTeam IS NOT NULL
  SQLQuery := SQLQuery || 'AND Team LIKE ''' || pTeam || ''' ';
END IF;


OPEN TestCursor FOR SQLQuery;

But this would be VERY vulnerable to SQL Injection…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T12:25:20+00:00Added an answer on June 5, 2026 at 12:25 pm

    This is not hugely elegant but it would mean that you could always supply all three bind variables even if some of them are null. You only add the extra WHERE clauses if needed.

    (I’ve tried to format the dynamic SQL to make it more readable, you could just supply it as one long string).

    FUNCTION myFunc (
       pName   IN VARCHAR2,
       pHeight IN VARCHAR2,
       pTeam   IN VARCHAR2
    )
       RETURN T_CURSOR
    IS
       -- Local Variables
       SQLQuery   VARCHAR2(6000);
       TestCursor T_CURSOR;
    BEGIN
       -- Build SQL query
       SQLQuery := 'WITH t_binds '||
                    ' AS (SELECT :v_name AS bv_name, '||
                               ' :v_height AS bv_height, '||
                               ' :v_team AS bv_team '||
                          ' FROM dual) '||
                   ' SELECT id, '||
                          ' name, '||
                          ' height, '||
                          ' team '||
                     ' FROM MyTable, '||
                          ' t_binds '||
                    ' WHERE id IS NOT NULL';
    
       -- Build the query WHERE clause based on the parameters passed.
       IF pName IS NOT NULL
       THEN
         SQLQuery := SQLQuery || ' AND Name LIKE bv_name ';
       END IF;
    
       IF pHeight > 0
       THEN
         SQLQuery := SQLQuery || ' AND Height = bv_height ';
       END IF;
    
       IF pTeam IS NOT NULL
       THEN
         SQLQuery := SQLQuery || ' AND Team LIKE bv_team ';
       END IF;
    
       OPEN TestCursor 
        FOR SQLQuery 
      USING pName, 
            pHeight, 
            pTeam;
    
       -- Return the cursor
       RETURN TestCursor;
    END myFunc;
    

    I’m not in front of a workstation with DB access so I can’t test the function but it should be close (please forgive any syntax errors, it’s been a long day!)

    Hope it helps…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

SQL Server 2005. I have a stored procedure which accepts a few parameters that
I have a stored procedure that takes no parameters, and it returns two fields.
I have a stored procedure that logs some data, how can I call this
i have a stored procedure that has to retrieve data from multiple tables something
I have a requirement to write a stored procedure that accepts a start date,
All, I have a stored procedure on SQL Server 2005 that accepts an XML
I have a stored procedure that returns multiple resultsets just as below CREATE StoredProcedure
I have a stored procedure that receives 2 parameters. @username VARCHAR(8), @xmlShiftDays XML I
I have a stored procedure that accepts a date input that is later set
I have stored procedures (they accept parameters) that return multiple result sets and I'd

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.