srss data query parameter / variable - sql-server

how can i pass a value from one dataset value as a variable/parameter into another dataset query to populate a table cell?
eg. Table displaying DataSet1 results. Last column comes from DataSet2 which has following query
SELECT * FROM tbl WHERE = $1
The WHERE value I want to come from a field value from DataSet1 resultset. How can I do this?
Thanks

You should be able to set up an internal parameter with your other dataset using "Default Values" "Get values from a query", then reference this parameter in your other dataset. Note your SQL Syntax needs correction in the WHERE clause. Note you may see an error if the dataset to be populated executes before the populating dataset.
If your second table is in a subreport, you can pass the field from your first query by simply referencing it as a parameter in the subreport:
=Fields!DataValue.Value

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.

Can SSRS dataset have a default value?

My SSRS report has one dataset (datasetMacys) that calls usp_GetStoreSales #Rundate, #StoreName.
Can the dataset have a default value? That way the report will only ask for one parameter (#Rundate) and the dataset has the string value "Macys" embedded?
In other words, the user will not have to select a value for the second parameter because it's already the default value of that dataset.
I would then add a 2nd dataset (datasetSears) where the default value for #StoreName is "Sears", which means it will only use the #Rundate that the user selected.
The stored procedure looks like this, even though it's not really needed for the question:
create procedure usp_GetStoreSales
(
#RunDate date,
#StoreName varchar(10)
)
as
select * from [Sales]
where RunDate = #RunDate and StoreName = #StoreName
I can easily fix this problem by creating two different stored procedures (ie. usp_GetMacysSales #RunDate and usp_GetSearsSales #RunDate), but that's exactly what I want to avoid.
You just need to create your two datasets and then, for each dataset, righ click the dataset name, choose properties, click the parameters tab and overwrite the parameter value for the StoreName parameter.
Yes - you can have a default value for the parameter of a dataset.
In the Parameters tab of the Dataset Properties, you can type in (hard code) a value in the Parameter Value expression box.
Of course the next question would be WHY? There may be better ways to do it.
If you are going to have both sets of data, why not make a query that combines the data into one so you only have one dataset?

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

SSRS: Dataset2 not showing data inserted in Dataset1

I have two datasets namely Dataset1 and Dataset2.
Dataset1 is a query type of "Stored Procedure". The sp "TestProcpk" is selected and parameter "value" is mapped to it.
TestProcpk query:
Create procedure TestProcpk #value varchar(20)
as
insert into testProc select #value
Dataset2 uses the above table as below (Dataset2 fields are used in the report display):
select value from testProc
where value = #value
Expected
Note: table "testProc" is empty.
While running the report I select parameter value as "ABC". The report should display value "ABC".
Why Dataset2 is not reflecting the value "ABC" in same time? Any other workaround to achieve this.
Thanks
I believe your problem is due to SSRS running the transaction in parallel. The table isn't created from Dataset 1 when Dataset 2 is run.
In the Datasource Properties, on the General tab there is a setting for Use single transaction when processing queries. This forces the queries to run one at a time in a single transaction (great for using temp tables). Check this box and it should work as you expect. It will execute in the order of your datasets (top down).
For more info:
http://blogs.msdn.com/b/robertbruckner/archive/2008/08/07/dataset-execution-order.aspx

Sql Server Reporting Services Must Declare the scalar variable #param

I have a Sql Server reporting services project. I have a dataset query called Total where I select certain data based on a parameter:
select ...
from ...
group by ...
having prop_id = #PropID
Now to populate a list of multiple values for this parameter, I have a dataset query called AllProps that selects all possible prop_id's:
select prop_id from proposal
order by prop_id
Now in the Report Data Pane I select the parameter properties from #PropID and fill out the forms as follows:
Under General I have,
Name: PropID
Data type: Text
(I select "Allow multiple values")
Under Available values I have,
Get values from a query
Dataset: AllProps
Value Fields: prop_id
label field: prop_id
Under Default Values I have,
Get values from a query
Dataset: AllProps
Valuefield: prop_id
When I click the preview tab to see my report I get the following error:
An error occurred during local report processing. An error has occurred during report processing. Query execution failed for dataset 'Total'.
MUST DECLARE THE SCALAR VARIABLE '#PropID'.
Where did I go wrong? What is scalar variable in SSRS and how is it properly used?
Thanks
The query which you have written needs to be corrected .Since you have selected multiple values you need to use in clause .
Select col1,col2....
from TableName
where prop_id in (#PropID)
in stored procedure you can directly pass the parameter and split the values using a function inside stored procedure.
if parameter is directly passed to a sql query instead of stored procedure, then concatenate the parameter values using a join and pass to datasetenter image description here

Resources