Google Like Search TextBox By Rahul Garad

25 Mart 2011

örnek makale için buraya tıklayınız.

FETCH (Transact-SQL) SQL Server 2008

Retrieves a specific row from a Transact-SQL server cursor.
Topic link iconTransact-SQL Syntax Conventions

FETCH 
          [ [ NEXT | PRIOR | FIRST | LAST 
                    | ABSOLUTE { n | @nvar } 
                    | RELATIVE { n | @nvar } 
               ] 
               FROM 
          ] 
{ { [ GLOBAL ] cursor_name } | @cursor_variable_name } 
[ INTO @variable_name [ ,...n ] ] 

NEXT

Returns the result row immediately following the current row and increments the current row to the row returned. If FETCH NEXT is the first fetch against a cursor, it returns the first row in the result set. NEXT is the default cursor fetch option.
PRIOR
Returns the result row immediately preceding the current row, and decrements the current row to the row returned. If FETCH PRIOR is the first fetch against a cursor, no row is returned and the cursor is left positioned before the first row.
FIRST
Returns the first row in the cursor and makes it the current row.
LAST
Returns the last row in the cursor and makes it the current row.
ABSOLUTE { n| @nvar}
If n or @nvar is positive, returns the row n rows from the front of the cursor and makes the returned row the new current row. If n or @nvar is negative, returns the row n rows before the end of the cursor and makes the returned row the new current row. If n or @nvar is 0, no rows are returned. n must be an integer constant and @nvar must be smallint, tinyint, orint.
RELATIVE { n| @nvar}
If n or @nvar is positive, returns the row n rows beyond the current row and makes the returned row the new current row. If n or @nvar is negative, returns the row n rows prior to the current row and makes the returned row the new current row. If n or @nvar is 0, returns the current row. If FETCH RELATIVE is specified with n or @nvar set to negative numbers or 0 on the first fetch done against a cursor, no rows are returned. n must be an integer constant and @nvar must be smallint, tinyint, orint.
GLOBAL
Specifies that cursor_name refers to a global cursor.
cursor_name
Is the name of the open cursor from which the fetch should be made. If both a global and a local cursor exist with cursor_name as their name, cursor_name to the global cursor if GLOBAL is specified and to the local cursor if GLOBAL is not specified.
@ cursor_variable_name
Is the name of a cursor variable referencing the open cursor from which the fetch should be made.
INTO @variable_name[ ,...n]
Allows data from the columns of a fetch to be placed into local variables. Each variable in the list, from left to right, is associated with the corresponding column in the cursor result set. The data type of each variable must either match or be a supported implicit conversion of the data type of the corresponding result set column. The number of variables must match the number of columns in the cursor select list.
If the SCROLL option is not specified in an ISO style DECLARE CURSOR statement, NEXT is the only FETCH option supported. If SCROLL is specified in an ISO style DECLARE CURSOR, all FETCH options are supported.
When the Transact-SQL DECLARE cursor extensions are used, these rules apply:
  • If either FORWARD_ONLY or FAST_FORWARD is specified, NEXT is the only FETCH option supported.
  • If DYNAMIC, FORWARD_ONLY or FAST_FORWARD are not specified, and one of KEYSET, STATIC, or SCROLL are specified, all FETCH options are supported.
  • DYNAMIC SCROLL cursors support all the FETCH options except ABSOLUTE.
The @@FETCH_STATUS function reports the status of the last FETCH statement. The same information is recorded in the fetch_status column in the cursor returned by sp_describe_cursor. This status information should be used to determine the validity of the data returned by a FETCH statement prior to attempting any operation against that data. For more information, see @@FETCH_STATUS (Transact-SQL).
FETCH permissions default to any valid user.

A.Using FETCH in a simple cursor

The following example declares a simple cursor for the rows in the Person.Contact table with a last name that starts with B, and uses FETCH NEXT to step through the rows. The FETCH statements return the value for the column specified in DECLARE CURSOR as a single-row result set.

USE AdventureWorks
GO
DECLARE contact_cursor CURSOR FOR
SELECT LastName FROM Person.Contact
WHERE LastName LIKE 'B%'
ORDER BY LastName

OPEN contact_cursor

-- Perform the first fetch.
FETCH NEXT FROM contact_cursor

-- Check @@FETCH_STATUS to see if there are any more rows to fetch.
WHILE @@FETCH_STATUS = 0
BEGIN
   -- This is executed as long as the previous fetch succeeds.
   FETCH NEXT FROM contact_cursor
END

CLOSE contact_cursor
DEALLOCATE contact_cursor
GO

B.Using FETCH to store values in variables

The following example is similar to example A, except the output of the FETCH statements is stored in local variables instead of being returned directly to the client. The PRINT statement combines the variables into a single string and returns them to the client.

USE AdventureWorks
GO
-- Declare the variables to store the values returned by FETCH.
DECLARE @LastName varchar(50), @FirstName varchar(50)

DECLARE contact_cursor CURSOR FOR
SELECT LastName, FirstName FROM Person.Contact
WHERE LastName LIKE 'B%'
ORDER BY LastName, FirstName

