home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 1997 March / VPR9703A.ISO / MS_DEV / VID / SERVER / ISTUDIO / ODBC / INSTCAT.SQL < prev    next >
Text File  |  1996-04-02  |  133KB  |  4,617 lines

  1.  
  2. /*
  3. **    INSTCAT.SQL
  4. **    Installs catalog stored procedures on the Microsoft SQL Server.
  5. **    Copyright 1992-1996, Microsoft Corp.    All rights reserved.
  6. */
  7.  
  8. /*
  9. NOTE:  you MUST change the last row inserted into spt_server_info
  10. to be version number of this file.    the convention is j.nn.bbb, where
  11. jj is the major version number ('6' now), nn is the minor version number
  12. ('50' now), and bbb is the build number.
  13. */
  14.  
  15. /****************************************************************************/
  16. /* This portion sets up the ability to perform all the functions in this    */
  17. /* script                                                                    */
  18. /****************************************************************************/
  19. go
  20. use master
  21. go
  22. dump tran master with no_log
  23. go
  24.  
  25. set quoted_identifier on
  26. go
  27.  
  28. if exists (select * from sysobjects
  29.        where name = 'sp_configure' and sysstat & 0xf = 4)
  30. begin
  31.     execute sp_configure 'update',1
  32. end
  33. reconfigure with override
  34. go
  35.  
  36. exec sp_MS_upd_sysobj_category 1 --Capture datetime for use below.
  37.  
  38. go
  39.  
  40. /*
  41. ** If old versions of tables exist, drop them.
  42. */
  43. if (exists (select * from sysobjects
  44.         where name = 'MSdatatype_info' and sysstat & 0xf = 3))
  45.     drop table MSdatatype_info
  46. go
  47. if (exists (select * from sysobjects
  48.         where name = 'MSdatatype_info_ext' and sysstat & 0xf = 3))
  49.     drop table MSdatatype_info_ext
  50. go
  51. if (exists (select * from sysobjects
  52.         where name = 'MStable_types' and sysstat & 0xf = 3))
  53.     drop table MStable_types
  54. go
  55. if (exists (select * from sysobjects
  56.         where name = 'MSserver_info' and sysstat & 0xf = 3))
  57.     drop table MSserver_info
  58. go
  59. if (exists (select * from sysobjects
  60.         where name = 'spt_table_types' and sysstat & 0xf = 3))
  61.     drop table spt_table_types    /* no longer used */
  62. go
  63.  
  64. /*
  65. ** If tables or procs already exist, drop them.
  66. */
  67.  
  68. if (exists (select * from sysobjects
  69.         where name = 'spt_datatype_info' and sysstat & 0xf = 3))
  70.     drop table spt_datatype_info
  71. go
  72. if (exists (select * from sysobjects
  73.         where name = 'spt_datatype_info_ext' and sysstat & 0xf = 3))
  74.     drop table spt_datatype_info_ext
  75. go
  76. if (exists (select * from sysobjects
  77.         where name = 'spt_server_info' and sysstat & 0xf = 3))
  78.     drop table spt_server_info
  79. go
  80. if (exists (select * from sysobjects
  81.         where name = 'sp_tables' and sysstat & 0xf = 4))
  82.     drop proc sp_tables
  83. go
  84. if (exists (select * from sysobjects
  85.         where name = 'sp_statistics' and sysstat & 0xf = 4))
  86.     drop proc sp_statistics
  87. go
  88. if (exists (select * from sysobjects
  89.         where name = 'sp_columns' and sysstat & 0xf = 4))
  90.     drop proc sp_columns
  91. go
  92. if (exists (select * from sysobjects
  93.         where name = 'sp_fkeys' and sysstat & 0xf = 4))
  94.     drop proc sp_fkeys
  95. go
  96. if (exists (select * from sysobjects
  97.         where name = 'sp_pkeys' and sysstat & 0xf = 4))
  98.     drop proc sp_pkeys
  99. dump tran master with no_log
  100. go
  101.  
  102. go
  103. if (exists (select * from sysobjects
  104.         where name = 'sp_stored_procedures' and sysstat & 0xf = 4))
  105.     drop proc sp_stored_procedures
  106. go
  107. if (exists (select * from sysobjects
  108.         where name = 'sp_sproc_columns' and sysstat & 0xf = 4))
  109.     drop proc sp_sproc_columns
  110. go
  111. if (exists (select * from sysobjects
  112.         where name = 'sp_table_privileges' and sysstat & 0xf = 4))
  113.     drop proc sp_table_privileges
  114. go
  115. if (exists (select * from sysobjects
  116.         where name = 'sp_column_privileges' and sysstat & 0xf = 4))
  117.     drop proc sp_column_privileges
  118. go
  119. if (exists (select * from sysobjects
  120.         where name = 'sp_server_info' and sysstat & 0xf = 4))
  121.     drop proc sp_server_info
  122. go
  123. if (exists (select * from sysobjects
  124.         where name = 'sp_datatype_info' and sysstat & 0xf = 4))
  125.     drop proc sp_datatype_info
  126. go
  127. if (exists (select * from sysobjects
  128.         where name = 'sp_special_columns' and sysstat & 0xf = 4))
  129.     drop proc sp_special_columns
  130. go
  131. if (exists (select * from sysobjects
  132.         where name = 'sp_databases' and sysstat & 0xf = 4))
  133.     drop proc sp_databases
  134. go
  135. if (exists (select * from sysobjects
  136.         where name = 'sp_ddopen' and sysstat & 0xf = 4))
  137.     drop proc sp_ddopen
  138. go
  139.  
  140. dump tran master with no_log
  141. go
  142.  
  143. print 'creating table spt_datatype_info_ext'
  144. go
  145. create table spt_datatype_info_ext (
  146.                 user_type        smallint    not null,
  147.                 CREATE_PARAMS    varchar(32) null,
  148.                 AUTO_INCREMENT    smallint null)
  149. go
  150.  
  151. create unique clustered index datatypeinfoextclust on spt_datatype_info_ext(user_type,AUTO_INCREMENT)
  152. go
  153.  
  154. grant select on spt_datatype_info_ext to public
  155. go
  156.  
  157.  
  158. insert into spt_datatype_info_ext
  159.     /* CHAR      user_type, create_params, auto_increment */
  160.     values             (1,    'length' ,0)
  161.  
  162. insert into spt_datatype_info_ext
  163.     /* VARCHAR     user_type, create_params, auto_increment */
  164.     values             (2,    'max length' ,0)
  165.  
  166. insert into spt_datatype_info_ext
  167.     /* BINARY     user_type, create_params, auto_increment */
  168.     values             (3,    'length' ,0)
  169.  
  170. insert into spt_datatype_info_ext
  171.     /* VARBINARY user_type, create_params, auto_increment */
  172.     values             (4,    'max length' ,0)
  173.  
  174. if    (charindex('6.00', @@version) > 0 or
  175.      charindex('6.50', @@version) > 0 or
  176.      charindex('7.00', @@version) > 0)
  177. begin    /*    Add 6.0 data types */
  178.     insert into spt_datatype_info_ext
  179.         /* DECIMAL user_type, create_params, auto_increment */
  180.         values             (26,    'precision,scale' ,0)
  181.  
  182.     insert into spt_datatype_info_ext
  183.         /* NUMERIC user_type, create_params, auto_increment */
  184.         values             (25,    'precision,scale' ,0)
  185.  
  186.     insert into spt_datatype_info_ext
  187.         /* DECIMAL IDENTITY user_type, create_params, auto_increment */
  188.         values             (26,    'precision' ,1)
  189.  
  190.     insert into spt_datatype_info_ext
  191.         /* NUMERIC IDENTITY user_type, create_params, auto_increment */
  192.         values             (25,    'precision' ,1)
  193.  
  194. end
  195. else    /*    Pre 6.0 server, add SYSNAME create param */
  196.     begin
  197.         insert into spt_datatype_info_ext
  198.             /* SYSNAME     user_type, create_param, auto_increments */
  199.             values             (18,    'max length' ,0)
  200.  
  201.     end
  202. go
  203.  
  204. print 'creating table spt_datatype_info'
  205. go
  206. create table spt_datatype_info (
  207.     ss_dtype            tinyint     not null,
  208.     fixlen                int         null,        /* datatype len for variable, else null */
  209.     ODBCVer             tinyint     null,        /* version if needed, else null */
  210.     TYPE_NAME            varchar(30)    not null,
  211.     DATA_TYPE            smallint    not null,
  212.     data_precision        int         null,
  213.     numeric_scale        smallint    null,    /* min scale if 6.0 */
  214.     RADIX                smallint    null,
  215.     length                int         null,
  216.     LITERAL_PREFIX        varchar(32) null,
  217.     LITERAL_SUFFIX        varchar(32) null,
  218.     CREATE_PARAMS        varchar(32) null,
  219.     NULLABLE            smallint    not null,
  220.     CASE_SENSITIVE        smallint    not null,
  221.     SEARCHABLE            smallint    not null,
  222.     UNSIGNED_ATTRIBUTE    smallint    null,
  223.     MONEY                smallint    not null,
  224.     AUTO_INCREMENT        smallint    null,
  225.     LOCAL_TYPE_NAME     varchar(128)null,
  226.     charbin             tinyint     null, /* 0 for char/binary types, NULL for all others */
  227.     SQL_DATA_TYPE        smallint    not null,
  228.     SQL_DATETIME_SUB    smallint    null)
  229. go
  230.  
  231. create unique clustered index datatypeinfoclust on spt_datatype_info(ss_dtype,fixlen,ODBCVer,AUTO_INCREMENT)
  232. go
  233.  
  234. grant select on spt_datatype_info to public
  235. go
  236.  
  237. /* Get case sensitivity */
  238. if 'A' = 'A' /* create dummy begin block */
  239. begin
  240.     declare @case smallint
  241.  
  242.     begin tran
  243.     select @case = 0
  244.     select @case = 1 where 'a' <> 'A'
  245.  
  246.     /* Local Binary */
  247.     insert into spt_datatype_info values
  248.     (45,null,null,'binary',-2,null,null,null,null,'0x',null,'length',1,0,2,null,0,null,'binary',0,-2,null)
  249.  
  250.     /* Local Bit */
  251.     insert into spt_datatype_info values
  252.     (50,null,null,'bit',-7,1,0,2,null,null,null,null,0,0,2,null,0,null,'bit',null,-7,null)
  253.  
  254.     /* Local Char */
  255.     insert into spt_datatype_info values
  256.     (47,null,null,'char',1,null,null,null,null,'''','''','length',1,@case,3,null,0,null,'char',0,1,null)
  257.  
  258.     /* Local Datetime */
  259.     insert into spt_datatype_info values
  260.     (61,8,2,'datetime',11,23,3,10,16,'''','''',null,1,0,3,null,0,null,'datetime',null,9,3)
  261.     insert into spt_datatype_info values
  262.     (61,8,3,'datetime',93,23,3,10,16,'''','''',null,1,0,3,null,0,null,'datetime',null,9,3)
  263.  
  264.     /* Local Smalldatetime */
  265.     insert into spt_datatype_info values
  266.     (58,4,2,'smalldatetime',11,16,0,10,16,'''','''',null,1,0,3,null,0,null,'smalldatetime',null,9,3)
  267.     insert into spt_datatype_info values
  268.     (58,4,3,'smalldatetime',93,16,0,10,16,'''','''',null,1,0,3,null,0,null,'smalldatetime',null,9,3)
  269.  
  270.     /* Local Datetimn */
  271.     insert into spt_datatype_info values
  272.     (111,4,2,'smalldatetime',11,16,0,10,16,'''','''',null,1,0,3,null,0,null,'smalldatetime',null,9,3)
  273.     insert into spt_datatype_info values
  274.     (111,4,3,'smalldatetime',93,16,0,10,16,'''','''',null,1,0,3,null,0,null,'smalldatetime',null,9,3)
  275.     insert into spt_datatype_info values /* sql server type is 'datetimn' */
  276.     (111,8,2,'datetime',11,23,3,10,16,'''','''',null,1,0,3,null,0,null,'datetime',null,9,3)
  277.     insert into spt_datatype_info values
  278.     (111,8,3,'datetime',93,23,3,10,16,'''','''',null,1,0,3,null,0,null,'datetime',null,9,3)
  279.  
  280.     /* Local Float */
  281.     insert into spt_datatype_info values
  282.     (62,8,2,'float',6,15,null,10,8,null,null,null,1,0,2,0,0,0,'float',null,6,null)
  283.     insert into spt_datatype_info values
  284.     (62,8,3,'float',6,53,null, 2,8,null,null,null,1,0,2,0,0,0,'float',null,6,null)
  285.  
  286.     /* Local Floatn */
  287.     insert into spt_datatype_info values /* sql server type is 'floatn' */
  288.     (109,4,2,'real',7, 7,null,10,4,null,null,null,1,0,2,0,0,0,'real',null,7,null)
  289.     insert into spt_datatype_info values
  290.     (109,4,3,'real',7,24,null, 2,4,null,null,null,1,0,2,0,0,0,'real',null,7,null)
  291.     insert into spt_datatype_info values /* sql server type is 'floatn' */
  292.     (109,8,2,'float',6,15,null,10,8,null,null,null,1,0,2,0,0,0,'float',null,6,null)
  293.     insert into spt_datatype_info values
  294.     (109,8,3,'float',6,53,null, 2,8,null,null,null,1,0,2,0,0,0,'float',null,6,null)
  295.  
  296.     /* Local Real */
  297.     insert into spt_datatype_info values
  298.     (59,4,2,'real',7, 7,null,10,4,null,null,null,1,0,2,0,0,0,'real',null,7,null)
  299.     insert into spt_datatype_info values
  300.     (59,4,3,'real',7,24,null, 2,4,null,null,null,1,0,2,0,0,0,'real',null,7,null)
  301.  
  302.     /* Local Smallmoney */
  303.     insert into spt_datatype_info values
  304.     (122,4,null,'smallmoney',3,10,4,10,12,'$',null,null,1,0,2,0,1,0,'smallmoney',null,3,null)
  305.  
  306.     /* Local Money */
  307.     insert into spt_datatype_info values
  308.     (60,8,null,'money',3,19,4,10,21,'$',null,null,1,0,2,0,1,0,'money',null,3,null)
  309.  
  310.     /* Local Moneyn */
  311.     insert into spt_datatype_info values    /* sql server type is 'moneyn' */
  312.     (110,4,null,'smallmoney',3,10,4,10,12,'$',null,null,1,0,2,0,1,0,'smallmoney',null,3,null)
  313.     insert into spt_datatype_info values    /* sql server type is 'moneyn' */
  314.     (110,8,null,'money',3,19,4,10,21,'$',null,null,1,0,2,0,1,0,'money',null,3,null)
  315.  
  316.     /* Local Int */
  317.     insert into spt_datatype_info values
  318.     (56,4,null,'int',4,10,0,10,4,null,null,null,1,0,2,0,0,0,'int',null,4,null)
  319.  
  320.     /* Local Intn */
  321.     insert into spt_datatype_info values /* sql server type is 'intn' */
  322.     (38,4,null,'int',4,10,0,10,4,null,null,null,1,0,2,0,0,0,'int',null,4,null)
  323.     insert into spt_datatype_info values /* sql server type is 'intn' */
  324.     (38,2,null,'smallint',5,5,0,10,2,null,null,null,1,0,2,0,0,0,'smallint',null,5,null)
  325.     insert into spt_datatype_info values
  326.     (38,1,null,'tinyint',-6,3,0,10,1,null,null,null,1,0,2,1,0,0,'tinyint',null,-6,null)
  327.  
  328.     /* Local Smallint */
  329.     insert into spt_datatype_info values
  330.     (52,2,null,'smallint',5,5,0,10,2,null,null,null,1,0,2,0,0,0,'smallint',null,5,null)
  331.     insert into spt_datatype_info values
  332.     (52,2,1,'smallint',5,5,0,10,2,null,null,null,1,0,2,0,0,0,'smallint',null,5,null)
  333.  
  334.     /* Local Tinyint */
  335.     insert into spt_datatype_info values
  336.     (48,1,null,'tinyint',-6,3,0,10,1,null,null,null,1,0,2,1,0,0,'tinyint',null,-6,null)
  337.  
  338.     /* Local Text */
  339.     insert into spt_datatype_info values
  340.     (35,null,null,'text',-1,2147483647,null,null,2147483647,'''','''',null,1,@case,1,null,0,null,'text',0,-1,null)
  341.  
  342.     /* Local Varbinary */
  343.     insert into spt_datatype_info values
  344.     (37,null,null,'varbinary',-3,null,null,null,null,'0x',null,'max length',1,0,2,null,0,null,'varbinary',0,-3,null)
  345.  
  346.     /* Local Varchar */
  347.     insert into spt_datatype_info values
  348.     (39,null,null,'varchar',12,null,null,null,null,'''','''','max length',1,@case,3,null,0,null,'varchar',0,12,null)
  349.  
  350.     /* Local Image */
  351.     insert into spt_datatype_info values
  352.     (34,null,null,'image',-4,2147483647,null,null,2147483647,'0x',null,null,1,0,0,null,0,null,'image',0,-4,null)
  353.  
  354.     if (charindex('6.00', @@version) > 0 or
  355.         charindex('6.50', @@version) > 0 or
  356.         charindex('7.00', @@version) > 0)
  357.     begin    /*    Add 6.0 data types */
  358.         /* Local Decimaln */
  359.         insert into spt_datatype_info values    /* sql server type is 'decimaln' */
  360.         (106,null,null,'decimal',3,38,0,10,null,null,null,'precision,scale',1,0,2,0,0,0,'decimal',null,3,null)
  361.  
  362.         /* Local Numericn */
  363.         insert into spt_datatype_info values    /* sql server type is 'numericn' */
  364.         (108,null,null,'numeric',2,38,0,10,null,null,null,'precision,scale',1,0,2,0,0,0,'numeric',null,2,null)
  365.  
  366.         /* Local Decimal */
  367.         insert into spt_datatype_info values    /* sql server type is 'decimaln' */
  368.         (55,null,null,'decimal',3,38,0,10,null,null,null,'precision,scale',1,0,2,0,0,0,'decimal',null,3,null)
  369.  
  370.         /* Local Numeric */
  371.         insert into spt_datatype_info values    /* sql server type is 'numericn' */
  372.         (63,null,null,'numeric',2    ,38,0,10,null,null,null,'precision,scale',1,0,2,0,0,0,'numeric',null,2,null)
  373.  
  374.         /* Identity attribute data types */
  375.  
  376.         /* Identity Int */
  377.         insert into spt_datatype_info values
  378.         (56,null,null,'int identity',4,10,0,10,null,null,null,null,0,0,2,0,0,1,'int identity',null,4,null)
  379.  
  380.         /* Identity Smallint */
  381.         insert into spt_datatype_info values
  382.         (52,null,null,'smallint identity',5,5,0,10,null,null,null,null,0,0,2,0,0,1,'smallint identity',null,5,null)
  383.  
  384.         /* Identity Tinyint */
  385.         insert into spt_datatype_info values
  386.         (48,null,null,'tinyint identity',-6,3,0,10,null,null,null,null,0,0,2,1,0,1,'tinyint identity',null,-6,null)
  387.  
  388.         /* Identity Numeric */
  389.         insert into spt_datatype_info values    /* sql server type is 'decmaln' */
  390.         (106,null,null,'decimal() identity',3,38,0,10,null,null,null,'precision,scale',0,0,2,0,0,1,'decimal() identity',null,3,null)
  391.         insert into spt_datatype_info values    /* sql server type is 'decmaln' */
  392.         (55,null,null,'decimal() identity',3,38,0,10,null,null,null,'precision,scale',0,0,2,0,0,1,'decimal() identity',null,3,null)
  393.  
  394.         /* Identity Numeric */
  395.         insert into spt_datatype_info values    /* sql server type is 'decmaln' */
  396.         (108,null,null,'numeric() identity',2,38,0,10,null,null,null,'precision,scale',0,0,2,0,0,1,'numeric() identity',null,2,null)
  397.         insert into spt_datatype_info values    /* sql server type is 'decmaln' */
  398.         (63,null,null,'numeric() identity',2,38,0,10,null,null,null,'precision,scale',0,0,2,0,0,1,'numeric() identity',null,2,null)
  399.  
  400.     end
  401.     commit tran
  402. end
  403. go
  404.  
  405. dump tran master with no_log
  406. go
  407.  
  408. print 'creating table spt_server_info'
  409. go
  410. create table spt_server_info (
  411.               attribute_id        int NOT NULL,
  412.               attribute_name    varchar(60) NOT NULL,
  413.               attribute_value    varchar(255) NOT NULL)
  414. go
  415.  
  416. create unique clustered index serverinfoclust on spt_server_info(attribute_id)
  417. go
  418.  
  419. insert into spt_server_info
  420.     values (1,'DBMS_NAME','Microsoft SQL Server')
  421. insert into spt_server_info
  422.     values (2,'DBMS_VER',@@version)
  423. insert into spt_server_info
  424.     values (10,'OWNER_TERM','owner')
  425. insert into spt_server_info
  426.     values (11,'TABLE_TERM','table')
  427. insert into spt_server_info
  428.     values (12,'MAX_OWNER_NAME_LENGTH','30')
  429. insert into spt_server_info
  430.     values (13,'TABLE_LENGTH','30')
  431. insert into spt_server_info
  432.     values (14,'MAX_QUAL_LENGTH','30')
  433. insert into spt_server_info
  434.     values (15,'COLUMN_LENGTH','30')
  435. if 'A' = 'a' /* If not case sensitive server */
  436. begin
  437.     insert into spt_server_info
  438.         values (16,'IDENTIFIER_CASE','MIXED')
  439. end
  440. else
  441. begin
  442.     insert into spt_server_info
  443.         values (16,'IDENTIFIER_CASE','SENSITIVE')
  444. end
  445. insert into spt_server_info
  446.     values (17,'TX_ISOLATION','2')
  447. if (charindex('6.00', @@version) > 0 or
  448.     charindex('6.50', @@version) > 0 or
  449.     charindex('7.00', @@version) > 0)
  450. begin    /*    Add 6.0 collation sequence */
  451.     insert into spt_server_info
  452.         select 18,'COLLATION_SEQ',
  453.             'charset='+t2.name+' sort_order='+t1.name
  454.             +' charset_num='+rtrim(convert(char(4),t1.csid))+
  455.             ' sort_order_num='+rtrim(convert(char(4),t1.id))
  456.         from syscharsets t1, syscharsets t2, sysconfigures t3
  457.         where t1.csid=t2.id and t1.id=t3.value and t3.config=1123
  458. end
  459. else
  460. begin    /*    Add 4.2x collation sequence */
  461.     insert into spt_server_info
  462.         select 18,'COLLATION_SEQ',
  463.             'charset='+t2.name+' sort_order='+t1.name
  464.             +' charset_num='+rtrim(convert(char(4),t1.csid))+
  465.             ' sort_order_num='+rtrim(convert(char(4),t1.id))
  466.         from syscharsets t1, syscharsets t2, sysconfigures t3
  467.         where t1.csid=t2.id and t1.id=t3.value and t3.config=123
  468. end
  469. insert into spt_server_info
  470.     values (19,'SAVEPOINT_SUPPORT','Y')
  471. insert into spt_server_info
  472.     values (20,'MULTI_RESULT_SETS','Y')
  473. insert into spt_server_info
  474.     values (22,'ACCESSIBLE_TABLES','Y')
  475. insert into spt_server_info
  476.     values (100,'USERID_LENGTH','30')
  477. insert into spt_server_info
  478.     values (101,'QUALIFIER_TERM','database')
  479. insert into spt_server_info
  480.     values (102,'NAMED_TRANSACTIONS','Y')
  481. insert into spt_server_info
  482.     values (103,'SPROC_AS_LANGUAGE','Y')
  483. insert into spt_server_info
  484.     values (104,'ACCESSIBLE_SPROC','Y')
  485. insert into spt_server_info
  486.     values (105,'MAX_INDEX_COLS','16')
  487. insert into spt_server_info
  488.     values (106,'RENAME_TABLE','Y')
  489. insert into spt_server_info
  490.     values (107,'RENAME_COLUMN','Y')
  491. insert into spt_server_info
  492.     values (108,'DROP_COLUMN','N')
  493. insert into spt_server_info
  494.     values (109,'INCREASE_COLUMN_LENGTH','N')
  495. if (charindex('6.50', @@version) = 0 and
  496.     charindex('7.00', @@version) = 0)
  497. begin
  498.     insert into spt_server_info
  499.         values (110,'DDL_IN_TRANSACTION','N')
  500. end
  501. else
  502. begin
  503.     insert into spt_server_info
  504.         values (110,'DDL_IN_TRANSACTION','Y')
  505. end
  506. insert into spt_server_info
  507.     values (111,'DESCENDING_INDEXES','N')
  508. insert into spt_server_info
  509.     values (112,'SP_RENAME','Y')
  510. insert into spt_server_info
  511.     values (113,'REMOTE_SPROC','Y')
  512. if (charindex('7.00', @@version) = 0)
  513. begin
  514.     insert into spt_server_info
  515.         values (500,'SYS_SPROC_VERSION','6.50.193')
  516. end
  517. else
  518. begin
  519.     insert into spt_server_info
  520.         values (500,'SYS_SPROC_VERSION','7.00.005')
  521. end
  522. go
  523.  
  524. grant select on spt_server_info to public
  525. go
  526.  
  527. print 'creating sp_column_privileges'
  528. go
  529.  
  530. /*    Procedure for pre 6.50 server */
  531. CREATE PROCEDURE sp_column_privileges (
  532.             @table_name         varchar(32),
  533.             @table_owner        varchar(32) = null,
  534.             @table_qualifier    varchar(32) = null,
  535.             @column_name        varchar(90) = null)
  536. as
  537.  
  538.     declare @table_id     int
  539.     DECLARE @full_table_name    char(70)
  540.     declare @low smallint                     /* range of userids to check */
  541.     declare @high smallint
  542.     declare @owner_uid smallint
  543.  
  544.     select @low = 0, @high = 32767
  545.  
  546.     if @column_name is null /*    If column name not supplied, match all */
  547.         select @column_name = '%'
  548.  
  549.     if @table_qualifier is not null
  550.     begin
  551.         if db_name() <> @table_qualifier
  552.         begin    /* If qualifier doesn't match current database */
  553.             raiserror 20001 'Table qualifier must be name of current database'
  554.             return
  555.         end
  556.     end
  557.     if @table_owner is null
  558.     begin    /* If unqualified table name */
  559.         SELECT @full_table_name = @table_name
  560.     end
  561.     else
  562.     begin    /* Qualified table name */
  563.         SELECT @full_table_name = @table_owner + '.' + @table_name
  564.     end
  565.     /*    Get Object ID */
  566.     select @table_id = object_id(@full_table_name)
  567.  
  568.     if (@@trancount <> 0)
  569.     begin    /* If inside a transaction */
  570.         raiserror 20003 'The procedure ''sp_column_privileges'' cannot be executed from within a transaction.'
  571.         return
  572.     end
  573.  
  574.     /*
  575.     ** We need to create a table which will contain a row for every row to
  576.     ** be returned to the client.
  577.     */
  578.  
  579.     create table #column_priv1(
  580.         COLUMN_NAME             varchar(32) NOT NULL,
  581.         grantor                 smallint NOT NULL,
  582.         grantee                 smallint NOT NULL,
  583.         select_privilege        bit,
  584.         select_grantable        bit,
  585.         insert_privilege        bit,
  586.         insert_grantable        bit,
  587.         update_privilege        bit,
  588.         update_grantable        bit,
  589.         references_privilege    bit,
  590.         references_grantable    bit,
  591.         uid                     smallint NOT NULL,
  592.         gid                     smallint NOT NULL)
  593.  
  594. /*
  595. ** insert a row for the table owner (who has all permissions)
  596. */
  597.     select @owner_uid = (
  598.         select uid
  599.         from sysobjects
  600.         where id = @table_id)
  601.  
  602.     if (charindex('6.00', @@version) > 0)
  603.     begin
  604.         insert into #column_priv1
  605.             select
  606.                 c.name,
  607.                 u.uid,
  608.                 @owner_uid,
  609.                 0,
  610.                 1,
  611.                 0,
  612.                 1,
  613.                 0,
  614.                 1,
  615.                 0,
  616.                 1,
  617.                 @owner_uid,
  618.                 0
  619.             from syscolumns c, sysusers u
  620.             where id = @table_id
  621.                 and c.number = 0
  622.                 and u.suid = 1        /* grantor is dbo of database */
  623.     end
  624.     else
  625.     begin
  626.         insert into #column_priv1
  627.             select
  628.                 c.name,
  629.                 u.uid,
  630.                 @owner_uid,
  631.                 0,
  632.                 1,
  633.                 0,
  634.                 1,
  635.                 0,
  636.                 1,
  637.                 0,
  638.                 0,
  639.                 @owner_uid,
  640.                 0
  641.             from syscolumns c, sysusers u
  642.             where id = @table_id
  643.                 and c.number = 0
  644.                 and u.suid = 1        /* grantor is dbo of database */
  645.     end
  646. /*
  647. ** now stick in a row for every column for every user in the database
  648. ** we will need to weed out those who have no permissions later
  649. ** (and yes this is a cartesion product: the uid field in sysprotects
  650. ** can also have a group id, in which case we need to extend those
  651. ** privileges to all group members).
  652. */
  653.  
  654.     insert into #column_priv1
  655.         select distinct
  656.             c.name,
  657.             o.uid,
  658.             u.uid,
  659.             0,
  660.             0,
  661.             0,
  662.             0,
  663.             0,
  664.             0,
  665.             0,
  666.             0,
  667.             u.uid,
  668.             u.gid
  669.         from sysusers u, syscolumns c, sysobjects o
  670.         where o.id = @table_id
  671.             and c.id = o.id
  672.             and c.number = 0
  673.             and u.gid <> u.uid
  674.             and u.uid <> @owner_uid
  675.  
  676.     /*
  677.     ** we need to create another temporary table to contain all the various
  678.     ** protection information for the table in question
  679.     */
  680.     create table #protects (
  681.                 uid         smallint NOT NULL,
  682.                 grantor        smallint NOT NULL,
  683.                 action        tinyint NOT NULL,
  684.                 protecttype tinyint NOT NULL,
  685.                 name        varchar(32) NOT NULL)
  686.  
  687.     insert into #protects
  688.         select
  689.             p.uid,
  690.             p.uid,
  691.             p.action,
  692.             p.protecttype,
  693.             isnull(col_name(id, c.number), '~All')
  694.             from
  695.                 sysprotects p,
  696.                 master.dbo.spt_values c,
  697.                 master.dbo.spt_values a,
  698.                 master.dbo.spt_values b
  699.             where
  700.                 convert(tinyint, substring(isnull(p.columns, 0x1), c.low, 1))
  701.                     & c.high <> 0
  702.                     and c.number <= (
  703.                         select count(*)
  704.                         from syscolumns
  705.                         where id = @table_id)
  706.                 and c.type = 'P'
  707.                 and a.type = 'T'
  708.                 and a.number = p.action
  709.                 and p.action in (193,195,197,26)
  710.                 and b.type = 'T'
  711.                 and b.number = p.protecttype
  712.                 and p.id = @table_id
  713.                 and p.uid between @low and @high
  714.  
  715.  
  716.     update #column_priv1
  717.     set select_privilege = 1
  718.         from #protects p
  719.     where
  720.         p.protecttype = 205
  721.         and p.action = 193
  722.         and (p.name = #column_priv1.COLUMN_NAME
  723.             or name = '~All')
  724.         and (p.uid = 0
  725.             or p.uid = #column_priv1.gid
  726.             or p.uid = #column_priv1.uid)
  727.         and not exists (
  728.             select * from #protects
  729.             where
  730.                 protecttype = 206
  731.                 and action = 193
  732.                 and (name = #column_priv1.COLUMN_NAME
  733.                     or name = '~All')
  734.                 and ( uid = 0
  735.                     or uid = #column_priv1.gid
  736.                     or uid = #column_priv1.uid))
  737.  
  738.     update #column_priv1
  739.     set insert_privilege = 1
  740.         from #protects p
  741.     where
  742.         p.protecttype = 205
  743.         and p.action = 195
  744.         and (p.name = #column_priv1.COLUMN_NAME
  745.             or name = '~All')
  746.         and (p.uid = 0
  747.             or p.uid = #column_priv1.gid
  748.             or p.uid = #column_priv1.uid)
  749.         and not exists (
  750.             select * from #protects
  751.             where
  752.                 protecttype = 206
  753.                 and action = 195
  754.                 and (name = #column_priv1.COLUMN_NAME
  755.                        or name = '~All')
  756.                 and (uid = 0
  757.                     or uid = #column_priv1.gid
  758.                     or uid = #column_priv1.uid))
  759.  
  760.     update #column_priv1
  761.     set update_privilege = 1
  762.         from #protects p
  763.     where
  764.         p.protecttype = 205
  765.         and p.action = 197
  766.         and (p.name = #column_priv1.COLUMN_NAME
  767.             or name = '~All')
  768.         and (p.uid = 0
  769.             or p.uid = #column_priv1.gid
  770.             or p.uid = #column_priv1.uid)
  771.         and not exists (
  772.             select * from #protects
  773.                 where protecttype = 206
  774.                 and action = 197
  775.                 and (name = #column_priv1.COLUMN_NAME
  776.                     or name = '~All')
  777.                 and (uid = 0
  778.                     or uid = #column_priv1.gid
  779.                     or uid = #column_priv1.uid))
  780.  
  781.     update #column_priv1
  782.     set references_privilege = 1
  783.         from #protects p
  784.     where
  785.         p.protecttype = 205
  786.         and p.action = 26
  787.         and (p.name = #column_priv1.COLUMN_NAME
  788.             or name = '~All')
  789.         and (p.uid = 0
  790.             or p.uid = #column_priv1.gid
  791.             or p.uid = #column_priv1.uid)
  792.         and not exists (
  793.             select * from #protects
  794.                 where protecttype = 206
  795.                 and action = 26
  796.                 and (name = #column_priv1.COLUMN_NAME
  797.                     or name = '~All')
  798.                 and (uid = 0
  799.                     or uid = #column_priv1.gid
  800.                     or uid = #column_priv1.uid))
  801.  
  802.     update #column_priv1
  803.     set select_grantable = 1
  804.         from #protects p
  805.     where
  806.         p.protecttype = 204
  807.         and p.action = 193
  808.         and (p.name = #column_priv1.COLUMN_NAME
  809.             or name = '~All')
  810.         and (p.uid = 0
  811.             or p.uid = #column_priv1.gid
  812.             or p.uid = #column_priv1.uid)
  813.         and not exists (
  814.             select * from #protects
  815.             where
  816.                 protecttype = 206
  817.                 and action = 193
  818.                 and (name = #column_priv1.COLUMN_NAME
  819.                     or name = '~All')
  820.                 and ( uid = 0
  821.                     or uid = #column_priv1.gid
  822.                     or uid = #column_priv1.uid))
  823.  
  824.     update #column_priv1
  825.     set insert_grantable = 1
  826.         from #protects p
  827.     where
  828.         p.protecttype = 204
  829.         and p.action = 195
  830.         and (p.name = #column_priv1.COLUMN_NAME
  831.             or name = '~All')
  832.         and (p.uid = 0
  833.             or p.uid = #column_priv1.gid
  834.             or p.uid = #column_priv1.uid)
  835.         and not exists (
  836.             select * from #protects
  837.             where
  838.                 protecttype = 206
  839.                 and action = 195
  840.                 and (name = #column_priv1.COLUMN_NAME
  841.                     or name = '~All')
  842.                 and ( uid = 0
  843.                     or uid = #column_priv1.gid
  844.                     or uid = #column_priv1.uid))
  845.  
  846.     update #column_priv1
  847.     set update_grantable = 1
  848.         from #protects p
  849.     where
  850.         p.protecttype = 204
  851.         and p.action = 197
  852.         and (p.name = #column_priv1.COLUMN_NAME
  853.             or name = '~All')
  854.         and (p.uid = 0
  855.             or p.uid = #column_priv1.gid
  856.             or p.uid = #column_priv1.uid)
  857.         and not exists (
  858.             select * from #protects
  859.             where
  860.                 protecttype = 206
  861.                 and action = 197
  862.                 and (name = #column_priv1.COLUMN_NAME
  863.                     or name = '~All')
  864.                 and ( uid = 0
  865.                     or uid = #column_priv1.gid
  866.                     or uid = #column_priv1.uid))
  867.  
  868.     update #column_priv1
  869.     set references_grantable = 1
  870.         from #protects p
  871.     where
  872.         p.protecttype = 204
  873.         and p.action = 26
  874.         and (p.name = #column_priv1.COLUMN_NAME
  875.             or name = '~All')
  876.         and (p.uid = 0
  877.             or p.uid = #column_priv1.gid
  878.             or p.uid = #column_priv1.uid)
  879.         and not exists (
  880.             select * from #protects
  881.             where
  882.                 protecttype = 206
  883.                 and action = 26
  884.                 and (name = #column_priv1.COLUMN_NAME
  885.                     or name = '~All')
  886.                 and ( uid = 0
  887.                     or uid = #column_priv1.gid
  888.                     or uid = #column_priv1.uid))
  889.  
  890.     create table #column_priv2(
  891.         COLUMN_NAME     varchar(32) NOT NULL,
  892.         grantor         smallint NULL,
  893.         grantee         smallint NOT NULL,
  894.         PRIVILEGE        varchar(32) NOT NULL,
  895.         IS_GRANTABLE    varchar(3) NULL)
  896.  
  897.     insert into #column_priv2
  898.         select
  899.             COLUMN_NAME,
  900.             grantor,
  901.             grantee,
  902.             'SELECT',
  903.             'NO'
  904.         from #column_priv1
  905.         where select_privilege = 1 and select_grantable    = 0
  906.  
  907.     insert into #column_priv2
  908.         select
  909.             COLUMN_NAME,
  910.             grantor,
  911.             grantee,
  912.             'INSERT',
  913.             'NO'
  914.         from #column_priv1
  915.         where insert_privilege = 1 and insert_grantable = 0
  916.  
  917.     insert into #column_priv2
  918.         select
  919.             COLUMN_NAME,
  920.             grantor,
  921.             grantee,
  922.             'UPDATE',
  923.             'NO'
  924.         from #column_priv1
  925.         where update_privilege = 1 and update_grantable = 0
  926.  
  927.     insert into #column_priv2
  928.         select
  929.             COLUMN_NAME,
  930.             grantor,
  931.             grantee,
  932.             'REFERENCES',
  933.             'NO'
  934.         from #column_priv1
  935.         where references_privilege = 1 and references_grantable = 0
  936.  
  937.     insert into #column_priv2
  938.         select
  939.             COLUMN_NAME,
  940.             grantor,
  941.             grantee,
  942.             'SELECT',
  943.             'YES'
  944.         from #column_priv1
  945.         where select_grantable = 1
  946.  
  947.     insert into #column_priv2
  948.         select
  949.             COLUMN_NAME,
  950.             grantor,
  951.             grantee,
  952.             'INSERT',
  953.             'YES'
  954.         from #column_priv1
  955.         where insert_grantable = 1
  956.  
  957.     insert into #column_priv2
  958.         select
  959.             COLUMN_NAME,
  960.             grantor,
  961.             grantee,
  962.             'UPDATE',
  963.             'YES'
  964.         from #column_priv1
  965.         where update_grantable = 1
  966.  
  967.     insert into #column_priv2
  968.         select
  969.             COLUMN_NAME,
  970.             grantor,
  971.             grantee,
  972.             'REFERENCES',
  973.             'YES'
  974.         from #column_priv1
  975.         where references_grantable = 1
  976.  
  977.     select
  978.         convert(varchar(32),db_name()) TABLE_QUALIFIER,
  979.         convert(varchar(32),user_name(@owner_uid)) TABLE_OWNER,
  980.         @table_name TABLE_NAME,
  981.         COLUMN_NAME,
  982.         convert(varchar(32),user_name(grantor)) GRANTOR,
  983.         convert(varchar(32),user_name(grantee)) GRANTEE,
  984.         PRIVILEGE,
  985.         IS_GRANTABLE
  986.     from #column_priv2
  987.     where COLUMN_NAME like @column_name
  988.     order by 4, 7
  989. go
  990.  
  991. if (charindex('6.50', @@version) = 0 and
  992.     charindex('7.00', @@version) = 0)
  993. begin
  994.     print ''
  995.     print ''
  996.     print 'Warning:'
  997.     print 'you are installing the stored procedures '
  998.     print 'on a pre 6.50 SQL Server.'
  999.     print 'Ignore the following errors.'
  1000. end
  1001. else
  1002.     drop proc sp_column_privileges
  1003. go
  1004.  
  1005. /*    Procedure for 6.50 server */
  1006. CREATE PROCEDURE sp_column_privileges (
  1007.             @table_name         varchar(32),
  1008.             @table_owner        varchar(32) = null,
  1009.             @table_qualifier    varchar(32) = null,
  1010.             @column_name        varchar(90) = null)
  1011. as
  1012.  
  1013.     declare @table_id     int
  1014.  
  1015.     if @column_name is null /*    If column name not supplied, match all */
  1016.         select @column_name = '%'
  1017.  
  1018.     if @table_qualifier is not null
  1019.     begin
  1020.         if db_name() <> @table_qualifier
  1021.         begin    /* If qualifier doesn't match current database */
  1022.             raiserror 20001 'Table qualifier must be name of current database'
  1023.             return
  1024.         end
  1025.     end
  1026.     if @table_owner is null
  1027.     begin    /* If unqualified table name */
  1028.         select @table_id = object_id(@table_name)
  1029.     end
  1030.     else
  1031.     begin    /* Qualified table name */
  1032.         select @table_id = object_id(@table_owner + '.' + @table_name)
  1033.     end
  1034.  
  1035.     select
  1036.         convert(varchar(32),db_name()) TABLE_QUALIFIER,
  1037.         convert(varchar(32),user_name(o.uid)) TABLE_OWNER,
  1038.         @table_name TABLE_NAME,
  1039.         convert(varchar(32),c.name) COLUMN_NAME,
  1040.         convert(varchar(32),user_name(p.grantor)) GRANTOR,
  1041.         convert(varchar(32),user_name(u.uid)) GRANTEE,
  1042.         convert (varchar(32),case p.action
  1043.              when 193 then 'SELECT'
  1044.              when 195 then 'INSERT'
  1045.              when 197 then 'UPDATE'
  1046.              else 'REFERENCES'
  1047.         end) PRIVILEGE,
  1048.         convert (varchar(3),case when p.protecttype = 205 then 'NO'
  1049.             else 'YES'
  1050.         end) IS_GRANTABLE
  1051.     from sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c
  1052.     where
  1053.         c.id = @table_id
  1054.         and c.name like @column_name
  1055.         and c.id = p.id
  1056.         and c.id = o.id
  1057.         and case substring(p.columns, 1, 1) & 1
  1058.                 when NULL then 255    /* all columns have permission */
  1059.                 when 0 then convert(tinyint, substring(p.columns, v.low, 1))
  1060.                 else (~convert(tinyint, isnull(substring(p.columns, v.low, 1),0)))
  1061.             end
  1062.             & v.high <> 0            /* permission applies to this column */
  1063.         and v.number <= (select count(*) from syscolumns
  1064.             where id = @table_id)    /* ranges from 1 to # of columns in table */
  1065.         and v.type = 'P'
  1066.         and v.number = c.colid
  1067.             /* expand groups */
  1068.         and ((p.uid = u.uid and u.uid <> u.gid) or
  1069.              (p.uid = u.gid and u.uid <> u.gid))
  1070.         and p.protecttype <> 206    /* only grant rows */
  1071.         and p.action in (26,193,195,197)
  1072.         and o.uid <> u.uid            /* no rows for owner */
  1073.         and not exists (            /* exclude revoke'd privileges */
  1074.             select *
  1075.             from sysprotects p1
  1076.             where
  1077.                 p1.protecttype = 206
  1078.                 and p1.action = p.action
  1079.                 and p1.id = p.id
  1080.                 and p1.uid = u.uid
  1081.                 and case substring(p1.columns, 1, 1) & 1
  1082.                         when NULL then 255    /* all columns have permission */
  1083.                         when 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  1084.                                             else (~convert(tinyint,isnull(substring(p.columns, v.low, 1),0)))
  1085.                     end
  1086.                     & v.high <> 0)            /* permission applies to this column */
  1087.     union all
  1088.     select    /*    Add rows for table owner */
  1089.         convert(varchar(32),db_name()) TABLE_QUALIFIER,
  1090.         convert(varchar(32),user_name(o.uid)) TABLE_OWNER,
  1091.         @table_name TABLE_NAME,
  1092.         convert(varchar(32),col_name(@table_id, c.colid)) COLUMN_NAME,
  1093.         convert(varchar(32),user_name(u.uid)) grantor,
  1094.         convert(varchar(32),user_name(o.uid)) grantee,
  1095.         convert (varchar(32),case v.number
  1096.             when 193 then 'SELECT'
  1097.             when 195 then 'INSERT'
  1098.             when 197 then 'UPDATE'
  1099.             else 'REFERENCES'
  1100.         end) PRIVILEGE,
  1101.         convert(varchar(3),'YES') IS_GRANTABLE
  1102.     from sysobjects o, spt_values v, sysusers u, syscolumns c
  1103.     where
  1104.         c.id = @table_id
  1105.         and c.name like @column_name
  1106.         and c.id = o.id
  1107.         and u.suid = 1        /* grantor is dbo of database */
  1108.         and v.type = 'P'    /* cross product to get all exposed privileges */
  1109.         and v.number in (26,193,195,197)
  1110.         and not exists (    /* exclude revoke'd privileges */
  1111.             select *
  1112.             from sysprotects p1
  1113.             where
  1114.                 p1.protecttype = 206
  1115.                 and p1.action = v.number
  1116.                 and p1.id = o.id
  1117.                 and p1.uid = o.uid)
  1118.     order by 4, 7
  1119. go
  1120.  
  1121. grant execute on sp_column_privileges to public
  1122. go
  1123.  
  1124. dump tran master with no_log
  1125. go
  1126.  
  1127. print 'creating sp_columns'
  1128. go
  1129.  
  1130. /*    Procedure for pre-6.0 server */
  1131. CREATE PROCEDURE sp_columns (
  1132.                  @table_name        varchar(90),
  1133.                  @table_owner        varchar(90) = null,
  1134.                  @table_qualifier    varchar(90) = null,
  1135.                  @column_name        varchar(90) = null,
  1136.                  @ODBCVer            int = 2)
  1137. AS
  1138.     DECLARE @full_table_name    char(181)
  1139.     DECLARE @table_id int
  1140.  
  1141.     if @ODBCVer <> 3
  1142.         select @ODBCVer = 2
  1143.     if @column_name is null /*    If column name not supplied, match all */
  1144.         select @column_name = '%'
  1145.     if @table_qualifier is not null
  1146.     begin
  1147.         if db_name() <> @table_qualifier
  1148.         begin    /* If qualifier doesn't match current database */
  1149.             raiserror 20001 'Table qualifier must be name of current database'
  1150.             return
  1151.         end
  1152.     end
  1153.     if @table_name is null
  1154.     begin    /*    If table name not supplied, match all */
  1155.         select @table_name = '%'
  1156.     end
  1157.     if @table_owner is null
  1158.     begin    /* If unqualified table name */
  1159.         SELECT @full_table_name = @table_name
  1160.     end
  1161.     else
  1162.     begin    /* Qualified table name */
  1163.         SELECT @full_table_name = @table_owner + '.' + @table_name
  1164.     end
  1165.  
  1166.     /*    Get Object ID */
  1167.     SELECT @table_id = object_id(@full_table_name)
  1168.     if ((charindex('%',@full_table_name) = 0) and
  1169.         (charindex('_',@full_table_name) = 0)  and
  1170.         @table_id <> 0)
  1171.     begin
  1172.         /* this block is for the case where there is no pattern
  1173.              matching required for the table name */
  1174.         SELECT
  1175.             TABLE_QUALIFIER = convert(varchar(32),DB_NAME()),
  1176.             TABLE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  1177.             TABLE_NAME = convert(varchar(32),o.name),
  1178.             COLUMN_NAME = convert(varchar(32),c.name),
  1179.             d.DATA_TYPE,
  1180.             TYPE_NAME = t.name,
  1181.             "PRECISION" = isnull(d.data_precision, convert(int,c.length)),
  1182.             LENGTH = isnull(d.length, convert(int,c.length)),
  1183.             SCALE = d.numeric_scale,
  1184.             d.RADIX,
  1185.             NULLABLE =    /* set nullability from status flag */
  1186.                 convert(smallint, convert(bit, c.status&8)),
  1187.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  1188.             COLUMN_DEF = convert(varchar(254),substring(text,2,datalength(text)-2)),
  1189.             d.SQL_DATA_TYPE,
  1190.             d.SQL_DATETIME_SUB,
  1191.             CHAR_OCTET_LENGTH = isnull(d.data_precision, convert(int,c.length))+d.charbin,
  1192.             ORDINAL_POSITION = convert(int,c.colid),
  1193.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO      YES',(c.status&8)+1,3))),
  1194.             SS_DATA_TYPE = c.type
  1195.         FROM
  1196.             syscolumns c,
  1197.             sysobjects o,
  1198.             syscomments m,
  1199.             master.dbo.spt_datatype_info d,
  1200.             systypes t
  1201.         WHERE
  1202.             o.id = @table_id
  1203.             AND c.id = o.id
  1204.             AND t.type = d.ss_dtype
  1205.             AND c.length = isnull(d.fixlen, c.length)
  1206.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  1207.             AND o.type <> 'P'
  1208.             AND c.usertype = t.usertype
  1209.             AND c.name like @column_name
  1210.             AND c.cdefault *= m.id
  1211.         ORDER BY 17
  1212.     end
  1213.     else
  1214.     begin
  1215.         /* this block is for the case where there IS pattern
  1216.              matching done on the table name */
  1217.         if @table_owner is null /*    If owner not supplied, match all */
  1218.             select @table_owner = '%'
  1219.         SELECT
  1220.             TABLE_QUALIFIER = convert(varchar(32),DB_NAME()),
  1221.             TABLE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  1222.             TABLE_NAME = convert(varchar(32),o.name),
  1223.             COLUMN_NAME = convert(varchar(32),c.name),
  1224.             d.DATA_TYPE,
  1225.             TYPE_NAME = t.name,
  1226.             "PRECISION" = isnull(d.data_precision, convert(int,c.length)),
  1227.             LENGTH = isnull(d.length, convert(int,c.length)),
  1228.             SCALE = d.numeric_scale,
  1229.             d.RADIX,
  1230.             NULLABLE =    /* set nullability from status flag */
  1231.                 convert(smallint, convert(bit, c.status&8)),
  1232.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  1233.             COLUMN_DEF = convert(varchar(254),substring(text,2,datalength(text)-2)),
  1234.             d.SQL_DATA_TYPE,
  1235.             d.SQL_DATETIME_SUB,
  1236.             CHAR_OCTET_LENGTH = isnull(d.data_precision, convert(int,c.length))+d.charbin,
  1237.             ORDINAL_POSITION = convert(int,c.colid),
  1238.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO      YES',(c.status&8)+1,3))),
  1239.             SS_DATA_TYPE = c.type
  1240.         FROM
  1241.             syscolumns c,
  1242.             sysobjects o,
  1243.             syscomments m,
  1244.             master.dbo.spt_datatype_info d,
  1245.             systypes t
  1246.         WHERE
  1247.             o.name like @table_name
  1248.             AND user_name(o.uid) like @table_owner
  1249.             AND o.id = c.id
  1250.             AND t.type = d.ss_dtype
  1251.             AND c.length = isnull(d.fixlen, c.length)
  1252.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  1253.             AND o.type <> 'P'
  1254.             AND c.usertype = t.usertype
  1255.             AND c.name like @column_name
  1256.             AND c.cdefault *= m.id
  1257.         ORDER BY 2, 3, 17
  1258.     end
  1259. go
  1260.  
  1261. if (charindex('6.00', @@version) = 0 and
  1262.     charindex('6.50', @@version) = 0 and
  1263.     charindex('7.00', @@version) = 0)
  1264. begin
  1265.     print ''
  1266.     print ''
  1267.     print 'Warning:'
  1268.     print 'you are installing the stored procedures '
  1269.     print 'on a pre 6.0 SQL Server.'
  1270.     print 'Ignore the following error.'
  1271. end
  1272. else
  1273.     drop proc sp_columns
  1274. go
  1275.  
  1276. /*    Procedure for 6.0 server */
  1277. CREATE PROCEDURE sp_columns (
  1278.                  @table_name        varchar(90),
  1279.                  @table_owner        varchar(90) = null,
  1280.                  @table_qualifier    varchar(90) = null,
  1281.                  @column_name        varchar(90) = null,
  1282.                  @ODBCVer            int = 2)
  1283. AS
  1284.     DECLARE @full_table_name    char(181)
  1285.     DECLARE @table_id int
  1286.  
  1287.     if @ODBCVer <> 3
  1288.         select @ODBCVer = 2
  1289.     if @column_name is null /*    If column name not supplied, match all */
  1290.         select @column_name = '%'
  1291.     if @table_qualifier is not null
  1292.     begin
  1293.         if db_name() <> @table_qualifier
  1294.         begin    /* If qualifier doesn't match current database */
  1295.             raiserror (15250, -1,-1,'Table')
  1296.             return
  1297.         end
  1298.     end
  1299.     if @table_name is null
  1300.     begin    /*    If table name not supplied, match all */
  1301.         select @table_name = '%'
  1302.     end
  1303.     if @table_owner is null
  1304.     begin    /* If unqualified table name */
  1305.         SELECT @full_table_name = @table_name
  1306.     end
  1307.     else
  1308.     begin    /* Qualified table name */
  1309.         SELECT @full_table_name = @table_owner + '.' + @table_name
  1310.     end
  1311.  
  1312.     /*    Get Object ID */
  1313.     SELECT @table_id = object_id(@full_table_name)
  1314.     if ((charindex('%',@full_table_name) = 0) and
  1315.         (charindex('_',@full_table_name) = 0)  and
  1316.         @table_id <> 0)
  1317.     begin
  1318.         /* this block is for the case where there is no pattern
  1319.             matching required for the table name */
  1320.         SELECT
  1321.             TABLE_QUALIFIER = convert(varchar(32),DB_NAME()),
  1322.             TABLE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  1323.             TABLE_NAME = convert(varchar(32),o.name),
  1324.             COLUMN_NAME = convert(varchar(32),c.name),
  1325.             d.DATA_TYPE,
  1326.             convert (varchar(30),case
  1327.                 when t.usertype > 100 or t.usertype in (18,80) then t.name
  1328.                 else d.TYPE_NAME
  1329.             end) TYPE_NAME,
  1330.             convert(int,case
  1331.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  1332.                 else isnull(convert(int,c.prec), 2147483647)
  1333.             end) "PRECISION",
  1334.             convert(int,case
  1335.                 when d.ss_dtype IN (106, 108, 55, 63) then    /* decimal/numeric types */
  1336.                     c.prec+2
  1337.                 else
  1338.                     isnull(d.length, c.length)
  1339.             end) LENGTH,
  1340.             SCALE = convert(smallint, c.scale),
  1341.             d.RADIX,
  1342.             NULLABLE =    /* set nullability from status flag */
  1343.                 convert(smallint, convert(bit, c.status&8)),
  1344.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  1345.             COLUMN_DEF = convert(varchar(254),substring(text,2,datalength(text)-2)),
  1346.             d.SQL_DATA_TYPE,
  1347.             d.SQL_DATETIME_SUB,
  1348.             CHAR_OCTET_LENGTH = isnull(convert(int,c.prec), 2147483647)+d.charbin,
  1349.             ORDINAL_POSITION = convert(int,c.colid),
  1350.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO      YES',(c.status&8)+1,3))),
  1351.             SS_DATA_TYPE = c.type
  1352.         FROM
  1353.             syscolumns c,
  1354.             sysobjects o,
  1355.             syscomments m,
  1356.             master.dbo.spt_datatype_info d,
  1357.             systypes t
  1358.         WHERE
  1359.             o.id = @table_id
  1360.             AND c.id = o.id
  1361.             AND t.type = d.ss_dtype
  1362.             AND c.length = isnull(d.fixlen, c.length)
  1363.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  1364.             AND o.type <> 'P'
  1365.             AND isnull(d.AUTO_INCREMENT,0) = (c.status&128)/128
  1366.             AND c.usertype = t.usertype
  1367.             AND c.name like @column_name
  1368.             AND c.cdefault *= m.id
  1369.         ORDER BY 17
  1370.     end
  1371.     else
  1372.     begin
  1373.         /* this block is for the case where there IS pattern
  1374.             matching done on the table name */
  1375.         if @table_owner is null /*    If owner not supplied, match all */
  1376.             select @table_owner = '%'
  1377.         SELECT
  1378.             TABLE_QUALIFIER = convert(varchar(32),DB_NAME()),
  1379.             TABLE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  1380.             TABLE_NAME = convert(varchar(32),o.name),
  1381.             COLUMN_NAME = convert(varchar(32),c.name),
  1382.             d.DATA_TYPE,
  1383.             convert(varchar(30),case
  1384.                 when t.usertype > 100 or t.usertype in (18,80) then t.name
  1385.                 else d.TYPE_NAME
  1386.             end) TYPE_NAME,
  1387.             convert(int,case
  1388.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  1389.                 else isnull(convert(int,c.prec), 2147483647)
  1390.             end) "PRECISION",
  1391.             convert(int,case
  1392.                 when d.ss_dtype IN (106, 108, 55, 63) then    /* decimal/numeric types */
  1393.                     c.prec+2
  1394.                 else
  1395.                     isnull(d.length, c.length)
  1396.             end) LENGTH,
  1397.             SCALE = convert(smallint, c.scale),
  1398.             d.RADIX,
  1399.             NULLABLE =    /* set nullability from status flag */
  1400.                 convert(smallint, convert(bit, c.status&8)),
  1401.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  1402.             COLUMN_DEF = convert(varchar(254),substring(text,2,datalength(text)-2)),
  1403.             d.SQL_DATA_TYPE,
  1404.             d.SQL_DATETIME_SUB,
  1405.             CHAR_OCTET_LENGTH = isnull(convert(int,c.prec), 2147483647)+d.charbin,
  1406.             ORDINAL_POSITION = convert(int,c.colid),
  1407.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO      YES',(c.status&8)+1,3))),
  1408.             SS_DATA_TYPE = c.type
  1409.         FROM
  1410.             syscolumns c,
  1411.             sysobjects o,
  1412.             syscomments m,
  1413.             master.dbo.spt_datatype_info d,
  1414.             systypes t
  1415.         WHERE
  1416.             o.name like @table_name
  1417.             AND user_name(o.uid) like @table_owner
  1418.             AND o.id = c.id
  1419.             AND t.type = d.ss_dtype
  1420.             AND c.length = isnull(d.fixlen, c.length)
  1421.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  1422.             AND o.type <> 'P'
  1423.             AND isnull(d.AUTO_INCREMENT,0) = (c.status&128)/128
  1424.             AND c.usertype = t.usertype
  1425.             AND c.name like @column_name
  1426.             AND c.cdefault *= m.id
  1427.         ORDER BY 2, 3, 17
  1428.     end
  1429. go
  1430.  
  1431. grant execute on sp_columns to public
  1432. go
  1433.  
  1434. dump tran master with no_log
  1435. go
  1436.  
  1437. print 'creating sp_databases'
  1438. go
  1439.  
  1440. create proc sp_databases
  1441. as
  1442.     /* Use temporary table to sum up database size w/o using group by */
  1443.     create table #databases (
  1444.                   DATABASE_NAME varchar(32) NOT NULL,
  1445.                   size int NOT NULL)
  1446.  
  1447.     /* Insert row for each database */
  1448.     insert into #databases
  1449.         select
  1450.             name,
  1451.             (select sum(size) from master.dbo.sysusages
  1452.                 where dbid = d.dbid)
  1453.         from master.dbo.sysdatabases d
  1454.  
  1455.     select
  1456.          convert(varchar(32),DATABASE_NAME),
  1457.          DATABASE_SIZE = size*2,    /* Convert from 2048 byte pages to K */
  1458.          REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  1459.     from #databases
  1460.     order by 1
  1461. go
  1462.  
  1463. grant execute on sp_databases to public
  1464. go
  1465.  
  1466. dump tran master with no_log
  1467. go
  1468.  
  1469. print 'creating sp_datatype_info'
  1470. go
  1471.  
  1472. /*    Procedure for pre-6.0 server */
  1473. create proc sp_datatype_info
  1474.     (@data_type int = 0, @ODBCVer tinyint = 2)
  1475. as
  1476.     if @ODBCVer <> 3
  1477.         select @ODBCVer = 2
  1478.     if @data_type = 0
  1479.         select
  1480.             TYPE_NAME = t.name,
  1481.             d.DATA_TYPE,
  1482.             "PRECISION" = isnull(d.data_precision, convert(int,t.length)),
  1483.             d.LITERAL_PREFIX,
  1484.             d.LITERAL_SUFFIX,
  1485.             e.CREATE_PARAMS,
  1486.             d.NULLABLE,
  1487.             d.CASE_SENSITIVE,
  1488.             d.SEARCHABLE,
  1489.             d.UNSIGNED_ATTRIBUTE,
  1490.             d.MONEY,
  1491.             d.AUTO_INCREMENT,
  1492.             LOCAL_TYPE_NAME = t.name,
  1493.             MINIMUM_SCALE = d.numeric_scale,
  1494.             MAXIMUM_SCALE = d.numeric_scale,
  1495.             d.SQL_DATA_TYPE,
  1496.             d.SQL_DATETIME_SUB,
  1497.             NUM_PREC_RADIX = convert(int,d.RADIX),
  1498.             INTERVAL_PRECISION = convert(smallint,NULL),
  1499.             USERTYPE = t.usertype
  1500.         from master.dbo.spt_datatype_info d, master.dbo.spt_datatype_info_ext e, systypes t
  1501.         where
  1502.             d.ss_dtype = t.type
  1503.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  1504.             and t.usertype *= e.user_type
  1505.             and t.type not in (111,109,38,110)    /* get rid of nullable types */
  1506.         order by 2, 12, 11, t.usertype
  1507.  
  1508.     else
  1509.         select
  1510.             TYPE_NAME = t.name,
  1511.             d.DATA_TYPE,
  1512.             "PRECISION" = isnull(d.data_precision, convert(int,t.length)),
  1513.             d.LITERAL_PREFIX,
  1514.             d.LITERAL_SUFFIX,
  1515.             e.CREATE_PARAMS,
  1516.             d.NULLABLE,
  1517.             d.CASE_SENSITIVE,
  1518.             d.SEARCHABLE,
  1519.             d.UNSIGNED_ATTRIBUTE,
  1520.             d.MONEY,
  1521.             d.AUTO_INCREMENT,
  1522.             LOCAL_TYPE_NAME = t.name,
  1523.             MINIMUM_SCALE = d.numeric_scale,
  1524.             MAXIMUM_SCALE = d.numeric_scale,
  1525.             d.SQL_DATA_TYPE,
  1526.             d.SQL_DATETIME_SUB,
  1527.             NUM_PREC_RADIX = convert(int,d.RADIX),
  1528.             INTERVAL_PRECISION = convert(smallint,NULL),
  1529.             USERTYPE = t.usertype
  1530.         from master.dbo.spt_datatype_info d, master.dbo.spt_datatype_info_ext e, systypes t
  1531.         where
  1532.             DATA_TYPE = @data_type
  1533.             and d.ss_dtype = t.type
  1534.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  1535.             and t.usertype *= e.user_type
  1536.             and t.type not in (111,109,38,110)    /* get rid of nullable types */
  1537.         order by 12, 11, t.usertype
  1538.  
  1539. go
  1540.  
  1541. if (charindex('6.00', @@version) = 0 and
  1542.     charindex('6.50', @@version) = 0 and
  1543.     charindex('7.00', @@version) = 0)
  1544. begin
  1545.     print ''
  1546.     print ''
  1547.     print 'Warning:'
  1548.     print 'you are installing the stored procedures '
  1549.     print 'on a pre 6.0 SQL Server.'
  1550.     print 'Ignore the following errors.'
  1551. end
  1552. else
  1553.     drop proc sp_datatype_info
  1554. go
  1555.  
  1556. /*    Procedure for 6.0 server */
  1557. create proc sp_datatype_info
  1558.     (@data_type int = 0, @ODBCVer tinyint = 2)
  1559. as
  1560.     if @ODBCVer <> 3
  1561.         select @ODBCVer = 2
  1562.     if @data_type = 0
  1563.         select
  1564.             convert(varchar(30),case
  1565.                 when t.usertype > 100 or t.usertype in (18,80) then t.name
  1566.                 else d.TYPE_NAME
  1567.             end) TYPE_NAME,
  1568.             d.DATA_TYPE,
  1569.             convert(int,case
  1570.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  1571.                 when d.ss_dtype in (35,34) then 2147483647                /* TEXT/IMAGE */
  1572.                 when d.ss_dtype in (55,63,106,108) then @@max_precision /* DECIMAL/NUMERIC */
  1573.                 else t.prec
  1574.             end) "PRECISION",
  1575.             d.LITERAL_PREFIX,
  1576.             d.LITERAL_SUFFIX,
  1577.             e.CREATE_PARAMS,
  1578.             convert(smallint,case
  1579.                 when d.AUTO_INCREMENT = 1 then 0 /* IDENTITY*/
  1580.                 else t.allownulls
  1581.             end) NULLABLE,
  1582.             d.CASE_SENSITIVE,
  1583.             d.SEARCHABLE,
  1584.             d.UNSIGNED_ATTRIBUTE,
  1585.             d.MONEY,
  1586.             d.AUTO_INCREMENT,
  1587.             convert(varchar(30),case
  1588.                 when t.usertype > 100 or t.usertype in (18,80) then t.name
  1589.                 else d.TYPE_NAME
  1590.             end) LOCAL_TYPE_NAME,
  1591.             MINIMUM_SCALE = d.numeric_scale,
  1592.             convert(smallint,case
  1593.                 when d.ss_dtype in (106,108) and d.AUTO_INCREMENT = 0 then @@max_precision /* DECIMAL/NUMERIC */
  1594.                 when d.ss_dtype in (106,108) and d.AUTO_INCREMENT = 1 then 0 /* DECIMAL/NUMERIC IDENTITY*/
  1595.                 else t.scale
  1596.             end) MAXIMUM_SCALE,
  1597.             d.SQL_DATA_TYPE,
  1598.             d.SQL_DATETIME_SUB,
  1599.             NUM_PREC_RADIX = convert(int,d.RADIX),
  1600.             INTERVAL_PRECISION = convert(smallint,NULL),
  1601.             USERTYPE = t.usertype
  1602.         from master.dbo.spt_datatype_info d, master.dbo.spt_datatype_info_ext e, systypes t
  1603.         where
  1604.             d.ss_dtype = t.type
  1605.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  1606.             and t.usertype *= e.user_type
  1607.             and isnull(d.AUTO_INCREMENT,0) *= e.AUTO_INCREMENT
  1608.             and t.type not in (111,109,38,110,55,63)    /* get rid of nullable types */
  1609.         order by 2, 12, 11, t.usertype
  1610.  
  1611.     else
  1612.         select
  1613.             convert (varchar(30),case
  1614.                 when t.usertype > 100 or t.usertype in (18,80) then t.name
  1615.                 else d.TYPE_NAME
  1616.             end) TYPE_NAME,
  1617.             d.DATA_TYPE,
  1618.             convert(int,case
  1619.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  1620.                 when d.ss_dtype in (35,34) then 2147483647                /* TEXT/IMAGE */
  1621.                 when d.ss_dtype in (55,63,106,108) then @@max_precision /* DECIMAL/NUMERIC */
  1622.                 else t.prec
  1623.             end) "PRECISION",
  1624.             d.LITERAL_PREFIX,
  1625.             d.LITERAL_SUFFIX,
  1626.             e.CREATE_PARAMS,
  1627.             convert(smallint,case
  1628.                 when d.AUTO_INCREMENT = 1 then 0 /* IDENTITY*/
  1629.                 else t.allownulls
  1630.             end) NULLABLE,
  1631.             d.CASE_SENSITIVE,
  1632.             d.SEARCHABLE,
  1633.             d.UNSIGNED_ATTRIBUTE,
  1634.             d.MONEY,
  1635.             d.AUTO_INCREMENT,
  1636.             convert(varchar(30),case
  1637.                 when t.usertype > 100 or t.usertype in (18,80) then t.name
  1638.                 else d.TYPE_NAME
  1639.             end) LOCAL_TYPE_NAME,
  1640.             MINIMUM_SCALE = d.numeric_scale,
  1641.             convert(smallint,case
  1642.                 when d.ss_dtype in (106,108) and d.AUTO_INCREMENT = 0 then convert(smallint,@@max_precision) /* DECIMAL/NUMERIC */
  1643.                 when d.ss_dtype in (106,108) and d.AUTO_INCREMENT = 1 then convert(smallint,0) /* DECIMAL/NUMERIC IDENTITY*/
  1644.                 else t.scale
  1645.             end) MAXIMUM_SCALE,
  1646.             d.SQL_DATA_TYPE,
  1647.             d.SQL_DATETIME_SUB,
  1648.             NUM_PREC_RADIX = convert(int,d.RADIX),
  1649.             INTERVAL_PRECISION = convert(smallint,NULL),
  1650.             USERTYPE = t.usertype
  1651.         from master.dbo.spt_datatype_info d, master.dbo.spt_datatype_info_ext e, systypes t
  1652.         where
  1653.             d.DATA_TYPE = @data_type
  1654.             and d.ss_dtype = t.type
  1655.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  1656.             and t.usertype *= e.user_type
  1657.             and isnull(d.AUTO_INCREMENT,0) *= e.AUTO_INCREMENT
  1658.             and t.type not in (111,109,38,110,55,63)    /* get rid of nullable types */
  1659.         order by 12, 11, t.usertype
  1660. go
  1661.  
  1662. grant execute on sp_datatype_info to public
  1663. go
  1664.  
  1665. dump tran master with no_log
  1666. go
  1667.  
  1668. print 'creating sp_fkeys'
  1669. go
  1670.  
  1671. /*    Procedure for pre-6.0 server */
  1672. CREATE PROCEDURE sp_fkeys(
  1673.                @pktable_name        varchar(32) = null,
  1674.                @pktable_owner        varchar(32) = null,
  1675.                @pktable_qualifier    varchar(32) = null,
  1676.                @fktable_name        varchar(32) = null,
  1677.                @fktable_owner        varchar(32) = null,
  1678.                @fktable_qualifier    varchar(32) = null )
  1679. as
  1680.     declare    @order_by_pk int
  1681.  
  1682.     select  @order_by_pk = 0
  1683.  
  1684.     if (@pktable_name is null) and (@fktable_name is null)
  1685.     begin    /* If neither primary key nor foreign key table names given */
  1686.         raiserror 20004 'PK table name or FK table name must be given.'
  1687.         return
  1688.     end
  1689.     if @fktable_qualifier is not null
  1690.     begin
  1691.         if db_name() <> @fktable_qualifier
  1692.         begin    /* If qualifier doesn't match current database */
  1693.             raiserror 20001 'Foreign Key Table qualifier must be name of current database'
  1694.             return
  1695.         end
  1696.     end
  1697.     if @pktable_qualifier is not null
  1698.     begin
  1699.         if db_name() <> @pktable_qualifier
  1700.         begin    /* If qualifier doesn't match current database */
  1701.             raiserror 20001 'Primary Key Table qualifier must be name of current database'
  1702.             return
  1703.         end
  1704.     end
  1705.  
  1706.     if @pktable_name is null
  1707.     begin /*  If table name not supplied, match all */
  1708.         select @pktable_name = '%'
  1709.         select @order_by_pk = 1
  1710.     end
  1711.     if @pktable_owner is null    /*    If PK owner not supplied, match all */
  1712.         select @pktable_owner = '%'
  1713.     if @fktable_name is null    /*    If table name not supplied, match all */
  1714.         select @fktable_name = '%'
  1715.     if @fktable_owner is null    /*    If FK owner not supplied, match all */
  1716.         select @fktable_owner = '%'
  1717.  
  1718.     if @@trancount <> 0
  1719.     begin    /* If inside a transaction */
  1720.         raiserror 20003 'The procedure ''sp_fkeys'' cannot be executed from within a transaction.'
  1721.         return
  1722.     end
  1723.     create table #fkeys(
  1724.              PKTABLE_QUALIFIER    varchar(32) NULL,
  1725.              PKTABLE_OWNER        varchar(32) NULL,
  1726.              PKTABLE_NAME        varchar(32) NOT NULL,
  1727.              PKCOLUMN_NAME        varchar(32) NOT NULL,
  1728.              FKTABLE_QUALIFIER    varchar(32) NULL,
  1729.              FKTABLE_OWNER        varchar(32) NULL,
  1730.              FKTABLE_NAME        varchar(32) NOT NULL,
  1731.              FKCOLUMN_NAME        varchar(32) NOT NULL,
  1732.              KEY_SEQ            smallint NOT NULL)
  1733.  
  1734.     /*    SQL Server supports upto 8 PK/FK relationships between 2 tables */
  1735.     /*    Process syskeys for each relationship */
  1736.     /*    The inserts below adds a row to the temp table for each of the
  1737.         8 possible relationships */
  1738.     insert into #fkeys
  1739.         select
  1740.             db_name(),
  1741.             user_name(o1.uid),
  1742.             object_name(k.depid),
  1743.             c2.name,
  1744.             db_name(),
  1745.             user_name(o2.uid),
  1746.             object_name(k.id),
  1747.             c1.name,
  1748.             1
  1749.         from
  1750.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  1751.         where
  1752.             c1.id = k.id
  1753.             and k.type = 2    /* Foreign type key */
  1754.             and c1.colid = k.key1
  1755.             and c2.id = k.depid
  1756.             and c2.colid = k.depkey1
  1757.             and o1.id = k.depid
  1758.             and o2.id = k.id
  1759.     union all
  1760.         select
  1761.             db_name(),
  1762.             user_name(o1.uid),
  1763.             object_name(k.depid),
  1764.             c2.name,
  1765.             db_name(),
  1766.             user_name(o2.uid),
  1767.             object_name(k.id),
  1768.             c1.name,
  1769.             2
  1770.         from
  1771.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  1772.         where
  1773.             c1.id = k.id
  1774.             and k.type = 2    /* Foreign type key */
  1775.             and c1.colid = k.key2
  1776.             and c2.id = k.depid
  1777.             and c2.colid = k.depkey2
  1778.             and o1.id = k.depid
  1779.             and o2.id = k.id
  1780.     union all
  1781.         select
  1782.             db_name(),
  1783.             user_name(o1.uid),
  1784.             object_name(k.depid),
  1785.             c2.name,
  1786.             db_name(),
  1787.             user_name(o2.uid),
  1788.             object_name(k.id),
  1789.             c1.name,
  1790.             3
  1791.         from
  1792.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  1793.         where
  1794.             c1.id = k.id
  1795.             and k.type = 2    /* Foreign type key */
  1796.             and c1.colid = k.key3
  1797.             and c2.id = k.depid
  1798.             and c2.colid = k.depkey3
  1799.             and o1.id = k.depid
  1800.             and o2.id = k.id
  1801.     union all
  1802.         select
  1803.             db_name(),
  1804.             user_name(o1.uid),
  1805.             object_name(k.depid),
  1806.             c2.name,
  1807.             db_name(),
  1808.             user_name(o2.uid),
  1809.             object_name(k.id),
  1810.             c1.name,
  1811.             4
  1812.         from
  1813.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  1814.         where
  1815.             c1.id = k.id
  1816.             and k.type = 2    /* Foreign type key */
  1817.             and c1.colid = k.key4
  1818.             and c2.id = k.depid
  1819.             and c2.colid = k.depkey4
  1820.             and o1.id = k.depid
  1821.             and o2.id = k.id
  1822.     union all
  1823.         select
  1824.             db_name(),
  1825.             user_name(o1.uid),
  1826.             object_name(k.depid),
  1827.             c2.name,
  1828.             db_name(),
  1829.             user_name(o2.uid),
  1830.             object_name(k.id),
  1831.             c1.name,
  1832.             5
  1833.         from
  1834.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  1835.         where
  1836.             c1.id = k.id
  1837.             and k.type = 2    /* Foreign type key */
  1838.             and c1.colid = k.key5
  1839.             and c2.id = k.depid
  1840.             and c2.colid = k.depkey5
  1841.             and o1.id = k.depid
  1842.             and o2.id = k.id
  1843.     union all
  1844.         select
  1845.             db_name(),
  1846.             user_name(o1.uid),
  1847.             object_name(k.depid),
  1848.             c2.name,
  1849.             db_name(),
  1850.             user_name(o2.uid),
  1851.             object_name(k.id),
  1852.             c1.name,
  1853.             6
  1854.         from
  1855.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  1856.         where
  1857.             c1.id = k.id
  1858.             and k.type = 2    /* Foreign type key */
  1859.             and c1.colid = k.key6
  1860.             and c2.id = k.depid
  1861.             and c2.colid = k.depkey6
  1862.             and o1.id = k.depid
  1863.             and o2.id = k.id
  1864.     union all
  1865.         select
  1866.             db_name(),
  1867.             user_name(o1.uid),
  1868.             object_name(k.depid),
  1869.             c2.name,
  1870.             db_name(),
  1871.             user_name(o2.uid),
  1872.             object_name(k.id),
  1873.             c1.name,
  1874.             7
  1875.         from
  1876.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  1877.         where
  1878.             c1.id = k.id
  1879.             and k.type = 2    /* Foreign type key */
  1880.             and c1.colid = k.key7
  1881.             and c2.id = k.depid
  1882.             and c2.colid = k.depkey7
  1883.             and o1.id = k.depid
  1884.             and o2.id = k.id
  1885.     union all
  1886.         select
  1887.             db_name(),
  1888.             user_name(o1.uid),
  1889.             object_name(k.depid),
  1890.             c2.name,
  1891.             db_name(),
  1892.             user_name(o2.uid),
  1893.             object_name(k.id),
  1894.             c1.name,
  1895.             8
  1896.         from
  1897.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  1898.         where
  1899.             c1.id = k.id
  1900.             and k.type = 2    /* Foreign type key */
  1901.             and c1.colid = k.key8
  1902.             and c2.id = k.depid
  1903.             and c2.colid = k.depkey8
  1904.             and o1.id = k.depid
  1905.             and o2.id = k.id
  1906.  
  1907.     if @order_by_pk = 1 /*    If order by PK fields */
  1908.         select
  1909.             PKTABLE_QUALIFIER,
  1910.             PKTABLE_OWNER,
  1911.             PKTABLE_NAME,
  1912.             PKCOLUMN_NAME,
  1913.             FKTABLE_QUALIFIER,
  1914.             FKTABLE_OWNER,
  1915.             FKTABLE_NAME,
  1916.             FKCOLUMN_NAME,
  1917.             KEY_SEQ,
  1918.             UPDATE_RULE = convert(smallint, null),
  1919.             DELETE_RULE = convert(smallint,null),
  1920.             FK_NAME = convert(varchar(32),null),
  1921.             PK_NAME = convert(varchar(32),null)
  1922.         from #fkeys
  1923.         where FKTABLE_NAME like @fktable_name
  1924.             and FKTABLE_OWNER like @fktable_owner
  1925.             and PKTABLE_NAME  like @pktable_name
  1926.             and PKTABLE_OWNER like @pktable_owner
  1927.         order by 1, 2, 3, 9
  1928.     else        /*    Order by FK fields */
  1929.         select
  1930.             PKTABLE_QUALIFIER,
  1931.             PKTABLE_OWNER,
  1932.             PKTABLE_NAME,
  1933.             PKCOLUMN_NAME,
  1934.             FKTABLE_QUALIFIER,
  1935.             FKTABLE_OWNER,
  1936.             FKTABLE_NAME,
  1937.             FKCOLUMN_NAME,
  1938.             KEY_SEQ,
  1939.             UPDATE_RULE = convert(smallint,null),
  1940.             DELETE_RULE = convert(smallint,null),
  1941.             FK_NAME = convert(varchar(32),null),
  1942.             PK_NAME = convert(varchar(32),null)
  1943.         from #fkeys
  1944.         where FKTABLE_NAME like @fktable_name
  1945.             and FKTABLE_OWNER like @fktable_owner
  1946.             and PKTABLE_NAME  like @pktable_name
  1947.             and PKTABLE_OWNER like @pktable_owner
  1948.         order by 5, 6, 7, 9
  1949. go
  1950.  
  1951. if (charindex('6.00', @@version) = 0 and
  1952.     charindex('6.50', @@version) = 0 and
  1953.     charindex('7.00', @@version) = 0)
  1954. begin
  1955.     print ''
  1956.     print ''
  1957.     print 'Warning:'
  1958.     print 'you are installing the stored procedures '
  1959.     print 'on a pre 6.0 SQL Server.'
  1960.     print 'Ignore the following errors.'
  1961. end
  1962. else
  1963.     drop proc sp_fkeys
  1964. go
  1965.  
  1966. /*    Procedure for 6.0 server */
  1967. CREATE PROCEDURE sp_fkeys(
  1968.                @pktable_name        varchar(32) = null,
  1969.                @pktable_owner        varchar(32) = null,
  1970.                @pktable_qualifier    varchar(32) = null,
  1971.                @fktable_name        varchar(32) = null,
  1972.                @fktable_owner        varchar(32) = null,
  1973.                @fktable_qualifier    varchar(32) = null )
  1974. as
  1975.     DECLARE @pktable_id            int
  1976.     DECLARE @pkfull_table_name    char(70)
  1977.     DECLARE @fktable_id            int
  1978.     DECLARE @fkfull_table_name    char(70)
  1979.     declare    @order_by_pk        int
  1980.  
  1981.     select  @order_by_pk = 0
  1982.  
  1983.     if (@pktable_name is null) and (@fktable_name is null)
  1984.     begin    /* If neither primary key nor foreign key table names given */
  1985.         raiserror (15252,-1,-1)
  1986.         return
  1987.     end
  1988.  
  1989.     if @pktable_owner is null
  1990.     begin    /* If unqualified primary key table name */
  1991.         SELECT @pkfull_table_name = @pktable_name
  1992.     end
  1993.     else
  1994.     begin    /* Qualified primary key table name */
  1995.         SELECT @pkfull_table_name = @pktable_owner + '.' + @pktable_name
  1996.     end
  1997.     /*    Get Object ID */
  1998.     SELECT @pktable_id = object_id(@pkfull_table_name)
  1999.  
  2000.     if @fktable_owner is null
  2001.     begin    /* If unqualified foreign key table name */
  2002.         SELECT @fkfull_table_name = @fktable_name
  2003.     end
  2004.     else
  2005.     begin    /* Qualified foreign key table name */
  2006.         SELECT @fkfull_table_name = @fktable_owner + '.' + @fktable_name
  2007.     end
  2008.     /*    Get Object ID */
  2009.     SELECT @fktable_id = object_id(@fkfull_table_name)
  2010.  
  2011.     if @fktable_name is not null
  2012.     begin
  2013.         if @fktable_id is null
  2014.             SELECT @fktable_id = 0    /* fk table not found, empty result */
  2015.     end
  2016.  
  2017.     if @pktable_name is null
  2018.     begin /*  If table name not supplied, match all */
  2019.         select @order_by_pk = 1
  2020.     end
  2021.     else
  2022.     begin
  2023.         if @pktable_id is null
  2024.         begin
  2025.             SELECT @pktable_id = 0    /* pk table not found, empty result */
  2026.         end
  2027.     end
  2028.  
  2029.     if (@@trancount <> 0 and
  2030.         charindex('6.50', @@version) = 0 and
  2031.         charindex('7.00', @@version) = 0)
  2032.     begin    /* If inside a transaction */
  2033.         raiserror(15002,-1,-1,'sp_fkeys')
  2034.         return
  2035.     end
  2036.  
  2037.     create table #fkeys(
  2038.              pkdb_id        int NOT NULL,
  2039.              pktable_id     int NOT NULL,
  2040.              pkcolid        int NOT NULL,
  2041.              fkdb_id        int NOT NULL,
  2042.              fktable_id        int NOT NULL,
  2043.              fkcolid        int NOT NULL,
  2044.              KEY_SEQ        smallint NOT NULL,
  2045.              fk_id            int NOT NULL,
  2046.              pk_id            int NOT NULL)
  2047.  
  2048.     /*    SQL Server supports upto 16 PK/FK relationships between 2 tables */
  2049.     /*    Process syskeys for each relationship */
  2050.     /*    The inserts below adds a row to the temp table for each of the
  2051.         16 possible relationships */
  2052.     insert into #fkeys
  2053.         select
  2054.             r.rkeydbid,
  2055.             r.rkeyid,
  2056.             r.rkey1,
  2057.             r.fkeydbid,
  2058.             r.fkeyid,
  2059.             r.fkey1,
  2060.             1,
  2061.             r.constid,
  2062.             s.constid
  2063.         from
  2064.             sysreferences r, sysconstraints s
  2065.         where    r.rkeyid = s.id
  2066.             AND (s.status & 0xf) = 1
  2067.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  2068.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  2069.       union all
  2070.         select
  2071.             r.rkeydbid,
  2072.             r.rkeyid,
  2073.             r.rkey2,
  2074.             r.fkeydbid,
  2075.             r.fkeyid,
  2076.             r.fkey2,
  2077.             2,
  2078.             r.constid,
  2079.             s.constid
  2080.         from
  2081.             sysreferences r, sysconstraints s
  2082.         where    r.rkeyid = s.id
  2083.             AND (s.status & 0xf) = 1
  2084.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  2085.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  2086.       union all
  2087.         select
  2088.             r.rkeydbid,
  2089.             r.rkeyid,
  2090.             r.rkey3,
  2091.             r.fkeydbid,
  2092.             r.fkeyid,
  2093.             r.fkey3,
  2094.             3,
  2095.             r.constid,
  2096.             s.constid
  2097.         from
  2098.             sysreferences r, sysconstraints s
  2099.         where    r.rkeyid = s.id
  2100.             AND (s.status & 0xf) = 1
  2101.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  2102.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  2103.       union all
  2104.         select
  2105.             r.rkeydbid,
  2106.             r.rkeyid,
  2107.             r.rkey4,
  2108.             r.fkeydbid,
  2109.             r.fkeyid,
  2110.             r.fkey4,
  2111.             4,
  2112.             r.constid,
  2113.             s.constid
  2114.         from
  2115.             sysreferences r, sysconstraints s
  2116.         where    r.rkeyid = s.id
  2117.             AND (s.status & 0xf) = 1
  2118.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  2119.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  2120.       union all
  2121.         select
  2122.             r.rkeydbid,
  2123.             r.rkeyid,
  2124.             r.rkey5,
  2125.             r.fkeydbid,
  2126.             r.fkeyid,
  2127.             r.fkey5,
  2128.             5,
  2129.             r.constid,
  2130.             s.constid
  2131.         from
  2132.             sysreferences r, sysconstraints s
  2133.         where    r.rkeyid = s.id
  2134.             AND (s.status & 0xf) = 1
  2135.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  2136.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  2137.       union all
  2138.         select
  2139.             r.rkeydbid,
  2140.             r.rkeyid,
  2141.             r.rkey6,
  2142.             r.fkeydbid,
  2143.             r.fkeyid,
  2144.             r.fkey6,
  2145.             6,
  2146.             r.constid,
  2147.             s.constid
  2148.         from
  2149.             sysreferences r, sysconstraints s
  2150.         where    r.rkeyid = s.id
  2151.             AND (s.status & 0xf) = 1
  2152.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  2153.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  2154.       union all
  2155.         select
  2156.             r.rkeydbid,
  2157.             r.rkeyid,
  2158.             r.rkey7,
  2159.             r.fkeydbid,
  2160.             r.fkeyid,
  2161.             r.fkey7,
  2162.             7,
  2163.             r.constid,
  2164.             s.constid
  2165.         from
  2166.             sysreferences r, sysconstraints s
  2167.         where    r.rkeyid = s.id
  2168.             AND (s.status & 0xf) = 1
  2169.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  2170.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  2171.       union all
  2172.         select
  2173.             r.rkeydbid,
  2174.             r.rkeyid,
  2175.             r.rkey8,
  2176.             r.fkeydbid,
  2177.             r.fkeyid,
  2178.             r.fkey8,
  2179.             8,
  2180.             r.constid,
  2181.             s.constid
  2182.         from
  2183.             sysreferences r, sysconstraints s
  2184.         where    r.rkeyid = s.id
  2185.             AND (s.status & 0xf) = 1
  2186.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  2187.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  2188.       union all
  2189.         select
  2190.             r.rkeydbid,
  2191.             r.rkeyid,
  2192.             r.rkey9,
  2193.             r.fkeydbid,
  2194.             r.fkeyid,
  2195.             r.fkey9,
  2196.             9,
  2197.             r.constid,
  2198.             s.constid
  2199.         from
  2200.             sysreferences r, sysconstraints s
  2201.         where    r.rkeyid = s.id
  2202.             AND (s.status & 0xf) = 1
  2203.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  2204.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  2205.       union all
  2206.         select
  2207.             r.rkeydbid,
  2208.             r.rkeyid,
  2209.             r.rkey10,
  2210.             r.fkeydbid,
  2211.             r.fkeyid,
  2212.             r.fkey10,
  2213.             10,
  2214.             r.constid,
  2215.             s.constid
  2216.         from
  2217.             sysreferences r, sysconstraints s
  2218.         where    r.rkeyid = s.id
  2219.             AND (s.status & 0xf) = 1
  2220.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  2221.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  2222.       union all
  2223.         select
  2224.             r.rkeydbid,
  2225.             r.rkeyid,
  2226.             r.rkey11,
  2227.             r.fkeydbid,
  2228.             r.fkeyid,
  2229.             r.fkey11,
  2230.             11,
  2231.             r.constid,
  2232.             s.constid
  2233.         from
  2234.             sysreferences r, sysconstraints s
  2235.         where    r.rkeyid = s.id
  2236.             AND (s.status & 0xf) = 1
  2237.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  2238.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  2239.       union all
  2240.         select
  2241.             r.rkeydbid,
  2242.             r.rkeyid,
  2243.             r.rkey12,
  2244.             r.fkeydbid,
  2245.             r.fkeyid,
  2246.             r.fkey12,
  2247.             12,
  2248.             r.constid,
  2249.             s.constid
  2250.         from
  2251.             sysreferences r, sysconstraints s
  2252.         where    r.rkeyid = s.id
  2253.             AND (s.status & 0xf) = 1
  2254.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  2255.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  2256.       union all
  2257.         select
  2258.             r.rkeydbid,
  2259.             r.rkeyid,
  2260.             r.rkey13,
  2261.             r.fkeydbid,
  2262.             r.fkeyid,
  2263.             r.fkey13,
  2264.             13,
  2265.             r.constid,
  2266.             s.constid
  2267.         from
  2268.             sysreferences r, sysconstraints s
  2269.         where    r.rkeyid = s.id
  2270.             AND (s.status & 0xf) = 1
  2271.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  2272.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  2273.       union all
  2274.         select
  2275.             r.rkeydbid,
  2276.             r.rkeyid,
  2277.             r.rkey14,
  2278.             r.fkeydbid,
  2279.             r.fkeyid,
  2280.             r.fkey14,
  2281.             14,
  2282.             r.constid,
  2283.             s.constid
  2284.         from
  2285.             sysreferences r, sysconstraints s
  2286.         where    r.rkeyid = s.id
  2287.             AND (s.status & 0xf) = 1
  2288.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  2289.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  2290.       union all
  2291.         select
  2292.             r.rkeydbid,
  2293.             r.rkeyid,
  2294.             r.rkey15,
  2295.             r.fkeydbid,
  2296.             r.fkeyid,
  2297.             r.fkey15,
  2298.             15,
  2299.             r.constid,
  2300.             s.constid
  2301.         from
  2302.             sysreferences r, sysconstraints s
  2303.         where    r.rkeyid = s.id
  2304.             AND (s.status & 0xf) = 1
  2305.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  2306.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  2307.       union all
  2308.         select
  2309.             r.rkeydbid,
  2310.             r.rkeyid,
  2311.             r.rkey16,
  2312.             r.fkeydbid,
  2313.             r.fkeyid,
  2314.             r.fkey16,
  2315.             16,
  2316.             r.constid,
  2317.             s.constid
  2318.         from
  2319.             sysreferences r, sysconstraints s
  2320.         where    r.rkeyid = s.id
  2321.             AND (s.status & 0xf) = 1
  2322.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  2323.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  2324.  
  2325.     if @order_by_pk = 1 /*    If order by PK fields */
  2326.         select
  2327.             PKTABLE_QUALIFIER = convert(varchar(32),DB_NAME(f.pkdb_id)),
  2328.             PKTABLE_OWNER = convert(varchar(32),USER_NAME(o1.uid)),
  2329.             PKTABLE_NAME = convert(varchar(32),o1.name),
  2330.             PKCOLUMN_NAME = convert(varchar(32),c1.name),
  2331.             FKTABLE_QUALIFIER = convert(varchar(32),DB_NAME(f.fkdb_id)),
  2332.             FKTABLE_OWNER = convert(varchar(32),USER_NAME(o2.uid)),
  2333.             FKTABLE_NAME = convert(varchar(32),o2.name),
  2334.             FKCOLUMN_NAME = convert(varchar(32),c2.name),
  2335.             KEY_SEQ,
  2336.             UPDATE_RULE = convert(smallint,1),
  2337.             DELETE_RULE = convert(smallint,1),
  2338.             FK_NAME = convert(varchar(128),OBJECT_NAME(fk_id)),
  2339.             PK_NAME = convert(varchar(128),OBJECT_NAME(pk_id))
  2340.         from #fkeys f,
  2341.             sysobjects o1, sysobjects o2,
  2342.             syscolumns c1, syscolumns c2
  2343.         where    o1.id = f.pktable_id
  2344.             AND o2.id = f.fktable_id
  2345.             AND c1.id = f.pktable_id
  2346.             AND c2.id = f.fktable_id
  2347.             AND c1.colid = f.pkcolid
  2348.             AND c2.colid = f.fkcolid
  2349.         order by 1,2,3,9
  2350.     else        /*    Order by FK fields */
  2351.         select
  2352.             PKTABLE_QUALIFIER = convert(varchar(32),DB_NAME(f.pkdb_id)),
  2353.             PKTABLE_OWNER = convert(varchar(32),USER_NAME(o1.uid)),
  2354.             PKTABLE_NAME = convert(varchar(32),o1.name),
  2355.             PKCOLUMN_NAME = convert(varchar(32),c1.name),
  2356.             FKTABLE_QUALIFIER = convert(varchar(32),DB_NAME(f.fkdb_id)),
  2357.             FKTABLE_OWNER = convert(varchar(32),USER_NAME(o2.uid)),
  2358.             FKTABLE_NAME = convert(varchar(32),o2.name),
  2359.             FKCOLUMN_NAME = convert(varchar(32),c2.name),
  2360.             KEY_SEQ,
  2361.             UPDATE_RULE = convert(smallint,1),
  2362.             DELETE_RULE = convert(smallint,1),
  2363.             FK_NAME = convert(varchar(128),OBJECT_NAME(fk_id)),
  2364.             PK_NAME = convert(varchar(128),OBJECT_NAME(pk_id))
  2365.         from #fkeys f,
  2366.             sysobjects o1, sysobjects o2,
  2367.             syscolumns c1, syscolumns c2
  2368.         where    o1.id = f.pktable_id
  2369.             AND o2.id = f.fktable_id
  2370.             AND c1.id = f.pktable_id
  2371.             AND c2.id = f.fktable_id
  2372.             AND c1.colid = f.pkcolid
  2373.             AND c2.colid = f.fkcolid
  2374.         order by 5,6,7,9
  2375. go
  2376.  
  2377. grant execute on sp_fkeys to public
  2378. go
  2379.  
  2380. dump tran master with no_log
  2381. go
  2382.  
  2383. print 'creating sp_pkeys'
  2384. go
  2385.  
  2386. /*    Procedure for pre-6.0 server */
  2387. CREATE PROCEDURE sp_pkeys(
  2388.                @table_name        varchar(32),
  2389.                @table_owner     varchar(32) = null,
  2390.                @table_qualifier varchar(32) = null )
  2391. as
  2392.     if @table_qualifier is not null
  2393.     begin
  2394.         if db_name() <> @table_qualifier
  2395.         begin    /* If qualifier doesn't match current database */
  2396.             raiserror 20001 'Table qualifier must be name of current database'
  2397.             return
  2398.         end
  2399.     end
  2400.     if @table_owner is null /*    If owner not supplied, match all */
  2401.         select @table_owner = '%'
  2402.     if @@trancount <> 0
  2403.     begin    /* If inside a transaction */
  2404.         raiserror 20003 'The procedure ''sp_pkeys'' cannot be executed from within a transaction.'
  2405.         return
  2406.     end
  2407.  
  2408.     create table #pkeys(
  2409.              TABLE_QUALIFIER varchar(32) NULL,
  2410.              TABLE_OWNER     varchar(32) NULL,
  2411.              TABLE_NAME      varchar(32) NOT NULL,
  2412.              COLUMN_NAME     varchar(32) NOT NULL,
  2413.              KEY_SEQ         smallint NOT NULL)
  2414.  
  2415.     /*    SQL Server supports upto 8 PK/FK relationships between 2 tables */
  2416.     /*    Process syskeys for each relationship */
  2417.     /*    The inserts below adds a row to the temp table for each of the
  2418.         8 possible relationships */
  2419.     insert into #pkeys
  2420.         select
  2421.             db_name(),
  2422.             (select user_name(uid) from sysobjects o where o.id = k.id),
  2423.             object_name(k.id),
  2424.             c.name,
  2425.             1
  2426.         from
  2427.             syskeys k, syscolumns c
  2428.         where
  2429.             c.id = k.id
  2430.             and k.type = 1    /* Primary type key */
  2431.             and c.colid = k.key1
  2432.     if (@@rowcount = 0)
  2433.         goto done
  2434.  
  2435.     insert into #pkeys
  2436.         select
  2437.             db_name(),
  2438.             (select user_name(uid) from sysobjects o where o.id = k.id),
  2439.             object_name(k.id),
  2440.             c.name,
  2441.             2
  2442.         from
  2443.             syskeys k, syscolumns c
  2444.         where
  2445.             c.id = k.id
  2446.             and k.type = 1    /* Primary type key */
  2447.             and c.colid = key2
  2448.     if (@@rowcount = 0)
  2449.         goto done
  2450.  
  2451.     insert into #pkeys
  2452.         select
  2453.             db_name(),
  2454.             (select user_name(uid) from sysobjects o where o.id = k.id),
  2455.             object_name(k.id),
  2456.             c.name,
  2457.             3
  2458.         from
  2459.             syskeys k, syscolumns c
  2460.         where
  2461.             c.id = k.id
  2462.             and k.type = 1    /* Primary type key */
  2463.             and c.colid = key3
  2464.     if (@@rowcount = 0)
  2465.         goto done
  2466.  
  2467.     insert into #pkeys
  2468.         select
  2469.             db_name(),
  2470.             (select user_name(uid) from sysobjects o where o.id = k.id),
  2471.             object_name(k.id),
  2472.             c.name,
  2473.             4
  2474.         from
  2475.             syskeys k, syscolumns c
  2476.         where
  2477.             c.id = k.id
  2478.             and k.type = 1    /* Primary type key */
  2479.             and c.colid = key4
  2480.     if (@@rowcount = 0)
  2481.         goto done
  2482.  
  2483.     insert into #pkeys
  2484.         select
  2485.             db_name(),
  2486.             (select user_name(uid) from sysobjects o where o.id = k.id),
  2487.             object_name(k.id),
  2488.             c.name,
  2489.             5
  2490.         from
  2491.             syskeys k, syscolumns c
  2492.         where
  2493.             c.id = k.id
  2494.             and k.type = 1    /* Primary type key */
  2495.             and c.colid = key5
  2496.     if (@@rowcount = 0)
  2497.         goto done
  2498.  
  2499.     insert into #pkeys
  2500.         select
  2501.             db_name(),
  2502.             (select user_name(uid) from sysobjects o where o.id = k.id),
  2503.             object_name(k.id),
  2504.             c.name,
  2505.             6
  2506.         from
  2507.             syskeys k, syscolumns c
  2508.         where
  2509.             c.id = k.id
  2510.             and k.type = 1    /* Primary type key */
  2511.             and c.colid = key6
  2512.     if (@@rowcount = 0)
  2513.         goto done
  2514.  
  2515.     insert into #pkeys
  2516.         select
  2517.             db_name(),
  2518.             (select user_name(uid) from sysobjects o where o.id = k.id),
  2519.             object_name(k.id),
  2520.             c.name,
  2521.             7
  2522.         from
  2523.             syskeys k, syscolumns c
  2524.         where
  2525.             c.id = k.id
  2526.             and k.type = 1    /* Primary type key */
  2527.             and c.colid = key7
  2528.     if (@@rowcount = 0)
  2529.         goto done
  2530.  
  2531.     insert into #pkeys
  2532.          select
  2533.              db_name(),
  2534.              (select user_name(uid) from sysobjects o where o.id = k.id),
  2535.              object_name(k.id),
  2536.              c.name,
  2537.              8
  2538.          from
  2539.              syskeys k, syscolumns c
  2540.          where
  2541.              c.id = k.id
  2542.              and k.type = 1 /* Primary type key */
  2543.              and c.colid = key8
  2544.  
  2545.     done:
  2546.     select
  2547.         TABLE_QUALIFIER,
  2548.         TABLE_OWNER,
  2549.         TABLE_NAME,
  2550.         COLUMN_NAME,
  2551.         KEY_SEQ,
  2552.         PK_NAME = convert(varchar(32),null)
  2553.     from #pkeys
  2554.     where TABLE_NAME = @table_name
  2555.         and TABLE_OWNER like @table_owner
  2556.     order by 1, 2, 3, 5
  2557. go
  2558.  
  2559. if (charindex('6.00', @@version) = 0 and
  2560.     charindex('6.50', @@version) = 0 and
  2561.     charindex('7.00', @@version) = 0)
  2562. begin
  2563.     print ''
  2564.     print ''
  2565.     print 'Warning:'
  2566.     print 'you are installing the stored procedures '
  2567.     print 'on a pre 6.0 SQL Server.'
  2568.     print 'Ignore the following error.'
  2569. end
  2570. else
  2571.     drop proc sp_pkeys
  2572. go
  2573.  
  2574. /*    Procedure for 6.0 server */
  2575. CREATE PROCEDURE sp_pkeys(
  2576.                @table_name        varchar(32),
  2577.                @table_owner     varchar(32) = null,
  2578.                @table_qualifier varchar(32) = null )
  2579. as
  2580.     DECLARE @table_id            int
  2581.     DECLARE @full_table_name    char(70)
  2582.  
  2583.     if @table_qualifier is not null
  2584.     begin
  2585.         if db_name() <> @table_qualifier
  2586.         begin    /* If qualifier doesn't match current database */
  2587.             raiserror (15250, -1,-1,'Table')
  2588.             return
  2589.         end
  2590.     end
  2591.     if @table_owner is null
  2592.     begin    /* If unqualified table name */
  2593.         SELECT @full_table_name = @table_name
  2594.     end
  2595.     else
  2596.     begin    /* Qualified table name */
  2597.         SELECT @full_table_name = @table_owner + '.' + @table_name
  2598.     end
  2599.     /*    Get Object ID */
  2600.     SELECT @table_id = object_id(@full_table_name)
  2601.  
  2602.     select
  2603.         TABLE_QUALIFIER = convert(varchar(32),db_name()),
  2604.         TABLE_OWNER = convert(varchar(32),user_name(o.uid)),
  2605.         TABLE_NAME = convert(varchar(32),o.name),
  2606.         COLUMN_NAME = convert(varchar(32),c.name),
  2607.         KEY_SEQ = convert(smallint,c1.colid),
  2608.         PK_NAME = convert(varchar(32),i.name)
  2609.     from
  2610.         sysindexes i, syscolumns c, sysobjects o, syscolumns c1
  2611.     where
  2612.         o.id = @table_id
  2613.         and o.id = c.id
  2614.         and o.id = i.id
  2615.         and (i.status & 0x800) = 0x800
  2616.         and c.name = index_col (@full_table_name, i.indid, c1.colid)
  2617.         and c1.colid <= i.keycnt    /* create rows from 1 to keycnt */
  2618.         and c1.id = @table_id
  2619.     order by 1, 2, 3, 5
  2620. go
  2621.  
  2622. grant execute on sp_pkeys to public
  2623. go
  2624.  
  2625. dump tran master with no_log
  2626. go
  2627.  
  2628. print 'creating sp_server_info'
  2629. go
  2630.  
  2631. create proc sp_server_info (
  2632.             @attribute_id  int = null)
  2633. as
  2634.     if @attribute_id is not null
  2635.         select *
  2636.         from master.dbo.spt_server_info
  2637.         where attribute_id = @attribute_id
  2638.     else
  2639.         select *
  2640.         from master.dbo.spt_server_info
  2641.         order by attribute_id
  2642. go
  2643.  
  2644. grant execute on sp_server_info to public
  2645. go
  2646.  
  2647. dump tran master with no_log
  2648. go
  2649.  
  2650. print 'creating sp_special_columns'
  2651. go
  2652.  
  2653. /*    Procedure for pre-6.0 server */
  2654. CREATE PROCEDURE sp_special_columns (
  2655.                  @table_name        varchar(32),
  2656.                  @table_owner        varchar(32) = null,
  2657.                  @table_qualifier    varchar(32) = null,
  2658.                  @col_type            char(1) = 'R',
  2659.                  @scope                char(1) = 'T',
  2660.                  @nullable            char(1) = 'U',
  2661.                  @ODBCVer            int = 2)
  2662. AS
  2663.     DECLARE @indid                int
  2664.     DECLARE @table_id            int
  2665.     DECLARE @full_table_name    char(70)
  2666.     DECLARE @msg                char(70)
  2667.     DECLARE @scopeout            smallint
  2668.  
  2669.     if @col_type not in ('R','V')
  2670.         begin
  2671.             raiserror 20002 'Illegal ''col_type'' specified -- must be ''R'' or ''V''.'
  2672.             return
  2673.         end
  2674.  
  2675.     if @scope = 'C'
  2676.         select @scopeout = 0
  2677.     else if @scope = 'T'
  2678.         select @scopeout = 1
  2679.     else
  2680.         begin
  2681.             raiserror 20002 'Illegal ''scope'' specified -- must be ''C'' or ''T''.'
  2682.             return
  2683.         end
  2684.  
  2685.     if @nullable not in ('U','O')
  2686.         begin
  2687.             raiserror 20002 'Illegal ''nullable'' specified -- must be ''U'' or ''O''.'
  2688.             return
  2689.         end
  2690.  
  2691.     if @table_qualifier is not null
  2692.        begin
  2693.           if db_name() <> @table_qualifier
  2694.               begin    /* If qualifier doesn't match current database */
  2695.                 raiserror 20001 'Table qualifier must be name of current database'
  2696.                 return
  2697.               end
  2698.        end
  2699.     if @table_owner is null
  2700.        begin    /* If unqualified table name */
  2701.           SELECT @full_table_name = @table_name
  2702.        end
  2703.     else
  2704.        begin    /* Qualified table name */
  2705.           SELECT @full_table_name = @table_owner + '.' + @table_name
  2706.        end
  2707.     /*    Get Object ID */
  2708.     SELECT @table_id = object_id(@full_table_name)
  2709.  
  2710.     if @col_type = 'V'
  2711.     BEGIN /* if ROWVER, just run that query */
  2712.         SELECT
  2713.             SCOPE = convert(smallint,NULL),
  2714.             COLUMN_NAME = convert(varchar(32),c.name),
  2715.             DATA_TYPE = convert(smallint, -3),
  2716.             TYPE_NAME = t.name,
  2717.             "PRECISION" = convert(int,8),
  2718.             LENGTH = convert(int,8),
  2719.             SCALE = convert(smallint, NULL),
  2720.             PSEUDO_COLUMN = convert(smallint,1)
  2721.         FROM
  2722.             systypes t, syscolumns c, master.dbo.spt_datatype_info d
  2723.         WHERE
  2724.             c.id = @table_id
  2725.             AND c.type = d.ss_dtype
  2726.             AND c.usertype = 80 /*    TIMESTAMP */
  2727.             AND t.usertype = 80 /*    TIMESTAMP */
  2728.         RETURN
  2729.     END
  2730.  
  2731.     /* ROWID, now find the id of the 'best' index for this table */
  2732.  
  2733.     IF @nullable = 'O'    /* Don't include any indexes that contain
  2734.                            nullable columns. */
  2735.  
  2736.             SELECT @indid = MIN(indid)
  2737.                 FROM sysindexes i,syscolumns c,syscolumns c2
  2738.                 WHERE
  2739.                     i.status&2 = 2        /*    If Unique Index */
  2740.                      AND c.id = i.id
  2741.                      AND c2.id = c.id
  2742.                      AND c2.colid < i.keycnt + (i.status&16)/16
  2743.                     AND i.id = @table_id
  2744.                     AND indid > 0        /*    Eliminate Table Row */
  2745.                     AND c.name = index_col(@table_name,i.indid,c2.colid)
  2746.                     GROUP BY indid HAVING SUM(c.status&8) = 0
  2747.  
  2748.     ELSE    /* Include indexes that are partially nullable. */
  2749.  
  2750.         SELECT @indid = MIN(indid)
  2751.             FROM sysindexes i
  2752.             WHERE
  2753.                 status&2 = 2        /*    If Unique Index */
  2754.                 AND id = @table_id
  2755.                 AND indid > 0        /*    Eliminate Table Row */
  2756.  
  2757.     SELECT
  2758.         SCOPE = @scopeout,
  2759.         COLUMN_NAME = convert(varchar(32),INDEX_COL(@full_table_name,indid,c2.colid)),
  2760.         d.DATA_TYPE,
  2761.         TYPE_NAME = t.name,
  2762.         "PRECISION" = isnull(d.data_precision, convert(int,c.length)),
  2763.         LENGTH = isnull(d.length, convert(int,c.length)),
  2764.         SCALE = d.numeric_scale,
  2765.         PSEUDO_COLUMN = convert(smallint,1)
  2766.     FROM
  2767.         sysindexes x,
  2768.         syscolumns c,
  2769.         master.dbo.spt_datatype_info d,
  2770.         systypes t,
  2771.         syscolumns c2    /* Self-join to generate list of index columns and */
  2772.                         /* to extract datatype names */
  2773.     WHERE
  2774.         x.id = @table_id
  2775.         AND c.name = INDEX_COL(@full_table_name,@indid,c2.colid)
  2776.         AND c.id = x.id
  2777.         AND c2.id = x.id
  2778.         AND c2.colid < keycnt+(x.status&16)/16
  2779.         AND x.indid = @indid
  2780.         AND t.type = d.ss_dtype
  2781.         AND c.length = d.fixlen
  2782.         AND c.usertype = t.usertype
  2783.  
  2784. go
  2785.  
  2786. if (charindex('6.00', @@version) = 0 and
  2787.     charindex('6.50', @@version) = 0 and
  2788.     charindex('7.00', @@version) = 0)
  2789. begin
  2790.     print ''
  2791.     print ''
  2792.     print 'Warning:'
  2793.     print 'you are installing the stored procedures '
  2794.     print 'on a pre 6.0 SQL Server.'
  2795.     print 'Ignore the following errors.'
  2796. end
  2797. else
  2798.     drop proc sp_special_columns
  2799. go
  2800.  
  2801. /*    Procedure for 6.0 server */
  2802. CREATE PROCEDURE sp_special_columns (
  2803.                  @table_name        varchar(32),
  2804.                  @table_owner        varchar(32) = null,
  2805.                  @table_qualifier    varchar(32) = null,
  2806.                  @col_type            char(1) = 'R',
  2807.                  @scope                char(1) = 'T',
  2808.                  @nullable            char(1) = 'U',
  2809.                  @ODBCVer            int = 2)
  2810. AS
  2811.     DECLARE @indid                int
  2812.     DECLARE @table_id            int
  2813.     DECLARE @full_table_name    char(70)
  2814.     DECLARE @msg                char(70)
  2815.     DECLARE @scopeout            smallint
  2816.  
  2817.     if @col_type not in ('R','V')
  2818.         begin
  2819.             raiserror (15251,-1,-1,'col_type','''R'' or ''V''')
  2820.             return
  2821.         end
  2822.  
  2823.     if @scope = 'C'
  2824.         select @scopeout = 0
  2825.     else if @scope = 'T'
  2826.         select @scopeout = 1
  2827.     else
  2828.         begin
  2829.             raiserror (15251,-1,-1,'scope','''C'' or ''T''')
  2830.             return
  2831.         end
  2832.  
  2833.     if @nullable not in ('U','O')
  2834.         begin
  2835.             raiserror (15251,-1,-1,'nullable','''U'' or ''O''')
  2836.             return
  2837.         end
  2838.  
  2839.     if @table_qualifier is not null
  2840.        begin
  2841.           if db_name() <> @table_qualifier
  2842.               begin    /* If qualifier doesn't match current database */
  2843.                 raiserror (15250, -1,-1,'Table')
  2844.                 return
  2845.               end
  2846.        end
  2847.     if @table_owner is null
  2848.        begin    /* If unqualified table name */
  2849.           SELECT @full_table_name = @table_name
  2850.        end
  2851.     else
  2852.        begin    /* Qualified table name */
  2853.           SELECT @full_table_name = @table_owner + '.' + @table_name
  2854.        end
  2855.     /*    Get Object ID */
  2856.     SELECT @table_id = object_id(@full_table_name)
  2857.  
  2858.     if @col_type = 'V'
  2859.     BEGIN /* if ROWVER, just run that query */
  2860.         SELECT
  2861.             SCOPE = convert(smallint,NULL),
  2862.             COLUMN_NAME = convert(varchar(32),c.name),
  2863.             DATA_TYPE = convert(smallint, -2),
  2864.             TYPE_NAME = t.name,
  2865.             "PRECISION" = convert(int,8),
  2866.             LENGTH = convert(int,8),
  2867.             SCALE = convert(smallint, NULL),
  2868.             PSEUDO_COLUMN = convert(smallint,1)
  2869.         FROM
  2870.             systypes t, syscolumns c, master.dbo.spt_datatype_info d
  2871.         WHERE
  2872.             c.id = @table_id
  2873.             AND c.type = d.ss_dtype
  2874.             AND c.usertype = 80 /*    TIMESTAMP */
  2875.             AND t.usertype = 80 /*    TIMESTAMP */
  2876.         RETURN
  2877.     END
  2878.  
  2879.     /* ROWID, now find the id of the 'best' index for this table */
  2880.  
  2881.     IF @nullable = 'O'    /* Don't include any indexes that contain
  2882.                            nullable columns. */
  2883.  
  2884.         SELECT @indid = MIN(indid)
  2885.             FROM sysindexes i,syscolumns c,syscolumns c2
  2886.             WHERE
  2887.                 i.status&2 = 2        /*    If Unique Index */
  2888.                 AND c.id = i.id
  2889.                 AND c2.id = c.id
  2890.                 AND c2.colid < i.keycnt + (i.status&16)/16
  2891.                 AND i.id = @table_id
  2892.                 AND indid > 0        /*    Eliminate Table Row */
  2893.                 AND c.name = index_col(@table_name,i.indid,c2.colid)
  2894.                 GROUP BY indid HAVING SUM(c.status&8) = 0
  2895.  
  2896.     ELSE    /* Include indexes that are partially nullable. */
  2897.  
  2898.         SELECT @indid = MIN(indid)
  2899.             FROM sysindexes i
  2900.             WHERE
  2901.                 status&2 = 2        /*    If Unique Index */
  2902.                 AND id = @table_id
  2903.                 AND indid > 0        /*    Eliminate Table Row */
  2904.  
  2905.     SELECT
  2906.         SCOPE = @scopeout,
  2907.         COLUMN_NAME = convert(varchar(32),INDEX_COL(@full_table_name,indid,c2.colid)),
  2908.         d.DATA_TYPE,
  2909.         convert(varchar(30),case
  2910.             when t.usertype > 100 or t.usertype in (18,80) then t.name
  2911.             else d.TYPE_NAME
  2912.         end) TYPE_NAME,
  2913.         convert(int,case
  2914.             when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  2915.             else isnull(convert(int,c.prec), 2147483647)
  2916.         end) "PRECISION",
  2917.         convert(int,case
  2918.             when d.ss_dtype IN (106, 108, 55, 63) then    /* decimal/numeric types */
  2919.                 convert(int,c.prec+2)
  2920.             else
  2921.                 isnull(d.length, c.length)
  2922.         end) LENGTH,
  2923.         SCALE = convert(smallint, c.scale),
  2924.         PSEUDO_COLUMN = convert(smallint,1)
  2925.     FROM
  2926.         sysindexes x,
  2927.         syscolumns c,
  2928.         master.dbo.spt_datatype_info d,
  2929.         systypes t,
  2930.         syscolumns c2    /* Self-join to generate list of index columns and */
  2931.                         /* to extract datatype names */
  2932.     WHERE
  2933.         x.id = @table_id
  2934.         AND c.name = INDEX_COL(@full_table_name,@indid,c2.colid)
  2935.         AND c.id = x.id
  2936.         AND c2.id = x.id
  2937.         AND c2.colid < x.keycnt+(x.status&16)/16
  2938.         AND x.indid = @indid
  2939.         AND t.type = d.ss_dtype
  2940.         AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2941.         AND isnull(d.AUTO_INCREMENT,0) = (c.status&128)/128
  2942.         AND c.usertype = t.usertype
  2943. go
  2944.  
  2945. grant execute on sp_special_columns to public
  2946. go
  2947.  
  2948. dump tran master with no_log
  2949. go
  2950.  
  2951. print 'creating sp_sproc_columns'
  2952. go
  2953.  
  2954. /*    Procedure for pre-6.0 server */
  2955. CREATE PROCEDURE sp_sproc_columns (
  2956.                  @procedure_name        varchar(96) = '%',
  2957.                  @procedure_owner        varchar(90) = null,
  2958.                  @procedure_qualifier    varchar(32) = null,
  2959.                  @column_name            varchar(90) = null,
  2960.                  @ODBCVer                int = 2)
  2961. AS
  2962.     DECLARE @group_num int
  2963.     DECLARE @semi_position int
  2964.     DECLARE @full_procedure_name    char(187)
  2965.     DECLARE @procedure_id int
  2966.  
  2967.     if @column_name is null /*    If column name not supplied, match all */
  2968.         select @column_name = '%'
  2969.     if @procedure_qualifier is not null
  2970.     begin
  2971.         if db_name() <> @procedure_qualifier
  2972.         begin
  2973.             if @procedure_qualifier = ''
  2974.             begin
  2975.                 /* in this case, we need to return an empty result set */
  2976.                 /* because the user has requested a database with an empty name */
  2977.                 select @procedure_name = ''
  2978.                 select @procedure_owner = ''
  2979.             end
  2980.             else
  2981.             begin    /* If qualifier doesn't match current database */
  2982.                 raiserror 20001 'Procedure qualifier must be name of current database'
  2983.                 return
  2984.             end
  2985.         end
  2986.     end
  2987.  
  2988.     if @procedure_name is null
  2989.     begin    /*    If procedure name not supplied, match all */
  2990.         select @procedure_name = '%'
  2991.     end
  2992.  
  2993.     /* first we need to extract the procedure group number, if one exists */
  2994.     select @semi_position = charindex(';',@procedure_name)
  2995.     if (@semi_position > 0)
  2996.     begin    /* If group number separator (;) found */
  2997.         select @group_num = convert(int,substring(@procedure_name, @semi_position + 1, 2))
  2998.         select @procedure_name = substring(@procedure_name, 1, @semi_position -1)
  2999.     end
  3000.     else
  3001.     begin    /* No group separator, so default to group number of 1 */
  3002.         select @group_num = 1
  3003.     end
  3004.  
  3005.     if @procedure_owner is null
  3006.     begin    /* If unqualified procedure name */
  3007.         SELECT @full_procedure_name = @procedure_name
  3008.     end
  3009.     else
  3010.     begin    /* Qualified procedure name */
  3011.         SELECT @full_procedure_name = @procedure_owner + '.' + @procedure_name
  3012.     end
  3013.  
  3014.     /*    Get Object ID */
  3015.     SELECT @procedure_id = object_id(@full_procedure_name)
  3016.     if ((charindex('%',@full_procedure_name) = 0) and
  3017.         (charindex('_',@full_procedure_name) = 0)  and
  3018.         @procedure_id <> 0)
  3019.     begin
  3020.         /* this block is for the case where there is no pattern
  3021.             matching required for the procedure name */
  3022.         SELECT
  3023.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  3024.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  3025.             PROCEDURE_NAME = convert(varchar(41),o.name +';'+ ltrim(str(c.number,5))),
  3026.             COLUMN_NAME = convert(varchar(32),c.name),
  3027.             COLUMN_TYPE = convert(smallint, 0),
  3028.             d.DATA_TYPE,
  3029.             TYPE_NAME = t.name,
  3030.             "PRECISION" = isnull(d.data_precision, convert(int,c.length)),
  3031.             LENGTH = isnull(d.length, convert(int,c.length)),
  3032.             SCALE = d.numeric_scale,
  3033.             d.RADIX,
  3034.             d.NULLABLE,
  3035.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  3036.             COLUMN_DEF = convert(varchar(254),null),
  3037.             d.SQL_DATA_TYPE,
  3038.             d.SQL_DATETIME_SUB,
  3039.             CHAR_OCTET_LENGTH = isnull(d.data_precision, convert(int,c.length))+d.charbin,
  3040.             ORDINAL_POSITION = convert(int,c.colid),
  3041.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  3042.             SS_DATA_TYPE = c.type
  3043.         FROM
  3044.             syscolumns c,
  3045.             sysobjects o,
  3046.             master.dbo.spt_datatype_info d,
  3047.             systypes t
  3048.         WHERE
  3049.             o.id = @procedure_id
  3050.             AND c.id = o.id
  3051.             AND t.type = d.ss_dtype
  3052.             AND c.length = isnull(d.fixlen, c.length)
  3053.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  3054.             AND c.usertype = t.usertype
  3055.             AND c.name like @column_name
  3056.             AND c.number = @group_num
  3057.         UNION ALL
  3058.         SELECT           /* return value row*/
  3059.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  3060.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  3061.             PROCEDURE_NAME = convert(varchar(41),o.name +';'+ isnull(ltrim(str(c.number,5)),'1')),
  3062.             COLUMN_NAME = convert(varchar(32),'RETURN_VALUE'),
  3063.             COLUMN_TYPE = convert(smallint, 5),
  3064.             DATA_TYPE = convert(smallint, 4),
  3065.             TYPE_NAME = convert(varchar(30),'int'),
  3066.             "PRECISION" = convert(int,10),
  3067.             LENGTH = convert(int,4),
  3068.             SCALE = convert(smallint,0),
  3069.             RADIX = convert(smallint,10),
  3070.             NULLABLE = convert(smallint,0),
  3071.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  3072.             COLUMN_DEF = convert(varchar(254),NULL),
  3073.             SQL_DATA_TYPE = convert(smallint, 4),
  3074.             SQL_DATETIME_SUB = convert(smallint,null),
  3075.             CHAR_OCTET_LENGTH = convert(int,null),
  3076.             ORDINAL_POSITION = convert(int,0),
  3077.             IS_NULLABLE = convert(varchar(254),'NO'),
  3078.             SS_DATA_TYPE = convert(tinyint,56)
  3079.         FROM
  3080.             syscolumns c,
  3081.             sysobjects o
  3082.         WHERE
  3083.             o.id = @procedure_id
  3084.             AND c.id =* o.id
  3085.             AND c.colid = 1
  3086.             AND 'RETURN_VALUE' like @column_name
  3087.         ORDER BY 1, 2, 3, 18
  3088.     end
  3089.     else
  3090.     begin
  3091.         /* this block is for the case where there IS pattern
  3092.             matching done on the procedure name */
  3093.         if @procedure_owner is null
  3094.             select @procedure_owner = '%'
  3095.         SELECT
  3096.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  3097.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  3098.             PROCEDURE_NAME = convert(varchar(41),o.name +';'+ ltrim(str(c.number,5))),
  3099.             COLUMN_NAME = convert(varchar(32),c.name),
  3100.             COLUMN_TYPE = convert(smallint, 0),
  3101.             d.DATA_TYPE,
  3102.             TYPE_NAME = t.name,
  3103.             "PRECISION" = isnull(d.data_precision, convert(int,c.length)),
  3104.             LENGTH = isnull(d.length, convert(int,c.length)),
  3105.             SCALE = d.numeric_scale,
  3106.             d.RADIX,
  3107.             d.NULLABLE,
  3108.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  3109.             COLUMN_DEF = convert(varchar(254),null),
  3110.             d.SQL_DATA_TYPE,
  3111.             d.SQL_DATETIME_SUB,
  3112.             CHAR_OCTET_LENGTH = isnull(d.data_precision, convert(int,c.length))+d.charbin,
  3113.             ORDINAL_POSITION = convert(int,c.colid),
  3114.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  3115.             SS_DATA_TYPE = c.type
  3116.         FROM
  3117.             syscolumns c,
  3118.             sysobjects o,
  3119.             master.dbo.spt_datatype_info d,
  3120.             systypes t
  3121.         WHERE
  3122.             o.name like @procedure_name
  3123.             AND user_name(o.uid) like @procedure_owner
  3124.             AND o.id = c.id
  3125.             AND t.type = d.ss_dtype
  3126.             AND c.length = isnull(d.fixlen, c.length)
  3127.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  3128.             AND c.usertype = t.usertype
  3129.             AND o.type = 'P'                            /* Just Procedures */
  3130.             AND c.name like @column_name
  3131.         UNION ALL
  3132.         SELECT           /* return value row*/
  3133.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  3134.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  3135.             PROCEDURE_NAME = convert(varchar(41),o.name +';'+ isnull(ltrim(str(c.number,5)),'1')),
  3136.             COLUMN_NAME = convert(varchar(32),'RETURN_VALUE'),
  3137.             COLUMN_TYPE = convert(smallint, 5),
  3138.             DATA_TYPE = convert(smallint, 4),
  3139.             TYPE_NAME = convert(varchar(30),'int'),
  3140.             "PRECISION" = convert(int,10),
  3141.             LENGTH = convert(int,4),
  3142.             SCALE = convert(smallint,0),
  3143.             RADIX = convert(smallint,10),
  3144.             NULLABLE = convert(smallint,0),
  3145.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  3146.             COLUMN_DEF = convert(varchar(254),NULL),
  3147.             SQL_DATA_TYPE = convert(smallint, 4),
  3148.             SQL_DATETIME_SUB = convert(smallint,null),
  3149.             CHAR_OCTET_LENGTH = convert(int,null),
  3150.             ORDINAL_POSITION = convert(int,0),
  3151.             IS_NULLABLE = convert(varchar(254),'NO'),
  3152.             SS_DATA_TYPE = convert(tinyint,56)
  3153.         FROM
  3154.             syscolumns c,
  3155.             sysobjects o
  3156.         WHERE
  3157.             o.name like @procedure_name
  3158.             AND user_name(o.uid) like @procedure_owner
  3159.             AND c.id =* o.id
  3160.             AND c.colid = 1
  3161.             AND o.type = 'P'                        /* Just Procedures */
  3162.             AND 'RETURN_VALUE' like @column_name
  3163.         ORDER BY 1, 2, 3, 18
  3164.     end
  3165. go
  3166.  
  3167. if (charindex('6.00', @@version) = 0 and
  3168.     charindex('6.50', @@version) = 0 and
  3169.     charindex('7.00', @@version) = 0)
  3170. begin
  3171.     print ''
  3172.     print ''
  3173.     print 'Warning:'
  3174.     print 'you are installing the stored procedures '
  3175.     print 'on a pre 6.0 SQL Server.'
  3176.     print 'Ignore the following error.'
  3177. end
  3178. else
  3179.     drop proc sp_sproc_columns
  3180. go
  3181.  
  3182. /*    Procedure for 6.0 server */
  3183. CREATE PROCEDURE sp_sproc_columns (
  3184.                  @procedure_name        varchar(96) = '%',
  3185.                  @procedure_owner        varchar(90) = null,
  3186.                  @procedure_qualifier    varchar(32) = null,
  3187.                  @column_name            varchar(90) = null,
  3188.                  @ODBCVer                int = 2)
  3189. AS
  3190.     DECLARE @group_num int
  3191.     DECLARE @semi_position int
  3192.     DECLARE @full_procedure_name    char(187)
  3193.     DECLARE @procedure_id int
  3194.  
  3195.     if @column_name is null /*    If column name not supplied, match all */
  3196.         select @column_name = '%'
  3197.     if @procedure_qualifier is not null
  3198.     begin
  3199.         if db_name() <> @procedure_qualifier
  3200.         begin
  3201.             if @procedure_qualifier = ''
  3202.             begin
  3203.                 /* in this case, we need to return an empty result set */
  3204.                 /* because the user has requested a database with an empty name */
  3205.                 select @procedure_name = ''
  3206.                 select @procedure_owner = ''
  3207.             end
  3208.             else
  3209.             begin    /* If qualifier doesn't match current database */
  3210.                 raiserror (15250, -1,-1,'Procedure')
  3211.                 return
  3212.             end
  3213.         end
  3214.     end
  3215.  
  3216.     if @procedure_name is null
  3217.     begin    /*    If procedure name not supplied, match all */
  3218.         select @procedure_name = '%'
  3219.     end
  3220.  
  3221.     /* first we need to extract the procedure group number, if one exists */
  3222.     select @semi_position = charindex(';',@procedure_name)
  3223.     if (@semi_position > 0)
  3224.     begin    /* If group number separator (;) found */
  3225.         select @group_num = convert(int,substring(@procedure_name, @semi_position + 1, 2))
  3226.         select @procedure_name = substring(@procedure_name, 1, @semi_position -1)
  3227.     end
  3228.     else
  3229.     begin    /* No group separator, so default to group number of 1 */
  3230.         select @group_num = 1
  3231.     end
  3232.  
  3233.     if @procedure_owner is null
  3234.     begin    /* If unqualified procedure name */
  3235.         SELECT @full_procedure_name = @procedure_name
  3236.     end
  3237.     else
  3238.     begin    /* Qualified procedure name */
  3239.         SELECT @full_procedure_name = @procedure_owner + '.' + @procedure_name
  3240.     end
  3241.  
  3242.     /*    Get Object ID */
  3243.     SELECT @procedure_id = object_id(@full_procedure_name)
  3244.     if ((charindex('%',@full_procedure_name) = 0) and
  3245.         (charindex('_',@full_procedure_name) = 0)  and
  3246.         @procedure_id <> 0)
  3247.     begin
  3248.         /* this block is for the case where there is no pattern
  3249.             matching required for the procedure name */
  3250.         SELECT
  3251.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  3252.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  3253.             PROCEDURE_NAME = convert(varchar(41),o.name +';'+ ltrim(str(c.number,5))),
  3254.             COLUMN_NAME = convert(varchar(32),c.name),
  3255.             COLUMN_TYPE = convert(smallint, 1+((c.status/64)&1)),
  3256.             d.DATA_TYPE,
  3257.             TYPE_NAME = t.name,
  3258.             convert(int,case
  3259.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  3260.                 else isnull(convert(int,c.prec), 2147483647)
  3261.             end) "PRECISION",
  3262.             convert(int,case
  3263.                 when d.ss_dtype IN (106, 108, 55, 63) then    /* decimal/numeric types */
  3264.                     c.prec+2
  3265.                 else
  3266.                     isnull(d.length, c.length)
  3267.             end) LENGTH,
  3268.             SCALE = convert(smallint, c.scale),
  3269.             d.RADIX,
  3270.             d.NULLABLE,
  3271.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  3272.             COLUMN_DEF = convert(varchar(254),NULL),
  3273.             d.SQL_DATA_TYPE,
  3274.             d.SQL_DATETIME_SUB,
  3275.             CHAR_OCTET_LENGTH = isnull(convert(int,c.prec), 2147483647)+d.charbin,
  3276.             ORDINAL_POSITION = convert(int,c.colid),
  3277.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  3278.             SS_DATA_TYPE = c.type
  3279.         FROM
  3280.             syscolumns c,
  3281.             sysobjects o,
  3282.             master.dbo.spt_datatype_info d,
  3283.             systypes t
  3284.         WHERE
  3285.             o.id = @procedure_id
  3286.             AND c.id = o.id
  3287.             AND c.type = d.ss_dtype
  3288.             AND c.length = isnull(d.fixlen, c.length)
  3289.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  3290.             AND isnull(d.AUTO_INCREMENT,0) = 0
  3291.             AND c.usertype *= t.usertype
  3292.             AND c.name like @column_name
  3293.             AND c.number = @group_num
  3294.         UNION ALL
  3295.         SELECT           /* return value row*/
  3296.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  3297.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  3298.             PROCEDURE_NAME = convert(varchar(41),o.name +';'+ isnull(ltrim(str(c.number,5)),'1')),
  3299.             COLUMN_NAME = convert(varchar(32),'RETURN_VALUE'),
  3300.             COLUMN_TYPE = convert(smallint, 5),
  3301.             DATA_TYPE = convert(smallint, 4),
  3302.             TYPE_NAME = convert(varchar(30),'int'),
  3303.             "PRECISION" = convert(int,10),
  3304.             LENGTH = convert(int,4),
  3305.             SCALE = convert(smallint,0),
  3306.             RADIX = convert(smallint,10),
  3307.             NULLABLE = convert(smallint,0),
  3308.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  3309.             COLUMN_DEF = convert(varchar(254),NULL),
  3310.             SQL_DATA_TYPE = convert(smallint, 4),
  3311.             SQL_DATETIME_SUB = convert(smallint,null),
  3312.             CHAR_OCTET_LENGTH = convert(int,null),
  3313.             ORDINAL_POSITION = convert(int,0),
  3314.             IS_NULLABLE = convert(varchar(254),'NO'),
  3315.             SS_DATA_TYPE = convert(tinyint,56)
  3316.         FROM
  3317.             syscolumns c,
  3318.             sysobjects o
  3319.         WHERE
  3320.             o.id = @procedure_id
  3321.             AND c.id =* o.id
  3322.             AND c.colid = 1
  3323.             AND 'RETURN_VALUE' like @column_name
  3324.         ORDER BY 1, 2, 3, 18
  3325.     end
  3326.     else
  3327.     begin
  3328.         /* this block is for the case where there IS pattern
  3329.             matching done on the procedure name */
  3330.         if @procedure_owner is null
  3331.             select @procedure_owner = '%'
  3332.         SELECT
  3333.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  3334.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  3335.             PROCEDURE_NAME = convert(varchar(41),o.name +';'+ ltrim(str(c.number,5))),
  3336.             COLUMN_NAME = convert(varchar(32),c.name),
  3337.             COLUMN_TYPE = convert(smallint, 1+((c.status/64)&1)),
  3338.             d.DATA_TYPE,
  3339.             TYPE_NAME = t.name,
  3340.             convert(int,case
  3341.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  3342.                 else isnull(convert(int,c.prec), 2147483647)
  3343.             end) "PRECISION",
  3344.             convert(int,case
  3345.                 when d.ss_dtype IN (106, 108, 55, 63) then    /* decimal/numeric types */
  3346.                     c.prec+2
  3347.                 else
  3348.                     isnull(d.length, c.length)
  3349.             end) LENGTH,
  3350.             SCALE = convert(smallint, c.scale),
  3351.             d.RADIX,
  3352.             d.NULLABLE,
  3353.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  3354.             COLUMN_DEF = convert(varchar(254),NULL),
  3355.             d.SQL_DATA_TYPE,
  3356.             d.SQL_DATETIME_SUB,
  3357.             CHAR_OCTET_LENGTH = isnull(convert(int,c.prec), 2147483647)+d.charbin,
  3358.             ORDINAL_POSITION = convert(int,c.colid),
  3359.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  3360.             SS_DATA_TYPE = c.type
  3361.         FROM
  3362.             syscolumns c,
  3363.             sysobjects o,
  3364.             master.dbo.spt_datatype_info d,
  3365.             systypes t
  3366.         WHERE
  3367.             o.name like @procedure_name
  3368.             AND user_name(o.uid) like @procedure_owner
  3369.             AND o.id = c.id
  3370.             AND c.type = d.ss_dtype
  3371.             AND c.length = isnull(d.fixlen, c.length)
  3372.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  3373.             AND isnull(d.AUTO_INCREMENT,0) = 0
  3374.             AND c.usertype *= t.usertype
  3375.             AND o.type = 'P'                            /* Just Procedures */
  3376.             AND c.name like @column_name
  3377.         UNION ALL
  3378.         SELECT           /* return value row*/
  3379.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  3380.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  3381.             PROCEDURE_NAME = convert(varchar(41),o.name +';'+ isnull(ltrim(str(c.number,5)),'1')),
  3382.             COLUMN_NAME = convert(varchar(32),'RETURN_VALUE'),
  3383.             COLUMN_TYPE = convert(smallint, 5),
  3384.             DATA_TYPE = convert(smallint, 4),
  3385.             TYPE_NAME = convert(varchar(30),'int'),
  3386.             "PRECISION" = convert(int,10),
  3387.             LENGTH = convert(int,4),
  3388.             SCALE = convert(smallint,0),
  3389.             RADIX = convert(smallint,10),
  3390.             NULLABLE = convert(smallint,0),
  3391.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  3392.             COLUMN_DEF = convert(varchar(254),NULL),
  3393.             SQL_DATA_TYPE = convert(smallint, 4),
  3394.             SQL_DATETIME_SUB = convert(smallint,null),
  3395.             CHAR_OCTET_LENGTH = convert(int,null),
  3396.             ORDINAL_POSITION = convert(int,0),
  3397.             IS_NULLABLE = convert(varchar(254),'NO'),
  3398.             SS_DATA_TYPE = convert(tinyint,56)
  3399.         FROM
  3400.             syscolumns c,
  3401.             sysobjects o
  3402.         WHERE
  3403.             o.name like @procedure_name
  3404.             AND user_name(o.uid) like @procedure_owner
  3405.             AND c.id =* o.id
  3406.             AND c.colid = 1
  3407.             AND o.type = 'P'                        /* Just Procedures */
  3408.             AND 'RETURN_VALUE' like @column_name
  3409.         ORDER BY 1, 2, 3, 18
  3410.     end
  3411. go
  3412.  
  3413. grant execute on sp_sproc_columns to public
  3414. go
  3415.  
  3416. dump tran master with no_log
  3417. go
  3418.  
  3419. print 'creating sp_statistics'
  3420. go
  3421.  
  3422. CREATE PROCEDURE sp_statistics (
  3423.                  @table_name        varchar(32),
  3424.                  @table_owner        varchar(32) = null,
  3425.                  @table_qualifier    varchar(32) = null,
  3426.                  @index_name        varchar(32) = '%',
  3427.                  @is_unique         char(1) = 'N',
  3428.                  @accuracy            char(1) = 'Q')
  3429. AS
  3430.     DECLARE @indid                int
  3431.     DECLARE @lastindid            int
  3432.     DECLARE @table_id            int
  3433.     DECLARE @full_table_name    char(70)
  3434.  
  3435.     if @table_qualifier is not null
  3436.     begin
  3437.         if db_name() <> @table_qualifier
  3438.         begin    /* If qualifier doesn't match current database */
  3439.             raiserror 20001 'Table qualifier must be name of current database'
  3440.             return
  3441.         end
  3442.     end
  3443.  
  3444.     if @accuracy not in ('Q','E')
  3445.         begin
  3446.             raiserror 20002 'Illegal ''accuracy'' specified -- must be ''Q'' or ''E''.'
  3447.             return
  3448.         end
  3449.  
  3450.     if (@@trancount <> 0 and
  3451.         charindex('6.50', @@version) = 0 and
  3452.         charindex('7.00', @@version) = 0)
  3453.     begin    /* If inside a transaction */
  3454.         raiserror 20003 'The procedure ''sp_statistics'' cannot be executed from within a transaction.'
  3455.         return
  3456.     end
  3457.     create table #TmpIndex(
  3458.         TABLE_QUALIFIER varchar(32) NULL,
  3459.         TABLE_OWNER     varchar(32) NULL,
  3460.         TABLE_NAME        varchar(32) NOT NULL,
  3461.         INDEX_QUALIFIER varchar(32) null,
  3462.         INDEX_NAME        varchar(32) null,
  3463.         NON_UNIQUE        smallint null,
  3464.         TYPE            smallint NOT NULL,
  3465.         SEQ_IN_INDEX    smallint null,
  3466.         COLUMN_NAME     varchar(32) null,
  3467.         COLLATION        char(1) null,
  3468.         index_id        int null,
  3469.         CARDINALITY     int null,
  3470.         PAGES            int null,
  3471.         status            smallint NOT NULL)
  3472.     if @table_owner is null
  3473.     begin    /* If unqualified table name */
  3474.         SELECT @full_table_name = @table_name
  3475.     end
  3476.     else
  3477.     begin    /* Qualified table name */
  3478.         SELECT @full_table_name = @table_owner + '.' + @table_name
  3479.     end
  3480.     /*    Get Object ID */
  3481.     SELECT @table_id = object_id(@full_table_name)
  3482.  
  3483.     /*    Start at lowest index id */
  3484.     SELECT @indid = min(indid)
  3485.     FROM sysindexes
  3486.     WHERE id = @table_id
  3487.         AND indid > 0
  3488.         AND indid < 255
  3489.  
  3490.     WHILE @indid <> NULL
  3491.     BEGIN
  3492.         INSERT #TmpIndex    /* Add all columns that are in index */
  3493.             SELECT
  3494.                 DB_NAME(),                                /* TABLE_QUALIFIER */
  3495.                 USER_NAME(o.uid),                        /* TABLE_OWNER       */
  3496.                 o.name,                                 /* TABLE_NAME       */
  3497.                 o.name,                                 /* INDEX_QUALIFIER */
  3498.                 x.name,                                 /* INDEX_NAME       */
  3499.                 0,                                        /* NON_UNIQUE       */
  3500.                 1,                                        /* SQL_INDEX_CLUSTERED */
  3501.                 colid,                                    /* SEQ_IN_INDEX    */
  3502.                 INDEX_COL(@full_table_name,indid,colid),/* COLUMN_NAME       */
  3503.                 'A',                                    /* COLLATION       */
  3504.                 @indid,                                 /* index_id        */
  3505.                 x.rows,                                 /* CARDINALITY       */
  3506.                 x.dpages,                                /* PAGES           */
  3507.                 x.status                                /* status            */
  3508.             FROM sysindexes x, syscolumns c, sysobjects o
  3509.             WHERE
  3510.                 x.id = @table_id
  3511.                 AND x.id = o.id
  3512.                 AND x.id = c.id
  3513.                 AND c.colid < keycnt+(x.status&16)/16
  3514.                 AND x.indid = @indid
  3515.         /*
  3516.         **      Now move @indid to the next index.
  3517.         */
  3518.         SELECT @lastindid = @indid
  3519.         SELECT @indid = NULL
  3520.  
  3521.         SELECT @indid = min(indid)
  3522.         FROM sysindexes
  3523.         WHERE id = @table_id
  3524.             AND indid > @lastindid
  3525.             AND indid < 255
  3526.     END
  3527.  
  3528.     UPDATE #TmpIndex
  3529.         SET NON_UNIQUE = 1
  3530.         WHERE status&2 <> 2 /* If non-unique index */
  3531.     UPDATE #TmpIndex
  3532.         SET
  3533.             TYPE = 3,            /* SQL_INDEX_OTHER */
  3534.             CARDINALITY = NULL,
  3535.             PAGES = NULL
  3536.         WHERE index_id > 1    /* If non-clustered index */
  3537.  
  3538.     /* now add row for table statistics */
  3539.     INSERT #TmpIndex
  3540.         SELECT
  3541.             DB_NAME(),                /* TABLE_QUALIFIER */
  3542.             USER_NAME(o.uid),        /* TABLE_OWNER       */
  3543.             o.name,                 /* TABLE_NAME       */
  3544.             null,                    /* INDEX_QUALIFIER */
  3545.             null,                    /* INDEX_NAME       */
  3546.             null,                    /* NON_UNIQUE       */
  3547.             0,                        /* SQL_TABLE_STAT  */
  3548.             null,                    /* SEQ_IN_INDEX    */
  3549.             null,                    /* COLUMN_NAME       */
  3550.             null,                    /* COLLATION       */
  3551.             0,                        /* index_id        */
  3552.             x.rows,                 /* CARDINALITY       */
  3553.             x.dpages,                /* PAGES           */
  3554.             0                        /* status           */
  3555.         FROM sysindexes x, sysobjects o
  3556.         WHERE o.id = @table_id
  3557.             AND x.id = o.id
  3558.             AND (x.indid = 0 or x.indid = 1)    /*    If there are no indexes */
  3559.                                                 /*    then table stats are in */
  3560.                                                 /*    a row with indid =0        */
  3561.  
  3562.     if @is_unique <> 'Y'    /* If all indexes desired */
  3563.         SELECT
  3564.             TABLE_QUALIFIER,
  3565.             TABLE_OWNER,
  3566.             TABLE_NAME,
  3567.             NON_UNIQUE,
  3568.             INDEX_QUALIFIER,
  3569.             INDEX_NAME,
  3570.             TYPE,
  3571.             SEQ_IN_INDEX,
  3572.             COLUMN_NAME,
  3573.             COLLATION,
  3574.             CARDINALITY,
  3575.             PAGES,
  3576.             FILTER_CONDITION = convert(varchar(128),null)
  3577.         FROM #TmpIndex
  3578.         WHERE
  3579.             INDEX_NAME like @index_name /* If matching name */
  3580.             or INDEX_NAME is null        /* If SQL_TABLE_STAT row */
  3581.         ORDER BY 4, 7, 6, 8
  3582.     else                    /* If only unique indexes desired */
  3583.         SELECT
  3584.             TABLE_QUALIFIER,
  3585.             TABLE_OWNER,
  3586.             TABLE_NAME,
  3587.             NON_UNIQUE,
  3588.             INDEX_QUALIFIER,
  3589.             INDEX_NAME,
  3590.             TYPE,
  3591.             SEQ_IN_INDEX,
  3592.             COLUMN_NAME,
  3593.             COLLATION,
  3594.             CARDINALITY,
  3595.             PAGES,
  3596.             FILTER_CONDITION = convert(varchar(128),null)
  3597.         FROM #TmpIndex
  3598.         WHERE
  3599.             (NON_UNIQUE = 0             /* If unique */
  3600.                 or NON_UNIQUE is NULL)    /* If SQL_TABLE_STAT row */
  3601.             and (INDEX_NAME like @index_name    /* If matching name */
  3602.                 or INDEX_NAME is null)    /* If SQL_TABLE_STAT row */
  3603.         ORDER BY 4, 7, 6, 8
  3604.  
  3605.     DROP TABLE #TmpIndex
  3606. go
  3607.  
  3608. grant execute on sp_statistics to public
  3609. go
  3610.  
  3611. dump tran master with no_log
  3612. go
  3613.  
  3614. print 'creating sp_stored_procedures'
  3615. go
  3616.  
  3617. create procedure sp_stored_procedures(
  3618.                         @sp_name        varchar(96) = null,
  3619.                         @sp_owner        varchar(90) = null,
  3620.                         @sp_qualifier    varchar(32) = null)
  3621. as
  3622.     declare @proc_type smallint
  3623.  
  3624.     if @sp_qualifier is not null
  3625.     begin
  3626.         if db_name() <> @sp_qualifier
  3627.         begin
  3628.             if @sp_qualifier = ''
  3629.             begin
  3630.                 /* in this case, we need to return an empty result set */
  3631.                 /* because the user has requested a database with an empty name */
  3632.                 select @sp_name = ''
  3633.                 select @sp_owner = ''
  3634.             end else
  3635.             begin    /* If qualifier doesn't match current database */
  3636.                 raiserror 20001 'Procedure qualifier must be name of current database'
  3637.                 return
  3638.             end
  3639.         end
  3640.     end
  3641.  
  3642.     if @sp_name is null
  3643.     begin  /*  If procedure name not supplied, match all */
  3644.         select @sp_name = '%'
  3645.     end
  3646.     else begin
  3647.         if (@sp_owner is null) and (charindex('%', @sp_name) = 0)
  3648.         begin
  3649.             if exists (select * from sysobjects
  3650.                 where uid = user_id()
  3651.                     and name = @sp_name
  3652.                     and type = 'P') /* Object type of Procedure */
  3653.             begin
  3654.                 select @sp_owner = user_name()
  3655.             end
  3656.         end
  3657.     end
  3658.     if @sp_owner is null    /*    If procedure owner not supplied, match all */
  3659.         select @sp_owner = '%'
  3660.  
  3661.     select @proc_type=2        /* Return 2 for 4.2 and later servers. */
  3662.  
  3663.     select
  3664.         PROCEDURE_QUALIFIER = convert(varchar(32),db_name()),
  3665.         PROCEDURE_OWNER = convert(varchar(32),user_name(o.uid)),
  3666.         PROCEDURE_NAME = convert(varchar(41),o.name +';'+ ltrim(str(p.number,5))),
  3667.         NUM_INPUT_PARAMS = -1,    /* Constant since value unknown */
  3668.         NUM_OUTPUT_PARAMS = -1, /* Constant since value unknown */
  3669.         NUM_RESULT_SETS = -1,    /* Constant since value unknown */
  3670.         REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  3671.         PROCEDURE_TYPE = @proc_type
  3672.     from
  3673.         sysobjects o,sysprocedures p,sysusers u
  3674.     where
  3675.         o.name like @sp_name
  3676.         and p.sequence = 0
  3677.         and user_name(o.uid) like @sp_owner
  3678.         and o.type = 'P'        /* Object type of Procedure */
  3679.         and p.id = o.id
  3680.         and u.uid = user_id()    /* constrain sysusers uid for use in subquery */
  3681.         and (suser_id() = 1     /* User is the System Administrator */
  3682.             or o.uid = user_id()    /* User created the object */
  3683.             /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  3684.             or ((select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  3685.              from sysprotects p
  3686.              /* outer join to correlate with all rows in sysobjects */
  3687.              where p.id =* o.id
  3688.                  /*  get rows for public,current user,user's group */
  3689.                  and (p.uid = 0 or p.uid = user_id() or p.uid =* u.gid)
  3690.                  /* check for SELECT,EXECUTE privilege */
  3691.                  and (action in (193,224)))&1    /* more magic...normalize GRANT */
  3692.             ) = 1     /* final magic...compare Grants    */
  3693.         )
  3694.     order by 1, 2, 3
  3695. go
  3696. grant execute on sp_stored_procedures to public
  3697. go
  3698.  
  3699. dump tran master with no_log
  3700. go
  3701.  
  3702.  
  3703. print 'creating sp_table_privileges'
  3704. go
  3705.  
  3706. /*    Procedure for pre 6.50 server */
  3707. CREATE PROCEDURE sp_table_privileges (
  3708.             @table_name         varchar(90),
  3709.             @table_owner        varchar(90) = null,
  3710.             @table_qualifier    varchar(32) = null)
  3711. as
  3712.  
  3713.     if @table_qualifier is not null
  3714.     begin
  3715.         if db_name() <> @table_qualifier
  3716.         begin    /* If qualifier doesn't match current database */
  3717.             raiserror 20001 'Table qualifier must be name of current database'
  3718.             return
  3719.         end
  3720.     end
  3721.     if (@@trancount <> 0)
  3722.     begin    /* If inside a transaction */
  3723.         raiserror 20003 'The procedure ''sp_table_privileges'' cannot be executed from within a transaction.'
  3724.         return
  3725.     end
  3726.     if @table_name is null
  3727.         select @table_name = '%'
  3728.     if @table_owner is null /* If no owner supplied, force wildcard */
  3729.         select @table_owner = '%'
  3730.  
  3731.     create table #table_priv1(
  3732.         id                        int NOT NULL,
  3733.         grantor                 smallint NOT NULL,
  3734.         grantee                 smallint NOT NULL,
  3735.         select_privilege        bit,
  3736.         insert_privilege        bit,
  3737.         update_privilege        bit,
  3738.         delete_privilege        bit,
  3739.         references_privilege    bit,
  3740.         select_privilege_grant    bit,
  3741.         insert_privilege_grant    bit,
  3742.         update_privilege_grant    bit,
  3743.         delete_privilege_grant    bit,
  3744.         references_privilege_grant    bit,
  3745.         uid                     smallint NOT NULL,
  3746.         gid                     smallint NOT NULL)
  3747.  
  3748.     insert into #table_priv1
  3749.         select distinct
  3750.             o.id,
  3751.             o.uid,
  3752.             u.uid,
  3753.             0,
  3754.             0,
  3755.             0,
  3756.             0,
  3757.             0,
  3758.             0,
  3759.             0,
  3760.             0,
  3761.             0,
  3762.             0,
  3763.             u.uid,
  3764.             u.gid
  3765.         from sysusers u, sysobjects o, sysprotects p
  3766.         where
  3767.             o.name like @table_name
  3768.             and user_name(o.uid) like @table_owner
  3769.             and u.uid <> u.gid
  3770.             and u.uid *= p.uid and o.id *= p.id
  3771.             and p.protecttype in (204, 205)
  3772.             and sysstat & 0xf in (1,2,3)    /* only valid for system tables,
  3773.                                             ** user tables, and views. */
  3774.  
  3775.     /*
  3776.     ** now add/update row for table owner
  3777.     */
  3778.     if exists (
  3779.         select *
  3780.             from #table_priv1
  3781.             where grantor = grantee)
  3782.     begin
  3783.         update #table_priv1
  3784.         set
  3785.             select_privilege_grant = 1,
  3786.             update_privilege_grant = 1,
  3787.             insert_privilege_grant = 1,
  3788.             delete_privilege_grant = 1,
  3789.             references_privilege_grant = 1,
  3790.             grantor = (select uid from sysusers where suid = 1)
  3791.         where grantor = grantee
  3792.     end
  3793.     else
  3794.     begin
  3795.         insert into #table_priv1
  3796.             select
  3797.                 o.id,
  3798.                 u1.uid,
  3799.                 o.uid,
  3800.                 0,
  3801.                 0,
  3802.                 0,
  3803.                 0,
  3804.                 0,
  3805.                 1,
  3806.                 1,
  3807.                 1,
  3808.                 1,
  3809.                 1,
  3810.                 o.uid,
  3811.                 u.gid
  3812.             from sysobjects o, sysusers u, sysusers u1
  3813.             where o.name like @table_name
  3814.             and user_name(o.uid) like @table_owner
  3815.             and u.uid = o.uid
  3816.             and sysstat & 0xf in (1,2,3)    /* only valid for system tables,
  3817.                                            ** user tables, and views. */
  3818.             and u1.suid = 1        /* grantor is dbo of database */
  3819.  
  3820.     end
  3821.  
  3822.     update #table_priv1
  3823.     set select_privilege = 1
  3824.         from sysprotects p
  3825.     where
  3826.         #table_priv1.id = p.id
  3827.         and (#table_priv1.uid = p.uid
  3828.             or #table_priv1.gid = p.uid
  3829.             or p.uid = 0)
  3830.         and protecttype = 205
  3831.         and action = 193
  3832.         and not exists (
  3833.             select * from sysprotects
  3834.             where
  3835.                 #table_priv1.id = sysprotects.id
  3836.                 and (#table_priv1.uid = uid
  3837.                     or #table_priv1.gid = uid
  3838.                     or uid = 0)
  3839.                 and protecttype = 206
  3840.                 and action = 193)
  3841.  
  3842.     update #table_priv1
  3843.     set insert_privilege = 1
  3844.         from sysprotects p
  3845.     where
  3846.         #table_priv1.id = p.id
  3847.         and (#table_priv1.uid = p.uid
  3848.             or #table_priv1.gid = p.uid
  3849.             or p.uid = 0)
  3850.         and protecttype = 205
  3851.         and action = 195
  3852.         and not exists (
  3853.             select * from sysprotects
  3854.             where
  3855.                 #table_priv1.id = sysprotects.id
  3856.                 and (#table_priv1.uid = uid
  3857.                     or #table_priv1.gid = uid
  3858.                     or uid = 0)
  3859.                 and protecttype = 206
  3860.                 and action = 195)
  3861.  
  3862.     update #table_priv1
  3863.     set delete_privilege = 1
  3864.         from sysprotects p
  3865.     where
  3866.         exists (
  3867.             select * from sysprotects
  3868.             where
  3869.                 #table_priv1.id = sysprotects.id
  3870.                 and (#table_priv1.uid = uid
  3871.                     or #table_priv1.gid = uid
  3872.                     or uid = 0)
  3873.                 and protecttype = 205
  3874.                 and action = 196)
  3875.         and not exists (select * from sysprotects
  3876.             where
  3877.                 #table_priv1.id = sysprotects.id
  3878.                 and (#table_priv1.uid = uid
  3879.                     or #table_priv1.gid = uid
  3880.                     or uid = 0)
  3881.                 and protecttype = 206
  3882.                 and action = 196)
  3883.  
  3884.     update #table_priv1
  3885.     set update_privilege = 1
  3886.         from sysprotects p
  3887.     where
  3888.         #table_priv1.id = p.id
  3889.         and (#table_priv1.uid = p.uid
  3890.             or #table_priv1.gid = p.uid
  3891.             or p.uid = 0)
  3892.         and protecttype = 205
  3893.         and action = 197
  3894.         and not exists (
  3895.             select * from sysprotects
  3896.             where
  3897.                 #table_priv1.id = sysprotects.id
  3898.                 and (#table_priv1.uid = uid
  3899.                     or #table_priv1.gid = uid
  3900.                     or uid = 0)
  3901.                 and protecttype = 206
  3902.                 and action = 197)
  3903.  
  3904.     update #table_priv1
  3905.     set references_privilege = 1
  3906.         from sysprotects p
  3907.     where
  3908.         #table_priv1.id = p.id
  3909.         and (#table_priv1.uid = p.uid
  3910.             or #table_priv1.gid = p.uid
  3911.             or p.uid = 0)
  3912.         and protecttype = 205
  3913.         and action = 26
  3914.         and not exists (
  3915.             select * from sysprotects
  3916.             where
  3917.                 #table_priv1.id = sysprotects.id
  3918.                 and (#table_priv1.uid = uid
  3919.                     or #table_priv1.gid = uid
  3920.                     or uid = 0)
  3921.                 and protecttype = 206
  3922.                 and action = 26)
  3923.  
  3924.     create table #table_priv2(
  3925.         id                int NOT NULL,
  3926.         grantor         smallint NOT NULL,
  3927.         grantee         smallint NOT NULL,
  3928.         PRIVILEGE        varchar(32) NOT NULL,
  3929.         IS_GRANTABLE    varchar(3) NULL)
  3930.  
  3931.     insert into #table_priv2
  3932.         select
  3933.             id,
  3934.             grantor,
  3935.             grantee,
  3936.             'SELECT',
  3937.             'NO'
  3938.         from #table_priv1
  3939.         where select_privilege = 1 and select_privilege_grant = 0
  3940.  
  3941.     insert into #table_priv2
  3942.         select
  3943.             id,
  3944.             grantor,
  3945.             grantee,
  3946.             'INSERT',
  3947.             'NO'
  3948.         from #table_priv1
  3949.         where insert_privilege = 1 and insert_privilege_grant = 0
  3950.  
  3951.     insert into #table_priv2
  3952.         select
  3953.             id,
  3954.             grantor,
  3955.             grantee,
  3956.             'DELETE',
  3957.             'NO'
  3958.         from #table_priv1
  3959.         where delete_privilege = 1 and delete_privilege_grant = 0
  3960.  
  3961.     insert into #table_priv2
  3962.         select
  3963.             id,
  3964.             grantor,
  3965.             grantee,
  3966.             'UPDATE',
  3967.             'NO'
  3968.         from #table_priv1
  3969.         where update_privilege = 1 and update_privilege_grant = 0
  3970.  
  3971.     insert into #table_priv2
  3972.         select
  3973.             id,
  3974.             grantor,
  3975.             grantee,
  3976.             'REFERENCES',
  3977.             'NO'
  3978.         from #table_priv1
  3979.         where references_privilege = 1 and references_privilege_grant = 0
  3980.  
  3981.     insert into #table_priv2
  3982.         select
  3983.             id,
  3984.             grantor,
  3985.             grantee,
  3986.             'SELECT',
  3987.             'YES'
  3988.         from #table_priv1
  3989.         where select_privilege_grant = 1
  3990.  
  3991.     insert into #table_priv2
  3992.         select
  3993.             id,
  3994.             grantor,
  3995.             grantee,
  3996.             'INSERT',
  3997.             'YES'
  3998.         from #table_priv1
  3999.         where insert_privilege_grant = 1
  4000.  
  4001.     insert into #table_priv2
  4002.         select
  4003.             id,
  4004.             grantor,
  4005.             grantee,
  4006.             'DELETE',
  4007.             'YES'
  4008.         from #table_priv1
  4009.         where delete_privilege_grant = 1
  4010.  
  4011.     insert into #table_priv2
  4012.         select
  4013.             id,
  4014.             grantor,
  4015.             grantee,
  4016.             'UPDATE',
  4017.             'YES'
  4018.         from #table_priv1
  4019.         where update_privilege_grant = 1
  4020.  
  4021.     insert into #table_priv2
  4022.         select
  4023.             id,
  4024.             grantor,
  4025.             grantee,
  4026.             'REFERENCES',
  4027.             'YES'
  4028.         from #table_priv1
  4029.         where references_privilege_grant = 1
  4030.  
  4031.  
  4032.     select
  4033.         convert(varchar(32),db_name()) TABLE_QUALIFIER,
  4034.         convert(varchar(32),user_name(o.uid)) TABLE_OWNER,
  4035.         convert(varchar(32),o.name) TABLE_NAME,
  4036.         convert(varchar(32),user_name(grantor)) GRANTOR,
  4037.         convert(varchar(32),user_name(grantee)) GRANTEE,
  4038.         PRIVILEGE,
  4039.         IS_GRANTABLE
  4040.     from #table_priv2 t, sysobjects o where o.id = t.id
  4041.     order by 2,3,6
  4042. go
  4043.  
  4044. if (charindex('6.50', @@version) = 0 and
  4045.     charindex('7.00', @@version) = 0)
  4046. begin
  4047.     print ''
  4048.     print ''
  4049.     print 'Warning:'
  4050.     print 'you are installing the stored procedures '
  4051.     print 'on a pre 6.50 SQL Server.'
  4052.     print 'Ignore the following errors.'
  4053. end
  4054. else
  4055.     drop proc sp_table_privileges
  4056. go
  4057.  
  4058. /*    Procedure for 6.50 server */
  4059. CREATE PROCEDURE sp_table_privileges (
  4060.             @table_name         varchar(90),
  4061.             @table_owner        varchar(90) = null,
  4062.             @table_qualifier    varchar(32) = null)
  4063. as
  4064.  
  4065.     if @table_qualifier is not null
  4066.     begin
  4067.         if db_name() <> @table_qualifier
  4068.         begin    /* If qualifier doesn't match current database */
  4069.             raiserror 20001 'Table qualifier must be name of current database'
  4070.             return
  4071.         end
  4072.     end
  4073.     if @table_name is null
  4074.         select @table_name = '%'
  4075.     if @table_owner is null /* If no owner supplied, force wildcard */
  4076.         select @table_owner = '%'
  4077.  
  4078.     select
  4079.         convert(varchar(32),db_name()) TABLE_QUALIFIER,
  4080.         convert(varchar(32),user_name(o.uid)) TABLE_OWNER,
  4081.         convert(varchar(32),object_name(o.id)) TABLE_NAME,
  4082.         convert(varchar(32),user_name(p.grantor)) GRANTOR,
  4083.         convert(varchar(32),user_name(u.uid)) GRANTEE,
  4084.         convert(varchar(32),case p.action
  4085.              when 193 then 'SELECT'
  4086.              when 195 then 'INSERT'
  4087.              when 196 then 'DELETE'
  4088.              when 197 then 'UPDATE'
  4089.              else 'REFERENCES'
  4090.         end) PRIVILEGE,
  4091.         convert(varchar(3),case when p.protecttype = 205 then 'NO'
  4092.             else 'YES'
  4093.         end) IS_GRANTABLE
  4094.     from sysprotects p, sysobjects o, sysusers u
  4095.     where
  4096.         p.id = o.id
  4097.         and object_name(o.id) like @table_name
  4098.         and user_name(o.uid) like @table_owner
  4099.             /* expand groups */
  4100.         and ((p.uid = u.uid and u.uid <> u.gid) or
  4101.              (p.uid = u.gid and u.uid <> u.gid))
  4102.         and p.protecttype <> 206    /* only grant rows */
  4103.         and p.action in (26,193,195,196,197)
  4104.         and o.uid <> u.uid            /* no rows for owner */
  4105.         and not exists (            /* exclude revoke'd privileges */
  4106.             select *
  4107.             from sysprotects p1
  4108.             where
  4109.                 p1.protecttype = 206
  4110.                 and p1.action = p.action
  4111.                 and p1.id = p.id
  4112.                 and p1.uid = u.uid)
  4113.     union all
  4114.     select    /*    Add rows for table owner */
  4115.         convert(varchar(32),db_name()) TABLE_QUALIFIER,
  4116.         convert(varchar(32),user_name(o.uid)) TABLE_OWNER,
  4117.         convert(varchar(32),object_name(o.id)) TABLE_NAME,
  4118.         convert(varchar(32),user_name(u.uid)) GRANTOR,
  4119.         convert(varchar(32),user_name(o.uid)) GRANTEE,
  4120.         convert(varchar(32),case v.number
  4121.             when 193 then 'SELECT'
  4122.             when 195 then 'INSERT'
  4123.             when 196 then 'DELETE'
  4124.             when 197 then 'UPDATE'
  4125.             else 'REFERENCES'
  4126.         end) PRIVILEGE,
  4127.         convert(varchar(3),'YES') IS_GRANTABLE
  4128.     from sysobjects o, spt_values v, sysusers u
  4129.     where
  4130.         object_name(o.id) like @table_name
  4131.         and user_name(o.uid) like @table_owner
  4132.         and u.suid = 1        /* grantor is dbo of database */
  4133.         and v.type = 'P'    /* cross product to get all exposed privileges */
  4134.         and v.number in (26,193,195,196,197)
  4135.         and not exists (    /* exclude revoke'd privileges */
  4136.             select *
  4137.             from sysprotects p1
  4138.             where
  4139.                 p1.protecttype = 206
  4140.                 and p1.action = v.number
  4141.                 and p1.id = o.id
  4142.                 and p1.uid = o.uid)
  4143.     order by 2,3,6
  4144. go
  4145.  
  4146. grant execute on sp_table_privileges to public
  4147. go
  4148.  
  4149. dump tran master with no_log
  4150. go
  4151.  
  4152. print 'creating sp_tables'
  4153. go
  4154.  
  4155. create procedure sp_tables(
  4156.                @table_name        varchar(90)    = null,
  4157.                @table_owner     varchar(90)    = null,
  4158.                @table_qualifier varchar(90)    = null,
  4159.                @table_type        varchar(100) = null)
  4160. as
  4161.     declare @type1 varchar(3)
  4162.     declare @tableindex int
  4163.  
  4164.  
  4165.     /* Special feature #1:    enumerate databases when owner and name
  4166.          are blank but qualifier is explicitly '%'.  */
  4167.     if @table_qualifier = '%' and
  4168.         @table_owner = '' and
  4169.         @table_name = ''
  4170.     begin    /* If enumerating databases */
  4171.         select
  4172.             TABLE_QUALIFIER = convert(varchar(32),d.name),
  4173.             TABLE_OWNER = convert(varchar(32),null),
  4174.             TABLE_NAME = convert(varchar(32),null),
  4175.             TABLE_TYPE = convert(varchar(32),null),
  4176.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  4177.         from master..sysdatabases d
  4178.         where d.name <> 'model'    /* eliminate MODEL database */
  4179.         order by 1
  4180.     end
  4181.  
  4182.     /* Special feature #2:    enumerate owners when qualifier and name
  4183.          are blank but owner is explicitly '%'.  */
  4184.     else if @table_qualifier = '' and
  4185.         @table_owner = '%' and
  4186.         @table_name = ''
  4187.     begin    /* If enumerating owners */
  4188.         select distinct
  4189.             TABLE_QUALIFIER = convert(varchar(32),null),
  4190.             TABLE_OWNER = convert(varchar(32),user_name(uid)),
  4191.             TABLE_NAME = convert(varchar(32),null),
  4192.             TABLE_TYPE = convert(varchar(32),null),
  4193.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  4194.         from sysobjects
  4195.         order by 2
  4196.     end
  4197.  
  4198.     /* Special feature #3:    enumerate table types when qualifier, owner and
  4199.          name are blank but table type is explicitly '%'.    */
  4200.     else if @table_qualifier = '' and
  4201.         @table_owner = '' and
  4202.         @table_name = '' and
  4203.         @table_type = '%'
  4204.     begin    /* If enumerating table types */
  4205.         select
  4206.             TABLE_QUALIFIER = convert(varchar(32),null),
  4207.             TABLE_OWNER = convert(varchar(32),null),
  4208.             TABLE_NAME = convert(varchar(32),null),
  4209.             TABLE_TYPE = convert(varchar(32),rtrim(substring('SYSTEM TABLETABLE       VIEW',(colid-1)*12+1,12))),
  4210.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  4211.         from sysobjects o, syscolumns c
  4212.         where o.id=c.id and o.name='sysusers' and colid<=3
  4213.     end
  4214.  
  4215.     else
  4216.     begin /* end of special features -- do normal processing */
  4217.         if @table_qualifier is not null
  4218.         begin
  4219.             if db_name() <> @table_qualifier
  4220.             begin
  4221.                 if @table_qualifier = ''
  4222.                 begin  /* If empty qualifier supplied */
  4223.                     /* Force an empty result set */
  4224.                     select @table_name = ''
  4225.                     select @table_owner = ''
  4226.                 end
  4227.                 else
  4228.                 begin    /* If qualifier doesn't match current database */
  4229.                     raiserror 20001 'Table qualifier must be name of current database'
  4230.                     return
  4231.                 end
  4232.             end
  4233.         end
  4234.         if @table_type is null
  4235.         begin    /* Select all ODBC supported table types */
  4236.             select @type1 = 'SUV'
  4237.         end
  4238.         else
  4239.         begin
  4240.             /*    TableType is case sensitive if CS server */
  4241.             select @type1 = null
  4242.             if (charindex('''SYSTEM TABLE''',@table_type) <> 0)
  4243.                 select @type1 = @type1 + 'S'    /* Add System Tables */
  4244.             if (charindex('''TABLE''',@table_type) <> 0)
  4245.                 select @type1 = @type1 + 'U'    /* Add User Tables */
  4246.             if (charindex('''VIEW''',@table_type) <> 0)
  4247.                 select @type1 = @type1 + 'V'    /* Add Views */
  4248.         end
  4249.         if @table_name is null
  4250.         begin    /*    If table name not supplied, match all */
  4251.             select @table_name = '%'
  4252.         end
  4253.         else
  4254.         begin
  4255.             if (@table_owner is null) and (charindex('%', @table_name) = 0)
  4256.             begin    /* If owner not specified and table is specified */
  4257.                 if exists (select * from sysobjects
  4258.                     where uid = user_id()
  4259.                     and name = @table_name
  4260.                     and (type = 'U' or type = 'V' or type = 'S'))
  4261.                 begin    /* Override supplied owner w/owner of table */
  4262.                     select @table_owner = user_name()
  4263.                 end
  4264.             end
  4265.         end
  4266.         if @table_owner is null /* If no owner supplied, force wildcard */
  4267.             select @table_owner = '%'
  4268.         select
  4269.             TABLE_QUALIFIER = convert(varchar(32),db_name()),
  4270.             TABLE_OWNER = convert(varchar(32),user_name(o.uid)),
  4271.             TABLE_NAME = convert(varchar(32),o.name),    /* make nullable */
  4272.             TABLE_TYPE = convert(varchar(32),rtrim(
  4273.                 substring('SYSTEM TABLE            TABLE       VIEW       ',
  4274.                     (ascii(o.type)-83)*12+1,12))),    /* 'S'=0,'U'=2,'V'=3 */
  4275.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  4276.         from sysusers u, sysobjects o
  4277.         where
  4278.             o.name like @table_name
  4279.             and user_name(o.uid) like @table_owner
  4280.             and charindex(substring(o.type,1,1),@type1)! = 0 /* Only desired types */
  4281.             and u.uid = user_id() /* constrain sysusers uid for use in subquery */
  4282.             and (
  4283.                 suser_id() = 1     /* User is the System Administrator */
  4284.                 or o.uid = user_id()     /* User created the object */
  4285.                 /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  4286.                 or ((select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  4287.                     from sysprotects p
  4288.                     /* outer join to correlate with all rows in sysobjects */
  4289.                     where p.id =* o.id
  4290.                         /* get rows for public,current user,user's group */
  4291.                         and (p.uid = 0 or p.uid = user_id() or p.uid =* u.gid)
  4292.                         /* check for SELECT,EXECUTE privilege */
  4293.                         and (action in (193,224)))&1     /* more magic...normalize GRANT */
  4294.                     ) = 1    /* final magic...compare Grants      */
  4295.             )
  4296.         order by 4, 1, 2, 3
  4297.     end
  4298. go
  4299.  
  4300. grant execute on sp_tables to public
  4301. go
  4302.  
  4303. dump tran master with no_log
  4304. go
  4305.  
  4306. if (charindex('6.50', @@version) = 0 and
  4307.     charindex('7.00', @@version) = 0)
  4308. begin
  4309.     print ''
  4310.     print ''
  4311.     print 'Warning:'
  4312.     print 'you are installing the stored procedures '
  4313.     print 'on a pre 6.50 SQL Server.'
  4314.     print 'Ignore the following errors.'
  4315. end
  4316.  
  4317. print 'creating sp_ddopen'
  4318. go
  4319.  
  4320. create procedure sp_ddopen(
  4321.                @handle            int output,
  4322.                @procname        sysname(30),
  4323.                @scrollopt        int output,
  4324.                @ccopt            int output,
  4325.                @rows            int output,
  4326.                @p1                varchar(255) = null,
  4327.                @p2                varchar(255) = null,
  4328.                @p3                varchar(255) = null,
  4329.                @p4                varchar(255) = null,
  4330.                @p5                varchar(255) = null,
  4331.                @p6                varchar(255) = null,
  4332.                @p7                int = null,
  4333.                @ODBCVer         int = 2)
  4334. as
  4335.     declare @ret int
  4336.  
  4337.     if @procname = 'sp_column_privileges'
  4338.     begin
  4339.         create table #spcolpriv (
  4340.             TABLE_QUALIFIER varchar(32) null,
  4341.             TABLE_OWNER varchar(32) null,
  4342.             TABLE_NAME varchar(32) not null,
  4343.             COLUMN_NAME varchar(32) not null,
  4344.             GRANTOR varchar(32) null,
  4345.             GRANTEE varchar(32) not null,
  4346.             PRIVILEGE varchar(32) not null,
  4347.             IS_GRANTABLE varchar(3) null
  4348.             )
  4349.         insert into #spcolpriv exec sp_column_privileges @p1,@p2,@p3,@p4
  4350.         exec @ret = sp_cursoropen @handle output,
  4351.             'select * from #spcolpriv',
  4352.             @scrollopt output, @ccopt output, @rows output
  4353.         drop table #spcolpriv
  4354.     end
  4355.     else if @procname = 'sp_columns'
  4356.     begin
  4357.         create table #spcolumns (
  4358.             TABLE_QUALIFIER varchar(32) null,
  4359.             TABLE_OWNER varchar(32) null,
  4360.             TABLE_NAME varchar(32) not null,
  4361.             COLUMN_NAME varchar(32) not null,
  4362.             DATA_TYPE smallint not null,
  4363.             TYPE_NAME varchar(30) not null,
  4364.             "PRECISION" int null,
  4365.             LENGTH int null,
  4366.             SCALE smallint null,
  4367.             RADIX smallint null,
  4368.             NULLABLE smallint not null,
  4369.             REMARKS varchar(254) null,
  4370.             COLUMN_DEF varchar(254) null,
  4371.             SQL_DATA_TYPE smallint not null,
  4372.             SQL_DATETIME_SUB smallint null,
  4373.             CHAR_OCTET_LENGTH int null,
  4374.             ORDINAL_POSITION int not null,
  4375.             IS_NULLABLE varchar(254) null,
  4376.             SS_DATA_TYPE tinyint null
  4377.             )
  4378.         insert into #spcolumns exec sp_columns @p1,@p2,@p3,@p4,@ODBCVer
  4379.         exec @ret = sp_cursoropen @handle output,
  4380.             'select * from #spcolumns',
  4381.             @scrollopt output, @ccopt output, @rows output
  4382.         drop table #spcolumns
  4383.     end
  4384.     else if @procname = 'sp_datatype_info'
  4385.     begin
  4386.         create table #spdatatypeinfo (
  4387.             TYPE_NAME            varchar(32)  not null,
  4388.             DATA_TYPE            smallint not null,
  4389.             "PRECISION"            int null,
  4390.             LITERAL_PREFIX        varchar(32)    null,
  4391.             LITERAL_SUFFIX        varchar(32)    null,
  4392.             CREATE_PARAMS        varchar(32)    null,
  4393.             NULLABLE            smallint   not null,
  4394.             CASE_SENSITIVE        smallint   not null,
  4395.             SEARCHABLE            smallint   not null,
  4396.             UNSIGNED_ATTRIBUTE    smallint   null,
  4397.             MONEY    smallint    not null,
  4398.             AUTO_INCREMENT        smallint    null,
  4399.             LOCAL_TYPE_NAME     varchar(128) null,
  4400.             MINIMUM_SCALE        smallint     null,
  4401.             MAXIMUM_SCALE        smallint   null,
  4402.             SQL_DATA_TYPE        smallint      not null,
  4403.             SQL_DATETIME_SUB    smallint   null,
  4404.             NUM_PREC_RADIX        int     null,
  4405.             INTERVAL_PRECISION    smallint    NULL,
  4406.             USERTYPE            tinyint not null)
  4407.         insert into #spdatatypeinfo exec sp_datatype_info @p7,@ODBCVer
  4408.         exec @ret = sp_cursoropen @handle output,
  4409.             'select * from #spdatatypeinfo',
  4410.             @scrollopt output, @ccopt output, @rows output
  4411.         drop table #spdatatypeinfo
  4412.     end
  4413.     else if @procname = 'sp_fkeys'
  4414.     begin
  4415.         create table #spfkeys (
  4416.             PKTABLE_QUALIFIER varchar(32)     null,
  4417.             PKTABLE_OWNER varchar(32)    null,
  4418.             PKTABLE_NAME varchar(32)  not null,
  4419.             PKCOLUMN_NAME varchar(32)  not null,
  4420.             FKTABLE_QUALIFIER varchar(32)    null,
  4421.             FKTABLE_OWNER varchar(32)    null,
  4422.             FKTABLE_NAME varchar(32)  not null,
  4423.             FKCOLUMN_NAME varchar(32)  not null,
  4424.             KEY_SEQ smallint not null,
  4425.             UPDATE_RULE smallint null,
  4426.             DELETE_RULE smallint null,
  4427.             FK_NAME varchar(32) null,
  4428.             PK_NAME varchar(32) null
  4429.             )
  4430.         insert into #spfkeys exec sp_fkeys @p1,@p2,@p3,@p4,@p5,@p6
  4431.         exec @ret = sp_cursoropen @handle output,
  4432.             'select * from #spfkeys',
  4433.             @scrollopt output, @ccopt output, @rows output
  4434.         drop table #spfkeys
  4435.     end
  4436.     else if @procname = 'sp_pkeys'
  4437.     begin
  4438.         create table #sppkeys (
  4439.             TABLE_QUALIFIER varchar(32)   null,
  4440.             TABLE_OWNER varchar(32)   null,
  4441.             TABLE_NAME varchar(32)    not null,
  4442.             COLUMN_NAME varchar(32)  not null,
  4443.             KEY_SEQ smallint not null,
  4444.             PK_NAME varchar(32) null
  4445.             )
  4446.         insert into #sppkeys exec sp_pkeys @p1,@p2,@p3
  4447.         exec @ret = sp_cursoropen @handle output,
  4448.             'select * from #sppkeys',
  4449.             @scrollopt output, @ccopt output, @rows output
  4450.         drop table #sppkeys
  4451.     end
  4452.     else if @procname = 'sp_special_columns'
  4453.     begin
  4454.         create table #spspeccol (
  4455.             SCOPE smallint null,
  4456.             COLUMN_NAME varchar(32) not null,
  4457.             DATA_TYPE smallint not null,
  4458.             TYPE_NAME varchar(30) not null,
  4459.             "PRECISION" int null,
  4460.             LENGTH int null,
  4461.             SCALE smallint null,
  4462.             PSEUDO_COLUMN smallint null
  4463.             )
  4464.         insert into #spspeccol exec sp_special_columns @p1,@p2,@p3,@p4,@p5,@p6,@ODBCVer
  4465.         exec @ret = sp_cursoropen @handle output,
  4466.             'select * from #spspeccol',
  4467.             @scrollopt output, @ccopt output, @rows output
  4468.         drop table #spspeccol
  4469.     end
  4470.     else if @procname = 'sp_sproc_columns'
  4471.     begin
  4472.         create table #spproccol (
  4473.             PROCEDURE_QUALIFIER varchar(32)  null,
  4474.             PROCEDURE_OWNER varchar(32)  null,
  4475.             PROCEDURE_NAME varchar(32) not null,
  4476.             COLUMN_NAME varchar(32) not null,
  4477.             COLUMN_TYPE smallint not null,
  4478.             DATA_TYPE smallint not null,
  4479.             TYPE_NAME varchar(30) not null,
  4480.             "PRECISION" int null,
  4481.             LENGTH int null,
  4482.             SCALE smallint null,
  4483.             RADIX smallint null,
  4484.             NULLABLE smallint not null,
  4485.             REMARKS varchar(254) null,
  4486.             COLUMN_DEF varchar(254) null,
  4487.             SQL_DATA_TYPE smallint not null,
  4488.             SQL_DATETIME_SUB smallint null,
  4489.             CHAR_OCTET_LENGTH int null,
  4490.             ORDINAL_POSITION int not null,
  4491.             IS_NULLABLE varchar(254) null,
  4492.             SS_DATA_TYPE tinyint null
  4493.             )
  4494.         insert into #spproccol exec sp_sproc_columns @p1,@p2,@p3,@p4,@ODBCVer
  4495.         exec @ret = sp_cursoropen @handle output,
  4496.             'select * from #spproccol',
  4497.             @scrollopt output, @ccopt output, @rows output
  4498.         drop table #spproccol
  4499.     end
  4500.     else if @procname = 'sp_statistics'
  4501.     begin
  4502.         create table #spstatistics (
  4503.             TABLE_QUALIFIER varchar(32)   null,
  4504.             TABLE_OWNER varchar(32)   null,
  4505.             TABLE_NAME varchar(32)    not null,
  4506.             NON_UNIQUE smallint null,
  4507.             INDEX_QUALIFIER varchar(32) null,
  4508.             INDEX_NAME varchar(32) null,
  4509.             TYPE smallint not null,
  4510.             SEQ_IN_INDEX smallint null,
  4511.             COLUMN_NAME varchar(32) null,
  4512.             COLLATION char(1) null,
  4513.             CARDINALITY int null,
  4514.             PAGES int null,
  4515.             FILTER_CONDITION varchar(128) null
  4516.             )
  4517.         insert into #spstatistics exec sp_statistics @p1,@p2,@p3,@p4,@p5,@p6
  4518.         exec @ret = sp_cursoropen @handle output,
  4519.             'select * from #spstatistics',
  4520.             @scrollopt output, @ccopt output, @rows output
  4521.         drop table #spstatistics
  4522.     end
  4523.     else if @procname = 'sp_stored_procedures'
  4524.     begin
  4525.         create table #spprocedures (
  4526.             PROCEDURE_QUALIFIER varchar(32)  null,
  4527.             PROCEDURE_OWNER varchar(32)  null,
  4528.             PROCEDURE_NAME varchar(32) not null,
  4529.             NUM_INPUT_PARAMS int null,
  4530.             NUM_OUTPUT_PARAMS int null,
  4531.             NUM_RESULT_SETS int null,
  4532.             REMARKS varchar(254) null,
  4533.             PROCEDURE_TYPE smallint null
  4534.             )
  4535.         insert into #spprocedures exec sp_stored_procedures @p1,@p2,@p3
  4536.         exec @ret = sp_cursoropen @handle output,
  4537.             'select * from #spprocedures',
  4538.             @scrollopt output, @ccopt output, @rows output
  4539.         drop table #spprocedures
  4540.     end
  4541.     else if @procname = 'sp_table_privileges'
  4542.     begin
  4543.         create table #sptabpriv (
  4544.             TABLE_QUALIFIER varchar(32) null,
  4545.             TABLE_OWNER varchar(32) null,
  4546.             TABLE_NAME varchar(32) not null,
  4547.             GRANTOR varchar(32) null,
  4548.             GRANTEE varchar(32) not null,
  4549.             PRIVILEGE varchar(32) not null,
  4550.             IS_GRANTABLE varchar(3) null
  4551.             )
  4552.         insert into #sptabpriv exec sp_table_privileges @p1,@p2,@p3
  4553.         exec @ret = sp_cursoropen @handle output,
  4554.             'select * from #sptabpriv',
  4555.             @scrollopt output, @ccopt output, @rows output
  4556.         drop table #sptabpriv
  4557.     end
  4558.     else if @procname = 'sp_tables'
  4559.     begin
  4560.         create table #sptables (
  4561.             TABLE_QUALIFIER varchar(32) null,
  4562.             TABLE_OWNER varchar(32) null,
  4563.             TABLE_NAME varchar(32) null,
  4564.             TABLE_TYPE    varchar(32) null,
  4565.             REMARKS varchar(254) null)
  4566.         insert into #sptables exec sp_tables @p1,@p2,@p3,@p4
  4567.         exec @ret = sp_cursoropen @handle output,
  4568.             'select * from #sptables',
  4569.             @scrollopt output, @ccopt output, @rows output
  4570.         drop table #sptables
  4571.     end
  4572.     select @ret = isnull(@ret,0)
  4573.     return isnull(@ret,0)
  4574. go
  4575.  
  4576. grant execute on sp_ddopen to public
  4577. go
  4578.  
  4579. dump tran master with no_log
  4580. go
  4581.  
  4582. if exists (select * from sysobjects where name = 'sp_configure'
  4583.             and sysstat & 0xf = 4)
  4584.     begin
  4585.         exec sp_configure 'allow updates',0
  4586.         reconfigure with override
  4587.     end
  4588. go
  4589.  
  4590. exec sp_MS_upd_sysobj_category 2 --set category | 2 based on crdate.
  4591.  
  4592. go
  4593.  
  4594. if exists (select * from sysobjects where name = 'sp_check_objects'
  4595.             and sysstat & 0xf = 4)
  4596.     begin
  4597.         /* Only supported on 6.0 servers */
  4598.         print ''
  4599.         print 'Checking objects created by instcat.sql.'
  4600.  
  4601.         exec sp_check_objects 'catalog'
  4602.     end
  4603. go
  4604.  
  4605. print ''
  4606. print 'instcat.sql completed successfully.'
  4607. go
  4608.  
  4609. set quoted_identifier off
  4610. go
  4611.  
  4612. dump tran master with no_log
  4613. go
  4614. checkpoint
  4615. go
  4616. /**/
  4617.