简体   繁体   English

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

[英]How is a stored procedure processed by Sql Server and .Net

I have been using a stored procedure for more than 1.5 years. 我使用存储过程超过1。5年。 But I've never considered how data is retrieved from the UI or within another stored procedure. 但我从未考虑过如何从UI或其他存储过程中检索数据。

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. C#如何将此结果导入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. 每当我必须在另一个过程中使用此过程的结果时,我认为我们必须使用table数据类型创建一个表值参数,并将其结果分配给表变量。 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? 如果上面的示例代码为true,那么使用上述方法和查询将记录插入临时表之间有什么区别?

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. 似乎将结果复制到临时表需要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. 但我会尽力改进。

For an beginer to intermediate level you should always consider #temp tables and @table variables two faces of the same coin. 对于初级到中级,您应该始终考虑#temp表和@table变量两个相同硬币的面。 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. 唯一的主要区别是@table变量不会被事务处理,因此不会受到回滚的影响。

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. 如果深入研究细节,#temp表的处理成本稍高(因为它们是交易的),但另一方面,@ table变量只有变量范围的生命周期。

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 将过程的结果集添加到表(真实表,#temp表或@tabel变量,无关紧要)只能通过使用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 . Erland Sommarskog详细分析了程序之间共享数据的主题,请参阅如何在存储过程之间共享数据

A select means "return data to client". select意味着“将数据返回给客户”。 C# is a client, therefore it gets the data. C#是一个客户端,因此它获取数据。
Then again, it's not exactly C# that does it, it's ADO.NET. 再说一遍,它不是C#,它是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. 有一个数据提供程序知道如何使用网络/内存/其他协议与SQL服务器通信并读取它生成的数据流。 This particular client (ADO.NET) uses the received data to construct certain classes, such as DataTable, other providers can do something completely different. 这个特定的客户端(ADO.NET)使用接收到的数据来构造某些类,例如DataTable,其他提供者可以做一些完全不同的事情。
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. 所有这些都与SQL Server级别无关,因为就服务器而言,数据已经使用建立连接的协议发送出去,就是这样。

From inside, it doesn't make much sense to have a stored procedure return simply select ed data to anything else. 从内部来看,让存储过程返回只是select ed数据到其他任何东西都没有多大意义。
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. 当您需要这样做时,您可以明确地告诉SQL Server您想要什么,例如将数据插入到两个相关SP可用的临时表中,将数据插入传递给过程的表值参数中,或重写存储过程作为返回表的函数。

Then again, it's not exacly clear to me what you were asking about. 再说一次,我对你所询问的内容并不是很清楚。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM