Pass Table Valued Parameters to Stored procedure using Bridge Parameter

Hi,
We are trying to build a dashboard from cube that is processed by stored procedure with table value parameters

Table value parameters
CREATE TYPE CarTableType AS TABLE
(
Id int primary key,
Name nvarchar(50),
company nvarchar(50)
)
Go

Store Procedure
CREATE PROCEDURE spInsertCars
@CarType CarTableType READONLY
AS
BEGIN
SELECT count(1) FROM @CarType
END

Based on distinct selection, we need to report the count in dashboard using bridge parameters.
We can return comma separated string using Bridge parameter but unable to return table/collection.
Would you able to suggest the approach here please ?

Hi Ric,

I have built table functions (in MS SQL) in order to deal with the parsing of the strings passed to the Stored Procedure, I have one for parsing integers (Ids) separated by a comma.
As the string (containing the Ids) can also contain “(none)” or “All” or “(All)” depending on the selection in Dundas, you’ll have to deal with those values in the query.
Here is the function:
Create function [dbo].[ExtractIdTable](@ChaineCsv nvarchar(MAX), @Separateur varchar(2) = ‘,’)
returns @tElement table (Element int)
as
begin
declare @Position int, @Element nvarchar(MAX)
If (@ChaineCsv = ‘Null’ or @ChaineCsv = ‘(none)’ or @ChaineCsv = ‘All’ or @ChaineCsv = ‘(All)’)
Begin
return
End
while len(@ChaineCsv) > 0
begin
set @Position = charindex(@Separateur, @ChaineCsv, 1)
if @Position = 0
begin
set @Element = @ChaineCsv
set @Element = Ltrim(RTrim(@Element))
set @ChaineCsv = ‘’
end
else
begin
set @Element = left(@ChaineCsv, @Position - 1)
set @Element = Ltrim(RTrim(@Element))
set @ChaineCsv = substring(@ChaineCsv, @Position + 1, Len(@ChaineCsv) - @Position)
end
insert into @tElement (Element) values (CAST(@Element AS int))
end
return
end

This will return you a table that you can use for filtering.
SELECT Table1.* FROM Table1
WHERE IIF(@TheStringContainingTheIds = ‘All’, 1=1,IIF(@TheStringContainingTheIds = ‘(All)’, 1=1, IIF(@TheStringContainingTheIds = ‘(none)’, Table1.TheForeignIdYouNeedToFilterOn = NULL, Table1.TheForeignIdYouNeedToFilterOn IN (SELECT Temp.[Element] FROM [dbo].[ExtractIdTable(@TheStringContainingTheIds, ‘,’) AS Temp))))

In Dundas, you assign a Hierarchy on the Bridge Parameter that corresponds to the table your Foreign Key is pointing to, and set this code on the Bridge Parameter:
CollectionMemberValue memberValue = (CollectionMemberValue)$input$.Resolve();
var commaDelimitedString = string.Empty;
//Pass “All” string if user selected “All” checkbox or “All” token
if (memberValue.Token != null && memberValue.Token == PredefinedToken.All)
{
commaDelimitedString = “All”;
}
else
{
for (int i = 0; i < memberValue.Values.Count; i++)
{
if (commaDelimitedString.Length == 0)
{
commaDelimitedString = memberValue.Values[i].UniqueName.Replace(".A", “”);
}
else
{
//concatenate all selected values to a comma-delimited string
commaDelimitedString = string.Format("{0},{1}", commaDelimitedString, memberValue.Values[i].UniqueName.Replace(".A", “”));
}
}
}
return new SingleStringValue($id$, commaDelimitedString);

I hope this helps,
Olivier

1 Like

Hi Olivier,
Thanks for the solution. But I think there is limitation on the length of the string which this code can return, there are in total 944 values in the hierarchy drop down, when I select all the values except for one i.e. I select 943 values , the code breaks and on the output I get the value corresponding to the unselected member. Have you anytime faced such an issue ?

Regards,
Ric

Hi Ric,

No, I have never came across such an issue. Have you tested the script set for the Bridge Parameter in the interface? You can simulate the functionment by injecting the inputs (selecting them thru a dropdown) and test the script to see what it returns directly in the interface (without passing thru the Dashboard, just at the Bridge Parameter level in the Data Cube, in the same window where you write the script under the script there is a “test script” functionality).

Regards,
Olivier

Hi Olivier,

Yes , the above scenario which I explained was simulated on the same interface which you have mentioned.

Regards,
Ric

Hi Ric,

Have you tried splitting the result into two parameters, I mean assigning two parameters in your DB Stored Procedure that will contain each a part of the result? in the code when assigning the values, just split the results with something like this:

  • for the first Stored Procedure’s string parameter:

if (Int32.Parse(memberValue.Values[i]) < TheNumericValueOfTheIdYouWantToSplitOn)
{
// perform the concatenation in the commaDelimitedString
}
else
{
// ignore this Id
}

  • for the second Stored Procedure’s string parameter
    if (Int32.Parse(memberValue.Values[i]) >= TheNumericValueOfTheIdYouWantToSplitOn)
    {
    // perform the concatenation in the commaDelimitedString
    }
    else
    {
    // ignore this Id
    }

You just need to assign the Bridge Parameter to both variable (I mean both Stored Procedure’s string parameters) and the “if” in the C# code will do the rest. in the Stored Procedure you’ll just need to add the string parameters passed for getting one that you will use in your query:
“DECLARE @JoinedStrings NVARCHAR(MAX)
if LEN(@StringParameter2) > 0
Begin
@JoinedStrings = @StringParameter1 + ‘,’ + @StringParameter2;
End
Else
Begin
@JoinedStrings = @StringParameter1;
End”

Or if it’s still too long, deal with both strings in your query by duplicating the WHERE condition and using an “OR”.

And if needed you can split into 3, 4, or more strings passed to the Stored Procedure, by basing the splitting on the numerical Id of the Hierarchy.

I hope this helps.

regards,
Olivier

Hi Olivier,
Thanks for the suggestion, the splitting option is good. We implemented the solution using Isinverted flag, wherein along with the string we are also passing if the value is Inverted i.e. “True” or not inverted i.e. “False”, based on this we have written condition in the stored proc to return the data.
Below is the link for reference.

Using a bridge parameter | Cleanse, Consolidate & Modify Data | Documentation | Learning (dundas.com)

Regards,
Tejas

Hi Ric (or should I say Tejas?),

Yes, it works also, as long as the string containing the inverted values is not too long too.

Are you Tejas from Dundas undercover? :face_with_raised_eyebrow:

Regards,
Olivier

:laughing::laughing: Actually on forums I go by Ric