Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
1.50/5 (3 votes)
See more:
SQL
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO

ALTER proc [dbo].[shortcode_coursebookingrpt] (@Fromdate varchar(20),@Todate varchar(20))
as 
begin

declare
   @stud_id varchar(10),
   @Mobileno varchar(15),
   @Messagetext varchar(10),
   @Msgdelivered  varchar(20),
   @Replymsg varchar(200)     

create table #TempTable(stud_id varchar(10),Mobileno varchar(100),Messagetext varchar(20),Msgdelivered  varchar(20),Replymsg varchar(200)) 

begin tran
declare short cursor for 
   select Message,Mobileno,MSgdelivered,Replymsg from Shortcode_Course_SMS where Msgdelivered > @Fromdate and Msgdelivered < @Todate

open short 
fetch next from short into @Msgdelivered
	While @@Fetch_status = 0
	   begin
		 begin tran
		   declare coursebooked cursor for  
               select s.stud_id,s.stud_mobile,sh.Mobileno,cbm.cmn_minor_code,
               case when cbm.cmn_minor_code = sh.Message then 'Booked' end as Booked_Status,
               case when cbm.cmn_minor_code <> sh.Message then 'Not Booked' end as UnBooked_Status
               from STUDENT s,course_registration cr,co_batch_master cbm,Shortcode_Course_SMS sh,batch_course_registration bcr 
               where cr.stud_id = s.stud_id and substring(sh.mobileno,3,20) = s.stud_mobile and sh.Message = cbm.cmn_minor_code and 
               bcr.cr_bill_no = cr.cr_bill_no and cbm.cbm_batch_id=bcr.bcr_batch_id 
               and cr.cr_active = 'A'  and cbm.cbm_active <> 'D' 
           open coursebooked 
                fetch next from coursebooked into @stud_id,@Mobileno
					while @@Fetch_status = 0
					begin
                        insert into #TempTable values(@stud_id,@Mobileno,@Messagetext,@Msgdelivered,@Replymsg)  
					fetch next from coursebooked into @stud_id,@Mobileno
					end 			
				close coursebooked
				deallocate coursebooked
			commit tran
     fetch next from short into @Msgdelivered
     end	
	close short
	deallocate short
	commit tran
select * from #TempTable
end

When i execute the above store procedure shows error as follows

exec [shortcode_coursebookingrpt] '20140205','20140209'
 Cursorfetch: The number of variables declared in the INTO list must match that of selected columns.

please help me.what is the problem in my above store procedure.

Regards,
Narasiman P.
Posted
Updated 15-Apr-14 2:09am
v2

sanket saxena is correct.

Specifically look at these lines
SQL
declare short cursor for
   select Message,Mobileno,MSgdelivered,Replymsg 
from Shortcode_Course_SMS where Msgdelivered > @Fromdate and Msgdelivered < @Todate

open short
fetch next from short into @Msgdelivered

The bits in bold are important.
You have declared that your cursor will be retrieving four items from each row, but when you do the fetch you have only supplied one "holder" (variable) for the results. Four into One does not go!

Either change the cursor declaration to
SQL
declare short cursor for
   select MSgdelivered from Shortcode_Course_SMS where Msgdelivered > @Fromdate and Msgdelivered < @Todate


OR, provide enough variables when you do the FETCH
SQL
open short
fetch next from short into @Message, @Mobileno, @Msgdelivered, @ReplyMsg


You will need to do something similar with the other cursors too
 
Share this answer
 
Comments
Sanket Saxena 15-Apr-14 8:24am    
Thanks CHill60...
Seems like your SELECT Columns are not same as INTO columns in the cursor. Hope it helps :)
 
Share this answer
 
Hi,

Your SELECT and FETCH...INTO are not matching.

Try this:
SQL
declare short cursor for 
select Message,Mobileno,MSgdelivered,Replymsg from Shortcode_Course_SMS where Msgdelivered > @Fromdate and Msgdelivered < @Todate

SQL
declare coursebooked cursor for 
select s.stud_id,s.stud_mobile,sh.Mobileno,cbm.cmn_minor_code,
case when cbm.cmn_minor_code = sh.Message then 'Booked' end as Booked_Status,
case when cbm.cmn_minor_code <> sh.Message then 'Not Booked' end as UnBooked_Status
from STUDENT s,course_registration cr,co_batch_master cbm,Shortcode_Course_SMS sh,batch_course_registration bcr 
where cr.stud_id = s.stud_id and substring(sh.mobileno,3,20) = s.stud_mobile and sh.Message = cbm.cmn_minor_code and 
bcr.cr_bill_no = cr.cr_bill_no and cbm.cbm_batch_id=bcr.bcr_batch_id 
and cr.cr_active = 'A' and cbm.cbm_active <> 'D' 

Selected columns and fetched into variables must match!
 
Share this answer
 
v2
Comments
Sanket Saxena 15-Apr-14 8:19am    
Yes this is what i suggest
Andrius Leonavicius 15-Apr-14 8:31am    
You're right, sanket. Actually, I haven't noticed your solution because I haven't refreshed the page before I submitted my answer...
CHill60 15-Apr-14 8:42am    
ditto with me and your solution ... I think we should blame the hamsters ;-)
Andrius Leonavicius 15-Apr-14 8:48am    
:)
Sanket Saxena 15-Apr-14 8:36am    
No problem andrius..it happens.. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900