Passing Dynamic parameter using STRING_SPLIT to IN parameter - sql-server

I am using SQL Server 2016 and I have to pass dynamic values to IN parameter.
For this reason, I am using a SPLIT_STRING() function that returns a table of items from a given comma-delimited string. It returns all the records if I passed the value like this (4,6,8) to the #Cylinders parameter.
Where CarCylinders IN (SELECT value FROM STRING_SPLIT(#Cylinders, ','))
But I have requirements where I want to pass Null value to the #Cylinder parameter. So that query returns all the records and ignores the condition.
I don't want to use dynamic SQL solution as the query is very complex and there are more than 20 input parameters.

Just use AND/OR logic:
where #Cylinders is null or CarCylinders IN (SELECT value FROM STRING_SPLIT(#Cylinders, ','))

Related

How to pass a parameter in a SQL query in MS SQL Server from an Excel sheet

Let's say I have this query in SQL Server:
select *
from Table1
where Column1 IN ('01061120192T')
I want to pass the values for Column1 from a column in an Excel sheet.
I'm able to pass a single value to the query as mentioned here. But parenthesis of IN can accept multiple values so I want to pass 1000 values in the parenthesis from a column in an Excel sheet.
Tried to pass multiple values to the parameter as below, but that's not working.
=Sheet1!$G$1:$G$5
You pass variables in SSIS as a ?, and then define your variables in your parameter mapping pane.
select *
from Table1
where Column1 = ?
Then, you need to go to your Parameter mapping pane and click Add. Select your variable's name in the Variable name Column. Then Input as the direction. Leave Paramter Size as -1.

Report Builder 3.0 using multiple values in parameter not working with IN Statement

I am trying to use a multi value parameter in my query statement rather than using a filter, however when using the IN command it is not using my parameters given. I have used the JOIN Function to join the values and tried adding quotations around the value or without, neither seems to have any effect on the query the report does not bring up any results with the given to_date and values provided in the IN statement via the Parameter. Please could someone help
Query
SELECT a.ha_code FROM a WHERE a.from_date <= ? AND a.to_date > ? AND a.ha_code != 'CT' OR a.to_date = ? AND a.ha_code IN (?)
Parameter
=join(Parameters!ReportParameter2.Value,",")
Parameter2
Even though it is not valid T-SQL, in both SSRS and RB, you can have a WHERE predicate with IN (#ParameterName) when working with multi-value parameters.

Multi Value Parameter in SSRS -Values are showing NUll when selected

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,','))

How can I extract multiple values from an XML column, using a function similar to value()?

I'm trying to extract xml metadata from from SQL Server's Report Server Database. My current query is listed below:
SELECT
a.Name as 'ReportName'
,a.ReportXML
,Parameter = x.value('(Parameter/Name)[1]','VARCHAR(250)')
FROM (SELECT C.Name,c.itemID,CONVERT(XML,CONVERT(VARCHAR(MAX),C.Parameter)) AS reportXML
FROM ReportServer.dbo.Catalog C
WHERE C.Content is not null
and c.Type = 2 -- Report only
) a
cross apply reportXML.nodes('/Parameters') r (x)
WHERE 1=1
and name ='ReportName'
My objective is to return all parameters associated with a report. The x.value method will only return 1 value at most. (It currently returns the first parameter of the report because 1 is hard coded in the string literal.) I know there are 5 parameters for the report I'm looking at.
Is there another function with similar syntax that will allow me to return all the values? Or is there a wildcard that I can use in place of the number? I've tried multiple functions on msdn with no luck.
Your query is using nodes('/Parameters'). This will return a resultset with one row for each Parameters element - but there is only one probably.
With Parameter = x.value('(Parameter/Name)[1]','VARCHAR(250)') you are reading the first name of the first Parameter. If there are more Parameter elements nested within Parameters you will read only the first...
Without an example of your actual XML it is not easy to answer but:
Try to add one nesting level by adding this below your cross apply
outer apply r.x.nodes('Parameter') AS p (y)
Then change the column to
,Parameter = y.value('Name[1]','VARCHAR(250)')
This should read the first Name element of each Parameter element
If you need further help, please poste an example of your XML.
Btw: I do not understand this:
CONVERT(XML,CONVERT(VARCHAR(MAX),C.Parameter)
What is the initial type of C.Parameter, that you have to cast it to VARCHAR(MAX) and then to XML? If you are still using the deprecated TEXT, NTEXT or IMAGE you should consider to change this!

SSRS - Format Output of Dataset for Input of Parameter

I have a dataset that is driven by a stored procedure requiring a parameter. The parameter is DATETIME but I am unable to present in that way in the report because DateTime's show as a calendar and I need a dropdown of values.
Alas, I have a dataset that drives the choices for the parameter called BaselineDate. The issue comes down to the way that SSRS formats the DateTime value for the parameter then passes as VARCHAR to the stored procedure. The VARCHAR value is in the wrong format.
SSRS
Expected Value
2016-04-07 13:01:19.173
Update
Per #Marco Bong's suggestions I have converted the dataset that is driving the Parameter options to the proper format. Unfortunately, SSRS I passing the parameter value as null.
Below is what is a debug table I created. I am simply inserting the value of the parameter into this table. As you can see SSRS is passing null to the stored procedure as NULL which in theory should be impossible as the parameter is set to not allow nulls. Any ideas?
select CONVERT(NVARCHAR,getdate(),21) as dtValue
//output will be ===> 2016-04-13 08:32:16.697
Update
If you want to use that selected value in another dataset (which execute your store prod), you may need to do this:
Then you can use this #param1 in your query.
Either where something = #param or set #baselineDate = #param

Resources