How are table variables used in stored procedure?

945    Asked by JamesScott in SQL Server , Asked on Jan 8, 2020
Answered by Rachit Gupta

Table variables declared in the same way a variable declared.

i.e. declare @EMP table

(EMP_ID INT,

EMP_NAME nvarchar (50))

The scope of table variable ends after the end of execution of batch/ procedure. Select/ insert/update/ delete can be performed as done on a normal table. But table variables can not be modified(altered) as normal tables can be done. In case you are using a table variable in joins then aliasing should be used which joins the table variable.

Query optimizer gives an excellent query plan while execution of procedure when a table variables is used in the procedure as it doesn’t contain any statistics.

Tables variables are stored in tempdb databases not in the permanent memory


Your Answer

Interviews

Parent Categories