遗忘海岸

江湖程序员 -Feiph(LM战士)

导航

< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8

统计

游标,存储过程,以及临时表

需要在SQL端实现业务逻辑,NND (-_-")

代码一:

ALTER proc [dbo].[tt_select]
@Id bigint
as
select PlanId,FGPartNo,p_FGName from backflush where Id<@Id

 代码二:

Declare @Id bigint
Set @Id=12
 
Declare @PlanId nvarchar(100),@FGPartNo nvarchar(100),@FGName nvarchar(100)
 
--确保临时表已经被删除了
IF object_id('tempdb..#t_bf_tmp') IS NOT NULL
  Begin
    Drop Table #t_bf_tmp
  End
 
--定义临时表
Create Table #t_bf_tmp
(
  PlanId nvarchar(50),
  FGPartNo nvarchar(50) ,
  p_FGName nvarchar(50)
)
--将存储过程的执行结果添加到临时表
Insert Into #t_bf_tmp
Exec tt_select @Id
 
 
--使用游标遍历临时表,并做对于处理
Declare MyCursor Cursor
For Select * from #t_bf_tmp
For Read Only
 
Open MyCursor
 
Fetch next From MyCursor
   Into @PlanId,@FGPartNo,@FGName
 
while(@@FETCH_STATUS = 0)
Begin
 print @PlanId 
 
 Fetch next From MyCursor
   Into @PlanId,@FGPartNo,@FGName
End
 
Close MyCursor
Deallocate MyCursor
 
Drop Table #t_bf_tmp

 

 

posted on   遗忘海岸  阅读(432)  评论(0编辑  收藏  举报

努力加载评论中...
点击右上角即可分享
微信分享提示