dishanm
dishanm

Reputation: 120

Select into variable not working

This is the mysql stored procedure to just print the fetched val from select query. Not sure where am i going wrong , it always prints null

DELIMITER $$
DROP PROCEDURE IF EXISTS test $$
CREATE PROCEDURE test(IN input VARCHAR(5000),OUT output VARCHAR(5000))
 BEGIN 
    DECLARE detail_id VARCHAR(128);
    DECLARE val VARCHAR(128);

    SET output = CONCAT(output, "0", ':', "Starting");

    select split_string_new(input,':', 1) into val;

    SET output = CONCAT(output, "detail_id0---",detail_id,"....");

    SET detail_id = "";

    SELECT `dtl_id` FROM detail_tbl WHERE `dtl_val` = val INTO detail_id; 
    SET output = CONCAT(output, "detail_id1---",detail_id,"....");

 END $$
DELIMITER ;

call test('123',@output);SELECT @output;

I tried few solutions found on stackoverflow only like below and few more , but still unsuccessful. Any help on this will be appreciated !! MySQL Stored procedure variables from SELECT statements MySql SELECT INTO Variable: returns null

Upvotes: 0

Views: 836

Answers (1)

Eric
Eric

Reputation: 3257

Try to put INTO clause before FROM clause.

SELECT `dtl_id` INTO detail_id FROM detail_tbl WHERE `dtl_val` = val; 

Upvotes: 1

Related Questions