JasperReports: A dynamic table name using a variable? [duplicate] - jasper-reports

This question already has an answer here:
Dynamic parameter of table name in Jasper [duplicate]
(1 answer)
Closed 3 years ago.
I have this problem with the JasperServer report I'm trying to create:
I have several db tables, named using a name and a date like this:
TABLE_NAME_YYYYMMDD
I want to be able to choose (and do a select from) the table which corresponds to the date submitted by the user from an ordinary Date input control.
I've tried creating a variable (called TABLE_NAME) which uses Java expressions for parsing the date like:
"MY_TABLE_" + new SimpleDateFormat("yyyyMMdd").format($P{RUN_DATE})
and when I print the value of the variable in the report it looks correct. But then I tried using that variable name in the SQL query like:
SELECT column1,column2.. from $V{TABLE_NAME}
but when I tried running the report in Jaspersoft Studio I got this Exception:
net.sf.jasperreports.engine.JRException: net.sf.jasperreports.engine.JRException: Error executing SQL statement for: my_report_x.
at com.jaspersoft.studio.editor.preview.view.control.ReportControler.fillReport(ReportControler.java:511)
at com.jaspersoft.studio.editor.preview.view.control.ReportControler.access$20(ReportControler.java:486)
So it doesn't seem to be working.
I read about the case when the whole table name can be specified in a parameter, and you're supposed to use:
$P!{tableName}
First I tried using that '!' with the variable name like:
..from $V!{TABLE_NAME}
but I got the same Exception.
Then I tried creating a new parameter instead, where "Is For Prompting" is Not checked, and as default value expression I put the same expression as I used in my variable:
"MY_TABLE_" + new SimpleDateFormat("yyyyMMdd").format($P{RUN_DATE})
but I still get the same error when I try to run the report in Jaspersoft Studio.
Does anyone know if there is a way to solve this? -Preferably a way that doesn't take several days to implement since I don't have that time.
I'm using Jaspersoft Studio 6.1.1.final and running the reports in JasperServer 5.5.0.

You should be able to get this to work by wrapping the whole of your FROM expression in the parameter e.g.
<parameter name="pTableName" class="java.lang.String">
<defaultValueExpression><![CDATA["from MY_TABLE_" + new SimpleDateFormat("yyyyMMdd").format($P{RUN_DATE})]]></defaultValueExpression>
</parameter>
And then using this in your SQL as a string literal:
SELECT column1,column2
$P!{pTableName}
WHERE 1 = 1

I just found out what I did wrong.
I admit it was rather stupid, but I only tried running the report in the Preview mode in Jaspersoft Studio. That's when I got the SQL error.
But I assume that the Preview mode does not support dynamic decisions about which table to read from, because when I ignored the Preview errors and published the report to JasperServer, I actually could run it there!
I ended up using the $P!{TABLE_NAME} parameter where the value is what I tried earlier:
"MY_TABLE_" + new SimpleDateFormat("yyyyMMdd").format($P{RUN_DATE})
I can print as much as possible of my SQL here (meaning that I have to replace names since this is a work report) if you want to see it:
select c.column1,c.column2, h.column3 from $P!{TABLE_NAME} h, TABLE2 i, TABLE3 p, TABLE4 ca, TABLE5 c
where h.P_ID = p.P_ID and h.A_ID = ca.A_ID and ca.C_ID = c.C_ID and ca.SOME_VALUE = 1 and ca.OTHER_VALUE = 1
and i.I_ID=h.I_ID
and i.OTHER_ID=1
and h.VALUE_X > 0
order by c.VALUE_Y
So my advice to others who create Jasper reports is not to let yourselves get fooled by the fact that some things don't work in Preview mode. -That might just be the "preview limitations".

Related

Crystal Reports Formula: Help forming IIF Statement

I am working with Crystal Reports 11.5.10.1263 [CR Developer Type: Full].
I am familiar with Programming and SQL syntax in general and have worked with expressions in SSRS & MS Access but never in Crystal Reports.
Problem:
I modified the SQL in the Crystal report and it has had the desired effect - except in the last part of the report output - where the changes I made to the SQL do not have the desired effect.
The 'desired effect' is that when a field named 'FundNumber' has the values '2595', '2597' Then the field named 'Organization' should be assigned the value '41600'.
The Detail Record in the Report has the following three rows as part of its 'Formula':
+ IIf({Data.Payments} > 0, "+01W ", "-01W ")
+ Left({Data.FundNumber}+ SPACE(6),6)
+ Left({Data.Organization}+ SPACE(6),6)
I want to change the Data.Organization row to an IIF Statement that says [pseucode]:
IIf Data.FundNumber IN ["2595", "2597"] Then "41600" Else Data.Organization
I believe the Left() function is saying 'return the left 6 characters of [Data.Organization + 6 Spaces] ?? and that it has to do with the way the data is presented in the output.
I would appreciate help with creating the IIF statement for this.
I included the 'IIf({Data.Payments} ...' row in case that helps - as my google searches have turned up examples that use IF instead of IIF.
Thanks!
The syntax most of the time is the same as VB. Try
IIF({Data.FundNumber} = "2595" or {Data.FundNumber} = "2597", "41600", {Data.Organization})

