Share via


FETCH (Transact-SQL)

Belirli bir satırdan alır bir Transact-SQL sunucu imleç.

Topic link iconTransact-SQL sözdizimi kuralları

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

Bağımsız değişkenler

  • İleri
    Geçerli satırda ve geçerli satır için satır döndürdü katları hemen sonucu satır döndürür.FETCH ILERI bir imleç karşı ilk olan getirme, sonuçta ilk satır döndürür küme.NEXT varsayılan imleç getirme seçenektir.

  • ÖNCEKİ
    Hemen bir satır geçerli satıra döndürdü azaltır ve geçerli satır önceki sonuç satır döndürür.FETCH ÖNCEKI bir imleç karşı ilk olan getirme, hiçbir satır döndürdü ve imleci sola önce ilk satırın konumlandırıldı.

  • Birinci
    Imleç içinde ilk satır döndürür ve geçerli satır kolaylaştırır.

  • SON
    Imleç içinde son satır döndürür ve geçerli satır kolaylaştırır.

  • ABSOLUTE { n| **@**nvar}
    If n or **@**nvar Satır pozitiftir'i verirn satırlarını önüne imleç ve döndürülen satır geçerli yeni satır oluşturur.If n or **@**nvar negatif, satır verirn imleç sonuna önce satırları ve döndürülen satır geçerli yeni satır oluşturur.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 Satır pozitiftir'i verirn geçerli satırın satırları ve döndürülen satır geçerli yeni satır oluşturur.If n or **@**nvar negatif, satır verirn geçerli satıra önce satırları ve döndürülen satır geçerli yeni satır oluşturur.If n or **@**nvar 0, geçerli satır döndürür.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.

  • Genel
    Belirtir cursor_name bir genel imlecine başvuruyor.

  • cursor_name
    Getirme alınıp yapılması gereken açık imleç adıdır.Hem genel hem de bir yerel imleç ile varsa cursor_name kendi adıyla cursor_name Genel imleci GENEL belirtilirse ve yerel imleç, GLOBAL belirtilmedi.

  • **@**cursor_variable_name
    Imleç bir değişkenin adını, getirme yapılması gereken açık imleç başvuran iş.

  • INTO **@**variable_name[ ,*...*n]
    Yerel değişken yerleştirilmesi için bir getirme sütunlarından verileri sağlar.Listesinde, soldan sağa, her değişkenin bir imleç sonuç karşılık gelen sütun ilişkili olduğu küme.Her değişkenin veri türü eşleşen ya karşılık gelen sonuç veri türüne örtülü desteklenen dönüştürme olması küme sütun.Değişken sayısı, imleç seçme listesinde sütun sayısı eşleşmelidir.

Remarks

NEXT ISO stilinde BILDIRMEK imleç deyim SCROLL seçeneği belirtilmediği takdirde, yalnızca GETIRME seçeneği desteklenmiyor.Bir ISO içinde belirtilen SCROLL BILDIRMEK imleç stil, tüm GETIRME seçenekleri desteklenmektedir.

Zaman Transact-SQL Imleç uzantıları kullanılır, bu kuralları uygula BILDIRIN:

  • NEXT FORWARD_ONLY veya FAST_FORWARD belirtilirse, yalnızca GETIRME seçeneği desteklenmiyor.

  • DYNAMIC, FORWARD_ONLY veya FAST_FORWARD belirtilmedi ve bir anahtar KÜMESI, STATIK ya da SCROLL belirtilirse, tüm GETIRME seçenekleri desteklenmektedir.

  • DYNAMIC SCROLL imleçler MUTLAK dışındaki tüm GETIRME seçenekleri destekler.

@@ FETCH_STATUS işlev, son GETIRME deyim durumunu raporlar.Aynı bilgileri kaydedilir fetch_status tarafından döndürülen imleç sütunsp_describe_cursor.Bu durum bilgileri herhangi bir işlem bu verilere karşı deneyen bir GETIRME deyim tarafından döndürülen verilerin geçerliliğini belirlemek için kullanılır.Daha fazla bilgi için bkz: @@ FETCH_STATUS (Transact-SQL).

İzinler

Izinleri varsayılan için geçerli bir kullanıcı FETCH.

Örnekler

C.Basit bir imleç içinde GETIRME kullanma

Aþaðýdaki örnek, satır için basit bir imleç bildirir Person.Contact Tablo ile başlayan bir Soyadı B, kullanır FETCH NEXT satırları arasında adım için . The FETCH statements return the value for the sütun specified in DECLARE CURSOR as a single-row sonuç kümesi.

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.FETCH değişkenlerin değerlerini depolamak için kullanma

Aşağıdaki örnek, örnek BIR çıktı dışında benzer FETCH ifadeyi doğrudan istemciye döndürülen yerine yerel değişkenler depolanır. The PRINT deyim combines the variables into a single dize and returns them to the istemci.

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.SCROLL imleç bildirmek ve ALMA seçeneklerini kullanma

Aşağıdaki örnek oluşturur bir SCROLL tam bir kaydırma yetenekleri aracılığıyla izin vermek için imleç LAST, PRIOR, RELATIVE, ve ABSOLUTE Seçenekler.

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