如何选择值 [英] How to select values
问题描述
Hi,
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
我不需要像上面那样进行硬编码.
我想要类似的逻辑,如果用户使用ram..i登录需要设置位置,如果用户使用krishna ..i登录需要设置位置...
通过使用SET,我们一次只能为一个变量设置一个值...所以当用户使用该特定用户名登录时,我需要为Location变量设置多个值..我该怎么做...
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...
@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
选择教授,学生名称,位置,来自学生的主题,其中Location = @ location
所以我想要上面的逻辑....
请帮助我
Select Professor,Stu_name,Location,Subject From student where Location=@location
So i want logic like above ....
Please Help me
推荐答案
按照您的方式进行操作或从表中查找.但是,如果您不能从表中查找它,那么您也可以执行CASE语句.
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.
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.
SELECT @location = location FROM locationTable WHERE userid = @username
这篇关于如何选择值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!