将MSSQL函数转换为MySQL函数

时间:2014-02-04 12:48:19

标签: mysql sql-server mysql-workbench

我正在尝试转换这个用户定义的函数(取自MSSQL)并调整它以便我可以运行它MYSQL。我做了几次尝试,但似乎都在declare变量上出错了。

我正在运行以下版本:5.6.11-log - MySQL社区服务器(GPL)

USE [DataGB]
GO
/****** Object:  UserDefinedFunction [dbo].[FullPostCodeFix]    Script Date: 11/20/2013      16:10:44 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO


CREATE FUNCTION [dbo].[FullPostCodeFix] (@Postcode  VARCHAR(20))  
RETURNS VARCHAR(20)

BEGIN 
/*
Puts postcode into correct format if it is currently in any of the below formats
AB12AA
AB 12AA
AB1 2AA
AB 1 2AA
Returns @Postcode
*/

DECL ARE @District Varchar(50)
DECLARE @Remainder Varchar(50)
DECLARE @Sector Varchar(50)

SET @District=  CASE 
            WHEN LEN(@Postcode) - CHARINDEX(' ', REVERSE(@Postcode)) =     len(@Postcode) THEN SUBSTRING(@Postcode,1,(len(@Postcode) - 3))
            WHEN LEN(@Postcode) - CHARINDEX(' ', REVERSE(@Postcode)) < 3 THEN     SUBSTRING(@Postcode,1,(len(@Postcode) - 3))
            ELSE SUBSTRING(@Postcode, 0, LEN(@Postcode) - CHARINDEX(' ', REVERSE(@Postcode)) + 1)
        END


SET @District = dbo.PostcodeFix(@District)

SET @Remainder= RIGHT(@Postcode,3)

SET @Sector = @District +  ' ' + LEFT(@Remainder,1)

SET @Postcode = @District +  ' ' + @Remainder

RETURN @Postcode

END

我创建MYSQL版本的尝试如下:

CREATE FUNCTION FullPostCodeFix (Postcode  VARCHAR(20))  
RETURNS VARCHAR(20)

BEGIN 


DECLARE District Varchar(50)
DECLARE Remainder Varchar(50)
DECLARE Sector Varchar(50)

SET District=  CASE 
            WHEN LEN(Postcode) - CHARINDEX(' ', REVERSE(Postcode)) =     len(Postcode) THEN SUBSTRING(Postcode,1,(len(Postcode) - 3))
            WHEN LEN(Postcode) - CHARINDEX(' ', REVERSE(Postcode)) < 3 THEN     SUBSTRING(Postcode,1,(len(Postcode) - 3))
            ELSE SUBSTRING(Postcode, 0, LEN(Postcode) - CHARINDEX(' ',     REVERSE(Postcode)) + 1)
        END

SET District = dbo.PostcodeFix(District)

SET Remainder= RIGHT(Postcode,3)

SET Sector = District +  ' ' + LEFT(Remainder,1)

SET Postcode = District +  ' ' + Remainder

RETURN Postcode

END

我得到的错误如下:

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 'DECLARE Remainder Varchar(50) DECLARE Sector Varchar(50)

在函数“FullPostCodeFix”中调用了另一个函数。这是我的尝试:

DELIMITER $$
CREATE FUNCTION PostCodeFix (strDistrict  VARCHAR(20))  
RETURNS VARCHAR(20)
DETERMINISTIC
BEGIN 
    DECLARE intASCII INTEGER;
        SET strDistrict = LTRIM(strDistrict);
        SET strDistrict = RTRIM(strDistrict);

            IF LENGTH(strDistrict) > 4  OR LENGTH(strDistrict) = 0 THEN RETURN 'ERROR: ' + strDistrict; 
            ELSE 
                BEGIN
                    SET intASCII = ASCII(LEFT(strDistrict, 1));
                    IF  ( intASCII > 47 AND intASCII < 58 ) THEN RETURN 'ERROR: ' + strDistrict;
                    ELSE
                        BEGIN
                            SET intASCII = ASCII(SUBSTRING(strDistrict, 2, 1));
                            IF  ( intASCII > 47 AND intASCII < 58 ) THEN SET strDistrict = LEFT(strDistrict, 1) + ' ' + RIGHT(strDistrict, LENGTH(strDistrict)-1);
                                SET intASCII = ASCII(SUBSTRING(strDistrict, 3, 1));
                            IF  ( intASCII < 48 OR intASCII > 57 ) AND (intASCII <> 32) THEN RETURN 'ERROR: ' + strDistrict; 
                            ELSE IF LENGTH(strDistrict) < 4 THEN     SET strDistrict = LEFT(strDistrict, 2) + ' ' + RIGHT(strDistrict, LENGTH(strDistrict)-2); 
                            END IF;
                        END IF;
                RETURN strDistrict;
            END IF;

