SQL Server Performance

Select statment where clause 1=2

Discussion in 'T-SQL Performance Tuning for Developers' started by ssathish, Jun 14, 2004.

  1. ssathish New Member

    Select Col1,Col2 from table1 where 1=2

    is this query is optimized query to get just the table skeletal.
    if not how would i get the table structure.

    will the above query scan all the row in table ?

    Thanks !
    Sathish
  2. thomas New Member

    No it doesn't appear to table scan (checked it on a 40million row table ).

    Tom Pullen
    DBA, Oxfam GB
  3. ssathish New Member

  4. thomas New Member

    Quote from Brad's tip

    "Don't include code that doesn't do anything. This may sound obvious, but I have seen this in some off-the-shelf SQL Server-based applications. For example, you may see code like this:

    SELECT column_name FROM table_name
    WHERE 1 = 0

    When this query is run, no rows will be returned. Obviously, this is a simple example (and most of the cases where I have seen this done have been very long queries), a query like this (or part of a larger query) like this doesn't perform anything useful, and shouldn't be run. It is just wasting SQL Server resources. In addition, I have seen more than one case where such dead code actually causes SQL Server to through errors, preventing the code from even running. [6.5, 7.0, 2000] Added 5-30-2003"

    Well that's true - but if you only run it as a one-off for a certain purpose, it won't harm anything. I think Brad meant in terms of putting it in an application.
    I use this kind of code frequently for generating column lists.


    Tom Pullen
    DBA, Oxfam GB
  5. bradmcgehee New Member

    Tom is correct. I was writing this tip from a purists point of view.

    -----------------------------
    Brad M. McGehee, MVP
    Webmaster
    SQL-Server-Performance.Com

Share This Page