Microsoft introduced table variables in SQL Server, table variable is used instant of Temporary Tables.
In Table variables we can store our records; the declaration of syntax is very similar to a CREATE TABLE statement.
Example:
Declare @customersvar Table(
Id int identity(1,1),
customerID nchar(5) NOTNULL,
Name varchar(50) ,
Address varchar(max) ,
PhoneNo varchar(50) )
We can write the following INSERT INTO statement to insert values in the table variable.
Insert into @customervar Table (customerID, Name, Address, PhoneNo )
Select customerID, Name, Address, PhoneNo from Customers
We can write the following SELECT statement to populate the table variable.
Select * from @customersvar
And to populate the table variables first fifty values, you can write the following SELECT statement
Select top 50 * from @customersvar
When we create a temporary table (#TABLE) , which physically creates the table in tempdb so it is creates burden . When we create a table variable which is creating in memory so it's much faster.
And we can use table variables when creating batches, stored procedures, and user-defined functions (UDFs).
And also you can UPDATE records in your table variable as well as DELETE records.
Example:
UPDATE @customersvar SET Name = ‘Reema’ WHERE customerID = 158
DELETE FROM @customersvar WHERE customerID = 1020
Monday, November 9, 2009
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment