Home >Database >Mysql Tutorial >MSSQL和Mysql 自定义函数与存储过程_MySQL

MSSQL和Mysql 自定义函数与存储过程_MySQL

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-01 13:57:11728browse

      mysql和mssql自定义都不带切割字符串的函数,例如在输入
(A,B,C),
我们需要得出一个列

数据
A
B
C


  这在系统自己种并不包含,需要自定义函数,先看看MSSQL的自定义函数:
CREATE    function    StrSplit(@c   varchar(2000),@split  varchar(2)=',')  
  returns   @t   table(col   varchar(100))  
  as  
    begin  
   
      while(charindex(@split,@c)0)  
        begin  
          insert   @t(col)   values   (substring(@c,1,charindex(@split,@c)-1))  
          set   @c   =   stuff(@c,1,charindex(@split,@c),'')  
        end  
      insert   @t(col)   values   (@c)  
      return  
    end

GO

 使用的时候
select * from  dbo.StrSplit('52,50,55',','))


即可。

   在MYSQL里面,我也希望能用自定义函数,但是,目前的版本并不支持返回table这种类型的函数,所以不得不考虑使用存储过程。
  首先,了解mysql创建临时表的语句:


set global log_bin_trust_function_creators = 1;
DELIMITER $$

DROP FUNCTION IF EXISTS `Myf`$$

CREATE DEFINER=`root`@`%` FUNCTION `Myf`() RETURNS varchar(50) CHARSET utf8
begin
return concat('-FUNCTION');
end$$

DELIMITER ;


然后看看使用:
SELECT Myf('MYF?');

然后是存储过程:

DELIMITER //

DROP PROCEDURE IF EXISTS split_string //
CREATE PROCEDURE split_string (
    IN input VARCHAR(2000)
    , IN `delimiter` VARCHAR(10)
)
SQL SECURITY INVOKER
COMMENT
'Splits a supplied string using using the given delimiter,
placing values in a temporary table'
BEGIN
    DECLARE cur_position INT DEFAULT 1 ;
    DECLARE remainder VARCHAR(2000);
    DECLARE cur_string VARCHAR(1000);
    DECLARE delimiter_length TINYINT UNSIGNED;
   
    DROP TEMPORARY TABLE IF EXISTS SplitValues;
    CREATE TEMPORARY TABLE SplitValues (
        value VARCHAR(1000) NOT NULL PRIMARY KEY
    ) ENGINE=MyISAM;
   
    SET remainder = input;
    SET delimiter_length = CHAR_LENGTH(delimiter);
   
    WHILE CHAR_LENGTH(remainder) > 0 AND cur_position > 0 DO
        SET cur_position = INSTR(remainder, `delimiter`);
        IF cur_position = 0 THEN
            SET cur_string = remainder;
        ELSE
            SET cur_string = LEFT(remainder, cur_position - 1);
        END IF;
        IF TRIM(cur_string) != '' THEN
            INSERT INTO SplitValues VALUES (cur_string);
        END IF;
        SET remainder = SUBSTRING(remainder, cur_position + delimiter_length);
    END WHILE;
   
END //

DELIMITER ;

/*
Tests
*/

CALL split_string('Steve,Jan,Bobby,Jay,Kaj,Colin',',');

SELECT * FROM SplitValues;

CREATE TEMPORARY TABLE TestJoin (join_field VARCHAR(20) PRIMARY KEY) ENGINE=MEMORY;

INSERT INTO TestJoin VALUES ('Kaj'),('Colin');

SELECT tj.* FROM TestJoin tj
INNER JOIN SplitValues sv
ON tj.join_field = sv.value;
//这个代码来自MYSQL官方,我没有验证

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn