存储过程的游标或循环

时间:2014-03-26 16:12:19

标签: sql loops cursor

我有一张表中有记录。其中一个字段是数量字段。对于那一行,我必须将相同的行放在另一个表中的数量字段中。我还必须从第三个表中获取使每个行唯一的数字赋值。

所以

tblrecnumassignments - 分配号码

claimstoexplode - 每行数量爆炸的行

声明 - 每个记录将驻留

这是我的三张桌子。

我可以在一行中插入数量的数量。但它使用相同的rec num,我如何让它去爆炸声明的下一行。我迷失了,我的老板跟着我完成了这件事。

SET ARITHABORT OFF SET ANSI_WARNINGS OFF

declare @recnum as varchar(100), 
        @lngrecnum as varchar(100), 
        @vwclaimscount as int, 
        @count as int

select @vwclaimscount = COUNT([dlr num]) from claimstoexplode --outer loop number
select @lngrecnum = MAX([rec num] + 1) from tmpClaims_RecNumAssignment 

print @lngrecnum

select @count = claimstoexplode.qty from dbo.claimstoexplode --inner loop




begin

if @vwclaimscount > 0


insert into tmpClaims_RecNumAssignment
( operator, dtmadded)
values ('explode5', getdate())


insert into tmpclaims1 
([Dlr Num]
   ,[Promo]
   ,[Trans Code]
   ,[Proc Date]
   ,[Control Num]
   ,[Invoice Num]
   ,[Clm Date]
   ,[Amt Claimed]
   ,[Amt Appr]
   ,[Amt Paid]
   ,[Ad From]
   ,[Ad To]
   ,[Media]
   ,[Media Desc]
   ,[Audit 1]
   ,[Audit 2]
   ,[Audit 3]
   ,[Audit 4]
   ,[Audit 5]
   ,[Audit 6]
   ,[Prod Code 1]
   ,[Prod Code 1 %]
   ,[Prod Code 1 Amt]
   ,[Prod Code 1 Adv Amt]
   ,[Prod Code 2]
   ,[Prod Code 2 %]
   ,[Prod Code 2 Amt]
   ,[Prod Code 2 Adv Amt]
   ,[Comment]
   ,[Received Date]
   ,[Last Modified]
   ,[Ratio]
   ,[Number Of Ads]
   ,[Amt Applied]
   ,[UM]
   ,[Audited Amt]
   ,[Rated Ads]
   ,[Branch Number]
   ,[Operator]
   ,[Bal Pending]
   ,[Paid From Pending]
   ,[Store Num]
   ,[Clm Spec 1]
   ,[Clm Spec 2]
   ,[Status]
   ,[Rec Num]
   ,[Doc Type] 
   ,locked

)

select 
[dlr num]
,prognum
,'X'
,GETDATE()
,'0'
,''
,[received date]
,round([claimed amt]/qty,2)
,'0'
,'0'
,dtmfrom
,dtmto
,'9'
,  dbo.Promos.[Promo Misc 1]
         ,'01'
,''
,''
,''
,''
,''
   ,[Product]

,'0'
,'0'
,'0'
,bundlemodel
,'0'
,'0'
,'0'
,''
,[received date]
,''

,'100'
,'1'
,'0'
,'0'
,'0'
,'0'
,'0'
,claimstoexplode.operator
,'0'
,'0'
,'0'
,LEFT(reference,10)
,[record number]
,'1'
, @lngrecnum ,'L' ,'1' from claimstoexplode inner join
dbo.promos ON dbo.Promos.Promo = dbo.claimstoexplode.prognum

set @vwclaimscount = @vwclaimscount - 1
set @lngrecnum = @lngrecnum + 1
set @count = @count - 1

结束

0 个答案:

没有答案