huhu78
huhu78

Reputation: 389

sp_cursorprepare 'SELECT ........ WHERE 0 = 1' - what for?

SQL Server Profiler shows that our ERP application sends quite a lot commands to DB server which look like:

declare @p1 int
set @p1=NULL
declare @p5 int
set @p5=16388
declare @p6 int
set @p6=8196
exec sp_cursorprepare @p1 output,NULL,N'SELECT * FROM SomeTable WHERE 0 = 1',1,@p5 output,@p6 output
select @p1, @p5, @p6

To emphasize:

exec sp_cursorprepare and then SELECT (...) WHERE 0 = 1

Does it make any sense? Is this a kind of trick?

Upvotes: 1

Views: 1326

Answers (1)

Tab Alleman
Tab Alleman

Reputation: 31785

When an app generates SQL statements with "WHERE 0=1" or "WHERE 1=1", it's usually being used as placeholder so that the app can dynamically build the rest of the WHERE clause using AND or OR without having to figure out which condition is first and needs to immediately follow "WHERE".

So yes, it's a trick to make building dynamic WHERE clauses easier for the app developer. I've used it myself.

Upvotes: 3

Related Questions