Cusorfetch错误 - 进入列表中太多了

时间:2013-10-23 14:40:40

标签: sql sql-server

请有人可以帮忙解决下面问题 - 我上周发布了这个帖子,我确实让代码运行但有人已经删除了代码。有人可以帮忙吗?

我得到的错误是 -

Msg 16924,Level 16,State 1,Line 43 Cursorfetch:INTO列表中声明的变量数必须与所选列的变量数相匹配。

  -- declare cursor variables
  declare @PKCustomerRecord int, @Agent varchar(100), @CallBackDateTime datetime
  declare @Title varchar(15), @Forename varchar(30), @Surname varchar(30), 
          @Address1 varchar(50), @Address2 varchar(50), @Address3 varchar(50), @Address4 varchar(50), @Address5 varchar(50), 
          @PostCode varchar(10), @ContactNumber varchar(16), @Telephone2 varchar(16), 
          @DOB datetime, @Source varchar(30), @list_id int, @user_group varchar(8), @campaign_id varchar(8), @ViciDialID int, 
          @PKUsers int, @LeadId int, @ViciUser varchar(20), @Comments varchar(max)


  -- declare cursor
 declare csr cursor for 
  select 
        ViciDialID, PKUsers, CallBackDateTime, CallBackDateTime, [user] as Agent,
        ci.PKCustomerRecord, ci.Title, ci.Forename, ci.Surname,
        ci.Address1, ci.Address2, ci.Address3, ci.Address4, ci.Address5, ci.PostCode,
        ci.DOB, case when ci.Source = 'CB' then ci.ContactLoadSource else ci.Source end as Source,
        cb.ContactNumber,
        case when cb.ContactNumber COLLATE DATABASE_DEFAULT <> ci.Telephone1 then ci.Telephone1 
        when cb.ContactNumber COLLATE DATABASE_DEFAULT <> Telephone2 then Telephone2 end as Telephone2,
        case 
        when ci.Source = 'SASurvey' then 8890 
        when ci.Source = 'AAR' then 8891
        when ci.Source = 'CB' then 8893
        when ci.Source = 'RTA' then 8894
        when ci.Source = 'Verve Survey' then 8895
        when ci.Source = 'Unregistered' then 8892 end as list_id, user_group, campaign_id,
        cb.Comments
  from CAGSQL.CallCenter.dbo.CallBacks cb with (nolock)
  join #ViciDialData u with (nolock) on cb.UserFK = u.PKUsers
  join CAGSQL.ConsumerCare.dbo.ContactInfo ci with (nolock) on cb.ContactInfoFK = ci.PKCustomerRecord  
  where PoolFK = '173'
  and CallBackDateTime >= getdate()  
  and CallBackDateTime <= convert(varchar,getdate()+1,112)  
  and UpdatedDate is null 
  and Source in ('SASurvey','AAR','CB','RTA','Verve Survey','Unregistered')
  and DataLength(Comments) < 255
  and UpdatedDate is null 
  and PKUsers <> 4678


  open csr
  -- fetch first record
  fetch next from csr into @PKCustomerRecord, @Agent, @CallBackDateTime, @Title, @Forename, @Surname, 
        @Address1, @Address2, @Address3, @Address4, @Address5, @PostCode, @ContactNumber, @Telephone2, @DOB, @Source, @list_id, @user_group, @campaign_id,
        @ViciDialID, @Comments

  while @@fetch_status = 0
  begin 



        if left(@ContactNumber, 1) = '0' set @ContactNumber = substring(@ContactNumber,2,11)
        if left(@Telephone2, 1) = '0' set @Telephone2 = substring(@Telephone2,2,11)

        insert into VICI...vicidial_list
        (status, entry_date, status, [user],  list_id, gmt_offset_now, called_since_last_reset, phone_code, phone_number, alt_phone, title, 
        first_name, last_name, address1, address2, address3, city, postal_code, date_of_birth, entry_list_id, source_id)
        values
        ('NEW', getdate(), 'CBHOLD', '', @list_id, 1, 'N', '44', @ContactNumber, @Telephone2, @Title, 
        @Forename, @Surname, @Address1, @Address2, @Address3, @address4, @PostCode, @DOB, @list_id, @Source)

        -- get the ID of the record just inserted
        select @LeadId = max(lead_id) from VICI...vicidial_list

        -- insert the callback record into Vici dial
        insert into VICI...vicidial_callbacks
        (lead_id, list_id, campaign_id, status, entry_time, callback_time, modify_date, [user], recipient, 
        comments, user_group, lead_status)
        values 
        (@LeadId, @list_id, 'CCWarm', 'ACTIVE', getdate(), @CallBackDateTime, getdate(), @ViciUser, 'USERONLY', 
        @Comments, 'CCWarm', 'CALLBK')


        -- fetch next record from CURSOR
        fetch next from csr into @PKCustomerRecord, @Agent, @CallBackDateTime, @Title, @Forename, @Surname, 
        @Address1, @Address2, @Address3, @Address4, @Address5, @PostCode, @ContactNumber, @Telephone2, @DOB, @Source, @list_id, @user_group, @campaign_id,
        @ViciDialID, @PKUsers, @Comments

  end -- END OF CURSOR LOOP

  close csr
  deallocate csr

提前致谢, 森

1 个答案:

答案 0 :(得分:1)

您为光标提供的列数(在“CURSOR FOR”文本之后)必须与您为获取新记录所定义的参数计数匹配(在“FETCH NEXT FROM csr INTO”文本之后)

定义为23个列进入游标,其中22个与参数匹配。