Click here to Skip to main content
16,003,333 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
Hi,

SQL
If(@username='Ram')
Begin
Select Professor,Stu_name,Location,Subject From student where Location in('Hyderabad','Banglore','Chennai')
End
Else if(@username='Krishna')
Begin
Select Professor,Stu_name,Location,Subject From student where Location in('Pune','Mumbai','Kolkata')
End


Ii don't need to to Hard code like above..
I want logic like , if user logined with ram..i need to set Location,if user logined with krishna ..i need to set Location...

by using SET we can set only one value at a time for a variable...so i need to set multiple values for Location variable when user logined with that particular username..how can i do it...

SQL
@Declare @location varchar(20)
If(@username='Ram')
Begin
set @location='Hyderabad','Banglore','Chennai'
end
Else if(@username='krishna')
Begin
set @location='Chennai','Pune','Mumbai'
End



Select Professor,Stu_name,Location,Subject From student where Location=@location


So i want logic like above ....

Please Help me
Posted
Updated 4-Mar-12 4:36am
v2
Comments
sahabiswarup 4-Mar-12 23:57pm    
nice1

1 solution

Either do it the way you are or look it up from a table. But if you can't look it up from a table you can also do a CASE statement.

SQL
SET @location = CASE WHEN @username='Ram' THEN '1,2,3' WHEN @username='t' THEN 'x' ELSE 'z' END


But the best thing to do is look it up, if you can.

SQL
SELECT @location = location FROM locationTable WHERE userid = @username
 
Share this answer
 

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