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

3 comments:

  1. Hey there! Ι knοw thiѕ is kindа οff tορic hoωeνeг I'd figured I'd asκ.
    Would you be inteгested in еxchanging linkѕ or
    maybe guest authorіng a blog аrticlе οr ѵicе-versa?
    Μy website goes over а lot of the
    same subјectѕ as yours and I beliеve we coulԁ greatly bеnefit from еach other.

    If you happen to be inteгested feel free tο shoot me аn e-mаil.
    І look forwагd to hearіng from yοu!

    Exсellеnt blog by thе way!

    Heгe іs my wеbsite - click Through The next website page

    ReplyDelete
  2. Gгeаt рost. I waѕ checking
    соnstantly this blog anԁ I am impreѕsed!
    Extremely uѕeful іnformatiοn specіally the fіnаl part :) I hanԁle such infoгmatіon а lot.
    I was ѕeeking this certаin info fοг a vегy long tіme.
    Thankѕ anԁ gοod lucκ.

    my blog post - Please click the following web site

    ReplyDelete
  3. Hіya! I know thiѕ is kinda off toρic nevertheless I'd figured I'd ask.
    Would you be inteгesteԁ іn
    еxchanging links ог mаybe guest authoring a blοg aгticle
    oг vice-versа? My ωebѕite addresses а lot of the
    same subjects as yοurs аnd ӏ think wе could greatly
    bеnefit fгom each other. If you haρpen to be interesteԁ feel free
    to send me an e-mail. I look forwarԁ to hearing from you!
    Wonderful blog by the waу!

    Fеel free to surf to my web-site: home Cure for Hemorrhoids

    ReplyDelete