IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

Use of predicate clause WHERE 1=1


IBM Mainframe Forums -> DB2
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
bipinpeter

Active User


Joined: 18 Jun 2007
Posts: 213
Location: Cochin/Kerala/India

PostPosted: Wed Jul 29, 2009 8:52 am
Reply with quote

Hi All,
In some of the SQLs i saw below predicate clause

Code:
WHERE 1=1


Whats is the use of this condition? Is any advantage or disadvantage for this??
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Wed Jul 29, 2009 9:01 am
Reply with quote

Hello,

It is a way to force an equal in a query. . . One will always equal one. . .

I don't know that i'd think in terms of an advantage or a disadvantage.

Suggest you not consider this outside the scope of the query using it.
Back to top
View user's profile Send private message
bipinpeter

Active User


Joined: 18 Jun 2007
Posts: 213
Location: Cochin/Kerala/India

PostPosted: Wed Jul 29, 2009 9:24 am
Reply with quote

Hi Dick,
I know that 1 is always equal to 1. Then Why we should use this is in a query. I dont think without any valid reason we are using that.

Behind this query there is some reason is there.
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Wed Jul 29, 2009 9:34 am
Reply with quote

Hello,

Suggest for more useful replies, it will help if you post the complete query icon_wink.gif
Back to top
View user's profile Send private message
bipinpeter

Active User


Joined: 18 Jun 2007
Posts: 213
Location: Cochin/Kerala/India

PostPosted: Wed Jul 29, 2009 10:00 am
Reply with quote

Hi Dick,
My query is like below,

Code:
DELETE FROM  tab1 WHERE 1=1
Back to top
View user's profile Send private message
Terry Heinze

JCL Moderator


Joined: 14 Jul 2008
Posts: 1249
Location: Richfield, MN, USA

PostPosted: Thu Jul 30, 2009 7:00 am
Reply with quote

The only reason I can see for including the WHERE predicate in this case would be to remind the next maintainer that a WHERE condition is an option when deleting rows of a table. Kind of like using a place holder. As written, ALL rows will be deleted.
Back to top
View user's profile Send private message
bipinpeter

Active User


Joined: 18 Jun 2007
Posts: 213
Location: Cochin/Kerala/India

PostPosted: Thu Jul 30, 2009 9:04 am
Reply with quote

Hi Terry,
Then what is the difference betwen below two codes?


Code:
DELETE FROM  tab1 WHERE 1=1



Code:
DELETE FROM  tab1
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Thu Jul 30, 2009 9:37 am
Reply with quote

Hello,

They should both delete all of the rows.
Back to top
View user's profile Send private message
bipinpeter

Active User


Joined: 18 Jun 2007
Posts: 213
Location: Cochin/Kerala/India

PostPosted: Thu Jul 30, 2009 4:56 pm
Reply with quote

I know that they both will delete all the rows.But what is the difference coming there?
Back to top
View user's profile Send private message
dbzTHEdinosauer

Global Moderator


Joined: 20 Oct 2006
Posts: 6966
Location: porcelain throne

PostPosted: Thu Jul 30, 2009 5:10 pm
Reply with quote

if your where clauses are always satifisfied (where 1 = 1),
that is the same as having no where clause.

terry may be correct in that the original code was written by an expert,
reminding the next pgmr.

but, I imagine it was written by someone who did not understand that
you do not need a where clause.....
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> DB2

 


Similar Topics
Topic Forum Replies
No new posts To search DB2 table based on Conditio... DB2 1
No new posts NOT IN clause in COBOL pgm COBOL Programming 8
No new posts SUSBSCRIPT WITH SIGN IN PIC CLAUSE COBOL Programming 3
No new posts usage of CASE in WHERE clause DB2 10
No new posts Cobol redefines for Signed pictured c... COBOL Programming 4
Search our Forums:

Back to Top