Introduction
What’s the difference between a Function vs Stored Procedure in SQL? It’s a question that we hear quite often and we hope we can you provide you with the answer in this
Function vs Stored Procedure
Functions and procedural statements are sets of sql statements which can be called to execute in a given process which would involve running similar statements repetitively. The procedural statements may include a set of flow-of-controls, declarations, loops, and assignments, which can be invoked during a specific program execution using an sql interface. This help in eliminating the grind of duplicating code to complete a given program task.
The main difference between function and stored procedure is that user-defined functions do not execute transactions. This means, inside a given function you cannot open a new transaction, neither can you commit or rollback the current transaction. It is important to note that stored procedures are just functions that can support transactions and were introduced in Postgresql 11. PostgreSQL functions can be created in a variety of languages including SQL, PL/pgSQL, C, Python.
This articles illustrates the application of functions using PostgreSQL.
The syntax for creating a PostgreSQL function is provided below:
Below is a description of the terms used in the syntax above:
- function-name indicates the name of the function.
- [OR REPLACE] in an option to modify an existing function if the current function has the same name as the original function.
- Note : Any complete function must have a return statement.
- RETURN statement tells the function the type of data it is going to return. The return_datatype in this case are normally a base, domain type or a composite. It can also be a reference to the type of a table column.
- function-body has the set of executable statements.
- The AS keyword is applied when creating a standalone function.
- plpgsql represents the name of the language in which the function is implemented. In our examples here we use PostgreSQL, as mentioned earlier , the language can also be SQL, C, internal, or any other user-defined procedural language.
In our application example we will use a student’s table and manipulate it information using a user-defined function. We first use a select statement to view the table content.
Now we will create a function called totalMarks() which will return the total marks of the four students above. We will then call the function to see the results. To sum the marks of the students we utilize the inbuilt aggregate function SUM().
The result of this statement is CREATE FUNCTION.
The following syntax is use when calling stored function:
In our case, we are calling totalMarks();
Results
Stored procedures
As mentioned earlier, Stored procedures are user defined functions that can execute transactions. We use the following syntax to create procedures.
Below is a description of the terms used in writing procedures.
- CREATE PROCEDURE clause precedes the name of the stored procedure procedure_name.
- Parameter_list refers to the list of parameters and is similar to user-defined functions.
- PLpgSQL is the programing language used to write the procedure. It can be done in other languages such as the SQL.
- The procedure statements are placed after the AS key word.
- The double dollar ($) sign indicate the end of the procedure.
Example of stored procedure for correcting students marks
Calling the stored procedure require use of CALL statements
Call syntax:
In our case, the call statement will be as follows
Conclusion
We hope you now understand the difference between a function vs stored procedure in SQL and you can apply it in your specific application. Thank you for keeping up with us at Object Rocket and if you need any help managing your data please don’t hesitate to ask us questions about your specific project.
No comments:
Post a Comment
It’s all about friendly conversation here at small review :) I’d love to be hear your thoughts!
Be sure to check back again because I do make every effort to reply to your comments here.