日期:2014-05-18 浏览次数:20581 次
declare @Row int, @Rows int, @DANo char(23), @DATime datetime, @LogTime datetime, @MeterType char(4), @MeterNo char(20), @Qty decimal(18,6) declare @t table ( Row int identity(1,1) not null, DANo char(23) not null, DATime datetime not null, LogTime datetime not null, MeterType char(4) not null, MeterNo char(20) not null, Qty decimal(18,6) null ) insert into @t select DANo,DATime,LogTime,MeterType,MeterNo,Qty set @Rows = @@ROWCOUNT set @Row = 1 while (@Row <=@Rows) begin select @DANo = DANo,@DATime= DATime,@LogTime= LogTime,@MeterType = MeterType,@MeterNo = MeterNo ,@Qty = Qty from @T where row = @Row exec Usp_DAListForEnergyDataDetail @DANo,@DATime,@LogTime,@MeterType,@MeterNo,@Qty set @Row = @Row + 1 end
------解决方案--------------------
问题1:这个叫表变量
问题2:@@ROWCOUNT 是SQL Server全局变量,表示执行上一语句所影响的行数
当你插入数据为10行时 @@ROWCOUNT =10
------解决方案--------------------
--当然可以 declare cursor_test cursor local forward_only static read_only for select DANo,DATime,LogTime,MeterType,MeterNo,Qty from table_name open cursor_test fetch next from cursor_test into @DANo,@DATime,@LogTime,@MeterType,@MeterNo,@Qty while(@@fetch_status=0) begin exec Usp_DAListForEnergyDataDetail @DANo,@DATime,@LogTime,@MeterType,@MeterNo,@Qty fetch next from cursor_test into @DANo,@DATime,@LogTime,@MeterType,@MeterNo,@Qty end close cursor_test deallocate cursor_test
------解决方案--------------------
不建议使用游标
1:游标效率低
2:游标会造成锁表
3:除非在很难处理的地方,否则不使用游标
------解决方案--------------------