无法使我的存储过程IF语句正常工作

时间:2011-02-08 16:16:28

标签: sql sql-server sql-server-2005 tsql

存储过程IF语句无法正常工作

    @manuel varchar(50),
    @tour int,
    @tourname varchar(50) OUTPUT ,
    @pricetax int output

    AS BEGIN  -- SET NOCOUNT ON added to prevent extra result sets from  -- interfering with SELECT statements.  SET NOCOUNT ON;      -- Insert statements for procedure here 

    if @manuel = 'no' then 

    SET @tourname = (select [title] from files.dbo.tours where tour = @tour) 

    SET @pricetax = (select top 1 [adult] from files.dbo.trprices where tour = @tour)


    select distinct CONVERT(varchar(12),CAST(CAST(ddate7 AS CHAR) AS DATETIME),101) as ddate7 from files.dbo.TDEPART where tour = @tour and depart > convert(int,getdate())  and status = 'OK'


    else if @manuel='yes' then

     SET @tourname = (select [title] from files.dbo.tours where tour = @tour) 

    SET @pricetax = (select top 1 [adult] from files.dbo.trprices where tour = @tour)


    select distinct CONVERT(varchar(12),CAST(CAST(ddate7 AS CHAR) AS DATETIME),101) as ddate7 from files.dbo.TDEPART where tour = 2525 and depart > convert(int,getdate())  and status = 'OK'




    END 

3 个答案:

答案 0 :(得分:6)

你需要在每个if和else之间放置一个BEGIN和END。

IF (@string = 'hello')
    BEGIN
       --some code
    END
ELSE
    BEGIN
        --some code
    END

希望这有帮助。

答案 1 :(得分:4)

您需要使用BEGIN / END创建块:

@manuel varchar(50),
@tour int,
@tourname varchar(50) OUTPUT ,
@pricetax int output

AS BEGIN  -- SET NOCOUNT ON added to prevent extra result sets from  -- interfering with SELECT statements.  SET NOCOUNT ON;      -- Insert statements for procedure here 

if @manuel = 'no' BEGIN

SET @tourname = (select [title] from files.dbo.tours where tour = @tour) 

SET @pricetax = (select top 1 [adult] from files.dbo.trprices where tour = @tour)


select distinct CONVERT(varchar(12),CAST(CAST(ddate7 AS CHAR) AS DATETIME),101) as ddate7 from files.dbo.TDEPART where tour = @tour and depart > convert(int,getdate())  and status = 'OK'

END
else if @manuel='yes' BEGIN

 SET @tourname = (select [title] from files.dbo.tours where tour = @tour) 

SET @pricetax = (select top 1 [adult] from files.dbo.trprices where tour = @tour)


select distinct CONVERT(varchar(12),CAST(CAST(ddate7 AS CHAR) AS DATETIME),101) as ddate7 from files.dbo.TDEPART where tour = 2525 and depart > convert(int,getdate())  and status = 'OK'

END


END 

答案 2 :(得分:2)

您需要将BeginEnd括起来包含if else if块的部分。

if @manuel = 'no' then 
    BEGIN
    SET @tourname = (select [title] from files.dbo.tours where tour = @tour) 

    SET @pricetax = (select top 1 [adult] from files.dbo.trprices where tour = @tour)


    select distinct CONVERT(varchar(12),CAST(CAST(ddate7 AS CHAR) AS DATETIME),101) as ddate7 from files.dbo.TDEPART where tour = @tour and depart > convert(int,getdate())  and status = 'OK'

    END
else 
Begin 
    if @manuel='yes' then
        BEGIN
        SET @tourname = (select [title] from files.dbo.tours where tour = @tour) 

        SET @pricetax = (select top 1 [adult] from files.dbo.trprices where tour = @tour)


         select distinct CONVERT(varchar(12),CAST(CAST(ddate7 AS CHAR) AS DATETIME),101) as ddate7 from files.dbo.TDEPART where tour = 2525 and depart > convert(int,getdate())  and status = 'OK'
        END
END 

在T-SQL中没有Else If,请检查编辑。

相关问题