当我尝试从工作中的 concat 选择创建函数时遇到问题
这里的功能
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, État 2, Procédure afEvrGetConsequencesConcat, Ligne 23 Select statements 包含在函数中无法将数据返回给客户端。
上面的 SELECT 在函数之外工作得很好。每次,我在@result 中得到 1 个与 SELECT 连接匹配的 varchar。这个相似的功能也很有效:
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 的结果中插入 next 效果很好:)
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