Technical Insights: Azure, .NET, Dynamics 365 & EV Charging Architecture

Author: fransiscuss Page 6 of 19

Degrading performance IIS 7 with Windows Authentication and Kerberos

If you use IIS7 with windows authentication and kerberos, you might want to look into this support article from microsoft. This support article also cover IIS6

http://support.microsoft.com/kb/954873

IIF in SQL Server 2012

Now you can use IIF in SQL Server 2012 instead of CASE (again this only if you have 2 possible values that you want to evaluate)

–This will return Apple
SELECT IIF(‘APPLE’ = ‘APPLE’, ‘Apple’, ‘Banana’)

–This will return Banana
SELECT IIF(‘APPLE’ = ‘Banana’, ‘Apple’, ‘Banana’)

OFFSET FETCH in SQL Server 2012 – Paging

In SQL Server 2005, normally when we do paging we need to use CTE or nested SELECT Statement, but in SQL Server 2012 we can use OFFSET and FETCH to do the paging

Let’s start of how we normally do paging in SQL Server 2005/2008 – this will return the first 10 rows ordered by REF

WITH Fields AS
(
SELECT REF, NAME, DISPLAY, ROW_NUMBER() OVER (ORDER BY REF) AS RowNumber
FROM SU_FIELD
)
SELECT REF, NAME, DISPLAY
FROM Fields
WHERE RowNumber BETWEEN 1 AND 10
ORDER BY RowNumber ASC;

To move to next page we do

WITH Fields AS
(
SELECT REF, NAME, DISPLAY, ROW_NUMBER() OVER (ORDER BY REF) AS RowNumber
FROM SU_FIELD
)
SELECT REF, NAME, DISPLAY
FROM Fields
WHERE RowNumber BETWEEN 11 AND 20
ORDER BY RowNumber ASC;

In SQL Server 2012 – it looks much more simpler to write the query. OFFSET is used for skipping the first x rows ad FETCH NEXT is used to control how many records to be returned

–This will return 10 records without skipping any row (REF from 0 – 9)
SELECT REF, NAME, DISPLAY
FROM SU_FIELD
ORDER BY REF
OFFSET 0 ROWS
FETCH NEXT 10 ROWS ONLY;

to move to the next page

–This will return 10 records with skipping first 10 rows (REF from 10 – 19)
SELECT REF, NAME, DISPLAY
FROM SU_FIELD
ORDER BY REF
OFFSET 10 ROWS
FETCH NEXT 10 ROWS ONLY;

TRY_CONVERT in SQL Server 2012

One of the new TSQL feature in SQL Server 2012 is TRY_CONVERT, basically it’s a function that will return null if the object passed is not compatible with the expected casting. In earlier version of SQL Server we don’t have this feature which means you need to make sure your data is cast-able to the expected format

Earlier Version of SQL Server – you can run this

SELECT CONVERT(INT, ‘abc’)

and it will throw an error

Msg 245, Level 16, State 1, Line 1
Conversion failed when converting the varchar value ‘abc’ to data type int.

Then you will normally do work around with this to avoid the error which means no record will be returned when the object is not a valid data type

SELECT CONVERT(INT, ‘abc’) AS TEST WHERE ISNUMERIC(‘abc’) = 1

SQL Server 2012

SELECT TRY_CONVERT(INT, ‘abc’) AS TEST

This will still return a record but with NULL value when the data is not cast-able

SQL Server udf Join

I found some interesting article to share in regards of joining the UDF into your join which ends up in executing the UDF as many as number of records being joined to. The solution is to put it into a temp table before joining it

http://sqlblog.com/blogs/rob_farley/archive/2011/11/08/when-is-a-sql-function-not-a-function.aspx

Tracing Dynamic SQL in the SQL Profiler

The default template in SQL profiler doesn’t trace the dynamic SQL executed by the stored procedure. In order to trace the Dynamic SQL, you need to turn on these 2 options in your tracing profile

SP:stmtstarting and SQL:stmtstarting

Tips: How to find Recursive Parent-Child relationship in SQL Server

Below is the sample in how to reproduce a recursive parent-child relationship and how to find out which records are the troublesome one

CREATE TABLE RecursiveSample
(
REF INT NOT NULL,
PARENT_REF INT NOT NULL,
NAME VARCHAR(255)
)
GO
/*Inserting the parents*/
INSERT INTO RecursiveSample(REF, PARENT_REF, NAME) VALUES(1, 0, ‘Parent 1’)
GO
INSERT INTO RecursiveSample(REF, PARENT_REF, NAME) VALUES(2, 0, ‘Parent 2’)
GO
INSERT INTO RecursiveSample(REF, PARENT_REF, NAME) VALUES(3, 0, ‘Parent 3’)
GO
/*Inserting the child*/
INSERT INTO RecursiveSample(REF, PARENT_REF, NAME) VALUES(4, 1, ‘Child Parent 1’)
GO
INSERT INTO RecursiveSample(REF, PARENT_REF, NAME) VALUES(5, 2, ‘Child Parent 2’)
GO
INSERT INTO RecursiveSample(REF, PARENT_REF, NAME) VALUES(6, 3, ‘Child Parent 3’)
GO
/*Inserting the grand child*/
INSERT INTO RecursiveSample(REF, PARENT_REF, NAME) VALUES(7, 4, ‘Grand Child Parent 1’)
GO
INSERT INTO RecursiveSample(REF, PARENT_REF, NAME) VALUES(8, 5, ‘Grand Child Parent 2’)
GO
INSERT INTO RecursiveSample(REF, PARENT_REF, NAME) VALUES(9, 6, ‘Grand Child Parent 3’)
GO
/*Let’s update the record to make it recursive*/
UPDATE RecursiveSample SET PARENT_REF = 7 WHERE REF = 4
GO
/*Run this query to find the Parent child structure*/
;WITH CTE
AS(
SELECT *,[PATH]=CAST(REF AS NVARCHAR(1000))FROM RecursiveSample WHERE PARENT_REF =0
UNION ALL
SELECT A.*,[PATH]=CAST(C.[PATH]+’/’+RTRIM(C.REF) AS NVARCHAR(1000))
FROM CTE C
INNER JOIN RecursiveSample A ON C.REF=A.PARENT_REF AND PATINDEX(‘/’+RTRIM(A.REF)+’/%’,’/’+C.[PATH]+’/’)=0
)
SELECT * FROM CTE
/*
REF PARENT_REF NAME PATH

Simulate Deadlock in SQL Server

How to simulate Deadlock in SQL Server, pretty simple

1. Create a table

CREATE TABLE MyDeadlockTable
(
REF INT PRIMARY KEY NOT NULL,
NAME VARCHAR(255)
)
GO

2. Populate a table with the record
INSERT INTO MyDeadlockTable(REF, NAME) VALUES(1, ‘Record 1’)
GO
INSERT INTO MyDeadlockTable(REF, NAME) VALUES(2, ‘Record 2’)
GO

3. On the first window let’s run this
BEGIN TRAN
UPDATE MyDeadlockTable set NAME=NAME WHERE REF = 1;
WAITFOR DELAY ’00:00:15′
UPDATE MyDeadlockTable set NAME=NAME WHERE REF = 2;
COMMIT

4. Open a 2nd window let’s run this

BEGIN TRAN
UPDATE MyDeadlockTable SET NAME=NAME WHERE REF = 2;
WAITFOR DELAY ’00:00:15′
UPDATE MyDeadlockTable SET NAME=NAME WHERE REF = 1;
COMMIT

Microsoft All in One Script Framework

I’ve just found that Microsoft has just released a repository for all scripts which are driven by Technet script repository and Microsoft Customer Script & Support, the sample scripts are based on the real world scenario which are expected to save the IT admin/developer to reinvent the wheel for a particular task and instead they can build upon it or modify it. The repository covers sample for Microsoft Office, Microsoft SQL Server, Microsoft Exchange, Microsoft Sharepoint, Microsoft Windows 7, Microsoft Windows Server 2008 and Office 365

http://blogs.technet.com/b/onescript/

Coding Standard for Javascript

This is the javascript coding standard that my colleague found on Github, but seriously this is a good standard and I recommend it to any developer

https://github.com/rwldrn/idiomatic.js/

Page 6 of 19

Powered by WordPress & Theme by Anders Norén