Creating Temp Variable Table

Posted on Updated on

Using temp variables is great if you’re using it for SSRS. Why? Because SSRS does not allow temp table i.e. (#mytable, ##myothertable) but it will allow temp variables. Although SQL will discard the temp variable when it’s done with it, always explicitly drop the temp. Furthermore, if you use GO; anywhere after your declared table, the table will go bye-bye!

DECLARE @customer TABLE  (ID INT IDENTITY, FirstName VARCHAR(20), LastName VARCHAR(20))

INSERT @customer (FirstName, LastName)

SELECT ‘Chris’, ‘Bui’

SELECT * FROM @customer

DROP TABLE @customer