2014-09-29 103 views
-1

我需要創建一個函數,我在這裏找到:http://vyaskn.tripod.com/code/propercase.txt它將文本轉換爲「ProperCase」,每個單詞的第一個字母大寫。sql創建函數錯誤代碼1064

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

您使用的數據庫是? – 2014-09-29 02:41:20

+2

「但它給我錯誤」不是一個問題。 – 2014-09-29 02:42:34

+0

什麼是錯誤? – siride 2014-09-29 03:18:48

回答

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

+0

謝謝,該信息幫助了我很多! :) – 2014-10-06 18:42:04