Assigning multi value parameter (values) to a local parameter in sql query - reporting-services

I have a situation where I need to store the multi value parameter to a local parameter in sql query.
In the SSRS report builder I have created two multi value parameters #Customer and #LogisticsGroup
And in my SQL query , I have to assign those values to local parameters something like this
DECLARE #Acct NVARCHAR(100) , #LgstGroup NVARCHAR(MAX)
SELECT #Acct = (#Customer)
,#LgstGroup = (#LogisticsGroup)
But with this kind of approach I'm able to select only one value , if I select two values then the query is failing.
I tried this , but it seems like incorrect syntax.
DECLARE #Acct NVARCHAR(100) , #LgstGroup NVARCHAR(MAX)
SELECT #Acct IN (#Customer)
,#LgstGroup IN (#LogisticsGroup)
Please help to resolve this issue. Thanks much

You can store delimited values in a parameter. So for example, #Customer might have a string of ID's in it like 1,2,3. The report can concatenate the multiple values into this format using the Join function.
Now, in the SQL, one option is to use the LIKE operator to search the string. You could write something like:
#Customer like '%,' + Column_Name + ',%'
However, this approach is inefficient and you have to be careful of partial value matches.
A better approach is to create a user-defined table-valued function that can split the values and treat them like a table. There are plenty of examples out there, it's a pretty simple function. Then in practice it would look like this:
WHERE Column_Name in (select * from Split(#Customer))
OR
INNER JOIN Split(#Customer) ON...

Related

Is it possible to add more than one value against a Label for a Parameter in Report Builder?

I'm using Report Builder and want to add more than one value against a Label (Available Values > Specify Values):
Example:
Label = "labelone" Value = Value1, Value2...
When I run the report, I get a message saying Parameter1 is missing.
I have tried following against value: Value1, Value2 and 'Value1', 'Value2'
My query has following in the where clause: WHERE (FIELD in (#Parameter1))
Basically, I want to specify multiple values against one label under the parameter.
Thank you
You cannot do this directly as what will get passed to the query would be
WHERE Field IN ('Value1, Value2')
or
WHERE Field IN ('''Value1'', ''Value2''')
The way to do this would be split the label using a table valued function, you could use string_split if you have a fairly recent version of SQL Server (I think this was introduced in SQL2016). If you have an older version there are loads of sample of string splitting functions around tha you could use.
Once you have you function you can join to the results of that, the query would be something like
DECLARE #pValues TABLE (myValues varchar(10))
INSERT INTO #pValues
SELECT * FROM string_split(#myReportParameter, ',')
SELECT *
FROM myTable a
JOIN #pValues p on a.Field = p.myValues

Possible to use IF in a query?

I'm using Grafana to plot data from a MySQL datasource. Is it possible to, in a panel's query editor, use an IF ... THEN ... type statement. I would like to create a variable that I could put in the IF. I want the variable to be a condition, not necessarily to be used directly in the query.
For example:
//IN THE DATA SOURCE:
CREATE TABLE Example (Id INT, ANIMALS VARCHAR(15));
INSERT INTO Example VALUES (1,'Dog'), (2,'Fish'), (3,'Cat'), (4,'Lizard')
For a variable Test with values "Mammal',"Reptile", "Other":
//WHAT I'D LIKE IN GRAFANA QUERY EDITOR:
IF($Test = "Mammal") THEN
SELECT * FROM Example WHERE Id = 1 OR Id =3;
ELSE
SELECT * FROM Example WHERE Id = 2 OR Id =4;
END IF;
Is this kind of condition based query even possible? If so, what is the proper syntax to get it to work? Is there any way I can use Grafana variables to have a similar effect?
Use query. Query starts with SELECT keyword. Don't use any IF ELSE conditions before query, e.g.:
SELECT *
FROM Example
WHERE
Data IN ( ${variable:csv} )
This WHERE condition syntax will work with single value, multi value Grafana dashboard variables and also with All value (no custom All value, but blank=auto). Of course this condition is mainly for INT column types. STRING types may need different one (e.g. with LIKE and regexp matching).
Code all your logic (dependency on the dashboard variable) in the WHERE section. Use query inspector to see SQL which is generated and tweak it to correct SQL syntax.
Instead of an if, you can use or. It's really useful for conditionally checking variables:
select * from Example
where (Id in (1,3) or '$Test' != 'Mammal')
and (Id in (2,4) or '$Test' == 'Mammal')

SQL SUM function for a column with integer name

I'm trying to return the sum of a column with an integer name (for example, a column named 251)
CREATE FUNCTION myfun(cid varchar(255))
RETURNS float
BEGIN
RETURN (SELECT SUM(`cid`) FROM percentages);
END
I want it to return the sum of all the rows of the cid column of my percentages table, but it's instead returning the sum of the cids.
For example: if i call myfun(251)
if the values in the columns 251 are 1, 2, 3, and 4, I would expect an output of 1+2+3+4=10
But it is returning 251+251+251+251=1004 instead
You cannot do what you want with standard SQL. You simply cannot parameterize a column name, even with backticks, using regular SQL.
I can give you the hint that dynamic SQL (prepare/exec) can do what you want.
More importantly, I want to point out a flaw in your data model. You presumably have many columns with the same information. Instead, this data should be on separate rows. Something like:
id col val
SQL is usually better with more rows rather multiplying columns.
using dynamic sql and putting the column name in brackets, should work:
something like:
declare #sql varchar(255) = 'SELECT SUM([' + #cid + ']) FROM percentages'
exec(#sql)

I want to query the content of the json inside column like below

I am saving JSON data my SQL server 2016 table and I want to query the table by applying where clause on the JSON like below.
select c.customer_details.name.fullName
from j_customer c
where c.customer_details.name.fullName like '%Gopi%';
this is possible in oracle but in mssql it gives the error like below
Cannot call methods on nvarchar(max).
After going thru internet finally I found out that there is a method called JSON_VALUE() which should be used for finding the content of the json. The syntax for mssql is like this.
select *
from j_customer where JSON_VALUE(c.customer_details, '$.name.fullName') = 'C';
As far as I understood it, you have stored your JSON data into the table using a query like below
declare #json nvarchar(max) = '"Customer_details":{"name":{"fullName":"Dhruv Joshi"}}'
INSERT INTO j_customer
SELECT *
FROM OPENJSON(#json)
WITH (---some columns
fullName nvarchar(max) '$.Customer_details.name.fullName'
)
In such cases you can simply query your table like below
select c.fullName
from j_customer c
where c.fullName like '%Gopi%';
In SQL the fully qualified SQL column name usually is like [DatabaseName].[schema].[tablename].[columnname], so here in WHERE clause SQL interprets c.customer_details.name.fullName as c.customer_details a column as c is a table alias. And then name.fullname looks like a method call on the column name which generates the error.

SSRS - multiple values for TEXT data type parameter

I am new to SSRS reports and have created a report based on a TEXT query:
select * from customers
where residence_state = :state -- oracle, or #state for SQL Server
This generated the parameters and I am able to give a value, such as 'CA' in the 'Report Parameter Properties - Available Values' Window. This works well, however, suppose that I want the :state(or #state) parameter to include 'CA' + 'AZ' + 'WA'. What would be the easiest way to accomplish this?
for MSSQL, change your query to
select * from customers
where residence_state in (#state)
This will make the parameter multivalued. And also, check the parameter properties in SSRS. Make sure
Allow multiple values
is checked.
For multi select parameters SSRS passes a comma delimited list of values. The code below is from a report where the parameter #program is a comma delimited set of uniqueidentifiers.
--I create a table variable
declare #programs table
(
program_id uniqueidentifier
)
declare #myid uniqueidentifier
-- Then I parse the parameter values (which was declare as #program varchar(max)
-- as you can see I know the length of each parameter.
-- If you did use charindex to find the location of the next value
while len(#program) > 0
begin
set #myid = convert(uniqueidentifier, left(#program, 36))
set #program = substring(#program, 38, len(#program))
--print #program
insert #programs values(#myid)
End
I hope this helps.