1 个答案:

答案 0 :(得分:2)

好的,让我们开始,用MySQL等效函数替换MS-SQL函数。

 MSSQL         MySQL
 LEN()         LENGTH()
 SUBTRING()    SUBSTR()
 CHARINDEX()   INSTR()

下面是所有MySQL String函数的文档和列表 http://dev.mysql.com/doc/refman/5.0/en/string-functions.html

这是我验证过的正确的MySQL语法。

CREATE FUNCTION FullPostCodeFix (Postcode  VARCHAR(20)) RETURNS VARCHAR(20) 
BEGIN   
    DECLARE district VARCHAR(50);
    DECLARE remainder VARCHAR(50);
    DECLARE sector VARCHAR(50);
        IF LENGTH(Postcode) - INSTR(' ', REVERSE(Postcode)) = LENGTH(Postcode) THEN SET district = SUBSTR(Postcode,1,(LENGTH(Postcode) - 3));
        ELSEIF LENGTH(Postcode) - INSTR(' ', REVERSE(Postcode)) < 3 THEN SET district = SUBSTR(Postcode,1,(LENGTH(Postcode) - 3));
        ELSE SET district = SUBSTR(Postcode, 0, LENGTH(Postcode) - INSTR(' ',     REVERSE(Postcode)) + 1);
        END IF;  
    SET District = dbo.PostcodeFix(District);
    SET Remainder= RIGHT(Postcode,3);
    SET Sector = CONCAT(District,' ',LEFT(Remainder,1));
    SET Postcode = CONCAT(District,' ',Remainder);  
    RETURN Postcode;
END

这是你的第二个功能

CREATE FUNCTION PostCodeFix (strDistrict  VARCHAR(20))  
RETURNS VARCHAR(20)
DETERMINISTIC
BEGIN 
    DECLARE intASCII INTEGER;
        SET strDistrict = LTRIM(strDistrict);
        SET strDistrict = RTRIM(strDistrict);

            IF LENGTH(strDistrict) > 4  OR LENGTH(strDistrict) = 0 THEN RETURN 'ERROR: ' + strDistrict; 
            ELSE                 
                    SET intASCII = ASCII(LEFT(strDistrict, 1));
                    IF  ( intASCII > 47 AND intASCII < 58 ) THEN RETURN 'ERROR: ' + strDistrict;
                    ELSE                        
                        SET intASCII = ASCII(SUBSTRING(strDistrict, 2, 1));
                        IF  ( intASCII > 47 AND intASCII < 58 ) THEN SET strDistrict = LEFT(strDistrict, 1) + ' ' + RIGHT(strDistrict, LENGTH(strDistrict)-1);
                        END IF;
                        SET intASCII = ASCII(SUBSTRING(strDistrict, 3, 1));
                        IF  ( intASCII < 48 OR intASCII > 57 ) AND (intASCII <> 32) THEN RETURN 'ERROR: ' + strDistrict; 
                        ELSEIF LENGTH(strDistrict) < 4 THEN     SET strDistrict = LEFT(strDistrict, 2) + ' ' + RIGHT(strDistrict, LENGTH(strDistrict)-2); 
                        END IF;                        
                    END IF;
                RETURN strDistrict;                
            END IF;
END