OPEN contact_cursor

-- Perform the first fetch and store the values in variables.
-- Note: The variables are in the same order as the columns
-- in the SELECT statement. 

FETCH NEXT FROM contact_cursor
INTO @LastName, @FirstName

-- Check @@FETCH_STATUS to see if there are any more rows to fetch.
WHILE @@FETCH_STATUS = 0
BEGIN

   -- Concatenate and display the current values in the variables.
   PRINT 'Contact Name: ' + @FirstName + ' ' +  @LastName

   -- This is executed as long as the previous fetch succeeds.
   FETCH NEXT FROM contact_cursor
   INTO @LastName, @FirstName
END

CLOSE contact_cursor
DEALLOCATE contact_cursor
GO

C.Declaring a SCROLL cursor and using the other FETCH options

The following example creates a SCROLL cursor to allow full scrolling capabilities through the LAST, PRIOR, RELATIVE, and ABSOLUTE options.

USE AdventureWorks
GO
-- Execute the SELECT statement alone to show the 
-- full result set that is used by the cursor.
SELECT LastName, FirstName FROM Person.Contact
ORDER BY LastName, FirstName

-- Declare the cursor.
DECLARE contact_cursor SCROLL CURSOR FOR
SELECT LastName, FirstName FROM Person.Contact
ORDER BY LastName, FirstName

OPEN contact_cursor

-- Fetch the last row in the cursor.
FETCH LAST FROM contact_cursor

-- Fetch the row immediately prior to the current row in the cursor.
FETCH PRIOR FROM contact_cursor

-- Fetch the second row in the cursor.
FETCH ABSOLUTE 2 FROM contact_cursor

-- Fetch the row that is three rows after the current row.
FETCH RELATIVE 3 FROM contact_cursor

-- Fetch the row that is two rows prior to the current row.
FETCH RELATIVE -2 FROM contact_cursor

CLOSE contact_cursor
DEALLOCATE contact_cursor
GO

SQL SERVER’DA VERİLERİ SAYFALAMAK

by Halil İbrahim ARAÇ 27. Eylül 2009 21:57
 
Veritabanından gelen verileri listelediğiniz bir örnekte eğer gelen veri sayısı çok fazla ise sayfalama yapmanız gerekir. Aksi halde 10000 kayıtlık bir veri yığınını veritabanından bir seferde çekerek  DataGrid ile göstermeye çalıştığınızda bu sayfanın yüklenme süresi çok uzun olacaktır.
Bazılarımızın aklına şöyle bir soru gelebilir. Ben GridView’in sayfalama özelliğini etkinleştiririm ve her sayfada 10 kayıt görüntülerim. Bu sayede sayfanın yoğunluğunu azaltırım. Bu çoğu kişinin düştüğü bir yanlışlıktır. Çünkü GridView’in sayfalamasını etkinleştirseniz bile tüm veri veritabanından çekilecektir. Peki o halde tüm veri Grid’ e bağlandığı halde GridView sayfalamayı nasıl gerçekleştiriyor da sizin istediğiniz sayfa numarası aralığındaki veriyi görüntüleyebiliyor?
Siz GridView’e veriyi bağlandığınızda bağlanan tüm veri sayfa içerisinde Viewstate içerisinde saklanıyor ve başka bir sayfa için Grid’in sayfa numarasına tıkladığınızda ViewState PostBack ile sayfaya taşınıyor ve bu veri içerisinde istediğiniz aralıkta yer alan kayıt yığını görüntülenebiliyor.
Ancak bu 10000 verilik bir kayıt seti için bu 10000 satırın sayfa her pospback edildiğinde Viewstate ile taşınması demektir. Çünkü sizin tüm kayıtları çekmek için veritabanından yaptığınız SQL sorgulaması muhtemelen “select * from Sozler” şeklinde bir sorgudur ve bu sorgunun sonucunda da pek tabii Sozler tablosunda yer alan tüm sözler getirilecektir. Eğer Sozler tablosunda yer alan tüm kayıtları değil de bizim belirlediğimiz aralıktaki (örneğin 20 ve 30. kayıt arasındaki tüm kayıtlar) kayıtları getirebilirsek çok daha kullanışlı bir sorgu yazabilmiş oluruz ve o zaman yukarıda bahsettiğimiz Grid sayfalama problemini de çözebiliriz.
Aslında yukarıdaki satırları yazarken aklıma MySql veritabanı üzerinde sorgu yazdığım günler geldi. MySql sorgularında tam bu ihtiyacımızı karşılayabilen bir sorgu ifadesi mevcuttu. O da LIMIT komutuydu. Kullandığımız veritabanı yönetim sistemi MySql olsaydı  Sozler tablosunda yer alan kayıtların 1 ve 5. kayıtlar arasındaki verileri çekmek için “select * from Sozler orderby SozID LIMIT 1,5” sorgusunu kullanmamız yeterli olurdu. 5 ve 10. kayıtlar arasındaki verileri getirmek için “select * from Sozler orderby SozID LIMIT 5,10” ifadesi yeterli olacaktı. Gördüğünüz gibi MySql’de bu sorun çok kolay bir şekilde aşılabiliyordu. Size düşen sayfanın her postback olduğunda hangi sayfanın talep edildiğini öğrenmek ve bu LIMIT ifadesine sayfa numarasına bağlı olarak değişen bir değişkeni bağlayarak Grid’e bu sql ifadesin sonucunda gelen veriyi bağlamaktır.
Böyle bir ifadenin Sql Server’da karşılığının olmaması çok garip olurdu. Evet garip olurdu diyorum çünkü SqlServer da birçok kullanıcının yoğun olarak kullandığı bir veritabanı yönetim sistemidir.Geçenlerde bu konu üzerine araştırma yaparken bu işin Sql Server’da nasıl yapılacağı üzerine makaleler okuyordum ve ilginç bir SqlServer ifadesi buldum. Bu makalemde sizinle onu paylaşmak istedim. Örneğimde kullanacağım veritabanım SozlerDB isimli veritabanı olacak. Bu veritabanı Asenkron Web Servisi Çağrılması üzerine yazmış olduğum makalemde yer alan veritabanıdır.  Bu veritabanımın içerisinde Sozler isimli bir tablom var ve içerisinde özlü sözler kaydedilmiş durumda. Tablomun görüntüsü aşağıdaki gibidir.

