存储过程无效

时间:2012-10-08 20:38:53

标签: sql-server sql-server-2008 stored-procedures

我已经创建了一个存储过程来根据传递给参数的特定值来检索一些细节。这需要在存储过程执行的SQL之间切换。以下是代码:

USE [DFS]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[DAFS]
@EmailID Nvarchar(128),
@clientID int,
@userType Varchar(50),
@Success numeric output, 
@msg varchar(100) output  

AS

BEGIN
    if @userType='Normal User'
        IF EXISTS (SELECT 1 FROM dbo.Allcdn 
                    WHERE EmailID = @EmailID AND ClientID = @clientID) 
            begin
                set @Success=0
                set @msg='Carry on ....'
            end
        else
            begin
                set @Success=6  
                set @msg='Not allowed ...' 
            END
        end
    else
        Begin
            IF EXISTS (SELECT 1 FROM dbo.Alcon 
                        WHERE EmailID = @EmailID AND ClientID = @clientID) 
                BEGIN
                    set @Success=0
                    set @msg='Carry on...' 
                END 
            END
        End
    end
END

整个处理基于变量@userType。不确定为什么存储过程没有编译。

1 个答案:

答案 0 :(得分:4)

格式化是您的朋友,只需快速浏览一下,您就会看到END太多了 - 请参阅SQL Fiddle with working Demo

CREATE PROCEDURE [dbo].[DAFS]
  @EmailID Nvarchar(128),
  @clientID int,
  @userType Varchar(50),
  @Success numeric output, 
  @msg varchar(100) output  

AS
BEGIN
  if @userType='Normal User'
    IF EXISTS (SELECT 1 FROM dbo.Allcdn 
                WHERE EmailID = @EmailID AND ClientID = @clientID) 
      begin
        set @Success=0
        set @msg='Carry on ....'
      end
    else
      begin
        set @Success=6  
        set @msg='Not allowed ...' 
      END
  else
    Begin
      IF EXISTS (SELECT 1 FROM dbo.Alcon 
                    WHERE EmailID = @EmailID AND ClientID = @clientID)
        BEGIN
          set @Success=0
          set @msg='Carry on...' 
        END 
    END
end