BEGIN
DECLARE @sql NVARCHAR(2000)
IF(@RoleName = '管理员')
SET @sql = 'SELECT * FROM [CustomerServiceDemoDB_zcx].[dbo].[CustomServiceInfo] WHERE 1=1 '
IF(@CusName <> '')
SET @sql = @sql + ' AND CusName = '+CHAR(39)+ @CusName +CHAR(39)
ELSE IF(@EmpName <> '')
SET @sql = @sql + ' AND EmpName = '+CHAR(39)+ @EmpName +CHAR(39)
ELSE IF(@Type <> '')
SET @sql = @sql + ' AND Type = '+CHAR(39)+ @Type +CHAR(39)
ELSE IF(@Audit <> '')
SET @sql = @sql + ' AND Audit = '+CHAR(39)+ @Audit +CHAR(39)
ELSE
SET @sql = @sql + ' ORDER BY ServiceNumber DESC ';
ELSE IF(@RoleName = '经理')
SELECT * FROM CustomServiceInfo CSI, EmployeeInfo EI WHERE CSI.EmpID = EI.EmpID AND EI.AreaName = @AreaName AND EI.RoleName = '工程师' ORDER BY ServiceNumber DESC ;
ELSE IF(@RoleName = '工程师')
SELECT * FROM [CustomerServiceDemoDB_zcx].[dbo].[CustomServiceInfo] WHERE EmpID = (SELECT EmpID FROM EmployeeInfo WHERE UserID = @UserID) ORDER BY ServiceNumber DESC ;
END
由于在第一个IF之间嵌套了一个IF判断,所以在最外层的ELSE IF报错。
请问是在存储过程中不能这样嵌套,还是我哪写错了?
------解决方案--------------------
if else语句块中如果只有单一语句,那么是包含在语体中的
如果是多条语句就不能像你这样写了,需要加begin end
如,
if 1<>2 begin
select 1
select 2
end
------解决方案--------------------
加begin end 哪怕只有一个语句,下面只增加了一对很可能漏掉的BEGIN END,其他的你最好自己也加上
BEGIN
DECLARE @sql NVARCHAR(2000)
IF(@RoleName = '管理员')
BEGIN
SET @sql = 'SELECT * FROM [CustomerServiceDemoDB_zcx].[dbo].[CustomServiceInfo] WHERE 1=1 '
IF(@CusName <> '')
SET @sql = @sql + ' AND CusName = '+CHAR(39)+ @CusName +CHAR(39)
ELSE IF(@EmpName <> '')
SET @sql = @sql + ' AND EmpName = '+CHAR(39)+ @EmpName +CHAR(39)
ELSE IF(@Type <> '')
SET @sql = @sql + ' AND Type = '+CHAR(39)+ @Type +CHAR(39)
ELSE IF(@Audit <> '')
SET @sql = @sql + ' AND Audit = '+CHAR(39)+ @Audit +CHAR(39)
ELSE
SET @sql = @sql + ' ORDER BY ServiceNumber DESC ';
END
ELSE IF(@RoleName = '经理')
SELECT * FROM CustomServiceInfo CSI, EmployeeInfo EI WHERE CSI.EmpID = EI.EmpID AND EI.AreaName = @AreaName AND EI.RoleName = '工程师' ORDER BY ServiceNumber DESC ;