If you're building a report where you'd like the table to vary, use a multi-statement UDF -- a SQL Server-specific feature -- to decide which table to use. For example, these two tables have equivalent schemas, but hold different data.
Data from 2 Equivalent Tables |
A SQL Server multi-statement UDF (User-Defined Function) can be written which will accept a parameter (in this case 'oldFlag') and return the specified table's data. Note that this function could also normalize the column names to a common definition.
CREATE FUNCTION dbo.GetContacts (@oldFlag AS INT)
RETURNS @cnct TABLE
(
contactId INT,
firstName VARCHAR(50),
lastName VARCHAR(50)
)
AS
BEGIN
IF @oldFlag = 1
INSERT @cnct SELECT contactId, firstName, lastName FROM Contact_old
ELSE
INSERT @cnct SELECT contactId, firstName, lastName FROM Contact
RETURN
END
To test this outside of Jasper Report, make the following calls:
SELECT * FROM dbo.GetContacts(0)
SELECT * FROM dbo.GetContacts(1) -- return _old records
Jasper Report
To create the Jasper Report, create a parameter 'oldFlag' which includes a default value. The default values is used in the query editor.
Report Query Configuration |
Parameter Configuration |
Report Config |
Running the report displays the oldFlag prompt. Putting in 0 and 1 will toggle the result sets.
oldFlag Prompt |
Results |
No comments:
Post a Comment