2017-10-15 124 views
1

我最近從sql server遷移到mysql,我無法將我的sql存儲過程轉換爲mysql存儲過程。 這是存儲在我的SQL SERVER程序:我怎樣才能把這個翻譯成mysql?

CREATE PROCEDURE dbo.GetGrades 
    @GradeId int = 0 
    ,@Name nvarchar(100) = '' 
AS 
BEGIN 
    SET NOCOUNT ON; 


    SELECT ISNULL(I.GradeId,0) as GradeId 
     ,ISNULL(I.Name, '') as Name 
     ,ISNULL(I.Details, '') as Details 
     ,ISNULL(I.StatId, 0) as StatId 
     ,ISNULL(I.CreatedById, 0) as CreatedById 
     ,ISNULL(U.UserName, '') as CreatedByName 
     ,ISNULL(I.CreatedOn, null) as CreatedOn 

    FROM Grades I 

     LEFT JOIN Users U ON U.UserId = I.CreatedById 

    WHERE (I.GradeId = @GradeId OR @GradeId = 0) 
     AND (I.Name LIKE '%' + @Name +'%' OR @Name = '') 
     AND I.StatId <> 2 

END 

GO 

這就是我在MySQL存儲過程中迄今所做的:

DELIMITER $$ 

CREATE PROCEDURE GetGrades 
( 
    IN intGradeId INT 
    ,IN strName VARCHAR(1000)  
) 
BEGIN 
SELECT 
     gradeId 
    , gradeName 
    , details 
    , statId 
    , createdById 
    , createdOn 
FROM grades 
WHERE (gradeId = intGradeId OR intGradeId = 0) 
AND (gradeName LIKE '%' + strName +'%' OR strName = ''); 
END 

當我執行這個,這是錯誤消息:

MySQL said: Documentation 

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '+ strName +'%' OR strName = '') 
AND statId = 1; 
END' at line 16 

此外,MySQL中是否有一個ISNULL()函數?

回答

2

事情是這樣的:

DELIMITER $$ 

CREATE PROCEDURE GetGrades ( 
    IN in_intGradeId INT, 
    IN in_strName VARCHAR(1000)  
) 
BEGIN 
    SELECT gradeId, gradeName, details, statId, createdById, createdOn 
    FROM grades 
    WHERE (gradeId = in_intGradeId OR in_intGradeId = 0) AND 
      (gradeName LIKE CONCAT('%', in_strName, '%') OR in_strName = ''); 
END;