home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / os / system / instcat.sql < prev    next >
Text File  |  1998-06-06  |  559KB  |  18,052 lines

  1.  
  2. /*
  3. **    INSTCAT.SQL
  4. **    Installs catalog stored procedures on the Microsoft SQL Server.
  5. **    Copyright 1992-1998, 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. j is the major version number ('7' now), nn is the minor version number
  12. ('00' now), and bbb is the build number.
  13. insert into spt_server_info
  14.     values (500, 'SYS_SPROC_VERSION', '7.00.bbb')
  15. */
  16.  
  17. /****************************************************************************/
  18. /* This portion sets up the ability to perform all the functions in this    */
  19. /* script                                                                    */
  20. /****************************************************************************/
  21. go
  22. use master
  23. go
  24. dump tran master with no_log
  25. go
  26.  
  27. set quoted_identifier on
  28. go
  29.  
  30. if (exists (select * from sysobjects
  31.         where name = 'sp_configure' and type = 'P '))
  32. begin
  33.     execute sp_configure 'update',1
  34. end
  35. reconfigure with override
  36. go
  37.  
  38. exec sp_MS_upd_sysobj_category 1 /*Capture datetime for use below.*/
  39.  
  40. go
  41.  
  42. /*
  43. ** If old versions of tables exist, drop them.
  44. */
  45. if (exists (select * from sysobjects
  46.         where name = 'MSdatatype_info' and type = 'U '))
  47.     drop table MSdatatype_info
  48. go
  49. if (exists (select * from sysobjects
  50.         where name = 'MSdatatype_info_ext' and type = 'U '))
  51.     drop table MSdatatype_info_ext
  52. go
  53. if (exists (select * from sysobjects
  54.         where name = 'MStable_types' and type = 'U '))
  55.     drop table MStable_types
  56. go
  57. if (exists (select * from sysobjects
  58.         where name = 'MSserver_info' and type = 'U '))
  59.     drop table MSserver_info
  60. go
  61. if (exists (select * from sysobjects
  62.         where name = 'spt_table_types' and type = 'U '))
  63.     drop table spt_table_types
  64. go
  65. /*
  66. ** If tables or procs already exist, drop them.
  67. */
  68.  
  69. if (exists (select * from sysobjects
  70.         where name = 'spt_datatype_info' and type = 'U '))
  71.     drop table spt_datatype_info
  72. go
  73. if (exists (select * from sysobjects
  74.         where name = 'spt_datatype_info_ext' and type = 'U '))
  75.     drop table spt_datatype_info_ext
  76. go
  77. if (exists (select * from sysobjects
  78.         where name = 'sp_add_server_sortinfo' and type = 'P '))
  79.     drop proc sp_add_server_sortinfo
  80. go
  81. if (exists (select * from sysobjects
  82.         where name = 'spt_server_info' and type = 'U '))
  83.     drop table spt_server_info
  84. go
  85. if (exists (select * from sysobjects
  86.         where name = 'sp_tables' and type = 'P '))
  87.     drop proc sp_tables
  88. go
  89. if (exists (select * from sysobjects
  90.         where name = 'sp_statistics' and type = 'P '))
  91.     drop proc sp_statistics
  92. go
  93. if (exists (select * from sysobjects
  94.         where name = 'sp_columns' and type = 'P '))
  95.     drop proc sp_columns
  96. go
  97. if (exists (select * from sysobjects
  98.         where name = 'sp_fkeys' and type = 'P '))
  99.     drop proc sp_fkeys
  100. go
  101. if (exists (select * from sysobjects
  102.         where name = 'sp_pkeys' and type = 'P '))
  103.     drop proc sp_pkeys
  104. dump tran master with no_log
  105. go
  106.  
  107. go
  108. if (exists (select * from sysobjects
  109.         where name = 'sp_stored_procedures' and type = 'P '))
  110.     drop proc sp_stored_procedures
  111. go
  112. if (exists (select * from sysobjects
  113.         where name = 'sp_sproc_columns' and type = 'P '))
  114.     drop proc sp_sproc_columns
  115. go
  116. if (exists (select * from sysobjects
  117.         where name = 'sp_table_privileges' and type = 'P '))
  118.     drop proc sp_table_privileges
  119. go
  120. if (exists (select * from sysobjects
  121.         where name = 'sp_column_privileges' and type = 'P '))
  122.     drop proc sp_column_privileges
  123. go
  124. dump tran master with no_log
  125. go
  126. if (exists (select * from sysobjects
  127.         where name = 'sp_server_info' and type = 'P '))
  128.     drop proc sp_server_info
  129. go
  130. if (exists (select * from sysobjects
  131.         where name = 'sp_datatype_info' and type = 'P '))
  132.     drop proc sp_datatype_info
  133. go
  134. if (exists (select * from sysobjects
  135.         where name = 'sp_special_columns' and type = 'P '))
  136.     drop proc sp_special_columns
  137. go
  138. if (exists (select * from sysobjects
  139.         where name = 'sp_databases' and type = 'P '))
  140.     drop proc sp_databases
  141. go
  142. dump tran master with no_log
  143. go
  144. if (exists (select * from sysobjects
  145.         where name = 'sp_ddopen' and type = 'P '))
  146.     drop proc sp_ddopen
  147. go
  148.  
  149. dump tran master with no_log
  150. go
  151.  
  152.  
  153. if (exists (select * from sysobjects
  154.         where name = 'spt_provider_types' and type = 'U '))
  155.     begin
  156.     drop table spt_provider_types
  157.     dump tran master with no_log
  158.     end
  159. go
  160. if (exists (select * from sysobjects
  161.         where name = 'sp_catalogs_rowset' and type = 'P '))
  162.     begin
  163.     drop procedure sp_catalogs_rowset
  164.     dump tran master with no_log
  165.     end
  166. go
  167. if (exists (select * from sysobjects
  168.         where name = 'sp_column_privileges_rowset' and type = 'P '))
  169.     begin
  170.     drop procedure sp_column_privileges_rowset
  171.     dump tran master with no_log
  172.     end
  173. go
  174. if (exists (select * from sysobjects
  175.         where name = 'sp_columns_rowset' and type = 'P '))
  176.     begin
  177.     drop procedure sp_columns_rowset
  178.     dump tran master with no_log
  179.     end
  180. go
  181. if (exists (select * from sysobjects
  182.         where name = 'sp_foreign_keys_rowset' and type = 'P '))
  183.     begin
  184.     drop procedure sp_foreign_keys_rowset
  185.     dump tran master with no_log
  186.     end
  187. go
  188. if (exists (select * from sysobjects
  189.         where name = 'sp_indexes_rowset' and type = 'P '))
  190.     begin
  191.     drop procedure sp_indexes_rowset
  192.     dump tran master with no_log
  193.     end
  194. go
  195. if (exists (select * from sysobjects
  196.         where name = 'sp_primary_keys_rowset' and type = 'P '))
  197.     begin
  198.     drop procedure sp_primary_keys_rowset
  199.     dump tran master with no_log
  200.     end
  201. go
  202. if (exists (select * from sysobjects
  203.         where name = 'sp_procedure_params_rowset' and type = 'P '))
  204.     begin
  205.     drop procedure sp_procedure_params_rowset
  206.     dump tran master with no_log
  207.     end
  208. go
  209. if (exists (select * from sysobjects
  210.         where name = 'sp_procedures_rowset' and type = 'P '))
  211.     begin
  212.     drop procedure sp_procedures_rowset
  213.     dump tran master with no_log
  214.     end
  215. go
  216. if (exists (select * from sysobjects
  217.         where name = 'sp_provider_types_rowset' and type = 'P '))
  218.     begin
  219.     drop procedure sp_provider_types_rowset
  220.     dump tran master with no_log
  221.     end
  222. go
  223. if (exists (select * from sysobjects
  224.         where name = 'sp_schemata_rowset' and type = 'P '))
  225.     begin
  226.     drop procedure sp_schemata_rowset
  227.     dump tran master with no_log
  228.     end
  229. go
  230. if (exists (select * from sysobjects
  231.         where name = 'sp_statistics_rowset' and type = 'P '))
  232.     begin
  233.     drop procedure sp_statistics_rowset
  234.     dump tran master with no_log
  235.     end
  236. go
  237. if (exists (select * from sysobjects
  238.         where name = 'sp_tables_rowset' and type = 'P '))
  239.     begin
  240.     drop procedure sp_tables_rowset
  241.     dump tran master with no_log
  242.     end
  243. go
  244. if (exists (select * from sysobjects
  245.         where name = 'sp_tables_info_rowset' and type = 'P '))
  246.     begin
  247.     drop procedure sp_tables_info_rowset
  248.     dump tran master with no_log
  249.     end
  250. go
  251. if (exists (select * from sysobjects
  252.         where name = 'sp_table_privileges_rowset' and type = 'P '))
  253.     begin
  254.     drop proc sp_table_privileges_rowset
  255.     dump tran master with no_log
  256.     end
  257. go
  258. if (exists (select * from sysobjects
  259.         where name = 'sp_linkedservers_rowset' and type = 'P '))
  260.     begin
  261.     drop proc sp_linkedservers_rowset
  262.     dump tran master with no_log
  263.     end
  264. go
  265.  
  266.  
  267. print 'creating table spt_datatype_info_ext'
  268. go
  269.  
  270. if (charindex('6.00', @@version) = 0 and
  271.     charindex('6.50', @@version) = 0 and
  272.     charindex('7.00', @@version) = 0)
  273. begin    /*    Pre 6.0 server */
  274.     print ''
  275.     print ''
  276.     print 'Warning:'
  277.     print 'you are installing the stored procedures '
  278.     print 'on a pre 6.0 SQL Server.'
  279.     print 'Ignore the following error.'
  280.     create table spt_datatype_info_ext (
  281.                 user_type        smallint    not null,
  282.                 CREATE_PARAMS    varchar(32) null,
  283.                 AUTO_INCREMENT    smallint null,
  284.                 typename        varchar(32))
  285. end
  286. go
  287. if (charindex('6.00', @@version) > 0 or
  288.     charindex('6.50', @@version) > 0 or
  289.     charindex('7.00', @@version) > 0)
  290. begin    /* 6.0 or later server */
  291.     create table spt_datatype_info_ext (
  292.                 user_type        smallint    not null,
  293.                 CREATE_PARAMS    varchar(32) null,
  294.                 AUTO_INCREMENT    smallint null,
  295.                 typename        sysname)    /* from systypes, to avoid xusertype hard-code */
  296. end
  297. go
  298.  
  299. grant select on spt_datatype_info_ext to public
  300. go
  301.  
  302.  
  303. insert into spt_datatype_info_ext
  304.     /* CHAR      user_type, create_params, auto_increment */
  305.     values             (1,    'length' ,0, 'char')
  306.  
  307. insert into spt_datatype_info_ext
  308.     /* VARCHAR     user_type, create_params, auto_increment */
  309.     values             (2,    'max length' ,0, 'varchar')
  310.  
  311. insert into spt_datatype_info_ext
  312.     /* BINARY     user_type, create_params, auto_increment */
  313.     values             (3,    'length' ,0, 'binary')
  314.  
  315. insert into spt_datatype_info_ext
  316.     /* VARBINARY user_type, create_params, auto_increment */
  317.     values             (4,    'max length' ,0, 'varbinary')
  318.  
  319. if    (charindex('6.00', @@version) > 0 or
  320.      charindex('6.50', @@version) > 0 or
  321.      charindex('7.00', @@version) > 0)
  322. begin    /*    Add 6.0 data types */
  323.     insert into spt_datatype_info_ext
  324.         /* DECIMAL user_type, create_params, auto_increment */
  325.         values             (26,    'precision,scale' ,0, 'decimal')
  326.  
  327.     insert into spt_datatype_info_ext
  328.         /* NUMERIC user_type, create_params, auto_increment */
  329.         values             (25,    'precision,scale' ,0, 'numeric')
  330.  
  331.     insert into spt_datatype_info_ext
  332.         /* DECIMAL IDENTITY user_type, create_params, auto_increment */
  333.         values             (26,    'precision' ,1, 'decimal')
  334.  
  335.     insert into spt_datatype_info_ext
  336.         /* NUMERIC IDENTITY user_type, create_params, auto_increment */
  337.         values             (25,    'precision' ,1, 'numeric')
  338.  
  339. end
  340. else    /*    Pre 6.0 server, add SYSNAME create param */
  341.     begin
  342.         insert into spt_datatype_info_ext
  343.             /* SYSNAME     user_type, create_param, auto_increments */
  344.             values             (18,    'max length' ,0, 'sysname')
  345.  
  346.     end
  347. go
  348.  
  349. if (charindex('7.00', @@version) = 0)
  350. begin
  351.     print ''
  352.     print ''
  353.     print 'Warning:'
  354.     print 'you are installing the stored procedures '
  355.     print 'on a pre 7.0 SQL Server.'
  356.     print 'Ignore the following errors.'
  357. end
  358. go
  359. if (charindex('7.00', @@version) > 0)
  360. begin    /*    Update usertypes for 7.00 server */
  361.     begin tran
  362.     insert into spt_datatype_info_ext
  363.         /* NCHAR     user_type, create_params, auto_increment */
  364.         values             (0,    'length' ,0, 'nchar')
  365.  
  366.     insert into spt_datatype_info_ext
  367.         /* NVARCHAR     user_type, create_params, auto_increment */
  368.         values             (0,    'max length' ,0, 'nvarchar')
  369.  
  370.     /* SET user_type TO SPHINX VALUES */
  371.     update spt_datatype_info_ext set user_type = xusertype
  372.         from spt_datatype_info_ext e, systypes t where t.name = e.typename
  373.  
  374.     commit tran
  375. end
  376. go
  377.  
  378. create unique clustered index datatypeinfoextclust on spt_datatype_info_ext(user_type,AUTO_INCREMENT)
  379. go
  380.  
  381. print 'creating table spt_datatype_info'
  382. go
  383. if (charindex('6.00', @@version) = 0 and
  384.     charindex('6.50', @@version) = 0 and
  385.     charindex('7.00', @@version) = 0)
  386. begin    /*    Pre 6.0 server */
  387.     print ''
  388.     print ''
  389.     print 'Warning:'
  390.     print 'you are installing the stored procedures '
  391.     print 'on a pre 6.0 SQL Server.'
  392.     print 'Ignore the following error.'
  393.     create table spt_datatype_info (
  394.         ss_dtype            tinyint     not null,
  395.         fixlen                int         null,        /* datatype len for variable, else null */
  396.         ODBCVer             tinyint     null,        /* version if needed, else null */
  397.         TYPE_NAME            varchar(32)    not null,
  398.         DATA_TYPE            smallint    not null,
  399.         data_precision        int         null,
  400.         numeric_scale        smallint    null,    /* min scale if 6.0 */
  401.         RADIX                smallint    null,
  402.         length                int         null,
  403.         LITERAL_PREFIX        varchar(32) null,
  404.         LITERAL_SUFFIX        varchar(32) null,
  405.         CREATE_PARAMS        varchar(32) null,
  406.         NULLABLE            smallint    not null,
  407.         CASE_SENSITIVE        smallint    not null,
  408.         SEARCHABLE            smallint    not null,
  409.         UNSIGNED_ATTRIBUTE    smallint    null,
  410.         MONEY                smallint    not null,
  411.         AUTO_INCREMENT        smallint    null,
  412.         LOCAL_TYPE_NAME     varchar(32)    null,
  413.         charbin             tinyint     null, /* 0 for char/binary types, NULL for all others */
  414.         SQL_DATA_TYPE        smallint    not null,
  415.         SQL_DATETIME_SUB    smallint    null)
  416. end
  417. go
  418. if (charindex('6.00', @@version) > 0 or
  419.     charindex('6.50', @@version) > 0 or
  420.     charindex('7.00', @@version) > 0)
  421. begin    /* 6.0 or later server */
  422.     create table spt_datatype_info (
  423.         ss_dtype            tinyint     not null,
  424.         fixlen                int         null,        /* datatype len for variable, else null */
  425.         ODBCVer             tinyint     null,        /* version if needed, else null */
  426.         TYPE_NAME            sysname     not null,
  427.         DATA_TYPE            smallint    not null,
  428.         data_precision        int         null,
  429.         numeric_scale        smallint    null,    /* min scale if 6.0 */
  430.         RADIX                smallint    null,
  431.         length                int         null,
  432.         LITERAL_PREFIX        varchar(32) null,
  433.         LITERAL_SUFFIX        varchar(32) null,
  434.         CREATE_PARAMS        varchar(32) null,
  435.         NULLABLE            smallint    not null,
  436.         CASE_SENSITIVE        smallint    not null,
  437.         SEARCHABLE            smallint    not null,
  438.         UNSIGNED_ATTRIBUTE    smallint    null,
  439.         MONEY                smallint    not null,
  440.         AUTO_INCREMENT        smallint    null,
  441.         LOCAL_TYPE_NAME     sysname     null,
  442.         charbin             tinyint     null, /* 0 for char/binary types, NULL for all others */
  443.         SQL_DATA_TYPE        smallint    not null,
  444.         SQL_DATETIME_SUB    smallint    null)
  445. end
  446. go
  447.  
  448. grant select on spt_datatype_info to public
  449. go
  450.  
  451. /* Get case sensitivity */
  452. /*(leih 10/8/96) SID: Part I. break into two batches to avoid OOM error */
  453. if 'A' = 'A' /* create dummy begin block */
  454. begin
  455.     declare @case smallint
  456.  
  457.     begin tran
  458.     select @case = 0
  459.     select @case = 1 where 'a' <> 'A'
  460.  
  461.     /* Local Binary */
  462.     insert into spt_datatype_info values
  463.     (45,null,null,'binary',-2,null,null,null,null,'0x',null,'length',1,0,2,null,0,null,'binary',0,-2,null)
  464.  
  465.     /* Local Bit */
  466.     insert into spt_datatype_info values
  467.     (50,null,null,'bit',-7,1,0,2,null,null,null,null,0,0,2,null,0,null,'bit',null,-7,null)
  468.  
  469.     /* Local Char */
  470.     insert into spt_datatype_info values
  471.     (47,null,null,'char',1,null,null,null,null,'''','''','length',1,@case,3,null,0,null,'char',0,1,null)
  472.  
  473.     /* Local Datetime */
  474.     insert into spt_datatype_info values
  475.     (61,8,2,'datetime',11,23,3,10,16,'''','''',null,1,0,3,null,0,null,'datetime',null,9,3)
  476.     insert into spt_datatype_info values
  477.     (61,8,3,'datetime',93,23,3,10,16,'''','''',null,1,0,3,null,0,null,'datetime',null,9,3)
  478.  
  479.     /* Local Smalldatetime */
  480.     insert into spt_datatype_info values
  481.     (58,4,2,'smalldatetime',11,16,0,10,16,'''','''',null,1,0,3,null,0,null,'smalldatetime',null,9,3)
  482.     insert into spt_datatype_info values
  483.     (58,4,3,'smalldatetime',93,16,0,10,16,'''','''',null,1,0,3,null,0,null,'smalldatetime',null,9,3)
  484.  
  485.     /* Local Float */
  486.     insert into spt_datatype_info values
  487.     (62,8,2,'float',6,15,null,10,8,null,null,null,1,0,2,0,0,0,'float',null,6,null)
  488.     insert into spt_datatype_info values
  489.     (62,8,3,'float',6,53,null, 2,8,null,null,null,1,0,2,0,0,0,'float',null,6,null)
  490.  
  491.     /* Local Real */
  492.     insert into spt_datatype_info values
  493.     (59,4,2,'real',7, 7,null,10,4,null,null,null,1,0,2,0,0,0,'real',null,7,null)
  494.     insert into spt_datatype_info values
  495.     (59,4,3,'real',7,24,null, 2,4,null,null,null,1,0,2,0,0,0,'real',null,7,null)
  496.  
  497.     /* Local Smallmoney */
  498.     insert into spt_datatype_info values
  499.     (122,4,null,'smallmoney',3,10,4,10,12,'$',null,null,1,0,2,0,1,0,'smallmoney',null,3,null)
  500.  
  501.     /* Local Money */
  502.     insert into spt_datatype_info values
  503.     (60,8,null,'money',3,19,4,10,21,'$',null,null,1,0,2,0,1,0,'money',null,3,null)
  504.  
  505.     /* Local Int */
  506.     insert into spt_datatype_info values
  507.     (56,4,null,'int',4,10,0,10,4,null,null,null,1,0,2,0,0,0,'int',null,4,null)
  508.  
  509.     commit tran
  510. end
  511. go
  512.  
  513. /*(leih 10/8/96) SID: Part II. break into two batches to avoid OOM error */
  514. if 'A' = 'A' /* create dummy begin block */
  515. begin
  516.     declare @case smallint
  517.  
  518.     begin tran
  519.     select @case = 0
  520.     select @case = 1 where 'a' <> 'A'
  521.  
  522.  
  523.     /* Local Smallint */
  524.     insert into spt_datatype_info values
  525.     (52,2,null,'smallint',5,5,0,10,2,null,null,null,1,0,2,0,0,0,'smallint',null,5,null)
  526.     insert into spt_datatype_info values
  527.     (52,2,1,'smallint',5,5,0,10,2,null,null,null,1,0,2,0,0,0,'smallint',null,5,null)
  528.  
  529.     /* Local Tinyint */
  530.     insert into spt_datatype_info values
  531.     (48,1,null,'tinyint',-6,3,0,10,1,null,null,null,1,0,2,1,0,0,'tinyint',null,-6,null)
  532.  
  533.     /* Local Text */
  534.     insert into spt_datatype_info values
  535.     (35,null,null,'text',-1,2147483647,null,null,2147483647,'''','''',null,1,@case,1,null,0,null,'text',0,-1,null)
  536.  
  537.     /* Local Varbinary */
  538.     insert into spt_datatype_info values
  539.     (37,null,null,'varbinary',-3,null,null,null,null,'0x',null,'max length',1,0,2,null,0,null,'varbinary',0,-3,null)
  540.  
  541.     /* Local Varchar */
  542.     insert into spt_datatype_info values
  543.     (39,null,null,'varchar',12,null,null,null,null,'''','''','max length',1,@case,3,null,0,null,'varchar',0,12,null)
  544.  
  545.     /* Local Image */
  546.     insert into spt_datatype_info values
  547.     (34,null,null,'image',-4,2147483647,null,null,2147483647,'0x',null,null,1,0,0,null,0,null,'image',0,-4,null)
  548.  
  549.     if (charindex('6.00', @@version) > 0 or
  550.         charindex('6.50', @@version) > 0 or
  551.         charindex('7.00', @@version) > 0)
  552.     begin    /*    Add 6.0 data types */
  553.         /* Local Decimal */
  554.         insert into spt_datatype_info values    /* sql server type is 'decimaln' */
  555.         (55,null,null,'decimal',3,38,0,10,null,null,null,'precision,scale',1,0,2,0,0,0,'decimal',null,3,null)
  556.  
  557.         /* Local Numeric */
  558.         insert into spt_datatype_info values    /* sql server type is 'numericn' */
  559.         (63,null,null,'numeric',2    ,38,0,10,null,null,null,'precision,scale',1,0,2,0,0,0,'numeric',null,2,null)
  560.  
  561.         /* Identity attribute data types */
  562.  
  563.         /* Identity Int */
  564.         insert into spt_datatype_info values
  565.         (56,null,null,'int identity',4,10,0,10,null,null,null,null,0,0,2,0,0,1,'int identity',null,4,null)
  566.  
  567.         /* Identity Smallint */
  568.         insert into spt_datatype_info values
  569.         (52,null,null,'smallint identity',5,5,0,10,null,null,null,null,0,0,2,0,0,1,'smallint identity',null,5,null)
  570.  
  571.         /* Identity Tinyint */
  572.         insert into spt_datatype_info values
  573.         (48,null,null,'tinyint identity',-6,3,0,10,null,null,null,null,0,0,2,1,0,1,'tinyint identity',null,-6,null)
  574.  
  575.         /* Identity Numeric */
  576.         insert into spt_datatype_info values    /* sql server type is 'decmaln' */
  577.         (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)
  578.  
  579.         /* Identity Numeric */
  580.         insert into spt_datatype_info values    /* sql server type is 'decmaln' */
  581.         (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)
  582.  
  583.     end
  584.  
  585.     if (charindex('7.00', @@version) = 0)
  586.     begin    /*    Add nullable type for non-Sphinx server */
  587.         /* Local Datetimn */
  588.         insert into spt_datatype_info values
  589.         (111,4,2,'smalldatetime',11,16,0,10,16,'''','''',null,1,0,3,null,0,null,'smalldatetime',null,9,3)
  590.         insert into spt_datatype_info values
  591.         (111,4,3,'smalldatetime',93,16,0,10,16,'''','''',null,1,0,3,null,0,null,'smalldatetime',null,9,3)
  592.         insert into spt_datatype_info values /* sql server type is 'datetimn' */
  593.         (111,8,2,'datetime',11,23,3,10,16,'''','''',null,1,0,3,null,0,null,'datetime',null,9,3)
  594.         insert into spt_datatype_info values
  595.         (111,8,3,'datetime',93,23,3,10,16,'''','''',null,1,0,3,null,0,null,'datetime',null,9,3)
  596.  
  597.         /* Local Floatn */
  598.         insert into spt_datatype_info values /* sql server type is 'floatn' */
  599.         (109,4,2,'real',7, 7,null,10,4,null,null,null,1,0,2,0,0,0,'real',null,7,null)
  600.         insert into spt_datatype_info values
  601.         (109,4,3,'real',7,24,null, 2,4,null,null,null,1,0,2,0,0,0,'real',null,7,null)
  602.         insert into spt_datatype_info values /* sql server type is 'floatn' */
  603.         (109,8,2,'float',6,15,null,10,8,null,null,null,1,0,2,0,0,0,'float',null,6,null)
  604.         insert into spt_datatype_info values
  605.         (109,8,3,'float',6,53,null, 2,8,null,null,null,1,0,2,0,0,0,'float',null,6,null)
  606.  
  607.         /* Local Moneyn */
  608.         insert into spt_datatype_info values    /* sql server type is 'moneyn' */
  609.         (110,4,null,'smallmoney',3,10,4,10,12,'$',null,null,1,0,2,0,1,0,'smallmoney',null,3,null)
  610.         insert into spt_datatype_info values    /* sql server type is 'moneyn' */
  611.         (110,8,null,'money',3,19,4,10,21,'$',null,null,1,0,2,0,1,0,'money',null,3,null)
  612.  
  613.         /* Local Intn */
  614.         insert into spt_datatype_info values /* sql server type is 'intn' */
  615.         (38,4,null,'int',4,10,0,10,4,null,null,null,1,0,2,0,0,0,'int',null,4,null)
  616.         insert into spt_datatype_info values /* sql server type is 'intn' */
  617.         (38,2,null,'smallint',5,5,0,10,2,null,null,null,1,0,2,0,0,0,'smallint',null,5,null)
  618.         insert into spt_datatype_info values
  619.         (38,1,null,'tinyint',-6,3,0,10,1,null,null,null,1,0,2,1,0,0,'tinyint',null,-6,null)
  620.  
  621.         if (charindex('6.00', @@version) > 0 or
  622.             charindex('6.50', @@version) > 0 or
  623.             charindex('7.00', @@version) > 0)
  624.         begin    /*    Add 6.0 data types */
  625.             /* Local Decimaln */
  626.             insert into spt_datatype_info values    /* sql server type is 'decimaln' */
  627.             (106,null,null,'decimal',3,38,0,10,null,null,null,'precision,scale',1,0,2,0,0,0,'decimal',null,3,null)
  628.             insert into spt_datatype_info values    /* sql server type is 'decmaln' */
  629.             (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)
  630.  
  631.             /* Local Numericn */
  632.             insert into spt_datatype_info values    /* sql server type is 'numericn' */
  633.             (108,null,null,'numeric',2,38,0,10,null,null,null,'precision,scale',1,0,2,0,0,0,'numeric',null,2,null)
  634.             insert into spt_datatype_info values    /* sql server type is 'decmaln' */
  635.             (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)
  636.         end
  637.     end
  638.  
  639.     commit tran
  640. end
  641. go
  642.  
  643. if (charindex('7.00', @@version) = 0)
  644. begin
  645.     print ''
  646.     print ''
  647.     print 'Warning:'
  648.     print 'you are installing the stored procedures '
  649.     print 'on a pre 7.0 SQL Server.'
  650.     print 'Ignore the following errors.'
  651. end
  652. go
  653. if (charindex('7.00', @@version) > 0)
  654.     begin
  655.         declare @case smallint
  656.  
  657.         select @case = 0
  658.         select @case = 1 where 'a' <> 'A'
  659.  
  660.         /* Local Timestamp */
  661.         insert into spt_datatype_info values
  662.         (0,null,null,'timestamp',-2,8,null,null,null,'0x',null,null,1,0,2,null,0,null,'timestamp',0,-2,null)
  663.  
  664.         /* Local GUID */
  665.         insert into spt_datatype_info values
  666.         (0,null,null,'uniqueidentifier',-11,36,null,null,null,'''','''',null,1,0,2,null,0,null,'uniqueidentifier',NULL,-11,null)
  667.  
  668.         /* Local NChar */
  669.         insert into spt_datatype_info values
  670.         (0,null,null,'nchar',-8,null,null,null,null,'N''','''','length',1,@case,3,null,0,null,'nchar',0,-8,null)
  671.  
  672.         /* Local NVarchar */
  673.         insert into spt_datatype_info values
  674.         (0,null,null,'nvarchar',-9,null,null,null,null,'N''','''','max length',1,@case,3,null,0,null,'nvarchar',0,-9,null)
  675.  
  676.         /* Local NText */
  677.         insert into spt_datatype_info values
  678.         (0,null,null,'ntext',-10,2147483646,null,null,2147483646,'N''','''',null,1,@case,1,null,0,null,'ntext',0,-10,null)
  679.  
  680.         update spt_datatype_info set nullable = 1
  681.             where TYPE_NAME = 'bit'
  682.  
  683.         update spt_datatype_info set ss_dtype = (select xtype from systypes
  684.             where TYPE_NAME like name+'%')
  685.     end
  686. go
  687.  
  688. create unique clustered index datatypeinfoclust on spt_datatype_info(ss_dtype,fixlen,ODBCVer,AUTO_INCREMENT)
  689. go
  690.  
  691. dump tran master with no_log
  692. go
  693.  
  694. print 'creating table spt_server_info'
  695. go
  696. create table spt_server_info (
  697.               attribute_id        int NOT NULL,
  698.               attribute_name    varchar(60) NOT NULL,
  699.               attribute_value    varchar(255) NOT NULL)
  700. go
  701.  
  702. create unique clustered index serverinfoclust on spt_server_info(attribute_id)
  703. go
  704.  
  705. /* 
  706. ** (leih 2/11/98) 
  707. ** The following SP is shared by instcat.sql and the server to add information
  708. ** related to server language collations. The server calls it at the end of
  709. ** collation change to update the catalog with the new language collations
  710. ** Everything in this SP should NOT use tempdb.  
  711. */
  712. create procedure sp_add_server_sortinfo
  713. as
  714. -- spt_server_info has unigue clustered index on attribute_id
  715. delete spt_server_info where attribute_id = 16
  716. delete spt_server_info where attribute_id = 18
  717.  
  718. --insert into spt_server_info
  719. --    select 18,'COLLATION_SEQ',
  720. --        'charset='+t2.name+' sort_order='+t1.name
  721. --        +' charset_num='+rtrim(convert(char(4),t1.csid))+
  722. --        ' sort_order_num='+rtrim(convert(char(4),t1.id))
  723. --    from master.dbo.syscharsets t1, master.dbo.syscharsets t2, master.dbo.sysconfigures t3
  724. --    where t1.csid=t2.id and t1.id=t3.value and t3.config in (123,1123)
  725. declare @sortid int, @csid int, @sortname varchar(255), @attribute_value varchar(255)
  726. select @sortid = value from sysconfigures where config = 1123
  727. select @csid = csid, @sortname = name from master.dbo.syscharsets where id = @sortid
  728. select @attribute_value = 'charset='+ name +' sort_order=' + @sortname
  729.         + ' charset_num=' + rtrim(convert(char(4),@csid))+
  730.         ' sort_order_num=' + rtrim(convert(char(4),@sortid))
  731.         from syscharsets where id = @csid
  732.  
  733. insert into spt_server_info
  734.         values (18,'COLLATION_SEQ', isnull(@attribute_value, ' '))
  735.  
  736. if 'A' = 'a' /* If not case sensitive server */
  737. begin
  738.     insert into spt_server_info
  739.         values (16,'IDENTIFIER_CASE','MIXED')
  740. end
  741. else
  742. begin
  743.     insert into spt_server_info
  744.         values (16,'IDENTIFIER_CASE','SENSITIVE')
  745. end
  746.  
  747. go
  748.  
  749. exec sp_add_server_sortinfo
  750. go
  751.  
  752. if (charindex('7.00', @@version) = 0)
  753.     begin
  754.         drop procedure sp_add_server_sortinfo    /* not used by older servers */
  755.         dump tran master with no_log
  756.     end
  757. go
  758.  
  759.  
  760. /*(leih 10/8/96) SID: Part I break into two batches to avoid OOM error */
  761. insert into spt_server_info
  762.     values (1,'DBMS_NAME','Microsoft SQL Server')
  763. insert into spt_server_info
  764.     values (2,'DBMS_VER',@@version)
  765. insert into spt_server_info
  766.     values (10,'OWNER_TERM','owner')
  767. insert into spt_server_info
  768.     values (11,'TABLE_TERM','table')
  769. insert into spt_server_info
  770.     values (12,'MAX_OWNER_NAME_LENGTH','30')
  771. insert into spt_server_info
  772.     values (13,'TABLE_LENGTH','30')
  773. insert into spt_server_info
  774.     values (14,'MAX_QUAL_LENGTH','30')
  775. insert into spt_server_info
  776.     values (15,'COLUMN_LENGTH','30')
  777. insert into spt_server_info
  778.     values (17,'TX_ISOLATION','2')
  779. insert into spt_server_info
  780.     values (19,'SAVEPOINT_SUPPORT','Y')
  781. insert into spt_server_info
  782.     values (20,'MULTI_RESULT_SETS','Y')
  783. insert into spt_server_info
  784.     values (22,'ACCESSIBLE_TABLES','Y')
  785. go
  786.  
  787. /*(leih 10/8/96) SID: Part II. break into two batches to avoid OOM error */
  788. insert into spt_server_info
  789.     values (100,'USERID_LENGTH','30')
  790. insert into spt_server_info
  791.     values (101,'QUALIFIER_TERM','database')
  792. insert into spt_server_info
  793.     values (102,'NAMED_TRANSACTIONS','Y')
  794. insert into spt_server_info
  795.     values (103,'SPROC_AS_LANGUAGE','Y')
  796. insert into spt_server_info
  797.     values (104,'ACCESSIBLE_SPROC','Y')
  798. insert into spt_server_info
  799.     values (105,'MAX_INDEX_COLS','16')
  800. insert into spt_server_info
  801.     values (106,'RENAME_TABLE','Y')
  802. insert into spt_server_info
  803.     values (107,'RENAME_COLUMN','Y')
  804. insert into spt_server_info
  805.     values (108,'DROP_COLUMN','N')
  806. insert into spt_server_info
  807.     values (109,'INCREASE_COLUMN_LENGTH','N')
  808. if (charindex('6.50', @@version) = 0 and
  809.     charindex('7.00', @@version) = 0)
  810. begin
  811.     insert into spt_server_info
  812.         values (110,'DDL_IN_TRANSACTION','N')
  813. end
  814. else
  815. begin
  816.     insert into spt_server_info
  817.         values (110,'DDL_IN_TRANSACTION','Y')
  818. end
  819. insert into spt_server_info
  820.     values (111,'DESCENDING_INDEXES','N')
  821. insert into spt_server_info
  822.     values (112,'SP_RENAME','Y')
  823. insert into spt_server_info
  824.     values (113,'REMOTE_SPROC','Y')
  825. insert into spt_server_info
  826.     values (500,'SYS_SPROC_VERSION','7.00.481')
  827. go
  828.  
  829. if (charindex('7.00', @@version) > 0)
  830. begin    /*    Update values for 7.00 server */
  831.     update spt_server_info set attribute_value = '128'
  832.         where attribute_id in (12,13,14,15,100)
  833. end
  834. go
  835.  
  836. grant select on spt_server_info to public
  837. go
  838.  
  839. print 'creating sp_column_privileges'
  840. go
  841.  
  842. /*    Procedure for pre 6.50 server */
  843. CREATE PROCEDURE sp_column_privileges (
  844.             @table_name         varchar(32),
  845.             @table_owner        varchar(32) = null,
  846.             @table_qualifier    varchar(32) = null,
  847.             @column_name        varchar(96) = null)    /* 3*32 */
  848. as
  849.  
  850.     set nocount on
  851.     declare @table_id     int
  852.     DECLARE @full_table_name    varchar(65)    /* 2*32 + 1 */
  853.     declare @low smallint                     /* range of userids to check */
  854.     declare @high smallint
  855.     declare @owner_uid smallint
  856.  
  857.     select @low = 0, @high = 32767
  858.  
  859.     if @column_name is null /*    If column name not supplied, match all */
  860.         select @column_name = '%'
  861.  
  862.     if @table_qualifier is not null
  863.     begin
  864.         if db_name() <> @table_qualifier
  865.         begin    /* If qualifier doesn't match current database */
  866.             raiserror 20001 '~~Rush_5~~'
  867.             return
  868.         end
  869.     end
  870.     if @table_owner is null
  871.     begin    /* If unqualified table name */
  872.         SELECT @full_table_name = @table_name
  873.     end
  874.     else
  875.     begin    /* Qualified table name */
  876.         SELECT @full_table_name = @table_owner + '.' + @table_name
  877.     end
  878.     /*    Get Object ID */
  879.     select @table_id = object_id(@full_table_name)
  880.  
  881.     if (@@trancount <> 0)
  882.     begin    /* If inside a transaction */
  883.         raiserror 20003 '~~Rush_6~~'
  884.         return
  885.     end
  886.  
  887.     /*
  888.     ** We need to create a table which will contain a row for every row to
  889.     ** be returned to the client.
  890.     */
  891.  
  892.     create table #column_priv1(
  893.         COLUMN_NAME             varchar(32) NOT NULL,
  894.         grantor                 smallint NOT NULL,
  895.         grantee                 smallint NOT NULL,
  896.         select_privilege        bit,
  897.         select_grantable        bit,
  898.         insert_privilege        bit,
  899.         insert_grantable        bit,
  900.         update_privilege        bit,
  901.         update_grantable        bit,
  902.         references_privilege    bit,
  903.         references_grantable    bit,
  904.         uid                     smallint NOT NULL,
  905.         gid                     smallint NOT NULL)
  906.  
  907. /*
  908. ** insert a row for the table owner (who has all permissions)
  909. */
  910.     select @owner_uid = (
  911.         select uid
  912.         from sysobjects
  913.         where id = @table_id)
  914.  
  915.     if (charindex('6.00', @@version) > 0)
  916.     begin
  917.         insert into #column_priv1
  918.             select
  919.                 c.name,
  920.                 u.uid,
  921.                 @owner_uid,
  922.                 0,
  923.                 1,
  924.                 0,
  925.                 1,
  926.                 0,
  927.                 1,
  928.                 0,
  929.                 1,
  930.                 @owner_uid,
  931.                 0
  932.             from syscolumns c, sysusers u
  933.             where id = @table_id
  934.                 and c.number = 0
  935.                 and u.suid = 1        /* grantor is dbo of database */
  936.     end
  937.     else
  938.     begin
  939.         insert into #column_priv1
  940.             select
  941.                 c.name,
  942.                 u.uid,
  943.                 @owner_uid,
  944.                 0,
  945.                 1,
  946.                 0,
  947.                 1,
  948.                 0,
  949.                 1,
  950.                 0,
  951.                 0,
  952.                 @owner_uid,
  953.                 0
  954.             from syscolumns c, sysusers u
  955.             where id = @table_id
  956.                 and c.number = 0
  957.                 and u.suid = 1        /* grantor is dbo of database */
  958.     end
  959. /*
  960. ** now stick in a row for every column for every user in the database
  961. ** we will need to weed out those who have no permissions later
  962. ** (and yes this is a cartesion product: the uid field in sysprotects
  963. ** can also have a group id, in which case we need to extend those
  964. ** privileges to all group members).
  965. */
  966.  
  967.     insert into #column_priv1
  968.         select distinct
  969.             c.name,
  970.             o.uid,
  971.             u.uid,
  972.             0,
  973.             0,
  974.             0,
  975.             0,
  976.             0,
  977.             0,
  978.             0,
  979.             0,
  980.             u.uid,
  981.             u.gid
  982.         from sysusers u, syscolumns c, sysobjects o
  983.         where o.id = @table_id
  984.             and c.id = o.id
  985.             and c.number = 0
  986.             and u.gid <> u.uid
  987.             and u.uid <> @owner_uid
  988.  
  989.     /*
  990.     ** we need to create another temporary table to contain all the various
  991.     ** protection information for the table in question
  992.     */
  993.     create table #protects (
  994.                 uid         smallint NOT NULL,
  995.                 grantor        smallint NOT NULL,
  996.                 action        tinyint NOT NULL,
  997.                 protecttype tinyint NOT NULL,
  998.                 name        varchar(32) NOT NULL)
  999.  
  1000.     insert into #protects
  1001.         select
  1002.             p.uid,
  1003.             p.uid,
  1004.             p.action,
  1005.             p.protecttype,
  1006.             isnull(col_name(id, c.number), '~All')
  1007.             from
  1008.                 sysprotects p,
  1009.                 master.dbo.spt_values c,
  1010.                 master.dbo.spt_values a,
  1011.                 master.dbo.spt_values b
  1012.             where
  1013.                 convert(tinyint, substring(isnull(p.columns, 0x1), c.low, 1))
  1014.                     & c.high <> 0
  1015.                     and c.number <= (
  1016.                         select count(*)
  1017.                         from syscolumns
  1018.                         where id = @table_id)
  1019.                 and c.type = 'P'
  1020.                 and a.type = 'T'
  1021.                 and a.number = p.action
  1022.                 and p.action in (193,195,197,26)
  1023.                 and b.type = 'T'
  1024.                 and b.number = p.protecttype
  1025.                 and p.id = @table_id
  1026.                 and p.uid between @low and @high
  1027.  
  1028.  
  1029.     update #column_priv1
  1030.     set select_privilege = 1
  1031.         from #protects p
  1032.     where
  1033.         p.protecttype = 205
  1034.         and p.action = 193
  1035.         and (p.name = #column_priv1.COLUMN_NAME
  1036.             or name = '~All')
  1037.         and (p.uid = 0
  1038.             or p.uid = #column_priv1.gid
  1039.             or p.uid = #column_priv1.uid)
  1040.         and not exists (
  1041.             select * from #protects
  1042.             where
  1043.                 protecttype = 206
  1044.                 and action = 193
  1045.                 and (name = #column_priv1.COLUMN_NAME
  1046.                     or name = '~All')
  1047.                 and ( uid = 0
  1048.                     or uid = #column_priv1.gid
  1049.                     or uid = #column_priv1.uid))
  1050.  
  1051.     update #column_priv1
  1052.     set insert_privilege = 1
  1053.         from #protects p
  1054.     where
  1055.         p.protecttype = 205
  1056.         and p.action = 195
  1057.         and (p.name = #column_priv1.COLUMN_NAME
  1058.             or name = '~All')
  1059.         and (p.uid = 0
  1060.             or p.uid = #column_priv1.gid
  1061.             or p.uid = #column_priv1.uid)
  1062.         and not exists (
  1063.             select * from #protects
  1064.             where
  1065.                 protecttype = 206
  1066.                 and action = 195
  1067.                 and (name = #column_priv1.COLUMN_NAME
  1068.                        or name = '~All')
  1069.                 and (uid = 0
  1070.                     or uid = #column_priv1.gid
  1071.                     or uid = #column_priv1.uid))
  1072.  
  1073.     update #column_priv1
  1074.     set update_privilege = 1
  1075.         from #protects p
  1076.     where
  1077.         p.protecttype = 205
  1078.         and p.action = 197
  1079.         and (p.name = #column_priv1.COLUMN_NAME
  1080.             or name = '~All')
  1081.         and (p.uid = 0
  1082.             or p.uid = #column_priv1.gid
  1083.             or p.uid = #column_priv1.uid)
  1084.         and not exists (
  1085.             select * from #protects
  1086.                 where protecttype = 206
  1087.                 and action = 197
  1088.                 and (name = #column_priv1.COLUMN_NAME
  1089.                     or name = '~All')
  1090.                 and (uid = 0
  1091.                     or uid = #column_priv1.gid
  1092.                     or uid = #column_priv1.uid))
  1093.  
  1094.     update #column_priv1
  1095.     set references_privilege = 1
  1096.         from #protects p
  1097.     where
  1098.         p.protecttype = 205
  1099.         and p.action = 26
  1100.         and (p.name = #column_priv1.COLUMN_NAME
  1101.             or name = '~All')
  1102.         and (p.uid = 0
  1103.             or p.uid = #column_priv1.gid
  1104.             or p.uid = #column_priv1.uid)
  1105.         and not exists (
  1106.             select * from #protects
  1107.                 where protecttype = 206
  1108.                 and action = 26
  1109.                 and (name = #column_priv1.COLUMN_NAME
  1110.                     or name = '~All')
  1111.                 and (uid = 0
  1112.                     or uid = #column_priv1.gid
  1113.                     or uid = #column_priv1.uid))
  1114.  
  1115.     update #column_priv1
  1116.     set select_grantable = 1
  1117.         from #protects p
  1118.     where
  1119.         p.protecttype = 204
  1120.         and p.action = 193
  1121.         and (p.name = #column_priv1.COLUMN_NAME
  1122.             or name = '~All')
  1123.         and (p.uid = 0
  1124.             or p.uid = #column_priv1.gid
  1125.             or p.uid = #column_priv1.uid)
  1126.         and not exists (
  1127.             select * from #protects
  1128.             where
  1129.                 protecttype = 206
  1130.                 and action = 193
  1131.                 and (name = #column_priv1.COLUMN_NAME
  1132.                     or name = '~All')
  1133.                 and ( uid = 0
  1134.                     or uid = #column_priv1.gid
  1135.                     or uid = #column_priv1.uid))
  1136.  
  1137.     update #column_priv1
  1138.     set insert_grantable = 1
  1139.         from #protects p
  1140.     where
  1141.         p.protecttype = 204
  1142.         and p.action = 195
  1143.         and (p.name = #column_priv1.COLUMN_NAME
  1144.             or name = '~All')
  1145.         and (p.uid = 0
  1146.             or p.uid = #column_priv1.gid
  1147.             or p.uid = #column_priv1.uid)
  1148.         and not exists (
  1149.             select * from #protects
  1150.             where
  1151.                 protecttype = 206
  1152.                 and action = 195
  1153.                 and (name = #column_priv1.COLUMN_NAME
  1154.                     or name = '~All')
  1155.                 and ( uid = 0
  1156.                     or uid = #column_priv1.gid
  1157.                     or uid = #column_priv1.uid))
  1158.  
  1159.     update #column_priv1
  1160.     set update_grantable = 1
  1161.         from #protects p
  1162.     where
  1163.         p.protecttype = 204
  1164.         and p.action = 197
  1165.         and (p.name = #column_priv1.COLUMN_NAME
  1166.             or name = '~All')
  1167.         and (p.uid = 0
  1168.             or p.uid = #column_priv1.gid
  1169.             or p.uid = #column_priv1.uid)
  1170.         and not exists (
  1171.             select * from #protects
  1172.             where
  1173.                 protecttype = 206
  1174.                 and action = 197
  1175.                 and (name = #column_priv1.COLUMN_NAME
  1176.                     or name = '~All')
  1177.                 and ( uid = 0
  1178.                     or uid = #column_priv1.gid
  1179.                     or uid = #column_priv1.uid))
  1180.  
  1181.     update #column_priv1
  1182.     set references_grantable = 1
  1183.         from #protects p
  1184.     where
  1185.         p.protecttype = 204
  1186.         and p.action = 26
  1187.         and (p.name = #column_priv1.COLUMN_NAME
  1188.             or name = '~All')
  1189.         and (p.uid = 0
  1190.             or p.uid = #column_priv1.gid
  1191.             or p.uid = #column_priv1.uid)
  1192.         and not exists (
  1193.             select * from #protects
  1194.             where
  1195.                 protecttype = 206
  1196.                 and action = 26
  1197.                 and (name = #column_priv1.COLUMN_NAME
  1198.                     or name = '~All')
  1199.                 and ( uid = 0
  1200.                     or uid = #column_priv1.gid
  1201.                     or uid = #column_priv1.uid))
  1202.  
  1203.     create table #column_priv2(
  1204.         COLUMN_NAME     varchar(32) NOT NULL,
  1205.         grantor         smallint NULL,
  1206.         grantee         smallint NOT NULL,
  1207.         PRIVILEGE        varchar(32) NOT NULL,
  1208.         IS_GRANTABLE    varchar(3) NULL)
  1209.  
  1210.     insert into #column_priv2
  1211.         select
  1212.             COLUMN_NAME,
  1213.             grantor,
  1214.             grantee,
  1215.             'SELECT',
  1216.             'NO'
  1217.         from #column_priv1
  1218.         where select_privilege = 1 and select_grantable    = 0
  1219.  
  1220.     insert into #column_priv2
  1221.         select
  1222.             COLUMN_NAME,
  1223.             grantor,
  1224.             grantee,
  1225.             'INSERT',
  1226.             'NO'
  1227.         from #column_priv1
  1228.         where insert_privilege = 1 and insert_grantable = 0
  1229.  
  1230.     insert into #column_priv2
  1231.         select
  1232.             COLUMN_NAME,
  1233.             grantor,
  1234.             grantee,
  1235.             'UPDATE',
  1236.             'NO'
  1237.         from #column_priv1
  1238.         where update_privilege = 1 and update_grantable = 0
  1239.  
  1240.     insert into #column_priv2
  1241.         select
  1242.             COLUMN_NAME,
  1243.             grantor,
  1244.             grantee,
  1245.             'REFERENCES',
  1246.             'NO'
  1247.         from #column_priv1
  1248.         where references_privilege = 1 and references_grantable = 0
  1249.  
  1250.     insert into #column_priv2
  1251.         select
  1252.             COLUMN_NAME,
  1253.             grantor,
  1254.             grantee,
  1255.             'SELECT',
  1256.             'YES'
  1257.         from #column_priv1
  1258.         where select_grantable = 1
  1259.  
  1260.     insert into #column_priv2
  1261.         select
  1262.             COLUMN_NAME,
  1263.             grantor,
  1264.             grantee,
  1265.             'INSERT',
  1266.             'YES'
  1267.         from #column_priv1
  1268.         where insert_grantable = 1
  1269.  
  1270.     insert into #column_priv2
  1271.         select
  1272.             COLUMN_NAME,
  1273.             grantor,
  1274.             grantee,
  1275.             'UPDATE',
  1276.             'YES'
  1277.         from #column_priv1
  1278.         where update_grantable = 1
  1279.  
  1280.     insert into #column_priv2
  1281.         select
  1282.             COLUMN_NAME,
  1283.             grantor,
  1284.             grantee,
  1285.             'REFERENCES',
  1286.             'YES'
  1287.         from #column_priv1
  1288.         where references_grantable = 1
  1289.  
  1290.     select
  1291.         convert(varchar(32),db_name()) TABLE_QUALIFIER,
  1292.         convert(varchar(32),user_name(@owner_uid)) TABLE_OWNER,
  1293.         @table_name TABLE_NAME,
  1294.         COLUMN_NAME,
  1295.         convert(varchar(32),user_name(grantor)) GRANTOR,
  1296.         convert(varchar(32),user_name(grantee)) GRANTEE,
  1297.         PRIVILEGE,
  1298.         IS_GRANTABLE
  1299.     from #column_priv2
  1300.     where COLUMN_NAME like @column_name
  1301.     order by 4, 7
  1302. go
  1303.  
  1304. if (charindex('6.50', @@version) = 0 and
  1305.     charindex('7.00', @@version) = 0)
  1306. begin
  1307.     print ''
  1308.     print ''
  1309.     print 'Warning:'
  1310.     print 'you are installing the stored procedures '
  1311.     print 'on a pre 6.50 SQL Server.'
  1312.     print 'Ignore the following errors.'
  1313. end
  1314. else
  1315.     drop proc sp_column_privileges
  1316. go
  1317.  
  1318. /*    Procedure for 6.50 server */
  1319. CREATE PROCEDURE sp_column_privileges (
  1320.             @table_name         varchar(32),
  1321.             @table_owner        varchar(32) = null,
  1322.             @table_qualifier    varchar(32) = null,
  1323.             @column_name        varchar(96) = null)    /* 3*32 */
  1324. as
  1325.  
  1326.     declare @table_id     int
  1327.  
  1328.     if @column_name is null /*    If column name not supplied, match all */
  1329.         select @column_name = '%'
  1330.  
  1331.     if @table_qualifier is not null
  1332.     begin
  1333.         if db_name() <> @table_qualifier
  1334.         begin    /* If qualifier doesn't match current database */
  1335.             raiserror (15250, -1,-1)
  1336.             return
  1337.         end
  1338.     end
  1339.     if @table_owner is null
  1340.     begin    /* If unqualified table name */
  1341.         select @table_id = object_id(@table_name)
  1342.     end
  1343.     else
  1344.     begin    /* Qualified table name */
  1345.         select @table_id = object_id(@table_owner + '.' + @table_name)
  1346.     end
  1347.  
  1348.     select
  1349.         convert(varchar(32),db_name()) TABLE_QUALIFIER,
  1350.         convert(varchar(32),user_name(o.uid)) TABLE_OWNER,
  1351.         @table_name TABLE_NAME,
  1352.         convert(varchar(32),c.name) COLUMN_NAME,
  1353.         convert(varchar(32),user_name(p.grantor)) GRANTOR,
  1354.         convert(varchar(32),user_name(u.uid)) GRANTEE,
  1355.         convert (varchar(32),case p.action
  1356.              when 193 then 'SELECT'
  1357.              when 195 then 'INSERT'
  1358.              when 197 then 'UPDATE'
  1359.              else 'REFERENCES'
  1360.         end) PRIVILEGE,
  1361.         convert (varchar(3),case when p.protecttype = 205 then 'NO'
  1362.             else 'YES'
  1363.         end) IS_GRANTABLE
  1364.     from sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c
  1365.     where
  1366.         c.id = @table_id
  1367.         and c.name like @column_name
  1368.         and c.id = p.id
  1369.         and c.id = o.id
  1370.         and case substring(p.columns, 1, 1) & 1
  1371.                 when NULL then 255    /* all columns have permission */
  1372.                 when 0 then convert(tinyint, substring(p.columns, v.low, 1))
  1373.                 else (~convert(tinyint, isnull(substring(p.columns, v.low, 1),0)))
  1374.             end
  1375.             & v.high <> 0            /* permission applies to this column */
  1376.         and v.number <= (select count(*) from syscolumns
  1377.             where id = @table_id)    /* ranges from 1 to # of columns in table */
  1378.         and v.type = 'P'
  1379.         and v.number = c.colid
  1380.             /* expand groups */
  1381.         and ((p.uid = u.uid and u.uid <> u.gid) or
  1382.              (p.uid = u.gid and u.uid <> u.gid))
  1383.         and p.protecttype <> 206    /* only grant rows */
  1384.         and p.action in (26,193,195,197)
  1385.         and o.uid <> u.uid            /* no rows for owner */
  1386.         and not exists (            /* exclude revoke'd privileges */
  1387.             select *
  1388.             from sysprotects p1
  1389.             where
  1390.                 p1.protecttype = 206
  1391.                 and p1.action = p.action
  1392.                 and p1.id = p.id
  1393.                 and p1.uid = u.uid
  1394.                 and case substring(p1.columns, 1, 1) & 1
  1395.                         when NULL then 255    /* all columns have permission */
  1396.                         when 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  1397.                                             else (~convert(tinyint,isnull(substring(p.columns, v.low, 1),0)))
  1398.                     end
  1399.                     & v.high <> 0)            /* permission applies to this column */
  1400.     union all
  1401.     select    /*    Add rows for table owner */
  1402.         convert(varchar(32),db_name()) TABLE_QUALIFIER,
  1403.         convert(varchar(32),user_name(o.uid)) TABLE_OWNER,
  1404.         @table_name TABLE_NAME,
  1405.         convert(varchar(32),col_name(@table_id, c.colid)) COLUMN_NAME,
  1406.         convert(varchar(32),user_name(u.uid)) grantor,
  1407.         convert(varchar(32),user_name(o.uid)) grantee,
  1408.         convert (varchar(32),case v.number
  1409.             when 193 then 'SELECT'
  1410.             when 195 then 'INSERT'
  1411.             when 197 then 'UPDATE'
  1412.             else 'REFERENCES'
  1413.         end) PRIVILEGE,
  1414.         convert(varchar(3),'YES') IS_GRANTABLE
  1415.     from sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  1416.     where
  1417.         c.id = @table_id
  1418.         and c.name like @column_name
  1419.         and c.id = o.id
  1420.         and u.suid = 1        /* grantor is dbo of database */
  1421.         and v.type = 'P'    /* cross product to get all exposed privileges */
  1422.         and v.number in (26,193,195,197)
  1423.         and not exists (    /* exclude revoke'd privileges */
  1424.             select *
  1425.             from sysprotects p1
  1426.             where
  1427.                 p1.protecttype = 206
  1428.                 and p1.action = v.number
  1429.                 and p1.id = o.id
  1430.                 and p1.uid = o.uid)
  1431.     order by 4, 7
  1432. go
  1433.  
  1434.  
  1435.  
  1436. if (charindex('7.00', @@version) = 0)
  1437. begin
  1438.     print ''
  1439.     print ''
  1440.     print 'Warning:'
  1441.     print 'you are installing the stored procedures '
  1442.     print 'on a pre 7.0 SQL Server.'
  1443.     print 'Ignore the following errors.'
  1444. end
  1445. else
  1446.     drop proc sp_column_privileges
  1447. go
  1448.  
  1449. /*    Procedure for 7.00 server */
  1450. CREATE PROCEDURE sp_column_privileges (
  1451.             @table_name         sysname,
  1452.             @table_owner        sysname = null,
  1453.             @table_qualifier    sysname = null,
  1454.             @column_name        nvarchar(384) = null)    /* 3*128 */
  1455. as
  1456.  
  1457.     declare @table_id     int
  1458.  
  1459.     if @column_name is null /*    If column name not supplied, match all */
  1460.         select @column_name = '%'
  1461.  
  1462.     if @table_qualifier is not null
  1463.     begin
  1464.         if db_name() <> @table_qualifier
  1465.         begin    /* If qualifier doesn't match current database */
  1466.             raiserror (15250, -1,-1)
  1467.             return
  1468.         end
  1469.     end
  1470.     if @table_owner is null
  1471.     begin    /* If unqualified table name */
  1472.         select @table_id = object_id(quotename(@table_name))
  1473.     end
  1474.     else
  1475.     begin    /* Qualified table name */
  1476.         if @table_owner = N''
  1477.         begin    /* If empty owner name */
  1478.             select @table_id = 0
  1479.         end
  1480.         else
  1481.         begin
  1482.             select @table_id = object_id(quotename(@table_owner) +
  1483.                 '.' + quotename(@table_name))
  1484.         end
  1485.     end
  1486.  
  1487.     select
  1488.         convert(sysname,db_name()) TABLE_QUALIFIER,
  1489.         convert(sysname,user_name(o.uid)) TABLE_OWNER,
  1490.         @table_name TABLE_NAME,
  1491.         convert(sysname,c.name) COLUMN_NAME,
  1492.         convert(sysname,user_name(p.grantor)) GRANTOR,
  1493.         convert(sysname,user_name(u.uid)) GRANTEE,
  1494.         convert (varchar(32),case p.action
  1495.              when 193 then 'SELECT'
  1496.              when 195 then 'INSERT'
  1497.              when 197 then 'UPDATE'
  1498.              else 'REFERENCES'
  1499.         end) PRIVILEGE,
  1500.         convert (varchar(3),case when p.protecttype = 205 then 'NO'
  1501.             else 'YES'
  1502.         end) IS_GRANTABLE
  1503.     from sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c, sysmembers m
  1504.     where
  1505.         c.id = @table_id
  1506.         and c.name like @column_name
  1507.         and c.id = p.id
  1508.         and c.id = o.id
  1509.         and case substring(p.columns, 1, 1) & 1
  1510.                 when NULL then 255    /* all columns have permission */
  1511.                 when 0 then convert(tinyint, substring(p.columns, v.low, 1))
  1512.                 else (~convert(tinyint, isnull(substring(p.columns, v.low, 1),0)))
  1513.             end
  1514.             & v.high <> 0            /* permission applies to this column */
  1515.         and v.number <= (select count(*) from syscolumns
  1516.             where id = @table_id)    /* ranges from 1 to # of columns in table */
  1517.         and v.type = 'P'
  1518.         and v.number = c.colid
  1519.             /* expand groups - AKUNDONE: only 1 level of group unrolling here. Need more?? */
  1520.         and (u.uid > 0 and u.uid < 16384)
  1521.         and ((p.uid = u.uid) or 
  1522.              (p.uid = m.groupuid and u.uid = m.memberuid))
  1523.         and p.protecttype <> 206    /* only grant rows */
  1524.         and p.action in (26,193,195,197)
  1525.         and o.uid <> u.uid            /* no rows for owner */
  1526.         and not exists (            /* exclude revoke'd privileges */
  1527.             select *
  1528.             from sysprotects p1
  1529.             where
  1530.                 p1.protecttype = 206
  1531.                 and p1.action = p.action
  1532.                 and p1.id = p.id
  1533.                 and p1.uid = u.uid
  1534.                 and case substring(p1.columns, 1, 1) & 1
  1535.                         when NULL then 255    /* all columns have permission */
  1536.                         when 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  1537.                                             else (~convert(tinyint,isnull(substring(p.columns, v.low, 1),0)))
  1538.                     end
  1539.                     & v.high <> 0)            /* permission applies to this column */
  1540.     union all
  1541.     select    /*    Add rows for table owner */
  1542.         convert(sysname,db_name()) TABLE_QUALIFIER,
  1543.         convert(sysname,user_name(o.uid)) TABLE_OWNER,
  1544.         @table_name TABLE_NAME,
  1545.         convert(sysname,col_name(@table_id, c.colid)) COLUMN_NAME,
  1546.         convert(sysname,user_name(u.uid)) grantor,
  1547.         convert(sysname,user_name(o.uid)) grantee,
  1548.         convert (varchar(32),case v.number
  1549.             when 193 then 'SELECT'
  1550.             when 195 then 'INSERT'
  1551.             when 197 then 'UPDATE'
  1552.             else 'REFERENCES'
  1553.         end) PRIVILEGE,
  1554.         convert(varchar(3),'YES') IS_GRANTABLE
  1555.     from sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  1556.     where
  1557.         c.id = @table_id
  1558.         and c.name like @column_name
  1559.         and c.id = o.id
  1560.         and u.uid = 1        /* grantor is 'dbo' of database */
  1561.         and v.type = 'P'    /* cross product to get all exposed privileges */
  1562.         and v.number in (26,193,195,197)
  1563.         and not exists (    /* exclude revoke'd privileges */
  1564.             select *
  1565.             from sysprotects p1
  1566.             where
  1567.                 p1.protecttype = 206
  1568.                 and p1.action = v.number
  1569.                 and p1.id = o.id
  1570.                 and p1.uid = o.uid)
  1571.     order by 4, 7
  1572. go
  1573.  
  1574.  
  1575. grant execute on sp_column_privileges to public
  1576. go
  1577.  
  1578. dump tran master with no_log
  1579. go
  1580.  
  1581. print 'creating sp_columns'
  1582. go
  1583.  
  1584. /*    Procedure for pre-6.0 server */
  1585. CREATE PROCEDURE sp_columns (
  1586.                  @table_name        varchar(96),
  1587.                  @table_owner        varchar(96) = null,
  1588.                  @table_qualifier    varchar(32) = null,
  1589.                  @column_name        varchar(96) = null,
  1590.                  @ODBCVer            int = 2)
  1591. AS
  1592.     DECLARE @full_table_name    varchar(193)
  1593.     DECLARE @table_id int
  1594.  
  1595.     if @ODBCVer <> 3
  1596.         select @ODBCVer = 2
  1597.     if @column_name is null /*    If column name not supplied, match all */
  1598.         select @column_name = '%'
  1599.     if @table_qualifier is not null
  1600.     begin
  1601.         if db_name() <> @table_qualifier
  1602.         begin    /* If qualifier doesn't match current database */
  1603.             raiserror 20001 '~~Rush_5~~'
  1604.             return
  1605.         end
  1606.     end
  1607.     if @table_name is null
  1608.     begin    /*    If table name not supplied, match all */
  1609.         select @table_name = '%'
  1610.     end
  1611.     if @table_owner is null
  1612.     begin    /* If unqualified table name */
  1613.         SELECT @full_table_name = @table_name
  1614.     end
  1615.     else
  1616.     begin    /* Qualified table name */
  1617.         SELECT @full_table_name = @table_owner + '.' + @table_name
  1618.     end
  1619.  
  1620.     /*    Get Object ID */
  1621.     SELECT @table_id = object_id(@full_table_name)
  1622.     if ((charindex('%',@full_table_name) = 0) and
  1623.         (charindex('_',@full_table_name) = 0) and
  1624.         @table_id <> 0)
  1625.     begin
  1626.         /* this block is for the case where there is no pattern
  1627.              matching required for the table name */
  1628.         SELECT
  1629.             TABLE_QUALIFIER = convert(varchar(32),DB_NAME()),
  1630.             TABLE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  1631.             TABLE_NAME = convert(varchar(32),o.name),
  1632.             COLUMN_NAME = convert(varchar(32),c.name),
  1633.             d.DATA_TYPE,
  1634.             TYPE_NAME = t.name,
  1635.             "PRECISION" = isnull(d.data_precision, convert(int,c.length)),
  1636.             LENGTH = isnull(d.length, convert(int,c.length)),
  1637.             SCALE = d.numeric_scale,
  1638.             d.RADIX,
  1639.             NULLABLE =    /* set nullability from status flag */
  1640.                 convert(smallint, convert(bit, c.status&8)),
  1641.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  1642.             COLUMN_DEF = text,
  1643.             d.SQL_DATA_TYPE,
  1644.             d.SQL_DATETIME_SUB,
  1645.             CHAR_OCTET_LENGTH = isnull(d.data_precision, convert(int,c.length))+d.charbin,
  1646.             ORDINAL_POSITION = convert(int,c.colid),
  1647.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO      YES',(c.status&8)+1,3))),
  1648.             SS_DATA_TYPE = c.type
  1649.         FROM
  1650.             syscolumns c,
  1651.             sysobjects o,
  1652.             syscomments m,
  1653.             master.dbo.spt_datatype_info d,
  1654.             systypes t
  1655.         WHERE
  1656.             o.id = @table_id
  1657.             AND c.id = o.id
  1658.             AND t.type = d.ss_dtype
  1659.             AND c.length = isnull(d.fixlen, c.length)
  1660.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  1661.             AND o.type <> 'P'
  1662.             AND c.usertype = t.usertype
  1663.             AND c.name like @column_name
  1664.             AND c.cdefault *= m.id
  1665.             AND m.colid = 1
  1666.         ORDER BY 17
  1667.     end
  1668.     else
  1669.     begin
  1670.         /* this block is for the case where there IS pattern
  1671.              matching done on the table name */
  1672.         if @table_owner is null /*    If owner not supplied, match all */
  1673.             select @table_owner = '%'
  1674.         SELECT
  1675.             TABLE_QUALIFIER = convert(varchar(32),DB_NAME()),
  1676.             TABLE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  1677.             TABLE_NAME = convert(varchar(32),o.name),
  1678.             COLUMN_NAME = convert(varchar(32),c.name),
  1679.             d.DATA_TYPE,
  1680.             TYPE_NAME = t.name,
  1681.             "PRECISION" = isnull(d.data_precision, convert(int,c.length)),
  1682.             LENGTH = isnull(d.length, convert(int,c.length)),
  1683.             SCALE = d.numeric_scale,
  1684.             d.RADIX,
  1685.             NULLABLE =    /* set nullability from status flag */
  1686.                 convert(smallint, convert(bit, c.status&8)),
  1687.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  1688.             COLUMN_DEF = text,
  1689.             d.SQL_DATA_TYPE,
  1690.             d.SQL_DATETIME_SUB,
  1691.             CHAR_OCTET_LENGTH = isnull(d.data_precision, convert(int,c.length))+d.charbin,
  1692.             ORDINAL_POSITION = convert(int,c.colid),
  1693.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO      YES',(c.status&8)+1,3))),
  1694.             SS_DATA_TYPE = c.type
  1695.         FROM
  1696.             syscolumns c,
  1697.             sysobjects o,
  1698.             syscomments m,
  1699.             master.dbo.spt_datatype_info d,
  1700.             systypes t
  1701.         WHERE
  1702.             o.name like @table_name
  1703.             AND user_name(o.uid) like @table_owner
  1704.             AND o.id = c.id
  1705.             AND t.type = d.ss_dtype
  1706.             AND c.length = isnull(d.fixlen, c.length)
  1707.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  1708.             AND o.type <> 'P'
  1709.             AND c.usertype = t.usertype
  1710.             AND c.name like @column_name
  1711.             AND c.cdefault *= m.id
  1712.             AND m.colid = 1
  1713.         ORDER BY 2, 3, 17
  1714.     end
  1715. go
  1716.  
  1717. if (charindex('6.00', @@version) = 0 and
  1718.     charindex('6.50', @@version) = 0 and
  1719.     charindex('7.00', @@version) = 0)
  1720. begin
  1721.     print ''
  1722.     print ''
  1723.     print 'Warning:'
  1724.     print 'you are installing the stored procedures '
  1725.     print 'on a pre 6.0 SQL Server.'
  1726.     print 'Ignore the following error.'
  1727. end
  1728. else
  1729.     drop proc sp_columns
  1730. go
  1731.  
  1732. /*    Procedure for 6.0 and 6.50 server */
  1733. CREATE PROCEDURE sp_columns (
  1734.                  @table_name        varchar(96),
  1735.                  @table_owner        varchar(96) = null,
  1736.                  @table_qualifier    varchar(32) = null,
  1737.                  @column_name        varchar(96) = null,
  1738.                  @ODBCVer            int = 2)
  1739. AS
  1740.     DECLARE @full_table_name    varchar(193)
  1741.     DECLARE @table_id int
  1742.  
  1743.     if @ODBCVer <> 3
  1744.         select @ODBCVer = 2
  1745.     if @column_name is null /*    If column name not supplied, match all */
  1746.         select @column_name = '%'
  1747.     if @table_qualifier is not null
  1748.     begin
  1749.         if db_name() <> @table_qualifier
  1750.         begin    /* If qualifier doesn't match current database */
  1751.             raiserror (15250, -1,-1)
  1752.             return
  1753.         end
  1754.     end
  1755.     if @table_name is null
  1756.     begin    /*    If table name not supplied, match all */
  1757.         select @table_name = '%'
  1758.     end
  1759.     if @table_owner is null
  1760.     begin    /* If unqualified table name */
  1761.         SELECT @full_table_name = @table_name
  1762.     end
  1763.     else
  1764.     begin    /* Qualified table name */
  1765.         SELECT @full_table_name = @table_owner + '.' + @table_name
  1766.     end
  1767.  
  1768.     /*    Get Object ID */
  1769.     SELECT @table_id = object_id(@full_table_name)
  1770.     if ((charindex('%',@full_table_name) = 0) and
  1771.         (charindex('[',@full_table_name) = 0) and
  1772.         (charindex('_',@full_table_name) = 0) and
  1773.         @table_id <> 0)
  1774.     begin
  1775.         /* this block is for the case where there is no pattern
  1776.             matching required for the table name */
  1777.         SELECT
  1778.             TABLE_QUALIFIER = convert(varchar(32),DB_NAME()),
  1779.             TABLE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  1780.             TABLE_NAME = convert(varchar(32),o.name),
  1781.             COLUMN_NAME = convert(varchar(32),c.name),
  1782.             d.DATA_TYPE,
  1783.             convert(varchar(32),case
  1784.                 when t.usertype > 100 or t.usertype in (18,80) then t.name
  1785.                 else d.TYPE_NAME
  1786.             end) TYPE_NAME,
  1787.             convert(int,case
  1788.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  1789.                 else isnull(convert(int,c.prec), 2147483647)
  1790.             end) "PRECISION",
  1791.             convert(int,case
  1792.                 when d.ss_dtype IN (106, 108, 55, 63) then    /* decimal/numeric types */
  1793.                     c.prec+2
  1794.                 else
  1795.                     isnull(d.length, c.length)
  1796.             end) LENGTH,
  1797.             SCALE = convert(smallint, c.scale),
  1798.             d.RADIX,
  1799.             NULLABLE =    /* set nullability from status flag */
  1800.                 convert(smallint, convert(bit, c.status&8)),
  1801.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  1802.             COLUMN_DEF = text,
  1803.             d.SQL_DATA_TYPE,
  1804.             d.SQL_DATETIME_SUB,
  1805.             CHAR_OCTET_LENGTH = isnull(convert(int,c.prec), 2147483647)+d.charbin,
  1806.             ORDINAL_POSITION = convert(int,c.colid),
  1807.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO      YES',(c.status&8)+1,3))),
  1808.             SS_DATA_TYPE = c.type
  1809.         FROM
  1810.             syscolumns c,
  1811.             sysobjects o,
  1812.             syscomments m,
  1813.             master.dbo.spt_datatype_info d,
  1814.             systypes t
  1815.         WHERE
  1816.             o.id = @table_id
  1817.             AND c.id = o.id
  1818.             AND t.type = d.ss_dtype
  1819.             AND c.length = isnull(d.fixlen, c.length)
  1820.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  1821.             AND o.type <> 'P'
  1822.             AND isnull(d.AUTO_INCREMENT,0) = (c.status&128)/128
  1823.             AND c.usertype = t.usertype
  1824.             AND c.name like @column_name
  1825.             AND c.cdefault *= m.id
  1826.             AND m.colid = 1
  1827.         ORDER BY 17
  1828.     end
  1829.     else
  1830.     begin
  1831.         /* this block is for the case where there IS pattern
  1832.             matching done on the table name */
  1833.         if @table_owner is null /*    If owner not supplied, match all */
  1834.             select @table_owner = '%'
  1835.         SELECT
  1836.             TABLE_QUALIFIER = convert(varchar(32),DB_NAME()),
  1837.             TABLE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  1838.             TABLE_NAME = convert(varchar(32),o.name),
  1839.             COLUMN_NAME = convert(varchar(32),c.name),
  1840.             d.DATA_TYPE,
  1841.             convert(varchar(32),case
  1842.                 when t.usertype > 100 or t.usertype in (18,80) then t.name
  1843.                 else d.TYPE_NAME
  1844.             end) TYPE_NAME,
  1845.             convert(int,case
  1846.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  1847.                 else isnull(convert(int,c.prec), 2147483647)
  1848.             end) "PRECISION",
  1849.             convert(int,case
  1850.                 when d.ss_dtype IN (106, 108, 55, 63) then    /* decimal/numeric types */
  1851.                     c.prec+2
  1852.                 else
  1853.                     isnull(d.length, c.length)
  1854.             end) LENGTH,
  1855.             SCALE = convert(smallint, c.scale),
  1856.             d.RADIX,
  1857.             NULLABLE =    /* set nullability from status flag */
  1858.                 convert(smallint, convert(bit, c.status&8)),
  1859.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  1860.             COLUMN_DEF = text,
  1861.             d.SQL_DATA_TYPE,
  1862.             d.SQL_DATETIME_SUB,
  1863.             CHAR_OCTET_LENGTH = isnull(convert(int,c.prec), 2147483647)+d.charbin,
  1864.             ORDINAL_POSITION = convert(int,c.colid),
  1865.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO      YES',(c.status&8)+1,3))),
  1866.             SS_DATA_TYPE = c.type
  1867.         FROM
  1868.             syscolumns c,
  1869.             sysobjects o,
  1870.             syscomments m,
  1871.             master.dbo.spt_datatype_info d,
  1872.             systypes t
  1873.         WHERE
  1874.             o.name like @table_name
  1875.             AND user_name(o.uid) like @table_owner
  1876.             AND o.id = c.id
  1877.             AND t.type = d.ss_dtype
  1878.             AND c.length = isnull(d.fixlen, c.length)
  1879.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  1880.             AND o.type <> 'P'
  1881.             AND isnull(d.AUTO_INCREMENT,0) = (c.status&128)/128
  1882.             AND c.usertype = t.usertype
  1883.             AND c.name like @column_name
  1884.             AND c.cdefault *= m.id
  1885.             AND m.colid = 1
  1886.         ORDER BY 2, 3, 17
  1887.     end
  1888. go
  1889.  
  1890. if (charindex('7.00', @@version) = 0)
  1891. begin
  1892.     print ''
  1893.     print ''
  1894.     print 'Warning:'
  1895.     print 'you are installing the stored procedures '
  1896.     print 'on a pre 7.0 SQL Server.'
  1897.     print 'Ignore the following errors.'
  1898. end
  1899. else
  1900.     drop proc sp_columns
  1901. go
  1902.  
  1903. /*    Procedure for 7.0 server */
  1904. CREATE PROCEDURE sp_columns (
  1905.                  @table_name        nvarchar(384),
  1906.                  @table_owner        nvarchar(384) = null,
  1907.                  @table_qualifier    sysname = null,
  1908.                  @column_name        nvarchar(384) = null,
  1909.                  @ODBCVer            int = 2)
  1910. AS
  1911.     DECLARE @full_table_name    nvarchar(769)
  1912.     DECLARE @table_id int
  1913.  
  1914.     if @ODBCVer <> 3
  1915.         select @ODBCVer = 2
  1916.     if @column_name is null /*    If column name not supplied, match all */
  1917.         select @column_name = '%'
  1918.     if @table_qualifier is not null
  1919.     begin
  1920.         if db_name() <> @table_qualifier
  1921.         begin    /* If qualifier doesn't match current database */
  1922.             raiserror (15250, -1,-1)
  1923.             return
  1924.         end
  1925.     end
  1926.     if @table_name is null
  1927.     begin    /*    If table name not supplied, match all */
  1928.         select @table_name = '%'
  1929.     end
  1930.     if @table_owner is null
  1931.     begin    /* If unqualified table name */
  1932.         SELECT @full_table_name = quotename(@table_name)
  1933.     end
  1934.     else
  1935.     begin    /* Qualified table name */
  1936.         if @table_owner = ''
  1937.         begin    /* If empty owner name */
  1938.             SELECT @full_table_name = quotename(@table_owner)
  1939.         end
  1940.         else
  1941.         begin
  1942.             SELECT @full_table_name = quotename(@table_owner) +
  1943.                 '.' + quotename(@table_name)
  1944.         end
  1945.     end
  1946.  
  1947.     /*    Get Object ID */
  1948.     SELECT @table_id = object_id(@full_table_name)
  1949.     if ((isnull(charindex('%', @full_table_name),0) = 0) and
  1950.         (isnull(charindex('[', @table_name),0) = 0) and
  1951.         (isnull(charindex('[', @table_owner),0) = 0) and
  1952.         (isnull(charindex('_', @full_table_name),0) = 0) and
  1953.         @table_id <> 0)
  1954.     begin
  1955.         /* this block is for the case where there is no pattern
  1956.             matching required for the table name */
  1957.         SELECT
  1958.             TABLE_QUALIFIER = convert(sysname,DB_NAME()),
  1959.             TABLE_OWNER = convert(sysname,USER_NAME(o.uid)),
  1960.             TABLE_NAME = convert(sysname,o.name),
  1961.             COLUMN_NAME = convert(sysname,c.name),
  1962.             d.DATA_TYPE,
  1963.             convert (sysname,case
  1964.                 when t.xusertype > 255 then t.name
  1965.                 else d.TYPE_NAME
  1966.             end) TYPE_NAME,
  1967.             convert(int,case
  1968.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  1969.                 else OdbcPrec(c.xtype,c.length,c.xprec)
  1970.             end) "PRECISION",
  1971.             convert(int,case
  1972.                 when type_name(d.ss_dtype) IN ('numeric','decimal') then    /* decimal/numeric types */
  1973.                     OdbcPrec(c.xtype,c.length,c.xprec)+2
  1974.                 else
  1975.                     isnull(d.length, c.length)
  1976.             end) LENGTH,
  1977.             SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
  1978.             d.RADIX,
  1979.             NULLABLE = convert(smallint, ColumnProperty (c.id, c.name, 'AllowsNull')),
  1980.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  1981.             COLUMN_DEF = text,
  1982.             d.SQL_DATA_TYPE,
  1983.             d.SQL_DATETIME_SUB,
  1984.             CHAR_OCTET_LENGTH = isnull(d.length, c.length)+d.charbin,
  1985.             ORDINAL_POSITION = convert(int,c.colid),
  1986.             IS_NULLABLE = convert(varchar(254),
  1987.                 substring('NO YES',(ColumnProperty (c.id, c.name, 'AllowsNull')*3)+1,3)),
  1988.             SS_DATA_TYPE = c.type
  1989.         FROM
  1990.             sysobjects o,
  1991.             master.dbo.spt_datatype_info d,
  1992.             systypes t,
  1993.             syscolumns c
  1994.             LEFT OUTER JOIN syscomments m on c.cdefault = m.id
  1995.                 AND m.colid = 1
  1996.         WHERE
  1997.             o.id = @table_id
  1998.             AND c.id = o.id
  1999.             AND t.xtype = d.ss_dtype
  2000.             AND c.length = isnull(d.fixlen, c.length)
  2001.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2002.             AND o.type <> 'P'
  2003.             AND isnull(d.AUTO_INCREMENT,0) = isnull(ColumnProperty (c.id, c.name, 'IsIdentity'),0)
  2004.             AND c.xusertype = t.xusertype
  2005.             AND c.name like @column_name
  2006.         ORDER BY 17
  2007.     end
  2008.     else
  2009.     begin
  2010.         /* this block is for the case where there IS pattern
  2011.             matching done on the table name */
  2012.         if @table_owner is null /*    If owner not supplied, match all */
  2013.             select @table_owner = '%'
  2014.         SELECT
  2015.             TABLE_QUALIFIER = convert(sysname,DB_NAME()),
  2016.             TABLE_OWNER = convert(sysname,USER_NAME(o.uid)),
  2017.             TABLE_NAME = convert(sysname,o.name),
  2018.             COLUMN_NAME = convert(sysname,c.name),
  2019.             d.DATA_TYPE,
  2020.             convert (sysname,case
  2021.                 when t.xusertype > 255 then t.name
  2022.                 else d.TYPE_NAME
  2023.             end) TYPE_NAME,
  2024.             convert(int,case
  2025.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  2026.                 else OdbcPrec(c.xtype,c.length,c.xprec)
  2027.             end) "PRECISION",
  2028.             convert(int,case
  2029.                 when type_name(d.ss_dtype) IN ('numeric','decimal') then    /* decimal/numeric types */
  2030.                     OdbcPrec(c.xtype,c.length,c.xprec)+2
  2031.                 else
  2032.                     isnull(d.length, c.length)
  2033.             end) LENGTH,
  2034.             SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
  2035.             d.RADIX,
  2036.             NULLABLE = convert(smallint, ColumnProperty (c.id, c.name, 'AllowsNull')),
  2037.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  2038.             COLUMN_DEF = text,
  2039.             d.SQL_DATA_TYPE,
  2040.             d.SQL_DATETIME_SUB,
  2041.             CHAR_OCTET_LENGTH = isnull(d.length, c.length)+d.charbin,
  2042.             ORDINAL_POSITION = convert(int,c.colid),
  2043.             IS_NULLABLE = convert(varchar(254),
  2044.                 rtrim(substring('NO YES',(ColumnProperty (c.id, c.name, 'AllowsNull')*3)+1,3))),
  2045.             SS_DATA_TYPE = c.type
  2046.         FROM
  2047.             sysobjects o,
  2048.             master.dbo.spt_datatype_info d,
  2049.             systypes t,
  2050.             syscolumns c
  2051.             LEFT OUTER JOIN syscomments m on c.cdefault = m.id
  2052.                 AND m.colid = 1
  2053.         WHERE
  2054.             o.name like @table_name
  2055.             AND user_name(o.uid) like @table_owner
  2056.             AND o.id = c.id
  2057.             AND t.xtype = d.ss_dtype
  2058.             AND c.length = isnull(d.fixlen, c.length)
  2059.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2060.             AND o.type <> 'P'
  2061.             AND isnull(d.AUTO_INCREMENT,0) = isnull(ColumnProperty (c.id, c.name, 'IsIdentity'),0)
  2062.             AND c.xusertype = t.xusertype
  2063.             AND c.name like @column_name
  2064.         ORDER BY 2, 3, 17
  2065.     end
  2066. go
  2067.  
  2068. grant execute on sp_columns to public
  2069. go
  2070.  
  2071. dump tran master with no_log
  2072. go
  2073.  
  2074. print 'creating sp_databases'
  2075. go
  2076.  
  2077. /*    Procedure for pre-7.0 server */
  2078. create proc sp_databases
  2079. as
  2080.     set nocount on
  2081.     /* Use temporary table to sum up database size w/o using group by */
  2082.     create table #databases (
  2083.                   DATABASE_NAME varchar(32) NOT NULL,
  2084.                   size int NOT NULL)
  2085.  
  2086.     /* Insert row for each database */
  2087.     insert into #databases
  2088.         select
  2089.             name,
  2090.             (select sum(size) from master.dbo.sysusages
  2091.                 where dbid = d.dbid)
  2092.         from master.dbo.sysdatabases d
  2093.  
  2094.     select
  2095.          DATABASE_NAME,
  2096.          DATABASE_SIZE = size*2,    /* Convert from 2048 byte pages to K */
  2097.          REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  2098.     from #databases
  2099.     order by 1
  2100. go
  2101.  
  2102. if (charindex('7.00', @@version) = 0)
  2103. begin
  2104.     print ''
  2105.     print ''
  2106.     print 'Warning:'
  2107.     print 'you are installing the stored procedures '
  2108.     print 'on a pre 7.0 SQL Server.'
  2109.     print 'Ignore the following errors.'
  2110. end
  2111. else
  2112.     drop proc sp_databases
  2113. go
  2114.  
  2115. /*    Procedure for 7.0 server */
  2116. create proc sp_databases
  2117. as
  2118.     set nocount on
  2119.     declare @name sysname
  2120.     declare @SQL  nvarchar(600)
  2121.  
  2122.     /* Use temporary table to sum up database size w/o using group by */
  2123.     create table #databases (
  2124.                   DATABASE_NAME sysname NOT NULL,
  2125.                   size int NOT NULL)
  2126.  
  2127.     declare c1 cursor for 
  2128.         select name from master.dbo.sysdatabases
  2129.  
  2130.     open c1
  2131.     fetch c1 into @name
  2132.  
  2133.     while @@fetch_status >= 0
  2134.     begin
  2135.         select @SQL = 'insert into #databases
  2136.                 select N'''+ @name + ''', sum(size) from '
  2137.                 + QuoteName(@name) + '.dbo.sysfiles'
  2138.         /* Insert row for each database */
  2139.         execute (@SQL)
  2140.         fetch c1 into @name
  2141.     end
  2142.     deallocate c1
  2143.  
  2144.     select    
  2145.         DATABASE_NAME,
  2146.         DATABASE_SIZE = size*8,/* Convert from 8192 byte pages to K */
  2147.         REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  2148.     from #databases
  2149.     order by 1
  2150. go
  2151.  
  2152. grant execute on sp_databases to public
  2153. go
  2154.  
  2155. dump tran master with no_log
  2156. go
  2157.  
  2158. print 'creating sp_datatype_info'
  2159. go
  2160.  
  2161. /*    Procedure for pre-6.0 server */
  2162. create proc sp_datatype_info
  2163.     (@data_type int = 0, @ODBCVer tinyint = 2)
  2164. as
  2165.     if @ODBCVer <> 3
  2166.         select @ODBCVer = 2
  2167.     if @data_type = 0
  2168.         select
  2169.             TYPE_NAME = t.name,
  2170.             d.DATA_TYPE,
  2171.             "PRECISION" = isnull(d.data_precision, convert(int,t.length)),
  2172.             d.LITERAL_PREFIX,
  2173.             d.LITERAL_SUFFIX,
  2174.             e.CREATE_PARAMS,
  2175.             d.NULLABLE,
  2176.             d.CASE_SENSITIVE,
  2177.             d.SEARCHABLE,
  2178.             d.UNSIGNED_ATTRIBUTE,
  2179.             d.MONEY,
  2180.             d.AUTO_INCREMENT,
  2181.             LOCAL_TYPE_NAME = t.name,
  2182.             MINIMUM_SCALE = d.numeric_scale,
  2183.             MAXIMUM_SCALE = d.numeric_scale,
  2184.             d.SQL_DATA_TYPE,
  2185.             d.SQL_DATETIME_SUB,
  2186.             NUM_PREC_RADIX = convert(int,d.RADIX),
  2187.             INTERVAL_PRECISION = convert(smallint,NULL),
  2188.             USERTYPE = t.usertype
  2189.         from master.dbo.spt_datatype_info d, master.dbo.spt_datatype_info_ext e, systypes t
  2190.         where
  2191.             d.ss_dtype = t.type
  2192.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2193.             and t.usertype *= e.user_type
  2194.             and t.type not in (111,109,38,110)    /* get rid of nullable types */
  2195.         order by 2, 12, 11, t.usertype
  2196.  
  2197.     else
  2198.         select
  2199.             TYPE_NAME = t.name,
  2200.             d.DATA_TYPE,
  2201.             "PRECISION" = isnull(d.data_precision, convert(int,t.length)),
  2202.             d.LITERAL_PREFIX,
  2203.             d.LITERAL_SUFFIX,
  2204.             e.CREATE_PARAMS,
  2205.             d.NULLABLE,
  2206.             d.CASE_SENSITIVE,
  2207.             d.SEARCHABLE,
  2208.             d.UNSIGNED_ATTRIBUTE,
  2209.             d.MONEY,
  2210.             d.AUTO_INCREMENT,
  2211.             LOCAL_TYPE_NAME = t.name,
  2212.             MINIMUM_SCALE = d.numeric_scale,
  2213.             MAXIMUM_SCALE = d.numeric_scale,
  2214.             d.SQL_DATA_TYPE,
  2215.             d.SQL_DATETIME_SUB,
  2216.             NUM_PREC_RADIX = convert(int,d.RADIX),
  2217.             INTERVAL_PRECISION = convert(smallint,NULL),
  2218.             USERTYPE = t.usertype
  2219.         from master.dbo.spt_datatype_info d, master.dbo.spt_datatype_info_ext e, systypes t
  2220.         where
  2221.             DATA_TYPE = @data_type
  2222.             and d.ss_dtype = t.type
  2223.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2224.             and t.usertype *= e.user_type
  2225.             and t.type not in (111,109,38,110)    /* get rid of nullable types */
  2226.         order by 12, 11, t.usertype
  2227.  
  2228. go
  2229.  
  2230. if (charindex('6.00', @@version) = 0 and
  2231.     charindex('6.50', @@version) = 0 and
  2232.     charindex('7.00', @@version) = 0)
  2233. begin
  2234.     print ''
  2235.     print ''
  2236.     print 'Warning:'
  2237.     print 'you are installing the stored procedures '
  2238.     print 'on a pre 6.0 SQL Server.'
  2239.     print 'Ignore the following errors.'
  2240. end
  2241. else
  2242.     drop proc sp_datatype_info
  2243. go
  2244.  
  2245. /*    Procedure for 6.0 and 6.50 servers */
  2246. create proc sp_datatype_info
  2247.     (@data_type int = 0, @ODBCVer tinyint = 2)
  2248. as
  2249.     if @ODBCVer <> 3
  2250.         select @ODBCVer = 2
  2251.     if @data_type = 0
  2252.         select
  2253.             convert(varchar(32),case
  2254.                 when t.usertype > 100 or t.usertype in (18,80) then t.name
  2255.                 else d.TYPE_NAME
  2256.             end) TYPE_NAME,
  2257.             d.DATA_TYPE,
  2258.             convert(int,case
  2259.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  2260.                 when d.ss_dtype in (35,34) then 2147483647                /* TEXT/IMAGE */
  2261.                 when d.ss_dtype in (55,63,106,108) then @@max_precision /* DECIMAL/NUMERIC */
  2262.                 else t.prec
  2263.             end) "PRECISION",
  2264.             d.LITERAL_PREFIX,
  2265.             d.LITERAL_SUFFIX,
  2266.             e.CREATE_PARAMS,
  2267.             convert(smallint,case
  2268.                 when d.AUTO_INCREMENT = 1 then 0 /* IDENTITY*/
  2269.                 else t.allownulls
  2270.             end) NULLABLE,
  2271.             d.CASE_SENSITIVE,
  2272.             d.SEARCHABLE,
  2273.             d.UNSIGNED_ATTRIBUTE,
  2274.             d.MONEY,
  2275.             d.AUTO_INCREMENT,
  2276.             convert(varchar(32),case
  2277.                 when t.usertype > 100 or t.usertype in (18,80) then t.name
  2278.                 else d.TYPE_NAME
  2279.             end) LOCAL_TYPE_NAME,
  2280.             MINIMUM_SCALE = d.numeric_scale,
  2281.             convert(smallint,case
  2282.                 when d.ss_dtype in (106,108) and d.AUTO_INCREMENT = 0 then @@max_precision /* DECIMAL/NUMERIC */
  2283.                 when d.ss_dtype in (106,108) and d.AUTO_INCREMENT = 1 then 0 /* DECIMAL/NUMERIC IDENTITY*/
  2284.                 else t.scale
  2285.             end) MAXIMUM_SCALE,
  2286.             d.SQL_DATA_TYPE,
  2287.             d.SQL_DATETIME_SUB,
  2288.             NUM_PREC_RADIX = convert(int,d.RADIX),
  2289.             INTERVAL_PRECISION = convert(smallint,NULL),
  2290.             USERTYPE = t.usertype
  2291.         from master.dbo.spt_datatype_info d, master.dbo.spt_datatype_info_ext e, systypes t
  2292.         where
  2293.             d.ss_dtype = t.type
  2294.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2295.             and t.usertype *= e.user_type
  2296.             and isnull(d.AUTO_INCREMENT,0) *= e.AUTO_INCREMENT
  2297.             and t.type not in (111,109,38,110,55,63)    /* get rid of nullable types */
  2298.         order by 2, 12, 11, t.usertype
  2299.  
  2300.     else
  2301.         select
  2302.             convert(varchar(32),case
  2303.                 when t.usertype > 100 or t.usertype in (18,80) then t.name
  2304.                 else d.TYPE_NAME
  2305.             end) TYPE_NAME,
  2306.             d.DATA_TYPE,
  2307.             convert(int,case
  2308.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  2309.                 when d.ss_dtype in (35,34) then 2147483647                /* TEXT/IMAGE */
  2310.                 when d.ss_dtype in (55,63,106,108) then @@max_precision /* DECIMAL/NUMERIC */
  2311.                 else t.prec
  2312.             end) "PRECISION",
  2313.             d.LITERAL_PREFIX,
  2314.             d.LITERAL_SUFFIX,
  2315.             e.CREATE_PARAMS,
  2316.             convert(smallint,case
  2317.                 when d.AUTO_INCREMENT = 1 then 0 /* IDENTITY*/
  2318.                 else t.allownulls
  2319.             end) NULLABLE,
  2320.             d.CASE_SENSITIVE,
  2321.             d.SEARCHABLE,
  2322.             d.UNSIGNED_ATTRIBUTE,
  2323.             d.MONEY,
  2324.             d.AUTO_INCREMENT,
  2325.             convert(varchar(32),case
  2326.                 when t.usertype > 100 or t.usertype in (18,80) then t.name
  2327.                 else d.TYPE_NAME
  2328.             end) LOCAL_TYPE_NAME,
  2329.             MINIMUM_SCALE = d.numeric_scale,
  2330.             convert(smallint,case
  2331.                 when d.ss_dtype in (106,108) and d.AUTO_INCREMENT = 0 then @@max_precision /* DECIMAL/NUMERIC */
  2332.                 when d.ss_dtype in (106,108) and d.AUTO_INCREMENT = 1 then 0 /* DECIMAL/NUMERIC IDENTITY*/
  2333.                 else t.scale
  2334.             end) MAXIMUM_SCALE,
  2335.             d.SQL_DATA_TYPE,
  2336.             d.SQL_DATETIME_SUB,
  2337.             NUM_PREC_RADIX = convert(int,d.RADIX),
  2338.             INTERVAL_PRECISION = convert(smallint,NULL),
  2339.             USERTYPE = t.usertype
  2340.         from master.dbo.spt_datatype_info d, master.dbo.spt_datatype_info_ext e, systypes t
  2341.         where
  2342.             d.DATA_TYPE = @data_type
  2343.             and d.ss_dtype = t.type
  2344.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2345.             and t.usertype *= e.user_type
  2346.             and isnull(d.AUTO_INCREMENT,0) *= e.AUTO_INCREMENT
  2347.             and t.type not in (111,109,38,110,55,63)    /* get rid of nullable types */
  2348.         order by 12, 11, t.usertype
  2349. go
  2350.  
  2351. if (charindex('7.00', @@version) = 0)
  2352. begin
  2353.     print ''
  2354.     print ''
  2355.     print 'Warning:'
  2356.     print 'you are installing the stored procedures '
  2357.     print 'on a pre 7.0 SQL Server.'
  2358.     print 'Ignore the following errors.'
  2359. end
  2360. else
  2361.     drop proc sp_datatype_info
  2362. go
  2363.  
  2364. /*    Procedure for 7.0 server */
  2365. create proc sp_datatype_info
  2366.     (@data_type int = 0, @ODBCVer tinyint = 2)
  2367. as
  2368.     declare @mintype int
  2369.     declare @maxtype int
  2370.  
  2371.     if @ODBCVer <> 3
  2372.         select @ODBCVer = 2
  2373.     if @data_type = 0
  2374.     begin
  2375.         select @mintype = -32768
  2376.         select @maxtype = 32767
  2377.     end
  2378.     else
  2379.     begin
  2380.         select @mintype = @data_type
  2381.         select @maxtype = @data_type
  2382.     end
  2383.  
  2384.     select
  2385.         convert(sysname,case
  2386.             when t.xusertype > 255 then t.name
  2387.             else d.TYPE_NAME
  2388.         end) TYPE_NAME,
  2389.         d.DATA_TYPE,
  2390.         convert(int,case
  2391.             when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  2392.             when type_name(d.ss_dtype) IN ('numeric','decimal') then @@max_precision /* DECIMAL/NUMERIC */
  2393.             else OdbcPrec(t.xtype, t.length, t.xprec)
  2394.         end) "PRECISION",
  2395.         d.LITERAL_PREFIX,
  2396.         d.LITERAL_SUFFIX,
  2397.         e.CREATE_PARAMS,
  2398.         convert(smallint,case
  2399.             when d.AUTO_INCREMENT = 1 then 0 /* IDENTITY*/
  2400.             else TypeProperty (t.name, 'AllowsNull')
  2401.         end) NULLABLE,
  2402.         d.CASE_SENSITIVE,
  2403.         d.SEARCHABLE,
  2404.         d.UNSIGNED_ATTRIBUTE,
  2405.         d.MONEY,
  2406.         d.AUTO_INCREMENT,
  2407.         convert(sysname,case
  2408.             when t.xusertype > 255 then t.name
  2409.             else d.LOCAL_TYPE_NAME
  2410.         end) LOCAL_TYPE_NAME,
  2411.         MINIMUM_SCALE = d.numeric_scale,
  2412.         convert(smallint,case
  2413.             when type_name(d.ss_dtype) IN ('numeric','decimal') and d.AUTO_INCREMENT = 0 then @@max_precision /* DECIMAL/NUMERIC */
  2414.             when type_name(d.ss_dtype) IN ('numeric','decimal') and d.AUTO_INCREMENT = 1 then 0 /* DECIMAL/NUMERIC IDENTITY*/
  2415.             else TypeProperty (t.name, 'Scale')
  2416.         end) MAXIMUM_SCALE,
  2417.         d.SQL_DATA_TYPE,
  2418.         d.SQL_DATETIME_SUB,
  2419.         NUM_PREC_RADIX = convert(int,d.RADIX),
  2420.         INTERVAL_PRECISION = convert(smallint,NULL),
  2421.         USERTYPE = t.usertype
  2422.     from master.dbo.spt_datatype_info d
  2423.         INNER JOIN systypes t on d.ss_dtype = t.xtype
  2424.         LEFT OUTER JOIN master.dbo.spt_datatype_info_ext e on
  2425.             t.xusertype = e.user_type
  2426.             and isnull(d.AUTO_INCREMENT,0) = e.AUTO_INCREMENT
  2427.     where
  2428.         d.DATA_TYPE between @mintype and @maxtype
  2429.         and (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2430.     order by 2, 12, 11,
  2431.     case
  2432.         when t.usertype=18 then 255
  2433.         else t.usertype
  2434.     end
  2435.  
  2436. go
  2437.  
  2438. grant execute on sp_datatype_info to public
  2439. go
  2440.  
  2441. dump tran master with no_log
  2442. go
  2443.  
  2444. print 'creating sp_fkeys'
  2445. go
  2446.  
  2447. /*    Procedure for pre-6.0 server */
  2448. CREATE PROCEDURE sp_fkeys(
  2449.                @pktable_name        varchar(32) = null,
  2450.                @pktable_owner        varchar(32) = null,
  2451.                @pktable_qualifier    varchar(32) = null,
  2452.                @fktable_name        varchar(32) = null,
  2453.                @fktable_owner        varchar(32) = null,
  2454.                @fktable_qualifier    varchar(32) = null )
  2455. as
  2456.     set nocount on
  2457.     declare    @order_by_pk int
  2458.  
  2459.     select  @order_by_pk = 0
  2460.  
  2461.     if (@pktable_name is null) and (@fktable_name is null)
  2462.     begin    /* If neither primary key nor foreign key table names given */
  2463.         raiserror 20004 '~~Rush_25~~'
  2464.         return
  2465.     end
  2466.     if @fktable_qualifier is not null
  2467.     begin
  2468.         if db_name() <> @fktable_qualifier
  2469.         begin    /* If qualifier doesn't match current database */
  2470.             raiserror 20001 '~~Rush_26~~'
  2471.             return
  2472.         end
  2473.     end
  2474.     if @pktable_qualifier is not null
  2475.     begin
  2476.         if db_name() <> @pktable_qualifier
  2477.         begin    /* If qualifier doesn't match current database */
  2478.             raiserror 20001 '~~Rush_27~~'
  2479.             return
  2480.         end
  2481.     end
  2482.  
  2483.     if @pktable_name is null
  2484.     begin /*  If table name not supplied, match all */
  2485.         select @pktable_name = '%'
  2486.         select @order_by_pk = 1
  2487.     end
  2488.     if @pktable_owner is null    /*    If PK owner not supplied, match all */
  2489.         select @pktable_owner = '%'
  2490.     if @fktable_name is null    /*    If table name not supplied, match all */
  2491.         select @fktable_name = '%'
  2492.     if @fktable_owner is null    /*    If FK owner not supplied, match all */
  2493.         select @fktable_owner = '%'
  2494.  
  2495.     if @@trancount <> 0
  2496.     begin    /* If inside a transaction */
  2497.         raiserror 20003 '~~Rush_28~~'
  2498.         return
  2499.     end
  2500.     create table #fkeys(
  2501.              PKTABLE_QUALIFIER    varchar(32) NULL,
  2502.              PKTABLE_OWNER        varchar(32) NULL,
  2503.              PKTABLE_NAME        varchar(32) NOT NULL,
  2504.              PKCOLUMN_NAME        varchar(32) NOT NULL,
  2505.              FKTABLE_QUALIFIER    varchar(32) NULL,
  2506.              FKTABLE_OWNER        varchar(32) NULL,
  2507.              FKTABLE_NAME        varchar(32) NOT NULL,
  2508.              FKCOLUMN_NAME        varchar(32) NOT NULL,
  2509.              KEY_SEQ            smallint NOT NULL)
  2510.  
  2511.     /*    SQL Server supports upto 8 PK/FK relationships between 2 tables */
  2512.     /*    Process syskeys for each relationship */
  2513.     /*    The inserts below adds a row to the temp table for each of the
  2514.         8 possible relationships */
  2515.     insert into #fkeys
  2516.         select
  2517.             db_name(),
  2518.             user_name(o1.uid),
  2519.             object_name(k.depid),
  2520.             c2.name,
  2521.             db_name(),
  2522.             user_name(o2.uid),
  2523.             object_name(k.id),
  2524.             c1.name,
  2525.             1
  2526.         from
  2527.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  2528.         where
  2529.             c1.id = k.id
  2530.             and k.type = 2    /* Foreign type key */
  2531.             and c1.colid = k.key1
  2532.             and c2.id = k.depid
  2533.             and c2.colid = k.depkey1
  2534.             and o1.id = k.depid
  2535.             and o2.id = k.id
  2536.     union all
  2537.         select
  2538.             db_name(),
  2539.             user_name(o1.uid),
  2540.             object_name(k.depid),
  2541.             c2.name,
  2542.             db_name(),
  2543.             user_name(o2.uid),
  2544.             object_name(k.id),
  2545.             c1.name,
  2546.             2
  2547.         from
  2548.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  2549.         where
  2550.             c1.id = k.id
  2551.             and k.type = 2    /* Foreign type key */
  2552.             and c1.colid = k.key2
  2553.             and c2.id = k.depid
  2554.             and c2.colid = k.depkey2
  2555.             and o1.id = k.depid
  2556.             and o2.id = k.id
  2557.     union all
  2558.         select
  2559.             db_name(),
  2560.             user_name(o1.uid),
  2561.             object_name(k.depid),
  2562.             c2.name,
  2563.             db_name(),
  2564.             user_name(o2.uid),
  2565.             object_name(k.id),
  2566.             c1.name,
  2567.             3
  2568.         from
  2569.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  2570.         where
  2571.             c1.id = k.id
  2572.             and k.type = 2    /* Foreign type key */
  2573.             and c1.colid = k.key3
  2574.             and c2.id = k.depid
  2575.             and c2.colid = k.depkey3
  2576.             and o1.id = k.depid
  2577.             and o2.id = k.id
  2578.     union all
  2579.         select
  2580.             db_name(),
  2581.             user_name(o1.uid),
  2582.             object_name(k.depid),
  2583.             c2.name,
  2584.             db_name(),
  2585.             user_name(o2.uid),
  2586.             object_name(k.id),
  2587.             c1.name,
  2588.             4
  2589.         from
  2590.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  2591.         where
  2592.             c1.id = k.id
  2593.             and k.type = 2    /* Foreign type key */
  2594.             and c1.colid = k.key4
  2595.             and c2.id = k.depid
  2596.             and c2.colid = k.depkey4
  2597.             and o1.id = k.depid
  2598.             and o2.id = k.id
  2599.     union all
  2600.         select
  2601.             db_name(),
  2602.             user_name(o1.uid),
  2603.             object_name(k.depid),
  2604.             c2.name,
  2605.             db_name(),
  2606.             user_name(o2.uid),
  2607.             object_name(k.id),
  2608.             c1.name,
  2609.             5
  2610.         from
  2611.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  2612.         where
  2613.             c1.id = k.id
  2614.             and k.type = 2    /* Foreign type key */
  2615.             and c1.colid = k.key5
  2616.             and c2.id = k.depid
  2617.             and c2.colid = k.depkey5
  2618.             and o1.id = k.depid
  2619.             and o2.id = k.id
  2620.     union all
  2621.         select
  2622.             db_name(),
  2623.             user_name(o1.uid),
  2624.             object_name(k.depid),
  2625.             c2.name,
  2626.             db_name(),
  2627.             user_name(o2.uid),
  2628.             object_name(k.id),
  2629.             c1.name,
  2630.             6
  2631.         from
  2632.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  2633.         where
  2634.             c1.id = k.id
  2635.             and k.type = 2    /* Foreign type key */
  2636.             and c1.colid = k.key6
  2637.             and c2.id = k.depid
  2638.             and c2.colid = k.depkey6
  2639.             and o1.id = k.depid
  2640.             and o2.id = k.id
  2641.     union all
  2642.         select
  2643.             db_name(),
  2644.             user_name(o1.uid),
  2645.             object_name(k.depid),
  2646.             c2.name,
  2647.             db_name(),
  2648.             user_name(o2.uid),
  2649.             object_name(k.id),
  2650.             c1.name,
  2651.             7
  2652.         from
  2653.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  2654.         where
  2655.             c1.id = k.id
  2656.             and k.type = 2    /* Foreign type key */
  2657.             and c1.colid = k.key7
  2658.             and c2.id = k.depid
  2659.             and c2.colid = k.depkey7
  2660.             and o1.id = k.depid
  2661.             and o2.id = k.id
  2662.     union all
  2663.         select
  2664.             db_name(),
  2665.             user_name(o1.uid),
  2666.             object_name(k.depid),
  2667.             c2.name,
  2668.             db_name(),
  2669.             user_name(o2.uid),
  2670.             object_name(k.id),
  2671.             c1.name,
  2672.             8
  2673.         from
  2674.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  2675.         where
  2676.             c1.id = k.id
  2677.             and k.type = 2    /* Foreign type key */
  2678.             and c1.colid = k.key8
  2679.             and c2.id = k.depid
  2680.             and c2.colid = k.depkey8
  2681.             and o1.id = k.depid
  2682.             and o2.id = k.id
  2683.  
  2684.     if @order_by_pk = 1 /*    If order by PK fields */
  2685.         select
  2686.             PKTABLE_QUALIFIER,
  2687.             PKTABLE_OWNER,
  2688.             PKTABLE_NAME,
  2689.             PKCOLUMN_NAME,
  2690.             FKTABLE_QUALIFIER,
  2691.             FKTABLE_OWNER,
  2692.             FKTABLE_NAME,
  2693.             FKCOLUMN_NAME,
  2694.             KEY_SEQ,
  2695.             UPDATE_RULE = convert(smallint, null),
  2696.             DELETE_RULE = convert(smallint,null),
  2697.             FK_NAME = convert(varchar(32),null),
  2698.             PK_NAME = convert(varchar(32),null),
  2699.             DEFERRABILITY = convert(smallint,7)
  2700.         from #fkeys
  2701.         where FKTABLE_NAME like @fktable_name
  2702.             and FKTABLE_OWNER like @fktable_owner
  2703.             and PKTABLE_NAME  like @pktable_name
  2704.             and PKTABLE_OWNER like @pktable_owner
  2705.         order by 1, 2, 3, 9
  2706.     else        /*    Order by FK fields */
  2707.         select
  2708.             PKTABLE_QUALIFIER,
  2709.             PKTABLE_OWNER,
  2710.             PKTABLE_NAME,
  2711.             PKCOLUMN_NAME,
  2712.             FKTABLE_QUALIFIER,
  2713.             FKTABLE_OWNER,
  2714.             FKTABLE_NAME,
  2715.             FKCOLUMN_NAME,
  2716.             KEY_SEQ,
  2717.             UPDATE_RULE = convert(smallint,null),
  2718.             DELETE_RULE = convert(smallint,null),
  2719.             FK_NAME = convert(varchar(32),null),
  2720.             PK_NAME = convert(varchar(32),null),
  2721.             DEFERRABILITY = convert(smallint,7)
  2722.         from #fkeys
  2723.         where FKTABLE_NAME like @fktable_name
  2724.             and FKTABLE_OWNER like @fktable_owner
  2725.             and PKTABLE_NAME  like @pktable_name
  2726.             and PKTABLE_OWNER like @pktable_owner
  2727.         order by 5, 6, 7, 9
  2728. go
  2729.  
  2730. if (charindex('6.00', @@version) = 0 and
  2731.     charindex('6.50', @@version) = 0 and
  2732.     charindex('7.00', @@version) = 0)
  2733. begin
  2734.     print ''
  2735.     print ''
  2736.     print 'Warning:'
  2737.     print 'you are installing the stored procedures '
  2738.     print 'on a pre 6.0 SQL Server.'
  2739.     print 'Ignore the following errors.'
  2740. end
  2741. else
  2742.     drop proc sp_fkeys
  2743. go
  2744.  
  2745. /*    Procedure for 6.0 and 6.50 servers */
  2746. CREATE PROCEDURE sp_fkeys(
  2747.                @pktable_name        varchar(32) = null,
  2748.                @pktable_owner        varchar(32) = null,
  2749.                @pktable_qualifier    varchar(32) = null,
  2750.                @fktable_name        varchar(32) = null,
  2751.                @fktable_owner        varchar(32) = null,
  2752.                @fktable_qualifier    varchar(32) = null )
  2753. as
  2754.     set nocount on
  2755.     DECLARE @pktable_id            int
  2756.     DECLARE @pkfull_table_name    varchar(65) /* 2*32 +1 */
  2757.     DECLARE @fktable_id            int
  2758.     DECLARE @fkfull_table_name    varchar(65) /* 2*32 +1 */
  2759.     declare    @order_by_pk        int
  2760.  
  2761.     select  @order_by_pk = 0
  2762.  
  2763.     if (@pktable_name is null) and (@fktable_name is null)
  2764.     begin    /* If neither primary key nor foreign key table names given */
  2765.         raiserror (15252,-1,-1)
  2766.         return
  2767.     end
  2768.     if @fktable_qualifier is not null
  2769.     begin
  2770.         if db_name() <> @fktable_qualifier
  2771.         begin    /* If qualifier doesn't match current database */
  2772.             raiserror (15250, -1,-1)
  2773.             return
  2774.         end
  2775.     end
  2776.     if @pktable_qualifier is not null
  2777.     begin
  2778.         if db_name() <> @pktable_qualifier
  2779.         begin    /* If qualifier doesn't match current database */
  2780.             raiserror (15250, -1,-1)
  2781.             return
  2782.         end
  2783.     end
  2784.  
  2785.     if @pktable_owner is null
  2786.     begin    /* If unqualified primary key table name */
  2787.         SELECT @pkfull_table_name = @pktable_name
  2788.     end
  2789.     else
  2790.     begin    /* Qualified primary key table name */
  2791.         SELECT @pkfull_table_name = @pktable_owner + '.' + @pktable_name
  2792.     end
  2793.     /*    Get Object ID */
  2794.     SELECT @pktable_id = object_id(@pkfull_table_name)
  2795.  
  2796.     if @fktable_owner is null
  2797.     begin    /* If unqualified foreign key table name */
  2798.         SELECT @fkfull_table_name = @fktable_name
  2799.     end
  2800.     else
  2801.     begin    /* Qualified foreign key table name */
  2802.         SELECT @fkfull_table_name = @fktable_owner + '.' + @fktable_name
  2803.     end
  2804.     /*    Get Object ID */
  2805.     SELECT @fktable_id = object_id(@fkfull_table_name)
  2806.  
  2807.     if @fktable_name is not null
  2808.     begin
  2809.         if @fktable_id is null
  2810.             SELECT @fktable_id = 0    /* fk table not found, empty result */
  2811.     end
  2812.  
  2813.     if @pktable_name is null
  2814.     begin /*  If table name not supplied, match all */
  2815.         select @order_by_pk = 1
  2816.     end
  2817.     else
  2818.     begin
  2819.         if @pktable_id is null
  2820.         begin
  2821.             SELECT @pktable_id = 0    /* pk table not found, empty result */
  2822.         end
  2823.     end
  2824.  
  2825.     if (@@trancount <> 0 and
  2826.         charindex('6.50', @@version) = 0)
  2827.     begin    /* If inside a transaction */
  2828.         raiserror (15002,-1,-1,'sp_fkeys')
  2829.         return
  2830.     end
  2831.  
  2832.     create table #fkeys(
  2833.              pkdb_id        int NOT NULL,
  2834.              pktable_id     int NOT NULL,
  2835.              pkcolid        int NOT NULL,
  2836.              fkdb_id        int NOT NULL,
  2837.              fktable_id        int NOT NULL,
  2838.              fkcolid        int NOT NULL,
  2839.              KEY_SEQ        smallint NOT NULL,
  2840.              fk_id            int NOT NULL,
  2841.              pk_id            int NOT NULL)
  2842.  
  2843.     /*    SQL Server supports upto 16 PK/FK relationships between 2 tables */
  2844.     /*    Process syskeys for each relationship */
  2845.     /*    The inserts below adds a row to the temp table for each of the
  2846.         16 possible relationships */
  2847.     insert into #fkeys
  2848.         select
  2849.             r.rkeydbid,
  2850.             r.rkeyid,
  2851.             r.rkey1,
  2852.             r.fkeydbid,
  2853.             r.fkeyid,
  2854.             r.fkey1,
  2855.             1,
  2856.             r.constid,
  2857.             s.constid
  2858.         from
  2859.             sysreferences r, sysconstraints s
  2860.         where    r.rkeyid = s.id
  2861.             AND (s.status & 0xf) = 1
  2862.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  2863.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  2864.       union all
  2865.         select
  2866.             r.rkeydbid,
  2867.             r.rkeyid,
  2868.             r.rkey2,
  2869.             r.fkeydbid,
  2870.             r.fkeyid,
  2871.             r.fkey2,
  2872.             2,
  2873.             r.constid,
  2874.             s.constid
  2875.         from
  2876.             sysreferences r, sysconstraints s
  2877.         where    r.rkeyid = s.id
  2878.             AND (s.status & 0xf) = 1
  2879.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  2880.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  2881.       union all
  2882.         select
  2883.             r.rkeydbid,
  2884.             r.rkeyid,
  2885.             r.rkey3,
  2886.             r.fkeydbid,
  2887.             r.fkeyid,
  2888.             r.fkey3,
  2889.             3,
  2890.             r.constid,
  2891.             s.constid
  2892.         from
  2893.             sysreferences r, sysconstraints s
  2894.         where    r.rkeyid = s.id
  2895.             AND (s.status & 0xf) = 1
  2896.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  2897.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  2898.       union all
  2899.         select
  2900.             r.rkeydbid,
  2901.             r.rkeyid,
  2902.             r.rkey4,
  2903.             r.fkeydbid,
  2904.             r.fkeyid,
  2905.             r.fkey4,
  2906.             4,
  2907.             r.constid,
  2908.             s.constid
  2909.         from
  2910.             sysreferences r, sysconstraints s
  2911.         where    r.rkeyid = s.id
  2912.             AND (s.status & 0xf) = 1
  2913.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  2914.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  2915.       union all
  2916.         select
  2917.             r.rkeydbid,
  2918.             r.rkeyid,
  2919.             r.rkey5,
  2920.             r.fkeydbid,
  2921.             r.fkeyid,
  2922.             r.fkey5,
  2923.             5,
  2924.             r.constid,
  2925.             s.constid
  2926.         from
  2927.             sysreferences r, sysconstraints s
  2928.         where    r.rkeyid = s.id
  2929.             AND (s.status & 0xf) = 1
  2930.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  2931.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  2932.       union all
  2933.         select
  2934.             r.rkeydbid,
  2935.             r.rkeyid,
  2936.             r.rkey6,
  2937.             r.fkeydbid,
  2938.             r.fkeyid,
  2939.             r.fkey6,
  2940.             6,
  2941.             r.constid,
  2942.             s.constid
  2943.         from
  2944.             sysreferences r, sysconstraints s
  2945.         where    r.rkeyid = s.id
  2946.             AND (s.status & 0xf) = 1
  2947.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  2948.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  2949.       union all
  2950.         select
  2951.             r.rkeydbid,
  2952.             r.rkeyid,
  2953.             r.rkey7,
  2954.             r.fkeydbid,
  2955.             r.fkeyid,
  2956.             r.fkey7,
  2957.             7,
  2958.             r.constid,
  2959.             s.constid
  2960.         from
  2961.             sysreferences r, sysconstraints s
  2962.         where    r.rkeyid = s.id
  2963.             AND (s.status & 0xf) = 1
  2964.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  2965.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  2966.       union all
  2967.         select
  2968.             r.rkeydbid,
  2969.             r.rkeyid,
  2970.             r.rkey8,
  2971.             r.fkeydbid,
  2972.             r.fkeyid,
  2973.             r.fkey8,
  2974.             8,
  2975.             r.constid,
  2976.             s.constid
  2977.         from
  2978.             sysreferences r, sysconstraints s
  2979.         where    r.rkeyid = s.id
  2980.             AND (s.status & 0xf) = 1
  2981.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  2982.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  2983.       union all
  2984.         select
  2985.             r.rkeydbid,
  2986.             r.rkeyid,
  2987.             r.rkey9,
  2988.             r.fkeydbid,
  2989.             r.fkeyid,
  2990.             r.fkey9,
  2991.             9,
  2992.             r.constid,
  2993.             s.constid
  2994.         from
  2995.             sysreferences r, sysconstraints s
  2996.         where    r.rkeyid = s.id
  2997.             AND (s.status & 0xf) = 1
  2998.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  2999.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3000.       union all
  3001.         select
  3002.             r.rkeydbid,
  3003.             r.rkeyid,
  3004.             r.rkey10,
  3005.             r.fkeydbid,
  3006.             r.fkeyid,
  3007.             r.fkey10,
  3008.             10,
  3009.             r.constid,
  3010.             s.constid
  3011.         from
  3012.             sysreferences r, sysconstraints s
  3013.         where    r.rkeyid = s.id
  3014.             AND (s.status & 0xf) = 1
  3015.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3016.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3017.       union all
  3018.         select
  3019.             r.rkeydbid,
  3020.             r.rkeyid,
  3021.             r.rkey11,
  3022.             r.fkeydbid,
  3023.             r.fkeyid,
  3024.             r.fkey11,
  3025.             11,
  3026.             r.constid,
  3027.             s.constid
  3028.         from
  3029.             sysreferences r, sysconstraints s
  3030.         where    r.rkeyid = s.id
  3031.             AND (s.status & 0xf) = 1
  3032.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3033.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3034.       union all
  3035.         select
  3036.             r.rkeydbid,
  3037.             r.rkeyid,
  3038.             r.rkey12,
  3039.             r.fkeydbid,
  3040.             r.fkeyid,
  3041.             r.fkey12,
  3042.             12,
  3043.             r.constid,
  3044.             s.constid
  3045.         from
  3046.             sysreferences r, sysconstraints s
  3047.         where    r.rkeyid = s.id
  3048.             AND (s.status & 0xf) = 1
  3049.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3050.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3051.       union all
  3052.         select
  3053.             r.rkeydbid,
  3054.             r.rkeyid,
  3055.             r.rkey13,
  3056.             r.fkeydbid,
  3057.             r.fkeyid,
  3058.             r.fkey13,
  3059.             13,
  3060.             r.constid,
  3061.             s.constid
  3062.         from
  3063.             sysreferences r, sysconstraints s
  3064.         where    r.rkeyid = s.id
  3065.             AND (s.status & 0xf) = 1
  3066.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3067.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3068.       union all
  3069.         select
  3070.             r.rkeydbid,
  3071.             r.rkeyid,
  3072.             r.rkey14,
  3073.             r.fkeydbid,
  3074.             r.fkeyid,
  3075.             r.fkey14,
  3076.             14,
  3077.             r.constid,
  3078.             s.constid
  3079.         from
  3080.             sysreferences r, sysconstraints s
  3081.         where    r.rkeyid = s.id
  3082.             AND (s.status & 0xf) = 1
  3083.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3084.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3085.       union all
  3086.         select
  3087.             r.rkeydbid,
  3088.             r.rkeyid,
  3089.             r.rkey15,
  3090.             r.fkeydbid,
  3091.             r.fkeyid,
  3092.             r.fkey15,
  3093.             15,
  3094.             r.constid,
  3095.             s.constid
  3096.         from
  3097.             sysreferences r, sysconstraints s
  3098.         where    r.rkeyid = s.id
  3099.             AND (s.status & 0xf) = 1
  3100.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3101.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3102.       union all
  3103.         select
  3104.             r.rkeydbid,
  3105.             r.rkeyid,
  3106.             r.rkey16,
  3107.             r.fkeydbid,
  3108.             r.fkeyid,
  3109.             r.fkey16,
  3110.             16,
  3111.             r.constid,
  3112.             s.constid
  3113.         from
  3114.             sysreferences r, sysconstraints s
  3115.         where    r.rkeyid = s.id
  3116.             AND (s.status & 0xf) = 1
  3117.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3118.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3119.  
  3120.     if @order_by_pk = 1 /*    If order by PK fields */
  3121.         select
  3122.             PKTABLE_QUALIFIER = convert(varchar(32),DB_NAME(f.pkdb_id)),
  3123.             PKTABLE_OWNER = convert(varchar(32),USER_NAME(o1.uid)),
  3124.             PKTABLE_NAME = convert(varchar(32),o1.name),
  3125.             PKCOLUMN_NAME = convert(varchar(32),c1.name),
  3126.             FKTABLE_QUALIFIER = convert(varchar(32),DB_NAME(f.fkdb_id)),
  3127.             FKTABLE_OWNER = convert(varchar(32),USER_NAME(o2.uid)),
  3128.             FKTABLE_NAME = convert(varchar(32),o2.name),
  3129.             FKCOLUMN_NAME = convert(varchar(32),c2.name),
  3130.             KEY_SEQ,
  3131.             UPDATE_RULE = convert(smallint,1),
  3132.             DELETE_RULE = convert(smallint,1),
  3133.             FK_NAME = convert(varchar(32),OBJECT_NAME(fk_id)),
  3134.             PK_NAME = convert(varchar(32),OBJECT_NAME(pk_id)),
  3135.             DEFERRABILITY = convert(smallint,7)
  3136.         from #fkeys f,
  3137.             sysobjects o1, sysobjects o2,
  3138.             syscolumns c1, syscolumns c2
  3139.         where    o1.id = f.pktable_id
  3140.             AND o2.id = f.fktable_id
  3141.             AND c1.id = f.pktable_id
  3142.             AND c2.id = f.fktable_id
  3143.             AND c1.colid = f.pkcolid
  3144.             AND c2.colid = f.fkcolid
  3145.         order by 1,2,3,9
  3146.     else        /*    Order by FK fields */
  3147.         select
  3148.             PKTABLE_QUALIFIER = convert(varchar(32),DB_NAME(f.pkdb_id)),
  3149.             PKTABLE_OWNER = convert(varchar(32),USER_NAME(o1.uid)),
  3150.             PKTABLE_NAME = convert(varchar(32),o1.name),
  3151.             PKCOLUMN_NAME = convert(varchar(32),c1.name),
  3152.             FKTABLE_QUALIFIER = convert(varchar(32),DB_NAME(f.fkdb_id)),
  3153.             FKTABLE_OWNER = convert(varchar(32),USER_NAME(o2.uid)),
  3154.             FKTABLE_NAME = convert(varchar(32),o2.name),
  3155.             FKCOLUMN_NAME = convert(varchar(32),c2.name),
  3156.             KEY_SEQ,
  3157.             UPDATE_RULE = convert(smallint,1),
  3158.             DELETE_RULE = convert(smallint,1),
  3159.             FK_NAME = convert(varchar(32),OBJECT_NAME(fk_id)),
  3160.             PK_NAME = convert(varchar(32),OBJECT_NAME(pk_id)),
  3161.             DEFERRABILITY = convert(smallint,7)
  3162.         from #fkeys f,
  3163.             sysobjects o1, sysobjects o2,
  3164.             syscolumns c1, syscolumns c2
  3165.         where    o1.id = f.pktable_id
  3166.             AND o2.id = f.fktable_id
  3167.             AND c1.id = f.pktable_id
  3168.             AND c2.id = f.fktable_id
  3169.             AND c1.colid = f.pkcolid
  3170.             AND c2.colid = f.fkcolid
  3171.         order by 5,6,7,9
  3172. go
  3173.  
  3174. if (charindex('7.00', @@version) = 0)
  3175. begin
  3176.     print ''
  3177.     print ''
  3178.     print 'Warning:'
  3179.     print 'you are installing the stored procedures '
  3180.     print 'on a pre 7.0 SQL Server.'
  3181.     print 'Ignore the following errors.'
  3182. end
  3183. else
  3184.     drop proc sp_fkeys
  3185. go
  3186.  
  3187. /*    Procedure for 7.0 and later servers */
  3188. CREATE PROCEDURE sp_fkeys(
  3189.                @pktable_name        sysname = null,
  3190.                @pktable_owner        sysname = null,
  3191.                @pktable_qualifier    sysname = null,
  3192.                @fktable_name        sysname = null,
  3193.                @fktable_owner        sysname = null,
  3194.                @fktable_qualifier    sysname = null )
  3195. as
  3196.     set nocount on
  3197.     DECLARE @pktable_id            int
  3198.     DECLARE @pkfull_table_name    nvarchar(257) /* 2*128 + 1 */
  3199.     DECLARE @fktable_id            int
  3200.     DECLARE @fkfull_table_name    nvarchar(257) /* 2*128 + 1 */
  3201.     declare    @order_by_pk        int
  3202.  
  3203.     /* select 'XXX starting table creation' */
  3204.  
  3205.     create table #fkeysall(
  3206.             rkeyid int NOT NULL,
  3207.             rkey1 int NOT NULL,
  3208.                 rkey2 int NOT NULL,
  3209.                 rkey3 int NOT NULL,
  3210.                 rkey4 int NOT NULL,
  3211.                 rkey5 int NOT NULL,
  3212.                 rkey6 int NOT NULL,
  3213.                 rkey7 int NOT NULL,
  3214.                 rkey8 int NOT NULL,
  3215.                 rkey9 int NOT NULL,
  3216.                 rkey10 int NOT NULL,
  3217.                 rkey11 int NOT NULL,
  3218.                 rkey12 int NOT NULL,
  3219.                 rkey13 int NOT NULL,
  3220.                 rkey14 int NOT NULL,
  3221.                 rkey15 int NOT NULL,
  3222.                 rkey16 int NOT NULL,
  3223.             fkeyid int NOT NULL,
  3224.             fkey1 int NOT NULL,
  3225.                 fkey2 int NOT NULL,
  3226.                 fkey3 int NOT NULL,
  3227.                 fkey4 int NOT NULL,
  3228.                 fkey5 int NOT NULL,
  3229.                 fkey6 int NOT NULL,
  3230.                 fkey7 int NOT NULL,
  3231.                 fkey8 int NOT NULL,
  3232.                 fkey9 int NOT NULL,
  3233.                 fkey10 int NOT NULL,
  3234.                 fkey11 int NOT NULL,
  3235.                 fkey12 int NOT NULL,
  3236.                 fkey13 int NOT NULL,
  3237.                 fkey14 int NOT NULL,
  3238.                 fkey15 int NOT NULL,
  3239.                 fkey16 int NOT NULL,
  3240.             constid int NOT NULL,
  3241.             name sysname NOT NULL)
  3242.  
  3243.     create table #fkeys(
  3244.             pktable_id        int NOT NULL,
  3245.             pkcolid         int NOT NULL,
  3246.             fktable_id        int NOT NULL,
  3247.             fkcolid         int NOT NULL,
  3248.             KEY_SEQ         smallint NOT NULL,
  3249.             fk_id            int NOT NULL,
  3250.             PK_NAME            sysname NOT NULL)
  3251.  
  3252.     create table #fkeysout(
  3253.             PKTABLE_QUALIFIER sysname NULL,
  3254.             PKTABLE_OWNER sysname NULL,
  3255.             PKTABLE_NAME sysname NOT NULL,
  3256.             PKCOLUMN_NAME sysname NOT NULL,
  3257.             FKTABLE_QUALIFIER sysname NULL,
  3258.             FKTABLE_OWNER sysname NULL,
  3259.             FKTABLE_NAME sysname NOT NULL,
  3260.             FKCOLUMN_NAME sysname NOT NULL,
  3261.             KEY_SEQ smallint NOT NULL,
  3262.             UPDATE_RULE smallint NULL,
  3263.             DELETE_RULE smallint NULL,
  3264.             FK_NAME sysname NULL,
  3265.             PK_NAME sysname NULL,
  3266.             DEFERRABILITY smallint null)
  3267.  
  3268.     /* select 'XXX starting parameter analysis' */
  3269.  
  3270.     select  @order_by_pk = 0
  3271.  
  3272.     if (@pktable_name is null) and (@fktable_name is null)
  3273.     begin    /* If neither primary key nor foreign key table names given */
  3274.         raiserror (15252,-1,-1)
  3275.         return
  3276.     end
  3277.     if @fktable_qualifier is not null
  3278.     begin
  3279.         if db_name() <> @fktable_qualifier
  3280.         begin    /* If qualifier doesn't match current database */
  3281.             raiserror (15250, -1,-1)
  3282.             return
  3283.         end
  3284.     end
  3285.     if @pktable_qualifier is not null
  3286.     begin
  3287.         if db_name() <> @pktable_qualifier
  3288.         begin    /* If qualifier doesn't match current database */
  3289.             raiserror (15250, -1,-1)
  3290.             return
  3291.         end
  3292.     end
  3293.  
  3294.     if @pktable_owner is null
  3295.     begin    /* If unqualified primary key table name */
  3296.         SELECT @pkfull_table_name = quotename(@pktable_name)
  3297.     end
  3298.     else
  3299.     begin    /* Qualified primary key table name */
  3300.         if @pktable_owner = ''
  3301.         begin    /* If empty owner name */
  3302.             SELECT @pkfull_table_name = quotename(@pktable_owner)
  3303.         end
  3304.         else
  3305.         begin
  3306.             SELECT @pkfull_table_name = quotename(@pktable_owner) +
  3307.                 '.' + quotename(@pktable_name)
  3308.         end
  3309.     end
  3310.     /*    Get Object ID */
  3311.     SELECT @pktable_id = object_id(@pkfull_table_name)
  3312.  
  3313.     if @fktable_owner is null
  3314.     begin    /* If unqualified foreign key table name */
  3315.         SELECT @fkfull_table_name = quotename(@fktable_name)
  3316.     end
  3317.     else
  3318.     begin    /* Qualified foreign key table name */
  3319.         if @fktable_owner = ''
  3320.         begin    /* If empty owner name */
  3321.             SELECT @fkfull_table_name = quotename(@fktable_owner)
  3322.         end
  3323.         else
  3324.         begin
  3325.             SELECT @fkfull_table_name = quotename(@fktable_owner) +
  3326.                 '.' + quotename(@fktable_name)
  3327.         end
  3328.     end
  3329.     /*    Get Object ID */
  3330.     SELECT @fktable_id = object_id(@fkfull_table_name)
  3331.  
  3332.     if @fktable_name is not null
  3333.     begin
  3334.         if @fktable_id is null
  3335.             SELECT @fktable_id = 0    /* fk table not found, empty result */
  3336.     end
  3337.  
  3338.     if @pktable_name is null
  3339.     begin /*  If table name not supplied, match all */
  3340.         select @order_by_pk = 1
  3341.     end
  3342.     else
  3343.     begin
  3344.         if @pktable_id is null
  3345.         begin
  3346.             SELECT @pktable_id = 0    /* pk table not found, empty result */
  3347.         end
  3348.     end
  3349.  
  3350.     /*    SQL Server supports upto 16 PK/FK relationships between 2 tables */
  3351.     /*    Process syskeys for each relationship */
  3352.     /*  First, attempt to get all 16 keys for each rel'ship, then sort
  3353.         them out with a 16-way "insert select ... union select ..." */
  3354.  
  3355.     /* select 'XXX starting data analysis' */
  3356.  
  3357.     insert into #fkeysall
  3358.         select
  3359.             r.rkeyid,
  3360.             r.rkey1, r.rkey2, r.rkey3, r.rkey4,
  3361.                 r.rkey5, r.rkey6, r.rkey7, r.rkey8,
  3362.                 r.rkey9, r.rkey10, r.rkey11, r.rkey12,
  3363.                 r.rkey13, r.rkey14, r.rkey15, r.rkey16,
  3364.             r.fkeyid,
  3365.             r.fkey1, r.fkey2, r.fkey3, r.fkey4,
  3366.                 r.fkey5, r.fkey6, r.fkey7, r.fkey8,
  3367.                 r.fkey9, r.fkey10, r.fkey11, r.fkey12,
  3368.                 r.fkey13, r.fkey14, r.fkey15, r.fkey16,
  3369.             r.constid,
  3370.             i.name
  3371.         from
  3372.             sysreferences r, sysobjects o, sysindexes i
  3373.         where    r.constid = o.id
  3374.             AND o.xtype = 'F'
  3375.             AND r.rkeyindid = i.indid
  3376.             AND r.rkeyid = i.id
  3377.             AND r.rkeyid between isnull(@pktable_id, 0)
  3378.                             and isnull(@pktable_id, 0x7fffffff)
  3379.             AND r.fkeyid between isnull(@fktable_id, 0)
  3380.                             and isnull(@fktable_id, 0x7fffffff)
  3381.  
  3382.     /* select count (*) as 'XXX countall' from #fkeysall */
  3383.  
  3384.     insert into #fkeys
  3385.             select rkeyid, rkey1, fkeyid, fkey1, 1, constid, name
  3386.             from #fkeysall
  3387.         union all
  3388.             select rkeyid, rkey2, fkeyid, fkey2, 2, constid, name
  3389.             from #fkeysall
  3390.         union all
  3391.             select rkeyid, rkey3, fkeyid, fkey3, 3, constid, name
  3392.             from #fkeysall
  3393.         union all
  3394.             select rkeyid, rkey4, fkeyid, fkey4, 4, constid, name
  3395.             from #fkeysall
  3396.         union all
  3397.             select rkeyid, rkey5, fkeyid, fkey5, 5, constid, name
  3398.             from #fkeysall
  3399.         union all
  3400.             select rkeyid, rkey6, fkeyid, fkey6, 6, constid, name
  3401.             from #fkeysall
  3402.         union all
  3403.             select rkeyid, rkey7, fkeyid, fkey7, 7, constid, name
  3404.             from #fkeysall
  3405.         union all
  3406.             select rkeyid, rkey8, fkeyid, fkey8, 8, constid, name
  3407.             from #fkeysall
  3408.         union all
  3409.             select rkeyid, rkey9, fkeyid, fkey9, 9, constid, name
  3410.             from #fkeysall
  3411.         union all
  3412.             select rkeyid, rkey10, fkeyid, fkey10, 10, constid, name
  3413.             from #fkeysall
  3414.         union all
  3415.             select rkeyid, rkey11, fkeyid, fkey11, 11, constid, name
  3416.             from #fkeysall
  3417.         union all
  3418.             select rkeyid, rkey12, fkeyid, fkey12, 12, constid, name
  3419.             from #fkeysall
  3420.         union all
  3421.             select rkeyid, rkey13, fkeyid, fkey13, 13, constid, name
  3422.             from #fkeysall
  3423.         union all
  3424.             select rkeyid, rkey14, fkeyid, fkey14, 14, constid, name
  3425.             from #fkeysall
  3426.         union all
  3427.             select rkeyid, rkey15, fkeyid, fkey15, 15, constid, name
  3428.             from #fkeysall
  3429.         union all
  3430.             select rkeyid, rkey16, fkeyid, fkey16, 16, constid, name
  3431.             from #fkeysall
  3432.  
  3433.     /* select count (*) as 'XXX count' from #fkeys */
  3434.  
  3435.     insert into #fkeysout
  3436.         select
  3437.             PKTABLE_QUALIFIER = convert(sysname,db_name()),
  3438.             PKTABLE_OWNER = convert(sysname,USER_NAME(o1.uid)),
  3439.             PKTABLE_NAME = convert(sysname,o1.name),
  3440.             PKCOLUMN_NAME = convert(sysname,c1.name),
  3441.             FKTABLE_QUALIFIER = convert(sysname,db_name()),
  3442.             FKTABLE_OWNER = convert(sysname,USER_NAME(o2.uid)),
  3443.             FKTABLE_NAME = convert(sysname,o2.name),
  3444.             FKCOLUMN_NAME = convert(sysname,c2.name),
  3445.             KEY_SEQ,
  3446.             UPDATE_RULE = convert(smallint,1),
  3447.             DELETE_RULE = convert(smallint,1),
  3448.             FK_NAME = convert(sysname,OBJECT_NAME(fk_id)),
  3449.             PK_NAME,
  3450.             DEFERRABILITY = 7    /* SQL_NOT_DEFERRABLE */
  3451.         from #fkeys f,
  3452.             sysobjects o1, sysobjects o2,
  3453.             syscolumns c1, syscolumns c2
  3454.         where    o1.id = f.pktable_id
  3455.             AND o2.id = f.fktable_id
  3456.             AND c1.id = f.pktable_id
  3457.             AND c2.id = f.fktable_id
  3458.             AND c1.colid = f.pkcolid
  3459.             AND c2.colid = f.fkcolid
  3460.  
  3461.     /* select count (*) as 'XXX countout' from #fkeysout */
  3462.  
  3463.     if @order_by_pk = 1 /*    If order by PK fields */
  3464.         select
  3465.             PKTABLE_QUALIFIER, PKTABLE_OWNER, PKTABLE_NAME, PKCOLUMN_NAME,
  3466.             FKTABLE_QUALIFIER, FKTABLE_OWNER, FKTABLE_NAME, FKCOLUMN_NAME,
  3467.             KEY_SEQ, UPDATE_RULE, DELETE_RULE, FK_NAME, PK_NAME, DEFERRABILITY
  3468.         from #fkeysout
  3469.         order by 1,2,3,9
  3470.     else        /*    Order by FK fields */
  3471.         select
  3472.             PKTABLE_QUALIFIER, PKTABLE_OWNER, PKTABLE_NAME, PKCOLUMN_NAME,
  3473.             FKTABLE_QUALIFIER, FKTABLE_OWNER, FKTABLE_NAME, FKCOLUMN_NAME,
  3474.             KEY_SEQ, UPDATE_RULE, DELETE_RULE, FK_NAME, PK_NAME, DEFERRABILITY
  3475.         from #fkeysout
  3476.         order by 5,6,7,9
  3477. go
  3478.  
  3479. grant execute on sp_fkeys to public
  3480. go
  3481.  
  3482. dump tran master with no_log
  3483. go
  3484.  
  3485. print 'creating sp_pkeys'
  3486. go
  3487.  
  3488. /*    Procedure for pre-6.0 server */
  3489. CREATE PROCEDURE sp_pkeys(
  3490.                @table_name        varchar(32),
  3491.                @table_owner     varchar(32) = null,
  3492.                @table_qualifier varchar(32) = null )
  3493. as
  3494.     set nocount on
  3495.     if @table_qualifier is not null
  3496.     begin
  3497.         if db_name() <> @table_qualifier
  3498.         begin    /* If qualifier doesn't match current database */
  3499.             raiserror 20001 '~~Rush_5~~'
  3500.             return
  3501.         end
  3502.     end
  3503.     if @table_owner is null /*    If owner not supplied, match all */
  3504.         select @table_owner = '%'
  3505.     if @@trancount <> 0
  3506.     begin    /* If inside a transaction */
  3507.         raiserror 20003 '~~Rush_35~~'
  3508.         return
  3509.     end
  3510.  
  3511.     create table #pkeys(
  3512.              TABLE_QUALIFIER varchar(32) NULL,
  3513.              TABLE_OWNER     varchar(32) NULL,
  3514.              TABLE_NAME      varchar(32) NOT NULL,
  3515.              COLUMN_NAME     varchar(32) NOT NULL,
  3516.              KEY_SEQ         smallint NOT NULL)
  3517.  
  3518.     /*    SQL Server supports upto 8 PK/FK relationships between 2 tables */
  3519.     /*    Process syskeys for each relationship */
  3520.     /*    The inserts below adds a row to the temp table for each of the
  3521.         8 possible relationships */
  3522.     insert into #pkeys
  3523.         select
  3524.             db_name(),
  3525.             (select user_name(uid) from sysobjects o where o.id = k.id),
  3526.             object_name(k.id),
  3527.             c.name,
  3528.             1
  3529.         from
  3530.             syskeys k, syscolumns c
  3531.         where
  3532.             c.id = k.id
  3533.             and k.type = 1    /* Primary type key */
  3534.             and c.colid = k.key1
  3535.     if (@@rowcount = 0)
  3536.         goto done
  3537.  
  3538.     insert into #pkeys
  3539.         select
  3540.             db_name(),
  3541.             (select user_name(uid) from sysobjects o where o.id = k.id),
  3542.             object_name(k.id),
  3543.             c.name,
  3544.             2
  3545.         from
  3546.             syskeys k, syscolumns c
  3547.         where
  3548.             c.id = k.id
  3549.             and k.type = 1    /* Primary type key */
  3550.             and c.colid = key2
  3551.     if (@@rowcount = 0)
  3552.         goto done
  3553.  
  3554.     insert into #pkeys
  3555.         select
  3556.             db_name(),
  3557.             (select user_name(uid) from sysobjects o where o.id = k.id),
  3558.             object_name(k.id),
  3559.             c.name,
  3560.             3
  3561.         from
  3562.             syskeys k, syscolumns c
  3563.         where
  3564.             c.id = k.id
  3565.             and k.type = 1    /* Primary type key */
  3566.             and c.colid = key3
  3567.     if (@@rowcount = 0)
  3568.         goto done
  3569.  
  3570.     insert into #pkeys
  3571.         select
  3572.             db_name(),
  3573.             (select user_name(uid) from sysobjects o where o.id = k.id),
  3574.             object_name(k.id),
  3575.             c.name,
  3576.             4
  3577.         from
  3578.             syskeys k, syscolumns c
  3579.         where
  3580.             c.id = k.id
  3581.             and k.type = 1    /* Primary type key */
  3582.             and c.colid = key4
  3583.     if (@@rowcount = 0)
  3584.         goto done
  3585.  
  3586.     insert into #pkeys
  3587.         select
  3588.             db_name(),
  3589.             (select user_name(uid) from sysobjects o where o.id = k.id),
  3590.             object_name(k.id),
  3591.             c.name,
  3592.             5
  3593.         from
  3594.             syskeys k, syscolumns c
  3595.         where
  3596.             c.id = k.id
  3597.             and k.type = 1    /* Primary type key */
  3598.             and c.colid = key5
  3599.     if (@@rowcount = 0)
  3600.         goto done
  3601.  
  3602.     insert into #pkeys
  3603.         select
  3604.             db_name(),
  3605.             (select user_name(uid) from sysobjects o where o.id = k.id),
  3606.             object_name(k.id),
  3607.             c.name,
  3608.             6
  3609.         from
  3610.             syskeys k, syscolumns c
  3611.         where
  3612.             c.id = k.id
  3613.             and k.type = 1    /* Primary type key */
  3614.             and c.colid = key6
  3615.     if (@@rowcount = 0)
  3616.         goto done
  3617.  
  3618.     insert into #pkeys
  3619.         select
  3620.             db_name(),
  3621.             (select user_name(uid) from sysobjects o where o.id = k.id),
  3622.             object_name(k.id),
  3623.             c.name,
  3624.             7
  3625.         from
  3626.             syskeys k, syscolumns c
  3627.         where
  3628.             c.id = k.id
  3629.             and k.type = 1    /* Primary type key */
  3630.             and c.colid = key7
  3631.     if (@@rowcount = 0)
  3632.         goto done
  3633.  
  3634.     insert into #pkeys
  3635.          select
  3636.              db_name(),
  3637.              (select user_name(uid) from sysobjects o where o.id = k.id),
  3638.              object_name(k.id),
  3639.              c.name,
  3640.              8
  3641.          from
  3642.              syskeys k, syscolumns c
  3643.          where
  3644.              c.id = k.id
  3645.              and k.type = 1 /* Primary type key */
  3646.              and c.colid = key8
  3647.  
  3648.     done:
  3649.     select
  3650.         TABLE_QUALIFIER,
  3651.         TABLE_OWNER,
  3652.         TABLE_NAME,
  3653.         COLUMN_NAME,
  3654.         KEY_SEQ,
  3655.         PK_NAME = convert(varchar(32),null)
  3656.     from #pkeys
  3657.     where TABLE_NAME = @table_name
  3658.         and TABLE_OWNER like @table_owner
  3659.     order by 1, 2, 3, 5
  3660. go
  3661.  
  3662. if (charindex('6.00', @@version) = 0 and
  3663.     charindex('6.50', @@version) = 0 and
  3664.     charindex('7.00', @@version) = 0)
  3665. begin
  3666.     print ''
  3667.     print ''
  3668.     print 'Warning:'
  3669.     print 'you are installing the stored procedures '
  3670.     print 'on a pre 6.0 SQL Server.'
  3671.     print 'Ignore the following error.'
  3672. end
  3673. else
  3674.     drop proc sp_pkeys
  3675. go
  3676.  
  3677. /*    Procedure for 6.0 and 6.50 servers */
  3678. CREATE PROCEDURE sp_pkeys(
  3679.                @table_name        sysname,
  3680.                @table_owner     sysname = null,
  3681.                @table_qualifier sysname = null )
  3682. as
  3683.     DECLARE @table_id            int
  3684.     DECLARE @full_table_name    varchar(255) /* 2*128 + 1 */
  3685.  
  3686.     if @table_qualifier is not null
  3687.     begin
  3688.         if db_name() <> @table_qualifier
  3689.         begin    /* If qualifier doesn't match current database */
  3690.             raiserror (15250, -1,-1)
  3691.             return
  3692.         end
  3693.     end
  3694.     if @table_owner is null
  3695.     begin    /* If unqualified table name */
  3696.         SELECT @full_table_name = @table_name
  3697.     end
  3698.     else
  3699.     begin    /* Qualified table name */
  3700.         if @table_owner = ''
  3701.         begin    /* If empty owner name */
  3702.             SELECT @full_table_name = @table_owner
  3703.         end
  3704.         else
  3705.         begin
  3706.             SELECT @full_table_name = @table_owner + '.' + @table_name
  3707.         end
  3708.     end
  3709.     /*    Get Object ID */
  3710.     SELECT @table_id = object_id(@full_table_name)
  3711.  
  3712.     select
  3713.         TABLE_QUALIFIER = convert(sysname,db_name()),
  3714.         TABLE_OWNER = convert(sysname,user_name(o.uid)),
  3715.         TABLE_NAME = convert(sysname,o.name),
  3716.         COLUMN_NAME = convert(sysname,c.name),
  3717.         KEY_SEQ = convert(smallint,c1.colid),
  3718.         PK_NAME = convert(sysname,i.name)
  3719.     from
  3720.         sysindexes i, syscolumns c, sysobjects o, syscolumns c1
  3721.     where
  3722.         o.id = @table_id
  3723.         and o.id = c.id
  3724.         and o.id = i.id
  3725.         and (i.status & 0x800) = 0x800
  3726.         and c.name = index_col (@full_table_name, i.indid, c1.colid)
  3727.         and c1.colid <= i.keycnt    /* create rows from 1 to keycnt */
  3728.         and c1.id = @table_id
  3729.     order by 1, 2, 3, 5
  3730. go
  3731.  
  3732. if (charindex('7.00', @@version) = 0)
  3733. begin
  3734.     print ''
  3735.     print ''
  3736.     print 'Warning:'
  3737.     print 'you are installing the stored procedures '
  3738.     print 'on a pre 7.0 SQL Server.'
  3739.     print 'Ignore the following errors.'
  3740. end
  3741. else
  3742.     drop proc sp_pkeys
  3743. go
  3744.  
  3745. /*    Procedure for 7.0 and later servers */
  3746. CREATE PROCEDURE sp_pkeys(
  3747.                @table_name        sysname,
  3748.                @table_owner     sysname = null,
  3749.                @table_qualifier sysname = null )
  3750. as
  3751.     DECLARE @table_id            int
  3752.     DECLARE @full_table_name    varchar(255) /* 2*128 + 1 */
  3753.  
  3754.     if @table_qualifier is not null
  3755.     begin
  3756.         if db_name() <> @table_qualifier
  3757.         begin    /* If qualifier doesn't match current database */
  3758.             raiserror (15250, -1,-1)
  3759.             return
  3760.         end
  3761.     end
  3762.     if @table_owner is null
  3763.     begin    /* If unqualified table name */
  3764.         SELECT @full_table_name = quotename(@table_name)
  3765.     end
  3766.     else
  3767.     begin    /* Qualified table name */
  3768.         if @table_owner = ''
  3769.         begin    /* If empty owner name */
  3770.             SELECT @full_table_name = quotename(@table_owner)
  3771.         end
  3772.         else
  3773.         begin
  3774.             SELECT @full_table_name = quotename(@table_owner) +
  3775.                 '.' + quotename(@table_name)
  3776.         end
  3777.     end
  3778.     /*    Get Object ID */
  3779.     SELECT @table_id = object_id(@full_table_name)
  3780.  
  3781.     select
  3782.         TABLE_QUALIFIER = convert(sysname,db_name()),
  3783.         TABLE_OWNER = convert(sysname,user_name(o.uid)),
  3784.         TABLE_NAME = convert(sysname,o.name),
  3785.         COLUMN_NAME = convert(sysname,c.name),
  3786.         KEY_SEQ = convert(smallint,c1.colid),
  3787.         PK_NAME = convert(sysname,i.name)
  3788.     from
  3789.         sysindexes i, syscolumns c, sysobjects o, syscolumns c1
  3790.     where
  3791.         o.id = @table_id
  3792.         and o.id = c.id
  3793.         and o.id = i.id
  3794.         and (i.status & 0x800) = 0x800
  3795.         and c.name = index_col (@full_table_name, i.indid, c1.colid)
  3796.         and c1.colid <= i.keycnt    /* create rows from 1 to keycnt */
  3797.         and c1.id = @table_id
  3798.     order by 1, 2, 3, 5
  3799. go
  3800.  
  3801. grant execute on sp_pkeys to public
  3802. go
  3803.  
  3804. dump tran master with no_log
  3805. go
  3806.  
  3807. print 'creating sp_server_info'
  3808. go
  3809.  
  3810. create proc sp_server_info (
  3811.             @attribute_id  int = null)
  3812. as
  3813.     if @attribute_id is not null
  3814.         select *
  3815.         from master.dbo.spt_server_info
  3816.         where attribute_id = @attribute_id
  3817.     else
  3818.         select *
  3819.         from master.dbo.spt_server_info
  3820.         order by attribute_id
  3821. go
  3822.  
  3823. grant execute on sp_server_info to public
  3824. go
  3825.  
  3826. dump tran master with no_log
  3827. go
  3828.  
  3829. print 'creating sp_special_columns'
  3830. go
  3831.  
  3832. /*    Procedure for pre-6.0 server */
  3833. CREATE PROCEDURE sp_special_columns (
  3834.                  @table_name        varchar(32),
  3835.                  @table_owner        varchar(32) = null,
  3836.                  @table_qualifier    varchar(32) = null,
  3837.                  @col_type            char(1) = 'R',
  3838.                  @scope                char(1) = 'T',
  3839.                  @nullable            char(1) = 'U',
  3840.                  @ODBCVer            int = 2)
  3841. AS
  3842.     DECLARE @indid                int
  3843.     DECLARE @table_id            int
  3844.     DECLARE @full_table_name    varchar(65) /* 2*32+1 */
  3845.     DECLARE @scopeout            smallint
  3846.  
  3847.     if @col_type not in ('R','V')
  3848.     begin
  3849.         raiserror 20002 '~~Rush_42~~'
  3850.         return
  3851.     end
  3852.  
  3853.     if @scope = 'C'
  3854.         select @scopeout = 0
  3855.     else if @scope = 'T'
  3856.         select @scopeout = 1
  3857.     else
  3858.     begin
  3859.         raiserror 20002 '~~Rush_43~~'
  3860.         return
  3861.     end
  3862.  
  3863.     if @nullable not in ('U','O')
  3864.     begin
  3865.         raiserror 20002 '~~Rush_44~~'
  3866.         return
  3867.     end
  3868.  
  3869.     if @table_qualifier is not null
  3870.     begin
  3871.         if db_name() <> @table_qualifier
  3872.         begin /* If qualifier doesn't match current database */
  3873.             raiserror 20001 '~~Rush_5~~'
  3874.             return
  3875.         end
  3876.     end
  3877.     if @table_owner is null
  3878.     begin     /* If unqualified table name */
  3879.         SELECT @full_table_name = @table_name
  3880.     end
  3881.     else
  3882.     begin    /* Qualified table name */
  3883.         SELECT @full_table_name = @table_owner + '.' + @table_name
  3884.     end
  3885.     /*    Get Object ID */
  3886.     SELECT @table_id = object_id(@full_table_name)
  3887.  
  3888.     if @col_type = 'V'
  3889.     BEGIN /* if ROWVER, just run that query */
  3890.         SELECT
  3891.             SCOPE = convert(smallint,NULL),
  3892.             COLUMN_NAME = convert(varchar(32),c.name),
  3893.             DATA_TYPE = convert(smallint, -3),
  3894.             TYPE_NAME = t.name,
  3895.             "PRECISION" = convert(int,8),
  3896.             LENGTH = convert(int,8),
  3897.             SCALE = convert(smallint, NULL),
  3898.             PSEUDO_COLUMN = convert(smallint,1)
  3899.         FROM
  3900.             systypes t, syscolumns c
  3901.         WHERE
  3902.             c.id = @table_id
  3903.             AND c.usertype = 80 /*    TIMESTAMP */
  3904.             AND t.usertype = 80 /*    TIMESTAMP */
  3905.         RETURN
  3906.     END
  3907.  
  3908.     /* ROWID, now find the id of the 'best' index for this table */
  3909.  
  3910.     IF @nullable = 'O'    /* Don't include any indexes that contain
  3911.                            nullable columns. */
  3912.  
  3913.             SELECT @indid = MIN(indid)
  3914.                 FROM sysindexes i,syscolumns c,syscolumns c2
  3915.                 WHERE
  3916.                     i.status&2 = 2        /*    If Unique Index */
  3917.                      AND c.id = i.id
  3918.                      AND c2.id = c.id
  3919.                      AND c2.colid < i.keycnt + (i.status&16)/16
  3920.                     AND i.id = @table_id
  3921.                     AND indid > 0        /*    Eliminate Table Row */
  3922.                     AND c.name = index_col(@table_name,i.indid,c2.colid)
  3923.                     GROUP BY indid HAVING SUM(c.status&8) = 0
  3924.  
  3925.     ELSE    /* Include indexes that are partially nullable. */
  3926.  
  3927.         SELECT @indid = MIN(indid)
  3928.             FROM sysindexes i
  3929.             WHERE
  3930.                 status&2 = 2        /*    If Unique Index */
  3931.                 AND id = @table_id
  3932.                 AND indid > 0        /*    Eliminate Table Row */
  3933.  
  3934.     SELECT
  3935.         SCOPE = @scopeout,
  3936.         COLUMN_NAME = convert(varchar(32),INDEX_COL(@full_table_name,indid,c2.colid)),
  3937.         d.DATA_TYPE,
  3938.         TYPE_NAME = t.name,
  3939.         "PRECISION" = isnull(d.data_precision, convert(int,c.length)),
  3940.         LENGTH = isnull(d.length, convert(int,c.length)),
  3941.         SCALE = d.numeric_scale,
  3942.         PSEUDO_COLUMN = convert(smallint,1)
  3943.     FROM
  3944.         sysindexes x,
  3945.         syscolumns c,
  3946.         master.dbo.spt_datatype_info d,
  3947.         systypes t,
  3948.         syscolumns c2    /* Self-join to generate list of index columns and */
  3949.                         /* to extract datatype names */
  3950.     WHERE
  3951.         x.id = @table_id
  3952.         AND c.name = INDEX_COL(@full_table_name,@indid,c2.colid)
  3953.         AND c.id = x.id
  3954.         AND c2.id = x.id
  3955.         AND c2.colid < keycnt+(x.status&16)/16
  3956.         AND x.indid = @indid
  3957.         AND t.type = d.ss_dtype
  3958.         AND c.length = d.fixlen
  3959.         AND c.usertype = t.usertype
  3960.  
  3961. go
  3962.  
  3963. if (charindex('6.00', @@version) = 0 and
  3964.     charindex('6.50', @@version) = 0 and
  3965.     charindex('7.00', @@version) = 0)
  3966. begin
  3967.     print ''
  3968.     print ''
  3969.     print 'Warning:'
  3970.     print 'you are installing the stored procedures '
  3971.     print 'on a pre 6.0 SQL Server.'
  3972.     print 'Ignore the following errors.'
  3973. end
  3974. else
  3975.     drop proc sp_special_columns
  3976. go
  3977.  
  3978. /*    Procedure for 6.0 and 6.50 servers */
  3979. CREATE PROCEDURE sp_special_columns (
  3980.                  @table_name        varchar(32),
  3981.                  @table_owner        varchar(32) = null,
  3982.                  @table_qualifier    varchar(32) = null,
  3983.                  @col_type            char(1) = 'R',
  3984.                  @scope                char(1) = 'T',
  3985.                  @nullable            char(1) = 'U',
  3986.                  @ODBCVer            int = 2)
  3987. AS
  3988.     DECLARE @indid                int
  3989.     DECLARE @table_id            int
  3990.     DECLARE @full_table_name    varchar(65) /* 2*32+1 */
  3991.     DECLARE @scopeout            smallint
  3992.  
  3993.     if @col_type not in ('R','V')
  3994.     begin
  3995.         raiserror (15251,-1,-1,'col_type','''R'' or ''V''')
  3996.         return
  3997.     end
  3998.  
  3999.     if @scope = 'C'
  4000.         select @scopeout = 0
  4001.     else if @scope = 'T'
  4002.         select @scopeout = 1
  4003.     else
  4004.     begin
  4005.         raiserror (15251,-1,-1,'scope','''C'' or ''T''')
  4006.         return
  4007.     end
  4008.  
  4009.     if @nullable not in ('U','O')
  4010.     begin
  4011.         raiserror (15251,-1,-1,'nullable','''U'' or ''O''')
  4012.         return
  4013.     end
  4014.  
  4015.     if @table_qualifier is not null
  4016.     begin
  4017.         if db_name() <> @table_qualifier
  4018.         begin /* If qualifier doesn't match current database */
  4019.             raiserror (15250, -1,-1)
  4020.             return
  4021.         end
  4022.     end
  4023.     if @table_owner is null
  4024.     begin     /* If unqualified table name */
  4025.         SELECT @full_table_name = @table_name
  4026.     end
  4027.     else
  4028.     begin     /* Qualified table name */
  4029.         SELECT @full_table_name = @table_owner + '.' + @table_name
  4030.     end
  4031.     /*    Get Object ID */
  4032.     SELECT @table_id = object_id(@full_table_name)
  4033.  
  4034.     if @col_type = 'V'
  4035.     BEGIN /* if ROWVER, just run that query */
  4036.         SELECT
  4037.             SCOPE = convert(smallint,NULL),
  4038.             COLUMN_NAME = convert(varchar(32),c.name),
  4039.             DATA_TYPE = convert(smallint, -2),
  4040.             TYPE_NAME = t.name,
  4041.             "PRECISION" = convert(int,8),
  4042.             LENGTH = convert(int,8),
  4043.             SCALE = convert(smallint, NULL),
  4044.             PSEUDO_COLUMN = convert(smallint,1)
  4045.         FROM
  4046.             systypes t, syscolumns c
  4047.         WHERE
  4048.             c.id = @table_id
  4049.             AND c.usertype = 80 /*    TIMESTAMP */
  4050.             AND t.usertype = 80 /*    TIMESTAMP */
  4051.         RETURN
  4052.     END
  4053.  
  4054.     /* ROWID, now find the id of the 'best' index for this table */
  4055.  
  4056.     IF @nullable = 'O'    /* Don't include any indexes that contain
  4057.                            nullable columns. */
  4058.  
  4059.         SELECT @indid = MIN(indid)
  4060.             FROM sysindexes i,syscolumns c,syscolumns c2
  4061.             WHERE
  4062.                 i.status&2 = 2        /*    If Unique Index */
  4063.                 AND c.id = i.id
  4064.                 AND c2.id = c.id
  4065.                 AND c2.colid < i.keycnt + (i.status&16)/16
  4066.                 AND i.id = @table_id
  4067.                 AND indid > 0        /*    Eliminate Table Row */
  4068.                 AND c.name = index_col(@table_name,i.indid,c2.colid)
  4069.                 GROUP BY indid HAVING SUM(c.status&8) = 0
  4070.  
  4071.     ELSE    /* Include indexes that are partially nullable. */
  4072.  
  4073.         SELECT @indid = MIN(indid)
  4074.             FROM sysindexes i
  4075.             WHERE
  4076.                 status&2 = 2        /*    If Unique Index */
  4077.                 AND id = @table_id
  4078.                 AND indid > 0        /*    Eliminate Table Row */
  4079.  
  4080.     SELECT
  4081.         SCOPE = @scopeout,
  4082.         COLUMN_NAME = convert(varchar(32),INDEX_COL(@full_table_name,indid,c2.colid)),
  4083.         d.DATA_TYPE,
  4084.         convert(varchar(32),case
  4085.             when (t.usertype > 100 or t.usertype in (18,80))
  4086.                 then t.name
  4087.             else d.TYPE_NAME
  4088.         end) TYPE_NAME,
  4089.         convert(int,case
  4090.             when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  4091.             else isnull(convert(int,c.prec), 2147483647)
  4092.         end) "PRECISION",
  4093.         convert(int,case
  4094.             when d.ss_dtype IN (106, 108, 55, 63) then    /* decimal/numeric types */
  4095.                 convert(int,c.prec+2)
  4096.             else
  4097.                 isnull(d.length, c.length)
  4098.         end) LENGTH,
  4099.         SCALE = convert(smallint, c.scale),
  4100.         PSEUDO_COLUMN = convert(smallint,1)
  4101.     FROM
  4102.         sysindexes x,
  4103.         syscolumns c,
  4104.         master.dbo.spt_datatype_info d,
  4105.         systypes t,
  4106.         syscolumns c2    /* Self-join to generate list of index columns and */
  4107.                         /* to extract datatype names */
  4108.     WHERE
  4109.         x.id = @table_id
  4110.         AND c.name = INDEX_COL(@full_table_name,@indid,c2.colid)
  4111.         AND c.id = x.id
  4112.         AND c2.id = x.id
  4113.         AND c2.colid < x.keycnt+(x.status&16)/16
  4114.         AND x.indid = @indid
  4115.         AND t.type = d.ss_dtype
  4116.         AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  4117.         AND isnull(d.AUTO_INCREMENT,0) = (c.status&128)/128
  4118.         AND c.usertype = t.usertype
  4119. go
  4120.  
  4121. if (charindex('7.00', @@version) = 0)
  4122. begin
  4123.     print ''
  4124.     print ''
  4125.     print 'Warning:'
  4126.     print 'you are installing the stored procedures '
  4127.     print 'on a pre 7.0 SQL Server.'
  4128.     print 'Ignore the following errors.'
  4129. end
  4130. else
  4131.     drop proc sp_special_columns
  4132. go
  4133.  
  4134. /*    Procedure for 7.0 and later servers */
  4135. CREATE PROCEDURE sp_special_columns (
  4136.                  @table_name        sysname,
  4137.                  @table_owner        sysname = null,
  4138.                  @table_qualifier    sysname = null,
  4139.                  @col_type            char(1) = 'R',
  4140.                  @scope                char(1) = 'T',
  4141.                  @nullable            char(1) = 'U',
  4142.                  @ODBCVer            int = 2)
  4143. AS
  4144.     DECLARE @indid                int
  4145.     DECLARE @table_id            int
  4146.     DECLARE @full_table_name    nvarchar(257)
  4147.     DECLARE @scopeout            smallint
  4148.  
  4149.     if @col_type not in ('R','V')
  4150.     begin
  4151.         raiserror (15251,-1,-1,'col_type','''R'' or ''V''')
  4152.         return
  4153.     end
  4154.  
  4155.     if @scope = 'C'
  4156.         select @scopeout = 0
  4157.     else if @scope = 'T'
  4158.         select @scopeout = 1
  4159.     else
  4160.     begin
  4161.         raiserror (15251,-1,-1,'scope','''C'' or ''T''')
  4162.         return
  4163.     end
  4164.  
  4165.     if @nullable not in ('U','O')
  4166.     begin
  4167.         raiserror (15251,-1,-1,'nullable','''U'' or ''O''')
  4168.         return
  4169.     end
  4170.  
  4171.     if @table_qualifier is not null
  4172.     begin
  4173.         if db_name() <> @table_qualifier
  4174.         begin /* If qualifier doesn't match current database */
  4175.             raiserror (15250, -1,-1)
  4176.             return
  4177.         end
  4178.     end
  4179.     if @table_owner is null
  4180.     begin     /* If unqualified table name */
  4181.         SELECT @full_table_name = quotename(@table_name)
  4182.     end
  4183.     else
  4184.     begin    /* Qualified table name */
  4185.         if @table_owner = ''
  4186.         begin    /* If empty owner name */
  4187.             SELECT @full_table_name = quotename(@table_owner)
  4188.         end
  4189.         else
  4190.         begin
  4191.             SELECT @full_table_name = quotename(@table_owner) +
  4192.                 '.' + quotename(@table_name)
  4193.         end
  4194.     end
  4195.     /*    Get Object ID */
  4196.     SELECT @table_id = object_id(@full_table_name)
  4197.  
  4198.     if @col_type = 'V'
  4199.     BEGIN /* if ROWVER, just run that query */
  4200.         SELECT
  4201.             SCOPE = convert(smallint,NULL),
  4202.             COLUMN_NAME = convert(sysname,c.name),
  4203.             DATA_TYPE = convert(smallint, -2),
  4204.             TYPE_NAME = t.name,
  4205.             "PRECISION" = convert(int,8),
  4206.             LENGTH = convert(int,8),
  4207.             SCALE = convert(smallint, NULL),
  4208.             PSEUDO_COLUMN = convert(smallint,1)
  4209.         FROM
  4210.             systypes t, syscolumns c
  4211.         WHERE
  4212.             not (@table_id is null)
  4213.             AND c.id = @table_id
  4214.             AND t.name = 'timestamp'    /*    TIMESTAMP  */
  4215.             AND t.xtype = c.xtype        
  4216.             AND t.xusertype = c.xusertype    
  4217.         RETURN
  4218.     END
  4219.  
  4220.     /* ROWID, now find the id of the 'best' index for this table */
  4221.  
  4222.     IF @nullable = 'O'    /* Don't include any indexes that contain
  4223.                            nullable columns. */
  4224.  
  4225.         SELECT @indid = MIN(indid)
  4226.             FROM sysindexes x, syscolumns c, syscolumns c2
  4227.             WHERE
  4228.                 not (@table_id is null)
  4229.                 AND x.status&2 = 2        /*    If Unique Index */
  4230.                 AND c.id = x.id
  4231.                 AND c2.id = c.id
  4232.                 AND c2.colid < x.keycnt + (x.status&16)/16
  4233.                 AND x.id = @table_id
  4234.                 AND indid > 0        /*    Eliminate Table Row */
  4235.                 AND c.name = index_col(@table_name,x.indid,c2.colid)
  4236.                 GROUP BY indid HAVING SUM(c.status&8) = 0
  4237.  
  4238.     ELSE    /* Include indexes that are partially nullable. */
  4239.  
  4240.         SELECT @indid = MIN(indid)
  4241.             FROM sysindexes x
  4242.             WHERE
  4243.                 not (@table_id is null)
  4244.                 AND status&2 = 2        /*    If Unique Index */
  4245.                 AND id = @table_id
  4246.                 AND indid > 0        /*    Eliminate Table Row */
  4247.  
  4248.     SELECT
  4249.         SCOPE = @scopeout,
  4250.         COLUMN_NAME = convert(sysname,INDEX_COL(@full_table_name,indid,c2.colid)),
  4251.         d.DATA_TYPE,
  4252.         convert(sysname,case
  4253.             when t.xusertype > 255 then t.name
  4254.             else d.TYPE_NAME
  4255.         end) TYPE_NAME,
  4256.         convert(int,case
  4257.             when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  4258.             else OdbcPrec(c.xtype,c.length,c.xprec)
  4259.         end) "PRECISION",
  4260.         convert(int,case
  4261.             when type_name(d.ss_dtype) IN ('numeric','decimal') then    /* decimal/numeric types */
  4262.                 OdbcPrec(c.xtype,c.length,c.xprec)+2
  4263.             else isnull(d.length, c.length)
  4264.         end) LENGTH,
  4265.         SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
  4266.         PSEUDO_COLUMN = convert(smallint,1)
  4267.     FROM
  4268.         sysindexes x,
  4269.         syscolumns c,
  4270.         master.dbo.spt_datatype_info d,
  4271.         systypes t,
  4272.         syscolumns c2    /* Self-join to generate list of index columns and */
  4273.                         /* to extract datatype names */
  4274.     WHERE
  4275.         not (@table_id is null)
  4276.         AND x.id = @table_id
  4277.         AND c.name = INDEX_COL(@full_table_name,@indid,c2.colid)
  4278.         AND c.id = x.id
  4279.         AND c2.id = x.id
  4280.         AND c2.colid < x.keycnt + (x.status&16)/16
  4281.         AND x.indid = @indid
  4282.         AND t.xtype = d.ss_dtype
  4283.         AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  4284.         AND isnull(d.AUTO_INCREMENT,0) = isnull(ColumnProperty (c.id, c.name, 'IsIdentity'),0)
  4285.         AND c.xusertype = t.xusertype
  4286. go
  4287.  
  4288. grant execute on sp_special_columns to public
  4289. go
  4290.  
  4291. dump tran master with no_log
  4292. go
  4293.  
  4294. print 'creating sp_sproc_columns'
  4295. go
  4296.  
  4297. /*    Procedure for pre-6.0 server */
  4298. CREATE PROCEDURE sp_sproc_columns (
  4299.                  @procedure_name        varchar(102) = '%', /* 3*32+5+1 */
  4300.                  @procedure_owner        varchar(96) = null,
  4301.                  @procedure_qualifier    varchar(32) = null,
  4302.                  @column_name            varchar(96) = null,
  4303.                  @ODBCVer                int = 2)
  4304. AS
  4305.     DECLARE @group_num_lower smallint
  4306.     DECLARE @group_num_upper smallint
  4307.     DECLARE @semi_position int
  4308.     DECLARE @full_procedure_name    varchar(205)/* 2*102+1 */
  4309.     DECLARE @procedure_id int
  4310.  
  4311.     if @column_name is null /*    If column name not supplied, match all */
  4312.         select @column_name = '%'
  4313.     if @procedure_qualifier is not null
  4314.     begin
  4315.         if db_name() <> @procedure_qualifier
  4316.         begin
  4317.             if @procedure_qualifier = ''
  4318.             begin
  4319.                 /* in this case, we need to return an empty result set */
  4320.                 /* because the user has requested a database with an empty name */
  4321.                 select @procedure_name = ''
  4322.                 select @procedure_owner = ''
  4323.             end
  4324.             else
  4325.             begin    /* If qualifier doesn't match current database */
  4326.                 raiserror 20001 '~~Rush_51~~'
  4327.                 return
  4328.             end
  4329.         end
  4330.     end
  4331.  
  4332.     if @procedure_name is null
  4333.     begin    /*    If procedure name not supplied, match all */
  4334.         select @procedure_name = '%'
  4335.     end
  4336.  
  4337.     /* first we need to extract the procedure group number, if one exists */
  4338.     select @semi_position = charindex(';',@procedure_name)
  4339.     if (@semi_position > 0)
  4340.     begin    /* If group number separator (;) found */
  4341.         select @group_num_lower = convert(int,substring(@procedure_name, @semi_position + 1, 2))
  4342.         select @group_num_upper = @group_num_lower
  4343.         select @procedure_name = substring(@procedure_name, 1, @semi_position -1)
  4344.     end
  4345.     else
  4346.     begin    /* No group separator, so default to group number of 1 */
  4347.         select @group_num_lower = 1
  4348.         select @group_num_upper = 32767            
  4349.     end
  4350.  
  4351.     if @procedure_owner is null
  4352.     begin    /* If unqualified procedure name */
  4353.         SELECT @full_procedure_name = @procedure_name
  4354.     end
  4355.     else
  4356.     begin    /* Qualified procedure name */
  4357.         SELECT @full_procedure_name = @procedure_owner + '.' + @procedure_name
  4358.     end
  4359.  
  4360.     /*    Get Object ID */
  4361.     SELECT @procedure_id = object_id(@full_procedure_name)
  4362.     if ((charindex('%',@full_procedure_name) = 0) and
  4363.         (charindex('_',@full_procedure_name) = 0) and
  4364.         @procedure_id <> 0)
  4365.     begin
  4366.         /* this block is for the case where there is no pattern
  4367.             matching required for the procedure name */
  4368.         SELECT
  4369.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  4370.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  4371.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ ltrim(str(c.number,5))),
  4372.             COLUMN_NAME = convert(varchar(32),c.name),
  4373.             COLUMN_TYPE = convert(smallint, 0),
  4374.             d.DATA_TYPE,
  4375.             TYPE_NAME = t.name,
  4376.             "PRECISION" = isnull(d.data_precision, convert(int,c.length)),
  4377.             LENGTH = isnull(d.length, convert(int,c.length)),
  4378.             SCALE = d.numeric_scale,
  4379.             d.RADIX,
  4380.             d.NULLABLE,
  4381.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  4382.             COLUMN_DEF = convert(varchar(255),null),
  4383.             d.SQL_DATA_TYPE,
  4384.             d.SQL_DATETIME_SUB,
  4385.             CHAR_OCTET_LENGTH = isnull(d.data_precision, convert(int,c.length))+d.charbin,
  4386.             ORDINAL_POSITION = convert(int,c.colid),
  4387.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  4388.             SS_DATA_TYPE = c.type
  4389.         FROM
  4390.             syscolumns c,
  4391.             sysobjects o,
  4392.             master.dbo.spt_datatype_info d,
  4393.             systypes t
  4394.         WHERE
  4395.             o.id = @procedure_id
  4396.             AND c.id = o.id
  4397.             AND t.type = d.ss_dtype
  4398.             AND c.length = isnull(d.fixlen, c.length)
  4399.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  4400.             AND c.usertype = t.usertype
  4401.             AND c.name like @column_name
  4402.             AND c.number between @group_num_lower and @group_num_upper
  4403.         UNION ALL
  4404.         SELECT           /* return value row*/
  4405.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  4406.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  4407.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ isnull(ltrim(str(c.number,5)),'1')),
  4408.             COLUMN_NAME = convert(varchar(32),'RETURN_VALUE'),
  4409.             COLUMN_TYPE = convert(smallint, 5),
  4410.             DATA_TYPE = convert(smallint, 4),
  4411.             TYPE_NAME = convert(varchar(32),'int'),
  4412.             "PRECISION" = convert(int,10),
  4413.             LENGTH = convert(int,4),
  4414.             SCALE = convert(smallint,0),
  4415.             RADIX = convert(smallint,10),
  4416.             NULLABLE = convert(smallint,0),
  4417.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  4418.             COLUMN_DEF = convert(varchar(255),NULL),
  4419.             SQL_DATA_TYPE = convert(smallint, 4),
  4420.             SQL_DATETIME_SUB = convert(smallint,null),
  4421.             CHAR_OCTET_LENGTH = convert(int,null),
  4422.             ORDINAL_POSITION = convert(int,0),
  4423.             IS_NULLABLE = convert(varchar(254),'NO'),
  4424.             SS_DATA_TYPE = convert(tinyint,56)
  4425.         FROM
  4426.             syscomments c, sysobjects o
  4427.         WHERE
  4428.             o.id = @procedure_id
  4429.             AND c.id = o.id
  4430.             AND c.colid = 1
  4431.             AND o.type = 'P'                        /* Just Procedures */
  4432.             AND 'RETURN_VALUE' like @column_name
  4433.             AND c.number between @group_num_lower and @group_num_upper        
  4434.         ORDER BY 1, 2, 3, 18
  4435.     end
  4436.     else
  4437.     begin
  4438.         /* this block is for the case where there IS pattern
  4439.             matching done on the procedure name */
  4440.         if @procedure_owner is null
  4441.             select @procedure_owner = '%'
  4442.         SELECT
  4443.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  4444.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  4445.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ ltrim(str(c.number,5))),
  4446.             COLUMN_NAME = convert(varchar(32),c.name),
  4447.             COLUMN_TYPE = convert(smallint, 0),
  4448.             d.DATA_TYPE,
  4449.             TYPE_NAME = t.name,
  4450.             "PRECISION" = isnull(d.data_precision, convert(int,c.length)),
  4451.             LENGTH = isnull(d.length, convert(int,c.length)),
  4452.             SCALE = d.numeric_scale,
  4453.             d.RADIX,
  4454.             d.NULLABLE,
  4455.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  4456.             COLUMN_DEF = convert(varchar(255),null),
  4457.             d.SQL_DATA_TYPE,
  4458.             d.SQL_DATETIME_SUB,
  4459.             CHAR_OCTET_LENGTH = isnull(d.data_precision, convert(int,c.length))+d.charbin,
  4460.             ORDINAL_POSITION = convert(int,c.colid),
  4461.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  4462.             SS_DATA_TYPE = c.type
  4463.         FROM
  4464.             syscolumns c,
  4465.             sysobjects o,
  4466.             master.dbo.spt_datatype_info d,
  4467.             systypes t
  4468.         WHERE
  4469.             o.name like @procedure_name
  4470.             AND user_name(o.uid) like @procedure_owner
  4471.             AND o.id = c.id
  4472.             AND t.type = d.ss_dtype
  4473.             AND c.length = isnull(d.fixlen, c.length)
  4474.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  4475.             AND c.usertype = t.usertype
  4476.             AND o.type = 'P'                            /* Just Procedures */
  4477.             AND c.name like @column_name
  4478.             AND c.number between @group_num_lower and @group_num_upper
  4479.         UNION ALL
  4480.         SELECT           /* return value row*/
  4481.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  4482.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  4483.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ isnull(ltrim(str(c.number,5)),'1')),
  4484.             COLUMN_NAME = convert(varchar(32),'RETURN_VALUE'),
  4485.             COLUMN_TYPE = convert(smallint, 5),
  4486.             DATA_TYPE = convert(smallint, 4),
  4487.             TYPE_NAME = convert(varchar(32),'int'),
  4488.             "PRECISION" = convert(int,10),
  4489.             LENGTH = convert(int,4),
  4490.             SCALE = convert(smallint,0),
  4491.             RADIX = convert(smallint,10),
  4492.             NULLABLE = convert(smallint,0),
  4493.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  4494.             COLUMN_DEF = convert(varchar(255),NULL),
  4495.             SQL_DATA_TYPE = convert(smallint, 4),
  4496.             SQL_DATETIME_SUB = convert(smallint,null),
  4497.             CHAR_OCTET_LENGTH = convert(int,null),
  4498.             ORDINAL_POSITION = convert(int,0),
  4499.             IS_NULLABLE = convert(varchar(254),'NO'),
  4500.             SS_DATA_TYPE = convert(tinyint,56)
  4501.         FROM
  4502.             syscomments c, sysobjects o
  4503.         WHERE
  4504.             o.name like @procedure_name
  4505.             AND user_name(o.uid) like @procedure_owner
  4506.             AND c.id = o.id
  4507.             AND c.colid = 1
  4508.             AND o.type = 'P'                        /* Just Procedures */
  4509.             AND 'RETURN_VALUE' like @column_name
  4510.             AND c.number between @group_num_lower and @group_num_upper
  4511.         ORDER BY 1, 2, 3, 18
  4512.     end
  4513. go
  4514.  
  4515. if (charindex('6.00', @@version) = 0 and
  4516.     charindex('6.50', @@version) = 0 and
  4517.     charindex('7.00', @@version) = 0)
  4518. begin
  4519.     print ''
  4520.     print ''
  4521.     print 'Warning:'
  4522.     print 'you are installing the stored procedures '
  4523.     print 'on a pre 6.0 SQL Server.'
  4524.     print 'Ignore the following error.'
  4525. end
  4526. else
  4527.     drop proc sp_sproc_columns
  4528. go
  4529.  
  4530. /*    Procedure for 6.0 and 6.50 servers */
  4531. CREATE PROCEDURE sp_sproc_columns (
  4532.                  @procedure_name        varchar(102) = '%', /* 3*36+5+1 */
  4533.                  @procedure_owner        varchar(96) = null,
  4534.                  @procedure_qualifier    varchar(32) = null,
  4535.                  @column_name            varchar(96) = null,
  4536.                  @ODBCVer                int = 2)
  4537. AS
  4538.     DECLARE @group_num_lower smallint
  4539.     DECLARE @group_num_upper smallint
  4540.     DECLARE @semi_position int
  4541.     DECLARE @full_procedure_name    varchar(205)
  4542.     DECLARE @procedure_id int
  4543.  
  4544.     if @column_name is null /*    If column name not supplied, match all */
  4545.         select @column_name = '%'
  4546.     if @procedure_qualifier is not null
  4547.     begin
  4548.         if db_name() <> @procedure_qualifier
  4549.         begin
  4550.             if @procedure_qualifier = ''
  4551.             begin
  4552.                 /* in this case, we need to return an empty result set */
  4553.                 /* because the user has requested a database with an empty name */
  4554.                 select @procedure_name = ''
  4555.                 select @procedure_owner = ''
  4556.             end
  4557.             else
  4558.             begin    /* If qualifier doesn't match current database */
  4559.                 raiserror (15250, -1,-1)
  4560.                 return
  4561.             end
  4562.         end
  4563.     end
  4564.  
  4565.     if @procedure_name is null
  4566.     begin    /*    If procedure name not supplied, match all */
  4567.         select @procedure_name = '%'
  4568.     end
  4569.  
  4570.     /* first we need to extract the procedure group number, if one exists */
  4571.     select @semi_position = charindex(';',@procedure_name)
  4572.     if (@semi_position > 0)
  4573.     begin    /* If group number separator (;) found */
  4574.         select @group_num_lower = convert(int,substring(@procedure_name, @semi_position + 1, 2))
  4575.         select @group_num_upper = @group_num_lower
  4576.         select @procedure_name = substring(@procedure_name, 1, @semi_position -1)
  4577.     end
  4578.     else
  4579.     begin    /* No group separator, so default to group number of 1 */
  4580.         select @group_num_lower = 1
  4581.         select @group_num_upper = 32767            
  4582.     end
  4583.  
  4584.     if @procedure_owner is null
  4585.     begin    /* If unqualified procedure name */
  4586.         SELECT @full_procedure_name = @procedure_name
  4587.     end
  4588.     else
  4589.     begin    /* Qualified procedure name */
  4590.         SELECT @full_procedure_name = @procedure_owner + '.' + @procedure_name
  4591.     end
  4592.  
  4593.     /*    Get Object ID */
  4594.     SELECT @procedure_id = object_id(@full_procedure_name)
  4595.     if ((charindex('%',@full_procedure_name) = 0) and
  4596.         (charindex('[',@full_procedure_name) = 0) and
  4597.         (charindex('_',@full_procedure_name) = 0) and
  4598.         @procedure_id <> 0)
  4599.     begin
  4600.         /* this block is for the case where there is no pattern
  4601.             matching required for the procedure name */
  4602.         SELECT
  4603.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  4604.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  4605.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ ltrim(str(c.number,5))),
  4606.             COLUMN_NAME = convert(varchar(32),c.name),
  4607.             COLUMN_TYPE = convert(smallint, 1+((c.status/64)&1)),
  4608.             d.DATA_TYPE,
  4609.             TYPE_NAME = t.name,
  4610.             convert(int,case
  4611.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  4612.                 else isnull(convert(int,c.prec), 2147483647)
  4613.             end) "PRECISION",
  4614.             convert(int,case
  4615.                 when d.ss_dtype IN (106, 108, 55, 63) then    /* decimal/numeric types */
  4616.                     c.prec+2
  4617.                 else
  4618.                     isnull(d.length, c.length)
  4619.             end) LENGTH,
  4620.             SCALE = convert(smallint, c.scale),
  4621.             d.RADIX,
  4622.             d.NULLABLE,
  4623.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  4624.             COLUMN_DEF = convert(varchar(255),NULL),
  4625.             d.SQL_DATA_TYPE,
  4626.             d.SQL_DATETIME_SUB,
  4627.             CHAR_OCTET_LENGTH = isnull(convert(int,c.prec), 2147483647)+d.charbin,
  4628.             ORDINAL_POSITION = convert(int,c.colid),
  4629.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  4630.             SS_DATA_TYPE = c.type
  4631.         FROM
  4632.             syscolumns c,
  4633.             sysobjects o,
  4634.             master.dbo.spt_datatype_info d,
  4635.             systypes t
  4636.         WHERE
  4637.             o.id = @procedure_id
  4638.             AND c.id = o.id
  4639.             AND c.type = d.ss_dtype
  4640.             AND c.length = isnull(d.fixlen, c.length)
  4641.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  4642.             AND isnull(d.AUTO_INCREMENT,0) = 0
  4643.             AND c.usertype = t.usertype
  4644.             AND c.name like @column_name
  4645.             AND c.number between @group_num_lower and @group_num_upper
  4646.         UNION ALL
  4647.         SELECT           /* return value row*/
  4648.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  4649.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  4650.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ isnull(ltrim(str(c.number,5)),'1')),
  4651.             COLUMN_NAME = convert(varchar(32),'RETURN_VALUE'),
  4652.             COLUMN_TYPE = convert(smallint, 5),
  4653.             DATA_TYPE = convert(smallint, 4),
  4654.             TYPE_NAME = convert(varchar(32),'int'),
  4655.             "PRECISION" = convert(int,10),
  4656.             LENGTH = convert(int,4),
  4657.             SCALE = convert(smallint,0),
  4658.             RADIX = convert(smallint,10),
  4659.             NULLABLE = convert(smallint,0),
  4660.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  4661.             COLUMN_DEF = convert(varchar(255),NULL),
  4662.             SQL_DATA_TYPE = convert(smallint, 4),
  4663.             SQL_DATETIME_SUB = convert(smallint,null),
  4664.             CHAR_OCTET_LENGTH = convert(int,null),
  4665.             ORDINAL_POSITION = convert(int,0),
  4666.             IS_NULLABLE = convert(varchar(254),'NO'),
  4667.             SS_DATA_TYPE = convert(tinyint,56)
  4668.         FROM
  4669.             syscomments c, sysobjects o
  4670.         WHERE
  4671.             o.id = @procedure_id
  4672.             AND c.id = o.id
  4673.             AND c.colid = 1
  4674.             AND o.type = 'P'                        /* Just Procedures */
  4675.             AND 'RETURN_VALUE' like @column_name
  4676.             AND c.number between @group_num_lower and @group_num_upper
  4677.         ORDER BY 1, 2, 3, 18
  4678.     end
  4679.     else
  4680.     begin
  4681.         /* this block is for the case where there IS pattern
  4682.             matching done on the procedure name */
  4683.         if @procedure_owner is null
  4684.             select @procedure_owner = '%'
  4685.         SELECT
  4686.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  4687.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  4688.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ ltrim(str(c.number,5))),
  4689.             COLUMN_NAME = convert(varchar(32),c.name),
  4690.             COLUMN_TYPE = convert(smallint, 1+((c.status/64)&1)),
  4691.             d.DATA_TYPE,
  4692.             TYPE_NAME = t.name,
  4693.             convert(int,case
  4694.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  4695.                 else isnull(convert(int,c.prec), 2147483647)
  4696.             end) "PRECISION",
  4697.             convert(int,case
  4698.                 when d.ss_dtype IN (106, 108, 55, 63) then    /* decimal/numeric types */
  4699.                     c.prec+2
  4700.                 else
  4701.                     isnull(d.length, c.length)
  4702.             end) LENGTH,
  4703.             SCALE = convert(smallint, c.scale),
  4704.             d.RADIX,
  4705.             d.NULLABLE,
  4706.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  4707.             COLUMN_DEF = convert(varchar(255),NULL),
  4708.             d.SQL_DATA_TYPE,
  4709.             d.SQL_DATETIME_SUB,
  4710.             CHAR_OCTET_LENGTH = isnull(convert(int,c.prec), 2147483647)+d.charbin,
  4711.             ORDINAL_POSITION = convert(int,c.colid),
  4712.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  4713.             SS_DATA_TYPE = c.type
  4714.         FROM
  4715.             syscolumns c,
  4716.             sysobjects o,
  4717.             master.dbo.spt_datatype_info d,
  4718.             systypes t
  4719.         WHERE
  4720.             o.name like @procedure_name
  4721.             AND user_name(o.uid) like @procedure_owner
  4722.             AND o.id = c.id
  4723.             AND c.type = d.ss_dtype
  4724.             AND c.length = isnull(d.fixlen, c.length)
  4725.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  4726.             AND isnull(d.AUTO_INCREMENT,0) = 0
  4727.             AND c.usertype = t.usertype
  4728.             AND o.type = 'P'                            /* Just Procedures */
  4729.             AND c.name like @column_name
  4730.             AND c.number between @group_num_lower and @group_num_upper
  4731.         UNION ALL
  4732.         SELECT           /* return value row*/
  4733.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  4734.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  4735.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ isnull(ltrim(str(c.number,5)),'1')),
  4736.             COLUMN_NAME = convert(varchar(32),'RETURN_VALUE'),
  4737.             COLUMN_TYPE = convert(smallint, 5),
  4738.             DATA_TYPE = convert(smallint, 4),
  4739.             TYPE_NAME = convert(varchar(32),'int'),
  4740.             "PRECISION" = convert(int,10),
  4741.             LENGTH = convert(int,4),
  4742.             SCALE = convert(smallint,0),
  4743.             RADIX = convert(smallint,10),
  4744.             NULLABLE = convert(smallint,0),
  4745.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  4746.             COLUMN_DEF = convert(varchar(255),NULL),
  4747.             SQL_DATA_TYPE = convert(smallint, 4),
  4748.             SQL_DATETIME_SUB = convert(smallint,null),
  4749.             CHAR_OCTET_LENGTH = convert(int,null),
  4750.             ORDINAL_POSITION = convert(int,0),
  4751.             IS_NULLABLE = convert(varchar(254),'NO'),
  4752.             SS_DATA_TYPE = convert(tinyint,56)
  4753.         FROM
  4754.             syscomments c, sysobjects o
  4755.         WHERE
  4756.             o.name like @procedure_name
  4757.             AND user_name(o.uid) like @procedure_owner
  4758.             AND c.id = o.id
  4759.             AND c.colid = 1
  4760.             AND o.type = 'P'                        /* Just Procedures */
  4761.             AND 'RETURN_VALUE' like @column_name
  4762.             AND c.number between @group_num_lower and @group_num_upper
  4763.         ORDER BY 1, 2, 3, 18
  4764.     end
  4765. go
  4766.  
  4767. if (charindex('7.00', @@version) = 0)
  4768. begin
  4769.     print ''
  4770.     print ''
  4771.     print 'Warning:'
  4772.     print 'you are installing the stored procedures '
  4773.     print 'on a pre 7.0 SQL Server.'
  4774.     print 'Ignore the following errors.'
  4775. end
  4776. else
  4777.     drop proc sp_sproc_columns
  4778. go
  4779.  
  4780. /*    Procedure for 7.0 server */
  4781. CREATE PROCEDURE sp_sproc_columns (
  4782.                  @procedure_name        nvarchar(390) = '%',
  4783.                  @procedure_owner        nvarchar(384) = null,
  4784.                  @procedure_qualifier    sysname = null,
  4785.                  @column_name            nvarchar(384) = null,
  4786.                  @ODBCVer                int = 2)
  4787. AS
  4788.     DECLARE @group_num_lower smallint
  4789.     DECLARE @group_num_upper smallint
  4790.     DECLARE @semi_position int
  4791.     DECLARE @full_procedure_name    nvarchar(774)
  4792.     DECLARE @procedure_id int
  4793.  
  4794.     if @column_name is null /*    If column name not supplied, match all */
  4795.         select @column_name = '%'
  4796.     if @procedure_qualifier is not null
  4797.     begin
  4798.         if db_name() <> @procedure_qualifier
  4799.         begin
  4800.             if @procedure_qualifier = ''
  4801.             begin
  4802.                 /* in this case, we need to return an empty result set */
  4803.                 /* because the user has requested a database with an empty name */
  4804.                 select @procedure_name = ''
  4805.                 select @procedure_owner = ''
  4806.             end
  4807.             else
  4808.             begin    /* If qualifier doesn't match current database */
  4809.                 raiserror (15250, -1,-1)
  4810.                 return
  4811.             end
  4812.         end
  4813.     end
  4814.  
  4815.     if @procedure_name is null
  4816.     begin    /*    If procedure name not supplied, match all */
  4817.         select @procedure_name = '%'
  4818.     end
  4819.  
  4820.     /* first we need to extract the procedure group number, if one exists */
  4821.     select @semi_position = charindex(';',@procedure_name)
  4822.     if (@semi_position > 0)
  4823.     begin    /* If group number separator (;) found */
  4824.         select @group_num_lower = convert(int,substring(@procedure_name, @semi_position + 1, 2))
  4825.         select @group_num_upper = @group_num_lower
  4826.         select @procedure_name = substring(@procedure_name, 1, @semi_position -1)
  4827.     end
  4828.     else
  4829.     begin    /* No group separator, so default to all groups */
  4830.         select @group_num_lower = 1
  4831.         select @group_num_upper = 32767            
  4832.     end
  4833.  
  4834.     if @procedure_owner is null
  4835.     begin    /* If unqualified procedure name */
  4836.         SELECT @full_procedure_name = quotename(@procedure_name)
  4837.     end
  4838.     else
  4839.     begin    /* Qualified procedure name */
  4840.         if @procedure_owner = ''
  4841.         begin    /* If empty owner name */
  4842.             SELECT @full_procedure_name = quotename(@procedure_owner)
  4843.         end
  4844.         else
  4845.         begin
  4846.             SELECT @full_procedure_name = quotename(@procedure_owner) +
  4847.                 '.' + quotename(@procedure_name)
  4848.         end
  4849.     end
  4850.  
  4851.     /*    Get Object ID */
  4852.     SELECT @procedure_id = object_id(@full_procedure_name)
  4853.     if ((isnull(charindex('%', @full_procedure_name),0) = 0) and
  4854.         (isnull(charindex('[', @procedure_name),0) = 0) and
  4855.         (isnull(charindex('[', @procedure_owner),0) = 0) and
  4856.         (isnull(charindex('_', @full_procedure_name),0) = 0) and
  4857.         not (@procedure_id is null))
  4858.     begin
  4859.         /* this block is for the case where there is no pattern
  4860.             matching required for the procedure name */
  4861.         SELECT
  4862.             PROCEDURE_QUALIFIER = convert(sysname,DB_NAME()),
  4863.             PROCEDURE_OWNER = convert(sysname,USER_NAME(o.uid)),
  4864.             PROCEDURE_NAME = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  4865.             COLUMN_NAME = convert(sysname,c.name),
  4866.             COLUMN_TYPE = convert(smallint, 1+c.isoutparam),
  4867.             d.DATA_TYPE,
  4868.             TYPE_NAME = t.name,
  4869.             convert(int,case
  4870.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  4871.                 else OdbcPrec(c.xtype,c.length,c.xprec)
  4872.             end) "PRECISION",
  4873.             convert(int,case
  4874.                 when type_name(d.ss_dtype) IN ('numeric','decimal') then    /* decimal/numeric types */
  4875.                     OdbcPrec(c.xtype,c.length,c.xprec)+2
  4876.                 else
  4877.                     isnull(d.length, c.length)
  4878.             end) LENGTH,
  4879.             SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
  4880.             d.RADIX,
  4881.             d.NULLABLE,
  4882.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  4883.             COLUMN_DEF = convert(nvarchar(4000),NULL),
  4884.             d.SQL_DATA_TYPE,
  4885.             d.SQL_DATETIME_SUB,
  4886.             CHAR_OCTET_LENGTH = isnull(d.length, c.length)+d.charbin,
  4887.             ORDINAL_POSITION = convert(int,c.colid),
  4888.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  4889.             SS_DATA_TYPE = c.type
  4890.         FROM
  4891.             syscolumns c,
  4892.             sysobjects o,
  4893.             master.dbo.spt_datatype_info d,
  4894.             systypes t
  4895.         WHERE
  4896.             o.id = @procedure_id
  4897.             AND c.id = o.id
  4898.             AND c.xtype = d.ss_dtype
  4899.             AND c.length = isnull(d.fixlen, c.length)
  4900.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  4901.             AND isnull(d.AUTO_INCREMENT,0) = 0
  4902.             AND c.xusertype = t.xusertype
  4903.             AND c.name like @column_name
  4904.             AND c.number between @group_num_lower and @group_num_upper
  4905.         UNION ALL
  4906.         SELECT           /* return value row*/
  4907.             PROCEDURE_QUALIFIER = convert(sysname,DB_NAME()),
  4908.             PROCEDURE_OWNER = convert(sysname,USER_NAME(o.uid)),
  4909.             PROCEDURE_NAME = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  4910.             COLUMN_NAME = convert(sysname,'RETURN_VALUE'),
  4911.             COLUMN_TYPE = convert(smallint, 5),
  4912.             DATA_TYPE = convert(smallint, 4),
  4913.             TYPE_NAME = convert(sysname,'int'),
  4914.             "PRECISION" = convert(int,10),
  4915.             LENGTH = convert(int,4),
  4916.             SCALE = convert(smallint,0),
  4917.             RADIX = convert(smallint,10),
  4918.             NULLABLE = convert(smallint,0),
  4919.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  4920.             COLUMN_DEF = convert(nvarchar(4000),NULL),
  4921.             SQL_DATA_TYPE = convert(smallint, 4),
  4922.             SQL_DATETIME_SUB = convert(smallint,null),
  4923.             CHAR_OCTET_LENGTH = convert(int,null),
  4924.             ORDINAL_POSITION = convert(int,0),
  4925.             IS_NULLABLE = convert(varchar(254),'NO'),
  4926.             SS_DATA_TYPE = convert(tinyint,56)
  4927.         FROM
  4928.             syscomments c, sysobjects o
  4929.         WHERE
  4930.             o.id = @procedure_id
  4931.             AND c.id = o.id
  4932.             AND c.colid = 1
  4933.             AND o.type = 'P'                        /* Just Procedures */
  4934.             AND 'RETURN_VALUE' like @column_name
  4935.             AND c.number between @group_num_lower and @group_num_upper        
  4936.         ORDER BY 1, 2, 3, 18
  4937.     end
  4938.     else
  4939.     begin
  4940.         /* this block is for the case where there IS pattern
  4941.             matching done on the procedure name */
  4942.         if @procedure_owner is null
  4943.             select @procedure_owner = '%'
  4944.         SELECT
  4945.             PROCEDURE_QUALIFIER = convert(sysname,DB_NAME()),
  4946.             PROCEDURE_OWNER = convert(sysname,USER_NAME(o.uid)),
  4947.             PROCEDURE_NAME = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  4948.             COLUMN_NAME = convert(sysname,c.name),
  4949.             COLUMN_TYPE = convert(smallint,    1+c.isoutparam),
  4950.             d.DATA_TYPE,
  4951.             TYPE_NAME = t.name,
  4952.             convert(int,case
  4953.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  4954.                 else OdbcPrec(c.xtype,c.length,c.xprec)
  4955.             end) "PRECISION",
  4956.             convert(int,case
  4957.                 when type_name(d.ss_dtype) IN ('numeric','decimal') then    /* decimal/numeric types */
  4958.                     OdbcPrec(c.xtype,c.length,c.xprec)+2
  4959.                 else
  4960.                     isnull(d.length, c.length)
  4961.             end) LENGTH,
  4962.             SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
  4963.             d.RADIX,
  4964.             d.NULLABLE,
  4965.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  4966.             COLUMN_DEF = convert(nvarchar(4000),NULL),
  4967.             d.SQL_DATA_TYPE,
  4968.             d.SQL_DATETIME_SUB,
  4969.             CHAR_OCTET_LENGTH = isnull(d.length, c.length)+d.charbin,
  4970.             ORDINAL_POSITION = convert(int,c.colid),
  4971.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  4972.             SS_DATA_TYPE = c.type
  4973.         FROM
  4974.             syscolumns c,
  4975.             sysobjects o,
  4976.             master.dbo.spt_datatype_info d,
  4977.             systypes t
  4978.         WHERE
  4979.             o.name like @procedure_name
  4980.             AND user_name(o.uid) like @procedure_owner
  4981.             AND o.id = c.id
  4982.             AND c.xtype = d.ss_dtype
  4983.             AND c.length = isnull(d.fixlen, c.length)
  4984.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  4985.             AND isnull(d.AUTO_INCREMENT,0) = 0
  4986.             AND c.xusertype = t.xusertype
  4987.             AND o.type = 'P'                            /* Just Procedures */
  4988.             AND c.name like @column_name
  4989.             AND c.number between @group_num_lower and @group_num_upper
  4990.         UNION ALL
  4991.         SELECT           /* return value row*/
  4992.             PROCEDURE_QUALIFIER = convert(sysname,DB_NAME()),
  4993.             PROCEDURE_OWNER = convert(sysname,USER_NAME(o.uid)),
  4994.             PROCEDURE_NAME = convert(nvarchar(134),o.name +';'+ isnull(ltrim(str(c.number,5)),'1')),
  4995.             COLUMN_NAME = convert(sysname,'RETURN_VALUE'),
  4996.             COLUMN_TYPE = convert(smallint, 5),
  4997.             DATA_TYPE = convert(smallint, 4),
  4998.             TYPE_NAME = convert(sysname,'int'),
  4999.             "PRECISION" = convert(int,10),
  5000.             LENGTH = convert(int,4),
  5001.             SCALE = convert(smallint,0),
  5002.             RADIX = convert(smallint,10),
  5003.             NULLABLE = convert(smallint,0),
  5004.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  5005.             COLUMN_DEF = convert(nvarchar(4000),NULL),
  5006.             SQL_DATA_TYPE = convert(smallint, 4),
  5007.             SQL_DATETIME_SUB = convert(smallint,null),
  5008.             CHAR_OCTET_LENGTH = convert(int,null),
  5009.             ORDINAL_POSITION = convert(int,0),
  5010.             IS_NULLABLE = convert(varchar(254),'NO'),
  5011.             SS_DATA_TYPE = convert(tinyint,56)
  5012.         FROM
  5013.             syscomments c, sysobjects o
  5014.         WHERE
  5015.             o.name like @procedure_name
  5016.             AND c.id = o.id
  5017.             AND user_name(o.uid) like @procedure_owner
  5018.             AND c.colid = 1
  5019.             AND o.type = 'P'                        /* Just Procedures */
  5020.             AND 'RETURN_VALUE' like @column_name
  5021.             AND c.number between @group_num_lower and @group_num_upper
  5022.         ORDER BY 1, 2, 3, 18
  5023.     end
  5024. go
  5025.  
  5026. grant execute on sp_sproc_columns to public
  5027. go
  5028.  
  5029. dump tran master with no_log
  5030. go
  5031.  
  5032. print 'creating sp_statistics'
  5033. go
  5034.  
  5035. /*    Procedure for pre-7.0 server */
  5036. CREATE PROCEDURE sp_statistics (
  5037.                  @table_name        varchar(32),
  5038.                  @table_owner        varchar(32) = null,
  5039.                  @table_qualifier    varchar(32) = null,
  5040.                  @index_name        varchar(32) = '%',
  5041.                  @is_unique         char(1) = 'N',
  5042.                  @accuracy            char(1) = 'Q')
  5043. AS
  5044.     set nocount on
  5045.     DECLARE @indid                int
  5046.     DECLARE @lastindid            int
  5047.     DECLARE @table_id            int
  5048.     DECLARE @full_table_name    varchar(65) /* 2*32+1 */
  5049.  
  5050.     create table #TmpIndex(
  5051.         TABLE_QUALIFIER varchar(32) NULL,
  5052.         TABLE_OWNER     varchar(32) NULL,
  5053.         TABLE_NAME        varchar(32) NOT NULL,
  5054.         INDEX_QUALIFIER varchar(32) null,
  5055.         INDEX_NAME        varchar(32) null,
  5056.         NON_UNIQUE        smallint null,
  5057.         TYPE            smallint NOT NULL,
  5058.         SEQ_IN_INDEX    smallint null,
  5059.         COLUMN_NAME     varchar(32) null,
  5060.         COLLATION        char(1) null,
  5061.         index_id        int null,
  5062.         CARDINALITY     int null,
  5063.         PAGES            int null,
  5064.         status            smallint NOT NULL)
  5065.  
  5066.     if @table_qualifier is not null
  5067.     begin
  5068.         if db_name() <> @table_qualifier
  5069.         begin    /* If qualifier doesn't match current database */
  5070.             raiserror 20001 '~~Rush_5~~'
  5071.             return
  5072.         end
  5073.     end
  5074.  
  5075.     if @accuracy not in ('Q','E')
  5076.         begin
  5077.             raiserror 20002 '~~Rush_58~~'
  5078.             return
  5079.         end
  5080.  
  5081.     if (@@trancount <> 0 and
  5082.         charindex('6.50', @@version) = 0 and
  5083.         charindex('7.00', @@version) = 0)
  5084.     begin    /* If inside a transaction */
  5085.         raiserror 20003 '~~Rush_59~~'
  5086.         return
  5087.     end
  5088.  
  5089.     if @table_owner is null
  5090.     begin    /* If unqualified table name */
  5091.         SELECT @full_table_name = @table_name
  5092.     end
  5093.     else
  5094.     begin    /* Qualified table name */
  5095.         if @table_owner = ''
  5096.         begin    /* If empty owner name */
  5097.             SELECT @full_table_name = @table_owner
  5098.         end
  5099.         else
  5100.         begin
  5101.             SELECT @full_table_name = @table_owner + '.' + @table_name
  5102.         end
  5103.     end
  5104.     /*    Get Object ID */
  5105.     SELECT @table_id = object_id(@full_table_name)
  5106.  
  5107.     /*    Start at lowest index id */
  5108.     SELECT @indid = min(indid)
  5109.     FROM sysindexes
  5110.     WHERE id = @table_id
  5111.         AND indid > 0
  5112.         AND indid < 255
  5113.  
  5114.     WHILE @indid is not NULL
  5115.     BEGIN
  5116.         INSERT #TmpIndex    /* Add all columns that are in index */
  5117.             SELECT
  5118.                 DB_NAME(),                                /* TABLE_QUALIFIER */
  5119.                 USER_NAME(o.uid),                        /* TABLE_OWNER       */
  5120.                 o.name,                                 /* TABLE_NAME       */
  5121.                 o.name,                                 /* INDEX_QUALIFIER */
  5122.                 x.name,                                 /* INDEX_NAME       */
  5123.                 0,                                        /* NON_UNIQUE       */
  5124.                 1,                                        /* SQL_INDEX_CLUSTERED */
  5125.                 colid,                                    /* SEQ_IN_INDEX    */
  5126.                 INDEX_COL(@full_table_name,indid,colid),/* COLUMN_NAME       */
  5127.                 'A',                                    /* COLLATION       */
  5128.                 @indid,                                 /* index_id        */
  5129.                 x.rows,                                 /* CARDINALITY       */
  5130.                 x.dpages,                                /* PAGES           */
  5131.                 x.status                                /* status            */
  5132.             FROM sysindexes x, syscolumns c, sysobjects o
  5133.             WHERE
  5134.                 x.id = @table_id
  5135.                 AND x.id = o.id
  5136.                 AND x.id = c.id
  5137.                 AND c.colid < keycnt+(x.status&18)/18    /* all but Unique Clust indices have an extra key */
  5138.                 AND x.indid = @indid
  5139.         /*
  5140.         **      Now move @indid to the next index.
  5141.         */
  5142.         SELECT @lastindid = @indid
  5143.         SELECT @indid = NULL
  5144.  
  5145.         SELECT @indid = min(indid)
  5146.         FROM sysindexes
  5147.         WHERE id = @table_id
  5148.             AND indid > @lastindid
  5149.             AND indid < 255
  5150.     END
  5151.  
  5152.     UPDATE #TmpIndex
  5153.         SET NON_UNIQUE = 1
  5154.         WHERE status&2 <> 2 /* If non-unique index */
  5155.     UPDATE #TmpIndex
  5156.         SET
  5157.             TYPE = 3,            /* SQL_INDEX_OTHER */
  5158.             CARDINALITY = NULL,
  5159.             PAGES = NULL
  5160.         WHERE index_id > 1    /* If non-clustered index */
  5161.  
  5162.     /* now add row for table statistics */
  5163.     INSERT #TmpIndex
  5164.         SELECT
  5165.             DB_NAME(),                /* TABLE_QUALIFIER */
  5166.             USER_NAME(o.uid),        /* TABLE_OWNER       */
  5167.             o.name,                 /* TABLE_NAME       */
  5168.             null,                    /* INDEX_QUALIFIER */
  5169.             null,                    /* INDEX_NAME       */
  5170.             null,                    /* NON_UNIQUE       */
  5171.             0,                        /* SQL_TABLE_STAT  */
  5172.             null,                    /* SEQ_IN_INDEX    */
  5173.             null,                    /* COLUMN_NAME       */
  5174.             null,                    /* COLLATION       */
  5175.             0,                        /* index_id        */
  5176.             x.rows,                 /* CARDINALITY       */
  5177.             x.dpages,                /* PAGES           */
  5178.             0                        /* status           */
  5179.         FROM sysindexes x, sysobjects o
  5180.         WHERE o.id = @table_id
  5181.             AND x.id = o.id
  5182.             AND (x.indid = 0 or x.indid = 1)    /*    If there are no indexes */
  5183.                                                 /*    then table stats are in */
  5184.                                                 /*    a row with indid =0        */
  5185.  
  5186.     if @is_unique <> 'Y'    /* If all indexes desired */
  5187.         SELECT
  5188.             TABLE_QUALIFIER,
  5189.             TABLE_OWNER,
  5190.             TABLE_NAME,
  5191.             NON_UNIQUE,
  5192.             INDEX_QUALIFIER,
  5193.             INDEX_NAME,
  5194.             TYPE,
  5195.             SEQ_IN_INDEX,
  5196.             COLUMN_NAME,
  5197.             COLLATION,
  5198.             CARDINALITY,
  5199.             PAGES,
  5200.             FILTER_CONDITION = convert(varchar(128),null)
  5201.         FROM #TmpIndex
  5202.         WHERE
  5203.             INDEX_NAME like @index_name /* If matching name */
  5204.             or INDEX_NAME is null        /* If SQL_TABLE_STAT row */
  5205.         ORDER BY 4, 7, 6, 8
  5206.     else                    /* If only unique indexes desired */
  5207.         SELECT
  5208.             TABLE_QUALIFIER,
  5209.             TABLE_OWNER,
  5210.             TABLE_NAME,
  5211.             NON_UNIQUE,
  5212.             INDEX_QUALIFIER,
  5213.             INDEX_NAME,
  5214.             TYPE,
  5215.             SEQ_IN_INDEX,
  5216.             COLUMN_NAME,
  5217.             COLLATION,
  5218.             CARDINALITY,
  5219.             PAGES,
  5220.             FILTER_CONDITION = convert(varchar(128),null)
  5221.         FROM #TmpIndex
  5222.         WHERE
  5223.             (NON_UNIQUE = 0             /* If unique */
  5224.                 or NON_UNIQUE is NULL)    /* If SQL_TABLE_STAT row */
  5225.             and (INDEX_NAME like @index_name    /* If matching name */
  5226.                 or INDEX_NAME is null)    /* If SQL_TABLE_STAT row */
  5227.         ORDER BY 4, 7, 6, 8
  5228.  
  5229.     DROP TABLE #TmpIndex
  5230. go
  5231.  
  5232. if (charindex('7.00', @@version) = 0)
  5233. begin
  5234.     print ''
  5235.     print ''
  5236.     print 'Warning:'
  5237.     print 'you are installing the stored procedures '
  5238.     print 'on a pre 7.0 SQL Server.'
  5239.     print 'Ignore the following errors.'
  5240. end
  5241. else
  5242.     drop proc sp_statistics
  5243. go
  5244.  
  5245. /*    Procedure for 7.0 server */
  5246. CREATE PROCEDURE sp_statistics (
  5247.                  @table_name        sysname,
  5248.                  @table_owner        sysname = null,
  5249.                  @table_qualifier    sysname = null,
  5250.                  @index_name        sysname = '%',
  5251.                  @is_unique         char(1) = 'N',
  5252.                  @accuracy            char(1) = 'Q')
  5253. AS
  5254.     set nocount on
  5255.     DECLARE @indid                int
  5256.     DECLARE @lastindid            int
  5257.     DECLARE @table_id            int
  5258.     DECLARE @full_table_name    nvarchar(257)
  5259.  
  5260.     create table #TmpIndex(
  5261.         TABLE_QUALIFIER sysname NULL,
  5262.         TABLE_OWNER     sysname NULL,
  5263.         TABLE_NAME        sysname NOT NULL,
  5264.         INDEX_QUALIFIER sysname null,
  5265.         INDEX_NAME        sysname null,
  5266.         NON_UNIQUE        smallint null,
  5267.         TYPE            smallint NOT NULL,
  5268.         SEQ_IN_INDEX    smallint null,
  5269.         COLUMN_NAME     sysname null,
  5270.         COLLATION        char(1) null,
  5271.         index_id        int null,
  5272.         CARDINALITY     int null,
  5273.         PAGES            int null,
  5274.         status            int NOT NULL)
  5275.  
  5276.     if @table_qualifier is not null
  5277.     begin
  5278.         if db_name() <> @table_qualifier
  5279.         begin    /* If qualifier doesn't match current database */
  5280.             raiserror (15250, -1,-1)
  5281.             return
  5282.         end
  5283.     end
  5284.  
  5285.     if @accuracy not in ('Q','E')
  5286.         begin
  5287.             raiserror (15251,-1,-1,'accuracy','''Q'' or ''E''')
  5288.             return
  5289.         end
  5290.  
  5291.     if @table_owner is null
  5292.     begin    /* If unqualified table name */
  5293.         SELECT @full_table_name = quotename(@table_name)
  5294.     end
  5295.     else
  5296.     begin    /* Qualified table name */
  5297.         if @table_owner = ''
  5298.         begin    /* If empty owner name */
  5299.             SELECT @full_table_name = quotename(@table_owner)
  5300.         end
  5301.         else
  5302.         begin
  5303.             SELECT @full_table_name = quotename(@table_owner) +
  5304.                 '.' + quotename(@table_name)
  5305.         end
  5306.     end
  5307.     /*    Get Object ID */
  5308.     SELECT @table_id = object_id(@full_table_name)
  5309.  
  5310.     /*    Start at lowest index id */
  5311.     SELECT @indid = min(indid)
  5312.     FROM sysindexes
  5313.     WHERE not (@table_id is null)
  5314.         AND id = @table_id
  5315.         AND indid > 0
  5316.         AND indid < 255
  5317.  
  5318.     WHILE @indid is not NULL
  5319.     BEGIN
  5320.         INSERT #TmpIndex    /* Add all columns that are in index */
  5321.             SELECT
  5322.                 DB_NAME(),                                /* TABLE_QUALIFIER */
  5323.                 USER_NAME(o.uid),                        /* TABLE_OWNER       */
  5324.                 o.name,                                 /* TABLE_NAME       */
  5325.                 o.name,                                 /* INDEX_QUALIFIER */
  5326.                 x.name,                                 /* INDEX_NAME       */
  5327.                 case                                    /* NON_UNIQUE       */
  5328.                     WHEN x.status&2 <> 2 then 1            /* Nonunique index */
  5329.                     else 0                                /* Unique index    */
  5330.                 end,
  5331.                 case                                    /* TYPE            */
  5332.                     when @indid > 1 then 3                /* Non-Clustered   */
  5333.                     else 1                                /* Clustered index */
  5334.                 end,
  5335.                 colid,                                    /* SEQ_IN_INDEX    */
  5336.                 INDEX_COL(@full_table_name,indid,colid),/* COLUMN_NAME       */
  5337.                 'A',                                    /* COLLATION       */
  5338.                 @indid,                                 /* index_id        */
  5339.                 case                                    /* CARDINALITY       */
  5340.                     when @indid > 1 then NULL            /* Non-Clustered   */
  5341.                     else x.rows                         /* Clustered index */
  5342.                 end,
  5343.                 case                                    /* PAGES           */
  5344.                     when @indid > 1 then NULL            /* Non-Clustered   */
  5345.                     else x.dpages                        /* Clustered index */
  5346.                 end,
  5347.                 x.status                                /* status            */
  5348.             FROM sysindexes x, syscolumns c, sysobjects o
  5349.             WHERE
  5350.                 not (@table_id is null)
  5351.                 AND x.id = @table_id
  5352.                 AND x.id = o.id
  5353.                 AND x.id = c.id
  5354.                 AND colid < keycnt+(x.status&18)/18    /* all but Unique Clust indices have an extra key */
  5355.                 AND INDEX_COL(@full_table_name,indid,colid) IS NOT NULL
  5356.                 AND indid = @indid
  5357.                 AND (x.status&2 = 2
  5358.                     OR @is_unique <> 'Y')
  5359.         /*
  5360.         **      Now move @indid to the next index.
  5361.         */
  5362.         SELECT @lastindid = @indid
  5363.         SELECT @indid = NULL
  5364.  
  5365.         SELECT @indid = min(indid)
  5366.         FROM sysindexes
  5367.         WHERE not (@table_id is null)
  5368.             AND id = @table_id
  5369.             AND indid > @lastindid
  5370.             AND indid < 255
  5371.     END
  5372.  
  5373.     /* now add row for table statistics */
  5374.     INSERT #TmpIndex
  5375.         SELECT
  5376.             DB_NAME(),                /* TABLE_QUALIFIER */
  5377.             USER_NAME(o.uid),        /* TABLE_OWNER       */
  5378.             o.name,                 /* TABLE_NAME       */
  5379.             null,                    /* INDEX_QUALIFIER */
  5380.             null,                    /* INDEX_NAME       */
  5381.             null,                    /* NON_UNIQUE       */
  5382.             0,                        /* SQL_TABLE_STAT  */
  5383.             null,                    /* SEQ_IN_INDEX    */
  5384.             null,                    /* COLUMN_NAME       */
  5385.             null,                    /* COLLATION       */
  5386.             0,                        /* index_id        */
  5387.             x.rows,                 /* CARDINALITY       */
  5388.             x.dpages,                /* PAGES           */
  5389.             0                        /* status           */
  5390.         FROM sysindexes x, sysobjects o
  5391.         WHERE not (@table_id is null)
  5392.             AND o.id = @table_id
  5393.             AND x.id = o.id
  5394.             AND (x.indid = 0 or x.indid = 1)    /*    If there are no indexes */
  5395.                                                 /*    then table stats are in */
  5396.                                                 /*    a row with indid =0        */
  5397.  
  5398.     SELECT
  5399.         TABLE_QUALIFIER,
  5400.         TABLE_OWNER,
  5401.         TABLE_NAME,
  5402.         NON_UNIQUE,
  5403.         INDEX_QUALIFIER,
  5404.         INDEX_NAME,
  5405.         TYPE,
  5406.         SEQ_IN_INDEX,
  5407.         COLUMN_NAME,
  5408.         COLLATION,
  5409.         CARDINALITY,
  5410.         PAGES,
  5411.         FILTER_CONDITION = convert(varchar(128),null)
  5412.     FROM #TmpIndex
  5413.     WHERE
  5414.         INDEX_NAME like @index_name /* If matching name */
  5415.         or INDEX_NAME is null        /* If SQL_TABLE_STAT row */
  5416.     ORDER BY 4, 7, 6, 8
  5417.  
  5418.     DROP TABLE #TmpIndex
  5419. go
  5420.  
  5421. grant execute on sp_statistics to public
  5422. go
  5423.  
  5424. dump tran master with no_log
  5425. go
  5426.  
  5427. print 'creating sp_stored_procedures'
  5428. go
  5429.  
  5430.  
  5431. /* pre 7.00 version */
  5432. create procedure sp_stored_procedures(
  5433.                         @sp_name        varchar(102) = null,
  5434.                         @sp_owner        varchar(96) = null,
  5435.                         @sp_qualifier    varchar(32) = null)
  5436. as
  5437.     declare @proc_type smallint
  5438.  
  5439.     if @sp_qualifier is not null
  5440.     begin
  5441.         if db_name() <> @sp_qualifier
  5442.         begin
  5443.             if @sp_qualifier = ''
  5444.             begin
  5445.                 /* in this case, we need to return an empty result set */
  5446.                 /* because the user has requested a database with an empty name */
  5447.                 select @sp_name = ''
  5448.                 select @sp_owner = ''
  5449.             end else
  5450.             begin    /* If qualifier doesn't match current database */
  5451.                 raiserror 20001 '~~Rush_51~~'
  5452.                 return
  5453.             end
  5454.         end
  5455.     end
  5456.  
  5457.     if @sp_name is null
  5458.     begin  /*  If procedure name not supplied, match all */
  5459.         select @sp_name = '%'
  5460.     end
  5461.     else begin
  5462.         if (@sp_owner is null) and (charindex('%', @sp_name) = 0)
  5463.         begin
  5464.             if exists (select * from sysobjects
  5465.                 where uid = user_id()
  5466.                     and name = @sp_name
  5467.                     and type = 'P') /* Object type of Procedure */
  5468.             begin
  5469.                 select @sp_owner = user_name()
  5470.             end
  5471.         end
  5472.     end
  5473.     if @sp_owner is null    /*    If procedure owner not supplied, match all */
  5474.         select @sp_owner = '%'
  5475.  
  5476.     select @proc_type=2        /* Return 2 for 4.2 and later servers. */
  5477.  
  5478.     select
  5479.         PROCEDURE_QUALIFIER = convert(varchar(32),db_name()),
  5480.         PROCEDURE_OWNER = convert(varchar(32),user_name(o.uid)),
  5481.         PROCEDURE_NAME = convert(varchar(36),o.name +';'+ ltrim(str(c.number,5))),
  5482.         NUM_INPUT_PARAMS = -1,    /* Constant since value unknown */
  5483.         NUM_OUTPUT_PARAMS = -1, /* Constant since value unknown */
  5484.         NUM_RESULT_SETS = -1,    /* Constant since value unknown */
  5485.         REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  5486.         PROCEDURE_TYPE = @proc_type
  5487.     from
  5488.         sysobjects o,syscomments c,sysusers u
  5489.     where
  5490.         o.name like @sp_name
  5491.         and c.colid = 1
  5492.         and user_name(o.uid) like @sp_owner
  5493.         and o.type = 'P'        /* Object type of Procedure */
  5494.         and c.id = o.id
  5495.         and u.uid = user_id()    /* constrain sysusers uid for use in subquery */
  5496.         and (suser_id() = 1     /* User is the System Administrator */
  5497.             or o.uid = user_id()    /* User created the object */
  5498.             /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  5499.             or ((select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  5500.              from sysprotects p
  5501.              /* outer join to correlate with all rows in sysobjects */
  5502.              where p.id =* o.id
  5503.                  /*  get rows for public,current user,user's group */
  5504.                  and (p.uid = 0 or p.uid = user_id() or p.uid =* u.gid)
  5505.                  /* check for SELECT,EXECUTE privilege */
  5506.                  and (action in (193,224)))&1    /* more magic...normalize GRANT */
  5507.             ) = 1     /* final magic...compare Grants    */
  5508.         )
  5509.     order by 1, 2, 3
  5510. go
  5511.  
  5512. grant execute on sp_stored_procedures to public
  5513. go
  5514.  
  5515.  
  5516. if (charindex('7.00', @@version) > 0)
  5517.     drop procedure sp_stored_procedures
  5518. else
  5519. begin
  5520.     print ''
  5521.     print ''
  5522.     print 'Warning:'
  5523.     print 'you are installing the stored procedures '
  5524.     print 'on a pre 7.0 SQL Server.'
  5525.     print 'Ignore the following errors.'
  5526. end
  5527. go
  5528.  
  5529.  
  5530. /* 7.00 version */
  5531. create procedure sp_stored_procedures(
  5532.                         @sp_name        nvarchar(390) = null,
  5533.                         @sp_owner        nvarchar(384) = null,
  5534.                         @sp_qualifier    sysname = null)
  5535. as
  5536.     declare @proc_type smallint
  5537.  
  5538.     if @sp_qualifier is not null
  5539.     begin
  5540.         if db_name() <> @sp_qualifier
  5541.         begin
  5542.             if @sp_qualifier = ''
  5543.             begin
  5544.                 /* in this case, we need to return an empty result set */
  5545.                 /* because the user has requested a database with an empty name */
  5546.                 select @sp_name = ''
  5547.                 select @sp_owner = ''
  5548.             end else
  5549.             begin    /* If qualifier doesn't match current database */
  5550.                 raiserror (15250, -1,-1)
  5551.                 return
  5552.             end
  5553.         end
  5554.     end
  5555.  
  5556.     if @sp_name is null
  5557.     begin  /*  If procedure name not supplied, match all */
  5558.         select @sp_name = '%'
  5559.     end
  5560.     else begin
  5561.         if (@sp_owner is null) and (charindex('%', @sp_name) = 0)
  5562.         begin
  5563.             if exists (select * from sysobjects
  5564.                 where uid = user_id()
  5565.                     and name = @sp_name
  5566.                     and type = 'P') /* Object type of Procedure */
  5567.             begin
  5568.                 select @sp_owner = user_name()
  5569.             end
  5570.         end
  5571.     end
  5572.     if @sp_owner is null    /*    If procedure owner not supplied, match all */
  5573.         select @sp_owner = '%'
  5574.  
  5575.     select @proc_type=2        /* Return 2 for 4.2 and later servers. */
  5576.  
  5577.     select
  5578.         PROCEDURE_QUALIFIER = convert(sysname,db_name()),
  5579.         PROCEDURE_OWNER = convert(sysname,user_name(o.uid)),
  5580.         PROCEDURE_NAME = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  5581.         NUM_INPUT_PARAMS = -1,    /* Constant since value unknown */
  5582.         NUM_OUTPUT_PARAMS = -1, /* Constant since value unknown */
  5583.         NUM_RESULT_SETS = -1,    /* Constant since value unknown */
  5584.         REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  5585.         PROCEDURE_TYPE = @proc_type
  5586.     from
  5587.         sysobjects o,syscomments c
  5588.     where
  5589.         o.name like @sp_name
  5590.         and c.colid = 1
  5591.         and user_name(o.uid) like @sp_owner
  5592.         and o.type = 'P'        /* Object type of Procedure */
  5593.         and c.id = o.id
  5594.         and permissions (o.id)&32 <> 0
  5595.     order by 1, 2, 3
  5596. go
  5597.  
  5598. grant execute on sp_stored_procedures to public
  5599. go
  5600.  
  5601. dump tran master with no_log
  5602. go
  5603.  
  5604.  
  5605. print 'creating sp_table_privileges'
  5606. go
  5607.  
  5608. /*    Procedure for pre 6.50 server */
  5609. CREATE PROCEDURE sp_table_privileges (
  5610.             @table_name         varchar(90),
  5611.             @table_owner        varchar(90) = null,
  5612.             @table_qualifier    varchar(32) = null)
  5613. as
  5614.     set nocount on
  5615.  
  5616.     declare @table_id    int,
  5617.             @owner_id     int,
  5618.             @full_table_name char(181)
  5619.     declare @refconst int
  5620.  
  5621.     select @refconst = 1
  5622.     if    (charindex('6.00', @@version) = 0)
  5623.         select @refconst = NULL
  5624.  
  5625.     if @table_qualifier is not null
  5626.     begin
  5627.         if db_name() != @table_qualifier
  5628.         begin    /* If qualifier doesn't match current database */
  5629.             raiserror 20001 'Table qualifier must be name of current database'
  5630.             return
  5631.         end
  5632.     end
  5633.     if @table_owner is null
  5634.     begin    /* If unqualified table name */
  5635.         SELECT @full_table_name = @table_name
  5636.     end
  5637.     else
  5638.     begin    /* Qualified table name */
  5639.         SELECT @full_table_name = @table_owner + '.' + @table_name
  5640.     end
  5641.     /*    Get Object ID */
  5642.     SELECT @table_id = object_id(@full_table_name)
  5643.  
  5644.     if @@trancount != 0
  5645.     begin    /* If inside a transaction */
  5646.         raiserror 20003 'The procedure ''sp_table_privileges'' cannot be executed from within a transaction.'
  5647.         return
  5648.     end
  5649.     create table #table_priv1(
  5650.         table_qualifier            varchar(32) NOT NULL,
  5651.         table_owner             varchar(32) NOT NULL,
  5652.         table_name                varchar(32) NOT NULL,
  5653.         grantor                 varchar(32) NOT NULL,
  5654.         grantee                 varchar(32) NOT NULL,
  5655.         select_privilege        int NOT NULL,
  5656.         insert_privilege        int NOT NULL,
  5657.         update_privilege        int NOT NULL,
  5658.         delete_privilege        int NOT NULL,
  5659.         references_privilege    int NULL,
  5660.         is_grantable            varchar(3) NOT NULL,
  5661.         uid                     int NOT NULL,
  5662.         gid                     int NOT NULL)
  5663.  
  5664.     insert into #table_priv1
  5665.         select distinct
  5666.             db_name(),
  5667.             user_name(o.uid),
  5668.             o.name,
  5669.             user_name(o.uid),
  5670.             u.name,
  5671.             0,
  5672.             0,
  5673.             0,
  5674.             0,
  5675.             @refconst,
  5676.             'no',
  5677.             u.uid,
  5678.             u.gid
  5679.         from sysusers u, sysobjects o
  5680.         where o.id = @table_id and u.uid != u.gid
  5681.            and sysstat & 0xf in (1,2,3)    /* only valid for system tables,
  5682.                                            ** user tables, and views. */
  5683.  
  5684.     /*
  5685.     ** now add row for table owner
  5686.     */
  5687.     if exists (
  5688.         select *
  5689.             from #table_priv1
  5690.             where grantor = grantee)
  5691.     begin
  5692.         update #table_priv1
  5693.         set
  5694.             select_privilege = 1,
  5695.             update_privilege = 1,
  5696.             insert_privilege = 1,
  5697.             delete_privilege = 1,
  5698.             references_privilege = 1,
  5699.             is_grantable = 'yes'
  5700.         where grantor = grantee
  5701.     end
  5702.     else
  5703.     begin
  5704.         insert into #table_priv1
  5705.             select    db_name(),
  5706.                 user_name(o.uid),
  5707.                 o.name,
  5708.                 user_name(o.uid),
  5709.                 user_name(o.uid),
  5710.                 1,
  5711.                 1,
  5712.                 1,
  5713.                 1,
  5714.                 @refconst,
  5715.                 'yes',
  5716.                 o.uid,
  5717.                 u.gid
  5718.             from sysobjects o, sysusers u
  5719.             where o.id = @table_id and u.uid = o.uid
  5720.             and sysstat & 0xf in (1,2,3)    /* only valid for system tables,
  5721.                                            ** user tables, and views. */
  5722.  
  5723.     end
  5724.  
  5725.     update #table_priv1
  5726.     set select_privilege = 1
  5727.     where
  5728.         exists (
  5729.             select * from sysprotects
  5730.             where
  5731.                 id = @table_id
  5732.                 and (#table_priv1.uid = uid
  5733.                     or #table_priv1.gid = uid
  5734.                     or uid = 0)
  5735.                 and protecttype = 205
  5736.                 and action = 193)
  5737.         and not exists (
  5738.             select * from sysprotects
  5739.             where
  5740.                 id = @table_id
  5741.                 and (#table_priv1.uid = uid
  5742.                     or #table_priv1.gid = uid
  5743.                     or uid = 0)
  5744.                 and protecttype = 206
  5745.                 and action = 193)
  5746.  
  5747.     update #table_priv1
  5748.     set insert_privilege = 1
  5749.     where
  5750.         exists (
  5751.             select * from sysprotects
  5752.             where
  5753.                 id = @table_id
  5754.                 and (#table_priv1.uid = uid
  5755.                     or #table_priv1.gid = uid
  5756.                     or uid = 0)
  5757.                 and protecttype = 205
  5758.                 and action = 195)
  5759.         and not exists (
  5760.             select * from sysprotects
  5761.             where
  5762.                 id = @table_id
  5763.                 and (#table_priv1.uid = uid
  5764.                     or #table_priv1.gid = uid
  5765.                     or uid = 0)
  5766.                 and protecttype = 206
  5767.                 and action = 195)
  5768.  
  5769.     update #table_priv1
  5770.     set delete_privilege = 1
  5771.     where
  5772.         exists (
  5773.             select * from sysprotects
  5774.             where
  5775.                 id = @table_id
  5776.                 and (#table_priv1.uid = uid
  5777.                     or #table_priv1.gid = uid
  5778.                     or uid = 0)
  5779.                 and protecttype = 205
  5780.                 and action = 196)
  5781.         and not exists (select * from sysprotects
  5782.             where
  5783.                 id = @table_id
  5784.                 and (#table_priv1.uid = uid
  5785.                     or #table_priv1.gid = uid
  5786.                     or uid = 0)
  5787.                 and protecttype = 206
  5788.                 and action = 196)
  5789.  
  5790.     update #table_priv1
  5791.     set update_privilege = 1
  5792.     where
  5793.         exists (
  5794.             select * from sysprotects
  5795.             where
  5796.                 id = @table_id
  5797.                 and (#table_priv1.uid = uid
  5798.                     or #table_priv1.gid = uid
  5799.                     or uid = 0)
  5800.                 and protecttype = 205
  5801.                 and action = 197)
  5802.         and not exists (
  5803.             select * from sysprotects
  5804.             where
  5805.                 id = @table_id
  5806.                 and (#table_priv1.uid = uid
  5807.                     or #table_priv1.gid = uid
  5808.                     or uid = 0)
  5809.                 and protecttype = 206
  5810.                 and action = 197)
  5811.  
  5812.     update #table_priv1
  5813.     set references_privilege = 1
  5814.     where
  5815.         exists (
  5816.             select * from sysprotects
  5817.             where
  5818.                 id = @table_id
  5819.                 and (#table_priv1.uid = uid
  5820.                     or #table_priv1.gid = uid
  5821.                     or uid = 0)
  5822.                 and protecttype = 205
  5823.                 and action = 26)
  5824.         and not exists (
  5825.             select * from sysprotects
  5826.             where
  5827.                 id = @table_id
  5828.                 and (#table_priv1.uid = uid
  5829.                     or #table_priv1.gid = uid
  5830.                     or uid = 0)
  5831.                 and protecttype = 206
  5832.                 and action = 26)
  5833.  
  5834.     create table #table_priv2(
  5835.         table_qualifier varchar(32) NULL,
  5836.         table_owner     varchar(32) NULL,
  5837.         table_name        varchar(32) NOT NULL,
  5838.         grantor         varchar(32) NULL,
  5839.         grantee         varchar(32) NOT NULL,
  5840.         privilege        varchar(32) NOT NULL,
  5841.         is_grantable    varchar(3) NULL)
  5842.  
  5843.     insert into #table_priv2
  5844.         select
  5845.             table_qualifier,
  5846.             table_owner,
  5847.             table_name,
  5848.             grantor,
  5849.             grantee,
  5850.             'SELECT',
  5851.             is_grantable
  5852.         from #table_priv1
  5853.         where select_privilege = 1
  5854.  
  5855.  
  5856.     insert into #table_priv2
  5857.         select
  5858.             table_qualifier,
  5859.             table_owner,
  5860.             table_name,
  5861.             grantor,
  5862.             grantee,
  5863.             'INSERT',
  5864.             is_grantable
  5865.         from #table_priv1
  5866.         where insert_privilege = 1
  5867.  
  5868.  
  5869.     insert into #table_priv2
  5870.         select
  5871.             table_qualifier,
  5872.             table_owner,
  5873.             table_name,
  5874.             grantor,
  5875.             grantee,
  5876.             'DELETE',
  5877.             is_grantable
  5878.         from #table_priv1
  5879.         where delete_privilege = 1
  5880.  
  5881.  
  5882.     insert into #table_priv2
  5883.         select
  5884.             table_qualifier,
  5885.             table_owner,
  5886.             table_name,
  5887.             grantor,
  5888.             grantee,
  5889.             'UPDATE',
  5890.             is_grantable
  5891.         from #table_priv1
  5892.         where update_privilege = 1
  5893.  
  5894.     insert into #table_priv2
  5895.         select
  5896.             table_qualifier,
  5897.             table_owner,
  5898.             table_name,
  5899.             grantor,
  5900.             grantee,
  5901.             'REFERENCES',
  5902.             is_grantable
  5903.         from #table_priv1
  5904.         where references_privilege = 1
  5905.  
  5906.  
  5907.     select * from #table_priv2
  5908.     order by table_owner,table_name,privilege,grantee
  5909. /*    order by 2,3,6,5 Can't use since fails on 4.21a server */
  5910. go
  5911.  
  5912. if (charindex('6.50', @@version) = 0 and
  5913.     charindex('7.00', @@version) = 0)
  5914. begin
  5915.     print ''
  5916.     print ''
  5917.     print 'Warning:'
  5918.     print 'you are installing the stored procedures '
  5919.     print 'on a pre 6.50 SQL Server.'
  5920.     print 'Ignore the following errors.'
  5921. end
  5922. else
  5923.     drop proc sp_table_privileges
  5924. go
  5925.  
  5926.  
  5927. /*    Procedure for 6.50 server */
  5928. CREATE PROCEDURE sp_table_privileges (
  5929.             @table_name         varchar(96),
  5930.             @table_owner        varchar(96) = null,
  5931.             @table_qualifier    varchar(32) = null)
  5932. as
  5933.  
  5934.     if @table_qualifier is not null
  5935.     begin
  5936.         if db_name() <> @table_qualifier
  5937.         begin    /* If qualifier doesn't match current database */
  5938.             raiserror (15250, -1,-1)
  5939.             return
  5940.         end
  5941.     end
  5942.     if @table_name is null
  5943.         select @table_name = '%'
  5944.     if @table_owner is null /* If no owner supplied, force wildcard */
  5945.         select @table_owner = '%'
  5946.  
  5947.     select
  5948.         convert(varchar(32),db_name()) TABLE_QUALIFIER,
  5949.         convert(varchar(32),user_name(o.uid)) TABLE_OWNER,
  5950.         convert(varchar(32),object_name(o.id)) TABLE_NAME,
  5951.         convert(varchar(32),user_name(p.grantor)) GRANTOR,
  5952.         convert(varchar(32),user_name(u.uid)) GRANTEE,
  5953.         convert(varchar(32),case p.action
  5954.              when 193 then 'SELECT'
  5955.              when 195 then 'INSERT'
  5956.              when 196 then 'DELETE'
  5957.              when 197 then 'UPDATE'
  5958.              else 'REFERENCES'
  5959.         end) PRIVILEGE,
  5960.         convert(varchar(3),case when p.protecttype = 205 then 'NO'
  5961.             else 'YES'
  5962.         end) IS_GRANTABLE
  5963.     from sysprotects p, sysobjects o, sysusers u
  5964.     where
  5965.         p.id = o.id
  5966.         and o.type in ('U','V','S')
  5967.         and object_name(o.id) like @table_name
  5968.         and user_name(o.uid) like @table_owner
  5969.             /* expand groups */
  5970.         and ((p.uid = u.uid and u.uid <> u.gid) or
  5971.              (p.uid = u.gid and u.uid <> u.gid))
  5972.         and p.protecttype <> 206    /* only grant rows */
  5973.         and p.action in (26,193,195,196,197)
  5974.         and o.uid <> u.uid            /* no rows for owner */
  5975.         and not exists (            /* exclude revoke'd privileges */
  5976.             select *
  5977.             from sysprotects p1
  5978.             where
  5979.                 p1.protecttype = 206
  5980.                 and p1.action = p.action
  5981.                 and p1.id = p.id
  5982.                 and p1.uid = u.uid)
  5983.     union all
  5984.     select    /*    Add rows for table owner */
  5985.         convert(varchar(32),db_name()) TABLE_QUALIFIER,
  5986.         convert(varchar(32),user_name(o.uid)) TABLE_OWNER,
  5987.         convert(varchar(32),object_name(o.id)) TABLE_NAME,
  5988.         convert(varchar(32),user_name(u.uid)) GRANTOR,
  5989.         convert(varchar(32),user_name(o.uid)) GRANTEE,
  5990.         convert(varchar(32),case v.number
  5991.             when 193 then 'SELECT'
  5992.             when 195 then 'INSERT'
  5993.             when 196 then 'DELETE'
  5994.             when 197 then 'UPDATE'
  5995.             else 'REFERENCES'
  5996.         end) PRIVILEGE,
  5997.         convert(varchar(3),'YES') IS_GRANTABLE
  5998.     from sysobjects o, master.dbo.spt_values v, sysusers u
  5999.     where
  6000.         object_name(o.id) like @table_name
  6001.         and o.type in ('U','V','S')
  6002.         and user_name(o.uid) like @table_owner
  6003.         and u.suid = 1        /* grantor is dbo of database */
  6004.         and v.type = 'P'    /* cross product to get all exposed privileges */
  6005.         and v.number in (26,193,195,196,197)
  6006.         and not exists (    /* exclude revoke'd privileges */
  6007.             select *
  6008.             from sysprotects p1
  6009.             where
  6010.                 p1.protecttype = 206
  6011.                 and p1.action = v.number
  6012.                 and p1.id = o.id
  6013.                 and p1.uid = o.uid)
  6014.     order by 2,3,6,5
  6015. go
  6016.  
  6017.  
  6018. if (charindex('7.00', @@version) > 0)
  6019.     drop proc sp_table_privileges
  6020. else
  6021. begin
  6022.     print ''
  6023.     print ''
  6024.     print 'Warning:'
  6025.     print 'you are installing the stored procedures '
  6026.     print 'on a pre 7.0 SQL Server.'
  6027.     print 'Ignore the following errors.'
  6028. end
  6029. go
  6030.  
  6031. /*    Procedure for 7.00 server */
  6032. CREATE PROCEDURE sp_table_privileges (
  6033.             @table_name         nvarchar(384),
  6034.             @table_owner        nvarchar(384) = null,
  6035.             @table_qualifier    sysname = null)
  6036. as
  6037.  
  6038.     if @table_qualifier is not null
  6039.     begin
  6040.         if db_name() <> @table_qualifier
  6041.         begin    /* If qualifier doesn't match current database */
  6042.             raiserror (15250, -1,-1)
  6043.             return
  6044.         end
  6045.     end
  6046.     if @table_name is null
  6047.         select @table_name = '%'
  6048.     if @table_owner is null /* If no owner supplied, force wildcard */
  6049.         select @table_owner = '%'
  6050.  
  6051.     select
  6052.         convert(sysname,db_name()) TABLE_QUALIFIER,
  6053.         convert(sysname,user_name(o.uid)) TABLE_OWNER,
  6054.         convert(sysname,object_name(o.id)) TABLE_NAME,
  6055.         convert(sysname,user_name(p.grantor)) GRANTOR,
  6056.         convert(sysname,user_name(u.uid)) GRANTEE,
  6057.         convert(varchar(32),case p.action
  6058.              when 193 then 'SELECT'
  6059.              when 195 then 'INSERT'
  6060.              when 196 then 'DELETE'
  6061.              when 197 then 'UPDATE'
  6062.              else 'REFERENCES'
  6063.         end) PRIVILEGE,
  6064.         convert(varchar(3),case when p.protecttype = 205 then 'NO'
  6065.             else 'YES'
  6066.         end) IS_GRANTABLE
  6067.     from sysprotects p, sysobjects o, sysusers u, sysmembers m
  6068.     where
  6069.         p.id = o.id
  6070.         and o.type in ('U','V','S')
  6071.         and object_name(o.id) like @table_name
  6072.         and user_name(o.uid) like @table_owner
  6073.             /* expand groups - AKUNDONE: only 1 level of grp unrolling. */
  6074.         and (u.uid > 0 and u.uid < 16384)
  6075.         and ((p.uid = u.uid) or
  6076.              (p.uid = m.groupuid and u.uid = m.memberuid))
  6077.         and p.protecttype <> 206    /* only grant rows */
  6078.         and p.action in (26,193,195,196,197)
  6079.         and o.uid <> u.uid            /* no rows for owner */
  6080.         and not exists (            /* exclude revoke'd privileges */
  6081.             select *
  6082.             from sysprotects p1
  6083.             where
  6084.                 p1.protecttype = 206
  6085.                 and p1.action = p.action
  6086.                 and p1.id = p.id
  6087.                 and p1.uid = u.uid)
  6088.     union all
  6089.     select    /*    Add rows for table owner */
  6090.         convert(sysname,db_name()) TABLE_QUALIFIER,
  6091.         convert(sysname,user_name(o.uid)) TABLE_OWNER,
  6092.         convert(sysname,object_name(o.id)) TABLE_NAME,
  6093.         convert(sysname,user_name(u.uid)) GRANTOR,
  6094.         convert(sysname,user_name(o.uid)) GRANTEE,
  6095.         convert(varchar(32),case v.number
  6096.             when 193 then 'SELECT'
  6097.             when 195 then 'INSERT'
  6098.             when 196 then 'DELETE'
  6099.             when 197 then 'UPDATE'
  6100.             else 'REFERENCES'
  6101.         end) PRIVILEGE,
  6102.         convert(varchar(3),'YES') IS_GRANTABLE
  6103.     from sysobjects o, master.dbo.spt_values v, sysusers u
  6104.     where
  6105.         object_name(o.id) like @table_name
  6106.         and o.type in ('U','V','S')
  6107.         and user_name(o.uid) like @table_owner
  6108.         and u.uid = 1        /* grantor is 'dbo' of database */
  6109.         and v.type = 'P'    /* cross product to get all exposed privileges */
  6110.         and v.number in (26,193,195,196,197)
  6111.         and not exists (    /* exclude revoke'd privileges */
  6112.             select *
  6113.             from sysprotects p1
  6114.             where
  6115.                 p1.protecttype = 206
  6116.                 and p1.action = v.number
  6117.                 and p1.id = o.id
  6118.                 and p1.uid = o.uid)
  6119.     order by 2,3,6,5
  6120. go
  6121.  
  6122. grant execute on sp_table_privileges to public
  6123. go
  6124.  
  6125. dump tran master with no_log
  6126. go
  6127.  
  6128. print 'creating sp_tables'
  6129. go
  6130.  
  6131. /*    Procedure for 6.50 and earlier servers */
  6132. create procedure sp_tables(
  6133.                @table_name        varchar(96)    = null,
  6134.                @table_owner     varchar(96)    = null,
  6135.                @table_qualifier varchar(32)    = null,
  6136.                @table_type        varchar(100) = null)
  6137. as
  6138.     declare @type1 varchar(3)
  6139.     declare @tableindex int
  6140.  
  6141.  
  6142.     /* Special feature #1:    enumerate databases when owner and name
  6143.          are blank but qualifier is explicitly '%'.  */
  6144.     if @table_qualifier = '%' and
  6145.         @table_owner = '' and
  6146.         @table_name = ''
  6147.     begin    /* If enumerating databases */
  6148.         select
  6149.             TABLE_QUALIFIER = convert(varchar(32),d.name),
  6150.             TABLE_OWNER = convert(varchar(32),null),
  6151.             TABLE_NAME = convert(varchar(32),null),
  6152.             TABLE_TYPE = convert(varchar(32),null),
  6153.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  6154.         from master.dbo.sysdatabases d
  6155.         where d.name <> 'model'    /* eliminate MODEL database */
  6156.         order by 1
  6157.     end
  6158.  
  6159.     /* Special feature #2:    enumerate owners when qualifier and name
  6160.          are blank but owner is explicitly '%'.  */
  6161.     else if @table_qualifier = '' and
  6162.         @table_owner = '%' and
  6163.         @table_name = ''
  6164.     begin    /* If enumerating owners */
  6165.         select distinct
  6166.             TABLE_QUALIFIER = convert(varchar(32),null),
  6167.             TABLE_OWNER = convert(varchar(32),user_name(uid)),
  6168.             TABLE_NAME = convert(varchar(32),null),
  6169.             TABLE_TYPE = convert(varchar(32),null),
  6170.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  6171.         from sysobjects
  6172.         order by 2
  6173.     end
  6174.  
  6175.     /* Special feature #3:    enumerate table types when qualifier, owner and
  6176.          name are blank but table type is explicitly '%'.    */
  6177.     else if @table_qualifier = '' and
  6178.         @table_owner = '' and
  6179.         @table_name = '' and
  6180.         @table_type = '%'
  6181.     begin    /* If enumerating table types */
  6182.         select
  6183.             TABLE_QUALIFIER = convert(varchar(32),null),
  6184.             TABLE_OWNER = convert(varchar(32),null),
  6185.             TABLE_NAME = convert(varchar(32),null),
  6186.             TABLE_TYPE = convert(varchar(32),rtrim(substring('SYSTEM TABLETABLE       VIEW',(colid-1)*12+1,12))),
  6187.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  6188.         from sysobjects o, syscolumns c
  6189.         where o.id=c.id and o.name='sysusers' and colid<=3
  6190.     end
  6191.  
  6192.     else
  6193.     begin /* end of special features - do normal processing */
  6194.         if @table_qualifier is not null
  6195.         begin
  6196.             if db_name() <> @table_qualifier
  6197.             begin
  6198.                 if @table_qualifier = ''
  6199.                 begin  /* If empty qualifier supplied */
  6200.                     /* Force an empty result set */
  6201.                     select @table_name = ''
  6202.                     select @table_owner = ''
  6203.                 end
  6204.                 else
  6205.                 begin    /* If qualifier doesn't match current database */
  6206.                     raiserror 20001 '~~Rush_5~~'
  6207.                     return
  6208.                 end
  6209.             end
  6210.         end
  6211.         if @table_type is null
  6212.         begin    /* Select all ODBC supported table types */
  6213.             select @type1 = 'SUV'
  6214.         end
  6215.         else
  6216.         begin
  6217.             /*    TableType is case sensitive if CS server */
  6218.             select @type1 = null
  6219.             if (charindex('''SYSTEM TABLE''',@table_type) <> 0)
  6220.                 select @type1 = @type1 + 'S'    /* Add System Tables */
  6221.             if (charindex('''TABLE''',@table_type) <> 0)
  6222.                 select @type1 = @type1 + 'U'    /* Add User Tables */
  6223.             if (charindex('''VIEW''',@table_type) <> 0)
  6224.                 select @type1 = @type1 + 'V'    /* Add Views */
  6225.         end
  6226.         if @table_name is null
  6227.         begin    /*    If table name not supplied, match all */
  6228.             select @table_name = '%'
  6229.         end
  6230.         else
  6231.         begin
  6232.             if (@table_owner is null) and (charindex('%', @table_name) = 0)
  6233.             begin    /* If owner not specified and table is specified */
  6234.                 if exists (select * from sysobjects
  6235.                     where uid = user_id()
  6236.                     and name = @table_name
  6237.                     and (type = 'U' or type = 'V' or type = 'S'))
  6238.                 begin    /* Override supplied owner w/owner of table */
  6239.                     select @table_owner = user_name()
  6240.                 end
  6241.             end
  6242.         end
  6243.         if @table_owner is null /* If no owner supplied, force wildcard */
  6244.             select @table_owner = '%'
  6245.         select
  6246.             TABLE_QUALIFIER = convert(varchar(32),db_name()),
  6247.             TABLE_OWNER = convert(varchar(32),user_name(o.uid)),
  6248.             TABLE_NAME = convert(varchar(32),o.name),    /* make nullable */
  6249.             TABLE_TYPE = convert(varchar(32),rtrim(
  6250.                 substring('SYSTEM TABLE            TABLE       VIEW       ',
  6251.                     (ascii(o.type)-83)*12+1,12))),    /* 'S'=0,'U'=2,'V'=3 */
  6252.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  6253.         from sysusers u, sysobjects o
  6254.         where
  6255.             o.name like @table_name
  6256.             and user_name(o.uid) like @table_owner
  6257.             and charindex(substring(o.type,1,1),@type1) <> 0 /* Only desired types */
  6258.             and u.uid = user_id() /* constrain sysusers uid for use in subquery */
  6259.             and (
  6260.                 suser_id() = 1     /* User is the System Administrator */
  6261.                 or o.uid = user_id()     /* User created the object */
  6262.                 /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  6263.                 or ((select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  6264.                     from sysprotects p
  6265.                     /* outer join to correlate with all rows in sysobjects */
  6266.                     where p.id =* o.id
  6267.                         /* get rows for public,current user,user's group */
  6268.                         and (p.uid = 0 or p.uid = user_id() or p.uid =* u.gid)
  6269.                         /* check for SELECT,EXECUTE privilege */
  6270.                         and (action in (193,224)))&1     /* more magic...normalize GRANT */
  6271.                     ) = 1    /* final magic...compare Grants      */
  6272.             )
  6273.         order by 4, 1, 2, 3
  6274.     end
  6275. go
  6276.  
  6277. if (charindex('7.00', @@version) > 0)
  6278.     drop procedure sp_tables
  6279. else
  6280. begin
  6281.     print ''
  6282.     print ''
  6283.     print 'Warning:'
  6284.     print 'you are installing the stored procedures '
  6285.     print 'on a pre 7.0 SQL Server.'
  6286.     print 'Ignore the following errors.'
  6287. end
  6288. go
  6289.  
  6290. /*    Procedure for 7.00 server */
  6291. create procedure sp_tables(
  6292.                @table_name        nvarchar(384)    = null,
  6293.                @table_owner     nvarchar(384)    = null,
  6294.                @table_qualifier sysname    = null,
  6295.                @table_type        varchar(100) = null)
  6296. as
  6297.     declare @type1 varchar(3)
  6298.     declare @tableindex int
  6299.  
  6300.  
  6301.     /* Special feature #1:    enumerate databases when owner and name
  6302.          are blank but qualifier is explicitly '%'.  */
  6303.     if @table_qualifier = '%' and
  6304.         @table_owner = '' and
  6305.         @table_name = ''
  6306.     begin    /* If enumerating databases */
  6307.         select
  6308.             TABLE_QUALIFIER = convert(sysname,d.name),
  6309.             TABLE_OWNER = convert(sysname,null),
  6310.             TABLE_NAME = convert(sysname,null),
  6311.             TABLE_TYPE = convert(varchar(32),null),
  6312.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  6313.         from master.dbo.sysdatabases d
  6314.         where d.name <> 'model'    /* eliminate MODEL database */
  6315.         order by 1
  6316.     end
  6317.  
  6318.     /* Special feature #2:    enumerate owners when qualifier and name
  6319.          are blank but owner is explicitly '%'.  */
  6320.     else if @table_qualifier = '' and
  6321.         @table_owner = '%' and
  6322.         @table_name = ''
  6323.     begin    /* If enumerating owners */
  6324.         select distinct
  6325.             TABLE_QUALIFIER = convert(sysname,null),
  6326.             TABLE_OWNER = convert(sysname,user_name(uid)),
  6327.             TABLE_NAME = convert(sysname,null),
  6328.             TABLE_TYPE = convert(varchar(32),null),
  6329.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  6330.         from sysobjects
  6331.         order by 2
  6332.     end
  6333.  
  6334.     /* Special feature #3:    enumerate table types when qualifier, owner and
  6335.          name are blank but table type is explicitly '%'.    */
  6336.     else if @table_qualifier = '' and
  6337.         @table_owner = '' and
  6338.         @table_name = '' and
  6339.         @table_type = '%'
  6340.     begin    /* If enumerating table types */
  6341.         select
  6342.             TABLE_QUALIFIER = convert(sysname,null),
  6343.             TABLE_OWNER = convert(sysname,null),
  6344.             TABLE_NAME = convert(sysname,null),
  6345.             TABLE_TYPE = convert(varchar(32),rtrim(substring('SYSTEM TABLETABLE       VIEW',(colid-1)*12+1,12))),
  6346.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  6347.         from sysobjects o, syscolumns c
  6348.         where o.id=c.id and o.name='sysusers' and colid<=3
  6349.     end
  6350.  
  6351.     else
  6352.     begin /* end of special features - do normal processing */
  6353.         if @table_qualifier is not null
  6354.         begin
  6355.             if db_name() <> @table_qualifier
  6356.             begin
  6357.                 if @table_qualifier = ''
  6358.                 begin  /* If empty qualifier supplied */
  6359.                     /* Force an empty result set */
  6360.                     select @table_name = ''
  6361.                     select @table_owner = ''
  6362.                 end
  6363.                 else
  6364.                 begin    /* If qualifier doesn't match current database */
  6365.                     raiserror (15250, -1,-1)
  6366.                     return
  6367.                 end
  6368.             end
  6369.         end
  6370.         if @table_type is null
  6371.         begin    /* Select all ODBC supported table types */
  6372.             select @type1 = 'SUV'
  6373.         end
  6374.         else
  6375.         begin
  6376.             /*    TableType is case sensitive if CS server */
  6377.             select @type1 = ''
  6378.             if (charindex('''SYSTEM TABLE''',@table_type) <> 0)
  6379.                 select @type1 = @type1 + 'S'    /* Add System Tables */
  6380.             if (charindex('''TABLE''',@table_type) <> 0)
  6381.                 select @type1 = @type1 + 'U'    /* Add User Tables */
  6382.             if (charindex('''VIEW''',@table_type) <> 0)
  6383.                 select @type1 = @type1 + 'V'    /* Add Views */
  6384.         end
  6385.         if @table_name is null
  6386.         begin    /*    If table name not supplied, match all */
  6387.             select @table_name = '%'
  6388.         end
  6389.         else
  6390.         begin
  6391.             if (@table_owner is null) and (charindex('%', @table_name) = 0)
  6392.             begin    /* If owner not specified and table is specified */
  6393.                 if exists (select * from sysobjects
  6394.                     where uid = user_id()
  6395.                     and name = @table_name
  6396.                     and (type = 'U' or type = 'V' or type = 'S'))
  6397.                 begin    /* Override supplied owner w/owner of table */
  6398.                     select @table_owner = user_name()
  6399.                 end
  6400.             end
  6401.         end
  6402.         if @table_owner is null /* If no owner supplied, force wildcard */
  6403.             select @table_owner = '%'
  6404.         select
  6405.             TABLE_QUALIFIER = convert(sysname,db_name()),
  6406.             TABLE_OWNER = convert(sysname,user_name(o.uid)),
  6407.             TABLE_NAME = convert(sysname,o.name),    /* make nullable */
  6408.             TABLE_TYPE = convert(varchar(32),rtrim(
  6409.                 substring('SYSTEM TABLE            TABLE       VIEW       ',
  6410.                     (ascii(o.type)-83)*12+1,12))),    /* 'S'=0,'U'=2,'V'=3 */
  6411.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  6412.         from sysobjects o
  6413.         where
  6414.             o.name like @table_name
  6415.             and user_name(o.uid) like @table_owner
  6416.             and (o.type in ('U','S') or
  6417.              (o.type = 'V' and ObjectProperty (o.id, 'IsMSShipped') = 0))
  6418.             and charindex(substring(o.type,1,1),@type1) <> 0 /* Only desired types */
  6419.             and permissions (o.id)&4096 <> 0
  6420.         order by 4, 1, 2, 3
  6421.     end
  6422. go
  6423.  
  6424.  
  6425. grant execute on sp_tables to public
  6426. go
  6427.  
  6428. dump tran master with no_log
  6429. go
  6430.  
  6431. if (charindex('6.50', @@version) = 0 and
  6432.     charindex('7.00', @@version) = 0)
  6433. begin
  6434.     print ''
  6435.     print ''
  6436.     print 'Warning:'
  6437.     print 'you are installing the stored procedures '
  6438.     print 'on a pre 6.50 SQL Server.'
  6439.     print 'Ignore the following errors.'
  6440. end
  6441.  
  6442. print 'creating sp_ddopen'
  6443. go
  6444.  
  6445. /*    Procedure for pre-7.00 server */
  6446. create procedure sp_ddopen(
  6447.                @handle            int output,
  6448.                @procname        sysname,
  6449.                @scrollopt        int output,
  6450.                @ccopt            int output,
  6451.                @rows            int output,
  6452.                @p1                varchar(255) = null,
  6453.                @p2                varchar(255) = null,
  6454.                @p3                varchar(255) = null,
  6455.                @p4                varchar(255) = null,
  6456.                @p5                varchar(255) = null,
  6457.                @p6                varchar(255) = null,
  6458.                @p7                int = null,
  6459.                @ODBCVer         int = 2)
  6460. as
  6461.     set nocount on
  6462.     declare @ret int
  6463.  
  6464.     if @procname = 'sp_column_privileges'
  6465.     begin
  6466.         create table #spcolpriv (
  6467.             TABLE_QUALIFIER varchar(32) null,
  6468.             TABLE_OWNER varchar(32) null,
  6469.             TABLE_NAME varchar(32)    not null,
  6470.             COLUMN_NAME varchar(32) not null,
  6471.             GRANTOR varchar(32) null,
  6472.             GRANTEE varchar(32) not null,
  6473.             PRIVILEGE varchar(32) not null,
  6474.             IS_GRANTABLE varchar(3) null
  6475.             )
  6476.         insert into #spcolpriv exec sp_column_privileges @p1,@p2,@p3,@p4
  6477.         exec @ret = sp_cursoropen @handle output,
  6478.             'select * from #spcolpriv',
  6479.             @scrollopt output, @ccopt output, @rows output
  6480.         drop table #spcolpriv
  6481.     end
  6482.     else if @procname = 'sp_columns'
  6483.     begin
  6484.         create table #spcolumns (
  6485.             TABLE_QUALIFIER varchar(32) null,
  6486.             TABLE_OWNER varchar(32) null,
  6487.             TABLE_NAME varchar(32)    not null,
  6488.             COLUMN_NAME varchar(32) not null,
  6489.             DATA_TYPE smallint not null,
  6490.             TYPE_NAME varchar(32) not null,
  6491.             "PRECISION" int null,
  6492.             LENGTH int null,
  6493.             SCALE smallint null,
  6494.             RADIX smallint null,
  6495.             NULLABLE smallint not null,
  6496.             REMARKS varchar(254) null,
  6497.             COLUMN_DEF varchar(255) null,
  6498.             SQL_DATA_TYPE smallint not null,
  6499.             SQL_DATETIME_SUB smallint null,
  6500.             CHAR_OCTET_LENGTH int null,
  6501.             ORDINAL_POSITION int not null,
  6502.             IS_NULLABLE varchar(254) null,
  6503.             SS_DATA_TYPE tinyint null
  6504.             )
  6505.         insert into #spcolumns exec sp_columns @p1,@p2,@p3,@p4,@ODBCVer
  6506.         exec @ret = sp_cursoropen @handle output,
  6507.             'select * from #spcolumns',
  6508.             @scrollopt output, @ccopt output, @rows output
  6509.         drop table #spcolumns
  6510.     end
  6511.     else if @procname = 'sp_datatype_info'
  6512.     begin
  6513.         create table #spdatatypeinfo (
  6514.             TYPE_NAME            varchar(32)  not null,
  6515.             DATA_TYPE            smallint not null,
  6516.             "PRECISION"            int null,
  6517.             LITERAL_PREFIX        varchar(32)    null,
  6518.             LITERAL_SUFFIX        varchar(32)    null,
  6519.             CREATE_PARAMS        varchar(32)    null,
  6520.             NULLABLE            smallint   not null,
  6521.             CASE_SENSITIVE        smallint   not null,
  6522.             SEARCHABLE            smallint   not null,
  6523.             UNSIGNED_ATTRIBUTE    smallint   null,
  6524.             MONEY    smallint    not null,
  6525.             AUTO_INCREMENT        smallint    null,
  6526.             LOCAL_TYPE_NAME     varchar(32) null,
  6527.             MINIMUM_SCALE        smallint     null,
  6528.             MAXIMUM_SCALE        smallint   null,
  6529.             SQL_DATA_TYPE        smallint      not null,
  6530.             SQL_DATETIME_SUB    smallint   null,
  6531.             NUM_PREC_RADIX        int     null,
  6532.             INTERVAL_PRECISION    smallint    NULL,
  6533.             USERTYPE            smallint not null)
  6534.         insert into #spdatatypeinfo exec sp_datatype_info @p7,@ODBCVer
  6535.         exec @ret = sp_cursoropen @handle output,
  6536.             'select * from #spdatatypeinfo',
  6537.             @scrollopt output, @ccopt output, @rows output
  6538.         drop table #spdatatypeinfo
  6539.     end
  6540.     else if @procname = 'sp_fkeys'
  6541.     begin
  6542.         create table #spfkeys (
  6543.             PKTABLE_QUALIFIER varchar(32)     null,
  6544.             PKTABLE_OWNER varchar(32)    null,
  6545.             PKTABLE_NAME varchar(32)  not null,
  6546.             PKCOLUMN_NAME varchar(32)    not null,
  6547.             FKTABLE_QUALIFIER varchar(32)    null,
  6548.             FKTABLE_OWNER varchar(32)    null,
  6549.             FKTABLE_NAME varchar(32)  not null,
  6550.             FKCOLUMN_NAME varchar(32)    not null,
  6551.             KEY_SEQ smallint not null,
  6552.             UPDATE_RULE smallint null,
  6553.             DELETE_RULE smallint null,
  6554.             FK_NAME varchar(32) null,
  6555.             PK_NAME varchar(32) null,
  6556.             DEFERRABILITY smallint null
  6557.             )
  6558.         insert into #spfkeys exec sp_fkeys @p1,@p2,@p3,@p4,@p5,@p6
  6559.         exec @ret = sp_cursoropen @handle output,
  6560.             'select * from #spfkeys',
  6561.             @scrollopt output, @ccopt output, @rows output
  6562.         drop table #spfkeys
  6563.     end
  6564.     else if @procname = 'sp_pkeys'
  6565.     begin
  6566.         create table #sppkeys (
  6567.             TABLE_QUALIFIER varchar(32)   null,
  6568.             TABLE_OWNER varchar(32)   null,
  6569.             TABLE_NAME varchar(32)    not null,
  6570.             COLUMN_NAME varchar(32)  not null,
  6571.             KEY_SEQ smallint not null,
  6572.             PK_NAME varchar(32) null
  6573.             )
  6574.         insert into #sppkeys exec sp_pkeys @p1,@p2,@p3
  6575.         exec @ret = sp_cursoropen @handle output,
  6576.             'select * from #sppkeys',
  6577.             @scrollopt output, @ccopt output, @rows output
  6578.         drop table #sppkeys
  6579.     end
  6580.     else if @procname = 'sp_special_columns'
  6581.     begin
  6582.         create table #spspeccol (
  6583.             SCOPE smallint null,
  6584.             COLUMN_NAME varchar(32) not null,
  6585.             DATA_TYPE smallint not null,
  6586.             TYPE_NAME varchar(32) not null,
  6587.             "PRECISION" int null,
  6588.             LENGTH int null,
  6589.             SCALE smallint null,
  6590.             PSEUDO_COLUMN smallint null
  6591.             )
  6592.         insert into #spspeccol exec sp_special_columns @p1,@p2,@p3,@p4,@p5,@p6,@ODBCVer
  6593.         exec @ret = sp_cursoropen @handle output,
  6594.             'select * from #spspeccol',
  6595.             @scrollopt output, @ccopt output, @rows output
  6596.         drop table #spspeccol
  6597.     end
  6598.     else if @procname = 'sp_sproc_columns'
  6599.     begin
  6600.         create table #spproccol (
  6601.             PROCEDURE_QUALIFIER varchar(32)  null,
  6602.             PROCEDURE_OWNER varchar(32)  null,
  6603.             PROCEDURE_NAME varchar(32)    not null,
  6604.             COLUMN_NAME varchar(32) not null,
  6605.             COLUMN_TYPE smallint not null,
  6606.             DATA_TYPE smallint not null,
  6607.             TYPE_NAME varchar(32) not null,
  6608.             "PRECISION" int null,
  6609.             LENGTH int null,
  6610.             SCALE smallint null,
  6611.             RADIX smallint null,
  6612.             NULLABLE smallint not null,
  6613.             REMARKS varchar(254) null,
  6614.             COLUMN_DEF varchar(255) null,
  6615.             SQL_DATA_TYPE smallint not null,
  6616.             SQL_DATETIME_SUB smallint null,
  6617.             CHAR_OCTET_LENGTH int null,
  6618.             ORDINAL_POSITION int not null,
  6619.             IS_NULLABLE varchar(254) null,
  6620.             SS_DATA_TYPE tinyint null
  6621.             )
  6622.         insert into #spproccol exec sp_sproc_columns @p1,@p2,@p3,@p4,@ODBCVer
  6623.         exec @ret = sp_cursoropen @handle output,
  6624.             'select * from #spproccol',
  6625.             @scrollopt output, @ccopt output, @rows output
  6626.         drop table #spproccol
  6627.     end
  6628.     else if @procname = 'sp_statistics'
  6629.     begin
  6630.         create table #spstatistics (
  6631.             TABLE_QUALIFIER varchar(32)   null,
  6632.             TABLE_OWNER varchar(32)   null,
  6633.             TABLE_NAME varchar(32)    not null,
  6634.             NON_UNIQUE smallint null,
  6635.             INDEX_QUALIFIER varchar(32) null,
  6636.             INDEX_NAME varchar(32)    null,
  6637.             TYPE smallint not null,
  6638.             SEQ_IN_INDEX smallint null,
  6639.             COLUMN_NAME varchar(32) null,
  6640.             COLLATION char(1) null,
  6641.             CARDINALITY int null,
  6642.             PAGES int null,
  6643.             FILTER_CONDITION varchar(128) null
  6644.             )
  6645.         insert into #spstatistics exec sp_statistics @p1,@p2,@p3,@p4,@p5,@p6
  6646.         exec @ret = sp_cursoropen @handle output,
  6647.             'select * from #spstatistics',
  6648.             @scrollopt output, @ccopt output, @rows output
  6649.         drop table #spstatistics
  6650.     end
  6651.     else if @procname = 'sp_stored_procedures'
  6652.     begin
  6653.         create table #spprocedures (
  6654.             PROCEDURE_QUALIFIER varchar(32)  null,
  6655.             PROCEDURE_OWNER varchar(32)  null,
  6656.             PROCEDURE_NAME varchar(32)    not null,
  6657.             NUM_INPUT_PARAMS int null,
  6658.             NUM_OUTPUT_PARAMS int null,
  6659.             NUM_RESULT_SETS int null,
  6660.             REMARKS varchar(254) null,
  6661.             PROCEDURE_TYPE smallint null
  6662.             )
  6663.         insert into #spprocedures exec sp_stored_procedures @p1,@p2,@p3
  6664.         exec @ret = sp_cursoropen @handle output,
  6665.             'select * from #spprocedures',
  6666.             @scrollopt output, @ccopt output, @rows output
  6667.         drop table #spprocedures
  6668.     end
  6669.     else if @procname = 'sp_table_privileges'
  6670.     begin
  6671.         create table #sptabpriv (
  6672.             TABLE_QUALIFIER varchar(32) null,
  6673.             TABLE_OWNER varchar(32) null,
  6674.             TABLE_NAME varchar(32)    not null,
  6675.             GRANTOR varchar(32) null,
  6676.             GRANTEE varchar(32) not null,
  6677.             PRIVILEGE varchar(32) not null,
  6678.             IS_GRANTABLE varchar(3) null
  6679.             )
  6680.         insert into #sptabpriv exec sp_table_privileges @p1,@p2,@p3
  6681.         exec @ret = sp_cursoropen @handle output,
  6682.             'select * from #sptabpriv',
  6683.             @scrollopt output, @ccopt output, @rows output
  6684.         drop table #sptabpriv
  6685.     end
  6686.     else if @procname = 'sp_tables'
  6687.     begin
  6688.         create table #sptables (
  6689.             TABLE_QUALIFIER varchar(32) null,
  6690.             TABLE_OWNER varchar(32) null,
  6691.             TABLE_NAME varchar(32)    null,
  6692.             TABLE_TYPE     varchar(32) null,
  6693.             REMARKS varchar(254) null)
  6694.         insert into #sptables exec sp_tables @p1,@p2,@p3,@p4
  6695.         exec @ret = sp_cursoropen @handle output,
  6696.             'select * from #sptables',
  6697.             @scrollopt output, @ccopt output, @rows output
  6698.         drop table #sptables
  6699.     end
  6700.     select @ret = isnull(@ret,0)
  6701.     return isnull(@ret,0)
  6702. go
  6703.  
  6704. if (charindex('7.00', @@version) > 0)
  6705.     drop procedure sp_ddopen
  6706. else
  6707. begin
  6708.     print ''
  6709.     print ''
  6710.     print 'Warning:'
  6711.     print 'you are installing the stored procedures '
  6712.     print 'on a pre 7.0 SQL Server.'
  6713.     print 'Ignore the following errors.'
  6714. end
  6715. go
  6716.  
  6717. /*    Procedure for 7.00 server */
  6718. create procedure sp_ddopen(
  6719.                @handle            int output,
  6720.                @procname        sysname,
  6721.                @scrollopt        int output,
  6722.                @ccopt            int output,
  6723.                @rows            int output,
  6724.                @p1                nvarchar(774) = null,
  6725.                @p2                nvarchar(774) = null,
  6726.                @p3                nvarchar(774) = null,
  6727.                @p4                nvarchar(774) = null,
  6728.                @p5                nvarchar(774) = null,
  6729.                @p6                nvarchar(774) = null,
  6730.                @p7                int = null,
  6731.                @ODBCVer         int = 2)
  6732. as
  6733.     set nocount on
  6734.     declare @ret int
  6735.  
  6736.     if @procname = 'sp_column_privileges'
  6737.     begin
  6738.         create table #spcolpriv (
  6739.             TABLE_QUALIFIER sysname null,
  6740.             TABLE_OWNER sysname null,
  6741.             TABLE_NAME sysname not null,
  6742.             COLUMN_NAME sysname not null,
  6743.             GRANTOR sysname null,
  6744.             GRANTEE sysname not null,
  6745.             PRIVILEGE varchar(32) not null,
  6746.             IS_GRANTABLE varchar(3) null
  6747.             )
  6748.         insert into #spcolpriv exec sp_column_privileges @p1,@p2,@p3,@p4
  6749.         exec @ret = sp_cursoropen @handle output,
  6750.             'select * from #spcolpriv',
  6751.             @scrollopt output, @ccopt output, @rows output
  6752.         drop table #spcolpriv
  6753.     end
  6754.     else if @procname = 'sp_columns' or @procname = 'sp_columns_ex'
  6755.     begin
  6756.         create table #spcolumns (
  6757.             TABLE_QUALIFIER sysname null,
  6758.             TABLE_OWNER sysname null,
  6759.             TABLE_NAME sysname not null,
  6760.             COLUMN_NAME sysname not null,
  6761.             DATA_TYPE smallint not null,
  6762.             TYPE_NAME sysname not null,
  6763.             "PRECISION" int null,
  6764.             LENGTH int null,
  6765.             SCALE smallint null,
  6766.             RADIX smallint null,
  6767.             NULLABLE smallint not null,
  6768.             REMARKS varchar(254) null,
  6769.             COLUMN_DEF nvarchar(3000) null,
  6770.             SQL_DATA_TYPE smallint not null,
  6771.             SQL_DATETIME_SUB smallint null,
  6772.             CHAR_OCTET_LENGTH int null,
  6773.             ORDINAL_POSITION int not null,
  6774.             IS_NULLABLE varchar(254) null,
  6775.             SS_DATA_TYPE tinyint null
  6776.             )
  6777.         if @procname = 'sp_columns'
  6778.         begin
  6779.             insert into #spcolumns exec sp_columns @p1,@p2,@p3,@p4,@ODBCVer
  6780.         end
  6781.         else
  6782.         begin
  6783.             insert into #spcolumns exec sp_columns_ex @p1,@p2,@p3,@p4,@p5,@ODBCVer
  6784.         end
  6785.         exec @ret = sp_cursoropen @handle output,
  6786.             'select * from #spcolumns',
  6787.             @scrollopt output, @ccopt output, @rows output
  6788.         drop table #spcolumns
  6789.     end
  6790.     else if @procname = 'sp_datatype_info'
  6791.     begin
  6792.         create table #spdatatypeinfo (
  6793.             TYPE_NAME            sysname  not null,
  6794.             DATA_TYPE            smallint not null,
  6795.             "PRECISION"            int null,
  6796.             LITERAL_PREFIX        varchar(32)    null,
  6797.             LITERAL_SUFFIX        varchar(32)    null,
  6798.             CREATE_PARAMS        varchar(32)    null,
  6799.             NULLABLE            smallint   not null,
  6800.             CASE_SENSITIVE        smallint   not null,
  6801.             SEARCHABLE            smallint   not null,
  6802.             UNSIGNED_ATTRIBUTE    smallint   null,
  6803.             MONEY    smallint    not null,
  6804.             AUTO_INCREMENT        smallint    null,
  6805.             LOCAL_TYPE_NAME     sysname null,
  6806.             MINIMUM_SCALE        smallint     null,
  6807.             MAXIMUM_SCALE        smallint   null,
  6808.             SQL_DATA_TYPE        smallint      not null,
  6809.             SQL_DATETIME_SUB    smallint   null,
  6810.             NUM_PREC_RADIX        int     null,
  6811.             INTERVAL_PRECISION    smallint    NULL,
  6812.             USERTYPE            smallint not null)
  6813.         insert into #spdatatypeinfo exec sp_datatype_info @p7,@ODBCVer
  6814.         exec @ret = sp_cursoropen @handle output,
  6815.             'select * from #spdatatypeinfo',
  6816.             @scrollopt output, @ccopt output, @rows output
  6817.         drop table #spdatatypeinfo
  6818.     end
  6819.     else if @procname = 'sp_fkeys'
  6820.     begin
  6821.         create table #spfkeys (
  6822.             PKTABLE_QUALIFIER sysname     null,
  6823.             PKTABLE_OWNER sysname    null,
  6824.             PKTABLE_NAME sysname  not null,
  6825.             PKCOLUMN_NAME sysname  not null,
  6826.             FKTABLE_QUALIFIER sysname    null,
  6827.             FKTABLE_OWNER sysname    null,
  6828.             FKTABLE_NAME sysname  not null,
  6829.             FKCOLUMN_NAME sysname  not null,
  6830.             KEY_SEQ smallint not null,
  6831.             UPDATE_RULE smallint null,
  6832.             DELETE_RULE smallint null,
  6833.             FK_NAME sysname null,
  6834.             PK_NAME sysname null,
  6835.             DEFERRABILITY smallint null
  6836.             )
  6837.         insert into #spfkeys exec sp_fkeys @p1,@p2,@p3,@p4,@p5,@p6
  6838.         exec @ret = sp_cursoropen @handle output,
  6839.             'select * from #spfkeys',
  6840.             @scrollopt output, @ccopt output, @rows output
  6841.         drop table #spfkeys
  6842.     end
  6843.     else if @procname = 'sp_pkeys'
  6844.     begin
  6845.         create table #sppkeys (
  6846.             TABLE_QUALIFIER sysname   null,
  6847.             TABLE_OWNER sysname   null,
  6848.             TABLE_NAME sysname    not null,
  6849.             COLUMN_NAME sysname  not null,
  6850.             KEY_SEQ smallint not null,
  6851.             PK_NAME sysname null
  6852.             )
  6853.         insert into #sppkeys exec sp_pkeys @p1,@p2,@p3
  6854.         exec @ret = sp_cursoropen @handle output,
  6855.             'select * from #sppkeys',
  6856.             @scrollopt output, @ccopt output, @rows output
  6857.         drop table #sppkeys
  6858.     end
  6859.     else if @procname = 'sp_special_columns'
  6860.     begin
  6861.         create table #spspeccol (
  6862.             SCOPE smallint null,
  6863.             COLUMN_NAME sysname not null,
  6864.             DATA_TYPE smallint not null,
  6865.             TYPE_NAME sysname not null,
  6866.             "PRECISION" int null,
  6867.             LENGTH int null,
  6868.             SCALE smallint null,
  6869.             PSEUDO_COLUMN smallint null
  6870.             )
  6871.         insert into #spspeccol exec sp_special_columns @p1,@p2,@p3,@p4,@p5,@p6,@ODBCVer
  6872.         exec @ret = sp_cursoropen @handle output,
  6873.             'select * from #spspeccol',
  6874.             @scrollopt output, @ccopt output, @rows output
  6875.         drop table #spspeccol
  6876.     end
  6877.     else if @procname = 'sp_sproc_columns'
  6878.     begin
  6879.         create table #spproccol (
  6880.             PROCEDURE_QUALIFIER sysname  null,
  6881.             PROCEDURE_OWNER sysname  null,
  6882.             PROCEDURE_NAME sysname not null,
  6883.             COLUMN_NAME sysname not null,
  6884.             COLUMN_TYPE smallint not null,
  6885.             DATA_TYPE smallint not null,
  6886.             TYPE_NAME sysname not null,
  6887.             "PRECISION" int null,
  6888.             LENGTH int null,
  6889.             SCALE smallint null,
  6890.             RADIX smallint null,
  6891.             NULLABLE smallint not null,
  6892.             REMARKS varchar(254) null,
  6893.             COLUMN_DEF nvarchar(3000) null,
  6894.             SQL_DATA_TYPE smallint not null,
  6895.             SQL_DATETIME_SUB smallint null,
  6896.             CHAR_OCTET_LENGTH int null,
  6897.             ORDINAL_POSITION int not null,
  6898.             IS_NULLABLE varchar(254) null,
  6899.             SS_DATA_TYPE tinyint null
  6900.             )
  6901.         insert into #spproccol exec sp_sproc_columns @p1,@p2,@p3,@p4,@ODBCVer
  6902.         exec @ret = sp_cursoropen @handle output,
  6903.             'select * from #spproccol',
  6904.             @scrollopt output, @ccopt output, @rows output
  6905.         drop table #spproccol
  6906.     end
  6907.     else if @procname = 'sp_statistics'
  6908.     begin
  6909.         create table #spstatistics (
  6910.             TABLE_QUALIFIER sysname   null,
  6911.             TABLE_OWNER sysname   null,
  6912.             TABLE_NAME sysname    not null,
  6913.             NON_UNIQUE smallint null,
  6914.             INDEX_QUALIFIER sysname null,
  6915.             INDEX_NAME sysname null,
  6916.             TYPE smallint not null,
  6917.             SEQ_IN_INDEX smallint null,
  6918.             COLUMN_NAME sysname null,
  6919.             COLLATION char(1) null,
  6920.             CARDINALITY int null,
  6921.             PAGES int null,
  6922.             FILTER_CONDITION varchar(128) null
  6923.             )
  6924.         insert into #spstatistics exec sp_statistics @p1,@p2,@p3,@p4,@p5,@p6
  6925.         exec @ret = sp_cursoropen @handle output,
  6926.             'select * from #spstatistics',
  6927.             @scrollopt output, @ccopt output, @rows output
  6928.         drop table #spstatistics
  6929.     end
  6930.     else if @procname = 'sp_stored_procedures'
  6931.     begin
  6932.         create table #spprocedures (
  6933.             PROCEDURE_QUALIFIER sysname  null,
  6934.             PROCEDURE_OWNER sysname  null,
  6935.             PROCEDURE_NAME sysname not null,
  6936.             NUM_INPUT_PARAMS int null,
  6937.             NUM_OUTPUT_PARAMS int null,
  6938.             NUM_RESULT_SETS int null,
  6939.             REMARKS varchar(254) null,
  6940.             PROCEDURE_TYPE smallint null
  6941.             )
  6942.         insert into #spprocedures exec sp_stored_procedures @p1,@p2,@p3
  6943.         exec @ret = sp_cursoropen @handle output,
  6944.             'select * from #spprocedures',
  6945.             @scrollopt output, @ccopt output, @rows output
  6946.         drop table #spprocedures
  6947.     end
  6948.     else if @procname = 'sp_table_privileges'
  6949.     begin
  6950.         create table #sptabpriv (
  6951.             TABLE_QUALIFIER sysname null,
  6952.             TABLE_OWNER sysname null,
  6953.             TABLE_NAME sysname not null,
  6954.             GRANTOR sysname null,
  6955.             GRANTEE sysname not null,
  6956.             PRIVILEGE varchar(32) not null,
  6957.             IS_GRANTABLE varchar(3) null
  6958.             )
  6959.         insert into #sptabpriv exec sp_table_privileges @p1,@p2,@p3
  6960.         exec @ret = sp_cursoropen @handle output,
  6961.             'select * from #sptabpriv',
  6962.             @scrollopt output, @ccopt output, @rows output
  6963.         drop table #sptabpriv
  6964.     end
  6965.     else if @procname = 'sp_tables' or @procname = 'sp_tables_ex'
  6966.     begin
  6967.         create table #sptables (
  6968.             TABLE_QUALIFIER sysname null,
  6969.             TABLE_OWNER sysname null,
  6970.             TABLE_NAME sysname null,
  6971.             TABLE_TYPE    varchar(32) null,
  6972.             REMARKS varchar(254) null)
  6973.         if @procname = 'sp_tables'
  6974.         begin
  6975.             insert into #sptables exec sp_tables @p1,@p2,@p3,@p4
  6976.         end
  6977.         else
  6978.         begin
  6979.             insert into #sptables exec sp_tables_ex @p1,@p2,@p3,@p4,@p5
  6980.         end
  6981.         exec @ret = sp_cursoropen @handle output,
  6982.             'select * from #sptables',
  6983.             @scrollopt output, @ccopt output, @rows output
  6984.         drop table #sptables
  6985.     end
  6986.     else
  6987.         print 'Unknown dd_open procedure'
  6988.     select @ret = isnull(@ret,0)
  6989.     return isnull(@ret,0)
  6990. go
  6991.  
  6992. grant execute on sp_ddopen to public
  6993. go
  6994.  
  6995. dump tran master with no_log
  6996. go
  6997.  
  6998. /*-------------------------------------------------------------------------*/
  6999. /*-------------- CATALOG STORED PROCEDURES FOR SQLOLEDB  ------------------*/
  7000. /*-------------------------------------------------------------------------*/
  7001.  
  7002. print ''
  7003. print 'creating spt_provider_types'
  7004. go
  7005. if (charindex('7.00', @@version) = 0)
  7006. begin /* Pre 7.00 Server */
  7007. create table spt_provider_types
  7008.     (
  7009.     ss_dtype        tinyint     not null,
  7010.     fixlen            int         null,        /* datatype len for variable, else null */
  7011.     type_name        sysname        not null,
  7012.     oledb_data_type        smallint    not null,
  7013.     best_match        bit        not null,
  7014.     is_nullable        tinyint        null,
  7015.     case_sensitive        bit        not null,
  7016.     fixed_prec_scale    bit        not null,
  7017.     is_long            bit        not null,
  7018.     auto_unique_value    tinyint        not null,
  7019.     data_precision        int         null,
  7020.     numeric_scale        smallint    null,        /* min scale if 6.0 */
  7021.     column_size        int         null,
  7022.     literal_prefix        varchar(32)    null,
  7023.     literal_suffix        varchar(32)     null,
  7024.     searchable        int        not null,
  7025.     unsigned_attribute    tinyint        null,
  7026.     local_type_name     sysname        null
  7027.     )
  7028. print ''
  7029. print ''
  7030. print 'Warning:'
  7031. print 'you are installing the stored procedures '
  7032. print 'on a pre 7.0 SQL Server.'
  7033. print 'Ignore the following errors.'
  7034. end
  7035. go
  7036. if (charindex('7.00', @@version) > 0)
  7037. begin /* 7.00 server*/
  7038. create table spt_provider_types
  7039.     (
  7040.     ss_dtype        tinyint     not null,
  7041.     fixlen            int         null,        /* datatype len for variable, else null */
  7042.     type_name        sysname        not null,
  7043.     oledb_data_type        smallint    not null,
  7044.     best_match        bit        not null,
  7045.     is_nullable        bit        null,
  7046.     case_sensitive        bit        not null,
  7047.     fixed_prec_scale    bit        not null,
  7048.     is_long            bit        not null,
  7049.     auto_unique_value    tinyint        not null,
  7050.     data_precision        int         null,
  7051.     numeric_scale        smallint    null,        /* min scale if 6.0 */
  7052.     column_size        int         null,
  7053.     literal_prefix        nvarchar(32)    null,
  7054.     literal_suffix        nvarchar(32)     null,
  7055.     searchable        int        not null,
  7056.     unsigned_attribute    tinyint        null,
  7057.     local_type_name     sysname        null
  7058.     )
  7059. end
  7060. go
  7061.  
  7062. grant select on spt_provider_types to public
  7063. go
  7064. dump tran master with no_log
  7065. go
  7066.  
  7067. /*
  7068. ** Insert the spt_provider_types rows for DBTYTPE_STR data types
  7069. */
  7070. begin tran
  7071.  
  7072. /* Get case sensitivity */
  7073. declare @case_sensitive bit
  7074. select @case_sensitive = case when 'a' <> 'A' then 1 else 0 end
  7075. /* Local Char */
  7076. insert into spt_provider_types values
  7077.     (
  7078.     47 /*SQLCHARACTER*/,        /* ss_dtype */
  7079.     0,                /* fixlen */
  7080.     'char',                /* type_name */
  7081.     129 /*DBTYPE_STR*/,        /* oledb_data_type */
  7082.     0,                /* best_match */
  7083.     null,                /* is_nullable */
  7084.     @case_sensitive,        /* case_sensitive */
  7085.     0,                /* fixed_prec_scale */
  7086.     0,                /* is_long */
  7087.     0,                /* auto_unique_value */
  7088.     null,                /* data_precision */
  7089.     null,                /* numeric_scale */
  7090.     null,                /* column_size */
  7091.     '''',                /* literal_prefix */
  7092.     '''',                /* literal_suffix */
  7093.     4 /*DB_SEARCHABLE*/,        /* searchable */
  7094.     null,                /* unsigned_attribute */
  7095.     'char'                /* local_type_name */
  7096.     )
  7097.  
  7098. /* Local Varchar */
  7099. insert into spt_provider_types values
  7100.     (
  7101.     39 /*SQLVARCHAR*/,        /* ss_dtype */
  7102.     null,                /* fixlen */
  7103.     'varchar',            /* type_name */
  7104.     129 /*DBTYPE_STR*/,        /* oledb_data_type */
  7105.     1,                /* best_match */
  7106.     null,                /* is_nullable */
  7107.     @case_sensitive,        /* case_sensitive */
  7108.     0,                /* fixed_prec_scale */
  7109.     0,                /* is_long */
  7110.     0,                /* auto_unique_value */
  7111.     null,                /* data_precision */
  7112.     null,                /* numeric_scale */
  7113.     null,                /* column_size */
  7114.     '''',                /* literal_prefix */
  7115.     '''',                /* literal_suffix */
  7116.     4 /*DB_SEARCHABLE*/,        /* searchable */
  7117.     null,                /* unsigned_attribute */
  7118.     'varchar'            /* local_type_name */
  7119.     )
  7120.  
  7121. /* Local Text */
  7122. insert into spt_provider_types values
  7123.     (
  7124.     35  /*SQLTEXT*/,        /* ss_dtype */
  7125.     null,                /* fixlen */
  7126.     'text',                /* type_name */
  7127.     129 /*DBTYPE_STR*/,        /* oledb_data_type */
  7128.     0,                /* best_match */
  7129.     null,                /* is_nullable */
  7130.     @case_sensitive,        /* case_sensitive */
  7131.     0,                /* fixed_prec_scale */
  7132.     1,                /* is_long */
  7133.     0,                /* auto_unique_value */
  7134.     null,                /* data_precision */
  7135.     null,                /* numeric_scale */
  7136.     2147483647,            /* column_size */
  7137.     '''',                /* literal_prefix */
  7138.     '''',                /* literal_suffix */
  7139.     2 /*DB_LIKE_ONLY*/,        /* searchable */
  7140.     null,                /* unsigned_attribute */
  7141.     'text'                /* local_type_name */
  7142.     )
  7143. commit tran
  7144. go
  7145. dump tran master with no_log
  7146. go
  7147.  
  7148. /*
  7149. ** Insert the spt_provider_types rows for DBTYTPE_BYTES data types
  7150. */
  7151. begin tran
  7152. /* Local Binary */
  7153. insert into spt_provider_types values
  7154.     (
  7155.     45 /*SQLBINARY*/,        /* ss_dtype */
  7156.     0,                /* fixlen */
  7157.     'binary',            /* type_name */
  7158.     128 /*DBTYPE_BYTES*/,        /* oledb_data_type */
  7159.     0,                /* best_match */
  7160.     null,                /* is_nullable */
  7161.     0,                /* case_sensitive */
  7162.     0,                /* fixed_prec_scale */
  7163.     0,                /* is_long */
  7164.     0,                /* auto_unique_value */
  7165.     null,                /* data_precision */
  7166.     null,                /* numeric_scale */
  7167.     null,                /* column_size */
  7168.     '0x',                /* literal_prefix */
  7169.     null,                /* literal_suffix */
  7170.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  7171.     null,                /* unsigned_attribute */
  7172.     'binary'            /* local_type_name */
  7173.     )
  7174.  
  7175. /* Local Varbinary */
  7176. insert into spt_provider_types values
  7177.     (
  7178.     37 /*SQLVARBINARY*/,        /* ss_dtype */
  7179.     null,                /* fixlen */
  7180.     'varbinary',            /* type_name */
  7181.     128 /*DBTYPE_BYTES*/,        /* oledb_data_type */
  7182.     1,                /* best_match */
  7183.     null,                /* is_nullable */
  7184.     0,                /* case_sensitive */
  7185.     0,                /* fixed_prec_scale */
  7186.     0,                /* is_long */
  7187.     0,                /* auto_unique_value */
  7188.     null,                /* data_precision */
  7189.     null,                /* numeric_scale */
  7190.     null,                /* column_size */
  7191.     '0x',                /* literal_prefix */
  7192.     null,                /* literal_suffix */
  7193.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  7194.     null,                /* unsigned_attribute */
  7195.     'varbinary'            /* local_type_name */
  7196.     )
  7197.  
  7198. /* Local Image */
  7199. insert into spt_provider_types values
  7200.     (
  7201.     34 /*SQLIMAGE*/,        /* ss_dtype */
  7202.     null,                /* fixlen */
  7203.     'image',            /* type_name */
  7204.     128 /*DBTYPE_BYTES*/,        /* oledb_data_type */
  7205.     0,                /* best_match */
  7206.     null,                /* is_nullable */
  7207.     0,                /* case_sensitive */
  7208.     0,                /* fixed_prec_scale */
  7209.     1,                /* is_long */
  7210.     0,                /* auto_unique_value */
  7211.     null,                /* data_precision */
  7212.     null,                /* numeric_scale */
  7213.     2147483647,            /* column_size */
  7214.     '0x',                /* literal_prefix */
  7215.     null,                /* literal_suffix */
  7216.     1 /*DB_UNSEARCHABLE*/,        /* searchable */
  7217.     null,                /* unsigned_attribute */
  7218.     'image'                /* local_type_name */
  7219.     )
  7220.  
  7221. commit tran
  7222. go
  7223. dump tran master with no_log
  7224. go
  7225.  
  7226. /*
  7227. ** Insert the spt_provider_types rows for DBTYPE_DBTIMESTAMP data types
  7228. */
  7229. begin tran
  7230.  
  7231. /* Local Datetime */
  7232. insert into spt_provider_types values
  7233.     (
  7234.     61 /*SQLDATETIME*/,        /* ss_dtype */
  7235.     8,                /* fixlen */
  7236.     'datetime',            /* type_name */
  7237.     135 /*DBTYPE_DBTIMESTAMP*/,    /* oledb_data_type */
  7238.     1,                /* best_match */
  7239.     null,                /* is_nullable */
  7240.     0,                /* case_sensitive */
  7241.     0,                /* fixed_prec_scale */
  7242.     0,                /* is_long */
  7243.     0,                /* auto_unique_value */
  7244.     23,                /* data_precision */
  7245.     null,                /* numeric_scale */
  7246.     null,                /* column_size */
  7247.     '''',                /* literal_prefix */
  7248.     '''',                /* literal_suffix */
  7249.     4 /*DB_SEARCHABLE*/,        /* searchable */
  7250.     null,                /* unsigned_attribute */
  7251.     'datetime'            /* local_type_name */
  7252.     )
  7253.  
  7254. /* Local Smalldatetime */
  7255. insert into spt_provider_types values
  7256.     (
  7257.     58 /*SQLDATETIM4*/,        /* ss_dtype */
  7258.     4,                /* fixlen */
  7259.     'smalldatetime',        /* type_name */
  7260.     135 /*DBTYPE_DBTIMESTAMP*/,    /* oledb_data_type */
  7261.     0,                /* best_match */
  7262.     null,                /* is_nullable */
  7263.     0,                /* case_sensitive */
  7264.     0,                /* fixed_prec_scale */
  7265.     0,                /* is_long */
  7266.     0,                /* auto_unique_value */
  7267.     16,                /* data_precision */
  7268.     null,                /* numeric_scale */
  7269.     null,                /* column_size */
  7270.     '''',                /* literal_prefix */
  7271.     '''',                /* literal_suffix */
  7272.     4 /*DB_SEARCHABLE*/,        /* searchable */
  7273.     null,                /* unsigned_attribute */
  7274.     'smalldatetime'            /* local_type_name */
  7275.     )
  7276.  
  7277. if (charindex('7.00', @@version) = 0)
  7278.     begin    /*    Add nullable type for non-Sphinx server */
  7279.     
  7280.     /* Local Datetimn */
  7281.     insert into spt_provider_types values
  7282.         (
  7283.         111 /*SQLDATETIMN*/,        /* ss_dtype */
  7284.         4,                /* fixlen */
  7285.         'smalldatetime',        /* type_name */
  7286.         135 /*DBTYPE_DBTIMESTAMP*/,    /* oledb_data_type */
  7287.         0,                /* best_match */
  7288.         null,                /* is_nullable */
  7289.         0,                /* case_sensitive */
  7290.         0,                /* fixed_prec_scale */
  7291.         0,                /* is_long */
  7292.         0,                /* auto_unique_value */
  7293.         16,                /* data_precision */
  7294.         null,                /* numeric_scale */
  7295.         null,                /* column_size */
  7296.         '''',                /* literal_prefix */
  7297.         '''',                /* literal_suffix */
  7298.         4 /*DB_SEARCHABLE*/,        /* searchable */
  7299.         null,                /* unsigned_attribute */
  7300.         'smalldatetime'            /* local_type_name */
  7301.         )    
  7302.     insert into spt_provider_types values
  7303.         (
  7304.         111 /*SQLDATETIMN*/,        /* ss_dtype */
  7305.         8,                /* fixlen */
  7306.         'datetime',            /* type_name */
  7307.         135 /*DBTYPE_DBTIMESTAMP*/,    /* oledb_data_type */
  7308.         0,                /* best_match */
  7309.         null,                /* is_nullable */
  7310.         0,                /* case_sensitive */
  7311.         0,                /* fixed_prec_scale */
  7312.         0,                /* is_long */
  7313.         0,                /* auto_unique_value */
  7314.         23,                /* data_precision */
  7315.         null,                /* numeric_scale */
  7316.         null,                /* column_size */
  7317.         '''',                /* literal_prefix */
  7318.         '''',                /* literal_suffix */
  7319.         4 /*DB_SEARCHABLE*/,        /* searchable */
  7320.         null,                /* unsigned_attribute */
  7321.         'datetime'            /* local_type_name */
  7322.         )
  7323.     end
  7324.  
  7325. commit tran
  7326. go
  7327. dump tran master with no_log
  7328. go
  7329.  
  7330. /*
  7331. ** Insert the spt_provider_types rows for DBTYPE_CY data types
  7332. */
  7333. begin tran
  7334.  
  7335. /* Local Smallmoney */
  7336. insert into spt_provider_types values
  7337.     (
  7338.     122 /*SQLMONEY4*/,        /* ss_dtype */
  7339.     4,                /* fixlen */
  7340.     'smallmoney',            /* type_name */
  7341.     6 /*DBTYPE_CY*/,        /* oledb_data_type */
  7342.     0,                /* best_match */
  7343.     null,                /* is_nullable */
  7344.     0,                /* case_sensitive */
  7345.     1,                /* fixed_prec_scale */
  7346.     0,                /* is_long */
  7347.     0,                /* auto_unique_value */
  7348.     10,                /* data_precision */
  7349.     null,                /* numeric_scale */
  7350.     null,                /* column_size */
  7351.     '$',                /* literal_prefix */
  7352.     null,                /* literal_suffix */
  7353.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  7354.     0,                /* unsigned_attribute */
  7355.     'smallmoney'            /* local_type_name */
  7356.     )    
  7357.     
  7358. /* Local Money */
  7359. insert into spt_provider_types values
  7360.     (
  7361.     60 /*SQLMONEY*/,        /* ss_dtype */
  7362.     8,                /* fixlen */
  7363.     'money',            /* type_name */
  7364.     6 /*DBTYPE_CY*/,        /* oledb_data_type */
  7365.     1,                /* best_match */
  7366.     null,                /* is_nullable */
  7367.     0,                /* case_sensitive */
  7368.     1,                /* fixed_prec_scale */
  7369.     0,                /* is_long */
  7370.     0,                /* auto_unique_value */
  7371.     19,                /* data_precision */
  7372.     null,                /* numeric_scale */
  7373.     null,                /* column_size */
  7374.     '$',                /* literal_prefix */
  7375.     null,                /* literal_suffix */
  7376.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  7377.     0,                /* unsigned_attribute */
  7378.     'money'                /* local_type_name */
  7379.     )    
  7380.  
  7381. if (charindex('7.00', @@version) = 0)
  7382.     begin    /*    Add nullable type for non-Sphinx server */
  7383.  
  7384.     /* Local Moneyn */
  7385.     insert into spt_provider_types values
  7386.         (
  7387.         110 /*SQLMONEYN*/,    /* ss_dtype */
  7388.         4,            /* fixlen */
  7389.         'smallmoney',        /* type_name */
  7390.         6 /*DBTYPE_CY*/,    /* oledb_data_type */
  7391.         0,            /* best_match */
  7392.         null,            /* is_nullable */
  7393.         0,            /* case_sensitive */
  7394.         1,            /* fixed_prec_scale */
  7395.         0,            /* is_long */
  7396.         0,            /* auto_unique_value */
  7397.         10,            /* data_precision */
  7398.         null,            /* numeric_scale */
  7399.         null,            /* column_size */
  7400.         '$',            /* literal_prefix */
  7401.         null,            /* literal_suffix */
  7402.         3 /*DB_ALL_EXCEPT_LIKE*/, /* searchable */
  7403.         0,            /* unsigned_attribute */
  7404.         'smallmoney'        /* local_type_name */
  7405.         )    
  7406.     insert into spt_provider_types values
  7407.         (
  7408.         110 /*SQLMONEYN*/,    /* ss_dtype */
  7409.         8,            /* fixlen */
  7410.         'money',        /* type_name */
  7411.         6 /*DBTYPE_CY*/,    /* oledb_data_type */
  7412.         0,            /* best_match */
  7413.         null,            /* is_nullable */
  7414.         0,            /* case_sensitive */
  7415.         1,            /* fixed_prec_scale */
  7416.         0,            /* is_long */
  7417.         0,            /* auto_unique_value */
  7418.         19,            /* data_precision */
  7419.         null,            /* numeric_scale */
  7420.         null,            /* column_size */
  7421.         '$',            /* literal_prefix */
  7422.         null,            /* literal_suffix */
  7423.         3 /*DB_ALL_EXCEPT_LIKE*/, /* searchable */
  7424.         null,            /* unsigned_attribute */
  7425.         'money'            /* local_type_name */
  7426.         )    
  7427.     end
  7428.  
  7429. commit tran
  7430. go
  7431. dump tran master with no_log
  7432. go
  7433.  
  7434. /*
  7435. ** Insert the spt_provider_types rows for the numeric data types
  7436. */
  7437. begin tran
  7438.  
  7439. /* Local Float */
  7440. insert into spt_provider_types values
  7441.     (
  7442.     62 /*SQLFLT8*/,            /* ss_dtype */
  7443.     8,                /* fixlen */
  7444.     'float',            /* type_name */
  7445.     5 /*DBTYPE_R8*/,        /* oledb_data_type */
  7446.     1,                /* best_match */
  7447.     null,                /* is_nullable */
  7448.     0,                /* case_sensitive */
  7449.     0,                /* fixed_prec_scale */
  7450.     0,                /* is_long */
  7451.     0,                /* auto_unique_value */
  7452.     15,                /* data_precision */
  7453.     null,                /* numeric_scale */
  7454.     null,                /* column_size */
  7455.     null,                /* literal_prefix */
  7456.     null,                /* literal_suffix */
  7457.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  7458.     0,                /* unsigned_attribute */
  7459.     'float'                /* local_type_name */
  7460.     )    
  7461.  
  7462. /* Local Real */
  7463. insert into spt_provider_types values
  7464.     (
  7465.     59 /*SQLFLT4*/,            /* ss_dtype */
  7466.     4,                /* fixlen */
  7467.     'real',                /* type_name */
  7468.     4 /*DBTYPE_R4*/,        /* oledb_data_type */
  7469.     1,                /* best_match */
  7470.     null,                /* is_nullable */
  7471.     0,                /* case_sensitive */
  7472.     0,                /* fixed_prec_scale */
  7473.     0,                /* is_long */
  7474.     0,                /* auto_unique_value */
  7475.     7,                /* data_precision */
  7476.     null,                /* numeric_scale */
  7477.     null,                /* column_size */
  7478.     null,                /* literal_prefix */
  7479.     null,                /* literal_suffix */
  7480.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  7481.     0,                /* unsigned_attribute */
  7482.     'real'                /* local_type_name */
  7483.     )    
  7484.  
  7485. /* Local Int */
  7486. insert into spt_provider_types values
  7487.     (
  7488.     56 /*SQLINT4*/,            /* ss_dtype */
  7489.     4,                /* fixlen */
  7490.     'int',                /* type_name */
  7491.     3 /*DBTYPE_I4*/,        /* oledb_data_type */
  7492.     1,                /* best_match */
  7493.     null,                /* is_nullable */
  7494.     0,                /* case_sensitive */
  7495.     1,                /* fixed_prec_scale */
  7496.     0,                /* is_long */
  7497.     1,                /* auto_unique_value */
  7498.     10,                /* data_precision */
  7499.     null,                /* numeric_scale */
  7500.     null,                /* column_size */
  7501.     null,                /* literal_prefix */
  7502.     null,                /* literal_suffix */
  7503.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  7504.     0,                /* unsigned_attribute */
  7505.     'int'                /* local_type_name */
  7506.     )    
  7507.  
  7508. /* Local Smallint */
  7509. insert into spt_provider_types values
  7510.     (
  7511.     52 /*SQLINT2*/,            /* ss_dtype */
  7512.     2,                /* fixlen */
  7513.     'smallint',            /* type_name */
  7514.     2 /*DBTYPE_I2*/,        /* oledb_data_type */
  7515.     1,                /* best_match */
  7516.     null,                /* is_nullable */
  7517.     0,                /* case_sensitive */
  7518.     1,                /* fixed_prec_scale */
  7519.     0,                /* is_long */
  7520.     1,                /* auto_unique_value */
  7521.     5,                /* data_precision */
  7522.     null,                /* numeric_scale */
  7523.     null,                /* column_size */
  7524.     null,                /* literal_prefix */
  7525.     null,                /* literal_suffix */
  7526.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  7527.     0,                /* unsigned_attribute */
  7528.     'smallint'            /* local_type_name */
  7529.     )    
  7530.  
  7531. /* Local Tinyint */
  7532. insert into spt_provider_types values
  7533.     (
  7534.     48 /*SQLINT1*/,            /* ss_dtype */
  7535.     1,                /* fixlen */
  7536.     'tinyint',            /* type_name */
  7537.     17 /*DBTYPE_UI1*/,        /* oledb_data_type */
  7538.     1,                /* best_match */
  7539.     null,                /* is_nullable */
  7540.     0,                /* case_sensitive */
  7541.     1,                /* fixed_prec_scale */
  7542.     0,                /* is_long */
  7543.     1,                /* auto_unique_value */
  7544.     3,                /* data_precision */
  7545.     null,                /* numeric_scale */
  7546.     null,                /* column_size */
  7547.     null,                /* literal_prefix */
  7548.     null,                /* literal_suffix */
  7549.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  7550.     1,                /* unsigned_attribute */
  7551.     'tinyint'            /* local_type_name */
  7552.     )    
  7553. commit tran
  7554. go
  7555. dump tran master with no_log
  7556. go
  7557. begin tran    
  7558. if (charindex('6.00', @@version) > 0 or
  7559.     charindex('6.50', @@version) > 0 or
  7560.     charindex('7.00', @@version) > 0)
  7561.     begin    /*    Add 6.0 data types */
  7562.     
  7563.     /* Local Decimal */
  7564.     insert into spt_provider_types values
  7565.         (
  7566.         55 /*SQLDECIMAL*/,        /* ss_dtype */
  7567.         0,                /* fixlen */
  7568.         'decimal',            /* type_name */
  7569.         131 /*DBTYPE_NUMERIC*/,        /* oledb_data_type */
  7570.         0,                /* best_match */
  7571.         null,                /* is_nullable */
  7572.         0,                /* case_sensitive */
  7573.         1,                /* fixed_prec_scale */
  7574.         0,                /* is_long */
  7575.         1,                /* auto_unique_value */
  7576.         28,                /* data_precision */
  7577.         null,                /* numeric_scale */
  7578.         null,                /* column_size */
  7579.         null,                /* literal_prefix */
  7580.         null,                /* literal_suffix */
  7581.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  7582.         0,                /* unsigned_attribute */
  7583.         'decimal'            /* local_type_name */
  7584.         )    
  7585.  
  7586.     /* Local Numeric */
  7587.     insert into spt_provider_types values
  7588.         (
  7589.         63 /*SQLNUMERIC*/,        /* ss_dtype */
  7590.         0,                /* fixlen */
  7591.         'numeric',            /* type_name */
  7592.         131 /*DBTYPE_NUMERIC*/,        /* oledb_data_type */
  7593.         1,                /* best_match */
  7594.         null,                /* is_nullable */
  7595.         0,                /* case_sensitive */
  7596.         1,                /* fixed_prec_scale */
  7597.         0,                /* is_long */
  7598.         1,                /* auto_unique_value */
  7599.         28,                /* data_precision */
  7600.         null,                /* numeric_scale */
  7601.         null,                /* column_size */
  7602.         null,                /* literal_prefix */
  7603.         null,                /* literal_suffix */
  7604.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  7605.         0,                /* unsigned_attribute */
  7606.         'numeric'            /* local_type_name */
  7607.         )    
  7608.     end
  7609. commit tran
  7610. go
  7611. dump tran master with no_log
  7612. go
  7613. begin tran
  7614. if (charindex('7.00', @@version) = 0)
  7615.     begin    /*    Add nullable type for non-Sphinx server */
  7616.     
  7617.     /* Local Floatn */
  7618.     insert into spt_provider_types values
  7619.         (
  7620.         109 /*SQLFLTN*/,        /* ss_dtype */
  7621.         8,                /* fixlen */
  7622.         'float',            /* type_name */
  7623.         5 /*DBTYPE_R8*/,        /* oledb_data_type */
  7624.         0,                /* best_match */
  7625.         null,                /* is_nullable */
  7626.         0,                /* case_sensitive */
  7627.         0,                /* fixed_prec_scale */
  7628.         0,                /* is_long */
  7629.         0,                /* auto_unique_value */
  7630.         15,                /* data_precision */
  7631.         null,                /* numeric_scale */
  7632.         null,                /* column_size */
  7633.         null,                /* literal_prefix */
  7634.         null,                /* literal_suffix */
  7635.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  7636.         null,                /* unsigned_attribute */
  7637.         'float'                /* local_type_name */
  7638.         )    
  7639.     insert into spt_provider_types values
  7640.         (
  7641.         109 /*SQLFLT4*/,        /* ss_dtype */
  7642.         4,                /* fixlen */
  7643.         'real',                /* type_name */
  7644.         4 /*DBTYPE_R4*/,        /* oledb_data_type */
  7645.         0,                /* best_match */
  7646.         null,                /* is_nullable */
  7647.         0,                /* case_sensitive */
  7648.         0,                /* fixed_prec_scale */
  7649.         0,                /* is_long */
  7650.         0,                /* auto_unique_value */
  7651.         7,                /* data_precision */
  7652.         null,                /* numeric_scale */
  7653.         null,                /* column_size */
  7654.         null,                /* literal_prefix */
  7655.         null,                /* literal_suffix */
  7656.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  7657.         null,                /* unsigned_attribute */
  7658.         'real'                /* local_type_name */
  7659.         )    
  7660.  
  7661.     /* Local Intn */
  7662.     insert into spt_provider_types values
  7663.         (
  7664.         38 /*SQLINTN*/,            /* ss_dtype */
  7665.         4,                /* fixlen */
  7666.         'int',                /* type_name */
  7667.         3 /*DBTYPE_I4*/,        /* oledb_data_type */
  7668.         1,                /* best_match */
  7669.         null,                /* is_nullable */
  7670.         0,                /* case_sensitive */
  7671.         1,                /* fixed_prec_scale */
  7672.         0,                /* is_long */
  7673.         1,                /* auto_unique_value */
  7674.         10,                /* data_precision */
  7675.         null,                /* numeric_scale */
  7676.         null,                /* column_size */
  7677.         null,                /* literal_prefix */
  7678.         null,                /* literal_suffix */
  7679.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  7680.         0,                /* unsigned_attribute */
  7681.         'int'                /* local_type_name */
  7682.         )    
  7683.     insert into spt_provider_types values
  7684.         (
  7685.         38 /*SQLINTN*/,            /* ss_dtype */
  7686.         2,                /* fixlen */
  7687.         'smallint',            /* type_name */
  7688.         2 /*DBTYPE_I2*/,        /* oledb_data_type */
  7689.         0,                /* best_match */
  7690.         null,                /* is_nullable */
  7691.         0,                /* case_sensitive */
  7692.         1,                /* fixed_prec_scale */
  7693.         0,                /* is_long */
  7694.         1,                /* auto_unique_value */
  7695.         5,                /* data_precision */
  7696.         null,                /* numeric_scale */
  7697.         null,                /* column_size */
  7698.         null,                /* literal_prefix */
  7699.         null,                /* literal_suffix */
  7700.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  7701.         0,                /* unsigned_attribute */
  7702.         'smallint'            /* local_type_name */
  7703.         )    
  7704.     insert into spt_provider_types values
  7705.         (
  7706.         38 /*SQLINTN*/,            /* ss_dtype */
  7707.         1,                /* fixlen */
  7708.         'tinyint',            /* type_name */
  7709.         17 /*DBTYPE_UI1*/,        /* oledb_data_type */
  7710.         0,                /* best_match */
  7711.         null,                /* is_nullable */
  7712.         0,                /* case_sensitive */
  7713.         1,                /* fixed_prec_scale */
  7714.         0,                /* is_long */
  7715.         1,                /* auto_unique_value */
  7716.         3,                /* data_precision */
  7717.         null,                /* numeric_scale */
  7718.         null,                /* column_size */
  7719.         null,                /* literal_prefix */
  7720.         null,                /* literal_suffix */
  7721.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  7722.         1,                /* unsigned_attribute */
  7723.         'tinyint'            /* local_type_name */
  7724.         )    
  7725.  
  7726.     if (charindex('6.00', @@version) > 0 or    charindex('6.50', @@version) > 0)
  7727.         begin    /*    Add 6.0 data types */
  7728.         
  7729.         /* Local Decimaln */
  7730.         insert into spt_provider_types values
  7731.             (
  7732.             106 /*SQLDECIMALN*/,        /* ss_dtype */
  7733.             0,                /* fixlen */
  7734.             'decimal',            /* type_name */
  7735.             131 /*DBTYPE_NUMERIC*/,        /* oledb_data_type */
  7736.             0,                /* best_match */
  7737.             null,                /* is_nullable */
  7738.             0,                /* case_sensitive */
  7739.             1,                /* fixed_prec_scale */
  7740.             0,                /* is_long */
  7741.             1,                /* auto_unique_value */
  7742.             28,                /* data_precision */
  7743.             null,                /* numeric_scale */
  7744.             null,                /* column_size */
  7745.             null,                /* literal_prefix */
  7746.             null,                /* literal_suffix */
  7747.             3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  7748.             0,                /* unsigned_attribute */
  7749.             'decimal'            /* local_type_name */
  7750.             )    
  7751.  
  7752.         /* Local Numericn */
  7753.         insert into spt_provider_types values
  7754.             (
  7755.             108 /*SQLNUMERICN*/,        /* ss_dtype */
  7756.             0,                /* fixlen */
  7757.             'numeric',            /* type_name */
  7758.             131 /*DBTYPE_NUMERIC*/,        /* oledb_data_type */
  7759.             0,                /* best_match */
  7760.             null,                /* is_nullable */
  7761.             0,                /* case_sensitive */
  7762.             1,                /* fixed_prec_scale */
  7763.             0,                /* is_long */
  7764.             1,                /* auto_unique_value */
  7765.             28,                /* data_precision */
  7766.             null,                /* numeric_scale */
  7767.             null,                /* column_size */
  7768.             null,                /* literal_prefix */
  7769.             null,                /* literal_suffix */
  7770.             3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  7771.             0,                /* unsigned_attribute */
  7772.             'numeric'            /* local_type_name */
  7773.             )    
  7774.         end
  7775.     end
  7776. commit tran
  7777. go
  7778. dump tran master with no_log
  7779. go
  7780.  
  7781. /*
  7782. **    Remaining data types
  7783. */
  7784. if (charindex('7.00', @@version) = 0)
  7785. begin
  7786.     print ''
  7787.     print ''
  7788.     print 'Warning:'
  7789.     print 'you are installing the stored procedures '
  7790.     print 'on a pre 7.0 SQL Server.'
  7791.     print 'Ignore the following errors.'
  7792. end
  7793. go
  7794. begin tran
  7795.  
  7796. if (charindex('7.00', @@version) > 0)
  7797.     begin
  7798.     /* Local Bit */
  7799.     insert into spt_provider_types values
  7800.         (
  7801.         50 /*SQLBIT*/,            /* ss_dtype */
  7802.         0,                /* fixlen */
  7803.         'bit',                /* type_name */
  7804.         11 /*DBTYPE_BOOL*/,        /* oledb_data_type */
  7805.         1,                /* best_match */
  7806.         null,                /* is_nullable */
  7807.         0,                /* case_sensitive */
  7808.         0,                /* fixed_prec_scale */
  7809.         0,                /* is_long */
  7810.         0,                /* auto_unique_value */
  7811.         1,                /* data_precision */
  7812.         null,                /* numeric_scale */
  7813.         null,                /* column_size */
  7814.         null,                /* literal_prefix */
  7815.         null,                /* literal_suffix */
  7816.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  7817.         null,                /* unsigned_attribute */
  7818.         'bit'                /* local_type_name */
  7819.         )        
  7820.     
  7821.     /* Local Timestamp */
  7822.     insert into spt_provider_types values
  7823.         (
  7824.         0,                /* ss_dtype */
  7825.         8,                /* fixlen */
  7826.         'timestamp',            /* type_name */
  7827.         128 /*DBTYPE_BYTES*/,        /* oledb_data_type */
  7828.         0,                /* best_match */
  7829.         null,                /* is_nullable */
  7830.         0,                /* case_sensitive */
  7831.         0,                /* fixed_prec_scale */
  7832.         0,                /* is_long */
  7833.         0,                /* auto_unique_value */
  7834.         null,                /* data_precision */
  7835.         null,                /* numeric_scale */
  7836.         null,                /* column_size */
  7837.         '0x',                /* literal_prefix */
  7838.         null,                /* literal_suffix */
  7839.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  7840.         null,                /* unsigned_attribute */
  7841.         'timestamp'            /* local_type_name */
  7842.         )    
  7843.     /* Local GUID */
  7844.     insert into spt_provider_types values
  7845.         (
  7846.         0,                /* ss_dtype */
  7847.         16,                /* fixlen */
  7848.         'uniqueidentifier',        /* type_name */
  7849.         72 /*DBTYPE_GUID*/,        /* oledb_data_type */
  7850.         1,                /* best_match */
  7851.         null,                /* is_nullable */
  7852.         0,                /* case_sensitive */
  7853.         0,                /* fixed_prec_scale */
  7854.         0,                /* is_long */
  7855.         0,                /* auto_unique_value */
  7856.         null,                /* data_precision */
  7857.         null,                /* numeric_scale */
  7858.         null,                /* column_size */
  7859.         '''',                /* literal_prefix */
  7860.         '''',                /* literal_suffix */
  7861.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  7862.         null,                /* unsigned_attribute */
  7863.         'uniqueidentifier'        /* local_type_name */
  7864.         )    
  7865.     
  7866.     /* Get case sensitivity */
  7867.     declare @case_sensitive bit
  7868.     select @case_sensitive = case when 'a' <> 'A' then 1 else 0 end
  7869.  
  7870.     /* Local NChar */
  7871.     insert into spt_provider_types values
  7872.         (
  7873.         0,                /* ss_dtype */
  7874.         0,                /* fixlen */
  7875.         'nchar',            /* type_name */
  7876.         130 /*DBTYPE_WSTR*/,        /* oledb_data_type */
  7877.         0,                /* best_match */
  7878.         null,                /* is_nullable */
  7879.         @case_sensitive,        /* case_sensitive */
  7880.         0,                /* fixed_prec_scale */
  7881.         0,                /* is_long */
  7882.         0,                /* auto_unique_value */
  7883.         null,                /* data_precision */
  7884.         null,                /* numeric_scale */
  7885.         null,                /* column_size */
  7886.         'N''',                /* literal_prefix */
  7887.         '''',                /* literal_suffix */
  7888.         4 /*DB_SEARCHABLE*/,        /* searchable */
  7889.         null,                /* unsigned_attribute */
  7890.         'nchar'                /* local_type_name */
  7891.         )    
  7892.  
  7893.     /* Local NVarChar */
  7894.     insert into spt_provider_types values
  7895.         (
  7896.         0,                /* ss_dtype */
  7897.         null,                /* fixlen */
  7898.         'nvarchar',            /* type_name */
  7899.         130 /*DBTYPE_WSTR*/,        /* oledb_data_type */
  7900.         1,                /* best_match */
  7901.         null,                /* is_nullable */
  7902.         @case_sensitive,        /* case_sensitive */
  7903.         0,                /* fixed_prec_scale */
  7904.         0,                /* is_long */
  7905.         0,                /* auto_unique_value */
  7906.         null,                /* data_precision */
  7907.         null,                /* numeric_scale */
  7908.         null,                /* column_size */
  7909.         'N''',                /* literal_prefix */
  7910.         '''',                /* literal_suffix */
  7911.         4 /*DB_SEARCHABLE*/,        /* searchable */
  7912.         null,                /* unsigned_attribute */
  7913.         'nvarchar'            /* local_type_name */
  7914.         )    
  7915.  
  7916.     /* Local NText */
  7917.     insert into spt_provider_types values
  7918.         (
  7919.         0,                /* ss_dtype */
  7920.         null,                /* fixlen */
  7921.         'ntext',            /* type_name */
  7922.         130 /*DBTYPE_WSTR*/,        /* oledb_data_type */
  7923.         0,                /* best_match */
  7924.         null,                /* is_nullable */
  7925.         @case_sensitive,        /* case_sensitive */
  7926.         0,                /* fixed_prec_scale */
  7927.         1,                /* is_long */
  7928.         0,                /* auto_unique_value */
  7929.         null,                /* data_precision */
  7930.         null,                /* numeric_scale */
  7931.         1073741823,            /* column_size */
  7932.         'N''',                /* literal_prefix */
  7933.         '''',                /* literal_suffix */
  7934.         2 /*DB_LIKE_ONLY*/,        /* searchable */
  7935.         null,                /* unsigned_attribute */
  7936.         'ntext'                /* local_type_name */
  7937.         )    
  7938.  
  7939.     /* data types changed in Sphinx: DO AFTER ALL TYPES INSERTED! */    
  7940.     update spt_provider_types set ss_dtype = (select xtype from systypes    where type_name like name+'%')
  7941.  
  7942.     end
  7943. commit tran
  7944. go
  7945. dump tran master with no_log
  7946. go
  7947. begin tran
  7948. if (charindex('7.00', @@version) = 0)
  7949.     begin
  7950.     /* Local Bit */
  7951.     insert into spt_provider_types values
  7952.         (
  7953.         50 /*SQLBIT*/,            /* ss_dtype */
  7954.         0,                /* fixlen */
  7955.         'bit',                /* type_name */
  7956.         11 /*DBTYPE_BOOL*/,        /* oledb_data_type */
  7957.         1,                /* best_match */
  7958.         0,                /* is_nullable */
  7959.         0,                /* case_sensitive */
  7960.         0,                /* fixed_prec_scale */
  7961.         0,                /* is_long */
  7962.         0,                /* auto_unique_value */
  7963.         1,                /* data_precision */
  7964.         null,                /* numeric_scale */
  7965.         null,                /* column_size */
  7966.         null,                /* literal_prefix */
  7967.         null,                /* literal_suffix */
  7968.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  7969.         null,                /* unsigned_attribute */
  7970.         'bit'                /* local_type_name */
  7971.         )    
  7972.  
  7973.     /* Local Timestamp */
  7974.     insert into spt_provider_types values
  7975.         (
  7976.         45 /*SQLBINARY*/,        /* ss_dtype */
  7977.         8,                /* fixlen */
  7978.         'timestamp',            /* type_name */
  7979.         128 /*DBTYPE_BYTES*/,        /* oledb_data_type */
  7980.         0,                /* best_match */
  7981.         null,                /* is_nullable */
  7982.         0,                /* case_sensitive */
  7983.         0,                /* fixed_prec_scale */
  7984.         0,                /* is_long */
  7985.         0,                /* auto_unique_value */
  7986.         null,                /* data_precision */
  7987.         null,                /* numeric_scale */
  7988.         null,                /* column_size */
  7989.         '0x',                /* literal_prefix */
  7990.         null,                /* literal_suffix */
  7991.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  7992.         null,                /* unsigned_attribute */
  7993.         'timestamp'            /* local_type_name */
  7994.         )        
  7995. end
  7996.  
  7997. commit tran
  7998. go
  7999. dump tran master with no_log
  8000. go
  8001.  
  8002. create unique clustered index datatypeinfoclust on spt_provider_types(ss_dtype,fixlen)
  8003. go
  8004.  
  8005. dump tran master with no_log
  8006. go
  8007. if (charindex('6.00', @@version) > 0)
  8008.     begin
  8009.     if (exists (select * from sysobjects
  8010.         where name = 'spt_provider_types' and type = 'U '))
  8011.         begin
  8012.         drop table spt_provider_types
  8013.         dump tran master with no_log
  8014.         end
  8015.     end
  8016. go
  8017.  
  8018. print ''
  8019. print 'creating sp_catalogs_rowset'
  8020. go
  8021.  
  8022. create procedure sp_catalogs_rowset
  8023.     (
  8024.     @catalog_name        varchar(255)
  8025.     )        
  8026. as
  8027.     select
  8028.         CATALOG_NAME    = name,
  8029.         DESCRIPTION    = convert(varchar(1),null)
  8030.     from     master.dbo.sysdatabases
  8031.     where    name = @catalog_name
  8032. go
  8033. dump tran master with no_log
  8034. go
  8035. create procedure sp_catalogs_rowset;2
  8036.     (
  8037.     @dummy        int /* remove when Hydra bug 17032 is fixed */
  8038.     )
  8039. as
  8040.     select
  8041.         CATALOG_NAME    = name,
  8042.         DESCRIPTION    = convert(varchar(1),null)
  8043.     from     master.dbo.sysdatabases
  8044.     order by 1
  8045. go
  8046. dump tran master with no_log
  8047. go
  8048.  
  8049. if (charindex('7.00', @@version) = 0)
  8050. begin
  8051.     print ''
  8052.     print ''
  8053.     print 'Warning:'
  8054.     print 'you are installing the stored procedures '
  8055.     print 'on a pre 7.0 SQL Server.'
  8056.     print 'Ignore the following errors.'
  8057. end
  8058. else
  8059.     drop proc sp_catalogs_rowset
  8060. go
  8061.  
  8062. /*    Procedure for 7.0 server */
  8063. create procedure sp_catalogs_rowset
  8064.     (
  8065.     @catalog_name        sysname
  8066.     )        
  8067. as
  8068.     select
  8069.         CATALOG_NAME    = name,
  8070.         DESCRIPTION    = convert(nvarchar(1),null)
  8071.     from     master.dbo.sysdatabases
  8072.     where    name = @catalog_name
  8073. go
  8074. dump tran master with no_log
  8075. go
  8076. create procedure sp_catalogs_rowset;2
  8077.     (
  8078.     @dummy        int /* remove when Hydra bug 17032 is fixed */
  8079.     )
  8080. as
  8081.     select
  8082.         CATALOG_NAME    = name,
  8083.         DESCRIPTION    = convert(nvarchar(1),null)
  8084.     from     master.dbo.sysdatabases
  8085.     order by 1
  8086. go
  8087. dump tran master with no_log
  8088. go
  8089. create procedure sp_catalogs_rowset;5
  8090.     (
  8091.     @server_name    sysname,
  8092.     @catalog_name    sysname = NULL
  8093.     )
  8094. as
  8095.     select    CATALOG_NAME,
  8096.         DESCRIPTION
  8097.     from master.dbo.SYSREMOTE_CATALOGS < @server_name, @catalog_name >
  8098.     order by 1
  8099. go
  8100.  
  8101. grant execute on sp_catalogs_rowset to public
  8102. go
  8103.  
  8104. dump tran master with no_log
  8105. go
  8106. if (charindex('6.00', @@version) > 0)
  8107.     begin
  8108.     if (exists (select * from sysobjects
  8109.             where name = 'sp_catalogs_rowset' and type = 'P '))
  8110.         begin
  8111.         drop procedure sp_catalogs_rowset
  8112.         dump tran master with no_log
  8113.         end
  8114.     end
  8115. go
  8116.  
  8117.  
  8118. print ''
  8119. print 'creating sp_column_privileges_rowset'
  8120. go
  8121.  
  8122. /*    Procedure for 6.0 and 6.5 server */
  8123. CREATE PROCEDURE sp_column_privileges_rowset
  8124.     (
  8125.     @table_name     varchar(255) = null,
  8126.     @table_schema    varchar(255) = null,
  8127.     @column_name    varchar(255) = null,
  8128.     @grantor    varchar(255) = null,
  8129.     @grantee    varchar(255) = null
  8130.     )
  8131. as
  8132. IF @table_name is not null
  8133.     BEGIN
  8134.     select
  8135.         GRANTOR        = user_name(p.grantor),
  8136.         GRANTEE        = user_name(u.uid),
  8137.         TABLE_CATALOG    = db_name(),
  8138.         TABLE_SCHEMA    = user_name(o.uid),
  8139.         TABLE_NAME    = o.name,
  8140.         COLUMN_NAME    = c.name,
  8141.         COLUMN_GUID    = convert(binary(16),null),
  8142.         COLUMN_PROPID    = convert(int,null),
  8143.         PRIVILEGE_TYPE    = convert(varchar(30),
  8144.                     case p.action
  8145.                     when 193 then 'SELECT'
  8146.                     when 195 then 'INSERT'
  8147.                     when 197 then 'UPDATE'
  8148.                     else 'REFERENCES'
  8149.                     end),
  8150.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  8151.     from 
  8152.         sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c
  8153.     where
  8154.         o.name = @table_name
  8155.     and     o.type in ('U','V','S')
  8156.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  8157.     and    c.id = o.id
  8158.     and     (@column_name is null or @column_name = c.name)
  8159.     and     c.id = p.id
  8160.     and    (@grantor is null or @grantor = user_name(p.grantor))
  8161.     and     case 
  8162.             when substring(p.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  8163.             when substring(p.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p.columns, v.low, 1))
  8164.             else (~convert(tinyint, coalesce(substring(p.columns, v.low, 1),0)))
  8165.             end & v.high <> 0            /* permission applies to this column */
  8166.     and     v.number <= (select count(*) from syscolumns where id = o.id)    /* ranges from 1 to # of columns in table */
  8167.     and     v.type = 'P'
  8168.     and     v.number = c.colid
  8169.     and    (@grantee is null or @grantee = user_name(u.uid))
  8170.             /* expand groups */
  8171.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  8172.     and     p.protecttype <> 206    /* only grant rows */
  8173.     and     p.action in (26,193,195,197)
  8174.     and     o.uid <> u.uid            /* no rows for owner */
  8175.     and     not exists (            /* exclude revoke'd privileges */
  8176.             select *
  8177.             from sysprotects p1
  8178.             where
  8179.                 p1.protecttype = 206
  8180.             and     p1.action = p.action
  8181.             and     p1.id = p.id
  8182.             and     p1.uid = u.uid
  8183.             and     case 
  8184.                 when substring(p1.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  8185.                 when substring(p1.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  8186.                                 else (~convert(tinyint,coalesce(substring(p.columns, v.low, 1),0)))
  8187.                 end & v.high <> 0)            /* permission applies to this column */
  8188.     union all
  8189.     select    /*    Add rows for table owner */
  8190.         GRANTOR        = user_name(u.uid),
  8191.         GRANTEE        = user_name(o.uid),
  8192.         TABLE_CATALOG    = db_name(),
  8193.         TABLE_SCHEMA    = user_name(o.uid),
  8194.         TABLE_NAME    = o.name,
  8195.         COLUMN_NAME    = c.name,
  8196.         COLUMN_GUID    = convert(binary(16),null),
  8197.         COLUMN_PROPID    = convert(int,null),
  8198.         PRIVILEGE_TYPE    = convert(varchar(30),
  8199.                     case v.number
  8200.                     when 193 then 'SELECT'
  8201.                     when 195 then 'INSERT'
  8202.                     when 197 then 'UPDATE'
  8203.                     else 'REFERENCES'
  8204.                     end),
  8205.         IS_GRANTABLE    = convert(bit,1)    
  8206.     from 
  8207.         sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  8208.     where
  8209.         o.name = @table_name
  8210.     and     o.type in ('U','V','S')
  8211.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  8212.     and    (@grantee is null or @grantee = user_name(o.uid))
  8213.     and    c.id = o.id
  8214.     and     (@column_name is null or @column_name = c.name)
  8215.     and     u.suid = 1        /* grantor is dbo of database */
  8216.     and    (@grantor is null or @grantor = user_name(u.uid))
  8217.     and     v.type = 'P'        /* cross product to get all exposed privileges */
  8218.     and     v.number in (26,193,195,197)
  8219.     and     not exists (        /* exclude revoke'd privileges */
  8220.             select *
  8221.             from     sysprotects p1
  8222.             where    p1.protecttype = 206
  8223.             and     p1.action = v.number
  8224.             and     p1.id = o.id
  8225.             and     p1.uid = o.uid)
  8226.     order by 4, 5, 6, 9, 1, 2
  8227.     END
  8228. ELSE
  8229.     BEGIN
  8230.     select
  8231.         GRANTOR        = user_name(p.grantor),
  8232.         GRANTEE        = user_name(u.uid),
  8233.         TABLE_CATALOG    = db_name(),
  8234.         TABLE_SCHEMA    = user_name(o.uid),
  8235.         TABLE_NAME    = o.name,
  8236.         COLUMN_NAME    = c.name,
  8237.         COLUMN_GUID    = convert(binary(16),null),
  8238.         COLUMN_PROPID    = convert(int,null),
  8239.         PRIVILEGE_TYPE    = convert(varchar(30),
  8240.                     case p.action
  8241.                     when 193 then 'SELECT'
  8242.                     when 195 then 'INSERT'
  8243.                     when 197 then 'UPDATE'
  8244.                     else 'REFERENCES'
  8245.                     end),
  8246.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  8247.     from 
  8248.         sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c
  8249.     where
  8250.         o.type in ('U','V','S')
  8251.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  8252.     and    c.id = o.id
  8253.     and     (@column_name is null or @column_name = c.name)
  8254.     and     c.id = p.id
  8255.     and    (@grantor is null or @grantor = user_name(p.grantor))
  8256.     and     case 
  8257.             when substring(p.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  8258.             when substring(p.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p.columns, v.low, 1))
  8259.             else (~convert(tinyint, coalesce(substring(p.columns, v.low, 1),0)))
  8260.             end & v.high <> 0            /* permission applies to this column */
  8261.     and     v.number <= (select count(*) from syscolumns where id = o.id)    /* ranges from 1 to # of columns in table */
  8262.     and     v.type = 'P'
  8263.     and     v.number = c.colid
  8264.     and    (@grantee is null or @grantee = user_name(u.uid))
  8265.             /* expand groups */
  8266.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  8267.     and     p.protecttype <> 206    /* only grant rows */
  8268.     and     p.action in (26,193,195,197)
  8269.     and     o.uid <> u.uid            /* no rows for owner */
  8270.     and     not exists (            /* exclude revoke'd privileges */
  8271.             select *
  8272.             from sysprotects p1
  8273.             where
  8274.                 p1.protecttype = 206
  8275.             and     p1.action = p.action
  8276.             and     p1.id = p.id
  8277.             and     p1.uid = u.uid
  8278.             and     case 
  8279.                 when substring(p1.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  8280.                 when substring(p1.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  8281.                                 else (~convert(tinyint,coalesce(substring(p.columns, v.low, 1),0)))
  8282.                 end & v.high <> 0)            /* permission applies to this column */
  8283.     union all
  8284.     select    /*    Add rows for table owner */
  8285.         GRANTOR        = user_name(u.uid),
  8286.         GRANTEE        = user_name(o.uid),
  8287.         TABLE_CATALOG    = db_name(),
  8288.         TABLE_SCHEMA    = user_name(o.uid),
  8289.         TABLE_NAME    = o.name,
  8290.         COLUMN_NAME    = c.name,
  8291.         COLUMN_GUID    = convert(binary(16),null),
  8292.         COLUMN_PROPID    = convert(int,null),
  8293.         PRIVILEGE_TYPE    = convert(varchar(30),
  8294.                     case v.number
  8295.                     when 193 then 'SELECT'
  8296.                     when 195 then 'INSERT'
  8297.                     when 197 then 'UPDATE'
  8298.                     else 'REFERENCES'
  8299.                     end),
  8300.         IS_GRANTABLE    = convert(bit,1)    
  8301.     from 
  8302.         sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  8303.     where
  8304.          o.type in ('U','V','S')
  8305.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  8306.     and    (@grantee is null or @grantee = user_name(o.uid))
  8307.     and    c.id = o.id
  8308.     and     (@column_name is null or @column_name = c.name)
  8309.     and     u.suid = 1        /* grantor is dbo of database */
  8310.     and    (@grantor is null or @grantor = user_name(u.uid))
  8311.     and     v.type = 'P'        /* cross product to get all exposed privileges */
  8312.     and     v.number in (26,193,195,197)
  8313.     and     not exists (        /* exclude revoke'd privileges */
  8314.             select *
  8315.             from     sysprotects p1
  8316.             where    p1.protecttype = 206
  8317.             and     p1.action = v.number
  8318.             and     p1.id = o.id
  8319.             and     p1.uid = o.uid)
  8320.     order by 4, 5, 6, 9, 1, 2
  8321.     END
  8322. go
  8323. dump tran master with no_log
  8324. go
  8325. CREATE PROCEDURE sp_column_privileges_rowset;2
  8326.     (
  8327.        @handle        int output,
  8328.        @scrollopt    int output,
  8329.     @ccopt        int output,
  8330.     @rows        int output,
  8331.     @table_name     varchar(255) = null,
  8332.     @table_schema    varchar(255) = null,
  8333.     @column_name    varchar(255) = null,
  8334.     @grantor    varchar(255) = null,
  8335.     @grantee    varchar(255) = null
  8336.     )
  8337. as
  8338.  
  8339. declare @ret int
  8340.  
  8341. SET NOCOUNT ON
  8342.     
  8343. create table #spcprivsrowset1
  8344.     (
  8345.     GRANTOR        sysname not null,
  8346.     GRANTEE        sysname not null,
  8347.     TABLE_CATALOG    sysname not null,
  8348.     TABLE_SCHEMA    sysname not null,
  8349.     TABLE_NAME    sysname not null,
  8350.     COLUMN_NAME    sysname not null,
  8351.     COLUMN_GUID    binary(16) null,
  8352.     COLUMN_PROPID    int null,
  8353.     PRIVILEGE_TYPE    sysname not null,
  8354.     IS_GRANTABLE    bit not null
  8355.     )
  8356.  
  8357. IF @table_name is not null
  8358.     BEGIN
  8359.     insert into #spcprivsrowset1
  8360.     select
  8361.         GRANTOR        = user_name(p.grantor),
  8362.         GRANTEE        = user_name(u.uid),
  8363.         TABLE_CATALOG    = db_name(),
  8364.         TABLE_SCHEMA    = user_name(o.uid),
  8365.         TABLE_NAME    = o.name,
  8366.         COLUMN_NAME    = c.name,
  8367.         COLUMN_GUID    = convert(binary(16),null),
  8368.         COLUMN_PROPID    = convert(int,null),
  8369.         PRIVILEGE_TYPE    = convert(varchar(30),
  8370.                     case p.action
  8371.                     when 193 then 'SELECT'
  8372.                     when 195 then 'INSERT'
  8373.                     when 197 then 'UPDATE'
  8374.                     else 'REFERENCES'
  8375.                     end),
  8376.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  8377.     from 
  8378.         sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c
  8379.     where
  8380.         o.name = @table_name
  8381.     and     o.type in ('U','V','S')
  8382.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  8383.     and    c.id = o.id
  8384.     and     (@column_name is null or @column_name = c.name)
  8385.     and     c.id = p.id
  8386.     and    (@grantor is null or @grantor = user_name(p.grantor))
  8387.     and     case 
  8388.             when substring(p.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  8389.             when substring(p.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p.columns, v.low, 1))
  8390.             else (~convert(tinyint, coalesce(substring(p.columns, v.low, 1),0)))
  8391.             end & v.high <> 0            /* permission applies to this column */
  8392.     and     v.number <= (select count(*) from syscolumns where id = o.id)    /* ranges from 1 to # of columns in table */
  8393.     and     v.type = 'P'
  8394.     and     v.number = c.colid
  8395.     and    (@grantee is null or @grantee = user_name(u.uid))
  8396.             /* expand groups */
  8397.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  8398.     and     p.protecttype <> 206    /* only grant rows */
  8399.     and     p.action in (26,193,195,197)
  8400.     and     o.uid <> u.uid            /* no rows for owner */
  8401.     and     not exists (            /* exclude revoke'd privileges */
  8402.             select *
  8403.             from sysprotects p1
  8404.             where
  8405.                 p1.protecttype = 206
  8406.             and     p1.action = p.action
  8407.             and     p1.id = p.id
  8408.             and     p1.uid = u.uid
  8409.             and     case 
  8410.                 when substring(p1.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  8411.                 when substring(p1.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  8412.                                 else (~convert(tinyint,coalesce(substring(p.columns, v.low, 1),0)))
  8413.                 end & v.high <> 0)            /* permission applies to this column */
  8414.     union all
  8415.     select    /*    Add rows for table owner */
  8416.         GRANTOR        = user_name(u.uid),
  8417.         GRANTEE        = user_name(o.uid),
  8418.         TABLE_CATALOG    = db_name(),
  8419.         TABLE_SCHEMA    = user_name(o.uid),
  8420.         TABLE_NAME    = o.name,
  8421.         COLUMN_NAME    = c.name,
  8422.         COLUMN_GUID    = convert(binary(16),null),
  8423.         COLUMN_PROPID    = convert(int,null),
  8424.         PRIVILEGE_TYPE    = convert(varchar(30),
  8425.                     case v.number
  8426.                     when 193 then 'SELECT'
  8427.                     when 195 then 'INSERT'
  8428.                     when 197 then 'UPDATE'
  8429.                     else 'REFERENCES'
  8430.                     end),
  8431.         IS_GRANTABLE    = convert(bit,1)    
  8432.     from 
  8433.         sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  8434.     where
  8435.         o.name = @table_name
  8436.     and     o.type in ('U','V','S')
  8437.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  8438.     and    (@grantee is null or @grantee = user_name(o.uid))
  8439.     and    c.id = o.id
  8440.     and     (@column_name is null or @column_name = c.name)
  8441.     and     u.suid = 1        /* grantor is dbo of database */
  8442.     and    (@grantor is null or @grantor = user_name(u.uid))
  8443.     and     v.type = 'P'        /* cross product to get all exposed privileges */
  8444.     and     v.number in (26,193,195,197)
  8445.     and     not exists (        /* exclude revoke'd privileges */
  8446.             select *
  8447.             from     sysprotects p1
  8448.             where    p1.protecttype = 206
  8449.             and     p1.action = v.number
  8450.             and     p1.id = o.id
  8451.             and     p1.uid = o.uid)
  8452.     order by 4, 5, 6, 9, 1, 2
  8453.     END
  8454. ELSE
  8455.     BEGIN
  8456.     insert into #spcprivsrowset1
  8457.     select
  8458.         GRANTOR        = user_name(p.grantor),
  8459.         GRANTEE        = user_name(u.uid),
  8460.         TABLE_CATALOG    = db_name(),
  8461.         TABLE_SCHEMA    = user_name(o.uid),
  8462.         TABLE_NAME    = o.name,
  8463.         COLUMN_NAME    = c.name,
  8464.         COLUMN_GUID    = convert(binary(16),null),
  8465.         COLUMN_PROPID    = convert(int,null),
  8466.         PRIVILEGE_TYPE    = convert(varchar(30),
  8467.                     case p.action
  8468.                     when 193 then 'SELECT'
  8469.                     when 195 then 'INSERT'
  8470.                     when 197 then 'UPDATE'
  8471.                     else 'REFERENCES'
  8472.                     end),
  8473.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  8474.     from 
  8475.         sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c
  8476.     where
  8477.         o.type in ('U','V','S')
  8478.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  8479.     and    c.id = o.id
  8480.     and     (@column_name is null or @column_name = c.name)
  8481.     and     c.id = p.id
  8482.     and    (@grantor is null or @grantor = user_name(p.grantor))
  8483.     and     case 
  8484.             when substring(p.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  8485.             when substring(p.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p.columns, v.low, 1))
  8486.             else (~convert(tinyint, coalesce(substring(p.columns, v.low, 1),0)))
  8487.             end & v.high <> 0            /* permission applies to this column */
  8488.     and     v.number <= (select count(*) from syscolumns where id = o.id)    /* ranges from 1 to # of columns in table */
  8489.     and     v.type = 'P'
  8490.     and     v.number = c.colid
  8491.     and    (@grantee is null or @grantee = user_name(u.uid))
  8492.             /* expand groups */
  8493.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  8494.     and     p.protecttype <> 206    /* only grant rows */
  8495.     and     p.action in (26,193,195,197)
  8496.     and     o.uid <> u.uid            /* no rows for owner */
  8497.     and     not exists (            /* exclude revoke'd privileges */
  8498.             select *
  8499.             from sysprotects p1
  8500.             where
  8501.                 p1.protecttype = 206
  8502.             and     p1.action = p.action
  8503.             and     p1.id = p.id
  8504.             and     p1.uid = u.uid
  8505.             and     case 
  8506.                 when substring(p1.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  8507.                 when substring(p1.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  8508.                                 else (~convert(tinyint,coalesce(substring(p.columns, v.low, 1),0)))
  8509.                 end & v.high <> 0)            /* permission applies to this column */
  8510.     union all
  8511.     select    /*    Add rows for table owner */
  8512.         GRANTOR        = user_name(u.uid),
  8513.         GRANTEE        = user_name(o.uid),
  8514.         TABLE_CATALOG    = db_name(),
  8515.         TABLE_SCHEMA    = user_name(o.uid),
  8516.         TABLE_NAME    = o.name,
  8517.         COLUMN_NAME    = c.name,
  8518.         COLUMN_GUID    = convert(binary(16),null),
  8519.         COLUMN_PROPID    = convert(int,null),
  8520.         PRIVILEGE_TYPE    = convert(varchar(30),
  8521.                     case v.number
  8522.                     when 193 then 'SELECT'
  8523.                     when 195 then 'INSERT'
  8524.                     when 197 then 'UPDATE'
  8525.                     else 'REFERENCES'
  8526.                     end),
  8527.         IS_GRANTABLE    = convert(bit,1)    
  8528.     from 
  8529.         sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  8530.     where
  8531.          o.type in ('U','V','S')
  8532.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  8533.     and    (@grantee is null or @grantee = user_name(o.uid))
  8534.     and    c.id = o.id
  8535.     and     (@column_name is null or @column_name = c.name)
  8536.     and     u.suid = 1        /* grantor is dbo of database */
  8537.     and    (@grantor is null or @grantor = user_name(u.uid))
  8538.     and     v.type = 'P'        /* cross product to get all exposed privileges */
  8539.     and     v.number in (26,193,195,197)
  8540.     and     not exists (        /* exclude revoke'd privileges */
  8541.             select *
  8542.             from     sysprotects p1
  8543.             where    p1.protecttype = 206
  8544.             and     p1.action = v.number
  8545.             and     p1.id = o.id
  8546.             and     p1.uid = o.uid)
  8547.     order by 4, 5, 6, 9, 1, 2
  8548.     END
  8549.  
  8550. exec @ret = sp_cursoropen @handle output, 'select * from #spcprivsrowset1',
  8551.     @scrollopt output, @ccopt output, @rows output
  8552.  
  8553. drop table #spcprivsrowset1
  8554.  
  8555. return isnull(@ret,0)
  8556. go
  8557. dump tran master with no_log
  8558. go
  8559. CREATE PROCEDURE sp_column_privileges_rowset;3
  8560. as
  8561.     select
  8562.         GRANTOR        = convert(sysname,' '),
  8563.         GRANTEE        = convert(sysname,' '),
  8564.         TABLE_CATALOG    = convert(sysname,' '),
  8565.         TABLE_SCHEMA    = convert(sysname,' '),
  8566.         TABLE_NAME    = convert(sysname,' '),
  8567.         COLUMN_NAME    = convert(sysname,' '),
  8568.         COLUMN_GUID    = convert(binary(16),null),
  8569.         COLUMN_PROPID    = convert(int,null),
  8570.         PRIVILEGE_TYPE    = convert(varchar(30),' '),
  8571.         IS_GRANTABLE    = convert(bit,1)
  8572.     where    1=0
  8573. go
  8574. dump tran master with no_log
  8575. go
  8576.  
  8577. if (charindex('7.00', @@version) = 0)
  8578. begin
  8579.     print ''
  8580.     print ''
  8581.     print 'Warning:'
  8582.     print 'you are installing the stored procedures '
  8583.     print 'on a pre 7.0 SQL Server.'
  8584.     print 'Ignore the following errors.'
  8585. end
  8586. else
  8587.     drop proc sp_column_privileges_rowset
  8588. go
  8589.  
  8590.  
  8591. /*    Procedure for 7.0 server */
  8592. CREATE PROCEDURE sp_column_privileges_rowset
  8593.     (
  8594.     @table_name         sysname,
  8595.     @table_schema        sysname = null,
  8596.     @column_name        sysname = null,
  8597.     @grantor        sysname = null,
  8598.     @grantee        sysname = null
  8599.     )
  8600. as
  8601.     select
  8602.         GRANTOR        = user_name(p.grantor),
  8603.         GRANTEE        = user_name(u.uid),
  8604.         TABLE_CATALOG    = db_name(),
  8605.         TABLE_SCHEMA    = user_name(o.uid),
  8606.         TABLE_NAME    = o.name,
  8607.         COLUMN_NAME    = c.name,
  8608.         COLUMN_GUID    = convert(uniqueidentifier,null),
  8609.         COLUMN_PROPID    = convert(int,null),
  8610.         PRIVILEGE_TYPE    = convert(nvarchar(30),
  8611.                     case p.action
  8612.                     when 193 then N'SELECT'
  8613.                     when 195 then N'INSERT'
  8614.                     when 197 then N'UPDATE'
  8615.                     else N'REFERENCES'
  8616.                     end),
  8617.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  8618.     from 
  8619.         sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c, sysmembers m
  8620.     where
  8621.         o.name = @table_name
  8622.     and     o.type in ('U','V','S')
  8623.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  8624.     and    c.id = o.id
  8625.     and     (@column_name is null or @column_name = c.name)
  8626.     and     c.id = p.id
  8627.     and    (@grantor is null or @grantor = user_name(p.grantor))
  8628.     and     case 
  8629.             when substring(p.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  8630.             when substring(p.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p.columns, v.low, 1))
  8631.             else (~convert(tinyint, coalesce(substring(p.columns, v.low, 1),0)))
  8632.             end & v.high <> 0            /* permission applies to this column */
  8633.     and     v.number <= (select count(*) from syscolumns where id = o.id)    /* ranges from 1 to # of columns in table */
  8634.     and     v.type = 'P'
  8635.     and     v.number = c.colid
  8636.     and    (@grantee is null or @grantee = user_name(u.uid))
  8637.         /* expand groups - AKUNDONE: only 1 level of group unrolling here. Need more?? */
  8638.     and     (u.uid > 0 and u.uid < 16384)
  8639.     and     ((p.uid = u.uid) or  (p.uid = m.groupuid and u.uid = m.memberuid))    
  8640.     and     p.protecttype <> 206    /* only grant rows */
  8641.     and     p.action in (26,193,195,197)
  8642.     and     o.uid <> u.uid            /* no rows for owner */
  8643.     and     not exists (            /* exclude revoke'd privileges */
  8644.             select *
  8645.             from sysprotects p1
  8646.             where
  8647.                 p1.protecttype = 206
  8648.             and     p1.action = p.action
  8649.             and     p1.id = p.id
  8650.             and     p1.uid = u.uid
  8651.             and     case 
  8652.                 when substring(p1.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  8653.                 when substring(p1.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  8654.                                 else (~convert(tinyint,coalesce(substring(p.columns, v.low, 1),0)))
  8655.                 end & v.high <> 0)            /* permission applies to this column */
  8656.     union all
  8657.     select    /*    Add rows for table owner */
  8658.         GRANTOR        = user_name(u.uid),
  8659.         GRANTEE        = user_name(o.uid),
  8660.         TABLE_CATALOG    = db_name(),
  8661.         TABLE_SCHEMA    = user_name(o.uid),
  8662.         TABLE_NAME    = o.name,
  8663.         COLUMN_NAME    = c.name,
  8664.         COLUMN_GUID    = convert(uniqueidentifier,null),
  8665.         COLUMN_PROPID    = convert(int,null),
  8666.         PRIVILEGE_TYPE    = convert(nvarchar(30),
  8667.                     case v.number
  8668.                     when 193 then N'SELECT'
  8669.                     when 195 then N'INSERT'
  8670.                     when 197 then N'UPDATE'
  8671.                     else N'REFERENCES'
  8672.                     end),
  8673.         IS_GRANTABLE    = convert(bit,1)    
  8674.     from 
  8675.         sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  8676.     where
  8677.         o.name = @table_name
  8678.     and     o.type in ('U','V','S')
  8679.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  8680.     and    (@grantee is null or @grantee = user_name(o.uid))
  8681.     and    c.id = o.id
  8682.     and     (@column_name is null or @column_name = c.name)
  8683.     and     u.suid = 1        /* grantor is dbo of database */
  8684.     and    (@grantor is null or @grantor = user_name(u.uid))
  8685.     and     v.type = 'P'        /* cross product to get all exposed privileges */
  8686.     and     v.number in (26,193,195,197)
  8687.     and     not exists (        /* exclude revoke'd privileges */
  8688.             select *
  8689.             from     sysprotects p1
  8690.             where    p1.protecttype = 206
  8691.             and     p1.action = v.number
  8692.             and     p1.id = o.id
  8693.             and     p1.uid = o.uid)
  8694.     order by 4, 5, 6, 9, 1, 2
  8695. go
  8696. dump tran master with no_log
  8697. go
  8698. CREATE PROCEDURE sp_column_privileges_rowset;2
  8699.     (
  8700.     @table_schema        sysname = null,
  8701.     @column_name        sysname = null,
  8702.     @grantor        sysname = null,
  8703.     @grantee        sysname = null
  8704.     )
  8705. as
  8706.     select
  8707.         GRANTOR        = user_name(p.grantor),
  8708.         GRANTEE        = user_name(u.uid),
  8709.         TABLE_CATALOG    = db_name(),
  8710.         TABLE_SCHEMA    = user_name(o.uid),
  8711.         TABLE_NAME    = o.name,
  8712.         COLUMN_NAME    = c.name,
  8713.         COLUMN_GUID    = convert(uniqueidentifier,null),
  8714.         COLUMN_PROPID    = convert(int,null),
  8715.         PRIVILEGE_TYPE    = convert(nvarchar(30),
  8716.                     case p.action
  8717.                     when 193 then N'SELECT'
  8718.                     when 195 then N'INSERT'
  8719.                     when 197 then N'UPDATE'
  8720.                     else N'REFERENCES'
  8721.                     end),
  8722.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  8723.     from 
  8724.         sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c, sysmembers m
  8725.     where
  8726.         o.type in ('U','V','S')
  8727.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  8728.     and    c.id = o.id
  8729.     and     (@column_name is null or @column_name = c.name)
  8730.     and     c.id = p.id
  8731.     and    (@grantor is null or @grantor = user_name(p.grantor))
  8732.     and     case 
  8733.             when substring(p.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  8734.             when substring(p.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p.columns, v.low, 1))
  8735.             else (~convert(tinyint, coalesce(substring(p.columns, v.low, 1),0)))
  8736.             end & v.high <> 0            /* permission applies to this column */
  8737.     and     v.number <= (select count(*) from syscolumns where id = o.id)    /* ranges from 1 to # of columns in table */
  8738.     and     v.type = 'P'
  8739.     and     v.number = c.colid
  8740.     and    (@grantee is null or @grantee = user_name(u.uid))
  8741.         /* expand groups - AKUNDONE: only 1 level of group unrolling here. Need more?? */
  8742.     and     (u.uid > 0 and u.uid < 16384)
  8743.     and     ((p.uid = u.uid) or  (p.uid = m.groupuid and u.uid = m.memberuid))    
  8744.     and     p.protecttype <> 206    /* only grant rows */
  8745.     and     p.action in (26,193,195,197)
  8746.     and     o.uid <> u.uid            /* no rows for owner */
  8747.     and     not exists (            /* exclude revoke'd privileges */
  8748.             select *
  8749.             from sysprotects p1
  8750.             where
  8751.                 p1.protecttype = 206
  8752.             and     p1.action = p.action
  8753.             and     p1.id = p.id
  8754.             and     p1.uid = u.uid
  8755.             and     case 
  8756.                 when substring(p1.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  8757.                 when substring(p1.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  8758.                                 else (~convert(tinyint,coalesce(substring(p.columns, v.low, 1),0)))
  8759.                 end & v.high <> 0)            /* permission applies to this column */
  8760.     union all
  8761.     select    /*    Add rows for table owner */
  8762.         GRANTOR        = user_name(u.uid),
  8763.         GRANTEE        = user_name(o.uid),
  8764.         TABLE_CATALOG    = db_name(),
  8765.         TABLE_SCHEMA    = user_name(o.uid),
  8766.         TABLE_NAME    = o.name,
  8767.         COLUMN_NAME    = c.name,
  8768.         COLUMN_GUID    = convert(uniqueidentifier,null),
  8769.         COLUMN_PROPID    = convert(int,null),
  8770.         PRIVILEGE_TYPE    = convert(nvarchar(30),
  8771.                     case v.number
  8772.                     when 193 then N'SELECT'
  8773.                     when 195 then N'INSERT'
  8774.                     when 197 then N'UPDATE'
  8775.                     else N'REFERENCES'
  8776.                     end),
  8777.         IS_GRANTABLE    = convert(bit,1)    
  8778.     from 
  8779.         sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  8780.     where
  8781.         o.type in ('U','V','S')
  8782.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  8783.     and    (@grantee is null or @grantee = user_name(o.uid))
  8784.     and    c.id = o.id
  8785.     and     (@column_name is null or @column_name = c.name)
  8786.     and     u.suid = 1        /* grantor is dbo of database */
  8787.     and    (@grantor is null or @grantor = user_name(u.uid))
  8788.     and     v.type = 'P'        /* cross product to get all exposed privileges */
  8789.     and     v.number in (26,193,195,197)
  8790.     and     not exists (        /* exclude revoke'd privileges */
  8791.             select *
  8792.             from     sysprotects p1
  8793.             where    p1.protecttype = 206
  8794.             and     p1.action = v.number
  8795.             and     p1.id = o.id
  8796.             and     p1.uid = o.uid)
  8797.     order by 4, 5, 6, 9, 1, 2
  8798. go
  8799. dump tran master with no_log
  8800. go
  8801. create procedure sp_column_privileges_rowset;5
  8802.     (
  8803.     @table_server        sysname,
  8804.     @table_catalog        sysname = null,
  8805.     @table_name        sysname = null,
  8806.     @table_schema        sysname = null,
  8807.     @column_name        sysname = null,
  8808.     @grantor        sysname = null,
  8809.     @grantee        sysname = null
  8810.     )
  8811. as
  8812.     select
  8813.         GRANTOR,
  8814.         GRANTEE,
  8815.         TABLE_CATALOG,
  8816.         TABLE_SCHEMA,
  8817.         TABLE_NAME,
  8818.         COLUMN_NAME,
  8819.         COLUMN_GUID,
  8820.         COLUMN_PROPID,    
  8821.         PRIVILEGE_TYPE,
  8822.         IS_GRANTABLE    
  8823.         from master.dbo.SYSREMOTE_COLUMN_PRIVILEGES <
  8824.                 @table_server,
  8825.                 @table_catalog,
  8826.                 @table_schema,
  8827.                 @table_name,
  8828.                 @column_name,
  8829.                 @grantor,
  8830.                 @grantee>
  8831.     order by 3, 4, 5, 6, 9, 1, 2
  8832. go
  8833.  
  8834. grant execute on sp_column_privileges_rowset to public
  8835. go
  8836.  
  8837. dump tran master with no_log
  8838. go
  8839. if (charindex('6.00', @@version) > 0)
  8840.     begin
  8841.     if (exists (select * from sysobjects
  8842.             where name = 'sp_column_privileges_rowset' and type = 'P '))
  8843.         begin
  8844.         drop procedure sp_column_privileges_rowset
  8845.         dump tran master with no_log
  8846.         end
  8847.     end
  8848. go
  8849.  
  8850.  
  8851. print ''
  8852. print 'creating sp_columns_rowset'
  8853. go
  8854.  
  8855. /*    Procedure for 6.0 and 6.50 server */
  8856. create procedure sp_columns_rowset
  8857.     (
  8858.        @table_name    varchar(255),
  8859.     @table_schema     varchar(255) = null,
  8860.     @column_name    varchar(255) = null
  8861.     )
  8862. as     
  8863.     select
  8864.         TABLE_CATALOG        = db_name(),
  8865.         TABLE_SCHEMA        = user_name(o.uid),
  8866.         TABLE_NAME        = o.name,
  8867.         COLUMN_NAME        = c.name,
  8868.         COLUMN_GUID        = convert(binary(16),null),
  8869.         COLUMN_PROPID        = convert(int,null),
  8870.         ORDINAL_POSITION    = convert(int,c.colid),
  8871.         COLUMN_HASDEFAULT    = convert(bit, case when m.text is null then 0 else 1 end),
  8872.         COLUMN_DEFAULT        = substring(m.text,2,datalength(m.text)-2),
  8873.         COLUMN_FLAGS        = convert(int,
  8874.                         case when d.is_long = 1 
  8875.                         then 0x82 /*DBCOLUMNFLAGS_ISLONG|DBCOLUMNFLAGS_MAYDEFER*/
  8876.                         else 0
  8877.                         end
  8878.                     |    case when d.fixlen is not null
  8879.                         then 0x10 /*DBCOLUMNFLAGS_ISFIXEDLENGTH*/
  8880.                         else 0
  8881.                         end
  8882.                     |    case when c.status&8 = 8
  8883.                         then 0x20 /*DBCOLUMNFLAGS_ISNULLABLE*/
  8884.                         else 0
  8885.                         end
  8886.                     |    case 
  8887.                         when d.type_name = 'timestamp'
  8888.                             then 0x200 /*DBCOLUMNFLAGS_ISROWVER*/
  8889.                         when (c.status&128) != 128
  8890.                             then 0x8 /*DBCOLUMNFLAGS_WRITEUNKNOWN*/ 
  8891.                         else 0 
  8892.                         end),
  8893.         IS_NULLABLE        = convert(bit,c.status&8),
  8894.         DATA_TYPE        = d.oledb_data_type,
  8895.         TYPE_GUID        = convert(binary(16),null),
  8896.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  8897.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  8898.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  8899.                         then coalesce(d.column_size,c.length)
  8900.                         else null 
  8901.                         end),
  8902.         CHARACTER_OCTET_LENGTH    = convert(int,
  8903.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  8904.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  8905.                         then coalesce(d.column_size,c.length)
  8906.                         else null 
  8907.                         end),
  8908.         NUMERIC_PRECISION    = convert(smallint,
  8909.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec else null end),
  8910.         NUMERIC_SCALE        = convert(smallint, 
  8911.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  8912.         DATETIME_PRECISION    = convert(int,
  8913.                         case when d.oledb_data_type = 135 /*DBTYPE_DBTIMESTAMP*/ then 3 else null end),
  8914.         CHARACTER_SET_CATALOG    = convert(sysname,
  8915.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  8916.                         then 'master' 
  8917.                         else null 
  8918.                         end),
  8919.         CHARACTER_SET_SCHEMA    = convert(sysname,
  8920.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  8921.                         then 'dbo' 
  8922.                         else null 
  8923.                         end),
  8924.         CHARACTER_SET_NAME    = convert(sysname,
  8925.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  8926.                         then a_cha.name 
  8927.                         else null 
  8928.                         end),
  8929.         COLLATION_CATALOG    = convert(sysname,
  8930.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  8931.                         then 'master' 
  8932.                         else null 
  8933.                         end),
  8934.         COLLATION_SCHEMA    = convert(sysname,
  8935.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  8936.                         then 'dbo' 
  8937.                         else null 
  8938.                         end),
  8939.         COLLATION_NAME        = convert(sysname,
  8940.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  8941.                         then b_cha.name 
  8942.                         else null 
  8943.                         end),
  8944.         DOMAIN_CATALOG        = case when t.usertype < 256 then null else db_name() end,
  8945.         DOMAIN_SCHEMA        = case when t.usertype < 256 then null else user_name(o.uid) end,
  8946.         DOMAIN_NAME        = case when t.usertype < 256 then null else t.name end,
  8947.         DESCRIPTION        = convert(varchar(1),null)    
  8948.     
  8949.     from
  8950.         syscolumns c,
  8951.         syscomments m,
  8952.         sysobjects o,
  8953.         master.dbo.spt_provider_types d,
  8954.         systypes t,
  8955.         sysusers u,
  8956.         master.dbo.sysconfigures    cfg,
  8957.         master.dbo.syscharsets        a_cha, /* charset/1001, not sortorder. */
  8958.         master.dbo.syscharsets        b_cha  /* sortorder/2001, not charset. */
  8959.     where
  8960.         o.name = @table_name
  8961.     and     o.type in ('U','V','S')
  8962.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  8963.     and     (
  8964.         suser_id() = 1     /* User is the System Administrator */
  8965.         or o.uid = user_id()     /* User created the object */
  8966.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  8967.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  8968.             from sysprotects p
  8969.             where p.id = o.id
  8970.             /* get rows for public,current user,user's group */
  8971.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  8972.             /* check for SELECT,EXECUTE privilege */
  8973.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  8974.             ) = 1    /* final magic...compare Grants      */
  8975.            )
  8976.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  8977.     and    (@column_name is null or @column_name = c.name)
  8978.     and     o.id = c.id
  8979.     and     t.type = d.ss_dtype
  8980.     and    (t.usertype != 80 or d.type_name='timestamp')
  8981.     and     c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  8982.     and     c.usertype = t.usertype
  8983.     and    c.cdefault *= m.id
  8984.     and    m.colid = 1
  8985.     and    cfg.comment = 'default sortorder id' 
  8986.     and    a_cha.type = 1001 /* type is charset */
  8987.     and    b_cha.type = 2001 /* type is sortorder */
  8988.     and    a_cha.id = b_cha.csid
  8989.     and     b_cha.id = cfg.value
  8990.     order by 2, 3, 7
  8991. go
  8992. dump tran master with no_log
  8993. go
  8994. create procedure sp_columns_rowset;2
  8995.     (
  8996.     @table_schema     varchar(255) = null,
  8997.     @column_name    varchar(255) = null
  8998.     )
  8999. as     
  9000.     select
  9001.         TABLE_CATALOG        = db_name(),
  9002.         TABLE_SCHEMA        = user_name(o.uid),
  9003.         TABLE_NAME        = o.name,
  9004.         COLUMN_NAME        = c.name,
  9005.         COLUMN_GUID        = convert(binary(16),null),
  9006.         COLUMN_PROPID        = convert(int,null),
  9007.         ORDINAL_POSITION    = convert(int,c.colid),
  9008.         COLUMN_HASDEFAULT    = convert(bit, case when m.text is null then 0 else 1 end),
  9009.         COLUMN_DEFAULT        = substring(m.text,2,datalength(m.text)-2),
  9010.         COLUMN_FLAGS        = convert(int,
  9011.                         case when d.is_long = 1 
  9012.                         then 0x82 /*DBCOLUMNFLAGS_ISLONG|DBCOLUMNFLAGS_MAYDEFER*/
  9013.                         else 0
  9014.                         end
  9015.                     |    case when d.fixlen is not null
  9016.                         then 0x10 /*DBCOLUMNFLAGS_ISFIXEDLENGTH*/
  9017.                         else 0
  9018.                         end
  9019.                     |    case when c.status&8 = 8
  9020.                         then 0x20 /*DBCOLUMNFLAGS_ISNULLABLE*/
  9021.                         else 0
  9022.                         end
  9023.                     |    case 
  9024.                         when d.type_name = 'timestamp'
  9025.                             then 0x200 /*DBCOLUMNFLAGS_ISROWVER*/
  9026.                         when (c.status&128) != 128
  9027.                             then 0x8 /*DBCOLUMNFLAGS_WRITEUNKNOWN*/ 
  9028.                         else 0 
  9029.                         end),
  9030.         IS_NULLABLE        = convert(bit,c.status&8),
  9031.         DATA_TYPE        = d.oledb_data_type,
  9032.         TYPE_GUID        = convert(binary(16),null),
  9033.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  9034.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  9035.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  9036.                         then coalesce(d.column_size,c.length)
  9037.                         else null 
  9038.                         end),
  9039.         CHARACTER_OCTET_LENGTH    = convert(int,
  9040.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  9041.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  9042.                         then coalesce(d.column_size,c.length)
  9043.                         else null 
  9044.                         end),
  9045.         NUMERIC_PRECISION    = convert(smallint,
  9046.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec else null end),
  9047.         NUMERIC_SCALE        = convert(smallint, 
  9048.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  9049.         DATETIME_PRECISION    = convert(int,
  9050.                         case when d.oledb_data_type = 135 /*DBTYPE_DBTIMESTAMP*/ then 3 else null end),
  9051.         CHARACTER_SET_CATALOG    = convert(sysname,
  9052.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9053.                         then 'master' 
  9054.                         else null 
  9055.                         end),
  9056.         CHARACTER_SET_SCHEMA    = convert(sysname,
  9057.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9058.                         then 'dbo' 
  9059.                         else null 
  9060.                         end),
  9061.         CHARACTER_SET_NAME    = convert(sysname,
  9062.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9063.                         then a_cha.name 
  9064.                         else null 
  9065.                         end),
  9066.         COLLATION_CATALOG    = convert(sysname,
  9067.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9068.                         then 'master' 
  9069.                         else null 
  9070.                         end),
  9071.         COLLATION_SCHEMA    = convert(sysname,
  9072.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9073.                         then 'dbo' 
  9074.                         else null 
  9075.                         end),
  9076.         COLLATION_NAME        = convert(sysname,
  9077.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9078.                         then b_cha.name 
  9079.                         else null 
  9080.                         end),
  9081.         DOMAIN_CATALOG        = case when t.usertype < 256 then null else db_name() end,
  9082.         DOMAIN_SCHEMA        = case when t.usertype < 256 then null else user_name(o.uid) end,
  9083.         DOMAIN_NAME        = case when t.usertype < 256 then null else t.name end,
  9084.         DESCRIPTION        = convert(varchar(1),null)    
  9085.     
  9086.     from
  9087.         syscolumns c,
  9088.         syscomments m,
  9089.         sysobjects o,
  9090.         master.dbo.spt_provider_types d,
  9091.         systypes t,
  9092.         sysusers u,
  9093.         master.dbo.sysconfigures    cfg,
  9094.         master.dbo.syscharsets        a_cha, /* charset/1001, not sortorder. */
  9095.         master.dbo.syscharsets        b_cha /* sortorder/2001, not charset. */
  9096.     where
  9097.          o.type in ('U','V','S')
  9098.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  9099.     and     (
  9100.         suser_id() = 1     /* User is the System Administrator */
  9101.         or o.uid = user_id()     /* User created the object */
  9102.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  9103.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  9104.             from sysprotects p
  9105.             where p.id = o.id
  9106.             /* get rows for public,current user,user's group */
  9107.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  9108.             /* check for SELECT,EXECUTE privilege */
  9109.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  9110.             ) = 1    /* final magic...compare Grants      */
  9111.            )
  9112.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  9113.     and    (@column_name is null or @column_name = c.name)
  9114.     and     o.id = c.id
  9115.     and     t.type = d.ss_dtype
  9116.     and    (t.usertype != 80 or d.type_name='timestamp')
  9117.     and     c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  9118.     and     c.usertype = t.usertype
  9119.     and    c.cdefault *= m.id
  9120.     and     m.colid = 1
  9121.     and    cfg.comment = 'default sortorder id' 
  9122.     and    a_cha.type = 1001 /* type is charset */
  9123.     and    b_cha.type = 2001 /* type is sortorder */
  9124.     and    a_cha.id = b_cha.csid
  9125.     and     b_cha.id = cfg.value
  9126.     order by 2, 3, 7
  9127. go
  9128. dump tran master with no_log
  9129. go
  9130.  
  9131. if (charindex('7.00', @@version) = 0)
  9132. begin
  9133.     print ''
  9134.     print ''
  9135.     print 'Warning:'
  9136.     print 'you are installing the stored procedures '
  9137.     print 'on a pre 7.0 SQL Server.'
  9138.     print 'Ignore the following errors.'
  9139. end
  9140. else
  9141.     drop proc sp_columns_rowset
  9142. go
  9143.  
  9144. /*    Procedure for 7.0 server */
  9145. create procedure sp_columns_rowset
  9146.     (
  9147.        @table_name    sysname,
  9148.     @table_schema     sysname = NULL,
  9149.     @column_name    sysname = NULL
  9150.     )
  9151. as     
  9152.     select
  9153.         TABLE_CATALOG        = db_name(),
  9154.         TABLE_SCHEMA        = user_name(o.uid),
  9155.         TABLE_NAME        = o.name,
  9156.         COLUMN_NAME        = c.name,
  9157.         COLUMN_GUID        = convert(uniqueidentifier,null),
  9158.         COLUMN_PROPID        = convert(int,null),
  9159.         ORDINAL_POSITION    = convert(int,c.colid),
  9160.         COLUMN_HASDEFAULT    = convert(bit, case when m.text is null then 0 else 1 end),
  9161.         COLUMN_DEFAULT        = convert(nvarchar(2000),
  9162.                         substring(m.text,2,datalength(m.text)/2-2)),
  9163.         COLUMN_FLAGS        = convert(int,
  9164.                         case when d.is_long = 1 
  9165.                         then 0x82 /*DBCOLUMNFLAGS_ISLONG|DBCOLUMNFLAGS_MAYDEFER*/
  9166.                         else 0
  9167.                         end
  9168.                     |    case when d.fixlen is not null
  9169.                         then 0x10 /*DBCOLUMNFLAGS_ISFIXEDLENGTH*/
  9170.                         else 0
  9171.                         end
  9172.                     |    case when ColumnProperty(c.id,c.name,'AllowsNull') = 1
  9173.                         then 0x20 /*DBCOLUMNFLAGS_ISNULLABLE*/
  9174.                         else 0
  9175.                         end
  9176.                     |    case 
  9177.                         when d.type_name = 'timestamp'
  9178.                             then 0x200 /*DBCOLUMNFLAGS_ISROWVER*/
  9179.                         when (c.status&128) != 128  and permissions(o.id,c.name)&2 = 2
  9180.                             then 0x4 /*DBCOLUMNFLAGS_WRITE*/ 
  9181.                         else 0 
  9182.                         end),
  9183.         IS_NULLABLE        = convert(bit,ColumnProperty(c.id,c.name,'AllowsNull')),
  9184.         DATA_TYPE        = d.oledb_data_type,
  9185.         TYPE_GUID        = convert(uniqueidentifier,null),
  9186.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  9187.                         case 
  9188.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  9189.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  9190.                         then coalesce(d.column_size,c.length)
  9191.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  9192.                         then coalesce(d.column_size,c.length/2)
  9193.                         else null 
  9194.                         end),
  9195.         CHARACTER_OCTET_LENGTH    = convert(int,
  9196.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  9197.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  9198.                             or d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  9199.                         then coalesce(d.column_size,c.length)
  9200.                         else null 
  9201.                         end),
  9202.         NUMERIC_PRECISION    = convert(smallint,
  9203.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.xprec else null end),
  9204.         NUMERIC_SCALE        = convert(smallint, 
  9205.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  9206.         DATETIME_PRECISION    = convert(int,
  9207.                         case when d.oledb_data_type = 135 /*DBTYPE_DBTIMESTAMP*/ then 3 else null end),
  9208.         CHARACTER_SET_CATALOG    = convert(sysname,
  9209.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9210.                         then N'master' 
  9211.                         else null 
  9212.                         end),
  9213.         CHARACTER_SET_SCHEMA    = convert(sysname,
  9214.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9215.                         then N'dbo' 
  9216.                         else null 
  9217.                         end),
  9218.         CHARACTER_SET_NAME    = convert(sysname,
  9219.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9220.                         then a_cha.name 
  9221.                         else null 
  9222.                         end),
  9223.         COLLATION_CATALOG    = convert(sysname,
  9224.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9225.                         then N'master' 
  9226.                         else null 
  9227.                         end),
  9228.         COLLATION_SCHEMA    = convert(sysname,
  9229.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9230.                         then N'dbo' 
  9231.                         else null 
  9232.                         end),
  9233.         COLLATION_NAME        = convert(sysname,
  9234.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9235.                         then b_cha.name 
  9236.                         else null 
  9237.                         end),
  9238.         DOMAIN_CATALOG        = case when t.usertype < 256 then null else db_name() end,
  9239.         DOMAIN_SCHEMA        = case when t.usertype < 256 then null else user_name(o.uid) end,
  9240.         DOMAIN_NAME        = case when t.usertype < 256 then null else t.name end,
  9241.         DESCRIPTION        = convert(nvarchar(1),null)    
  9242.     
  9243.     from
  9244.         syscolumns c left join syscomments m on c.cdefault = m.id and m.colid = 1,
  9245.         sysobjects o,
  9246.         master.dbo.spt_provider_types d,
  9247.         systypes t,
  9248.         master.dbo.sysconfigures    cfg,
  9249.         master.dbo.syscharsets        a_cha, /* charset/1001, not sortorder. */
  9250.         master.dbo.syscharsets        b_cha /* sortorder/2001, not charset. */
  9251.     where
  9252.         permissions(o.id, c.name) <> 0
  9253.     and    o.name = @table_name
  9254.     and     o.type in ('U','V','S')
  9255.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  9256.     and    (@column_name is null or @column_name = c.name)
  9257.     and     o.id = c.id
  9258.     and     t.xtype = d.ss_dtype
  9259.     and     c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  9260.     and     c.xusertype = t.xusertype
  9261.     and    cfg.comment = 'default sortorder id' 
  9262.     and    a_cha.type = 1001 /* type is charset */
  9263.     and    b_cha.type = 2001 /* type is sortorder */
  9264.     and    a_cha.id = b_cha.csid
  9265.     and     b_cha.id = cfg.value
  9266.     order by 2, 3, 7
  9267. go
  9268. dump tran master with no_log
  9269. go
  9270. create procedure sp_columns_rowset;2
  9271.     (
  9272.     @table_schema     sysname = NULL,
  9273.     @column_name    sysname = NULL
  9274.     )
  9275. as     
  9276.     select
  9277.         TABLE_CATALOG        = db_name(),
  9278.         TABLE_SCHEMA        = user_name(o.uid),
  9279.         TABLE_NAME        = o.name,
  9280.         COLUMN_NAME        = c.name,
  9281.         COLUMN_GUID        = convert(uniqueidentifier,null),
  9282.         COLUMN_PROPID        = convert(int,null),
  9283.         ORDINAL_POSITION    = convert(int,c.colid),
  9284.         COLUMN_HASDEFAULT    = convert(bit, case when m.text is null then 0 else 1 end),
  9285.         COLUMN_DEFAULT        = convert(nvarchar(2000),
  9286.                         substring(m.text,2,datalength(m.text)/2-2)),
  9287.         COLUMN_FLAGS        = convert(int,
  9288.                         case when d.is_long = 1 
  9289.                         then 0x82 /*DBCOLUMNFLAGS_ISLONG|DBCOLUMNFLAGS_MAYDEFER*/
  9290.                         else 0
  9291.                         end
  9292.                     |    case when d.fixlen is not null
  9293.                         then 0x10 /*DBCOLUMNFLAGS_ISFIXEDLENGTH*/
  9294.                         else 0
  9295.                         end
  9296.                     |    case when ColumnProperty(c.id,c.name,'AllowsNull') = 1
  9297.                         then 0x20 /*DBCOLUMNFLAGS_ISNULLABLE*/
  9298.                         else 0
  9299.                         end
  9300.                     |    case 
  9301.                         when d.type_name = 'timestamp'
  9302.                             then 0x200 /*DBCOLUMNFLAGS_ISROWVER*/
  9303.                         when (c.status&128) != 128 and permissions(o.id,c.name)&2 = 2
  9304.                             then 0x4 /*DBCOLUMNFLAGS_WRITE*/ 
  9305.                         else 0 
  9306.                         end),
  9307.         IS_NULLABLE        = convert(bit,ColumnProperty(c.id,c.name,'AllowsNull')),
  9308.         DATA_TYPE        = d.oledb_data_type,
  9309.         TYPE_GUID        = convert(uniqueidentifier,null),
  9310.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  9311.                         case 
  9312.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  9313.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  9314.                         then coalesce(d.column_size,c.length)
  9315.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  9316.                         then coalesce(d.column_size,c.length/2)
  9317.                         else null 
  9318.                         end),
  9319.         CHARACTER_OCTET_LENGTH    = convert(int,
  9320.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  9321.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  9322.                             or d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  9323.                         then coalesce(d.column_size,c.length)
  9324.                         else null 
  9325.                         end),
  9326.         NUMERIC_PRECISION    = convert(smallint,
  9327.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.xprec else null end),
  9328.         NUMERIC_SCALE        = convert(smallint, 
  9329.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  9330.         DATETIME_PRECISION    = convert(int,
  9331.                         case when d.oledb_data_type = 135 /*DBTYPE_DBTIMESTAMP*/ then 3 else null end),
  9332.         CHARACTER_SET_CATALOG    = convert(sysname,
  9333.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9334.                         then N'master' 
  9335.                         else null 
  9336.                         end),
  9337.         CHARACTER_SET_SCHEMA    = convert(sysname,
  9338.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9339.                         then N'dbo' 
  9340.                         else null 
  9341.                         end),
  9342.         CHARACTER_SET_NAME    = convert(sysname,
  9343.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9344.                         then a_cha.name 
  9345.                         else null 
  9346.                         end),
  9347.         COLLATION_CATALOG    = convert(sysname,
  9348.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9349.                         then N'master' 
  9350.                         else null 
  9351.                         end),
  9352.         COLLATION_SCHEMA    = convert(sysname,
  9353.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9354.                         then N'dbo' 
  9355.                         else null 
  9356.                         end),
  9357.         COLLATION_NAME        = convert(sysname,
  9358.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9359.                         then b_cha.name 
  9360.                         else null 
  9361.                         end),
  9362.         DOMAIN_CATALOG        = case when t.usertype < 256 then null else db_name() end,
  9363.         DOMAIN_SCHEMA        = case when t.usertype < 256 then null else user_name(o.uid) end,
  9364.         DOMAIN_NAME        = case when t.usertype < 256 then null else t.name end,
  9365.         DESCRIPTION        = convert(nvarchar(1),null)    
  9366.     
  9367.     from
  9368.         syscolumns c left join syscomments m on c.cdefault = m.id and m.colid = 1,
  9369.         sysobjects o,
  9370.         master.dbo.spt_provider_types d,
  9371.         systypes t,
  9372.         master.dbo.sysconfigures    cfg,
  9373.         master.dbo.syscharsets        a_cha, /* charset/1001, not sortorder.*/
  9374.         master.dbo.syscharsets        b_cha /* sortorder/2001, not charset.*/
  9375.     where
  9376.         permissions(o.id, c.name) <> 0
  9377.     and     o.type in ('U','V','S')
  9378.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  9379.     and    (@column_name is null or @column_name = c.name)
  9380.     and     o.id = c.id
  9381.     and     t.xtype = d.ss_dtype
  9382.     and     c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  9383.     and     c.xusertype = t.xusertype
  9384.     and    cfg.comment = 'default sortorder id' 
  9385.     and a_cha.type = 1001 /* type is charset */
  9386.     and b_cha.type = 2001 /* type is sortorder */
  9387.     and    a_cha.id = b_cha.csid
  9388.     and     b_cha.id = cfg.value
  9389.     order by 2, 3, 7
  9390. go
  9391. dump tran master with no_log
  9392. go
  9393. create procedure sp_columns_rowset;5
  9394.     (
  9395.     @table_server        sysname,
  9396.     @table_catalog        sysname = null,
  9397.     @table_name        sysname = null,
  9398.     @table_schema        sysname = null,
  9399.     @column_name        sysname = null
  9400.     )
  9401. as
  9402.     select
  9403.         TABLE_CATALOG,
  9404.         TABLE_SCHEMA,
  9405.         TABLE_NAME,
  9406.         COLUMN_NAME,
  9407.         COLUMN_GUID,
  9408.         COLUMN_PROPID,
  9409.         ORDINAL_POSITION,
  9410.         COLUMN_HASDEFAULT,
  9411.         COLUMN_DEFAULT,
  9412.         COLUMN_FLAGS,
  9413.         IS_NULLABLE,
  9414.         DATA_TYPE,
  9415.         TYPE_GUID,
  9416.         CHARACTER_MAXIMUM_LENGTH,
  9417.         CHARACTER_OCTET_LENGTH,
  9418.         NUMERIC_PRECISION,
  9419.         NUMERIC_SCALE,
  9420.         DATETIME_PRECISION,
  9421.         CHARACTER_SET_CATALOG,
  9422.         CHARACTER_SET_SCHEMA,
  9423.         CHARACTER_SET_NAME,
  9424.         COLLATION_CATALOG,
  9425.         COLLATION_SCHEMA,
  9426.         COLLATION_NAME,
  9427.         DOMAIN_CATALOG,
  9428.         DOMAIN_SCHEMA,
  9429.         DOMAIN_NAME,
  9430.         DESCRIPTION
  9431.     from master.dbo.SYSREMOTE_COLUMNS <
  9432.                 @table_server,
  9433.                 @table_catalog,
  9434.                 @table_schema,
  9435.                 @table_name,
  9436.                 @column_name > 
  9437.     order by 1, 2, 3, 7
  9438. go
  9439.  
  9440. grant execute on sp_columns_rowset to public
  9441. go
  9442.  
  9443. dump tran master with no_log
  9444. go
  9445. if (charindex('6.00', @@version) > 0)
  9446.     begin
  9447.     if (exists (select * from sysobjects
  9448.             where name = 'sp_columns_rowset' and type = 'P '))
  9449.         begin
  9450.         drop procedure sp_columns_rowset
  9451.         dump tran master with no_log
  9452.         end
  9453.     end
  9454. go
  9455.  
  9456.  
  9457. print ''
  9458. print 'creating sp_foreign_keys_rowset'
  9459. go
  9460.  
  9461. /*    Procedure for 6.0 and 6.5 server */
  9462. CREATE PROCEDURE sp_foreign_keys_rowset
  9463.     (
  9464.      @pk_table_name        varchar(255) = null,
  9465.     @pk_table_schema    varchar(255) = null,
  9466.     @pk_table_catalog    varchar(255) = null,
  9467.     @fk_table_name        varchar(255) = null,
  9468.     @fk_table_schema    varchar(255) = null,
  9469.     @fk_table_catalog    varchar(255) = null
  9470.     )
  9471. as
  9472. IF @pk_table_name is not null
  9473.     BEGIN
  9474.     select
  9475.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  9476.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  9477.         PK_TABLE_NAME         = o1.name,
  9478.         PK_COLUMN_NAME         = c1.name,
  9479.         PK_COLUMN_GUID        = convert(binary(16),null),
  9480.         PK_COLUMN_PROPID    = convert(int,null),
  9481.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  9482.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  9483.         FK_TABLE_NAME         = o2.name,
  9484.         FK_COLUMN_NAME         = c2.name,
  9485.         FK_COLUMN_GUID        = convert(binary(16),null),
  9486.         FK_COLUMN_PROPID    = convert(int,null),
  9487.         ORDINAL            = convert(int,1),
  9488.         UPDATE_RULE        = 'NO ACTION',
  9489.         DELETE_RULE         = 'NO ACTION'        
  9490.     from
  9491.         sysobjects o1, sysobjects o2,
  9492.         syscolumns c1, syscolumns c2,
  9493.         sysreferences r
  9494.     where    
  9495.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  9496.     and    o1.name = @pk_table_name
  9497.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  9498.     and    o1.id = r.rkeyid
  9499.     and    o1.id = c1.id
  9500.     and    c1.colid = r.rkey1
  9501.     and     r.fkeyid = o2.id
  9502.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  9503.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  9504.     and    o2.id = c2.id    
  9505.     and    c2.colid = r.fkey1
  9506.     union all
  9507.     select
  9508.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  9509.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  9510.         PK_TABLE_NAME         = o1.name,
  9511.         PK_COLUMN_NAME         = c1.name,
  9512.         PK_COLUMN_GUID        = convert(binary(16),null),
  9513.         PK_COLUMN_PROPID    = convert(int,null),
  9514.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  9515.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  9516.         FK_TABLE_NAME         = o2.name,
  9517.         FK_COLUMN_NAME         = c2.name,
  9518.         FK_COLUMN_GUID        = convert(binary(16),null),
  9519.         FK_COLUMN_PROPID    = convert(int,null),
  9520.         ORDINAL            = convert(int,2),
  9521.         UPDATE_RULE        = 'NO ACTION',
  9522.         DELETE_RULE         = 'NO ACTION'        
  9523.     from
  9524.         sysobjects o1, sysobjects o2,
  9525.         syscolumns c1, syscolumns c2,
  9526.         sysreferences r
  9527.     where    
  9528.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  9529.     and    o1.name = @pk_table_name
  9530.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  9531.     and    o1.id = r.rkeyid
  9532.     and    o1.id = c1.id
  9533.     and    c1.colid = r.rkey2
  9534.     and    r.keycnt >= 2
  9535.     and     r.fkeyid = o2.id
  9536.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  9537.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  9538.     and    o2.id = c2.id    
  9539.     and    c2.colid = r.fkey2
  9540.     union all
  9541.     select
  9542.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  9543.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  9544.         PK_TABLE_NAME         = o1.name,
  9545.         PK_COLUMN_NAME         = c1.name,
  9546.         PK_COLUMN_GUID        = convert(binary(16),null),
  9547.         PK_COLUMN_PROPID    = convert(int,null),
  9548.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  9549.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  9550.         FK_TABLE_NAME         = o2.name,
  9551.         FK_COLUMN_NAME         = c2.name,
  9552.         FK_COLUMN_GUID        = convert(binary(16),null),
  9553.         FK_COLUMN_PROPID    = convert(int,null),
  9554.         ORDINAL            = convert(int,3),
  9555.         UPDATE_RULE        = 'NO ACTION',
  9556.         DELETE_RULE         = 'NO ACTION'        
  9557.     from
  9558.         sysobjects o1, sysobjects o2,
  9559.         syscolumns c1, syscolumns c2,
  9560.         sysreferences r
  9561.     where    
  9562.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  9563.     and    o1.name = @pk_table_name
  9564.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  9565.     and    o1.id = r.rkeyid
  9566.     and    o1.id = c1.id
  9567.     and    c1.colid = r.rkey3
  9568.     and    r.keycnt >= 3
  9569.     and     r.fkeyid = o2.id
  9570.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  9571.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  9572.     and    o2.id = c2.id    
  9573.     and    c2.colid = r.fkey3
  9574.     union all
  9575.     select
  9576.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  9577.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  9578.         PK_TABLE_NAME         = o1.name,
  9579.         PK_COLUMN_NAME         = c1.name,
  9580.         PK_COLUMN_GUID        = convert(binary(16),null),
  9581.         PK_COLUMN_PROPID    = convert(int,null),
  9582.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  9583.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  9584.         FK_TABLE_NAME         = o2.name,
  9585.         FK_COLUMN_NAME         = c2.name,
  9586.         FK_COLUMN_GUID        = convert(binary(16),null),
  9587.         FK_COLUMN_PROPID    = convert(int,null),
  9588.         ORDINAL            = convert(int,4),
  9589.         UPDATE_RULE        = 'NO ACTION',
  9590.         DELETE_RULE         = 'NO ACTION'        
  9591.     from
  9592.         sysobjects o1, sysobjects o2,
  9593.         syscolumns c1, syscolumns c2,
  9594.         sysreferences r
  9595.     where    
  9596.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  9597.     and    o1.name = @pk_table_name
  9598.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  9599.     and    o1.id = r.rkeyid
  9600.     and    r.keycnt >= 4
  9601.     and    o1.id = c1.id
  9602.     and    c1.colid = r.rkey4
  9603.     and     r.fkeyid = o2.id
  9604.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  9605.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  9606.     and    o2.id = c2.id    
  9607.     and    c2.colid = r.fkey4
  9608.     union all
  9609.     select
  9610.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  9611.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  9612.         PK_TABLE_NAME         = o1.name,
  9613.         PK_COLUMN_NAME         = c1.name,
  9614.         PK_COLUMN_GUID        = convert(binary(16),null),
  9615.         PK_COLUMN_PROPID    = convert(int,null),
  9616.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  9617.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  9618.         FK_TABLE_NAME         = o2.name,
  9619.         FK_COLUMN_NAME         = c2.name,
  9620.         FK_COLUMN_GUID        = convert(binary(16),null),
  9621.         FK_COLUMN_PROPID    = convert(int,null),
  9622.         ORDINAL            = convert(int,5),
  9623.         UPDATE_RULE        = 'NO ACTION',
  9624.         DELETE_RULE         = 'NO ACTION'        
  9625.     from
  9626.         sysobjects o1, sysobjects o2,
  9627.         syscolumns c1, syscolumns c2,
  9628.         sysreferences r
  9629.     where    
  9630.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  9631.     and    o1.name = @pk_table_name
  9632.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  9633.     and    o1.id = r.rkeyid
  9634.     and    r.keycnt >= 5
  9635.     and    o1.id = c1.id
  9636.     and    c1.colid = r.rkey5
  9637.     and     r.fkeyid = o2.id
  9638.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  9639.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  9640.     and    o2.id = c2.id    
  9641.     and    c2.colid = r.fkey5
  9642.     union all
  9643.     select
  9644.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  9645.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  9646.         PK_TABLE_NAME         = o1.name,
  9647.         PK_COLUMN_NAME         = c1.name,
  9648.         PK_COLUMN_GUID        = convert(binary(16),null),
  9649.         PK_COLUMN_PROPID    = convert(int,null),
  9650.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  9651.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  9652.         FK_TABLE_NAME         = o2.name,
  9653.         FK_COLUMN_NAME         = c2.name,
  9654.         FK_COLUMN_GUID        = convert(binary(16),null),
  9655.         FK_COLUMN_PROPID    = convert(int,null),
  9656.         ORDINAL            = convert(int,6),
  9657.         UPDATE_RULE        = 'NO ACTION',
  9658.         DELETE_RULE         = 'NO ACTION'        
  9659.     from
  9660.         sysobjects o1, sysobjects o2,
  9661.         syscolumns c1, syscolumns c2,
  9662.         sysreferences r
  9663.     where    
  9664.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  9665.     and    o1.name = @pk_table_name
  9666.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  9667.     and    o1.id = r.rkeyid
  9668.     and    r.keycnt >= 6
  9669.     and    o1.id = c1.id
  9670.     and    c1.colid = r.rkey6
  9671.     and     r.fkeyid = o2.id
  9672.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  9673.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  9674.     and    o2.id = c2.id    
  9675.     and    c2.colid = r.fkey6
  9676.     union all
  9677.     select
  9678.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  9679.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  9680.         PK_TABLE_NAME         = o1.name,
  9681.         PK_COLUMN_NAME         = c1.name,
  9682.         PK_COLUMN_GUID        = convert(binary(16),null),
  9683.         PK_COLUMN_PROPID    = convert(int,null),
  9684.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  9685.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  9686.         FK_TABLE_NAME         = o2.name,
  9687.         FK_COLUMN_NAME         = c2.name,
  9688.         FK_COLUMN_GUID        = convert(binary(16),null),
  9689.         FK_COLUMN_PROPID    = convert(int,null),
  9690.         ORDINAL            = convert(int,7),
  9691.         UPDATE_RULE        = 'NO ACTION',
  9692.         DELETE_RULE         = 'NO ACTION'        
  9693.     from
  9694.         sysobjects o1, sysobjects o2,
  9695.         syscolumns c1, syscolumns c2,
  9696.         sysreferences r
  9697.     where    
  9698.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  9699.     and    o1.name = @pk_table_name
  9700.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  9701.     and    o1.id = r.rkeyid
  9702.     and    r.keycnt >= 7
  9703.     and    o1.id = c1.id
  9704.     and    c1.colid = r.rkey7
  9705.     and     r.fkeyid = o2.id
  9706.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  9707.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  9708.     and    o2.id = c2.id    
  9709.     and    c2.colid = r.fkey7
  9710.     union all
  9711.     select
  9712.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  9713.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  9714.         PK_TABLE_NAME         = o1.name,
  9715.         PK_COLUMN_NAME         = c1.name,
  9716.         PK_COLUMN_GUID        = convert(binary(16),null),
  9717.         PK_COLUMN_PROPID    = convert(int,null),
  9718.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  9719.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  9720.         FK_TABLE_NAME         = o2.name,
  9721.         FK_COLUMN_NAME         = c2.name,
  9722.         FK_COLUMN_GUID        = convert(binary(16),null),
  9723.         FK_COLUMN_PROPID    = convert(int,null),
  9724.         ORDINAL            = convert(int,8),
  9725.         UPDATE_RULE        = 'NO ACTION',
  9726.         DELETE_RULE         = 'NO ACTION'        
  9727.     from
  9728.         sysobjects o1, sysobjects o2,
  9729.         syscolumns c1, syscolumns c2,
  9730.         sysreferences r
  9731.     where    
  9732.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  9733.     and    o1.name = @pk_table_name
  9734.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  9735.     and    o1.id = r.rkeyid
  9736.     and    r.keycnt >= 8
  9737.     and    o1.id = c1.id
  9738.     and    c1.colid = r.rkey8
  9739.     and     r.fkeyid = o2.id
  9740.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  9741.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  9742.     and    o2.id = c2.id    
  9743.     and    c2.colid = r.fkey8
  9744.     union all
  9745.     select
  9746.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  9747.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  9748.         PK_TABLE_NAME         = o1.name,
  9749.         PK_COLUMN_NAME         = c1.name,
  9750.         PK_COLUMN_GUID        = convert(binary(16),null),
  9751.         PK_COLUMN_PROPID    = convert(int,null),
  9752.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  9753.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  9754.         FK_TABLE_NAME         = o2.name,
  9755.         FK_COLUMN_NAME         = c2.name,
  9756.         FK_COLUMN_GUID        = convert(binary(16),null),
  9757.         FK_COLUMN_PROPID    = convert(int,null),
  9758.         ORDINAL            = convert(int,9),
  9759.         UPDATE_RULE        = 'NO ACTION',
  9760.         DELETE_RULE         = 'NO ACTION'        
  9761.     from
  9762.         sysobjects o1, sysobjects o2,
  9763.         syscolumns c1, syscolumns c2,
  9764.         sysreferences r
  9765.     where    
  9766.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  9767.     and    o1.name = @pk_table_name
  9768.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  9769.     and    o1.id = r.rkeyid
  9770.     and    r.keycnt >= 9
  9771.     and    o1.id = c1.id
  9772.     and    c1.colid = r.rkey9
  9773.     and     r.fkeyid = o2.id
  9774.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  9775.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  9776.     and    o2.id = c2.id    
  9777.     and    c2.colid = r.fkey9
  9778.     union all
  9779.     select
  9780.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  9781.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  9782.         PK_TABLE_NAME         = o1.name,
  9783.         PK_COLUMN_NAME         = c1.name,
  9784.         PK_COLUMN_GUID        = convert(binary(16),null),
  9785.         PK_COLUMN_PROPID    = convert(int,null),
  9786.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  9787.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  9788.         FK_TABLE_NAME         = o2.name,
  9789.         FK_COLUMN_NAME         = c2.name,
  9790.         FK_COLUMN_GUID        = convert(binary(16),null),
  9791.         FK_COLUMN_PROPID    = convert(int,null),
  9792.         ORDINAL            = convert(int,10),
  9793.         UPDATE_RULE        = 'NO ACTION',
  9794.         DELETE_RULE         = 'NO ACTION'        
  9795.     from
  9796.         sysobjects o1, sysobjects o2,
  9797.         syscolumns c1, syscolumns c2,
  9798.         sysreferences r
  9799.     where    
  9800.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  9801.     and    o1.name = @pk_table_name
  9802.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  9803.     and    o1.id = r.rkeyid
  9804.     and    r.keycnt >= 10
  9805.     and    o1.id = c1.id
  9806.     and    c1.colid = r.rkey10
  9807.     and     r.fkeyid = o2.id
  9808.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  9809.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  9810.     and    o2.id = c2.id    
  9811.     and    c2.colid = r.fkey10
  9812.     union all
  9813.     select
  9814.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  9815.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  9816.         PK_TABLE_NAME         = o1.name,
  9817.         PK_COLUMN_NAME         = c1.name,
  9818.         PK_COLUMN_GUID        = convert(binary(16),null),
  9819.         PK_COLUMN_PROPID    = convert(int,null),
  9820.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  9821.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  9822.         FK_TABLE_NAME         = o2.name,
  9823.         FK_COLUMN_NAME         = c2.name,
  9824.         FK_COLUMN_GUID        = convert(binary(16),null),
  9825.         FK_COLUMN_PROPID    = convert(int,null),
  9826.         ORDINAL            = convert(int,11),
  9827.         UPDATE_RULE        = 'NO ACTION',
  9828.         DELETE_RULE         = 'NO ACTION'        
  9829.     from
  9830.         sysobjects o1, sysobjects o2,
  9831.         syscolumns c1, syscolumns c2,
  9832.         sysreferences r
  9833.     where    
  9834.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  9835.     and    o1.name = @pk_table_name
  9836.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  9837.     and    o1.id = r.rkeyid
  9838.     and    r.keycnt >= 11
  9839.     and    o1.id = c1.id
  9840.     and    c1.colid = r.rkey11
  9841.     and     r.fkeyid = o2.id
  9842.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  9843.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  9844.     and    o2.id = c2.id    
  9845.     and    c2.colid = r.fkey11
  9846.     union all
  9847.     select
  9848.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  9849.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  9850.         PK_TABLE_NAME         = o1.name,
  9851.         PK_COLUMN_NAME         = c1.name,
  9852.         PK_COLUMN_GUID        = convert(binary(16),null),
  9853.         PK_COLUMN_PROPID    = convert(int,null),
  9854.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  9855.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  9856.         FK_TABLE_NAME         = o2.name,
  9857.         FK_COLUMN_NAME         = c2.name,
  9858.         FK_COLUMN_GUID        = convert(binary(16),null),
  9859.         FK_COLUMN_PROPID    = convert(int,null),
  9860.         ORDINAL            = convert(int,12),
  9861.         UPDATE_RULE        = 'NO ACTION',
  9862.         DELETE_RULE         = 'NO ACTION'        
  9863.     from
  9864.         sysobjects o1, sysobjects o2,
  9865.         syscolumns c1, syscolumns c2,
  9866.         sysreferences r
  9867.     where    
  9868.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  9869.     and    o1.name = @pk_table_name
  9870.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  9871.     and    o1.id = r.rkeyid
  9872.     and    r.keycnt >= 12
  9873.     and    o1.id = c1.id
  9874.     and    c1.colid = r.rkey12
  9875.     and     r.fkeyid = o2.id
  9876.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  9877.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  9878.     and    o2.id = c2.id    
  9879.     and    c2.colid = r.fkey12
  9880.     union all
  9881.     select
  9882.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  9883.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  9884.         PK_TABLE_NAME         = o1.name,
  9885.         PK_COLUMN_NAME         = c1.name,
  9886.         PK_COLUMN_GUID        = convert(binary(16),null),
  9887.         PK_COLUMN_PROPID    = convert(int,null),
  9888.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  9889.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  9890.         FK_TABLE_NAME         = o2.name,
  9891.         FK_COLUMN_NAME         = c2.name,
  9892.         FK_COLUMN_GUID        = convert(binary(16),null),
  9893.         FK_COLUMN_PROPID    = convert(int,null),
  9894.         ORDINAL            = convert(int,13),
  9895.         UPDATE_RULE        = 'NO ACTION',
  9896.         DELETE_RULE         = 'NO ACTION'        
  9897.     from
  9898.         sysobjects o1, sysobjects o2,
  9899.         syscolumns c1, syscolumns c2,
  9900.         sysreferences r
  9901.     where    
  9902.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  9903.     and    o1.name = @pk_table_name
  9904.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  9905.     and    o1.id = r.rkeyid
  9906.     and    r.keycnt >= 13
  9907.     and    o1.id = c1.id
  9908.     and    c1.colid = r.rkey13
  9909.     and     r.fkeyid = o2.id
  9910.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  9911.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  9912.     and    o2.id = c2.id    
  9913.     and    c2.colid = r.fkey13
  9914.     union all
  9915.     select
  9916.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  9917.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  9918.         PK_TABLE_NAME         = o1.name,
  9919.         PK_COLUMN_NAME         = c1.name,
  9920.         PK_COLUMN_GUID        = convert(binary(16),null),
  9921.         PK_COLUMN_PROPID    = convert(int,null),
  9922.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  9923.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  9924.         FK_TABLE_NAME         = o2.name,
  9925.         FK_COLUMN_NAME         = c2.name,
  9926.         FK_COLUMN_GUID        = convert(binary(16),null),
  9927.         FK_COLUMN_PROPID    = convert(int,null),
  9928.         ORDINAL            = convert(int,14),
  9929.         UPDATE_RULE        = 'NO ACTION',
  9930.         DELETE_RULE         = 'NO ACTION'        
  9931.     from
  9932.         sysobjects o1, sysobjects o2,
  9933.         syscolumns c1, syscolumns c2,
  9934.         sysreferences r
  9935.     where    
  9936.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  9937.     and    o1.name = @pk_table_name
  9938.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  9939.     and    o1.id = r.rkeyid
  9940.     and    r.keycnt >= 14
  9941.     and    o1.id = c1.id
  9942.     and    c1.colid = r.rkey14
  9943.     and     r.fkeyid = o2.id
  9944.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  9945.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  9946.     and    o2.id = c2.id    
  9947.     and    c2.colid = r.fkey14
  9948.     union all
  9949.     select
  9950.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  9951.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  9952.         PK_TABLE_NAME         = o1.name,
  9953.         PK_COLUMN_NAME         = c1.name,
  9954.         PK_COLUMN_GUID        = convert(binary(16),null),
  9955.         PK_COLUMN_PROPID    = convert(int,null),
  9956.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  9957.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  9958.         FK_TABLE_NAME         = o2.name,
  9959.         FK_COLUMN_NAME         = c2.name,
  9960.         FK_COLUMN_GUID        = convert(binary(16),null),
  9961.         FK_COLUMN_PROPID    = convert(int,null),
  9962.         ORDINAL            = convert(int,15),
  9963.         UPDATE_RULE        = 'NO ACTION',
  9964.         DELETE_RULE         = 'NO ACTION'        
  9965.     from
  9966.         sysobjects o1, sysobjects o2,
  9967.         syscolumns c1, syscolumns c2,
  9968.         sysreferences r
  9969.     where    
  9970.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  9971.     and    o1.name = @pk_table_name
  9972.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  9973.     and    o1.id = r.rkeyid
  9974.     and    r.keycnt >= 15
  9975.     and    o1.id = c1.id
  9976.     and    c1.colid = r.rkey15
  9977.     and     r.fkeyid = o2.id
  9978.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  9979.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  9980.     and    o2.id = c2.id    
  9981.     and    c2.colid = r.fkey15
  9982.     union all
  9983.     select
  9984.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  9985.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  9986.         PK_TABLE_NAME         = o1.name,
  9987.         PK_COLUMN_NAME         = c1.name,
  9988.         PK_COLUMN_GUID        = convert(binary(16),null),
  9989.         PK_COLUMN_PROPID    = convert(int,null),
  9990.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  9991.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  9992.         FK_TABLE_NAME         = o2.name,
  9993.         FK_COLUMN_NAME         = c2.name,
  9994.         FK_COLUMN_GUID        = convert(binary(16),null),
  9995.         FK_COLUMN_PROPID    = convert(int,null),
  9996.         ORDINAL            = convert(int,16),
  9997.         UPDATE_RULE        = 'NO ACTION',
  9998.         DELETE_RULE         = 'NO ACTION'        
  9999.     from
  10000.         sysobjects o1, sysobjects o2,
  10001.         syscolumns c1, syscolumns c2,
  10002.         sysreferences r
  10003.     where    
  10004.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  10005.     and    o1.name = @pk_table_name
  10006.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10007.     and    o1.id = r.rkeyid
  10008.     and    r.keycnt >= 16
  10009.     and    o1.id = c1.id
  10010.     and    c1.colid = r.rkey16
  10011.     and     r.fkeyid = o2.id
  10012.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  10013.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10014.     and    o2.id = c2.id    
  10015.     and    c2.colid = r.fkey16
  10016.     order by 8,9,2,3,13
  10017.     END
  10018. ELSE IF @fk_table_name is not null
  10019.     BEGIN
  10020.     select
  10021.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10022.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10023.         PK_TABLE_NAME         = o1.name,
  10024.         PK_COLUMN_NAME         = c1.name,
  10025.         PK_COLUMN_GUID        = convert(binary(16),null),
  10026.         PK_COLUMN_PROPID    = convert(int,null),
  10027.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10028.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  10029.         FK_TABLE_NAME         = o2.name,
  10030.         FK_COLUMN_NAME         = c2.name,
  10031.         FK_COLUMN_GUID        = convert(binary(16),null),
  10032.         FK_COLUMN_PROPID    = convert(int,null),
  10033.         ORDINAL            = convert(int,1),
  10034.         UPDATE_RULE        = 'NO ACTION',
  10035.         DELETE_RULE         = 'NO ACTION'        
  10036.     from
  10037.         sysobjects o1, sysobjects o2,
  10038.         syscolumns c1, syscolumns c2,
  10039.         sysreferences r
  10040.     where    
  10041.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  10042.     and    o2.name = @fk_table_name
  10043.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10044.     and    o2.id = r.fkeyid
  10045.     and    o2.id = c2.id
  10046.     and    c2.colid = r.fkey1
  10047.     and     r.rkeyid = o1.id
  10048.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  10049.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10050.     and    o1.id = c1.id    
  10051.     and    c1.colid = r.rkey1
  10052.     union    all
  10053.     select
  10054.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10055.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10056.         PK_TABLE_NAME         = o1.name,
  10057.         PK_COLUMN_NAME         = c1.name,
  10058.         PK_COLUMN_GUID        = convert(binary(16),null),
  10059.         PK_COLUMN_PROPID    = convert(int,null),
  10060.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10061.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  10062.         FK_TABLE_NAME         = o2.name,
  10063.         FK_COLUMN_NAME         = c2.name,
  10064.         FK_COLUMN_GUID        = convert(binary(16),null),
  10065.         FK_COLUMN_PROPID    = convert(int,null),
  10066.         ORDINAL            = convert(int,2),
  10067.         UPDATE_RULE        = 'NO ACTION',
  10068.         DELETE_RULE         = 'NO ACTION'        
  10069.     from
  10070.         sysobjects o1, sysobjects o2,
  10071.         syscolumns c1, syscolumns c2,
  10072.         sysreferences r
  10073.     where    
  10074.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  10075.     and    o2.name = @fk_table_name
  10076.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10077.     and    o2.id = r.fkeyid
  10078.     and    r.keycnt >= 2
  10079.     and    o2.id = c2.id
  10080.     and    c2.colid = r.fkey2
  10081.     and     r.rkeyid = o1.id
  10082.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  10083.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10084.     and    o1.id = c1.id    
  10085.     and    c1.colid = r.rkey2
  10086.     union    all
  10087.     select
  10088.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10089.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10090.         PK_TABLE_NAME         = o1.name,
  10091.         PK_COLUMN_NAME         = c1.name,
  10092.         PK_COLUMN_GUID        = convert(binary(16),null),
  10093.         PK_COLUMN_PROPID    = convert(int,null),
  10094.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10095.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  10096.         FK_TABLE_NAME         = o2.name,
  10097.         FK_COLUMN_NAME         = c2.name,
  10098.         FK_COLUMN_GUID        = convert(binary(16),null),
  10099.         FK_COLUMN_PROPID    = convert(int,null),
  10100.         ORDINAL            = convert(int,3),
  10101.         UPDATE_RULE        = 'NO ACTION',
  10102.         DELETE_RULE         = 'NO ACTION'        
  10103.     from
  10104.         sysobjects o1, sysobjects o2,
  10105.         syscolumns c1, syscolumns c2,
  10106.         sysreferences r
  10107.     where    
  10108.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  10109.     and    o2.name = @fk_table_name
  10110.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10111.     and    o2.id = r.fkeyid
  10112.     and    r.keycnt >= 3
  10113.     and    o2.id = c2.id
  10114.     and    c2.colid = r.fkey3
  10115.     and     r.rkeyid = o1.id
  10116.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  10117.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10118.     and    o1.id = c1.id    
  10119.     and    c1.colid = r.rkey3
  10120.     union    all
  10121.     select
  10122.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10123.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10124.         PK_TABLE_NAME         = o1.name,
  10125.         PK_COLUMN_NAME         = c1.name,
  10126.         PK_COLUMN_GUID        = convert(binary(16),null),
  10127.         PK_COLUMN_PROPID    = convert(int,null),
  10128.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10129.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  10130.         FK_TABLE_NAME         = o2.name,
  10131.         FK_COLUMN_NAME         = c2.name,
  10132.         FK_COLUMN_GUID        = convert(binary(16),null),
  10133.         FK_COLUMN_PROPID    = convert(int,null),
  10134.         ORDINAL            = convert(int,4),
  10135.         UPDATE_RULE        = 'NO ACTION',
  10136.         DELETE_RULE         = 'NO ACTION'        
  10137.     from
  10138.         sysobjects o1, sysobjects o2,
  10139.         syscolumns c1, syscolumns c2,
  10140.         sysreferences r
  10141.     where    
  10142.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  10143.     and    o2.name = @fk_table_name
  10144.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10145.     and    o2.id = r.fkeyid
  10146.     and    r.keycnt >= 4
  10147.     and    o2.id = c2.id
  10148.     and    c2.colid = r.fkey4
  10149.     and     r.rkeyid = o1.id
  10150.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  10151.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10152.     and    o1.id = c1.id    
  10153.     and    c1.colid = r.rkey4
  10154.     union    all
  10155.     select
  10156.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10157.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10158.         PK_TABLE_NAME         = o1.name,
  10159.         PK_COLUMN_NAME         = c1.name,
  10160.         PK_COLUMN_GUID        = convert(binary(16),null),
  10161.         PK_COLUMN_PROPID    = convert(int,null),
  10162.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10163.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  10164.         FK_TABLE_NAME         = o2.name,
  10165.         FK_COLUMN_NAME         = c2.name,
  10166.         FK_COLUMN_GUID        = convert(binary(16),null),
  10167.         FK_COLUMN_PROPID    = convert(int,null),
  10168.         ORDINAL            = convert(int,5),
  10169.         UPDATE_RULE        = 'NO ACTION',
  10170.         DELETE_RULE         = 'NO ACTION'        
  10171.     from
  10172.         sysobjects o1, sysobjects o2,
  10173.         syscolumns c1, syscolumns c2,
  10174.         sysreferences r
  10175.     where    
  10176.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  10177.     and    o2.name = @fk_table_name
  10178.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10179.     and    o2.id = r.fkeyid
  10180.     and    r.keycnt >= 5
  10181.     and    o2.id = c2.id
  10182.     and    c2.colid = r.fkey5
  10183.     and     r.rkeyid = o1.id
  10184.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  10185.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10186.     and    o1.id = c1.id    
  10187.     and    c1.colid = r.rkey5
  10188.     union    all
  10189.     select
  10190.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10191.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10192.         PK_TABLE_NAME         = o1.name,
  10193.         PK_COLUMN_NAME         = c1.name,
  10194.         PK_COLUMN_GUID        = convert(binary(16),null),
  10195.         PK_COLUMN_PROPID    = convert(int,null),
  10196.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10197.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  10198.         FK_TABLE_NAME         = o2.name,
  10199.         FK_COLUMN_NAME         = c2.name,
  10200.         FK_COLUMN_GUID        = convert(binary(16),null),
  10201.         FK_COLUMN_PROPID    = convert(int,null),
  10202.         ORDINAL            = convert(int,6),
  10203.         UPDATE_RULE        = 'NO ACTION',
  10204.         DELETE_RULE         = 'NO ACTION'        
  10205.     from
  10206.         sysobjects o1, sysobjects o2,
  10207.         syscolumns c1, syscolumns c2,
  10208.         sysreferences r
  10209.     where    
  10210.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  10211.     and    o2.name = @fk_table_name
  10212.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10213.     and    o2.id = r.fkeyid
  10214.     and    r.keycnt >= 6
  10215.     and    o2.id = c2.id
  10216.     and    c2.colid = r.fkey6
  10217.     and     r.rkeyid = o1.id
  10218.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  10219.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10220.     and    o1.id = c1.id    
  10221.     and    c1.colid = r.rkey6
  10222.     union    all
  10223.     select
  10224.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10225.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10226.         PK_TABLE_NAME         = o1.name,
  10227.         PK_COLUMN_NAME         = c1.name,
  10228.         PK_COLUMN_GUID        = convert(binary(16),null),
  10229.         PK_COLUMN_PROPID    = convert(int,null),
  10230.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10231.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  10232.         FK_TABLE_NAME         = o2.name,
  10233.         FK_COLUMN_NAME         = c2.name,
  10234.         FK_COLUMN_GUID        = convert(binary(16),null),
  10235.         FK_COLUMN_PROPID    = convert(int,null),
  10236.         ORDINAL            = convert(int,7),
  10237.         UPDATE_RULE        = 'NO ACTION',
  10238.         DELETE_RULE         = 'NO ACTION'        
  10239.     from
  10240.         sysobjects o1, sysobjects o2,
  10241.         syscolumns c1, syscolumns c2,
  10242.         sysreferences r
  10243.     where    
  10244.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  10245.     and    o2.name = @fk_table_name
  10246.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10247.     and    o2.id = r.fkeyid
  10248.     and    r.keycnt >= 7
  10249.     and    o2.id = c2.id
  10250.     and    c2.colid = r.fkey7
  10251.     and     r.rkeyid = o1.id
  10252.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  10253.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10254.     and    o1.id = c1.id    
  10255.     and    c1.colid = r.rkey7
  10256.     union    all
  10257.     select
  10258.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10259.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10260.         PK_TABLE_NAME         = o1.name,
  10261.         PK_COLUMN_NAME         = c1.name,
  10262.         PK_COLUMN_GUID        = convert(binary(16),null),
  10263.         PK_COLUMN_PROPID    = convert(int,null),
  10264.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10265.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  10266.         FK_TABLE_NAME         = o2.name,
  10267.         FK_COLUMN_NAME         = c2.name,
  10268.         FK_COLUMN_GUID        = convert(binary(16),null),
  10269.         FK_COLUMN_PROPID    = convert(int,null),
  10270.         ORDINAL            = convert(int,8),
  10271.         UPDATE_RULE        = 'NO ACTION',
  10272.         DELETE_RULE         = 'NO ACTION'        
  10273.     from
  10274.         sysobjects o1, sysobjects o2,
  10275.         syscolumns c1, syscolumns c2,
  10276.         sysreferences r
  10277.     where    
  10278.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  10279.     and    o2.name = @fk_table_name
  10280.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10281.     and    o2.id = r.fkeyid
  10282.     and    r.keycnt >= 8
  10283.     and    o2.id = c2.id
  10284.     and    c2.colid = r.fkey8
  10285.     and     r.rkeyid = o1.id
  10286.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  10287.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10288.     and    o1.id = c1.id    
  10289.     and    c1.colid = r.rkey8
  10290.     union    all
  10291.     select
  10292.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10293.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10294.         PK_TABLE_NAME         = o1.name,
  10295.         PK_COLUMN_NAME         = c1.name,
  10296.         PK_COLUMN_GUID        = convert(binary(16),null),
  10297.         PK_COLUMN_PROPID    = convert(int,null),
  10298.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10299.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  10300.         FK_TABLE_NAME         = o2.name,
  10301.         FK_COLUMN_NAME         = c2.name,
  10302.         FK_COLUMN_GUID        = convert(binary(16),null),
  10303.         FK_COLUMN_PROPID    = convert(int,null),
  10304.         ORDINAL            = convert(int,9),
  10305.         UPDATE_RULE        = 'NO ACTION',
  10306.         DELETE_RULE         = 'NO ACTION'        
  10307.     from
  10308.         sysobjects o1, sysobjects o2,
  10309.         syscolumns c1, syscolumns c2,
  10310.         sysreferences r
  10311.     where    
  10312.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  10313.     and    o2.name = @fk_table_name
  10314.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10315.     and    o2.id = r.fkeyid
  10316.     and    r.keycnt >= 9
  10317.     and    o2.id = c2.id
  10318.     and    c2.colid = r.fkey9
  10319.     and     r.rkeyid = o1.id
  10320.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  10321.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10322.     and    o1.id = c1.id    
  10323.     and    c1.colid = r.rkey9
  10324.     union    all
  10325.     select
  10326.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10327.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10328.         PK_TABLE_NAME         = o1.name,
  10329.         PK_COLUMN_NAME         = c1.name,
  10330.         PK_COLUMN_GUID        = convert(binary(16),null),
  10331.         PK_COLUMN_PROPID    = convert(int,null),
  10332.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10333.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  10334.         FK_TABLE_NAME         = o2.name,
  10335.         FK_COLUMN_NAME         = c2.name,
  10336.         FK_COLUMN_GUID        = convert(binary(16),null),
  10337.         FK_COLUMN_PROPID    = convert(int,null),
  10338.         ORDINAL            = convert(int,10),
  10339.         UPDATE_RULE        = 'NO ACTION',
  10340.         DELETE_RULE         = 'NO ACTION'        
  10341.     from
  10342.         sysobjects o1, sysobjects o2,
  10343.         syscolumns c1, syscolumns c2,
  10344.         sysreferences r
  10345.     where    
  10346.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  10347.     and    o2.name = @fk_table_name
  10348.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10349.     and    o2.id = r.fkeyid
  10350.     and    r.keycnt >= 10
  10351.     and    o2.id = c2.id
  10352.     and    c2.colid = r.fkey10
  10353.     and     r.rkeyid = o1.id
  10354.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  10355.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10356.     and    o1.id = c1.id    
  10357.     and    c1.colid = r.rkey10
  10358.     union    all
  10359.     select
  10360.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10361.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10362.         PK_TABLE_NAME         = o1.name,
  10363.         PK_COLUMN_NAME         = c1.name,
  10364.         PK_COLUMN_GUID        = convert(binary(16),null),
  10365.         PK_COLUMN_PROPID    = convert(int,null),
  10366.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10367.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  10368.         FK_TABLE_NAME         = o2.name,
  10369.         FK_COLUMN_NAME         = c2.name,
  10370.         FK_COLUMN_GUID        = convert(binary(16),null),
  10371.         FK_COLUMN_PROPID    = convert(int,null),
  10372.         ORDINAL            = convert(int,11),
  10373.         UPDATE_RULE        = 'NO ACTION',
  10374.         DELETE_RULE         = 'NO ACTION'        
  10375.     from
  10376.         sysobjects o1, sysobjects o2,
  10377.         syscolumns c1, syscolumns c2,
  10378.         sysreferences r
  10379.     where    
  10380.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  10381.     and    o2.name = @fk_table_name
  10382.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10383.     and    o2.id = r.fkeyid
  10384.     and    r.keycnt >= 11
  10385.     and    o2.id = c2.id
  10386.     and    c2.colid = r.fkey11
  10387.     and     r.rkeyid = o1.id
  10388.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  10389.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10390.     and    o1.id = c1.id    
  10391.     and    c1.colid = r.rkey11
  10392.     union    all
  10393.     select
  10394.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10395.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10396.         PK_TABLE_NAME         = o1.name,
  10397.         PK_COLUMN_NAME         = c1.name,
  10398.         PK_COLUMN_GUID        = convert(binary(16),null),
  10399.         PK_COLUMN_PROPID    = convert(int,null),
  10400.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10401.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  10402.         FK_TABLE_NAME         = o2.name,
  10403.         FK_COLUMN_NAME         = c2.name,
  10404.         FK_COLUMN_GUID        = convert(binary(16),null),
  10405.         FK_COLUMN_PROPID    = convert(int,null),
  10406.         ORDINAL            = convert(int,12),
  10407.         UPDATE_RULE        = 'NO ACTION',
  10408.         DELETE_RULE         = 'NO ACTION'        
  10409.     from
  10410.         sysobjects o1, sysobjects o2,
  10411.         syscolumns c1, syscolumns c2,
  10412.         sysreferences r
  10413.     where    
  10414.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  10415.     and    o2.name = @fk_table_name
  10416.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10417.     and    o2.id = r.fkeyid
  10418.     and    r.keycnt >= 12
  10419.     and    o2.id = c2.id
  10420.     and    c2.colid = r.fkey12
  10421.     and     r.rkeyid = o1.id
  10422.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  10423.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10424.     and    o1.id = c1.id    
  10425.     and    c1.colid = r.rkey12
  10426.     union    all
  10427.     select
  10428.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10429.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10430.         PK_TABLE_NAME         = o1.name,
  10431.         PK_COLUMN_NAME         = c1.name,
  10432.         PK_COLUMN_GUID        = convert(binary(16),null),
  10433.         PK_COLUMN_PROPID    = convert(int,null),
  10434.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10435.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  10436.         FK_TABLE_NAME         = o2.name,
  10437.         FK_COLUMN_NAME         = c2.name,
  10438.         FK_COLUMN_GUID        = convert(binary(16),null),
  10439.         FK_COLUMN_PROPID    = convert(int,null),
  10440.         ORDINAL            = convert(int,13),
  10441.         UPDATE_RULE        = 'NO ACTION',
  10442.         DELETE_RULE         = 'NO ACTION'        
  10443.     from
  10444.         sysobjects o1, sysobjects o2,
  10445.         syscolumns c1, syscolumns c2,
  10446.         sysreferences r
  10447.     where    
  10448.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  10449.     and    o2.name = @fk_table_name
  10450.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10451.     and    o2.id = r.fkeyid
  10452.     and    r.keycnt >= 13
  10453.     and    o2.id = c2.id
  10454.     and    c2.colid = r.fkey13
  10455.     and     r.rkeyid = o1.id
  10456.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  10457.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10458.     and    o1.id = c1.id    
  10459.     and    c1.colid = r.rkey13
  10460.     union    all
  10461.     select
  10462.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10463.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10464.         PK_TABLE_NAME         = o1.name,
  10465.         PK_COLUMN_NAME         = c1.name,
  10466.         PK_COLUMN_GUID        = convert(binary(16),null),
  10467.         PK_COLUMN_PROPID    = convert(int,null),
  10468.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10469.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  10470.         FK_TABLE_NAME         = o2.name,
  10471.         FK_COLUMN_NAME         = c2.name,
  10472.         FK_COLUMN_GUID        = convert(binary(16),null),
  10473.         FK_COLUMN_PROPID    = convert(int,null),
  10474.         ORDINAL            = convert(int,14),
  10475.         UPDATE_RULE        = 'NO ACTION',
  10476.         DELETE_RULE         = 'NO ACTION'        
  10477.     from
  10478.         sysobjects o1, sysobjects o2,
  10479.         syscolumns c1, syscolumns c2,
  10480.         sysreferences r
  10481.     where    
  10482.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  10483.     and    o2.name = @fk_table_name
  10484.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10485.     and    o2.id = r.fkeyid
  10486.     and    r.keycnt >= 14
  10487.     and    o2.id = c2.id
  10488.     and    c2.colid = r.fkey14
  10489.     and     r.rkeyid = o1.id
  10490.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  10491.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10492.     and    o1.id = c1.id    
  10493.     and    c1.colid = r.rkey14
  10494.     union    all
  10495.     select
  10496.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10497.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10498.         PK_TABLE_NAME         = o1.name,
  10499.         PK_COLUMN_NAME         = c1.name,
  10500.         PK_COLUMN_GUID        = convert(binary(16),null),
  10501.         PK_COLUMN_PROPID    = convert(int,null),
  10502.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10503.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  10504.         FK_TABLE_NAME         = o2.name,
  10505.         FK_COLUMN_NAME         = c2.name,
  10506.         FK_COLUMN_GUID        = convert(binary(16),null),
  10507.         FK_COLUMN_PROPID    = convert(int,null),
  10508.         ORDINAL            = convert(int,15),
  10509.         UPDATE_RULE        = 'NO ACTION',
  10510.         DELETE_RULE         = 'NO ACTION'        
  10511.     from
  10512.         sysobjects o1, sysobjects o2,
  10513.         syscolumns c1, syscolumns c2,
  10514.         sysreferences r
  10515.     where    
  10516.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  10517.     and    o2.name = @fk_table_name
  10518.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10519.     and    o2.id = r.fkeyid
  10520.     and    r.keycnt >= 15
  10521.     and    o2.id = c2.id
  10522.     and    c2.colid = r.fkey15
  10523.     and     r.rkeyid = o1.id
  10524.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  10525.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10526.     and    o1.id = c1.id    
  10527.     and    c1.colid = r.rkey15
  10528.     union    all
  10529.     select
  10530.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10531.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10532.         PK_TABLE_NAME         = o1.name,
  10533.         PK_COLUMN_NAME         = c1.name,
  10534.         PK_COLUMN_GUID        = convert(binary(16),null),
  10535.         PK_COLUMN_PROPID    = convert(int,null),
  10536.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10537.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  10538.         FK_TABLE_NAME         = o2.name,
  10539.         FK_COLUMN_NAME         = c2.name,
  10540.         FK_COLUMN_GUID        = convert(binary(16),null),
  10541.         FK_COLUMN_PROPID    = convert(int,null),
  10542.         ORDINAL            = convert(int,16),
  10543.         UPDATE_RULE        = 'NO ACTION',
  10544.         DELETE_RULE         = 'NO ACTION'        
  10545.     from
  10546.         sysobjects o1, sysobjects o2,
  10547.         syscolumns c1, syscolumns c2,
  10548.         sysreferences r
  10549.     where    
  10550.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  10551.     and    o2.name = @fk_table_name
  10552.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10553.     and    o2.id = r.fkeyid
  10554.     and    r.keycnt >= 16
  10555.     and    o2.id = c2.id
  10556.     and    c2.colid = r.fkey16
  10557.     and     r.rkeyid = o1.id
  10558.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  10559.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10560.     and    o1.id = c1.id    
  10561.     and    c1.colid = r.rkey16
  10562.     order by 8,9,2,3,13
  10563.     END
  10564. ELSE
  10565.     BEGIN
  10566.     select
  10567.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10568.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10569.         PK_TABLE_NAME         = o1.name,
  10570.         PK_COLUMN_NAME         = c1.name,
  10571.         PK_COLUMN_GUID        = convert(binary(16),null),
  10572.         PK_COLUMN_PROPID    = convert(int,null),
  10573.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10574.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  10575.         FK_TABLE_NAME         = o2.name,
  10576.         FK_COLUMN_NAME         = c2.name,
  10577.         FK_COLUMN_GUID        = convert(binary(16),null),
  10578.         FK_COLUMN_PROPID    = convert(int,null),
  10579.         ORDINAL            = convert(int,1),
  10580.         UPDATE_RULE        = 'NO ACTION',
  10581.         DELETE_RULE         = 'NO ACTION'        
  10582.     from
  10583.         sysobjects o1, sysobjects o2,
  10584.         syscolumns c1, syscolumns c2,
  10585.         sysreferences r
  10586.     where    
  10587.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  10588.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  10589.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10590.     and    o1.id = r.rkeyid
  10591.     and    o1.id = c1.id
  10592.     and    c1.colid = r.rkey1
  10593.     and     r.fkeyid = o2.id
  10594.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10595.     and    o2.id = c2.id    
  10596.     and    c2.colid = r.fkey1
  10597.     union all
  10598.     select
  10599.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10600.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10601.         PK_TABLE_NAME         = o1.name,
  10602.         PK_COLUMN_NAME         = c1.name,
  10603.         PK_COLUMN_GUID        = convert(binary(16),null),
  10604.         PK_COLUMN_PROPID    = convert(int,null),
  10605.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10606.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  10607.         FK_TABLE_NAME         = o2.name,
  10608.         FK_COLUMN_NAME         = c2.name,
  10609.         FK_COLUMN_GUID        = convert(binary(16),null),
  10610.         FK_COLUMN_PROPID    = convert(int,null),
  10611.         ORDINAL            = convert(int,2),
  10612.         UPDATE_RULE        = 'NO ACTION',
  10613.         DELETE_RULE         = 'NO ACTION'        
  10614.     from
  10615.         sysobjects o1, sysobjects o2,
  10616.         syscolumns c1, syscolumns c2,
  10617.         sysreferences r
  10618.     where    
  10619.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  10620.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  10621.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10622.     and    o1.id = r.rkeyid
  10623.     and    r.keycnt >= 2
  10624.     and    o1.id = c1.id
  10625.     and    c1.colid = r.rkey2
  10626.     and     r.fkeyid = o2.id
  10627.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10628.     and    o2.id = c2.id    
  10629.     and    c2.colid = r.fkey2
  10630.     union all
  10631.     select
  10632.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10633.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10634.         PK_TABLE_NAME         = o1.name,
  10635.         PK_COLUMN_NAME         = c1.name,
  10636.         PK_COLUMN_GUID        = convert(binary(16),null),
  10637.         PK_COLUMN_PROPID    = convert(int,null),
  10638.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10639.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  10640.         FK_TABLE_NAME         = o2.name,
  10641.         FK_COLUMN_NAME         = c2.name,
  10642.         FK_COLUMN_GUID        = convert(binary(16),null),
  10643.         FK_COLUMN_PROPID    = convert(int,null),
  10644.         ORDINAL            = convert(int,3),
  10645.         UPDATE_RULE        = 'NO ACTION',
  10646.         DELETE_RULE         = 'NO ACTION'        
  10647.     from
  10648.         sysobjects o1, sysobjects o2,
  10649.         syscolumns c1, syscolumns c2,
  10650.         sysreferences r
  10651.     where    
  10652.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  10653.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  10654.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10655.     and    o1.id = r.rkeyid
  10656.     and    r.keycnt >= 3
  10657.     and    o1.id = c1.id
  10658.     and    c1.colid = r.rkey3
  10659.     and     r.fkeyid = o2.id
  10660.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10661.     and    o2.id = c2.id    
  10662.     and    c2.colid = r.fkey3
  10663.     union all
  10664.     select
  10665.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10666.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10667.         PK_TABLE_NAME         = o1.name,
  10668.         PK_COLUMN_NAME         = c1.name,
  10669.         PK_COLUMN_GUID        = convert(binary(16),null),
  10670.         PK_COLUMN_PROPID    = convert(int,null),
  10671.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10672.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  10673.         FK_TABLE_NAME         = o2.name,
  10674.         FK_COLUMN_NAME         = c2.name,
  10675.         FK_COLUMN_GUID        = convert(binary(16),null),
  10676.         FK_COLUMN_PROPID    = convert(int,null),
  10677.         ORDINAL            = convert(int,4),
  10678.         UPDATE_RULE        = 'NO ACTION',
  10679.         DELETE_RULE         = 'NO ACTION'        
  10680.     from
  10681.         sysobjects o1, sysobjects o2,
  10682.         syscolumns c1, syscolumns c2,
  10683.         sysreferences r
  10684.     where    
  10685.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  10686.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  10687.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10688.     and    o1.id = r.rkeyid
  10689.     and    r.keycnt >= 4
  10690.     and    o1.id = c1.id
  10691.     and    c1.colid = r.rkey4
  10692.     and     r.fkeyid = o2.id
  10693.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10694.     and    o2.id = c2.id    
  10695.     and    c2.colid = r.fkey4
  10696.     union all
  10697.     select
  10698.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10699.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10700.         PK_TABLE_NAME         = o1.name,
  10701.         PK_COLUMN_NAME         = c1.name,
  10702.         PK_COLUMN_GUID        = convert(binary(16),null),
  10703.         PK_COLUMN_PROPID    = convert(int,null),
  10704.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10705.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  10706.         FK_TABLE_NAME         = o2.name,
  10707.         FK_COLUMN_NAME         = c2.name,
  10708.         FK_COLUMN_GUID        = convert(binary(16),null),
  10709.         FK_COLUMN_PROPID    = convert(int,null),
  10710.         ORDINAL            = convert(int,5),
  10711.         UPDATE_RULE        = 'NO ACTION',
  10712.         DELETE_RULE         = 'NO ACTION'        
  10713.     from
  10714.         sysobjects o1, sysobjects o2,
  10715.         syscolumns c1, syscolumns c2,
  10716.         sysreferences r
  10717.     where    
  10718.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  10719.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  10720.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10721.     and    o1.id = r.rkeyid
  10722.     and    r.keycnt >= 5
  10723.     and    o1.id = c1.id
  10724.     and    c1.colid = r.rkey5
  10725.     and     r.fkeyid = o2.id
  10726.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10727.     and    o2.id = c2.id    
  10728.     and    c2.colid = r.fkey5
  10729.     union all
  10730.     select
  10731.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10732.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10733.         PK_TABLE_NAME         = o1.name,
  10734.         PK_COLUMN_NAME         = c1.name,
  10735.         PK_COLUMN_GUID        = convert(binary(16),null),
  10736.         PK_COLUMN_PROPID    = convert(int,null),
  10737.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10738.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  10739.         FK_TABLE_NAME         = o2.name,
  10740.         FK_COLUMN_NAME         = c2.name,
  10741.         FK_COLUMN_GUID        = convert(binary(16),null),
  10742.         FK_COLUMN_PROPID    = convert(int,null),
  10743.         ORDINAL            = convert(int,6),
  10744.         UPDATE_RULE        = 'NO ACTION',
  10745.         DELETE_RULE         = 'NO ACTION'        
  10746.     from
  10747.         sysobjects o1, sysobjects o2,
  10748.         syscolumns c1, syscolumns c2,
  10749.         sysreferences r
  10750.     where    
  10751.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  10752.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  10753.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10754.     and    o1.id = r.rkeyid
  10755.     and    r.keycnt >= 6
  10756.     and    o1.id = c1.id
  10757.     and    c1.colid = r.rkey6
  10758.     and     r.fkeyid = o2.id
  10759.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10760.     and    o2.id = c2.id    
  10761.     and    c2.colid = r.fkey6
  10762.     union all
  10763.     select
  10764.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10765.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10766.         PK_TABLE_NAME         = o1.name,
  10767.         PK_COLUMN_NAME         = c1.name,
  10768.         PK_COLUMN_GUID        = convert(binary(16),null),
  10769.         PK_COLUMN_PROPID    = convert(int,null),
  10770.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10771.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  10772.         FK_TABLE_NAME         = o2.name,
  10773.         FK_COLUMN_NAME         = c2.name,
  10774.         FK_COLUMN_GUID        = convert(binary(16),null),
  10775.         FK_COLUMN_PROPID    = convert(int,null),
  10776.         ORDINAL            = convert(int,7),
  10777.         UPDATE_RULE        = 'NO ACTION',
  10778.         DELETE_RULE         = 'NO ACTION'        
  10779.     from
  10780.         sysobjects o1, sysobjects o2,
  10781.         syscolumns c1, syscolumns c2,
  10782.         sysreferences r
  10783.     where    
  10784.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  10785.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  10786.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10787.     and    o1.id = r.rkeyid
  10788.     and    r.keycnt >= 7
  10789.     and    o1.id = c1.id
  10790.     and    c1.colid = r.rkey7
  10791.     and     r.fkeyid = o2.id
  10792.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10793.     and    o2.id = c2.id    
  10794.     and    c2.colid = r.fkey7
  10795.     union all
  10796.     select
  10797.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10798.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10799.         PK_TABLE_NAME         = o1.name,
  10800.         PK_COLUMN_NAME         = c1.name,
  10801.         PK_COLUMN_GUID        = convert(binary(16),null),
  10802.         PK_COLUMN_PROPID    = convert(int,null),
  10803.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10804.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  10805.         FK_TABLE_NAME         = o2.name,
  10806.         FK_COLUMN_NAME         = c2.name,
  10807.         FK_COLUMN_GUID        = convert(binary(16),null),
  10808.         FK_COLUMN_PROPID    = convert(int,null),
  10809.         ORDINAL            = convert(int,8),
  10810.         UPDATE_RULE        = 'NO ACTION',
  10811.         DELETE_RULE         = 'NO ACTION'        
  10812.     from
  10813.         sysobjects o1, sysobjects o2,
  10814.         syscolumns c1, syscolumns c2,
  10815.         sysreferences r
  10816.     where    
  10817.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  10818.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  10819.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10820.     and    o1.id = r.rkeyid
  10821.     and    r.keycnt >= 8
  10822.     and    o1.id = c1.id
  10823.     and    c1.colid = r.rkey8
  10824.     and     r.fkeyid = o2.id
  10825.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10826.     and    o2.id = c2.id    
  10827.     and    c2.colid = r.fkey8
  10828.     union all
  10829.     select
  10830.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10831.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10832.         PK_TABLE_NAME         = o1.name,
  10833.         PK_COLUMN_NAME         = c1.name,
  10834.         PK_COLUMN_GUID        = convert(binary(16),null),
  10835.         PK_COLUMN_PROPID    = convert(int,null),
  10836.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10837.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  10838.         FK_TABLE_NAME         = o2.name,
  10839.         FK_COLUMN_NAME         = c2.name,
  10840.         FK_COLUMN_GUID        = convert(binary(16),null),
  10841.         FK_COLUMN_PROPID    = convert(int,null),
  10842.         ORDINAL            = convert(int,9),
  10843.         UPDATE_RULE        = 'NO ACTION',
  10844.         DELETE_RULE         = 'NO ACTION'        
  10845.     from
  10846.         sysobjects o1, sysobjects o2,
  10847.         syscolumns c1, syscolumns c2,
  10848.         sysreferences r
  10849.     where    
  10850.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  10851.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  10852.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10853.     and    o1.id = r.rkeyid
  10854.     and    r.keycnt >= 9
  10855.     and    o1.id = c1.id
  10856.     and    c1.colid = r.rkey9
  10857.     and     r.fkeyid = o2.id
  10858.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10859.     and    o2.id = c2.id    
  10860.     and    c2.colid = r.fkey9
  10861.     union all
  10862.     select
  10863.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10864.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10865.         PK_TABLE_NAME         = o1.name,
  10866.         PK_COLUMN_NAME         = c1.name,
  10867.         PK_COLUMN_GUID        = convert(binary(16),null),
  10868.         PK_COLUMN_PROPID    = convert(int,null),
  10869.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10870.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  10871.         FK_TABLE_NAME         = o2.name,
  10872.         FK_COLUMN_NAME         = c2.name,
  10873.         FK_COLUMN_GUID        = convert(binary(16),null),
  10874.         FK_COLUMN_PROPID    = convert(int,null),
  10875.         ORDINAL            = convert(int,10),
  10876.         UPDATE_RULE        = 'NO ACTION',
  10877.         DELETE_RULE         = 'NO ACTION'        
  10878.     from
  10879.         sysobjects o1, sysobjects o2,
  10880.         syscolumns c1, syscolumns c2,
  10881.         sysreferences r
  10882.     where    
  10883.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  10884.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  10885.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10886.     and    o1.id = r.rkeyid
  10887.     and    r.keycnt >= 10
  10888.     and    o1.id = c1.id
  10889.     and    c1.colid = r.rkey10
  10890.     and     r.fkeyid = o2.id
  10891.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10892.     and    o2.id = c2.id    
  10893.     and    c2.colid = r.fkey10
  10894.     union all
  10895.     select
  10896.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10897.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10898.         PK_TABLE_NAME         = o1.name,
  10899.         PK_COLUMN_NAME         = c1.name,
  10900.         PK_COLUMN_GUID        = convert(binary(16),null),
  10901.         PK_COLUMN_PROPID    = convert(int,null),
  10902.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10903.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  10904.         FK_TABLE_NAME         = o2.name,
  10905.         FK_COLUMN_NAME         = c2.name,
  10906.         FK_COLUMN_GUID        = convert(binary(16),null),
  10907.         FK_COLUMN_PROPID    = convert(int,null),
  10908.         ORDINAL            = convert(int,11),
  10909.         UPDATE_RULE        = 'NO ACTION',
  10910.         DELETE_RULE         = 'NO ACTION'        
  10911.     from
  10912.         sysobjects o1, sysobjects o2,
  10913.         syscolumns c1, syscolumns c2,
  10914.         sysreferences r
  10915.     where    
  10916.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  10917.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  10918.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10919.     and    o1.id = r.rkeyid
  10920.     and    r.keycnt >= 11
  10921.     and    o1.id = c1.id
  10922.     and    c1.colid = r.rkey11
  10923.     and     r.fkeyid = o2.id
  10924.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10925.     and    o2.id = c2.id    
  10926.     and    c2.colid = r.fkey11
  10927.     union all
  10928.     select
  10929.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10930.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10931.         PK_TABLE_NAME         = o1.name,
  10932.         PK_COLUMN_NAME         = c1.name,
  10933.         PK_COLUMN_GUID        = convert(binary(16),null),
  10934.         PK_COLUMN_PROPID    = convert(int,null),
  10935.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10936.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  10937.         FK_TABLE_NAME         = o2.name,
  10938.         FK_COLUMN_NAME         = c2.name,
  10939.         FK_COLUMN_GUID        = convert(binary(16),null),
  10940.         FK_COLUMN_PROPID    = convert(int,null),
  10941.         ORDINAL            = convert(int,12),
  10942.         UPDATE_RULE        = 'NO ACTION',
  10943.         DELETE_RULE         = 'NO ACTION'        
  10944.     from
  10945.         sysobjects o1, sysobjects o2,
  10946.         syscolumns c1, syscolumns c2,
  10947.         sysreferences r
  10948.     where    
  10949.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  10950.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  10951.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10952.     and    o1.id = r.rkeyid
  10953.     and    r.keycnt >= 12
  10954.     and    o1.id = c1.id
  10955.     and    c1.colid = r.rkey12
  10956.     and     r.fkeyid = o2.id
  10957.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10958.     and    o2.id = c2.id    
  10959.     and    c2.colid = r.fkey12
  10960.     union all
  10961.     select
  10962.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10963.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10964.         PK_TABLE_NAME         = o1.name,
  10965.         PK_COLUMN_NAME         = c1.name,
  10966.         PK_COLUMN_GUID        = convert(binary(16),null),
  10967.         PK_COLUMN_PROPID    = convert(int,null),
  10968.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10969.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  10970.         FK_TABLE_NAME         = o2.name,
  10971.         FK_COLUMN_NAME         = c2.name,
  10972.         FK_COLUMN_GUID        = convert(binary(16),null),
  10973.         FK_COLUMN_PROPID    = convert(int,null),
  10974.         ORDINAL            = convert(int,13),
  10975.         UPDATE_RULE        = 'NO ACTION',
  10976.         DELETE_RULE         = 'NO ACTION'        
  10977.     from
  10978.         sysobjects o1, sysobjects o2,
  10979.         syscolumns c1, syscolumns c2,
  10980.         sysreferences r
  10981.     where    
  10982.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  10983.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  10984.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10985.     and    o1.id = r.rkeyid
  10986.     and    r.keycnt >= 13
  10987.     and    o1.id = c1.id
  10988.     and    c1.colid = r.rkey13
  10989.     and     r.fkeyid = o2.id
  10990.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10991.     and    o2.id = c2.id    
  10992.     and    c2.colid = r.fkey13
  10993.     union all
  10994.     select
  10995.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10996.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10997.         PK_TABLE_NAME         = o1.name,
  10998.         PK_COLUMN_NAME         = c1.name,
  10999.         PK_COLUMN_GUID        = convert(binary(16),null),
  11000.         PK_COLUMN_PROPID    = convert(int,null),
  11001.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11002.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  11003.         FK_TABLE_NAME         = o2.name,
  11004.         FK_COLUMN_NAME         = c2.name,
  11005.         FK_COLUMN_GUID        = convert(binary(16),null),
  11006.         FK_COLUMN_PROPID    = convert(int,null),
  11007.         ORDINAL            = convert(int,14),
  11008.         UPDATE_RULE        = 'NO ACTION',
  11009.         DELETE_RULE         = 'NO ACTION'        
  11010.     from
  11011.         sysobjects o1, sysobjects o2,
  11012.         syscolumns c1, syscolumns c2,
  11013.         sysreferences r
  11014.     where    
  11015.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  11016.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11017.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11018.     and    o1.id = r.rkeyid
  11019.     and    r.keycnt >= 14
  11020.     and    o1.id = c1.id
  11021.     and    c1.colid = r.rkey14
  11022.     and     r.fkeyid = o2.id
  11023.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11024.     and    o2.id = c2.id    
  11025.     and    c2.colid = r.fkey14
  11026.     union all
  11027.     select
  11028.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11029.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11030.         PK_TABLE_NAME         = o1.name,
  11031.         PK_COLUMN_NAME         = c1.name,
  11032.         PK_COLUMN_GUID        = convert(binary(16),null),
  11033.         PK_COLUMN_PROPID    = convert(int,null),
  11034.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11035.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  11036.         FK_TABLE_NAME         = o2.name,
  11037.         FK_COLUMN_NAME         = c2.name,
  11038.         FK_COLUMN_GUID        = convert(binary(16),null),
  11039.         FK_COLUMN_PROPID    = convert(int,null),
  11040.         ORDINAL            = convert(int,15),
  11041.         UPDATE_RULE        = 'NO ACTION',
  11042.         DELETE_RULE         = 'NO ACTION'        
  11043.     from
  11044.         sysobjects o1, sysobjects o2,
  11045.         syscolumns c1, syscolumns c2,
  11046.         sysreferences r
  11047.     where    
  11048.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  11049.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11050.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11051.     and    o1.id = r.rkeyid
  11052.     and    r.keycnt >= 15
  11053.     and    o1.id = c1.id
  11054.     and    c1.colid = r.rkey15
  11055.     and     r.fkeyid = o2.id
  11056.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11057.     and    o2.id = c2.id    
  11058.     and    c2.colid = r.fkey15
  11059.     union all
  11060.     select
  11061.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11062.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11063.         PK_TABLE_NAME         = o1.name,
  11064.         PK_COLUMN_NAME         = c1.name,
  11065.         PK_COLUMN_GUID        = convert(binary(16),null),
  11066.         PK_COLUMN_PROPID    = convert(int,null),
  11067.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11068.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  11069.         FK_TABLE_NAME         = o2.name,
  11070.         FK_COLUMN_NAME         = c2.name,
  11071.         FK_COLUMN_GUID        = convert(binary(16),null),
  11072.         FK_COLUMN_PROPID    = convert(int,null),
  11073.         ORDINAL            = convert(int,16),
  11074.         UPDATE_RULE        = 'NO ACTION',
  11075.         DELETE_RULE         = 'NO ACTION'        
  11076.     from
  11077.         sysobjects o1, sysobjects o2,
  11078.         syscolumns c1, syscolumns c2,
  11079.         sysreferences r
  11080.     where    
  11081.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  11082.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11083.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11084.     and    o1.id = r.rkeyid
  11085.     and    o1.id = c1.id
  11086.     and    r.keycnt >= 16
  11087.     and    c1.colid = r.rkey16
  11088.     and     r.fkeyid = o2.id
  11089.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11090.     and    o2.id = c2.id    
  11091.     and    c2.colid = r.fkey16
  11092.     order by 8,9,2,3,13
  11093.     END
  11094. go
  11095. dump tran master with no_log
  11096. go
  11097. CREATE PROCEDURE sp_foreign_keys_rowset;2
  11098.     (
  11099.        @handle            int output,
  11100.        @scrollopt        int output,
  11101.     @ccopt            int output,
  11102.     @rows            int output,
  11103.      @pk_table_name        varchar(255) = null,
  11104.     @pk_table_schema    varchar(255) = null,
  11105.     @pk_table_catalog    varchar(255) = null,
  11106.     @fk_table_name        varchar(255) = null,
  11107.     @fk_table_schema    varchar(255) = null,
  11108.     @fk_table_catalog    varchar(255) = null
  11109.     )
  11110. as
  11111.     declare @ret int
  11112.     
  11113.     SET NOCOUNT ON
  11114.  
  11115.     create table #spfkeysrowset1
  11116.         (
  11117.         PK_TABLE_CATALOG    sysname not null,
  11118.         PK_TABLE_SCHEMA        sysname not null,
  11119.         PK_TABLE_NAME         sysname not null,
  11120.         PK_COLUMN_NAME         sysname not null,
  11121.         PK_COLUMN_GUID        binary(16) null,
  11122.         PK_COLUMN_PROPID    int null,
  11123.         FK_TABLE_CATALOG    sysname not null,
  11124.         FK_TABLE_SCHEMA        sysname not null,
  11125.         FK_TABLE_NAME         sysname not null,
  11126.         FK_COLUMN_NAME         sysname not null,
  11127.         FK_COLUMN_GUID        binary(16) null,
  11128.         FK_COLUMN_PROPID    int null,
  11129.         ORDINAL            int not null,
  11130.         UPDATE_RULE        sysname not null,
  11131.         DELETE_RULE         sysname not null        
  11132.         )
  11133.  
  11134. IF @pk_table_name is not null
  11135.     BEGIN
  11136.     insert into #spfkeysrowset1
  11137.     select
  11138.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11139.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11140.         PK_TABLE_NAME         = o1.name,
  11141.         PK_COLUMN_NAME         = c1.name,
  11142.         PK_COLUMN_GUID        = convert(binary(16),null),
  11143.         PK_COLUMN_PROPID    = convert(int,null),
  11144.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11145.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  11146.         FK_TABLE_NAME         = o2.name,
  11147.         FK_COLUMN_NAME         = c2.name,
  11148.         FK_COLUMN_GUID        = convert(binary(16),null),
  11149.         FK_COLUMN_PROPID    = convert(int,null),
  11150.         ORDINAL            = convert(int,1),
  11151.         UPDATE_RULE        = 'NO ACTION',
  11152.         DELETE_RULE         = 'NO ACTION'        
  11153.     from
  11154.         sysobjects o1, sysobjects o2,
  11155.         syscolumns c1, syscolumns c2,
  11156.         sysreferences r
  11157.     where    
  11158.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11159.     and    o1.name = @pk_table_name
  11160.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11161.     and    o1.id = r.rkeyid
  11162.     and    o1.id = c1.id
  11163.     and    c1.colid = r.rkey1
  11164.     and     r.fkeyid = o2.id
  11165.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  11166.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11167.     and    o2.id = c2.id    
  11168.     and    c2.colid = r.fkey1
  11169.     union all
  11170.     select
  11171.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11172.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11173.         PK_TABLE_NAME         = o1.name,
  11174.         PK_COLUMN_NAME         = c1.name,
  11175.         PK_COLUMN_GUID        = convert(binary(16),null),
  11176.         PK_COLUMN_PROPID    = convert(int,null),
  11177.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11178.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  11179.         FK_TABLE_NAME         = o2.name,
  11180.         FK_COLUMN_NAME         = c2.name,
  11181.         FK_COLUMN_GUID        = convert(binary(16),null),
  11182.         FK_COLUMN_PROPID    = convert(int,null),
  11183.         ORDINAL            = convert(int,2),
  11184.         UPDATE_RULE        = 'NO ACTION',
  11185.         DELETE_RULE         = 'NO ACTION'        
  11186.     from
  11187.         sysobjects o1, sysobjects o2,
  11188.         syscolumns c1, syscolumns c2,
  11189.         sysreferences r
  11190.     where    
  11191.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11192.     and    o1.name = @pk_table_name
  11193.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11194.     and    o1.id = r.rkeyid
  11195.     and    o1.id = c1.id
  11196.     and    c1.colid = r.rkey2
  11197.     and    r.keycnt >= 2
  11198.     and     r.fkeyid = o2.id
  11199.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  11200.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11201.     and    o2.id = c2.id    
  11202.     and    c2.colid = r.fkey2
  11203.     union all
  11204.     select
  11205.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11206.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11207.         PK_TABLE_NAME         = o1.name,
  11208.         PK_COLUMN_NAME         = c1.name,
  11209.         PK_COLUMN_GUID        = convert(binary(16),null),
  11210.         PK_COLUMN_PROPID    = convert(int,null),
  11211.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11212.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  11213.         FK_TABLE_NAME         = o2.name,
  11214.         FK_COLUMN_NAME         = c2.name,
  11215.         FK_COLUMN_GUID        = convert(binary(16),null),
  11216.         FK_COLUMN_PROPID    = convert(int,null),
  11217.         ORDINAL            = convert(int,3),
  11218.         UPDATE_RULE        = 'NO ACTION',
  11219.         DELETE_RULE         = 'NO ACTION'        
  11220.     from
  11221.         sysobjects o1, sysobjects o2,
  11222.         syscolumns c1, syscolumns c2,
  11223.         sysreferences r
  11224.     where    
  11225.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11226.     and    o1.name = @pk_table_name
  11227.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11228.     and    o1.id = r.rkeyid
  11229.     and    o1.id = c1.id
  11230.     and    c1.colid = r.rkey3
  11231.     and    r.keycnt >= 3
  11232.     and     r.fkeyid = o2.id
  11233.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  11234.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11235.     and    o2.id = c2.id    
  11236.     and    c2.colid = r.fkey3
  11237.     union all
  11238.     select
  11239.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11240.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11241.         PK_TABLE_NAME         = o1.name,
  11242.         PK_COLUMN_NAME         = c1.name,
  11243.         PK_COLUMN_GUID        = convert(binary(16),null),
  11244.         PK_COLUMN_PROPID    = convert(int,null),
  11245.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11246.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  11247.         FK_TABLE_NAME         = o2.name,
  11248.         FK_COLUMN_NAME         = c2.name,
  11249.         FK_COLUMN_GUID        = convert(binary(16),null),
  11250.         FK_COLUMN_PROPID    = convert(int,null),
  11251.         ORDINAL            = convert(int,4),
  11252.         UPDATE_RULE        = 'NO ACTION',
  11253.         DELETE_RULE         = 'NO ACTION'        
  11254.     from
  11255.         sysobjects o1, sysobjects o2,
  11256.         syscolumns c1, syscolumns c2,
  11257.         sysreferences r
  11258.     where    
  11259.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11260.     and    o1.name = @pk_table_name
  11261.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11262.     and    o1.id = r.rkeyid
  11263.     and    r.keycnt >= 4
  11264.     and    o1.id = c1.id
  11265.     and    c1.colid = r.rkey4
  11266.     and     r.fkeyid = o2.id
  11267.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  11268.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11269.     and    o2.id = c2.id    
  11270.     and    c2.colid = r.fkey4
  11271.     union all
  11272.     select
  11273.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11274.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11275.         PK_TABLE_NAME         = o1.name,
  11276.         PK_COLUMN_NAME         = c1.name,
  11277.         PK_COLUMN_GUID        = convert(binary(16),null),
  11278.         PK_COLUMN_PROPID    = convert(int,null),
  11279.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11280.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  11281.         FK_TABLE_NAME         = o2.name,
  11282.         FK_COLUMN_NAME         = c2.name,
  11283.         FK_COLUMN_GUID        = convert(binary(16),null),
  11284.         FK_COLUMN_PROPID    = convert(int,null),
  11285.         ORDINAL            = convert(int,5),
  11286.         UPDATE_RULE        = 'NO ACTION',
  11287.         DELETE_RULE         = 'NO ACTION'        
  11288.     from
  11289.         sysobjects o1, sysobjects o2,
  11290.         syscolumns c1, syscolumns c2,
  11291.         sysreferences r
  11292.     where    
  11293.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11294.     and    o1.name = @pk_table_name
  11295.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11296.     and    o1.id = r.rkeyid
  11297.     and    r.keycnt >= 5
  11298.     and    o1.id = c1.id
  11299.     and    c1.colid = r.rkey5
  11300.     and     r.fkeyid = o2.id
  11301.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  11302.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11303.     and    o2.id = c2.id    
  11304.     and    c2.colid = r.fkey5
  11305.     union all
  11306.     select
  11307.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11308.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11309.         PK_TABLE_NAME         = o1.name,
  11310.         PK_COLUMN_NAME         = c1.name,
  11311.         PK_COLUMN_GUID        = convert(binary(16),null),
  11312.         PK_COLUMN_PROPID    = convert(int,null),
  11313.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11314.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  11315.         FK_TABLE_NAME         = o2.name,
  11316.         FK_COLUMN_NAME         = c2.name,
  11317.         FK_COLUMN_GUID        = convert(binary(16),null),
  11318.         FK_COLUMN_PROPID    = convert(int,null),
  11319.         ORDINAL            = convert(int,6),
  11320.         UPDATE_RULE        = 'NO ACTION',
  11321.         DELETE_RULE         = 'NO ACTION'        
  11322.     from
  11323.         sysobjects o1, sysobjects o2,
  11324.         syscolumns c1, syscolumns c2,
  11325.         sysreferences r
  11326.     where    
  11327.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11328.     and    o1.name = @pk_table_name
  11329.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11330.     and    o1.id = r.rkeyid
  11331.     and    r.keycnt >= 6
  11332.     and    o1.id = c1.id
  11333.     and    c1.colid = r.rkey6
  11334.     and     r.fkeyid = o2.id
  11335.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  11336.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11337.     and    o2.id = c2.id    
  11338.     and    c2.colid = r.fkey6
  11339.     union all
  11340.     select
  11341.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11342.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11343.         PK_TABLE_NAME         = o1.name,
  11344.         PK_COLUMN_NAME         = c1.name,
  11345.         PK_COLUMN_GUID        = convert(binary(16),null),
  11346.         PK_COLUMN_PROPID    = convert(int,null),
  11347.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11348.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  11349.         FK_TABLE_NAME         = o2.name,
  11350.         FK_COLUMN_NAME         = c2.name,
  11351.         FK_COLUMN_GUID        = convert(binary(16),null),
  11352.         FK_COLUMN_PROPID    = convert(int,null),
  11353.         ORDINAL            = convert(int,7),
  11354.         UPDATE_RULE        = 'NO ACTION',
  11355.         DELETE_RULE         = 'NO ACTION'        
  11356.     from
  11357.         sysobjects o1, sysobjects o2,
  11358.         syscolumns c1, syscolumns c2,
  11359.         sysreferences r
  11360.     where    
  11361.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11362.     and    o1.name = @pk_table_name
  11363.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11364.     and    o1.id = r.rkeyid
  11365.     and    r.keycnt >= 7
  11366.     and    o1.id = c1.id
  11367.     and    c1.colid = r.rkey7
  11368.     and     r.fkeyid = o2.id
  11369.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  11370.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11371.     and    o2.id = c2.id    
  11372.     and    c2.colid = r.fkey7
  11373.     union all
  11374.     select
  11375.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11376.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11377.         PK_TABLE_NAME         = o1.name,
  11378.         PK_COLUMN_NAME         = c1.name,
  11379.         PK_COLUMN_GUID        = convert(binary(16),null),
  11380.         PK_COLUMN_PROPID    = convert(int,null),
  11381.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11382.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  11383.         FK_TABLE_NAME         = o2.name,
  11384.         FK_COLUMN_NAME         = c2.name,
  11385.         FK_COLUMN_GUID        = convert(binary(16),null),
  11386.         FK_COLUMN_PROPID    = convert(int,null),
  11387.         ORDINAL            = convert(int,8),
  11388.         UPDATE_RULE        = 'NO ACTION',
  11389.         DELETE_RULE         = 'NO ACTION'        
  11390.     from
  11391.         sysobjects o1, sysobjects o2,
  11392.         syscolumns c1, syscolumns c2,
  11393.         sysreferences r
  11394.     where    
  11395.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11396.     and    o1.name = @pk_table_name
  11397.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11398.     and    o1.id = r.rkeyid
  11399.     and    r.keycnt >= 8
  11400.     and    o1.id = c1.id
  11401.     and    c1.colid = r.rkey8
  11402.     and     r.fkeyid = o2.id
  11403.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  11404.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11405.     and    o2.id = c2.id    
  11406.     and    c2.colid = r.fkey8
  11407.     union all
  11408.     select
  11409.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11410.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11411.         PK_TABLE_NAME         = o1.name,
  11412.         PK_COLUMN_NAME         = c1.name,
  11413.         PK_COLUMN_GUID        = convert(binary(16),null),
  11414.         PK_COLUMN_PROPID    = convert(int,null),
  11415.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11416.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  11417.         FK_TABLE_NAME         = o2.name,
  11418.         FK_COLUMN_NAME         = c2.name,
  11419.         FK_COLUMN_GUID        = convert(binary(16),null),
  11420.         FK_COLUMN_PROPID    = convert(int,null),
  11421.         ORDINAL            = convert(int,9),
  11422.         UPDATE_RULE        = 'NO ACTION',
  11423.         DELETE_RULE         = 'NO ACTION'        
  11424.     from
  11425.         sysobjects o1, sysobjects o2,
  11426.         syscolumns c1, syscolumns c2,
  11427.         sysreferences r
  11428.     where    
  11429.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11430.     and    o1.name = @pk_table_name
  11431.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11432.     and    o1.id = r.rkeyid
  11433.     and    r.keycnt >= 9
  11434.     and    o1.id = c1.id
  11435.     and    c1.colid = r.rkey9
  11436.     and     r.fkeyid = o2.id
  11437.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  11438.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11439.     and    o2.id = c2.id    
  11440.     and    c2.colid = r.fkey9
  11441.     union all
  11442.     select
  11443.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11444.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11445.         PK_TABLE_NAME         = o1.name,
  11446.         PK_COLUMN_NAME         = c1.name,
  11447.         PK_COLUMN_GUID        = convert(binary(16),null),
  11448.         PK_COLUMN_PROPID    = convert(int,null),
  11449.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11450.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  11451.         FK_TABLE_NAME         = o2.name,
  11452.         FK_COLUMN_NAME         = c2.name,
  11453.         FK_COLUMN_GUID        = convert(binary(16),null),
  11454.         FK_COLUMN_PROPID    = convert(int,null),
  11455.         ORDINAL            = convert(int,10),
  11456.         UPDATE_RULE        = 'NO ACTION',
  11457.         DELETE_RULE         = 'NO ACTION'        
  11458.     from
  11459.         sysobjects o1, sysobjects o2,
  11460.         syscolumns c1, syscolumns c2,
  11461.         sysreferences r
  11462.     where    
  11463.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11464.     and    o1.name = @pk_table_name
  11465.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11466.     and    o1.id = r.rkeyid
  11467.     and    r.keycnt >= 10
  11468.     and    o1.id = c1.id
  11469.     and    c1.colid = r.rkey10
  11470.     and     r.fkeyid = o2.id
  11471.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  11472.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11473.     and    o2.id = c2.id    
  11474.     and    c2.colid = r.fkey10
  11475.     union all
  11476.     select
  11477.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11478.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11479.         PK_TABLE_NAME         = o1.name,
  11480.         PK_COLUMN_NAME         = c1.name,
  11481.         PK_COLUMN_GUID        = convert(binary(16),null),
  11482.         PK_COLUMN_PROPID    = convert(int,null),
  11483.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11484.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  11485.         FK_TABLE_NAME         = o2.name,
  11486.         FK_COLUMN_NAME         = c2.name,
  11487.         FK_COLUMN_GUID        = convert(binary(16),null),
  11488.         FK_COLUMN_PROPID    = convert(int,null),
  11489.         ORDINAL            = convert(int,11),
  11490.         UPDATE_RULE        = 'NO ACTION',
  11491.         DELETE_RULE         = 'NO ACTION'        
  11492.     from
  11493.         sysobjects o1, sysobjects o2,
  11494.         syscolumns c1, syscolumns c2,
  11495.         sysreferences r
  11496.     where    
  11497.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11498.     and    o1.name = @pk_table_name
  11499.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11500.     and    o1.id = r.rkeyid
  11501.     and    r.keycnt >= 11
  11502.     and    o1.id = c1.id
  11503.     and    c1.colid = r.rkey11
  11504.     and     r.fkeyid = o2.id
  11505.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  11506.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11507.     and    o2.id = c2.id    
  11508.     and    c2.colid = r.fkey11
  11509.     union all
  11510.     select
  11511.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11512.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11513.         PK_TABLE_NAME         = o1.name,
  11514.         PK_COLUMN_NAME         = c1.name,
  11515.         PK_COLUMN_GUID        = convert(binary(16),null),
  11516.         PK_COLUMN_PROPID    = convert(int,null),
  11517.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11518.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  11519.         FK_TABLE_NAME         = o2.name,
  11520.         FK_COLUMN_NAME         = c2.name,
  11521.         FK_COLUMN_GUID        = convert(binary(16),null),
  11522.         FK_COLUMN_PROPID    = convert(int,null),
  11523.         ORDINAL            = convert(int,12),
  11524.         UPDATE_RULE        = 'NO ACTION',
  11525.         DELETE_RULE         = 'NO ACTION'        
  11526.     from
  11527.         sysobjects o1, sysobjects o2,
  11528.         syscolumns c1, syscolumns c2,
  11529.         sysreferences r
  11530.     where    
  11531.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11532.     and    o1.name = @pk_table_name
  11533.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11534.     and    o1.id = r.rkeyid
  11535.     and    r.keycnt >= 12
  11536.     and    o1.id = c1.id
  11537.     and    c1.colid = r.rkey12
  11538.     and     r.fkeyid = o2.id
  11539.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  11540.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11541.     and    o2.id = c2.id    
  11542.     and    c2.colid = r.fkey12
  11543.     union all
  11544.     select
  11545.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11546.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11547.         PK_TABLE_NAME         = o1.name,
  11548.         PK_COLUMN_NAME         = c1.name,
  11549.         PK_COLUMN_GUID        = convert(binary(16),null),
  11550.         PK_COLUMN_PROPID    = convert(int,null),
  11551.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11552.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  11553.         FK_TABLE_NAME         = o2.name,
  11554.         FK_COLUMN_NAME         = c2.name,
  11555.         FK_COLUMN_GUID        = convert(binary(16),null),
  11556.         FK_COLUMN_PROPID    = convert(int,null),
  11557.         ORDINAL            = convert(int,13),
  11558.         UPDATE_RULE        = 'NO ACTION',
  11559.         DELETE_RULE         = 'NO ACTION'        
  11560.     from
  11561.         sysobjects o1, sysobjects o2,
  11562.         syscolumns c1, syscolumns c2,
  11563.         sysreferences r
  11564.     where    
  11565.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11566.     and    o1.name = @pk_table_name
  11567.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11568.     and    o1.id = r.rkeyid
  11569.     and    r.keycnt >= 13
  11570.     and    o1.id = c1.id
  11571.     and    c1.colid = r.rkey13
  11572.     and     r.fkeyid = o2.id
  11573.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  11574.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11575.     and    o2.id = c2.id    
  11576.     and    c2.colid = r.fkey13
  11577.     union all
  11578.     select
  11579.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11580.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11581.         PK_TABLE_NAME         = o1.name,
  11582.         PK_COLUMN_NAME         = c1.name,
  11583.         PK_COLUMN_GUID        = convert(binary(16),null),
  11584.         PK_COLUMN_PROPID    = convert(int,null),
  11585.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11586.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  11587.         FK_TABLE_NAME         = o2.name,
  11588.         FK_COLUMN_NAME         = c2.name,
  11589.         FK_COLUMN_GUID        = convert(binary(16),null),
  11590.         FK_COLUMN_PROPID    = convert(int,null),
  11591.         ORDINAL            = convert(int,14),
  11592.         UPDATE_RULE        = 'NO ACTION',
  11593.         DELETE_RULE         = 'NO ACTION'        
  11594.     from
  11595.         sysobjects o1, sysobjects o2,
  11596.         syscolumns c1, syscolumns c2,
  11597.         sysreferences r
  11598.     where    
  11599.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11600.     and    o1.name = @pk_table_name
  11601.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11602.     and    o1.id = r.rkeyid
  11603.     and    r.keycnt >= 14
  11604.     and    o1.id = c1.id
  11605.     and    c1.colid = r.rkey14
  11606.     and     r.fkeyid = o2.id
  11607.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  11608.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11609.     and    o2.id = c2.id    
  11610.     and    c2.colid = r.fkey14
  11611.     union all
  11612.     select
  11613.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11614.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11615.         PK_TABLE_NAME         = o1.name,
  11616.         PK_COLUMN_NAME         = c1.name,
  11617.         PK_COLUMN_GUID        = convert(binary(16),null),
  11618.         PK_COLUMN_PROPID    = convert(int,null),
  11619.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11620.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  11621.         FK_TABLE_NAME         = o2.name,
  11622.         FK_COLUMN_NAME         = c2.name,
  11623.         FK_COLUMN_GUID        = convert(binary(16),null),
  11624.         FK_COLUMN_PROPID    = convert(int,null),
  11625.         ORDINAL            = convert(int,15),
  11626.         UPDATE_RULE        = 'NO ACTION',
  11627.         DELETE_RULE         = 'NO ACTION'        
  11628.     from
  11629.         sysobjects o1, sysobjects o2,
  11630.         syscolumns c1, syscolumns c2,
  11631.         sysreferences r
  11632.     where    
  11633.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11634.     and    o1.name = @pk_table_name
  11635.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11636.     and    o1.id = r.rkeyid
  11637.     and    r.keycnt >= 15
  11638.     and    o1.id = c1.id
  11639.     and    c1.colid = r.rkey15
  11640.     and     r.fkeyid = o2.id
  11641.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  11642.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11643.     and    o2.id = c2.id    
  11644.     and    c2.colid = r.fkey15
  11645.     union all
  11646.     select
  11647.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11648.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11649.         PK_TABLE_NAME         = o1.name,
  11650.         PK_COLUMN_NAME         = c1.name,
  11651.         PK_COLUMN_GUID        = convert(binary(16),null),
  11652.         PK_COLUMN_PROPID    = convert(int,null),
  11653.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11654.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  11655.         FK_TABLE_NAME         = o2.name,
  11656.         FK_COLUMN_NAME         = c2.name,
  11657.         FK_COLUMN_GUID        = convert(binary(16),null),
  11658.         FK_COLUMN_PROPID    = convert(int,null),
  11659.         ORDINAL            = convert(int,16),
  11660.         UPDATE_RULE        = 'NO ACTION',
  11661.         DELETE_RULE         = 'NO ACTION'        
  11662.     from
  11663.         sysobjects o1, sysobjects o2,
  11664.         syscolumns c1, syscolumns c2,
  11665.         sysreferences r
  11666.     where    
  11667.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11668.     and    o1.name = @pk_table_name
  11669.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11670.     and    o1.id = r.rkeyid
  11671.     and    r.keycnt >= 16
  11672.     and    o1.id = c1.id
  11673.     and    c1.colid = r.rkey16
  11674.     and     r.fkeyid = o2.id
  11675.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  11676.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11677.     and    o2.id = c2.id    
  11678.     and    c2.colid = r.fkey16
  11679.     order by 8,9,2,3,13
  11680.     END
  11681. ELSE IF @fk_table_name is not null
  11682.     BEGIN
  11683.     insert into #spfkeysrowset1
  11684.     select
  11685.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11686.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11687.         PK_TABLE_NAME         = o1.name,
  11688.         PK_COLUMN_NAME         = c1.name,
  11689.         PK_COLUMN_GUID        = convert(binary(16),null),
  11690.         PK_COLUMN_PROPID    = convert(int,null),
  11691.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11692.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  11693.         FK_TABLE_NAME         = o2.name,
  11694.         FK_COLUMN_NAME         = c2.name,
  11695.         FK_COLUMN_GUID        = convert(binary(16),null),
  11696.         FK_COLUMN_PROPID    = convert(int,null),
  11697.         ORDINAL            = convert(int,1),
  11698.         UPDATE_RULE        = 'NO ACTION',
  11699.         DELETE_RULE         = 'NO ACTION'        
  11700.     from
  11701.         sysobjects o1, sysobjects o2,
  11702.         syscolumns c1, syscolumns c2,
  11703.         sysreferences r
  11704.     where    
  11705.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  11706.     and    o2.name = @fk_table_name
  11707.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11708.     and    o2.id = r.fkeyid
  11709.     and    o2.id = c2.id
  11710.     and    c2.colid = r.fkey1
  11711.     and     r.rkeyid = o1.id
  11712.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  11713.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11714.     and    o1.id = c1.id    
  11715.     and    c1.colid = r.rkey1
  11716.     union    all
  11717.     select
  11718.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11719.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11720.         PK_TABLE_NAME         = o1.name,
  11721.         PK_COLUMN_NAME         = c1.name,
  11722.         PK_COLUMN_GUID        = convert(binary(16),null),
  11723.         PK_COLUMN_PROPID    = convert(int,null),
  11724.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11725.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  11726.         FK_TABLE_NAME         = o2.name,
  11727.         FK_COLUMN_NAME         = c2.name,
  11728.         FK_COLUMN_GUID        = convert(binary(16),null),
  11729.         FK_COLUMN_PROPID    = convert(int,null),
  11730.         ORDINAL            = convert(int,2),
  11731.         UPDATE_RULE        = 'NO ACTION',
  11732.         DELETE_RULE         = 'NO ACTION'        
  11733.     from
  11734.         sysobjects o1, sysobjects o2,
  11735.         syscolumns c1, syscolumns c2,
  11736.         sysreferences r
  11737.     where    
  11738.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  11739.     and    o2.name = @fk_table_name
  11740.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11741.     and    o2.id = r.fkeyid
  11742.     and    r.keycnt >= 2
  11743.     and    o2.id = c2.id
  11744.     and    c2.colid = r.fkey2
  11745.     and     r.rkeyid = o1.id
  11746.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  11747.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11748.     and    o1.id = c1.id    
  11749.     and    c1.colid = r.rkey2
  11750.     union    all
  11751.     select
  11752.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11753.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11754.         PK_TABLE_NAME         = o1.name,
  11755.         PK_COLUMN_NAME         = c1.name,
  11756.         PK_COLUMN_GUID        = convert(binary(16),null),
  11757.         PK_COLUMN_PROPID    = convert(int,null),
  11758.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11759.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  11760.         FK_TABLE_NAME         = o2.name,
  11761.         FK_COLUMN_NAME         = c2.name,
  11762.         FK_COLUMN_GUID        = convert(binary(16),null),
  11763.         FK_COLUMN_PROPID    = convert(int,null),
  11764.         ORDINAL            = convert(int,3),
  11765.         UPDATE_RULE        = 'NO ACTION',
  11766.         DELETE_RULE         = 'NO ACTION'        
  11767.     from
  11768.         sysobjects o1, sysobjects o2,
  11769.         syscolumns c1, syscolumns c2,
  11770.         sysreferences r
  11771.     where    
  11772.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  11773.     and    o2.name = @fk_table_name
  11774.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11775.     and    o2.id = r.fkeyid
  11776.     and    r.keycnt >= 3
  11777.     and    o2.id = c2.id
  11778.     and    c2.colid = r.fkey3
  11779.     and     r.rkeyid = o1.id
  11780.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  11781.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11782.     and    o1.id = c1.id    
  11783.     and    c1.colid = r.rkey3
  11784.     union    all
  11785.     select
  11786.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11787.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11788.         PK_TABLE_NAME         = o1.name,
  11789.         PK_COLUMN_NAME         = c1.name,
  11790.         PK_COLUMN_GUID        = convert(binary(16),null),
  11791.         PK_COLUMN_PROPID    = convert(int,null),
  11792.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11793.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  11794.         FK_TABLE_NAME         = o2.name,
  11795.         FK_COLUMN_NAME         = c2.name,
  11796.         FK_COLUMN_GUID        = convert(binary(16),null),
  11797.         FK_COLUMN_PROPID    = convert(int,null),
  11798.         ORDINAL            = convert(int,4),
  11799.         UPDATE_RULE        = 'NO ACTION',
  11800.         DELETE_RULE         = 'NO ACTION'        
  11801.     from
  11802.         sysobjects o1, sysobjects o2,
  11803.         syscolumns c1, syscolumns c2,
  11804.         sysreferences r
  11805.     where    
  11806.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  11807.     and    o2.name = @fk_table_name
  11808.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11809.     and    o2.id = r.fkeyid
  11810.     and    r.keycnt >= 4
  11811.     and    o2.id = c2.id
  11812.     and    c2.colid = r.fkey4
  11813.     and     r.rkeyid = o1.id
  11814.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  11815.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11816.     and    o1.id = c1.id    
  11817.     and    c1.colid = r.rkey4
  11818.     union    all
  11819.     select
  11820.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11821.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11822.         PK_TABLE_NAME         = o1.name,
  11823.         PK_COLUMN_NAME         = c1.name,
  11824.         PK_COLUMN_GUID        = convert(binary(16),null),
  11825.         PK_COLUMN_PROPID    = convert(int,null),
  11826.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11827.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  11828.         FK_TABLE_NAME         = o2.name,
  11829.         FK_COLUMN_NAME         = c2.name,
  11830.         FK_COLUMN_GUID        = convert(binary(16),null),
  11831.         FK_COLUMN_PROPID    = convert(int,null),
  11832.         ORDINAL            = convert(int,5),
  11833.         UPDATE_RULE        = 'NO ACTION',
  11834.         DELETE_RULE         = 'NO ACTION'        
  11835.     from
  11836.         sysobjects o1, sysobjects o2,
  11837.         syscolumns c1, syscolumns c2,
  11838.         sysreferences r
  11839.     where    
  11840.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  11841.     and    o2.name = @fk_table_name
  11842.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11843.     and    o2.id = r.fkeyid
  11844.     and    r.keycnt >= 5
  11845.     and    o2.id = c2.id
  11846.     and    c2.colid = r.fkey5
  11847.     and     r.rkeyid = o1.id
  11848.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  11849.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11850.     and    o1.id = c1.id    
  11851.     and    c1.colid = r.rkey5
  11852.     union    all
  11853.     select
  11854.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11855.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11856.         PK_TABLE_NAME         = o1.name,
  11857.         PK_COLUMN_NAME         = c1.name,
  11858.         PK_COLUMN_GUID        = convert(binary(16),null),
  11859.         PK_COLUMN_PROPID    = convert(int,null),
  11860.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11861.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  11862.         FK_TABLE_NAME         = o2.name,
  11863.         FK_COLUMN_NAME         = c2.name,
  11864.         FK_COLUMN_GUID        = convert(binary(16),null),
  11865.         FK_COLUMN_PROPID    = convert(int,null),
  11866.         ORDINAL            = convert(int,6),
  11867.         UPDATE_RULE        = 'NO ACTION',
  11868.         DELETE_RULE         = 'NO ACTION'        
  11869.     from
  11870.         sysobjects o1, sysobjects o2,
  11871.         syscolumns c1, syscolumns c2,
  11872.         sysreferences r
  11873.     where    
  11874.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  11875.     and    o2.name = @fk_table_name
  11876.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11877.     and    o2.id = r.fkeyid
  11878.     and    r.keycnt >= 6
  11879.     and    o2.id = c2.id
  11880.     and    c2.colid = r.fkey6
  11881.     and     r.rkeyid = o1.id
  11882.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  11883.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11884.     and    o1.id = c1.id    
  11885.     and    c1.colid = r.rkey6
  11886.     union    all
  11887.     select
  11888.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11889.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11890.         PK_TABLE_NAME         = o1.name,
  11891.         PK_COLUMN_NAME         = c1.name,
  11892.         PK_COLUMN_GUID        = convert(binary(16),null),
  11893.         PK_COLUMN_PROPID    = convert(int,null),
  11894.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11895.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  11896.         FK_TABLE_NAME         = o2.name,
  11897.         FK_COLUMN_NAME         = c2.name,
  11898.         FK_COLUMN_GUID        = convert(binary(16),null),
  11899.         FK_COLUMN_PROPID    = convert(int,null),
  11900.         ORDINAL            = convert(int,7),
  11901.         UPDATE_RULE        = 'NO ACTION',
  11902.         DELETE_RULE         = 'NO ACTION'        
  11903.     from
  11904.         sysobjects o1, sysobjects o2,
  11905.         syscolumns c1, syscolumns c2,
  11906.         sysreferences r
  11907.     where    
  11908.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  11909.     and    o2.name = @fk_table_name
  11910.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11911.     and    o2.id = r.fkeyid
  11912.     and    r.keycnt >= 7
  11913.     and    o2.id = c2.id
  11914.     and    c2.colid = r.fkey7
  11915.     and     r.rkeyid = o1.id
  11916.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  11917.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11918.     and    o1.id = c1.id    
  11919.     and    c1.colid = r.rkey7
  11920.     union    all
  11921.     select
  11922.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11923.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11924.         PK_TABLE_NAME         = o1.name,
  11925.         PK_COLUMN_NAME         = c1.name,
  11926.         PK_COLUMN_GUID        = convert(binary(16),null),
  11927.         PK_COLUMN_PROPID    = convert(int,null),
  11928.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11929.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  11930.         FK_TABLE_NAME         = o2.name,
  11931.         FK_COLUMN_NAME         = c2.name,
  11932.         FK_COLUMN_GUID        = convert(binary(16),null),
  11933.         FK_COLUMN_PROPID    = convert(int,null),
  11934.         ORDINAL            = convert(int,8),
  11935.         UPDATE_RULE        = 'NO ACTION',
  11936.         DELETE_RULE         = 'NO ACTION'        
  11937.     from
  11938.         sysobjects o1, sysobjects o2,
  11939.         syscolumns c1, syscolumns c2,
  11940.         sysreferences r
  11941.     where    
  11942.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  11943.     and    o2.name = @fk_table_name
  11944.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11945.     and    o2.id = r.fkeyid
  11946.     and    r.keycnt >= 8
  11947.     and    o2.id = c2.id
  11948.     and    c2.colid = r.fkey8
  11949.     and     r.rkeyid = o1.id
  11950.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  11951.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11952.     and    o1.id = c1.id    
  11953.     and    c1.colid = r.rkey8
  11954.     union    all
  11955.     select
  11956.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11957.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11958.         PK_TABLE_NAME         = o1.name,
  11959.         PK_COLUMN_NAME         = c1.name,
  11960.         PK_COLUMN_GUID        = convert(binary(16),null),
  11961.         PK_COLUMN_PROPID    = convert(int,null),
  11962.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11963.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  11964.         FK_TABLE_NAME         = o2.name,
  11965.         FK_COLUMN_NAME         = c2.name,
  11966.         FK_COLUMN_GUID        = convert(binary(16),null),
  11967.         FK_COLUMN_PROPID    = convert(int,null),
  11968.         ORDINAL            = convert(int,9),
  11969.         UPDATE_RULE        = 'NO ACTION',
  11970.         DELETE_RULE         = 'NO ACTION'        
  11971.     from
  11972.         sysobjects o1, sysobjects o2,
  11973.         syscolumns c1, syscolumns c2,
  11974.         sysreferences r
  11975.     where    
  11976.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  11977.     and    o2.name = @fk_table_name
  11978.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11979.     and    o2.id = r.fkeyid
  11980.     and    r.keycnt >= 9
  11981.     and    o2.id = c2.id
  11982.     and    c2.colid = r.fkey9
  11983.     and     r.rkeyid = o1.id
  11984.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  11985.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11986.     and    o1.id = c1.id    
  11987.     and    c1.colid = r.rkey9
  11988.     union    all
  11989.     select
  11990.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11991.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11992.         PK_TABLE_NAME         = o1.name,
  11993.         PK_COLUMN_NAME         = c1.name,
  11994.         PK_COLUMN_GUID        = convert(binary(16),null),
  11995.         PK_COLUMN_PROPID    = convert(int,null),
  11996.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11997.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  11998.         FK_TABLE_NAME         = o2.name,
  11999.         FK_COLUMN_NAME         = c2.name,
  12000.         FK_COLUMN_GUID        = convert(binary(16),null),
  12001.         FK_COLUMN_PROPID    = convert(int,null),
  12002.         ORDINAL            = convert(int,10),
  12003.         UPDATE_RULE        = 'NO ACTION',
  12004.         DELETE_RULE         = 'NO ACTION'        
  12005.     from
  12006.         sysobjects o1, sysobjects o2,
  12007.         syscolumns c1, syscolumns c2,
  12008.         sysreferences r
  12009.     where    
  12010.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12011.     and    o2.name = @fk_table_name
  12012.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12013.     and    o2.id = r.fkeyid
  12014.     and    r.keycnt >= 10
  12015.     and    o2.id = c2.id
  12016.     and    c2.colid = r.fkey10
  12017.     and     r.rkeyid = o1.id
  12018.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  12019.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12020.     and    o1.id = c1.id    
  12021.     and    c1.colid = r.rkey10
  12022.     union    all
  12023.     select
  12024.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12025.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12026.         PK_TABLE_NAME         = o1.name,
  12027.         PK_COLUMN_NAME         = c1.name,
  12028.         PK_COLUMN_GUID        = convert(binary(16),null),
  12029.         PK_COLUMN_PROPID    = convert(int,null),
  12030.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12031.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  12032.         FK_TABLE_NAME         = o2.name,
  12033.         FK_COLUMN_NAME         = c2.name,
  12034.         FK_COLUMN_GUID        = convert(binary(16),null),
  12035.         FK_COLUMN_PROPID    = convert(int,null),
  12036.         ORDINAL            = convert(int,11),
  12037.         UPDATE_RULE        = 'NO ACTION',
  12038.         DELETE_RULE         = 'NO ACTION'        
  12039.     from
  12040.         sysobjects o1, sysobjects o2,
  12041.         syscolumns c1, syscolumns c2,
  12042.         sysreferences r
  12043.     where    
  12044.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12045.     and    o2.name = @fk_table_name
  12046.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12047.     and    o2.id = r.fkeyid
  12048.     and    r.keycnt >= 11
  12049.     and    o2.id = c2.id
  12050.     and    c2.colid = r.fkey11
  12051.     and     r.rkeyid = o1.id
  12052.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  12053.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12054.     and    o1.id = c1.id    
  12055.     and    c1.colid = r.rkey11
  12056.     union    all
  12057.     select
  12058.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12059.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12060.         PK_TABLE_NAME         = o1.name,
  12061.         PK_COLUMN_NAME         = c1.name,
  12062.         PK_COLUMN_GUID        = convert(binary(16),null),
  12063.         PK_COLUMN_PROPID    = convert(int,null),
  12064.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12065.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  12066.         FK_TABLE_NAME         = o2.name,
  12067.         FK_COLUMN_NAME         = c2.name,
  12068.         FK_COLUMN_GUID        = convert(binary(16),null),
  12069.         FK_COLUMN_PROPID    = convert(int,null),
  12070.         ORDINAL            = convert(int,12),
  12071.         UPDATE_RULE        = 'NO ACTION',
  12072.         DELETE_RULE         = 'NO ACTION'        
  12073.     from
  12074.         sysobjects o1, sysobjects o2,
  12075.         syscolumns c1, syscolumns c2,
  12076.         sysreferences r
  12077.     where    
  12078.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12079.     and    o2.name = @fk_table_name
  12080.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12081.     and    o2.id = r.fkeyid
  12082.     and    r.keycnt >= 12
  12083.     and    o2.id = c2.id
  12084.     and    c2.colid = r.fkey12
  12085.     and     r.rkeyid = o1.id
  12086.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  12087.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12088.     and    o1.id = c1.id    
  12089.     and    c1.colid = r.rkey12
  12090.     union    all
  12091.     select
  12092.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12093.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12094.         PK_TABLE_NAME         = o1.name,
  12095.         PK_COLUMN_NAME         = c1.name,
  12096.         PK_COLUMN_GUID        = convert(binary(16),null),
  12097.         PK_COLUMN_PROPID    = convert(int,null),
  12098.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12099.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  12100.         FK_TABLE_NAME         = o2.name,
  12101.         FK_COLUMN_NAME         = c2.name,
  12102.         FK_COLUMN_GUID        = convert(binary(16),null),
  12103.         FK_COLUMN_PROPID    = convert(int,null),
  12104.         ORDINAL            = convert(int,13),
  12105.         UPDATE_RULE        = 'NO ACTION',
  12106.         DELETE_RULE         = 'NO ACTION'        
  12107.     from
  12108.         sysobjects o1, sysobjects o2,
  12109.         syscolumns c1, syscolumns c2,
  12110.         sysreferences r
  12111.     where    
  12112.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12113.     and    o2.name = @fk_table_name
  12114.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12115.     and    o2.id = r.fkeyid
  12116.     and    r.keycnt >= 13
  12117.     and    o2.id = c2.id
  12118.     and    c2.colid = r.fkey13
  12119.     and     r.rkeyid = o1.id
  12120.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  12121.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12122.     and    o1.id = c1.id    
  12123.     and    c1.colid = r.rkey13
  12124.     union    all
  12125.     select
  12126.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12127.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12128.         PK_TABLE_NAME         = o1.name,
  12129.         PK_COLUMN_NAME         = c1.name,
  12130.         PK_COLUMN_GUID        = convert(binary(16),null),
  12131.         PK_COLUMN_PROPID    = convert(int,null),
  12132.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12133.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  12134.         FK_TABLE_NAME         = o2.name,
  12135.         FK_COLUMN_NAME         = c2.name,
  12136.         FK_COLUMN_GUID        = convert(binary(16),null),
  12137.         FK_COLUMN_PROPID    = convert(int,null),
  12138.         ORDINAL            = convert(int,14),
  12139.         UPDATE_RULE        = 'NO ACTION',
  12140.         DELETE_RULE         = 'NO ACTION'        
  12141.     from
  12142.         sysobjects o1, sysobjects o2,
  12143.         syscolumns c1, syscolumns c2,
  12144.         sysreferences r
  12145.     where    
  12146.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12147.     and    o2.name = @fk_table_name
  12148.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12149.     and    o2.id = r.fkeyid
  12150.     and    r.keycnt >= 14
  12151.     and    o2.id = c2.id
  12152.     and    c2.colid = r.fkey14
  12153.     and     r.rkeyid = o1.id
  12154.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  12155.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12156.     and    o1.id = c1.id    
  12157.     and    c1.colid = r.rkey14
  12158.     union    all
  12159.     select
  12160.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12161.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12162.         PK_TABLE_NAME         = o1.name,
  12163.         PK_COLUMN_NAME         = c1.name,
  12164.         PK_COLUMN_GUID        = convert(binary(16),null),
  12165.         PK_COLUMN_PROPID    = convert(int,null),
  12166.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12167.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  12168.         FK_TABLE_NAME         = o2.name,
  12169.         FK_COLUMN_NAME         = c2.name,
  12170.         FK_COLUMN_GUID        = convert(binary(16),null),
  12171.         FK_COLUMN_PROPID    = convert(int,null),
  12172.         ORDINAL            = convert(int,15),
  12173.         UPDATE_RULE        = 'NO ACTION',
  12174.         DELETE_RULE         = 'NO ACTION'        
  12175.     from
  12176.         sysobjects o1, sysobjects o2,
  12177.         syscolumns c1, syscolumns c2,
  12178.         sysreferences r
  12179.     where    
  12180.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12181.     and    o2.name = @fk_table_name
  12182.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12183.     and    o2.id = r.fkeyid
  12184.     and    r.keycnt >= 15
  12185.     and    o2.id = c2.id
  12186.     and    c2.colid = r.fkey15
  12187.     and     r.rkeyid = o1.id
  12188.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  12189.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12190.     and    o1.id = c1.id    
  12191.     and    c1.colid = r.rkey15
  12192.     union    all
  12193.     select
  12194.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12195.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12196.         PK_TABLE_NAME         = o1.name,
  12197.         PK_COLUMN_NAME         = c1.name,
  12198.         PK_COLUMN_GUID        = convert(binary(16),null),
  12199.         PK_COLUMN_PROPID    = convert(int,null),
  12200.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12201.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  12202.         FK_TABLE_NAME         = o2.name,
  12203.         FK_COLUMN_NAME         = c2.name,
  12204.         FK_COLUMN_GUID        = convert(binary(16),null),
  12205.         FK_COLUMN_PROPID    = convert(int,null),
  12206.         ORDINAL            = convert(int,16),
  12207.         UPDATE_RULE        = 'NO ACTION',
  12208.         DELETE_RULE         = 'NO ACTION'        
  12209.     from
  12210.         sysobjects o1, sysobjects o2,
  12211.         syscolumns c1, syscolumns c2,
  12212.         sysreferences r
  12213.     where    
  12214.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12215.     and    o2.name = @fk_table_name
  12216.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12217.     and    o2.id = r.fkeyid
  12218.     and    r.keycnt >= 16
  12219.     and    o2.id = c2.id
  12220.     and    c2.colid = r.fkey16
  12221.     and     r.rkeyid = o1.id
  12222.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  12223.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12224.     and    o1.id = c1.id    
  12225.     and    c1.colid = r.rkey16
  12226.     order by 8,9,2,3,13
  12227.     END
  12228. ELSE
  12229.     BEGIN
  12230.     insert into #spfkeysrowset1
  12231.     select
  12232.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12233.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12234.         PK_TABLE_NAME         = o1.name,
  12235.         PK_COLUMN_NAME         = c1.name,
  12236.         PK_COLUMN_GUID        = convert(binary(16),null),
  12237.         PK_COLUMN_PROPID    = convert(int,null),
  12238.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12239.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  12240.         FK_TABLE_NAME         = o2.name,
  12241.         FK_COLUMN_NAME         = c2.name,
  12242.         FK_COLUMN_GUID        = convert(binary(16),null),
  12243.         FK_COLUMN_PROPID    = convert(int,null),
  12244.         ORDINAL            = convert(int,1),
  12245.         UPDATE_RULE        = 'NO ACTION',
  12246.         DELETE_RULE         = 'NO ACTION'        
  12247.     from
  12248.         sysobjects o1, sysobjects o2,
  12249.         syscolumns c1, syscolumns c2,
  12250.         sysreferences r
  12251.     where    
  12252.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12253.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12254.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12255.     and    o1.id = r.rkeyid
  12256.     and    o1.id = c1.id
  12257.     and    c1.colid = r.rkey1
  12258.     and     r.fkeyid = o2.id
  12259.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12260.     and    o2.id = c2.id    
  12261.     and    c2.colid = r.fkey1
  12262.     union all
  12263.     select
  12264.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12265.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12266.         PK_TABLE_NAME         = o1.name,
  12267.         PK_COLUMN_NAME         = c1.name,
  12268.         PK_COLUMN_GUID        = convert(binary(16),null),
  12269.         PK_COLUMN_PROPID    = convert(int,null),
  12270.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12271.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  12272.         FK_TABLE_NAME         = o2.name,
  12273.         FK_COLUMN_NAME         = c2.name,
  12274.         FK_COLUMN_GUID        = convert(binary(16),null),
  12275.         FK_COLUMN_PROPID    = convert(int,null),
  12276.         ORDINAL            = convert(int,2),
  12277.         UPDATE_RULE        = 'NO ACTION',
  12278.         DELETE_RULE         = 'NO ACTION'        
  12279.     from
  12280.         sysobjects o1, sysobjects o2,
  12281.         syscolumns c1, syscolumns c2,
  12282.         sysreferences r
  12283.     where    
  12284.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12285.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12286.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12287.     and    o1.id = r.rkeyid
  12288.     and    r.keycnt >= 2
  12289.     and    o1.id = c1.id
  12290.     and    c1.colid = r.rkey2
  12291.     and     r.fkeyid = o2.id
  12292.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12293.     and    o2.id = c2.id    
  12294.     and    c2.colid = r.fkey2
  12295.     union all
  12296.     select
  12297.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12298.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12299.         PK_TABLE_NAME         = o1.name,
  12300.         PK_COLUMN_NAME         = c1.name,
  12301.         PK_COLUMN_GUID        = convert(binary(16),null),
  12302.         PK_COLUMN_PROPID    = convert(int,null),
  12303.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12304.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  12305.         FK_TABLE_NAME         = o2.name,
  12306.         FK_COLUMN_NAME         = c2.name,
  12307.         FK_COLUMN_GUID        = convert(binary(16),null),
  12308.         FK_COLUMN_PROPID    = convert(int,null),
  12309.         ORDINAL            = convert(int,3),
  12310.         UPDATE_RULE        = 'NO ACTION',
  12311.         DELETE_RULE         = 'NO ACTION'        
  12312.     from
  12313.         sysobjects o1, sysobjects o2,
  12314.         syscolumns c1, syscolumns c2,
  12315.         sysreferences r
  12316.     where    
  12317.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12318.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12319.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12320.     and    o1.id = r.rkeyid
  12321.     and    r.keycnt >= 3
  12322.     and    o1.id = c1.id
  12323.     and    c1.colid = r.rkey3
  12324.     and     r.fkeyid = o2.id
  12325.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12326.     and    o2.id = c2.id    
  12327.     and    c2.colid = r.fkey3
  12328.     union all
  12329.     select
  12330.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12331.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12332.         PK_TABLE_NAME         = o1.name,
  12333.         PK_COLUMN_NAME         = c1.name,
  12334.         PK_COLUMN_GUID        = convert(binary(16),null),
  12335.         PK_COLUMN_PROPID    = convert(int,null),
  12336.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12337.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  12338.         FK_TABLE_NAME         = o2.name,
  12339.         FK_COLUMN_NAME         = c2.name,
  12340.         FK_COLUMN_GUID        = convert(binary(16),null),
  12341.         FK_COLUMN_PROPID    = convert(int,null),
  12342.         ORDINAL            = convert(int,4),
  12343.         UPDATE_RULE        = 'NO ACTION',
  12344.         DELETE_RULE         = 'NO ACTION'        
  12345.     from
  12346.         sysobjects o1, sysobjects o2,
  12347.         syscolumns c1, syscolumns c2,
  12348.         sysreferences r
  12349.     where    
  12350.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12351.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12352.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12353.     and    o1.id = r.rkeyid
  12354.     and    r.keycnt >= 4
  12355.     and    o1.id = c1.id
  12356.     and    c1.colid = r.rkey4
  12357.     and     r.fkeyid = o2.id
  12358.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12359.     and    o2.id = c2.id    
  12360.     and    c2.colid = r.fkey4
  12361.     union all
  12362.     select
  12363.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12364.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12365.         PK_TABLE_NAME         = o1.name,
  12366.         PK_COLUMN_NAME         = c1.name,
  12367.         PK_COLUMN_GUID        = convert(binary(16),null),
  12368.         PK_COLUMN_PROPID    = convert(int,null),
  12369.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12370.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  12371.         FK_TABLE_NAME         = o2.name,
  12372.         FK_COLUMN_NAME         = c2.name,
  12373.         FK_COLUMN_GUID        = convert(binary(16),null),
  12374.         FK_COLUMN_PROPID    = convert(int,null),
  12375.         ORDINAL            = convert(int,5),
  12376.         UPDATE_RULE        = 'NO ACTION',
  12377.         DELETE_RULE         = 'NO ACTION'        
  12378.     from
  12379.         sysobjects o1, sysobjects o2,
  12380.         syscolumns c1, syscolumns c2,
  12381.         sysreferences r
  12382.     where    
  12383.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12384.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12385.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12386.     and    o1.id = r.rkeyid
  12387.     and    r.keycnt >= 5
  12388.     and    o1.id = c1.id
  12389.     and    c1.colid = r.rkey5
  12390.     and     r.fkeyid = o2.id
  12391.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12392.     and    o2.id = c2.id    
  12393.     and    c2.colid = r.fkey5
  12394.     union all
  12395.     select
  12396.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12397.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12398.         PK_TABLE_NAME         = o1.name,
  12399.         PK_COLUMN_NAME         = c1.name,
  12400.         PK_COLUMN_GUID        = convert(binary(16),null),
  12401.         PK_COLUMN_PROPID    = convert(int,null),
  12402.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12403.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  12404.         FK_TABLE_NAME         = o2.name,
  12405.         FK_COLUMN_NAME         = c2.name,
  12406.         FK_COLUMN_GUID        = convert(binary(16),null),
  12407.         FK_COLUMN_PROPID    = convert(int,null),
  12408.         ORDINAL            = convert(int,6),
  12409.         UPDATE_RULE        = 'NO ACTION',
  12410.         DELETE_RULE         = 'NO ACTION'        
  12411.     from
  12412.         sysobjects o1, sysobjects o2,
  12413.         syscolumns c1, syscolumns c2,
  12414.         sysreferences r
  12415.     where    
  12416.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12417.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12418.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12419.     and    o1.id = r.rkeyid
  12420.     and    r.keycnt >= 6
  12421.     and    o1.id = c1.id
  12422.     and    c1.colid = r.rkey6
  12423.     and     r.fkeyid = o2.id
  12424.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12425.     and    o2.id = c2.id    
  12426.     and    c2.colid = r.fkey6
  12427.     union all
  12428.     select
  12429.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12430.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12431.         PK_TABLE_NAME         = o1.name,
  12432.         PK_COLUMN_NAME         = c1.name,
  12433.         PK_COLUMN_GUID        = convert(binary(16),null),
  12434.         PK_COLUMN_PROPID    = convert(int,null),
  12435.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12436.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  12437.         FK_TABLE_NAME         = o2.name,
  12438.         FK_COLUMN_NAME         = c2.name,
  12439.         FK_COLUMN_GUID        = convert(binary(16),null),
  12440.         FK_COLUMN_PROPID    = convert(int,null),
  12441.         ORDINAL            = convert(int,7),
  12442.         UPDATE_RULE        = 'NO ACTION',
  12443.         DELETE_RULE         = 'NO ACTION'        
  12444.     from
  12445.         sysobjects o1, sysobjects o2,
  12446.         syscolumns c1, syscolumns c2,
  12447.         sysreferences r
  12448.     where    
  12449.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12450.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12451.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12452.     and    o1.id = r.rkeyid
  12453.     and    r.keycnt >= 7
  12454.     and    o1.id = c1.id
  12455.     and    c1.colid = r.rkey7
  12456.     and     r.fkeyid = o2.id
  12457.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12458.     and    o2.id = c2.id    
  12459.     and    c2.colid = r.fkey7
  12460.     union all
  12461.     select
  12462.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12463.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12464.         PK_TABLE_NAME         = o1.name,
  12465.         PK_COLUMN_NAME         = c1.name,
  12466.         PK_COLUMN_GUID        = convert(binary(16),null),
  12467.         PK_COLUMN_PROPID    = convert(int,null),
  12468.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12469.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  12470.         FK_TABLE_NAME         = o2.name,
  12471.         FK_COLUMN_NAME         = c2.name,
  12472.         FK_COLUMN_GUID        = convert(binary(16),null),
  12473.         FK_COLUMN_PROPID    = convert(int,null),
  12474.         ORDINAL            = convert(int,8),
  12475.         UPDATE_RULE        = 'NO ACTION',
  12476.         DELETE_RULE         = 'NO ACTION'        
  12477.     from
  12478.         sysobjects o1, sysobjects o2,
  12479.         syscolumns c1, syscolumns c2,
  12480.         sysreferences r
  12481.     where    
  12482.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12483.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12484.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12485.     and    o1.id = r.rkeyid
  12486.     and    r.keycnt >= 8
  12487.     and    o1.id = c1.id
  12488.     and    c1.colid = r.rkey8
  12489.     and     r.fkeyid = o2.id
  12490.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12491.     and    o2.id = c2.id    
  12492.     and    c2.colid = r.fkey8
  12493.     union all
  12494.     select
  12495.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12496.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12497.         PK_TABLE_NAME         = o1.name,
  12498.         PK_COLUMN_NAME         = c1.name,
  12499.         PK_COLUMN_GUID        = convert(binary(16),null),
  12500.         PK_COLUMN_PROPID    = convert(int,null),
  12501.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12502.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  12503.         FK_TABLE_NAME         = o2.name,
  12504.         FK_COLUMN_NAME         = c2.name,
  12505.         FK_COLUMN_GUID        = convert(binary(16),null),
  12506.         FK_COLUMN_PROPID    = convert(int,null),
  12507.         ORDINAL            = convert(int,9),
  12508.         UPDATE_RULE        = 'NO ACTION',
  12509.         DELETE_RULE         = 'NO ACTION'        
  12510.     from
  12511.         sysobjects o1, sysobjects o2,
  12512.         syscolumns c1, syscolumns c2,
  12513.         sysreferences r
  12514.     where    
  12515.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12516.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12517.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12518.     and    o1.id = r.rkeyid
  12519.     and    r.keycnt >= 9
  12520.     and    o1.id = c1.id
  12521.     and    c1.colid = r.rkey9
  12522.     and     r.fkeyid = o2.id
  12523.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12524.     and    o2.id = c2.id    
  12525.     and    c2.colid = r.fkey9
  12526.     union all
  12527.     select
  12528.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12529.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12530.         PK_TABLE_NAME         = o1.name,
  12531.         PK_COLUMN_NAME         = c1.name,
  12532.         PK_COLUMN_GUID        = convert(binary(16),null),
  12533.         PK_COLUMN_PROPID    = convert(int,null),
  12534.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12535.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  12536.         FK_TABLE_NAME         = o2.name,
  12537.         FK_COLUMN_NAME         = c2.name,
  12538.         FK_COLUMN_GUID        = convert(binary(16),null),
  12539.         FK_COLUMN_PROPID    = convert(int,null),
  12540.         ORDINAL            = convert(int,10),
  12541.         UPDATE_RULE        = 'NO ACTION',
  12542.         DELETE_RULE         = 'NO ACTION'        
  12543.     from
  12544.         sysobjects o1, sysobjects o2,
  12545.         syscolumns c1, syscolumns c2,
  12546.         sysreferences r
  12547.     where    
  12548.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12549.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12550.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12551.     and    o1.id = r.rkeyid
  12552.     and    r.keycnt >= 10
  12553.     and    o1.id = c1.id
  12554.     and    c1.colid = r.rkey10
  12555.     and     r.fkeyid = o2.id
  12556.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12557.     and    o2.id = c2.id    
  12558.     and    c2.colid = r.fkey10
  12559.     union all
  12560.     select
  12561.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12562.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12563.         PK_TABLE_NAME         = o1.name,
  12564.         PK_COLUMN_NAME         = c1.name,
  12565.         PK_COLUMN_GUID        = convert(binary(16),null),
  12566.         PK_COLUMN_PROPID    = convert(int,null),
  12567.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12568.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  12569.         FK_TABLE_NAME         = o2.name,
  12570.         FK_COLUMN_NAME         = c2.name,
  12571.         FK_COLUMN_GUID        = convert(binary(16),null),
  12572.         FK_COLUMN_PROPID    = convert(int,null),
  12573.         ORDINAL            = convert(int,11),
  12574.         UPDATE_RULE        = 'NO ACTION',
  12575.         DELETE_RULE         = 'NO ACTION'        
  12576.     from
  12577.         sysobjects o1, sysobjects o2,
  12578.         syscolumns c1, syscolumns c2,
  12579.         sysreferences r
  12580.     where    
  12581.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12582.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12583.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12584.     and    o1.id = r.rkeyid
  12585.     and    r.keycnt >= 11
  12586.     and    o1.id = c1.id
  12587.     and    c1.colid = r.rkey11
  12588.     and     r.fkeyid = o2.id
  12589.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12590.     and    o2.id = c2.id    
  12591.     and    c2.colid = r.fkey11
  12592.     union all
  12593.     select
  12594.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12595.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12596.         PK_TABLE_NAME         = o1.name,
  12597.         PK_COLUMN_NAME         = c1.name,
  12598.         PK_COLUMN_GUID        = convert(binary(16),null),
  12599.         PK_COLUMN_PROPID    = convert(int,null),
  12600.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12601.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  12602.         FK_TABLE_NAME         = o2.name,
  12603.         FK_COLUMN_NAME         = c2.name,
  12604.         FK_COLUMN_GUID        = convert(binary(16),null),
  12605.         FK_COLUMN_PROPID    = convert(int,null),
  12606.         ORDINAL            = convert(int,12),
  12607.         UPDATE_RULE        = 'NO ACTION',
  12608.         DELETE_RULE         = 'NO ACTION'        
  12609.     from
  12610.         sysobjects o1, sysobjects o2,
  12611.         syscolumns c1, syscolumns c2,
  12612.         sysreferences r
  12613.     where    
  12614.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12615.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12616.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12617.     and    o1.id = r.rkeyid
  12618.     and    r.keycnt >= 12
  12619.     and    o1.id = c1.id
  12620.     and    c1.colid = r.rkey12
  12621.     and     r.fkeyid = o2.id
  12622.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12623.     and    o2.id = c2.id    
  12624.     and    c2.colid = r.fkey12
  12625.     union all
  12626.     select
  12627.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12628.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12629.         PK_TABLE_NAME         = o1.name,
  12630.         PK_COLUMN_NAME         = c1.name,
  12631.         PK_COLUMN_GUID        = convert(binary(16),null),
  12632.         PK_COLUMN_PROPID    = convert(int,null),
  12633.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12634.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  12635.         FK_TABLE_NAME         = o2.name,
  12636.         FK_COLUMN_NAME         = c2.name,
  12637.         FK_COLUMN_GUID        = convert(binary(16),null),
  12638.         FK_COLUMN_PROPID    = convert(int,null),
  12639.         ORDINAL            = convert(int,13),
  12640.         UPDATE_RULE        = 'NO ACTION',
  12641.         DELETE_RULE         = 'NO ACTION'        
  12642.     from
  12643.         sysobjects o1, sysobjects o2,
  12644.         syscolumns c1, syscolumns c2,
  12645.         sysreferences r
  12646.     where    
  12647.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12648.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12649.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12650.     and    o1.id = r.rkeyid
  12651.     and    r.keycnt >= 13
  12652.     and    o1.id = c1.id
  12653.     and    c1.colid = r.rkey13
  12654.     and     r.fkeyid = o2.id
  12655.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12656.     and    o2.id = c2.id    
  12657.     and    c2.colid = r.fkey13
  12658.     union all
  12659.     select
  12660.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12661.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12662.         PK_TABLE_NAME         = o1.name,
  12663.         PK_COLUMN_NAME         = c1.name,
  12664.         PK_COLUMN_GUID        = convert(binary(16),null),
  12665.         PK_COLUMN_PROPID    = convert(int,null),
  12666.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12667.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  12668.         FK_TABLE_NAME         = o2.name,
  12669.         FK_COLUMN_NAME         = c2.name,
  12670.         FK_COLUMN_GUID        = convert(binary(16),null),
  12671.         FK_COLUMN_PROPID    = convert(int,null),
  12672.         ORDINAL            = convert(int,14),
  12673.         UPDATE_RULE        = 'NO ACTION',
  12674.         DELETE_RULE         = 'NO ACTION'        
  12675.     from
  12676.         sysobjects o1, sysobjects o2,
  12677.         syscolumns c1, syscolumns c2,
  12678.         sysreferences r
  12679.     where    
  12680.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12681.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12682.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12683.     and    o1.id = r.rkeyid
  12684.     and    r.keycnt >= 14
  12685.     and    o1.id = c1.id
  12686.     and    c1.colid = r.rkey14
  12687.     and     r.fkeyid = o2.id
  12688.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12689.     and    o2.id = c2.id    
  12690.     and    c2.colid = r.fkey14
  12691.     union all
  12692.     select
  12693.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12694.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12695.         PK_TABLE_NAME         = o1.name,
  12696.         PK_COLUMN_NAME         = c1.name,
  12697.         PK_COLUMN_GUID        = convert(binary(16),null),
  12698.         PK_COLUMN_PROPID    = convert(int,null),
  12699.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12700.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  12701.         FK_TABLE_NAME         = o2.name,
  12702.         FK_COLUMN_NAME         = c2.name,
  12703.         FK_COLUMN_GUID        = convert(binary(16),null),
  12704.         FK_COLUMN_PROPID    = convert(int,null),
  12705.         ORDINAL            = convert(int,15),
  12706.         UPDATE_RULE        = 'NO ACTION',
  12707.         DELETE_RULE         = 'NO ACTION'        
  12708.     from
  12709.         sysobjects o1, sysobjects o2,
  12710.         syscolumns c1, syscolumns c2,
  12711.         sysreferences r
  12712.     where    
  12713.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12714.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12715.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12716.     and    o1.id = r.rkeyid
  12717.     and    r.keycnt >= 15
  12718.     and    o1.id = c1.id
  12719.     and    c1.colid = r.rkey15
  12720.     and     r.fkeyid = o2.id
  12721.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12722.     and    o2.id = c2.id    
  12723.     and    c2.colid = r.fkey15
  12724.     union all
  12725.     select
  12726.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12727.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12728.         PK_TABLE_NAME         = o1.name,
  12729.         PK_COLUMN_NAME         = c1.name,
  12730.         PK_COLUMN_GUID        = convert(binary(16),null),
  12731.         PK_COLUMN_PROPID    = convert(int,null),
  12732.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12733.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  12734.         FK_TABLE_NAME         = o2.name,
  12735.         FK_COLUMN_NAME         = c2.name,
  12736.         FK_COLUMN_GUID        = convert(binary(16),null),
  12737.         FK_COLUMN_PROPID    = convert(int,null),
  12738.         ORDINAL            = convert(int,16),
  12739.         UPDATE_RULE        = 'NO ACTION',
  12740.         DELETE_RULE         = 'NO ACTION'        
  12741.     from
  12742.         sysobjects o1, sysobjects o2,
  12743.         syscolumns c1, syscolumns c2,
  12744.         sysreferences r
  12745.     where    
  12746.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12747.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12748.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12749.     and    o1.id = r.rkeyid
  12750.     and    o1.id = c1.id
  12751.     and    r.keycnt >= 16
  12752.     and    c1.colid = r.rkey16
  12753.     and     r.fkeyid = o2.id
  12754.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12755.     and    o2.id = c2.id    
  12756.     and    c2.colid = r.fkey16
  12757.     order by 8,9,2,3,13
  12758.     END
  12759.  
  12760.     exec @ret = sp_cursoropen @handle output, 'select * from #spfkeysrowset1',
  12761.         @scrollopt output, @ccopt output, @rows output
  12762.  
  12763.     drop table #spfkeysrowset1
  12764.        return isnull(@ret,0)
  12765. go
  12766. dump tran master with no_log
  12767. go
  12768. CREATE PROCEDURE sp_foreign_keys_rowset;3
  12769. as
  12770.     select
  12771.         PK_TABLE_CATALOG    = convert(sysname,' '),
  12772.         PK_TABLE_SCHEMA        = convert(sysname,' '),
  12773.         PK_TABLE_NAME         = convert(sysname,' '),
  12774.         PK_COLUMN_NAME         = convert(sysname,' '),
  12775.         PK_COLUMN_GUID        = convert(binary(16),null),
  12776.         PK_COLUMN_PROPID    = convert(int,null),
  12777.         FK_TABLE_CATALOG    = convert(sysname,' '),
  12778.         FK_TABLE_SCHEMA        = convert(sysname,' '),
  12779.         FK_TABLE_NAME         = convert(sysname,' '),
  12780.         FK_COLUMN_NAME         = convert(sysname,' '),
  12781.         FK_COLUMN_GUID        = convert(binary(16),null),
  12782.         FK_COLUMN_PROPID    = convert(int,null),
  12783.         ORDINAL            = convert(int,1),
  12784.         UPDATE_RULE        = 'NO ACTION',
  12785.         DELETE_RULE         = 'NO ACTION'        
  12786.     where    1=0
  12787. go
  12788. dump tran master with no_log
  12789. go
  12790.  
  12791.  
  12792. if (charindex('7.00', @@version) = 0)
  12793. begin
  12794.     print ''
  12795.     print ''
  12796.     print 'Warning:'
  12797.     print 'you are installing the stored procedures '
  12798.     print 'on a pre 7.0 SQL Server.'
  12799.     print 'Ignore the following errors.'
  12800. end
  12801. else
  12802.     drop proc sp_foreign_keys_rowset
  12803. go
  12804.  
  12805.  
  12806. /*    Procedure for 7.0 server */
  12807. CREATE PROCEDURE sp_foreign_keys_rowset
  12808.     (
  12809.     @pk_table_name        sysname,
  12810.     @pk_table_schema    sysname = null,
  12811.     @fk_table_name        sysname = null,
  12812.     @fk_table_schema    sysname = null,
  12813.     @fk_table_catalog    sysname = null
  12814.     )
  12815. as
  12816.     select
  12817.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12818.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12819.         PK_TABLE_NAME         = o1.name,
  12820.         PK_COLUMN_NAME         = c1.name,
  12821.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  12822.         PK_COLUMN_PROPID    = convert(int,null),
  12823.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12824.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  12825.         FK_TABLE_NAME         = o2.name,
  12826.         FK_COLUMN_NAME         = c2.name,
  12827.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  12828.         FK_COLUMN_PROPID    = convert(int,null),
  12829.         ORDINAL            = convert(int,1),
  12830.         UPDATE_RULE        = N'NO ACTION',
  12831.         DELETE_RULE         = N'NO ACTION'        
  12832.     from
  12833.         sysobjects o1, sysobjects o2,
  12834.         syscolumns c1, syscolumns c2,
  12835.         sysreferences r
  12836.     where    
  12837.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12838.     and    o1.name = @pk_table_name
  12839.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12840.     and    o1.id = r.rkeyid
  12841.     and    o1.id = c1.id
  12842.     and    c1.colid = r.rkey1
  12843.     and     r.fkeyid = o2.id
  12844.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  12845.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12846.     and    o2.id = c2.id    
  12847.     and    c2.colid = r.fkey1
  12848.     union all
  12849.     select
  12850.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12851.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12852.         PK_TABLE_NAME         = o1.name,
  12853.         PK_COLUMN_NAME         = c1.name,
  12854.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  12855.         PK_COLUMN_PROPID    = convert(int,null),
  12856.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12857.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  12858.         FK_TABLE_NAME         = o2.name,
  12859.         FK_COLUMN_NAME         = c2.name,
  12860.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  12861.         FK_COLUMN_PROPID    = convert(int,null),
  12862.         ORDINAL            = convert(int,2),
  12863.         UPDATE_RULE        = N'NO ACTION',
  12864.         DELETE_RULE         = N'NO ACTION'        
  12865.     from
  12866.         sysobjects o1, sysobjects o2,
  12867.         syscolumns c1, syscolumns c2,
  12868.         sysreferences r
  12869.     where    
  12870.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12871.     and    o1.name = @pk_table_name
  12872.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12873.     and    o1.id = r.rkeyid
  12874.     and    o1.id = c1.id
  12875.     and    c1.colid = r.rkey2
  12876.     and    r.keycnt >= 2
  12877.     and     r.fkeyid = o2.id
  12878.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  12879.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12880.     and    o2.id = c2.id    
  12881.     and    c2.colid = r.fkey2
  12882.     union all
  12883.     select
  12884.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12885.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12886.         PK_TABLE_NAME         = o1.name,
  12887.         PK_COLUMN_NAME         = c1.name,
  12888.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  12889.         PK_COLUMN_PROPID    = convert(int,null),
  12890.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12891.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  12892.         FK_TABLE_NAME         = o2.name,
  12893.         FK_COLUMN_NAME         = c2.name,
  12894.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  12895.         FK_COLUMN_PROPID    = convert(int,null),
  12896.         ORDINAL            = convert(int,3),
  12897.         UPDATE_RULE        = N'NO ACTION',
  12898.         DELETE_RULE         = N'NO ACTION'        
  12899.     from
  12900.         sysobjects o1, sysobjects o2,
  12901.         syscolumns c1, syscolumns c2,
  12902.         sysreferences r
  12903.     where    
  12904.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12905.     and    o1.name = @pk_table_name
  12906.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12907.     and    o1.id = r.rkeyid
  12908.     and    o1.id = c1.id
  12909.     and    c1.colid = r.rkey3
  12910.     and    r.keycnt >= 3
  12911.     and     r.fkeyid = o2.id
  12912.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  12913.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12914.     and    o2.id = c2.id    
  12915.     and    c2.colid = r.fkey3
  12916.     union all
  12917.     select
  12918.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12919.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12920.         PK_TABLE_NAME         = o1.name,
  12921.         PK_COLUMN_NAME         = c1.name,
  12922.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  12923.         PK_COLUMN_PROPID    = convert(int,null),
  12924.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12925.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  12926.         FK_TABLE_NAME         = o2.name,
  12927.         FK_COLUMN_NAME         = c2.name,
  12928.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  12929.         FK_COLUMN_PROPID    = convert(int,null),
  12930.         ORDINAL            = convert(int,4),
  12931.         UPDATE_RULE        = N'NO ACTION',
  12932.         DELETE_RULE         = N'NO ACTION'        
  12933.     from
  12934.         sysobjects o1, sysobjects o2,
  12935.         syscolumns c1, syscolumns c2,
  12936.         sysreferences r
  12937.     where    
  12938.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12939.     and    o1.name = @pk_table_name
  12940.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12941.     and    o1.id = r.rkeyid
  12942.     and    r.keycnt >= 4
  12943.     and    o1.id = c1.id
  12944.     and    c1.colid = r.rkey4
  12945.     and     r.fkeyid = o2.id
  12946.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  12947.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12948.     and    o2.id = c2.id    
  12949.     and    c2.colid = r.fkey4
  12950.     union all
  12951.     select
  12952.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12953.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12954.         PK_TABLE_NAME         = o1.name,
  12955.         PK_COLUMN_NAME         = c1.name,
  12956.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  12957.         PK_COLUMN_PROPID    = convert(int,null),
  12958.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12959.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  12960.         FK_TABLE_NAME         = o2.name,
  12961.         FK_COLUMN_NAME         = c2.name,
  12962.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  12963.         FK_COLUMN_PROPID    = convert(int,null),
  12964.         ORDINAL            = convert(int,5),
  12965.         UPDATE_RULE        = N'NO ACTION',
  12966.         DELETE_RULE         = N'NO ACTION'        
  12967.     from
  12968.         sysobjects o1, sysobjects o2,
  12969.         syscolumns c1, syscolumns c2,
  12970.         sysreferences r
  12971.     where    
  12972.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12973.     and    o1.name = @pk_table_name
  12974.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12975.     and    o1.id = r.rkeyid
  12976.     and    r.keycnt >= 5
  12977.     and    o1.id = c1.id
  12978.     and    c1.colid = r.rkey5
  12979.     and     r.fkeyid = o2.id
  12980.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  12981.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12982.     and    o2.id = c2.id    
  12983.     and    c2.colid = r.fkey5
  12984.     union all
  12985.     select
  12986.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12987.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12988.         PK_TABLE_NAME         = o1.name,
  12989.         PK_COLUMN_NAME         = c1.name,
  12990.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  12991.         PK_COLUMN_PROPID    = convert(int,null),
  12992.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12993.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  12994.         FK_TABLE_NAME         = o2.name,
  12995.         FK_COLUMN_NAME         = c2.name,
  12996.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  12997.         FK_COLUMN_PROPID    = convert(int,null),
  12998.         ORDINAL            = convert(int,6),
  12999.         UPDATE_RULE        = N'NO ACTION',
  13000.         DELETE_RULE         = N'NO ACTION'        
  13001.     from
  13002.         sysobjects o1, sysobjects o2,
  13003.         syscolumns c1, syscolumns c2,
  13004.         sysreferences r
  13005.     where    
  13006.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13007.     and    o1.name = @pk_table_name
  13008.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13009.     and    o1.id = r.rkeyid
  13010.     and    r.keycnt >= 6
  13011.     and    o1.id = c1.id
  13012.     and    c1.colid = r.rkey6
  13013.     and     r.fkeyid = o2.id
  13014.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  13015.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13016.     and    o2.id = c2.id    
  13017.     and    c2.colid = r.fkey6
  13018.     union all
  13019.     select
  13020.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  13021.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  13022.         PK_TABLE_NAME         = o1.name,
  13023.         PK_COLUMN_NAME         = c1.name,
  13024.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  13025.         PK_COLUMN_PROPID    = convert(int,null),
  13026.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  13027.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  13028.         FK_TABLE_NAME         = o2.name,
  13029.         FK_COLUMN_NAME         = c2.name,
  13030.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  13031.         FK_COLUMN_PROPID    = convert(int,null),
  13032.         ORDINAL            = convert(int,7),
  13033.         UPDATE_RULE        = N'NO ACTION',
  13034.         DELETE_RULE         = N'NO ACTION'        
  13035.     from
  13036.         sysobjects o1, sysobjects o2,
  13037.         syscolumns c1, syscolumns c2,
  13038.         sysreferences r
  13039.     where    
  13040.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13041.     and    o1.name = @pk_table_name
  13042.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13043.     and    o1.id = r.rkeyid
  13044.     and    r.keycnt >= 7
  13045.     and    o1.id = c1.id
  13046.     and    c1.colid = r.rkey7
  13047.     and     r.fkeyid = o2.id
  13048.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  13049.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13050.     and    o2.id = c2.id    
  13051.     and    c2.colid = r.fkey7
  13052.     union all
  13053.     select
  13054.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  13055.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  13056.         PK_TABLE_NAME         = o1.name,
  13057.         PK_COLUMN_NAME         = c1.name,
  13058.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  13059.         PK_COLUMN_PROPID    = convert(int,null),
  13060.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  13061.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  13062.         FK_TABLE_NAME         = o2.name,
  13063.         FK_COLUMN_NAME         = c2.name,
  13064.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  13065.         FK_COLUMN_PROPID    = convert(int,null),
  13066.         ORDINAL            = convert(int,8),
  13067.         UPDATE_RULE        = N'NO ACTION',
  13068.         DELETE_RULE         = N'NO ACTION'        
  13069.     from
  13070.         sysobjects o1, sysobjects o2,
  13071.         syscolumns c1, syscolumns c2,
  13072.         sysreferences r
  13073.     where    
  13074.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13075.     and    o1.name = @pk_table_name
  13076.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13077.     and    o1.id = r.rkeyid
  13078.     and    r.keycnt >= 8
  13079.     and    o1.id = c1.id
  13080.     and    c1.colid = r.rkey8
  13081.     and     r.fkeyid = o2.id
  13082.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  13083.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13084.     and    o2.id = c2.id    
  13085.     and    c2.colid = r.fkey8
  13086.     union all
  13087.     select
  13088.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  13089.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  13090.         PK_TABLE_NAME         = o1.name,
  13091.         PK_COLUMN_NAME         = c1.name,
  13092.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  13093.         PK_COLUMN_PROPID    = convert(int,null),
  13094.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  13095.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  13096.         FK_TABLE_NAME         = o2.name,
  13097.         FK_COLUMN_NAME         = c2.name,
  13098.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  13099.         FK_COLUMN_PROPID    = convert(int,null),
  13100.         ORDINAL            = convert(int,9),
  13101.         UPDATE_RULE        = N'NO ACTION',
  13102.         DELETE_RULE         = N'NO ACTION'        
  13103.     from
  13104.         sysobjects o1, sysobjects o2,
  13105.         syscolumns c1, syscolumns c2,
  13106.         sysreferences r
  13107.     where    
  13108.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13109.     and    o1.name = @pk_table_name
  13110.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13111.     and    o1.id = r.rkeyid
  13112.     and    r.keycnt >= 9
  13113.     and    o1.id = c1.id
  13114.     and    c1.colid = r.rkey9
  13115.     and     r.fkeyid = o2.id
  13116.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  13117.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13118.     and    o2.id = c2.id    
  13119.     and    c2.colid = r.fkey9
  13120.     union all
  13121.     select
  13122.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  13123.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  13124.         PK_TABLE_NAME         = o1.name,
  13125.         PK_COLUMN_NAME         = c1.name,
  13126.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  13127.         PK_COLUMN_PROPID    = convert(int,null),
  13128.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  13129.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  13130.         FK_TABLE_NAME         = o2.name,
  13131.         FK_COLUMN_NAME         = c2.name,
  13132.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  13133.         FK_COLUMN_PROPID    = convert(int,null),
  13134.         ORDINAL            = convert(int,10),
  13135.         UPDATE_RULE        = N'NO ACTION',
  13136.         DELETE_RULE         = N'NO ACTION'        
  13137.     from
  13138.         sysobjects o1, sysobjects o2,
  13139.         syscolumns c1, syscolumns c2,
  13140.         sysreferences r
  13141.     where    
  13142.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13143.     and    o1.name = @pk_table_name
  13144.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13145.     and    o1.id = r.rkeyid
  13146.     and    r.keycnt >= 10
  13147.     and    o1.id = c1.id
  13148.     and    c1.colid = r.rkey10
  13149.     and     r.fkeyid = o2.id
  13150.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  13151.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13152.     and    o2.id = c2.id    
  13153.     and    c2.colid = r.fkey10
  13154.     union all
  13155.     select
  13156.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  13157.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  13158.         PK_TABLE_NAME         = o1.name,
  13159.         PK_COLUMN_NAME         = c1.name,
  13160.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  13161.         PK_COLUMN_PROPID    = convert(int,null),
  13162.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  13163.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  13164.         FK_TABLE_NAME         = o2.name,
  13165.         FK_COLUMN_NAME         = c2.name,
  13166.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  13167.         FK_COLUMN_PROPID    = convert(int,null),
  13168.         ORDINAL            = convert(int,11),
  13169.         UPDATE_RULE        = N'NO ACTION',
  13170.         DELETE_RULE         = N'NO ACTION'        
  13171.     from
  13172.         sysobjects o1, sysobjects o2,
  13173.         syscolumns c1, syscolumns c2,
  13174.         sysreferences r
  13175.     where    
  13176.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13177.     and    o1.name = @pk_table_name
  13178.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13179.     and    o1.id = r.rkeyid
  13180.     and    r.keycnt >= 11
  13181.     and    o1.id = c1.id
  13182.     and    c1.colid = r.rkey11
  13183.     and     r.fkeyid = o2.id
  13184.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  13185.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13186.     and    o2.id = c2.id    
  13187.     and    c2.colid = r.fkey11
  13188.     union all
  13189.     select
  13190.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  13191.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  13192.         PK_TABLE_NAME         = o1.name,
  13193.         PK_COLUMN_NAME         = c1.name,
  13194.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  13195.         PK_COLUMN_PROPID    = convert(int,null),
  13196.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  13197.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  13198.         FK_TABLE_NAME         = o2.name,
  13199.         FK_COLUMN_NAME         = c2.name,
  13200.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  13201.         FK_COLUMN_PROPID    = convert(int,null),
  13202.         ORDINAL            = convert(int,12),
  13203.         UPDATE_RULE        = N'NO ACTION',
  13204.         DELETE_RULE         = N'NO ACTION'        
  13205.     from
  13206.         sysobjects o1, sysobjects o2,
  13207.         syscolumns c1, syscolumns c2,
  13208.         sysreferences r
  13209.     where    
  13210.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13211.     and    o1.name = @pk_table_name
  13212.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13213.     and    o1.id = r.rkeyid
  13214.     and    r.keycnt >= 12
  13215.     and    o1.id = c1.id
  13216.     and    c1.colid = r.rkey12
  13217.     and     r.fkeyid = o2.id
  13218.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  13219.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13220.     and    o2.id = c2.id    
  13221.     and    c2.colid = r.fkey12
  13222.     union all
  13223.     select
  13224.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  13225.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  13226.         PK_TABLE_NAME         = o1.name,
  13227.         PK_COLUMN_NAME         = c1.name,
  13228.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  13229.         PK_COLUMN_PROPID    = convert(int,null),
  13230.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  13231.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  13232.         FK_TABLE_NAME         = o2.name,
  13233.         FK_COLUMN_NAME         = c2.name,
  13234.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  13235.         FK_COLUMN_PROPID    = convert(int,null),
  13236.         ORDINAL            = convert(int,13),
  13237.         UPDATE_RULE        = N'NO ACTION',
  13238.         DELETE_RULE         = N'NO ACTION'        
  13239.     from
  13240.         sysobjects o1, sysobjects o2,
  13241.         syscolumns c1, syscolumns c2,
  13242.         sysreferences r
  13243.     where    
  13244.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13245.     and    o1.name = @pk_table_name
  13246.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13247.     and    o1.id = r.rkeyid
  13248.     and    r.keycnt >= 13
  13249.     and    o1.id = c1.id
  13250.     and    c1.colid = r.rkey13
  13251.     and     r.fkeyid = o2.id
  13252.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  13253.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13254.     and    o2.id = c2.id    
  13255.     and    c2.colid = r.fkey13
  13256.     union all
  13257.     select
  13258.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  13259.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  13260.         PK_TABLE_NAME         = o1.name,
  13261.         PK_COLUMN_NAME         = c1.name,
  13262.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  13263.         PK_COLUMN_PROPID    = convert(int,null),
  13264.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  13265.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  13266.         FK_TABLE_NAME         = o2.name,
  13267.         FK_COLUMN_NAME         = c2.name,
  13268.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  13269.         FK_COLUMN_PROPID    = convert(int,null),
  13270.         ORDINAL            = convert(int,14),
  13271.         UPDATE_RULE        = N'NO ACTION',
  13272.         DELETE_RULE         = N'NO ACTION'        
  13273.     from
  13274.         sysobjects o1, sysobjects o2,
  13275.         syscolumns c1, syscolumns c2,
  13276.         sysreferences r
  13277.     where    
  13278.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13279.     and    o1.name = @pk_table_name
  13280.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13281.     and    o1.id = r.rkeyid
  13282.     and    r.keycnt >= 14
  13283.     and    o1.id = c1.id
  13284.     and    c1.colid = r.rkey14
  13285.     and     r.fkeyid = o2.id
  13286.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  13287.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13288.     and    o2.id = c2.id    
  13289.     and    c2.colid = r.fkey14
  13290.     union all
  13291.     select
  13292.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  13293.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  13294.         PK_TABLE_NAME         = o1.name,
  13295.         PK_COLUMN_NAME         = c1.name,
  13296.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  13297.         PK_COLUMN_PROPID    = convert(int,null),
  13298.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  13299.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  13300.         FK_TABLE_NAME         = o2.name,
  13301.         FK_COLUMN_NAME         = c2.name,
  13302.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  13303.         FK_COLUMN_PROPID    = convert(int,null),
  13304.         ORDINAL            = convert(int,15),
  13305.         UPDATE_RULE        = N'NO ACTION',
  13306.         DELETE_RULE         = N'NO ACTION'        
  13307.     from
  13308.         sysobjects o1, sysobjects o2,
  13309.         syscolumns c1, syscolumns c2,
  13310.         sysreferences r
  13311.     where    
  13312.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13313.     and    o1.name = @pk_table_name
  13314.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13315.     and    o1.id = r.rkeyid
  13316.     and    r.keycnt >= 15
  13317.     and    o1.id = c1.id
  13318.     and    c1.colid = r.rkey15
  13319.     and     r.fkeyid = o2.id
  13320.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  13321.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13322.     and    o2.id = c2.id    
  13323.     and    c2.colid = r.fkey15
  13324.     union all
  13325.     select
  13326.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  13327.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  13328.         PK_TABLE_NAME         = o1.name,
  13329.         PK_COLUMN_NAME         = c1.name,
  13330.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  13331.         PK_COLUMN_PROPID    = convert(int,null),
  13332.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  13333.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  13334.         FK_TABLE_NAME         = o2.name,
  13335.         FK_COLUMN_NAME         = c2.name,
  13336.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  13337.         FK_COLUMN_PROPID    = convert(int,null),
  13338.         ORDINAL            = convert(int,16),
  13339.         UPDATE_RULE        = N'NO ACTION',
  13340.         DELETE_RULE         = N'NO ACTION'        
  13341.     from
  13342.         sysobjects o1, sysobjects o2,
  13343.         syscolumns c1, syscolumns c2,
  13344.         sysreferences r
  13345.     where    
  13346.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13347.     and    o1.name = @pk_table_name
  13348.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13349.     and    o1.id = r.rkeyid
  13350.     and    r.keycnt >= 16
  13351.     and    o1.id = c1.id
  13352.     and    c1.colid = r.rkey16
  13353.     and     r.fkeyid = o2.id
  13354.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  13355.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13356.     and    o2.id = c2.id    
  13357.     and    c2.colid = r.fkey16
  13358.     order by 8,9,2,3,13
  13359. go
  13360. dump tran master with no_log
  13361. go
  13362. CREATE PROCEDURE sp_foreign_keys_rowset;2
  13363.     (
  13364.     @fk_table_name        sysname,
  13365.     @fk_table_schema    sysname = null,
  13366.     @pk_table_name        sysname = null,
  13367.     @pk_table_schema    sysname = null,
  13368.     @pk_table_catalog    sysname = null
  13369.     )
  13370. as
  13371.     select
  13372.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  13373.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  13374.         PK_TABLE_NAME         = o1.name,
  13375.         PK_COLUMN_NAME         = c1.name,
  13376.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  13377.         PK_COLUMN_PROPID    = convert(int,null),
  13378.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  13379.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  13380.         FK_TABLE_NAME         = o2.name,
  13381.         FK_COLUMN_NAME         = c2.name,
  13382.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  13383.         FK_COLUMN_PROPID    = convert(int,null),
  13384.         ORDINAL            = convert(int,1),
  13385.         UPDATE_RULE        = N'NO ACTION',
  13386.         DELETE_RULE         = N'NO ACTION'        
  13387.     from
  13388.         sysobjects o1, sysobjects o2,
  13389.         syscolumns c1, syscolumns c2,
  13390.         sysreferences r
  13391.     where    
  13392.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13393.     and    o2.name = @fk_table_name
  13394.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13395.     and    o2.id = r.fkeyid
  13396.     and    o2.id = c2.id
  13397.     and    c2.colid = r.fkey1
  13398.     and     r.rkeyid = o1.id
  13399.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  13400.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13401.     and    o1.id = c1.id    
  13402.     and    c1.colid = r.rkey1
  13403.     union    all
  13404.     select
  13405.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  13406.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  13407.         PK_TABLE_NAME         = o1.name,
  13408.         PK_COLUMN_NAME         = c1.name,
  13409.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  13410.         PK_COLUMN_PROPID    = convert(int,null),
  13411.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  13412.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  13413.         FK_TABLE_NAME         = o2.name,
  13414.         FK_COLUMN_NAME         = c2.name,
  13415.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  13416.         FK_COLUMN_PROPID    = convert(int,null),
  13417.         ORDINAL            = convert(int,2),
  13418.         UPDATE_RULE        = N'NO ACTION',
  13419.         DELETE_RULE         = N'NO ACTION'        
  13420.     from
  13421.         sysobjects o1, sysobjects o2,
  13422.         syscolumns c1, syscolumns c2,
  13423.         sysreferences r
  13424.     where    
  13425.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13426.     and    o2.name = @fk_table_name
  13427.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13428.     and    o2.id = r.fkeyid
  13429.     and    r.keycnt >= 2
  13430.     and    o2.id = c2.id
  13431.     and    c2.colid = r.fkey2
  13432.     and     r.rkeyid = o1.id
  13433.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  13434.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13435.     and    o1.id = c1.id    
  13436.     and    c1.colid = r.rkey2
  13437.     union    all
  13438.     select
  13439.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  13440.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  13441.         PK_TABLE_NAME         = o1.name,
  13442.         PK_COLUMN_NAME         = c1.name,
  13443.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  13444.         PK_COLUMN_PROPID    = convert(int,null),
  13445.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  13446.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  13447.         FK_TABLE_NAME         = o2.name,
  13448.         FK_COLUMN_NAME         = c2.name,
  13449.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  13450.         FK_COLUMN_PROPID    = convert(int,null),
  13451.         ORDINAL            = convert(int,3),
  13452.         UPDATE_RULE        = N'NO ACTION',
  13453.         DELETE_RULE         = N'NO ACTION'        
  13454.     from
  13455.         sysobjects o1, sysobjects o2,
  13456.         syscolumns c1, syscolumns c2,
  13457.         sysreferences r
  13458.     where    
  13459.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13460.     and    o2.name = @fk_table_name
  13461.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13462.     and    o2.id = r.fkeyid
  13463.     and    r.keycnt >= 3
  13464.     and    o2.id = c2.id
  13465.     and    c2.colid = r.fkey3
  13466.     and     r.rkeyid = o1.id
  13467.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  13468.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13469.     and    o1.id = c1.id    
  13470.     and    c1.colid = r.rkey3
  13471.     union    all
  13472.     select
  13473.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  13474.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  13475.         PK_TABLE_NAME         = o1.name,
  13476.         PK_COLUMN_NAME         = c1.name,
  13477.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  13478.         PK_COLUMN_PROPID    = convert(int,null),
  13479.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  13480.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  13481.         FK_TABLE_NAME         = o2.name,
  13482.         FK_COLUMN_NAME         = c2.name,
  13483.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  13484.         FK_COLUMN_PROPID    = convert(int,null),
  13485.         ORDINAL            = convert(int,4),
  13486.         UPDATE_RULE        = N'NO ACTION',
  13487.         DELETE_RULE         = N'NO ACTION'        
  13488.     from
  13489.         sysobjects o1, sysobjects o2,
  13490.         syscolumns c1, syscolumns c2,
  13491.         sysreferences r
  13492.     where    
  13493.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13494.     and    o2.name = @fk_table_name
  13495.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13496.     and    o2.id = r.fkeyid
  13497.     and    r.keycnt >= 4
  13498.     and    o2.id = c2.id
  13499.     and    c2.colid = r.fkey4
  13500.     and     r.rkeyid = o1.id
  13501.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  13502.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13503.     and    o1.id = c1.id    
  13504.     and    c1.colid = r.rkey4
  13505.     union    all
  13506.     select
  13507.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  13508.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  13509.         PK_TABLE_NAME         = o1.name,
  13510.         PK_COLUMN_NAME         = c1.name,
  13511.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  13512.         PK_COLUMN_PROPID    = convert(int,null),
  13513.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  13514.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  13515.         FK_TABLE_NAME         = o2.name,
  13516.         FK_COLUMN_NAME         = c2.name,
  13517.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  13518.         FK_COLUMN_PROPID    = convert(int,null),
  13519.         ORDINAL            = convert(int,5),
  13520.         UPDATE_RULE        = N'NO ACTION',
  13521.         DELETE_RULE         = N'NO ACTION'        
  13522.     from
  13523.         sysobjects o1, sysobjects o2,
  13524.         syscolumns c1, syscolumns c2,
  13525.         sysreferences r
  13526.     where    
  13527.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13528.     and    o2.name = @fk_table_name
  13529.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13530.     and    o2.id = r.fkeyid
  13531.     and    r.keycnt >= 5
  13532.     and    o2.id = c2.id
  13533.     and    c2.colid = r.fkey5
  13534.     and     r.rkeyid = o1.id
  13535.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  13536.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13537.     and    o1.id = c1.id    
  13538.     and    c1.colid = r.rkey5
  13539.     union    all
  13540.     select
  13541.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  13542.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  13543.         PK_TABLE_NAME         = o1.name,
  13544.         PK_COLUMN_NAME         = c1.name,
  13545.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  13546.         PK_COLUMN_PROPID    = convert(int,null),
  13547.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  13548.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  13549.         FK_TABLE_NAME         = o2.name,
  13550.         FK_COLUMN_NAME         = c2.name,
  13551.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  13552.         FK_COLUMN_PROPID    = convert(int,null),
  13553.         ORDINAL            = convert(int,6),
  13554.         UPDATE_RULE        = N'NO ACTION',
  13555.         DELETE_RULE         = N'NO ACTION'        
  13556.     from
  13557.         sysobjects o1, sysobjects o2,
  13558.         syscolumns c1, syscolumns c2,
  13559.         sysreferences r
  13560.     where    
  13561.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13562.     and    o2.name = @fk_table_name
  13563.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13564.     and    o2.id = r.fkeyid
  13565.     and    r.keycnt >= 6
  13566.     and    o2.id = c2.id
  13567.     and    c2.colid = r.fkey6
  13568.     and     r.rkeyid = o1.id
  13569.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  13570.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13571.     and    o1.id = c1.id    
  13572.     and    c1.colid = r.rkey6
  13573.     union    all
  13574.     select
  13575.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  13576.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  13577.         PK_TABLE_NAME         = o1.name,
  13578.         PK_COLUMN_NAME         = c1.name,
  13579.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  13580.         PK_COLUMN_PROPID    = convert(int,null),
  13581.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  13582.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  13583.         FK_TABLE_NAME         = o2.name,
  13584.         FK_COLUMN_NAME         = c2.name,
  13585.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  13586.         FK_COLUMN_PROPID    = convert(int,null),
  13587.         ORDINAL            = convert(int,7),
  13588.         UPDATE_RULE        = N'NO ACTION',
  13589.         DELETE_RULE         = N'NO ACTION'        
  13590.     from
  13591.         sysobjects o1, sysobjects o2,
  13592.         syscolumns c1, syscolumns c2,
  13593.         sysreferences r
  13594.     where    
  13595.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13596.     and    o2.name = @fk_table_name
  13597.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13598.     and    o2.id = r.fkeyid
  13599.     and    r.keycnt >= 7
  13600.     and    o2.id = c2.id
  13601.     and    c2.colid = r.fkey7
  13602.     and     r.rkeyid = o1.id
  13603.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  13604.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13605.     and    o1.id = c1.id    
  13606.     and    c1.colid = r.rkey7
  13607.     union    all
  13608.     select
  13609.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  13610.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  13611.         PK_TABLE_NAME         = o1.name,
  13612.         PK_COLUMN_NAME         = c1.name,
  13613.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  13614.         PK_COLUMN_PROPID    = convert(int,null),
  13615.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  13616.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  13617.         FK_TABLE_NAME         = o2.name,
  13618.         FK_COLUMN_NAME         = c2.name,
  13619.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  13620.         FK_COLUMN_PROPID    = convert(int,null),
  13621.         ORDINAL            = convert(int,8),
  13622.         UPDATE_RULE        = N'NO ACTION',
  13623.         DELETE_RULE         = N'NO ACTION'        
  13624.     from
  13625.         sysobjects o1, sysobjects o2,
  13626.         syscolumns c1, syscolumns c2,
  13627.         sysreferences r
  13628.     where    
  13629.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13630.     and    o2.name = @fk_table_name
  13631.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13632.     and    o2.id = r.fkeyid
  13633.     and    r.keycnt >= 8
  13634.     and    o2.id = c2.id
  13635.     and    c2.colid = r.fkey8
  13636.     and     r.rkeyid = o1.id
  13637.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  13638.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13639.     and    o1.id = c1.id    
  13640.     and    c1.colid = r.rkey8
  13641.     union    all
  13642.     select
  13643.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  13644.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  13645.         PK_TABLE_NAME         = o1.name,
  13646.         PK_COLUMN_NAME         = c1.name,
  13647.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  13648.         PK_COLUMN_PROPID    = convert(int,null),
  13649.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  13650.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  13651.         FK_TABLE_NAME         = o2.name,
  13652.         FK_COLUMN_NAME         = c2.name,
  13653.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  13654.         FK_COLUMN_PROPID    = convert(int,null),
  13655.         ORDINAL            = convert(int,9),
  13656.         UPDATE_RULE        = N'NO ACTION',
  13657.         DELETE_RULE         = N'NO ACTION'        
  13658.     from
  13659.         sysobjects o1, sysobjects o2,
  13660.         syscolumns c1, syscolumns c2,
  13661.         sysreferences r
  13662.     where    
  13663.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13664.     and    o2.name = @fk_table_name
  13665.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13666.     and    o2.id = r.fkeyid
  13667.     and    r.keycnt >= 9
  13668.     and    o2.id = c2.id
  13669.     and    c2.colid = r.fkey9
  13670.     and     r.rkeyid = o1.id
  13671.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  13672.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13673.     and    o1.id = c1.id    
  13674.     and    c1.colid = r.rkey9
  13675.     union    all
  13676.     select
  13677.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  13678.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  13679.         PK_TABLE_NAME         = o1.name,
  13680.         PK_COLUMN_NAME         = c1.name,
  13681.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  13682.         PK_COLUMN_PROPID    = convert(int,null),
  13683.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  13684.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  13685.         FK_TABLE_NAME         = o2.name,
  13686.         FK_COLUMN_NAME         = c2.name,
  13687.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  13688.         FK_COLUMN_PROPID    = convert(int,null),
  13689.         ORDINAL            = convert(int,10),
  13690.         UPDATE_RULE        = N'NO ACTION',
  13691.         DELETE_RULE         = N'NO ACTION'        
  13692.     from
  13693.         sysobjects o1, sysobjects o2,
  13694.         syscolumns c1, syscolumns c2,
  13695.         sysreferences r
  13696.     where    
  13697.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13698.     and    o2.name = @fk_table_name
  13699.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13700.     and    o2.id = r.fkeyid
  13701.     and    r.keycnt >= 10
  13702.     and    o2.id = c2.id
  13703.     and    c2.colid = r.fkey10
  13704.     and     r.rkeyid = o1.id
  13705.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  13706.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13707.     and    o1.id = c1.id    
  13708.     and    c1.colid = r.rkey10
  13709.     union    all
  13710.     select
  13711.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  13712.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  13713.         PK_TABLE_NAME         = o1.name,
  13714.         PK_COLUMN_NAME         = c1.name,
  13715.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  13716.         PK_COLUMN_PROPID    = convert(int,null),
  13717.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  13718.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  13719.         FK_TABLE_NAME         = o2.name,
  13720.         FK_COLUMN_NAME         = c2.name,
  13721.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  13722.         FK_COLUMN_PROPID    = convert(int,null),
  13723.         ORDINAL            = convert(int,11),
  13724.         UPDATE_RULE        = N'NO ACTION',
  13725.         DELETE_RULE         = N'NO ACTION'        
  13726.     from
  13727.         sysobjects o1, sysobjects o2,
  13728.         syscolumns c1, syscolumns c2,
  13729.         sysreferences r
  13730.     where    
  13731.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13732.     and    o2.name = @fk_table_name
  13733.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13734.     and    o2.id = r.fkeyid
  13735.     and    r.keycnt >= 11
  13736.     and    o2.id = c2.id
  13737.     and    c2.colid = r.fkey11
  13738.     and     r.rkeyid = o1.id
  13739.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  13740.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13741.     and    o1.id = c1.id    
  13742.     and    c1.colid = r.rkey11
  13743.     union    all
  13744.     select
  13745.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  13746.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  13747.         PK_TABLE_NAME         = o1.name,
  13748.         PK_COLUMN_NAME         = c1.name,
  13749.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  13750.         PK_COLUMN_PROPID    = convert(int,null),
  13751.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  13752.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  13753.         FK_TABLE_NAME         = o2.name,
  13754.         FK_COLUMN_NAME         = c2.name,
  13755.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  13756.         FK_COLUMN_PROPID    = convert(int,null),
  13757.         ORDINAL            = convert(int,12),
  13758.         UPDATE_RULE        = N'NO ACTION',
  13759.         DELETE_RULE         = N'NO ACTION'        
  13760.     from
  13761.         sysobjects o1, sysobjects o2,
  13762.         syscolumns c1, syscolumns c2,
  13763.         sysreferences r
  13764.     where    
  13765.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13766.     and    o2.name = @fk_table_name
  13767.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13768.     and    o2.id = r.fkeyid
  13769.     and    r.keycnt >= 12
  13770.     and    o2.id = c2.id
  13771.     and    c2.colid = r.fkey12
  13772.     and     r.rkeyid = o1.id
  13773.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  13774.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13775.     and    o1.id = c1.id    
  13776.     and    c1.colid = r.rkey12
  13777.     union    all
  13778.     select
  13779.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  13780.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  13781.         PK_TABLE_NAME         = o1.name,
  13782.         PK_COLUMN_NAME         = c1.name,
  13783.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  13784.         PK_COLUMN_PROPID    = convert(int,null),
  13785.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  13786.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  13787.         FK_TABLE_NAME         = o2.name,
  13788.         FK_COLUMN_NAME         = c2.name,
  13789.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  13790.         FK_COLUMN_PROPID    = convert(int,null),
  13791.         ORDINAL            = convert(int,13),
  13792.         UPDATE_RULE        = N'NO ACTION',
  13793.         DELETE_RULE         = N'NO ACTION'        
  13794.     from
  13795.         sysobjects o1, sysobjects o2,
  13796.         syscolumns c1, syscolumns c2,
  13797.         sysreferences r
  13798.     where    
  13799.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13800.     and    o2.name = @fk_table_name
  13801.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13802.     and    o2.id = r.fkeyid
  13803.     and    r.keycnt >= 13
  13804.     and    o2.id = c2.id
  13805.     and    c2.colid = r.fkey13
  13806.     and     r.rkeyid = o1.id
  13807.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  13808.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13809.     and    o1.id = c1.id    
  13810.     and    c1.colid = r.rkey13
  13811.     union    all
  13812.     select
  13813.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  13814.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  13815.         PK_TABLE_NAME         = o1.name,
  13816.         PK_COLUMN_NAME         = c1.name,
  13817.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  13818.         PK_COLUMN_PROPID    = convert(int,null),
  13819.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  13820.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  13821.         FK_TABLE_NAME         = o2.name,
  13822.         FK_COLUMN_NAME         = c2.name,
  13823.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  13824.         FK_COLUMN_PROPID    = convert(int,null),
  13825.         ORDINAL            = convert(int,14),
  13826.         UPDATE_RULE        = N'NO ACTION',
  13827.         DELETE_RULE         = N'NO ACTION'        
  13828.     from
  13829.         sysobjects o1, sysobjects o2,
  13830.         syscolumns c1, syscolumns c2,
  13831.         sysreferences r
  13832.     where    
  13833.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13834.     and    o2.name = @fk_table_name
  13835.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13836.     and    o2.id = r.fkeyid
  13837.     and    r.keycnt >= 14
  13838.     and    o2.id = c2.id
  13839.     and    c2.colid = r.fkey14
  13840.     and     r.rkeyid = o1.id
  13841.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  13842.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13843.     and    o1.id = c1.id    
  13844.     and    c1.colid = r.rkey14
  13845.     union    all
  13846.     select
  13847.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  13848.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  13849.         PK_TABLE_NAME         = o1.name,
  13850.         PK_COLUMN_NAME         = c1.name,
  13851.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  13852.         PK_COLUMN_PROPID    = convert(int,null),
  13853.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  13854.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  13855.         FK_TABLE_NAME         = o2.name,
  13856.         FK_COLUMN_NAME         = c2.name,
  13857.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  13858.         FK_COLUMN_PROPID    = convert(int,null),
  13859.         ORDINAL            = convert(int,15),
  13860.         UPDATE_RULE        = N'NO ACTION',
  13861.         DELETE_RULE         = N'NO ACTION'        
  13862.     from
  13863.         sysobjects o1, sysobjects o2,
  13864.         syscolumns c1, syscolumns c2,
  13865.         sysreferences r
  13866.     where    
  13867.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13868.     and    o2.name = @fk_table_name
  13869.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13870.     and    o2.id = r.fkeyid
  13871.     and    r.keycnt >= 15
  13872.     and    o2.id = c2.id
  13873.     and    c2.colid = r.fkey15
  13874.     and     r.rkeyid = o1.id
  13875.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  13876.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13877.     and    o1.id = c1.id    
  13878.     and    c1.colid = r.rkey15
  13879.     union    all
  13880.     select
  13881.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  13882.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  13883.         PK_TABLE_NAME         = o1.name,
  13884.         PK_COLUMN_NAME         = c1.name,
  13885.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  13886.         PK_COLUMN_PROPID    = convert(int,null),
  13887.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  13888.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  13889.         FK_TABLE_NAME         = o2.name,
  13890.         FK_COLUMN_NAME         = c2.name,
  13891.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  13892.         FK_COLUMN_PROPID    = convert(int,null),
  13893.         ORDINAL            = convert(int,16),
  13894.         UPDATE_RULE        = N'NO ACTION',
  13895.         DELETE_RULE         = N'NO ACTION'        
  13896.     from
  13897.         sysobjects o1, sysobjects o2,
  13898.         syscolumns c1, syscolumns c2,
  13899.         sysreferences r
  13900.     where    
  13901.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13902.     and    o2.name = @fk_table_name
  13903.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13904.     and    o2.id = r.fkeyid
  13905.     and    r.keycnt >= 16
  13906.     and    o2.id = c2.id
  13907.     and    c2.colid = r.fkey16
  13908.     and     r.rkeyid = o1.id
  13909.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  13910.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13911.     and    o1.id = c1.id    
  13912.     and    c1.colid = r.rkey16
  13913.     order by 8,9,2,3,13
  13914. go
  13915. dump tran master with no_log
  13916. go
  13917. CREATE PROCEDURE sp_foreign_keys_rowset;3
  13918.     (
  13919.     @pk_table_schema    sysname = null,
  13920.     @pk_table_catalog    sysname = null,
  13921.     @fk_table_schema    sysname = null,
  13922.     @fk_table_catalog    sysname = null
  13923.     )
  13924. as
  13925.     select
  13926.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  13927.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  13928.         PK_TABLE_NAME         = o1.name,
  13929.         PK_COLUMN_NAME         = c1.name,
  13930.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  13931.         PK_COLUMN_PROPID    = convert(int,null),
  13932.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  13933.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  13934.         FK_TABLE_NAME         = o2.name,
  13935.         FK_COLUMN_NAME         = c2.name,
  13936.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  13937.         FK_COLUMN_PROPID    = convert(int,null),
  13938.         ORDINAL            = convert(int,1),
  13939.         UPDATE_RULE        = N'NO ACTION',
  13940.         DELETE_RULE         = N'NO ACTION'        
  13941.     from
  13942.         sysobjects o1, sysobjects o2,
  13943.         syscolumns c1, syscolumns c2,
  13944.         sysreferences r
  13945.     where    
  13946.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13947.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13948.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13949.     and    o1.id = r.rkeyid
  13950.     and    o1.id = c1.id
  13951.     and    c1.colid = r.rkey1
  13952.     and     r.fkeyid = o2.id
  13953.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13954.     and    o2.id = c2.id    
  13955.     and    c2.colid = r.fkey1
  13956.     union all
  13957.     select
  13958.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  13959.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  13960.         PK_TABLE_NAME         = o1.name,
  13961.         PK_COLUMN_NAME         = c1.name,
  13962.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  13963.         PK_COLUMN_PROPID    = convert(int,null),
  13964.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  13965.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  13966.         FK_TABLE_NAME         = o2.name,
  13967.         FK_COLUMN_NAME         = c2.name,
  13968.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  13969.         FK_COLUMN_PROPID    = convert(int,null),
  13970.         ORDINAL            = convert(int,2),
  13971.         UPDATE_RULE        = N'NO ACTION',
  13972.         DELETE_RULE         = N'NO ACTION'        
  13973.     from
  13974.         sysobjects o1, sysobjects o2,
  13975.         syscolumns c1, syscolumns c2,
  13976.         sysreferences r
  13977.     where    
  13978.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13979.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13980.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13981.     and    o1.id = r.rkeyid
  13982.     and    r.keycnt >= 2
  13983.     and    o1.id = c1.id
  13984.     and    c1.colid = r.rkey2
  13985.     and     r.fkeyid = o2.id
  13986.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13987.     and    o2.id = c2.id    
  13988.     and    c2.colid = r.fkey2
  13989.     union all
  13990.     select
  13991.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  13992.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  13993.         PK_TABLE_NAME         = o1.name,
  13994.         PK_COLUMN_NAME         = c1.name,
  13995.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  13996.         PK_COLUMN_PROPID    = convert(int,null),
  13997.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  13998.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  13999.         FK_TABLE_NAME         = o2.name,
  14000.         FK_COLUMN_NAME         = c2.name,
  14001.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  14002.         FK_COLUMN_PROPID    = convert(int,null),
  14003.         ORDINAL            = convert(int,3),
  14004.         UPDATE_RULE        = N'NO ACTION',
  14005.         DELETE_RULE         = N'NO ACTION'        
  14006.     from
  14007.         sysobjects o1, sysobjects o2,
  14008.         syscolumns c1, syscolumns c2,
  14009.         sysreferences r
  14010.     where    
  14011.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14012.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14013.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14014.     and    o1.id = r.rkeyid
  14015.     and    r.keycnt >= 3
  14016.     and    o1.id = c1.id
  14017.     and    c1.colid = r.rkey3
  14018.     and     r.fkeyid = o2.id
  14019.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14020.     and    o2.id = c2.id    
  14021.     and    c2.colid = r.fkey3
  14022.     union all
  14023.     select
  14024.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14025.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14026.         PK_TABLE_NAME         = o1.name,
  14027.         PK_COLUMN_NAME         = c1.name,
  14028.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  14029.         PK_COLUMN_PROPID    = convert(int,null),
  14030.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14031.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  14032.         FK_TABLE_NAME         = o2.name,
  14033.         FK_COLUMN_NAME         = c2.name,
  14034.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  14035.         FK_COLUMN_PROPID    = convert(int,null),
  14036.         ORDINAL            = convert(int,4),
  14037.         UPDATE_RULE        = N'NO ACTION',
  14038.         DELETE_RULE         = N'NO ACTION'        
  14039.     from
  14040.         sysobjects o1, sysobjects o2,
  14041.         syscolumns c1, syscolumns c2,
  14042.         sysreferences r
  14043.     where    
  14044.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14045.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14046.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14047.     and    o1.id = r.rkeyid
  14048.     and    r.keycnt >= 4
  14049.     and    o1.id = c1.id
  14050.     and    c1.colid = r.rkey4
  14051.     and     r.fkeyid = o2.id
  14052.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14053.     and    o2.id = c2.id    
  14054.     and    c2.colid = r.fkey4
  14055.     union all
  14056.     select
  14057.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14058.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14059.         PK_TABLE_NAME         = o1.name,
  14060.         PK_COLUMN_NAME         = c1.name,
  14061.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  14062.         PK_COLUMN_PROPID    = convert(int,null),
  14063.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14064.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  14065.         FK_TABLE_NAME         = o2.name,
  14066.         FK_COLUMN_NAME         = c2.name,
  14067.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  14068.         FK_COLUMN_PROPID    = convert(int,null),
  14069.         ORDINAL            = convert(int,5),
  14070.         UPDATE_RULE        = N'NO ACTION',
  14071.         DELETE_RULE         = N'NO ACTION'        
  14072.     from
  14073.         sysobjects o1, sysobjects o2,
  14074.         syscolumns c1, syscolumns c2,
  14075.         sysreferences r
  14076.     where    
  14077.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14078.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14079.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14080.     and    o1.id = r.rkeyid
  14081.     and    r.keycnt >= 5
  14082.     and    o1.id = c1.id
  14083.     and    c1.colid = r.rkey5
  14084.     and     r.fkeyid = o2.id
  14085.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14086.     and    o2.id = c2.id    
  14087.     and    c2.colid = r.fkey5
  14088.     union all
  14089.     select
  14090.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14091.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14092.         PK_TABLE_NAME         = o1.name,
  14093.         PK_COLUMN_NAME         = c1.name,
  14094.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  14095.         PK_COLUMN_PROPID    = convert(int,null),
  14096.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14097.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  14098.         FK_TABLE_NAME         = o2.name,
  14099.         FK_COLUMN_NAME         = c2.name,
  14100.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  14101.         FK_COLUMN_PROPID    = convert(int,null),
  14102.         ORDINAL            = convert(int,6),
  14103.         UPDATE_RULE        = N'NO ACTION',
  14104.         DELETE_RULE         = N'NO ACTION'        
  14105.     from
  14106.         sysobjects o1, sysobjects o2,
  14107.         syscolumns c1, syscolumns c2,
  14108.         sysreferences r
  14109.     where    
  14110.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14111.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14112.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14113.     and    o1.id = r.rkeyid
  14114.     and    r.keycnt >= 6
  14115.     and    o1.id = c1.id
  14116.     and    c1.colid = r.rkey6
  14117.     and     r.fkeyid = o2.id
  14118.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14119.     and    o2.id = c2.id    
  14120.     and    c2.colid = r.fkey6
  14121.     union all
  14122.     select
  14123.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14124.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14125.         PK_TABLE_NAME         = o1.name,
  14126.         PK_COLUMN_NAME         = c1.name,
  14127.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  14128.         PK_COLUMN_PROPID    = convert(int,null),
  14129.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14130.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  14131.         FK_TABLE_NAME         = o2.name,
  14132.         FK_COLUMN_NAME         = c2.name,
  14133.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  14134.         FK_COLUMN_PROPID    = convert(int,null),
  14135.         ORDINAL            = convert(int,7),
  14136.         UPDATE_RULE        = N'NO ACTION',
  14137.         DELETE_RULE         = N'NO ACTION'        
  14138.     from
  14139.         sysobjects o1, sysobjects o2,
  14140.         syscolumns c1, syscolumns c2,
  14141.         sysreferences r
  14142.     where    
  14143.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14144.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14145.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14146.     and    o1.id = r.rkeyid
  14147.     and    r.keycnt >= 7
  14148.     and    o1.id = c1.id
  14149.     and    c1.colid = r.rkey7
  14150.     and     r.fkeyid = o2.id
  14151.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14152.     and    o2.id = c2.id    
  14153.     and    c2.colid = r.fkey7
  14154.     union all
  14155.     select
  14156.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14157.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14158.         PK_TABLE_NAME         = o1.name,
  14159.         PK_COLUMN_NAME         = c1.name,
  14160.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  14161.         PK_COLUMN_PROPID    = convert(int,null),
  14162.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14163.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  14164.         FK_TABLE_NAME         = o2.name,
  14165.         FK_COLUMN_NAME         = c2.name,
  14166.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  14167.         FK_COLUMN_PROPID    = convert(int,null),
  14168.         ORDINAL            = convert(int,8),
  14169.         UPDATE_RULE        = N'NO ACTION',
  14170.         DELETE_RULE         = N'NO ACTION'        
  14171.     from
  14172.         sysobjects o1, sysobjects o2,
  14173.         syscolumns c1, syscolumns c2,
  14174.         sysreferences r
  14175.     where    
  14176.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14177.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14178.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14179.     and    o1.id = r.rkeyid
  14180.     and    r.keycnt >= 8
  14181.     and    o1.id = c1.id
  14182.     and    c1.colid = r.rkey8
  14183.     and     r.fkeyid = o2.id
  14184.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14185.     and    o2.id = c2.id    
  14186.     and    c2.colid = r.fkey8
  14187.     union all
  14188.     select
  14189.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14190.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14191.         PK_TABLE_NAME         = o1.name,
  14192.         PK_COLUMN_NAME         = c1.name,
  14193.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  14194.         PK_COLUMN_PROPID    = convert(int,null),
  14195.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14196.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  14197.         FK_TABLE_NAME         = o2.name,
  14198.         FK_COLUMN_NAME         = c2.name,
  14199.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  14200.         FK_COLUMN_PROPID    = convert(int,null),
  14201.         ORDINAL            = convert(int,9),
  14202.         UPDATE_RULE        = N'NO ACTION',
  14203.         DELETE_RULE         = N'NO ACTION'        
  14204.     from
  14205.         sysobjects o1, sysobjects o2,
  14206.         syscolumns c1, syscolumns c2,
  14207.         sysreferences r
  14208.     where    
  14209.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14210.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14211.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14212.     and    o1.id = r.rkeyid
  14213.     and    r.keycnt >= 9
  14214.     and    o1.id = c1.id
  14215.     and    c1.colid = r.rkey9
  14216.     and     r.fkeyid = o2.id
  14217.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14218.     and    o2.id = c2.id    
  14219.     and    c2.colid = r.fkey9
  14220.     union all
  14221.     select
  14222.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14223.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14224.         PK_TABLE_NAME         = o1.name,
  14225.         PK_COLUMN_NAME         = c1.name,
  14226.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  14227.         PK_COLUMN_PROPID    = convert(int,null),
  14228.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14229.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  14230.         FK_TABLE_NAME         = o2.name,
  14231.         FK_COLUMN_NAME         = c2.name,
  14232.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  14233.         FK_COLUMN_PROPID    = convert(int,null),
  14234.         ORDINAL            = convert(int,10),
  14235.         UPDATE_RULE        = N'NO ACTION',
  14236.         DELETE_RULE         = N'NO ACTION'        
  14237.     from
  14238.         sysobjects o1, sysobjects o2,
  14239.         syscolumns c1, syscolumns c2,
  14240.         sysreferences r
  14241.     where    
  14242.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14243.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14244.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14245.     and    o1.id = r.rkeyid
  14246.     and    r.keycnt >= 10
  14247.     and    o1.id = c1.id
  14248.     and    c1.colid = r.rkey10
  14249.     and     r.fkeyid = o2.id
  14250.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14251.     and    o2.id = c2.id    
  14252.     and    c2.colid = r.fkey10
  14253.     union all
  14254.     select
  14255.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14256.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14257.         PK_TABLE_NAME         = o1.name,
  14258.         PK_COLUMN_NAME         = c1.name,
  14259.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  14260.         PK_COLUMN_PROPID    = convert(int,null),
  14261.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14262.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  14263.         FK_TABLE_NAME         = o2.name,
  14264.         FK_COLUMN_NAME         = c2.name,
  14265.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  14266.         FK_COLUMN_PROPID    = convert(int,null),
  14267.         ORDINAL            = convert(int,11),
  14268.         UPDATE_RULE        = N'NO ACTION',
  14269.         DELETE_RULE         = N'NO ACTION'        
  14270.     from
  14271.         sysobjects o1, sysobjects o2,
  14272.         syscolumns c1, syscolumns c2,
  14273.         sysreferences r
  14274.     where    
  14275.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14276.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14277.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14278.     and    o1.id = r.rkeyid
  14279.     and    r.keycnt >= 11
  14280.     and    o1.id = c1.id
  14281.     and    c1.colid = r.rkey11
  14282.     and     r.fkeyid = o2.id
  14283.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14284.     and    o2.id = c2.id    
  14285.     and    c2.colid = r.fkey11
  14286.     union all
  14287.     select
  14288.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14289.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14290.         PK_TABLE_NAME         = o1.name,
  14291.         PK_COLUMN_NAME         = c1.name,
  14292.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  14293.         PK_COLUMN_PROPID    = convert(int,null),
  14294.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14295.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  14296.         FK_TABLE_NAME         = o2.name,
  14297.         FK_COLUMN_NAME         = c2.name,
  14298.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  14299.         FK_COLUMN_PROPID    = convert(int,null),
  14300.         ORDINAL            = convert(int,12),
  14301.         UPDATE_RULE        = N'NO ACTION',
  14302.         DELETE_RULE         = N'NO ACTION'        
  14303.     from
  14304.         sysobjects o1, sysobjects o2,
  14305.         syscolumns c1, syscolumns c2,
  14306.         sysreferences r
  14307.     where    
  14308.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14309.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14310.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14311.     and    o1.id = r.rkeyid
  14312.     and    r.keycnt >= 12
  14313.     and    o1.id = c1.id
  14314.     and    c1.colid = r.rkey12
  14315.     and     r.fkeyid = o2.id
  14316.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14317.     and    o2.id = c2.id    
  14318.     and    c2.colid = r.fkey12
  14319.     union all
  14320.     select
  14321.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14322.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14323.         PK_TABLE_NAME         = o1.name,
  14324.         PK_COLUMN_NAME         = c1.name,
  14325.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  14326.         PK_COLUMN_PROPID    = convert(int,null),
  14327.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14328.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  14329.         FK_TABLE_NAME         = o2.name,
  14330.         FK_COLUMN_NAME         = c2.name,
  14331.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  14332.         FK_COLUMN_PROPID    = convert(int,null),
  14333.         ORDINAL            = convert(int,13),
  14334.         UPDATE_RULE        = N'NO ACTION',
  14335.         DELETE_RULE         = N'NO ACTION'        
  14336.     from
  14337.         sysobjects o1, sysobjects o2,
  14338.         syscolumns c1, syscolumns c2,
  14339.         sysreferences r
  14340.     where    
  14341.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14342.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14343.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14344.     and    o1.id = r.rkeyid
  14345.     and    r.keycnt >= 13
  14346.     and    o1.id = c1.id
  14347.     and    c1.colid = r.rkey13
  14348.     and     r.fkeyid = o2.id
  14349.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14350.     and    o2.id = c2.id    
  14351.     and    c2.colid = r.fkey13
  14352.     union all
  14353.     select
  14354.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14355.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14356.         PK_TABLE_NAME         = o1.name,
  14357.         PK_COLUMN_NAME         = c1.name,
  14358.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  14359.         PK_COLUMN_PROPID    = convert(int,null),
  14360.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14361.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  14362.         FK_TABLE_NAME         = o2.name,
  14363.         FK_COLUMN_NAME         = c2.name,
  14364.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  14365.         FK_COLUMN_PROPID    = convert(int,null),
  14366.         ORDINAL            = convert(int,14),
  14367.         UPDATE_RULE        = N'NO ACTION',
  14368.         DELETE_RULE         = N'NO ACTION'        
  14369.     from
  14370.         sysobjects o1, sysobjects o2,
  14371.         syscolumns c1, syscolumns c2,
  14372.         sysreferences r
  14373.     where    
  14374.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14375.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14376.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14377.     and    o1.id = r.rkeyid
  14378.     and    r.keycnt >= 14
  14379.     and    o1.id = c1.id
  14380.     and    c1.colid = r.rkey14
  14381.     and     r.fkeyid = o2.id
  14382.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14383.     and    o2.id = c2.id    
  14384.     and    c2.colid = r.fkey14
  14385.     union all
  14386.     select
  14387.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14388.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14389.         PK_TABLE_NAME         = o1.name,
  14390.         PK_COLUMN_NAME         = c1.name,
  14391.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  14392.         PK_COLUMN_PROPID    = convert(int,null),
  14393.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14394.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  14395.         FK_TABLE_NAME         = o2.name,
  14396.         FK_COLUMN_NAME         = c2.name,
  14397.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  14398.         FK_COLUMN_PROPID    = convert(int,null),
  14399.         ORDINAL            = convert(int,15),
  14400.         UPDATE_RULE        = N'NO ACTION',
  14401.         DELETE_RULE         = N'NO ACTION'        
  14402.     from
  14403.         sysobjects o1, sysobjects o2,
  14404.         syscolumns c1, syscolumns c2,
  14405.         sysreferences r
  14406.     where    
  14407.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14408.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14409.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14410.     and    o1.id = r.rkeyid
  14411.     and    r.keycnt >= 15
  14412.     and    o1.id = c1.id
  14413.     and    c1.colid = r.rkey15
  14414.     and     r.fkeyid = o2.id
  14415.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14416.     and    o2.id = c2.id    
  14417.     and    c2.colid = r.fkey15
  14418.     union all
  14419.     select
  14420.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14421.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14422.         PK_TABLE_NAME         = o1.name,
  14423.         PK_COLUMN_NAME         = c1.name,
  14424.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  14425.         PK_COLUMN_PROPID    = convert(int,null),
  14426.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14427.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  14428.         FK_TABLE_NAME         = o2.name,
  14429.         FK_COLUMN_NAME         = c2.name,
  14430.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  14431.         FK_COLUMN_PROPID    = convert(int,null),
  14432.         ORDINAL            = convert(int,16),
  14433.         UPDATE_RULE        = N'NO ACTION',
  14434.         DELETE_RULE         = N'NO ACTION'        
  14435.     from
  14436.         sysobjects o1, sysobjects o2,
  14437.         syscolumns c1, syscolumns c2,
  14438.         sysreferences r
  14439.     where    
  14440.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14441.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14442.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14443.     and    o1.id = r.rkeyid
  14444.     and    o1.id = c1.id
  14445.     and    r.keycnt >= 16
  14446.     and    c1.colid = r.rkey16
  14447.     and     r.fkeyid = o2.id
  14448.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14449.     and    o2.id = c2.id    
  14450.     and    c2.colid = r.fkey16
  14451.     order by 8,9,2,3,13
  14452. go
  14453. dump tran master with no_log
  14454. go
  14455. create procedure sp_foreign_keys_rowset;5
  14456.     (
  14457.     @server_name        sysname,
  14458.     @pk_catalog_name    sysname = null,
  14459.     @fk_catalog_name    sysname = null,
  14460.     @pk_table_name        sysname = null,
  14461.     @pk_table_schema    sysname = null,
  14462.     @fk_table_name        sysname = null,
  14463.     @fk_table_schema    sysname = null
  14464.     )
  14465. as
  14466.     select
  14467.         PK_TABLE_CATALOG,
  14468.         PK_TABLE_SCHEMA,    
  14469.         PK_TABLE_NAME,     
  14470.         PK_COLUMN_NAME,     
  14471.         PK_COLUMN_GUID,    
  14472.         PK_COLUMN_PROPID,
  14473.         FK_TABLE_CATALOG,
  14474.         FK_TABLE_SCHEMA, 
  14475.         FK_TABLE_NAME,     
  14476.         FK_COLUMN_NAME,     
  14477.         FK_COLUMN_GUID,    
  14478.         FK_COLUMN_PROPID,
  14479.         ORDINAL,        
  14480.         UPDATE_RULE,    
  14481.         DELETE_RULE     
  14482.     from master.dbo.SYSREMOTE_FOREIGN_KEYS <
  14483.                 @server_name,
  14484.                 @pk_catalog_name,
  14485.                 @pk_table_schema,
  14486.                 @pk_table_name,
  14487.                 @fk_catalog_name,
  14488.                 @fk_table_schema,
  14489.                 @fk_table_name >
  14490.     order by 7,8,9,1,2,3,13
  14491. go
  14492.  
  14493. grant execute on sp_foreign_keys_rowset to public
  14494. go
  14495.  
  14496. dump tran master with no_log
  14497. go
  14498. if (charindex('6.00', @@version) > 0)
  14499.     begin
  14500.     if (exists (select * from sysobjects
  14501.             where name = 'sp_foreign_keys_rowset' and type = 'P '))
  14502.         begin
  14503.         drop procedure sp_foreign_keys_rowset
  14504.         dump tran master with no_log
  14505.         end
  14506.     end
  14507. go
  14508.  
  14509.  
  14510. print ''
  14511. print 'creating sp_indexes_rowset'
  14512. go
  14513.  
  14514. /*    6.0 and 6.5 version */
  14515. create procedure sp_indexes_rowset
  14516.     (
  14517.     @table_name    varchar(255), 
  14518.     @index_name    varchar(255) = null,
  14519.     @table_schema    varchar(255) = null     
  14520.     )
  14521. as
  14522.     select    TABLE_CATALOG        = db_name(),
  14523.         TABLE_SCHEMA        = user_name(o.uid),
  14524.         TABLE_NAME        = o.name,
  14525.         INDEX_CATALOG        = db_name(),        
  14526.         INDEX_SCHEMA        = user_name(o.uid),
  14527.         INDEX_NAME        = x.name,
  14528.         PRIMARY_KEY        = convert(bit,(x.status & 0x800)/0x800),
  14529.         "UNIQUE"        = convert(bit,(x.status & 2)/2),
  14530.         "CLUSTERED"        = convert(bit,(x.status & 16)/16),
  14531.         "TYPE"            = convert(smallint, 1 /*DBPROPVAL_IT_BTREE*/),
  14532.         FILL_FACTOR        = convert(int, x.OrigFillFactor),
  14533.         INITIAL_SIZE        = convert(int,null),
  14534.         NULLS            = convert(int,null),
  14535.         SORT_BOOKMARKS        = convert(bit,0),
  14536.         AUTO_UPDATE        = convert(bit,1),
  14537.         NULL_COLLATION        = convert(int,4 /*DBPROPVAL_NC_LOW*/),
  14538.         ORDINAL_POSITION    = convert(int,c.colid),
  14539.         COLUMN_NAME        = index_col(user_name(o.uid)+'.'+o.name, x.indid, c.colid),
  14540.         COLUMN_GUID        = convert(binary(16),null),
  14541.         COLUMN_PROPID        = convert(int,null),
  14542.         COLLATION        = convert(smallint, 1 /*DB_COLLATION_ASC*/),
  14543.         CARDINALITY        = case when (x.status & 2) = 2 then x.rows else null end,
  14544.         PAGES            = convert(int, x.dpages),
  14545.         FILTER_CONDITION    = convert(varchar(1),null),
  14546.         INTEGRATED        = convert(bit,(x.status & 16)/16)
  14547.         
  14548.     from    sysobjects o, sysindexes x, syscolumns c, sysusers u
  14549.     where    o.type in ('U')
  14550.     and     o.name = @table_name
  14551.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  14552.     and    (@index_name is null or @index_name = x.name)
  14553.     and     x.id = o.id
  14554.     and     x.id = c.id
  14555.     and      c.colid < x.keycnt+(x.status&16)/16
  14556.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  14557.     and     (
  14558.         suser_id() = 1     /* User is the System Administrator */
  14559.         or o.uid = user_id()     /* User created the object */
  14560.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  14561.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  14562.             from sysprotects p
  14563.             where p.id = o.id
  14564.             /* get rows for public,current user,user's group */
  14565.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  14566.             /* check for SELECT,EXECUTE privilege */
  14567.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  14568.             ) = 1    /* final magic...compare Grants      */
  14569.            )
  14570.     order by 8 desc, 4, 5, 6, 17
  14571. go
  14572. dump tran master with no_log
  14573. go
  14574. create procedure sp_indexes_rowset;2
  14575.     (
  14576.     @index_name    varchar(255) = null,
  14577.     @table_schema    varchar(255) = null     
  14578.     )
  14579. as
  14580.     select    TABLE_CATALOG        = db_name(),
  14581.         TABLE_SCHEMA        = user_name(o.uid),
  14582.         TABLE_NAME        = o.name,
  14583.         INDEX_CATALOG        = db_name(),        
  14584.         INDEX_SCHEMA        = user_name(o.uid),
  14585.         INDEX_NAME        = x.name,
  14586.         PRIMARY_KEY        = convert(bit,(x.status & 0x800)/0x800),
  14587.         "UNIQUE"        = convert(bit,(x.status & 2)/2),
  14588.         "CLUSTERED"        = convert(bit,(x.status & 16)/16),
  14589.         "TYPE"            = convert(smallint, 1 /*DBPROPVAL_IT_BTREE*/),
  14590.         FILL_FACTOR        = convert(int, x.OrigFillFactor),
  14591.         INITIAL_SIZE        = convert(int,null),
  14592.         NULLS            = convert(int,null),
  14593.         SORT_BOOKMARKS        = convert(bit,0),
  14594.         AUTO_UPDATE        = convert(bit,1),
  14595.         NULL_COLLATION        = convert(int,4 /*DBPROPVAL_NC_LOW*/),
  14596.         ORDINAL_POSITION    = convert(int,c.colid),
  14597.         COLUMN_NAME        = index_col(user_name(o.uid)+'.'+o.name, x.indid, c.colid),
  14598.         COLUMN_GUID        = convert(binary(16),null),
  14599.         COLUMN_PROPID        = convert(int,null),
  14600.         COLLATION        = convert(smallint, 1 /*DB_COLLATION_ASC*/),
  14601.         CARDINALITY        = case when (x.status & 2) = 2 then x.rows else null end,
  14602.         PAGES            = convert(int, x.dpages),
  14603.         FILTER_CONDITION    = convert(varchar(1),null),
  14604.         INTEGRATED        = convert(bit,(x.status & 16)/16) 
  14605.         
  14606.     from    sysobjects o, sysindexes x, syscolumns c, sysusers u
  14607.     where    o.type in ('U')
  14608.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  14609.     and    (@index_name is null or @index_name = x.name)
  14610.     and     x.id = o.id
  14611.     and     x.id = c.id
  14612.     and      c.colid < x.keycnt+(x.status&16)/16
  14613.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  14614.     and     (
  14615.         suser_id() = 1     /* User is the System Administrator */
  14616.         or o.uid = user_id()     /* User created the object */
  14617.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  14618.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  14619.             from sysprotects p
  14620.             where p.id = o.id
  14621.             /* get rows for public,current user,user's group */
  14622.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  14623.             /* check for SELECT,EXECUTE privilege */
  14624.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  14625.             ) = 1    /* final magic...compare Grants      */
  14626.            )
  14627.     order by 8 desc, 4, 5, 6, 17
  14628. go
  14629. dump tran master with no_log
  14630. go
  14631.  
  14632.  
  14633. if (charindex('7.00', @@version) > 0)
  14634.     drop procedure sp_indexes_rowset
  14635. else
  14636. begin
  14637.     print ''
  14638.     print ''
  14639.     print 'Warning:'
  14640.     print 'you are installing the stored procedures '
  14641.     print 'on a pre 7.0 SQL Server.'
  14642.     print 'Ignore the following errors.'
  14643. end
  14644. go
  14645.  
  14646. /*     7.0 version */
  14647. create procedure sp_indexes_rowset
  14648.     (
  14649.     @table_name    sysname, 
  14650.     @index_name    sysname = null,
  14651.     @table_schema    sysname = null     
  14652.     )
  14653. as
  14654.     select    TABLE_CATALOG        = db_name(),
  14655.         TABLE_SCHEMA        = user_name(o.uid),
  14656.         TABLE_NAME        = o.name,
  14657.         INDEX_CATALOG        = db_name(),        
  14658.         INDEX_SCHEMA        = user_name(o.uid),
  14659.         INDEX_NAME        = x.name,
  14660.         PRIMARY_KEY        = convert(bit,(x.status & 0x800)/0x800),
  14661.         "UNIQUE"        = convert(bit,(x.status & 2)/2),
  14662.         "CLUSTERED"        = convert(bit,(x.status & 16)/16),
  14663.         "TYPE"            = convert(smallint, 1 /*DBPROPVAL_IT_BTREE*/),
  14664.         FILL_FACTOR        = convert(int, x.OrigFillFactor),
  14665.         INITIAL_SIZE        = convert(int,null),
  14666.         NULLS            = convert(int,null),
  14667.         SORT_BOOKMARKS        = convert(bit,0),
  14668.         AUTO_UPDATE        = convert(bit,1),
  14669.         NULL_COLLATION        = convert(int,4 /*DBPROPVAL_NC_LOW*/),
  14670.         ORDINAL_POSITION    = convert(int,c.colid),
  14671.         COLUMN_NAME        = index_col(user_name(o.uid)+'.'+o.name, x.indid, c.colid),
  14672.         COLUMN_GUID        = convert(uniqueidentifier,null),
  14673.         COLUMN_PROPID        = convert(int,null),
  14674.         COLLATION        = convert(smallint, 1 /*DB_COLLATION_ASC*/),
  14675.         CARDINALITY        = case when (x.status & 2) = 2 then x.rows else null end,
  14676.         PAGES            = convert(int, x.dpages),
  14677.         FILTER_CONDITION    = convert(nvarchar(1),null),
  14678.         INTEGRATED        = convert(bit,(x.status & 16)/16) 
  14679.         
  14680.     from    sysobjects o, sysindexes x, syscolumns c
  14681.     where    o.type in ('U')
  14682.     and     o.name = @table_name
  14683.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  14684.     and    (@index_name is null or @index_name = x.name)
  14685.     and     x.id = o.id
  14686.     and     x.id = c.id
  14687.     and      c.colid < x.keycnt+(x.status&16)/16
  14688.     and    index_col(user_name(o.uid)+'.'+o.name, x.indid, c.colid) is not null
  14689.     and     permissions(o.id) <> 0
  14690.     order by 8 desc, 4, 5, 6, 17
  14691. go
  14692. dump tran master with no_log
  14693. go
  14694. create procedure sp_indexes_rowset;2
  14695.     (
  14696.     @index_name    sysname = null,
  14697.     @table_schema    sysname = null     
  14698.     )
  14699. as
  14700.     select    TABLE_CATALOG        = db_name(),
  14701.         TABLE_SCHEMA        = user_name(o.uid),
  14702.         TABLE_NAME        = o.name,
  14703.         INDEX_CATALOG        = db_name(),        
  14704.         INDEX_SCHEMA        = user_name(o.uid),
  14705.         INDEX_NAME        = x.name,
  14706.         PRIMARY_KEY        = convert(bit,(x.status & 0x800)/0x800),
  14707.         "UNIQUE"        = convert(bit,(x.status & 2)/2),
  14708.         "CLUSTERED"        = convert(bit,(x.status & 16)/16),
  14709.         "TYPE"            = convert(smallint, 1 /*DBPROPVAL_IT_BTREE*/),
  14710.         FILL_FACTOR        = convert(int, x.OrigFillFactor),
  14711.         INITIAL_SIZE        = convert(int,null),
  14712.         NULLS            = convert(int,null),
  14713.         SORT_BOOKMARKS        = convert(bit,0),
  14714.         AUTO_UPDATE        = convert(bit,1),
  14715.         NULL_COLLATION        = convert(int,4 /*DBPROPVAL_NC_LOW*/),
  14716.         ORDINAL_POSITION    = convert(int,c.colid),
  14717.         COLUMN_NAME        = index_col(user_name(o.uid)+'.'+o.name, x.indid, c.colid),
  14718.         COLUMN_GUID        = convert(uniqueidentifier,null),
  14719.         COLUMN_PROPID        = convert(int,null),
  14720.         COLLATION        = convert(smallint, 1 /*DB_COLLATION_ASC*/),
  14721.         CARDINALITY        = case when (x.status & 2) = 2 then x.rows else null end,
  14722.         PAGES            = convert(int, x.dpages),
  14723.         FILTER_CONDITION    = convert(nvarchar(1),null),
  14724.         INTEGRATED        = convert(bit,(x.status & 16)/16) 
  14725.         
  14726.     from    sysobjects o, sysindexes x, syscolumns c
  14727.     where    o.type in ('U')
  14728.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  14729.     and    (@index_name is null or @index_name = x.name)
  14730.     and     x.id = o.id
  14731.     and     x.id = c.id
  14732.     and      c.colid < x.keycnt+(x.status&16)/16
  14733.     and    index_col(user_name(o.uid)+'.'+o.name, x.indid, c.colid) is not null
  14734.     and     permissions(o.id) <> 0
  14735.     order by 8 desc, 4, 5, 6, 17
  14736. go
  14737. dump tran master with no_log
  14738. go
  14739. create procedure sp_indexes_rowset;5
  14740.     (
  14741.     @table_server        sysname,
  14742.     @table_catalog        sysname = null,
  14743.     @table_name        sysname = null,
  14744.     @index_name        sysname = null,
  14745.     @table_schema        sysname = null
  14746.     )
  14747. as
  14748.     select
  14749.         TABLE_CATALOG,    
  14750.         TABLE_SCHEMA,    
  14751.         TABLE_NAME,    
  14752.         INDEX_CATALOG,        
  14753.         INDEX_SCHEMA,    
  14754.         INDEX_NAME,    
  14755.         PRIMARY_KEY,    
  14756.         "UNIQUE",    
  14757.         "CLUSTERED",    
  14758.         "TYPE",        
  14759.         FILL_FACTOR,    
  14760.         INITIAL_SIZE,    
  14761.         NULLS,        
  14762.         SORT_BOOKMARKS,    
  14763.         AUTO_UPDATE,    
  14764.         NULL_COLLATION,    
  14765.         ORDINAL_POSITION,
  14766.         COLUMN_NAME,    
  14767.         COLUMN_GUID,    
  14768.         COLUMN_PROPID,    
  14769.         COLLATION,    
  14770.         CARDINALITY,    
  14771.         PAGES,        
  14772.         FILTER_CONDITION
  14773.     --    INTEGRATED
  14774.     from master.dbo.SYSREMOTE_INDEXES <
  14775.                 @table_server,
  14776.                 @table_catalog,
  14777.                 @table_schema,
  14778.                         @index_name,
  14779.                 NULL,            /* TYPE (index type) */
  14780.                 @table_name >
  14781.     order by 8 desc, 4, 5, 6, 17
  14782. go
  14783.  
  14784. grant execute on sp_indexes_rowset to public
  14785. go
  14786.  
  14787. dump tran master with no_log
  14788. go
  14789. if (charindex('6.00', @@version) > 0)
  14790.     begin
  14791.     if (exists (select * from sysobjects
  14792.             where name = 'sp_indexes_rowset' and type = 'P '))
  14793.         begin
  14794.         drop procedure sp_indexes_rowset
  14795.         dump tran master with no_log
  14796.         end
  14797.     end
  14798. go
  14799.  
  14800.  
  14801. print ''
  14802. print 'creating sp_primary_keys_rowset'
  14803. go
  14804.  
  14805. /*    Procedure for 6.0 and 6.5 servers */
  14806. create procedure sp_primary_keys_rowset
  14807.     (
  14808.        @table_name        varchar(255),
  14809.     @table_schema         varchar(244) = null
  14810.     )
  14811. as
  14812.     select    TABLE_CATALOG    = db_name(),
  14813.         TABLE_SCHEMA    = user_name(o.uid),
  14814.         TABLE_NAME    = o.name,    
  14815.         COLUMN_NAME    = c.name,
  14816.         COLUMN_GUID    = convert(binary(16),null),
  14817.         COLUMN_PROPID    = convert(int,null),
  14818.         ORDINAL        = convert(int,c1.colid)
  14819.     from    sysindexes i, syscolumns c, sysobjects o, syscolumns c1, sysusers u
  14820.     where    o.type in ('U')
  14821.     and     o.name = @table_name
  14822.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  14823.     and     o.id = c.id
  14824.     and     o.id = i.id
  14825.     and     (i.status & 0x800) = 0x800
  14826.     and     c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, c1.colid)
  14827.     and     c1.id = c.id
  14828.     and     c1.colid <= i.keycnt    /* create rows from 1 to keycnt */
  14829.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  14830.     and     (
  14831.         suser_id() = 1     /* User is the System Administrator */
  14832.         or o.uid = user_id()     /* User created the object */
  14833.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  14834.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  14835.             from sysprotects p
  14836.             where p.id = o.id
  14837.             /* get rows for public,current user,user's group */
  14838.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  14839.             /* check for SELECT,EXECUTE privilege */
  14840.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  14841.             ) = 1    /* final magic...compare Grants      */
  14842.            )
  14843.     order by 2, 3
  14844. go
  14845. dump tran master with no_log
  14846. go
  14847. create procedure sp_primary_keys_rowset;2
  14848.     (
  14849.      @table_schema         varchar(244) = null
  14850.     )
  14851. as
  14852.     select    TABLE_CATALOG    = db_name(),
  14853.         TABLE_SCHEMA    = user_name(o.uid),
  14854.         TABLE_NAME    = o.name,    
  14855.         COLUMN_NAME    = c.name,
  14856.         COLUMN_GUID    = convert(binary(16),null),
  14857.         COLUMN_PROPID    = convert(int,null),
  14858.         ORDINAL        = convert(int,c1.colid)
  14859.     from    sysindexes i, syscolumns c, sysobjects o, syscolumns c1, sysusers u
  14860.     where    o.type in ('U')
  14861.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  14862.     and     o.id = c.id
  14863.     and     o.id = i.id
  14864.     and     (i.status & 0x800) = 0x800
  14865.     and     c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, c1.colid)
  14866.     and     c1.id = c.id
  14867.     and     c1.colid <= i.keycnt    /* create rows from 1 to keycnt */
  14868.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  14869.     and     (
  14870.         suser_id() = 1     /* User is the System Administrator */
  14871.         or o.uid = user_id()     /* User created the object */
  14872.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  14873.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  14874.             from sysprotects p
  14875.             where p.id = o.id
  14876.             /* get rows for public,current user,user's group */
  14877.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  14878.             /* check for SELECT,EXECUTE privilege */
  14879.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  14880.             ) = 1    /* final magic...compare Grants      */
  14881.            )
  14882.     order by 2, 3
  14883. go
  14884.  
  14885. if (charindex('7.00', @@version) = 0)
  14886. begin
  14887.     print ''
  14888.     print ''
  14889.     print 'Warning:'
  14890.     print 'you are installing the stored procedures '
  14891.     print 'on a pre 7.0 SQL Server.'
  14892.     print 'Ignore the following errors.'
  14893. end
  14894. else
  14895.     drop proc sp_primary_keys_rowset
  14896. go
  14897.  
  14898. /*    Procedure for 7.0 servers */
  14899. create procedure sp_primary_keys_rowset
  14900.     (
  14901.        @table_name        sysname,
  14902.     @table_schema         sysname = null
  14903.     )
  14904. as
  14905.     select    TABLE_CATALOG    = db_name(),
  14906.         TABLE_SCHEMA    = user_name(o.uid),
  14907.         TABLE_NAME    = o.name,    
  14908.         COLUMN_NAME    = c.name,
  14909.         COLUMN_GUID    = convert(uniqueidentifier,null),
  14910.         COLUMN_PROPID    = convert(int,null),
  14911.         ORDINAL        = convert(int,c1.colid)
  14912.     from    sysindexes i, syscolumns c, sysobjects o, syscolumns c1
  14913.     where    o.type in ('U')
  14914.     and     o.name = @table_name
  14915.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  14916.     and     o.id = c.id
  14917.     and     o.id = i.id
  14918.     and     (i.status & 0x800) = 0x800
  14919.     and     c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, c1.colid)
  14920.     and     c1.id = c.id
  14921.     and     c1.colid <= i.keycnt    /* create rows from 1 to keycnt */
  14922.     and    permissions(o.id) <> 0
  14923.     order by 2, 3
  14924. go
  14925. dump tran master with no_log
  14926. go
  14927. create procedure sp_primary_keys_rowset;2
  14928.     (
  14929.      @table_schema     sysname = null
  14930.     )
  14931. as
  14932.     select    TABLE_CATALOG    = db_name(),
  14933.         TABLE_SCHEMA    = user_name(o.uid),
  14934.         TABLE_NAME    = o.name,    
  14935.         COLUMN_NAME    = c.name,
  14936.         COLUMN_GUID    = convert(uniqueidentifier,null),
  14937.         COLUMN_PROPID    = convert(int,null),
  14938.         ORDINAL        = convert(int,c1.colid)
  14939.     from    sysindexes i, syscolumns c, sysobjects o, syscolumns c1
  14940.     where    o.type in ('U')
  14941.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  14942.     and     o.id = c.id
  14943.     and     o.id = i.id
  14944.     and     (i.status & 0x800) = 0x800
  14945.     and     c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, c1.colid)
  14946.     and     c1.id = c.id
  14947.     and     c1.colid <= i.keycnt    /* create rows from 1 to keycnt */
  14948.     and    permissions(o.id) <> 0
  14949.     order by 2, 3
  14950. go
  14951. dump tran master with no_log
  14952. go
  14953. create procedure sp_primary_keys_rowset;3
  14954.     (
  14955.        @table_name        sysname = null,
  14956.     @table_schema         sysname = null
  14957.     )
  14958. as
  14959. IF @table_name is not NULL
  14960.     BEGIN
  14961.     select    TABLE_CATALOG    = db_name(),
  14962.         TABLE_SCHEMA    = user_name(o.uid),
  14963.         TABLE_NAME    = o.name,    
  14964.         COLUMN_NAME    = c.name,
  14965.         COLUMN_GUID    = convert(uniqueidentifier,null),
  14966.         COLUMN_PROPID    = convert(int,null),
  14967.         ORDINAL        = convert(int,c1.colid)
  14968.     from    sysindexes i, syscolumns c, sysobjects o, syscolumns c1
  14969.     where    o.type in ('U')
  14970.     and     o.name = @table_name
  14971.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  14972.     and     o.id = c.id
  14973.     and     o.id = i.id
  14974.     and     (i.status & 0x800) = 0x800
  14975.     and     c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, c1.colid)
  14976.     and     c1.id = c.id
  14977.     and     c1.colid <= i.keycnt    /* create rows from 1 to keycnt */
  14978.     and    permissions(o.id) <> 0
  14979.     END
  14980. ELSE
  14981.     BEGIN
  14982.     select    TABLE_CATALOG    = db_name(),
  14983.         TABLE_SCHEMA    = user_name(o.uid),
  14984.         TABLE_NAME    = o.name,    
  14985.         COLUMN_NAME    = c.name,
  14986.         COLUMN_GUID    = convert(uniqueidentifier,null),
  14987.         COLUMN_PROPID    = convert(int,null),
  14988.         ORDINAL        = convert(int,c1.colid)
  14989.     from    sysindexes i, syscolumns c, sysobjects o, syscolumns c1
  14990.     where    o.type in ('U')
  14991.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  14992.     and     o.id = c.id
  14993.     and     o.id = i.id
  14994.     and     (i.status & 0x800) = 0x800
  14995.     and     c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, c1.colid)
  14996.     and     c1.id = c.id
  14997.     and     c1.colid <= i.keycnt    /* create rows from 1 to keycnt */
  14998.     and    permissions(o.id) <> 0
  14999.     END
  15000. go
  15001. dump tran master with no_log
  15002. go
  15003. create procedure sp_primary_keys_rowset;5
  15004.     (
  15005.     @table_server        sysname,
  15006.     @table_catalog        sysname = null,
  15007.     @table_name        sysname = null,
  15008.     @table_schema        sysname = null
  15009.     )
  15010. as
  15011.     select
  15012.         TABLE_CATALOG,    
  15013.         TABLE_SCHEMA,    
  15014.         TABLE_NAME,    
  15015.         COLUMN_NAME,    
  15016.         COLUMN_GUID,    
  15017.         COLUMN_PROPID,    
  15018.         ORDINAL        
  15019.     from master.dbo.SYSREMOTE_PRIMARY_KEYS <
  15020.                 @table_server,
  15021.                 @table_catalog,
  15022.                 @table_schema,
  15023.                 @table_name >
  15024.     order by 1,2,3
  15025. go
  15026.  
  15027. grant execute on sp_primary_keys_rowset to public
  15028. go
  15029.  
  15030. dump tran master with no_log
  15031. go
  15032. if (charindex('6.00', @@version) > 0)
  15033.     begin
  15034.     if (exists (select * from sysobjects
  15035.             where name = 'sp_primary_keys_rowset' and type = 'P '))
  15036.         begin
  15037.         drop procedure sp_primary_keys_rowset
  15038.         dump tran master with no_log
  15039.         end
  15040.     end
  15041. go
  15042.  
  15043.  
  15044. print ''
  15045. print 'creating sp_provider_types_rowset'
  15046. go
  15047.  
  15048. /*    Procedure for 6.0 and 6.50 servers */
  15049. create proc sp_provider_types_rowset
  15050.     (
  15051.     @data_type     smallint = null,
  15052.     @best_match    tinyint  = null
  15053.     )
  15054. as
  15055.     select
  15056.         TYPE_NAME         = case    when t.usertype = 80 then t.name 
  15057.                         else d.type_name 
  15058.                         end,
  15059.         DATA_TYPE         = d.oledb_data_type,                            
  15060.         COLUMN_SIZE        = coalesce(d.column_size,d.data_precision,t.prec),
  15061.         LITERAL_PREFIX         = d.literal_prefix,
  15062.         LITERAL_SUFFIX         = d.literal_suffix,
  15063.         CREATE_PARAMS         = convert(varchar(32),e.CREATE_PARAMS),
  15064.         IS_NULLABLE        = t.allownulls,
  15065.         CASE_SENSITIVE        = d.case_sensitive,
  15066.         SEARCHABLE         = d.searchable,
  15067.         UNSIGNED_ATTRIBUTE    = d.unsigned_attribute,
  15068.         FIXED_PREC_SCALE    = d.fixed_prec_scale,
  15069.         AUTO_UNIQUE_VALUE    = d.auto_unique_value, 
  15070.         LOCAL_TYPE_NAME        = case    when t.usertype = 80 then t.name 
  15071.                         else d.local_type_name
  15072.                         end,
  15073.         MINIMUM_SCALE        = convert(smallint,
  15074.                         case 
  15075.                         when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then 0 
  15076.                         else null 
  15077.                         end),
  15078.         MAXIMUM_SCALE        = convert(smallint,
  15079.                         case 
  15080.                         when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ 
  15081.                         then d.data_precision 
  15082.                         else null 
  15083.                         end),
  15084.         GUID            = convert(binary(16),null),
  15085.         TYPELIB            = convert(varchar(1),null),
  15086.         VERSION            = convert(varchar(1),null),
  15087.         IS_LONG            = d.is_long,
  15088.         BEST_MATCH        = case when t.usertype = 80
  15089.                         then convert(bit,0)
  15090.                         else d.best_match
  15091.                         end,
  15092.         IS_FIXEDLENGTH        = convert(bit, 
  15093.                         case when d.fixlen is null then 0 else 1 end)
  15094.     from     master.dbo.spt_provider_types d, master.dbo.spt_datatype_info_ext e, systypes t
  15095.     where    d.ss_dtype = t.type 
  15096.     and    t.usertype <= 100
  15097.     and    t.usertype <> 18 /* sysname */
  15098.     and    (case when t.usertype = 80 /* TIMESTAMP */ then 1 else 0 end 
  15099.             = case when d.type_name = 'timestamp' then 1 else 0 end)
  15100.     and     t.usertype *= e.user_type 
  15101.     and     e.AUTO_INCREMENT = 0
  15102.     and     t.type not in (111,109,38,110,55,63)    /* get rid of nullable types */
  15103.     and    (@data_type is null or d.oledb_data_type = @data_type)
  15104.     and    (@best_match is null or d.best_match = @best_match)
  15105.     order by 2
  15106. go
  15107. dump tran master with no_log
  15108. go
  15109.  
  15110. if (charindex('7.00', @@version) = 0)
  15111. begin
  15112.     print ''
  15113.     print ''
  15114.     print 'Warning:'
  15115.     print 'you are installing the stored procedures '
  15116.     print 'on a pre 7.0 SQL Server.'
  15117.     print 'Ignore the following errors.'
  15118. end
  15119. else
  15120.     drop proc sp_provider_types_rowset
  15121. go
  15122.  
  15123. /*    Procedure for 7.0 server */
  15124. create proc sp_provider_types_rowset
  15125.     (
  15126.     @data_type     smallint = null,
  15127.     @best_match    tinyint  = null
  15128.     )
  15129. as
  15130.     select
  15131.         TYPE_NAME         = case    when t.usertype = 80 then t.name 
  15132.                         else d.type_name 
  15133.                         end,
  15134.         DATA_TYPE         = d.oledb_data_type,                            
  15135.         COLUMN_SIZE        = coalesce(d.column_size,d.data_precision,t.prec),
  15136.         LITERAL_PREFIX         = d.literal_prefix,
  15137.         LITERAL_SUFFIX         = d.literal_suffix,
  15138.         CREATE_PARAMS         = convert(nvarchar(32),e.CREATE_PARAMS),
  15139.         IS_NULLABLE        = t.allownulls,
  15140.         CASE_SENSITIVE        = d.case_sensitive,
  15141.         SEARCHABLE         = d.searchable,
  15142.         UNSIGNED_ATTRIBUTE    = d.unsigned_attribute,
  15143.         FIXED_PREC_SCALE    = d.fixed_prec_scale,
  15144.         AUTO_UNIQUE_VALUE    = d.auto_unique_value, 
  15145.         LOCAL_TYPE_NAME        = case    when t.usertype = 80 then t.name 
  15146.                         else d.local_type_name
  15147.                         end,
  15148.         MINIMUM_SCALE        = convert(smallint,
  15149.                         case 
  15150.                         when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then 0 
  15151.                         else null 
  15152.                         end),
  15153.         MAXIMUM_SCALE        = convert(smallint,
  15154.                         case 
  15155.                         when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ 
  15156.                         then d.data_precision 
  15157.                         else null 
  15158.                         end),
  15159.         GUID            = convert(uniqueidentifier,null),
  15160.         TYPELIB            = convert(nvarchar(1),null),
  15161.         VERSION            = convert(nvarchar(1),null),
  15162.         IS_LONG            = d.is_long,
  15163.         BEST_MATCH        = case    when t.usertype = 80
  15164.                         then convert(bit,0)
  15165.                         else d.best_match
  15166.                         end,
  15167.         IS_FIXEDLENGTH        = convert(bit, 
  15168.                         case when d.fixlen is null then 0 else 1 end)
  15169.  
  15170.     from master.dbo.spt_provider_types d
  15171.         INNER JOIN model.dbo.systypes t on d.ss_dtype = t.xtype
  15172.         LEFT OUTER JOIN master.dbo.spt_datatype_info_ext e on
  15173.             t.xusertype = e.user_type
  15174.             and e.AUTO_INCREMENT = 0
  15175.     where    (@data_type is null or d.oledb_data_type = @data_type)
  15176.     and    (@best_match is null or d.best_match = @best_match)    
  15177.     and    t.usertype <= 255
  15178.     and    t.usertype <> 18 /* sysname */
  15179.     order by 2
  15180. go
  15181.  
  15182. grant execute on sp_provider_types_rowset to public
  15183. go
  15184.  
  15185. dump tran master with no_log
  15186. go
  15187. if (charindex('6.00', @@version) > 0)
  15188.     begin
  15189.     if (exists (select * from sysobjects
  15190.         where name = 'sp_provider_types_rowset' and type = 'P '))
  15191.         begin
  15192.         drop procedure sp_provider_types_rowset
  15193.         dump tran master with no_log
  15194.         end
  15195.     end
  15196. go
  15197.  
  15198.  
  15199. print ''
  15200. print 'creating sp_procedure_params_rowset'
  15201. go
  15202.  
  15203. /*    Procedure for 6.0 and 6.50 servers */
  15204. create procedure sp_procedure_params_rowset
  15205.     (
  15206.        @procedure_name        varchar(255) = null,
  15207.     @group_number        int = null,
  15208.     @procedure_schema     varchar(255) = null,
  15209.     @parameter_name        varchar(255) = null
  15210.     )
  15211. as
  15212. IF @procedure_name is not null
  15213.     BEGIN
  15214.     select
  15215.         PROCEDURE_CATALOG     = db_name(),
  15216.         PROCEDURE_SCHEMA     = user_name(),
  15217.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  15218.         PARAMETER_NAME         = c.name,
  15219.         ORDINAL_POSITION     = convert(smallint,c.colid),
  15220.         PARAMETER_TYPE         = convert(smallint, 1+((c.status/64)&1)),
  15221.         PARAMETER_HASDEFAULT    = convert(tinyint, null),
  15222.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  15223.         IS_NULLABLE        = convert(bit,
  15224.                         case when d.oledb_data_type = 11 /*DBTYPE_BOOL*/ 
  15225.                         then 0 else 1
  15226.                         end),
  15227.         DATA_TYPE        = d.oledb_data_type,
  15228.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  15229.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  15230.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  15231.                         then coalesce(d.column_size,c.length)
  15232.                         else null 
  15233.                         end),
  15234.         CHARACTER_OCTET_LENGTH    = convert(int,
  15235.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  15236.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  15237.                         then coalesce(d.column_size,c.length)
  15238.                         else null 
  15239.                         end),
  15240.         NUMERIC_PRECISION    = convert(smallint,
  15241.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec else null end),
  15242.         NUMERIC_SCALE        = convert(smallint, 
  15243.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  15244.         DESCRIPTION        = convert(varchar(1),null),
  15245.         TYPE_NAME        = d.type_name,
  15246.         LOCAL_TYPE_NAME        = d.local_type_name
  15247.     
  15248.     from
  15249.         syscolumns c,
  15250.         sysobjects o,
  15251.         master.dbo.spt_provider_types d,
  15252.         systypes t
  15253.     where
  15254.         o.name = @procedure_name
  15255.     and    o.type = 'P'                            /* Just Procedures */
  15256.     and    (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  15257.     and    o.id = c.id
  15258.     and    c.number = @group_number
  15259.     and    c.type = d.ss_dtype
  15260.     and    c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  15261.     and    c.usertype = t.usertype
  15262.     and    (@parameter_name is null or @parameter_name = c.name)
  15263.     UNION ALL
  15264.     SELECT           /* return value row*/
  15265.         PROCEDURE_CATALOG     = db_name(),
  15266.         PROCEDURE_SCHEMA     = user_name(),
  15267.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  15268.         PARAMETER_NAME         = convert(sysname,'RETURN_VALUE'),
  15269.         ORDINAL_POSITION     = convert(smallint,0),
  15270.         PARAMETER_TYPE         = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  15271.         PARAMETER_HASDEFAULT    = convert(tinyint,null),
  15272.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  15273.         IS_NULLABLE        = convert(bit,0),
  15274.         DATA_TYPE        = convert(smallint, 3 /*DBTYPE_I4*/),
  15275.         CHARACTER_MAXIMUM_LENGTH= convert(int,null),
  15276.         CHARACTER_OCTET_LENGTH    = convert(int,null),
  15277.         NUMERIC_PRECISION    = convert(smallint,null),
  15278.         NUMERIC_SCALE        = convert(smallint,null),
  15279.         DESCRIPTION        = convert(varchar(1),null),
  15280.         TYPE_NAME        = convert(sysname,'int'),
  15281.         LOCAL_TYPE_NAME        = convert(sysname,'int')
  15282.     from
  15283.         syscomments c,
  15284.         sysobjects o
  15285.     where
  15286.         o.name = @procedure_name
  15287.     and    o.type = 'P'                        /* Just Procedures */
  15288.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  15289.     and    (@parameter_name is null or @parameter_name = 'RETURN_VALUE')
  15290.     and    c.id = o.id
  15291.     and     c.colid = 1
  15292.     order by 2, 3, 5
  15293.     END
  15294. ELSE
  15295.     BEGIN
  15296.     select
  15297.         PROCEDURE_CATALOG     = db_name(),
  15298.         PROCEDURE_SCHEMA     = user_name(),
  15299.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  15300.         PARAMETER_NAME         = c.name,
  15301.         ORDINAL_POSITION     = convert(smallint,c.colid),
  15302.         PARAMETER_TYPE         = convert(smallint, 1+((c.status/64)&1)),
  15303.         PARAMETER_HASDEFAULT    = convert(tinyint,null),
  15304.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  15305.         IS_NULLABLE        = convert(bit,
  15306.                         case when d.oledb_data_type = 11 /*DBTYPE_BOOL*/ 
  15307.                         then 0 else 1
  15308.                         end),
  15309.         DATA_TYPE        = d.oledb_data_type,
  15310.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  15311.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  15312.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  15313.                         then coalesce(d.column_size,c.length)
  15314.                         else null 
  15315.                         end),
  15316.         CHARACTER_OCTET_LENGTH    = convert(int,
  15317.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  15318.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  15319.                         then coalesce(d.column_size,c.length)
  15320.                         else null 
  15321.                         end),
  15322.         NUMERIC_PRECISION    = convert(smallint,
  15323.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec else null end),
  15324.         NUMERIC_SCALE        = convert(smallint, 
  15325.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  15326.         DESCRIPTION        = convert(varchar(1),null),
  15327.         TYPE_NAME        = d.type_name,
  15328.         LOCAL_TYPE_NAME        = d.local_type_name
  15329.     
  15330.     from
  15331.         syscolumns c,
  15332.         sysobjects o,
  15333.         master.dbo.spt_provider_types d,
  15334.         systypes t
  15335.     where
  15336.         o.type = 'P'                            /* Just Procedures */
  15337.     and    (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  15338.     and    o.id = c.id
  15339.     and    c.type = d.ss_dtype
  15340.     and    c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  15341.     and    c.usertype = t.usertype
  15342.     and    (@parameter_name is null or @parameter_name = c.name)
  15343.     UNION ALL
  15344.     SELECT           /* return value row*/
  15345.         PROCEDURE_CATALOG     = db_name(),
  15346.         PROCEDURE_SCHEMA     = user_name(),
  15347.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  15348.         PARAMETER_NAME         = convert(sysname,'RETURN_VALUE'),
  15349.         ORDINAL_POSITION     = convert(smallint,0),
  15350.         PARAMETER_TYPE         = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  15351.         PARAMETER_HASDEFAULT    = convert(tinyint,null),
  15352.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  15353.         IS_NULLABLE        = convert(bit,0),
  15354.         DATA_TYPE        = convert(smallint, 3 /*DBTYPE_I4*/),
  15355.         CHARACTER_MAXIMUM_LENGTH= convert(int,null),
  15356.         CHARACTER_OCTET_LENGTH    = convert(int,null),
  15357.         NUMERIC_PRECISION    = convert(smallint,null),
  15358.         NUMERIC_SCALE        = convert(smallint,null),
  15359.         DESCRIPTION        = convert(varchar(1),null),
  15360.         TYPE_NAME        = convert(sysname,'int'),
  15361.         LOCAL_TYPE_NAME        = convert(sysname,'int')
  15362.     from
  15363.         syscomments c,
  15364.         sysobjects o
  15365.     where
  15366.         o.type = 'P'                        /* Just Procedures */
  15367.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  15368.     and    (@parameter_name is null or @parameter_name = 'RETURN_VALUE')
  15369.     and    c.id = o.id
  15370.     and     c.colid = 1
  15371.     order by 2, 3, 5
  15372.     END
  15373. go
  15374. dump tran master with no_log
  15375. go
  15376. create procedure sp_procedure_params_rowset;2
  15377.     (
  15378.       @handle            int output,
  15379.        @scrollopt        int output,
  15380.     @ccopt            int output,
  15381.     @rows            int output,
  15382.        @procedure_name        varchar(255) = null,
  15383.     @group_number        int = null,
  15384.     @procedure_schema     varchar(255) = null,
  15385.     @parameter_name        varchar(255) = null
  15386.     )
  15387. as
  15388.     declare @ret int
  15389.  
  15390.  
  15391. SET NOCOUNT ON
  15392.  
  15393.     create table #spprocparamrowset1
  15394.         (
  15395.         PROCEDURE_CATALOG     sysname not null,
  15396.         PROCEDURE_SCHEMA     sysname not null,
  15397.         PROCEDURE_NAME         varchar(35) not null,
  15398.         PARAMETER_NAME         sysname not null,
  15399.         ORDINAL_POSITION     smallint not null,
  15400.         PARAMETER_TYPE         smallint null,
  15401.         PARAMETER_HASDEFAULT    tinyint null,
  15402.         PARAMETER_DEFAULT    varchar(255) null,
  15403.         IS_NULLABLE        bit not null,
  15404.         DATA_TYPE        smallint null,
  15405.         CHARACTER_MAXIMUM_LENGTH int null,
  15406.         CHARACTER_OCTET_LENGTH    int null,
  15407.         NUMERIC_PRECISION    smallint null,
  15408.         NUMERIC_SCALE        smallint null,
  15409.         DESCRIPTION        varchar(1) null,
  15410.         TYPE_NAME        sysname null,
  15411.         LOCAL_TYPE_NAME        sysname null,
  15412.         )
  15413.  
  15414. IF @procedure_name is not null
  15415.     BEGIN
  15416.     insert into #spprocparamrowset1
  15417.     select
  15418.         PROCEDURE_CATALOG     = db_name(),
  15419.         PROCEDURE_SCHEMA     = user_name(),
  15420.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  15421.         PARAMETER_NAME         = c.name,
  15422.         ORDINAL_POSITION     = convert(smallint,c.colid),
  15423.         PARAMETER_TYPE         = convert(smallint, 1+((c.status/64)&1)),
  15424.         PARAMETER_HASDEFAULT    = convert(tinyint, null),
  15425.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  15426.         IS_NULLABLE        = convert(bit,
  15427.                         case when d.oledb_data_type = 11 /*DBTYPE_BOOL*/ 
  15428.                         then 0 else 1
  15429.                         end),
  15430.         DATA_TYPE        = d.oledb_data_type,
  15431.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  15432.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  15433.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  15434.                         then coalesce(d.column_size,c.length)
  15435.                         else null 
  15436.                         end),
  15437.         CHARACTER_OCTET_LENGTH    = convert(int,
  15438.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  15439.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  15440.                         then coalesce(d.column_size,c.length)
  15441.                         else null 
  15442.                         end),
  15443.         NUMERIC_PRECISION    = convert(smallint,
  15444.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec else null end),
  15445.         NUMERIC_SCALE        = convert(smallint, 
  15446.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  15447.         DESCRIPTION        = convert(varchar(1),null),
  15448.         TYPE_NAME        = d.type_name,
  15449.         LOCAL_TYPE_NAME        = d.local_type_name
  15450.     
  15451.     from
  15452.         syscolumns c,
  15453.         sysobjects o,
  15454.         master.dbo.spt_provider_types d,
  15455.         systypes t
  15456.     where
  15457.         o.name = @procedure_name
  15458.     and    o.type = 'P'                            /* Just Procedures */
  15459.     and    (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  15460.     and    o.id = c.id
  15461.     and    c.number = @group_number
  15462.     and    c.type = d.ss_dtype
  15463.     and    c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  15464.     and    c.usertype = t.usertype
  15465.     and    (@parameter_name is null or @parameter_name = c.name)
  15466.     UNION ALL
  15467.     SELECT           /* return value row*/
  15468.         PROCEDURE_CATALOG     = db_name(),
  15469.         PROCEDURE_SCHEMA     = user_name(),
  15470.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  15471.         PARAMETER_NAME         = convert(sysname,'RETURN_VALUE'),
  15472.         ORDINAL_POSITION     = convert(smallint,0),
  15473.         PARAMETER_TYPE         = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  15474.         PARAMETER_HASDEFAULT    = convert(tinyint,null),
  15475.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  15476.         IS_NULLABLE        = convert(bit,0),
  15477.         DATA_TYPE        = convert(smallint, 3 /*DBTYPE_I4*/),
  15478.         CHARACTER_MAXIMUM_LENGTH= convert(int,null),
  15479.         CHARACTER_OCTET_LENGTH    = convert(int,null),
  15480.         NUMERIC_PRECISION    = convert(smallint,null),
  15481.         NUMERIC_SCALE        = convert(smallint,null),
  15482.         DESCRIPTION        = convert(varchar(1),null),
  15483.         TYPE_NAME        = convert(sysname,'int'),
  15484.         LOCAL_TYPE_NAME        = convert(sysname,'int')
  15485.     from
  15486.         syscomments c,
  15487.         sysobjects o
  15488.     where
  15489.         o.name = @procedure_name
  15490.     and    o.type = 'P'                        /* Just Procedures */
  15491.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  15492.     and    (@parameter_name is null or @parameter_name = 'RETURN_VALUE')
  15493.     and    c.id = o.id
  15494.     and     c.colid = 1
  15495.     order by 2, 3, 5
  15496.     END
  15497. ELSE
  15498.     BEGIN
  15499.     insert into #spprocparamrowset1
  15500.     select
  15501.         PROCEDURE_CATALOG     = db_name(),
  15502.         PROCEDURE_SCHEMA     = user_name(),
  15503.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  15504.         PARAMETER_NAME         = c.name,
  15505.         ORDINAL_POSITION     = convert(smallint,c.colid),
  15506.         PARAMETER_TYPE         = convert(smallint, 1+((c.status/64)&1)),
  15507.         PARAMETER_HASDEFAULT    = convert(tinyint,null),
  15508.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  15509.         IS_NULLABLE        = convert(bit,
  15510.                         case when d.oledb_data_type = 11 /*DBTYPE_BOOL*/ 
  15511.                         then 0 else 1
  15512.                         end),
  15513.         DATA_TYPE        = d.oledb_data_type,
  15514.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  15515.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  15516.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  15517.                         then coalesce(d.column_size,c.length)
  15518.                         else null 
  15519.                         end),
  15520.         CHARACTER_OCTET_LENGTH    = convert(int,
  15521.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  15522.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  15523.                         then coalesce(d.column_size,c.length)
  15524.                         else null 
  15525.                         end),
  15526.         NUMERIC_PRECISION    = convert(smallint,
  15527.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec else null end),
  15528.         NUMERIC_SCALE        = convert(smallint, 
  15529.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  15530.         DESCRIPTION        = convert(varchar(1),null),
  15531.         TYPE_NAME        = d.type_name,
  15532.         LOCAL_TYPE_NAME        = d.local_type_name
  15533.     
  15534.     from
  15535.         syscolumns c,
  15536.         sysobjects o,
  15537.         master.dbo.spt_provider_types d,
  15538.         systypes t
  15539.     where
  15540.         o.type = 'P'                            /* Just Procedures */
  15541.     and    (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  15542.     and    o.id = c.id
  15543.     and    c.type = d.ss_dtype
  15544.     and    c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  15545.     and    c.usertype = t.usertype
  15546.     and    (@parameter_name is null or @parameter_name = c.name)
  15547.     UNION ALL
  15548.     SELECT           /* return value row*/
  15549.         PROCEDURE_CATALOG     = db_name(),
  15550.         PROCEDURE_SCHEMA     = user_name(),
  15551.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  15552.         PARAMETER_NAME         = convert(sysname,'RETURN_VALUE'),
  15553.         ORDINAL_POSITION     = convert(smallint,0),
  15554.         PARAMETER_TYPE         = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  15555.         PARAMETER_HASDEFAULT    = convert(tinyint,null),
  15556.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  15557.         IS_NULLABLE        = convert(bit,0),
  15558.         DATA_TYPE        = convert(smallint, 3 /*DBTYPE_I4*/),
  15559.         CHARACTER_MAXIMUM_LENGTH= convert(int,null),
  15560.         CHARACTER_OCTET_LENGTH    = convert(int,null),
  15561.         NUMERIC_PRECISION    = convert(smallint,null),
  15562.         NUMERIC_SCALE        = convert(smallint,null),
  15563.         DESCRIPTION        = convert(varchar(1),null),
  15564.         TYPE_NAME        = convert(sysname,'int'),
  15565.         LOCAL_TYPE_NAME        = convert(sysname,'int')
  15566.     from
  15567.         syscomments c,
  15568.         sysobjects o
  15569.     where
  15570.         o.type = 'P'                        /* Just Procedures */
  15571.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  15572.     and    (@parameter_name is null or @parameter_name = 'RETURN_VALUE')
  15573.     and    c.id = o.id
  15574.     and     c.colid = 1
  15575.     order by 2, 3, 5
  15576.     END
  15577.  
  15578.     exec @ret = sp_cursoropen @handle output, 'select * from #spprocparamrowset1',
  15579.         @scrollopt output, @ccopt output, @rows output
  15580.  
  15581.     drop table #spprocparamrowset1
  15582.        return isnull(@ret,0)
  15583. go
  15584. dump tran master with no_log
  15585. go
  15586. create procedure sp_procedure_params_rowset;3
  15587. as
  15588.     select
  15589.         PROCEDURE_CATALOG     = convert(sysname, ' '),
  15590.         PROCEDURE_SCHEMA     = convert(sysname, ' '),
  15591.         PROCEDURE_NAME         = convert(varchar(35), ' '),
  15592.         PARAMETER_NAME         = convert(sysname, ' '),
  15593.         ORDINAL_POSITION     = convert(smallint, 0),
  15594.         PARAMETER_TYPE         = convert(smallint, 0),
  15595.         PARAMETER_HASDEFAULT    = convert(tinyint, null),
  15596.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  15597.         IS_NULLABLE        = convert(bit, 0),
  15598.         DATA_TYPE        = convert(smallint, 0),
  15599.         CHARACTER_MAXIMUM_LENGTH= convert(int, 0),
  15600.         CHARACTER_OCTET_LENGTH    = convert(int, 0),
  15601.         NUMERIC_PRECISION    = convert(smallint, 0),
  15602.         NUMERIC_SCALE        = convert(smallint, 0),
  15603.         DESCRIPTION        = convert(varchar(1),null),
  15604.         TYPE_NAME        = convert(sysname,null),
  15605.         LOCAL_TYPE_NAME        = convert(sysname,null)
  15606.     where    1=0
  15607. go
  15608. dump tran master with no_log
  15609. go
  15610.  
  15611.  
  15612. if (charindex('7.00', @@version) > 0)
  15613.     drop procedure sp_procedure_params_rowset
  15614. else
  15615. begin
  15616.     print ''
  15617.     print ''
  15618.     print 'Warning:'
  15619.     print 'you are installing the stored procedures '
  15620.     print 'on a pre 7.0 SQL Server.'
  15621.     print 'Ignore the following errors.'
  15622. end
  15623. go
  15624.  
  15625. /*    Procedure for 7.0 servers */
  15626. create procedure sp_procedure_params_rowset
  15627.     (
  15628.        @procedure_name        sysname,
  15629.     @group_number        int = 1,
  15630.     @procedure_schema     sysname = null,
  15631.     @parameter_name        sysname = null
  15632.     )
  15633. as
  15634.     select
  15635.         PROCEDURE_CATALOG     = db_name(),
  15636.         PROCEDURE_SCHEMA     = user_name(),
  15637.         PROCEDURE_NAME         = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  15638.         PARAMETER_NAME         = c.name,
  15639.         ORDINAL_POSITION     = convert(smallint,c.colid),
  15640.         PARAMETER_TYPE         = convert(smallint, 1+c.isoutparam),
  15641.         PARAMETER_HASDEFAULT    = convert(tinyint, null),
  15642.         PARAMETER_DEFAULT    = convert(nvarchar(255),null),
  15643.         IS_NULLABLE        = convert(bit,ColumnProperty(c.id,c.name,'AllowsNull')),
  15644.         DATA_TYPE        = d.oledb_data_type,
  15645.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  15646.                         case 
  15647.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  15648.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  15649.                         then coalesce(d.column_size,c.length)
  15650.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  15651.                         then coalesce(d.column_size,c.length/2)
  15652.                         else null 
  15653.                         end),
  15654.         CHARACTER_OCTET_LENGTH    = convert(int,
  15655.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  15656.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  15657.                             or d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  15658.                         then coalesce(d.column_size,c.length)
  15659.                         else null 
  15660.                         end),
  15661.         NUMERIC_PRECISION    = convert(smallint,
  15662.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec else null end),
  15663.         NUMERIC_SCALE        = convert(smallint, 
  15664.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  15665.         DESCRIPTION        = convert(nvarchar(1),null),
  15666.         TYPE_NAME        = d.type_name,
  15667.         LOCAL_TYPE_NAME        = d.local_type_name
  15668.     
  15669.     from
  15670.         sysobjects o,
  15671.         syscolumns c,
  15672.         master.dbo.spt_provider_types d,
  15673.         systypes t
  15674.     where
  15675.         o.name = @procedure_name
  15676.     and    o.type = 'P'                            /* Just Procedures */
  15677.     and    (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  15678.     and    o.id = c.id
  15679.     and    c.number = @group_number
  15680.     and    c.xtype = d.ss_dtype
  15681.     and    c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  15682.     and    c.xusertype = t.xusertype
  15683.     and    (@parameter_name is null or @parameter_name = c.name)
  15684.     UNION ALL
  15685.     SELECT           /* return value row*/
  15686.         PROCEDURE_CATALOG     = db_name(),
  15687.         PROCEDURE_SCHEMA     = user_name(),
  15688.         PROCEDURE_NAME         = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  15689.         PARAMETER_NAME         = convert(sysname,'RETURN_VALUE'),
  15690.         ORDINAL_POSITION     = convert(smallint,0),
  15691.         PARAMETER_TYPE         = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  15692.         PARAMETER_HASDEFAULT    = convert(tinyint, null),
  15693.         PARAMETER_DEFAULT    = convert(nvarchar(255),null),
  15694.         IS_NULLABLE        = convert(bit,0),
  15695.         DATA_TYPE        = convert(smallint, 3 /*DBTYPE_I4*/),
  15696.         CHARACTER_MAXIMUM_LENGTH= convert(int,null),
  15697.         CHARACTER_OCTET_LENGTH    = convert(int,null),
  15698.         NUMERIC_PRECISION    = convert(smallint,null),
  15699.         NUMERIC_SCALE        = convert(smallint,null),
  15700.         DESCRIPTION        = convert(nvarchar(1),null),
  15701.         TYPE_NAME        = convert(sysname,N'int'),
  15702.         LOCAL_TYPE_NAME        = convert(sysname,N'int')
  15703.     from
  15704.         sysobjects o,
  15705.         syscomments c
  15706.     where
  15707.         o.name = @procedure_name
  15708.     and    o.id = c.id 
  15709.     and    c.colid = 1
  15710.     and    o.type = 'P'                        /* Just Procedures */
  15711.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  15712.     and    (@parameter_name is null or @parameter_name = 'RETURN_VALUE')
  15713.     order by 2, 3, 5
  15714. go
  15715. dump tran master with no_log
  15716. go
  15717. create procedure sp_procedure_params_rowset;2
  15718.     (
  15719.     @procedure_schema     sysname = null,
  15720.     @parameter_name        sysname = null
  15721.     )
  15722. as
  15723.     select
  15724.         PROCEDURE_CATALOG     = db_name(),
  15725.         PROCEDURE_SCHEMA     = user_name(),
  15726.         PROCEDURE_NAME         = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  15727.         PARAMETER_NAME         = c.name,
  15728.         ORDINAL_POSITION     = convert(smallint,c.colid),
  15729.         PARAMETER_TYPE         = convert(smallint, 1+c.isoutparam),
  15730.         PARAMETER_HASDEFAULT    = convert(tinyint, null),
  15731.         PARAMETER_DEFAULT    = convert(nvarchar(255),null),
  15732.         IS_NULLABLE        = convert(bit,ColumnProperty(c.id,c.name,'AllowsNull')),
  15733.         DATA_TYPE        = d.oledb_data_type,
  15734.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  15735.                         case 
  15736.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  15737.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  15738.                         then coalesce(d.column_size,c.length)
  15739.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  15740.                         then coalesce(d.column_size,c.length/2)
  15741.                         else null 
  15742.                         end),
  15743.         CHARACTER_OCTET_LENGTH    = convert(int,
  15744.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  15745.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  15746.                             or d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  15747.                         then coalesce(d.column_size,c.length)
  15748.                         else null 
  15749.                         end),
  15750.         NUMERIC_PRECISION    = convert(smallint,
  15751.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec else null end),
  15752.         NUMERIC_SCALE        = convert(smallint, 
  15753.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  15754.         DESCRIPTION        = convert(nvarchar(1),null),
  15755.         TYPE_NAME        = d.type_name,
  15756.         LOCAL_TYPE_NAME        = d.local_type_name
  15757.     
  15758.     from
  15759.         sysobjects o,
  15760.         syscolumns c,
  15761.         master.dbo.spt_provider_types d,
  15762.         systypes t
  15763.     where
  15764.         o.type = 'P'                            /* Just Procedures */
  15765.     and    (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  15766.     and    o.id = c.id
  15767.     and    c.xtype = d.ss_dtype
  15768.     and    c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  15769.     and    c.xusertype = t.xusertype
  15770.     and    (@parameter_name is null or @parameter_name = c.name)
  15771.     UNION ALL
  15772.     SELECT           /* return value row*/
  15773.         PROCEDURE_CATALOG     = db_name(),
  15774.         PROCEDURE_SCHEMA     = user_name(),
  15775.         PROCEDURE_NAME         = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  15776.         PARAMETER_NAME         = convert(sysname,'RETURN_VALUE'),
  15777.         ORDINAL_POSITION     = convert(smallint,0),
  15778.         PARAMETER_TYPE         = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  15779.         PARAMETER_HASDEFAULT    = convert(tinyint, null),
  15780.         PARAMETER_DEFAULT    = convert(nvarchar(255),null),
  15781.         IS_NULLABLE        = convert(bit,0),
  15782.         DATA_TYPE        = convert(smallint, 3 /*DBTYPE_I4*/),
  15783.         CHARACTER_MAXIMUM_LENGTH= convert(int,null),
  15784.         CHARACTER_OCTET_LENGTH    = convert(int,null),
  15785.         NUMERIC_PRECISION    = convert(smallint,null),
  15786.         NUMERIC_SCALE        = convert(smallint,null),
  15787.         DESCRIPTION        = convert(nvarchar(1),null),
  15788.         TYPE_NAME        = convert(sysname,N'int'),
  15789.         LOCAL_TYPE_NAME        = convert(sysname,N'int')
  15790.     from
  15791.         sysobjects o,
  15792.         syscomments c
  15793.     where
  15794.         o.type = 'P'        /* Just Procedures */
  15795.     and    o.id = c.id 
  15796.     and    c.colid = 1
  15797.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  15798.     and    (@parameter_name is null or @parameter_name = 'RETURN_VALUE')
  15799.     order by 2, 3, 5
  15800. go
  15801.  
  15802.  
  15803. grant execute on sp_procedure_params_rowset to public
  15804. go
  15805.  
  15806. dump tran master with no_log
  15807. go
  15808. if (charindex('6.00', @@version) > 0)
  15809.     begin
  15810.     if (exists (select * from sysobjects
  15811.             where name = 'sp_procedure_params_rowset' and type = 'P '))
  15812.         begin
  15813.         drop procedure sp_procedure_params_rowset
  15814.         dump tran master with no_log
  15815.         end
  15816.     end
  15817. go
  15818.  
  15819.  
  15820. print ''
  15821. print 'creating sp_procedures_rowset'
  15822. go
  15823.  
  15824.  
  15825. /* pre 7.00 version */
  15826. create procedure sp_procedures_rowset
  15827.     (
  15828.     @procedure_name        varchar(255), 
  15829.     @group_number        int = 1,
  15830.     @procedure_schema    varchar(255) = null
  15831.     )        
  15832. as
  15833.     select
  15834.         PROCEDURE_CATALOG    = db_name(),
  15835.         PROCEDURE_SCHEMA    = user_name(o.uid),
  15836.         PROCEDURE_NAME        = convert(varchar(35),o.name +';'+ ltrim(str(p.number,5))),
  15837.         PROCEDURE_TYPE        = convert(smallint, 3 /*DB_PT_FUNCTION*/),
  15838.         PROCEDURE_DEFINITION    = convert(varchar(1),null),
  15839.         DESCRIPTION        = convert(varchar(1),null),
  15840.         DATE_CREATED        = o.crdate,
  15841.         DATE_MODIFIED        = convert(datetime,null)
  15842.     from     
  15843.         sysobjects o, 
  15844.         syscomments p,
  15845.         sysusers u
  15846.     where
  15847.             o.name = @procedure_name
  15848.         and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  15849.     and     o.type = 'P'        /* Object type of Procedure */
  15850.     and     p.colid = 1
  15851.     and     p.id = o.id
  15852.     and    p.number = @group_number
  15853.     and     u.uid = user_id()    /* constrain sysusers uid for use in subquery */
  15854.     and     (    suser_id() = 1     /* User is the System Administrator */
  15855.         or     o.uid = user_id()    /* User created the object */
  15856.             /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15857.         or     ((select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15858.              from sysprotects p
  15859.              where p.id = o.id
  15860.                  /*  get rows for public,current user,user's group */
  15861.                  and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15862.                  /* check for SELECT,EXECUTE privilege */
  15863.              and (action in (193,224)))&1    /* more magic...normalize GRANT */
  15864.             ) = 1     /* final magic...compare Grants    */
  15865.         )
  15866.     order by 2, 3
  15867. go
  15868. dump tran master with no_log
  15869. go
  15870. create procedure sp_procedures_rowset;2
  15871.     (
  15872.     @procedure_schema    varchar(255) = null
  15873.     )        
  15874. as
  15875.     select
  15876.         PROCEDURE_CATALOG    = db_name(),
  15877.         PROCEDURE_SCHEMA    = user_name(o.uid),
  15878.         PROCEDURE_NAME        = convert(varchar(35),o.name +';'+ ltrim(str(p.number,5))),
  15879.         PROCEDURE_TYPE        = convert(smallint, 3 /*DB_PT_FUNCTION*/),
  15880.         PROCEDURE_DEFINITION    = convert(varchar(1),null),
  15881.         DESCRIPTION        = convert(varchar(1),null),
  15882.         DATE_CREATED        = o.crdate,
  15883.         DATE_MODIFIED        = convert(datetime,null)
  15884.     from     
  15885.         sysobjects o, 
  15886.         syscomments p,
  15887.         sysusers u
  15888.     where
  15889.             (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  15890.     and     o.type = 'P'        /* Object type of Procedure */
  15891.     and     p.colid = 1
  15892.     and     p.id = o.id
  15893.     and     u.uid = user_id()    /* constrain sysusers uid for use in subquery */
  15894.     and     (    suser_id() = 1     /* User is the System Administrator */
  15895.         or     o.uid = user_id()    /* User created the object */
  15896.             /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15897.         or     ((select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15898.              from sysprotects p
  15899.              where p.id = o.id
  15900.                  /*  get rows for public,current user,user's group */
  15901.                  and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15902.                  /* check for SELECT,EXECUTE privilege */
  15903.              and (action in (193,224)))&1    /* more magic...normalize GRANT */
  15904.             ) = 1     /* final magic...compare Grants    */
  15905.         )
  15906.     order by 2, 3
  15907. go
  15908. dump tran master with no_log
  15909. go
  15910.  
  15911. if (charindex('7.00', @@version) > 0)
  15912.     drop procedure sp_procedures_rowset
  15913. else
  15914. begin
  15915.     print ''
  15916.     print ''
  15917.     print 'Warning:'
  15918.     print 'you are installing the stored procedures '
  15919.     print 'on a pre 7.0 SQL Server.'
  15920.     print 'Ignore the following errors.'
  15921. end
  15922. go
  15923.  
  15924. /* 7.00 version */
  15925. create procedure sp_procedures_rowset
  15926.     (
  15927.     @procedure_name        sysname, 
  15928.     @group_number        int = 1,
  15929.     @procedure_schema    sysname = null
  15930.     )        
  15931. as
  15932.     select
  15933.         PROCEDURE_CATALOG    = db_name(),
  15934.         PROCEDURE_SCHEMA    = user_name(o.uid),
  15935.         PROCEDURE_NAME        = convert(nvarchar(134),o.name +';'+ ltrim(str(p.number,5))),
  15936.         PROCEDURE_TYPE        = convert(smallint, 3 /*DB_PT_FUNCTION*/),
  15937.         PROCEDURE_DEFINITION    = convert(nvarchar(1),null),
  15938.         DESCRIPTION        = convert(nvarchar(1),null),
  15939.         DATE_CREATED        = o.crdate,
  15940.         DATE_MODIFIED        = convert(datetime,null)
  15941.     from     
  15942.         sysobjects o, 
  15943.         syscomments p
  15944.     where
  15945.         permissions(o.id) <> 0
  15946.     and    o.name = @procedure_name
  15947.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  15948.     and     o.type = 'P'        /* Object type of Procedure */
  15949.     and     p.colid = 1
  15950.     and     p.id = o.id
  15951.     and    p.number = @group_number
  15952.     order by 2, 3
  15953. go
  15954. dump tran master with no_log
  15955. go
  15956. create procedure sp_procedures_rowset;2
  15957.     (
  15958.     @procedure_schema    sysname = null
  15959.     )        
  15960. as
  15961.     select
  15962.         PROCEDURE_CATALOG    = db_name(),
  15963.         PROCEDURE_SCHEMA    = user_name(o.uid),
  15964.         PROCEDURE_NAME        = convert(nvarchar(134),o.name +';'+ ltrim(str(p.number,5))),
  15965.         PROCEDURE_TYPE        = convert(smallint, 3 /*DB_PT_FUNCTION*/),
  15966.         PROCEDURE_DEFINITION    = convert(nvarchar(1),null),
  15967.         DESCRIPTION        = convert(nvarchar(1),null),
  15968.         DATE_CREATED        = o.crdate,
  15969.         DATE_MODIFIED        = convert(datetime,null)
  15970.     from     sysobjects o, syscomments p
  15971.     where
  15972.         permissions(o.id) <> 0
  15973.     and        (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  15974.     and     o.type = 'P'        /* Object type of Procedure */
  15975.     and p.colid = 1
  15976.     and p.id = o.id
  15977.     order by 2, 3
  15978. go
  15979.  
  15980. grant execute on sp_procedures_rowset to public
  15981. go
  15982.  
  15983. dump tran master with no_log
  15984. go
  15985. if (charindex('6.00', @@version) > 0)
  15986.     begin
  15987.     if (exists (select * from sysobjects
  15988.             where name = 'sp_procedures_rowset' and type = 'P '))
  15989.         begin
  15990.         drop procedure sp_procedures_rowset
  15991.         dump tran master with no_log
  15992.         end
  15993.     end
  15994. go
  15995.  
  15996.  
  15997. print ''
  15998. print 'creating sp_schemata_rowset'
  15999. go
  16000.  
  16001. /*    6.0 and 6.5 version */
  16002. create procedure sp_schemata_rowset
  16003.     (
  16004.     @schema_name    varchar(90) = null, 
  16005.     @schema_owner    varchar(90) = null
  16006.     )        
  16007. as
  16008.     select    distinct
  16009.         CATALOG_NAME            = db_name(),
  16010.         SCHEMA_NAME            = user_name(o.uid),    
  16011.         SCHEMA_OWNER            = user_name(o.uid),    
  16012.         DEFAULT_CHARACTER_SET_CATALOG    = convert(sysname,'master'),
  16013.         DEFAULT_CHARACTER_SET_SCHEMA    = convert(sysname,'dbo'),
  16014.         DEFAULT_CHARACTER_SET_NAME    = convert(sysname,a_cha.name)
  16015.     from    
  16016.         sysobjects o,
  16017.         master.dbo.sysconfigures    cfg,
  16018.         master.dbo.syscharsets        a_cha, /* charset/1001, not sortorder */
  16019.         master.dbo.syscharsets        b_cha  /* sortorder/2001, not charset */
  16020.     where   
  16021.         (@schema_name is null or @schema_name = user_name(o.uid))
  16022.     and     (@schema_owner is null or @schema_owner = user_name(o.uid))
  16023.     and    a_cha.type = 1001 /* type is charset */
  16024.     and     b_cha.type = 2001 /* type is sortorder */
  16025.     and     a_cha.id = b_cha.csid
  16026.     and     b_cha.id = cfg.value
  16027.     order by 2
  16028. go
  16029. dump tran master with no_log
  16030. go
  16031.  
  16032. if (charindex('7.00', @@version) > 0)
  16033.     drop procedure sp_schemata_rowset
  16034. else
  16035. begin
  16036.     print ''
  16037.     print ''
  16038.     print 'Warning:'
  16039.     print 'you are installing the stored procedures '
  16040.     print 'on a pre 7.0 SQL Server.'
  16041.     print 'Ignore the following errors.'
  16042. end
  16043. go
  16044.  
  16045. /* 7.00 version */
  16046. create procedure sp_schemata_rowset
  16047.     (
  16048.     @schema_name    sysname = null, 
  16049.     @schema_owner    sysname = null
  16050.     )        
  16051. as
  16052.     select    distinct
  16053.         CATALOG_NAME            = db_name(),
  16054.         SCHEMA_NAME            = user_name(o.uid),    
  16055.         SCHEMA_OWNER            = user_name(o.uid),    
  16056.         DEFAULT_CHARACTER_SET_CATALOG    = convert(sysname,N'master'),
  16057.         DEFAULT_CHARACTER_SET_SCHEMA    = convert(sysname,N'dbo'),
  16058.         DEFAULT_CHARACTER_SET_NAME    = convert(sysname,a_cha.name)
  16059.     from    
  16060.         sysobjects o,
  16061.         master.dbo.sysconfigures    cfg,
  16062.         master.dbo.syscharsets        a_cha, /* charset/1001, not sortorder */
  16063.         master.dbo.syscharsets        b_cha  /* sortorder/2001, not charset */
  16064.     where   
  16065.         (@schema_name is null or @schema_name = user_name(o.uid))
  16066.     and     (@schema_owner is null or @schema_owner = user_name(o.uid))
  16067.     and    a_cha.type = 1001 /* type is charset */
  16068.     and     b_cha.type = 2001 /* type is sortorder */
  16069.     and     a_cha.id = b_cha.csid
  16070.     and     b_cha.id = cfg.value
  16071.     order by 2
  16072. go
  16073. dump tran master with no_log
  16074. go
  16075. go
  16076. /* The following stored procedure is used for Sphinx and Hydra */
  16077. create procedure sp_schemata_rowset;3
  16078. as
  16079.     select
  16080.         CATALOG_NAME            = convert(sysname,' '),
  16081.         SCHEMA_NAME            = convert(sysname,' '),    
  16082.         SCHEMA_OWNER            = convert(sysname,' '),    
  16083.         DEFAULT_CHARACTER_SET_CATALOG    = convert(sysname,' '),
  16084.         DEFAULT_CHARACTER_SET_SCHEMA    = convert(sysname,' '),
  16085.         DEFAULT_CHARACTER_SET_NAME    = convert(sysname,' ')
  16086.     where    1=0
  16087. go
  16088. grant execute on sp_schemata_rowset to public
  16089. go
  16090.  
  16091. dump tran master with no_log
  16092. go
  16093. if (charindex('6.00', @@version) > 0)
  16094.     begin
  16095.     if (exists (select * from sysobjects
  16096.             where name = 'sp_schemata_rowset' and type = 'P '))
  16097.         begin
  16098.         drop procedure sp_schemata_rowset
  16099.         dump tran master with no_log
  16100.         end
  16101.     end
  16102. go
  16103.  
  16104.  
  16105. print ''
  16106. print 'creating sp_statistics_rowset'
  16107. go
  16108.  
  16109.  
  16110. /*    6.0 and 6.5 version */
  16111. create procedure sp_statistics_rowset
  16112.     (
  16113.     @table_name    varchar(255),
  16114.     @table_schema    varchar(255) = null     
  16115.     )
  16116. as
  16117.     select    db_name()                as TABLE_CATALOG,        
  16118.         user_name(o.uid)            as TABLE_SCHEMA,
  16119.         o.name                    as TABLE_NAME,
  16120.         x.rows                    as CARDINALITY
  16121.     from    sysobjects o, sysindexes x, sysusers u
  16122.     where    o.type in ('U')
  16123.     and     o.name = @table_name
  16124.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  16125.     and     x.id = o.id
  16126.     and     x.indid in (0,1)    /*If there are no indexes then table stats are in a row with indid =0 */
  16127.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  16128.     and     (
  16129.         suser_id() = 1     /* User is the System Administrator */
  16130.         or o.uid = user_id()     /* User created the object */
  16131.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16132.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16133.             from sysprotects p
  16134.             where p.id = o.id
  16135.             /* get rows for public,current user,user's group */
  16136.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16137.             /* check for SELECT,EXECUTE privilege */
  16138.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16139.             ) = 1    /* final magic...compare Grants      */
  16140.            )
  16141.     order by 2, 3
  16142. go
  16143. dump tran master with no_log
  16144. go
  16145. create procedure sp_statistics_rowset;2
  16146.     (
  16147.     @table_schema    varchar(255) = null     
  16148.     )
  16149. as
  16150.     select    db_name()                as TABLE_CATALOG,        
  16151.         user_name(o.uid)            as TABLE_SCHEMA,
  16152.         o.name                    as TABLE_NAME,
  16153.         x.rows                    as CARDINALITY
  16154.     from    sysobjects o, sysindexes x, sysusers u
  16155.     where    o.type in ('U')
  16156.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  16157.     and     x.id = o.id
  16158.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  16159.     and     (
  16160.         suser_id() = 1     /* User is the System Administrator */
  16161.         or o.uid = user_id()     /* User created the object */
  16162.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16163.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16164.             from sysprotects p
  16165.             where p.id = o.id
  16166.             /* get rows for public,current user,user's group */
  16167.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16168.             /* check for SELECT,EXECUTE privilege */
  16169.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16170.             ) = 1    /* final magic...compare Grants      */
  16171.            )        
  16172.     order by 2, 3
  16173. go
  16174. dump tran master with no_log
  16175. go
  16176.  
  16177. if (charindex('7.00', @@version) > 0)
  16178.     drop procedure sp_statistics_rowset
  16179. else
  16180. begin
  16181.     print ''
  16182.     print ''
  16183.     print 'Warning:'
  16184.     print 'you are installing the stored procedures '
  16185.     print 'on a pre 7.0 SQL Server.'
  16186.     print 'Ignore the following errors.'
  16187. end
  16188. go
  16189.  
  16190. /*     7.0 version */
  16191. create procedure sp_statistics_rowset
  16192.     (
  16193.     @table_name    sysname,
  16194.     @table_schema    sysname = null     
  16195.     )
  16196. as
  16197.     select    db_name()                as TABLE_CATALOG,        
  16198.         user_name(o.uid)            as TABLE_SCHEMA,
  16199.         o.name                    as TABLE_NAME,
  16200.         x.rows                    as CARDINALITY
  16201.     from    sysobjects o, sysindexes x
  16202.     where    o.type in ('U')
  16203.     and     o.name = @table_name
  16204.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  16205.     and     x.id = o.id
  16206.     and     x.indid in (0,1)    /*If there are no indexes then table stats are in a row with indid =0 */
  16207.     and     permissions(o.id) <> 0
  16208.     order by 2, 3
  16209. go
  16210. dump tran master with no_log
  16211. go
  16212. create procedure sp_statistics_rowset;2
  16213.     (
  16214.     @table_schema    sysname = null     
  16215.     )
  16216. as
  16217.     select    db_name()                as TABLE_CATALOG,        
  16218.         user_name(o.uid)            as TABLE_SCHEMA,
  16219.         o.name                    as TABLE_NAME,
  16220.         x.rows                    as CARDINALITY
  16221.     from    sysobjects o, sysindexes x
  16222.     where    o.type in ('U')
  16223.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  16224.     and     x.id = o.id
  16225.     and     x.indid in (0,1)    /*If there are no indexes then table stats are in a row with indid =0 */
  16226.     and     permissions(o.id) <> 0
  16227.     order by 2, 3
  16228. go
  16229.  
  16230. grant execute on sp_statistics_rowset to public
  16231. go
  16232.  
  16233. dump tran master with no_log
  16234. go
  16235. if (charindex('6.00', @@version) > 0)
  16236.     begin
  16237.     if (exists (select * from sysobjects
  16238.             where name = 'sp_statistics_rowset' and type = 'P '))
  16239.         begin
  16240.         drop procedure sp_statistics_rowset
  16241.         dump tran master with no_log
  16242.         end
  16243.     end
  16244. go
  16245.  
  16246.  
  16247. print ''
  16248. print 'creating sp_tables_rowset'
  16249. go
  16250.  
  16251.  
  16252. /*    Procedure for 6.50 and earlier servers */
  16253. create procedure sp_tables_rowset
  16254.     (
  16255.     @table_name    varchar(255), 
  16256.     @table_schema    varchar(255) = null,    
  16257.     @table_type    varchar(255) = null 
  16258.     )
  16259. as
  16260.     select    TABLE_CATALOG    = db_name(),
  16261.         TABLE_SCHEMA    = user_name(o.uid),
  16262.         TABLE_NAME    = o.name,
  16263.         TABLE_TYPE    = convert(varchar(30),
  16264.                     case o.type 
  16265.                     when 'U' then 'TABLE'
  16266.                     when 'V' then 'VIEW'
  16267.                     when 'S' then 'SYSTEM TABLE'
  16268.                     end),
  16269.         TABLE_GUID    = convert(binary(16), null),
  16270.         DESCRIPTION    = convert(varchar(1), null),
  16271.         TABLE_PROPID    = convert(int,null),
  16272.         DATE_CREATED    = o.crdate,
  16273.         DATE_MODIFIED    = convert(datetime,null)
  16274.     from    sysusers u, sysobjects o
  16275.     where    o.type in ('U','V','S')
  16276.     and     o.name = @table_name
  16277.     and     (    @table_schema is null
  16278.         or    @table_schema = user_name(o.uid)
  16279.         )
  16280.     and     (
  16281.             @table_type is null
  16282.         or    @table_type = case o.type 
  16283.                     when 'U' then 'TABLE'
  16284.                     when 'V' then 'VIEW'
  16285.                     when 'S' then 'SYSTEM TABLE'
  16286.                     end
  16287.         )
  16288.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  16289.     and     (
  16290.         suser_id() = 1     /* User is the System Administrator */
  16291.         or o.uid = user_id()     /* User created the object */
  16292.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16293.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16294.             from sysprotects p
  16295.             /* join to correlate with all rows in sysobjects */
  16296.             where p.id = o.id
  16297.             /* get rows for public,current user,user's group */
  16298.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16299.             /* check for SELECT,EXECUTE privilege */
  16300.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16301.             ) = 1    /* final magic...compare Grants      */
  16302.         )        
  16303.     order by 4, 2, 3
  16304. go
  16305. dump tran master with no_log
  16306. go
  16307. create procedure sp_tables_rowset;2
  16308.     (
  16309.     @table_schema    varchar(255) = null,    
  16310.     @table_type    varchar(255) = null 
  16311.     )
  16312. as
  16313.     select    TABLE_CATALOG    = db_name(),
  16314.         TABLE_SCHEMA    = user_name(o.uid),
  16315.         TABLE_NAME    = o.name,
  16316.         TABLE_TYPE    = convert(varchar(30),
  16317.                     case o.type 
  16318.                     when 'U' then 'TABLE'
  16319.                     when 'V' then 'VIEW'
  16320.                     when 'S' then 'SYSTEM TABLE'
  16321.                     end),
  16322.         TABLE_GUID    = convert(binary(16), null),
  16323.         DESCRIPTION    = convert(varchar(1), null),
  16324.         TABLE_PROPID    = convert(int,null),
  16325.         DATE_CREATED    = o.crdate,
  16326.         DATE_MODIFIED    = convert(datetime,null)
  16327.     from    sysusers u, sysobjects o
  16328.     where    o.type in ('U','V','S')
  16329.     and     (    @table_schema is null
  16330.         or    @table_schema = user_name(o.uid)
  16331.         )
  16332.     and     (
  16333.             @table_type is null
  16334.         or    @table_type = case o.type 
  16335.                     when 'U' then 'TABLE'
  16336.                     when 'V' then 'VIEW'
  16337.                     when 'S' then 'SYSTEM TABLE'
  16338.                     end
  16339.         )
  16340.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  16341.     and     (
  16342.         suser_id() = 1     /* User is the System Administrator */
  16343.         or o.uid = user_id()     /* User created the object */
  16344.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16345.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16346.             from sysprotects p
  16347.             /* join to correlate with all rows in sysobjects */
  16348.             where p.id = o.id
  16349.             /* get rows for public,current user,user's group */
  16350.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16351.             /* check for SELECT,EXECUTE privilege */
  16352.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16353.             ) = 1    /* final magic...compare Grants      */
  16354.         )        
  16355.     order by 4, 2, 3
  16356. go
  16357. dump tran master with no_log
  16358. go
  16359.  
  16360. if (charindex('7.00', @@version) > 0)
  16361.     drop procedure sp_tables_rowset
  16362. else
  16363. begin
  16364.     print ''
  16365.     print ''
  16366.     print 'Warning:'
  16367.     print 'you are installing the stored procedures '
  16368.     print 'on a pre 7.0 SQL Server.'
  16369.     print 'Ignore the following errors.'
  16370. end
  16371. go
  16372.  
  16373. /*    Procedure for 7.00 server */
  16374. create procedure sp_tables_rowset
  16375.     (
  16376.     @table_name    sysname, 
  16377.     @table_schema    sysname = null,    
  16378.     @table_type    nvarchar(255) = null 
  16379.     )
  16380. as
  16381.     select    TABLE_CATALOG    = db_name(),
  16382.         TABLE_SCHEMA    = user_name(o.uid),
  16383.         TABLE_NAME    = o.name,
  16384.         TABLE_TYPE    = convert(nvarchar(30),
  16385.                     case o.type 
  16386.                     when 'U' then N'TABLE'
  16387.                     when 'V' then N'VIEW'
  16388.                     when 'S' then N'SYSTEM TABLE'
  16389.                     end),
  16390.         TABLE_GUID    = convert(uniqueidentifier, null),
  16391.         DESCRIPTION    = convert(nvarchar(1), null),
  16392.         TABLE_PROPID    = convert(int,null),
  16393.         DATE_CREATED    = o.crdate,
  16394.         DATE_MODIFIED    = convert(datetime,null)
  16395.     from    sysobjects o
  16396.     where    o.name = @table_name
  16397.     and     o.type in ('U','V','S')
  16398.     and    permissions(o.id) <> 0
  16399.     and     (    @table_schema is null
  16400.         or    @table_schema = user_name(o.uid)
  16401.         )
  16402.     and     (
  16403.             @table_type is null
  16404.         or    @table_type = case o.type 
  16405.                     when 'U' then N'TABLE'
  16406.                     when 'V' then N'VIEW'
  16407.                     when 'S' then N'SYSTEM TABLE'
  16408.                     end
  16409.         )    
  16410.     order by 4, 2, 3
  16411. go
  16412. dump tran master with no_log
  16413. go
  16414. create procedure sp_tables_rowset;2
  16415.     (
  16416.     @table_schema    sysname = null,    
  16417.     @table_type    nvarchar(255) = null 
  16418.     )
  16419. as
  16420.     select    TABLE_CATALOG    = db_name(),
  16421.         TABLE_SCHEMA    = user_name(o.uid),
  16422.         TABLE_NAME    = o.name,
  16423.         TABLE_TYPE    = convert(nvarchar(30),
  16424.                     case o.type 
  16425.                     when 'U' then N'TABLE'
  16426.                     when 'V' then N'VIEW'
  16427.                     when 'S' then N'SYSTEM TABLE'
  16428.                     end),
  16429.         TABLE_GUID    = convert(uniqueidentifier, null),
  16430.         DESCRIPTION    = convert(nvarchar(1), null),
  16431.         TABLE_PROPID    = convert(int,null),
  16432.         DATE_CREATED    = o.crdate,
  16433.         DATE_MODIFIED    = convert(datetime,null)
  16434.     from    sysobjects o
  16435.     where    o.type in ('U','V','S')
  16436.     and    permissions(o.id) <> 0
  16437.     and     (    @table_schema is null
  16438.         or    @table_schema = user_name(o.uid)
  16439.         )
  16440.     and     (
  16441.             @table_type is null
  16442.         or    @table_type = case o.type 
  16443.                     when 'U' then N'TABLE'
  16444.                     when 'V' then N'VIEW'
  16445.                     when 'S' then N'SYSTEM TABLE'
  16446.                     end
  16447.         )    
  16448.     order by 4, 2, 3
  16449. go
  16450. dump tran master with no_log
  16451. go
  16452. create procedure sp_tables_rowset;5
  16453.     (
  16454.     @table_server        sysname,
  16455.     @table_catalog        sysname = null,
  16456.     @table_name        sysname = null,
  16457.     @table_schema        sysname = null,
  16458.     @table_type        sysname = null
  16459.     )
  16460. as
  16461.     select
  16462.         TABLE_CATALOG,
  16463.         TABLE_SCHEMA,
  16464.         TABLE_NAME,
  16465.         TABLE_TYPE,
  16466.         TABLE_GUID,
  16467.         DESCRIPTION
  16468.     --    TABLE_PROPID,
  16469.     --    DATE_CREATED,
  16470.     --    DATE_MODIFIED
  16471.     from master.dbo.SYSREMOTE_TABLES <
  16472.                 @table_server,
  16473.                 @table_catalog,
  16474.                 @table_schema,
  16475.                 @table_name,
  16476.                 @table_type >
  16477.     order by 4,1,2,3
  16478. go
  16479.  
  16480. grant execute on sp_tables_rowset to public
  16481. go
  16482.  
  16483. dump tran master with no_log
  16484. go
  16485. if (charindex('6.00', @@version) > 0)
  16486.     begin
  16487.     if (exists (select * from sysobjects
  16488.             where name = 'sp_tables_rowset' and type = 'P '))
  16489.         begin
  16490.         drop procedure sp_tables_rowset
  16491.         dump tran master with no_log
  16492.         end
  16493.     end
  16494. go
  16495.  
  16496.  
  16497.  
  16498. print ''
  16499. print 'creating sp_tables_info_rowset'
  16500. go
  16501.  
  16502.  
  16503. /*    Procedure for 6.50 and earlier servers */
  16504. create procedure sp_tables_info_rowset
  16505.     (
  16506.     @table_name    varchar(255), 
  16507.     @table_schema    varchar(255) = null,    
  16508.     @table_type    varchar(255) = null 
  16509.     )
  16510. as
  16511.     select    TABLE_CATALOG        = db_name(),
  16512.         TABLE_SCHEMA        = user_name(o.uid),
  16513.         TABLE_NAME        = o.name,
  16514.         TABLE_TYPE        = convert(varchar(30),
  16515.                         case o.type 
  16516.                         when 'U' then 'TABLE'
  16517.                         when 'V' then 'VIEW'
  16518.                         when 'S' then 'SYSTEM TABLE'
  16519.                         end),
  16520.         TABLE_GUID        = convert(binary(16), null),
  16521.         BOOKMARKS        = convert(bit, 1),
  16522.         BOOKMARK_TYPE        = convert(int, 1 /*DBPROPVAL_BMK_NUMERIC*/),
  16523.         BOOKMARK_DATATYPE    = convert(smallint, 19 /*DBTYPE_UI4 */),
  16524.         BOOKMARK_MAXIMUM_LENGTH = convert(int, 4),
  16525.         BOOKMARK_INFORMATION    = convert(int, 0),
  16526.         TABLE_VERSION        = convert(int, o.schema_ver),
  16527.         CARDINALITY        = x.rows,
  16528.         DESCRIPTION        = convert(varchar(1), null),
  16529.         TABLE_PROPID        = convert(int, null)
  16530.  
  16531.     from    sysusers u, 
  16532.         sysobjects o,
  16533.         sysindexes x
  16534.  
  16535.     where    o.type in ('U','V','S')
  16536.     and     o.name = @table_name
  16537.     and     (    @table_schema is null
  16538.         or    @table_schema = user_name(o.uid)
  16539.         )
  16540.     and     (
  16541.             @table_type is null
  16542.         or    @table_type = case o.type 
  16543.                     when 'U' then 'TABLE'
  16544.                     when 'V' then 'VIEW'
  16545.                     when 'S' then 'SYSTEM TABLE'
  16546.                     end
  16547.         )
  16548.     and    o.id *= x.id
  16549.     and    x.indid in (0,1)
  16550.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  16551.     and     (
  16552.         suser_id() = 1     /* User is the System Administrator */
  16553.         or o.uid = user_id()     /* User created the object */
  16554.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16555.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16556.             from sysprotects p
  16557.             /* join to correlate with all rows in sysobjects */
  16558.             where p.id = o.id
  16559.             /* get rows for public,current user,user's group */
  16560.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16561.             /* check for SELECT,EXECUTE privilege */
  16562.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16563.             ) = 1    /* final magic...compare Grants      */
  16564.         )        
  16565.     order by 4, 2, 3
  16566. go
  16567. dump tran master with no_log
  16568. go
  16569. create procedure sp_tables_info_rowset;2
  16570.     (
  16571.     @table_schema    varchar(255) = null,    
  16572.     @table_type    varchar(255) = null 
  16573.     )
  16574. as
  16575.     select    TABLE_CATALOG        = db_name(),
  16576.         TABLE_SCHEMA        = user_name(o.uid),
  16577.         TABLE_NAME        = o.name,
  16578.         TABLE_TYPE        = convert(varchar(30),
  16579.                         case o.type 
  16580.                         when 'U' then 'TABLE'
  16581.                         when 'V' then 'VIEW'
  16582.                         when 'S' then 'SYSTEM TABLE'
  16583.                         end),
  16584.         TABLE_GUID        = convert(binary(16), null),
  16585.         BOOKMARKS        = convert(bit, 1),
  16586.         BOOKMARK_TYPE        = convert(int, 1 /*DBPROPVAL_BMK_NUMERIC*/),
  16587.         BOOKMARK_DATATYPE    = convert(smallint, 19 /*DBTYPE_UI4 */),
  16588.         BOOKMARK_MAXIMUM_LENGTH = convert(int, 4),
  16589.         BOOKMARK_INFORMATION    = convert(int, 0),
  16590.         TABLE_VERSION        = convert(int, o.schema_ver),
  16591.         CARDINALITY        = x.rows,
  16592.         DESCRIPTION        = convert(varchar(1), null),
  16593.         TABLE_PROPID        = convert(int, null)
  16594.  
  16595.     from    sysusers u, 
  16596.         sysobjects o,
  16597.         sysindexes x
  16598.  
  16599.     where    o.type in ('U','V','S')
  16600.     and     (    @table_schema is null
  16601.         or    @table_schema = user_name(o.uid)
  16602.         )
  16603.     and     (
  16604.             @table_type is null
  16605.         or    @table_type = case o.type 
  16606.                     when 'U' then 'TABLE'
  16607.                     when 'V' then 'VIEW'
  16608.                     when 'S' then 'SYSTEM TABLE'
  16609.                     end
  16610.         )
  16611.     and    o.id *= x.id
  16612.     and    x.indid in (0,1)
  16613.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  16614.     and     (
  16615.         suser_id() = 1     /* User is the System Administrator */
  16616.         or o.uid = user_id()     /* User created the object */
  16617.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16618.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16619.             from sysprotects p
  16620.             /* join to correlate with all rows in sysobjects */
  16621.             where p.id = o.id
  16622.             /* get rows for public,current user,user's group */
  16623.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16624.             /* check for SELECT,EXECUTE privilege */
  16625.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16626.             ) = 1    /* final magic...compare Grants      */
  16627.         )        
  16628.     order by 4, 2, 3
  16629. go
  16630. dump tran master with no_log
  16631. go
  16632.  
  16633. if (charindex('7.00', @@version) > 0)
  16634.     drop procedure sp_tables_info_rowset
  16635. else
  16636. begin
  16637.     print ''
  16638.     print ''
  16639.     print 'Warning:'
  16640.     print 'you are installing the stored procedures '
  16641.     print 'on a pre 7.0 SQL Server.'
  16642.     print 'Ignore the following errors.'
  16643. end
  16644. go
  16645.  
  16646. /*    Procedure for 7.00 server */
  16647. create procedure sp_tables_info_rowset
  16648.     (
  16649.     @table_name    sysname, 
  16650.     @table_schema    sysname = null,    
  16651.     @table_type    nvarchar(255) = null 
  16652.     )
  16653. as
  16654.     select    TABLE_CATALOG    = db_name(),
  16655.         TABLE_SCHEMA    = user_name(o.uid),
  16656.         TABLE_NAME    = o.name,
  16657.         TABLE_TYPE    = convert(nvarchar(30),
  16658.                     case o.type 
  16659.                     when 'U' then N'TABLE'
  16660.                     when 'V' then N'VIEW'
  16661.                     when 'S' then N'SYSTEM TABLE'
  16662.                     end),
  16663.         TABLE_GUID    = convert(uniqueidentifier, null),
  16664.         BOOKMARKS        = convert(bit, 1),
  16665.         BOOKMARK_TYPE        = convert(int, 1 /*DBPROPVAL_BMK_NUMERIC*/),
  16666.         BOOKMARK_DATATYPE    = convert(smallint, 19 /*DBTYPE_UI4 */),
  16667.         BOOKMARK_MAXIMUM_LENGTH = convert(int, 4),
  16668.         BOOKMARK_INFORMATION    = convert(int, 0),
  16669.         TABLE_VERSION        = convert(int, o.schema_ver),
  16670.         CARDINALITY        = x.rows,
  16671.         DESCRIPTION        = convert(nvarchar(1), null),
  16672.         TABLE_PROPID        = convert(int, null) 
  16673.  
  16674.     from    sysobjects o left join sysindexes x on o.id = x.id and x.indid in (0,1)
  16675.  
  16676.     where    o.name = @table_name
  16677.     and     o.type in ('U','V','S')
  16678.     and    permissions(o.id) <> 0
  16679.     and     (    @table_schema is null
  16680.         or    @table_schema = user_name(o.uid)
  16681.         )
  16682.     and     (
  16683.             @table_type is null
  16684.         or    @table_type = case o.type 
  16685.                     when 'U' then N'TABLE'
  16686.                     when 'V' then N'VIEW'
  16687.                     when 'S' then N'SYSTEM TABLE'
  16688.                     end
  16689.         )    
  16690.     order by 4, 2, 3
  16691. go
  16692. dump tran master with no_log
  16693. go
  16694. create procedure sp_tables_info_rowset;2
  16695.     (
  16696.     @table_schema    sysname = null,    
  16697.     @table_type    nvarchar(255) = null 
  16698.     )
  16699. as
  16700.     select    TABLE_CATALOG    = db_name(),
  16701.         TABLE_SCHEMA    = user_name(o.uid),
  16702.         TABLE_NAME    = o.name,
  16703.         TABLE_TYPE    = convert(nvarchar(30),
  16704.                     case o.type 
  16705.                     when 'U' then N'TABLE'
  16706.                     when 'V' then N'VIEW'
  16707.                     when 'S' then N'SYSTEM TABLE'
  16708.                     end),
  16709.         TABLE_GUID    = convert(uniqueidentifier, null),
  16710.         BOOKMARKS        = convert(bit, 1),
  16711.         BOOKMARK_TYPE        = convert(int, 1 /*DBPROPVAL_BMK_NUMERIC*/),
  16712.         BOOKMARK_DATATYPE    = convert(smallint, 19 /*DBTYPE_UI4 */),
  16713.         BOOKMARK_MAXIMUM_LENGTH = convert(int, 4),
  16714.         BOOKMARK_INFORMATION    = convert(int, 0),
  16715.         TABLE_VERSION        = convert(int, o.schema_ver),
  16716.         CARDINALITY        = x.rows,
  16717.         DESCRIPTION        = convert(nvarchar(1), null),
  16718.         TABLE_PROPID        = convert(int, null) 
  16719.  
  16720.     from    sysobjects o left join sysindexes x on o.id = x.id and x.indid in (0,1)
  16721.  
  16722.     where    o.type in ('U','V','S')
  16723.     and    permissions(o.id) <> 0
  16724.     and     (    @table_schema is null
  16725.         or    @table_schema = user_name(o.uid)
  16726.         )
  16727.     and     (
  16728.             @table_type is null
  16729.         or    @table_type = case o.type 
  16730.                     when 'U' then N'TABLE'
  16731.                     when 'V' then N'VIEW'
  16732.                     when 'S' then N'SYSTEM TABLE'
  16733.                     end
  16734.         )    
  16735.     order by 4, 2, 3
  16736. go
  16737.  
  16738. grant execute on sp_tables_info_rowset to public
  16739. go
  16740.  
  16741. dump tran master with no_log
  16742. go
  16743. if (charindex('6.00', @@version) > 0)
  16744.     begin
  16745.     if (exists (select * from sysobjects
  16746.         where name = 'sp_tables_info_rowset' and type = 'P '))
  16747.         begin
  16748.         drop procedure sp_tables_info_rowset
  16749.         dump tran master with no_log
  16750.         end
  16751.     end
  16752. go
  16753.  
  16754.  
  16755. print ''
  16756. print 'creating sp_table_privileges_rowset'
  16757. go
  16758.  
  16759. /*    Procedure for 6.0 and 6.5 server */
  16760. CREATE PROCEDURE sp_table_privileges_rowset
  16761.     (
  16762.     @table_name    varchar(255) = null,
  16763.     @table_schema    varchar(255) = null,
  16764.     @grantor    varchar(255) = null,
  16765.     @grantee    varchar(255) = null
  16766.     )
  16767. as
  16768. IF @table_name is not null
  16769.     BEGIN
  16770.     select
  16771.         GRANTOR        = user_name(p.grantor),
  16772.         GRANTEE        = user_name(u.uid),
  16773.         TABLE_CATALOG    = db_name(),
  16774.         TABLE_SCHEMA    = user_name(o.uid),
  16775.         TABLE_NAME    = o.name,
  16776.         PRIVILEGE_TYPE    = convert(varchar(30),
  16777.                     case p.action
  16778.                     when 193 then 'SELECT'
  16779.                     when 195 then 'INSERT'
  16780.                     when 196 then 'DELETE'
  16781.                     when 197 then 'UPDATE'
  16782.                     else 'REFERENCES'
  16783.                     end),
  16784.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  16785.     from 
  16786.         sysprotects p, sysobjects o, sysusers u
  16787.     where
  16788.         o.name = @table_name
  16789.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  16790.     and    (@grantee is null or @grantee = user_name(u.uid))
  16791.     and     o.type in ('U','V','S')
  16792.     and    p.id = o.id
  16793.     and     (@grantor is null or @grantor = user_name(p.grantor))
  16794.             /* expand groups */
  16795.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  16796.     and     p.protecttype <> 206    /* only grant rows */
  16797.     and     p.action in (26,193,195,196,197)
  16798.     and     o.uid <> u.uid            /* no rows for owner */
  16799.     and     not exists (            /* exclude revoke'd privileges */
  16800.             select     *
  16801.             from     sysprotects p1
  16802.             where    p1.protecttype = 206
  16803.             and     p1.action = p.action
  16804.             and     p1.id = p.id
  16805.             and     p1.uid = u.uid)
  16806.     union all
  16807.     select    /*    Add rows for table owner */
  16808.         GRANTOR        = user_name(u.uid),
  16809.         GRANTEE        = user_name(o.uid),
  16810.         TABLE_CATALOG    = db_name(),
  16811.         TABLE_SCHEMA    = user_name(o.uid),
  16812.         TABLE_NAME    = o.name,
  16813.         PRIVILEGE_TYPE    = convert(varchar(30),
  16814.                     case v.number
  16815.                     when 193 then 'SELECT'
  16816.                     when 195 then 'INSERT'
  16817.                     when 196 then 'DELETE'
  16818.                     when 197 then 'UPDATE'
  16819.                     else 'REFERENCES'
  16820.                     end),
  16821.         IS_GRANTABLE    = convert(bit,1)    
  16822.     from 
  16823.         sysobjects o, master.dbo.spt_values v, sysusers u
  16824.     where
  16825.         o.name = @table_name
  16826.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  16827.     and    (@grantee is null or @grantee = user_name(u.uid))
  16828.     and     o.type in ('U','V','S')
  16829.     and     u.suid = 1        /* grantor is dbo of database */
  16830.     and    (@grantor is null or @grantor = user_name(u.uid))
  16831.     and     v.type = 'P'    /* cross product to get all exposed privileges */
  16832.     and     v.number in (26,193,195,196,197)
  16833.     and     not exists (    /* exclude revoke'd privileges */
  16834.             select     *
  16835.             from     sysprotects p1
  16836.             where    p1.protecttype = 206
  16837.             and     p1.action = v.number
  16838.             and     p1.id = o.id
  16839.             and     p1.uid = o.uid)
  16840.     order by 4,5,6,1,2
  16841.     END
  16842. ELSE
  16843.     BEGIN
  16844.     select
  16845.         GRANTOR        = user_name(p.grantor),
  16846.         GRANTEE        = user_name(u.uid),
  16847.         TABLE_CATALOG    = db_name(),
  16848.         TABLE_SCHEMA    = user_name(o.uid),
  16849.         TABLE_NAME    = o.name,
  16850.         PRIVILEGE_TYPE    = convert(varchar(30),
  16851.                     case p.action
  16852.                     when 193 then 'SELECT'
  16853.                     when 195 then 'INSERT'
  16854.                     when 196 then 'DELETE'
  16855.                     when 197 then 'UPDATE'
  16856.                     else 'REFERENCES'
  16857.                     end),
  16858.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  16859.     from 
  16860.         sysprotects p, sysobjects o, sysusers u
  16861.     where
  16862.         o.type in ('U','V','S')
  16863.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  16864.     and    (@grantee is null or @grantee = user_name(u.uid))
  16865.     and    p.id = o.id
  16866.     and     (@grantor is null or @grantor = user_name(p.grantor))
  16867.             /* expand groups */
  16868.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  16869.     and     p.protecttype <> 206    /* only grant rows */
  16870.     and     p.action in (26,193,195,196,197)
  16871.     and     o.uid <> u.uid            /* no rows for owner */
  16872.     and     not exists (            /* exclude revoke'd privileges */
  16873.             select     *
  16874.             from     sysprotects p1
  16875.             where    p1.protecttype = 206
  16876.             and     p1.action = p.action
  16877.             and     p1.id = p.id
  16878.             and     p1.uid = u.uid)
  16879.     union all
  16880.     select    /*    Add rows for table owner */
  16881.         GRANTOR        = user_name(u.uid),
  16882.         GRANTEE        = user_name(o.uid),
  16883.         TABLE_CATALOG    = db_name(),
  16884.         TABLE_SCHEMA    = user_name(o.uid),
  16885.         TABLE_NAME    = o.name,
  16886.         PRIVILEGE_TYPE    = convert(varchar(30),
  16887.                     case v.number
  16888.                     when 193 then 'SELECT'
  16889.                     when 195 then 'INSERT'
  16890.                     when 196 then 'DELETE'
  16891.                     when 197 then 'UPDATE'
  16892.                     else 'REFERENCES'
  16893.                     end),
  16894.         IS_GRANTABLE    = convert(bit,1)    
  16895.     from 
  16896.         sysobjects o, master.dbo.spt_values v, sysusers u
  16897.     where
  16898.         o.type in ('U','V','S')
  16899.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  16900.     and    (@grantee is null or @grantee = user_name(u.uid))
  16901.     and     u.suid = 1        /* grantor is dbo of database */
  16902.     and    (@grantor is null or @grantor = user_name(u.uid))
  16903.     and     v.type = 'P'    /* cross product to get all exposed privileges */
  16904.     and     v.number in (26,193,195,196,197)
  16905.     and     not exists (    /* exclude revoke'd privileges */
  16906.             select     *
  16907.             from     sysprotects p1
  16908.             where    p1.protecttype = 206
  16909.             and     p1.action = v.number
  16910.             and     p1.id = o.id
  16911.             and     p1.uid = o.uid)
  16912.     order by 4,5,6,1,2
  16913.     END
  16914. go
  16915. dump tran master with no_log
  16916. go
  16917. CREATE PROCEDURE sp_table_privileges_rowset;2
  16918.     (
  16919.        @handle        int output,
  16920.        @scrollopt    int output,
  16921.     @ccopt        int output,
  16922.     @rows        int output,
  16923.     @table_name     varchar(255) = null,
  16924.     @table_schema    varchar(255) = null,
  16925.     @grantor    varchar(255) = null,
  16926.     @grantee    varchar(255) = null
  16927.     )
  16928. as
  16929. declare @ret int
  16930. SET NOCOUNT ON
  16931. create table #sptprivsrowset1
  16932.     (
  16933.     GRANTOR        sysname not null,
  16934.     GRANTEE        sysname not null,
  16935.     TABLE_CATALOG    sysname not null,
  16936.     TABLE_SCHEMA    sysname not null,
  16937.     TABLE_NAME    sysname not null,
  16938.     PRIVILEGE_TYPE    sysname not null,
  16939.     IS_GRANTABLE    bit not null
  16940.     )
  16941.     
  16942. IF @table_name is not null
  16943.     BEGIN
  16944.     insert into #sptprivsrowset1
  16945.     select
  16946.         GRANTOR        = user_name(p.grantor),
  16947.         GRANTEE        = user_name(u.uid),
  16948.         TABLE_CATALOG    = db_name(),
  16949.         TABLE_SCHEMA    = user_name(o.uid),
  16950.         TABLE_NAME    = o.name,
  16951.         PRIVILEGE_TYPE    = convert(varchar(30),
  16952.                     case p.action
  16953.                     when 193 then 'SELECT'
  16954.                     when 195 then 'INSERT'
  16955.                     when 196 then 'DELETE'
  16956.                     when 197 then 'UPDATE'
  16957.                     else 'REFERENCES'
  16958.                     end),
  16959.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  16960.     from 
  16961.         sysprotects p, sysobjects o, sysusers u
  16962.     where
  16963.         o.name = @table_name
  16964.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  16965.     and    (@grantee is null or @grantee = user_name(u.uid))
  16966.     and     o.type in ('U','V','S')
  16967.     and    p.id = o.id
  16968.     and     (@grantor is null or @grantor = user_name(p.grantor))
  16969.             /* expand groups */
  16970.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  16971.     and     p.protecttype <> 206    /* only grant rows */
  16972.     and     p.action in (26,193,195,196,197)
  16973.     and     o.uid <> u.uid            /* no rows for owner */
  16974.     and     not exists (            /* exclude revoke'd privileges */
  16975.             select     *
  16976.             from     sysprotects p1
  16977.             where    p1.protecttype = 206
  16978.             and     p1.action = p.action
  16979.             and     p1.id = p.id
  16980.             and     p1.uid = u.uid)
  16981.     union all
  16982.     select    /*    Add rows for table owner */
  16983.         GRANTOR        = user_name(u.uid),
  16984.         GRANTEE        = user_name(o.uid),
  16985.         TABLE_CATALOG    = db_name(),
  16986.         TABLE_SCHEMA    = user_name(o.uid),
  16987.         TABLE_NAME    = o.name,
  16988.         PRIVILEGE_TYPE    = convert(varchar(30),
  16989.                     case v.number
  16990.                     when 193 then 'SELECT'
  16991.                     when 195 then 'INSERT'
  16992.                     when 196 then 'DELETE'
  16993.                     when 197 then 'UPDATE'
  16994.                     else 'REFERENCES'
  16995.                     end),
  16996.         IS_GRANTABLE    = convert(bit,1)    
  16997.     from 
  16998.         sysobjects o, master.dbo.spt_values v, sysusers u
  16999.     where
  17000.         o.name = @table_name
  17001.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  17002.     and    (@grantee is null or @grantee = user_name(u.uid))
  17003.     and     o.type in ('U','V','S')
  17004.     and     u.suid = 1        /* grantor is dbo of database */
  17005.     and    (@grantor is null or @grantor = user_name(u.uid))
  17006.     and     v.type = 'P'    /* cross product to get all exposed privileges */
  17007.     and     v.number in (26,193,195,196,197)
  17008.     and     not exists (    /* exclude revoke'd privileges */
  17009.             select     *
  17010.             from     sysprotects p1
  17011.             where    p1.protecttype = 206
  17012.             and     p1.action = v.number
  17013.             and     p1.id = o.id
  17014.             and     p1.uid = o.uid)
  17015.     order by 4,5,6,1,2
  17016.     END
  17017. ELSE
  17018.     BEGIN
  17019.     insert into #sptprivsrowset1
  17020.     select
  17021.         GRANTOR        = user_name(p.grantor),
  17022.         GRANTEE        = user_name(u.uid),
  17023.         TABLE_CATALOG    = db_name(),
  17024.         TABLE_SCHEMA    = user_name(o.uid),
  17025.         TABLE_NAME    = o.name,
  17026.         PRIVILEGE_TYPE    = convert(varchar(30),
  17027.                     case p.action
  17028.                     when 193 then 'SELECT'
  17029.                     when 195 then 'INSERT'
  17030.                     when 196 then 'DELETE'
  17031.                     when 197 then 'UPDATE'
  17032.                     else 'REFERENCES'
  17033.                     end),
  17034.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  17035.     from 
  17036.         sysprotects p, sysobjects o, sysusers u
  17037.     where
  17038.         o.type in ('U','V','S')
  17039.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  17040.     and    (@grantee is null or @grantee = user_name(u.uid))
  17041.     and    p.id = o.id
  17042.     and     (@grantor is null or @grantor = user_name(p.grantor))
  17043.             /* expand groups */
  17044.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  17045.     and     p.protecttype <> 206    /* only grant rows */
  17046.     and     p.action in (26,193,195,196,197)
  17047.     and     o.uid <> u.uid            /* no rows for owner */
  17048.     and     not exists (            /* exclude revoke'd privileges */
  17049.             select     *
  17050.             from     sysprotects p1
  17051.             where    p1.protecttype = 206
  17052.             and     p1.action = p.action
  17053.             and     p1.id = p.id
  17054.             and     p1.uid = u.uid)
  17055.     union all
  17056.     select    /*    Add rows for table owner */
  17057.         GRANTOR        = user_name(u.uid),
  17058.         GRANTEE        = user_name(o.uid),
  17059.         TABLE_CATALOG    = db_name(),
  17060.         TABLE_SCHEMA    = user_name(o.uid),
  17061.         TABLE_NAME    = o.name,
  17062.         PRIVILEGE_TYPE    = convert(varchar(30),
  17063.                     case v.number
  17064.                     when 193 then 'SELECT'
  17065.                     when 195 then 'INSERT'
  17066.                     when 196 then 'DELETE'
  17067.                     when 197 then 'UPDATE'
  17068.                     else 'REFERENCES'
  17069.                     end),
  17070.         IS_GRANTABLE    = convert(bit,1)    
  17071.     from 
  17072.         sysobjects o, master.dbo.spt_values v, sysusers u
  17073.     where
  17074.         o.type in ('U','V','S')
  17075.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  17076.     and    (@grantee is null or @grantee = user_name(u.uid))
  17077.     and     u.suid = 1        /* grantor is dbo of database */
  17078.     and    (@grantor is null or @grantor = user_name(u.uid))
  17079.     and     v.type = 'P'    /* cross product to get all exposed privileges */
  17080.     and     v.number in (26,193,195,196,197)
  17081.     and     not exists (    /* exclude revoke'd privileges */
  17082.             select     *
  17083.             from     sysprotects p1
  17084.             where    p1.protecttype = 206
  17085.             and     p1.action = v.number
  17086.             and     p1.id = o.id
  17087.             and     p1.uid = o.uid)
  17088.     order by 4,5,6,1,2
  17089.     END
  17090.  
  17091. exec @ret = sp_cursoropen @handle output, 'select * from #sptprivsrowset1',
  17092.     @scrollopt output, @ccopt output, @rows output
  17093.  
  17094. drop table #sptprivsrowset1
  17095. return isnull(@ret,0)
  17096. go
  17097. dump tran master with no_log
  17098. go
  17099. CREATE PROCEDURE sp_table_privileges_rowset;3
  17100. as
  17101.     select
  17102.         GRANTOR        = convert(sysname, ' '),
  17103.         GRANTEE        = convert(sysname, ' '),
  17104.         TABLE_CATALOG    = convert(sysname, ' '),
  17105.         TABLE_SCHEMA    = convert(sysname, ' '),
  17106.         TABLE_NAME    = convert(sysname, ' '),
  17107.         PRIVILEGE_TYPE    = convert(varchar(30), ' '),
  17108.         IS_GRANTABLE    = convert(bit, 0)
  17109.     where    1=0
  17110. go
  17111.  
  17112. if (charindex('7.00', @@version) = 0)
  17113. begin
  17114.     print ''
  17115.     print ''
  17116.     print 'Warning:'
  17117.     print 'you are installing the stored procedures '
  17118.     print 'on a pre 7.0 SQL Server.'
  17119.     print 'Ignore the following errors.'
  17120. end
  17121. else
  17122.     drop proc sp_table_privileges_rowset
  17123. go
  17124.  
  17125.  
  17126. /*    Procedure for 7.0 server */
  17127. CREATE PROCEDURE sp_table_privileges_rowset
  17128.     (
  17129.     @table_name         sysname,
  17130.     @table_schema        sysname = null,
  17131.     @grantor        sysname = null,
  17132.     @grantee        sysname = null
  17133.     )
  17134. as
  17135.     select
  17136.         GRANTOR        = user_name(p.grantor),
  17137.         GRANTEE        = user_name(u.uid),
  17138.         TABLE_CATALOG    = db_name(),
  17139.         TABLE_SCHEMA    = user_name(o.uid),
  17140.         TABLE_NAME    = o.name,
  17141.         PRIVILEGE_TYPE    = convert(nvarchar(30),
  17142.                     case p.action
  17143.                     when 193 then N'SELECT'
  17144.                     when 195 then N'INSERT'
  17145.                     when 196 then N'DELETE'
  17146.                     when 197 then N'UPDATE'
  17147.                     else N'REFERENCES'
  17148.                     end),
  17149.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  17150.     from 
  17151.         sysprotects p, sysobjects o, sysusers u, sysmembers m
  17152.     where
  17153.         o.name = @table_name
  17154.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  17155.     and    (@grantee is null or @grantee = user_name(u.uid))
  17156.     and     o.type in ('U','V','S')
  17157.     and    p.id = o.id
  17158.     and     (@grantor is null or @grantor = user_name(p.grantor))
  17159.         /* expand groups - AKUNDONE: only 1 level of grp unrolling. */
  17160.     and     (u.uid > 0 and u.uid < 16384)
  17161.     and     ((p.uid = u.uid) or (p.uid = m.groupuid and u.uid = m.memberuid))
  17162.     and     p.protecttype <> 206    /* only grant rows */
  17163.     and     p.action in (26,193,195,196,197)
  17164.     and     o.uid <> u.uid            /* no rows for owner */
  17165.     and     not exists (            /* exclude revoke'd privileges */
  17166.             select     *
  17167.             from     sysprotects p1
  17168.             where    p1.protecttype = 206
  17169.             and     p1.action = p.action
  17170.             and     p1.id = p.id
  17171.             and     p1.uid = u.uid)
  17172.     union all
  17173.     select    /*    Add rows for table owner */
  17174.         GRANTOR        = user_name(u.uid),
  17175.         GRANTEE        = user_name(o.uid),
  17176.         TABLE_CATALOG    = db_name(),
  17177.         TABLE_SCHEMA    = user_name(o.uid),
  17178.         TABLE_NAME    = o.name,
  17179.         PRIVILEGE_TYPE    = convert(nvarchar(30),
  17180.                     case v.number
  17181.                     when 193 then N'SELECT'
  17182.                     when 195 then N'INSERT'
  17183.                     when 196 then N'DELETE'
  17184.                     when 197 then N'UPDATE'
  17185.                     else N'REFERENCES'
  17186.                     end),
  17187.         IS_GRANTABLE    = convert(bit,1)    
  17188.     from 
  17189.         sysobjects o, master.dbo.spt_values v, sysusers u
  17190.     where
  17191.         o.name = @table_name
  17192.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  17193.     and    (@grantee is null or @grantee = user_name(u.uid))
  17194.     and     o.type in ('U','V','S')
  17195.     and     u.suid = 1        /* grantor is dbo of database */
  17196.     and    (@grantor is null or @grantor = user_name(u.uid))
  17197.     and     v.type = 'P'    /* cross product to get all exposed privileges */
  17198.     and     v.number in (26,193,195,196,197)
  17199.     and     not exists (    /* exclude revoke'd privileges */
  17200.             select     *
  17201.             from     sysprotects p1
  17202.             where    p1.protecttype = 206
  17203.             and     p1.action = v.number
  17204.             and     p1.id = o.id
  17205.             and     p1.uid = o.uid)
  17206.     order by 4,5,6,1,2
  17207. go
  17208. dump tran master with no_log
  17209. go
  17210. CREATE PROCEDURE sp_table_privileges_rowset;2
  17211.     (
  17212.     @table_schema        sysname = null,
  17213.     @grantor        sysname = null,
  17214.     @grantee        sysname = null
  17215.     )
  17216. as
  17217.     select
  17218.         GRANTOR        = user_name(p.grantor),
  17219.         GRANTEE        = user_name(u.uid),
  17220.         TABLE_CATALOG    = db_name(),
  17221.         TABLE_SCHEMA    = user_name(o.uid),
  17222.         TABLE_NAME    = o.name,
  17223.         PRIVILEGE_TYPE    = convert(nvarchar(30),
  17224.                     case p.action
  17225.                     when 193 then N'SELECT'
  17226.                     when 195 then N'INSERT'
  17227.                     when 196 then N'DELETE'
  17228.                     when 197 then N'UPDATE'
  17229.                     else N'REFERENCES'
  17230.                     end),
  17231.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  17232.     from 
  17233.         sysprotects p, sysobjects o, sysusers u, sysmembers m
  17234.     where
  17235.         o.type in ('U','V','S')
  17236.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  17237.     and    (@grantee is null or @grantee = user_name(u.uid))
  17238.     and    p.id = o.id
  17239.     and     (@grantor is null or @grantor = user_name(p.grantor))
  17240.         /* expand groups - AKUNDONE: only 1 level of grp unrolling. */
  17241.     and     (u.uid > 0 and u.uid < 16384)
  17242.     and     ((p.uid = u.uid) or (p.uid = m.groupuid and u.uid = m.memberuid))
  17243.     and     p.protecttype <> 206    /* only grant rows */
  17244.     and     p.action in (26,193,195,196,197)
  17245.     and     o.uid <> u.uid            /* no rows for owner */
  17246.     and     not exists (            /* exclude revoke'd privileges */
  17247.             select     *
  17248.             from     sysprotects p1
  17249.             where    p1.protecttype = 206
  17250.             and     p1.action = p.action
  17251.             and     p1.id = p.id
  17252.             and     p1.uid = u.uid)
  17253.     union all
  17254.     select    /*    Add rows for table owner */
  17255.         GRANTOR        = user_name(u.uid),
  17256.         GRANTEE        = user_name(o.uid),
  17257.         TABLE_CATALOG    = db_name(),
  17258.         TABLE_SCHEMA    = user_name(o.uid),
  17259.         TABLE_NAME    = o.name,
  17260.         PRIVILEGE_TYPE    = convert(nvarchar(30),
  17261.                     case v.number
  17262.                     when 193 then N'SELECT'
  17263.                     when 195 then N'INSERT'
  17264.                     when 196 then N'DELETE'
  17265.                     when 197 then N'UPDATE'
  17266.                     else N'REFERENCES'
  17267.                     end),
  17268.         IS_GRANTABLE    = convert(bit,1)    
  17269.     from 
  17270.         sysobjects o, master.dbo.spt_values v, sysusers u
  17271.     where
  17272.         o.type in ('U','V','S')
  17273.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  17274.     and    (@grantee is null or @grantee = user_name(u.uid))
  17275.     and     u.suid = 1        /* grantor is dbo of database */
  17276.     and    (@grantor is null or @grantor = user_name(u.uid))
  17277.     and     v.type = 'P'    /* cross product to get all exposed privileges */
  17278.     and     v.number in (26,193,195,196,197)
  17279.     and     not exists (    /* exclude revoke'd privileges */
  17280.             select     *
  17281.             from     sysprotects p1
  17282.             where    p1.protecttype = 206
  17283.             and     p1.action = v.number
  17284.             and     p1.id = o.id
  17285.             and     p1.uid = o.uid)
  17286.     order by 4,5,6,1,2
  17287. go
  17288. dump tran master with no_log
  17289. go
  17290. create procedure sp_table_privileges_rowset;5
  17291.     (
  17292.     @table_server        sysname,
  17293.     @table_catalog        sysname = null,
  17294.     @table_name        sysname = null,
  17295.     @table_schema        sysname = null,
  17296.     @grantor        sysname = null,
  17297.     @grantee        sysname = null
  17298.     )
  17299. as
  17300.     select
  17301.         GRANTOR,        
  17302.         GRANTEE,        
  17303.         TABLE_CATALOG,    
  17304.         TABLE_SCHEMA,    
  17305.         TABLE_NAME,    
  17306.         PRIVILEGE_TYPE,    
  17307.         IS_GRANTABLE    
  17308.     from master.dbo.SYSREMOTE_TABLE_PRIVILEGES <
  17309.                 @table_server,
  17310.                 @table_catalog,
  17311.                 @table_schema,
  17312.                 @table_name,
  17313.                 @grantor,
  17314.                 @grantee >
  17315.     order by 3,4,5,6,1,2
  17316. go
  17317.  
  17318. grant execute on sp_table_privileges_rowset to public
  17319. go
  17320. dump tran master with no_log
  17321. go
  17322. if (charindex('6.00', @@version) > 0)
  17323.     begin
  17324.     if (exists (select * from sysobjects
  17325.         where name = 'sp_table_privileges_rowset' and type = 'P '))
  17326.         begin
  17327.         drop proc sp_table_privileges_rowset
  17328.         dump tran master with no_log
  17329.         end
  17330.     end
  17331. go
  17332.  
  17333.  
  17334. print ''
  17335. print 'creating sp_linkedservers_rowset'
  17336. go
  17337. if (charindex('7.00', @@version) = 0)
  17338. begin
  17339.     print ''
  17340.     print ''
  17341.     print 'Warning:'
  17342.     print 'you are installing the stored procedures '
  17343.     print 'on a pre 7.0 SQL Server.'
  17344.     print 'Ignore the following errors.'
  17345. end
  17346. go
  17347. /*    Procedure for 7.0 server */
  17348. create proc sp_linkedservers_rowset
  17349.     (
  17350.     @srvname     sysname
  17351.     )
  17352. as
  17353.     select
  17354.         SVR_NAME         = srvname,
  17355.         SVR_PRODUCT        = srvproduct,
  17356.         SVR_PROVIDERNAME    = providername,
  17357.         SVR_DATASOURCE        = datasource,
  17358.         SVR_PROVIDERSTRING    = providerstring,
  17359.         SVR_LOCATION        = location,
  17360.         SVR_CATALOG        = catalog
  17361.     from master.dbo.sysservers
  17362.     where srvname = @srvname and (srvstatus & 128) = 128
  17363.     order by 1
  17364. go
  17365. dump tran master with no_log
  17366. go
  17367. create proc sp_linkedservers_rowset;2
  17368. as
  17369.     select
  17370.         SVR_NAME         = srvname,
  17371.         SVR_PRODUCT        = srvproduct,
  17372.         SVR_PROVIDERNAME    = providername,
  17373.         SVR_DATASOURCE        = datasource,
  17374.         SVR_PROVIDERSTRING    = providerstring,
  17375.         SVR_LOCATION        = location,
  17376.         SVR_CATALOG        = catalog
  17377.     from master.dbo.sysservers
  17378.     where (srvstatus & 128) = 128
  17379.     order by 1
  17380. go
  17381.  
  17382. grant execute on sp_linkedservers_rowset to public
  17383. go
  17384.  
  17385. dump tran master with no_log
  17386. print ''
  17387. go
  17388.  
  17389. /*-----------------------------------------------------------------------------*/
  17390. /*-------------- CATALOG STORED PROCEDURES FOR OLEDB SOURCES ------------------*/
  17391. /*-----------------------------------------------------------------------------*/
  17392.  
  17393. if object_id('sp_linkedservers', 'P') is not null
  17394.     drop proc sp_linkedservers
  17395. go
  17396. raiserror(15339,-1,-1,'sp_linkedservers')
  17397. go
  17398. create proc sp_linkedservers as
  17399.     select
  17400.         SRV_NAME = srvname,
  17401.         SRV_PROVIDERNAME = providername,
  17402.         SRV_PRODUCT = srvproduct,
  17403.         SRV_DATASOURCE = datasource,
  17404.         SRV_PROVIDERSTRING = providerstring,
  17405.         SRV_LOCATION = location,
  17406.         SRV_CAT = catalog
  17407.     from master.dbo.sysservers
  17408.     order by 1
  17409. go
  17410. grant execute on sp_linkedservers to public
  17411. go
  17412.  
  17413.  
  17414. if object_id('sp_catalogs', 'P') is not null
  17415.     drop proc sp_catalogs
  17416. go
  17417. raiserror(15339,-1,-1,'sp_catalogs')
  17418. go
  17419. create procedure sp_catalogs(
  17420.     @server_name        sysname)
  17421. as
  17422.     select
  17423.         CATALOG_NAME,
  17424.         convert (nvarchar(255),DESCRIPTION)
  17425.     from master.dbo.SYSREMOTE_CATALOGS < @server_name >
  17426.     order by CATALOG_NAME
  17427. go
  17428. grant execute on sp_catalogs to public
  17429. go
  17430.  
  17431.  
  17432. if object_id('sp_tables_ex', 'P') is not null
  17433.     drop proc sp_tables_ex
  17434. go
  17435. raiserror(15339,-1,-1,'sp_tables_ex')
  17436. go
  17437. create procedure sp_tables_ex(
  17438.     @table_server        sysname,
  17439.     @table_name            sysname = null,
  17440.     @table_schema        sysname = null,
  17441.     @table_catalog        sysname = null,
  17442.     @table_type            sysname = null)
  17443. as
  17444.     if ((isnull(charindex('%', @table_name),0) = 0) and
  17445.         (isnull(charindex('[', @table_name),0) = 0) and
  17446.         (isnull(charindex('_', @table_name),0) = 0))
  17447.     begin    /*    If no wild carding */
  17448.         select
  17449.             TABLE_CAT = TABLE_CATALOG,
  17450.             TABLE_SCHEM = TABLE_SCHEMA,
  17451.             TABLE_NAME = TABLE_NAME,
  17452.             TABLE_TYPE = TABLE_TYPE,
  17453.             REMARKS = convert(nvarchar(255),DESCRIPTION)
  17454.         from master.dbo.SYSREMOTE_TABLES <
  17455.                     @table_server,
  17456.                     @table_catalog,
  17457.                     @table_schema,
  17458.                     @table_name,
  17459.                     @table_type >
  17460.         order by TABLE_TYPE, TABLE_CAT, TABLE_SCHEM, TABLE_NAME
  17461.     end
  17462.     else
  17463.     begin    /*    If wild carding */
  17464.         select
  17465.             TABLE_CAT = TABLE_CATALOG,
  17466.             TABLE_SCHEM = TABLE_SCHEMA,
  17467.             TABLE_NAME = TABLE_NAME,
  17468.             TABLE_TYPE = TABLE_TYPE,
  17469.             REMARKS = convert(nvarchar(255),DESCRIPTION)
  17470.         from master.dbo.SYSREMOTE_TABLES <
  17471.                     @table_server,
  17472.                     @table_catalog,
  17473.                     @table_schema,
  17474.                     NULL,
  17475.                     @table_type >
  17476.         where TABLE_NAME like @table_name
  17477.         order by TABLE_TYPE, TABLE_CAT, TABLE_SCHEM, TABLE_NAME
  17478.     end
  17479. go
  17480. grant execute on sp_tables_ex to public
  17481. go
  17482.  
  17483.  
  17484. if object_id('sp_columns_ex', 'P') is not null
  17485.     drop proc sp_columns_ex
  17486. go
  17487. raiserror(15339,-1,-1,'sp_columns_ex')
  17488. go
  17489. create procedure sp_columns_ex(
  17490.     @table_server        sysname,
  17491.     @table_name            sysname = null,
  17492.     @table_schema        sysname = null,
  17493.     @table_catalog        sysname = null,
  17494.     @column_name        sysname = null,
  17495.     @ODBCVer            int = 2)
  17496. as
  17497.  
  17498.     set nocount on
  17499.     declare
  17500.     @DBCOLUMNFLAGS_ISFIXEDLENGTH binary(1),    @DBCOLUMNFLAGS_ISLONG    binary(1),
  17501.     @DBCOLUMNFLAGS_ISROWVER    binary(2)
  17502.  
  17503.     select
  17504.     @DBCOLUMNFLAGS_ISFIXEDLENGTH    = 0x10,    @DBCOLUMNFLAGS_ISLONG    = 0x80,
  17505.     @DBCOLUMNFLAGS_ISROWVER    = 0x0200
  17506.  
  17507.     declare    
  17508.     @DBTYPE_I2 smallint,            @DBTYPE_I4 smallint,
  17509.     @DBTYPE_R4 smallint,            @DBTYPE_R8 smallint,
  17510.     @DBTYPE_CY smallint,            @DBTYPE_DATE smallint,
  17511.     @DBTYPE_BSTR smallint,            @DBTYPE_BOOL smallint,
  17512.     @DBTYPE_VARIANT smallint,        @DBTYPE_DECIMAL smallint,
  17513.     @DBTYPE_UI1 smallint,            @DBTYPE_BYREF smallint,
  17514.     @DBTYPE_I1 smallint,            @DBTYPE_UI2 smallint,
  17515.     @DBTYPE_UI4 smallint,            @DBTYPE_I8 smallint,
  17516.     @DBTYPE_UI8 smallint,            @DBTYPE_GUID smallint,
  17517.     @DBTYPE_BYTES smallint,            @DBTYPE_STR smallint,
  17518.     @DBTYPE_WSTR smallint,            @DBTYPE_NUMERIC smallint,
  17519.     @DBTYPE_DBDATE smallint,        @DBTYPE_DBTIME smallint,
  17520.     @DBTYPE_DBTIMESTAMP smallint
  17521.  
  17522.     select
  17523.     @DBTYPE_I2            = 2,    @DBTYPE_I4            = 3,
  17524.     @DBTYPE_R4            = 4,    @DBTYPE_R8            = 5,
  17525.     @DBTYPE_CY            = 6,    @DBTYPE_DATE        = 7,
  17526.     @DBTYPE_BSTR        = 8,    @DBTYPE_BOOL        = 11,
  17527.     @DBTYPE_VARIANT        = 12,    @DBTYPE_DECIMAL        = 14,
  17528.     @DBTYPE_UI1            = 17,    @DBTYPE_BYREF        = 16384,
  17529.     @DBTYPE_I1            = 16,    @DBTYPE_UI2            = 18,
  17530.     @DBTYPE_UI4            = 19,    @DBTYPE_I8            = 20,
  17531.     @DBTYPE_UI8            = 21,    @DBTYPE_GUID        = 72,
  17532.     @DBTYPE_BYTES        = 128,    @DBTYPE_STR            = 129,
  17533.     @DBTYPE_WSTR        = 130,    @DBTYPE_NUMERIC        = 131,
  17534.     @DBTYPE_DBDATE        = 133,    @DBTYPE_DBTIME        = 134,    
  17535.     @DBTYPE_DBTIMESTAMP    = 135
  17536.  
  17537.     declare    
  17538.     @ODS_SMALLINT_NOT_NULL tinyint,    @ODS_INT_NULL tinyint,
  17539.     @ODS_INT_NOT_NULL tinyint,        @ODS_TINYINT_NOT_NULL tinyint,
  17540.     @ODS_REAL_NOT_NULL tinyint,        @ODS_FLOAT_NULL tinyint,
  17541.     @ODS_FLOAT_NOT_NULL tinyint,    @ODS_MONEY_NULL tinyint,
  17542.     @ODS_MONEY_NOT_NULL tinyint,    @ODS_DATETIME_NULL tinyint,
  17543.     @ODS_DATETIME_NOT_NULL tinyint,    @ODS_TEXT tinyint,
  17544.     @ODS_BIT tinyint,                @ODS_DECIMAL tinyint,
  17545.     @ODS_NUMERIC tinyint,            @ODS_GUID tinyint,
  17546.     @ODS_IMAGE tinyint,                @ODS_TIMESTAMP tinyint,
  17547.     @ODS_BINARY_NULL tinyint,        @ODS_BINARY_NOT_NULL tinyint,
  17548.     @ODS_VARBINARY tinyint,            @ODS_CHAR_NULL tinyint,
  17549.     @ODS_CHAR_NOT_NULL tinyint,        @ODS_VARCHAR tinyint
  17550.  
  17551.     select     
  17552.     @ODS_IMAGE = 34,            @ODS_TEXT = 35,
  17553.     @ODS_GUID = 36,                @ODS_BINARY_NULL = 37,            
  17554.     @ODS_VARBINARY = 37,        @ODS_INT_NULL = 38,
  17555.     @ODS_CHAR_NULL = 39,        @ODS_VARCHAR = 39,
  17556.     @ODS_TIMESTAMP = 45,        @ODS_BINARY_NOT_NULL = 45,
  17557.     @ODS_CHAR_NOT_NULL = 47,    @ODS_TINYINT_NOT_NULL = 48,
  17558.     @ODS_BIT = 50,                @ODS_SMALLINT_NOT_NULL = 52,    
  17559.     @ODS_INT_NOT_NULL = 56,        @ODS_REAL_NOT_NULL = 59,        
  17560.     @ODS_MONEY_NOT_NULL = 60,    @ODS_DATETIME_NOT_NULL = 61,    
  17561.     @ODS_FLOAT_NOT_NULL = 62,    @ODS_DECIMAL = 106,
  17562.     @ODS_NUMERIC = 108,            @ODS_FLOAT_NULL = 109,
  17563.     @ODS_MONEY_NULL = 110,        @ODS_DATETIME_NULL = 111
  17564.  
  17565.     declare 
  17566.     @ODBC_GUID smallint,        @ODBC_NTEXT smallint,
  17567.     @ODBC_NVARCHAR smallint,    @ODBC_NCHAR smallint,
  17568.     @ODBC_BIT smallint,            @ODBC_TINYINT smallint,
  17569.     @ODBC_IMAGE smallint,        @ODBC_VARBINARY smallint,
  17570.     @ODBC_TIMESTAMP smallint,    @ODBC_BINARY smallint,
  17571.     @ODBC_TEXT smallint,        @ODBC_CHAR smallint,
  17572.     @ODBC_NUMERIC smallint,        @ODBC_DECIMAL smallint,
  17573.     @ODBC_MONEY smallint,        @ODBC_INT smallint,        
  17574.     @ODBC_SMALLINT smallint,    @ODBC_FLOAT smallint,    
  17575.     @ODBC_REAL smallint,        @ODBC_VARCHAR smallint,
  17576.     @ODBC_DATETIME smallint
  17577.  
  17578.     select
  17579.     @ODBC_GUID        = -11,    @ODBC_NTEXT        = -10,
  17580.     @ODBC_NVARCHAR    = -9,    @ODBC_NCHAR        = -8,
  17581.     @ODBC_BIT        = -7,    @ODBC_TINYINT    = -6,
  17582.     @ODBC_IMAGE        = -4,    @ODBC_VARBINARY    = -3,
  17583.     @ODBC_TIMESTAMP    = -2,    @ODBC_BINARY    = -2,
  17584.     @ODBC_TEXT        = -1,    @ODBC_CHAR        = 1,
  17585.     @ODBC_NUMERIC    = 2,    @ODBC_DECIMAL    = 3,
  17586.     @ODBC_MONEY        = 3,    @ODBC_INT        = 4,        
  17587.     @ODBC_SMALLINT    = 5,    @ODBC_FLOAT        = 6,    
  17588.     @ODBC_REAL        = 7,    @ODBC_VARCHAR    = 12,
  17589.     @ODBC_DATETIME    =    
  17590.     case @ODBCVer
  17591.         when 2 then 11
  17592.         else 93
  17593.     end
  17594.  
  17595.     create table #tmp_columns
  17596.     (    TABLE_CAT nvarchar(127),
  17597.         TABLE_SCHEM nvarchar(127),
  17598.         TABLE_NAME nvarchar(127),
  17599.         COLUMN_NAME nvarchar(127),
  17600.         DATA_TYPE smallint,
  17601.         TYPE_NAME nvarchar(127) NULL,
  17602.         COLUMN_SIZE int NULL,
  17603.         BUFFER_LENGTH int NULL,
  17604.         DECIMAL_DIGITS smallint NULL,
  17605.         NUM_PREC_RADIX smallint NULL,
  17606.         NULLABLE smallint, 
  17607.         REMARKS nvarchar(255) NULL,
  17608.         COLUMN_DEF nvarchar(127) NULL,
  17609.         SQL_DATA_TYPE smallint null,
  17610.         SQL_DATETIME_SUB smallint NULL,
  17611.         CHAR_OCTET_LENGTH int NULL,
  17612.         ORDINAL_POSITION smallint,
  17613.         IS_NULLABLE char(4),
  17614.         SS_DATA_TYPE tinyint null,
  17615.         COLUMN_FLAGS int)    
  17616.  
  17617.     insert into #tmp_columns
  17618.     select
  17619.         TABLE_CAT = c.TABLE_CATALOG,
  17620.         TABLE_SCHEM = c.TABLE_SCHEMA,
  17621.         TABLE_NAME = c.TABLE_NAME,
  17622.         COLUMN_NAME = c.COLUMN_NAME,
  17623.         DATA_TYPE = 
  17624.         case c.DATA_TYPE & ~@DBTYPE_BYREF
  17625.             when @DBTYPE_I2                then @ODBC_SMALLINT
  17626.             when @DBTYPE_I4                then @ODBC_INT
  17627.             when @DBTYPE_R4                then @ODBC_REAL
  17628.             when @DBTYPE_R8                then @ODBC_FLOAT
  17629.             when @DBTYPE_CY                then @ODBC_MONEY
  17630.             when @DBTYPE_DATE            then @ODBC_DATETIME 
  17631.             when @DBTYPE_DBDATE            then @ODBC_DATETIME    
  17632.             when @DBTYPE_DBTIME            then @ODBC_DATETIME    
  17633.             when @DBTYPE_DBTIMESTAMP    then @ODBC_DATETIME
  17634.             when @DBTYPE_BOOL            then @ODBC_BIT
  17635.             when @DBTYPE_VARIANT        then @ODBC_NVARCHAR
  17636.             when @DBTYPE_DECIMAL        then @ODBC_DECIMAL
  17637.             when @DBTYPE_UI1            then @ODBC_TINYINT
  17638.             when @DBTYPE_I1                then @ODBC_NUMERIC
  17639.             when @DBTYPE_UI2            then @ODBC_NUMERIC
  17640.             when @DBTYPE_UI4            then @ODBC_NUMERIC
  17641.             when @DBTYPE_I8                then @ODBC_NUMERIC
  17642.             when @DBTYPE_UI8            then @ODBC_NUMERIC
  17643.             when @DBTYPE_NUMERIC        then @ODBC_NUMERIC
  17644.             when @DBTYPE_GUID            then @ODBC_GUID
  17645.             when @DBTYPE_BYTES then  
  17646.                 case 
  17647.                     when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG ) or (p.COLUMN_SIZE > 8000) then @ODBC_IMAGE
  17648.                     when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISROWVER) = @DBCOLUMNFLAGS_ISROWVER AND
  17649.                             (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH) = @DBCOLUMNFLAGS_ISFIXEDLENGTH and
  17650.                              p.COLUMN_SIZE = 8
  17651.                         then @ODBC_TIMESTAMP
  17652.                     when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH)
  17653.                         then @ODBC_BINARY
  17654.                     else @ODBC_VARBINARY
  17655.                 end
  17656.              when @DBTYPE_STR  then
  17657.                 case
  17658.                     when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG ) or (p.COLUMN_SIZE > 8000) then @ODBC_TEXT
  17659.                     when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_CHAR
  17660.                     else @ODBC_VARCHAR
  17661.                 end
  17662.             when @DBTYPE_WSTR then
  17663.                 case
  17664.                     when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG) or (p.COLUMN_SIZE > 4000) then @ODBC_NTEXT
  17665.                     when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_NCHAR
  17666.                     else @ODBC_NVARCHAR
  17667.                 end
  17668.             when @DBTYPE_BSTR then
  17669.                 case
  17670.                     when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG) or (p.COLUMN_SIZE > 4000) then @ODBC_NTEXT
  17671.                     when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_NCHAR
  17672.                     else @ODBC_NVARCHAR
  17673.                 end
  17674.         end,
  17675.         TYPE_NAME = NULL,
  17676.         COLUMN_SIZE = 
  17677.             COALESCE(c.CHARACTER_MAXIMUM_LENGTH, c.NUMERIC_PRECISION, p.COLUMN_SIZE),        
  17678.         BUFFER_LENGTH = isnull(c.CHARACTER_OCTET_LENGTH,0),
  17679.         DECIMAL_DIGITS = c.NUMERIC_SCALE,
  17680.         NUM_PREC_RADIX = NULL,
  17681.         NULLABLE = convert(smallint, c.IS_NULLABLE),
  17682.         REMARKS = convert(nvarchar(255),c.DESCRIPTION),
  17683.         COLUMN_DEF = c.COLUMN_DEFAULT,
  17684.         SQL_DATA_TYPE = NULL,
  17685.         SQL_DATETIME_SUB = convert(smallint, c.DATETIME_PRECISION),
  17686.         CHAR_OCTET_LENGTH = c.CHARACTER_OCTET_LENGTH,
  17687.         ORDINAL_POSITION = c.ORDINAL_POSITION,
  17688.         IS_NULLABLE = 
  17689.         case c.IS_NULLABLE
  17690.             when 1 then 'YES'
  17691.             else 'NO'
  17692.         end,
  17693.         SS_DATA_TYPE = NULL,
  17694.         COLUMN_FLAGS = c.COLUMN_FLAGS
  17695.     from master.dbo.SYSREMOTE_COLUMNS <
  17696.                 @table_server,
  17697.                 @table_catalog,
  17698.                 @table_schema,
  17699.                 @table_name,
  17700.                 @column_name > c
  17701.         LEFT OUTER JOIN
  17702.          master.dbo.SYSREMOTE_PROVIDER_TYPES <
  17703.                 @table_server > p
  17704.                 /* LUXOR/KAGERA dont support restricting DATA_TYPE or BEST_MATCH */
  17705.         ON c.DATA_TYPE = p.DATA_TYPE AND p.BEST_MATCH = 1
  17706.  
  17707.  
  17708.     update #tmp_columns
  17709.     set SQL_DATA_TYPE = spt_dt.SQL_DATA_TYPE,
  17710.         TYPE_NAME = 
  17711.         case 
  17712.             when #tmp_columns.DATA_TYPE = @ODBC_BINARY and (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISROWVER) = @DBCOLUMNFLAGS_ISROWVER AND
  17713.                     (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH) = @DBCOLUMNFLAGS_ISFIXEDLENGTH and
  17714.                     BUFFER_LENGTH = 8
  17715.                 then 'timestamp'
  17716.             when #tmp_columns.DATA_TYPE = @ODBC_BINARY and (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH)
  17717.                 then 'binary'
  17718.             else spt_dt.TYPE_NAME
  17719.         end,
  17720.         DECIMAL_DIGITS = COALESCE(DECIMAL_DIGITS,spt_dt.numeric_scale ),
  17721.         NUM_PREC_RADIX = spt_dt.RADIX,
  17722.         SS_DATA_TYPE = 
  17723.         case
  17724.             when #tmp_columns.DATA_TYPE in ( @ODBC_TINYINT, @ODBC_SMALLINT, @ODBC_INT ) 
  17725.                 and #tmp_columns.NULLABLE = 1
  17726.                     then @ODS_INT_NULL
  17727.             when #tmp_columns.DATA_TYPE = @ODBC_TINYINT and #tmp_columns.NULLABLE = 0 
  17728.                 then @ODS_TINYINT_NOT_NULL
  17729.             when #tmp_columns.DATA_TYPE = @ODBC_SMALLINT    and #tmp_columns.NULLABLE = 0
  17730.                 then @ODS_SMALLINT_NOT_NULL
  17731.             when #tmp_columns.DATA_TYPE = @ODBC_INT  and #tmp_columns.NULLABLE = 0
  17732.                 then @ODS_INT_NOT_NULL
  17733.             when #tmp_columns.DATA_TYPE in (@ODBC_REAL, @ODBC_FLOAT) and #tmp_columns.NULLABLE = 1
  17734.                 then @ODS_FLOAT_NULL
  17735.             when #tmp_columns.DATA_TYPE = @ODBC_REAL and #tmp_columns.NULLABLE = 0
  17736.                 then @ODS_REAL_NOT_NULL
  17737.             when #tmp_columns.DATA_TYPE = @ODBC_FLOAT and #tmp_columns.NULLABLE = 0
  17738.                 then @ODS_FLOAT_NOT_NULL
  17739.             when #tmp_columns.DATA_TYPE = @ODBC_MONEY and #tmp_columns.NULLABLE = 1 
  17740.                 then @ODS_MONEY_NULL
  17741.             when #tmp_columns.DATA_TYPE = @ODBC_MONEY and #tmp_columns.NULLABLE = 0 
  17742.                 then @ODS_MONEY_NOT_NULL
  17743.             when #tmp_columns.DATA_TYPE = @ODBC_DATETIME and #tmp_columns.NULLABLE = 1
  17744.                 then @ODS_DATETIME_NULL
  17745.             when #tmp_columns.DATA_TYPE = @ODBC_DATETIME and #tmp_columns.NULLABLE = 0
  17746.                 then @ODS_DATETIME_NOT_NULL
  17747.             when #tmp_columns.DATA_TYPE in (@ODBC_NTEXT, @ODBC_TEXT) then @ODS_TEXT
  17748.             when #tmp_columns.DATA_TYPE = @ODBC_BIT then @ODS_BIT
  17749.             when #tmp_columns.DATA_TYPE = @ODBC_DECIMAL then  @ODS_DECIMAL    
  17750.             when #tmp_columns.DATA_TYPE = @ODBC_NUMERIC then  @ODS_NUMERIC
  17751.             when #tmp_columns.DATA_TYPE = @ODBC_GUID then @ODS_GUID
  17752.             when #tmp_columns.DATA_TYPE = @ODBC_IMAGE then  @ODS_IMAGE
  17753.             when #tmp_columns.DATA_TYPE = @ODBC_TIMESTAMP
  17754.                 and (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISROWVER) = @DBCOLUMNFLAGS_ISROWVER AND
  17755.                     (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH) = @DBCOLUMNFLAGS_ISFIXEDLENGTH and
  17756.                     BUFFER_LENGTH = 8
  17757.                 then @ODS_TIMESTAMP
  17758.             when #tmp_columns.DATA_TYPE = @ODBC_BINARY and #tmp_columns.NULLABLE =    1
  17759.                 and (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH)
  17760.                 then  @ODS_BINARY_NULL
  17761.             when #tmp_columns.DATA_TYPE = @ODBC_BINARY and #tmp_columns.NULLABLE =    0
  17762.                 and (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH)
  17763.                 then @ODS_BINARY_NOT_NULL
  17764.             when #tmp_columns.DATA_TYPE = @ODBC_VARBINARY
  17765.                 then @ODS_VARBINARY    
  17766.             when #tmp_columns.DATA_TYPE in (@ODBC_CHAR, @ODBC_NCHAR) and #tmp_columns.NULLABLE = 1
  17767.                 then @ODS_CHAR_NULL
  17768.             when #tmp_columns.DATA_TYPE in (@ODBC_CHAR, @ODBC_NCHAR) and #tmp_columns.NULLABLE = 0
  17769.                 then @ODS_CHAR_NOT_NULL
  17770.             when #tmp_columns.DATA_TYPE in (@ODBC_VARCHAR, @ODBC_NVARCHAR) then @ODS_VARCHAR
  17771.             else null
  17772.         end
  17773.         from master.dbo.spt_datatype_info spt_dt
  17774.         where spt_dt.DATA_TYPE = #tmp_columns.DATA_TYPE
  17775.             and spt_dt.ODBCVer in (null,@ODBCVer)
  17776.             and spt_dt.AUTO_INCREMENT in ( 0, null)
  17777.             and not spt_dt.TYPE_NAME = 'smalldatetime'
  17778.  
  17779.         select
  17780.             TABLE_CAT,            TABLE_SCHEM,        TABLE_NAME,
  17781.             COLUMN_NAME,        DATA_TYPE,            TYPE_NAME,
  17782.             COLUMN_SIZE,        BUFFER_LENGTH,        DECIMAL_DIGITS,
  17783.             NUM_PREC_RADIX,        NULLABLE,            REMARKS,
  17784.             COLUMN_DEF,            SQL_DATA_TYPE,        SQL_DATETIME_SUB,
  17785.             CHAR_OCTET_LENGTH,    ORDINAL_POSITION,    IS_NULLABLE,
  17786.             SS_DATA_TYPE
  17787.         from #tmp_columns
  17788.         order by TABLE_CAT, TABLE_SCHEM, TABLE_NAME, ORDINAL_POSITION
  17789.  
  17790.         drop table #tmp_columns
  17791.  
  17792. go
  17793. grant execute on sp_columns_ex to public
  17794. go
  17795.  
  17796.  
  17797. if object_id('sp_table_privileges_ex', 'P') is not null
  17798.     drop proc sp_table_privileges_ex
  17799. go
  17800. raiserror(15339,-1,-1,'sp_table_privileges_ex')
  17801. go
  17802. create procedure sp_table_privileges_ex(
  17803.     @table_server        sysname,
  17804.     @table_name            sysname = null,
  17805.     @table_schema        sysname = null,
  17806.     @table_catalog        sysname = null)
  17807. as
  17808.     select
  17809.         TABLE_CAT = TABLE_CATALOG,
  17810.         TABLE_SCHEM = TABLE_SCHEMA,
  17811.         TABLE_NAME = TABLE_NAME,
  17812.         GRANTOR = GRANTOR,
  17813.         GRANTEE = GRANTEE,
  17814.         PRIVILEGE = PRIVILEGE_TYPE,
  17815.         IS_GRANTABLE = 
  17816.         case IS_GRANTABLE
  17817.             when 1 then 'YES'
  17818.             when 0 then 'NO'
  17819.             else null
  17820.         end
  17821.     from master.dbo.SYSREMOTE_TABLE_PRIVILEGES <
  17822.                 @table_server,
  17823.                 @table_catalog,
  17824.                 @table_schema,
  17825.                 @table_name >
  17826.     order by TABLE_CAT, TABLE_SCHEM, TABLE_NAME, PRIVILEGE
  17827. go
  17828. grant execute on sp_table_privileges_ex to public
  17829. go
  17830.  
  17831.  
  17832. if object_id('sp_column_privileges_ex', 'P') is not null
  17833.     drop proc sp_column_privileges_ex
  17834. go
  17835. raiserror(15339,-1,-1,'sp_column_privileges_ex')
  17836. go
  17837. create procedure sp_column_privileges_ex(
  17838.     @table_server        sysname,
  17839.     @table_name            sysname = null,
  17840.     @table_schema        sysname = null,
  17841.     @table_catalog        sysname = null,
  17842.     @column_name        sysname = null)
  17843. as
  17844.     select
  17845.         TABLE_CAT = TABLE_CATALOG,
  17846.         TABLE_SCHEM = TABLE_SCHEMA,
  17847.         TABLE_NAME = TABLE_NAME,
  17848.         COLUMN_NAME = COLUMN_NAME,
  17849.         GRANTOR = GRANTOR,
  17850.         GRANTEE = GRANTEE,
  17851.         PRIVILEGE = PRIVILEGE_TYPE,
  17852.         IS_GRANTABLE = 
  17853.         case IS_GRANTABLE
  17854.             when 1 then 'YES'
  17855.             when 0 then 'NO'
  17856.             else null
  17857.         end
  17858.     from master.dbo.SYSREMOTE_COLUMN_PRIVILEGES <
  17859.                 @table_server,
  17860.                 @table_catalog,
  17861.                 @table_schema,
  17862.                 @table_name,
  17863.                 @column_name >
  17864.     order by TABLE_CAT, TABLE_SCHEM, TABLE_NAME, COLUMN_NAME, PRIVILEGE
  17865. go
  17866. grant execute on sp_column_privileges_ex to public
  17867. go
  17868.  
  17869.  
  17870. if object_id('sp_indexes', 'P') is not null
  17871.     drop proc sp_indexes
  17872. go
  17873. raiserror(15339,-1,-1,'sp_indexes')
  17874. go
  17875. create procedure sp_indexes(
  17876.     @table_server        sysname,
  17877.     @table_name            sysname = null,
  17878.     @table_schema        sysname = null,
  17879.     @table_catalog        sysname = null,
  17880.     @index_name            sysname = null,
  17881.     @is_unique          bit = null)
  17882. as
  17883.     select
  17884.         TABLE_CAT = TABLE_CATALOG,
  17885.         TABLE_SCHEM = TABLE_SCHEMA,
  17886.         TABLE_NAME = TABLE_NAME,
  17887.         NON_UNIQUE = convert(smallint, 1 - [UNIQUE]),
  17888.         INDEX_QUALIFIER = TABLE_NAME,
  17889.         INDEX_NAME = INDEX_NAME,
  17890.         TYPE = 
  17891.         case [CLUSTERED]
  17892.             when 1 then 1
  17893.             else 3
  17894.         end,
  17895.         ORDINAL_POSITION = ORDINAL_POSITION,
  17896.         COLUMN_NAME = COLUMN_NAME,
  17897.         ASC_OR_DESC = 
  17898.         case [COLLATION]
  17899.             when 1 then 'A'
  17900.             when 2 then 'D'
  17901.             else null
  17902.         end,
  17903.         CARDINALITY = CARDINALITY,
  17904.         PAGES = 
  17905.         case [CLUSTERED]
  17906.             when 1 then PAGES
  17907.             else NULL
  17908.         end,
  17909.         FILTER_CONDITION = FILTER_CONDITION
  17910.     from master.dbo.SYSREMOTE_INDEXES <
  17911.                 @table_server,
  17912.                 @table_catalog,
  17913.                 @table_schema,
  17914.                 @index_name,
  17915.                 NULL,            /* TYPE (index type) */
  17916.                 @table_name >
  17917.     where @is_unique is null or @is_unique = [UNIQUE]
  17918.     order by NON_UNIQUE, TYPE, INDEX_QUALIFIER, INDEX_NAME, ORDINAL_POSITION
  17919. go
  17920. grant execute on sp_indexes to public
  17921. go
  17922.  
  17923.  
  17924. if object_id('sp_foreignkeys', 'P') is not null
  17925.     drop proc sp_foreignkeys
  17926. go
  17927. raiserror(15339,-1,-1,'sp_foreignkeys')
  17928. go
  17929. create procedure sp_foreignkeys(
  17930.     @table_server        sysname,
  17931.     @pktab_name         sysname = null,
  17932.     @pktab_schema       sysname = null,
  17933.     @pktab_catalog      sysname = null,
  17934.     @fktab_name         sysname = null,
  17935.     @fktab_schema       sysname = null,
  17936.     @fktab_catalog      sysname = null)
  17937. as
  17938.     select
  17939.         PKTABLE_CAT = PK_TABLE_CATALOG,
  17940.         PKTABLE_SCHEM = PK_TABLE_SCHEMA,
  17941.         PKTABLE_NAME = PK_TABLE_NAME,
  17942.         PKCOLUMN_NAME = PK_COLUMN_NAME,
  17943.         FKTABLE_CAT = FK_TABLE_CATALOG,
  17944.         FKTABLE_SCHEM = FK_TABLE_SCHEMA,
  17945.         FKTABLE_NAME = FK_TABLE_NAME,
  17946.         FKCOLUMN_NAME = FK_COLUMN_NAME,
  17947.         KEY_SEQ = ORDINAL,
  17948.         UPDATE_RULE = 
  17949.         case UPDATE_RULE
  17950.             when 'CASCADE' then 0
  17951.             when 'NO ACTION' then 1
  17952.             when 'SET NULL' then 2
  17953.             else null
  17954.         end,
  17955.         DELETE_RULE = 
  17956.         case DELETE_RULE
  17957.             when 'CASCADE' then 0
  17958.             when 'NO ACTION' then 1
  17959.             when 'SET NULL' then 2
  17960.             else null
  17961.         end,
  17962.         FK_NAME = convert(sysname, NULL),
  17963.         PK_NAME = convert(sysname, NULL),
  17964.         DEFERRABILITY = convert(smallint, null)
  17965.  
  17966.     from master.dbo.SYSREMOTE_FOREIGN_KEYS <
  17967.                 @table_server,
  17968.                 @pktab_catalog,
  17969.                 @pktab_schema,
  17970.                 @pktab_name,
  17971.                 @fktab_catalog,
  17972.                 @fktab_schema,
  17973.                 @fktab_name >
  17974.     order by FKTABLE_CAT, FKTABLE_SCHEM, FKTABLE_NAME, KEY_SEQ
  17975. go
  17976. grant execute on sp_foreignkeys to public
  17977. go
  17978.  
  17979.  
  17980. if object_id('sp_primarykeys', 'P') is not null
  17981.     drop proc sp_primarykeys
  17982. go
  17983. raiserror(15339,-1,-1,'sp_primarykeys')
  17984. go
  17985. create procedure sp_primarykeys(
  17986.     @table_server        sysname,
  17987.     @table_name         sysname = null,
  17988.     @table_schema       sysname = null,
  17989.     @table_catalog      sysname = null)
  17990. as
  17991.     select
  17992.         TABLE_CAT = TABLE_CATALOG,
  17993.         TABLE_SCHEM = TABLE_SCHEMA,
  17994.         TABLE_NAME = TABLE_NAME,
  17995.         COLUMN_NAME = COLUMN_NAME,
  17996.         KEY_SEQ = ORDINAL,
  17997.         PK_NAME = convert(sysname, NULL)
  17998.     from master.dbo.SYSREMOTE_PRIMARY_KEYS <
  17999.                 @table_server,
  18000.                 @table_catalog,
  18001.                 @table_schema,
  18002.                 @table_name >
  18003.     order by TABLE_CAT, TABLE_SCHEM, TABLE_NAME, KEY_SEQ
  18004. go
  18005. grant execute on sp_primarykeys to public
  18006. go
  18007.  
  18008. /*---------------------------- END OLEDB CATALOG PROCS ------------------------*/
  18009.  
  18010.  
  18011. dump tran master with no_log
  18012. go
  18013.  
  18014.  
  18015.  
  18016.  
  18017. if (exists (select * from sysobjects
  18018.         where name = 'sp_configure' and type = 'P '))
  18019.     begin
  18020.         exec sp_configure 'allow updates',0
  18021.         reconfigure with override
  18022.     end
  18023. go
  18024.  
  18025. exec sp_MS_upd_sysobj_category 2 /* set category | 2 based on crdate. */
  18026.  
  18027. go
  18028.  
  18029. if exists (select * from sysobjects where name = 'sp_check_objects'
  18030.             and type = 'P ')
  18031.     begin
  18032.         /* Only supported on 6.0 servers */
  18033.         print ''
  18034.         print 'Checking objects created by instcat.sql.'
  18035.  
  18036.         exec sp_check_objects 'catalog'
  18037.     end
  18038. go
  18039.  
  18040. print ''
  18041. print 'instcat.sql completed successfully.'
  18042. go
  18043.  
  18044. set quoted_identifier off
  18045. go
  18046.  
  18047. dump tran master with no_log
  18048. go
  18049. checkpoint
  18050. go
  18051. /**/
  18052.