1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[SQL] Issues making a postgresSQL procdude

Discussão em 'Outras Linguagens' iniciado por Stack, Novembro 1, 2024 às 17:12.

  1. Stack

    Stack Membro Participativo

    Im currently working on a personal project. I'm trying to set up a database in postgresSQL. Im trying to to create a procedure that checks to see if an artist's name is already in the table. if it is, increment the count by 1. If not, insert the artist's name and set the count to one. I'm not sure were my problem is. I don't think I have any settings of configuration wrong

    This is currently what I have.

    BEGIN
    -- Check to see if name is already in the table
    IF EXISTS (SELECT 1 FROM artist WHERE artist_name = NEW.artist_name)
    BEGIN
    UPDATE artist
    SET artist_count = artist_count + 1
    WHERE artist_name = NEW.artist_name;
    END
    ELSE
    BEGIN
    INSERT INTO artist (artist_name, artist_count)
    VALUES (NEW.artist_name, 1);
    END

    RETURN NEW
    END


    This is what the final SQL looks like in sql

    CREATE PROCEDURE public.increment_artist_count()
    LANGUAGE 'sql'

    BEGIN
    -- Check to see if name is already in the table
    IF EXISTS (SELECT 1 FROM artist WHERE artist_name = NEW.artist_name)
    BEGIN
    UPDATE artist
    SET artist_count = artist_count + 1
    WHERE artist_name = NEW.artist_name;
    END
    ELSE
    BEGIN
    INSERT INTO artist (artist_name, artist_count)
    VALUES (NEW.artist_name, 1);
    END

    RETURN NEW
    END;

    ALTER PROCEDURE public.increment_artist_count()
    OWNER TO postgres;

    Continue reading...

Compartilhe esta Página