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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:24:55+00:00 2026-05-16T05:24:55+00:00

In our Oracle database we have a table called RULES, with a field called

  • 0

In our Oracle database we have a table called RULES, with a field called SQLQUERY. This field is a varchar with an SQL statement stored. The PK is DM_PROJECT.

A typical statement that is stored could be

select ACCOUNTNUMBER from CUSTOMERS where ACCUMULATED_SALES > 500000

I want to do something like this:

select 
  * 
from 
  customers 
where
     accountnumber like 'A%'
  or salesregion = 999
  or accountnumber in
     (
       <run the query SQLQUERY from RULES where DM_PROJECT=:DM_PROJECT>
     )

Can this be done?

(Secondary concern: Can it be done if the stored query uses its own variables, like

select ACCOUNTNUMBER from CUSTOMERS where ACCUMULATEDSALES > :LIMIT 

)

  • 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-16T05:24:56+00:00Added an answer on May 16, 2026 at 5:24 am

    Interesting question indeed. There are two aspects to this.

    The first is whether it’s a good idea. The problem is, the text in the RULE is invisible to the database. It won’t show up in dependency checks, so impact analysis becomes hard. Obviously (or maybe not obviously) the syntax of the Rule can only be verified by running it. This can create problems with adding rules. So it can also be a maintenance problem. And, as we shall see, once you move beyond simplistic queries it is tough to program with.

    The second aspect is whether it’s possible. It is. We need to use dynamic SQL; combining dynamic SQL with static SQL is doable but gnarly.

    create table rules (project_name varchar2(30)
                        , rule_name varchar2(30)
                        , rule_text varchar2(4000) )
    /
    insert into rules 
    values ('SO', 'ACC_SALES'
            , 'select ACCOUNTNUMBER from CUSTOMERS where ACCUMULATED_SALES > 500000 ')
    /
    create table customers (accountnumber number(7,0) 
                            , name varchar2(20)
                            , accumulated_sales number
                            , sales_region varchar2(3))
    /
    insert into customers values (111, 'ACME Industries', 450000, 'AA')
    /
    insert into customers values (222, 'Tyrell Corporation', 550000, 'BB')
    /
    insert into customers values (333, 'Lorax Textiles Co', 500000, 'BB')
    /
    

    This function gets a rule, executes it and returns a numeric array.

    create or replace type rule_numbers as table of number
    /
    
    create or replace function exec_numeric_rule
        ( p_pname in rules.project_name%type
          ,  p_rname in rules.rule_name%type )
        return rule_numbers
    is
        return_value rule_numbers;
        stmt rules.rule_text%type;
    begin
        select rule_text into stmt
        from rules
        where project_name = p_pname
        and   rule_name = p_rname;
    
        execute immediate stmt 
            bulk collect into return_value;
    
        return return_value;
    end exec_numeric_rule;
    /
    

    Let’s test it.

    SQL> select * from customers
      2  where accountnumber in
      3      ( select * from table (exec_numeric_rule('SO', 'ACC_SALES')))
      4  /
    
    ACCOUNTNUMBER NAME                 ACCUMULATED_SALES SAL
    ------------- -------------------- ----------------- ---
              222 Tyrell Corporation              550000 BB
    
    1 row selected.
    
    SQL>
    

    Which is the one and only correct answer.

    But now we come to your suplementary question:

    “Can it be done if the stored query
    uses its own variables”

    Yes it can, but things start to get a bit more brittle. New rule:

    insert into rules 
    values ('SO', 'ACC_SALES_VAR'
            , 'select ACCOUNTNUMBER from CUSTOMERS where ACCUMULATED_SALES > :LMT ')
    /
    

    We amend the function to apply it:

    create or replace function exec_numeric_rule
        ( p_pname in rules.project_name%type
          , p_rname in rules.rule_name%type
          , p_variable in number := null)
        return rule_numbers
    is
        return_value rule_numbers;
        stmt rules.rule_text%type;
    begin
        select rule_text into stmt
        from rules
        where project_name = p_pname
        and   rule_name = p_rname;
    
        if p_variable is null then
            execute immediate stmt 
                bulk collect into return_value;
        else
            execute immediate stmt 
                bulk collect into return_value
                using p_variable;        
        end if;
    
        return return_value;
    end exec_numeric_rule;
    /
    

    Fingers crossed!

    SQL> select * from customers
      2  where accountnumber in
      3      ( select * from table (exec_numeric_rule('SO', 'ACC_SALES_VAR', 480000)))
      4  /
    
    ACCOUNTNUMBER NAME                 ACCUMULATED_SALES SAL
    ------------- -------------------- ----------------- ---
              222 Tyrell Corporation              550000 BB
              333 Lorax Textiles Co               500000 BB
    
    2 rows selected.
    
    SQL>
    

    Okay, so it still works. But you can see that the permutations are not friendly. If you want to pass more than one argument to the RULE then you need more functions or a twistier internal logic. If you want to return sets of dates or strings you need more functions. If you want to pass P_VARIABLE parameters of different data_types you may need more functions. You certainly need some type checking pre-conditions.

    Which comes back to my first point: yes it can be done, but is it worth the hassle?

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

Sidebar

Related Questions

In our Oracle database, I have a table (say MYTABLE) with column VALUE_NUMBER of
I have a data mart mastered from our OLTP Oracle database using basic Materialized
I have a table in an Oracle database that contains actions performed by users
We have a shell script that perform a physical backup of our oracle database
I am using Oracle 10g Enterprise edition. A table in our Oracle database stores
I have a table that exists in an Oracle database, but doesn't show on
I have an Oracle (10.2.0.4) database table with a column which is encrypted by
I need to find all the tables in our Oracle database that have attributes
We ran into serious performance problems with our Oracle database and we would like
I am trying to retrieve CLOB data from our Oracle database. the code is

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.