Re: Change owner of SP

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Kalen Delaney (replies_at_public_newsgroups.com)
Date: 10/16/04


Date: Sat, 16 Oct 2004 15:13:58 -0700

Hi Mark

Basically, what John was suggesting was that you just manually do what the
sp_MSforeachtable does. It creates a cursor that steps through the system
table 'sysobjects' finding all the user tables, and then uses them in
whatever command you specify. He suggested you just create a similar cursor,
but instead of looking for user tables, you look for procedures.

Here is a procedure that will do that for you. Make sure you are connected
as a SQL sysadmin before you create it in the master database.
------------------------------------------------
use master
go
create proc sp_MSforeachproc
 @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 procedure (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 hCForEach cursor global for select ''['' +
REPLACE(user_name(uid), N'']'', N'']]'') + '']'' + ''.'' + ''['' +
REPLACE(object_name(id), N'']'', N'']]'') + '']'' from dbo.sysobjects o '
         + N' where OBJECTPROPERTY(o.id, N''IsProcedure'') = 1 ' + N' and
o.category & ' + @mscat + N' = 0 '
         + @whereand)
 declare @retval int
 select @retval = @@error
 if (@retval = 0)
  exec @retval = sp_MSforeach_worker @command1, @replacechar, @command2,
@command3

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

 return @retval
----------------------------------------------------------------

After you create the above procedure, USE the db where your procedures are
located and run the procedure just like you would run sp_MSforeachtable:

sp_msforeachproc "sp_changeobjectowner '?', 'dbo'"

This procedure is provided without warranties.

-- 
HTH
----------------
Kalen Delaney
SQL Server MVP
www.SolidQualityLearning.com
"Mark" <anonymous@devdex.com> wrote in message 
news:%23bEBNj8sEHA.2864@TK2MSFTNGP09.phx.gbl...
>
> Sorry John - I'm not an SQL Server programmer, so don't really
> understand - just need to change the ownership - am I missing the point?
>
>
> *** Sent via Developersdex http://www.developersdex.com ***
> Don't just participate in USENET...get rewarded for it! 


Relevant Pages

  • Re: Error adding command properties for stored procedure call
    ... I see you are using the SQL Native Client for SQL Server ... A server cursor is not allowed on a remote stored procedure or stored ... The stored procedure called is a test one that simply SELECTs the name field ... int InitializeAndConnect; ...
    (microsoft.public.data.oledb)
  • Row by Row Operations Help req
    ... Create temp table 1 ... Nigel,The cursor question is the SQL equivalent of the GOTOs / NO GOTOs ... question about whether the familiarity of procedural code is worth the ... leverages the strengths of SQL Server and is usually faster since it uses ...
    (microsoft.public.sqlserver.programming)
  • Re: C5 woes. How stable is VFP really?
    ... After that I close the SQL server cursor and work with my manual cursor instead. ... a VFP9 app on a really old machine ... app was under more "pressure", VFP showed ...
    (microsoft.public.fox.programmer.exchange)
  • Re: SQL HELP PLEASE!! Cursor only returns part of the data
    ... First of all, it's not the cursor as such that is bad, it is the looping. ... and re-writing it to handle set-based data will cost you more ... set-based solution do not perform well, ... Erland Sommarskog, SQL Server MVP, esquel@xxxxxxxxxxxxx ...
    (comp.databases.ms-sqlserver)
  • Re: Using Cursors
    ... it seems I've stirred up a hornet's nest with my cursor question. ... finding the discussion valuable in my circumstances (moving from Oracle to ... SQL Server). ... > DECLARE curEpisode SCROLL CURSOR FOR ...
    (microsoft.public.sqlserver.programming)