2014-01-10 41 views
0

我需要標量轉換成內聯函數如何將標量轉化爲內聯函數的SQL Server 2008

我的標量函數

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 [email protected]_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 @[email protected][email protected]_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

請不要用[tag:mysql]標記SQL Server問題。 「MySQL」並不意味着「我當前的SQL Server問題」... –

+0

你是否試圖問你同一個問題,你有早些發佈http://stackoverflow.com/questions/21046294/how-to-return-multilpe- values-from-function-sql-server? – Sachin

+0

是的,有一些錯誤,這是更新的代碼。現在我需要轉換成內聯函數? – Happy

回答

0

如果函數具有return語句,則不認爲它是「內聯」的。我不100%知道你要什麼,但是你可以重寫你試圖重寫這樣並將它被認爲是內聯:

SELECT CASE @[email protected]_initial 
    WHEN '0' THEN 'Maintained' 
    WHEN '-1' THEN 'Minor Improvement' 
    WHEN '-2' THEN 'Moderate Improvement' 
    .... 
    ELSE 'some default value' END AS ReturnValue 

從本質上講,爲了使函數內聯,它必須是一個單一的SQL語句。

編輯

我不相信你可以創建一個內聯標量函數(有人請糾正我,如果我錯了)。內聯函數必須是表值函數(儘管該表可以由單列和單列組成)。

+0

好的,謝謝我將改變concept3。 – Happy

+0

我做了幾個編輯(包括別名從SELECT語句列)。 –

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 [email protected]_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應該在數據庫中的表然後埋在一個功能不硬編碼值。

+0

錯誤「當子查詢遵循=,!=,<, <= , >,> =或子查詢用作表達式時,這是不允許的。」該怎麼做幫助我... – Happy

相關問題