Не могу заставить мою хранимую процедуру, если оператор IF работает - PullRequest
2 голосов
/ 08 февраля 2011

Хранимая процедура 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 ]

6 голосов
/ 08 февраля 2011

Вам нужно поместить между ними НАЧАЛО и КОНЕЦ, если и где-либо еще, как показано ниже.

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

Надеюсь, это поможет.

4 голосов
/ 08 февраля 2011

Вам нужно использовать 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 голосов
/ 08 февраля 2011

Вам нужно поставить Begin и End для включения частей блока 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 проверить исправления.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...