MSSQL and Mysql User-defined functions and stored procedures _ MySQL

Source: Internet
Author: User
MSSQL and Mysql User-defined functions and stored procedures mysql and mssql user-defined functions without the cut string, such as in the input
(A, B, C ),
We need to obtain a column

Data
A
B
C


This is not included in the system itself. you need to define a function. first look at the custom function of 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

When using
Select * from dbo. StrSplit ('52, 50, 55 ',','))


You can.

In MYSQL, I also want to use user-defined functions. However, the current version does not support returning functions of the table type, so I have to consider using stored procedures.
First, understand the statement for creating a temporary table in 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;


Then let's take a look at the usage:
SELECT Myf ('myf? ');

Then there is the stored procedure:

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 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;
// This code is from the MYSQL official website and I have not verified it

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.