0

ms sql 関数を mysql に変換する際に問題に直面しています。ms sql 関数コードは次のとおりです。

CREATE FUNCTION [crewu2].[isAvailable] (@OwnerID int, @DateFrom Smalldatetime, @DateTo Smalldatetime)  
RETURNS bit AS  
BEGIN  
    DECLARE @t bit  
    IF @DateFrom IS NULL or @DateTo IS NULL 
        or EXISTS (select DateID
                     from [client_BlackDates]
                    where OwnerID=@OwnerID 
                      and ((DateFrom<=@DateFrom and @DateFrom<=DateTo) 
                            or (DateFrom<=@DateTo and @DateTo<=DateTo)))  
        SET @t=0  
    ELSE  
        SET @t=1  
RETURN @t  
END  

そして、これはmysqlで変換されます:

DELIMITER $$  
CREATE FUNCTION isAvailable (OwnerID INT, DateFrom DATETIME, DateTo DATETIME)  
RETURNS BIT   
BEGIN  
    DECLARE t BIT;  
    IF DateFrom IS NULL OR DateTo IS NULL
        OR EXISTS (SELECT DateID
                     FROM client_BlackDates
                    WHERE OwnerID=OwnerID 
                      AND ((DateFrom<=DateFrom AND DateFrom<=DateTo)
                            OR (DateFrom<=DateTo AND DateTo<=DateTo)))  
        SET t=0;  
    ELSE  
        SET t=1;  
    RETURN t  
END $$  
DELIMITER;

しかし、それは私に次のエラーを与えます:

Error Code : 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SET t=0; ELSE SET t=1; RETURN t END' at line 6

これに関して誰か助けてください。

4

2 に答える 2

2

これはうまくいくはずです(あなたは逃したTHENEND IF):

DELIMITER $$
CREATE FUNCTION isAvailable (OwnerID INT, DateFrom DATETIME, DateTo DATETIME)
RETURNS BIT
BEGIN
DECLARE t BIT;
IF DateFrom IS NULL OR DateTo IS NULL OR EXISTS (SELECT DateID FROM client_BlackDates WHERE OwnerID=OwnerID AND ((DateFrom<=DateFrom AND DateFrom<=DateTo) OR (DateFrom<=DateTo AND DateTo<=DateTo)))
**THEN** 
SET t=0;
ELSE
SET t=1;    
RETURN t;
**END IF;**
END $$
DELIMITER ;

MySQLのドキュメントを参照してください。

于 2012-08-03T07:02:55.980 に答える
1

IF..THEN..ELSE シンタックスの MySQL マニュアルを参照してください。

DELIMITER //
CREATE FUNCTION isAvailable (OwnerID INT, DateFrom DATETIME, DateTo DATETIME)  
RETURNS BIT   
BEGIN  
    DECLARE t BIT;  
    IF DateFrom IS NULL OR DateTo IS NULL 
       OR EXISTS (SELECT DateID
                    FROM client_BlackDates
                   WHERE OwnerID=OwnerID
                      AND ((DateFrom<=DateFrom AND DateFrom<=DateTo)
                            OR (DateFrom<=DateTo AND DateTo<=DateTo)))
        THEN
        SET t = 0;
    ELSE  
        SET t = 1;  
    END IF;
    RETURN t;
END //
DELIMITER ;
于 2012-08-03T07:07:49.760 に答える