SQL
SQL
SQL
GO
/****** Object: StoredProcedure [dbo].[sp_jasper_reports] Script Date:
11/2/2023 10:52:28 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
/*
--
===================================================================================
===========================================
--
-- Object Name : [dbo].[sp_jasper_reports]
-- Author : true
-- Date :
-- Test Script : EXEC [sp_jasper_reports]
-- Description : Populate Reports for Jasper DB
-- Last Modified Changes : Populate Version in Jasper Tables
-- Prerna Bharti, 13 Mar 2018
-- Last Modified Changes : Changes for Dynamic Reporting Table
-- Prerna Bharti, 18 Jan 2019
-- Last Modified Changes : Changes for Dynamic Reporting Table at same and
different server
-- Prerna Bharti, 16 Apr 2019
-- Last Modified Changes : Changes for Standard Reporting and Transpose
--
--
===================================================================================
===========================================
*/
BEGIN TRY
SET XACT_ABORT ON
DECLARE @Reporting_Period DATE
DECLARE @Eligibility_ID NVARCHAR(MAX)
DECLARE @Roster_ID NVARCHAR(MAX)
DECLARE @Alignment_ID NVARCHAR(MAX)
DECLARE @Component_Name NVARCHAR(MAX)
DECLARE @Component_Type NVARCHAR(MAX)
DECLARE @Measure_Table NVARCHAR(MAX)
DECLARE @Report_Type NVARCHAR(MAX)
DECLARE @col NVARCHAR(MAX)
DECLARE @mes NVARCHAR(MAX)
DECLARE @sql NVARCHAR(MAX)
DECLARE @Reporting_Year NVARCHAR(100)
DECLARE @business_unit NVARCHAR(2000)
DECLARE @Reporting_Month NVARCHAR(100),@Reporting_Semester
VARCHAR(10),@Reporting_Quarter VARCHAR(10),@Reporting_Week VARCHAR(10)
DECLARE @Type_Table NVARCHAR(2000)
DECLARE @geo_label NVARCHAR(100)
DECLARE @emp_label NVARCHAR(100)
DECLARE @role_list NVARCHAR(1000)
DECLARE @measureid NVARCHAR(1000)
DECLARE @schemaName NVARCHAR(1000)
DECLARE @measureinstanceid NVARCHAR(1000)
DECLARE @Year NVARCHAR(100)
DECLARE @Month NVARCHAR(100)
DECLARE @Version NVARCHAR(100)
DECLARE @reportingDBPrefix NVARCHAR(100)
DECLARE @ReportSchemaName NVARCHAR(100)
DECLARE @CurrentDBname NVARCHAR(100)
DECLARE @Quarter INT
DECLARE @TenantFlag INT
DECLARE @count INT
DECLARE @i INT
DECLARE @success_count INT
DECLARE @row INT
DECLARE @schema_count INT
DECLARE @isMasterDynamic BIT
DECLARE @isComponentDynamic BIT
DECLARE @Header_Flag BIT
DECLARE @ServerConfigFlag BIT
DECLARE @InputType NVARCHAR(100)
DECLARE @DatasetID NVARCHAR(100)
DECLARE @InstanceID NVARCHAR(100)
DECLARE @Parent_component_Name NVARCHAR(400)
DECLARE @Report_Table_name NVARCHAR(500)
DECLARE @Where_Clause NVARCHAR(max)
Declare @Report_Design Nvarchar(200)
Declare @Transposed_Count int
Declare @mes_Transposed Nvarchar(Max)
Declare @mes_UnTransposed Nvarchar(Max)
Declare @frequency Nvarchar(Max)
Declare @sql_query Nvarchar(Max)
Declare @periodid Nvarchar(Max)
-----------------------------------master
table-----------------------------------------------------
--get the current database name
SELECT @CurrentDBname = DB_NAME()
-----------------------------------------------------------------------
-----------------------------
--------------- to get the translated month name for
Mexico-----------------------------------------
-----------------------------------------------------------------------
-----------------------------
--IF ((@Report_Type = 'Score Card' OR @Report_Type = 'ScoreCard' OR
@Report_Type = 'ScoreCards') AND @TenantFlag = 2)
--BEGIN
--SELECT @Reporting_Month = UPPER(Month_Italian)
--FROM Month_name
--WHERE Month_English = DATENAME(MONTH, @Reporting_Period)
--END
-----------------------------------------------------------------------
----------------------------------
--Take only those components for which Measure is attached
--get the component name and measure attached to each report component
-----------------------------------------------------------------------
----------------------------------
INSERT INTO #component_detail (
component_name
,component_type
,measure_table
,isComponentDynamic
,inputtype
,datasetid
,instanceid
,Parent_Component_Name
,Report_Design
,Current_Mtable
,Report_Table_name
,Where_Clause
,Seq
)
SELECT DISTINCT Component_Name
,component_type
,CASE WHEN input_type = 'Measure' then 'ic_output.' +
current_Mtable
WHEN input_type = 'Input data' then 'dbo.' + current_Mtable
ELSE current_Mtable
END
,ISNULL(isDynamic,0)
,input_type
,Dataset_Id
,Instance_Id
,Parent_Component_Name
,@Report_design
,Current_Mtable
,Report_name
,filter_clause
,Seq
FROM REPORT_COMPONENT_MAPPING
WHERE reportid = @Report_ID
AND Current_Mtable IS NOT NULL AND Current_Mtable != 'None'
AND isS3='false' order by Seq -----to remove those components for
which no measure is attached
SET @schema_count = 0
SET @row = 0
SET @success_count = 0
SET @i = 1
IF @schema_count = 1
BEGIN
SELECT @schemaName = SchemaName
FROM t_report_bu_mapping
WHERE report_type = @report_type
AND BU = 'All'
END
ELSE
BEGIN
SELECT @schemaName = SchemaName
FROM t_report_bu_mapping
WHERE report_type = @report_type
AND BU = @business_unit
END
----------------------update Status of
t_ho_reports_master-------------------
UPDATE t_ho_reports_master
SET error_log = NULL
,STATUS = 'Processing'
,Summary = NULL
WHERE Report_ID = @Report_ID
-----------------------------------------------------------------------
-------------------
-----------------------Delete previous records from Jasper
table--------------------------
-----------------------------------------------------------------------
-------------------
END
ELSE IF(@isMasterDynamic = 1) OR (@isComponentDynamic = 1)
BEGIN
PRINT '*****Delete Dynamic Report Table Data *****'
PRINT @component_name
SET @Type_Table = @ReportSchemaName
+'.t_'+@component_name+'_'+@Report_ID
SET @Type_Table = REPLACE(@Type_Table, ' ','_')
SET @sql = 'IF OBJECT_ID('''+ @Type_Table +''',''U'') IS
NOT NULL DROP TABLE '+ @Type_Table
PRINT @Sql
--to execute the command at linked server if its on
different db
IF @ServerConfigFlag = 1
BEGIN
PRINT '----- @ServerConfigFlag is 1 -----'
EXECUTE (@Sql) AT Linked
END
ELSE
BEGIN
PRINT '---- @ServerConfigFlag is 0 ----'
EXECUTE (@Sql)
END
END
SET @i = @i + 1
END
BEGIN
print 44
SET @sql_query = 'DELETE FROM '+@ReportSchemaName+
'.T_SD_Published_Report_Dimension WHERE Report_ID =''' + @Report_ID+''''
print(@sql_query)
EXECUTE (@sql_query)
END
end
-----------------------------------------------------------------------
-------------------------------
-------------------------Loop to insert records into Jasper
DB----------------------------------------
-----------------------------------------------------------------------
-------------------------------
SET @i = 1
WHILE (@i <= @count)
BEGIN
PRINT '********Component*********'
PRINT @i
PRINT '**************************'
WHERE id = @i
AND a.Measure_table = b.MeasureOutputTable
AND a.inputtype = 'Measure'
Print ('Testing')
Print @Where_Clause
print '-------Atul--------'
print @count
print @i
print @Component_Name
IF(@isMasterDynamic = 0) AND (@isComponentDynamic = 0)
BEGIN
IF (@Report_Design='Standard' and @Report_Type<> 'Standad
Data Mart')
BEGIN
SET @Type_Table =
@ReportSchemaName+'.'+@Report_Table_name
END
ELSE IF(@Report_Type='Payroll Report')
BEGIN
SET @Type_Table =
@ReportSchemaName+'.'+@Report_Table_name
END
ELSE
BEGIN
SELECT @Type_Table =
ISNULL(MAX(TableName),'Destination_Table')
FROM T_REPORT_TABLE_DETAILS
WHERE report_type = @Report_type
AND isnull(Component_Type, 'a') =
isnull(@Component_name, 'a')
AND isnull(Parent_Component_Name, 'a') =
isnull(@Parent_Component_name, 'a')
and isnull(Report_design, 'a') =
isnull(@Report_design, 'a')
AND SchemaName = @schemaName
END
END
ELSE IF(@isMasterDynamic = 1) OR (@isComponentDynamic = 1)
BEGIN
SET @Type_Table = @ReportSchemaName
+'.t_'+@component_name+'_'+@Report_ID
END
print @Report_design
-----------------------------------------------------------------------------------
--------
PRINT 'emp Label ========= '
PRINT @emp_label
PRINT 'Col =============== '
PRINT @col
PRINT 'Type Tables ======== '
PRINT @Type_Table
PRINT 'Mes =============== '
PRINT @mes
PRINT 'Report ID ========= '
PRINT @Report_ID
PRINT 'MeasureTab ======== '
PRINT @measure_table
PRINT 'ReportingMonth ==== '
PRINT @Reporting_Month
PRINT 'Month ============= '
PRINT @Month
PRINT 'Year ============== '
PRINT @Year
PRINT 'Report Type ======= '
PRINT @Report_Type
PRINT 'ReportingYear ==== '
PRINT @Reporting_Year
PRINT 'Version =========== '
PRINT @Version
PRINT 'component_name ==== '
PRINT @component_name
PRINT '@isComponentDynamic==='
PRINT @isComponentDynamic
PRINT 'parent_component_name==='
PRINT @parent_component_name
BEGIN
PRINT '*************Col Prepare*****************'
BEGIN
SELECT @col = COALESCE(@col, '') + ',' +
'"' + MappedColumn3 + '"'
,@mes = COALESCE(@mes, '') + ',' + CASE
WHEN a.format LIKE '€%'
THEN 'case when ' +
replace(formated_metric, MappedColumn, 'a.' + MappedColumn) + '=''€'' then ''€0''
else ' + replace(formated_metric, MappedColumn, 'a.' + MappedColumn) + ' end'
WHEN Metric_Dataformat LIKE 'Percentage'
AND ( a.format IS NOT NULL and
a.format !='None')
THEN 'case when ' +
replace(formated_metric, MappedColumn, 'a.' + MappedColumn) + '=''%'' then ''0%''
else ' + replace(formated_metric, MappedColumn, 'a.' + MappedColumn) + ' end'
WHEN a.format LIKE '$%'
THEN 'case when ' +
replace(formated_metric, MappedColumn, 'a.' + MappedColumn) + '=''$'' then ''$0''
else ' + replace(formated_metric, MappedColumn, 'a.' + MappedColumn) + ' end'
WHEN Metric_Dataformat = 'number'
AND ( a.format IS NOT NULL and
a.format !='None')
THEN 'case when ' +
replace(formated_metric, MappedColumn, 'a.' + MappedColumn) + '='''' then ''0''
else ' + replace(formated_metric, MappedColumn, 'a.' + MappedColumn) + ' end'
When isnull(istranspose,0)=1
And ( a.format IS NULL or a.format
='None')
THEN replace(formated_metric,
MappedColumn, 'cast(a.' + MappedColumn+ ' as Nvarchar(4000))')
ELSE replace(formated_metric,
MappedColumn, 'a.' + MappedColumn)
END + ' ' + 'as' + ' ' + case when
@transposed_count>0 then '"' + Metric + '"' else '"' + MappedColumn + '"' end
FROM REPORT_COMPONENT_METRIC_MAPPING a
WHERE a.REPORT_ID = @Report_ID
AND isnull(a.component_name, 'a') =
isnull(@component_name, 'a')
AND isnull(Parent_Component_type,
'a') = isnull(@Parent_Component_name, 'a')
SELECT @mes_Untransposed =
COALESCE(@mes_Untransposed, '') + ',' + '"' + Metric + '"'
, @col = COALESCE(@col, '') + ',' + '"' +
MappedColumn3 + '"'
FROM REPORT_COMPONENT_METRIC_MAPPING a
WHERE a.REPORT_ID = @Report_ID
AND isnull(a.component_name, 'a') =
isnull(@component_name, 'a')
AND isnull(Parent_Component_type,
'a') = isnull(@Parent_Component_name, 'a')
AND a.mappedcolumn <> 'none'
AND isnull(IsTranspose,0)=0
ORDER BY a.Sequence
SELECT @mes_transposed =
COALESCE(@mes_transposed, '') + ',' + '"' + Metric + '"'
FROM REPORT_COMPONENT_METRIC_MAPPING a
WHERE a.REPORT_ID = @Report_ID
AND isnull(a.component_name, 'a') =
isnull(@component_name, 'a')
AND isnull(Parent_Component_type,
'a') = isnull(@Parent_Component_name, 'a')
AND a.mappedcolumn <> 'none'
AND isnull(IsTranspose,0)=1
ORDER BY a.Transposed_Sequence
PRINT @mes_transposed
END
-----------------------------------------------------------------------------------
----
IF @mes_Transposed<>''
SET @mes_Transposed = right(@mes_Transposed,
len(@mes_Transposed) - 1)
IF @mes_Untransposed<>''
SET @mes_Untransposed = right(@mes_Untransposed,
len(@mes_Untransposed) - 1)
IF @COL<>''
SET @col = right(@col, len(@col) - 1)
IF @mes<>''
SET @mes = right(@mes, len(@mes) - 1)
SET @row = 0
print @Report_Type
print 'atul'
---------------------------------------Insert Statement
Prepare--------------------------------
IF @isComponentDynamic != 1 ---NON Dynamic
BEGIN
IF @Report_Design='Standard'
BEGIN
print '12345'
print @Report_Type
IF @Transposed_Count>0
BEGIN
print(@sql)
END
ElSE IF (@Report_Type = 'Standard Data Mart')
begin
print '1234'
print @col
print @mes
if @Type_Table like '%Dimension%'
BEGIN
IF (@InputType != 'Input
data')
END
ELSE
BEGIN
IF (@InputType != 'Input data')
SET @sql = 'INSERT INTO ' +
@Type_Table + ' (' + @col + ',report_id) SELECT ' + @mes + ' ,''' + @Report_ID +
''' FROM ' + @measure_table + ' a '+@Where_Clause
ELSE
SET @sql = 'INSERT INTO ' +
@Type_Table + ' (' + @col + ',report_id) SELECT ' + @mes + ' ,''' + @Report_ID +
''' FROM ' + @measure_table + ' a where (a.ObjectId='''+@DatasetID +''' and
a.InstanceId='''+@InstanceID+''''+')'+replace(@Where_Clause ,'where',' and ')
END
end
ElSE
BEGIN
IF (@InputType != 'Input data')
SET @sql= 'INSERT INTO ' + @Type_Table + ' (' +
@col + ',report_id) select distinct ' + @mes + ' ,''' + @Report_ID + ''' FROM ' +
@measure_table + ' a '+@Where_Clause
ELSE
SET @sql= 'INSERT INTO ' + @Type_Table + ' (' +
@col + ',report_id) select distinct ' + @mes + ' ,''' + @Report_ID + ''' FROM ' +
@measure_table + ' a where (a.ObjectId='''+@DatasetID +''' and
a.InstanceId='''+@InstanceID+''''+')'+replace(@Where_Clause ,'where',' and ')
END --Standard
END
IF (@Where_Clause != '' )
SET @Where_Clause = @Where_Clause +
' and a.'+@ReportName+' = ''True'''
ELSE
SET @Where_Clause = @Where_Clause +
' where a.'+@ReportName+' = ''True'''
print 'Where Clause---'
print @Where_Clause
PRINT @sql
END
ElSE IF (@Report_Type = 'Performance Summary Report')
BEGIN
print 'Performance Summary Report------'
select @ReportName = items from
Split_String(@ReportName,'(') where t_pkey = 1
IF (@Where_Clause != '')
SET @Where_Clause = @Where_Clause + ' and
a.'+@ReportName+' = ''True'''
ELSE
SET @Where_Clause = @Where_Clause + '
where a.'+@ReportName+' = ''True'''
PRINT @sql
END
ELSE IF(@isMasterDynamic = 1)
BEGIN
IF (@InputType != 'Input data')
SET @sql='select '+ @mes+' INTO
'+@Type_Table+' FROM '+@measure_table+' a '+@Where_Clause
ELSE
SET @sql='select '+ @mes+' INTO
'+@Type_Table+' FROM '+@measure_table+' a where (a.ObjectId='''+@DatasetID +''' and
a.InstanceId='''+@InstanceID+''''+')'+replace(@Where_Clause ,'where',' and ')
END
ELSE IF @isComponentDynamic = 1
BEGIN
PRINT '*******Dynamic Reports*********'
else BEGIN
IF @ServerConfigFlag = 1
BEGIN
ELSE
SET @sql = 'INSERT INTO '+@Type_Table+' SELECT
'+@mes+' FROM Linked.'+ @CurrentDBname + '.' +@measure_table + ' A WHERE
(a.ObjectId='''+@DatasetID +''' and a.InstanceId='''+@InstanceID+''''
+')'+replace(@Where_Clause ,'where',''' and ')
print '--------------------------------------------'
PRINT @sql
Execute(@sql) At Linked
END
ELSE
BEGIN
DECLARE @createCommand1 NVARCHAR(MAX);
DECLARE @minSeq1 INT;
DECLARE @maxSeq1 INT;
DECLARE @columnname1 NVARCHAR(MAX);
DECLARE @metricType1 NVARCHAR(MAX);
DECLARE @columnValue1 NVARCHAR(MAX);
print '-------start1----'
PRINT @sql
print '-------start2----'
EXECUTE (@sql)
END
END
--For non-dynamic reports execute normal query
ELSE
BEGIN
PRINT 'For non-dynamic reports'
PRINT @sql
EXECUTE (@sql)
END
SET @i = @i + 1
END
-----------------------------------------------------------------------
----------------------------------
----------------------------------------------------LOOP
end---------------------------------------------
-----------------------------------------------------------------------
----------------------------------
-----------------------------------------------------------------------
------------
----Calling Header Insertion Proc for Mexico Tenant
-----------------------------------------------------------------------
------------
IF (@TenantFlag = 2)
BEGIN
--Calling payout insertion stored proc
IF (@Report_Type = 'Score Card' OR @Report_Type = 'ScoreCard' OR
@Report_Type = 'ScoreCards')
BEGIN
EXEC payout_insertion @Report_ID
END
-----------------------------------------------------------------------
------------
--Updating Status of t_ho_reports_master
-----------------------------------------------------------------------
------------
BEGIN
PRINT 'success count = '
PRINT @success_count
UPDATE t_ho_reports_master
SET STATUS = CASE
WHEN @success_count = 0
THEN 'Failed'
ELSE 'Pushed'
END
WHERE report_id = @report_id
END
SET @Success_Flag = 1
END TRY
--END -- remove
-----------------------------------------------------------------------------
--------------
------------------------------------Catch
Block--------------------------------------------
-----------------------------------------------------------------------------
--------------
BEGIN CATCH
PRINT '******Catch Block*******'
PRINT @report_type
UPDATE t_ho_reports_master
SET error_log = @Error_Message
,STATUS = 'Failed'
,summary = isnull(@component_type, '-') + ' -' + @Error_Message
WHERE Report_ID = @Report_ID
SET @Success_Flag = 0
SELECT
@ErrorMessage = ERROR_MESSAGE(),
@ErrorSeverity = ERROR_SEVERITY(),
@ErrorState = ERROR_STATE();
RAISERROR (@ErrorMessage, -- Message text.
11, -- Severity.
@ErrorState -- State.
);
END CATCH
EXECUTE (@sqlmax)
END