It is a confusing thing where temp tables and table variables are stored, Most of the people think that table variables are stored in memory and temp tables stored in tempdb. However both are saved and executed from tempdb. Lets check the below example.
CREATE TABLE #Temptable(Cust_Name VARCHAR(50))
DECLARE @TABLE_VARIABLE AS TABLE(Cust_Name VARCHAR(50))
SELECT *
FROM tempdb.sys.objects
WHERE CAST(create_date AS DATE) = CAST(GETDATE() AS DATE)
From the example its clear that both are created in temp db or disk, the first object is created for temp table and second one is for table variable. In terms of performance, I choose table variable, because its more efficient than temp table and also it is automatically dropped after execution.
No comments:
Post a Comment