currency names of the FX rates or the connection between the FX rates
Hi Everyone.
I am running an SQL query, but I am now encountering an issue with currency conversions. The only thing my table contains is the column with the currency name, but it is not very helpful because the FX rates table only contains the ID. I would like to know where I can find the FX rate IDs and names in the query, or if there is an intermediary table to accomplish this. The tables I am currently using are: XFW_PLP_Register and XFW_PLP_Plan.
Hi Marco
Since this information does not exist in a table. The only option available other than your CASE statement would be to get creative and do something with your SQL in a Business Rule instead. For example, if you move your logic to a Business Rule e.g. Dashboard DataSet. You have a bit more flexibility with how you can reference things e.g. using a dictionary. Below is an untested example of how you can do this.
Here we create a placeholder NVARCHAR column called Currency in our SQL query. We then loop through the DataTable result and use a Dictionary (this time the ID is the Key and the Name is the Value) to lookup the Name based on the SourceCurrencyId and populate the Currency column with the corresponding Currency Name
This should give you same result, without that big CASE statement.
' Get Currency Dictionary Dim currencyDict As Dictionary(Of Integer, String) = Currency.GetItems().ToDictionary(Function(kvp) kvp.Id, Function(kvp) kvp.Name) ' Declare SQL Dim sql As String = "WITH CTE AS ( SELECT fxt.[UniqueID] ,fxt.Name ,fx.SourceCurrencyId ,'Unknown' Currency ,fx.Amount FROM [FxRateType] fxt LEFT JOIN [FxRate] fx ON fxt.UniqueID = fx.FxRateTypeID WHERE ( Month(GETDATE()) = 1 AND Name LIKE '%' + CAST((Year(GETDATE()) -1) AS NVARCHAR(4)) + 'M2%' ) OR ( Month(GETDATE()) > 1 AND Name LIKE '%' + CAST(Year(GETDATE()) AS NVARCHAR(4)) + 'M2%' ) ) SELECT SourceCurrencyId, Currency, Amount FROM CTE" 'Execute Query on App DB Dim dt As New DataTable() Using dbConnApp As DBConnInfo = BRApi.Database.CreateApplicationDbConnInfo(si) dt = BRApi.Database.ExecuteSql(dbConnApp, sql.ToString(), True) If Not dt Is Nothing Then ' prepare placeholder column dt.Columns("Currency").ReadOnly = False dt.Columns("Currency").MaxLength = -1 ' Loop on rows For Each dr As DataRow In dt.Rows 'Process rows dr.BeginEdit ' Lookup Currency Name from Dictionary by passing in SourceCurrencyId DataRow and assign value to Currency column dr("Currency") = currencyDict.Item(dr("SourceCurrencyId")) dr.EndEdit Next End If End Using