Tuesday, 11 August 2020

How To Create A Stored Procedure And Insert Data Into A Table By Calling...




How To Create A Stored Procedure And Insert Data Into A Table By Calling/Using The Stored Procedure

In this tutorial, you’ll master the process of creating a stored procedure in PostgreSQL and learn how to insert data into a table by calling this procedure. Stored procedures are essential for encapsulating complex SQL operations, and this video will guide you through each step of the process.

We begin by introducing the concept of stored procedures and their benefits, particularly in terms of code reuse and simplifying database management. You'll see a detailed explanation of how to define a stored procedure in PL/pgSQL, the procedural language of PostgreSQL.

Next, we focus on a practical example: creating a stored procedure that inserts data into a specific table. This part of the video will show you:

  • How to declare the procedure.
  • How to define the input parameters that will pass the data to be inserted.
  • How to write the SQL commands within the procedure to perform the data insertion.

Finally, you’ll learn how to call this stored procedure from both pgAdmin and SQL Shell (psql), ensuring that you can apply this knowledge in your daily database management tasks.

This tutorial is perfect for database administrators, developers, and anyone looking to improve their PostgreSQL skills. By the end of the video, you’ll have the knowledge to efficiently create and utilize stored procedures for data manipulation in PostgreSQL.

PostgreSQL, stored procedure, insert data, SQL, table insertion, PLpgSQL, PostgreSQL tutorial, database management, pgAdmin, SQL Shell, data manipulation

-- the purpose is to create a simple stored procedure in PostgreSQL

-- and Insert Data in the table by calling it using pgAdmin
CREATE TABLE public.testing_table (dummy_column text);
CREATE OR REPLACE PROCEDURE public.testing_procedure(p_msg text)
LANGUAGE 'plpgsql'
AS $BODY$
DECLARE
BEGIN
INSERT INTO public.testing_table(dummy_column) VALUES('Before Inserting Parameter Value');
INSERT INTO public.testing_table(dummy_column) VALUES (p_msg);
INSERT INTO public.testing_table(dummy_column) VALUES('After Inserting Parameter Value');
END;
$BODY$;
SELECT * FROM public.testing_table;
DELETE from public.testing_table;

CALL public.testing_procedure('Hello Akram, How are you?');
Before Inserting
the message I pass as a parameter
After Inserting...

No comments:

Post a Comment