1

我刚刚从一位同事那里继承了下面的程序,但我无法弄清楚问题所在。我正在使用 MS SQL 服务器,我相信他使用的是不同的软件。当我运行使用临时表和系统中的表的查询时,我不断收到错误消息: Msg 102, Level 15, State 1, Line 8 'month' 附近的语法不正确。同样,我似乎无法弄清楚如何纠正语法问题。

CREATE TABLE #Temp_Contact (
    Person_ID INT,
    Contact_Date DATETIME,
    Success INT)
GO

INSERT INTO #Temp_Contact (Person_ID, Contact_Date, Success)
SELECT Person_ID, Contact_Date,
       gw_ppp.dbo.fnWasContacted(Contact_Method, Contact_Result, Participant)
FROM gw_dw.dbo.DimContacts_Child
GO

CREATE TABLE #Temp_Months (Month VARCHAR(30))
INSERT INTO #Temp_Months 
SELECT 'January' UNION ALL
SELECT 'February' UNION ALL
SELECT 'March' UNION ALL
SELECT 'April' UNION ALL
SELECT 'May' UNION ALL
SELECT 'June' UNION ALL
SELECT 'July' UNION ALL
SELECT 'August' UNION ALL
SELECT 'September' UNION ALL
SELECT 'October' UNION ALL
SELECT 'November' UNION ALL
SELECT 'December';

这是生成错误消息的查询

SELECT lft.Person_ID,  
m.Month month, 
gw_PPP.dbo.fnFmtContact(src.cnt) result
  FROM gw_dw.dbo.DimContacts_Child AS lft
  JOIN  #Temp_Months m
  on m.Month=month (lft.Contact_Date)
  LEFT OUTER JOIN
    (SELECT Person_ID, 
     DATENAME(month, MONTH(Contact_Date)) as Month, 
     sum(Success) as cnt
     FROM #Temp_Contact
     GROUP BY Person_ID, DATENAME(month, MONTH(Contact_Date))) AS src
  ON (lft.Person_ID = src.Person_ID AND month (lft.Contact_Date) = src.month)  
4

2 回答 2

1
SELECT Person_ID, month, sum(Success) cnt
     FROM #Temp_Contact

我没有看到月份#temp_Contact

CREATE TABLE #Temp_Contact (
    Person_ID INT,
    Contact_Date DATETIME,
    Success INT)
GO
于 2013-10-07T21:05:42.383 回答
0

您在这里缺少一个逗号:

GROUP BY Person_ID, DATENAME(month, MONTH(Contact_Date)), month) AS src
                                                        ^

你有两个JOIN子句,但只有一个ON子句。每次加入都需要一个。

您缺少表month中的字段#Temp_Contact

于 2013-10-07T21:13:20.497 回答