Feeds:
Posts
Comments

Archive for March, 2015

SET NOEXEC is one of my favorite SET statements. I am sure, when I explain it in detail you will love it too. Mostly, we come across in situations when we need to execute batch SQL statements (insert, update, delete) on production databases for the deployment / troubleshooting etc. Sometimes due to the syntax error, some parts of the batch statements execute and the remaining DO NOT and it creates the mess/headache for you to correct the data. And you are left with no option, but to restore the latest backup and fix the script and execute it again.

The solution for this problem is SET NOEXEC. It actually compiles the SQL Query and validate the syntax. The best/fun part is that it DOES NOT EXECUTE the SQL Query. So even if SQL Query is having errors, it DOES NOT affect the data and you are SAFE.

Let me create a sample to demonstrate, how it works.
Sample :

USE AdventureWorks2014
GO
--DROP TABLE tbl_sample
--GO
--Create a sample table
CREATE TABLE tbl_sample
(
 [ID] INT,
 [Letter] VARCHAR(1)
)
GO
--Insert a record in the table
INSERT INTO tbl_sample VALUES (1,'A')
GO
--Browse the data
SELECT * FROM tbl_sample
GO

NoEXEC.1.1

Now, let me turn on the NOEXEC and execute few statements and view the result-set.

USE AdventureWorks2014
GO
-- SET NOEXEC to ON
SET NOEXEC ON;

--Update the table
UPDATE tbl_sample SET [Letter]='B' WHERE [ID]=1
GO
--Delete the table
DELETE FROM tbl_sample  WHERE [ID]=1
GO
-- SET NOEXEC to OFF
SET NOEXEC OFF;
GO

NoEXEC.1.2

The output shows Command(s) completed successfully. It seems, that it not ONLY compiles but executes as well. Lets browse and check the impact of above queries on the data.

USE AdventureWorks2014
GO
--Browse the data to check the impact.
SELECT * FROM tbl_sample
GO

NoEXEC.1.1

As you can see above, the SQL Statements (Update & Delete) DID NOT IMPACT sample table data because NOEXEC was TURNED ON. Hence, it is PROVED that it ONLY compiles the SQL Statements and gives you either an error or Command(s) completed successfully message but it DOES NOT execute SQL Statements.

Note : Once you compiled the statement, DO NOT FORGET to turn NOEXEC off.

Read Full Post »

Paging became quite simpler & easy to script and manage by using OFFSET & FETCH NEXT keywords in SQL Server 2012 & above. I have written quite a detailed article earlier about it and implemented it in my most of the solutions wherever required. However, when you implement/use paging in your script, you face a big challenge, that is, to find the total number of records in that particular result-set.

Given below are the three methods which you can use to get the total row count from OFFSET / FETCH NEXT.
Before proceeding with the solutions, let me create a sample.

Sample :

USE AdventureWorks2014
GO
-- Create Sample Table Table
CREATE TABLE [dbo].[SalesOrderDetail](
[SalesOrderID] [int] NOT NULL,
[SalesOrderDetailID] [int] NOT NULL,
[CarrierTrackingNumber] [nvarchar](25) NULL,
[OrderQty] [smallint] NOT NULL,
[ProductID] [int] NOT NULL,
[SpecialOfferID] [int] NOT NULL,
[UnitPrice] [money] NOT NULL,
[UnitPriceDiscount] [money] NOT NULL,
[LineTotal] [numeric](38, 6) NOT NULL,
[rowguid] [uniqueidentifier] NOT NULL,
[ModifiedDate] [datetime] NOT NULL
) ON [PRIMARY]
GO

-- Insert bulk data into sample table
-- It may take few minutes depends upon the server performance
INSERT INTO [dbo].[SalesOrderDetail]
SELECT * FROM [SALES].[SalesOrderDetail]
GO 100

-- Verfiy the data
Select * from [dbo].[SalesOrderDetail]
GO

Method 1 : Using COUNT(*) OVER()

USE AdventureWorks2014
GO
DECLARE
@PageSize INT = 10,
@PageNum  INT = 1;

