exec失败,因为名称不是有效的标识符?


71

我有一个查询,我需要将其作为动态查询运行以输出有意义的列名。例如,如果我直接运行查询,它将正确返回数据。但是,如果我使用下面的代码,它将显示:

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;

是因为加入了吗?


对于那个很抱歉。我花了一段时间才知道可以单击复选符号。
urlreader 2012年

Answers:


256

最后尝试以下方法:

exec (@query)

如果没有括号,SQL Server会将变量的值假定为存储过程名称。

要么

EXECUTE sp_executesql @query

而且这不应该是因为FULL JOIN。
但我希望您已经创建了临时表:#TrafficFinal,#TrafficFinal2,#TrafficFinal3。


请注意,在使用EXEC和sp_executesql之间有性能方面的考虑。因为sp_executesql像sp一样使用强制语句缓存。
更多细节在这里


另一方面,在可以按原样使用查询的情况下,考虑到您没有进行任何查询操作并按原样执行查询,是否有理由在这种情况下使用动态sql?


这是因为列标题,即Receiver Count 1,...,需要通过日期变量对用户友好。
urlreader 2012年

sp_executesql用于从文件批量加载插入,而exec则不行。
伊兰·哈森

1
你是救生员。从昨天开始,我一直在苦苦挣扎
Rob

2

就像我的情况一样,如果您的sql是通过级联生成或使用converts,则执行时的sql必须以字母N作为前缀,如下所示

例如

Exec N'Select bla..' 

N定义的字符串文字是unicode。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.