首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在嵌套的While循环中插入

在嵌套的While循环中插入
EN

Stack Overflow用户
提问于 2017-09-28 18:05:17
回答 1查看 325关注 0票数 0

我尝试在嵌套中插入记录,而循环.Below是我在SQL server中的存储过程,它只插入两条记录,它应该插入4条记录。这里出了什么问题。提前谢谢。

代码语言:javascript
复制
create proc Insert_LeadDistributionClient   
--Insert_LeadDistributionClient   
@LeadDistribution_code varchar(1000) = '100,101'  
,@Client_code varchar(1000) ='1,2'  
,@medium_code varchar(1000) = '10,11'  
as  
begin  
declare @LeadDistributionLength int  
declare @ClientLength int
declare @MediumLength int

declare @SingleLeadDistCode int  
declare @SingleClientCode int  
declare @SingleMediumCode int  

select * into #LeadDistributionCode from dbo.Split(@LeadDistribution_code , ',')  
select * into #ClientCode from dbo.Split(@Client_code , ',')  
select * into #MediumCode from dbo.Split(@medium_code , ',')  

set @LeadDistributionLength = (select count(*) from #LeadDistributionCode)  
set @ClientLength = (select count(*) from #ClientCode)  
set @MediumLength = (select count(*) from #MediumCode)  

while(@ClientLength <> 0 )
begin
  set @SingleClientCode = (select top 1 [data] from #ClientCode)
  while(@LeadDistributionLength <> 0)
  begin
    set @SingleLeadDistCode = (select top 1 [data] from #LeadDistributionCode)
    insert into leaddistributionclient(LeadDistribution_code,Client_code,Medium_Code , LeadRatio_Count , CurrentLead_Count,Is_Sent)  
    select  @SingleLeadDistCode,@SingleClientCode,1 ,0 ,0 ,0  
    delete top (1) from #LeadDistributionCode
    set @LeadDistributionLength = @LeadDistributionLength -1
  end
  delete top (1) from #ClientCode
  set @ClientLength = @ClientLength - 1
end


drop table #LeadDistributionCode  
drop table #ClientCode  
drop table #MediumCode  


end
EN

回答 1

Stack Overflow用户

发布于 2017-09-28 18:27:28

删除此行

代码语言:javascript
复制
delete top (1) from #LeadDistributionCode

在first while的开头添加此行

代码语言:javascript
复制
set @LeadDistributionLength = (select count(*) from #LeadDistributionCode) 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46466419

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档