Click here to Skip to main content

The Weird and The Wonderful

   

The Weird and The Wonderful forum is a place to post Coding Horrors, Worst Practices, and the occasional flash of brilliance.

We all come across code that simply boggles the mind. Lazy kludges, embarrasing mistakes, horrid workarounds and developers just not quite getting it. And then somedays we come across - or write - the truly sublime.

Post your Best, your worst, and your most interesting. But please - no programming questions . This forum is purely for amusement and discussions on code snippets. All actual programming questions will be removed.

 
GeneralRe: for(int i=0; i<size; i++) PinmemberPeterTheSwede19-May-13 23:58 
GeneralRe: for(int i=0; i<size; i++) PinmemberJohn Hunley29-May-13 8:46 
GeneralRe: for(int i=0; i<size; i++) PinmemberBCantor31-May-13 9:54 
GeneralInteresting Observation Pinmembervirang_2129-Apr-13 14:45 
I was looking at one old SQL stored proc and found this strange behaviour.
 
 
DECLARE @Scrap_Qty int
DECLARE @FilledQty int
SET @Scrap_Qty=10 --Change this to 1 and it still run
SET @FilledQty = 2
 
BEGIN
 
IF @Scrap_Qty  > @FilledQty
 declare @currDate varchar(10)   
 Set @currDate= Convert(VARCHAR(10),GetDate(),103)  
 PRINT @currDate
 
END  
 
 
Look at the if condition .. it does not have BEGIN..END... To make thing interesting weather if condition is true or false you will get the output for parameter @currDate
 
If I add PRINT @Scrap_Qty right after IF and run it again it will only print value for @Scrap_Qty if condition is true. Which leads to question why sql is not complaining about undeclared variable if @Scrap_Qty is less than @FilledQty ?
 
I would have assumed if condition without BEGIN .. END block will behave like a normal one line to execute if condition is true which in above block is declaration of @currDate variable.
 
DECLARE @Scrap_Qty int
DECLARE @FilledQty int
SET @Scrap_Qty=10 --Change this to 1 and it won't output @Scrap_Qty
SET @FilledQty = 2
 
BEGIN
 
IF @Scrap_Qty  > @FilledQty
	PRINT @Scrap_Qty
 declare @currDate varchar(10)   
 Set @currDate= Convert(VARCHAR(10),GetDate(),103)  
 PRINT @currDate
 
END  
Zen and the art of software maintenance : rm -rf *
 
Math is like love : a simple idea but it can get complicated.

GeneralRe: Interesting Observation PinmemberPablo Aliskevicius29-Apr-13 19:47 
GeneralRe: Interesting Observation Pinmemberagolddog1-May-13 3:42 
GeneralRe: Interesting Observation PinprofessionalRaja Sekhar S5-Jun-13 21:14 
GeneralRe: Interesting Observation PinmemberPablo Aliskevicius5-Jun-13 21:21 
GeneralRe: Interesting Observation PinprofessionalRaja Sekhar S5-Jun-13 22:05 
GeneralRe: Interesting Observation PinmemberPablo Aliskevicius5-Jun-13 23:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.


Advertise | Privacy | Mobile
Web02 | 2.8.140827.1 | Last Updated 2 Sep 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid