sql create function错误代码1064

时间:2014-09-29 02:39:46

标签: mysql stored-procedures

我需要创建一个我在这里找到的函数:http://vyaskn.tripod.com/code/propercase.txt它将文本转换为“ProperCase”,每个单词的第一个字母为大写。

CREATE FUNCTION PROPERCASE
(
-- The string to be converted to proper case
@input VARCHAR( 8000 )
)
-- This function returns the proper case string of varchar type
RETURNS VARCHAR( 8000 )
AS
BEGIN
IF @input IS NULL 
BEGIN
    -- Just return NULL if input string is NULL
    RETURN NULL
END

-- Character variable declarations
DECLARE @output VARCHAR( 8000 )
-- Integer variable declarations
DECLARE @ctr INT, @len INT, @found_at INT
-- Constant declarations
DECLARE @LOWER_CASE_a INT, @LOWER_CASE_z INT, @Delimiter CHAR(3), @UPPER_CASE_A INT, @UPPER_CASE_Z INT

-- Variable/Constant initializations
SET @ctr = 1
SET @len = LEN(@input)
SET @output = ''
SET @LOWER_CASE_a = 97
SET @LOWER_CASE_z = 122
SET @Delimiter = ' ,-'
SET @UPPER_CASE_A = 65
SET @UPPER_CASE_Z = 90

WHILE @ctr <= @len
BEGIN
    -- This loop will take care of reccuring white spaces
    WHILE CHARINDEX(SUBSTRING(@input,@ctr,1), @Delimiter) > 0
    BEGIN
        SET @output = @output + SUBSTRING(@input,@ctr,1)
        SET @ctr = @ctr + 1
    END

    IF ASCII(SUBSTRING(@input,@ctr,1)) BETWEEN @LOWER_CASE_a AND @LOWER_CASE_z
    BEGIN
        -- Converting the first character to upper case
        SET @output = @output + UPPER(SUBSTRING(@input,@ctr,1))
    END
    ELSE
    BEGIN
        SET @output = @output + SUBSTRING(@input,@ctr,1)
    END

    SET @ctr = @ctr + 1

    WHILE CHARINDEX(SUBSTRING(@input,@ctr,1), @Delimiter) = 0 AND (@ctr <= @len)
    BEGIN
        IF ASCII(SUBSTRING(@input,@ctr,1)) BETWEEN @UPPER_CASE_A AND @UPPER_CASE_Z
        BEGIN
            SET @output = @output + LOWER(SUBSTRING(@input,@ctr,1))
        END
        ELSE
        BEGIN
            SET @output = @output + SUBSTRING(@input,@ctr,1)
        END
        SET @ctr = @ctr + 1
    END     
END
RETURN @output

END

我需要一个功能来做到这一点,但它给了我错误......

1 个答案:

答案 0 :(得分:1)

您正在使用MySQL,但您使用的语法是SQL Server。请阅读有关MySQL语法的文档,并转换您的过程以使用该语法。主要构造是相同的,但语法略有不同。以下是一些事情:

  • 本地变量永远不会以@。
  • 开头
  • IF是条件,后跟THEN,后跟任意数量的代码行,后跟END IFBEGIN...END结构不用于MySQL中的IF语句。
  • 功能不同。您不会使用CHARINDEX,而是使用INSTR

以下是相关的MySQL文档:http://dev.mysql.com/doc/refman/5.5/en/stored-routines-syntax.htmlhttp://dev.mysql.com/doc/refman/5.5/en/sql-syntax-compound-statements.html