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:

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

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

The URI to TrackBack this entry is: https://dmcmeans.wordpress.com/2010/12/09/grab-tablename-from-select-statement-using-oracles-regexp-functions/trackback/

RSS feed for comments on this post.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: