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

Monday, February 11, 2013

TOP 100 PERCENT and ORDER BY obsolete from SQL Server 2005


In SQL 2000 it was an attempt to return all records of the view in the correct order. In SQL 2005 and up you can not simulate ORDER BY using this trick anymore, so SELECT TOP (100) PERCENT has no meaning.

Lets stimulate the scenerio. We create a table testTop100 for our explanation with field id and name

create table testTop100
(id int identity(1,1),
name varchar(10))

Command(s) completed successfully.

Lets insert few records to testTop100

insert into testTop100 values('Abhaya')
insert into testTop100 values('Nitya')
insert into testTop100 values('Ananya')
insert into testTop100 values('Roma')

(1 row(s) affected)
(1 row(s) affected)
(1 row(s) affected)
(1 row(s) affected)

Now we will be creating view vwTestTop100 with top 100 Percent and Order by clause

create view vwTestTop100
as
select top 100 percent * from testTop100 order by id desc
go

Command(s) completed successfully.

Lets see the result with simple select query

select * from testTop100
id          name
----------- ----------
1           Abhaya
2           Nitya
3           Ananya
4           Roma

(4 row(s) affected)

Now lets see the result of View which is using Top 100 Percent and order by on id desc

select * from vwTestTop100
id          name
----------- ----------
1           Abhaya
2           Nitya
3           Ananya
4           Roma

(4 row(s) affected)

As we can see SQL Server neither throws error nor it acknowledge the presence of order by clause, it simply ignore the order by clause in View defination.

Now lets try the following

select * from vwTestTop100 order by id desc
id          name
----------- ----------
4           Roma
3           Ananya
2           Nitya
1           Abhaya

(4 row(s) affected)

Now from the output we can see, desired result found

Conclusion :  It is pointless to add ORDER BY clause to the view definition and expect records to come in that ORDER. If you need records ordered, don't put ORDER BY in the view, but rather
select * from myView ORDER BY OrderFields.
This way the correct order will be guaranteed.

Abide by code semantic, u miss all coding fun..Walk off beyond   


Post Reference: Vikram Aristocratic Elfin Share

Thursday, February 7, 2013

Sorting table separately while using UNION All, How to??


There are times when developer want top and bottom query of the UNION ALL resultset sorted independently, like this

select Columns from table1 order by Columns
union all
select Columns from tabbe2 order by Columns

However the above query will fail and give the output as

Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'union'.

NOTE :It is not possible to have two different order by in the UNION statement because UNION returns single resultant.

However if your requirement is saying to sort top and bottom query independently then how to do?

Let’s create a scenario, by taking two tables TABLE1 and TABLE2

create table table1
(id int,
name varchar(15)
)
Command(s) completed successfully.

create table table2
(id int,
name varchar(15)
)
Command(s) completed successfully.

insert into table1(id,name)
select 1,'Ananya_Tab1'
union all
select 2,'Abhiroop_Tab1'
union all
select 3,'Gunjal_Tab1'

(3 row(s) affected)

insert into table2(id,name)
select 3,'Bikshapati_Tab2'
union all
select 2,'Sanjana_Tab2'
union all
select 1,'Akshit_Tab3'

(3 row(s) affected)


Now our tables are in place let’s do proceed to stimulate our requirement, we want like this  

select id,name, 'tab1' as odrCol from table1
order by id
union all
select id,name, 'tab2' as odr1 from table2
order by id

But firing the above script on ssms, gives error                          

Msg 156, Level 15, State 1, Line 3
Incorrect syntax near the keyword 'union'.

And if we execute the select UNION without order by

select id,name from table1
union all
select id,name from table2

id          name
----------- ---------------
1           Ananya_Tab1
2           Abhiroop_Tab1
3           Gunjal_Tab1
3           Bikshapati_Tab2
2           Sanjana_Tab2
1           Akshit_Tab3

(6 row(s) affected)

However our requirement is like this

id          name          
----------- ---------------
1           Ananya_Tab1    
2           Abhiroop_Tab1  
3           Gunjal_Tab1    
1           Akshit_Tab3    
2           Sanjana_Tab2   
3           Bikshapati_Tab2

So to get the output as required, lets add a additional column ordCol and use it in order by clause

select id,name, 'tab1' as odrCol from table1
union all
select id,name, 'tab2' as odrCol from table2
order by odrCol,id

id          name            odrCol
----------- --------------- ------
1           Ananya_Tab1     tab1
2           Abhiroop_Tab1   tab1
3           Gunjal_Tab1     tab1
1           Akshit_Tab3     tab2
2           Sanjana_Tab2    tab2
3           Bikshapati_Tab2 tab2

(6 row(s) affected)

Now we can see we achieved our desired requirement of independent sorting of select query in UNION ALL.

Code exercises your creative instincts; do code n enjy J   


Post Reference: Vikram Aristocratic Elfin Share

Tuesday, February 5, 2013

Track changes on your table with OUTPUT Clause no need for trigger


SQL Server provides a simplified method of auditing changes using DML statements without having to use Triggers. With the invent of OUTPUT clause as a part of DML statement helps us in tracking changes made during any DML operations.

The OUTPUT clause can save the result-set in a table or table variable. Let stimulate the change tracking with OUTPUT clause

Let’s create a table testOutputClause with id and name as a column and insert few data.

create table testOutputClause
(id int identity(1,1),
name varchar(15))

insert into testOutputClause values('Purushottam')
insert into testOutputClause values('Vrishab')
insert into testOutputClause values('Palash')

select * from testOutputClause
id          name
----------- ---------------
1           Purushottam
2           Vrishab
3           Palash

(3 row(s) affected)

Now we are creating audit table tracker which will record all the changes that will be performed on testOutputClause table.

create table tracker
(tableName varchar(18),
changesDone varchar(100))   

Now we going to fire update statement on testOutputClause and at the same time we will be recording the changes using OUTPUT Clause.

update testOutputClause
set name = 'Ipsita'
output 'tracker','the name'+ deleted.name +'is changed to'+ inserted.name
into tracker
where id=1
(1 row(s) affected)

Lets see the result of update statement on testOutputClause, so records are now updated with name = Ipsita where the id was 1.
                                                             
select * from testOutputClause
id          name
----------- ---------------
1           Ipsita
2           Vrishab
3           Palash

(3 row(s) affected)

Now let’s see the result of tracker (the audit table).

select * from tracker
tableName          changesDone
------------------ ----------------------------------------
tracker            the name Purushottam is changed to Ipsita
(1 row(s) affected)

We can see the update is tracked successfully in audit table.

NOTE: You can use SQL Server magic table i.e INERTED and DELETED with OUTPUT Clause and records the changes in DML.

Code until you loses yourself

 Post Reference: Vikram Aristocratic Elfin Share