SQL 是否可以在函数中包含临时表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9844854/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to have temp tables in a function?
提问by aarona
Apparently, I can't use them. I'm getting an error message like:
显然,我不能使用它们。我收到如下错误消息:
Invalid use of a side-effecting operator 'SELECT' within a function
在函数中无效使用副作用运算符“SELECT”
If I want to do something like this:
如果我想做这样的事情:
select bleh
into #temp
from Blah
... inside a function.
...在一个函数内。
回答by Justin Pihony
No, per this thread where the same question was asked, you cannot, but you can use a table variable
不,根据提出相同问题的这个线程,您不能,但您可以使用table variable
DECLARE @MyTempTableVariable TABLE (SCHEMA)
INSERT INTO @MyTempTableVariable
SELECT bleh
FROM bleh
回答by Lew Wolf
You can also do it with a CTE. See the template browser in SSMS. IntelliSense confuses the issue and will show an error until you complete the CTE and the following insert/select, but it will work.
您也可以使用 CTE 来完成。请参阅 SSMS 中的模板浏览器。IntelliSense 会混淆该问题,并会在您完成 CTE 和以下插入/选择之前显示错误,但它会起作用。