Function inside sql server

0

I'm trying to make a function inside the sql server, however, I can not use it because an error appears saying that the result is more than one line (Error Code: 1172 Result consisted of more than one row) I can solve this.

'CREATE FUNCTION 'retornaSolo'(id int(11)) RETURNS varchar(80) CHARSET latin1
BEGIN
declare solos varchar (80);
select tipo_solo into solos from solo where status_solo = 'Ativado' 
and cod_usuario = id;
RETURN solos;
END'
    
asked by anonymous 02.11.2017 / 15:43

1 answer

0

Function returns only one result, what you can do is create a stored procedure, or bring the results concatenated and separated by "," which would look something like this:

DROP FUNCTION IF EXISTS retornaSolo;
CREATE FUNCTION 'retornaSolo'(id int(11)) 
RETURNS varchar(80) CHARSET latin1
BEGIN

    DECLARE solos varchar (80);

    SELECT 
            GROUP_CONCAT(DISTINCT tipo_solo SEPARATOR ',') 
    INTO solos 
    FROM solo 
    WHERE status_solo = 'Ativado' 
    AND cod_usuario = id;

RETURN solos;
END;

or the stored procedure that would look like this:

DROP PROCEDURE IF EXISTS sp_retornaSolo;
DELIMITER |
CREATE PROCEDURE sp_retornaSolo(id INT(11))
BEGIN

    SELECT
        tipo_solo
    FROM solo
    WHERE status_solo = 'Ativado'
    AND cod_usuario = id;


END
|
DELIMITER ;
    
16.11.2017 / 14:36