RowNumber

Bize sayfalama imkanı sunan fonksiyonumuz ROW_NUMBER() isimli fonksiyondur. Bu fonksiyon her bir kaydın kaçıncı satırda olduğunu veren bir fonksiyondur. Örneğin her bir kaydın satır numarasını veren bir sorgu yazalım.

select ROW_NUMBER() OVER(ORDER BY SozID) AS RowNumber,* from Sozler

Sorguyu çalıştırdığımızda aşağıdaki çıktıyı elde ederiz.

Sql Server Sayfalama

Bu ekran görüntüsünden aslında ROW_NUMBER() fonksiyonun ne iş yaptığını anlamışsınızdır. Bu fonksiyon OVER ifadesi ile beraber çalışır ve OVER ile sıralamanın esas alınacağı kolonu belirtmeniz gerekir. Yukarıdaki sorguyu eğer aşağıdaki gibi yazarsak sonuç çok daha farklı olacaktır.


select ROW_NUMBER() OVER(ORDER BY SozID desc) AS RowNumber,* from Sozler

Sorgu sonucu aşağıdaki gibi olacaktır.

ROW_NUMBER()

Bu sefer OVER ifadesinde ID’ler tersten sıralandığı için satır numaraları da bu sıraya göre verilmiştir. İşte biz belirli bir aralıkta yer alan verileri sıralamak için bu RowNumber isimli kullanmaya çalışağız. Yani 3 ve 8. kayıtlar arasını getirebilmek için RowNumber kolonunun değeri 3 ve 5 arasında olanlar şeklinde bir şart koyacağız.
Daha esnek olması için biz bu değerleri dışarıdan parametre olarak alan bir prosedür yazmaya çalışalım.

CREATE proc [dbo].[VERIGETIR]
(
@bas int,
@son int
)
as
begin

with kayitseti as
(
      select ROW_NUMBER() OVER(ORDER BY SozID) AS 'RowNumber',* from Sozler
)

select * from kayitseti where RowNumber between @bas and @son

end

Bu prosedürü aşağıdaki değerlerle çalıştırdığınızda

exec VERIGETIR 3,8

Aşağıdaki sonucu elde edeceğiz.

OVER

Bu prosedürde önce RowNumber kolonunu içeren bir kayıtseti oluşturulmuş ve daha sonra bu kayıt seti içerisinde istediğimiz aralığı sorgulayan bir select ifadesi kullanılmıştır. Burada kayitseti dediğimiz veri kümesini aslında temp bir tablo gibi düşünebilirsiniz.
Yukarıdaki prosedürü aşağıdaki gibi de yazabilirsiniz.

ALTER proc [dbo].[VERIGETIR]
(
@bas int,
@son int
)
as
begin

select * from(SELECT ROW_NUMBER() OVER(ORDER BY SozID) AS RowNumber,* FROM Sozler) as kayitseti
where RowNumber between @bas and @son

end

Gene 3 ve 8 değerleri ile prosedürü çalıştırdığınızda aynı sonucu elde edeceksiniz. Bu sefer RowNumber kolonunu da içeren tüm veri kümesi as ifadesi ile bir kayıt seti olarak elde edilmiştir.
Böylece MySql’de yer alan LIMIT ifadesini karşılayacak bir prosedür yazmış olduk. Böylece bu prosedürü kullanarak verileri çeken bir Grid uygulamasında tüm veriyi değilde görüntülenecek olan sayfaya ait verileri veritabanından çekersiniz ki bu da ciddi bir performans artışı sağlayacaktır.
Bu makalemizin sonuna geldik. Başka makalelerde buluşmak dileğiyle hepinize iyi çalışmalar.

Örnek veritabanını indirmek için tıklayınız.(SozlerDB.bak (2,91 mb))