2017-02-23 78 views
0
SET @QUERY = 'SELECT ID, CODE, (FIRST_NAME+" "+ISNULL(MIDDLE_NAME,'')+" "+SUR_NAME) AS NAME FROM [Common].[CM_Personnel_MF] 

上述查詢執行顯示如下錯誤:無效的列名稱'上串聯列

Invalid column name ' '.

如果我改變查詢:

SET @QUERY = 'SELECT ID, CODE, (FIRST_NAME+' '+ISNULL(MIDDLE_NAME,' ')+' '+SUR_NAME) AS NAME FROM [Common].[CM_Personnel_MF]'; 

獲得不正確的語法如下所示

Incorrect syntax near '+ISNULL(MIDDLE_NAME,'.

回答

1

您需要轉義'with''(使用雙單引號)

SET @QUERY = 'SELECT ID, CODE, (FIRST_NAME+'' ''+ISNULL(MIDDLE_NAME,'''')+'' ''+SUR_NAME) AS NAME FROM [Common].[CM_Personnel_MF]