SQL Server 2008 : T-SQL Enhancement | SansSQL

Thursday, November 25, 2010

SQL Server 2008 : T-SQL Enhancement

As we all know, SQL Server 2008 came up with a huge number of changes which are helpful for both developers as well as DBA's. In this post I will be demonstrating about once such enhancement that is very helpful for data insertion.

The usual syntax for inserting data to a table is 

Insert into tbl_TSQLEnhancement values (1,'Usual','SansSQL')
Insert into tbl_TSQLEnhancement values (2,'Usual','Blog')
Insert into tbl_TSQLEnhancement values (3,'Usual','T-SQL')
Insert into tbl_TSQLEnhancement values (4,'Usual','SQL Server 2008')

If we are inserting multiple records at a time then we will have to write statements as shown above. But from SQL Server 2008 onwards, it is made easy by giving an option to insert more than one record in a single T-SQL statement as shown below.

Insert into tbl_TSQLEnhancement values
       (1,'New','SansSQL')
      ,(2,'New','Blog')
      ,(3,'New','T-SQL')
      ,(4,'New','SQL Server 2008')

Cool feature right?
So now it's time to try it out yourself.

/* Create Table */
Create Table tbl_TSQLEnhancement
(Id int not null,
Method nvarchar(10),
Name nvarchar(20))

/* Insert Data to table using Usual method */
Insert into tbl_TSQLEnhancement values (1,'Usual','SansSQL')
Insert into tbl_TSQLEnhancement values (2,'Usual','Blog')
Insert into tbl_TSQLEnhancement values (3,'Usual','T-SQL')
Insert into tbl_TSQLEnhancement values (4,'Usual','SQL Server 2008')

/* Insert Data to table using New method */
Insert into tbl_TSQLEnhancement values
       (1,'New','SansSQL')
      ,(2,'New','Blog')
      ,(3,'New','T-SQL')
      ,(4,'New','SQL Server 2008')

/* Retrive Data */
Select * from tbl_TSQLEnhancement

/* Cleanup Process */
Drop table tbl_TSQLEnhancement

Ads