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?
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.
0
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>.
0
PortletPaulfreelancerCommented:
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.
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

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

Bye, Olaf.
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
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

0

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 Developer, Architect, and AuthorCommented:
(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
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.