Uma Shankar Patel
When and why you should use 1=1 in WHERE clause?
By Uma Shankar Patel in C# on Jan 24 2014
  • Jignesh Trivedi
    Feb, 2014 24

    When ever you want to build or create dynamic SQL and when you writing condition but you do not know whether your dynamic query has where clause or not. Use where 1=1 to make sure your dynamic query has where clause

    • 1
  • Avikshith Aradhya
    May, 2016 31

    This will select all records from the mentioned table. Usually used in SQL injection to retrieve all data from Table.

    • 0
  • Pankaj  Kumar Choudhary
    May, 2015 25

    when we want to retrieve all record of a table..........

    • 0
  • Vithal Wadje
    Feb, 2014 24

    I some time use 1=1 for checking sql injection attack possibilty

    • 0
  • Vithal Wadje
    Feb, 2014 23

    it shows the all records from table

    • 0
  • Uma Shankar Patel
    Jan, 2014 24

    If you don't know  the list of conditions at compile time and it will built at run time, Then you can made a condition with “where 1=1”. and for other conditions that will affect run time, use
    and .

    1. StringBuilder sb = new StringBuilder();  
    2.          sb.Append("SELECT * FROM Products");  // Your query  
    3.          sb.Append(" WHERE 1=1"); // always true condition  
    4.          // append query's where clause  
    5.          if (catID != 0)  
    6.          {  
    7.              sb.Append(" AND categoryID= {0}", catID);  
    8.          }  
    9.          if (minPrice > 0)  
    10.          {  
    11.              sb.Append(" AND itemPrice >= {0}", minPrice);  
    12.          }  
    13.          SqlCommand cmd = new SqlCommand(sb.ToString(), cnn);  
    14.          SqlDataReader dr = cmd.ExecuteReader();  
    15.          // your code to read data from dr.  

    • 0


Most Popular Job Functions


MOST LIKED QUESTIONS