Ecco i punti importanti da ricordare su KEYSET Cursor in SQL Server
- Se la query di selezione utilizza una tabella senza indice univoco, il cursore KEYSET verrà semplicemente convertito in cursore statico. Assicurati che tutte le tabelle che stai utilizzando in Seleziona query abbiano un indice univoco. Questo è importante poiché KEYSET Cursor crea identificatori univoci per le righe utilizzando questi valori univoci.
- Se inseriamo le righe nelle tabelle di origine una volta che il cursore è aperto. Tali inserti non saranno visibili nel cursore già aperto.
- Se aggiorniamo i valori non chiave nelle tabelle di base, tali modifiche saranno visibili nel cursore.
- Se aggiorni il valore della colonna chiave in Base Table/s mentre il cursore è aperto e poi provi a recuperare il valore. @@FETCH_STATUS ti restituirà -2. L'aggiornamento effettuato all'interno del cursore alla Colonna Chiave con la clausola WHERE CURRENT OF sarà visibile alla fine del Cursore.
- Se elimini la riga dalla/e tabella/e di base mentre il cursore è aperto e poi provi a recuperare quella riga nel cursore, @@FETCH_STATUS restituirà -2.
- I cursori KEYSET sono scorrevoli.
Create table dbo.Customer (
CustomerId Int ,
CustomerName VARCHAR(100),
StreetAddress VARCHAr(100),
City VARCHAR(100),
State CHAR(2))
go
--Create Unique Index on CustomerID
CREATE UNIQUE INDEX UQ_CustomerID
ON dbo.Customer (CustomerID);
--Insert few Records in Sample Table
Insert into dbo.Customer
Select 1,'Aamir shahzad','Test Street Address','Charlotte','NC'
Union all
Select 2,'M Raza','Test Street Address','Charlotte','NC'
union all
Select 3,'John Smith','Test Street Address','New York City','NY'
union All
Select 4,'Christy Richard','Test Street Address','Rio Rancho','NM'
--Insert NEW Record
Insert into dbo.Customer
Select 5,'Robert Ladson','Pathway Street Address','High Point','NC'
--Delete Records
Delete from dbo.Customer
Where CustomerID in (3,4)
--Update All Record for NONKEY Column
Update dbo.Customer
set CustomerName='NO NAME'
--Update Key Column value
Update dbo.customer
set CustomerID=9
where Customerid=3
--KEYSET CURSOR Script
Declare @CustomerID INT
Declare @CustomerNAme VARCHAR (100)
DECLARE @StreetAddress VARCHAR(100)
DECLARE @City VARCHAR(100)
DECLARE @State CHAR(2)
--DECLARE A CURSOR
DECLARE CUR CURSOR
KEYSET
FOR
Select CustomerID,CustomerName,StreetAddress,City,State from dbo.Customer
--OPEN CURSOR
OPEN CUR
Print 'CURSOR IS OPEN'
--FETCH NEXT RECORD
FETCH NEXT FROM CUR INTO @CustomerID,@CustomerNAme,@StreetAddress,@City,@State
WHILE @@FETCH_STATUS=0
BEGIN
RAISERROR ('',0,1) WITH NOWAIT
WAITFOR DELAY '00:00:15'
--You can use CONCAT Function in SQL 2012 AND Latest for Contatenation of Integters and Strings
--PRINT CONCAT(@CustomerID,' ',@CustomerNAme,' ',@StreetAddress,' ',@City,' ',@State)
Print CAST(@CustomerID AS VARCHAR(10))+' '+@CustomerNAme+' '+@StreetAddress+' '+@City+' '+@State
FETCH NEXT FROM CUR INTO @CustomerID,@CustomerNAme,@StreetAddress,@City,@State
Print @@FETCH_STATUS
END
CLOSE CUR
DEALLOCATE CUR Video demo:cosa sono i cursori KEYSET in SQL Server e come funziona il cursore KEYSET