1

This query is from the answer here. There is a (1=1) in the where clause, but the execution plan is the same (SQL Express 2008R2).

I'm guessing it is an artifact from the old days?

SELECT 
     ind.name 
    ,ind.index_id 
    ,ic.index_column_id 
    ,col.name 
    ,ind.* 
    ,ic.* 
    ,col.* 
FROM sys.indexes ind 

INNER JOIN sys.index_columns ic 
    ON  ind.object_id = ic.object_id and ind.index_id = ic.index_id 

INNER JOIN sys.columns col 
    ON ic.object_id = col.object_id and ic.column_id = col.column_id 

INNER JOIN sys.tables t 
    ON ind.object_id = t.object_id 

WHERE (1=1) 
    AND ind.is_primary_key = 0 
    AND ind.is_unique = 0 
    AND ind.is_unique_constraint = 0 
    AND t.is_ms_shipped = 0 
ORDER BY 
    t.name, ind.name, ind.index_id, ic.index_column_id 
Community
  • 1
  • 1
surfasb
  • 968
  • 1
  • 13
  • 31
  • 1
    DUP: [why would someone use WHERE 1=1 AND in a SQL clause?](http://stackoverflow.com/questions/242822/why-would-someone-use-where-1-1-and-conditions-in-a-sql-clause) – Tim Schmelter Apr 14 '13 at 21:41
  • (1=1) equals true, try removing that where statement and see what happens. – DragonZero Apr 14 '13 at 21:41
  • Sometimes 1 = 1, or 1=2 is used to do something that will always return either true or false to help the original programmer develop their logic. Then they simply leave it there. – Dan Bracuk Apr 15 '13 at 02:08

1 Answers1

9

When generating dynamic SQL from code, it can be useful to just start with a WHERE (1=1) then concatenate\append AND <Condition> as needed. Not sure if that's the case with that answer, but perhaps it was based on some code for generating SQL.

mutex
  • 7,536
  • 8
  • 45
  • 66
  • They do that at my job for that reason. I, personally, think it's the lamest thing I've ever seen. But I code it like that at my job to fit in with the other developers on my team. There's being right, then there's being cool. – Russell Hankins Oct 04 '16 at 15:16