Con trỏ động có thể cập nhật. Con trỏ động sẽ tìm nạp các thay đổi (chèn, cập nhật hoặc xóa) trên mỗi lần tìm nạp trong khi con trỏ đang mở nếu có bất kỳ sửa đổi nào xảy ra với dữ liệu gốc trong bảng / s. Loại Con trỏ này hữu ích khi chúng ta muốn trích xuất các bản ghi từ / s bảng gốc trong khi con trỏ đang mở và chúng ta vẫn đang trong quá trình sửa đổi dữ liệu hoặc chèn các bản ghi mới và muốn đưa chúng vào tập kết quả con trỏ để thực hiện thao tác.
Con trỏ động có thể cuộn (Đầu tiên, Cuối cùng, Trước đó, Tiếp theo, Tương đối) nhưng tuyệt đối tùy chọn không hoạt động với Con trỏ động.
Tập lệnh cho Con trỏ động trong SQL Server cũng được sử dụng trong video.
--drop table dbo.Customer
Create table dbo.Customer (
CustomerId Int Identity(1,1),
CustomerName VARCHAR(100),
StreetAddress VARCHAr(100),
City VARCHAR(100),
State CHAR(2))
go
--Insert couple of Records in Sample Table
Insert into dbo.Customer
Select 'Aamir shahzad','Test Street Address','Charlotte','NC'
Union
Select 'M Raza','Test Street Address','Charlotte','NC'
Select * from dbo.Customer
--Insert NEW Record
Insert into dbo.Customer
Select 'John Smith','Test Street Address','New York City','NY'
--Delete Records
Delete from dbo.Customer
Where CustomerName in ('Aamir Shahzad','M Raza')
--Update All Record
Update dbo.Customer
set CustomerName='NO NAME'
--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
DYNAMIC
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'
PRINT CONCAT(@CustomerID,' ',@CustomerNAme,' ',@StreetAddress,' ',@City,' ',@State)
FETCH NEXT FROM CUR INTO @CustomerID,@CustomerNAme,@StreetAddress,@City,@State
END
CLOSE CUR
DEALLOCATE CUR Vui lòng xem video để biết Demo Chi tiết về Con trỏ Động trong SQL Server.