Concat在功能上

时间:2013-04-10 15:52:13

标签: sql-server

当我尝试从工作的concat select

创建一个函数时,我遇到了一个问题

这里的功能

CREATE FUNCTION dbo.afEvrGetConsequencesConcat
(@id_evr int,@type_evr int)
RETURNS varchar(max)
AS
BEGIN

    declare @result varchar(max)
    set @result= ''

IF @type_evr = 0 
BEGIN

    SELECT @result = @result + (case when (CEV.ID_CON is null) then CEV.NOM_CEV else CON.NOM_CON end) + ' ; '  
    from T_CONSEQUENCE_EVRP CEV 
    left join T_CONSEQUENCE CON on CEV.id_con=CON.id_con
    WHERE CEV.id_evr = @id_evr

END

IF @type_evr = 1
BEGIN

        SELECT DISTINCT @result = @result + isnull(NOM_PHR,'') + ' ; '  
        FROM T_EVRP EVR
        INNER JOIN T_EVR_CHIM CHIM on EVR.ID_EVR=CHIM.ID_EVR
        INNER JOIN T_PRODUIT PROD on CHIM.ID_PROD=PROD.ID_PROD
        INNER JOIN T_PRODUIT_PHRASE_R PROD_PHR on PROD.ID_PROD=PROD_PHR.ID_PROD
        INNER JOIN T_PHRASE_R PHR on PROD_PHR.ID_PHR=PHR.ID_PHR
        WHERE TYPE_EVR=1 AND EVR.id_evr = @id_evr AND PHR.flag_consequence = 1

        UNION 

        SELECT DISTINCT @result = @result + isnull(nom_cons_txt,'') +' ; '  
        FROM T_EVRP EVR
        INNER JOIN T_EVR_CHIM CHIM on EVR.ID_EVR=CHIM.ID_EVR
        INNER JOIN T_PRODUIT PROD on CHIM.ID_PROD=PROD.ID_PROD
        INNER JOIN t_consequence_txt PROD_CONS on PROD.ID_PROD=PROD_CONS.ID_PROD
        WHERE TYPE_EVR=1 AND EVR.id_evr = @id_evr AND PROD.transverse_prod = 1

END

    RETURN @result

END
GO

消息错误是'Msg 444,Niveau 16,État2,ProcédureafEvrGetConsequencesConcat,Ligne 23 函数中包含的Select语句不能将数据返回给客户端。'

上面的SELECT在函数外部运行良好。每次,我在@result中得到1个varchar,它与SELECT concat匹配。而这个相似的功能也很有效:

CREATE FUNCTION dbo.afEvrGetControleConcat
(@id_evr int,@type_evr int,@type_ctrl int)
RETURNS varchar(8000)
AS
BEGIN
    declare @result varchar(8000)
    set @result= ''

    -- 0 tous
    -- 1 date + comm


    IF @type_ctrl = 0
    BEGIN   

        SELECT @result = @result + ( CONVERT(VARCHAR(10),date_ctr, 103) + ' - ' + CONVERT(VARCHAR(30),isnull(valeur_mesure,'')) + ' - ' + isnull(unite_mesure,'') + ' - ' + com_ctr  ) + ' ;'
        FROM t_controle_exposition 
        WHERE id_evr = @id_evr

    END

    IF @type_ctrl = 1
    BEGIN   

        SELECT @result = @result + ( CONVERT(VARCHAR(10),date_ctr, 103) + ' - ' + com_ctr  ) + ' ;'
        FROM t_controle_exposition 
        WHERE id_evr = @id_evr

    END

    RETURN @result

END
GO

然后是什么问题?按逻辑,我想要返回varchar,而不是表或任何时髦的数据。所以我不会解决这个错误信息。

感谢您的帮助。

编辑:添加一个临时表并在UNION的结果中插入,concat接下来效果很好:)

CREATE FUNCTION dbo.afEvrGetConsequencesConcat
(@id_evr int,@type_evr int)
RETURNS varchar(max)
AS
BEGIN

    declare @result varchar(max)
    set @result= ''
    declare @t as table (v varchar(max))

IF @type_evr = 0 
BEGIN

    SELECT @result = @result + (case when (CEV.ID_CON is null) then CEV.NOM_CEV else CON.NOM_CON end) + ' ; '  
    from T_CONSEQUENCE_EVRP CEV 
    left join T_CONSEQUENCE CON on CEV.id_con=CON.id_con
    WHERE CEV.id_evr = @id_evr

END

IF @type_evr = 1
BEGIN

        insert into @t 
        SELECT DISTINCT isnull(NOM_PHR,'') + ' ; '  
        FROM T_EVRP EVR
        INNER JOIN T_EVR_CHIM CHIM on EVR.ID_EVR=CHIM.ID_EVR
        INNER JOIN T_PRODUIT PROD on CHIM.ID_PROD=PROD.ID_PROD
        INNER JOIN T_PRODUIT_PHRASE_R PROD_PHR on PROD.ID_PROD=PROD_PHR.ID_PROD
        INNER JOIN T_PHRASE_R PHR on PROD_PHR.ID_PHR=PHR.ID_PHR
        WHERE TYPE_EVR=1 AND EVR.id_evr = @id_evr AND PHR.flag_consequence = 1

        UNION 

        SELECT DISTINCT isnull(nom_cons_txt,'') +' ; '  
        FROM T_EVRP EVR
        INNER JOIN T_EVR_CHIM CHIM on EVR.ID_EVR=CHIM.ID_EVR
        INNER JOIN T_PRODUIT PROD on CHIM.ID_PROD=PROD.ID_PROD
        INNER JOIN t_consequence_txt PROD_CONS on PROD.ID_PROD=PROD_CONS.ID_PROD
        WHERE TYPE_EVR=1 AND EVR.id_evr = @id_evr AND PROD.transverse_prod = 1

SELECT @result = @result + v FROM @t

END

    RETURN @result

END
GO

1 个答案:

答案 0 :(得分:0)

我认为这与UNION有关。尝试运行:

DECLARE @result varchar(max)

SELECT @result = 'test'

UNION 

SELECT @result = 'another'

你会得到:

Msg 10734, Level 15, State 1, Line 5
Variable assignment is not allowed in a statement containing a top level UNION, INTERSECT or EXCEPT operator.

如果你必须做UNION,你将不得不返回一个表,否则在没有UNION的情况下想出其他方法。这可以解释为什么第二个函数有效,因为它没有UNION