home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 6 / IOPROG_06.ISO / trial / pb / server / pbsyb.sql < prev    next >
Encoding:
Text File  |  1996-06-07  |  19.2 KB  |  674 lines

  1. /*---------------------------------------------------------*/
  2. /*  The "use" stmt below connects you to master.           */
  3. /*  If you are installing on a Sybase System 10 server,    */
  4. /*  we recommend that you "use sybsystemprocs" instead.    */
  5. /*  SPECIAL INSTRUCTIONS FOR WISQL USERS:                  */
  6. /*  WISQL appears to require that the command              */
  7. /*  "use database" be issued by itself. Once you           */
  8. /*  connect to the appropriate database (master or         */
  9. /*  sybsystemprocs), issue a "file, open, PBSYB.SQL" to    */
  10. /*  read this script into the WISQL command buffer.        */
  11. /*  Next, delete the "use" command and the "go" below.     */
  12. /*  Then issue "query, execute all".                       */
  13. /*                                                         */
  14. /*  By default, execute authority for these procedures is  */
  15. /*  granted to "public".  You may search and replace       */
  16. /*  "public"  with a group-id if you wish tigher security. */
  17. /*---------------------------------------------------------*/
  18. use master
  19. go
  20.  
  21. /*---------------------------------------------------------*/
  22. /*  Drop old PBSYB system procedures                       */
  23. /*---------------------------------------------------------*/
  24. if exists (select 1 from sysobjects 
  25.            where name = "sp_pbcolumn" and type = "P")
  26. begin   
  27.    drop procedure sp_pbcolumn
  28. end
  29. go
  30. if exists (select 1 from sysobjects 
  31.            where name = "sp_pbdb" and type = "P")
  32. begin   
  33.    drop procedure sp_pbdb
  34. end
  35. go
  36. if exists (select 1 from sysobjects 
  37.            where name = "sp_pbindex" and type = "P")
  38. begin   
  39.    drop procedure sp_pbindex
  40. end
  41. go
  42. if exists (select 1 from sysobjects 
  43.            where name = "sp_pbproc" and type = "P")
  44. begin   
  45.    drop procedure sp_pbproc
  46. end
  47. go
  48. if exists (select 1 from sysobjects 
  49.            where name = "sp_pbhelprotect" and type = "P")
  50. begin   
  51.    drop procedure sp_pbhelprotect
  52. end
  53. go
  54. if exists (select 1 from sysobjects 
  55.            where name = "sp_pbtable" and type = "P")
  56. begin   
  57.    drop procedure sp_pbtable
  58. end
  59. go
  60. if exists (select 1 from sysobjects 
  61.            where name = "sp_pbtext" and type = "P")
  62. begin   
  63.    drop procedure sp_pbtext
  64. end
  65. go
  66. if exists (select 1 from sysobjects 
  67.            where name = "sp_pbprimarykey" and type = "P")
  68. begin   
  69.    drop procedure sp_pbprimarykey
  70. end
  71. go
  72. if exists (select 1 from sysobjects 
  73.            where name = "sp_pbforeignkey" and type = "P")
  74. begin   
  75.    drop procedure sp_pbforeignkey
  76. end
  77. go
  78. if exists (select 1 from sysobjects 
  79.            where name = "sp_pbfktable" and type = "P")
  80. begin   
  81.    drop procedure sp_pbfktable
  82. end
  83. go
  84.  
  85. /*------------------------------------------------*/
  86. /*  PowerBuilder DB-Lib Interface                 */
  87. /*  sp_pbcolumn lists the columns in a table.     */
  88. /*  The objectid is required as arg1.             */
  89. /*------------------------------------------------*/
  90. create proc sp_pbcolumn @id int as
  91.       select colid, status, type, length, name, usertype
  92.              from dbo.syscolumns  where id = @id
  93. go
  94. if @@error = 0
  95. begin
  96.    grant execute on sp_pbcolumn to public
  97. end  
  98. go
  99.  
  100. /*-----------------------------------------------------*/
  101. /*  PowerBuilder DB-Lib Interface                      */
  102. /*  sp_pbdb retrieves the names of all databases       */
  103. /*  available for this server.                         */
  104. /*-----------------------------------------------------*/
  105. create proc sp_pbdb as
  106.      select name from master.dbo.sysdatabases
  107. go
  108. if @@error = 0
  109. begin
  110.    grant execute on sp_pbdb to public
  111. end  
  112. go
  113.  
  114. /*-----------------------------------------------------*/
  115. /*  PowerBuilder DB-Lib Interface                      */
  116. /*  sp_pbindex retrieves info about all indexes for    */
  117. /*  a specific table (@objname is required).           */
  118. /*-----------------------------------------------------*/
  119. create procedure sp_pbindex
  120. @objname varchar(92)                    /* the table to check for indexes */
  121. as
  122. declare @objid int                      /* the object id of the table */
  123. declare @indid int                      /* the index id of an index */
  124. declare @key1 varchar(30)               /* first key  */
  125. declare @key2 varchar(30)               /* second key  */
  126. declare @key3 varchar(30)               /* third key  */
  127. declare @key4 varchar(30)               /* fourth key  */
  128. declare @key5 varchar(30)               /* ...  */
  129. declare @key6 varchar(30)
  130. declare @key7 varchar(30)
  131. declare @key8 varchar(30)
  132. declare @key9 varchar(30)               /* ...  */
  133. declare @key10 varchar(30)
  134. declare @key11 varchar(30)
  135. declare @key12 varchar(30)
  136. declare @key13 varchar(30)               /* ...  */
  137. declare @key14 varchar(30)
  138. declare @key15 varchar(30)
  139. declare @key16 varchar(30)
  140.  
  141. declare @unique smallint                /* index is unique */
  142. declare @clustered  smallint            /* index is clustered */
  143.  
  144. /*
  145. **  Check to see the the table exists and initialize @objid.
  146. */
  147. select @objid = object_id(@objname)
  148.  
  149. /*
  150. **  Table doesn't exist so return.
  151. */
  152. if @objid is NULL
  153. begin
  154.        return
  155. end
  156.  
  157. /*
  158. **  See if the object has any indexes.
  159. **  Since there may be more than one entry in sysindexes for the object,
  160. **  this select will set @indid to the index id of the first index.
  161. */
  162. select @indid = min(indid)
  163.        from sysindexes
  164.              where id = @objid
  165.                     and indid > 0
  166.                     and indid < 255
  167.  
  168. /*
  169. **  If no indexes, return.
  170. */
  171. if @indid is NULL
  172. begin
  173.        return
  174. end
  175.  
  176. /*
  177. **  Now check out each index, figure out it's type and keys and
  178. **  save the info in a temporary table that we'll print out at the end.
  179. */
  180. create table #spindtab
  181. (
  182.        index_name      varchar(30),
  183.        index_num       int,
  184.        index_key1      varchar(30) NULL,
  185.        index_key2      varchar(30) NULL,
  186.        index_key3      varchar(30) NULL,
  187.        index_key4      varchar(30) NULL,
  188.        index_key5      varchar(30) NULL,
  189.        index_key6      varchar(30) NULL,
  190.        index_key7      varchar(30) NULL,
  191.        index_key8      varchar(30) NULL,
  192.        index_key9      varchar(30) NULL,
  193.        index_key10     varchar(30) NULL,
  194.        index_key11     varchar(30) NULL,
  195.        index_key12     varchar(30) NULL,
  196.        index_key13     varchar(30) NULL,
  197.        index_key14     varchar(30) NULL,
  198.        index_key15     varchar(30) NULL,
  199.        index_key16     varchar(30) NULL,
  200.        index_unique    smallint,
  201.        index_clustered smallint
  202. )
  203.  
  204. while @indid != NULL
  205. begin
  206.  
  207.        /*
  208.        **  First we'll figure out what the keys are.
  209.        */
  210.        declare @i int
  211.        declare @thiskey varchar(30)
  212.        declare @lastindid int
  213.  
  214.        select @i = 1
  215.  
  216.        set nocount on
  217.  
  218.        while @i <= 16
  219.        begin
  220.              select @thiskey = index_col(@objname, @indid, @i)
  221.  
  222.              if @thiskey = NULL
  223.              begin
  224.                     goto keysdone
  225.              end
  226.  
  227.              if @i = 1
  228.              begin
  229.                 select @key1 = index_col(@objname, @indid, @i)
  230.              end
  231.              else
  232.              if @i = 2
  233.              begin
  234.                 select @key2 = index_col(@objname, @indid, @i)
  235.              end
  236.              else
  237.              if @i = 3
  238.              begin
  239.                 select @key3 = index_col(@objname, @indid, @i)
  240.              end
  241.              else
  242.              if @i = 4
  243.              begin
  244.                 select @key4 = index_col(@objname, @indid, @i)
  245.              end
  246.              else
  247.              if @i = 5
  248.              begin
  249.                 select @key5 = index_col(@objname, @indid, @i)
  250.              end
  251.              else
  252.              if @i = 6
  253.              begin
  254.                 select @key6 = index_col(@objname, @indid, @i)
  255.              end
  256.              else
  257.              if @i = 7
  258.              begin
  259.                 select @key7 = index_col(@objname, @indid, @i)
  260.              end
  261.              else
  262.              if @i = 8
  263.              begin
  264.                 select @key8 = index_col(@objname, @indid, @i)
  265.              end
  266.              else
  267.              if @i = 9
  268.              begin
  269.                 select @key9 = index_col(@objname, @indid, @i)
  270.              end
  271.              else
  272.              if @i = 10
  273.              begin
  274.                 select @key10 = index_col(@objname, @indid, @i)
  275.              end
  276.              else
  277.              if @i = 11
  278.              begin
  279.                 select @key11 = index_col(@objname, @indid, @i)
  280.              end
  281.              else
  282.              if @i = 12
  283.              begin
  284.                 select @key12 = index_col(@objname, @indid, @i)
  285.              end
  286.              else
  287.              if @i = 13
  288.              begin
  289.                 select @key13 = index_col(@objname, @indid, @i)
  290.              end
  291.              else
  292.              if @i = 14
  293.              begin
  294.                 select @key14 = index_col(@objname, @indid, @i)
  295.              end
  296.              else
  297.              if @i = 15
  298.              begin
  299.                 select @key15 = index_col(@objname, @indid, @i)
  300.              end
  301.              else
  302.              if @i = 16
  303.              begin
  304.                 select @key16 = index_col(@objname, @indid, @i)
  305.              end
  306.  
  307.              /*
  308.              **  Increment @i so it will check for the next key.
  309.              */
  310.              select @i = @i + 1
  311.  
  312.        end
  313.  
  314.  
  315.        /*
  316.        **  When we get here we now have all the keys.
  317.        */
  318.        keysdone:
  319.              set nocount off
  320.  
  321.        /*
  322.        **  Figure out if it's a  clustered or nonclustered index.
  323.        */
  324.        if @indid = 1
  325.          select @clustered = 1
  326.  
  327.        if @indid > 1
  328.          select @clustered = 0
  329.  
  330.        /*
  331.        **  Now we'll check out the status bits for this index
  332.        */
  333.  
  334.        /*
  335.        **  See if the index is unique (0x02).
  336.        */
  337.        if exists (select *
  338.              from master.dbo.spt_values v, sysindexes i
  339.                     where i.status & v.number = v.number
  340.                           and v.type = "I"
  341.                           and v.number = 2
  342.                           and i.id = @objid
  343.                           and i.indid = @indid)
  344.          select @unique = 1
  345.        else
  346.          select @unique = 0
  347.  
  348.        /*
  349.        **  Now we have all the needed info for the index so we'll add
  350.        **  the goods to the temporary table.
  351.        */
  352.        insert into #spindtab
  353.              select name, @i - 1, @key1, @key2, @key3, @key4,
  354.                    @key5, @key6, @key7, @key8, @key9,
  355.                    @key10, @key11, @key12, @key13, @key14,
  356.                    @key15, @key16, @unique, @clustered
  357.                     from sysindexes
  358.                           where id = @objid
  359.                                 and indid = @indid
  360.        /*
  361.        **  Now move @indid to the next index.
  362.        */
  363.        select @lastindid = @indid
  364.        select @indid = NULL
  365.        select @indid = min(indid)
  366.              from sysindexes
  367.                     where id = @objid
  368.                           and indid > @lastindid
  369.                           and indid < 255
  370. end
  371.  
  372. /*
  373. **  Now print out the contents of the temporary index table.
  374. */
  375. select index_name, index_num, index_key1, index_key2,
  376.       index_key3, index_key4, index_key5, index_key6,
  377.       index_key7, index_key8, index_key9, index_key10,
  378.       index_key11, index_key12, index_key13, index_key14,
  379.       index_key15, index_key16, index_unique, index_clustered
  380.        from #spindtab
  381.  
  382. drop table #spindtab
  383. go
  384. if @@error = 0
  385. begin
  386.    grant execute on sp_pbindex to public
  387. end  
  388. go
  389.  
  390. /*------------------------------------------------*/
  391. /*  PowerBuilder DB-Lib Interface                 */
  392. /*  sp_pbproc lists available stored procedures   */
  393. /*------------------------------------------------*/
  394. create proc sp_pbproc
  395.           @procid int = NULL ,
  396.           @procnumber smallint = NULL  as
  397.           if @procid = null
  398.             begin
  399.              select o.id, o.name, o.uid, user_name(o.uid),
  400.              c.number from dbo.sysobjects o, dbo.syscomments c
  401.                where o.type = 'P' and o.id = c.id  and c.colid = 1  
  402.             end
  403.           else
  404.             begin
  405.              select name, type, length, colid from dbo.syscolumns
  406.              where (id = @procid and number = @procnumber)
  407.             end
  408.           return
  409. go
  410. if @@error = 0
  411. begin
  412.    grant execute on sp_pbproc to public
  413. end  
  414. go
  415.  
  416. /*-----------------------------------------------------*/
  417. /*  PowerBuilder DB-Lib Interface                      */
  418. /*  sp_pbhelprotect retrieves security information.    */
  419. /*  @name arg may be an object name or a userid.       */
  420. /*-----------------------------------------------------*/
  421. create procedure sp_pbhelprotect
  422.             @name varchar(92)         /* name of object or user to check */
  423.             as declare @low int       /* range of userids to check */
  424.             declare @high int
  425.             declare @objid int        /* id of @name if object */
  426.         /* There are two cases handled by this procedure.
  427.         If the first parameter  is an object (table, view, procedure)
  428.         then @name is taken as an object  name and the procedure will
  429.         figure out permissions for the object.
  430.         If the first parameter is not one of the objects
  431.         mentioned it will be taken as user name and all
  432.         the permissions for the user or group name will be shown. */
  433.         /* **  Check to see if it's an object. */
  434.  
  435.         if exists (select *  from dbo.sysobjects
  436.             where id = object_id(@name)
  437.             and (sysstat & 7 = 1    /* system table */
  438.             or sysstat & 7 = 2      /* view */
  439.             or sysstat & 7 = 3      /* user table */
  440.             or sysstat & 7 = 4))    /* procedure */
  441.  
  442.             begin
  443.  
  444.         /*  This is the case where we will show the
  445.            various permissions for an object, possibly restricted by
  446.            a particular user.  */
  447.              select type = substring(b.name, 1, 6), action = a.name,
  448.                    pbuser = substring(user_name(p.uid), 1, 15),
  449.                    column = substring(isnull(col_name(id, c.number),
  450.                           "All"), 1, 31)
  451.                from sysprotects p, master.dbo.spt_values c,
  452.                    master.dbo.spt_values a, master.dbo.spt_values b
  453.                 where convert(tinyint, substring(isnull(p.columns, 0x1),
  454.                      c.low, 1)) & c.high != 0
  455.                      and c.number <=
  456.                      (select count(*) from dbo.syscolumns
  457.                         where id = object_id(@name))
  458.                             and a.type = "T"
  459.                             and a.number = p.action
  460.                             and b.type = "T"
  461.                             and b.number = p.protecttype
  462.                             and p.id = object_id(@name)
  463.                         order by type, action, pbuser, column
  464.                return
  465.             end
  466.  
  467.         /* Since @name is not an object let's try it as a user. */
  468.  
  469.         select @low = uid, @high = uid
  470.            from dbo.sysusers where name = @name
  471.         /*  Now we have the user so run the same
  472.            protection query as before but restrict on user and
  473.            not on object. */
  474.         select distinct type = substring(b.name, 1, 6), action = a.name,
  475.          object = substring(isnull(object_name(p.id), ""), 1, 15),
  476.          column = substring(isnull(col_name(id, c.number), "All"), 1, 10)
  477.          from sysprotects p, master.dbo.spt_values c,
  478.             master.dbo.spt_values a, master.dbo.spt_values b
  479.            where convert(tinyint, substring(isnull(p.columns, 0x1),
  480.                    c.low, 1))
  481.             & c.high != 0
  482.             and c.number <= 255  and a.type = "T"
  483.             and a.number = p.action
  484.             and b.type = "T"
  485.             and b.number = p.protecttype
  486.             and p.uid = @low
  487.            order by type, object, column, action
  488.         return
  489. go
  490. if @@error = 0
  491. begin
  492.    grant execute on sp_pbhelprotect to public
  493. end  
  494. go
  495.  
  496. /*-----------------------------------------------------*/
  497. /*  PowerBuilder DB-Lib Interface                      */
  498. /*  sp_pbtable retrieves info about all tables in a    */
  499. /*  database (if @tblname is NULL) or info about a     */
  500. /*  specific table (if @tblname ispassed as arg1).     */
  501. /*-----------------------------------------------------*/
  502. create procedure sp_pbtable
  503.       @tblname varchar(60) = NULL as
  504.       declare @objid int
  505.  
  506.       if @tblname = null
  507.         select name, id, type, uid, user_name(uid) from sysobjects where
  508.            (type = 'S' or type = 'U' or type = 'V')
  509.       else
  510.         begin
  511.           select @objid = object_id(@tblname)
  512.           select name, id, type, uid, user_name(uid) from sysobjects
  513.              where id = @objid
  514.         end
  515.       return
  516. go
  517. if @@error = 0
  518. begin
  519.    grant execute on sp_pbtable to public
  520. end  
  521. go
  522.  
  523. /*-----------------------------------------------------*/
  524. /*  PowerBuilder DB-Lib Interface                      */
  525. /*  sp_pbtext retrieves the text of a stored           */
  526. /*  procedure from the syscomments table.  requires    */
  527. /*  and @objid argument and an optional @number arg    */
  528. /*-----------------------------------------------------*/
  529. create procedure sp_pbtext
  530.       @objid  int ,
  531.       @number smallint = NULL
  532.       as
  533.       if (@number = NULL)
  534.         select text  from dbo.syscomments where id = @objid
  535.       else
  536.         select text  from dbo.syscomments where
  537.             (id = @objid and number = @number)
  538.       return
  539. go
  540. if @@error = 0
  541. begin
  542.    grant execute on sp_pbtext to public
  543. end  
  544. go
  545.  
  546.  
  547. /*------------------------------------------------*/
  548. /*  PowerBuilder DB-Lib Interface                 */
  549. /*  sp_pbprimarykey lists the columns that        */
  550. /*  comprise the primary key for a table.  The    */
  551. /*  table name is required as arg1.               */
  552. /*  This routine lists syskeys entries, not the   */
  553. /*  declarative referential integrity feature in  */
  554. /*  Sybase SQL Server Version 10.                 */
  555. /*------------------------------------------------*/
  556. create procedure sp_pbprimarykey
  557. @tabname varchar(92)                    /* the table to check for indexes */
  558. as
  559. declare @tabid int                      /* the object id of the table */
  560. /*
  561. **  Check to see the the table exists and initialize @objid.
  562. */
  563. select @tabid = object_id(@tabname)
  564.  
  565. /*
  566. **  Table doesn't exist so return.
  567. */
  568. if @tabid is NULL
  569.    begin
  570.       return
  571.    end
  572. else
  573. /*
  574. **  See if the object has a primary key
  575. */
  576.    begin
  577.       select k.keycnt,
  578.        objectkey1 = col_name(k.id, key1),
  579.        objectkey2 = col_name(k.id, key2),
  580.        objectkey3 = col_name(k.id, key3),
  581.        objectkey4 = col_name(k.id, key4),
  582.        objectkey5 = col_name(k.id, key5),
  583.        objectkey6 = col_name(k.id, key6),
  584.        objectkey7 = col_name(k.id, key7),
  585.        objectkey8 = col_name(k.id, key8)
  586.        from syskeys k, master.dbo.spt_values v
  587.              where  k.type = v.number and v.type =  'K'
  588.              and k.type = 1 and k.id = @tabid
  589.       return
  590.    end
  591. go
  592. if @@error = 0
  593. begin
  594.    grant execute on sp_pbprimarykey to public
  595. end  
  596. go
  597.  
  598.  
  599. /*-----------------------------------------------------------------*/
  600. /*  PowerBuilder DB-Lib Interface                                  */
  601. /*  sp_pbforeignkey lists all foreign keys associated with         */
  602. /*  a table whose name is passed as arg1 (required).               */ 
  603. /*  This procedure lists entries in syskeys, not the declarative   */
  604. /*  referential integrity foreign keys that System 10 provides.    */
  605. /*-----------------------------------------------------------------*/
  606. create procedure sp_pbforeignkey
  607. @tabname varchar(92)                    /* the table to check for indexes */
  608. as
  609. declare @tabid int                      /* the object id of the table */
  610. /*
  611. **  Check to see the the table exists and initialize @objid.
  612. */
  613. select @tabid = object_id(@tabname)
  614. /*
  615. **  Table doesn't exist so return.
  616. */
  617. if @tabid is NULL
  618. begin
  619.        return
  620. end
  621. else
  622. /*
  623. **  See if the object has any foreign keys
  624. */
  625. begin
  626. select k.keycnt, OBJECT_NAME(k.depid), 
  627.        (select USER_NAME(o.uid) from dbo.sysobjects o 
  628.          where o.id = @tabid),
  629.        objectkey1 = col_name(k.id, key1),
  630.        objectkey2 = col_name(k.id, key2),
  631.        objectkey3 = col_name(k.id, key3),
  632.        objectkey4 = col_name(k.id, key4),
  633.        objectkey5 = col_name(k.id, key5),
  634.        objectkey6 = col_name(k.id, key6),
  635.        objectkey7 = col_name(k.id, key7),
  636.        objectkey8 = col_name(k.id, key8)
  637.        from syskeys k, master.dbo.spt_values v
  638.              where  k.type = v.number and v.type =  'K'
  639.              and k.type = 2 and k.id = @tabid
  640. return
  641. end
  642. go
  643. if @@error = 0
  644. begin
  645.    grant execute on sp_pbforeignkey to public
  646. end  
  647. go
  648.  
  649. /*--------------------------------------------------------------*/
  650. /*  PowerBuilder DB-Lib Interface                               */
  651. /*  sp_pbfktable lists the tables that reference this table.    */
  652. /*--------------------------------------------------------------*/
  653. create procedure sp_pbfktable
  654.       @tblname varchar(60) = NULL as
  655.       declare @objid int
  656.  
  657.       if @tblname = null
  658.         return
  659.       else
  660.         begin
  661.           select @objid = object_id(@tblname)
  662.           select name, id, type, uid, user_name(uid) from sysobjects
  663.              where id in (select k.id from syskeys k where k.depid =
  664.         @objid) 
  665.         end
  666.       return
  667. go
  668. if @@error = 0
  669. begin
  670.    grant execute on sp_pbfktable to public
  671. end  
  672. go
  673.  
  674.