2012-08-08 94 views
0

我目前有一個Storedproc t hat正在返回值,以便在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 ('[email protected]+')' 
PRINT(@SQL) 
EXEC(@SQL) 

所以我想MED_CTR與MED_CTR_LEVEL由「/」分隔,並標記爲醫療中心/水平結合起來。我似乎不能將它結合到目前爲止,因爲我嘗試在其中添加「/」的每種方法都沒有給我返回任何內容。

回答

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 ('[email protected]+')' 

如果你想確保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 ('[email protected]+')' 
+0

感謝您的建議,但它不做我想做的事情。這給了MEDY的結果,而不是MED/Y或MED/Y – user1512593 2012-08-08 17:21:14

+0

@ user1512593我更新了我的答案。 – Icarus 2012-08-08 17:24:45

+0

謝謝你的工作。第二個是什麼意思? – user1512593 2012-08-08 17:54:10