'where' 子句中的 SQL 切换/大小写 [英] SQL Switch/Case in 'where' clause
本文介绍了'where' 子句中的 SQL 切换/大小写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试四处搜索,但找不到任何可以帮助我的东西.
I tried searching around, but I couldn't find anything that would help me out.
我正在尝试在 SQL 中执行此操作:
I'm trying to do this in SQL:
declare @locationType varchar(50);
declare @locationID int;
SELECT column1, column2
FROM viewWhatever
WHERE
CASE @locationType
WHEN 'location' THEN account_location = @locationID
WHEN 'area' THEN xxx_location_area = @locationID
WHEN 'division' THEN xxx_location_division = @locationID
我知道我不应该把'= @locationID'放在每一个的末尾,但我无法得到接近正确的语法.SQL 一直在抱怨我的 '=' 在第一行 WHEN...
I know that I shouldn't have to put '= @locationID' at the end of each one, but I can't get the syntax even close to being correct. SQL keeps complaining about my '=' on the first WHEN line...
我该怎么做?
推荐答案
declare @locationType varchar(50);
declare @locationID int;
SELECT column1, column2
FROM viewWhatever
WHERE
@locationID =
CASE @locationType
WHEN 'location' THEN account_location
WHEN 'area' THEN xxx_location_area
WHEN 'division' THEN xxx_location_division
END
这篇关于'where' 子句中的 SQL 切换/大小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文