Sql Server和.Net如何处理存储过程

发布于 2024-09-14 01:14:18 字数 750 浏览 4 评论 0原文

我使用存储过程已经超过 1.5 年了。但我从未考虑过如何从 UI 或另一个存储过程中检索数据。

当我编写一个简单的存储过程时。 例如。

CREATE PROCEDURE sp_test
AS
BEGIN
 SELECT * FROM tblTest --Considering table has 3 columns.
END

C# 如何将此结果存入 DataTable。

每当我必须在另一个过程中使用此过程的结果时,我认为我们必须使用表数据类型创建一个表值参数,并将其结果分配给表变量。我从来没有尝试过。

CREATE PROCEDURE sp_testcall
AS
BEGIN
 @temp = exec sp_test -- I think this would be the way, never tried
END

如果上面的示例代码是正确的,那么使用上面的方法和查询向临时表插入记录有什么区别呢?

CREATE PROCEDURE sp_test
AS
BEGIN
 SELECT * INTO #tmp FROM tblTest --Considering table has 3 columns.
END

看来将结果复制到临时表中需要 sql server 的另一项工作。 但幕后会发生什么呢?它会直接将结果的引用分配给表值参数还是使用与临时表相同的过程?

我的问题可能不太清楚。但我会努力改进。

I have been using a stored procedure for more than 1.5 years. But I've never considered how data is retrieved from the UI or within another stored procedure.

When I write a simple stored procedure.
eg.

CREATE PROCEDURE sp_test
AS
BEGIN
 SELECT * FROM tblTest --Considering table has 3 columns.
END

How does C# gets this result into DataTable.

Whenever I have to use the result of this procedure in another procedure, I think we have to create a table valued parameter using the table datatype and assign its result to a table variable. I've never tried it.

CREATE PROCEDURE sp_testcall
AS
BEGIN
 @temp = exec sp_test -- I think this would be the way, never tried
END

If the above sample code is true, then what is the difference between using the above method and a query to insert records into a temporary table?

CREATE PROCEDURE sp_test
AS
BEGIN
 SELECT * INTO #tmp FROM tblTest --Considering table has 3 columns.
END

It would seem that copying the result into a temporary table requires another effort by sql server.
But what would be going on behind the scenes? Would it directly assign references of the result into a table valued parameter or does it use the same process as a temporary table?

My question might not be clear. But I will try to improve.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

薄凉少年不暖心 2024-09-21 01:14:18

对于初级到中级水平,您应该始终将 #temp 表和 @table 变量视为同一枚硬币的两个面。虽然它们之间存在一些差异,但出于所有实际目的,它们的成本相同并且性能几乎相同。唯一的主要区别是 @table 变量不是事务处理的,因此不受回滚的影响。

如果深入了解细节,#temp 表的处理成本稍高(因为它们是事务性的),但另一方面 @table 变量只有变量作用域的生命周期。

至于您的问题提出的其他问题:

  • 表值参数始终是只读的,您无法修改它们(插入/更新/删除它们)
  • 将过程的结果集添加到表中(真实表,#temp 表或@tabel)变量,无关紧要)只能通过使用 INSERT INTO 来完成EXEC sp_test
  • 作为经验法则,产生另一个过程所需结果的过程可能作为用户定义函数更好。Erland

Sommarskog 详细分析了过程之间共享数据的主题,请参阅如何在存储过程之间共享数据

For an beginer to intermediate level you should always consider #temp tables and @table variables two faces of the same coin. While there are some differences between them, for all practical purposes they cost the same and behave nearly identical. The sole major difference is that @table variables are not transacted and hence not affected by rollbacks.

If you drill down into details, #temp tables are slightly more expensive to process (since they are transacted) but on the other hand @table variables have only the lifetime of a variable scope.

As to other issues raised by your question:

  • table value parameters are always read only and you cannot modify them (insert/update/delete into them)
  • tacking the result set of a procedure into a table (real table, #temp table or @tabel variable, doesn't matter) can only be done by using INSERT INTO <table> EXEC sp_test
  • as a rule of thumb a procedure that produces a result that is needed in another procedure is likely to be better of as a User Defined Function

The topic of sharing data between procedures was analyzed at length by Erland Sommarskog, see How to Share Data Between Stored Procedures.

隱形的亼 2024-09-21 01:14:18

select 的意思是“将数据返回给客户端”。 C# 是客户端,因此它获取数据。
话又说回来,这并不完全是 C# 做的,而是 ADO.NET 做的。有一个数据提供者知道如何使用网络/内存/其他一些协议与 SQL 服务器通信并读取它生成的数据流。这个特定的客户端(ADO.NET)使用接收到的数据来构造某些类,例如DataTable,其他提供程序可以做完全不同的事情。
所有这些在 SQL Server 级别都是无关紧要的,因为就服务器而言,数据已使用建立连接的协议发送出去,仅此而已。

从内部来看,让存储过程将简单的选择数据返回给其他任何东西并没有多大意义。
当您需要这样做时,您可以显式地告诉 SQL Server 您想要什么,例如将数据插入到两个相关 SP 都可用的临时表中,将数据插入到传递给过程的表值参数中,或者重写您的存储过程作为返回表的函数。

话又说回来,我不太清楚你在问什么。

A select means "return data to client". C# is a client, therefore it gets the data.
Then again, it's not exactly C# that does it, it's ADO.NET. There's a data provider that knows how to use a network/memory/some other protocol to talk to the SQL server and read data streams it generates. This particular client (ADO.NET) uses the received data to construct certain classes, such as DataTable, other providers can do something completely different.
All that is irrelevant at SQL Server level, because as far as the server is concerned, the data has been sent out using the protocol with which the connection was established, that's it.

From inside, it doesn't make much sense to have a stored procedure return simply selected data to anything else.
When you need to do that, you have the means to explicitly tell SQL Server what you want, such as inserting the data into a temporary table available to both involved SPs, inserting data into a table-valued parameter passed to the procedure, or rewriting your stored procedure as a function that returns a table.

Then again, it's not exacly clear to me what you were asking about.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文