I have string like below. I have to get all table name from this.
select SEQ_NO,CODE,CD_NAME,CD_TYPE,CD_CITY,CDS_STATUS,CDS_SUBSTATUS,
to_char(CDS_LAST_MOD_DATE,'dd/mm/yyyy') as CDS_LAST_MOD_DATE from
company_details left outer join on company_details_status where
cd_seq_no=cds_seq_no,CODE=(select CODE from company_details where cd_seq_no='1' ) order by CDS_LAST_MOD_DATE.
Insert into table1 value(?,?)
Insert into table1 (col1,col2) values(?,?)
How to get table name for insert query like above.
Here is a short code which hints at how you might approach this problem. It uses a regex matcher with the following pattern:
The aim here is to capture table names whenever they occur in select statements (after
FROM
andJOIN
), or in insert statements (afterINSERT INTO
). Note that the pattern uses an alternation with three capture groups.Demo
Obviously there are many edge cases I may have missed. In general, for a complete solution you might have to write an actual SQL parser. For simple select and insert queries, this could be a good starting point.