How to assign default property value for IN parameters in teradata stored procedure - stored-procedures

Need to develop a stored procedure to insert values into a table. Consider the table has 5 columns, where not all columns values are mandatory for user inputs. The DDL for stored procedure will be like
CREATE OR REPLACE PROCEDURE UPDATE_EMPLOYEE_INFO
(IN EMPLOYEE_NUMBER CHAR(10),
IN EMP_DEPT CHAR(3),
IN PHONE_NUMBER CHAR(4),
IN JOB CHAR(8),
IN ELEVEL SMALLINT)
Begin
--- Insert query here ---
End
This procedure can be execute using commands
CALL UPDATE_EMPLOYEE_INFO(1,'',1234567890,'admin','')
However,columns EMP_DEPT and ELEVEL are not mandatory fields to have values. How can I mention in stored procedure call to take default values like below.
CALL UPDATE_EMPLOYEE_INFO(1,DEFAULT,1234567890,'admin',DEFAULT).
Basically, I want to achieve something kind of this in link using teradata - https://www.ibm.com/support/knowledgecenter/en/ssw_ibm_i_72/sqlp/rbafyprocdefaults.htm

There are no optional parameters. All parameters are mandatory. So you would probably have to pass in optional paramters as null and then write some conditional logic into your stored proc based on whether the parameter is null.
For example:
CALL UPDATE_EMPLOYEE_INFO(1,NULL,1234567890,'admin',NULL)
The Teradata documentation says:
Rules For Specifying Input And Output Parameters
Call arguments consisting of input and output parameters must be
submitted with a CALL statement. No default parameter values can be
defined at the time a procedure is created; the CALL returns an error
if the required call arguments are not specified.
https://info.teradata.com/HTMLPubs/DB_TTU_16_00/index.html#page/SQL_Reference/B035-1146-160K/cum1472240816735.html

Related

Call same stored procedure to run parallel for a different parameter passed to each call

Consider a table containing a list of variables to be passed to a stored procedure's parameter.
The stored procedure (ex : call test(input_id);) has a set of logic which appends the transformed results to a column in a new table and this is required to run in parallel for multiple variable passed from the other table.
Example : Table T having a column contain 5 IDs 35,66,33,88,64
Objective to achieve is write a code to feed these IDs into stored procedure and trigger in parallel:
call test(35);
call test(66);
call test(33);
call test(88);
call test(64);

Return only the first parameter from stored procedure

How can I make it possible to return only the first parameter from a stored procedure with multiple OUT-parameters?
Overview of Parameters
Position
Parameter Type
1
OUT
2
OUT
So instead of create the CALL statement like this
CALL SCHEMA.PROC123(?, ?);
I try to do this
CALL SCHEMA.PROC123(?);
OUT parameters in SQLScript are not optional. At the moment the only way to reach want you want to have, is, to create a wrapper procedure with only the one required OUT parameter.

DB2 calling stored Procedure in conditional statement

I need to query a stored procedure and on the basis of result set of that procedure need to make decisions in conditional statement
For instance I have a stored Procedure "Main_SP"
Now if result of "Main_SP" is 'null' then the result should be 'Tweety', but if th result set is not null then result set should be retrieved,
how to do it?
I tried following and some other but none worked.
SELECT
case Main_SP('MyVariable')
when 'null'
then 'Tweety' end
FROM SYSIBM.SYSDUMMY1 WITH UR
SELECT
case Main_SP('MyVariable')
when null
then 'Tweety' end
FROM SYSIBM.SYSDUMMY1 WITH UR
It is failing the condition, In first command when even it is 'null' it is not printing 'Tweety'.
and while using second, Getting error that 'Null' is not valid in the context.
I don't believe you can use a stored procedure that way.
A stored procedure can return data in two ways
As a result set
in an output or input/output parm
You're not using option 2 and I don't think a result set is ever NULL. There might be no records, but the RS itself isn't NULL. In addition, from what I can see in the manuals, processing a RS from a stored proc inside another stored proc in DB2 requires you to declare an allocate result set locators. But I've never done this.
If your procedure returns a single value or null, you'd be better served by defining it as a function with a return value. Then your code would be simply:
SELECT
COALESCE(Main_Fnc('MyVariable'),'Tweety')
FROM
SYSIBM.SYSDUMMY1 WITH UR
You can't call a stored procedure as part of an SQL fullselect; you would need to have the client that calls the stored procedure handle this logic. If you must do it on the server, implement a new stored procedure that calls Main_SP itself and contains your logic to interpret the result.

Send a List Parameter to DB2 Stored Procedure

I have a db2 stored procedure which has an sql statement using the 'recseq In(1,2,3)' syntax.
I have added a parameter(Character(5) to hold the list but in my call statement I cannot figure out the correct syntax.
I have tried call myprocedure(1, '1,2,3') but this generates a Conversion error
Note: call myprocedure(1, '1') will work but just not the list style.

Dynamic Stored Procedure in Sql Server 2005

Is it possible to write a stored procedure with dynamic parameters in sql server 2005
Technically no, but there's a workaround: temp tables. Create a temp table, then set up your stored procedure to read its configuration from the temp table.
Here's an example of how to call it:
CREATE TABLE #inputParams (ParamName VARCHAR(20), ParamValue VARCHAR(20))
INSERT INTO #inputParams (ParamName, ParamValue) VALUES ('First Name', 'John')
INSERT INTO #inputParams (ParamName, ParamValue) VALUES ('Last Name', 'Doe')
EXEC dbo.USP_MyStoredProcedure
and then inside your stored procedure, you could retrieve whatever parameters you needed by checking for the existence of that temp table, and querying the parameters from it like this:
CREATE PROCEDURE dbo.USP_MyStoredProcedure AS
DECLARE #FirstName VARCHAR(20)
SET #FirstName = SELECT ParamValue FROM #inputParams WHERE ParamName = 'First Name'
GO
That way, your stored procedure's number of parameters never changes, which makes it easier for some dev applications to call it over time, but this is reeeeeally dangerous. From the outside of the stored proc, you never have any idea what version of the stored proc you're calling, and whether or not it's going to do anything with the parameters you're sending in.
What you might think about instead is abstracting away your stored proc inside another stored proc as a wrapper.
No, both the count and parameter types are fixed as part of the stored procedure declaration.
If there's a discrete set of parameters you could pass them into the sproc as XML. Then you can shred the XML into variables inside the body of the sproc e.g.
CREATE PROCEDURE MyDynamicStoredProc
#ParameterXML XML
AS
SET NOCOUNT ON
DECLARE #Parameter1 NVARCHAR(50)
DECLARE #Parameter2 INT
SET #Parameter1 = #ParameterXML.value('/Root/Parameters/StringParameter/#Value','nvarchar(50)')
SET #Parameter2 = #ParameterXML.value('/Root/Parameters/IntegerParameter/#Value','int')
...
NB: My XQuery-fu is weak, don't rely on this example!

Resources