我需要将标量转换为内联函数
我的标量函数
create function [dbo].[fun_functional_score] (@phy_id varchar(20))
returns varchar(50)
as
begin
declare @level_initial int, @level_current int
-- initial functional score
set @level_initial=(SELECT pflag.fun_level
FROM tbl_phy_demographic_details as [phy]
inner join tbl_all_purple_flag_level as [pflag] on phy.Demographic_id=pflag.Id
WHERE phy.Physicion_id= @phy_id
and pflag.visited_count in (select MAX(visited_count)-1 from tbl_all_purple_flag_level group by id ))
-- current functional score
set @level_current=(SELECT pflag.fun_level
FROM tbl_phy_demographic_details as [phy]
inner join tbl_all_purple_flag_level as [pflag] on phy.Demographic_id=pflag.Id
WHERE phy.Physicion_id=@phy_id
and pflag.visited_count in (select MAX(visited_count) from tbl_all_purple_flag_level group by id ))
-- current functional scor
--set @level_current=(SELECT pflag.fun_level
-- FROM tbl_phy_demographic_details as [phy]
--to calculate functional score
declare @fun_level varchar(20),@result varchar(50)
set @fun_level=@level_current-@level_initial;
if @fun_level = 0 set @result='Maintained'
if @fun_level = '-1' set @result='Minor Improvement'
if @fun_level = '-2' set @result='Moderate Improvement'
if @fun_level = '-3' set @result='Significant Improvement'
if @fun_level = '-4' set @result='Substantial Improvement'
if @fun_level = '1' set @result='Minor Reduction'
if @fun_level = '2' set @result='Moderate Reduction'
if @fun_level = '3' set @result='Significant Reduction'
if @fun_level = '4' set @result='Substantial Reduction'
return @result
end
有可能吗?
答案 0 :(得分:0)
如果函数有return语句,则不认为该函数是“inline”。我不是100%知道你想要什么,但你可以像这样重写你的尝试重写并将其视为内联:
SELECT CASE @level_current-@level_initial
WHEN '0' THEN 'Maintained'
WHEN '-1' THEN 'Minor Improvement'
WHEN '-2' THEN 'Moderate Improvement'
....
ELSE 'some default value' END AS ReturnValue
本质上,为了使函数内联,它必须是单个SQL语句。
修改强>
我不相信你可以创建一个内联标量值函数(如果我错了,有人请纠正我)。内联函数必须是表值函数(尽管表可以包含单个列和单个行)。
答案 1 :(得分:0)
create function [dbo].[fun_functional_score] (@phy_id varchar(20))
returns table
as
return (
WITH
t1(fun_level) AS (
SELECT
(
SELECT pflag.fun_level -- current functional score
FROM tbl_phy_demographic_details as [phy]
inner join tbl_all_purple_flag_level as [pflag] on phy.Demographic_id=pflag.Id
WHERE phy.Physicion_id= @phy_id
and pflag.visited_count in (select MAX(visited_count) from tbl_all_purple_flag_level group by id )
) -
(
SELECT pflag.fun_level -- initial functional score
FROM tbl_phy_demographic_details as [phy]
inner join tbl_all_purple_flag_level as [pflag] on phy.Demographic_id=pflag.Id
WHERE phy.Physicion_id=@phy_id
and pflag.visited_count in (select MAX(visited_count)-1 from tbl_all_purple_flag_level group by id )
)
),
t2(fun_level, result) AS (
SELECT '0', 'Maintianed' UNION ALL
SELECT '-1', 'Minor Improvement' UNION ALL
SELECT '-2', 'Moderate Improvement' UNION ALL
SELECT '-3', 'Significant Improvement' UNION ALL
SELECT '-4', 'Substantial Improvement' UNION ALL
SELECT '1', 'Minor Reduction' UNION ALL
SELECT '2', 'Moderate Reduction' UNION ALL
SELECT '3', 'Significant Reduction' UNION ALL
SELECT '4', 'Substantial Reduction'
)
SELECT result FROM t2 INNER JOIN t1 ON t1.fun_level = t2.fun_level
)
理想情况下,t2
应该是数据库中的真实表,而不是功能中隐藏的硬编码值。