I have the stored procedure to which I pass the parameters. These parameters are indicated by another tool. One of the parameter has a list of entities like C200
, C010
etc.
But the requirement is that the person who will run the stored procedure from another tool (Fluence) should be able to call by each entity but also to retrieve the data related to all the entities.
I have the SQL code shown here, which perfectly works if you choose one entity at a time. In the Where
clause, I filter it based on the @Entitygroup
which is Declared
. From another tool to fetch the all the Entity
is passed at Total_group
parameter name.
ALTER PROCEDURE [DW].[SP_Fetch_Data]
@par_FiscalCalendarYear varchar(10),
@par_Entity AS varchar (10)
AS
BEGIN
/*
BALANCE ACCOUNTS
*/
DECLARE @FiscalCalendarYear int = SUBSTRING(@par_FiscalCalendarYear,1,4) /* 2022 */
, @FiscalCalendarMonth int = SUBSTRING (@par_FiscalCalendarYear,7,10) /* 11 */;
DECLARE @FiscalCalendarPeriod int = @FiscalCalendarYear * 100 + @FiscalCalendarMonth
DECLARE @Entitygroup varchar = @par_Entity
SELECT UPPER([GeneralJournalEntry].SubledgerVoucherDataAreaId) as [Entity]
, CONCAT(@FiscalCalendarYear, ' P', @FiscalCalendarMonth) as [Date]
, ISNULL(ConsolidationMainAccount, '') as [Account]
, [GeneralJournalAccountEntry].TransactionCurrencyCode as [Currency]
, SUM([GeneralJournalAccountEntry].TransactionCurrencyAmount) as [Amount]
, 'Import' as [Audit]
, 'TCUR' as [DataView]
, ISNULL([CostCenter].[GroupDimension], 'No Costcenter') as [CostCenter]
, 'No Group' as [Group]
, ISNULL([Intercompany].[DisplayValue], 'No Intercompany') as [Intercompany]
, 'Closing' as [Movement]
, ISNULL([ProductCategory].[GroupDimension], 'No ProductCategory') as [ProductCategory]
, ISNULL([Region].[GroupDimension], 'No Region') as [Region]
, ISNULL([SalesChannel].[GroupDimension], 'No SalesChannel') as [SalesChannel]
, 'Actual' as [Scenario]
FROM [D365].[GeneralJournalAccountEntry]
LEFT JOIN [D365].[GeneralJournalEntry] ON [GeneralJournalAccountEntry].GENERALJOURNALENTRY = [GeneralJournalEntry].[RECID]
AND [GeneralJournalAccountEntry].[PARTITION] = [GeneralJournalEntry].[PARTITION]
LEFT JOIN [D365].[FiscalCalendarPeriod] ON [GeneralJournalEntry].FiscalCalendarPeriod = FiscalCalendarPeriod.FiscalCalendarPeriod
LEFT JOIN [DW].[MainAccounts] ON [GeneralJournalAccountEntry].MainAccount = [MainAccounts].[RECID]
LEFT JOIN [DW].[Intercompany] ON [GeneralJournalAccountEntry].[RECID] = [Intercompany].[RECID]
LEFT JOIN [DW].[ProductCategory] ON [GeneralJournalAccountEntry].[RECID] = [ProductCategory].[RECID]
LEFT JOIN [DW].[Region] ON [GeneralJournalAccountEntry].[RECID] = [Region].[RECID]
LEFT JOIN [DW].[SalesChannel] ON [GeneralJournalAccountEntry].[RECID] = [SalesChannel].[RECID]
LEFT JOIN [DW].[CostCenter] ON [GeneralJournalAccountEntry].[RECID] = [CostCenter].[RECID]
WHERE [EnumItemName] IN ('Revenue', 'Expense', 'BalanceSheet', 'Asset', 'Liability')
AND [FiscalCalendarPeriod].FiscalCalendarPeriodInt <= @FiscalCalendarPeriod
AND [GeneralJournalEntry].SubledgerVoucherDataAreaId <= @Entitygroup
GROUP BY UPPER([GeneralJournalEntry].SubledgerVoucherDataAreaId)
, ISNULL(ConsolidationMainAccount, '')
, [GeneralJournalAccountEntry].TransactionCurrencyCode
, ISNULL([CostCenter].[GroupDimension], 'No Costcenter')
, ISNULL([Intercompany].[DisplayValue], 'No Intercompany')
, ISNULL([ProductCategory].[GroupDimension], 'No ProductCategory')
, ISNULL([Region].[GroupDimension], 'No Region')
, ISNULL([SalesChannel].[GroupDimension], 'No SalesChannel')
(would be a mess as a comment)
Do you mean if @par_entity is not null and has a value other than '' then use that else go on as if it is not there at all? Then you can change your code:
DECLARE @Entitygroup varchar = @par_Entity
To:
DECLARE @Entitygroup varchar = case
when @par_Entity is not null then @par_Entity
else ''
end;
And:
AND [GeneralJournalEntry].SubledgerVoucherDataAreaId <= @Entitygroup
To:
AND (@Entitygroup = '' OR [GeneralJournalEntry].SubledgerVoucherDataAreaId <= @Entitygroup)
PS: Performance wise it wouldn't be optimal.
EDIT: You might also set it to the possible max value when it is not passed. ie:
DECLARE @Entitygroup varchar = case
when @par_Entity is null or @par_entity = '' then 'zzzzzz'
else @par_Entity
end;
AND [GeneralJournalEntry].SubledgerVoucherDataAreaId <= @Entitygroup
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.