CP.
CP.

Reputation: 47

Problems with Table Type in SQL Server 2008

In working on passing a table type around, I’m running into a strange issue that I’m hoping is something simple that I can get help with. My table type looks like this:

CREATE TYPE [dbo].[StoreTableType] AS TABLE(
[StoreGUID] [uniqueidentifier] NULL
 )

I have a stored proc that I pass this to that contains a group by query to show some totals by market. But here’s where it gets interesting… if I run the query in the SQLEM, it works, but if it calls the stored proc, it returns nothing. Here’s what I run to get a result:

declare @p3 dbo.StoreTableType
insert into @p3 values('121A267F-F994-4B01-8318-9E307AF9415B')
insert into @p3 values('B2BB61CE-5ED8-4C91-ADB7-DA903B6D506A')
insert into @p3 values('5818BA65-A1B5-432E-BF76-68EF51635A39')

select * from survey where storeguid in (select StoreGUID from @p3)

select m.[Name] as Market, count(svy.SurveyGUID) as Total
from survey svy inner join store s on svy.StoreGUID = s.StoreGUID
inner join Market m on s.MarketID = m.MarketID
where s.StoreGUID in (select StoreGUID from @p3)
group by m.[Name]

This will give me:

enter image description here

Now, if I run the statement from my code, SQL Profiler shows me that it generates the following:

 declare @p3 dbo.StoreTableType
 insert into @p3 values('121A267F-F994-4B01-8318-9E307AF9415B')
 insert into @p3 values('B2BB61CE-5ED8-4C91-ADB7-DA903B6D506A')
 insert into @p3 values('5818BA65-A1B5-432E-BF76-68EF51635A39')

 exec sp_executesql N'snus_MarketTotals',N'@StoreGUIDs [StoreTableType]      READONLY',@StoreGUIDs=@p3

However, when I run this, I get:

enter image description here

The group by query in the proc is the same as the one I tested in SQLEM. Anyone have any ideas? I’m about ready to bail out on the table type… but wanted to see if I’m just overlooking something.

UPDATE: Here's the code that makes the call.

        // EF <hates> table type parameters... so... gonna put some SQL in here.
        SqlConnection conn = new SqlConnection(connectionString);
        DataSet ds = new DataSet(datasetName);

        using (SqlCommand cmd = new SqlCommand(spName, conn))
        {
            SqlParameter param = new SqlParameter("@StoreGUIDs", SqlDbType.Structured);
            param.Value = storeGuids;
            param.TypeName = "StoreTableType";
            cmd.Parameters.Add(param);

            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            adapter.Fill(ds);

        }

        // get some xml
        return(ds.GetXml());

And for the proc:

CREATE PROCEDURE [dbo].[MarketTotals]
(
    @StoreGUIDs StoreTableType readonly
)
AS
begin

-- market totals
select m.[Name] as Market, count(svy.SurveyGUID) as Total
from survey svy inner join store s on svy.StoreGUID = s.StoreGUID
    inner join Market m on s.MarketID = m.MarketID
where s.StoreGUID in (select StoreGUID from @StoreGUIDs)
group by m.[Name]

Upvotes: 2

Views: 983

Answers (1)

anon
anon

Reputation:

I suspect that you are getting sp_executesql because you've declared your CommandType as something other than StoredProcedure. What you are seeing in Profiler does not seem right.

Upvotes: 1

Related Questions