Click here to Skip to main content
14,298,693 members
Rate this:
Please Sign up or sign in to 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:

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

Rate this:
Please Sign up or sign in to vote.

Solution 1

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.
   
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, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100