Question : Using a select statement in side a select count(*) statement

I have an app that displays the results of a query on the web.  To display correctly it needs to know the number of records.  The users define the SQL statements.  To get a record count, I just automatically add a select count(*) from (their select statement here) which works on Oracle and MS-SQL.  It is failing with Sybase.

Example:

user chooses: Select * from COMM

I run SELECT COUNT(*) AS ANT FROM (select * from COMM)

The error I get is:
System.Data.OleDb.OleDbException: [Native Error code: 156] [DataDirect ADO Sybase Provider] Incorrect syntax near the keyword 'select'.
[Native Error code: 102] [DataDirect ADO Sybase Provider] Incorrect syntax near ')'.

Is there a way of writing a select count from the result of another select??

Thanks!

Answer : Using a select statement in side a select count(*) statement

Can you buffer the results of the query in your client app? If so, run the query and just test @@rowcount afterwards for the number of rows.

Another option might be to build your query dynamically based on the query your user builds - replace everything between "SELECT" and "FROM" with "count(*)" and run that using dynamic exec. This is available in 12.0 so will work for you.
Random Solutions  
 
programming4us programming4us