Search code examples
sqlpostgresqlcasesql-function

How to fix query has no destination for result data? - postgreSQL


I'm trying create a function to take active(integer of 0 or 1) from customer and return it as 'Yes' or 'No' in active_status. This query runs just fine but when I actually try to use the function it gives me the error: "query has no destination for result data"

This is the function code, which I thought would take active and transform it, then return active_status. Obviously I have messed up somewhere (or many places honestly).

CREATE OR REPLACE FUNCTION active_func()
RETURNS char
LANGUAGE plpgsql
AS $$
BEGIN
SELECT active,
CASE active
WHEN O THEN 'NO' 
WHEN 1 THEN 'Yes'
END AS active _status
FROM customer;
End; $$

Solution

  • Maybe you need this:

    CREATE OR REPLACE FUNCTION active_func(active integer)
    RETURNS varchar
    LANGUAGE plpgsql
    AS $$
    BEGIN
        if (active = 1) then 
            return 'YES';
        else 
            return 'NO';
        end if;
    End; $$
    

    This function will be return 'YES' or NO

    active_func(0) --> NO
    active_func(1) --> YES
    

    And you can use this function on your selecting tables:

    select id, name, active_func(active) from customer
    
    Result: 
    1   Sarah   YES
    2   Jon     NO 
    3   Smith   YES
    4   Casus   YES