I have an Execute SQL Task which runs a simple query
datatype filename varchar(255) filenumber int id uniqueidentifier
select i.filename , i.filenumber,s.id , 'Files loaded over days' as ErrorMessage from table I inner join tableB s on i.filenumber = s.filenumber where datediff(day,i.filedate,i.adddate)>=5
I have added the resultset as user:ObjServiceID
Now I have used a Foreach ADO Enumerator and in Ado Object Source variable called user::objServiceID
Variable mappings User:ID 0 User::Filenumber 1 User::Filename 2 User::Errormessage 3
and inside the ForEach Loop container I have another Execute Sql task that calls a Stored procedure Parameter Mappings User:: ID GUID 0 -1 User::Filename Varchar 1 -1 User :: Filenumber Numeric 2 -1 User::ErrorMessage Varchar 3 -1
The stored procedure Exec XYZ has variables #filename varchar (255), #filenumber int, #id uniqueidentifier, #Errormessage varchar(2000)
I am constantly getting an error that says "[Execute SQL Task] Error: Executing the query "exec XYZ" failed with the following error: "Procedure or function "XYZ" expects parameter '#ErrorMessage', which was not supplied.". Possible failure reasons: Problems with the query, "ResultSet" property not set correctly, parameters not set correctly, or connection not established correctly."
Even if I try to remove the Errormessage from the parameter and variable mappings it still throws this error. Could someone help with this. Thanks in advance
The m in Error message needs to be upper case in your mapping. It says ErrorMessage was not supplied because you have it as Errormessage.
I have the same code below:
CREATE PROCEDURE TEST1
(#id INT, #name NVARCHAR(30) OUTPUT)
AS
SELECT #name = NAME
FROM TEACHER
WHERE ID = #id;
AND
CREATE PROCEDURE TEST2(#id INT)
AS
DECLARE #name NVARCHAR(30);
SELECT #name = NAME
FROM TEACHER
WHERE ID = #id;
RETURN #name;
The top code is OK, but the second I get an error :
Msg 245, Level 16, State 1, Procedure TEST2, Line 174
Conversion failed when converting the nvarchar value 'Nguyễn Thanh Tùng' to data type int.
Can you help me explain and fix it ??
You cannot return a varchar from a SQL Server stored procedure using RETURN - you can only return integer values.
See the MSDN documentation on "Returning Data Using a Return Code" which clearly states:
A procedure can return an integer value called a return code to indicate the execution status of a procedure.
Typically, such a value is used to either indicate the number of rows affected by your stored procedure, or to indicate an error code.
If you need to return a string, you have to use either an OUTPUT parameter, or return a result set (using a SELECT statement).
After doing some research here and online I am at a loss as to whether this is possible. What I want to do is call a stored procedure that has several parameters one of which is a table-value parameter.
This is my stored procedure snippet:
ALTER PROCEDURE [dbo].[procName]
#Action nvarchar(10) = 'view'
,#var1 int = 0
,#var2 int = 0
,#var3 myType ReadOnly
I now have another procedure (proc2) that has the following lines:
insert into #varX
select top 5
field1, field2
from
sourceTable
print 'Processing from table values...'
exec dbo.procName 'refresh', -1, 0, #varX
Note that varX and var3 are of the same type MyType
When I execute proc2 I get the error that I am specifying too many arguments for dbo.procName
I am at the point in thinking it is not possible to specify multiple parameters inclusive of a table-value parameter to a stored procedure. I am now tending towards the thought of changing my procName definition to only have one parameter (as all of the examples online seem to have) and have my table-value paramter act as an array of parameter values inclusive of the information I had in my previous select statement (in proc2). If however it is possible to do this call, please illustrate how this is done.
Thanks
This compiles and runs for me:
create type TT as table (ID int not null);
go
create procedure P1
#Val1 int,
#Val2 TT readonly,
#Val3 int
as
select #Val1 as Val1,ID,#Val3
from #Val2;
go
create procedure P2
as
declare #T TT;
insert into #T(ID) values (1),(2)
exec P1 10,#T,13
go
exec P2
Result:
Val1 ID
----------- ----------- -----------
10 1 13
10 2 13
So, I don't know what your issue is, but it's not being able to mix table and non-table parameters.
Does anybody know if this is allowed?
IF CALL GET_RIGHT_NODE(edge) = 15
THEN
SELECT "IT WORKS";
I'm getting an error on this syntax, is it possible any other way?
The return values from stored procedures should be captured in OUT paramters (whereas those from user defined functions can be captured as #returnValue = function()).
So, your GET_RIGHT_NODE should take an OUT parameter and set it to the return value.
CREATE PROCEDURE GET_RIGHT_NODE
(
#edge INT,
#returnValue INT OUTPUT
)
AS
-- Definition of the proc.
then you would call the procedure as follows:
DECLARE #returnValue INT
CALL GET_RIGHT_NODE(#edge, #returnValue)
IF (#returnValue = 15)
THEN
SELECT 'IT WORKS'
I am working on a SSRS report that uses a stored procedure containing a few parameters. I am having problems with two of the parameters because I want to have the option of selecting more than one item.
Here's a condensed version of what I have:
CREATE PROCEDURE [dbo].[uspMyStoredProcedure]
(#ReportProductSalesGroupID AS VARCHAR(MAX)
,#ReportProductFamilyID AS VARCHAR(MAX)
,#ReportStartDate AS DATETIME
,#ReportEndDate AS DATETIME)
--THE REST OF MY QUERY HERE WHICH PULLS ALL OF THE NEEDED COLUMNS
WHERE DateInvoicedID BETWEEN #ReportStartDate AND #ReportEndDate
AND ProductSalesGroupID IN (#ReportProductSalesGroupID)
AND ProductFamilyID IN (#ReportProductFamilyID)
When I try to just run the stored procedure I only return values if I enter only 1 value for #ReportProductSalesGroupID and 1 value #ReportProductFamilyID. If I try to enter two SalesGroupID and/or 2 ProductFamilyID it doesn't error, but I return nothing.
-- Returns data
EXEC uspMyStoredProcedure 'G23', 'NOF', '7/1/2009', '7/31/2009'
-- Doesn't return data
EXEC uspMyStoredProcedure 'G23,G22', 'NOF,ALT', '7/1/2009', '7/31/2009'
In SSRS I get an error that says:
Incorrect syntax near ','
It appears that the , separator is being included in the string instead of a delimiter
You need three things:
In the SSRS dataset properties, pass the multi-value param to the stored procedure as a comma-delimited string
=Join(Parameters!TerritoryMulti.Value, ",")
In Sql Server, you need a table-value function that can split a comma-delimited string back out into a mini table (eg see here). edit: Since SQL Server 2016 you can use the built-in function STRING_SPLIT for this
In the stored procedure, have a where clause something like this:
WHERE sometable.TerritoryID in (select Item from dbo.ufnSplit(#TerritoryMulti,','))
... where ufnSplit is your splitting function from step 2.
(Full steps and code in my blog post 'SSRS multi-value parameters with less fail'):
Let us assume that you have a multi value list #param1
Create another Internal Parameter on your SSRS report called #param2 and set the default value to:
=Join(Parameters!param1.value, 'XXX')
XXX can be any delimiter that you want, EXCEPT a comma (see below)
Then, you can pass #param2 to your query or stored procedure.
If you try to do it any other way, it will cause any string function that uses commas to separate arguments, to fail. (e.g. CHARINDEX, REPLACE).
For example Replace(#param2, ',', 'replacement') will not work. You will end up with errors like "Replace function requires 3 arguments".
Finally I was able to get a simple solution for this problem. Below I have provided all (3) steps that I followed.
I hope you guys will like it :)
Step 1 - I have created a Global Temp Table with one column.
CREATE GLOBAL TEMPORARY TABLE TEMP_PARAM_TABLE(
COL_NAME VARCHAR2(255 BYTE)
) ON COMMIT PRESERVE ROWS NOCACHE;
Step 2 - In the split Procedure, I didn't use any array or datatable, I have directly loaded the split values into my global temp table.
CREATE OR REPLACE PROCEDURE split_param(p_string IN VARCHAR2 ,p_separator IN VARCHAR2
)
IS
v_string VARCHAR2(4000);
v_initial_pos NUMBER(9) := 1;
v_position NUMBER(9) := 1;
BEGIN
v_string := p_string || p_separator;
delete from temp_param_policy;
LOOP
v_position :=
INSTR(v_string, p_separator, v_initial_pos, 1);
EXIT WHEN(NVL(v_position, 0) = 0);
INSERT INTO temp_param_table
VALUES (SUBSTR(v_string, v_initial_pos
, v_position - v_initial_pos));
v_initial_pos := v_position + 1;
END LOOP;
commit;
END split_param;
/
Step 3 - In the SSRS dataset parameters, I have used
=Join(Parameters!A_COUNTRY.Value, ",")
Step 4: In the start of your stored procedure executes the Procedure
Exec split_param(A_Country, ‘,’);
Step 5: In your stored procedure sql use the condition like below.
Where country_name in (select * from TEMP_PARAM_TABLE)
When SSRS passes the parameter it is in the form: Param1,Param2,Param3.
In the procedure, you just need to put identifiers around each parameter. And also identifiers around the value that is returned by the dataset. In my case, I used semicolons.
CREATE OR REPLACE PROCEDURE user.parameter_name (
i_multivalue_parameter
)
AS
l_multivalue_parameter varchar2(25555) := ';' || replace(i_multivalue_parameter,',',';') || ';';
BEGIN
select something
from dual
where (
instr(l_multivalue_parameter, ';' || database_value_that_is_singular || ';') > 0
)
END;
i_multivalue_parameter is passed in via SSRS.
l_multivalue_parameter reads the parameter passed in via SSRS and puts identifiers around each value.
database_value_that_is_singular is the value returned for each record.
So if 'Type1,Type2,Type3'is passed in via SSRS:
i_multivalue_parameter is: Type1,Type2,Type3
l_multivalue_parameter is: ;Type1;Type2;Type3;
database_value_that_is_singular is: ;Type1; or ;Type2; or ;Type3;
Instr will return a value over 0 if the parameter matches.
This works even if each parameters are similar. EG: "Type A" and "Type AA". That is "Type A" will not match "Type AA".
I found a simple way for my solution. Define the parameter value in the report as an expression like this
="'" + Join(Parameters!parm.Value,"','") + "'"
(in case you can't read it the first and last literals are double quote, single quote, double quote. The join literal is double quote, single quote, comma, single quote, double quote)
Then in the stored procedure you can use dynamic sql to create your statement. I did this to create a temp table of values to join to in a later query, like this:
CREATE #nametable (name nvarchar(64))
SET #sql = N'SELECT Name from realtable where name in (' + #namelist + ')'
INSERT INTO #nametable exec sp_executesql #sql
#namelist would be the name of the stored procedure parameter.