Tuesday, April 7, 2009

SQL Server 2008 Row Constructors

SQL Server 2008 Row Constructors

SQL Server 2008 come with “Row Constructor”. It helps to write more compact code in less time.
With the use of Row Constructors its possible to insert some rows in a table using a single INSERT instruction:

INSERT INTO dbo.Customers(custid, companyname, phone, address)
VALUES
(1, 'cust 1', '(111) 111-1111', 'address 1')
,(2, 'cust 2', '(222) 222-2222', 'address 2')
,(3, 'cust 3', '(333) 333-3333', 'address 3')
,(4, 'cust 4', '(444) 444-4444', 'address 4')
,(5, 'cust 5', '(555) 555-5555', 'address 5');



In aggiunta a questa possibilità, now VALUES clause also works with other commands. In simple words, its possible to rewrite some SELECt with UNION operator as a single SELECT…VALUES:

SELECT *
FROM
(VALUES
(1, 'cust 1', '(111) 111-1111', 'address 1')
,(2, 'cust 2', '(222) 222-2222', 'address 2')
,(3, 'cust 3', '(333) 333-3333', 'address 3')
,(4, 'cust 4', '(444) 444-4444', 'address 4')
,(5, 'cust 5', '(555) 555-5555', 'address 5')
) AS C(custid, companyname, phone, address);

No comments: