Solved

Stored Procedure cursorFetch error on insert

Posted on 2010-09-02
6
353 Views
Last Modified: 2012-05-10
The follwoing is my stored procedure that grabs sql code sittng in tables to run.  What I did not figure in that if the sql code finds nothing I get the following error.

Msg 16924, Level 16, State 1, Line 42
Cursorfetch: The number of variables declared in the INTO list must match that of selected columns.

I am guessing becaue the code found nothing and it wants to insert something, How can I change the Stored procedure to ignore this and keep going if it finds nothing.  Becaue finding nothing is good in this case.
declare @counter int, 
		@countfilter int, 
		@FilterID int, 
		@AssignedID int, 
		@UnitID int, 
		@DeployDate datetime,
		@TaskName varchar(60), 
		@Proponent varchar(60),
		@ApproveLev varchar(15),
		@Days int, 
		@Sql nvarchar(4000), 
		@PositionId int, 
		@Para varchar(4), 
		@Line varchar(3), 
		@Posn varchar(60), 
		@Sid varchar(60), 
		@Name varchar(60), 
		@SSN varchar(10), 
		@cur cursor, 
		@taskID int 
 
Declare Vend_Cursor CURSOR FOR Select uf.intFilterId, uf.intAssignedID, uf.intUnitMobId, ud.dtdeployDate,
										f.strTaskname, f.strProponent, f.strApprovalLevel, f.strSusDays, f.strSql 
										from tblSoldierFilters as uf Inner join tblUnitDeployData as ud on 
										ud.intUnitMobId = uf.intUnitMobId Inner Join tblFilter as f on 
										f.intFilterID = uf.intFilterId where uf.dtRemoved is NULL 
										and ud.intUnitMobId = 256 and f.intFilterID = 70
										order by uf.intfilterID, uf.intAssignedID, ud.intUnitMobId

OPEN Vend_Cursor

FETCH NEXT FROM Vend_Cursor INTO @FilterID,@AssignedId,@UnitId,@DeployDate,@Taskname,@Proponent,@ApproveLev,@Days,@Sql
 
while @@FETCH_STATUS =0 
begin
	--- Call the SQL Filter and assign the unit # as the variable to get at table	
	
	set @Sql = N'set @cur = cursor for ' + @Sql + Cast(@UnitID as varchar) + ';open @cur'

	exec sp_executesql @Sql, N'@cur cursor OUTPUT', @cur output

	fetch next from @cur into @PositionId, @Para, @Line, @Posn, @Sid, @Name, @SSN
	
	while @@FETCH_STATUS =0 
	begin
		set @taskID = (select intTaskId from tblTask where intFilterId = @FilterID and intPositionId = @PositionId)
		
		if (select isnull(@taskID, 0)) = 0
		begin
			Insert Into tblTask (intAssignedId, intFilterId, intPositionID, strTaskName, dtFound, strStatus, 
						dtStatus, strSSN, strName, strPositionData, strSoldierData, strProponent, dtNotification,
						dtCompleteTask) 
			VALUES (@AssignedId, @FilterID, @PositionId, @Taskname, Getdate(), @ApproveLev, Getdate(),@SSN, 
					@Name, @Posn, @Sid, @Proponent, Getdate(), dateadd(DAY, -@Days, @DeployDate)) 
		end
		
		fetch next from @cur into @PositionId, @Para, @Line, @Posn, @Sid, @Name, @SSN
	end
	
	close @cur
	deallocate @cur	
	  
	FETCH NEXT FROM Vend_Cursor INTO @FilterID,@AssignedId,@UnitId,@DeployDate,@Taskname,@Proponent,@ApproveLev,@Days,@Sql
end

Close vend_Cursor
deallocate vend_Cursor

Open in new window

0
Comment
Question by:kdeutsch
  • 3
  • 3
6 Comments
 
LVL 8

Accepted Solution

by:
Mohit Vijay earned 500 total points
ID: 33589238
here is problem

set @Sql = N'set @cur = cursor for ' + @Sql + Cast(@UnitID as varchar) + ';open @cur'

        exec sp_executesql @Sql, N'@cur cursor OUTPUT', @cur output

        fetch next from @cur into @PositionId, @Para, @Line, @Posn, @Sid, @Name, @SSN


check number of fields in select list and total number of into fields
0
 

Author Comment

by:kdeutsch
ID: 33589523
VjSoft:,

This has worked this way before,  I thought it was on the insert statement when it finds nothing.  I don't see how to check it, since I don't see what it is grabbing.
0
 
LVL 8

Expert Comment

by:Mohit Vijay
ID: 33589578
below line set @Sql = N'set @cur = cursor for ' + @Sql + Cast(@UnitID as varchar) + ';open @cur'

write

print @SQL

and see what is printing in Message window
0
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 

Author Comment

by:kdeutsch
ID: 33589610
VjSoft:,

ok did a response.write on teh sql statement and yep that is the cause of the problem, I will have to get with the person to fix it.
THanks
0
 
LVL 8

Expert Comment

by:Mohit Vijay
ID: 33589616
Great to hear :)
0
 

Author Closing Comment

by:kdeutsch
ID: 33589833
Thanks, I have been banging my head on this for days.
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
query execution hang 5 28
T-SQL: "HAVING CASE" Clause 1 23
Unable to Uninstall Visual Studio 2015 7 23
Can > be used for a Text field 6 35
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now