Table Variable
This acts like a variable and exists for a particular batch of query execution. It gets dropped once it comes out of batch. This is also created in the Tempdb database but not the memory. This also allows you to create primary key, identity at the time of Table variable declaration but not non-clustered index.
- GO
- DECLARE @TProduct TABLE
- (
- SNo INT IDENTITY(1,1),
- ProductID INT,
- Qty INT
- )
- --Insert data to Table variable @Product
- INSERT INTO @TProduct(ProductID,Qty)
- SELECT DISTINCT ProductID, Qty FROM ProductsSales ORDER BY ProductID ASC
- --Select data
- Select * from @TProduct
- --Next batch
- GO
- Select * from @TProduct --gives error in next batch
No comments:
Post a Comment