Friday, November 25, 2011

SQL Server 2012 - Improved Version of EXECUTE keyword

In Previous version of SQL Server 2005/2008, if you execute the SELECT Stored Procedure using EXECUTE keyword, it will return all the records as it is. if you want to edit the column name and data type of result set, you have to modify the stored procedure respectively.

But in SQL Server 2012, improved version of EXECUTE keyword allows to modify the result set with changing the column name and data type without modifying the stored procedure.

To explore this features, execute the following script to create a table, insert the record and creating the stored procedure.

 CREATE TABLE Customers  
(
ID INT IDENTITY(1,1),
FirstName VARCHAR(50),
LastName VARCHAR(50),
DOB DATE
)
GO
INSERT INTO Customers (FirstName, LastName, DOB)
SELECT 'Ayyanar', 'Jayabalan', '08/12/1982'
UNION ALL
SELECT 'Greg', 'Damico', '08/12/1970'
GO
CREATE PROCEDURE Customers_SelectAll
AS
BEGIN
SELECT
ID,
FirstName,
Lastname,
DOB
FROM dbo.Customers
END
GO


In SQL Server 2005/2008:
 -- SQL 2005/2008  
EXECUTE Customers_SelectAll


RESULT:


In SQL Server 2012:
Here i am changing the column "id" into "CustomerId" and also changing the data type of firstname from VARCHAR(50) to VARCHAR(4). Since i reduced the length of FirstName to 4, it shows truncated firstname value.
 -- SQL 2012  
EXEC Customers_SelectAll
WITH RESULT SETS
(
(
CustomerID SMALLINT,
FirstName VARCHAR(4),
Lastname VARCHAR(20),
DateOfBirth DATE
)
);


RESULT:

SQL Server 2012 - Throwing Exception using THROW in TRY CATCH BLOCK instead of using RAISERROR

In SQL Server 2005/2008, if you want to throw an error again in catch block of TRY CATCH statement, you have to use RAISERROR with ERROR_MESSAGE(), ERROR_SEVERITY().

In SQL Server 2005/2008:
 -- EXCEPTION HANDLING in SQL Server 2005/2008  
BEGIN TRY
DECLARE @VALUE INT
SET @VALUE = 1 / 0
END TRY
BEGIN CATCH
DECLARE @Message nvarchar(4000), @Severity int
SELECT @Message = ERROR_MESSAGE(), @Severity = ERROR_SEVERITY()
RAISERROR ( @Message, @Severity, 1 )
END CATCH


SQL Server 2012:
Just, you can use THROW keyword to throw the complete error information.
 -- EXCEPTION HANDLING using THROW in SQL Server 2012  
BEGIN TRY
DECLARE @VALUE INT
SET @VALUE = 1/0
END TRY
BEGIN CATCH
THROW
END CATCH


if you execute the above statement, you will receive an error like below.
 Msg 8134, Level 16, State 1, Line 5  
Divide by zero error encountered.

Custom Paging in SQL Server 2012 - Using OFFSET

In SQL Server 2005/2008, if you have want to implement the custom paging for ASP.Net GridView or any other controls, you have use either COMMON TABLE EXPRESSIONS with ROW_NUMBER() or just ROW_NUMBER() keyword.

CUSTOM PAGING IN SQL SERVER 2005/2008:

 -- CUSTOM PAGING IN SQL SERVER 2005   
USE AdventureWorks
DECLARE @PageIndex SMALLINT, @PageSize SMALLINT, @StartRowId INT, @EndRowId INT
SET @PageIndex = 1
SET @PageSize = 10
SET @StartRowId = ( (@PageIndex - 1) * @PageSize) + 1
SET @EndRowId = (@StartRowId + @PageSize) - 1
SELECT *
FROM (
SELECT ROW_NUMBER() OVER(ORDER BY EmployeeId) AS RowId, *
FROM HumanResources.Employee) AS TT
WHERE RowId >= @StartRowId and RowId <= @EndRowId


CUSTOM PAGING IN SQL SERVER 2012 using OFFSET:
In SQL Server 2012, by using the OFFSET feature, we can implement the custom paging easily without having too many SQL statements.
Just you have to specify starting row id and page size like below.
 OFFSET <Offset> ROWS  
FETCH NEXT <PageSize> ROWS ONLY;


Here is the complete T-SQL statement for custom paging.
 -- CUSTOM PAGING IN SQL SERVER 2012 RC0  
DECLARE @PageIndex SMALLINT, @PageSize SMALLINT, @Offset INT
SET @PageIndex = 1
SET @PageSize = 10
SET @Offset = ( (@PageIndex - 1) * @PageSize)
SELECT *
FROM HumanResources.Employee
ORDER BY EmployeeId
OFFSET @Offset ROWS
FETCH NEXT @PageSize ROWS ONLY;

Tuesday, November 22, 2011

SQL Server 2012 Features - Sequence Number

Today,i have installed to explore the features of SQL Server 2012. First feature which i am going to discuss is "Sequence Number". You can generate sequence number like identity column. In Previous release, if you want to generate a sequence id before inserting into the table, you have to create a seperate physical table with identity column. Before inserting record into table, we have to insert a row in custom sequence table and get the latest value.

But in SQL Server 2012, you no need to insert record in any physical custom table to retrieve the number in sequence. Just you have to create sequence object which binds to schema.

Here are the steps to create Sequence object using SQL Server Management Studio.

1. Create a New Sequence:



2. Specify the NAME, MIN, MAX, CYCLE and OTHER PROPERTIES:



3. Once you entered the values for sequence object. You have to use following query to get NEXT sequence number using NEXT VALUE FOR keyword.

 select NEXT VALUE FOR dbo.GlobalSequence;  


RESULT:
 1  


If you execute above query, you will get the value as 1. Next time, if you executing same query, you will get the value as 2. Same way, you would get the number in sequential for every execution.

4. With CYCLE OPTION:

 select NEXT VALUE FOR dbo.GlobalSequence;  
select NEXT VALUE FOR dbo.GlobalSequence;
select NEXT VALUE FOR dbo.GlobalSequence;
select NEXT VALUE FOR dbo.GlobalSequence;
select NEXT VALUE FOR dbo.GlobalSequence;
select NEXT VALUE FOR dbo.GlobalSequence;


if you execute the above query, you would get values like below.

 1  
2
3
4
5
1


if sequence value reaches maximum value, it will start again with start value if you enabled the CYCLE option. Otherwise, it will throw an error like below;

 The sequence object 'GlobalSequence' has reached its minimum or maximum value. Restart the sequence object to allow new values to be generated.