TechBubbles

T-SQL TIP in SQL Server 2008

You love this if you write a lot of T-SQL code. This tip I can say as  T-SQL language syntax enhancement. The following are the enhancements in T-SQL

  1. Now We can declare and initialize the variables with single statement.

 

  2. Compound assignment operators are supported.

  3. You can build multiple lines data inline with Row Constructors.

 

Examples:

Variable Assignment

CREATE TABLE EMP(EMPId int, EMPName varchar(150))
GO
-- Declare variable and assign a value in a single statement
DECLARE @EMPId int = 5

 

Compound Assignment Operator

-- Use compound assignment operator to increment ID values
-- to 6, 7, and 8
UPDATE EMP
 SET EMPId += 1

Row Constructors

-- Insert multiple rows in a single statement with 
--IDs 5, 6, and 7
DECLARE @EMPID int = 5;
INSERT INTO EMP VALUES(@EMPId, 'Nick'),
 (@EmpId + 1, 'David'), (@EmpId + 2, 'Major')

Share this post :

Related Posts:

No comments

No comments yet. Be the first.

Leave a reply