0

I have this stored procedure:

ALTER PROCEDURE [dbo].[uspPages_HotelPrices_Lookup_Select] 
    @HotelCode nvarchar(100)
AS
BEGIN
    SET NOCOUNT ON;

    SELECT * 
    FROM tPages_HotelPrices_Lookup 
    WHERE HotelCode IN (SELECT * FROM DBO.ufSplit(@HotelCode, ',')) 
END

DBO.ufsplit splits a comma delimited string and returns a table of which each row containing each of the comma separated values.

I am passing a string to this stored procedure with the code below:

static void Main(string[] args)
{
    HotelCodesTableAdapter hcTa = new HotelCodesTableAdapter();
    DestinationMarketingEntity.HotelCodesDataTable hotelCodesDt = hcTa.GetData();

    string hotelCodesString = "";
    //Comma separating hotel codes and putting each word in '' to be passed to sql sproc as a list
    for (int i = 0; i < hotelCodesDt.Count; i++)
    {
        hotelCodesString += hotelCodesDt.Rows[i].ItemArray.GetValue(0).ToString() + ",";
    }

    hotelCodesString = hotelCodesString.TrimEnd(',');

    HiltonEEHotelPricesTableAdapter hEETa = new HiltonEEHotelPricesTableAdapter();
    WorldWideFeedEntity.HiltonEEHotelPricesDataTable hEEDt= hEETa.GetData(hotelCodesString);
}

The last line is where the stored procedure is being called.

Essentially hotelCodesString will be similar to "1,2,3" but this is returning nothing form this stored procedure. But if I run the below:

select * 
from tPages_HotelPrices_Lookup 
  where HotelCode IN 
(
SELECT *
FROM DBO.ufSplit('1,2,3',',')
);

It gets back everything that I want. Am I missing something here? Why will it not return anything when passing from values with c#?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Srb1313711
  • 2,017
  • 5
  • 24
  • 35

2 Answers2

3

Don't do the split at all. Create a table valued parameter and pass this to your stored procedure. Then change your stored procedure to join to the table valued parameter.

Your sproc will end up looking like this:

CREATE PROCEDURE [dbo].[uspPages_HotelPrices_Lookup_Select] 
    @HotelCodes dbo.MyCodesTable READONLY
AS
BEGIN
    SET NOCOUNT ON;

    SELECT * 
    FROM tPages_HotelPrices_Lookup a 
    INNER JOIN @HotelCodes b ON (a.ID = b.ID)
END

There are lots of good examples of using table values parameters on SO and the internet. A good method to get used to.

SteveB
  • 1,474
  • 1
  • 13
  • 21
  • Thank you for your answer It is now working but I will look it to using this method instead, what is a table valued parametre? – Srb1313711 Nov 04 '13 at 16:43
  • 1
    It's a table that can be declared in both .net and sql server and it allows you to pass lists of IDs in the manner you want. At the sql end you can then join directly to the table. Have a look on the internet and find some examples - I'm currently working on a project that uses them quite a lot and I've been pleasantly surprised at how easy they are to use. Some caveats but on the whole a +ve experience. Enjoy :-) – SteveB Nov 04 '13 at 16:48
1

You can try doing the split in C# instead of at the db level.

 string[] m_separators = new string[] { "," };
 string[] m_stringarray = somestring.Split(m_separators, StringSplitOptions.RemoveEmptyEntries);

Or follow the examples on SO regarding passing an array to a stored proc. It is probably what you want to do anyway.

smoore4
  • 4,520
  • 3
  • 36
  • 55