我们可以从函数返回临时表而不是表变量吗?
我有一个函数...其中我将返回类型作为表变量... 但是如果我们使用临时表,性能将会提高......因为我们有更多的数据。
I have a function...in which I have return type as table variable...
But performance will be increased if we use temp tables..as we have more data.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不,您不能“返回”临时表 - 您可以在调用函数之前创建该临时表,并让您的函数将数据写入该临时表。
但这往往会变得相当混乱……您需要确保在调用该函数之前创建了临时表……
您是否真的、诚实地测量了该函数的性能两种方法??我不认为临时表总是更快......使用表变量的方法对我来说似乎更干净、更直观。
No, you cannot "return" a temp table - you can create that temp table before calling your function, and have your function write data into that temp table.
But this has a tendency to get rather messy .... you need to make sure to have the temp table created before calling the function.....
Have you really, honestly measured the performance of the two approaches?? I don't think temp tables are just always faster.... the approach with the table variable seems a lot cleaner and more intuitive to me.
您当然不能返回临时表。但您可以在调用存储过程中定义它。请记住,这仅适用于存储过程。您无法从函数内部访问临时表。
You cannot of course return the temp table. But you can define it in the calling stored procedure. Bear in mind this will only work for stored procedures. You cannot access temporary tables from within functions.
您可以使用返回实际表的 CLR 表值函数。 CLR 函数是您可以用母语 C#、VB 等编写的函数,并且 dll 嵌入在数据库中。
从这里了解更多信息:
http://msdn.microsoft.com/en-我们/library/ms131103.aspx
You can use CLR Table Valued functions that returns the actual table. CLR functions are functions you can write in your native language C#, VB etc. and the dll are embedded in the database.
Learn more about it from here:
http://msdn.microsoft.com/en-us/library/ms131103.aspx
这涵盖了几乎所有方式的优点/缺点和示例代码:
如何在存储过程之间共享数据,作者:Erland Sommarskog< /a>
this cover just about every way with pros/cons and sample code:
How to Share Data Between Stored Procedures by Erland Sommarskog