Middle+
What are stored procedures and functions?
sobes.tech AI
Answer from AI
Stored procedures and functions are software objects that are stored and executed directly within the database. They allow encapsulating data processing logic on the DBMS side, which enhances performance and security.
-
Stored Procedure — a set of SQL commands and control logic that executes upon invocation. It can perform insert, update, delete operations, and also return results via parameters or cursors.
-
Function — similar to a procedure but returns a value and can be used in SQL expressions.
Example in SQL (PostgreSQL):
CREATE FUNCTION get_user_email(user_id INT) RETURNS TEXT AS $$
BEGIN
RETURN (SELECT email FROM users WHERE id = user_id);
END;
$$ LANGUAGE plpgsql;
In Go, interaction with stored procedures is usually done through database drivers, calling them using Exec or Query methods.