Click here to Skip to main content
15,900,644 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SQL
Declare @MaxValue int
Select @MaxValue=(case when @MaxValue>marks then @MaxValue else marks end) From [stud].[dbo].[Stud]
Select @MaxValue


How this query will work?
can you help me step by step.

I don't know how this will execute step by step so that i can know its flow of execution.

refer below table


VB
id  marks
1   100
2   200
3   300
4   50
Posted
Updated 10-Dec-13 0:43am
v3

1 solution

Or you could just do this:
SQL
SELECT MAX(marks) FROM MyTable
 
Share this answer
 
Comments
patil.nitin8358 10-Dec-13 6:45am    
Hello OriginalGriff,

Thanks for that, i dont know the query execution step by step

could u please help me.
OriginalGriff 10-Dec-13 6:54am    
What part of it do you not understand? You wrote it!
patil.nitin8358 10-Dec-13 6:59am    
Select @MaxValue=(case when @MaxValue>marks then @MaxValue else marks end) From [stud].[dbo].[Stud]

As it returning @MaxValue=300

how will it compare @MaxValue & marks then finally returning @MaxValue=300
OriginalGriff 10-Dec-13 7:08am    
SELECT operates on all rows in the specified table, so each "marks" column value is compared in turn against the current value of @MaxValue, and @MaxValue is updated in turn.
patil.nitin8358 10-Dec-13 7:31am    
suppose

first row marks=100
@MaxValue>marks i.e. 0 >100 it return false
so @Maxvalue is initialize to 100 so @Maxvalue=100

second row marks=200
@MaxValue>marks i.e. 100 > 200 it return false
so @Maxvalue is initialize to 200 so @Maxvalue=200

Third row marks=300
@MaxValue>marks i.e. 200 > 300 it return false
so @Maxvalue is initialize to 300 so @Maxvalue=300

Fourth row marks=50
@MaxValue>marks i.e. 300 > 50 it return true
so @Maxvalue is initialize to 300 so @Maxvalue=300

Finally it select @Maxvalue which contain 300

Please confirm . Is it correct?

Thanks...

Regards
Nitin

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