復制代碼 代碼如下:
DECLARE @OldId varchar(50)
DECLARE @CustomerID varchar(50)
DECLARE My_Cursor CURSOR --定義游標
FOR (select CustomerID,oldid from Customer where Area='bj') --查出需要的集合放到游標中
OPEN My_Cursor; --打開游標
FETCH NEXT FROM My_Cursor INTO @CustomerID,@OldId; --讀取第一行數據
WHILE @@FETCH_STATUS = 0
BEGIN
DECLARE @OtherPro VARCHAR(500)
DECLARE @StatusID varchar(200)
DECLARE @Userid varchar(200)
DECLARE @FinalUserid varchar(200)
SELECT @OtherPro=OtherPro,@StatusID=CustomerStatusID,@Userid=UserID FROM bjsunmis.dbo.Customer WHERE CustomerID=@OldId;
SELECT @FinalUserid=UserID from Users where oldid=@Userid
INSERT INTO CustomerotherInfo(CustomerID,OtherPro,CustomerStatusID,UserID)VALUES(@CustomerID,@OtherPro,@StatusID,@FinalUserid)
FETCH NEXT FROM My_Cursor INTO @CustomerID,@OldId; --讀取下一行數據
END
CLOSE My_Cursor; --關閉游標
DEALLOCATE My_Cursor; --釋放游標
GO