skip to Main Content

After defining a table-valued function in mssql
select * from funcA() is possible.

But mysql seems impossible, is that correct?

How can I do it in MYSQL?
If this is not possible with a function, is there a similar way?
I want to split a string and bring it into a table to create the data I want.

2

Answers


  1. in MySql it’s not possible, but what you can do is create a stored procedure that splits your string and stores the data in tempTable. the. fetch your data from tempTable

    Login or Signup to reply.
  2. Yes, MySQL is not able to support table valued functions.

    1. You can do this with creating procedures similar to this
    IN str VARCHAR(255),
     OUT table_result TABLE(
       column_1 VARCHAR(255),
       column_2 VARCHAR(255)
     )
    )
    BEGIN
     DECLARE i INT DEFAULT 0;
     WHILE i < LENGTH(str) DO
       SET table_result.column_1 = SUBSTRING(str, i, 1);
       SET table_result.column_2 = SUBSTRING(str, i + 1, 1);
       SET i = i + 2;
     END WHILE;
    END;
    

    for execution you can use :

     CALL split_string('Hello World', @table_result);
    
    1. The other way is to use a temporary table:
    DECLARE str VARCHAR(255) DEFAULT 'Hello World';
    CREATE TEMPORARY TABLE table_result (
      column_1 VARCHAR(255),
      column_2 VARCHAR(255)
    );
    INSERT INTO table_result (column_1, column_2)
    SELECT SUBSTRING(str, i, 1), SUBSTRING(str, i + 1, 1)
    FROM
      (
        SELECT 1 AS i, 2 AS i
      ) AS t
    WHERE
      i < LENGTH(str);
    

    This will help you to delete the table.

    I hope this will work!!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search