[英]Stored Procedure with Datatable as param returns nothing
I have set up a stored procedure which I am passing a data table into and calling directly from Entity Framework. 我已经建立了一个存储过程,该过程将数据表传递到实体框架中并直接从中调用。
I have created a Type with the following sql: 我用以下sql创建了一个Type:
CREATE TYPE Regions AS TABLE
( RegionId int,
Region varchar(max),
BodyId int NULL,
Body varchar(max),
AreaId int NULL,
Area varchar(max),
Location varchar(max),
LocationId int
)
My test stored procedure is as follows: 我的测试存储过程如下:
CREATE PROCEDURE [dbo].[GetStats]
@regions dbo.Regions READONLY
AS
BEGIN
SELECT * INTO #tmptble from @regions
Select * from #tmptble
END
I am using the following to call the stored procedure: 我正在使用以下方法来调用存储过程:
SqlParameter param = new SqlParameter();
param.SqlDbType = SqlDbType.Structured;
param.TypeName = "dbo.Regions";
param.Value = myDataTable;
param.ParameterName = "@regions";
return _context.Database.SqlQuery<RegionDetails>("GetStats", param);
My datatable is definitely the correct format as I have run this through profiler passing it in to the stored procedure and you can see all the inserts appearing. 我的数据表绝对是正确的格式,因为我已经通过探查器将其传递到存储过程中,并且可以看到所有插入物出现了。 If I generate a test table from all the insert statements the procedure runs fine against that but when I run it with the passed in datatable it just returns no rows. 如果我从所有插入语句生成测试表,则该过程可以正常运行,但是当我使用传入的数据表运行该表时,它仅不返回任何行。
EDIT - for further info When I run this through profiler I get the following: 编辑-有关更多信息,当我通过探查器运行此程序时,将得到以下信息:
declare @p3 dbo.Regions
~~~a Load of insert statements of all my datatable data~~~
exec sp_executesql N'GetStats',N'@regions [dbo].[Regions] READONLY',@regions =@p3
UPDATE on the above I have been playing around with what is shown in profiler and if I replace 上面的更新我一直在分析探查器中显示的内容,如果我替换
exec sp_executesql N'GetStats',N'@regions [dbo].[Regions] READONLY',@regions =@p3 exec sp_executesql N'GetStats',N'@ regions [dbo]。[地区] READONLY',@ regions = @ p3
with 与
EXEC GetStats @p3 EXEC GetStats @ p3
Again it works. 再次有效。 Has anyone got any clue why? 有人知道为什么吗?
Try this approach: 试试这个方法:
//create parameter
var param = new SqlParameter("@regions", SqlDbType.Structured);
param.Value = myDataTable;
param.TypeName = "dbo.Regions";
//return result set
return _context.ExecuteFunction<RegionDetails>("dbo.Regions", param);
//OR
//execute stored procedure for inserts, returns rows effective
return _context.Database.ExecuteSqlCommand("exec dbo.Regions @regions", param);
Answer based on @SteveD's answer but to clarify exactly what I did incase it helps any one else. 答案基于@SteveD的答案,但要弄清楚我所做的一切,以防其他人帮忙。 There was nothing wrong with my stored procedure. 我的存储过程没有任何问题。 It was purely my calling of it. 纯粹是我的要求。 It needed the parameter name in the actual call like so: 它需要在实际调用中使用参数名称,如下所示:
SqlParameter param = new SqlParameter();
param.SqlDbType = SqlDbType.Structured;
param.TypeName = "dbo.Regions";
param.Value = myDataTable;
param.ParameterName = "@regions";
return _context.Database.SqlQuery<RegionDetails>("GetStats @regions", param);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.