Return text if tablix results are empty - reporting-services

I have a tablix that is linked to DataSet1.
DataSet1 uses the following TSQL code
select ir.SourceRef as Account_Ref,
rab.BalanceFromDate,
rab.ClosingBalance Current_Balance,
ra.Account_ID as rserial,
ra.Current_Balance as Current_Balance
from db1..RentAccountBalance rab
left join db1..ImportReference ir on ir.EntityID = rab.AccountId and ir.EntityType='XXXX.XXX.X.XX'
left join db2..RentAccounts ra on convert(varchar(50),ra.Account_ID) = ir.SourceRef
where ir.SourceRef = '12857'
order by rab.AccountBalanceId
As I know that there is no ir.SourceRef that is equal to 12857, the result set is blank. Therefore, my tablix comes back just blank.Is there a way that if no results are returned that a text of say "All Accounts are OK." be displayed by the report instead?
Hope that's clear?
Thanks

You can try expression like:
=IIF(IsNothing(Fields!FieldName.Value, "Accounts are OK", Fields!FieldName.Value))
Or if you want to check if there is no data at all, you can try to throw an error in TSQL in following:
--add this line to the end of query:
IF ##ROWCOUNT = 0 RAISERROR('Accounts are OK', 16, 1)

If you use a Stored Procedure you can then insert your Select statement data into a table variable before returning it. From this you can perform a check on its contents before it is returned to the report.
For example if you populate a table of data you wish to return as follows
INSERT INTO #ReturnTable (Account_Ref, ...)
SELECT ir.SourceRef, ...
You can then query it's contents by using a command such as
IF (SELECT COUNT(*) FROM #ReturnTable) = 0
BEGIN
INSERT INTO #ReturnTable (Account_Ref, ...)
SELECT 'All Accounts are OK', ...
END
You can then perform a check within the report to see if the Account_Ref is 'All Accounts are OK', and if so display the report appropriately. You can even set the entire report's contents inside a rectangle with the visibility set to the result of
=iif(First(Fields!Account_Ref.Value) = "All Accounts are OK", false, true)
You can layer another object (an information message perhaps) on top of this with the inverse of this visibility set.

Related

Use ValueA when JOIN returns a row, otherwise ValueB (like a default)

I have four tables for a form-builder in my databse.
fields (fieldID(PK), typeID, fieldName, ...) - This table is a row by row list of all fields to be in the form
fields_types (typeID(PK), htmlType, ...) - This is a table that links fields to html types (and other settings)
fields_meta (FieldMetaID(PK), FieldID, mName, mValue) - Additional settings for fields, but more specific. A textarea field might have a height attribute, but almost no other field would use that.
fields_tyeps_meta (TypeMetaID(PK), typeID, tmName, tmValue) - Defines what extraneous settings a field can have, and also supplies default values if it's not explicitly set)
So my Query currently looks something like this
SELECT *
FROM Fields F
JOIN Field_Types FT
on FT.FieldID = F.FieldID
LEFT
JOIN Field_Meta FM
on FM.FieldID = F.FieldID
I was wondering if there's a way to join Fields_Types_Meta so that when the row's JOIN to Fields_Meta doesn't return a row (no mValue), it returns tmValue
I realize I can use something like (CASE WHEN mValue = "" THEN tmValue ELSE mValue END) AS UseValue, but I might have fields where I want to allow the value to be set to empty.
Edit: I could probably do something with a subquery and COUNT, using a CASE decision based on that. It might not be the healthiest performance-wise, but this query runs and caches itself til server restart, or until it's told to run again (updates to form design)
It looks like you just want ¢oalesce():
coalesce(FM.mValue, FT.tmValue) as UseValue
When FM.mValue is null, coalesce() returns FT.tmValue instead.
If you have null values in FM that you want to preserve in the result set, then use a case expression instead:
case when FM.FieldID IS NULL THEN FT.tmValue ELSE FM.mValue END as UseValue
This phrases as: when the left join did find a match in FM, use mValue from that row (even if it is null), else use FT.tmValue.

Update when unchecked value is checked

I have a query where the report name and report id are both displayed This only applies when the reports are pre-checked. The values are populated in a datagrid. If the report is unchecked, only the name is displayed. I tried using the UPDATE keyword but I kept running into syntax error. I know that the small change to the query is simple, but I am having a tricky time attempting to display the reportid when the report is unchecked or basically reportvisible being 0. How do I work around this to show the reportid regardless of if it is checked or not?
valsql1 = "SELECT c.ReportID, c.COMPANYID, rl.REPORTNAME
FROM CompanyReportListTable c
right join ReportList rl on c.reportid = rl.ReportID
and reportvisible = 1
and CompanyID =" & DropDownList1.SelectedValue & "
where rl.ReportID in (
Select ReportID
from ReportList
where ReportVisible = 1
)
order by ReportName"
You're right joining exclusively on reportvisible = 1, and then you're specifically selecting only reportIDs with reportvisible = 1. You are twice-over filtering out rows with reportvisible = 0, so of course you aren't going to get reportids for those rows. You should add rl.reportvisible to the SELECT clause and remove reportvisible = 1 from the join and where clauses.
It is only a one word/two letter change. At the first SELECT statement, instead of c.ReportID, it should be rl.REPORTID. Refer back to the right join; y using a right join, it returns the selected rows from reportlist, matches it with crl table. Now we made it so reportID appears regardless if it matches or not. Kind of like report name/title

Allowing Optional Parameters for MySQL Query

I have a search page that has multiple fields that are used to create a refined search. Every field is optional. I'm trying to start crafting my sql query so that it will work given the proper variables but I'm having trouble.
Here is the SQL query I currently have:
SELECT
indicator.indid,
indicator.indicator,
indtype.indtype,
provider.provider,
report.report,
actor.actor
FROM
actor,
indicator,
indtype,
report,
provider
WHERE
indicator.indtypeid = indtype.indtypeid
AND indicator.actorid = actor.actorid
AND indicator.reportid = report.reportid
AND report.providerid = provider.providerid
AND indicator.indicator LIKE '%$indicator%'
AND indicator.indtypeid = $indtypeid;
Whenever I provide an indicator and an indtypeid, the search works just fine. However, when I leave the indtypeid field blank, and have the variable set to * (as its default value), the query returns no results. I've tried playing with the query manually and it doesn't seem to like the * or a % sign. Basically, if only an indicator is specified and no indtypeid is specified, I want to return all indicators for all indtypeids.
I'm sure I'm missing something minor, but I would appreciate any assistance that could be provided. I may be going about this all wrong in the first place.
Try this instead:
SELECT i.indid, i.indicator, it.indtype,
p.provider, r.report, a.actor
FROM actor a
INNER JOIN indicator i ON a.actorid = i.actorid
INNER JOIN indtype it ON i.indtypeid = it.indtypeid
INNER JOIN report r ON i.reportid = r.reportid
INNER JOIN provider p ON r.providerid = p.providerid
WHERE 1 = 1
AND ($indicator IS NULL OR i.indicator LIKE '%$indicator%')
AND ($indtypeid IS NULL OR i.indtypeid = $indtypeid);
So if you pass a $indicator = NULL, then the first condition AND ($indicator IS NULL OR i.indicator LIKE '%$indicator%') will be ignored since it will resolve to True, and the same thing for the second condition.
I've removed other Where condition and replace them with JOINs, and for WHERE 1 = 1 to make the query work fine in case you pass the two variables $indicator and $indtypeid with NULL values for each, in this case it will return all results since 1 = 1 always true.

getting the updated value from an UPDATE query

I have an UPDATE Query :
UPDATE FKMS_GNST_Transaction_Details
SET Received_Quantity=Received_Quantity+(
CASE
WHEN (#int_Updated_Qty)>=(GTD.Quantity-GTD.Received_Quantity)
THEN GTD.Quantity-GTD.Received_Quantity
ELSE (#int_Updated_Qty)
END)
,#int_GNST_Reference_Id=GTD.Transaction_Detail_Id
FROM FKMS_GNST_Transaction_Details GTD
INNER JOIN #tbl_transactions tmp
ON tmp.Transaction_id=GTD.Transaction_id
AND GTD.Item_id=tmp.Item_id
I want to get the quantity which is added to the Received_Quantity field. That is, if (#int_Updated_Qty)>=(GTD.Quantity-GTD.Received_Quantity) then GTD.Quantity-GTD.Received_Quantity other wise #int_Updated_Qty.
How can we take this value (into a variable or any other way)? Please help.
Use the OUTPUT clause
UPDATE FKMS_GNST_Transaction_Details
SET Received_Quantity=Received_Quantity+(
CASE
WHEN (#int_Updated_Qty)>=(GTD.Quantity-GTD.Received_Quantity)
THEN GTD.Quantity-GTD.Received_Quantity
ELSE (#int_Updated_Qty)
END)
,#int_GNST_Reference_Id=GTD.Transaction_Detail_Id
--start gbn code
OUTPUT INSERTED.Received_Quantity
--end gbn code
FROM FKMS_GNST_Transaction_Details GTD
INNER JOIN #tbl_transactions tmp
ON tmp.Transaction_id=GTD.Transaction_id
AND GTD.Item_id=tmp.Item_id
The OUTPUT results can go
into a table (real, temp or variable)
to the client as a recordset
You can't assigned directly to a local variable

Help with MySQL Coalesce and Stored Procedures

I'm (attempting) to write a MySQL stored procedure that parses a large text file. Part of what this procedure does is check to see if the entities (in this case, government contractors) named in each record are already contained in the db. (This is a follow up to this question.) This is my first stored procedure and so I'm sure I've wondered off the rails here, and I would appreciated any help.
Here's what I have right now (after declaring the variables):
-- try and fetch first organization (a government agency)
SET agency = COALESCE(SELECT org_agency_o_id FROM orgs_agencies WHERE org_agency_code = maj_agency_cat,SELECT min(org_id) FROM orgs WHERE org_name LIKE CONCAT('U.S. ',SUBSTRING(maj_agency_cat,5)))
-- check to see if that worked
IF agency = NULL THEN
INSERT INTO orgs (org_name,org_name_length,org_type,org_sub_types) VALUES (CONCAT('U.S. ',SUBSTRING(maj_agency_cat,5)),LENGTH(CONCAT('U.S. ',SUBSTRING(maj_agency_cat,5))),'org','Org,GovernmentEntity,Federal,Agency');
SET agency = LAST_INSERT_ID();
END IF;
-- try and fetch second organization
SET org = COALESCE(SELECT MIN(org_id) FROM orgs WHERE org_name IN (vendorname, vendoralternatename, vendorlegalorganizationname, vendordoingasbusinessname), SELECT MIN(org_alias_org_id) FROM orgs_aliases WHERE org_alias in (endorname, vendoralternatename, vendorlegalorganizationname, vendordoingasbusinessname))
IF org = NULL THEN
INSERT INTO orgs(org_name,org_name_length,org_type,org_sub_types,org_created) VALUES (vendorname,LENGTH(vendorname),'org','org',DATE());
SET org = LAST_INSERT_ID();
END IF
Right now MySQL is throwing an error on the line:
SET agency = COALESCE(SELECT org_agency_o_id FROM orgs_agencies WHERE org_agency_code = maj_agency_cat,SELECT min(org_id) FROM orgs WHERE org_name LIKE CONCAT('U.S. ',SUBSTRING(maj_agency_cat,5)))
'maj_agency_cat' is a variable that I declare at the beginning of the procedure and then is assigned dynamically using a cursor that goes through my staging data. The full stored procedure can be viewed here.
I'm sure I'm missing something basic and would appreciate any help.
Try wrapping another () around the inner SELECT statements in your COALESCE arguments. Otherwise, they are not treated as subqueries to be executed first and the value returned, but as query objects passed into COALESCE, which is not a valid argument type for COALESCE:
SET agency = COALESCE((SELECT ..), (SELECT ..))