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

  • SEARCH
  • Home
  • 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 405501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:24:21+00:00 2026-05-12T17:24:21+00:00

This is my use case: Input is a string representing an Oracle PL/SQL statement

  • 0

This is my use case: Input is a string representing an Oracle PL/SQL statement of arbitray complexity. We may assume it’s a single statement (not a script).
Now, several bits of this input string have to be rewritten.

E.g. table names need to be prefixed, aggregate functions in the selection list that don’t use a column alias should be assigned a default one:

SELECT SUM(ABS(x.value)), 
TO_CHAR(y.ID,'111,111'),
y.some_col
FROM
tableX x,
(SELECT DISTINCT ID
FROM tableZ z
WHERE ID > 10) y
WHERE
...

becomes

SELECT SUM(ABS(x.value)) COL1, 
TO_CHAR(y.ID,'111,111') COL2,
y.some_col
FROM
pref.tableX x,
(SELECT DISTINCT ID, some_col
FROM pref.tableZ z
WHERE ID > 10) y
WHERE
...

(Disclaimer: just to illustrate the issue, statement does not make sense)

Since aggregate functions might be nested and subSELECTs are a b_tch, I dare not use regular expressions. Well, actually I did and achieved 80% of success, but I do need the remaining 20%.

The right approach, I presume, is to use grammars and parsers.
I fiddled around with c++ ANTLR2 (although I do not know much about grammars and parsing with the help of such). I do not see an easy way to get the SQL bits:

list<string> *ssel = theAST.getSubSelectList(); // fantasy land

Could anybody maybe provide some pointers on how “parsing professionals” would pursue this issue?
EDIT: I am using Oracle 9i.

  • 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-12T17:24:22+00:00Added an answer on May 12, 2026 at 5:24 pm

    Maybe you can use this, it changes an select statement into an xml block:

    declare
        cl clob;
    begin
        dbms_lob.createtemporary (
            cl,
            true
        );
        sys.utl_xml.parsequery (
            user,
            'select e.deptno from emp e where deptno = 10',
            cl
        );
        dbms_output.put_line (cl);
        dbms_lob.freetemporary (cl);
    end;
    / 
    
    <QUERY>
      <SELECT>
        <SELECT_LIST>
          <SELECT_LIST_ITEM>
            <COLUMN_REF>
              <SCHEMA>MICHAEL</SCHEMA>
              <TABLE>EMP</TABLE>
              <TABLE_ALIAS>E</TABLE_ALIAS>
              <COLUMN_ALIAS>DEPTNO</COLUMN_ALIAS>
              <COLUMN>DEPTNO</COLUMN>
            </COLUMN_REF>
            ....
            ....
            ....
    </QUERY>
    

    See here: http://forums.oracle.com/forums/thread.jspa?messageID=3693276&#3693276

    Now you ‘only’ need to parse this xml block.

    Edit1:

    Sadly I don’t fully understand the needs of the OP but I hope this can help (It is another way of asking the ‘names’ of the columns of for example query select count(*),max(dummy) from dual):

    set serveroutput on
    
    DECLARE
     c       NUMBER;
     d       NUMBER;
     col_cnt PLS_INTEGER;
     f       BOOLEAN;
     rec_tab dbms_sql.desc_tab;
     col_num NUMBER;
    
    PROCEDURE print_rec(rec in dbms_sql.desc_rec) IS
    BEGIN
      dbms_output.new_line;
      dbms_output.put_line('col_type = ' || rec.col_type);
      dbms_output.put_line('col_maxlen = ' || rec.col_max_len);
      dbms_output.put_line('col_name = ' || rec.col_name);
      dbms_output.put_line('col_name_len = ' || rec.col_name_len);
      dbms_output.put_line('col_schema_name= ' || rec.col_schema_name);
      dbms_output.put_line('col_schema_name_len= ' || rec.col_schema_name_len);
      dbms_output.put_line('col_precision = ' || rec.col_precision);
      dbms_output.put_line('col_scale = ' || rec.col_scale);
      dbms_output.put('col_null_ok = ');
    
      IF (rec.col_null_ok) THEN
        dbms_output.put_line('True');
      ELSE
        dbms_output.put_line('False');
      END IF;
    END;
    
    BEGIN
      c := dbms_sql.open_cursor; 
      dbms_sql.parse(c,'select count(*),max(dummy) from dual ',dbms_sql.NATIVE); 
      dbms_sql.describe_columns(c, col_cnt, rec_tab);
    
      for i in rec_tab.first..rec_tab.last loop
        print_rec(rec_tab(i));
      end loop;
    
      dbms_sql.close_cursor(c);
    END;
    /
    

    (See here for more info: http://www.psoug.org/reference/dbms_sql.html)

    The OP also want to be able to change the schema name of the table in a query. I think the easiest say to achieve that is to query the table names from user_tables and search in sql statement for those table names and prefix them or to do a 'alter session set current_schema = ....'.

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

Sidebar

Ask A Question

Stats

  • Questions 227k
  • Answers 227k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer setcookie('fayer', FALSE, 1); And make sure to use the same… May 13, 2026 at 1:24 am
  • Editorial Team
    Editorial Team added an answer It looks like you're trying to run 64-bit wxPython on… May 13, 2026 at 1:24 am
  • Editorial Team
    Editorial Team added an answer Browsers by themselves don't do this at all. They only… May 13, 2026 at 1:24 am

Related Questions

In my Java code, I occasionally run into situations where I have to catch
This IS NOT homework, but it is a practice that the teacher gave us
The title of this is kind of awkward; I wasn't really sure how to
Yes, another NULL vs empty string question. I agree with the idea that NULL

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.