存储过程错误

时间:2009-10-02 17:18:50

标签: sql-server stored-procedures

运行此代码时出现以下错误:消息102,级别15,状态1,行1 ')'附近的语法不正确。有想法该怎么解决这个吗?任何帮助,将不胜感激。

Declare @Month int 
Declare @Year int
Declare @newYear int 
Declare @EndYearMonth int 
Declare @PreviousMonthInt int 
Declare @OldYear int 
Declare @PreviousMonthName varchar(10)
Declare @NextMonthName varchar(10)
Declare @SQL nvarchar(max)
DECLARE @ParameterDefinition AS NVARCHAR(100)
SET @ParameterDefinition =  '@Month int, @Year int, @newYear int, @EndYearMonth int,  @PreviousMonthInt int, @OldYear int, @PreviousMonthName varchar(10), @NextMonthName varchar(10)' 


Set @SQL = 'Select Y.*, X.* from (Select T1.[1], T1.[2], T1.[3], T1.[4], T1.[5], T1.[6], T1.[7], T1.[8], T1.[9], T1.[10],    T1.[11], T1.[12], T1.[13], T1.[14], T1.[15], T1.[16], T1.[17], T1.[18], T1.[19], T1.[20],    T1.[21], T1.[22], T1.[23], T1.[24], T1.[25], T1.[26], T1.[27], T1.[28], T1.[29], T1.[30], T1.[31], T2.[1] as February1, T2.DoctorName from   (SELECT [1], [2], [3], [4], [5], [6], [7], [8], [9], [10],    [11], [12], [13], [14], [15], [16], [17], [18], [19], [20],    [21], [22], [23], [24], [25], [26], [27], [28], [29], [30], [31], DoctorName  FROM   (SELECT DoctorName, WeekDay, DayType  FROM View_GridbyWeekDay where TheMonth = @Month and TheYear = @Year) ps  PIVOT  (  Max(DayType)  FOR WeekDay IN  ([1], [2], [3], [4], [5], [6], [7], [8], [9], [10],    [11], [12], [13], [14], [15], [16], [17], [18], [19], [20],    [21], [22], [23], [24], [25], [26], [27], [28], [29], [30], [31])  ) AS pvt) as T1  Left outer Join    (SELECT [1], [2], [3], [4], [5], [6], [7], [8], [9], [10],    [11], [12], [13], [14], [15], [16], [17], [18], [19], [20],    [21], [22], [23], [24], [25], [26], [27], [28], [29], [30], [31], DoctorName  FROM   (SELECT DoctorName, WeekDay, DayType  FROM View_GridbyWeekDay where TheMonth = @EndYearMonth and TheYear = @newYear) ps   PIVOT  (  Max(DayType)  FOR WeekDay IN  ([1], [2], [3], [4], [5], [6], [7], [8], [9], [10],    [11], [12], [13], [14], [15], [16], [17], [18], [19], [20],    [21], [22], [23], [24], [25], [26], [27], [28], [29], [30], [31])  ) AS pvt) as T2   on (T1.DoctorName = T2.DoctorName)) as X  Left Outer Join   (Select DoctorName, [27] as December27, [28] as December28, [29] as December29,   [30] as December30, [31] as December31  FROM   (SELECT DoctorName, WeekDay, DayType  FROM View_GridbyWeekDay where TheMonth = @PreviousMonthInt  and TheYear = @OldYear) ps  PIVOT  (  Max(DayType)  FOR WeekDay IN  ([1], [2], [3], [4], [5], [6], [7], [8], [9], [10],    [11], [12], [13], [14], [15], [16], [17], [18], [19], [20],    [21], [22], [23], [24], [25], [26], [27], [28], [29], [30], [31])  ) AS pvt) as Y on (X.DoctorName = Y.DoctorName)  Order by X.DoctorName asc '

EXECUTE sp_executesql @SQL, @ParameterDefinition, @Month, @Year, @newYear, @EndYearMonth, @PreviousMonthInt, @OldYear, @PreviousMonthName,  @NextMonthName

2 个答案:

答案 0 :(得分:2)

您的PIVOT命令至少缺少一个结束括号。

编辑:看起来你有太多了。

我从你的陈述中删除了所有文本,然后离开了括号。这就是它的样子:

Select Y.*, X.* from 
((()(()()))(()(()))())(()(()))()

答案 1 :(得分:1)

正如@George Stocker所说,你错过了PIVOT命令的至少一个结束括号。

尝试将@SQL字符串放在编辑器中并将其分解为多行并正确缩进,并且您可能会注意到缺少“)”的位置。