• There is a simple logic behind putting where 1=1 while creating the dynamic queries.

    For ex: for getting user information based on the few conditions like with Firstname or Lastname or both.

    SqlQuery= "Select * from tblUser Where 1=1 ";

    If(firstname is not null)

    SqlQuery=SqlQuery & " And Firstname="+@Firstname ;

    If(Lastname is not null)

    SqlQuery=SqlQuery & " And Lastname ="+@Lastname ;

    Explanation:

    If we have not mentioned Where 1=1 in above query there could be a more logical code we have to write for checking for adding the AND in front of Firstname and Lastname. So for avoiding that logical code and performance point many are using "WHERE 1=1" in the dynamic queries.

    Please let me know it helps you are not.