Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Creating your own sp_MSforeach stored procedure Expand / Collapse
Author
Message
Posted Friday, June 14, 2002 12:00 AM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: Moderators
Last Login: Monday, August 19, 2013 12:07 PM
Points: 1,929, Visits: 228
Comments posted to this topic are about the content posted at http://www.sqlservercentral.com/columnists/bknight/sp_msforeachworker.asp

Brian Knight
Free SQL Server Training Webinars
Post #4836
Posted Monday, June 04, 2007 7:49 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Sunday, January 13, 2013 9:22 AM
Points: 1, Visits: 14

Hi Brian,

In trying to run the MSforEachTrigger script in 2005 I'm getting errors - any idea what's going on??

Thanks, Simon

Msg 16950, Level 16, State 2, Procedure sp_MSforeach_worker, Line 27
The variable '@local_cursor' does not currently have a cursor allocated to it.
Msg 16950, Level 16, State 2, Procedure sp_MSforeach_worker, Line 32
The variable '@local_cursor' does not currently have a cursor allocated to it.
Msg 16950, Level 16, State 2, Procedure sp_MSforeach_worker, Line 153
The variable '@local_cursor' does not currently have a cursor allocated to it.
Msg 16916, Level 16, State 1, Procedure sp_MSforeach_worker, Line 155
A cursor with the name 'hCForEachDatabase' does not exist.

Post #370999
Posted Wednesday, March 19, 2008 12:29 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Saturday, April 12, 2014 11:40 AM
Points: 2,795, Visits: 8,297
And the 4 "foreach" links at the bottom of the page are dead.


Post #471819
Posted Thursday, March 27, 2008 11:16 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, August 05, 2013 5:03 PM
Points: 39, Visits: 64
Just came across the error myself.

Msg 16950, Level 16, State 2, Procedure sp_MSforeach_worker, Line 31
The variable '@local_cursor' does not currently have a cursor allocated to it.
The statement has been terminated.

The sp's [sp_MSforeachtable] and [sp_MSforeach_worker] have been changed, for a start they now belong to the sys schema (no longer prefixed dbo, but I think SQL finds the sys schema copy anyway) and the cursor sp_MSforeachtable uses when it calls sp_MSforeach_worker is "hCForEachTable" (or "hCForEachDatabase" for databases) instead of "hCForEach". They've also added another variable to distinguish for each database and each table. The error message you get is because the new cursor name doesn't match the old name (which is in the sp's in the original article).

Just follow the original instructions to alter the current sp_MSforeachtable to create the new ones, you could try fixing the originals but easier to start again.

Cheers
Fred
Post #475881
Posted Friday, May 25, 2012 12:31 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, March 05, 2014 10:45 AM
Points: 10, Visits: 103
Just had a go at this myself, ran into some errors, thought I'd post what needed to be done for SQL 2008 R2 to hopefully help the next guy get through it a bit faster.

The key, as mentioned above, is the changed procedure. After that, it's important to change SET QUOTED IDENTIFIER OFF - not ON, as in the original article code. Took me a minute (and a helpful blog from a fellow in Romania) to get that.

USE MASTER
GO

if exists (select name from sysobjects
where name = 'sp_MSforeachview' AND type = 'P')
drop procedure sp_MSforeachview
GO


SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO


create proc sp_MSforeachview
@command1 nvarchar(2000), @replacechar nchar(1) = N'?', @command2 nvarchar(2000) = null,
@command3 nvarchar(2000) = null, @whereand nvarchar(2000) = null,
@precommand nvarchar(2000) = null, @postcommand nvarchar(2000) = null
as
/* This proc returns one or more rows for each table (optionally, matching @where), with each table defaulting to its own result set */
/* @precommand and @postcommand may be used to force a single result set via a temp table. */

/* Preprocessor won't replace within quotes so have to use str(). */
declare @mscat nvarchar(12)
select @mscat = ltrim(str(convert(int, 0x0002)))

if (@precommand is not null)
exec(@precommand)

/* Create the select */
exec(N'declare hCForEachTable cursor global for select ''['' + REPLACE(schema_name(syso.schema_id), N'']'', N'']]'') + '']'' + ''.'' + ''['' + REPLACE(object_name(o.id), N'']'', N'']]'') + '']'' from dbo.sysobjects o join sys.all_objects syso on o.id =
syso.object_id '
+ N' where OBJECTPROPERTY(o.id, N''IsView'') = 1 ' + N' and o.category & ' + @mscat + N' = 0 '
+ @whereand)
declare @retval int
select @retval = @@error
if (@retval = 0)
exec @retval = sys.sp_MSforeach_worker @command1, @replacechar, @command2, @command3, 0

if (@retval = 0 and @postcommand is not null)
exec(@postcommand)

return @retval

GO
SET ANSI_NULLS ON
GO

Post #1306771
Posted Friday, May 25, 2012 1:55 PM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Friday, March 28, 2014 2:25 PM
Points: 9,902, Visits: 9,479
Here'e another way to do this: here

-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
Post #1306807
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse