2013-02-12 79 views
0

我有一个嵌套的if语句的存储过程,但它给我的错误:不正确的语法错误

Msg 102, Level 15, State 1, Procedure SPCRMDoctorRequestList, Line 28
Incorrect syntax near 'DR'.

也就是说在第二if语句的else部分

这里我的存储过程:

CREATE PROCEDURE [dbo].[SPCRMList] 
@Records INT, 
@Type VARCHAR(10), /*Recent, Priority, Pending, Approved*/ 
@ViewType VARCHAR(10) /*Dashboard, List*/ 
AS 
BEGIN 
SET NOCOUNT ON 
IF (@Type = 'Recent') 
BEGIN 
    IF(@ViewType = 'Dashboard') 
    BEGIN 
     Select top (@Records) 
     DR.Id AS Id, /*Unique ID*/ 
     USERS.USERNAME AS Requester_Name,  /*Employee Name*/ 
     ST.ServiceName AS Service_Type_Name, /*Nature of Service*/ 
     DR.Date_Created AS Date_Created /*Created Date*/ 
     From [Doctor_Request] AS DR 
     JOIN [ASES].[dbo].[USERS] AS USERS 
     ON DR.Requester COLLATE SQL_Latin1_General_CP1_CI_AS = USERS.RID COLLATE SQL_Latin1_General_CP1_CI_AS 
     JOIN [SERVICE_TYPE] AS ST 
     ON DR.Service_Type_Id=ST.Id 
     WHERE DR.Is_Deleted=0 
     ORDER BY DR.Date_Created DESC 
    END 
    ELSE IF (@ViewType = 'List') 
    BEGIN 
     Select top (@Records) 
     DR.Id AS Id, /*Unique ID*/ 
     USERS.USERNAME AS Requester_Name,  /*Employee Name*/ 
     ST.ServiceName AS Service_Type_Name, /*Nature of Service*/ 
     DR.Date_Created AS Date_Created /*Created Date*/ 
      DR.State AS State /*State of Request*/ 
      DR.Deadline AS Deadline /*Deadline of request*/ 
     From [Doctor_Request] AS DR 
     JOIN [USERS] AS USERS 
     ON DR.Requester COLLATE SQL_Latin1_General_CP1_CI_AS = USERS.RID COLLATE SQL_Latin1_General_CP1_CI_AS 
     JOIN [SERVICE_TYPE] AS ST 
     ON DR.Service_Type_Id=ST.Id 
     WHERE DR.Is_Deleted=0 
     ORDER BY DR.Date_Created DESC 
    END 
END 

回答

3

你有没有在你的定义的表别名DR两个询问。例如,它应该像

Select DR.colName, X.colName 
from Table1 DR join Table2 X on DR.id = X.id 
... 

而且在秒查询你需要添加一个commas旁边列Date_Created后......作为

SELECT ... 
    DR.Date_Created AS Date_Created, /*Created Date*/ 
    DR.State AS State, /*State of Request*/ 
    DR.Deadline AS Deadline /*Deadline of request*/ 
FROM ... 
+1

感谢,这是愚蠢的错误...... – Dhwani 2013-02-12 12:18:27