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 7172837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:41:56+00:00 2026-05-28T15:41:56+00:00

I currently have a prepared statement in Java which uses the following SQL statement

  • 0

I currently have a prepared statement in Java which uses the following SQL statement in the WHERE clause of my query, but I would like to re-write this into a function to limit the user parameters passed to it and possibly make it more efficient.

(
  (USER_PARAM2 IS NULL AND 
    ( COLUMN_NAME = nvl(USER_PARAM1, COLUMN_NAME) OR 
      (nvl(USER_PARAM1, COLUMN_NAME) IS NULL)
    )
  ) 
  OR 
  (USER_PARAM2 IS NOT NULL AND COLUMN_NAME IS NULL)
)

USER_PARAM1 and USER_PARAM2 are passed into the prepared statement by the user.
USER_PARAM1 represents what the application user wants to search this particular COLUMN_NAME for. If the user does not include this parameter, it will default to NULL.
USER_PARAM2 was my way to allow a user to request a NULL value only search on this COLUMN_NAME. Additionally I have some server logic that sets USER_PARAM2 to ‘true’ if passed in by the user or NULL if it wasn’t specified by the user.

The intended behavior is that if USER_PARAM2 was declared then only COLUMN_NAME values of NULL are returned. If USER_PARAM2 wasn’t declared and USER_PARAM1 was declared then only COLUMN_NAME = USER_PARAM1 are returned. If neither user params are declared then all rows are returned.

Could anyone help me out on this?
Thanks in advance…

EDIT:
Just to clarify this is how my current query looks (without the other WHERE clause statements..)

SELECT * 
FROM TABLE_NAME
WHERE (
  (USER_PARAM2 IS NULL AND 
    ( COLUMN_NAME = nvl(USER_PARAM1, COLUMN_NAME) OR 
      (nvl(USER_PARAM1, COLUMN_NAME) IS NULL)
    )
  ) 
  OR 
  (USER_PARAM2 IS NOT NULL AND COLUMN_NAME IS NULL)
)

… and this is where I would like to get to…

SELECT *
FROM TABLE_NAME
WHERE customSearchFunction(USER_PARAM1, USER_PARAM2, COLUMN_NAME)

EDIT #2:
OK, so another co-worker helped me out with this…

CREATE OR REPLACE function searchNumber (pVal IN NUMBER, onlySearchForNull IN CHAR, column_value IN NUMBER)
  RETURN NUMBER
IS
BEGIN
  IF onlySearchForNull IS NULL THEN 
    IF pVal IS NULL THEN
      RETURN 1;
    ELSE
      IF pVal = column_value THEN
        RETURN 1;
      ELSE
        RETURN 0;
      END IF;  
    END IF;  
  ELSE 
    IF column_value IS NULL THEN
      RETURN 1;
    ELSE
      RETURN 0;
    END IF;  
  END IF;
END;

… this seems to work in my initial trials..

SELECT * 
FROM TABLE_NAME
WHERE 1=searchNumber(USER_PARAM1, USER_PARAM2, COLUMN_NAME);

… the only issues I have with it would be
1)possible performance concerns vs the complex SQL statement I started with.
2)that I would have to create similar functions for each data type.
However, the latter would be less of an issue for me.

EDIT #3 2012.02.01

So we ended up going with the solution I chose below, while using the function based approach where code/query cleanliness trumps performance. We found that the function based approach performed roughly 6x worse than using pure SQL.

Thanks everyone for the great input everyone!

EDIT #4 2012.02.14

So looking back I noticed that applying the virtual table concept in @Alan’s solution with the clarity of @danihp’s solution gives a very nice overall solution in terms of clarity and performance. Here’s what I now have

WITH   params AS (SELECT user_param1 AS param, user_param2 AS param_nullsOnly FROM DUAL)
SELECT *
FROM   table_name, params p
WHERE  ( nvl(p.param_nullsOnly, p.param) IS NULL                  --1)
         OR p.param_nullsOnly IS NOT NULL AND column_name IS NULL --2)
         OR p.param IS NOT NULL AND column_name = p.param         --3)
       )
-- 1) Test if all rows should be returned
-- 2) Test if only NULL values should be returned
-- 3) Test if param equals the column value

Thanks again for the suggestions and comments!

  • 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-05-28T15:41:57+00:00Added an answer on May 28, 2026 at 3:41 pm

    There’s a simple way of to pass your parameters only once and refer to them as many times as needed, using common-table expressions:

    WITH params AS (SELECT user_param1 AS up1, user_param2 AS up2 FROM DUAL)
    SELECT *
    FROM   table_name, params p
    WHERE  ((p.up2 IS NULL 
             AND (column_name = NVL(p.up1, column_name) 
                  OR (NVL(p.up1, column_name) IS NULL)))
            OR (p.up2 IS NOT NULL AND column_name IS NULL))
    

    In effect, you’re creating a virtual table, where the columns are your parameters, that is populated with a single row.

    Conveniently, this also ensures that all of your parameters are collected in the same place and can be specified in an arbitrary order (as opposed to the order that the naturally appear in the query).

    There are a couple big advantages to this over a function-based approach. First, this will not prevent the use of indexes (as pointed out by @Bob Jarvis). Second, this keeps the query’s logic in the query, rather than hidden in functions.

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

Sidebar

Related Questions

I am currently getting the error, java.sql.SQLException: Method 'executeQuery(String)' not allowed on prepared statement.
I currently have a functioning in-house Windows Forms application which extensively uses the DataGridView
G'day! I have one million different words which I'd like to query for in
I would like to use prepared statements, for many reasons . But, I would
I currently have to implement a query on a postgres database using a prepared
I currently have a fairly robust server-side validation system in place, but I'm looking
We currently have code like this: Dim xDoc = XDocument.Load(myXMLFilePath) The only way we
I have a form, the purpose of which is to place the currently displayed
I have written the following function in Java. This function returns current timestamp by
Currently, I have a table being read from like so: ps = con.prepareStatement(SELECT *

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.