ANSI Sql query to force return 0 records ANSI Sql query to force return 0 records postgresql postgresql

ANSI Sql query to force return 0 records


What about adding your own SELECT around the user's SELECT?

SELECT * FROM (select  c.lastname,  sum(cs.amount)from customersales csjoin customers c on c.idcustomer=cs.idcustomer/* where 1=0 */group by c.idcustomer, c.lastname) xWHERE 0=1

EDIT: ORDER BY would not work with that solution, but since you get no rows, you could try to remove that from the query when necessary.


For future reference in case people end up here with a different goal: Note that making the WHERE-clause a contradiction can cause the optimizer to decide to not execute the sub-plan at all. So if you need some side-effects of the query (be it warm a cache, execute a procedure, whatever), be advised. :-)


if your using MSSQL Server, then you can wrap your query around SET FMTONLY

SET FMTONLY ON SELECT * FROM tablename SET FMTONLY OFF