Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
Hi,
Can anyone tell me what is the use of the condition
1=1 in WHERE Clause?
Thanks in advance
Posted
Comments
Sandeep Mewara 10-Jan-11 3:30am    
Where did you see it?
Hiren solanki 10-Jan-11 9:52am    
good question it is.

auauahua maybe use the parameters without test if append "WHERE" clause or not, i saw in a lot of systems this clause where developers use only for append the clause "AND"

SELECT NAME WHERE 1=1
AND FILTER1="BLA BLA BLA"
AND FILTER2 ="AGA AGA AG"
...

X|
 
Share this answer
 
It's supposed to increase performance on a SQL query that otherwsie doesn't have any conditions. It also allows you to build dynamic queries because with the WHERE 1=1 at the end of your query, all you have to add is AND clauses for your actual conditions.

 
Share this answer
 
v2
Comments
Dylan Morley 10-Jan-11 8:36am    
Yep, I've used it for building dynamic SQL where the user has lots of different options available to search by
Other then JSOP said the nasty SQL injection is also using the same.

Suppose there's one query

SELECT * FROM Table WHERE userid='"+TextBox1.Text+"';


at a time of passing textbox value as hiren' OR '1'='1

will make Query looking like this

SELECT * FROM Table WHERE userid='hiren' OR '1'='1'


for getting list of all user information using SQL Injection.

See some more example of Injection HERE[^], Please use it for good cause for powering security in your query rather then applying Injection for deflating others.
 
Share this answer
 
Comments
Dalek Dave 10-Jan-11 9:48am    
Still can't see it.
I understand the logic, but not the requirement.
Hiren solanki 10-Jan-11 9:50am    
It's just for fetching a records illegally if some one have used inline query rather then parameterized.
Could be just a placeholder for real clause, like "if (true) {/* ... */}"
 
Share this answer
 
Comments
aswathy.s.88 10-Jan-11 2:23am    
I didnt get u
Sergey Alexandrovich Kryukov 10-Jan-11 2:30am    
I think this condition makes no difference. You can always test it by removing this condition. I speculate, somebody just reserved a placeholder to change this condition later into something useful.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900