Data Insertion with PostgreSQL Functions

The difference between a Procedure and a Function depends on how the DBMS has implemented these objects. However, PostgreSQL has long provided access to functions. Therefore, I created a function that doesn't return any value but inserts records into a table called SomeTable. You simply need to call the function to perform the insertion. CREATE OR REPLACE FUNCTION charge() RETURNS VOID AS $$
DECLARE i INTEGER := 0;

BEGIN CREATE TABLE IF NOT EXISTS SomeTable(Id INTEGER GENERATED ALWAYS AS IDENTITY, Description VARCHAR, CONSTRAINT pk_SomeTable PRIMARY KEY(Id));
    WHILE i < 10 LOOP
        INSERT INTO SomeTable(Description) VALUES ('Lorem ipsum dolor sit amet, consectetur adipiscing elit.');
        i = i + 1;
    END LOOP;
END;
$$ LANGUAGE plpgsql;

Comentários

Postagens mais visitadas deste blog

Integration of PostgreSQL and pgAdmin Containers with Docker Compose

Safely Refactoring Endpoints with Postman Tests

Views and Functions in PostgreSQL