SQL Server 2008中的表类型问题

时间:2012-02-23 17:23:49

标签: sql-server sql-server-2008

在处理传递表类型的过程中,我遇到了一个奇怪的问题,我希望这是一个简单的问题,我可以得到帮助。我的表格类型如下:

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

我有一个存储过程,我将其传递给包含按查询分组的组以按市场显示一些总计。但是这里有趣的地方......如果我在SQLEM中运行查询,它可以工作,但如果它调用存储过程,它什么也不返回。这是我为了得到结果而跑的地方:

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]

这会给我:

enter image description here

现在,如果我从我的代码运行语句,SQL事件探查器会告诉我它生成以下内容:

 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

然而,当我运行时,我得到:

enter image description here

proc中的group by查询与我在SQLEM中测试的相同。有人有主意吗?我准备好在桌子上拯救......但是想看看我是不是只是忽略了什么。

更新:这是拨打电话的代码。

        // 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());

对于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]

1 个答案:

答案 0 :(得分:1)

我怀疑您收到sp_executesql,因为您已将CommandType声明为StoredProcedure以外的其他内容。您在Profiler中看到的内容似乎不正确。