Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
We use CASE statement in SQL for a bool condition to get TRUE or FALSE. But in this situation I need non-bool means n'th condition and result. Actually bold part in following code. Please help. It's urgent...

What I have tried:

SQL
ALTER proc [dbo].[sp_StudentList](@CreatedBy nvarchar(max))
as

begin
declare @LikedBy nvarchar(max)=(Select LikedBy from LikeStatus)
declare @TeacherRequestID int=(Select TeacherRequestID from LikeStatus where LikedBy=@CreatedBy)

declare @UserName nvarchar(max)= @CreatedBy

DECLARE @i INT = 1
DECLARE @NumberOfRows INT = (SELECT COUNT(*) FROM TeacherRequest) 

select SP.StuThana, SP.StuDist, TR.StudentName,TR.StudentCode, TR.Class, TR.Subject, TR.StuGroup,TR.StuRelation, TR.Institute,TR.Status, TR.LikeStatus,


             CASE
		 WHEN 	
		 
		 WHILE(@i <= @NumberOfRows)
			BEGIN
				@TeacherRequestID = TR.ID THEN 'Liked' Else 'Like'		
				set @i = @i + 1       
			END	 	 

		END as LikeFlag 

from StudentsProfile SP join TeacherRequest TR on SP.CreatedBy=TR.CreatedBy


--sp_StudentList 'teacher1@gmail.com'


end
Posted
Updated 6-Sep-19 3:59am
v2
Comments
Richard Deeming 6-Sep-19 14:37pm    
It's NOT "urgent". And it's not even clear what you're trying to do.

Rather than asking us how to make an imaginary made-up syntax work, try describing the actual problem you're trying to solve.

1 solution

This seems problematice, see: sql server - SQL CASE with WHILE loop in not working - Stack Overflow[^]
I would try in another way, e.g. using a Cursor, see: T-SQL: A Simple Example Using a Cursor - Steve Stedman[^]
And see example B here: WHILE (Transact-SQL) - SQL Server | Microsoft Docs[^]

But in general trying to do things in a procedural way is not the best way in SQL.
 
Share this answer
 
v2
Comments
HefazUddin 6-Sep-19 14:07pm    
You can use IF statement to compare limited value but I have unlimited values to compare so I need FOREACH or WHILE loop to solve this

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