'6'附近的语法不正确

时间:2014-02-11 14:00:35

标签: sql sql-server sql-server-2008

我在代码

中的第一个6上收到错误

这来自动态sql,这就是它打印的内容。

...+ '' of '' ('+@Total+') AS TargetStatus...:总数为6

SELECT [Target Status] + '' + CAST(COUNT(*) AS NVARCHAR(255))+ ' of ' (6) AS TargetStatus, CAST(COUNT(*) AS FLOAT) / 6 AS [Count]
    FROM (
    SELECT CASE WHEN CONVERT(DATETIME,CONVERT(CHAR(10),DATEADD(DAY,0,ISNULL(t2.AUD_CloseDate,GETDATE())), 101)) > CONVERT(DATETIME,CONVERT(CHAR(10),DATEADD(DAY,0,t2.AUD_TargetDate), 101))
            THEN 'Over Due: '
            ELSE 'On Time: ' END AS [Target Status]
            FROM #tmp1 t1 INNER JOIN dbo.Audit t2
            ON t1.AUD_ID = t2.AUD_ID
            WHERE t2.AUD_Deleted = 0
            AND t2.AUD_LeadAuditor IN (SELECT ID FROM [dbo].[fx_SplitCommaSeperatedValues] ('29'))
            AND t2.AUD_Year = 2011
            AND t2.AUD_ORGID IN (Select Org_ID From [database].[dbo].fx_Rights_ORGIDs( 120))
            AND AUD_Quarter IN (SELECT ID FROM [dbo].[fx_SplitCommaSeperatedValues] ('1')))

不知道为什么它会说near 6它的2个括号?

4 个答案:

答案 0 :(得分:2)

使用

[Target Status] + '' + CAST(COUNT(*) AS NVARCHAR(255))+ ' of  6' AS TargetStatus

6应该是字符串的一部分。

或者,使用

[Target Status] + '' + CAST(COUNT(*) AS NVARCHAR(255))+ ' of  ' + CAST((6) AS NVARCHAR(15)) AS TargetStatus

答案 1 :(得分:1)

将单引号移到6的右侧。

答案 2 :(得分:0)

尝试移动该单引号,(6)应该是字符串的一部分。

SELECT [Target Status] + '' + CAST(COUNT(*) AS NVARCHAR(255))+ ' of  (6)' AS TargetStatus, CAST(COUNT(*) AS FLOAT) / 6 AS [Count]
    FROM (
    SELECT CASE WHEN CONVERT(DATETIME,CONVERT(CHAR(10),DATEADD(DAY,0,ISNULL(t2.AUD_CloseDate,GETDATE())), 101)) > CONVERT(DATETIME,CONVERT(CHAR(10),DATEADD(DAY,0,t2.AUD_TargetDate), 101))
            THEN 'Over Due: '
            ELSE 'On Time: ' END AS [Target Status]
            FROM #tmp1 t1 INNER JOIN dbo.Audit t2
            ON t1.AUD_ID = t2.AUD_ID
            WHERE t2.AUD_Deleted = 0
            AND t2.AUD_LeadAuditor IN (SELECT ID FROM [dbo].[fx_SplitCommaSeperatedValues] ('29'))
            AND t2.AUD_Year = 2011
            AND t2.AUD_ORGID IN (Select Org_ID From [database].[dbo].fx_Rights_ORGIDs( 120))
            AND AUD_Quarter IN (SELECT ID FROM [dbo].[fx_SplitCommaSeperatedValues] ('1')))

答案 3 :(得分:0)

如果您正在查看打印状态并且您正在使用“已处理的X of 6”之类的字符串,请将(6)移至字符串...

SELECT [Target Status] + '' + CAST(COUNT(*) AS NVARCHAR(255))+ ' of 6' AS TargetStatus, CAST(COUNT(*) AS FLOAT) / 6 AS [Count]
    FROM (
    SELECT CASE WHEN CONVERT(DATETIME,CONVERT(CHAR(10),DATEADD(DAY,0,ISNULL(t2.AUD_CloseDate,GETDATE())), 101)) > CONVERT(DATETIME,CONVERT(CHAR(10),DATEADD(DAY,0,t2.AUD_TargetDate), 101))
            THEN 'Over Due: '
            ELSE 'On Time: ' END AS [Target Status]
            FROM #tmp1 t1 INNER JOIN dbo.Audit t2
            ON t1.AUD_ID = t2.AUD_ID
            WHERE t2.AUD_Deleted = 0
            AND t2.AUD_LeadAuditor IN (SELECT ID FROM [dbo].[fx_SplitCommaSeperatedValues] ('29'))
            AND t2.AUD_Year = 2011
            AND t2.AUD_ORGID IN (Select Org_ID From [database].[dbo].fx_Rights_ORGIDs( 120))
            AND AUD_Quarter IN (SELECT ID FROM [dbo].[fx_SplitCommaSeperatedValues] ('1')))

如果您希望'6'变为变量,那么您将不得不考虑使用动态SQL。