2012-08-16 76 views
44

我有一個查詢我需要運行它作爲動態查詢輸出一個有意義的列名稱。作爲一個例子,如果我直接運行查詢,它會正確返回數據。但是,如果我用下面的代碼,它顯示:exec失敗,因爲名稱不是有效的標識符?

The name ' 
      SELECT (CASE WHEN A.Domain IS NOT NULL THEN A.Domain ELSE B.Domain END) AS [Domain], 
        (CASE WHEN A.Email IS NOT NULL THEN A.Email ELSE B.Email END) AS [Email], 
        A.[Sender Size] AS [Sender Size 1], A.[Sender Count] AS [Sender Count 1], 
         A.[Receiver Size] AS [Receiver Size 1], A.[Receiver Count] AS [Receiver Count 1], 
        A.[Sender Size 2] AS [Sender Size 2], A.[Sender Count 2] AS [Sender Count 2], 
         A.[Receiver Size 2] AS [Receiver Size 2], A.[Receiver Count 2] AS [Receiver Count 2], 
        B.SenderSize AS [Sender Size Average], B.SenderCount AS [Sender Count Average], 
         B.ReceiverSize AS [Receiv' is not a valid identifier. 

在它下面是代碼:

DECLARE @query NVARCHAR(4000) 
SET @query = N'SELECT * 
      FROM 
      (
       SELECT (CASE WHEN A.Domain IS NOT NULL THEN A.Domain ELSE B.Domain END) AS [Domain], 
         (CASE WHEN A.Email IS NOT NULL THEN A.Email ELSE B.Email END) AS [Email], 
         A.[Sender Size] AS [Sender Size 1], A.[Sender Count] AS [Sender Count 1], 
          A.[Receiver Size] AS [Receiver Size 1], A.[Receiver Count] AS [Receiver Count 1], 
         A.[Sender Size 2] AS [Sender Size 2], A.[Sender Count 2] AS [Sender Count 2], 
          A.[Receiver Size 2] AS [Receiver Size 2], A.[Receiver Count 2] AS [Receiver Count 2], 
         B.SenderSize AS [Sender Size Average], B.SenderCount AS [Sender Count Average], 
          B.ReceiverSize AS [Receiver Size Average], B.ReceiverCount AS [Receiver Count Average] 
       FROM 
        (
        SELECT (CASE WHEN tf.Domain IS NOT NULL THEN tf.Domain ELSE tf2.Domain END) AS Domain, 
          (CASE WHEN tf.Email IS NOT NULL THEN tf.Email ELSE tf2.Email END) AS Email, 
         ISNULL(tf.SenderSize,0) AS [Sender Size] , ISNULL(tf.SenderCount,0) AS [Sender Count], ISNULL(tf.ReceiverSize,0) AS [Receiver Size], ISNULL(tf.ReceiverCount,0) AS [Receiver Count], 
         ISNULL(tf2.SenderSize,0) AS [Sender Size 2], ISNULL(tf2.SenderCount,0) AS [Sender Count 2], ISNULL(tf2.ReceiverSize,0) AS [Receiver Size 2], ISNULL(tf2.ReceiverCount,0) AS [Receiver Count 2] 
        FROM #TrafficFinal tf FULL JOIN #TrafficFinal2 tf2 ON (tf.Email = tf2.Email AND tf.Domain = tf2.Domain) 
        ) A FULL JOIN #TrafficFinal3 B ON (A.Email = B.Email AND A.Domain = B.Domain) 
      ) C 
      ORDER BY Domain, Email';  

PRINT @query; 

-- run it 
exec @query; 

是因爲充分加入?

+0

對不起。我花了一段時間才弄清楚可以點擊檢查符號。 – urlreader 2012-08-16 17:20:19

回答

155

嘗試這一句結尾:

exec (@query) 

如果沒有括號,SQL Server假定該變量的值是一個存儲過程的名稱。

OR

EXECUTE sp_executesql @query 

,它不應該是因爲FULL JOIN的。
但我希望你已經創建了臨時表:#TrafficFinal,#TrafficFinal2,#TrafficFinal3在此之前。


請注意,使用EXEC和sp_executesql之間有性能方面的考慮因素。因爲sp_executesql像sp一樣使用強制語句緩存。
更多詳細信息here


在另一方面,是有一個原因,你正在使用動態SQL這種情況下,你可以直接使用的查詢,考慮你是不是做任何的查詢操作,並執行它,它是這樣的?

+1

非常感謝,我知道我錯過了一些簡單的事情! :) – urlreader 2012-08-16 17:10:39

+0

這是因爲列標題,即接收方計數1,...需要用戶友好的日期變量。 – urlreader 2012-08-16 17:22:03

+1

我剛碰到同樣的問題,感謝解決方案。我覺得很愚蠢,不是因爲它太簡單。 – Jackson 2013-05-08 16:56:43

0

正如在我的情況下,如果通過連接產生的SQL或用途轉換,然後在執行需要SQL來與字母N前綴如下

例如

Exec N'Select bla..' 

N定義字符串文字是unicode。

相關問題