21
votes

Apparently, I can't use them. I'm getting an error message like:

Invalid use of a side-effecting operator 'SELECT' within a function

If I want to do something like this:

select bleh
  into #temp
  from Blah

... inside a function.

2
You could use a table variable instead.Joe Stefanelli

2 Answers

38
votes

No, per this thread where the same question was asked, you cannot, but you can use a table variable

DECLARE @MyTempTableVariable TABLE (SCHEMA)

INSERT INTO @MyTempTableVariable
SELECT bleh
FROM bleh
2
votes

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.