New Features For Developers in SQL Server 2008
New Features For Developers in SQL Server 2008
There are many new features that are designed for SQL Server 2008 developers.
IntelliSense
Microsoft IntelliSense available in SQL Server 2008 provides an array of options that make language
references easily accessible. When coding, it is not needed to leave the editor to perform searches on
language elements. The developer can keep his context, find the information he needs, insert language
elements directly into his code, and even have IntelliSense complete his typing for him.
Syntax enhancements
Inline variable initialization
-- Declare and initialize variable
DECLARE @count INT = 1;
Compound Operators;
-- Compound assignments
SET @count += 1;
SET @count /= 2;
SET @count *= 5;
SET @count %= 3;
SET @count -= 1;
Row Constructors (Table Value Constructors)
Transact-SQL is enhanced to allow multiple value inserts within a single INSERT statement; it
means multiple row predicates in the VALUES clause:
INSERT INTO Foo (keycol, datacol)
VALUES (1, 'Books'), (2, 'CDs'), (3, 'DVDs');
Merge Statement
One very common requirement is to merge source data to a target table. In earlier versions of SQL
Server this was accomplished by using separate INSERT and UPDATE statements. It involved checking if
key column exists to perform update and insert if not, or attempt an update first and then if not rows
were affected perform insert. Not to mention if we have to check if account is missing from the branch
office data and needs to be deleted from the central accounts table. That way we end up with multiple
(sometimes complex) statements to implement one transaction, accessing both source and target tables
multiple times.
SQL Server 2008 offers a lot more elegant way using the MERGE statement. MERGE statement allows
the developer to check for the existence of data before trying to insert the data. This check prior to
performing the INSERT statement allows the data to be updated. No longer is it necessary to create
complex joins in order to update data that exists and to insert data that does not already exist, all during
a single statement. MERGE statement also improves the performance of database as it passes through
data only once.
-- Update existing that changed and add missing
-- Use of predicates
MERGE INTO CentralOfficeAccounts AS C
-- Target
USING BranchOfficeAccounts AS B
-- Source
ON C.account_nbr = B.account_nbr
WHEN MATCHED
-- On match update
AND (C.company_name <> B.company_name -- Additional search conditions
OR C.primary_contact <> B.primary_contact
OR C.contact_phone <> B.contact_phone) THEN
UPDATE SET C.company_name = B.company_name,
C.primary_contact = B.primary_contact,
C.contact_phone = B.contact_phone
WHEN NOT MATCHED THEN
-- Add missing
INSERT (account_nbr, company_name, primary_contact, contact_phone)
VALUES
(B.account_nbr,
B.company_name,
B.primary_contact,
B.contact_phone);
Table-Valued Parameters
Table-valued parameters allow sending a table as parameter to the stored procedure:
/* Create a table type. */
CREATE TYPE LocationTableType AS TABLE
( LocationName VARCHAR(50)
, CostRate INT );
GO
/* Create a procedure to receive data for the table-valued parameter.
*/
CREATE PROCEDURE usp_InsertProductionLocation
@TVP LocationTableType READONLY
AS
SET NOCOUNT ON
INSERT INTO [AdventureWorks].[Production].[Location]
([Name]
,[CostRate]
,[Availability]
,[ModifiedDate])
SELECT *, 0, GETDATE()
FROM @TVP;
GO
SQL Server 2008 enhances LINQ by providing a new LINQ to SQL provider that allows developers to issue
LINQ commands directly against SQL Server tables and columns. This will reduce the amount of time it
takes to create new data queries. LINQ to SQL should not be used due to the inability of production
DBAs to control code, its security and performance, or to effectively troubleshoot potential issues.