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.
Related
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.
How do I save procedure out parameter value to variable for later reuse in sqlworkbench/j script?
It is possible to run a stored procedure with out parameters using WbCall
However, the result of that can not be used to be stored into a Workbench variable.
That is only possible with "regular" queries (SELECT) that return a result set.
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
I am currently testing with:
A SQLConnection which is pointed towards an IB database.
A SQLDataset that has a SQLConnection field set to the one above.
A DatasetProvider that has the SQLDataset in (2) as its Dataset field value.
A ClientDataset, with the ProviderName field pointing to the provider in (3).
I use the following method (borrowed from Alister Christie) to get the data...
function TForm1.GetCurrEmployee(const IEmployeeID: integer): OleVariant;
const
SQLSELEMP = 'SELECT E.* FROM EMPLOYEE E WHERE E.EMPLOYEEID = %s';
begin
MainDM.SQLDataset1.CommandText := Format(SQLSELEMP, [Edit1.Text]);
Result := MainDM.DataSetProvider1.Data;
end;
Which populates the DBGrid with just one record. However, when I manually edit the record, click on Post, then try to commit the changes, using
MainDM.ClientDataset1.ApplyUpdates(0); // <<<<<<
It bombs, with the message "SQLDataset1: Cannot modify a read-only dataset."
I have checked the ReadOnly property of the Provider, and of the ClientDataset, and the SQL has no joins.
What could be causing the error?
It appears that your ClientDataSet.Data property is being populated from the Data property of the DataSetProvider. With the setup you described, you should be able to simply call ClientDataSet.Open, which will get the data from the DataSetProvider.
BTW, the default behavior of the DataSetProvider when you call the ClientDataSet.ApplyUpdates method is to send a SQL query to the connection object, and not the DataSet from which the data was obtained (assuming a homogeneous query). Make sure that your DataSetProvider.ResolveToDataSet property is not set to true.
Finally, on an unrelated note, your code above appears to be open to a SQL injection attack (though I have not tested this). It is safer to use a parameter to define the WHERE clause. If someone enters the following into Edit1 you might be in trouble (assuming the InterBase uses the drop table syntax): 1;drop table employee;
Check the LiveMode property of the TIBDataSet.
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!