2012-10-08 108 views
-2

我創建了一個存儲過程,以根據傳遞給參數的特定值檢索一些細節。這需要在要由存儲過程執行的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。不知道爲什麼存儲過程沒有編譯。

+0

忘了補充一點,我們使用的是SQL Server 2008數據庫。 –

+2

你是什麼意思「不工作」和「不編譯」?你得到什麼錯誤? – LittleBobbyTables

回答

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 
+0

只要注意到,只要我完成格式化代碼牆。爲了澄清,「IF ... ELSE」語句不需要結束「END」。 – LittleBobbyTables

+0

@LittleBobbyTables正確,在這個過程中有太多的ENDs – Taryn

+0

拍攝,當天的upvotes。回到3小時! – LittleBobbyTables