'How to call Stored Procedure / Function dynamically in Spring Boot
Is it possible to create a stored procedure / function call in spring boot based on DBMeta data. So that I can set the parameters and its types based on the Meta Data dynamically.
Example: A table will be having 1 stored function for retrieval and 3 stored procedures for insert/delete/update. This will be the case for all tables.
If SPTEST is the table with 5 columns with different data types. Then there will be
SPTEST_PKG.SPTEST_R - This will be a function for retrieval only, with all columns as parameters and return a ref_cursor
SPTEST_PKG.SPTEST_I - This will be for Inserting data, with all columns as parameters
SPTEST_PKG.SPTEST_U - This will be for Inserting data, with all columns as parameters(always updates the full data)
SPTEST_PKG.SPTEST_D - For delete a row.
How can we call these functions / procedure dynamically in spring boot. This will be created as a package and the DB is Oracle 19c. The retrieval function always returns a cursor.
SPTEST Package body example
create or replace NONEDITIONABLE PACKAGE BODY SpTest_pkg
AS
FUNCTION SpTest_r (
p_Name IN SpTest.name%TYPE DEFAULT NULL
,p_DOB IN SpTest.Dob%TYPE DEFAULT NULL
,p_TraDate IN SpTest.TraDate%TYPE DEFAULT NULL
,p_Alive IN SpTest.Alive%TYPE DEFAULT NULL
,p_Age IN SpTest.Age%TYPE DEFAULT NULL
)
RETURN ref_output
AS
ref_cur ref_output;
BEGIN
-- PK
IF p_Name IS NOT NULL
THEN
OPEN ref_cur FOR
SELECT
Name AS "Name"
,DOB AS "DOB"
,TraDate AS "TraDate"
,Alive AS "Alive"
,Age AS "Age"
FROM SPTEST
WHERE
(Name = p_Name)
;
-- All
ELSE
OPEN ref_cur FOR
SELECT
Name AS "Name"
,DOB AS "DOB"
,TraDate AS "TraDate"
,Alive AS "Alive"
,Age AS "Age"
FROM SPTEST
;
END IF;
RETURN ref_cur;
EXCEPTION
WHEN OTHERS
THEN
errpkg.handle (SQLCODE, SQLERRM);
RAISE;
END SpTest_r;
PROCEDURE SpTest_i (
p_Name IN SpTest.name%TYPE
,p_DOB IN SpTest.Dob%TYPE DEFAULT NULL
,p_TraDate IN SpTest.TraDate%TYPE DEFAULT NULL
,p_Alive IN SpTest.Alive%TYPE DEFAULT NULL
,p_Age IN SpTest.Age%TYPE DEFAULT NULL
)
AS
BEGIN
INSERT INTO SpTest
(
Name
,DOB
,TraDate
,Alive
,Age
)
VALUES
(
p_Name
,p_dob
,p_tradate
,p_alive
,p_age
);
EXCEPTION
WHEN OTHERS
THEN
errpkg.handle (SQLCODE, SQLERRM);
RAISE;
END SpTest_c;
PROCEDURE SpTest_d (
p_Name IN SpTest.name%TYPE
)
AS
BEGIN
DELETE FROM SpTest
WHERE
name = p_name
;
EXCEPTION
WHEN OTHERS
THEN
errpkg.handle (SQLCODE, SQLERRM);
RAISE;
END SpTest_d;
PROCEDURE SpTest_u (
p_Name IN SpTest.name%TYPE
,p_DOB IN SpTest.Dob%TYPE DEFAULT NULL
,p_TraDate IN SpTest.TraDate%TYPE DEFAULT NULL
,p_Alive IN SpTest.Alive%TYPE DEFAULT NULL
,p_Age IN SpTest.Age%TYPE DEFAULT NULL
)
AS
BEGIN
UPDATE SpTest
SET
dob = p_dob
,tradate = p_tradate
,alive = p_alive
,age = p_age
WHERE
name = p_name
;
EXCEPTION
WHEN OTHERS
THEN
errpkg.handle (SQLCODE, SQLERRM);
RAISE;
END SpTest_u;
END SpTest_pkg;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|