sqlserver可更新的游标

一个可更新游标的例子

use fisherysale;
declare my_cursor scroll  cursor
for
select 地址 from Buyorder$ where (手机 is null  and 电话 is  null) order by 地址
for update;
open my_cursor

declare @mobile sysname,@tel sysname,@mobile2 sysname,@tel2 sysname;
fetch first from my_cursor into @mobile

while(@@fetch_status=0)
begin
    fetch next from my_cursor into @mobile2
    if (@@fetch_status=0)
    begin
        if (@mobile2 = @mobile)
        begin
              --print @mobile + '-------' + @tel
                      delete from Buyorder$ where current of my_cursor                        
        end
        else
        begin                      
            set @mobile = @mobile2;
            set @tel = @tel2;
        end
   
    end
       

end

close my_cursor
deallocate my_cursor

在查询分析器里面执行,如果碰到"在READ ONLY 游标上不能指定 FOR UPDATE"的错误提示,请检查一下这张表是否设置了主键或者索引......
USE AA if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[tmp_Last]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [dbo].[tmp_Last] CREATE TABLE [tmp_Last] ( [FEntryID] [int] NOT NULL , [FBillNo] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL , [FNumber] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL , [FQty] [decimal](18, 4) NOT NULL , [FBatchNo] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL , [FSPID] [int] NOT NULL ) ON [PRIMARY] GO if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[tmp_Source]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [dbo].[tmp_Source] if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[tmp_Batch]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [dbo].[tmp_Batch] Select nOutBillSeq As FEntryID , cOutBillNo As FBillNo , FNumber , nQua As FQty , FStockPlaceID As FSPID Into tmp_Source From Czq092801 Select FNumber ,FBatchNo , FQty , FStockID , FStockPlaceID As FSPID Into tmp_Batch From Czq092802 ALTER TABLE tmp_Batch ADD FDetailID int IDENTITY (1, 1) NOT NULL go Update tmp_Batch Set FSPID = 464 where FNumber = 'E.A.AZ9719410054' Update tmp_Batch Set FSPID = 464 where FNumber = 'E.A.880410054' Update tmp_Batch Set FSPID = 465 Where fnumber = 'E.A.AZ9719410054' Update tmp_Source Set FSPID = t.FSPID From tmp_Batch t Where tmp_Source.FNumber = t.FNumber --以下是把批号拆分的游标 declare @EntryID int --定义变量行号 declare @BillNo varchar(100) --定义变量单据号 declare @Number varchar(100) --定义变量物料代码 declare @qty decimal(18,6) --定义变量数量 declare @SPID int --定义变量仓位代码 declare @BatchNo varchar(100) --定义变量批号 declare @DID int declare @BQty decimal(18,6) Declare subgroup Cursor For Select FEntryID, FBillNo , FNumber , FQty , FSPID From tmp_Source Open subgroup Fetch Next From subgroup Into @EntryID , @BillNo , @Number , @qty , @SPID While @@FETCH_STATUS = 0 Begin re: set @DID = 0 set @DID = (Select min(FDetailID) From tmp_Batch Where FNumber = @Number And FSPID = @SPID And FQty <> 0 ) set @BatchNo =(Select FBatchNo From tmp_Batch Where FDetailID = @DID ) Set @BQty =(Select FQty From tmp_Batch Where FDetailID = @DID ) print @BillNo print @EntryID if @BQty >= @Qty Begin Insert Into tmp_Last values (@EntryID , @BillNo , @Number , @Qty , @BatchNo , @SPID) Update tmp_Batch Set FQty = FQty - @Qty Where FDetailID = @DID print -1 Select -1 print @qty end Else Begin Insert Into tmp_Last values (@EntryID , @BillNo , @Number , @BQty , @BatchNo , @SPID) Update tmp_Batch Set FQty = FQty - @BQty Where FDetailID = @DID set @Qty = @Qty - @BQty print 1 print @qty goto re --返回到re: End print '888888' Fetch Next From subgroup Into @EntryID , @BillNo , @Number , @qty , @SPID End Close subgroup Deallocate subgroup --释放游标 ----------------------------------------------------------------- --更新行号 ALTER TABLE tmp_Last ADD FDetailID int IDENTITY (1, 1) NOT NULL ALTER TABLE tmp_Last ADD FNewEntryID int go declare @DID int declare @BillNo varchar(100) declare @i int Declare subgroup Cursor For Select Distinct FBillNo From tmp_Last Open subgroup Fetch Next From subgroup Into @BillNo While @@FETCH_STATUS = 0 Begin set @i =1 Declare detail Cursor For Select FDetailID From tmp_Last Where FBillNo = @BillNo Open detail Fetch Next From detail Into @DID --可以认为FDetailID的值附给了@DID While @@FETCH_STATUS = 0 Begin update tmp_Last Set FNewEntryID = @i Where FDetailID = @DID set @i = @i + 1 Fetch Next From detail Into @DID End Close detail Deallocate detail Fetch Next From subgroup Into @BillNo End Close subgroup Deallocate subgroup Select * From tmp_Last ------------------------------------------------------------------- /* Select FNumber,Sum(FQty) FQty From tmp_Last Group by FNumber Select FBillNo,Sum(FQty) FQty From tmp_Last Group by FBillNo Select FNumber,Sum(nQua) nQua From Czq092801 Group by FNumber Select cOutBillNo,Sum(nQua) nQua From Czq092801 Group by cOutBillNo */
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值