SSRS Passing Multiple value parameter to a stored procedure - stored-procedures

Am trying to pass a multi value parameter to a Stored Procedure from the SSRS report.
After following the query
SSRS multi-value parameter using a stored procedure
I've made the stored procedure accept the CSV list by modifying the query as
value IN (#CampaignID)
I've added the Join(Parameters!TerritoryMulti.Value, ",") at dataset parameters as this would pass the values as CSV.
Still, I see that, only single (first value selected) value is being passed to Stored Procedure from SSRS despite the number of values selected.
This is how, I checked the parameter being passed from SSRS.
Added the Statement: Insert into RF.Dashboard.ErrorMessages values (1, #CampaignID) in the stored procedure.
After running the report in SSRS. I see the following entries in the table.
msgID Message
1 1
1 1
1 1
Please help me understand what am I missing.

Related

How to write a stored procedure in DB2 to perform UPDATE / DELETE operation by passing Dynamic values?

I want to write a stored procedure in DB2 database for UPDATE and DELETE operations.
I am able to do that by directly providing values in procedure, but I want to do it by passing dynamic values.
My Table Structure is -
create table emp2 (int_1 int, char_1 char(10))
Below is my stored procedure for UPDATE operation which I am able to run but, its not behaving as per expectations. Changes are not reflecting in DB even after passing correct parameters while calling stored procedure.
CREATE OR REPLACE PROCEDURE "DB2INST1"."UPDATE_1" (IN int_1 int, IN
char_1 char(10)) SPECIFIC UPDATE_1
LANGUAGE SQL
DYNAMIC RESULT SETS 1
BEGIN
update emp2 set char_1=char_1 where int_1=int_1;
END;
This is the my stored procedure for DELETE operation which I am able to run successfully, but it's deleting all rows from the database table instead of deleting a single row:
CREATE OR REPLACE PROCEDURE "DB2INST1"."DELETE_1" (IN int_1 int)
SPECIFIC DELETE_1
LANGUAGE SQL
DYNAMIC RESULT SETS 1
BEGIN
delete from emp2 where int_1=int_1;
END;
Please provide me syntax for creating stored procedure for UPDATE and DELETE operations by passing dynamic values in a DB2 database.
The problem is that you don't qualify your columns and parameters having the same names.
According to the References to SQL parameters, SQL variables, and global variables:
Names that are the same should be explicitly qualified. Qualifying a
name clearly indicates whether the name refers to a column, SQL
variable, SQL parameter, row variable field, or global variable. If
the name is not qualified, or qualified but still ambiguous, the
following rules describe whether the name refers to a column, an SQL
variable, an SQL parameter, or a global variable:
If the tables and views specified in an SQL routine body exist at the time the routine is created, the name is first checked as a column
name. If not found as a column, it is then checked as an SQL variable
in the compound statement, then checked as an SQL parameter, and then,
finally, checked as a global variable.
That is, the following statements changing all the table rows obviously are equivalent:
update emp2 set char_1 = char_1 where int_1 = int_1;
--and
update emp2 e set e.char_1 = e.char_1 where e.int_1 = e.int_1;
What you need is to rewrite this statement to the following, if you want to use the same column and parameter names (the routine name UPDATE_1 is used here for the parameter qualification).
update emp2 set char_1 = UPDATE_1.char_1 where int_1 = UPDATE_1.int_1
fiddle

DB2 Stored Procedure Not able to assign data to a variable

I have a simple stored procedure to calculate the sum of salaries of employees, sum of their squares and number of rows.
This is the stored procedure I have written:
I get an error in fetching the number of rows from the database and assigning it to a variable. What do I do? Using DB2 11.5
It helps to specify the exact error code when asking questions (don't write get an error, do write instead 'get error SQL0104N ...`.
Your mistake is that you have not followed the documented order for SQL statements in compound SQL blocks.
The SELECT statement can only appear after any cursor definitions, local procedures , and handlers if you have any.
So move the statement SELECT COUNT(*) INTO TOTAL_ROWS FROM EMPLOYEE; so that it appears after the DECLARE CURSOR1 ... line, the try to recompile.

Return 2 resultset from cursor based on one query (nested cursor)

I'm trying to obtain 2 different resultset from stored procedure, based on a single query. What I'm trying to do is that:
1.) return query result into OUT cursor;
2.) from this cursor results, get all longest values in each column and return that as second OUT
resultset.
I'm trying to avoid doing same thing twice with this - get data and after that get longest column values of that same data. I'm not sure If this is even possible, but If It is, can somebody show me HOW ?
This is an example of what I want to do (just for illustration):
CREATE OR REPLACE PROCEDURE MySchema.Test(RESULT OUT SYS_REFCURSOR,MAX_RESULT OUT SYS_REFCURSOR)
AS
BEGIN
OPEN RESULT FOR SELECT Name,Surname FROM MyTable;
OPEN MAX_RESULT FOR SELECT Max(length(Name)),Max(length(Surname)) FROM RESULT; --error here
END Test;
This example compiles with "ORA-00942: table or view does not exist".
I know It's a silly example, but I've been investigating and testing all sorts of things (implicit cursors, fetching cursors, nested cursors, etc.) and found nothing that would help me, specially when working with stored procedure returning multiple resultsets.
My overall goal with this is to shorten data export time for Excel. Currently I have to run same query twice - once for calculating data size to autofit Excel columns, and then for writing data into Excel.
I believe that manipulating first resultset in order to get second one would be much faster - with less DB cycles made.
I'm using Oracle 11g, Any help much appreciated.
Each row of data from a cursor can be read exactly once; once the next row (or set of rows) is read from the cursor then the previous row (or set of rows) cannot be returned to and the cursor cannot be re-used. So what you are asking is impossible as if you read the cursor to find the maximum values (ignoring that you can't use a cursor as a source in a SELECT statement but, instead, you could read it using a PL/SQL loop) then the cursor's rows would have been "used up" and the cursor closed so it could not be read from when it is returned from the procedure.
You would need to use two separate queries:
CREATE PROCEDURE MySchema.Test(
RESULT OUT SYS_REFCURSOR,
MAX_RESULT OUT SYS_REFCURSOR
)
AS
BEGIN
OPEN RESULT FOR
SELECT Name,
Surname
FROM MyTable;
OPEN MAX_RESULT FOR
SELECT MAX(LENGTH(Name)) AS max_name_length,
MAX(LENGTH(Surname)) AS max_surname_length
FROM MyTable;
END Test;
/
Just for theoretical purposes, it is possible to only read from the table once if you bulk collect the data into a collection then select from a table-collection expression (however, it is going to be more complicated to code/maintain and is going to require that the rows from the table are stored in memory [which your DBA might not appreciate if the table is large] and may not be more performant than compared to just querying the table twice as you'll end up with three SELECT statements instead of two).
Something like:
CREATE TYPE test_obj IS OBJECT(
name VARCHAR2(50),
surname VARCHAR2(50)
);
CREATE TYPE test_obj_table IS TABLE OF test_obj;
CREATE PROCEDURE MySchema.Test(
RESULT OUT SYS_REFCURSOR,
MAX_RESULT OUT SYS_REFCURSOR
)
AS
t_names test_obj_table;
BEGIN
SELECT Name,
Surname
BULK COLLECT INTO t_names
FROM MyTable;
OPEN RESULT FOR
SELECT * FROM TABLE( t_names );
OPEN MAX_RESULT FOR
SELECT MAX(LENGTH(Name)) AS max_name_length,
MAX(LENGTH(Surname)) AS max_surname_length
FROM TABLE( t_names );
END Test;
/

Stored procedure for updating values dynamically in db2 table

I am trying to update the values of a table dynamically, using stored procedure. My stored procedure is as follows,
CREATE OR REPLACE PROCEDURE Update
(
IN ID1 BIGINT,
IN SOURCE1 VARCHAR(100),
IN NAME1 VARCHAR(100)
)
DYNAMIC RESULT SETS 2
LANGUAGE SQL
BEGIN
UPDATE MessageTable
SET SOURCE=SOURCE1,
NAME=NAME1
WHERE ID=ID1;
END
When I try to pass the value for ID1 and SOURCE1 alone, the values are not getting updated. When I pass all the three values, they are getting updated properly. My requirement is even if I pass two values it should get updated. I tried giving DEFAULT NULL for the arguments. Since the fields are declared NOT NULL, it was not working. Could someone help to overcome this. The stored procedure should work even if I pass single value. Thanks in advance.
You could use COALESCE:
UPDATE MessageTable
SET SOURCE = COALESCE(SOURCE1, SOURCE),
NAME = COALESCE(NAME1, NAME)
WHERE ID = ID1;
When NAME1 is NULL then the original NAME will be preserved.
You could check with IF or another statement whether all arguments have non-NULL values and then switch to either an UPDATE statement that sets one or two column values.

Informatica - Getting multiple records using Teradata stored procedure

I am trying to get a resultset from Teradata stored Procedure to my Mapping.
The stored procedure is to hold multiple select statements and the final output is to be sent to informatica. Below is a sample of how the select statement looks like
sel 'INH1' AS QC_CODE,count(*) from Table 1
UNION
sel 'INH2' AS QC_CODE,count(*) from Table 2
UNION
sel 'INH3' AS QC_CODE,count(*) from table 3
I need a stored procedure that can send the output of the above query (2 columns, 3 records) to Informatica, where I can call the stored procedure in my source qualifier or through SP transformation
Any help??
You can call the Stored Procedure in Source Qualifier transformation using SQL Query property. Just make sure the ports, their order and datatypes reflect the columns returned by the Stored Procedure.

Resources