Display result of a variable in teradata stored procedure - stored-procedures

I need to print results of variables in Teradata stored procedures, but print does not work in teradata, should I use the below method or please suggest the proper one:
My code:
REPLACE PROCEDURE Name()
---some code goes here
BEGIN
TRANSACTION;
SET var1= var2+ 3;
SET var3= var2;
/* !!! PRINT var1*/
/* !!! PRINT var3*/
end;
--some code here
I used the out in stored procedure declaration, suggest me another way to print the values of variable:
REPLACE PROCEDURE Name(out var1, out var3)

As long as there's only a single line of values has to be returned you can utilize OUT variables.
For multiple lines you can use a Volatile Table where those lines are inserted and a final DYNAMIC RESULT SET returns them as an answer set.

If you want to print the value of a variable inside a stored procedure I assume you are asking in the context of debugging the store procedure. Teradata doesn't appear to have the debugging tricks that other databases provide, e.g. RAISE and NOTICE. One workaround is the create a log table with at least one column, message VARCHAR(500) and then insert your debugging messages into that table.

Related

Use of variable SET statement in stored procedures compiles but fires exception at runtime

In a Snowflake SQL-stored procedure, I would like to SET value of a variable like the following:
DROP PROCEDURE IF EXISTS MY_DB.MY_SCHEMA.MY_SP_SQL();
CREATE PROCEDURE MY_DB.MY_SCHEMA.MY_SP_SQL()
RETURNS STRING NOT NULL
LANGUAGE SQL
AS
DECLARE
retValue VARCHAR;
BEGIN
SET retVal = '';
[...]
Everything rocks at compile time but fails at runtime with the following exception
Uncaught exception of type 'STATEMENT_ERROR' on line x at position y :
Stored procedure execution error: Unsupported statement type 'SET'.
despite documentation says this to be possible ๐Ÿคจ
Can someone gently explain to the noob of me? ๐Ÿ˜
Thanks,
Within a SQL stored procedure use LET instead of SET. The syntax for stored procedures (also known as Snowflake Scripting) is different from interactive SQL. The valid commands in SQL scripting are described in the Snowflake Scripting Reference

Dynamic SQL calling another stored procedure in Teradata

I've seen in the help guides that you cannot use a call statement in a teradata dynamic sql statement without output parameter.
I assume this implies I can, If the proc has an output parameter.
has anyone done this?
Scenario -
I have a table that at some point I'll expand out in terms of fields for logic on when things should run, and this table is managed elsewhere -
CREATE TABLE DB.SP_Test
(
ProcName VARCHAR(250)
,ProcRun VARCHAR(1)
);
now I added chrTest as an output however, I am still getting an error on run (no compile error)
The error :-
SQL_State SQL_Exception
T7689 Invalid dynamic SQL statement.
REPLACE PROCEDURE DB.Test_Control (OUT chrTest VARCHAR(250) )
SQL SECURITY INVOKER
LMain:
BEGIN
DECLARE sqlProcRun VARCHAR(20000);
DECLARE CONTINUE HANDLER
FOR SqlException
BEGIN
-----------------------------------------------------------------------------
DECLARE strExceptionText VARCHAR(250);
GET DIAGNOSTICS EXCEPTION 1 strExceptionText = Message_Text;
INSERT INTO DB.PROC_ERROR VALUES
(
'Test_Control'
,:SqlState
,strExceptionText
,Current_Timestamp
)
;
END;
-----------------------------------------------------------------------------
SET sqlProcRun ='';
L0:
FOR procs_run_cursor AS select_list
CURSOR FOR
SELECT Trim(ProcName) AS ProcName
FROM DB.SP_Test
DO
/*creating a lost of call statements to run*/
SET sqlProcRun = sqlProcRun ||'CALL DB.'||procs_run_cursor.ProcName|| '();';
END FOR L0;
EXECUTE IMMEDIATE sqlProcRun;
END;

How to get the restult table of a stored procedure into a temp table?

