I have a report that needs the user to enter a number that is passed to a parameter and that number determines the number of rows populated in a table. I then need the other parameter to pop up once per row. This is to populate a form that will be populated by a customer before the final report is run.
My code that is going into SSRS at the moment is:
DECLARE #Results TABLE
(
PositionNumber NVARCHAR(25),
JobTitle NVARCHAR(50),
JobStartDate NVARCHAR(25),
FullTimeEquivalent NVARCHAR(35),
Ethnicity NVARCHAR(25),
Gender NVARCHAR(25),
AgeBand NVARCHAR(25),
Disability NVARCHAR(25)
);
DECLARE #HLC INT, #COUNTER INT;
SET #COUNTER = 0;
SET #HLC = #NumberOfJobs;
WHILE #COUNTER < #HLC
BEGIN
INSERT INTO #Results
VALUES
(
'Position '+CAST(#COUNTER + 1 AS VARCHAR),
'',
#JobStartDate,
#FullTimeEquivalent,
#Ethnicity,
#Gender,
#AgeBand,
#Disability
);
SET #COUNTER = #COUNTER + 1;
END;
SELECT
*
FROM #Results;
Unfortunately I am getting all of the parameters at once.
I have used multiple parameters before but never in order to populate a table as the report is running. I am assuming that I have my while loop in the wrong place but cant find anything to point me in the right direction. How do I get the first parameter to come up on its own and after that is populated by the user, the rest of the parameters to pop up so that the user can make their selections (and then just those parameters to pop up again in a group for each row that was entered in the first parameter). I.e. if the number 5 is entered in the first parameter the other 6 parameters should pop up 5 times.
I build my SQL query in SQL query pane and then copy it into the SSRS design query pane. I am using SSRS 2008 r2
Related
I have created an SP with dynamic pivot logic to bring in data for the number of calls made on an hourly basis. Since I have this report on different servers. Now I need to create a master SP that pulls data from this dynamic pivot table. I have defined my temp table but this breaks the report for the current day as all the buckets are not created before 12 pm for the same day. For individual reports on each server, I am inserting the dynamic Pivot table data into a temporary table. For example:
create table #temp( Emp_name varchar(30), calltime varchar(30), totalcall varchar(30),
midnighthr int not null default 0 , 1am int not null default 0,
2am,3am,4am, 5am, 6am, 7am,...,9pm,10pm, to 11pm)
insert into #temp
EXEC #sql
select (Emp_name, calltime , totalcall , midnighthr, 1am ,
2am,3am,4am,5am,6am, 7am,...,9pm,10pm,to 11pm)
So if I run this report for DateTime: 6/24/2020 4:00 PM then this report fails because the number of columns created by #sql doesn't justify the defined columns in #temp table since we have no columns for 5 pm onwards.
Any idea how to overcome this problem?
Thanks for your feedback. The requirement changed and they decided to set the default to 0. I'm creating a temp table
SET #SQL = ' SELECT wrkGrp, userID., DTName,' + #Columns + ' FROM( SELECT tsdh.wrkGrp,tsdh.userID,SUBSTRING(tsdh.Datehour,1,10) As DTName, SUBSTRING(tsdh.Datehour,12,2) AS HourName,tsdh.CallTotal FROM #tmpsummaryDH tsdh) as PivotData PIVOT(SUM(CallTotal ) FOR HourName IN (' + #Columns + ' ) ) AS PivotResult ORDER BY wrkGrp, userID, DTName' ; INSERT INTO #tmpPivot EXEC(#SQL); SELECT COALESCE([Midnight hr],0) AS [Midnight hr], etc
We have a company website where BI reports are hosted. For one particular report (and possibly for others, if this can be made to work), there is a requirement to:
a) retrieve saved values for report parameters
and
b) to save any changed values for report parameters
I know that parameter values can be retrieved from data by setting the Default Values to "Get values from a query".
However, what I would like to do is when the user presses View Report that the values that [s]he has selected should be saved to a database so that these will then form the default values for the next user.
Can this be done? There doesn't seem to be any way "out of the box".
This is quite simple.
Lets assume you had a table of Countries that drive your parameter's available values and that this table myCountryTable has two columns CountryID and CountryName.
You available values dataset would be something simple like
SELECT * FROM myCountryTable
CountryID would be the parameter value and CountryName would be the parameter label.
OK so you will have probably done all the above already.
Now, in your main dataset query simply add an INSERT statement before you main query runs.
So, if you dataset query looks like this..
SELECT * FROM SomeBigTable WHERE CountryID in (#CountryID)
you would change it to something like
INSERT INTO myLogTable
SELECT CountryID, CountryName FROM myCountryTable WHERE CountryID IN (#CountryID)
-- original query follows
SELECT * FROM SomeBigTable WHERE CountryID in (#CountryID)
Note: If you cannot change your main dataset query for whatever reason, you can do this in a separate dataset but there are a few things you will have to do
First: Change the sql so that it returns a value at the end, anything will do e.g.
INSERT INTO myLogTable
SELECT CountryID, CountryName FROM myCountryTable WHERE CountryID IN (#CountryID)
SELECT 1 as myReturnValue
Second: You must bind this dataset to something on the report such as a table or list, this is to make sure the query only executes when the report is executed, not when parameters are changed.
You could store parameters and their values every time the report is executed.
Note: Some of these integrated SQL functions maybe do not exist on your server, which depends on the server version. If that is the case, it is easy to find alternative, or even create your own function.
For example, at the end of every stored procedure that is used by report place this part of SQL query that uses newly created table dbo.ReportParameterValuePairs:
INSERT INTO dbo.ReportParameterValuePairs
(ReportName, ParameterValuePair, ExecutionDateTime)
VALUES(
'MyReport',
'$$$parameter1$$$: ' + #parameter1 + ',' +
'$$$parameter2$$$: ' + #parameter2,
GETDATE())
Later on will be clear why are these data stored and why in this way.
Nest step would be creating procedure which will retrieve value of some parameter during the last execution of report:
CREATE PROCEDURE spRetrieveReportParameterValue
#parameter NVARCHAR(100),
#report NVARCHAR(100)
AS
BEGIN
-- this is an example
DECLARE #parameters NVARCHAR(MAX) = '$$$parameter1$$$: value1, $$$parameter2$$$: value2'
-- in reality parameter-value pairs will be retrieved from database
--DECLARE #parameters NVARCHAR(MAX) =
-- (SELECT TOP 1 ParameterValuePair
-- FROM dbo.ReportParameterValuePairs
-- WHERE ReportName = #report
-- ORDER BY ExecutionDateTime DESC)
--SELECT #parameters
DECLARE #parameterValuePair NVARCHAR(200) =
(SELECT * FROM STRING_SPLIT (#parameters, ',')
WHERE
VALUE LIKE '%$$$' + #parameter + '$$$%')
--SELECT #parameterValuePair
DECLARE #value NVARCHAR(100) =
(SELECT * FROM STRING_SPLIT (#parameterValuePair, ':') WHERE value NOT LIKE '%$$$%')
SELECT TRIM(#value) AS ParameterValue
END
Parameters of the procedure are: parameter which value is needed, report that is executing.
Parameter-value pairs are stored in a single string. To access that data search table dbo.ReportParameterValuePairs for currently executing report. Order data by date and time of execution, starting from the latest.
Parameter-value pairs string will be split using ,. The result of this split is a table that consists of parameter-value pairs. Distinction between parameters and their values is $$$ mark. Because of that the condition in query is VALUE LIKE '%$$$' + #parameter + '$$$%'.
Variable #parameterValuePair now stores desired parameter and its value.
After another one split, this time using : because it separates value from parameter name, the result of split will be two rows. One contains parameter and $$$ marks ($$$[parameter]$$$) and the other contains the value. Using condition WHERE value NOT LIKE '%$$$%' parameter's value will be stored to #value variable.
Last step of the procedure is to trim the value in case there are empty spaces at the end and at the beginning of the #value and return it as ParameterValue.
In order to retrieve this value to report create DataSet for every report parameter. This DataSet will supply parameter with default value:
right click on DataSets
choose Add Dataset
choose tab/card Query
name DataSet
select Data source
for query type choose Text
enter spRetrieveReportParameterValue 'parameter1', 'MyReport' where parameter1 is name of parameter which last value will be retrieved
click Refresh Fields
The last step is to set default value to the parameter:
right click on parameter
select Parameter Properties
choose card/tab Default Values
choose option Get values from a query
for Dataset choose newly created dataset
for Value field choose ParameterValue
This should be the result:
Parameters - Multiple Values For One Label - Is it Possible?
Is it possible to have multiple values for ONE label in one row in non-queried parameter value,
so that i can have ONE LABEL in Drop-down List for multiple values.
Basically if i write single value for each label then it works fine but the problem is when i run the report it shows me check-box for each name. It does not look nice for the user to tick and select all values for Site-1 like nine or ten values. Where as ideally it should be one label and multiple values (which i want).
Works Fine: (But this bring a long list in drop down menu since i have more value for site-1, which i do not want)
Label Value
---------------------------
Site-1 150
Site-1 151
Site-1 152
Site-1 160
Site-2 161
Site-2 162
and in query ColumnName IN (#Site) works fine too.
Required result (In one line so that drop down menu should give only two labels)
Label Value
---------------------------
Site-1 150,151,152,160
Site-2 161,162
which will bring one label and related multi-value in drop down or combo box, and in query ColumnName IN (#Site) DOES NOT work.
Sorry. Site is a column name in DB and #Site is a variable name.
Site-1 & Site-2 are label names for drop down menu.
The basic problem is your database sees your comma-separated values as a normal string when you pass it as a parameter, so it doesn't know what to do with '150,151,152,160' when it's expecting an int.
A good general way to handle this is by creating a table-valued function that returns a table when you pass in your comma-separated string. The easiest way I've found is Erland Sommarskog's Arrays and Lists in SQL Server:
CREATE FUNCTION split_csv (#list nvarchar(MAX))
RETURNS #tbl TABLE (number int NOT NULL) AS
BEGIN
DECLARE #pos int,
#nextpos int,
#valuelen int
SELECT #pos = 0, #nextpos = 1
WHILE #nextpos > 0
BEGIN
SELECT #nextpos = charindex(',', #list, #pos + 1)
SELECT #valuelen = CASE WHEN #nextpos > 0
THEN #nextpos
ELSE len(#list) + 1
END - #pos - 1
INSERT #tbl (number)
VALUES (convert(int, substring(#list, #pos + 1, #valuelen)))
SELECT #pos = #nextpos
END
RETURN
END
Then using the above function, that line in your query becomes:
ColumnName IN (SELECT number FROM split_csv(#Site))
If you are using SQL Server 2008+, you can use table-valued parameters instead which are even simpler. I'll refer you to the same Erland Sommarskog site for info on that.
how to write query for following request?
my table:
id designation
1 developer,tester,projectlead
1 developer
1 techlead
if id=1,designation="'developer'"
Then need to first,second records.Because 2 rows are having venkat.
if id=1,designation="'developer','techlead'" then need to get 3 records as result.
i wrote one service for inserting records to that table .so that i am maintaining one table to store all designation with same column with comas.
By using service if user pass id=1 designation="'developer','techlead'" then need to pull the above 3 records.so that i am maintaining only one table to save all designations
SP:
ALTER PROCEDURE [dbo].[usp_GetDevices]
#id INT,
#designation NVARCHAR (MAX)
AS
BEGIN
declare #idsplat varchar(MAX)
set #idsplat = #UserIds
create table #u1 (id1 varchar(MAX))
set #idsplat = 'insert #u1 select ' + replace(#idsplat, ',', ' union select ')
exec(#idsplat)
Select
id FROM dbo.DevicesList WHERE id=#id AND designation IN (select id1 from #u1)
END
You need to use the boolean operators AND and OR in conjunction with LIKE:
IF empid = 1 AND (empname LIKE '%venkat%' OR empname LIKE '%vasu%')
The above example will return all rows with empid equals 1 and empname containing venkat or vasu.
Apparently you need to create that query based on the input from user, this is just an example of how the finally query should look like.
Edit: Trying to do this within SqlServer can be quite hard so you should really change your approach on how you call the stored procedure. If you can't do this then you could try and split your designation parameter on , (the answers to this question show several ways of how to do this) and insert the values into a temporary table. Then you can JOIN on this temporary table with LIKE as described in this article.
I have been working on a script that will swap two IDs round which are primary keys:
CREATE PROCEDURE dbo.ID
#OldName NVARCHAR(128),
#NewName NVARCHAR(128)
AS
DECLARE #NewId INT, #OldId INT
CREATE TABLE TmpTable (ID INT, Name NVARCHAR(128))
INSERT INTO TmpTable (Name,ID)
VALUES (#NewName, (SELECT ID FROM Table1 WHERE [Name] = #NewName));
INSERT INTO TmpTable (Name,ID)
VALUES(#OldName, (SELECT ID FROM Table1 WHERE [Name] = #OldName))
UPDATE Table1
SET ID = (SELECT MAX(ID) + 1000 FROM Table1)
WHERE [NAME] = #NewName
UPDATE Table1
SET ID = (SELECT MAX(ID) + 2000 FROM Table1)
WHERE [NAME] = #OldName
UPDATE Table1
SET ID = (SELECT ID FROM TmpTable WHERE Name = #NewName)
WHERE [Name] = #OldName
UPDATE Table1
SET ID = (SELECT ID FROM TmpTable WHERE Name = #OldName)
WHERE [Name] = #NewName
DROP TABLE TmpTable
go
What I am now trying to do is to run this as a report in reporting services where the NewNAme and OldNAme will be selected from a drop down box that queries the Table1 Table. At present when I try to import this code it asks for the values.
How do I import a stored procedure into a report.
How do I get a dropdown box to query the column [Name].
when you add a stored procedure to your report ass data source, the ssrs automatically add its parameters to report ass report parameter.
if you want to specify the parameters by drop down, you must write an application like a web application and pass this parameters to your report.
I have been using the Report wizard most of the time, using text to import the stored procedure like
exec ID 1,2
That gets me that base return values for the tablix. However in your case that does not apply as there is no select return.
After the wizard creates the report, right click on the dataset and select the stored proc. Click Refresh Fields and the parameters will be added automagically.
To get your report parameters to be drop downs you need a data source for them. I add a new dataset based on either a select statement or another stored proc to select the values I need.
Right click on the parameter and select Parameter Properties
You can now jump to the available values tab and choose "Get values from a query"
Select the dataset that provides the drop down values.
There are other options like allowing nulls etc that can fine tune how the drop down works.
Pretty curious why you would want a report that runs the SQL you show without returning any values. Seems there would be better ways to give the user the power to run something like that.