For context, I am very new to Teradata SQL and am currently trying to understand how to build out user defined functions (UDF) and procedures (UDP), in SQL form only, though am having some difficulties.
I have created the following UDF to calculate the sum of three inputs
-- 1: how to create the function in the correct database
CREATE FUNCTION db_name.combine_three_numbers (
num1 FLOAT,
num2 FLOAT,
num3 FLOAT
) RETURNS FLOAT
RETURNS NULL ON NULL INPUT
CONTAINS SQL
DETERMINISTIC
COLLATION INVOKER
INLINE TYPE 1
RETURN num1 + num2 - num3;
Using this, I can call the function on both scalars and columns of a table
-- 2: how to call the function from the database on three scalars
SELECT db_name.combine_three_numbers(5, 10, 3) AS num_comb;
-- 3: how to call the function from the database on three columns
SELECT db_name.combine_three_numbers(tbl_name.col1, tbl_name.col2, tbl_name.col3) AS num_comb;
-- 4: how to remove the function from the database
DROP FUNCTION db_name.combine_three_numbers;
and everything works fine. In 2 it returns a column named num_comb with one row containing a value of 12.0 and in 3 it returns a column named num_comb with multiple rows, where each row value is just a linear combination of the three input columns tbl_name.col1, tbl_name.col2, tbl_name.col3
Next, I tried to perform the same calculation using a stored procedure
-- 1: how to create the procedure in the correct database
CREATE PROCEDURE db_name.combine_three_numbers (
IN num1 FLOAT,
IN num2 FLOAT,
IN num3 FLOAT,
OUT num_comb FLOAT
)
BEGIN
SET num_comb = num1 + num2 - num3;
END;
When I call the procedure on scalars
-- 2: how to call the procedure from the database on three scalars
CALL db_name.combine_three_numbers(5, 10, 3, num_comb);
everything works fine, returning the same result as the function equivalent. However, when I try to pass table columns as arguments into a stored procedure
-- 3: how to call the procedure from the database on three columns
CALL db_name.combine_three_numbers(tbl_name.col1, tbl_name.col2, tbl_name.col3, num_comb);
it doesn't work, yielding the error message CALL Failed 5531: (-5531)Named-list is not supported for arguments of a procedure. I am assuming this is because we are unable to pass table columns as arguments to stored procedures, but I can't find anything in the documentation that explicitly says this. So I have two questions
- Is it possible to pass table columns as arguments to a stored procedure?
- If it is possible, how do I modify my procedure to do this?
SELECT db_name.combined_three_numbers(tbl_name.col1, tbl_name.col2, tbl_name.col3) AS num_combis actually implicitly callingSELECT db_name.combined_three_numbers(t.col1, t.col2, t.col3) AS num_comb FROM tbl_name AS t?FROMclause is allowed in conjunction with aSELECTclause, it is not allowed in conjunction with aCALLclause, and hence we can't write a statement of the formCALL db_name.combined_three_numbers(t.col1, t.col2, t.col3, num_comb) FROM tbl_name AS twhich is why I get an error when passing intbl_name.colto the procedure?