Can we store OUTPUT Results into a Temp Table without creating Temp table

Sudip Bhatt 2,276 Reputation points
2020-09-18T18:11:21.37+00:00

it is possible to store data into temp table without creating temp table.

this way we can do it. select * into #tmp1 from sourcetable

so when we store OUTPUT Results into temp table then is it possible to do it without creating temp table.

see a example
DECLARE @Updated table( [DepartmentID] int,
[Name] varchar(50),
[GroupName_old] varchar(50),
[GroupName_new] varchar(50),
[ModifiedDate_old] datetime,
[ModifiedDate_new] datetime);
Update [Department_SRC]
Set [GroupName]='IT',
ModifiedDate=Getdate()
OUTPUT deleted.DepartmentID, deleted.Name, deleted.GroupName as GroupName_old,
inserted.GroupName as GroupName_new, deleted.ModifiedDate as ModifiedDate_old,
inserted.ModifiedDate as ModifiedDate_new
INTO @Updated
Where [Name]='Engineering'
--Querying from @Updated output table
Select * from @Updated

in this example first create table variable and then store data there. i like to store data into temp table from OUTPUT resultset without creating temp table. if it is possible then please show me the approach. thanks

Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,597 questions
0 comments No comments
{count} votes

Accepted answer
  1. Erland Sommarskog 106.5K Reputation points
    2020-09-18T22:09:23.557+00:00

    No, it is not possible. When you use the OUTPUT clause, you need to create the table in advance.

    Which I think is best practice in most cases anyway. I may use SELECT INTO when I'm writing something on the fly, but if it is code that is to be in a stored procedure, I use CREATE TABLE.

    0 comments No comments

0 additional answers

Sort by: Most helpful