Cursor:- Cursor is a database object to retrieve data from a result set one row at time. we use cursor when we need to update record databse table in Sigleton fashion means row wise.
For Exampl :
select * from Products
know i want change unit price of unit of each based on some condition.
Declare @UnitPrice decimal(5,2)
Declare @ProductId int
Declare UnitPriceUpdateCursore Cursor FOR
Select ProductID From Products
open UnitPriceUpdateCursore
Fetch Next From UnitPriceUpdateCursore into @ProductId
While (@@Fetch_Status=0)
Begin
select @UnitPrice=UnitPrice from Products
Begin Update Products set UnitPrice = case when UnitPrice between 5 And 10 then 15
when UnitPrice between 11 AND 15 then 20
else UnitPrice
end
Where ProductID=@ProductId
Fetch Next From UnitPriceUpdateCursore into @ProductId
END
End
Close UnitPriceUpdateCursore
Deallocate UnitPriceUpdateCursore
Set NoCount Off
For Exampl :
select * from Products
know i want change unit price of unit of each based on some condition.
Declare @UnitPrice decimal(5,2)
Declare @ProductId int
Declare UnitPriceUpdateCursore Cursor FOR
Select ProductID From Products
open UnitPriceUpdateCursore
Fetch Next From UnitPriceUpdateCursore into @ProductId
While (@@Fetch_Status=0)
Begin
select @UnitPrice=UnitPrice from Products
Begin Update Products set UnitPrice = case when UnitPrice between 5 And 10 then 15
when UnitPrice between 11 AND 15 then 20
else UnitPrice
end
Where ProductID=@ProductId
Fetch Next From UnitPriceUpdateCursore into @ProductId
END
End
Close UnitPriceUpdateCursore
Deallocate UnitPriceUpdateCursore
Set NoCount Off
No comments:
Post a Comment