Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TSQL Declare a variable in tsql without defining columns explicitly

Tags:

tsql

is it possible to declare a variable in tsql without defining columns explicitly ?

Something like

declare @tab as select * from myTable
like image 602
Tony Avatar asked Oct 18 '11 13:10

Tony


People also ask

How do I DECLARE a variable in a SQL table?

To declare a table variable, start the DECLARE statement. The name of table variable must start with at(@) sign. The TABLE keyword defines that used variable is a table variable. After the TABLE keyword, define column names and datatypes of the table variable in SQL Server.

How do you assign a column value to a variable in SQL?

The syntax for assigning a value to a SQL variable within a SELECT query is @ var_name := value , where var_name is the variable name and value is a value that you're retrieving. The variable may be used in subsequent queries wherever an expression is allowed, such as in a WHERE clause or in an INSERT statement.

What are the advantages when using a variable in T SQL?

Advantages of table variables. Table variables don't require locking and logging resources, nor do they have to be stored in a database (although see hinit below). For this reason, they will run more quickly than temporary tables.


1 Answers

You can select into a temp table ... which looks like it will do what you're after.

select * 
into #myTempTable
from myTable
like image 148
John MacIntyre Avatar answered Jan 03 '23 17:01

John MacIntyre