About Me

My photo
Mumbai, Maharastra, India
He has more than 7.6 years of experience in the software development. He has spent most of the times in web/desktop application development. He has sound knowledge in various database concepts. You can reach him at viki.keshari@gmail.com https://www.linkedin.com/in/vikrammahapatra/ https://twitter.com/VikramMahapatra http://www.facebook.com/viki.keshari

Search This Blog

Sunday, June 8, 2014

Warning: Null value is eliminated by an aggregate or other SET operation.



Whenever you are using Aggregate function, keep a special cleansing activity for NULL value before you apply aggregate function on it.
By default SQL Server escape null value while calculating aggregate. Let’s see with an example.

Here we are creating a table to hold some null value in salary column.

create table testNullAggregate_TB
(id int identity(1,1),
dept_id int,
ename varchar(10),
salary int)
Command(s) completed successfully.

Lets insert some records with null values in Salary field

insert into testNullAggregate_TB values(1,'Suprabha',2000)
insert into testNullAggregate_TB values(2,'Lopamudra',null)
insert into testNullAggregate_TB values(1,'Sourabhi',2000)
insert into testNullAggregate_TB values(1,'Akansha',null)
insert into testNullAggregate_TB values(2,'Pragya',2000)
insert into testNullAggregate_TB values(1,'Sulagna',2000)
insert into testNullAggregate_TB values(3,'Priya',2000)

Testing Aggregating Function with Null Values

Here we are checking total row count on the basis of salary field.

select count(salary) from testNullAggregate_TB
RowsCount
-----------
5
Here we saw actual Number of records present in table is 7 but count(salary) escape the record count where it found null in salary equal null.

Here we are trying to find the number of employee in each department.

select dept_id, count(salary) as 'No of Employee' from testNullAggregate_TB
group by dept_id
dept_id     No of Employee
----------- --------------
1           3
2           1
3           1
Here again we saw, it produce wrong result. Dept 1 has 4 employee whereas result shows 3, similarly Department 2 has 2 employee whereas Result shows 1.

Here we are trying to find average salary for each department.

select dept_id, avg(salary) as 'Avg Salary' from testNullAggregate_TB
group by dept_id
dept_id     Avg Salary
----------- -----------
1           2000
2           2000
3           2000

Here we saw the average salary for department 1 and 2 are not correct.

How to Rectify the Problem occurred while using Aggregate function with null values.

ISNULL function we will be using to rectify the null problems.

Total Number of Rows on the basis of salary
select count(isnull(salary,0)) as RowsCount from testNullAggregate_TB
RowsCount
-----------
7

Total employee for each department.
select dept_id, count(isnull(salary,0)) as 'No of Employee' from testNullAggregate_TB
group by dept_id
dept_id     No of Employee
----------- --------------
1           4
2           2
3           1

Average salary for each department.
select dept_id, avg(isnull(salary,0)) as 'Avg Salary' from testNullAggregate_TB
group by dept_id
dept_id     Avg Salary
----------- -----------
1           1500
2           1000
3           2000

Let  your code speak, you be silent!!!


Post Reference: Vikram Aristocratic Elfin Share

Thursday, June 5, 2014

With SQL Server 2014, New way to create NONCLUSTERED Index on Persisted Table Object


With SQL Server 2014 we can create NONCLUSTERED Index at table level while creating table object., Lets see how we can do by doing a small practical.

In SQL Server 2014

We are creating a table and trying to define NONCLUSTERED index at table creation.

create table testDemo
(col1 int primary key,
col2 int index idx_col2_testDemo,
col3 int index idx_col3_testDemo)
Command(s) completed successfully.

Since command has successfully executed, lets see indexes created by querying Sys.indexes Catalog view.

select name, index_id, type_desc
from sys.indexes where object_id = OBJECT_ID('testDemo')

name                           index_id    type_desc
------------------------------ ----------- -------------
PK__testDemo__357D0D3EF2E23FAC 1           CLUSTERED
idx_col3_testDemo              2           NONCLUSTERED
idx_col2_testDemo              3           NONCLUSTERED

By querying Sys.indexes we are sure, we have three indexes on testDemo table. So this is how you can create NC index at table creation.

Just for our approval, lets see whether the same syntax is working in SQL Server 2008?

In SQL Server 2008 R2

create table testDemo
(col1 int primary key,
col2 int index idx_col2_testDemo,
col3 int index idx_col3_testDemo)
Msg 1018, Level 15, State 1, Line 3
Incorrect syntax near 'index'. If this is intended as a part of a table hint, A WITH keyword and parenthesis are now required. See SQL Server Books Online for proper syntax.

So here we saw in SQL Server 2008 R2 we don’t have option to created NC indexes at table creation.

The essence of code; it binds programmer in a non-fragile bond and never let his hand go without her!!!

Post Reference: Vikram Aristocratic Elfin Share

Wednesday, June 4, 2014

NONCLUSTERED Index on Table Variable in SQL Server 2014



There are always advantages of using table variable when working with small dataset. But the only flaw with Table Variable was its lack of ability to create NON Clustered index on it but  SQL Server 2014  has given support to create NONCLUSTERED index on table variable.

In SQL Server 2008 R2
Lets try to create NC on @table variable column.

declare @tab_variable table
     ( id int identity(1,1) primary key,
     f_name varchar(10) index idx_f_name,
     l_name varchar(10))
Msg 1018, Level 15, State 1, Line 4
Incorrect syntax near 'index'. If this is intended as a part of a table hint, A WITH keyword and parenthesis are now required. See SQL Server Books Online for proper syntax.

We saw SQL Server 2008 R2 has no support for NC on table variable.

In SQL Server 2014
Now let’s see how we can create non clustered index on table variable on SQL Server 2014

declare @tab_variable table
       ( id int identity(1,1) primary key,
       f_name varchar(10) index idx_f_name,
       l_name varchar(10))

insert into @tab_variable(f_name,l_name)
select top 1000000 SUBSTRING(name,0,9), SUBSTRING(name,0,9) from  sys.columns

select f_name, l_name from @tab_variable
where f_name like 'TSQL'

f_name     l_name
---------- ----------
TSQL       TSQL

(1 row(s) affected)

So here we say that Non Clustered index, we can create on @table variable column, it is an awesome feature added to SQL Server 2014, the gap of temp variable and table variable reduced to just scope difference J

Your code has potential to bow every head.  
 

Post Reference: Vikram Aristocratic Elfin Share