|
也提供一個在sql中怎樣使用指針的例子.
USE PROMIS
Begin Tran
declare @docid varchar(10) --宣告文件編號
declare @ToDate datetime --宣告日期
set @ToDate=Getdate()
set @docid='IR0000001'
declare RawminDetail Scroll cursor --宣告指針及指針來源
for
select LotID,DocID,PODetailID,Item_No,Quantity,Weight,Result,Result_Confirm,
To_LocID,Area
from tbl_Store_Rawmin_Detail
where docid='IR0000001' and Result_Confirm=0 and Result>0
open RawminDetail --打開指針
declare @Lotid varchar(10),@Docids varchar(10),@PODetailID int,@Item_NO varchar(30),@Quantity numeric(18,4),
@weight numeric(18,4),@Result int,@Result_Confirm bit,@To_LocID varchar(5),@Area varchar(6)
Fetch next from RawminDetail --一筆一筆地向后讀取記錄
into @Lotid,@Docids,@PODetailID,@Item_NO,@Quantity,@weight ,@Result ,@Result_Confirm ,@To_LocID ,@Area
WHILE (@@Fetch_Status=0) --循環指標集直到指針為零時
Begin
--查找該指針的當筆記錄在tbl_Store_QOH中的記錄數, 如果不存在記錄則將該指標插入. 存在則更改數量.
Select Locid,Item_NO from tbl_Store_QOH where (Locid=@To_Locid) and (Item_NO=@Item_NO)
if @@rowcount =0
begin
insert tbl_Store_QOH (Item_NO,Locid,QOH,Lotid)
values(@Item_NO,@To_Locid,@Quantity,@Lotid)
end
else
begin
update dbo.tbl_Store_QOH
set QOH=QOH+@Quantity,LotID=@LotID
Where (LocID=@To_LocID) and (Item_NO=@Item_NO)
end
--將記錄插入到tbl_Store_TranDtl表中
declare @QOH numeric(18,4)
Set @QOH=(Select QOH from tbl_Store_QOH where (Locid=@To_Locid) and (Item_NO=@Item_NO))
insert tbl_Store_TranDtl(TranDtlID,Lotid,LocID,DocID,TransactType,Item_NO,Quantity,QOH,Weight,Tran_Time,Area)
values(@Lotid,@Lotid,@To_Locid,@DocID,'IN' ,@Item_NO,@Quantity, @QOH, @Weight,@ToDate,@Area)
--更新tbl_Purch_PO_Detail采購單細節表中的ReceQuantity已收數量, Recedate最后收貨日期, Finish是否完成
update tbl_Purch_PO_Detail
set ReceDate=@ToDate,ReceQuantity=ReceQuantity+@Quantity,
Finish=Case when ReceQuantity+@Quantity=Quantity then 1 else 0 end
where DetailID=@PODetailID
--更新本筆指針記錄Result_Confirm是否批核過帳
update tbl_Store_RawMin_Detail
set Result_Confirm=1
Where Current of RawminDetail
Fetch next from RawminDetail --一筆一筆地向后讀取記錄
into @Lotid,@Docids,@PODetailID,@Item_NO,@Quantity,@weight ,@Result ,
@Result_Confirm ,@To_LocID ,@Area
End
if @@Error >0
Rollback Tran
else
COMMIT TRAN
close RawminDetail --關閉指針
deallocate RawminDetail --移除指針
--Fetch Next from RawminDetail --到下一筆
--Fetch Prior from RawminDetail --到前一筆
--Fetch Absolute 1 from RawminDetail --到指定的筆
--Fetch Relative 3 from RawminDetail --目前位置向后跳躍位置
--Fetch Last from RawminDetail --到最后一筆
--Select @@cursor_rows --讀取指標筆數(測試用) |
|