起因:游标执行存储过程 下载begin 外面了.. ,造成一直触发存储过程
收获:定义变量统一在游标外部使用, 书写内容在begin 内部书写
alter PROCEDURE USP_dgd_wzh_INPUT_SAVE @inputrowidhd VARCHAR(10), @doccode varchar(30) as DECLARE @rowid varchar(40) begin --游标更改数据 --申明一个游标 DECLARE @po_no varchar(50),@modelcode varchar(50),@size_state varchar(50),@rec_date datetime,@plan_date datetime, @exdate datetime,@lot_date datetime,@to_country varchar(50) --循环变量 DECLARE MyCursor CURSOR FOR SELECT po_no,modelcode,size_state,rec_date,plan_date,exdate,lot_date,to_country FROM po_order_input where inputrowid = @inputrowidhd and size_state !='码制' --循环一个游标 --打开一个游标 OPEN MyCursor FETCH NEXT FROM MyCursor INTO @po_no,@modelcode,@size_state,@rec_date,@plan_date,@exdate,@lot_date ,@to_country WHILE @@FETCH_STATUS =0 BEGIN exec getxxxx @rowid output INSERT INTO wzh_ddgl_item(Doccode,rowid,ordercode,modelcode,size_state,Orderdate,Originaldate,postdate,lot_date,Portmsg) values (@doccode,@rowid, @po_no,@modelcode,@size_state,@rec_date,@plan_date,@exdate,@lot_date ,@to_country) FETCH NEXT FROM MyCursor INTO @po_no,@modelcode,@size_state,@rec_date,@plan_date,@exdate,@lot_date ,@to_country END --关闭游标 CLOSE MyCursor --释放资源 DEALLOCATE MyCursor end
太久不用忘记游标的写法了
DECLARE cur CURSOR FOR SELECT DISTINCT mtl_no FROM pc_mtl_staytest_item WHERE doccode = @doccode OPEN cur FETCH NEXT FROM cur INTO @mtl_no WHILE(@@FETCH_STATUS = 0 ) BEGIN SET @cnt = ISNULL(@cnt,0) + 1 -- 循环代码 IF @cnt >= 100 BEGIN BREAK END FETCH NEXT FROM cur INTO @mtl_no END CLOSE cur DEALLOCATE cur
如果确实死循环了,需要删掉死锁进程,查出死锁的进出
SELECT request_session_id spid, OBJECT_NAME(resource_associated_entity_id) tableName
FROM sys.dm_tran_locks WITH(NOLOCK)
WHERE resource_type='OBJECT'
然后 KILL + 进程号
年与时驰,意与日去,遂成枯落,
多不接世,悲守穷庐,将复何及。