[SQL]Declare Cursor範例

  • 3137
  • 0
  • 2023-08-31

摘要:[SQL]Declare Cursor範例


DECLARE vendor_cursor CURSOR FOR 
SELECT VendorID, Name
FROM Purchasing.Vendor
WHERE PreferredVendorStatus = 1
ORDER BY VendorID;

OPEN vendor_cursor

FETCH NEXT FROM vendor_cursor 
INTO @vendor_id, @vendor_name
WHILE @@FETCH_STATUS = 0
BEGIN
    -- start 執行任何你想要執行的sql---
    
    -----------end------------
	FETCH NEXT FROM vendor_cursor 
    INTO @vendor_id, @vendor_name
END 
CLOSE vendor_cursor;
DEALLOCATE vendor_cursor;