SELECT
[SalesOrderID]
, [SalesOrderDetailID]
, [CarrierTrackingNumber]
, [OrderQty]
, [ProductID]
, [SpecialOfferID]
, [TotalCount]= COUNT(*) OVER()
FROM [dbo].[SalesOrderDetail]
ORDER BY SalesOrderID
OFFSET (@PageNum-1)*@PageSize ROWS
FETCH NEXT @PageSize ROWS ONLY;
GO
--OUTPUT

row count using Offset 1.1

Method 2 : Using Common Table Expression

USE AdventureWorks2014
GO
DECLARE
@PageSize INT = 10,
@PageNum  INT = 1;

;WITH Main_CTE AS(
SELECT [SalesOrderID]
, [SalesOrderDetailID]
, [CarrierTrackingNumber]
, [OrderQty]
, [ProductID]
, [SpecialOfferID]
FROM [dbo].[SalesOrderDetail]
)
, Count_CTE AS (
SELECT COUNT(*) AS [TotalCount]
FROM Main_CTE
)
SELECT *
FROM Main_CTE, Count_CTE
ORDER BY Main_CTE.SalesOrderID
OFFSET (@PageNum-1)*@PageSize ROWS
FETCH NEXT @PageSize ROWS ONLY
GO
--OUTPUT

row count using Offset 1.1

Method 3 : Using Cross Apply

USE AdventureWorks2014
GO
DECLARE @PageSize INT = 10,
@PageNum  INT = 1;

SELECT
[SalesOrderID]
, [SalesOrderDetailID]
, [CarrierTrackingNumber]
, [OrderQty]
, [ProductID]
, [SpecialOfferID]
, [TotalCount]
FROM [dbo].[SalesOrderDetail]

CROSS APPLY (SELECT COUNT(*) TotalCount
FROM [dbo].[SalesOrderDetail] ) [Count]
ORDER BY SalesOrderID
OFFSET (@PageNum-1)*@PageSize ROWS
FETCH NEXT @PageSize ROWS ONLY
GO
--OUTPUT

row count using Offset 1.1

All of the above methods give you the same result-sets. Lets view their performance given below.

S.No

Method

CPU Time

Elapsed Time

1

COUNT(*) OVER()

30654 ms

40372 ms

2

Common Table Expression

11762 ms

7665 ms

3

Cross Apply

11794 ms

7373 ms

Conclusion :
On the basis of above results, I would recommend that you either use Common Table Expression or Cross Apply to get the faster results.

Note : The above queries have been tested on ~12 Million records.

Read Full Post »

Yesterday (24 March 2015), I had the opportunity to present Extended Events at SQL Server User Group meet-up. This event was held at Microsoft Office in Dubai, UAE. Generally this event is held every last Tuesday of the month. SQL Server User group (www.uaessug.com) have been organizing it extremely well for the last one year. This meet up usually consists of two comprehensive and informative sessions that focus on the SQL Server latest technology, best practice, user experience, tips & tricks. I usually attend this meet up because I get an opportunity to network with SQL Server experts face to face instead of Facebook chat :). I was the speaker in yesterday’s meet up along with Mr. Mike Mohan (CFO at Microsoft).

Following topics were covered in yesterday’s session:

  • Real Life Power BI presented by Mr. Mike Mohan – Microsoft
  • Introduction to Extended Events was by me

We focused on how a non technical user can make best use of Power BI & its integration on different devices, best practices and troubleshooting (the scariest part) throughout the event and hands on with live demos to demonstrate the practical implementations of Power BI & Extended Events.

I hope yesterday’s session was informative as usual. Our next meetup will be held in April 2015. Hope the coming sessions would witness more participants.

Some glimpses of the session:

Mr. Mike busy explaining Power BI :

IMG_0079_U

He showed us how to generate a power map and its auto zoom feature :

IMG_0080_U

Showed us, how to combine many graphs in one page :
IMG_0083_U

Displaying look n feel of Power BI on multiple devices :

IMG_0092_U

Me presenting SQL Server Extended Events :

IMG_9654_U

Showing the components of EE :
IMG_9656_U

IMG_9657_U

Thank you guys. See you next month.

Read Full Post »