use master
go
sp_configure 'allow updates', 1
go
reconfigure with override
go

use sybsystemprocs
go
drop procedure sp_addcomment
go

set quoted_identifier on
go

create procedure sp_addcomment (@OBJECT   varchar ( 30),
                                @CLASS    tinyint,
                                @COMMENT  varchar (255)) as
   set ansinull                      on
   set string_rtruncation            on

   if (@@trancount = 0)
      begin
         set chained off

         if (@@isolation > 1)
            begin
               set transaction isolation level 1
            end
      end
   else
      begin
         print '    <sp_addcomment> CANNOT BE RUN FROM WITHIN A TRANSACTION.'

         print ' '

         return 1
      end

   declare @maxseq  tinyint,
           @objid   int

   if (suser_id () = 1)
      begin
         select @objid = id
         from sybsystemprocs.dbo.sysobjects
         where name           = @OBJECT
           and uid            = 1
           and (sysstat & 15) = 4

         if (@objid IS NULL)
            begin
               print ' '

               print '    STORED PROCEDURE <%1!> WAS NOT FOUND.', @OBJECT

               print ' '

               if (exists (select *
                           from sybsystemprocs.dbo.sysobjects
                           where name = @OBJECT
                             and uid  = 1))
                  begin
                     print '    OBJECT <%1!> IS NOT A STORED PROCEDURE.', @OBJECT

                     print ' '
                  end

               if (exists (select *
                           from sybsystemprocs.dbo.sysobjects
                           where name           = @OBJECT
                             and (sysstat & 15) = 4))
                  begin
                     print '    STORED PROCEDURE(S) <%1!> IS NOT OWNED BY sa.', @OBJECT

                     print ' '
                  end
            end
         else
            begin
               select @maxseq = 1 + max (seq)
               from sybsystemprocs.dbo.udescriptions
               where objid = @objid

               if (@maxseq IS NULL)
                  begin
                     -- AN EARLIER VERSION OF THE PROC MAY HAVE BEEN DROPPED.
                     -- CLEAN UP udescriptions IN THIS CASE.

                     delete from sybsystemprocs.dbo.udescriptions
                     where spname = @OBJECT

                     insert into sybsystemprocs.dbo.udescriptions
                        select @OBJECT, @CLASS, @objid, 1, @COMMENT
                  end
               else if (@maxseq < 255)
                  begin
                     insert into sybsystemprocs.dbo.udescriptions
                        select @OBJECT, @CLASS, @objid, @maxseq, @COMMENT
                  end
               else
                  begin
                     print '    TABLE udescriptions HAS NO ROOM FOR THIS COMMENT.'

                     print ' '
                  end
            end
      end
   else
      begin
         print '    ONLY A SYSTEM ADMINISTRATOR CAN EXECUTE THIS PROCEDURE.'

         print ' '
      end
go
sp_procxmode sp_addcomment, anymode
go

use master
go
sp_configure 'allow updates', 0
go
reconfigure with override
go
