Brent Arias
Brent Arias

Reputation: 30155

Declare a Table Variable Based on Select Statement

I want to declare a table variable and fill it with a select, without having to explicitly define its columns. Does T-SQL allow something like this:

DECLARE @people TABLE() SELECT * FROM Persons;

Hypothetically, the above statement would match column types identically, and fill the @people table variable at the same time. :)

Upvotes: 9

Views: 8690

Answers (1)

Joe Stefanelli
Joe Stefanelli

Reputation: 135729

You can't do it with a table variable since a variable has to be declared before it can be used, but you could use a temp table instead.

SELECT * INTO #people FROM Persons;

Upvotes: 16

Related Questions