Tsql , why use where 1-1 in Tsql query ?

I've seen seen in one of our developed t-sql query code were written with where  1=1 and *****.

I just don't  get it why it must with where 1=1 and then only followed by the actual condition, mind someone here to explain.
motioneyeAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Brian CroweDatabase AdministratorCommented:
Usually that is used in dynamic sql so each subsequent clause in the WHERE statement can be added without having to determine whether it is the first clause or not.  If you start with WHERE 1=1 then every subsequent clause can be added with "AND ..." without worrying about a syntax error.
Karthik RamachandranCommented:
If the list of conditions is unknown at the compile time of the TSQL and if it is built at run time, you don't have to worry about whether you have one or more than one condition. You can keep simply adding AND <cond>.
PortletPaulEE Topic AdvisorCommented:
This is not T-SQL specific by the way, it is true of any SQL.

& It is also just a convention.
It could be 'a' = 'a' that gets used, but 1=1 is way easier to type :)

no points pl.
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

Olaf DoschkeSoftware DeveloperCommented:
Other databases just accept true , though, instead of needing a logical expression evaluating to true.

Bye, Olaf.
Jim HornMicrosoft SQL Server Data DudeCommented:
Barring any actual reason identified above, it's a handy way to identify amateur programmers.

I have seen 0 = 1 before, usually to create a new table whose schema matches an existing table, but without populating it with any rows.

SELECT * FROM OldTable INTO NewTable WHERE 0 = 1

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jim HornMicrosoft SQL Server Data DudeCommented:
(another possibility based on my current gig that I started after the last comment)
Maybe the client has a big honkin' UI that parses dynamic SQL based on user selections, and if the user doesn't select anything relevant then the UI passes a WHERE 1 = 1.   Uglo-riffic!

Thanks for the split, good luck with your project.  -Jim
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.