Monday 2 February 2009

SQL server 2008 TSQL - 1 : TOP

Using Top


In 2008, Parameters are accepted in Top. You can declare a parameter and pass it in the top statement.
Eg :

Declare @topcust int
set @topcust = 10

select top (@topcust) * from Person.Address
------------------------------------------------------------------------

Top in DML
------------

create table Test
(
id int primary key
)

go
------------------------------------------------------------------------------------
--insert
insert top(5) into test
select 1
union select 2 union select 3 union select 4 union select 5 union select 6 union select 7

select * from test

id
1
2
3
4
5

Only the top 5 records have been inserted
--------------------------------------------------------------------------------

--update

update top(1) Test set id = id * 10

select * from test

id
2
3
4
5
10

only on record is updated that is the last record (10)

-----------------------------------------------------------------------------------------
--Delete

Delete top(3) from test

select * from test


id
5
10

The top 3 records have been deleted

-----------------------------------------------------------------------------------------

Though the top use is very limited it can be used for processing the records in batches.


Top is the preferred way than the Set Rowcount on as Set Rowcount will be deprecated in the future versions.

Saturday 13 December 2008

SQL SERVER 2008




This blog is all about Sql server 2008.