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:
Hey i'm trying to do some sql request but got a problem.
Firstly i do a replace request who filled my database.
After i do an update request that update some values.
And i'm doing another replace which is used to avoid duplicates, however some fields that were updated just before going to null because of the delete that is done before the insert (replace case)
How can I proceed so that the replace avoids putting some attribute to null and keep the values from the update?
Here is the sequence of the 3 queries that I make
REPLACE INTO all.serviceworker_subscriptions
(userId, userAgent, getDisplayMedia)
VALUES ('"+userId+"','"+userAgent+"','"+getDisplayMedia+"')
UPDATE all.serviceworker_subscriptions
SET dateDerniereConnexion = '"+socketDisconnectDate+"'
WHERE userId = '"+userId+"'
REPLACE INTO all.serviceworker_subscriptions
(userId, userAgent, getDisplayMedia)
VALUES ('"+userId+"','"+userAgent+"','"+getDisplayMedia+"')
My field : dateDerniereConnexion disappear after each 2nd replace ...
Thanks
iam using visual studio 2017 and make an rdl report
and i have a parameter (drop down)that takes it's values from the query
select column1 from table1
suppose that there are five elements A,B,C,D,E in column1 of table1
how to set first value which appears form available values
to be selected as default in order to not obligate the user to select a value
i mean i don't want to see the phrase instead i want first value to be selected
For single select values this is the default behaviour.
For multi select you can create another dataset using TOP and set it as the dataset for the parameter default values
select TOP(1) column1 from table1
I am trying to use an update query to update fields from one table to another for fields but only if the fields in the table that i am updating into is blank. If they contain information, I do not want to overwrite the existing data
e.g
Field: Name
Table: Table 1
Update to: [Table2.][Name]
Criteria:
I am unsure of what to put in the criteria. I tried, 'Is Null', Like "".
Here is an example:
UPDATE MyTable SET MyTable.FieldB = "MyNewValue" WHERE (((MyTable.FieldB) Is Null));
Looking at the Query from within Access, you can switch to SQL view. You just need to put Is Null in the criteria column: UPDATE MyTable SET MyTable.FieldB = "MyNewValue" WHERE (((MyTable.FieldB) Is Null));
Furthermore, you can just write Is Null on alternate lines and it will count as OR.
I'm trying to update a field of my table with the CONCAT of the some fields of the same table.
Whith this
UPDATE tabex SET field1=CONCAT(tabex.a1,', ',tabex.a2,', ',tabex.a3,', ',tabex.a4,', ',tabex.a5,', ',tabex.a6,', 'tabex.a7,', ',tabex.a8,', ',tabex.a9 );
This query has 0 rows affected and no errors.
With this other query
UPDATE tabex SET field1=CONCAT_WS(tabex.a1,', ',tabex.a2,', ',tabex.a3,', ',tabex.a4,', ',tabex.a5,', ',tabex.a6,', 'tabex.a7,', ',tabex.a8,', ',tabex.a9 );
If the content of some of a(n) fields is NULL mysql puts a copy of the previous result
Someone can help me?
When this query
UPDATE tabex SET field1=CONCAT(tabex.a1,', ',tabex.a2,', ',tabex.a3,', ',tabex.a4,', ',tabex.a5,', ',tabex.a6,', 'tabex.a7,', ',tabex.a8,', ',tabex.a9 );
doesn't affect a row, the only explanation would be, that the table is empty. It would update every row in the table. But if one of the columns is NULL, your field1 column will also be NULL.
To avoid that, you have to use the COALESCE() function. This function returns the first of its parameters which is not NULL.
UPDATE tabex SET field1=CONCAT(COALESCE(tabex.a1, ''),', ',...);
On a sidenote I have to ask, why you want to do this. Comma separated values in columns are a bad idea most of the times.
And finally, your query using CONCAT_WS() is wrong. The _WS in the function name is short for "with separator", so the first parameter is the separator which then is placed between the other parameters of the function. So you should write it like this:
UPDATE tabex SET field1=CONCAT_WS(',', tabex.a1, tabex.a2, tabex.a3,...);
Another advantage of the CONCAT_WS() function is, that it ignores NULL values. Read more about the two functions in the manual.