T-SQL Enhancements in 2008
T-SQL Enhancements in 2008
Every new version of SQL Server comes with several T-SQL features and enhancements.
SQL Server 2008 is no exception. In the newest version of SQL Server, Microsoft has
responded to developer requests in a big way. SQL Server 2008 supports brand new
developer convenience features like single statement declaration and initialization, new
data types to manipulate and query date-based and spatial data, new statements like
MERGE that add much needed functionality to T-SQL and a whole lot more. Once
you’ve made the move to SQL Server 2008, you’ll undoubtedly want to take advantage
of the new features and functionality to solve common problems.
The first of these new features is single statement variable declaration and
initialization. In the past, developers had to declare new variables in one or more
DECLARE statements and assign values to the variables in one or more SET or
SELECT statements as shown in below Listing.
With single statement declaration and initialization, you can combine these two steps
into a single step as shown in below Listing
T-SQL has further improved with the addition of several C-Style assignment
operators.
Operator Description
+= Addition with assignment, or string
concatenation with assignment
-= Subtraction with assignment
*= Multiplication with assignment
/= Division with assignment
%= Modulo with assignment
Ex:
DECLARE @x int=4, @y int=25, @s1 varchar(20)=’Sql’
SET @x *= @y
SET @s1+= ‘SERVER’
SYNTAX:
INSERT INTO TABLE_NAME
VALUES
(VALUE1, VALUE2,……),
(VALUE1, VALUE2,…..),
(VALUE1,VALU2,……..)
EX:
Like in SQL Server 2005 we need not to write multiple INSERT statements for multiple
records. With in single INSERT statement we can insert multiple records into the table at
a time
4.MERGE Statement
This Merge statement takes Target and Source Tables, When Source rows are
matched with Target it will update the rows of source table into target table otherwise
it will insert the rows of source table into target table. Always merge statement takes a
temp table as its source and base table as its target table. Using this Merge statement
we can combine the rows of a source table to target table.
Syntax:
SQL Server 2008 provides new data types that support storage, manipulation and
querying of new forms of data. Some, like the date data type which stores a simple
date with out the time component. Others like the geometry data type, which allows
storage and querying of spatial data, have only recently addressed.
SQL Server supports four brand new date and time data types, which includes
1. date
2. time
3. datetime2
4. datetimeoffset
The date data type finally allows us to store date only without the time component.
It can also store a much larger range of dates than datetime and smalldatetime data
types.
The date data type can handle dates from January 1, 1 CE(0001-01-01) to December
31, 9999 CE(9999-12-31)
Ex:
Ex:
The cleverly named datetime2 data type is an extension to the standard datetime
data type. The datetime2 combines the benefits of the new date and time data
types, giving you the wide date range of the date data type and the greater
fractional second precision of the time data type.
Ex:
The new datetimeoffset data type builds on datetime2 by adding the ability to
store offsets relative to the International Telecommunication Union(ITU)
standard for Coordinated Universal Time(UTC) with your date and time data
type.
The hierarchyid data type offers to represent hierarchical data in the database. This
new data type offers built-in support for representing your hierarchical data using one
of the simplest models available: materialized paths
Hierarchyid Data Type Methods
Method Description
GetAncestor(n) Retrieves the nth ancestor of the
hierarchyid node instance
GetDescendent(n) Retrieves the nth descendent of the
hierarchyid node instance
GetLevel() Gets the level of the hierarchyid node
instance in the hierarchy
GetRoot() Gets the root node of the hierarchyid
instance in the hierarchy
IsDescendent(node) Returns 1 if a specified node is descendent
Parse(String) Converts the given canonical string, in
forward slash- separated format to a
hierarchyid path.
Reparent(old_root,new_root) Reparents a node by moving nodes from
old_root to new_root
ToString() Converts a hierarchyid instance to a
canonical forward slash-separated string
representation
SQL Server 2008 includes two new data types for storing, querying, and manipulating
spatial data.
Geometry
Geography
Conclusion
www.microsoft.com/sql/default.mspx
http://msdn2.microsoft.com/en-us/library/bb543165(sql:100).aspx
You can get the latest BOL updates from the Microsoft update site
http://update.microsoft.com/microsoftupdate