I need to switch from using a #temp table to a @table variable so that I can use it in a function.
My query uses insert into #temp (from multiple tables) like so:
SELECT
a.col1,
a.col2,
b.col1...
INTO #temp
FROM ...
Is there an easy way to find out the data types of the columns in the #temp table so that I can create the @table variable with the same columns and data types as #temp?
You need to make sure
sp_helpruns in the same database where the table is located (tempdb). You can do this by prefixing the call directly:Or by prefixing a join against
tempdb.sys.columns:This doesn’t handle nice things for you, like adjusting max_length for varchar differently from nvarchar, but it’s a good start.
In SQL Server 2012 or better, you can use a new DMF to describe a resultset, which takes that issue away (and also assembles max_length/precision/scale for you). But it doesn’t support #temp tables, so just inject the query without the INTO: