Yearly Archives: 2014


JOINs in SQL Server 1

What is a join?

Before starting discuss on joins, first we should know what is join?
SQL Server is a RDBMS(Relation database management system), i.e. it has tables which holds rows and columns and relationship between multiple tables using keys. So when we have to get data from more than one table, we use joins to get it. Join in sql is used to combine rows from two or more table based on comparing one or more common fields.

Types of Joins in SQL

INNER JOIN:- Returns all rows when at least one match in both tables.

LEFT JOIN:- Returns all rows from left table and matched rows from right table.

RIGHT JOIN : – Returns all rows from right table and matched rows from left table.

FULL JOIN :- Returns all rows from both tables.… More


Restart Computer Failure issue during SQL Server 2008 uninstall

Problem:

Last night i was uninstalling SQL Server 2008 from my machine and got stuck with an issue “Restart Computer Failure”.

I tried several time but after goggling, i got the solution and now sharing with you all

Solution :

  1. Start Regedit
  2. Go to the key “HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager”
  3. Delete “PendingFileRenameOperations” entry and try again
More

New features in SQL Server 2014 (In Database Engine)

New features added in SQL 2014 (Headings only)

Below are the enhancement points list :

Database Engine Feature Enhancements
o    Memory-Optimized Tables
o    SQL Server Data Files in Windows Azure
o    Host a SQL Server Database in a Windows Azure Virtual Machine
o    Backup and Restore Enhancements
o    New Design for Cardinality Estimation
o    Delayed Durability
o    AlwaysOn Enhancements
o    Partition Switching and Indexing
o    Managing the Lock Priority of Online Operations
o    Columnstore Indexes
o    Buffer Pool Extension
o    Incremental Statistics
o    Resource Governor Enhancements for Physical IO Control
o    Online Index Operation Event Class
o    Database Compatibility Level
·         Transact-SQL Enhancements
·         System View Enhancements
·         Security Enhancements
·         Deployment Enhancements

Visit below link for details;

http://msdn.microsoft.com/en-in/library/bb510411.aspxMore


Convert a comma separated list into table 4

In this post, we are going to learn “how we can convert a comma separated list into a table using a recursive CTE”. Below is the code:


DECLARE @vc_CommaSepValues VARCHAR(1000)
SET @vc_CommaSepValues = '1,R,TYF,776876,GHGF,6856,TYFT,1,R,TYF,776876,GHGF,6856,TYFT,1,R,TYF,776876,GHGF,6856,TYFT,1'

;WITH CTE AS
(
SELECT CAST(LEFT(@vc_CommaSepValues, CHARINDEX(',', @vc_CommaSepValues + ',') - 1) AS VARCHAR(MAX)) AS Val
,CAST(STUFF(@vc_CommaSepValues, 1, CHARINDEX(',', @vc_CommaSepValues + ','), '') AS VARCHAR(MAX)) AS RecVal
UNION ALL
SELECT CAST(LEFT(RecVal, CHARINDEX(',', RecVal + ',') - 1) AS VARCHAR(MAX)) AS Val
,CAST(STUFF(RecVal, 1, CHARINDEX(',', RecVal + ','), '') AS VARCHAR(MAX)) AS RecVal FROM CTE
WHERE RecVal > ''
)

SELECT Val FROM CTE
OPTION(MAXRECURSION 32767)

Why OPTION MAXRECURSION?

Hint OPTION(MAXRECURSION 32767) is a query hint which instructs to compiler to iterate 32767 times. Default, max recursion of a loop in SQL is 100 and in case we will not put this hint at the end, any string which requires more loop than 100 will raise an … More


Set Width of gridview columns dynamically when AutoGenerateColumns is true 1

Introduction

I faced a lot of problems when I asked to set the width of a gridview dynamically, that is the property AutoGenerateColumns set to AutoGenerateColumns=”true” and the gridview was directly bounded from a dataset in code behind. Thus, we don’t have bound fields to set the width of columns individually. I tried a lot of methods found over the internet but sadly anyone could not help me. But I have overcome the problem using some tricks and am now sharing this article with all of you so that if anyone has a problem like me, he will not get more frustrated.

Background

I used many methods found over the internet, but they could not work for me. Then I used both methods – that is one for bound field and another for setting the AutoGenerateColumns=”true” – individually in two projects and then when page got rendered in browser, I … More


Avoid Use of While Loops to get a Comma Seperated list of rows 1

Problem Detail

In the morning at your workplace, your manager or any colleague comes to you and asks to get the list of all the dates in a comma separated list.

Now the first and the quickest way of getting the output which will knock us is using a loop to get the list.

/*Getting list with While loop*/

DECLARE @fromdate DATETIME; SET @fromdate = '01-Sep-2014'

DECLARE @todate DATETIME; SET @todate = '30-Sep-2014'

DECLARE @vc_PVTColumns VARCHAR(1000); SET @vc_PVTColumns = ''

WHILE (@fromdate <= @todate)

BEGIN

SET @vc_PVTColumns = @vc_PVTColumns + '[' + CONVERT(VARCHAR(25), @fromdate, 106) + '], '

SET @fromdate = DATEADD(d, 1, @fromdate)

END

SELECT @vc_PVTColumns =LEFT(@vc_PVTColumns, LEN(@vc_PVTColumns) - 1)

SELECT @vc_PVTColumns

In above case, DB engine has to iterate for each and every value of day and do the same task again and again. The same thing can be implementedin SET based approach using COALESCE function with a … More


Be aware when using ISNULL 1

ISNULL vs COALESCE in SQL Server

In our T-SQL programming, we mostly use ISNULL function to replace the null value of a column with another value. The same can be achieved using COALESCE function too. In this blog post, we are going to see a significant difference between NULL and COALESCE functions. We are going to discuss about the implicit conversion used by ISNULL and COALESCE functions.

What is COALESCE:

Evaluates the arguments in order and returns the current value of the first expression that initially does not evaluate to NULL.

Thus COALESCE gives a substitute value for NULL values from the given list of columns. It always returns the first NON-NULL from the list.

Syntax : – COALESCE ( expression [ ,…n ] )

COALESCE vs ISNULL

When we use ISNULL to get the substitute value for any NULL value it implicitly type casts the substitute as per the … More