How to pass multiple values in place of single value to a stored procedure - sql

I have a stored procedure, and it is executed perfectly, when i am using:
exec spSampleAmount 12212,0
Now i need to pass like "12212,12213,12214,12215" and these values coming from another query. How to pass this query result to a 2 argument stored proc.
exec spSampleAmount param1,param2;
param1 have set of values.

Store the output of first query in parameter. First declare the parameter and then assign the value of first query to that parameter then call exec command to execure the stored procedure.
DECLARE #V_Param1 VARCHAR(1000);
SELECT #V_Param1 = Rest of query
EXEC spSampleAmount #V_Param1, 0

if it is called by SSRS it's possible, i don't know exactly how it works, but it is.
You have 2 ways to call a stored procedure from ssrs, sql query and write execute. Or select execute stored procedure.
In that way you have the possibility to pass a selection from a multiple item selection combobox to the stored procedure and in your stored procedure do a : where myparam in (#paramreceived).
It should be also possible in .Net.
I tried to do that in tsql and i never succeeded, so i'm interrested too if there is a solution to that problem

Related

how many result test we can give a for execute sql task

I have an ssis package which have a exectue sql task it. I tried to modify the task by adding a stored procedure as sql statement. The stored procedure returns two result sets.
stored procedure:
create proc test
as
begin
select EmpID from Employee
select PersonID from person
end
I mapped the result sets to result set tab in the task with parmeter name 0 & 1 and assigned them to two variables.
When I run the package I got below error
[Execute SQL Task] Error: There is an invalid number of result
bindings returned for the ResultSetType: "ResultSetType_Rowset".
However if i delete one result set and execute package , it runs successfully. Is it possible to add more than one result set to execute sql task? What I am missing here?
Thank you
The Execute SQL task cannot access multiple result sets returned by a stored procedure. It can only access the first result set.
The Result Set tab you are showing in your screen shot is not for assigning different result sets to object-type variables. It is for assigning different columns to scalar variables.
If you changed your stored proc to return a single result set with two columns, then the way you have it in your screenshot would work.

Procedure or function 'sp_***' expects parameter '#', which was not supplied

I am trying to execute a stored procedure but getting following error:
Procedure or function 'SP_DELETE_DESIGN_PARAMETERS' expects parameter '#DESIGN_ID', which was not supplied.
Below is my stored procedure.
I am trying to execute it in SQL need result in table.
CREATE PROCEDURE SP_DELETE_DESIGN_PARAMETERS
#DESIGN_ID INT
AS
BEGIN
Delete From Design_Parameters where Design_ID = #DESIGN_ID
END
exec SP_DELETE_DESIGN_PARAMETERS
I know I am missing some very easy points. I would appreciate any help into this.
I am using SQL Server 2008 R2 and VS2010 with VB.Net
Thanks :)
exec SP_DELETE_DESIGN_PARAMETERS #DESIGN_ID = 5
exec SP_DELETE_DESIGN_PARAMETERS 5
Read up on how to pass parameters to a stored procedure. You are asking for a parameter but not supplying it.
When you exec your stored procedure you need to pass it the #DESIGN_ID parameter it expects.
EXEC SP_DELETE_DESIGN_PARAMETERS 123
where 123 is the ID you want to pass to the stored procedure

SQL Server:exec('CALL' DB2; advice on managing result set columns

Techies--
If I were issuing an openquery select, my problems would be solved--but as far as i know openquery doesn't allow the calling lingo/w. parameter(s) to remote db2 servers! :)
Here's what works:
declare #z varchar(max);
set #z = '999990480,888887530';
exec ('CALL S1CATALOG.HCMDEV.EMP_ALL_STARS(?)',#z) AT DB2I;
This stored proc (EMP_ALL_STARS) accepts the concatenated string as a clob, then returns roughly 35 columns. Not all the applcations with an interest in utilizing this sproc need all 35 columns. Any advice on how to manage the result set?
Do you have control over the remote DB2 procedure? If so, you could define multiple cursors inside the proc, each with a different set of columns in the result set. At runtime, when it's time for the proc to open a cursor that performs the query and returns the result set to the caller, a bit of logic can evaluate the relevant input parameters to determine which which cursor to open.

SQL Server: Load stored procedure results into table

I have a stored Procedure that returns multiple resultsets(to be specific three resultsets). I just need the first resultset. I am calling the original procedure from a different procedure where i will store the returned resultset into a #table and use it from my further processing.
Also, i can not modify the original stored procedure to achieve this.
Please help!
It's not possible to retrieve the second or further result set from a stored procedure inside SQL.
Two workarounds:
A scheduled job (like a C# program) that periodically calls the stored procedure and stores the result in tables that other procedures can use.
A SQL CLR stored procedure that does the same. The advantage of a SQL CLR procedure is that you can call it from normal SQL stored procedures, so you don't have to wait for the scheduled task.
I haven't tested this, but a work around would be to use OpenQuery and call your SP using it because "Although the query may return multiple result sets, OPENQUERY returns only the first one". OPENROWSET will also do the same...

How to Parse and Append text to a stored procedure in SQL Server 2005 via a parameter

Does anyone know of a way to append text to a stored procedure from within another stored procedure? I would like to do something like the following in SQL Server 2005:
Declare str as Nvarchar(Max) = ''
set #spStr = dbo.spTest + 'Where testCol1 = ''Test'''
exec(#spStr)
I understand this may open some discussion about SQL injection attacks. I'm simply looking to see if syntax exsists to extend a stored procedure by passing it a where clause dynamically in the above manner.
There is no syntax like this available in Sql Server any version. You've got a couple of options:
You could obviously modify the procedure to include a parameter that the procedure code itself would handle as a filter in the final statement(s) that returned the result set from the procedure call. Though I'd advise against it, you could certainly have a parameter that was just a varchar/nvarchar data type which included the actual 'where' clause you want to add and have the procedure code append it to these final select statement(s) as well
Use the insert/exec syntax to populate a temp table with the results of the stored procedure execution and then simply run a filtered select against that temp table.
There are some options.
You can alter the actual SP using the metadata in INFORMATION_SCHEMA.ROUTINES (not really what I think you are wanting to be doing)
You can parameterize the SP - this should not be vulnerable to injection if the SP uses the variable directly and not to dynamically make SQL.
You might consider using a view or an inline or multi-step table-valued function instead, which can be used like a parameterized view (inline being more efficient) - SELECT * FROM udf_Test WHERE TestCol1 = 'Test'.
You can take the results of the SP and put them in a temporary table or table variable and query against that.