Computing.Net > Forums > Unix > getting table names from sqls

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

getting table names from sqls

Reply to Message Icon

Name: arulactive
Date: September 17, 2009 at 10:39:55 Pacific
OS: Windows Vista
CPU/Ram: 12
Product: Sun microsystems Unix essentials featuring the solaris 10 operating system -
Subcategory: General
Tags: solaris
Comment:

Can some one help me in getting the table names from sql's

e.g
select d.acc_no, d.mon ,
case
when prev.balance < 1 then U
when prev.balance > 1 then L
else X
end bal_rate,
opening_balance
from cust_bill cb
join
cust_bill_2 d
on cb.acc_no = d.acc_no
and cb.mon = d.mon
left outer join cust_bill prev
on prev.acc_no = d.acc_no

Then table names are cust_bill, cust_bill_2 , cust_bill
corresponding aliases are cb, d and prev
Can this be done in unix?. Since the table names and aliases are wrapping each other
i find it more difficult to parse it. Like this I have many sqls.



Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: September 17, 2009 at 13:36:12 Pacific
Reply:

This nawk script searches for the "from" and "join" keywords and decides whether the table name is on the same line or the next line. This breaks if you have the "join" or "from" keyword in a comment:

#!/bin/ksh

nawk ' BEGIN { nfrom=0; }
{
if(NF == 0) # skip blank lines
   continue

if(nfrom == 1)
   { # print the table name form the next line
   printf("%s\n", $1)
   nfrom=0
   continue
   }

if( $1 ~ /from/)
  {
  if(NF > 1)
     # table name after from keyword
     printf("%s\n", $2)
  else
     # table name on next line, get it
     nfrom=1

   continue
   }

# if join ends the string get the next line
if( $1 ~ /join$/)
  {
  nfrom=1
  continue
  }

# join does not start the line
if( $0 ~ /join/)
  # table name after join keyword, table name second field from end
  printf("%s\n", $(NF-1) )

} ' sqlfile.txt


1

Response Number 2
Name: arulactive
Date: September 17, 2009 at 20:13:59 Pacific
Reply:

Thanks nails..It works well. some exceptions are there

e.g the last 2 lines may be in a single line.

i.e left outer join cust_bill prev on prev.acc_no = d.acc_no

in this case it picks up the "=" sign.

So now the expectation is

if from/join is $1, then $2 is my table name ( ie NF > 1)

if from/join is $NF, then $1 in next line is my table name

if from/join is anywhere in the line, then the next word will be my table name.

How to search for the next word?


0

Response Number 3
Name: nails
Date: September 17, 2009 at 21:08:05 Pacific
Reply:

Wow! you are making my life more difficut <grin>

In the case where the "join" keyword doesn't end the string, change the algorithm finding the field number of the "join" keyword. Grab the next field which will be the table name:

#!/bin/ksh

nawk ' BEGIN { nfrom=0; }
{
if(NF == 0) # skip blank lines
   continue

if(nfrom == 1)
   {
   printf("%s\n", $1)
   nfrom=0
   continue
   }

if( $1 ~ /from/)
  {
  if(NF > 1)
     # table name after from keyword
     printf("%s\n", $2)
  else
     # table name on next line, get it
     nfrom=1

   continue
   }

# if join ends the string get the next line
if( $1 ~ /join$/)
  {
  nfrom=1
  continue
  }

if( $0 ~ /join/)
  {
  # find the field that contains join
  # grab the next field
  for(i=1; i<= NF; i++)
     if($i == "join")
        printf("%s\n", $(i+1) )
  }
} ' sqlfile.txt


1

Response Number 4
Name: arulactive
Date: September 18, 2009 at 10:53:15 Pacific
Reply:

hi nails..thanks a lot..
your way of solving things is different from others..
its unique and much appreciated !!!.


0

Response Number 5
Name: nails
Date: September 21, 2009 at 08:19:35 Pacific
Reply:

arulactive:

Thank you for the kind words; it's much appreciated.

Someday I will know awk as good as Ghostdog. :)

Regards,

Nails


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon

translation of ' in syste... Merging two files by matc...


Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: getting table names from sqls

get process name from pid www.computing.net/answers/unix/get-process-name-from-pid/1742.html

Extracting characters from a field www.computing.net/answers/unix/extracting-characters-from-a-field/6850.html

unix sqlplus appworx www.computing.net/answers/unix/unix-sqlplus-appworx/8434.html