-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcursor.sql
44 lines (36 loc) · 1.26 KB
/
cursor.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
Declare @ProductId int
-- Declare the cursor using the declare keyword
Declare ProductIdCursor CURSOR FOR
Select ProductId from tblProductSales
-- Open statement, executes the SELECT statment
-- and populates the result set
Open ProductIdCursor
-- Fetch the row from the result set into the variable
Fetch Next from ProductIdCursor into @ProductId
-- If the result set still has rows, @@FETCH_STATUS will be ZERO
While(@@FETCH_STATUS = 0)
Begin
Declare @ProductName nvarchar(50)
Select @ProductName = Name from tblProducts where Id = @ProductId
if(@ProductName = 'Product-55')
Begin
Update tblProductSales set UnitPrice = 55 where ProductId = @ProductId
End
else if(@ProductName = 'Product-65')
Begin
Update tblProductSales set UnitPrice = 65 where ProductId = @ProductId
End
else if(@ProductName like 'Product-100%')
Begin
Update tblProductSales set UnitPrice = 1000 where ProductId = @ProductId
End
Fetch Next from ProductIdCursor into @ProductId
End
-- Release the row set
CLOSE ProductIdCursor
-- Deallocate, the resources associated with the cursor
DEALLOCATE ProductIdCursor
Select Name, UnitPrice
from tblProducts join
tblProductSales on tblProducts.Id = tblProductSales.ProductId
where (Name='Product-55' or Name='Product-65' or Name like 'Product-100%')