Enviroment:
DB2 Version 11.1,
OS - Linux
How to get the result table of stored procedure into a temp table?
The table and the result have the same table configuration (firstColumn int, secondColumn nvarchar(255))
I'm assuming your stored procedure returns an open cursor, so you want to consume that cursor, inserting its contents into a session table (declared global temporary table) on Db2-LUW.
In addition to fetch and insert statements, you need to understand the following statements:
associate result set locator ... with procedure ...
allocate ... cursor for result set ...
Here is a deliberately artificial example of a nested stored procedure, which shows fetching a result-set from a nested procedure into a session table. The purpose is to show how the syntax works, rather than to do anything useful with data (as the net effect can be equally met by a simple catalog query in this case). This example can be run at the Db2 command-line (for example at the bash shell, after you connected to a database with appropriate permissions):
update command options using s on ;
--#SET TERMINATOR #
create or replace procedure alltabs
dynamic result sets 1
language sql
specific alltabs
begin
declare v_cur cursor with return to caller for select tabschema,tabname,type from syscat.tables ;
open v_cur;
end#
declare global temporary table session.thetables(tabschema varchar(128), tabname varchar(128))
not logged with replace on commit preserve rows #
create or replace procedure populate_dgtt()
language sql
specific populate_dgtt
begin
declare v_rs result_set_locator varying;
declare v_tabschema varchar(128);
declare v_tabname varchar(128);
declare v_type char(1);
declare sqlstate char(5) default '00000';
call alltabs;
associate result set locator (v_rs) with procedure alltabs;
allocate v_rscur cursor for result set v_rs;
fetch from v_rscur into v_tabschema, v_tabname, v_type;
while ( sqlstate = '00000') do
if v_type='V' and v_tabschema='SYSSTAT'
then
insert into session.thetables(tabschema,tabname) values (v_tabschema, v_tabname);
end if;
fetch from v_rscur into v_tabschema, v_tabname, v_type;
end while;
return;
end#
call populate_dgtt()#
select rtrim(Tabschema)||'.'||rtrim(tabname) from session.thetables #

passing arrays as parameters to plsql procedure

I need was playing around with sql developer and I have ran into a wall here...
I need to run a simple update query on a table and I want to pass in an array of Ids and update all the rows pointed by those Ids.
I have written the following stored procedure
PROCEDURE SAMPLE_PROCEDURE(SAMPLE_ARRAY IN NUM_ARRAY)
AS
BEGIN
UPDATE RETURNLIST_PICKLIST_MAPPING
SET PICKLIST_ID = 1111111
WHERE RETURNLIST_ID IN (SELECT * FROM TABLE(SAMPLE_ARRAY));
END SAMPLE_PROCEDURE;
NUM_ARRAY is a custom type defined as follows
create or replace
TYPE NUM_ARRAY
AS VARRAY(40) OF NUMBER(38, 0);
When I run the stored procedure in sql developer I want to input the value for SAMPLE_ARRAY. I have tried (2222,1111,1234) and [2222,1111,1234] and {2222,1111,1234} and each time I get "expression is of wrong type" error.
I desperate need help with this guys....
You did not show how did you assign values to your varray variable. But, I believe you can do it like this:
DECLARE
V_T NUM_ARRAY;
BEGIN
V_T := NUM_ARRAY(1,2,3);
SAMPLE_PROCEDURE(V_T);
END;
/
In general, you can define a standalone VARRAY as follows:
CREATE Or REPLACE TYPE varray_type_name AS VARRAY(n) OF <element_type>;
Or, within PL/SQL block:
TYPE varray_type_name IS VARRAY(n) of <element_type>
Refer to this for more details

How to define an Array of values (or a Column) into a Procedure Argument?

I am working on a Netezza SP and is stuck with a problem.
I have a SP, defined as say:
CREATE OR REPLACE PROCEDURE MY_PROC(VARCHAR(ANY)) RETURNS INTEGER LANGUAGE NZPLSQL
AS
BEGIN_PROC
DECLARE
v_temp ALIAS FOR $1;
/* Other decalarations */
result_ts INTEGER;
BEGIN
result_ts := 0;
/* Procedure Body */
RETURN result_ts;
EXCEPTION WHEN OTHERS THEN
RAISE NOTICE 'Exception Raised: %', SQLERRM;
END;
END_PROC;
If I am running this SP with one value, such as:
SELECT MY_PROC('TEST_INPUT');
But if I am trying to run it with a column value, such as:
SELECT MY_PROC(TEST_COLUMN) FROM TEST_TABLE;
Its giving me error as:
ERROR: Can't use a stored procedure in this context
I know that in the second scenario I am passing an Array (i guess) but this is not what the Procedure has expected.
Now I am trying to have a procedure that can accept these kind of values but could not succeeded so far, LOOPing and all I have taken care but only problem is the Argument which I don't know how to pass.
Any help would be appreciated, let me know if I need to provide any extra info on this.
Asif
Stored procedures in Netezza, as of v7.2, can only be called in the following ways, as documented here.
CALL sproc_name(...);
EXEC sproc_name(...);
SELECT sproc_name(...);
Note that the SELECT form does not allow a FROM clause.
If you want the stored procedure to act on a particular column from a particular table that changes from invocation to invocation, you could pass the names of those as arguments to the stored procedure and have the entirety of the SQL logic encoded within. You could even pass arbitrary code into the stored procedure to build a query internally.
The way you are trying to call it now is more like calling a user defined function, and that simply won't work with stored procedures here.

Resources