Subject [IBO] Re: Extracting Table names from a SQL select statement
Author Svein Erling Tysvær
Parsing to find table names isn't difficult, after all, there's just a few options where table names can be specified. The options I can think of (from the top of my head) are:

FROM <TableName> [<alias>][[,] <TableName> [<alias>]]...

and

JOIN <TableName> [<alias>]

So just look for the first token after FROM and JOIN, and when FROM you should additionally check if there's a comma in the next two tokens and if so treat the following token as a table name as well.

I think this should work equally well for subselects (including when used in common table expressions), the only thing you will not be able to, is to separate between views and tables (and you will not find the underlying table name for a view).

Set

-----Original Message-----
From: IBObjects@yahoogroups.com [mailto:IBObjects@yahoogroups.com] On Behalf Of sshowl09
Sent: 8. mars 2009 14:02
To: IBObjects@yahoogroups.com
Subject: [IBO] Re: Extracting Table names from a SQL select statement


Thanks for the help Helen and Markus.

I need to take any SQL statement and extract from it all of the table names, as a stringlist perhaps.

Irrespective of the validity of the query (although obviously totally malformed SQL can't be parsed obviously) and irrespective of whether all, some or none of the tables actually exist.

I was hoping there might be a simple way to do it using IBObjects (which is a great library).

What I am hearing is I can either

1 - get into the parsing of the SQL statement string, in Markus' example leveraging off IB_Parse.

or

2 - Extract the table names from the exception messages that are raised when a table doesn't exist, whether this would work for tables that do exist, and aren't causing a problem I'm not sure.

Hmmmm.

Cheers
Sam H



------------------------------------

___________________________________________________________________________
IB Objects - direct, complete, custom connectivity to Firebird or InterBase
without the need for BDE, ODBC or any other layer.
___________________________________________________________________________
http://www.ibobjects.com - your IBO community resource for Tech Info papers,
keyword-searchable FAQ, community code contributions and more ! Yahoo! Groups Links