Grab Tablename From Stored SELECT Statement Using Oracle’s REGEXP Functions

I have an sqltext column in my database that contains select statements like

  • select firstname, lastname from employees

  • select firstname, lastname from managers
    where empid = 21

I need to extract the tablenames from these statements. Here is my cheeseball solution:

trim(REGEXP_REPLACE(replace(sqltext,chr(10),' '),
'(.*)( from )(.*)( where .*)' ||case when instr(replace(sqltext,chr(10), ' '), ' where ') = 0 then '?' end,
from someTableThatContainsSqlText

Published in: on December 9, 2010 at 1:18 pm  Leave a Comment  
Tags: , ,

The URI to TrackBack this entry is:

RSS feed for comments on this post.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: