I have a simple stored procedure, which executes in base of one input parameter. But I have to execute it about 100 times because of different parameters (parameter is int)Is there any other way except of inputing parameter values one by one? I mean something like #parameter in (value 1, value 2, .....value n).Thanks a lot.
This is the procedure:
ALTER PROCEDURE procname
#parameter AS BIGINT
AS
DELETE FROM tbl1 WHERE id = #parameter
DELETE FROM tbl2 WHERE tbl2_field=#parameter
DELETE FROM tbl3 WHERE tbl3_field=#parameter
DELETE FROM tbl4 WHERE tbl4_field=#parameter
1) make the parameter optional
2) when parameter is provided, use it to filter the results
3) when parameter is null, return all results, grouped on the field represented by the parameter.
Such as :
Select EmpNum, EmpNameFull, DeptName, HireDate
From Employee
Where DeptName = paramDeptName
or paramDeptName IS NULL
Order
By DeptName
, EmpNameFull
You could pass an array to the stored procedure. This is already answered with different database versions and multiple solutions on Stack Overflow here:
How to pass an array into a SQL Server stored procedure
and here
Passing an array of parameters to a stored procedure
Another solution could be to send the parameter comma separated and split it in T-SQL and loop through the values, How to split a comma separated string and loop its values in SQL Server (string parameter would be needed).
Related
l had a parameter in my stored procedure that was varchar(2000), my problem now is the characters being sent on the parameter have grown, the parameter now needs to handle 30 000 characters which are comma-separated. The biggest variable type that am now using (TEXT) is giving me issues in my where clause as below
Now using TEXT data type
CREATE PROCEDURE
(#ArrayList TEXT ='' -- '93238128,93238131,93238130,93238133,93238132 ......
)
then in my where clause
WHERE c.CLAIM_ID IN (SELECT Element FROM dbo.Split(#ArrayList ,','))
AND #ArrayList <>'') OR #ArrayList =''
Getting this error :
The data types text and varchar are incompatible in the not equal to operator.
Here's what l ended up using Tables! And it works great. Sending a table with one column to a stored procedure.
How to stop showing messages from procedure?
Currently, the problem is I have created a stored procedure Procedure A. It executes another procedure B from its code, and the B procedure executes yet other procedures C,D,E from its code.
The problem is when the A procedure executes, it shows 4 result sets like 0,1 or another values. But I want to show only the result from procedure A. How can I achieve this? I can't change the other procedures B,C,D,E because they also perform their individual tasks.
If you need clarification please ask.
You can try the followings:
if any of the sub procedures is returning only one row set and it is static (same columns with the same type are always returned), you can materialized the result set in temporary tables (or table variables)
For example, let's say that procedure X returns a table with two int columns. You
materialized the result like this:
CREATE TABLE #X
(
A INT
,B INT
);
INSERT INTO #X
EXEC usp_X;
Add additional parameter to the sub procedures or use any of the existing ones to not return the row set(s) if certain option is passed.
For example, add #MiscSettings parameter to your existing procedure X:
ALTER PROCEDURE usp_X AS
(
#Param01 INT
,#Parame02 VARCHAR(12)
,...
,#MiscSettings NVARCHAR(MAX) = NULL
)
The parameter is not mandatory, so you are not going to break any existing reference. Then in the procedure you can check if [DoNotReturnResultSet] string is passed in the #MiscSettings to not return the result sets. Existing references will continue to work because by default the row sets are returned.
IF Option Is Not Passed
BEGIN;
SELECT ...
END;
In both ways you can suffer if someone change the code of the sub routines. For example, if a type of returned column is changed, or someone add additional row set without checking if your special option is passed.
Note, in the second technique, if you do not like to add additional parameter, you can use some of the existing strings (for example). Just check if the string contains your option and then replace it.
I tried using
Where Location IN(#Location)
in my storedprocedure
In SSRS
i used =join(Parameters!Location.Value,",")
When i select all values the result is null
The value is in string format
Eg: 'XXXX,YY','yyy,mm'
You need to change your Sql stored procedure code so that it could recognize the comma separated values being passed from SSRS.
One way would be using table-value function that can split a comma-delimited string back out into a mini table - link
Once done the code in your stored procedure should be like this -
WHERE Location IN (SELECT Param FROM dbo.fn_MVParam(#Location,','))
I have a stored procedure that has structure like this:
ALTER PROCEDURE dbo.GetUserFullName
(#NoteStoreType INT = NULL)
AS
SELECT DISTINCT dbo.tblUsers.LastName + ', ' + dbo.tblUsers.FirstName as UnderwriterName
FROM tblUsers
WHERE tblUsers.Type = COALESCE(#NoteStoreType, tblNoteStore.Type )
In my SSRS report I want to say if #NoteStoreType= NULL then select ALL NoteStoreTypes, if not then use #NoteStoreType that user will select.
If I allow multiple values then it gives me an error converting datatype int to string.
Can I do that in SSRS without changing anything in my stored procedure?
The multiple parameters that are selected will get passed to stored proc as a string. You will need to change stored proc to split this string. Also if you want to pass null you will need to check the allow null value.
I am having a sql stored procedure that takes 2 inputs and returns a resultset based on the given input.
I am using the input1 in sql IN clause like,
WHERE myCol IN(#input1)
when executing the store procedure from kettle table input step,
If i give a single value for the input1, it works fine.
EXEC sp_procedureName #input1='07423', #input2='2014-09-02'
If i give multiple values like below, it results empty resultset.
EXEC sp_procedureName #input1='07423,07022,07033', #input2='2014-09-02'
How can i pass multiple values as a parameter to my procedure.
the transformation will execute the procedure and insert the result set into another table using table output step.
What you need to do inside your stored procedure is split the comma delimited list of numbers into a list of single values in rows. Once you have this you can use WHERE myCol IN(SELECT v FROM #r), or use an INNER JOIN instead of IN. One point you might have to watch for is that you have leading zeros - if you want to keep those you will need to use strings instead of integers.
There are lots of articles around on the subject of splitting delimited values into rows, you could start here.