0

我目前有一个 Storedproc,它正在返回值让我与 asp.net 中的 gridview 绑定。我想做的一件事是将两个字段组合成一个由 / 分隔的字段,但我似乎无法使其正常工作。

这是我的存储过程

DECLARE @SQL VARCHAR(MAX);

BEGIN
SET @SQL = N'SELECT SYSTEM AS ''System''
            , ENTN AS ''Entity''
            , MED_CTR AS ''Medical Center''
            , MED_CTR_LEVEL AS ''Medical Center Level''
            , DEPT_ID AS ''Department ID''
    FROM V_ROLLUP_SYSTEM_DESC_REV1
    WHERE (DEPT_ID = '+CONVERT(VARCHAR,@DEPTCODE)+')
        AND SYSTEM IN ('+@SYS+')'
PRINT(@SQL)
EXEC(@SQL)

所以我想将 MED_CTR 与 MED_CTR_LEVEL 结合起来,用“/”分隔并标记为医疗中心/级别。到目前为止,我似乎无法将它结合起来,因为我尝试在其中添加“/”的每种方法都没有返回任何结果。

4

1 回答 1

1

这应该有效:

SET @SQL = N'SELECT SYSTEM AS [System]
            , ENTN AS [Entity]
            , (MED_CTR +''/''+ MED_CTR_LEVEL) as [Medical Center/ Level]
            , DEPT_ID AS [Department ID]
    FROM V_ROLLUP_SYSTEM_DESC_REV1
    WHERE (DEPT_ID = '+CONVERT(VARCHAR,@DEPTCODE)+')
        AND SYSTEM IN ('+@SYS+')'

如果你想确保 2 列的 concat 总是成功,你可以这样做:

SET @SQL = N'SELECT SYSTEM AS [System]
            , ENTN AS [Entity]
            , ISNULL(MED_CTR,''N/A'') +''/''+ ISNULL(MED_CTR_LEVEL,''N/A'') ) as [Medical Center/ Level]
            , DEPT_ID AS [Department ID]
    FROM V_ROLLUP_SYSTEM_DESC_REV1
    WHERE (DEPT_ID = '+CONVERT(VARCHAR,@DEPTCODE)+')
        AND SYSTEM IN ('+@SYS+')'
于 2012-08-08T17:16:57.073 回答