Avatar of looknow12
looknow12

asked on 

Cursorfetch: The number of variables declared in the INTO list must match that of selected columns

I'm receiving the error Cursorfetch: The number of variables declared in the INTO list must match that of selected columns when it appears this stored procedure tries to go to the next record.

I've noted where the problem is occurring.

Thank you,
alter PROCEDURE [dbo].[SendMyMails]
AS
BEGIN
 SET NOCOUNT ON;

DECLARE @recipients varchar(20),
   @subject varchar(50),
   @body varchar(1000),
   @custid varchar (8), 
   @firstname varchar (100), 
   @lastname varchar(100), 
   @ccexpdate varchar(8), 
   @bmonthlyactive varchar(1), 
   @dtmonthlynextpay varchar (50),
   @dtmonthlypaid varchar (50), 
   @iemailoptin varchar (8), 
   @recipientemail varchar (80), 
   @sbarcode varchar (50), 
   @monthlyaccountid varchar (50), 
   @monthlyaccountdescription  varchar (250),
   @monthlyamount varchar(12),
   @bodyformat varchar(12)
   
DECLARE Mail_Cursor CURSOR FOR
select C.lcustomerid, c.sFirstName,c.sLastName,c.sCCExpDate,c.bMonthlyActive,c.dtMonthlyNextPay,c.dtMonthlyPaid,c.iEmailOptIn,c.sEmail,c.sBarcode,c.lMonthlyAccountTypeId,v.sDescription,v.dblAmount
		from Custtable as c
		join AccountTypes as v on v.laccounttype = c.lMonthlyAccountTypeId
		
		where bMonthlyActive = 1 and dtMonthlyPaid < Convert(datetime, Convert(int, GetDate())) and iEmailOptIn = '1'		
		

OPEN Mail_Cursor;
FETCH NEXT FROM Mail_Cursor
 INTO @custid, @firstname, @lastname, @ccexpdate, @bmonthlyactive, @dtmonthlynextpay, @dtmonthlypaid, @iemailoptin, @recipientemail, @sbarcode, @monthlyaccountid, @monthlyaccountdescription,@monthlyamount ;

declare @tmp_subject varchar(1000)
declare @tmp_body varchar(1000)

WHILE @@FETCH_STATUS = 0
   BEGIN
	
	set @tmp_subject = 'Your '+@monthlyaccountdescription+' Program at the Acme Company'
	set @tmp_body = '<p>Dear '+@firstname+' '+@lastname+',</p></p>
				   <p>Your credit card ending with an expiration date of '+SUBSTRING(ltrim(rtrim(@ccexpdate)),1,2)+'/'+SUBSTRING(ltrim(rtrim(@ccexpdate)),3,2)+' has been charged in the amount of $'+@monthlyamount+' for the '+@monthlyaccountdescription+'.</p>
				   <p>Thank you for your business.</p>'
    --Sending Mail
    EXEC msdb.dbo.sp_send_dbmail @recipients = @recipientemail,
          @subject = @tmp_subject,
          @body = @tmp_body,
          @body_format = 'HTML';
             
      FETCH NEXT FROM Mail_Cursor
Problem here --->>      INTO @recipients, @subject, @body ;
   END;
   
CLOSE Mail_Cursor;
DEALLOCATE Mail_Cursor;
END

Open in new window

Microsoft SQL Server

Avatar of undefined
Last Comment
rocky_lotus_newbie
ASKER CERTIFIED SOLUTION
Avatar of JuanchoVzla
JuanchoVzla
Flag of Venezuela, Bolivarian Republic of image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
You need to have the same fetch statement right before the END in the Cursor


FETCH NEXT FROM Mail_Cursor
 INTO @custid, @firstname, @lastname, @ccexpdate, @bmonthlyactive, @dtmonthlynextpay, @dtmonthlypaid, @iemailoptin, @recipientemail, @sbarcode, @monthlyaccountid, @monthlyaccountdescription,@monthlyamount ;

<<Where you mentioned "Problem here --->>" >>

Thanks,
Microsoft SQL Server
Microsoft SQL Server

Microsoft SQL Server is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.SQL Server is available in multiple versions, typically identified by release year, and versions are subdivided into editions to distinguish between product functionality. Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning.

171K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo