Cursor use during storage

xiaoxiao2021-03-06  111

Use the stored procedure to eliminate data redundancy in the database

Create Procedure SP_MYTEST AS

Declare @pro varchar (50)

Declare @MM INT

Declare Wu Cursor for Select DistINCT Product from MyTest

Open wu

Fetch next from wu @Pro

While @@ fetch_status = 0

Begin

SELECT @ mm = sum ([COST]) from mytest where [product] = @ pro

Delete from mytest where [product] = @ pro

INSERT INTO MyTest ([Product], [COST]) VALUES (@ pro, @ mm)

Fetch next from wu @Pro

end

Close Wu

Deallocate wu

Go

We can use jobs to update the corresponding data

转载请注明原文地址:https://www.9cbs.com/read-99640.html

New Post(0)