这就是我想做的事情:
case p.residency_status
when 0 then
dbo.fn_formatAddress(r1.prm_name, p.py_hospital, p.py_address1, p.py_city, p.pyear_state)
when 1 then
dbo.fn_formatAddress(r1.prm_name, p.respgm_hospital, p.respgm_address1, p.respgm_city, p.respgm_state)
when 2 then
dbo.fn_formatAddress(r1.prm_name, p.curr_hospital, p.curr_address1, curr.city, p.curr_state)
end
有没有办法做到这一点?基本上,基于residency_status,我需要将不同的参数传递给函数。
答案 0 :(得分:2)
是的,但是您不能像使用select case作为控件结构那样使用T-SQL中的case结构。这种情况下的语法(heh)将更像下面的内容。
Select
case
when p.residency_status = 0 then
dbo.fn_formatAddress(r1.prm_name, p.py_hospital, p.py_address1, p.py_city, p.pyear_state)
when p.residency_status = 1 then
dbo.fn_formatAddress(r1.prm_name, p.respgm_hospital, p.respgm_address1, p.respgm_city, p.respgm_state)
when p.residency_status = 2 then
dbo.fn_formatAddress(r1.prm_name, p.curr_hospital, p.curr_address1, curr.city, p.curr_state)
end
from
table p
答案 1 :(得分:0)
如果你需要函数之外的各个字段,你可以像这样进行子选择:
SELECT
*,
dbo.fn_formatAddress(
prm_name,
resident_hospital,
resident_address1,
resident_city,
resident_state
) FormattedAddress
FROM
(
SELECT
r1.prm_name,
...,
CASE p.residency_status
WHEN 0 THEN p.py_hospital
WHEN 1 THEN p.respgm_hospital
WHEN 2 THEN p.curr_hospital
END resident_hospital,
CASE p.residency_status
WHEN 0 THEN p.py_address1
WHEN 1 THEN p.respgm_address1
WHEN 2 THEN p.curr_address1
END resident_address1,
CASE p.residency_status
WHEN 0 THEN p.py_city
WHEN 1 THEN p.respgm_city
WHEN 2 THEN p.curr_city
END resident_city,
CASE p.residency_status
WHEN 0 THEN p.pyear_state
WHEN 1 THEN p.respgm_state
WHEN 2 THEN p.curr_state
END resident_city
FROM
r1 INNER JOIN p ON p.someField = r1.someField
WHERE
someCondition = 1
) AS InnerQuery