我需要帮助使用 convert varchar(max) 识别 T-SQL string agg 函数的语法错误
I need help identifying syntax error with T-SQL string agg function using convert varchar(max)
我正在尝试使用 convert(varchar(max))
的 string_agg
函数,因为结果超出了 8000 个字符的限制,但现在我在
下遇到语法错误
WITHIN GROUP (ORDER BY PURP.PROGRAM_YEAR DESC) AS 'GS SUMMARY'
这是我的资料:
SELECT DISTINCT
tbl1.ID_NUMBER,
STRING_AGG(CONVERT(VARCHAR(MAX), 'PROPOSAL# ', pro.PROPOSAL_ID,': ', purp.PROGRAM_YEAR,', ', tpro.SHORT_DESC, ', ', tsub.SHORT_DESC, ', ASK: ',
FORMAT(pro.ORIGINAL_ASK_AMT,'C0','en-us'),', ANTIC: ',
FORMAT(pro.ANTICIPATED_AMT,'C0','en-us'), ', UNIT: ',
pro.UNIT_CODE, ', SUBMITTER: ',
purp.SUBMITTER, ', BENEFIT UNIT: ', TFUND.SHORT_DESC), ';') WITHIN GROUP (ORDER BY PURP.PROGRAM_YEAR DESC) AS 'SUMMARY'
FROM
TABLE1 tbl1
试试 CONCAT
然后 CAST or CONVERT 连接的字符串到 VARCHAR(MAX)
, STRING_AGG(CAST(CONCAT(
'PROPOSAL# ', pro.PROPOSAL_ID,
': ', purp.PROGRAM_YEAR,
', ', tpro.SHORT_DESC,
', ', tsub.SHORT_DESC,
', ASK: ', FORMAT(pro.ORIGINAL_ASK_AMT,'C0','en-us'),
', ANTIC: ', FORMAT(pro.ANTICIPATED_AMT,'C0','en-us'),
', UNIT: ', pro.UNIT_CODE,
', SUBMITTER: ', purp.SUBMITTER,
', BENEFIT UNIT: ', TFUND.SHORT_DESC
) AS VARCHAR(MAX)), ';') WITHIN GROUP (ORDER BY PURP.PROGRAM_YEAR DESC) AS SUMMARY
A CONVERT
不希望有那么多参数。
万一其他人遇到这个问题,我找到了答案。我能够通过添加 concat 函数来补救它:
, STRING_AGG(CONVERT(VARCHAR(max), CONCAT('PROPOSAL# ', pro.PROPOSAL_ID,': ', purp.PROGRAM_YEAR,', ', tpro.SHORT_DESC, ', ', tsub.SHORT_DESC, ', ASK: '
, FORMAT(pro.ORIGINAL_ASK_AMT,'C0','en-us'),', ANTIC: ', FORMAT(pro.ANTICIPATED_AMT,'C0','en-us'), ', UNIT: ', pro.UNIT_CODE, ', SUBMITTER: '
, purp.SUBMITTER, ', BENEFIT UNIT: ', TFUND.SHORT_DESC)), '; ')
WITHIN GROUP (ORDER BY PURP.PROGRAM_YEAR DESC) AS 'SUMMARY'
我正在尝试使用 convert(varchar(max))
的 string_agg
函数,因为结果超出了 8000 个字符的限制,但现在我在
WITHIN GROUP (ORDER BY PURP.PROGRAM_YEAR DESC) AS 'GS SUMMARY'
这是我的资料:
SELECT DISTINCT
tbl1.ID_NUMBER,
STRING_AGG(CONVERT(VARCHAR(MAX), 'PROPOSAL# ', pro.PROPOSAL_ID,': ', purp.PROGRAM_YEAR,', ', tpro.SHORT_DESC, ', ', tsub.SHORT_DESC, ', ASK: ',
FORMAT(pro.ORIGINAL_ASK_AMT,'C0','en-us'),', ANTIC: ',
FORMAT(pro.ANTICIPATED_AMT,'C0','en-us'), ', UNIT: ',
pro.UNIT_CODE, ', SUBMITTER: ',
purp.SUBMITTER, ', BENEFIT UNIT: ', TFUND.SHORT_DESC), ';') WITHIN GROUP (ORDER BY PURP.PROGRAM_YEAR DESC) AS 'SUMMARY'
FROM
TABLE1 tbl1
试试 CONCAT
然后 CAST or CONVERT 连接的字符串到 VARCHAR(MAX)
, STRING_AGG(CAST(CONCAT(
'PROPOSAL# ', pro.PROPOSAL_ID,
': ', purp.PROGRAM_YEAR,
', ', tpro.SHORT_DESC,
', ', tsub.SHORT_DESC,
', ASK: ', FORMAT(pro.ORIGINAL_ASK_AMT,'C0','en-us'),
', ANTIC: ', FORMAT(pro.ANTICIPATED_AMT,'C0','en-us'),
', UNIT: ', pro.UNIT_CODE,
', SUBMITTER: ', purp.SUBMITTER,
', BENEFIT UNIT: ', TFUND.SHORT_DESC
) AS VARCHAR(MAX)), ';') WITHIN GROUP (ORDER BY PURP.PROGRAM_YEAR DESC) AS SUMMARY
A CONVERT
不希望有那么多参数。
万一其他人遇到这个问题,我找到了答案。我能够通过添加 concat 函数来补救它:
, STRING_AGG(CONVERT(VARCHAR(max), CONCAT('PROPOSAL# ', pro.PROPOSAL_ID,': ', purp.PROGRAM_YEAR,', ', tpro.SHORT_DESC, ', ', tsub.SHORT_DESC, ', ASK: '
, FORMAT(pro.ORIGINAL_ASK_AMT,'C0','en-us'),', ANTIC: ', FORMAT(pro.ANTICIPATED_AMT,'C0','en-us'), ', UNIT: ', pro.UNIT_CODE, ', SUBMITTER: '
, purp.SUBMITTER, ', BENEFIT UNIT: ', TFUND.SHORT_DESC)), '; ')
WITHIN GROUP (ORDER BY PURP.PROGRAM_YEAR DESC) AS 'SUMMARY'