If I have a function that returns a temporal table, how do I set a var in SQL Server that saves it?
If the result of the function is something like:
temp table
item1 item2...itemn
1 2... n
Is this valid?
SELECT @table = * FROM dbo.SOME_FUNCTION()
??
If this is possible, could you make an example, of how to use the @table var for knowing values?
If not possible, how can I access the temp table that the function returns??
If the function is returning a table, you should be able to have:
You’d need to specify the columns you’re going to be using for either the INSERT or SELECT portion of the statement.
UPDATE:
Here’s something I’ve come up with so you can find out the columns. The first part is enabling your server so it can return and use stored procs as you would a normal table (check with your DBA or whoever runs the server that they’re ok with these options being turned on). You select your resultset into a temp table, then run the
sp_columnsstored proc intempdband insert that into another temp table (which you then drop). That way, you at least know what columns you’ve got, and can possibly use that to make it easier to work with dynamic column names perhaps.ANOTHER UPDATE:
If you want to get the list of columns for a table in the database, you could use this query:
However! If you’re going to be running this query and creating tables of varying sizes many times a day, I would suggest that you stick with temp tables. That way you won’t be writing unnecessarily to the database (except tempdb). If for some reason you NEED permanent tables created each time, then, I guess that’s fine too!