Operating System - HP-UX
1823249 Members
3159 Online
109648 Solutions
New Discussion

grepping for table_name in sql statement

 
Ian McClement_1
Occasional Advisor

grepping for table_name in sql statement

Hi,
Can someone suggest a way to grep out the tables names from an sql statement. (Table names all "T....", and sql atatement all on one line)
eg: would return TW4S0 and TW4R0 from below.
SELECT POLICY_TRANSACTION FROM TW4S0 WHERE(POLICY_NUMBER=:b1)AND(POLICY_TRANSACTION=:b2)AND SEQUENCE_NUM=(SELECT MAX(SEQUENCE_NUM)FROM TW4R0 WHERE POLICY_NUMBER=:b1 AND POLICY_
TRANSACTION=:b2)

Thanks
Ian
1 REPLY 1
Rodney Hills
Honored Contributor

Re: grepping for table_name in sql statement

Save the following program as print_table.pl

while(<>) {
while(/FROM (\w+)/ig) { print $1,"\n"; }
}

Then enter
perl print_table.pl your_sql_select_file

Where your_sql_select_file has the select statement. This will print one line be table name that follows a "from" clause (upper or lowercase).
There be dragons...