Multiple Search in SSRS when using only a part of the field

I Have created a stored procedure:
#DeviceID nvarchar(20) =''
WITH EXECUTE AS CALLER
AS
SELECT
amd.BRANDID,
amd.DEVICEID
FROM AMDEVICETABLE amd
where
left(amd.Deviceid,len(#DeviceID)) in (#DeviceID)
The length of amd.Deviceid is about 15 characters
In Visual Studio I create a parameter #DeviceID and when I am entering e.g ABCDE ( the first 5 characters from Deviceid) everything is working perfect.
the problem is that I want to put multiple values like
jhmcl*, jhmgd*.
So I created my own little version of your report and I believe the problem is your LEN() function. I'm surprised it doesn't return an error because it errors out in Report Builder for SQL Server 2014(simple version of SSRS). I would test what your LEN(#DeviceID) is returning. I would bet it's not returning the correct value. Instead you might try this to cover every possible pattern. I don't know how it will work performance wise.
SELECT DeviceID
FROM YourTable
WHERE LEN(DeviceID,1) IN (#DeviceID)
OR LEN(DeviceID,2) IN (#DeviceID)
OR LEN(DeviceID,3) IN (#DeviceID)
..
OR LEN(DeviceID,15),IN(#DeviceID)

simple where clause SSRS 2005 parameter not working

this should be a simple thing but I've spent hours to no avail. Basically, I need to look up a salesrep # in a SQL database using the user's Window's user id. The format of the user id is
"Norstar\kjones" and I need the "kjones" portion of it.
using the split function, I am able to pull just the 'kjones' part out:
split(User!UserID,"\").GetValue(1)
I've created a parameter called SlsmnNum and created a dataset to be used to look up the salesrep # using the user id (the slsm_num field is a varchar, not an integer):
select slsm_num from Salesman_Msid where slsm_msid = ''' + split(User!UserID,"\").GetValue(1) + '''
However, I get no results. How can I get the select to work?
alternatively, I tried the following:
in parameter SlsmnNum, I set the default to an expression using:
split(User!UserID,"\").GetValue(1) and this returns 'kjones', as expected.
I created a SECOND parameter (which is positioned BELOW the SlsmnNum parameter), SlsmnNum2, that has a default (and an available) value using a query, which is a dataset containing the following select statement:
select slsm_num from Salesman_Msid where slsm_msid = (#SlsmnNum)
When I run the query on the Data tab, when I type in 'kjones' into the parameter box, it returns '1366', the salesrep # I'm expecting.
But, when I Preview the report, all I get in SlsmnNum2 box is Select a Value and nothing is there (it should return '1366').
Any help would be greatly appreciated!
Try your first approach with Query Text as
="select slsm_num from Salesman_Msid where slsm_msid = '" & split(User!UserID,"\").GetValue(1) & "'"

iReports 4.5.1 throwing error while using TABLE Report Element

Hi I am getting the following error message while using the iReport designer Table Report Element:
Error filling print... java.lang.String cannot be cast to java.lang.Boolean
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Boolean
at net.sf.jasperreports.components.table.fill.FillTable.toPrintColumn(FillTable.java:116)
at net.sf.jasperreports.components.table.fill.FillTable$FillColumnEvaluator.visitColumn(FillTable.java:143)
at net.sf.jasperreports.components.table.fill.FillTable$FillColumnEvaluator.visitColumn(FillTable.java:130)
at net.sf.jasperreports.components.table.StandardColumn.visitColumn(StandardColumn.java:73)
at net.sf.jasperreports.components.table.fill.FillTable.evaluateColumns(FillTable.java:205)
at net.sf.jasperreports.components.table.fill.FillTable.evaluate(FillTable.java:98)
at net.sf.jasperreports.engine.fill.JRFillComponentElement.evaluate(JRFillComponentElement.java:88)
at net.sf.jasperreports.engine.fill.JRFillElementContainer.evaluate(JRFillElementContainer.java:257)
at net.sf.jasperreports.engine.fill.JRFillBand.evaluate(JRFillBand.java:473)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillColumnBand(JRVerticalFiller.java:2021)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillGroupHeader(JRVerticalFiller.java:616)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillGroupHeaders(JRVerticalFiller.java:538)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReportStart(JRVerticalFiller.java:263)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:128)
at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:836)
at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:746)
at net.sf.jasperreports.engine.fill.JRFiller.fillReport(JRFiller.java:58)
at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:417)
at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:247)
at com.jaspersoft.ireport.designer.compiler.IReportCompiler.run(IReportCompiler.java:879)
at org.openide.util.RequestProcessor$Task.run(RequestProcessor.java:572)   
at org.openide.util.RequestProcessor$Processor.run(RequestProcessor.java:997)
I am using Stored Procedures that returns a cursor as output.
I have placed this Table Report element in one of my group header.
thanks in advance
Meeza
Check in your style conditions if there's an evaluation with String. To ensure that everything works fine surround all the style conditions with new Boolean( ... ).

SSRS Reports Versioning

I'm looking for a way to get versioning informations out of my SSRS reports. I have several environments and would like to be able to compare which report version is deployed on these environments. In SSIS this is very easy because each SSIS package gets a new version when it was modified and safed. Is there something similiar with the reports ?
Unfortunately there currently is no built-in functionality similar to a dll assembly version for RDL files.
The only way to get some kind of version information is to query the last modified date of the RDL file on the server via C# or VB.Net. You could do this using the ReportingServices webservice.
You could also implement a custom function which updates some field in your database to the current date each time the RDL file modified.
The problem with all the file modified information: You still don't know which version is on which server, you just know when it was uploaded/modified.
See the following pages for some more information - unfortunately no solution:
SSRS 2005 Can I dynamically determine when .rdl file was last modified
.RDL "Version" properties like in ".DTSX" files
In my reports, I create a variable named Version and make it a string data type (and move it to the top of the variable list). Every time I change a report, I update the Version variable based on Semantic Versioning.
Then, I can query my Report Server and look at the Parameter field of the ExecutionLog table and I can see what version was run. Technically I take care of all of this in an SSIS job that writes to another table, but that's a little outside the scope here.
There still seems to be no good solution.
If you create a hidden Parameter "Version" with a default value you could use a build Task to modify its value in the .rdl file, e.g. the revision part with the source control changeset number.
Sadly you may need another visual Studio Project to place this build task since the reporting project type seems not capable.
You may also create inline code or an assembly that may do some lookup.
Cheap suboptimal alternative:
Use the last modified date form the report database:
Select
Name,
Path,
CreationDate,
ModifiedDate,
ModUser.UserName AS ModUser,
CAST(catalog.parameter as xml).value(/Parameters[1]/Parameter[Name="Version"][1]/Values[1]/Value[1]','NVARCHAR(20)') as Version
FROM Reportserver.dbo.Catalog
INNER JOIN ReportServer.dbo.Users ModUser on Moduser.UserID = ModifiedByID
WHERE Type = 2
and convert the date to a revision number...
Doesen't help with versions across different server instances though.
The Report Server dbo.Catalog table has both CreationDate and ModifiedDate.
As part of an investigation I put together a query to list out the queries in RDL files and perform a simple CHECKSUM on that to facilitate checking for drift.
You can of course do the same on the whole of Content field much more simply, or do a better job of opening the XML using real xml processing.
Hope it is helpful;
DECLARE #reftextstart VARCHAR(255);
DECLARE #reftextend VARCHAR(255);
SELECT #reftextstart = '%<CommandText>%';
SELECT #reftextend = '</CommandText>';
SELECT SQ2.Path,
SQ2.Name,
SQ2.CreationDate,
SQ2.ModifiedDate,
ReportQuery,
CHECKSUM(ReportQuery) ReportQueryChecksum
FROM
(
SELECT SQ.Path,
SQ.Name,
SQ.CreationDate,
SQ.ModifiedDate,
CASE PATINDEX(#reftextstart, Report)
WHEN 0 THEN
N''
ELSE
SUBSTRING(
Report,
PATINDEX(#reftextstart, Report) + LEN(#reftextstart) - 2,
CHARINDEX(
#reftextend,
SUBSTRING(
Report,
PATINDEX(#reftextstart, Report) + LEN(#reftextstart) - 2,
1024
)
) - 1
)
END ReportQuery
FROM
(
SELECT TOP 1000
[Path],
[Name],
[CreationDate],
[ModifiedDate],
[ExecutionTime],
CONVERT(VARCHAR(MAX), CONVERT(VARBINARY(MAX), Content)) Report
FROM [ReportServer].[dbo].[Catalog]
WHERE Name IN ( N'Report1', N'Report2' )
) SQ
) SQ2
ORDER BY [Path],
Name;
I tried this solution for my customers:
Open the .rdl file with notepad and add a comment in the first line and save it, like:
<?xml version="1.0" encoding="utf-8"?>
<!--
=============================================
Author: My Name (My Company)
CHANGE date: Release Date
Description: Short description
Current Version: Ver x.x.x
Vx.x.x: Version change history
=============================================
-->
<Report MustUnderstand="df" xmlns=...
I hope it helps others