home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 February / CHIP_2_98.iso / software / pelne / optionp / sqlsrv.cab / instcat.sql < prev    next >
Text File  |  1997-10-24  |  655KB  |  20,637 lines

  1.  
  2. /*
  3. **    INSTCAT.SQL
  4. **    Installs catalog stored procedures on the Microsoft SQL Server.
  5. **    Copyright 1992-1997, Microsoft Corp.    All rights reserved.
  6. */
  7.  
  8. /*
  9. NOTE:  you MUST change the last row inserted into spt_server_info
  10. to be version number of this file.    the convention is j.nn.bbb, where
  11. jj is the major version number ('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. if (exists (select * from sysobjects
  66.         where name = 'spt_provider_types' and type = 'U '))
  67.     drop table spt_provider_types
  68. go
  69. /*
  70. ** If tables or procs already exist, drop them.
  71. */
  72.  
  73. if (exists (select * from sysobjects
  74.         where name = 'spt_datatype_info' and type = 'U '))
  75.     drop table spt_datatype_info
  76. go
  77. if (exists (select * from sysobjects
  78.         where name = 'spt_datatype_info_ext' and type = 'U '))
  79.     drop table spt_datatype_info_ext
  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 = 'sp_catalogs_rowset' and type = 'P '))
  155.     drop procedure sp_catalogs_rowset
  156. go
  157. if (exists (select * from sysobjects
  158.         where name = 'sp_column_privileges_rowset' and type = 'P '))
  159.     drop procedure sp_column_privileges_rowset
  160. go
  161. if (exists (select * from sysobjects
  162.         where name = 'sp_columns_rowset' and type = 'P '))
  163.     drop procedure sp_columns_rowset
  164. go
  165. if (exists (select * from sysobjects
  166.         where name = 'sp_foreign_keys_rowset' and type = 'P '))
  167.     drop procedure sp_foreign_keys_rowset
  168. go
  169. if (exists (select * from sysobjects
  170.         where name = 'sp_indexes_rowset' and type = 'P '))
  171.     drop procedure sp_indexes_rowset
  172. go
  173. if (exists (select * from sysobjects
  174.         where name = 'sp_primary_keys_rowset' and type = 'P '))
  175.     drop procedure sp_primary_keys_rowset
  176. go
  177. if (exists (select * from sysobjects
  178.         where name = 'sp_procedure_params_rowset' and type = 'P '))
  179.     drop procedure sp_procedure_params_rowset
  180. go
  181. if (exists (select * from sysobjects
  182.         where name = 'sp_procedures_rowset' and type = 'P '))
  183.     drop procedure sp_procedures_rowset
  184. go
  185. if (exists (select * from sysobjects
  186.         where name = 'sp_provider_types_rowset' and type = 'P '))
  187.     drop procedure sp_provider_types_rowset
  188. go
  189. if (exists (select * from sysobjects
  190.         where name = 'sp_schemata_rowset' and type = 'P '))
  191.     drop procedure sp_schemata_rowset
  192. go
  193. if (exists (select * from sysobjects
  194.         where name = 'sp_statistics_rowset' and type = 'P '))
  195.     drop procedure sp_statistics_rowset
  196. go
  197. if (exists (select * from sysobjects
  198.         where name = 'sp_tables_rowset' and type = 'P '))
  199.     drop procedure sp_tables_rowset
  200. go
  201. if (exists (select * from sysobjects
  202.         where name = 'sp_table_privileges_rowset' and type = 'P '))
  203.     drop proc sp_table_privileges_rowset
  204. go
  205.  
  206. dump tran master with no_log
  207. go
  208.  
  209. print 'creating table spt_datatype_info_ext'
  210. go
  211. create table spt_datatype_info_ext (
  212.                 user_type        smallint    not null,
  213.                 CREATE_PARAMS    varchar(32) null,
  214.                 AUTO_INCREMENT    smallint null)
  215. go
  216.  
  217. grant select on spt_datatype_info_ext to public
  218. go
  219.  
  220.  
  221. insert into spt_datatype_info_ext
  222.     /* CHAR      user_type, create_params, auto_increment */
  223.     values             (1,    'length' ,0)
  224.  
  225. insert into spt_datatype_info_ext
  226.     /* VARCHAR     user_type, create_params, auto_increment */
  227.     values             (2,    'max length' ,0)
  228.  
  229. insert into spt_datatype_info_ext
  230.     /* BINARY     user_type, create_params, auto_increment */
  231.     values             (3,    'length' ,0)
  232.  
  233. insert into spt_datatype_info_ext
  234.     /* VARBINARY user_type, create_params, auto_increment */
  235.     values             (4,    'max length' ,0)
  236.  
  237. if    (charindex('6.00', @@version) > 0 or
  238.      charindex('6.50', @@version) > 0 or
  239.      charindex('7.00', @@version) > 0)
  240. begin    /*    Add 6.0 data types */
  241.     insert into spt_datatype_info_ext
  242.         /* DECIMAL user_type, create_params, auto_increment */
  243.         values             (26,    'precision,scale' ,0)
  244.  
  245.     insert into spt_datatype_info_ext
  246.         /* NUMERIC user_type, create_params, auto_increment */
  247.         values             (25,    'precision,scale' ,0)
  248.  
  249.     insert into spt_datatype_info_ext
  250.         /* DECIMAL IDENTITY user_type, create_params, auto_increment */
  251.         values             (26,    'precision' ,1)
  252.  
  253.     insert into spt_datatype_info_ext
  254.         /* NUMERIC IDENTITY user_type, create_params, auto_increment */
  255.         values             (25,    'precision' ,1)
  256.  
  257. end
  258. else    /*    Pre 6.0 server, add SYSNAME create param */
  259.     begin
  260.         insert into spt_datatype_info_ext
  261.             /* SYSNAME     user_type, create_param, auto_increments */
  262.             values             (18,    'max length' ,0)
  263.  
  264.     end
  265. go
  266.  
  267. if (charindex('7.00', @@version) > 0)
  268. begin    /*    Update usertypes for 7.00 server */
  269.     begin tran
  270.     insert into spt_datatype_info_ext
  271.         /* NCHAR     user_type, create_params, auto_increment */
  272.         values             (143,    'length' ,0)
  273.  
  274.     insert into spt_datatype_info_ext
  275.         /* NVARCHAR     user_type, create_params, auto_increment */
  276.         values             (146,    'max length' ,0)
  277.  
  278.     update spt_datatype_info_ext set user_type = 141 where user_type = 3    /* binary */
  279.     update spt_datatype_info_ext set user_type = 142 where user_type = 1    /* char */
  280.     update spt_datatype_info_ext set user_type = 139 where user_type = 26    /* decimaln */
  281.     update spt_datatype_info_ext set user_type = 140 where user_type = 25    /* numericn */
  282.     update spt_datatype_info_ext set user_type = 144 where user_type = 4    /* varbinary */
  283.     update spt_datatype_info_ext set user_type = 145 where user_type = 2    /* varchar */
  284.     commit tran
  285. end
  286. go
  287.  
  288. create unique clustered index datatypeinfoextclust on spt_datatype_info_ext(user_type,AUTO_INCREMENT)
  289. go
  290.  
  291. print 'creating table spt_datatype_info'
  292. go
  293. if    (charindex('7.00', @@version) = 0)
  294. begin    /*    Pre 7.0 server */
  295. create table spt_datatype_info (
  296.     ss_dtype            tinyint     not null,
  297.     fixlen                int         null,        /* datatype len for variable, else null */
  298.     ODBCVer             tinyint     null,        /* version if needed, else null */
  299.     TYPE_NAME            varchar(32)    not null,
  300.     DATA_TYPE            smallint    not null,
  301.     data_precision        int         null,
  302.     numeric_scale        smallint    null,    /* min scale if 6.0 */
  303.     RADIX                smallint    null,
  304.     length                int         null,
  305.     LITERAL_PREFIX        varchar(32) null,
  306.     LITERAL_SUFFIX        varchar(32) null,
  307.     CREATE_PARAMS        varchar(32) null,
  308.     NULLABLE            smallint    not null,
  309.     CASE_SENSITIVE        smallint    not null,
  310.     SEARCHABLE            smallint    not null,
  311.     UNSIGNED_ATTRIBUTE    smallint    null,
  312.     MONEY                smallint    not null,
  313.     AUTO_INCREMENT        smallint    null,
  314.     LOCAL_TYPE_NAME     varchar(32)    null,
  315.     charbin             tinyint     null, /* 0 for char/binary types, NULL for all others */
  316.     SQL_DATA_TYPE        smallint    not null,
  317.     SQL_DATETIME_SUB    smallint    null)
  318. end
  319. go
  320. if    ( charindex('7.00', @@version) > 0)
  321. begin    /*    7.0 and later server */
  322. create table spt_datatype_info (
  323.     ss_dtype            tinyint     not null,
  324.     fixlen                int         null,        /* datatype len for variable, else null */
  325.     ODBCVer             tinyint     null,        /* version if needed, else null */
  326.     TYPE_NAME            sysname        not null,
  327.     DATA_TYPE            smallint    not null,
  328.     data_precision        int         null,
  329.     numeric_scale        smallint    null,    /* min scale if 6.0 */
  330.     RADIX                smallint    null,
  331.     length                int         null,
  332.     LITERAL_PREFIX        varchar(32) null,
  333.     LITERAL_SUFFIX        varchar(32) null,
  334.     CREATE_PARAMS        varchar(32) null,
  335.     NULLABLE            smallint    not null,
  336.     CASE_SENSITIVE        smallint    not null,
  337.     SEARCHABLE            smallint    not null,
  338.     UNSIGNED_ATTRIBUTE    smallint    null,
  339.     MONEY                smallint    not null,
  340.     AUTO_INCREMENT        smallint    null,
  341.     LOCAL_TYPE_NAME     sysname     null,
  342.     charbin             tinyint     null, /* 0 for char/binary types, NULL for all others */
  343.     SQL_DATA_TYPE        smallint    not null,
  344.     SQL_DATETIME_SUB    smallint    null)
  345. end
  346. go
  347.  
  348. grant select on spt_datatype_info to public
  349. go
  350.  
  351. /* Get case sensitivity */
  352. /*(leih 10/8/96) SID: Part I. break into two batches to avoid OOM error */
  353. if 'A' = 'A' /* create dummy begin block */
  354. begin
  355.     declare @case smallint
  356.  
  357.     begin tran
  358.     select @case = 0
  359.     select @case = 1 where 'a' <> 'A'
  360.  
  361.     /* Local Binary */
  362.     insert into spt_datatype_info values
  363.     (45,null,null,'binary',-2,null,null,null,null,'0x',null,'length',1,0,2,null,0,null,'binary',0,-2,null)
  364.  
  365.     /* Local Bit */
  366.     insert into spt_datatype_info values
  367.     (50,null,null,'bit',-7,1,0,2,null,null,null,null,0,0,2,null,0,null,'bit',null,-7,null)
  368.  
  369.     /* Local Char */
  370.     insert into spt_datatype_info values
  371.     (47,null,null,'char',1,null,null,null,null,'''','''','length',1,@case,3,null,0,null,'char',0,1,null)
  372.  
  373.     /* Local Datetime */
  374.     insert into spt_datatype_info values
  375.     (61,8,2,'datetime',11,23,3,10,16,'''','''',null,1,0,3,null,0,null,'datetime',null,9,3)
  376.     insert into spt_datatype_info values
  377.     (61,8,3,'datetime',93,23,3,10,16,'''','''',null,1,0,3,null,0,null,'datetime',null,9,3)
  378.  
  379.     /* Local Smalldatetime */
  380.     insert into spt_datatype_info values
  381.     (58,4,2,'smalldatetime',11,16,0,10,16,'''','''',null,1,0,3,null,0,null,'smalldatetime',null,9,3)
  382.     insert into spt_datatype_info values
  383.     (58,4,3,'smalldatetime',93,16,0,10,16,'''','''',null,1,0,3,null,0,null,'smalldatetime',null,9,3)
  384.  
  385.     /* Local Float */
  386.     insert into spt_datatype_info values
  387.     (62,8,2,'float',6,15,null,10,8,null,null,null,1,0,2,0,0,0,'float',null,6,null)
  388.     insert into spt_datatype_info values
  389.     (62,8,3,'float',6,53,null, 2,8,null,null,null,1,0,2,0,0,0,'float',null,6,null)
  390.  
  391.     /* Local Real */
  392.     insert into spt_datatype_info values
  393.     (59,4,2,'real',7, 7,null,10,4,null,null,null,1,0,2,0,0,0,'real',null,7,null)
  394.     insert into spt_datatype_info values
  395.     (59,4,3,'real',7,24,null, 2,4,null,null,null,1,0,2,0,0,0,'real',null,7,null)
  396.  
  397.     /* Local Smallmoney */
  398.     insert into spt_datatype_info values
  399.     (122,4,null,'smallmoney',3,10,4,10,12,'$',null,null,1,0,2,0,1,0,'smallmoney',null,3,null)
  400.  
  401.     /* Local Money */
  402.     insert into spt_datatype_info values
  403.     (60,8,null,'money',3,19,4,10,21,'$',null,null,1,0,2,0,1,0,'money',null,3,null)
  404.  
  405.     /* Local Int */
  406.     insert into spt_datatype_info values
  407.     (56,4,null,'int',4,10,0,10,4,null,null,null,1,0,2,0,0,0,'int',null,4,null)
  408.  
  409.     commit tran
  410. end
  411. go
  412.  
  413. /*(leih 10/8/96) SID: Part II. break into two batches to avoid OOM error */
  414. if 'A' = 'A' /* create dummy begin block */
  415. begin
  416.     declare @case smallint
  417.  
  418.     begin tran
  419.     select @case = 0
  420.     select @case = 1 where 'a' <> 'A'
  421.  
  422.  
  423.     /* Local Smallint */
  424.     insert into spt_datatype_info values
  425.     (52,2,null,'smallint',5,5,0,10,2,null,null,null,1,0,2,0,0,0,'smallint',null,5,null)
  426.     insert into spt_datatype_info values
  427.     (52,2,1,'smallint',5,5,0,10,2,null,null,null,1,0,2,0,0,0,'smallint',null,5,null)
  428.  
  429.     /* Local Tinyint */
  430.     insert into spt_datatype_info values
  431.     (48,1,null,'tinyint',-6,3,0,10,1,null,null,null,1,0,2,1,0,0,'tinyint',null,-6,null)
  432.  
  433.     /* Local Text */
  434.     insert into spt_datatype_info values
  435.     (35,null,null,'text',-1,2147483647,null,null,2147483647,'''','''',null,1,@case,1,null,0,null,'text',0,-1,null)
  436.  
  437.     /* Local Varbinary */
  438.     insert into spt_datatype_info values
  439.     (37,null,null,'varbinary',-3,null,null,null,null,'0x',null,'max length',1,0,2,null,0,null,'varbinary',0,-3,null)
  440.  
  441.     /* Local Varchar */
  442.     insert into spt_datatype_info values
  443.     (39,null,null,'varchar',12,null,null,null,null,'''','''','max length',1,@case,3,null,0,null,'varchar',0,12,null)
  444.  
  445.     /* Local Image */
  446.     insert into spt_datatype_info values
  447.     (34,null,null,'image',-4,2147483647,null,null,2147483647,'0x',null,null,1,0,0,null,0,null,'image',0,-4,null)
  448.  
  449.     if (charindex('6.00', @@version) > 0 or
  450.         charindex('6.50', @@version) > 0 or
  451.         charindex('7.00', @@version) > 0)
  452.     begin    /*    Add 6.0 data types */
  453.         /* Local Decimal */
  454.         insert into spt_datatype_info values    /* sql server type is 'decimaln' */
  455.         (55,null,null,'decimal',3,38,0,10,null,null,null,'precision,scale',1,0,2,0,0,0,'decimal',null,3,null)
  456.  
  457.         /* Local Numeric */
  458.         insert into spt_datatype_info values    /* sql server type is 'numericn' */
  459.         (63,null,null,'numeric',2    ,38,0,10,null,null,null,'precision,scale',1,0,2,0,0,0,'numeric',null,2,null)
  460.  
  461.         /* Identity attribute data types */
  462.  
  463.         /* Identity Int */
  464.         insert into spt_datatype_info values
  465.         (56,null,null,'int identity',4,10,0,10,null,null,null,null,0,0,2,0,0,1,'int identity',null,4,null)
  466.  
  467.         /* Identity Smallint */
  468.         insert into spt_datatype_info values
  469.         (52,null,null,'smallint identity',5,5,0,10,null,null,null,null,0,0,2,0,0,1,'smallint identity',null,5,null)
  470.  
  471.         /* Identity Tinyint */
  472.         insert into spt_datatype_info values
  473.         (48,null,null,'tinyint identity',-6,3,0,10,null,null,null,null,0,0,2,1,0,1,'tinyint identity',null,-6,null)
  474.  
  475.         /* Identity Numeric */
  476.         insert into spt_datatype_info values    /* sql server type is 'decmaln' */
  477.         (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)
  478.  
  479.         /* Identity Numeric */
  480.         insert into spt_datatype_info values    /* sql server type is 'decmaln' */
  481.         (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)
  482.  
  483.     end
  484.  
  485.     if (charindex('7.00', @@version) = 0)
  486.     begin    /*    Add nullable type for non-Sphinx server */
  487.         /* Local Datetimn */
  488.         insert into spt_datatype_info values
  489.         (111,4,2,'smalldatetime',11,16,0,10,16,'''','''',null,1,0,3,null,0,null,'smalldatetime',null,9,3)
  490.         insert into spt_datatype_info values
  491.         (111,4,3,'smalldatetime',93,16,0,10,16,'''','''',null,1,0,3,null,0,null,'smalldatetime',null,9,3)
  492.         insert into spt_datatype_info values /* sql server type is 'datetimn' */
  493.         (111,8,2,'datetime',11,23,3,10,16,'''','''',null,1,0,3,null,0,null,'datetime',null,9,3)
  494.         insert into spt_datatype_info values
  495.         (111,8,3,'datetime',93,23,3,10,16,'''','''',null,1,0,3,null,0,null,'datetime',null,9,3)
  496.  
  497.         /* Local Floatn */
  498.         insert into spt_datatype_info values /* sql server type is 'floatn' */
  499.         (109,4,2,'real',7, 7,null,10,4,null,null,null,1,0,2,0,0,0,'real',null,7,null)
  500.         insert into spt_datatype_info values
  501.         (109,4,3,'real',7,24,null, 2,4,null,null,null,1,0,2,0,0,0,'real',null,7,null)
  502.         insert into spt_datatype_info values /* sql server type is 'floatn' */
  503.         (109,8,2,'float',6,15,null,10,8,null,null,null,1,0,2,0,0,0,'float',null,6,null)
  504.         insert into spt_datatype_info values
  505.         (109,8,3,'float',6,53,null, 2,8,null,null,null,1,0,2,0,0,0,'float',null,6,null)
  506.  
  507.         /* Local Moneyn */
  508.         insert into spt_datatype_info values    /* sql server type is 'moneyn' */
  509.         (110,4,null,'smallmoney',3,10,4,10,12,'$',null,null,1,0,2,0,1,0,'smallmoney',null,3,null)
  510.         insert into spt_datatype_info values    /* sql server type is 'moneyn' */
  511.         (110,8,null,'money',3,19,4,10,21,'$',null,null,1,0,2,0,1,0,'money',null,3,null)
  512.  
  513.         /* Local Intn */
  514.         insert into spt_datatype_info values /* sql server type is 'intn' */
  515.         (38,4,null,'int',4,10,0,10,4,null,null,null,1,0,2,0,0,0,'int',null,4,null)
  516.         insert into spt_datatype_info values /* sql server type is 'intn' */
  517.         (38,2,null,'smallint',5,5,0,10,2,null,null,null,1,0,2,0,0,0,'smallint',null,5,null)
  518.         insert into spt_datatype_info values
  519.         (38,1,null,'tinyint',-6,3,0,10,1,null,null,null,1,0,2,1,0,0,'tinyint',null,-6,null)
  520.  
  521.         if (charindex('6.00', @@version) > 0 or
  522.             charindex('6.50', @@version) > 0 or
  523.             charindex('7.00', @@version) > 0)
  524.         begin    /*    Add 6.0 data types */
  525.             /* Local Decimaln */
  526.             insert into spt_datatype_info values    /* sql server type is 'decimaln' */
  527.             (106,null,null,'decimal',3,38,0,10,null,null,null,'precision,scale',1,0,2,0,0,0,'decimal',null,3,null)
  528.             insert into spt_datatype_info values    /* sql server type is 'decmaln' */
  529.             (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)
  530.  
  531.             /* Local Numericn */
  532.             insert into spt_datatype_info values    /* sql server type is 'numericn' */
  533.             (108,null,null,'numeric',2,38,0,10,null,null,null,'precision,scale',1,0,2,0,0,0,'numeric',null,2,null)
  534.             insert into spt_datatype_info values    /* sql server type is 'decmaln' */
  535.             (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)
  536.         end
  537.     end
  538.  
  539.     commit tran
  540. end
  541. go
  542.  
  543. if (charindex('7.00', @@version) > 0)
  544.     begin
  545.         declare @case smallint
  546.  
  547.         select @case = 0
  548.         select @case = 1 where 'a' <> 'A'
  549.         update spt_datatype_info set ss_dtype = (select xtype from systypes
  550.             where TYPE_NAME like name+'%')
  551.  
  552.         /* Local Timestamp */
  553.         insert into spt_datatype_info values
  554.         (152,null,null,'timestamp',-2,8,null,null,null,'0x',null,null,1,0,2,null,0,null,'timestamp',0,-2,null)
  555.  
  556.         /* Local GUID */
  557.         insert into spt_datatype_info values
  558.         (72,null,null,'uniqueidentifier',-11,16,null,null,null,'''','''',null,1,0,2,null,0,null,'uniqueidentifier',0,-11,null)
  559.  
  560.         /* Local NChar */
  561. /*    Not until server fixes bugs
  562.         insert into spt_datatype_info values
  563.         (143,null,null,'nchar',-8,null,null,null,null,'N''','''','length',1,@case,3,null,0,null,'nchar',0,-8,null)
  564. */
  565.  
  566.         /* Local NVarchar */
  567.         insert into spt_datatype_info values
  568.         (146,null,null,'nvarchar',-9,null,null,null,null,'N''','''','max length',1,@case,3,null,0,null,'nvarchar',0,-9,null)
  569.  
  570.         /* Local NText */
  571. /*    Not until server fixes bugs
  572.         insert into spt_datatype_info values
  573.         (156,null,null,'ntext',-10,2147483647,null,null,2147483647,'N''','''',null,1,@case,1,null,0,null,'ntext',0,-10,null)
  574. */
  575.     end
  576. go
  577.  
  578. create unique clustered index datatypeinfoclust on spt_datatype_info(ss_dtype,fixlen,ODBCVer,AUTO_INCREMENT)
  579. go
  580.  
  581. dump tran master with no_log
  582. go
  583.  
  584. print 'creating table spt_server_info'
  585. go
  586. create table spt_server_info (
  587.               attribute_id        int NOT NULL,
  588.               attribute_name    varchar(60) NOT NULL,
  589.               attribute_value    varchar(255) NOT NULL)
  590. go
  591.  
  592. create unique clustered index serverinfoclust on spt_server_info(attribute_id)
  593. go
  594.  
  595. /*(leih 10/8/96) SID: Part I break into two batches to avoid OOM error */
  596. insert into spt_server_info
  597.     values (1,'DBMS_NAME','Microsoft SQL Server')
  598. insert into spt_server_info
  599.     values (2,'DBMS_VER',@@version)
  600. insert into spt_server_info
  601.     values (10,'OWNER_TERM','owner')
  602. insert into spt_server_info
  603.     values (11,'TABLE_TERM','table')
  604. insert into spt_server_info
  605.     values (12,'MAX_OWNER_NAME_LENGTH','30')
  606. insert into spt_server_info
  607.     values (13,'TABLE_LENGTH','30')
  608. insert into spt_server_info
  609.     values (14,'MAX_QUAL_LENGTH','30')
  610. insert into spt_server_info
  611.     values (15,'COLUMN_LENGTH','30')
  612. if 'A' = 'a' /* If not case sensitive server */
  613. begin
  614.     insert into spt_server_info
  615.         values (16,'IDENTIFIER_CASE','MIXED')
  616. end
  617. else
  618. begin
  619.     insert into spt_server_info
  620.         values (16,'IDENTIFIER_CASE','SENSITIVE')
  621. end
  622. insert into spt_server_info
  623.     values (17,'TX_ISOLATION','2')
  624. insert into spt_server_info
  625.     select 18,'COLLATION_SEQ',
  626.         'charset='+t2.name+' sort_order='+t1.name
  627.         +' charset_num='+rtrim(convert(char(4),t1.csid))+
  628.         ' sort_order_num='+rtrim(convert(char(4),t1.id))
  629.     from master.dbo.syscharsets t1, master.dbo.syscharsets t2, master.dbo.sysconfigures t3
  630.     where t1.csid=t2.id and t1.id=t3.value and t3.config in (123,1123)
  631. insert into spt_server_info
  632.     values (19,'SAVEPOINT_SUPPORT','Y')
  633. insert into spt_server_info
  634.     values (20,'MULTI_RESULT_SETS','Y')
  635. insert into spt_server_info
  636.     values (22,'ACCESSIBLE_TABLES','Y')
  637. go
  638.  
  639. /*(leih 10/8/96) SID: Part II. break into two batches to avoid OOM error */
  640. insert into spt_server_info
  641.     values (100,'USERID_LENGTH','30')
  642. insert into spt_server_info
  643.     values (101,'QUALIFIER_TERM','database')
  644. insert into spt_server_info
  645.     values (102,'NAMED_TRANSACTIONS','Y')
  646. insert into spt_server_info
  647.     values (103,'SPROC_AS_LANGUAGE','Y')
  648. insert into spt_server_info
  649.     values (104,'ACCESSIBLE_SPROC','Y')
  650. insert into spt_server_info
  651.     values (105,'MAX_INDEX_COLS','16')
  652. insert into spt_server_info
  653.     values (106,'RENAME_TABLE','Y')
  654. insert into spt_server_info
  655.     values (107,'RENAME_COLUMN','Y')
  656. insert into spt_server_info
  657.     values (108,'DROP_COLUMN','N')
  658. insert into spt_server_info
  659.     values (109,'INCREASE_COLUMN_LENGTH','N')
  660. if (charindex('6.50', @@version) = 0 and
  661.     charindex('7.00', @@version) = 0)
  662. begin
  663.     insert into spt_server_info
  664.         values (110,'DDL_IN_TRANSACTION','N')
  665. end
  666. else
  667. begin
  668.     insert into spt_server_info
  669.         values (110,'DDL_IN_TRANSACTION','Y')
  670. end
  671. insert into spt_server_info
  672.     values (111,'DESCENDING_INDEXES','N')
  673. insert into spt_server_info
  674.     values (112,'SP_RENAME','Y')
  675. insert into spt_server_info
  676.     values (113,'REMOTE_SPROC','Y')
  677. insert into spt_server_info
  678.     values (500,'SYS_SPROC_VERSION','7.00.324')
  679. go
  680.  
  681. if (charindex('7.00', @@version) > 0)
  682. begin    /*    Update values for 7.00 server */
  683.     update spt_server_info set attribute_value = '128'
  684.         where attribute_id in (12,13,14,15,100)
  685. end
  686. go
  687.  
  688. grant select on spt_server_info to public
  689. go
  690.  
  691. print 'creating sp_column_privileges'
  692. go
  693.  
  694. /*    Procedure for pre 6.50 server */
  695. CREATE PROCEDURE sp_column_privileges (
  696.             @table_name         varchar(32),
  697.             @table_owner        varchar(32) = null,
  698.             @table_qualifier    varchar(32) = null,
  699.             @column_name        varchar(96) = null)    /* 3*32 */
  700. as
  701.  
  702.     declare @table_id     int
  703.     DECLARE @full_table_name    varchar(65)    /* 2*32 + 1 */
  704.     declare @low smallint                     /* range of userids to check */
  705.     declare @high smallint
  706.     declare @owner_uid smallint
  707.  
  708.     select @low = 0, @high = 32767
  709.  
  710.     if @column_name is null /*    If column name not supplied, match all */
  711.         select @column_name = '%'
  712.  
  713.     if @table_qualifier is not null
  714.     begin
  715.         if db_name() <> @table_qualifier
  716.         begin    /* If qualifier doesn't match current database */
  717.             raiserror 20001 'Table qualifier must be name of current database'
  718.             return
  719.         end
  720.     end
  721.     if @table_owner is null
  722.     begin    /* If unqualified table name */
  723.         SELECT @full_table_name = @table_name
  724.     end
  725.     else
  726.     begin    /* Qualified table name */
  727.         SELECT @full_table_name = @table_owner + '.' + @table_name
  728.     end
  729.     /*    Get Object ID */
  730.     select @table_id = object_id(@full_table_name)
  731.  
  732.     if (@@trancount <> 0)
  733.     begin    /* If inside a transaction */
  734.         raiserror 20003 'The procedure ''sp_column_privileges'' cannot be executed from within a transaction.'
  735.         return
  736.     end
  737.  
  738.     /*
  739.     ** We need to create a table which will contain a row for every row to
  740.     ** be returned to the client.
  741.     */
  742.  
  743.     create table #column_priv1(
  744.         COLUMN_NAME             varchar(32) NOT NULL,
  745.         grantor                 smallint NOT NULL,
  746.         grantee                 smallint NOT NULL,
  747.         select_privilege        bit,
  748.         select_grantable        bit,
  749.         insert_privilege        bit,
  750.         insert_grantable        bit,
  751.         update_privilege        bit,
  752.         update_grantable        bit,
  753.         references_privilege    bit,
  754.         references_grantable    bit,
  755.         uid                     smallint NOT NULL,
  756.         gid                     smallint NOT NULL)
  757.  
  758. /*
  759. ** insert a row for the table owner (who has all permissions)
  760. */
  761.     select @owner_uid = (
  762.         select uid
  763.         from sysobjects
  764.         where id = @table_id)
  765.  
  766.     if (charindex('6.00', @@version) > 0)
  767.     begin
  768.         insert into #column_priv1
  769.             select
  770.                 c.name,
  771.                 u.uid,
  772.                 @owner_uid,
  773.                 0,
  774.                 1,
  775.                 0,
  776.                 1,
  777.                 0,
  778.                 1,
  779.                 0,
  780.                 1,
  781.                 @owner_uid,
  782.                 0
  783.             from syscolumns c, sysusers u
  784.             where id = @table_id
  785.                 and c.number = 0
  786.                 and u.suid = 1        /* grantor is dbo of database */
  787.     end
  788.     else
  789.     begin
  790.         insert into #column_priv1
  791.             select
  792.                 c.name,
  793.                 u.uid,
  794.                 @owner_uid,
  795.                 0,
  796.                 1,
  797.                 0,
  798.                 1,
  799.                 0,
  800.                 1,
  801.                 0,
  802.                 0,
  803.                 @owner_uid,
  804.                 0
  805.             from syscolumns c, sysusers u
  806.             where id = @table_id
  807.                 and c.number = 0
  808.                 and u.suid = 1        /* grantor is dbo of database */
  809.     end
  810. /*
  811. ** now stick in a row for every column for every user in the database
  812. ** we will need to weed out those who have no permissions later
  813. ** (and yes this is a cartesion product: the uid field in sysprotects
  814. ** can also have a group id, in which case we need to extend those
  815. ** privileges to all group members).
  816. */
  817.  
  818.     insert into #column_priv1
  819.         select distinct
  820.             c.name,
  821.             o.uid,
  822.             u.uid,
  823.             0,
  824.             0,
  825.             0,
  826.             0,
  827.             0,
  828.             0,
  829.             0,
  830.             0,
  831.             u.uid,
  832.             u.gid
  833.         from sysusers u, syscolumns c, sysobjects o
  834.         where o.id = @table_id
  835.             and c.id = o.id
  836.             and c.number = 0
  837.             and u.gid <> u.uid
  838.             and u.uid <> @owner_uid
  839.  
  840.     /*
  841.     ** we need to create another temporary table to contain all the various
  842.     ** protection information for the table in question
  843.     */
  844.     create table #protects (
  845.                 uid         smallint NOT NULL,
  846.                 grantor        smallint NOT NULL,
  847.                 action        tinyint NOT NULL,
  848.                 protecttype tinyint NOT NULL,
  849.                 name        varchar(32) NOT NULL)
  850.  
  851.     insert into #protects
  852.         select
  853.             p.uid,
  854.             p.uid,
  855.             p.action,
  856.             p.protecttype,
  857.             isnull(col_name(id, c.number), '~All')
  858.             from
  859.                 sysprotects p,
  860.                 master.dbo.spt_values c,
  861.                 master.dbo.spt_values a,
  862.                 master.dbo.spt_values b
  863.             where
  864.                 convert(tinyint, substring(isnull(p.columns, 0x1), c.low, 1))
  865.                     & c.high <> 0
  866.                     and c.number <= (
  867.                         select count(*)
  868.                         from syscolumns
  869.                         where id = @table_id)
  870.                 and c.type = 'P'
  871.                 and a.type = 'T'
  872.                 and a.number = p.action
  873.                 and p.action in (193,195,197,26)
  874.                 and b.type = 'T'
  875.                 and b.number = p.protecttype
  876.                 and p.id = @table_id
  877.                 and p.uid between @low and @high
  878.  
  879.  
  880.     update #column_priv1
  881.     set select_privilege = 1
  882.         from #protects p
  883.     where
  884.         p.protecttype = 205
  885.         and p.action = 193
  886.         and (p.name = #column_priv1.COLUMN_NAME
  887.             or name = '~All')
  888.         and (p.uid = 0
  889.             or p.uid = #column_priv1.gid
  890.             or p.uid = #column_priv1.uid)
  891.         and not exists (
  892.             select * from #protects
  893.             where
  894.                 protecttype = 206
  895.                 and action = 193
  896.                 and (name = #column_priv1.COLUMN_NAME
  897.                     or name = '~All')
  898.                 and ( uid = 0
  899.                     or uid = #column_priv1.gid
  900.                     or uid = #column_priv1.uid))
  901.  
  902.     update #column_priv1
  903.     set insert_privilege = 1
  904.         from #protects p
  905.     where
  906.         p.protecttype = 205
  907.         and p.action = 195
  908.         and (p.name = #column_priv1.COLUMN_NAME
  909.             or name = '~All')
  910.         and (p.uid = 0
  911.             or p.uid = #column_priv1.gid
  912.             or p.uid = #column_priv1.uid)
  913.         and not exists (
  914.             select * from #protects
  915.             where
  916.                 protecttype = 206
  917.                 and action = 195
  918.                 and (name = #column_priv1.COLUMN_NAME
  919.                        or name = '~All')
  920.                 and (uid = 0
  921.                     or uid = #column_priv1.gid
  922.                     or uid = #column_priv1.uid))
  923.  
  924.     update #column_priv1
  925.     set update_privilege = 1
  926.         from #protects p
  927.     where
  928.         p.protecttype = 205
  929.         and p.action = 197
  930.         and (p.name = #column_priv1.COLUMN_NAME
  931.             or name = '~All')
  932.         and (p.uid = 0
  933.             or p.uid = #column_priv1.gid
  934.             or p.uid = #column_priv1.uid)
  935.         and not exists (
  936.             select * from #protects
  937.                 where protecttype = 206
  938.                 and action = 197
  939.                 and (name = #column_priv1.COLUMN_NAME
  940.                     or name = '~All')
  941.                 and (uid = 0
  942.                     or uid = #column_priv1.gid
  943.                     or uid = #column_priv1.uid))
  944.  
  945.     update #column_priv1
  946.     set references_privilege = 1
  947.         from #protects p
  948.     where
  949.         p.protecttype = 205
  950.         and p.action = 26
  951.         and (p.name = #column_priv1.COLUMN_NAME
  952.             or name = '~All')
  953.         and (p.uid = 0
  954.             or p.uid = #column_priv1.gid
  955.             or p.uid = #column_priv1.uid)
  956.         and not exists (
  957.             select * from #protects
  958.                 where protecttype = 206
  959.                 and action = 26
  960.                 and (name = #column_priv1.COLUMN_NAME
  961.                     or name = '~All')
  962.                 and (uid = 0
  963.                     or uid = #column_priv1.gid
  964.                     or uid = #column_priv1.uid))
  965.  
  966.     update #column_priv1
  967.     set select_grantable = 1
  968.         from #protects p
  969.     where
  970.         p.protecttype = 204
  971.         and p.action = 193
  972.         and (p.name = #column_priv1.COLUMN_NAME
  973.             or name = '~All')
  974.         and (p.uid = 0
  975.             or p.uid = #column_priv1.gid
  976.             or p.uid = #column_priv1.uid)
  977.         and not exists (
  978.             select * from #protects
  979.             where
  980.                 protecttype = 206
  981.                 and action = 193
  982.                 and (name = #column_priv1.COLUMN_NAME
  983.                     or name = '~All')
  984.                 and ( uid = 0
  985.                     or uid = #column_priv1.gid
  986.                     or uid = #column_priv1.uid))
  987.  
  988.     update #column_priv1
  989.     set insert_grantable = 1
  990.         from #protects p
  991.     where
  992.         p.protecttype = 204
  993.         and p.action = 195
  994.         and (p.name = #column_priv1.COLUMN_NAME
  995.             or name = '~All')
  996.         and (p.uid = 0
  997.             or p.uid = #column_priv1.gid
  998.             or p.uid = #column_priv1.uid)
  999.         and not exists (
  1000.             select * from #protects
  1001.             where
  1002.                 protecttype = 206
  1003.                 and action = 195
  1004.                 and (name = #column_priv1.COLUMN_NAME
  1005.                     or name = '~All')
  1006.                 and ( uid = 0
  1007.                     or uid = #column_priv1.gid
  1008.                     or uid = #column_priv1.uid))
  1009.  
  1010.     update #column_priv1
  1011.     set update_grantable = 1
  1012.         from #protects p
  1013.     where
  1014.         p.protecttype = 204
  1015.         and p.action = 197
  1016.         and (p.name = #column_priv1.COLUMN_NAME
  1017.             or name = '~All')
  1018.         and (p.uid = 0
  1019.             or p.uid = #column_priv1.gid
  1020.             or p.uid = #column_priv1.uid)
  1021.         and not exists (
  1022.             select * from #protects
  1023.             where
  1024.                 protecttype = 206
  1025.                 and action = 197
  1026.                 and (name = #column_priv1.COLUMN_NAME
  1027.                     or name = '~All')
  1028.                 and ( uid = 0
  1029.                     or uid = #column_priv1.gid
  1030.                     or uid = #column_priv1.uid))
  1031.  
  1032.     update #column_priv1
  1033.     set references_grantable = 1
  1034.         from #protects p
  1035.     where
  1036.         p.protecttype = 204
  1037.         and p.action = 26
  1038.         and (p.name = #column_priv1.COLUMN_NAME
  1039.             or name = '~All')
  1040.         and (p.uid = 0
  1041.             or p.uid = #column_priv1.gid
  1042.             or p.uid = #column_priv1.uid)
  1043.         and not exists (
  1044.             select * from #protects
  1045.             where
  1046.                 protecttype = 206
  1047.                 and action = 26
  1048.                 and (name = #column_priv1.COLUMN_NAME
  1049.                     or name = '~All')
  1050.                 and ( uid = 0
  1051.                     or uid = #column_priv1.gid
  1052.                     or uid = #column_priv1.uid))
  1053.  
  1054.     create table #column_priv2(
  1055.         COLUMN_NAME     varchar(32) NOT NULL,
  1056.         grantor         smallint NULL,
  1057.         grantee         smallint NOT NULL,
  1058.         PRIVILEGE        varchar(32) NOT NULL,
  1059.         IS_GRANTABLE    varchar(3) NULL)
  1060.  
  1061.     insert into #column_priv2
  1062.         select
  1063.             COLUMN_NAME,
  1064.             grantor,
  1065.             grantee,
  1066.             'SELECT',
  1067.             'NO'
  1068.         from #column_priv1
  1069.         where select_privilege = 1 and select_grantable    = 0
  1070.  
  1071.     insert into #column_priv2
  1072.         select
  1073.             COLUMN_NAME,
  1074.             grantor,
  1075.             grantee,
  1076.             'INSERT',
  1077.             'NO'
  1078.         from #column_priv1
  1079.         where insert_privilege = 1 and insert_grantable = 0
  1080.  
  1081.     insert into #column_priv2
  1082.         select
  1083.             COLUMN_NAME,
  1084.             grantor,
  1085.             grantee,
  1086.             'UPDATE',
  1087.             'NO'
  1088.         from #column_priv1
  1089.         where update_privilege = 1 and update_grantable = 0
  1090.  
  1091.     insert into #column_priv2
  1092.         select
  1093.             COLUMN_NAME,
  1094.             grantor,
  1095.             grantee,
  1096.             'REFERENCES',
  1097.             'NO'
  1098.         from #column_priv1
  1099.         where references_privilege = 1 and references_grantable = 0
  1100.  
  1101.     insert into #column_priv2
  1102.         select
  1103.             COLUMN_NAME,
  1104.             grantor,
  1105.             grantee,
  1106.             'SELECT',
  1107.             'YES'
  1108.         from #column_priv1
  1109.         where select_grantable = 1
  1110.  
  1111.     insert into #column_priv2
  1112.         select
  1113.             COLUMN_NAME,
  1114.             grantor,
  1115.             grantee,
  1116.             'INSERT',
  1117.             'YES'
  1118.         from #column_priv1
  1119.         where insert_grantable = 1
  1120.  
  1121.     insert into #column_priv2
  1122.         select
  1123.             COLUMN_NAME,
  1124.             grantor,
  1125.             grantee,
  1126.             'UPDATE',
  1127.             'YES'
  1128.         from #column_priv1
  1129.         where update_grantable = 1
  1130.  
  1131.     insert into #column_priv2
  1132.         select
  1133.             COLUMN_NAME,
  1134.             grantor,
  1135.             grantee,
  1136.             'REFERENCES',
  1137.             'YES'
  1138.         from #column_priv1
  1139.         where references_grantable = 1
  1140.  
  1141.     select
  1142.         convert(varchar(32),db_name()) TABLE_QUALIFIER,
  1143.         convert(varchar(32),user_name(@owner_uid)) TABLE_OWNER,
  1144.         @table_name TABLE_NAME,
  1145.         COLUMN_NAME,
  1146.         convert(varchar(32),user_name(grantor)) GRANTOR,
  1147.         convert(varchar(32),user_name(grantee)) GRANTEE,
  1148.         PRIVILEGE,
  1149.         IS_GRANTABLE
  1150.     from #column_priv2
  1151.     where COLUMN_NAME like @column_name
  1152.     order by 4, 7
  1153. go
  1154.  
  1155. if (charindex('6.50', @@version) = 0 and
  1156.     charindex('7.00', @@version) = 0)
  1157. begin
  1158.     print ''
  1159.     print ''
  1160.     print 'Warning:'
  1161.     print 'you are installing the stored procedures '
  1162.     print 'on a pre 6.50 SQL Server.'
  1163.     print 'Ignore the following errors.'
  1164. end
  1165. else
  1166.     drop proc sp_column_privileges
  1167. go
  1168.  
  1169. /*    Procedure for 6.50 server */
  1170. CREATE PROCEDURE sp_column_privileges (
  1171.             @table_name         varchar(32),
  1172.             @table_owner        varchar(32) = null,
  1173.             @table_qualifier    varchar(32) = null,
  1174.             @column_name        varchar(96) = null)    /* 3*32 */
  1175. as
  1176.  
  1177.     declare @table_id     int
  1178.  
  1179.     if @column_name is null /*    If column name not supplied, match all */
  1180.         select @column_name = '%'
  1181.  
  1182.     if @table_qualifier is not null
  1183.     begin
  1184.         if db_name() <> @table_qualifier
  1185.         begin    /* If qualifier doesn't match current database */
  1186.             raiserror 20001 'Table qualifier must be name of current database'
  1187.             return
  1188.         end
  1189.     end
  1190.     if @table_owner is null
  1191.     begin    /* If unqualified table name */
  1192.         select @table_id = object_id(@table_name)
  1193.     end
  1194.     else
  1195.     begin    /* Qualified table name */
  1196.         select @table_id = object_id(@table_owner + '.' + @table_name)
  1197.     end
  1198.  
  1199.     select
  1200.         convert(varchar(32),db_name()) TABLE_QUALIFIER,
  1201.         convert(varchar(32),user_name(o.uid)) TABLE_OWNER,
  1202.         @table_name TABLE_NAME,
  1203.         convert(varchar(32),c.name) COLUMN_NAME,
  1204.         convert(varchar(32),user_name(p.grantor)) GRANTOR,
  1205.         convert(varchar(32),user_name(u.uid)) GRANTEE,
  1206.         convert (varchar(32),case p.action
  1207.              when 193 then 'SELECT'
  1208.              when 195 then 'INSERT'
  1209.              when 197 then 'UPDATE'
  1210.              else 'REFERENCES'
  1211.         end) PRIVILEGE,
  1212.         convert (varchar(3),case when p.protecttype = 205 then 'NO'
  1213.             else 'YES'
  1214.         end) IS_GRANTABLE
  1215.     from sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c
  1216.     where
  1217.         c.id = @table_id
  1218.         and c.name like @column_name
  1219.         and c.id = p.id
  1220.         and c.id = o.id
  1221.         and case substring(p.columns, 1, 1) & 1
  1222.                 when NULL then 255    /* all columns have permission */
  1223.                 when 0 then convert(tinyint, substring(p.columns, v.low, 1))
  1224.                 else (~convert(tinyint, isnull(substring(p.columns, v.low, 1),0)))
  1225.             end
  1226.             & v.high <> 0            /* permission applies to this column */
  1227.         and v.number <= (select count(*) from syscolumns
  1228.             where id = @table_id)    /* ranges from 1 to # of columns in table */
  1229.         and v.type = 'P'
  1230.         and v.number = c.colid
  1231.             /* expand groups */
  1232.         and ((p.uid = u.uid and u.uid <> u.gid) or
  1233.              (p.uid = u.gid and u.uid <> u.gid))
  1234.         and p.protecttype <> 206    /* only grant rows */
  1235.         and p.action in (26,193,195,197)
  1236.         and o.uid <> u.uid            /* no rows for owner */
  1237.         and not exists (            /* exclude revoke'd privileges */
  1238.             select *
  1239.             from sysprotects p1
  1240.             where
  1241.                 p1.protecttype = 206
  1242.                 and p1.action = p.action
  1243.                 and p1.id = p.id
  1244.                 and p1.uid = u.uid
  1245.                 and case substring(p1.columns, 1, 1) & 1
  1246.                         when NULL then 255    /* all columns have permission */
  1247.                         when 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  1248.                                             else (~convert(tinyint,isnull(substring(p.columns, v.low, 1),0)))
  1249.                     end
  1250.                     & v.high <> 0)            /* permission applies to this column */
  1251.     union all
  1252.     select    /*    Add rows for table owner */
  1253.         convert(varchar(32),db_name()) TABLE_QUALIFIER,
  1254.         convert(varchar(32),user_name(o.uid)) TABLE_OWNER,
  1255.         @table_name TABLE_NAME,
  1256.         convert(varchar(32),col_name(@table_id, c.colid)) COLUMN_NAME,
  1257.         convert(varchar(32),user_name(u.uid)) grantor,
  1258.         convert(varchar(32),user_name(o.uid)) grantee,
  1259.         convert (varchar(32),case v.number
  1260.             when 193 then 'SELECT'
  1261.             when 195 then 'INSERT'
  1262.             when 197 then 'UPDATE'
  1263.             else 'REFERENCES'
  1264.         end) PRIVILEGE,
  1265.         convert(varchar(3),'YES') IS_GRANTABLE
  1266.     from sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  1267.     where
  1268.         c.id = @table_id
  1269.         and c.name like @column_name
  1270.         and c.id = o.id
  1271.         and u.suid = 1        /* grantor is dbo of database */
  1272.         and v.type = 'P'    /* cross product to get all exposed privileges */
  1273.         and v.number in (26,193,195,197)
  1274.         and not exists (    /* exclude revoke'd privileges */
  1275.             select *
  1276.             from sysprotects p1
  1277.             where
  1278.                 p1.protecttype = 206
  1279.                 and p1.action = v.number
  1280.                 and p1.id = o.id
  1281.                 and p1.uid = o.uid)
  1282.     order by 4, 7
  1283. go
  1284.  
  1285.  
  1286.  
  1287. if (charindex('7.00', @@version) = 0)
  1288. begin
  1289.     print ''
  1290.     print ''
  1291.     print 'Warning:'
  1292.     print 'you are installing the stored procedures '
  1293.     print 'on a pre 7.0 SQL Server.'
  1294.     print 'Ignore the following errors.'
  1295. end
  1296. else
  1297.     drop proc sp_column_privileges
  1298. go
  1299.  
  1300. /*    Procedure for 7.00 server */
  1301. CREATE PROCEDURE sp_column_privileges (
  1302.             @table_name         sysname,
  1303.             @table_owner        sysname = null,
  1304.             @table_qualifier    sysname = null,
  1305.             @column_name        varchar(384) = null)    /* 3*128 */
  1306. as
  1307.  
  1308.     declare @table_id     int
  1309.  
  1310.     if @column_name is null /*    If column name not supplied, match all */
  1311.         select @column_name = '%'
  1312.  
  1313.     if @table_qualifier is not null
  1314.     begin
  1315.         if db_name() <> @table_qualifier
  1316.         begin    /* If qualifier doesn't match current database */
  1317.             raiserror 20001 'Table qualifier must be name of current database'
  1318.             return
  1319.         end
  1320.     end
  1321.     if @table_owner is null
  1322.     begin    /* If unqualified table name */
  1323.         select @table_id = object_id(@table_name)
  1324.     end
  1325.     else
  1326.     begin    /* Qualified table name */
  1327.         if @table_owner = ''
  1328.         begin    /* If empty owner name */
  1329.             select @table_id = 0
  1330.         end
  1331.         else
  1332.         begin
  1333.             select @table_id = object_id(@table_owner + '.' + @table_name)
  1334.         end
  1335.     end
  1336.  
  1337.     select
  1338.         convert(sysname,db_name()) TABLE_QUALIFIER,
  1339.         convert(sysname,user_name(o.uid)) TABLE_OWNER,
  1340.         @table_name TABLE_NAME,
  1341.         convert(sysname,c.name) COLUMN_NAME,
  1342.         convert(sysname,user_name(p.grantor)) GRANTOR,
  1343.         convert(sysname,user_name(u.uid)) GRANTEE,
  1344.         convert (varchar(32),case p.action
  1345.              when 193 then 'SELECT'
  1346.              when 195 then 'INSERT'
  1347.              when 197 then 'UPDATE'
  1348.              else 'REFERENCES'
  1349.         end) PRIVILEGE,
  1350.         convert (varchar(3),case when p.protecttype = 205 then 'NO'
  1351.             else 'YES'
  1352.         end) IS_GRANTABLE
  1353.     from sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c, sysmembers m
  1354.     where
  1355.         c.id = @table_id
  1356.         and c.name like @column_name
  1357.         and c.id = p.id
  1358.         and c.id = o.id
  1359.         and case substring(p.columns, 1, 1) & 1
  1360.                 when NULL then 255    /* all columns have permission */
  1361.                 when 0 then convert(tinyint, substring(p.columns, v.low, 1))
  1362.                 else (~convert(tinyint, isnull(substring(p.columns, v.low, 1),0)))
  1363.             end
  1364.             & v.high <> 0            /* permission applies to this column */
  1365.         and v.number <= (select count(*) from syscolumns
  1366.             where id = @table_id)    /* ranges from 1 to # of columns in table */
  1367.         and v.type = 'P'
  1368.         and v.number = c.colid
  1369.             /* expand groups - AKUNDONE: only 1 level of group unrolling here. Need more?? */
  1370.         and (u.uid > 0 and u.uid < 16384)
  1371.         and ((p.uid = u.uid) or 
  1372.              (p.uid = m.groupuid and u.uid = m.memberuid))
  1373.         and p.protecttype <> 206    /* only grant rows */
  1374.         and p.action in (26,193,195,197)
  1375.         and o.uid <> u.uid            /* no rows for owner */
  1376.         and not exists (            /* exclude revoke'd privileges */
  1377.             select *
  1378.             from sysprotects p1
  1379.             where
  1380.                 p1.protecttype = 206
  1381.                 and p1.action = p.action
  1382.                 and p1.id = p.id
  1383.                 and p1.uid = u.uid
  1384.                 and case substring(p1.columns, 1, 1) & 1
  1385.                         when NULL then 255    /* all columns have permission */
  1386.                         when 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  1387.                                             else (~convert(tinyint,isnull(substring(p.columns, v.low, 1),0)))
  1388.                     end
  1389.                     & v.high <> 0)            /* permission applies to this column */
  1390.     union all
  1391.     select    /*    Add rows for table owner */
  1392.         convert(sysname,db_name()) TABLE_QUALIFIER,
  1393.         convert(sysname,user_name(o.uid)) TABLE_OWNER,
  1394.         @table_name TABLE_NAME,
  1395.         convert(sysname,col_name(@table_id, c.colid)) COLUMN_NAME,
  1396.         convert(sysname,user_name(u.uid)) grantor,
  1397.         convert(sysname,user_name(o.uid)) grantee,
  1398.         convert (varchar(32),case v.number
  1399.             when 193 then 'SELECT'
  1400.             when 195 then 'INSERT'
  1401.             when 197 then 'UPDATE'
  1402.             else 'REFERENCES'
  1403.         end) PRIVILEGE,
  1404.         convert(varchar(3),'YES') IS_GRANTABLE
  1405.     from sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  1406.     where
  1407.         c.id = @table_id
  1408.         and c.name like @column_name
  1409.         and c.id = o.id
  1410.         and u.uid = 1        /* grantor is 'dbo' of database */
  1411.         and v.type = 'P'    /* cross product to get all exposed privileges */
  1412.         and v.number in (26,193,195,197)
  1413.         and not exists (    /* exclude revoke'd privileges */
  1414.             select *
  1415.             from sysprotects p1
  1416.             where
  1417.                 p1.protecttype = 206
  1418.                 and p1.action = v.number
  1419.                 and p1.id = o.id
  1420.                 and p1.uid = o.uid)
  1421.     order by 4, 7
  1422. go
  1423.  
  1424.  
  1425. grant execute on sp_column_privileges to public
  1426. go
  1427.  
  1428. dump tran master with no_log
  1429. go
  1430.  
  1431. print 'creating sp_columns'
  1432. go
  1433.  
  1434. /*    Procedure for pre-6.0 server */
  1435. CREATE PROCEDURE sp_columns (
  1436.                  @table_name        varchar(96),
  1437.                  @table_owner        varchar(96) = null,
  1438.                  @table_qualifier    varchar(32) = null,
  1439.                  @column_name        varchar(96) = null,
  1440.                  @ODBCVer            int = 2)
  1441. AS
  1442.     DECLARE @full_table_name    varchar(193)
  1443.     DECLARE @table_id int
  1444.  
  1445.     if @ODBCVer <> 3
  1446.         select @ODBCVer = 2
  1447.     if @column_name is null /*    If column name not supplied, match all */
  1448.         select @column_name = '%'
  1449.     if @table_qualifier is not null
  1450.     begin
  1451.         if db_name() <> @table_qualifier
  1452.         begin    /* If qualifier doesn't match current database */
  1453.             raiserror 20001 'Table qualifier must be name of current database'
  1454.             return
  1455.         end
  1456.     end
  1457.     if @table_name is null
  1458.     begin    /*    If table name not supplied, match all */
  1459.         select @table_name = '%'
  1460.     end
  1461.     if @table_owner is null
  1462.     begin    /* If unqualified table name */
  1463.         SELECT @full_table_name = @table_name
  1464.     end
  1465.     else
  1466.     begin    /* Qualified table name */
  1467.         SELECT @full_table_name = @table_owner + '.' + @table_name
  1468.     end
  1469.  
  1470.     /*    Get Object ID */
  1471.     SELECT @table_id = object_id(@full_table_name)
  1472.     if ((charindex('%',@full_table_name) = 0) and
  1473.         (charindex('_',@full_table_name) = 0)  and
  1474.         @table_id <> 0)
  1475.     begin
  1476.         /* this block is for the case where there is no pattern
  1477.              matching required for the table name */
  1478.         SELECT
  1479.             TABLE_QUALIFIER = convert(varchar(32),DB_NAME()),
  1480.             TABLE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  1481.             TABLE_NAME = convert(varchar(32),o.name),
  1482.             COLUMN_NAME = convert(varchar(32),c.name),
  1483.             d.DATA_TYPE,
  1484.             TYPE_NAME = t.name,
  1485.             "PRECISION" = isnull(d.data_precision, convert(int,c.length)),
  1486.             LENGTH = isnull(d.length, convert(int,c.length)),
  1487.             SCALE = d.numeric_scale,
  1488.             d.RADIX,
  1489.             NULLABLE =    /* set nullability from status flag */
  1490.                 convert(smallint, convert(bit, c.status&8)),
  1491.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  1492.             COLUMN_DEF = convert(varchar(254),substring(text,2,datalength(text)-2)),
  1493.             d.SQL_DATA_TYPE,
  1494.             d.SQL_DATETIME_SUB,
  1495.             CHAR_OCTET_LENGTH = isnull(d.data_precision, convert(int,c.length))+d.charbin,
  1496.             ORDINAL_POSITION = convert(int,c.colid),
  1497.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO      YES',(c.status&8)+1,3))),
  1498.             SS_DATA_TYPE = c.type
  1499.         FROM
  1500.             syscolumns c,
  1501.             sysobjects o,
  1502.             syscomments m,
  1503.             master.dbo.spt_datatype_info d,
  1504.             systypes t
  1505.         WHERE
  1506.             o.id = @table_id
  1507.             AND c.id = o.id
  1508.             AND t.type = d.ss_dtype
  1509.             AND c.length = isnull(d.fixlen, c.length)
  1510.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  1511.             AND o.type <> 'P'
  1512.             AND c.usertype = t.usertype
  1513.             AND c.name like @column_name
  1514.             AND c.cdefault *= m.id
  1515.             AND m.colid = 1
  1516.         ORDER BY 17
  1517.     end
  1518.     else
  1519.     begin
  1520.         /* this block is for the case where there IS pattern
  1521.              matching done on the table name */
  1522.         if @table_owner is null /*    If owner not supplied, match all */
  1523.             select @table_owner = '%'
  1524.         SELECT
  1525.             TABLE_QUALIFIER = convert(varchar(32),DB_NAME()),
  1526.             TABLE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  1527.             TABLE_NAME = convert(varchar(32),o.name),
  1528.             COLUMN_NAME = convert(varchar(32),c.name),
  1529.             d.DATA_TYPE,
  1530.             TYPE_NAME = t.name,
  1531.             "PRECISION" = isnull(d.data_precision, convert(int,c.length)),
  1532.             LENGTH = isnull(d.length, convert(int,c.length)),
  1533.             SCALE = d.numeric_scale,
  1534.             d.RADIX,
  1535.             NULLABLE =    /* set nullability from status flag */
  1536.                 convert(smallint, convert(bit, c.status&8)),
  1537.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  1538.             COLUMN_DEF = convert(varchar(254),substring(text,2,datalength(text)-2)),
  1539.             d.SQL_DATA_TYPE,
  1540.             d.SQL_DATETIME_SUB,
  1541.             CHAR_OCTET_LENGTH = isnull(d.data_precision, convert(int,c.length))+d.charbin,
  1542.             ORDINAL_POSITION = convert(int,c.colid),
  1543.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO      YES',(c.status&8)+1,3))),
  1544.             SS_DATA_TYPE = c.type
  1545.         FROM
  1546.             syscolumns c,
  1547.             sysobjects o,
  1548.             syscomments m,
  1549.             master.dbo.spt_datatype_info d,
  1550.             systypes t
  1551.         WHERE
  1552.             o.name like @table_name
  1553.             AND user_name(o.uid) like @table_owner
  1554.             AND o.id = c.id
  1555.             AND t.type = d.ss_dtype
  1556.             AND c.length = isnull(d.fixlen, c.length)
  1557.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  1558.             AND o.type <> 'P'
  1559.             AND c.usertype = t.usertype
  1560.             AND c.name like @column_name
  1561.             AND c.cdefault *= m.id
  1562.             AND m.colid = 1
  1563.         ORDER BY 2, 3, 17
  1564.     end
  1565. go
  1566.  
  1567. if (charindex('6.00', @@version) = 0 and
  1568.     charindex('6.50', @@version) = 0 and
  1569.     charindex('7.00', @@version) = 0)
  1570. begin
  1571.     print ''
  1572.     print ''
  1573.     print 'Warning:'
  1574.     print 'you are installing the stored procedures '
  1575.     print 'on a pre 6.0 SQL Server.'
  1576.     print 'Ignore the following error.'
  1577. end
  1578. else
  1579.     drop proc sp_columns
  1580. go
  1581.  
  1582. /*    Procedure for 6.0 and 6.50 server */
  1583. CREATE PROCEDURE sp_columns (
  1584.                  @table_name        varchar(96),
  1585.                  @table_owner        varchar(96) = null,
  1586.                  @table_qualifier    varchar(32) = null,
  1587.                  @column_name        varchar(96) = null,
  1588.                  @ODBCVer            int = 2)
  1589. AS
  1590.     DECLARE @full_table_name    varchar(193)
  1591.     DECLARE @table_id int
  1592.  
  1593.     if @ODBCVer <> 3
  1594.         select @ODBCVer = 2
  1595.     if @column_name is null /*    If column name not supplied, match all */
  1596.         select @column_name = '%'
  1597.     if @table_qualifier is not null
  1598.     begin
  1599.         if db_name() <> @table_qualifier
  1600.         begin    /* If qualifier doesn't match current database */
  1601.             raiserror (15250, -1,-1,'Table')
  1602.             return
  1603.         end
  1604.     end
  1605.     if @table_name is null
  1606.     begin    /*    If table name not supplied, match all */
  1607.         select @table_name = '%'
  1608.     end
  1609.     if @table_owner is null
  1610.     begin    /* If unqualified table name */
  1611.         SELECT @full_table_name = @table_name
  1612.     end
  1613.     else
  1614.     begin    /* Qualified table name */
  1615.         SELECT @full_table_name = @table_owner + '.' + @table_name
  1616.     end
  1617.  
  1618.     /*    Get Object ID */
  1619.     SELECT @table_id = object_id(@full_table_name)
  1620.     if ((charindex('%',@full_table_name) = 0) and
  1621.         (charindex('_',@full_table_name) = 0)  and
  1622.         @table_id <> 0)
  1623.     begin
  1624.         /* this block is for the case where there is no pattern
  1625.             matching required for the table name */
  1626.         SELECT
  1627.             TABLE_QUALIFIER = convert(varchar(32),DB_NAME()),
  1628.             TABLE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  1629.             TABLE_NAME = convert(varchar(32),o.name),
  1630.             COLUMN_NAME = convert(varchar(32),c.name),
  1631.             d.DATA_TYPE,
  1632.             convert(varchar(32),case
  1633.                 when t.usertype > 100 or t.usertype in (18,80) then t.name
  1634.                 else d.TYPE_NAME
  1635.             end) TYPE_NAME,
  1636.             convert(int,case
  1637.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  1638.                 else isnull(convert(int,c.prec), 2147483647)
  1639.             end) "PRECISION",
  1640.             convert(int,case
  1641.                 when d.ss_dtype IN (106, 108, 55, 63) then    /* decimal/numeric types */
  1642.                     c.prec+2
  1643.                 else
  1644.                     isnull(d.length, c.length)
  1645.             end) LENGTH,
  1646.             SCALE = convert(smallint, c.scale),
  1647.             d.RADIX,
  1648.             NULLABLE =    /* set nullability from status flag */
  1649.                 convert(smallint, convert(bit, c.status&8)),
  1650.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  1651.             COLUMN_DEF = convert(varchar(254),substring(text,2,datalength(text)-2)),
  1652.             d.SQL_DATA_TYPE,
  1653.             d.SQL_DATETIME_SUB,
  1654.             CHAR_OCTET_LENGTH = isnull(convert(int,c.prec), 2147483647)+d.charbin,
  1655.             ORDINAL_POSITION = convert(int,c.colid),
  1656.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO      YES',(c.status&8)+1,3))),
  1657.             SS_DATA_TYPE = c.type
  1658.         FROM
  1659.             syscolumns c,
  1660.             sysobjects o,
  1661.             syscomments m,
  1662.             master.dbo.spt_datatype_info d,
  1663.             systypes t
  1664.         WHERE
  1665.             o.id = @table_id
  1666.             AND c.id = o.id
  1667.             AND t.type = d.ss_dtype
  1668.             AND c.length = isnull(d.fixlen, c.length)
  1669.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  1670.             AND o.type <> 'P'
  1671.             AND isnull(d.AUTO_INCREMENT,0) = (c.status&128)/128
  1672.             AND c.usertype = t.usertype
  1673.             AND c.name like @column_name
  1674.             AND c.cdefault *= m.id
  1675.             AND m.colid = 1
  1676.         ORDER BY 17
  1677.     end
  1678.     else
  1679.     begin
  1680.         /* this block is for the case where there IS pattern
  1681.             matching done on the table name */
  1682.         if @table_owner is null /*    If owner not supplied, match all */
  1683.             select @table_owner = '%'
  1684.         SELECT
  1685.             TABLE_QUALIFIER = convert(varchar(32),DB_NAME()),
  1686.             TABLE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  1687.             TABLE_NAME = convert(varchar(32),o.name),
  1688.             COLUMN_NAME = convert(varchar(32),c.name),
  1689.             d.DATA_TYPE,
  1690.             convert(varchar(32),case
  1691.                 when t.usertype > 100 or t.usertype in (18,80) then t.name
  1692.                 else d.TYPE_NAME
  1693.             end) TYPE_NAME,
  1694.             convert(int,case
  1695.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  1696.                 else isnull(convert(int,c.prec), 2147483647)
  1697.             end) "PRECISION",
  1698.             convert(int,case
  1699.                 when d.ss_dtype IN (106, 108, 55, 63) then    /* decimal/numeric types */
  1700.                     c.prec+2
  1701.                 else
  1702.                     isnull(d.length, c.length)
  1703.             end) LENGTH,
  1704.             SCALE = convert(smallint, c.scale),
  1705.             d.RADIX,
  1706.             NULLABLE =    /* set nullability from status flag */
  1707.                 convert(smallint, convert(bit, c.status&8)),
  1708.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  1709.             COLUMN_DEF = convert(varchar(254),substring(text,2,datalength(text)-2)),
  1710.             d.SQL_DATA_TYPE,
  1711.             d.SQL_DATETIME_SUB,
  1712.             CHAR_OCTET_LENGTH = isnull(convert(int,c.prec), 2147483647)+d.charbin,
  1713.             ORDINAL_POSITION = convert(int,c.colid),
  1714.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO      YES',(c.status&8)+1,3))),
  1715.             SS_DATA_TYPE = c.type
  1716.         FROM
  1717.             syscolumns c,
  1718.             sysobjects o,
  1719.             syscomments m,
  1720.             master.dbo.spt_datatype_info d,
  1721.             systypes t
  1722.         WHERE
  1723.             o.name like @table_name
  1724.             AND user_name(o.uid) like @table_owner
  1725.             AND o.id = c.id
  1726.             AND t.type = d.ss_dtype
  1727.             AND c.length = isnull(d.fixlen, c.length)
  1728.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  1729.             AND o.type <> 'P'
  1730.             AND isnull(d.AUTO_INCREMENT,0) = (c.status&128)/128
  1731.             AND c.usertype = t.usertype
  1732.             AND c.name like @column_name
  1733.             AND c.cdefault *= m.id
  1734.             AND m.colid = 1
  1735.         ORDER BY 2, 3, 17
  1736.     end
  1737. go
  1738.  
  1739. if (charindex('7.00', @@version) = 0)
  1740. begin
  1741.     print ''
  1742.     print ''
  1743.     print 'Warning:'
  1744.     print 'you are installing the stored procedures '
  1745.     print 'on a pre 7.0 SQL Server.'
  1746.     print 'Ignore the following errors.'
  1747. end
  1748. else
  1749.     drop proc sp_columns
  1750. go
  1751.  
  1752. /*    Procedure for 7.0 server */
  1753. CREATE PROCEDURE sp_columns (
  1754.                  @table_name        varchar(384),
  1755.                  @table_owner        varchar(384) = null,
  1756.                  @table_qualifier    sysname = null,
  1757.                  @column_name        varchar(384) = null,
  1758.                  @ODBCVer            int = 2)
  1759. AS
  1760.     DECLARE @full_table_name    varchar(769)
  1761.     DECLARE @table_id int
  1762.  
  1763.     if @ODBCVer <> 3
  1764.         select @ODBCVer = 2
  1765.     if @column_name is null /*    If column name not supplied, match all */
  1766.         select @column_name = '%'
  1767.     if @table_qualifier is not null
  1768.     begin
  1769.         if db_name() <> @table_qualifier
  1770.         begin    /* If qualifier doesn't match current database */
  1771.             raiserror (15250, -1,-1,'Table')
  1772.             return
  1773.         end
  1774.     end
  1775.     if @table_name is null
  1776.     begin    /*    If table name not supplied, match all */
  1777.         select @table_name = '%'
  1778.     end
  1779.     if @table_owner is null
  1780.     begin    /* If unqualified table name */
  1781.         SELECT @full_table_name = @table_name
  1782.     end
  1783.     else
  1784.     begin    /* Qualified table name */
  1785.         if @table_owner = ''
  1786.         begin    /* If empty owner name */
  1787.             SELECT @full_table_name = @table_owner
  1788.         end
  1789.         else
  1790.         begin
  1791.             SELECT @full_table_name = @table_owner + '.' + @table_name
  1792.         end
  1793.     end
  1794.  
  1795.     /*    Get Object ID */
  1796.     SELECT @table_id = object_id(@full_table_name)
  1797.     if ((charindex('%',@full_table_name) = 0) and
  1798.         (charindex('_',@full_table_name) = 0)  and
  1799.         @table_id <> 0)
  1800.     begin
  1801.         /* this block is for the case where there is no pattern
  1802.             matching required for the table name */
  1803.         SELECT
  1804.             TABLE_QUALIFIER = convert(sysname,DB_NAME()),
  1805.             TABLE_OWNER = convert(sysname,USER_NAME(o.uid)),
  1806.             TABLE_NAME = convert(sysname,o.name),
  1807.             COLUMN_NAME = convert(sysname,c.name),
  1808.             d.DATA_TYPE,
  1809.             convert (sysname,case
  1810.                 when t.xusertype > 255 then t.name
  1811.                 else d.TYPE_NAME
  1812.             end) TYPE_NAME,
  1813.             convert(int,case
  1814.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  1815.                 else ColumnProperty (c.id, c.name, 'Precision')
  1816.             end) "PRECISION",
  1817.             convert(int,case
  1818.                 when d.ss_dtype IN (139, 140) then    /* decimal/numeric types */
  1819.                     ColumnProperty (c.id, c.name, 'Precision')+2
  1820.                 else
  1821.                     isnull(d.length, c.length)
  1822.             end) LENGTH,
  1823.             SCALE = convert(smallint, ColumnProperty (c.id, c.name, 'Scale')),
  1824.             d.RADIX,
  1825.             NULLABLE = convert(smallint, ColumnProperty (c.id, c.name, 'AllowsNull')),
  1826.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  1827.             COLUMN_DEF = convert(varchar(254),substring(text,2,datalength(text)-2)),
  1828.             d.SQL_DATA_TYPE,
  1829.             d.SQL_DATETIME_SUB,
  1830.             CHAR_OCTET_LENGTH = isnull(convert(int,c.prec), 2147483647)+d.charbin,
  1831.             ORDINAL_POSITION = convert(int,c.colid),
  1832.             IS_NULLABLE = convert(varchar(254),
  1833.                 substring('NO YES',(ColumnProperty (c.id, c.name, 'AllowsNull')*3)+1,3)),
  1834.             SS_DATA_TYPE = c.type
  1835.         FROM
  1836.             sysobjects o,
  1837.             master.dbo.spt_datatype_info d,
  1838.             systypes t,
  1839.             syscolumns c
  1840.             LEFT OUTER JOIN syscomments m on c.cdefault = m.id
  1841.                 AND m.colid = 1
  1842.         WHERE
  1843.             o.id = @table_id
  1844.             AND c.id = o.id
  1845.             AND t.xtype = d.ss_dtype
  1846.             AND c.length = isnull(d.fixlen, c.length)
  1847.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  1848.             AND o.type <> 'P'
  1849.             AND isnull(d.AUTO_INCREMENT,0) = isnull(ColumnProperty (c.id, c.name, 'IsIdentity'),0)
  1850.             AND c.xusertype = t.xusertype
  1851.             AND c.name like @column_name
  1852.         ORDER BY 17
  1853.     end
  1854.     else
  1855.     begin
  1856.         /* this block is for the case where there IS pattern
  1857.             matching done on the table name */
  1858.         if @table_owner is null /*    If owner not supplied, match all */
  1859.             select @table_owner = '%'
  1860.         SELECT
  1861.             TABLE_QUALIFIER = convert(sysname,DB_NAME()),
  1862.             TABLE_OWNER = convert(sysname,USER_NAME(o.uid)),
  1863.             TABLE_NAME = convert(sysname,o.name),
  1864.             COLUMN_NAME = convert(sysname,c.name),
  1865.             d.DATA_TYPE,
  1866.             convert (sysname,case
  1867.                 when t.xusertype > 255 then t.name
  1868.                 else d.TYPE_NAME
  1869.             end) TYPE_NAME,
  1870.             convert(int,case
  1871.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  1872.                 else ColumnProperty (c.id, c.name, 'Precision')
  1873.             end) "PRECISION",
  1874.             convert(int,case
  1875.                 when d.ss_dtype IN (139, 140) then    /* decimal/numeric types */
  1876.                     ColumnProperty (c.id, c.name, 'Precision')+2
  1877.                 else
  1878.                     isnull(d.length, c.length)
  1879.             end) LENGTH,
  1880.             SCALE = convert(smallint, ColumnProperty (c.id, c.name, 'Scale')),
  1881.             d.RADIX,
  1882.             NULLABLE = convert(smallint, ColumnProperty (c.id, c.name, 'AllowsNull')),
  1883.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  1884.             COLUMN_DEF = convert(varchar(254),substring(text,2,datalength(text)-2)),
  1885.             d.SQL_DATA_TYPE,
  1886.             d.SQL_DATETIME_SUB,
  1887.             CHAR_OCTET_LENGTH = isnull(convert(int,c.prec), 2147483647)+d.charbin,
  1888.             ORDINAL_POSITION = convert(int,c.colid),
  1889.             IS_NULLABLE = convert(varchar(254),
  1890.                 rtrim(substring('NO YES',(ColumnProperty (c.id, c.name, 'AllowsNull')*3)+1,3))),
  1891.             SS_DATA_TYPE = c.type
  1892.         FROM
  1893.             sysobjects o,
  1894.             master.dbo.spt_datatype_info d,
  1895.             systypes t,
  1896.             syscolumns c
  1897.             LEFT OUTER JOIN syscomments m on c.cdefault = m.id
  1898.                 AND m.colid = 1
  1899.         WHERE
  1900.             o.name like @table_name
  1901.             AND user_name(o.uid) like @table_owner
  1902.             AND o.id = c.id
  1903.             AND t.xtype = d.ss_dtype
  1904.             AND c.length = isnull(d.fixlen, c.length)
  1905.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  1906.             AND o.type <> 'P'
  1907.             AND isnull(d.AUTO_INCREMENT,0) = isnull(ColumnProperty (c.id, c.name, 'IsIdentity'),0)
  1908.             AND c.xusertype = t.xusertype
  1909.             AND c.name like @column_name
  1910.         ORDER BY 2, 3, 17
  1911.     end
  1912. go
  1913.  
  1914. grant execute on sp_columns to public
  1915. go
  1916.  
  1917. dump tran master with no_log
  1918. go
  1919.  
  1920. print 'creating sp_databases'
  1921. go
  1922.  
  1923. /*    Procedure for pre-7.0 server */
  1924. create proc sp_databases
  1925. as
  1926.     /* Use temporary table to sum up database size w/o using group by */
  1927.     create table #databases (
  1928.                   DATABASE_NAME varchar(32) NOT NULL,
  1929.                   size int NOT NULL)
  1930.  
  1931.     /* Insert row for each database */
  1932.     insert into #databases
  1933.         select
  1934.             name,
  1935.             (select sum(size) from master.dbo.sysusages
  1936.                 where dbid = d.dbid)
  1937.         from master.dbo.sysdatabases d
  1938.  
  1939.     select
  1940.          convert(varchar(32),DATABASE_NAME),
  1941.          DATABASE_SIZE = size*2,    /* Convert from 2048 byte pages to K */
  1942.          REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  1943.     from #databases
  1944.     order by 1
  1945. go
  1946.  
  1947. if (charindex('7.00', @@version) = 0)
  1948. begin
  1949.     print ''
  1950.     print ''
  1951.     print 'Warning:'
  1952.     print 'you are installing the stored procedures '
  1953.     print 'on a pre 7.0 SQL Server.'
  1954.     print 'Ignore the following errors.'
  1955. end
  1956. else
  1957.     drop proc sp_databases
  1958. go
  1959.  
  1960. /*    Procedure for 7.0 server */
  1961. create proc sp_databases
  1962. as
  1963.     /* Use temporary table to sum up database size w/o using group by */
  1964.     create table #databases (
  1965.                   DATABASE_NAME sysname NOT NULL,
  1966.                   size int NOT NULL)
  1967.  
  1968.     /* Insert row for each database */
  1969.     insert into #databases
  1970.         select
  1971.             name,
  1972.             (select sum(size) from master.dbo.sysdevices
  1973.                 where dbid = d.dbid)
  1974.         from master.dbo.sysdatabases d
  1975.  
  1976.     select
  1977.          convert(sysname,DATABASE_NAME),
  1978.          DATABASE_SIZE = size*2,    /* Convert from 2048 byte pages to K */
  1979.          REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  1980.     from #databases
  1981.     order by 1
  1982. go
  1983.  
  1984. grant execute on sp_databases to public
  1985. go
  1986.  
  1987. dump tran master with no_log
  1988. go
  1989.  
  1990. print 'creating sp_datatype_info'
  1991. go
  1992.  
  1993. /*    Procedure for pre-6.0 server */
  1994. create proc sp_datatype_info
  1995.     (@data_type int = 0, @ODBCVer tinyint = 2)
  1996. as
  1997.     if @ODBCVer <> 3
  1998.         select @ODBCVer = 2
  1999.     if @data_type = 0
  2000.         select
  2001.             TYPE_NAME = t.name,
  2002.             d.DATA_TYPE,
  2003.             "PRECISION" = isnull(d.data_precision, convert(int,t.length)),
  2004.             d.LITERAL_PREFIX,
  2005.             d.LITERAL_SUFFIX,
  2006.             e.CREATE_PARAMS,
  2007.             d.NULLABLE,
  2008.             d.CASE_SENSITIVE,
  2009.             d.SEARCHABLE,
  2010.             d.UNSIGNED_ATTRIBUTE,
  2011.             d.MONEY,
  2012.             d.AUTO_INCREMENT,
  2013.             LOCAL_TYPE_NAME = t.name,
  2014.             MINIMUM_SCALE = d.numeric_scale,
  2015.             MAXIMUM_SCALE = d.numeric_scale,
  2016.             d.SQL_DATA_TYPE,
  2017.             d.SQL_DATETIME_SUB,
  2018.             NUM_PREC_RADIX = convert(int,d.RADIX),
  2019.             INTERVAL_PRECISION = convert(smallint,NULL),
  2020.             USERTYPE = t.usertype
  2021.         from master.dbo.spt_datatype_info d, master.dbo.spt_datatype_info_ext e, systypes t
  2022.         where
  2023.             d.ss_dtype = t.type
  2024.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2025.             and t.usertype *= e.user_type
  2026.             and t.type not in (111,109,38,110)    /* get rid of nullable types */
  2027.         order by 2, 12, 11, t.usertype
  2028.  
  2029.     else
  2030.         select
  2031.             TYPE_NAME = t.name,
  2032.             d.DATA_TYPE,
  2033.             "PRECISION" = isnull(d.data_precision, convert(int,t.length)),
  2034.             d.LITERAL_PREFIX,
  2035.             d.LITERAL_SUFFIX,
  2036.             e.CREATE_PARAMS,
  2037.             d.NULLABLE,
  2038.             d.CASE_SENSITIVE,
  2039.             d.SEARCHABLE,
  2040.             d.UNSIGNED_ATTRIBUTE,
  2041.             d.MONEY,
  2042.             d.AUTO_INCREMENT,
  2043.             LOCAL_TYPE_NAME = t.name,
  2044.             MINIMUM_SCALE = d.numeric_scale,
  2045.             MAXIMUM_SCALE = d.numeric_scale,
  2046.             d.SQL_DATA_TYPE,
  2047.             d.SQL_DATETIME_SUB,
  2048.             NUM_PREC_RADIX = convert(int,d.RADIX),
  2049.             INTERVAL_PRECISION = convert(smallint,NULL),
  2050.             USERTYPE = t.usertype
  2051.         from master.dbo.spt_datatype_info d, master.dbo.spt_datatype_info_ext e, systypes t
  2052.         where
  2053.             DATA_TYPE = @data_type
  2054.             and d.ss_dtype = t.type
  2055.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2056.             and t.usertype *= e.user_type
  2057.             and t.type not in (111,109,38,110)    /* get rid of nullable types */
  2058.         order by 12, 11, t.usertype
  2059.  
  2060. go
  2061.  
  2062. if (charindex('6.00', @@version) = 0 and
  2063.     charindex('6.50', @@version) = 0 and
  2064.     charindex('7.00', @@version) = 0)
  2065. begin
  2066.     print ''
  2067.     print ''
  2068.     print 'Warning:'
  2069.     print 'you are installing the stored procedures '
  2070.     print 'on a pre 6.0 SQL Server.'
  2071.     print 'Ignore the following errors.'
  2072. end
  2073. else
  2074.     drop proc sp_datatype_info
  2075. go
  2076.  
  2077. /*    Procedure for 6.0 and 6.50 servers */
  2078. create proc sp_datatype_info
  2079.     (@data_type int = 0, @ODBCVer tinyint = 2)
  2080. as
  2081.     if @ODBCVer <> 3
  2082.         select @ODBCVer = 2
  2083.     if @data_type = 0
  2084.         select
  2085.             convert(varchar(32),case
  2086.                 when t.usertype > 100 or t.usertype in (18,80) then t.name
  2087.                 else d.TYPE_NAME
  2088.             end) TYPE_NAME,
  2089.             d.DATA_TYPE,
  2090.             convert(int,case
  2091.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  2092.                 when d.ss_dtype in (35,34) then 2147483647                /* TEXT/IMAGE */
  2093.                 when d.ss_dtype in (55,63,106,108) then @@max_precision /* DECIMAL/NUMERIC */
  2094.                 else t.prec
  2095.             end) "PRECISION",
  2096.             d.LITERAL_PREFIX,
  2097.             d.LITERAL_SUFFIX,
  2098.             e.CREATE_PARAMS,
  2099.             convert(smallint,case
  2100.                 when d.AUTO_INCREMENT = 1 then 0 /* IDENTITY*/
  2101.                 else t.allownulls
  2102.             end) NULLABLE,
  2103.             d.CASE_SENSITIVE,
  2104.             d.SEARCHABLE,
  2105.             d.UNSIGNED_ATTRIBUTE,
  2106.             d.MONEY,
  2107.             d.AUTO_INCREMENT,
  2108.             convert(varchar(32),case
  2109.                 when t.usertype > 100 or t.usertype in (18,80) then t.name
  2110.                 else d.TYPE_NAME
  2111.             end) LOCAL_TYPE_NAME,
  2112.             MINIMUM_SCALE = d.numeric_scale,
  2113.             convert(smallint,case
  2114.                 when d.ss_dtype in (106,108) and d.AUTO_INCREMENT = 0 then @@max_precision /* DECIMAL/NUMERIC */
  2115.                 when d.ss_dtype in (106,108) and d.AUTO_INCREMENT = 1 then 0 /* DECIMAL/NUMERIC IDENTITY*/
  2116.                 else t.scale
  2117.             end) MAXIMUM_SCALE,
  2118.             d.SQL_DATA_TYPE,
  2119.             d.SQL_DATETIME_SUB,
  2120.             NUM_PREC_RADIX = convert(int,d.RADIX),
  2121.             INTERVAL_PRECISION = convert(smallint,NULL),
  2122.             USERTYPE = t.usertype
  2123.         from master.dbo.spt_datatype_info d, master.dbo.spt_datatype_info_ext e, systypes t
  2124.         where
  2125.             d.ss_dtype = t.type
  2126.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2127.             and t.usertype *= e.user_type
  2128.             and isnull(d.AUTO_INCREMENT,0) *= e.AUTO_INCREMENT
  2129.             and t.type not in (111,109,38,110,55,63)    /* get rid of nullable types */
  2130.         order by 2, 12, 11, t.usertype
  2131.  
  2132.     else
  2133.         select
  2134.             convert(varchar(32),case
  2135.                 when t.usertype > 100 or t.usertype in (18,80) then t.name
  2136.                 else d.TYPE_NAME
  2137.             end) TYPE_NAME,
  2138.             d.DATA_TYPE,
  2139.             convert(int,case
  2140.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  2141.                 when d.ss_dtype in (35,34) then 2147483647                /* TEXT/IMAGE */
  2142.                 when d.ss_dtype in (55,63,106,108) then @@max_precision /* DECIMAL/NUMERIC */
  2143.                 else t.prec
  2144.             end) "PRECISION",
  2145.             d.LITERAL_PREFIX,
  2146.             d.LITERAL_SUFFIX,
  2147.             e.CREATE_PARAMS,
  2148.             convert(smallint,case
  2149.                 when d.AUTO_INCREMENT = 1 then 0 /* IDENTITY*/
  2150.                 else t.allownulls
  2151.             end) NULLABLE,
  2152.             d.CASE_SENSITIVE,
  2153.             d.SEARCHABLE,
  2154.             d.UNSIGNED_ATTRIBUTE,
  2155.             d.MONEY,
  2156.             d.AUTO_INCREMENT,
  2157.             convert(varchar(32),case
  2158.                 when t.usertype > 100 or t.usertype in (18,80) then t.name
  2159.                 else d.TYPE_NAME
  2160.             end) LOCAL_TYPE_NAME,
  2161.             MINIMUM_SCALE = d.numeric_scale,
  2162.             convert(smallint,case
  2163.                 when d.ss_dtype in (106,108) and d.AUTO_INCREMENT = 0 then convert(smallint,@@max_precision) /* DECIMAL/NUMERIC */
  2164.                 when d.ss_dtype in (106,108) and d.AUTO_INCREMENT = 1 then convert(smallint,0) /* DECIMAL/NUMERIC IDENTITY*/
  2165.                 else t.scale
  2166.             end) MAXIMUM_SCALE,
  2167.             d.SQL_DATA_TYPE,
  2168.             d.SQL_DATETIME_SUB,
  2169.             NUM_PREC_RADIX = convert(int,d.RADIX),
  2170.             INTERVAL_PRECISION = convert(smallint,NULL),
  2171.             USERTYPE = t.usertype
  2172.         from master.dbo.spt_datatype_info d, master.dbo.spt_datatype_info_ext e, systypes t
  2173.         where
  2174.             d.DATA_TYPE = @data_type
  2175.             and d.ss_dtype = t.type
  2176.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2177.             and t.usertype *= e.user_type
  2178.             and isnull(d.AUTO_INCREMENT,0) *= e.AUTO_INCREMENT
  2179.             and t.type not in (111,109,38,110,55,63)    /* get rid of nullable types */
  2180.         order by 12, 11, t.usertype
  2181. go
  2182.  
  2183. if (charindex('7.00', @@version) = 0)
  2184. begin
  2185.     print ''
  2186.     print ''
  2187.     print 'Warning:'
  2188.     print 'you are installing the stored procedures '
  2189.     print 'on a pre 7.0 SQL Server.'
  2190.     print 'Ignore the following errors.'
  2191. end
  2192. else
  2193.     drop proc sp_datatype_info
  2194. go
  2195.  
  2196. /*    Procedure for 7.0 server */
  2197. create proc sp_datatype_info
  2198.     (@data_type int = 0, @ODBCVer tinyint = 2)
  2199. as
  2200.     declare @mintype int
  2201.     declare @maxtype int
  2202.  
  2203.     if @ODBCVer <> 3
  2204.         select @ODBCVer = 2
  2205.     if @data_type = 0
  2206.     begin
  2207.         select @mintype = -32768
  2208.         select @maxtype = 32767
  2209.     end
  2210.     else
  2211.     begin
  2212.         select @mintype = @data_type
  2213.         select @maxtype = @data_type
  2214.     end
  2215.  
  2216.     select
  2217.         convert(sysname,case
  2218.             when t.xusertype > 255 then t.name
  2219.             else d.TYPE_NAME
  2220.         end) TYPE_NAME,
  2221.         d.DATA_TYPE,
  2222.         convert(int,case
  2223.             when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  2224.             when d.ss_dtype in (139,140) then @@max_precision /* DECIMAL/NUMERIC */
  2225.             else TypeProperty (t.name, 'Precision')
  2226.         end) "PRECISION",
  2227.         d.LITERAL_PREFIX,
  2228.         d.LITERAL_SUFFIX,
  2229.         e.CREATE_PARAMS,
  2230.         convert(smallint,case
  2231.             when d.AUTO_INCREMENT = 1 then 0 /* IDENTITY*/
  2232.             else TypeProperty (t.name, 'AllowsNull')
  2233.         end) NULLABLE,
  2234.         d.CASE_SENSITIVE,
  2235.         d.SEARCHABLE,
  2236.         d.UNSIGNED_ATTRIBUTE,
  2237.         d.MONEY,
  2238.         d.AUTO_INCREMENT,
  2239.         convert(sysname,case
  2240.             when t.xusertype > 255 then t.name
  2241.             else d.LOCAL_TYPE_NAME
  2242.         end) LOCAL_TYPE_NAME,
  2243.         MINIMUM_SCALE = d.numeric_scale,
  2244.         convert(smallint,case
  2245.             when d.ss_dtype in (139,140) and d.AUTO_INCREMENT = 0 then @@max_precision /* DECIMAL/NUMERIC */
  2246.             when d.ss_dtype in (139,140) and d.AUTO_INCREMENT = 1 then 0 /* DECIMAL/NUMERIC IDENTITY*/
  2247.             else TypeProperty (t.name, 'Scale')
  2248.         end) MAXIMUM_SCALE,
  2249.         d.SQL_DATA_TYPE,
  2250.         d.SQL_DATETIME_SUB,
  2251.         NUM_PREC_RADIX = convert(int,d.RADIX),
  2252.         INTERVAL_PRECISION = convert(smallint,NULL),
  2253.         USERTYPE = t.xusertype
  2254.     from master.dbo.spt_datatype_info d
  2255.         INNER JOIN systypes t on d.ss_dtype = t.xtype
  2256.         LEFT OUTER JOIN master.dbo.spt_datatype_info_ext e on
  2257.             t.xusertype = e.user_type
  2258.             and isnull(d.AUTO_INCREMENT,0) = e.AUTO_INCREMENT
  2259.     where
  2260.         d.DATA_TYPE between @mintype and @maxtype
  2261.         and (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2262.     order by 2, 12, 11, t.xusertype
  2263.  
  2264. go
  2265.  
  2266. grant execute on sp_datatype_info to public
  2267. go
  2268.  
  2269. dump tran master with no_log
  2270. go
  2271.  
  2272. print 'creating sp_fkeys'
  2273. go
  2274.  
  2275. /*    Procedure for pre-6.0 server */
  2276. CREATE PROCEDURE sp_fkeys(
  2277.                @pktable_name        varchar(32) = null,
  2278.                @pktable_owner        varchar(32) = null,
  2279.                @pktable_qualifier    varchar(32) = null,
  2280.                @fktable_name        varchar(32) = null,
  2281.                @fktable_owner        varchar(32) = null,
  2282.                @fktable_qualifier    varchar(32) = null )
  2283. as
  2284.     declare    @order_by_pk int
  2285.  
  2286.     select  @order_by_pk = 0
  2287.  
  2288.     if (@pktable_name is null) and (@fktable_name is null)
  2289.     begin    /* If neither primary key nor foreign key table names given */
  2290.         raiserror 20004 'PK table name or FK table name must be given.'
  2291.         return
  2292.     end
  2293.     if @fktable_qualifier is not null
  2294.     begin
  2295.         if db_name() <> @fktable_qualifier
  2296.         begin    /* If qualifier doesn't match current database */
  2297.             raiserror 20001 'Foreign Key Table qualifier must be name of current database'
  2298.             return
  2299.         end
  2300.     end
  2301.     if @pktable_qualifier is not null
  2302.     begin
  2303.         if db_name() <> @pktable_qualifier
  2304.         begin    /* If qualifier doesn't match current database */
  2305.             raiserror 20001 'Primary Key Table qualifier must be name of current database'
  2306.             return
  2307.         end
  2308.     end
  2309.  
  2310.     if @pktable_name is null
  2311.     begin /*  If table name not supplied, match all */
  2312.         select @pktable_name = '%'
  2313.         select @order_by_pk = 1
  2314.     end
  2315.     if @pktable_owner is null    /*    If PK owner not supplied, match all */
  2316.         select @pktable_owner = '%'
  2317.     if @fktable_name is null    /*    If table name not supplied, match all */
  2318.         select @fktable_name = '%'
  2319.     if @fktable_owner is null    /*    If FK owner not supplied, match all */
  2320.         select @fktable_owner = '%'
  2321.  
  2322.     if @@trancount <> 0
  2323.     begin    /* If inside a transaction */
  2324.         raiserror 20003 'The procedure ''sp_fkeys'' cannot be executed from within a transaction.'
  2325.         return
  2326.     end
  2327.     create table #fkeys(
  2328.              PKTABLE_QUALIFIER    varchar(32) NULL,
  2329.              PKTABLE_OWNER        varchar(32) NULL,
  2330.              PKTABLE_NAME        varchar(32) NOT NULL,
  2331.              PKCOLUMN_NAME        varchar(32) NOT NULL,
  2332.              FKTABLE_QUALIFIER    varchar(32) NULL,
  2333.              FKTABLE_OWNER        varchar(32) NULL,
  2334.              FKTABLE_NAME        varchar(32) NOT NULL,
  2335.              FKCOLUMN_NAME        varchar(32) NOT NULL,
  2336.              KEY_SEQ            smallint NOT NULL)
  2337.  
  2338.     /*    SQL Server supports upto 8 PK/FK relationships between 2 tables */
  2339.     /*    Process syskeys for each relationship */
  2340.     /*    The inserts below adds a row to the temp table for each of the
  2341.         8 possible relationships */
  2342.     insert into #fkeys
  2343.         select
  2344.             db_name(),
  2345.             user_name(o1.uid),
  2346.             object_name(k.depid),
  2347.             c2.name,
  2348.             db_name(),
  2349.             user_name(o2.uid),
  2350.             object_name(k.id),
  2351.             c1.name,
  2352.             1
  2353.         from
  2354.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  2355.         where
  2356.             c1.id = k.id
  2357.             and k.type = 2    /* Foreign type key */
  2358.             and c1.colid = k.key1
  2359.             and c2.id = k.depid
  2360.             and c2.colid = k.depkey1
  2361.             and o1.id = k.depid
  2362.             and o2.id = k.id
  2363.     union all
  2364.         select
  2365.             db_name(),
  2366.             user_name(o1.uid),
  2367.             object_name(k.depid),
  2368.             c2.name,
  2369.             db_name(),
  2370.             user_name(o2.uid),
  2371.             object_name(k.id),
  2372.             c1.name,
  2373.             2
  2374.         from
  2375.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  2376.         where
  2377.             c1.id = k.id
  2378.             and k.type = 2    /* Foreign type key */
  2379.             and c1.colid = k.key2
  2380.             and c2.id = k.depid
  2381.             and c2.colid = k.depkey2
  2382.             and o1.id = k.depid
  2383.             and o2.id = k.id
  2384.     union all
  2385.         select
  2386.             db_name(),
  2387.             user_name(o1.uid),
  2388.             object_name(k.depid),
  2389.             c2.name,
  2390.             db_name(),
  2391.             user_name(o2.uid),
  2392.             object_name(k.id),
  2393.             c1.name,
  2394.             3
  2395.         from
  2396.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  2397.         where
  2398.             c1.id = k.id
  2399.             and k.type = 2    /* Foreign type key */
  2400.             and c1.colid = k.key3
  2401.             and c2.id = k.depid
  2402.             and c2.colid = k.depkey3
  2403.             and o1.id = k.depid
  2404.             and o2.id = k.id
  2405.     union all
  2406.         select
  2407.             db_name(),
  2408.             user_name(o1.uid),
  2409.             object_name(k.depid),
  2410.             c2.name,
  2411.             db_name(),
  2412.             user_name(o2.uid),
  2413.             object_name(k.id),
  2414.             c1.name,
  2415.             4
  2416.         from
  2417.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  2418.         where
  2419.             c1.id = k.id
  2420.             and k.type = 2    /* Foreign type key */
  2421.             and c1.colid = k.key4
  2422.             and c2.id = k.depid
  2423.             and c2.colid = k.depkey4
  2424.             and o1.id = k.depid
  2425.             and o2.id = k.id
  2426.     union all
  2427.         select
  2428.             db_name(),
  2429.             user_name(o1.uid),
  2430.             object_name(k.depid),
  2431.             c2.name,
  2432.             db_name(),
  2433.             user_name(o2.uid),
  2434.             object_name(k.id),
  2435.             c1.name,
  2436.             5
  2437.         from
  2438.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  2439.         where
  2440.             c1.id = k.id
  2441.             and k.type = 2    /* Foreign type key */
  2442.             and c1.colid = k.key5
  2443.             and c2.id = k.depid
  2444.             and c2.colid = k.depkey5
  2445.             and o1.id = k.depid
  2446.             and o2.id = k.id
  2447.     union all
  2448.         select
  2449.             db_name(),
  2450.             user_name(o1.uid),
  2451.             object_name(k.depid),
  2452.             c2.name,
  2453.             db_name(),
  2454.             user_name(o2.uid),
  2455.             object_name(k.id),
  2456.             c1.name,
  2457.             6
  2458.         from
  2459.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  2460.         where
  2461.             c1.id = k.id
  2462.             and k.type = 2    /* Foreign type key */
  2463.             and c1.colid = k.key6
  2464.             and c2.id = k.depid
  2465.             and c2.colid = k.depkey6
  2466.             and o1.id = k.depid
  2467.             and o2.id = k.id
  2468.     union all
  2469.         select
  2470.             db_name(),
  2471.             user_name(o1.uid),
  2472.             object_name(k.depid),
  2473.             c2.name,
  2474.             db_name(),
  2475.             user_name(o2.uid),
  2476.             object_name(k.id),
  2477.             c1.name,
  2478.             7
  2479.         from
  2480.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  2481.         where
  2482.             c1.id = k.id
  2483.             and k.type = 2    /* Foreign type key */
  2484.             and c1.colid = k.key7
  2485.             and c2.id = k.depid
  2486.             and c2.colid = k.depkey7
  2487.             and o1.id = k.depid
  2488.             and o2.id = k.id
  2489.     union all
  2490.         select
  2491.             db_name(),
  2492.             user_name(o1.uid),
  2493.             object_name(k.depid),
  2494.             c2.name,
  2495.             db_name(),
  2496.             user_name(o2.uid),
  2497.             object_name(k.id),
  2498.             c1.name,
  2499.             8
  2500.         from
  2501.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  2502.         where
  2503.             c1.id = k.id
  2504.             and k.type = 2    /* Foreign type key */
  2505.             and c1.colid = k.key8
  2506.             and c2.id = k.depid
  2507.             and c2.colid = k.depkey8
  2508.             and o1.id = k.depid
  2509.             and o2.id = k.id
  2510.  
  2511.     if @order_by_pk = 1 /*    If order by PK fields */
  2512.         select
  2513.             PKTABLE_QUALIFIER,
  2514.             PKTABLE_OWNER,
  2515.             PKTABLE_NAME,
  2516.             PKCOLUMN_NAME,
  2517.             FKTABLE_QUALIFIER,
  2518.             FKTABLE_OWNER,
  2519.             FKTABLE_NAME,
  2520.             FKCOLUMN_NAME,
  2521.             KEY_SEQ,
  2522.             UPDATE_RULE = convert(smallint, null),
  2523.             DELETE_RULE = convert(smallint,null),
  2524.             FK_NAME = convert(varchar(32),null),
  2525.             PK_NAME = convert(varchar(32),null)
  2526.         from #fkeys
  2527.         where FKTABLE_NAME like @fktable_name
  2528.             and FKTABLE_OWNER like @fktable_owner
  2529.             and PKTABLE_NAME  like @pktable_name
  2530.             and PKTABLE_OWNER like @pktable_owner
  2531.         order by 1, 2, 3, 9
  2532.     else        /*    Order by FK fields */
  2533.         select
  2534.             PKTABLE_QUALIFIER,
  2535.             PKTABLE_OWNER,
  2536.             PKTABLE_NAME,
  2537.             PKCOLUMN_NAME,
  2538.             FKTABLE_QUALIFIER,
  2539.             FKTABLE_OWNER,
  2540.             FKTABLE_NAME,
  2541.             FKCOLUMN_NAME,
  2542.             KEY_SEQ,
  2543.             UPDATE_RULE = convert(smallint,null),
  2544.             DELETE_RULE = convert(smallint,null),
  2545.             FK_NAME = convert(varchar(32),null),
  2546.             PK_NAME = convert(varchar(32),null)
  2547.         from #fkeys
  2548.         where FKTABLE_NAME like @fktable_name
  2549.             and FKTABLE_OWNER like @fktable_owner
  2550.             and PKTABLE_NAME  like @pktable_name
  2551.             and PKTABLE_OWNER like @pktable_owner
  2552.         order by 5, 6, 7, 9
  2553. go
  2554.  
  2555. if (charindex('6.00', @@version) = 0 and
  2556.     charindex('6.50', @@version) = 0 and
  2557.     charindex('7.00', @@version) = 0)
  2558. begin
  2559.     print ''
  2560.     print ''
  2561.     print 'Warning:'
  2562.     print 'you are installing the stored procedures '
  2563.     print 'on a pre 6.0 SQL Server.'
  2564.     print 'Ignore the following errors.'
  2565. end
  2566. else
  2567.     drop proc sp_fkeys
  2568. go
  2569.  
  2570. /*    Procedure for 6.0 and 6.50 servers */
  2571. CREATE PROCEDURE sp_fkeys(
  2572.                @pktable_name        varchar(32) = null,
  2573.                @pktable_owner        varchar(32) = null,
  2574.                @pktable_qualifier    varchar(32) = null,
  2575.                @fktable_name        varchar(32) = null,
  2576.                @fktable_owner        varchar(32) = null,
  2577.                @fktable_qualifier    varchar(32) = null )
  2578. as
  2579.     DECLARE @pktable_id            int
  2580.     DECLARE @pkfull_table_name    varchar(65) /* 2*32 +1 */
  2581.     DECLARE @fktable_id            int
  2582.     DECLARE @fkfull_table_name    varchar(65) /* 2*32 +1 */
  2583.     declare    @order_by_pk        int
  2584.  
  2585.     select  @order_by_pk = 0
  2586.  
  2587.     if (@pktable_name is null) and (@fktable_name is null)
  2588.     begin    /* If neither primary key nor foreign key table names given */
  2589.         raiserror (15252,-1,-1)
  2590.         return
  2591.     end
  2592.     if @fktable_qualifier is not null
  2593.     begin
  2594.         if db_name() <> @fktable_qualifier
  2595.         begin    /* If qualifier doesn't match current database */
  2596.             raiserror 20001 'Foreign Key Table qualifier must be name of current database'
  2597.             return
  2598.         end
  2599.     end
  2600.     if @pktable_qualifier is not null
  2601.     begin
  2602.         if db_name() <> @pktable_qualifier
  2603.         begin    /* If qualifier doesn't match current database */
  2604.             raiserror 20001 'Primary Key Table qualifier must be name of current database'
  2605.             return
  2606.         end
  2607.     end
  2608.  
  2609.     if @pktable_owner is null
  2610.     begin    /* If unqualified primary key table name */
  2611.         SELECT @pkfull_table_name = @pktable_name
  2612.     end
  2613.     else
  2614.     begin    /* Qualified primary key table name */
  2615.         SELECT @pkfull_table_name = @pktable_owner + '.' + @pktable_name
  2616.     end
  2617.     /*    Get Object ID */
  2618.     SELECT @pktable_id = object_id(@pkfull_table_name)
  2619.  
  2620.     if @fktable_owner is null
  2621.     begin    /* If unqualified foreign key table name */
  2622.         SELECT @fkfull_table_name = @fktable_name
  2623.     end
  2624.     else
  2625.     begin    /* Qualified foreign key table name */
  2626.         SELECT @fkfull_table_name = @fktable_owner + '.' + @fktable_name
  2627.     end
  2628.     /*    Get Object ID */
  2629.     SELECT @fktable_id = object_id(@fkfull_table_name)
  2630.  
  2631.     if @fktable_name is not null
  2632.     begin
  2633.         if @fktable_id is null
  2634.             SELECT @fktable_id = 0    /* fk table not found, empty result */
  2635.     end
  2636.  
  2637.     if @pktable_name is null
  2638.     begin /*  If table name not supplied, match all */
  2639.         select @order_by_pk = 1
  2640.     end
  2641.     else
  2642.     begin
  2643.         if @pktable_id is null
  2644.         begin
  2645.             SELECT @pktable_id = 0    /* pk table not found, empty result */
  2646.         end
  2647.     end
  2648.  
  2649.     if (@@trancount <> 0 and
  2650.         charindex('6.50', @@version) = 0)
  2651.     begin    /* If inside a transaction */
  2652.         raiserror(15002,-1,-1,'sp_fkeys')
  2653.         return
  2654.     end
  2655.  
  2656.     create table #fkeys(
  2657.              pkdb_id        int NOT NULL,
  2658.              pktable_id     int NOT NULL,
  2659.              pkcolid        int NOT NULL,
  2660.              fkdb_id        int NOT NULL,
  2661.              fktable_id        int NOT NULL,
  2662.              fkcolid        int NOT NULL,
  2663.              KEY_SEQ        smallint NOT NULL,
  2664.              fk_id            int NOT NULL,
  2665.              pk_id            int NOT NULL)
  2666.  
  2667.     /*    SQL Server supports upto 16 PK/FK relationships between 2 tables */
  2668.     /*    Process syskeys for each relationship */
  2669.     /*    The inserts below adds a row to the temp table for each of the
  2670.         16 possible relationships */
  2671.     insert into #fkeys
  2672.         select
  2673.             r.rkeydbid,
  2674.             r.rkeyid,
  2675.             r.rkey1,
  2676.             r.fkeydbid,
  2677.             r.fkeyid,
  2678.             r.fkey1,
  2679.             1,
  2680.             r.constid,
  2681.             s.constid
  2682.         from
  2683.             sysreferences r, sysconstraints s
  2684.         where    r.rkeyid = s.id
  2685.             AND (s.status & 0xf) = 1
  2686.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  2687.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  2688.       union all
  2689.         select
  2690.             r.rkeydbid,
  2691.             r.rkeyid,
  2692.             r.rkey2,
  2693.             r.fkeydbid,
  2694.             r.fkeyid,
  2695.             r.fkey2,
  2696.             2,
  2697.             r.constid,
  2698.             s.constid
  2699.         from
  2700.             sysreferences r, sysconstraints s
  2701.         where    r.rkeyid = s.id
  2702.             AND (s.status & 0xf) = 1
  2703.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  2704.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  2705.       union all
  2706.         select
  2707.             r.rkeydbid,
  2708.             r.rkeyid,
  2709.             r.rkey3,
  2710.             r.fkeydbid,
  2711.             r.fkeyid,
  2712.             r.fkey3,
  2713.             3,
  2714.             r.constid,
  2715.             s.constid
  2716.         from
  2717.             sysreferences r, sysconstraints s
  2718.         where    r.rkeyid = s.id
  2719.             AND (s.status & 0xf) = 1
  2720.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  2721.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  2722.       union all
  2723.         select
  2724.             r.rkeydbid,
  2725.             r.rkeyid,
  2726.             r.rkey4,
  2727.             r.fkeydbid,
  2728.             r.fkeyid,
  2729.             r.fkey4,
  2730.             4,
  2731.             r.constid,
  2732.             s.constid
  2733.         from
  2734.             sysreferences r, sysconstraints s
  2735.         where    r.rkeyid = s.id
  2736.             AND (s.status & 0xf) = 1
  2737.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  2738.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  2739.       union all
  2740.         select
  2741.             r.rkeydbid,
  2742.             r.rkeyid,
  2743.             r.rkey5,
  2744.             r.fkeydbid,
  2745.             r.fkeyid,
  2746.             r.fkey5,
  2747.             5,
  2748.             r.constid,
  2749.             s.constid
  2750.         from
  2751.             sysreferences r, sysconstraints s
  2752.         where    r.rkeyid = s.id
  2753.             AND (s.status & 0xf) = 1
  2754.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  2755.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  2756.       union all
  2757.         select
  2758.             r.rkeydbid,
  2759.             r.rkeyid,
  2760.             r.rkey6,
  2761.             r.fkeydbid,
  2762.             r.fkeyid,
  2763.             r.fkey6,
  2764.             6,
  2765.             r.constid,
  2766.             s.constid
  2767.         from
  2768.             sysreferences r, sysconstraints s
  2769.         where    r.rkeyid = s.id
  2770.             AND (s.status & 0xf) = 1
  2771.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  2772.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  2773.       union all
  2774.         select
  2775.             r.rkeydbid,
  2776.             r.rkeyid,
  2777.             r.rkey7,
  2778.             r.fkeydbid,
  2779.             r.fkeyid,
  2780.             r.fkey7,
  2781.             7,
  2782.             r.constid,
  2783.             s.constid
  2784.         from
  2785.             sysreferences r, sysconstraints s
  2786.         where    r.rkeyid = s.id
  2787.             AND (s.status & 0xf) = 1
  2788.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  2789.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  2790.       union all
  2791.         select
  2792.             r.rkeydbid,
  2793.             r.rkeyid,
  2794.             r.rkey8,
  2795.             r.fkeydbid,
  2796.             r.fkeyid,
  2797.             r.fkey8,
  2798.             8,
  2799.             r.constid,
  2800.             s.constid
  2801.         from
  2802.             sysreferences r, sysconstraints s
  2803.         where    r.rkeyid = s.id
  2804.             AND (s.status & 0xf) = 1
  2805.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  2806.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  2807.       union all
  2808.         select
  2809.             r.rkeydbid,
  2810.             r.rkeyid,
  2811.             r.rkey9,
  2812.             r.fkeydbid,
  2813.             r.fkeyid,
  2814.             r.fkey9,
  2815.             9,
  2816.             r.constid,
  2817.             s.constid
  2818.         from
  2819.             sysreferences r, sysconstraints s
  2820.         where    r.rkeyid = s.id
  2821.             AND (s.status & 0xf) = 1
  2822.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  2823.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  2824.       union all
  2825.         select
  2826.             r.rkeydbid,
  2827.             r.rkeyid,
  2828.             r.rkey10,
  2829.             r.fkeydbid,
  2830.             r.fkeyid,
  2831.             r.fkey10,
  2832.             10,
  2833.             r.constid,
  2834.             s.constid
  2835.         from
  2836.             sysreferences r, sysconstraints s
  2837.         where    r.rkeyid = s.id
  2838.             AND (s.status & 0xf) = 1
  2839.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  2840.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  2841.       union all
  2842.         select
  2843.             r.rkeydbid,
  2844.             r.rkeyid,
  2845.             r.rkey11,
  2846.             r.fkeydbid,
  2847.             r.fkeyid,
  2848.             r.fkey11,
  2849.             11,
  2850.             r.constid,
  2851.             s.constid
  2852.         from
  2853.             sysreferences r, sysconstraints s
  2854.         where    r.rkeyid = s.id
  2855.             AND (s.status & 0xf) = 1
  2856.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  2857.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  2858.       union all
  2859.         select
  2860.             r.rkeydbid,
  2861.             r.rkeyid,
  2862.             r.rkey12,
  2863.             r.fkeydbid,
  2864.             r.fkeyid,
  2865.             r.fkey12,
  2866.             12,
  2867.             r.constid,
  2868.             s.constid
  2869.         from
  2870.             sysreferences r, sysconstraints s
  2871.         where    r.rkeyid = s.id
  2872.             AND (s.status & 0xf) = 1
  2873.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  2874.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  2875.       union all
  2876.         select
  2877.             r.rkeydbid,
  2878.             r.rkeyid,
  2879.             r.rkey13,
  2880.             r.fkeydbid,
  2881.             r.fkeyid,
  2882.             r.fkey13,
  2883.             13,
  2884.             r.constid,
  2885.             s.constid
  2886.         from
  2887.             sysreferences r, sysconstraints s
  2888.         where    r.rkeyid = s.id
  2889.             AND (s.status & 0xf) = 1
  2890.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  2891.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  2892.       union all
  2893.         select
  2894.             r.rkeydbid,
  2895.             r.rkeyid,
  2896.             r.rkey14,
  2897.             r.fkeydbid,
  2898.             r.fkeyid,
  2899.             r.fkey14,
  2900.             14,
  2901.             r.constid,
  2902.             s.constid
  2903.         from
  2904.             sysreferences r, sysconstraints s
  2905.         where    r.rkeyid = s.id
  2906.             AND (s.status & 0xf) = 1
  2907.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  2908.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  2909.       union all
  2910.         select
  2911.             r.rkeydbid,
  2912.             r.rkeyid,
  2913.             r.rkey15,
  2914.             r.fkeydbid,
  2915.             r.fkeyid,
  2916.             r.fkey15,
  2917.             15,
  2918.             r.constid,
  2919.             s.constid
  2920.         from
  2921.             sysreferences r, sysconstraints s
  2922.         where    r.rkeyid = s.id
  2923.             AND (s.status & 0xf) = 1
  2924.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  2925.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  2926.       union all
  2927.         select
  2928.             r.rkeydbid,
  2929.             r.rkeyid,
  2930.             r.rkey16,
  2931.             r.fkeydbid,
  2932.             r.fkeyid,
  2933.             r.fkey16,
  2934.             16,
  2935.             r.constid,
  2936.             s.constid
  2937.         from
  2938.             sysreferences r, sysconstraints s
  2939.         where    r.rkeyid = s.id
  2940.             AND (s.status & 0xf) = 1
  2941.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  2942.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  2943.  
  2944.     if @order_by_pk = 1 /*    If order by PK fields */
  2945.         select
  2946.             PKTABLE_QUALIFIER = convert(varchar(32),DB_NAME(f.pkdb_id)),
  2947.             PKTABLE_OWNER = convert(varchar(32),USER_NAME(o1.uid)),
  2948.             PKTABLE_NAME = convert(varchar(32),o1.name),
  2949.             PKCOLUMN_NAME = convert(varchar(32),c1.name),
  2950.             FKTABLE_QUALIFIER = convert(varchar(32),DB_NAME(f.fkdb_id)),
  2951.             FKTABLE_OWNER = convert(varchar(32),USER_NAME(o2.uid)),
  2952.             FKTABLE_NAME = convert(varchar(32),o2.name),
  2953.             FKCOLUMN_NAME = convert(varchar(32),c2.name),
  2954.             KEY_SEQ,
  2955.             UPDATE_RULE = convert(smallint,1),
  2956.             DELETE_RULE = convert(smallint,1),
  2957.             FK_NAME = convert(varchar(32),OBJECT_NAME(fk_id)),
  2958.             PK_NAME = convert(varchar(32),OBJECT_NAME(pk_id))
  2959.         from #fkeys f,
  2960.             sysobjects o1, sysobjects o2,
  2961.             syscolumns c1, syscolumns c2
  2962.         where    o1.id = f.pktable_id
  2963.             AND o2.id = f.fktable_id
  2964.             AND c1.id = f.pktable_id
  2965.             AND c2.id = f.fktable_id
  2966.             AND c1.colid = f.pkcolid
  2967.             AND c2.colid = f.fkcolid
  2968.         order by 1,2,3,9
  2969.     else        /*    Order by FK fields */
  2970.         select
  2971.             PKTABLE_QUALIFIER = convert(varchar(32),DB_NAME(f.pkdb_id)),
  2972.             PKTABLE_OWNER = convert(varchar(32),USER_NAME(o1.uid)),
  2973.             PKTABLE_NAME = convert(varchar(32),o1.name),
  2974.             PKCOLUMN_NAME = convert(varchar(32),c1.name),
  2975.             FKTABLE_QUALIFIER = convert(varchar(32),DB_NAME(f.fkdb_id)),
  2976.             FKTABLE_OWNER = convert(varchar(32),USER_NAME(o2.uid)),
  2977.             FKTABLE_NAME = convert(varchar(32),o2.name),
  2978.             FKCOLUMN_NAME = convert(varchar(32),c2.name),
  2979.             KEY_SEQ,
  2980.             UPDATE_RULE = convert(smallint,1),
  2981.             DELETE_RULE = convert(smallint,1),
  2982.             FK_NAME = convert(varchar(32),OBJECT_NAME(fk_id)),
  2983.             PK_NAME = convert(varchar(32),OBJECT_NAME(pk_id))
  2984.         from #fkeys f,
  2985.             sysobjects o1, sysobjects o2,
  2986.             syscolumns c1, syscolumns c2
  2987.         where    o1.id = f.pktable_id
  2988.             AND o2.id = f.fktable_id
  2989.             AND c1.id = f.pktable_id
  2990.             AND c2.id = f.fktable_id
  2991.             AND c1.colid = f.pkcolid
  2992.             AND c2.colid = f.fkcolid
  2993.         order by 5,6,7,9
  2994. go
  2995.  
  2996. if (charindex('7.00', @@version) = 0)
  2997. begin
  2998.     print ''
  2999.     print ''
  3000.     print 'Warning:'
  3001.     print 'you are installing the stored procedures '
  3002.     print 'on a pre 7.0 SQL Server.'
  3003.     print 'Ignore the following errors.'
  3004. end
  3005. else
  3006.     drop proc sp_fkeys
  3007. go
  3008.  
  3009. /*    Procedure for 7.0 and later servers */
  3010. CREATE PROCEDURE sp_fkeys(
  3011.                @pktable_name        sysname = null,
  3012.                @pktable_owner        sysname = null,
  3013.                @pktable_qualifier    sysname = null,
  3014.                @fktable_name        sysname = null,
  3015.                @fktable_owner        sysname = null,
  3016.                @fktable_qualifier    sysname = null )
  3017. as
  3018.     DECLARE @pktable_id            int
  3019.     DECLARE @pkfull_table_name    varchar(257) /* 2*128 + 1 */
  3020.     DECLARE @fktable_id            int
  3021.     DECLARE @fkfull_table_name    varchar(257) /* 2*128 + 1 */
  3022.     declare    @order_by_pk        int
  3023.  
  3024.     /* select 'XXX starting table creation' */
  3025.  
  3026.     create table #fkeysall(
  3027.             rkeyid int NOT NULL,
  3028.             rkey1 int NOT NULL,
  3029.                 rkey2 int NOT NULL,
  3030.                 rkey3 int NOT NULL,
  3031.                 rkey4 int NOT NULL,
  3032.                 rkey5 int NOT NULL,
  3033.                 rkey6 int NOT NULL,
  3034.                 rkey7 int NOT NULL,
  3035.                 rkey8 int NOT NULL,
  3036.                 rkey9 int NOT NULL,
  3037.                 rkey10 int NOT NULL,
  3038.                 rkey11 int NOT NULL,
  3039.                 rkey12 int NOT NULL,
  3040.                 rkey13 int NOT NULL,
  3041.                 rkey14 int NOT NULL,
  3042.                 rkey15 int NOT NULL,
  3043.                 rkey16 int NOT NULL,
  3044.             fkeyid int NOT NULL,
  3045.             fkey1 int NOT NULL,
  3046.                 fkey2 int NOT NULL,
  3047.                 fkey3 int NOT NULL,
  3048.                 fkey4 int NOT NULL,
  3049.                 fkey5 int NOT NULL,
  3050.                 fkey6 int NOT NULL,
  3051.                 fkey7 int NOT NULL,
  3052.                 fkey8 int NOT NULL,
  3053.                 fkey9 int NOT NULL,
  3054.                 fkey10 int NOT NULL,
  3055.                 fkey11 int NOT NULL,
  3056.                 fkey12 int NOT NULL,
  3057.                 fkey13 int NOT NULL,
  3058.                 fkey14 int NOT NULL,
  3059.                 fkey15 int NOT NULL,
  3060.                 fkey16 int NOT NULL,
  3061.             constid int NOT NULL,
  3062.             name sysname NOT NULL)
  3063.  
  3064.     create table #fkeys(
  3065.             pktable_id        int NOT NULL,
  3066.             pkcolid         int NOT NULL,
  3067.             fktable_id        int NOT NULL,
  3068.             fkcolid         int NOT NULL,
  3069.             KEY_SEQ         smallint NOT NULL,
  3070.             fk_id            int NOT NULL,
  3071.             PK_NAME            sysname NOT NULL)
  3072.  
  3073.     create table #fkeysout(
  3074.             PKTABLE_QUALIFIER sysname NULL,
  3075.             PKTABLE_OWNER sysname NULL,
  3076.             PKTABLE_NAME sysname NOT NULL,
  3077.             PKCOLUMN_NAME sysname NOT NULL,
  3078.             FKTABLE_QUALIFIER sysname NULL,
  3079.             FKTABLE_OWNER sysname NULL,
  3080.             FKTABLE_NAME sysname NOT NULL,
  3081.             FKCOLUMN_NAME sysname NOT NULL,
  3082.             KEY_SEQ smallint NOT NULL,
  3083.             UPDATE_RULE smallint NULL,
  3084.             DELETE_RULE smallint NULL,
  3085.             FK_NAME sysname NULL,
  3086.             PK_NAME sysname NULL,
  3087.             DEFERRABILITY smallint null)
  3088.  
  3089.     /* select 'XXX starting parameter analysis' */
  3090.  
  3091.     select  @order_by_pk = 0
  3092.  
  3093.     if (@pktable_name is null) and (@fktable_name is null)
  3094.     begin    /* If neither primary key nor foreign key table names given */
  3095.         raiserror (15252,-1,-1)
  3096.         return
  3097.     end
  3098.     if @fktable_qualifier is not null
  3099.     begin
  3100.         if db_name() <> @fktable_qualifier
  3101.         begin    /* If qualifier doesn't match current database */
  3102.             raiserror 20001 'Foreign Key Table qualifier must be name of current database'
  3103.             return
  3104.         end
  3105.     end
  3106.     if @pktable_qualifier is not null
  3107.     begin
  3108.         if db_name() <> @pktable_qualifier
  3109.         begin    /* If qualifier doesn't match current database */
  3110.             raiserror 20001 'Primary Key Table qualifier must be name of current database'
  3111.             return
  3112.         end
  3113.     end
  3114.  
  3115.     if @pktable_owner is null
  3116.     begin    /* If unqualified primary key table name */
  3117.         SELECT @pkfull_table_name = @pktable_name
  3118.     end
  3119.     else
  3120.     begin    /* Qualified primary key table name */
  3121.         if @pktable_owner = ''
  3122.         begin    /* If empty owner name */
  3123.             SELECT @pkfull_table_name = @pktable_owner
  3124.         end
  3125.         else
  3126.         begin
  3127.             SELECT @pkfull_table_name = @pktable_owner + '.' + @pktable_name
  3128.         end
  3129.     end
  3130.     /*    Get Object ID */
  3131.     SELECT @pktable_id = object_id(@pkfull_table_name)
  3132.  
  3133.     if @fktable_owner is null
  3134.     begin    /* If unqualified foreign key table name */
  3135.         SELECT @fkfull_table_name = @fktable_name
  3136.     end
  3137.     else
  3138.     begin    /* Qualified foreign key table name */
  3139.         if @fktable_owner = ''
  3140.         begin    /* If empty owner name */
  3141.             SELECT @fkfull_table_name = @fktable_owner
  3142.         end
  3143.         else
  3144.         begin
  3145.             SELECT @fkfull_table_name = @fktable_owner + '.' + @fktable_name
  3146.         end
  3147.     end
  3148.     /*    Get Object ID */
  3149.     SELECT @fktable_id = object_id(@fkfull_table_name)
  3150.  
  3151.     if @fktable_name is not null
  3152.     begin
  3153.         if @fktable_id is null
  3154.             SELECT @fktable_id = 0    /* fk table not found, empty result */
  3155.     end
  3156.  
  3157.     if @pktable_name is null
  3158.     begin /*  If table name not supplied, match all */
  3159.         select @order_by_pk = 1
  3160.     end
  3161.     else
  3162.     begin
  3163.         if @pktable_id is null
  3164.         begin
  3165.             SELECT @pktable_id = 0    /* pk table not found, empty result */
  3166.         end
  3167.     end
  3168.  
  3169.     /*    SQL Server supports upto 16 PK/FK relationships between 2 tables */
  3170.     /*    Process syskeys for each relationship */
  3171.     /*  First, attempt to get all 16 keys for each rel'ship, then sort
  3172.         them out with a 16-way "insert select ... union select ..." */
  3173.  
  3174.     /* select 'XXX starting data analysis' */
  3175.  
  3176.     insert into #fkeysall
  3177.         select
  3178.             r.rkeyid,
  3179.             r.rkey1, r.rkey2, r.rkey3, r.rkey4,
  3180.                 r.rkey5, r.rkey6, r.rkey7, r.rkey8,
  3181.                 r.rkey9, r.rkey10, r.rkey11, r.rkey12,
  3182.                 r.rkey13, r.rkey14, r.rkey15, r.rkey16,
  3183.             r.fkeyid,
  3184.             r.fkey1, r.fkey2, r.fkey3, r.fkey4,
  3185.                 r.fkey5, r.fkey6, r.fkey7, r.fkey8,
  3186.                 r.fkey9, r.fkey10, r.fkey11, r.fkey12,
  3187.                 r.fkey13, r.fkey14, r.fkey15, r.fkey16,
  3188.             r.constid,
  3189.             i.name
  3190.         from
  3191.             sysreferences r, sysobjects o, sysindexes i
  3192.         where    r.constid = o.id
  3193.             AND o.xtype = 'F'
  3194.             AND r.rkeyindid = i.indid
  3195.             AND r.rkeyid = i.id
  3196.             AND r.rkeyid between isnull(@pktable_id, 0)
  3197.                             and isnull(@pktable_id, 0x7fffffff)
  3198.             AND r.fkeyid between isnull(@fktable_id, 0)
  3199.                             and isnull(@fktable_id, 0x7fffffff)
  3200.  
  3201.     /* select count (*) as 'XXX countall' from #fkeysall */
  3202.  
  3203.     insert into #fkeys
  3204.             select rkeyid, rkey1, fkeyid, fkey1, 1, constid, name
  3205.             from #fkeysall
  3206.         union all
  3207.             select rkeyid, rkey2, fkeyid, fkey2, 2, constid, name
  3208.             from #fkeysall
  3209.         union all
  3210.             select rkeyid, rkey3, fkeyid, fkey3, 3, constid, name
  3211.             from #fkeysall
  3212.         union all
  3213.             select rkeyid, rkey4, fkeyid, fkey4, 4, constid, name
  3214.             from #fkeysall
  3215.         union all
  3216.             select rkeyid, rkey5, fkeyid, fkey5, 5, constid, name
  3217.             from #fkeysall
  3218.         union all
  3219.             select rkeyid, rkey6, fkeyid, fkey6, 6, constid, name
  3220.             from #fkeysall
  3221.         union all
  3222.             select rkeyid, rkey7, fkeyid, fkey7, 7, constid, name
  3223.             from #fkeysall
  3224.         union all
  3225.             select rkeyid, rkey8, fkeyid, fkey8, 8, constid, name
  3226.             from #fkeysall
  3227.         union all
  3228.             select rkeyid, rkey9, fkeyid, fkey9, 9, constid, name
  3229.             from #fkeysall
  3230.         union all
  3231.             select rkeyid, rkey10, fkeyid, fkey10, 10, constid, name
  3232.             from #fkeysall
  3233.         union all
  3234.             select rkeyid, rkey11, fkeyid, fkey11, 11, constid, name
  3235.             from #fkeysall
  3236.         union all
  3237.             select rkeyid, rkey12, fkeyid, fkey12, 12, constid, name
  3238.             from #fkeysall
  3239.         union all
  3240.             select rkeyid, rkey13, fkeyid, fkey13, 13, constid, name
  3241.             from #fkeysall
  3242.         union all
  3243.             select rkeyid, rkey14, fkeyid, fkey14, 14, constid, name
  3244.             from #fkeysall
  3245.         union all
  3246.             select rkeyid, rkey15, fkeyid, fkey15, 15, constid, name
  3247.             from #fkeysall
  3248.         union all
  3249.             select rkeyid, rkey16, fkeyid, fkey16, 16, constid, name
  3250.             from #fkeysall
  3251.  
  3252.     /* select count (*) as 'XXX count' from #fkeys */
  3253.  
  3254.     insert into #fkeysout
  3255.         select
  3256.             PKTABLE_QUALIFIER = convert(sysname,db_name()),
  3257.             PKTABLE_OWNER = convert(sysname,USER_NAME(o1.uid)),
  3258.             PKTABLE_NAME = convert(sysname,o1.name),
  3259.             PKCOLUMN_NAME = convert(sysname,c1.name),
  3260.             FKTABLE_QUALIFIER = convert(sysname,db_name()),
  3261.             FKTABLE_OWNER = convert(sysname,USER_NAME(o2.uid)),
  3262.             FKTABLE_NAME = convert(sysname,o2.name),
  3263.             FKCOLUMN_NAME = convert(sysname,c2.name),
  3264.             KEY_SEQ,
  3265.             UPDATE_RULE = convert(smallint,1),
  3266.             DELETE_RULE = convert(smallint,1),
  3267.             FK_NAME = convert(sysname,OBJECT_NAME(fk_id)),
  3268.             PK_NAME,
  3269.             DEFERRABILITY = 7    /* SQL_NOT_DEFERRABLE */
  3270.         from #fkeys f,
  3271.             sysobjects o1, sysobjects o2,
  3272.             syscolumns c1, syscolumns c2
  3273.         where    o1.id = f.pktable_id
  3274.             AND o2.id = f.fktable_id
  3275.             AND c1.id = f.pktable_id
  3276.             AND c2.id = f.fktable_id
  3277.             AND c1.colid = f.pkcolid
  3278.             AND c2.colid = f.fkcolid
  3279.  
  3280.     /* select count (*) as 'XXX countout' from #fkeysout */
  3281.  
  3282.     if @order_by_pk = 1 /*    If order by PK fields */
  3283.         select
  3284.             PKTABLE_QUALIFIER, PKTABLE_OWNER, PKTABLE_NAME, PKCOLUMN_NAME,
  3285.             FKTABLE_QUALIFIER, FKTABLE_OWNER, FKTABLE_NAME, FKCOLUMN_NAME,
  3286.             KEY_SEQ, UPDATE_RULE, DELETE_RULE, FK_NAME, PK_NAME, DEFERRABILITY
  3287.         from #fkeysout
  3288.         order by 1,2,3,9
  3289.     else        /*    Order by FK fields */
  3290.         select
  3291.             PKTABLE_QUALIFIER, PKTABLE_OWNER, PKTABLE_NAME, PKCOLUMN_NAME,
  3292.             FKTABLE_QUALIFIER, FKTABLE_OWNER, FKTABLE_NAME, FKCOLUMN_NAME,
  3293.             KEY_SEQ, UPDATE_RULE, DELETE_RULE, FK_NAME, PK_NAME, DEFERRABILITY
  3294.         from #fkeysout
  3295.         order by 5,6,7,9
  3296. go
  3297.  
  3298. grant execute on sp_fkeys to public
  3299. go
  3300.  
  3301. dump tran master with no_log
  3302. go
  3303.  
  3304. print 'creating sp_pkeys'
  3305. go
  3306.  
  3307. /*    Procedure for pre-6.0 server */
  3308. CREATE PROCEDURE sp_pkeys(
  3309.                @table_name        varchar(32),
  3310.                @table_owner     varchar(32) = null,
  3311.                @table_qualifier varchar(32) = null )
  3312. as
  3313.     if @table_qualifier is not null
  3314.     begin
  3315.         if db_name() <> @table_qualifier
  3316.         begin    /* If qualifier doesn't match current database */
  3317.             raiserror 20001 'Table qualifier must be name of current database'
  3318.             return
  3319.         end
  3320.     end
  3321.     if @table_owner is null /*    If owner not supplied, match all */
  3322.         select @table_owner = '%'
  3323.     if @@trancount <> 0
  3324.     begin    /* If inside a transaction */
  3325.         raiserror 20003 'The procedure ''sp_pkeys'' cannot be executed from within a transaction.'
  3326.         return
  3327.     end
  3328.  
  3329.     create table #pkeys(
  3330.              TABLE_QUALIFIER varchar(32) NULL,
  3331.              TABLE_OWNER     varchar(32) NULL,
  3332.              TABLE_NAME      varchar(32) NOT NULL,
  3333.              COLUMN_NAME     varchar(32) NOT NULL,
  3334.              KEY_SEQ         smallint NOT NULL)
  3335.  
  3336.     /*    SQL Server supports upto 8 PK/FK relationships between 2 tables */
  3337.     /*    Process syskeys for each relationship */
  3338.     /*    The inserts below adds a row to the temp table for each of the
  3339.         8 possible relationships */
  3340.     insert into #pkeys
  3341.         select
  3342.             db_name(),
  3343.             (select user_name(uid) from sysobjects o where o.id = k.id),
  3344.             object_name(k.id),
  3345.             c.name,
  3346.             1
  3347.         from
  3348.             syskeys k, syscolumns c
  3349.         where
  3350.             c.id = k.id
  3351.             and k.type = 1    /* Primary type key */
  3352.             and c.colid = k.key1
  3353.     if (@@rowcount = 0)
  3354.         goto done
  3355.  
  3356.     insert into #pkeys
  3357.         select
  3358.             db_name(),
  3359.             (select user_name(uid) from sysobjects o where o.id = k.id),
  3360.             object_name(k.id),
  3361.             c.name,
  3362.             2
  3363.         from
  3364.             syskeys k, syscolumns c
  3365.         where
  3366.             c.id = k.id
  3367.             and k.type = 1    /* Primary type key */
  3368.             and c.colid = key2
  3369.     if (@@rowcount = 0)
  3370.         goto done
  3371.  
  3372.     insert into #pkeys
  3373.         select
  3374.             db_name(),
  3375.             (select user_name(uid) from sysobjects o where o.id = k.id),
  3376.             object_name(k.id),
  3377.             c.name,
  3378.             3
  3379.         from
  3380.             syskeys k, syscolumns c
  3381.         where
  3382.             c.id = k.id
  3383.             and k.type = 1    /* Primary type key */
  3384.             and c.colid = key3
  3385.     if (@@rowcount = 0)
  3386.         goto done
  3387.  
  3388.     insert into #pkeys
  3389.         select
  3390.             db_name(),
  3391.             (select user_name(uid) from sysobjects o where o.id = k.id),
  3392.             object_name(k.id),
  3393.             c.name,
  3394.             4
  3395.         from
  3396.             syskeys k, syscolumns c
  3397.         where
  3398.             c.id = k.id
  3399.             and k.type = 1    /* Primary type key */
  3400.             and c.colid = key4
  3401.     if (@@rowcount = 0)
  3402.         goto done
  3403.  
  3404.     insert into #pkeys
  3405.         select
  3406.             db_name(),
  3407.             (select user_name(uid) from sysobjects o where o.id = k.id),
  3408.             object_name(k.id),
  3409.             c.name,
  3410.             5
  3411.         from
  3412.             syskeys k, syscolumns c
  3413.         where
  3414.             c.id = k.id
  3415.             and k.type = 1    /* Primary type key */
  3416.             and c.colid = key5
  3417.     if (@@rowcount = 0)
  3418.         goto done
  3419.  
  3420.     insert into #pkeys
  3421.         select
  3422.             db_name(),
  3423.             (select user_name(uid) from sysobjects o where o.id = k.id),
  3424.             object_name(k.id),
  3425.             c.name,
  3426.             6
  3427.         from
  3428.             syskeys k, syscolumns c
  3429.         where
  3430.             c.id = k.id
  3431.             and k.type = 1    /* Primary type key */
  3432.             and c.colid = key6
  3433.     if (@@rowcount = 0)
  3434.         goto done
  3435.  
  3436.     insert into #pkeys
  3437.         select
  3438.             db_name(),
  3439.             (select user_name(uid) from sysobjects o where o.id = k.id),
  3440.             object_name(k.id),
  3441.             c.name,
  3442.             7
  3443.         from
  3444.             syskeys k, syscolumns c
  3445.         where
  3446.             c.id = k.id
  3447.             and k.type = 1    /* Primary type key */
  3448.             and c.colid = key7
  3449.     if (@@rowcount = 0)
  3450.         goto done
  3451.  
  3452.     insert into #pkeys
  3453.          select
  3454.              db_name(),
  3455.              (select user_name(uid) from sysobjects o where o.id = k.id),
  3456.              object_name(k.id),
  3457.              c.name,
  3458.              8
  3459.          from
  3460.              syskeys k, syscolumns c
  3461.          where
  3462.              c.id = k.id
  3463.              and k.type = 1 /* Primary type key */
  3464.              and c.colid = key8
  3465.  
  3466.     done:
  3467.     select
  3468.         TABLE_QUALIFIER,
  3469.         TABLE_OWNER,
  3470.         TABLE_NAME,
  3471.         COLUMN_NAME,
  3472.         KEY_SEQ,
  3473.         PK_NAME = convert(varchar(32),null)
  3474.     from #pkeys
  3475.     where TABLE_NAME = @table_name
  3476.         and TABLE_OWNER like @table_owner
  3477.     order by 1, 2, 3, 5
  3478. go
  3479.  
  3480. if (charindex('6.00', @@version) = 0 and
  3481.     charindex('6.50', @@version) = 0 and
  3482.     charindex('7.00', @@version) = 0)
  3483. begin
  3484.     print ''
  3485.     print ''
  3486.     print 'Warning:'
  3487.     print 'you are installing the stored procedures '
  3488.     print 'on a pre 6.0 SQL Server.'
  3489.     print 'Ignore the following error.'
  3490. end
  3491. else
  3492.     drop proc sp_pkeys
  3493. go
  3494.  
  3495. /*    Procedure for 6.0 and later servers */
  3496. CREATE PROCEDURE sp_pkeys(
  3497.                @table_name        sysname,
  3498.                @table_owner     sysname = null,
  3499.                @table_qualifier sysname = null )
  3500. as
  3501.     DECLARE @table_id            int
  3502.     DECLARE @full_table_name    varchar(255) /* 2*128 + 1 */
  3503.  
  3504.     if @table_qualifier is not null
  3505.     begin
  3506.         if db_name() <> @table_qualifier
  3507.         begin    /* If qualifier doesn't match current database */
  3508.             raiserror (15250, -1,-1,'Table')
  3509.             return
  3510.         end
  3511.     end
  3512.     if @table_owner is null
  3513.     begin    /* If unqualified table name */
  3514.         SELECT @full_table_name = @table_name
  3515.     end
  3516.     else
  3517.     begin    /* Qualified table name */
  3518.         if @table_owner = ''
  3519.         begin    /* If empty owner name */
  3520.             SELECT @full_table_name = @table_owner
  3521.         end
  3522.         else
  3523.         begin
  3524.             SELECT @full_table_name = @table_owner + '.' + @table_name
  3525.         end
  3526.     end
  3527.     /*    Get Object ID */
  3528.     SELECT @table_id = object_id(@full_table_name)
  3529.  
  3530.     select
  3531.         TABLE_QUALIFIER = convert(sysname,db_name()),
  3532.         TABLE_OWNER = convert(sysname,user_name(o.uid)),
  3533.         TABLE_NAME = convert(sysname,o.name),
  3534.         COLUMN_NAME = convert(sysname,c.name),
  3535.         KEY_SEQ = convert(smallint,c1.colid),
  3536.         PK_NAME = convert(sysname,i.name)
  3537.     from
  3538.         sysindexes i, syscolumns c, sysobjects o, syscolumns c1
  3539.     where
  3540.         o.id = @table_id
  3541.         and o.id = c.id
  3542.         and o.id = i.id
  3543.         and (i.status & 0x800) = 0x800
  3544.         and c.name = index_col (@full_table_name, i.indid, c1.colid)
  3545.         and c1.colid <= i.keycnt    /* create rows from 1 to keycnt */
  3546.         and c1.id = @table_id
  3547.     order by 1, 2, 3, 5
  3548. go
  3549.  
  3550. grant execute on sp_pkeys to public
  3551. go
  3552.  
  3553. dump tran master with no_log
  3554. go
  3555.  
  3556. print 'creating sp_server_info'
  3557. go
  3558.  
  3559. create proc sp_server_info (
  3560.             @attribute_id  int = null)
  3561. as
  3562.     if @attribute_id is not null
  3563.         select *
  3564.         from master.dbo.spt_server_info
  3565.         where attribute_id = @attribute_id
  3566.     else
  3567.         select *
  3568.         from master.dbo.spt_server_info
  3569.         order by attribute_id
  3570. go
  3571.  
  3572. grant execute on sp_server_info to public
  3573. go
  3574.  
  3575. dump tran master with no_log
  3576. go
  3577.  
  3578. print 'creating sp_special_columns'
  3579. go
  3580.  
  3581. /*    Procedure for pre-6.0 server */
  3582. CREATE PROCEDURE sp_special_columns (
  3583.                  @table_name        varchar(32),
  3584.                  @table_owner        varchar(32) = null,
  3585.                  @table_qualifier    varchar(32) = null,
  3586.                  @col_type            char(1) = 'R',
  3587.                  @scope                char(1) = 'T',
  3588.                  @nullable            char(1) = 'U',
  3589.                  @ODBCVer            int = 2)
  3590. AS
  3591.     DECLARE @indid                int
  3592.     DECLARE @table_id            int
  3593.     DECLARE @full_table_name    varchar(65) /* 2*32+1 */
  3594.     DECLARE @scopeout            smallint
  3595.  
  3596.     if @col_type not in ('R','V')
  3597.     begin
  3598.         raiserror 20002 'Illegal ''col_type'' specified -- must be ''R'' or ''V''.'
  3599.         return
  3600.     end
  3601.  
  3602.     if @scope = 'C'
  3603.         select @scopeout = 0
  3604.     else if @scope = 'T'
  3605.         select @scopeout = 1
  3606.     else
  3607.     begin
  3608.         raiserror 20002 'Illegal ''scope'' specified -- must be ''C'' or ''T''.'
  3609.         return
  3610.     end
  3611.  
  3612.     if @nullable not in ('U','O')
  3613.     begin
  3614.         raiserror 20002 'Illegal ''nullable'' specified -- must be ''U'' or ''O''.'
  3615.         return
  3616.     end
  3617.  
  3618.     if @table_qualifier is not null
  3619.     begin
  3620.         if db_name() <> @table_qualifier
  3621.         begin /* If qualifier doesn't match current database */
  3622.             raiserror 20001 'Table qualifier must be name of current database'
  3623.             return
  3624.         end
  3625.     end
  3626.     if @table_owner is null
  3627.     begin     /* If unqualified table name */
  3628.         SELECT @full_table_name = @table_name
  3629.     end
  3630.     else
  3631.     begin    /* Qualified table name */
  3632.         SELECT @full_table_name = @table_owner + '.' + @table_name
  3633.     end
  3634.     /*    Get Object ID */
  3635.     SELECT @table_id = object_id(@full_table_name)
  3636.  
  3637.     if @col_type = 'V'
  3638.     BEGIN /* if ROWVER, just run that query */
  3639.         SELECT
  3640.             SCOPE = convert(smallint,NULL),
  3641.             COLUMN_NAME = convert(varchar(32),c.name),
  3642.             DATA_TYPE = convert(smallint, -3),
  3643.             TYPE_NAME = t.name,
  3644.             "PRECISION" = convert(int,8),
  3645.             LENGTH = convert(int,8),
  3646.             SCALE = convert(smallint, NULL),
  3647.             PSEUDO_COLUMN = convert(smallint,1)
  3648.         FROM
  3649.             systypes t, syscolumns c
  3650.         WHERE
  3651.             c.id = @table_id
  3652.             AND c.usertype = 80 /*    TIMESTAMP */
  3653.             AND t.usertype = 80 /*    TIMESTAMP */
  3654.         RETURN
  3655.     END
  3656.  
  3657.     /* ROWID, now find the id of the 'best' index for this table */
  3658.  
  3659.     IF @nullable = 'O'    /* Don't include any indexes that contain
  3660.                            nullable columns. */
  3661.  
  3662.             SELECT @indid = MIN(indid)
  3663.                 FROM sysindexes i,syscolumns c,syscolumns c2
  3664.                 WHERE
  3665.                     i.status&2 = 2        /*    If Unique Index */
  3666.                      AND c.id = i.id
  3667.                      AND c2.id = c.id
  3668.                      AND c2.colid < i.keycnt + (i.status&16)/16
  3669.                     AND i.id = @table_id
  3670.                     AND indid > 0        /*    Eliminate Table Row */
  3671.                     AND c.name = index_col(@table_name,i.indid,c2.colid)
  3672.                     GROUP BY indid HAVING SUM(c.status&8) = 0
  3673.  
  3674.     ELSE    /* Include indexes that are partially nullable. */
  3675.  
  3676.         SELECT @indid = MIN(indid)
  3677.             FROM sysindexes i
  3678.             WHERE
  3679.                 status&2 = 2        /*    If Unique Index */
  3680.                 AND id = @table_id
  3681.                 AND indid > 0        /*    Eliminate Table Row */
  3682.  
  3683.     SELECT
  3684.         SCOPE = @scopeout,
  3685.         COLUMN_NAME = convert(varchar(32),INDEX_COL(@full_table_name,indid,c2.colid)),
  3686.         d.DATA_TYPE,
  3687.         TYPE_NAME = t.name,
  3688.         "PRECISION" = isnull(d.data_precision, convert(int,c.length)),
  3689.         LENGTH = isnull(d.length, convert(int,c.length)),
  3690.         SCALE = d.numeric_scale,
  3691.         PSEUDO_COLUMN = convert(smallint,1)
  3692.     FROM
  3693.         sysindexes x,
  3694.         syscolumns c,
  3695.         master.dbo.spt_datatype_info d,
  3696.         systypes t,
  3697.         syscolumns c2    /* Self-join to generate list of index columns and */
  3698.                         /* to extract datatype names */
  3699.     WHERE
  3700.         x.id = @table_id
  3701.         AND c.name = INDEX_COL(@full_table_name,@indid,c2.colid)
  3702.         AND c.id = x.id
  3703.         AND c2.id = x.id
  3704.         AND c2.colid < keycnt+(x.status&16)/16
  3705.         AND x.indid = @indid
  3706.         AND t.type = d.ss_dtype
  3707.         AND c.length = d.fixlen
  3708.         AND c.usertype = t.usertype
  3709.  
  3710. go
  3711.  
  3712. if (charindex('6.00', @@version) = 0 and
  3713.     charindex('6.50', @@version) = 0 and
  3714.     charindex('7.00', @@version) = 0)
  3715. begin
  3716.     print ''
  3717.     print ''
  3718.     print 'Warning:'
  3719.     print 'you are installing the stored procedures '
  3720.     print 'on a pre 6.0 SQL Server.'
  3721.     print 'Ignore the following errors.'
  3722. end
  3723. else
  3724.     drop proc sp_special_columns
  3725. go
  3726.  
  3727. /*    Procedure for 6.0 and 6.50 servers */
  3728. CREATE PROCEDURE sp_special_columns (
  3729.                  @table_name        varchar(32),
  3730.                  @table_owner        varchar(32) = null,
  3731.                  @table_qualifier    varchar(32) = null,
  3732.                  @col_type            char(1) = 'R',
  3733.                  @scope                char(1) = 'T',
  3734.                  @nullable            char(1) = 'U',
  3735.                  @ODBCVer            int = 2)
  3736. AS
  3737.     DECLARE @indid                int
  3738.     DECLARE @table_id            int
  3739.     DECLARE @full_table_name    varchar(65) /* 2*32+1 */
  3740.     DECLARE @scopeout            smallint
  3741.  
  3742.     if @col_type not in ('R','V')
  3743.     begin
  3744.         raiserror (15251,-1,-1,'col_type','''R'' or ''V''')
  3745.         return
  3746.     end
  3747.  
  3748.     if @scope = 'C'
  3749.         select @scopeout = 0
  3750.     else if @scope = 'T'
  3751.         select @scopeout = 1
  3752.     else
  3753.     begin
  3754.         raiserror (15251,-1,-1,'scope','''C'' or ''T''')
  3755.         return
  3756.     end
  3757.  
  3758.     if @nullable not in ('U','O')
  3759.     begin
  3760.         raiserror (15251,-1,-1,'nullable','''U'' or ''O''')
  3761.         return
  3762.     end
  3763.  
  3764.     if @table_qualifier is not null
  3765.     begin
  3766.         if db_name() <> @table_qualifier
  3767.         begin /* If qualifier doesn't match current database */
  3768.             raiserror (15250, -1,-1,'Table')
  3769.             return
  3770.         end
  3771.     end
  3772.     if @table_owner is null
  3773.     begin     /* If unqualified table name */
  3774.         SELECT @full_table_name = @table_name
  3775.     end
  3776.     else
  3777.     begin     /* Qualified table name */
  3778.         SELECT @full_table_name = @table_owner + '.' + @table_name
  3779.     end
  3780.     /*    Get Object ID */
  3781.     SELECT @table_id = object_id(@full_table_name)
  3782.  
  3783.     if @col_type = 'V'
  3784.     BEGIN /* if ROWVER, just run that query */
  3785.         SELECT
  3786.             SCOPE = convert(smallint,NULL),
  3787.             COLUMN_NAME = convert(varchar(32),c.name),
  3788.             DATA_TYPE = convert(smallint, -2),
  3789.             TYPE_NAME = t.name,
  3790.             "PRECISION" = convert(int,8),
  3791.             LENGTH = convert(int,8),
  3792.             SCALE = convert(smallint, NULL),
  3793.             PSEUDO_COLUMN = convert(smallint,1)
  3794.         FROM
  3795.             systypes t, syscolumns c
  3796.         WHERE
  3797.             c.id = @table_id
  3798.             AND c.usertype = 80 /*    TIMESTAMP */
  3799.             AND t.usertype = 80 /*    TIMESTAMP */
  3800.         RETURN
  3801.     END
  3802.  
  3803.     /* ROWID, now find the id of the 'best' index for this table */
  3804.  
  3805.     IF @nullable = 'O'    /* Don't include any indexes that contain
  3806.                            nullable columns. */
  3807.  
  3808.         SELECT @indid = MIN(indid)
  3809.             FROM sysindexes i,syscolumns c,syscolumns c2
  3810.             WHERE
  3811.                 i.status&2 = 2        /*    If Unique Index */
  3812.                 AND c.id = i.id
  3813.                 AND c2.id = c.id
  3814.                 AND c2.colid < i.keycnt + (i.status&16)/16
  3815.                 AND i.id = @table_id
  3816.                 AND indid > 0        /*    Eliminate Table Row */
  3817.                 AND c.name = index_col(@table_name,i.indid,c2.colid)
  3818.                 GROUP BY indid HAVING SUM(c.status&8) = 0
  3819.  
  3820.     ELSE    /* Include indexes that are partially nullable. */
  3821.  
  3822.         SELECT @indid = MIN(indid)
  3823.             FROM sysindexes i
  3824.             WHERE
  3825.                 status&2 = 2        /*    If Unique Index */
  3826.                 AND id = @table_id
  3827.                 AND indid > 0        /*    Eliminate Table Row */
  3828.  
  3829.     SELECT
  3830.         SCOPE = @scopeout,
  3831.         COLUMN_NAME = convert(varchar(32),INDEX_COL(@full_table_name,indid,c2.colid)),
  3832.         d.DATA_TYPE,
  3833.         convert(varchar(32),case
  3834.             when (t.usertype > 100 or t.usertype in (18,80))
  3835.                 then t.name
  3836.             else d.TYPE_NAME
  3837.         end) TYPE_NAME,
  3838.         convert(int,case
  3839.             when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  3840.             else isnull(convert(int,c.prec), 2147483647)
  3841.         end) "PRECISION",
  3842.         convert(int,case
  3843.             when d.ss_dtype IN (106, 108, 55, 63) then    /* decimal/numeric types */
  3844.                 convert(int,c.prec+2)
  3845.             else
  3846.                 isnull(d.length, c.length)
  3847.         end) LENGTH,
  3848.         SCALE = convert(smallint, c.scale),
  3849.         PSEUDO_COLUMN = convert(smallint,1)
  3850.     FROM
  3851.         sysindexes x,
  3852.         syscolumns c,
  3853.         master.dbo.spt_datatype_info d,
  3854.         systypes t,
  3855.         syscolumns c2    /* Self-join to generate list of index columns and */
  3856.                         /* to extract datatype names */
  3857.     WHERE
  3858.         x.id = @table_id
  3859.         AND c.name = INDEX_COL(@full_table_name,@indid,c2.colid)
  3860.         AND c.id = x.id
  3861.         AND c2.id = x.id
  3862.         AND c2.colid < x.keycnt+(x.status&16)/16
  3863.         AND x.indid = @indid
  3864.         AND t.type = d.ss_dtype
  3865.         AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  3866.         AND isnull(d.AUTO_INCREMENT,0) = (c.status&128)/128
  3867.         AND c.usertype = t.usertype
  3868. go
  3869.  
  3870. if (charindex('7.00', @@version) = 0)
  3871. begin
  3872.     print ''
  3873.     print ''
  3874.     print 'Warning:'
  3875.     print 'you are installing the stored procedures '
  3876.     print 'on a pre 7.0 SQL Server.'
  3877.     print 'Ignore the following errors.'
  3878. end
  3879. else
  3880.     drop proc sp_special_columns
  3881. go
  3882.  
  3883. /*    Procedure for 7.0 and later servers */
  3884. CREATE PROCEDURE sp_special_columns (
  3885.                  @table_name        sysname,
  3886.                  @table_owner        sysname = null,
  3887.                  @table_qualifier    sysname = null,
  3888.                  @col_type            char(1) = 'R',
  3889.                  @scope                char(1) = 'T',
  3890.                  @nullable            char(1) = 'U',
  3891.                  @ODBCVer            int = 2)
  3892. AS
  3893.     DECLARE @indid                int
  3894.     DECLARE @table_id            int
  3895.     DECLARE @full_table_name    varchar(257)
  3896.     DECLARE @scopeout            smallint
  3897.  
  3898.     if @col_type not in ('R','V')
  3899.     begin
  3900.         raiserror (15251,-1,-1,'col_type','''R'' or ''V''')
  3901.         return
  3902.     end
  3903.  
  3904.     if @scope = 'C'
  3905.         select @scopeout = 0
  3906.     else if @scope = 'T'
  3907.         select @scopeout = 1
  3908.     else
  3909.     begin
  3910.         raiserror (15251,-1,-1,'scope','''C'' or ''T''')
  3911.         return
  3912.     end
  3913.  
  3914.     if @nullable not in ('U','O')
  3915.     begin
  3916.         raiserror (15251,-1,-1,'nullable','''U'' or ''O''')
  3917.         return
  3918.     end
  3919.  
  3920.     if @table_qualifier is not null
  3921.     begin
  3922.         if db_name() <> @table_qualifier
  3923.         begin /* If qualifier doesn't match current database */
  3924.             raiserror (15250, -1,-1,'Table')
  3925.             return
  3926.         end
  3927.     end
  3928.     if @table_owner is null
  3929.     begin     /* If unqualified table name */
  3930.         SELECT @full_table_name = @table_name
  3931.     end
  3932.     else
  3933.     begin    /* Qualified table name */
  3934.         if @table_owner = ''
  3935.         begin    /* If empty owner name */
  3936.             SELECT @full_table_name = @table_owner
  3937.         end
  3938.         else
  3939.         begin
  3940.             SELECT @full_table_name = @table_owner + '.' + @table_name
  3941.         end
  3942.     end
  3943.     /*    Get Object ID */
  3944.     SELECT @table_id = object_id(@full_table_name)
  3945.  
  3946.     if @col_type = 'V'
  3947.     BEGIN /* if ROWVER, just run that query */
  3948.         SELECT
  3949.             SCOPE = convert(smallint,NULL),
  3950.             COLUMN_NAME = convert(sysname,c.name),
  3951.             DATA_TYPE = convert(smallint, -2),
  3952.             TYPE_NAME = t.name,
  3953.             "PRECISION" = convert(int,8),
  3954.             LENGTH = convert(int,8),
  3955.             SCALE = convert(smallint, NULL),
  3956.             PSEUDO_COLUMN = convert(smallint,1)
  3957.         FROM
  3958.             systypes t, syscolumns c
  3959.         WHERE
  3960.             c.id = @table_id
  3961.             AND t.name = 'timestamp'    /*    TIMESTAMP  */
  3962.             AND t.xtype = c.xtype        
  3963.             AND t.xusertype = c.xusertype    
  3964.         RETURN
  3965.     END
  3966.  
  3967.     /* ROWID, now find the id of the 'best' index for this table */
  3968.  
  3969.     IF @nullable = 'O'    /* Don't include any indexes that contain
  3970.                            nullable columns. */
  3971.  
  3972.         SELECT @indid = MIN(indid)
  3973.             FROM sysindexes x, syscolumns c, syscolumns c2
  3974.             WHERE
  3975.                 x.status&2 = 2        /*    If Unique Index */
  3976.                 AND c.id = x.id
  3977.                 AND c2.id = c.id
  3978.                 AND c2.colid < x.keycnt + (x.status&16)/16
  3979.                 AND x.id = @table_id
  3980.                 AND indid > 0        /*    Eliminate Table Row */
  3981.                 AND c.name = index_col(@table_name,x.indid,c2.colid)
  3982.                 GROUP BY indid HAVING SUM(c.status&8) = 0
  3983.  
  3984.     ELSE    /* Include indexes that are partially nullable. */
  3985.  
  3986.         SELECT @indid = MIN(indid)
  3987.             FROM sysindexes x
  3988.             WHERE
  3989.                 status&2 = 2        /*    If Unique Index */
  3990.                 AND id = @table_id
  3991.                 AND indid > 0        /*    Eliminate Table Row */
  3992.  
  3993.     SELECT
  3994.         SCOPE = @scopeout,
  3995.         COLUMN_NAME = convert(sysname,INDEX_COL(@full_table_name,indid,c2.colid)),
  3996.         d.DATA_TYPE,
  3997.         convert(sysname,case
  3998.             when t.xusertype > 255 then t.name
  3999.             else d.TYPE_NAME
  4000.         end) TYPE_NAME,
  4001.         convert(int,case
  4002.             when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  4003.             else ColumnProperty (c.id, c.name, 'Precision')
  4004.         end) "PRECISION",
  4005.         convert(int,case
  4006.             when d.ss_dtype IN (139, 140) then    /* decimal/numeric types */
  4007.                 ColumnProperty (c.id, c.name, 'Precision')+2
  4008.             else isnull(d.length, c.length)
  4009.         end) LENGTH,
  4010.         SCALE = convert(smallint, ColumnProperty (c.id, c.name, 'Scale')),
  4011.         PSEUDO_COLUMN = convert(smallint,1)
  4012.     FROM
  4013.         sysindexes x,
  4014.         syscolumns c,
  4015.         master.dbo.spt_datatype_info d,
  4016.         systypes t,
  4017.         syscolumns c2    /* Self-join to generate list of index columns and */
  4018.                         /* to extract datatype names */
  4019.     WHERE
  4020.         x.id = @table_id
  4021.         AND c.name = INDEX_COL(@full_table_name,@indid,c2.colid)
  4022.         AND c.id = x.id
  4023.         AND c2.id = x.id
  4024.         AND c2.colid < x.keycnt + (x.status&16)/16
  4025.         AND x.indid = @indid
  4026.         AND t.xtype = d.ss_dtype
  4027.         AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  4028.         AND isnull(d.AUTO_INCREMENT,0) = isnull(ColumnProperty (c.id, c.name, 'IsIdentity'),0)
  4029.         AND c.xusertype = t.xusertype
  4030. go
  4031.  
  4032. grant execute on sp_special_columns to public
  4033. go
  4034.  
  4035. dump tran master with no_log
  4036. go
  4037.  
  4038. print 'creating sp_sproc_columns'
  4039. go
  4040.  
  4041. /*    Procedure for pre-6.0 server */
  4042. CREATE PROCEDURE sp_sproc_columns (
  4043.                  @procedure_name        varchar(102) = '%', /* 3*32+5+1 */
  4044.                  @procedure_owner        varchar(96) = null,
  4045.                  @procedure_qualifier    varchar(32) = null,
  4046.                  @column_name            varchar(96) = null,
  4047.                  @ODBCVer                int = 2)
  4048. AS
  4049.     DECLARE @group_num int
  4050.     DECLARE @semi_position int
  4051.     DECLARE @full_procedure_name    varchar(205)/* 2*102+1 */
  4052.     DECLARE @procedure_id int
  4053.  
  4054.     if @column_name is null /*    If column name not supplied, match all */
  4055.         select @column_name = '%'
  4056.     if @procedure_qualifier is not null
  4057.     begin
  4058.         if db_name() <> @procedure_qualifier
  4059.         begin
  4060.             if @procedure_qualifier = ''
  4061.             begin
  4062.                 /* in this case, we need to return an empty result set */
  4063.                 /* because the user has requested a database with an empty name */
  4064.                 select @procedure_name = ''
  4065.                 select @procedure_owner = ''
  4066.             end
  4067.             else
  4068.             begin    /* If qualifier doesn't match current database */
  4069.                 raiserror 20001 'Procedure qualifier must be name of current database'
  4070.                 return
  4071.             end
  4072.         end
  4073.     end
  4074.  
  4075.     if @procedure_name is null
  4076.     begin    /*    If procedure name not supplied, match all */
  4077.         select @procedure_name = '%'
  4078.     end
  4079.  
  4080.     /* first we need to extract the procedure group number, if one exists */
  4081.     select @semi_position = charindex(';',@procedure_name)
  4082.     if (@semi_position > 0)
  4083.     begin    /* If group number separator (;) found */
  4084.         select @group_num = convert(int,substring(@procedure_name, @semi_position + 1, 2))
  4085.         select @procedure_name = substring(@procedure_name, 1, @semi_position -1)
  4086.     end
  4087.     else
  4088.     begin    /* No group separator, so default to group number of 1 */
  4089.         select @group_num = 1
  4090.     end
  4091.  
  4092.     if @procedure_owner is null
  4093.     begin    /* If unqualified procedure name */
  4094.         SELECT @full_procedure_name = @procedure_name
  4095.     end
  4096.     else
  4097.     begin    /* Qualified procedure name */
  4098.         SELECT @full_procedure_name = @procedure_owner + '.' + @procedure_name
  4099.     end
  4100.  
  4101.     /*    Get Object ID */
  4102.     SELECT @procedure_id = object_id(@full_procedure_name)
  4103.     if ((charindex('%',@full_procedure_name) = 0) and
  4104.         (charindex('_',@full_procedure_name) = 0)  and
  4105.         @procedure_id <> 0)
  4106.     begin
  4107.         /* this block is for the case where there is no pattern
  4108.             matching required for the procedure name */
  4109.         SELECT
  4110.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  4111.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  4112.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ ltrim(str(c.number,5))),
  4113.             COLUMN_NAME = convert(varchar(32),c.name),
  4114.             COLUMN_TYPE = convert(smallint, 0),
  4115.             d.DATA_TYPE,
  4116.             TYPE_NAME = t.name,
  4117.             "PRECISION" = isnull(d.data_precision, convert(int,c.length)),
  4118.             LENGTH = isnull(d.length, convert(int,c.length)),
  4119.             SCALE = d.numeric_scale,
  4120.             d.RADIX,
  4121.             d.NULLABLE,
  4122.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  4123.             COLUMN_DEF = convert(varchar(254),null),
  4124.             d.SQL_DATA_TYPE,
  4125.             d.SQL_DATETIME_SUB,
  4126.             CHAR_OCTET_LENGTH = isnull(d.data_precision, convert(int,c.length))+d.charbin,
  4127.             ORDINAL_POSITION = convert(int,c.colid),
  4128.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  4129.             SS_DATA_TYPE = c.type
  4130.         FROM
  4131.             syscolumns c,
  4132.             sysobjects o,
  4133.             master.dbo.spt_datatype_info d,
  4134.             systypes t
  4135.         WHERE
  4136.             o.id = @procedure_id
  4137.             AND c.id = o.id
  4138.             AND t.type = d.ss_dtype
  4139.             AND c.length = isnull(d.fixlen, c.length)
  4140.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  4141.             AND c.usertype = t.usertype
  4142.             AND c.name like @column_name
  4143.             AND c.number = @group_num
  4144.         UNION ALL
  4145.         SELECT           /* return value row*/
  4146.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  4147.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  4148.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ isnull(ltrim(str(c.number,5)),'1')),
  4149.             COLUMN_NAME = convert(varchar(32),'RETURN_VALUE'),
  4150.             COLUMN_TYPE = convert(smallint, 5),
  4151.             DATA_TYPE = convert(smallint, 4),
  4152.             TYPE_NAME = convert(varchar(32),'int'),
  4153.             "PRECISION" = convert(int,10),
  4154.             LENGTH = convert(int,4),
  4155.             SCALE = convert(smallint,0),
  4156.             RADIX = convert(smallint,10),
  4157.             NULLABLE = convert(smallint,0),
  4158.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  4159.             COLUMN_DEF = convert(varchar(254),NULL),
  4160.             SQL_DATA_TYPE = convert(smallint, 4),
  4161.             SQL_DATETIME_SUB = convert(smallint,null),
  4162.             CHAR_OCTET_LENGTH = convert(int,null),
  4163.             ORDINAL_POSITION = convert(int,0),
  4164.             IS_NULLABLE = convert(varchar(254),'NO'),
  4165.             SS_DATA_TYPE = convert(tinyint,56)
  4166.         FROM
  4167.             syscolumns c,
  4168.             sysobjects o
  4169.         WHERE
  4170.             o.id = @procedure_id
  4171.             AND c.id =* o.id
  4172.             AND c.colid = 1
  4173.             AND o.type = 'P'                        /* Just Procedures */
  4174.             AND 'RETURN_VALUE' like @column_name
  4175.             AND c.number = @group_num
  4176.         ORDER BY 1, 2, 3, 18
  4177.     end
  4178.     else
  4179.     begin
  4180.         /* this block is for the case where there IS pattern
  4181.             matching done on the procedure name */
  4182.         if @procedure_owner is null
  4183.             select @procedure_owner = '%'
  4184.         SELECT
  4185.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  4186.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  4187.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ ltrim(str(c.number,5))),
  4188.             COLUMN_NAME = convert(varchar(32),c.name),
  4189.             COLUMN_TYPE = convert(smallint, 0),
  4190.             d.DATA_TYPE,
  4191.             TYPE_NAME = t.name,
  4192.             "PRECISION" = isnull(d.data_precision, convert(int,c.length)),
  4193.             LENGTH = isnull(d.length, convert(int,c.length)),
  4194.             SCALE = d.numeric_scale,
  4195.             d.RADIX,
  4196.             d.NULLABLE,
  4197.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  4198.             COLUMN_DEF = convert(varchar(254),null),
  4199.             d.SQL_DATA_TYPE,
  4200.             d.SQL_DATETIME_SUB,
  4201.             CHAR_OCTET_LENGTH = isnull(d.data_precision, convert(int,c.length))+d.charbin,
  4202.             ORDINAL_POSITION = convert(int,c.colid),
  4203.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  4204.             SS_DATA_TYPE = c.type
  4205.         FROM
  4206.             syscolumns c,
  4207.             sysobjects o,
  4208.             master.dbo.spt_datatype_info d,
  4209.             systypes t
  4210.         WHERE
  4211.             o.name like @procedure_name
  4212.             AND user_name(o.uid) like @procedure_owner
  4213.             AND o.id = c.id
  4214.             AND t.type = d.ss_dtype
  4215.             AND c.length = isnull(d.fixlen, c.length)
  4216.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  4217.             AND c.usertype = t.usertype
  4218.             AND o.type = 'P'                            /* Just Procedures */
  4219.             AND c.name like @column_name
  4220.         UNION ALL
  4221.         SELECT           /* return value row*/
  4222.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  4223.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  4224.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ isnull(ltrim(str(c.number,5)),'1')),
  4225.             COLUMN_NAME = convert(varchar(32),'RETURN_VALUE'),
  4226.             COLUMN_TYPE = convert(smallint, 5),
  4227.             DATA_TYPE = convert(smallint, 4),
  4228.             TYPE_NAME = convert(varchar(32),'int'),
  4229.             "PRECISION" = convert(int,10),
  4230.             LENGTH = convert(int,4),
  4231.             SCALE = convert(smallint,0),
  4232.             RADIX = convert(smallint,10),
  4233.             NULLABLE = convert(smallint,0),
  4234.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  4235.             COLUMN_DEF = convert(varchar(254),NULL),
  4236.             SQL_DATA_TYPE = convert(smallint, 4),
  4237.             SQL_DATETIME_SUB = convert(smallint,null),
  4238.             CHAR_OCTET_LENGTH = convert(int,null),
  4239.             ORDINAL_POSITION = convert(int,0),
  4240.             IS_NULLABLE = convert(varchar(254),'NO'),
  4241.             SS_DATA_TYPE = convert(tinyint,56)
  4242.         FROM
  4243.             syscolumns c,
  4244.             sysobjects o
  4245.         WHERE
  4246.             o.name like @procedure_name
  4247.             AND user_name(o.uid) like @procedure_owner
  4248.             AND c.id =* o.id
  4249.             AND c.colid = 1
  4250.             AND o.type = 'P'                        /* Just Procedures */
  4251.             AND 'RETURN_VALUE' like @column_name
  4252.         ORDER BY 1, 2, 3, 18
  4253.     end
  4254. go
  4255.  
  4256. if (charindex('6.00', @@version) = 0 and
  4257.     charindex('6.50', @@version) = 0 and
  4258.     charindex('7.00', @@version) = 0)
  4259. begin
  4260.     print ''
  4261.     print ''
  4262.     print 'Warning:'
  4263.     print 'you are installing the stored procedures '
  4264.     print 'on a pre 6.0 SQL Server.'
  4265.     print 'Ignore the following error.'
  4266. end
  4267. else
  4268.     drop proc sp_sproc_columns
  4269. go
  4270.  
  4271. /*    Procedure for 6.0 and 6.50 servers */
  4272. CREATE PROCEDURE sp_sproc_columns (
  4273.                  @procedure_name        varchar(102) = '%', /* 3*36+5+1 */
  4274.                  @procedure_owner        varchar(96) = null,
  4275.                  @procedure_qualifier    varchar(32) = null,
  4276.                  @column_name            varchar(96) = null,
  4277.                  @ODBCVer                int = 2)
  4278. AS
  4279.     DECLARE @group_num int
  4280.     DECLARE @semi_position int
  4281.     DECLARE @full_procedure_name    varchar(205)
  4282.     DECLARE @procedure_id int
  4283.  
  4284.     if @column_name is null /*    If column name not supplied, match all */
  4285.         select @column_name = '%'
  4286.     if @procedure_qualifier is not null
  4287.     begin
  4288.         if db_name() <> @procedure_qualifier
  4289.         begin
  4290.             if @procedure_qualifier = ''
  4291.             begin
  4292.                 /* in this case, we need to return an empty result set */
  4293.                 /* because the user has requested a database with an empty name */
  4294.                 select @procedure_name = ''
  4295.                 select @procedure_owner = ''
  4296.             end
  4297.             else
  4298.             begin    /* If qualifier doesn't match current database */
  4299.                 raiserror (15250, -1,-1,'Procedure')
  4300.                 return
  4301.             end
  4302.         end
  4303.     end
  4304.  
  4305.     if @procedure_name is null
  4306.     begin    /*    If procedure name not supplied, match all */
  4307.         select @procedure_name = '%'
  4308.     end
  4309.  
  4310.     /* first we need to extract the procedure group number, if one exists */
  4311.     select @semi_position = charindex(';',@procedure_name)
  4312.     if (@semi_position > 0)
  4313.     begin    /* If group number separator (;) found */
  4314.         select @group_num = convert(int,substring(@procedure_name, @semi_position + 1, 2))
  4315.         select @procedure_name = substring(@procedure_name, 1, @semi_position -1)
  4316.     end
  4317.     else
  4318.     begin    /* No group separator, so default to group number of 1 */
  4319.         select @group_num = 1
  4320.     end
  4321.  
  4322.     if @procedure_owner is null
  4323.     begin    /* If unqualified procedure name */
  4324.         SELECT @full_procedure_name = @procedure_name
  4325.     end
  4326.     else
  4327.     begin    /* Qualified procedure name */
  4328.         SELECT @full_procedure_name = @procedure_owner + '.' + @procedure_name
  4329.     end
  4330.  
  4331.     /*    Get Object ID */
  4332.     SELECT @procedure_id = object_id(@full_procedure_name)
  4333.     if ((charindex('%',@full_procedure_name) = 0) and
  4334.         (charindex('_',@full_procedure_name) = 0)  and
  4335.         @procedure_id <> 0)
  4336.     begin
  4337.         /* this block is for the case where there is no pattern
  4338.             matching required for the procedure name */
  4339.         SELECT
  4340.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  4341.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  4342.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ ltrim(str(c.number,5))),
  4343.             COLUMN_NAME = convert(varchar(32),c.name),
  4344.             COLUMN_TYPE = convert(smallint, 1+((c.status/64)&1)),
  4345.             d.DATA_TYPE,
  4346.             TYPE_NAME = t.name,
  4347.             convert(int,case
  4348.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  4349.                 else isnull(convert(int,c.prec), 2147483647)
  4350.             end) "PRECISION",
  4351.             convert(int,case
  4352.                 when d.ss_dtype IN (106, 108, 55, 63) then    /* decimal/numeric types */
  4353.                     c.prec+2
  4354.                 else
  4355.                     isnull(d.length, c.length)
  4356.             end) LENGTH,
  4357.             SCALE = convert(smallint, c.scale),
  4358.             d.RADIX,
  4359.             d.NULLABLE,
  4360.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  4361.             COLUMN_DEF = convert(varchar(254),NULL),
  4362.             d.SQL_DATA_TYPE,
  4363.             d.SQL_DATETIME_SUB,
  4364.             CHAR_OCTET_LENGTH = isnull(convert(int,c.prec), 2147483647)+d.charbin,
  4365.             ORDINAL_POSITION = convert(int,c.colid),
  4366.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  4367.             SS_DATA_TYPE = c.type
  4368.         FROM
  4369.             syscolumns c,
  4370.             sysobjects o,
  4371.             master.dbo.spt_datatype_info d,
  4372.             systypes t
  4373.         WHERE
  4374.             o.id = @procedure_id
  4375.             AND c.id = o.id
  4376.             AND c.type = d.ss_dtype
  4377.             AND c.length = isnull(d.fixlen, c.length)
  4378.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  4379.             AND isnull(d.AUTO_INCREMENT,0) = 0
  4380.             AND c.usertype = t.usertype
  4381.             AND c.name like @column_name
  4382.             AND c.number = @group_num
  4383.         UNION ALL
  4384.         SELECT           /* return value row*/
  4385.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  4386.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  4387.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ isnull(ltrim(str(c.number,5)),'1')),
  4388.             COLUMN_NAME = convert(varchar(32),'RETURN_VALUE'),
  4389.             COLUMN_TYPE = convert(smallint, 5),
  4390.             DATA_TYPE = convert(smallint, 4),
  4391.             TYPE_NAME = convert(varchar(32),'int'),
  4392.             "PRECISION" = convert(int,10),
  4393.             LENGTH = convert(int,4),
  4394.             SCALE = convert(smallint,0),
  4395.             RADIX = convert(smallint,10),
  4396.             NULLABLE = convert(smallint,0),
  4397.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  4398.             COLUMN_DEF = convert(varchar(254),NULL),
  4399.             SQL_DATA_TYPE = convert(smallint, 4),
  4400.             SQL_DATETIME_SUB = convert(smallint,null),
  4401.             CHAR_OCTET_LENGTH = convert(int,null),
  4402.             ORDINAL_POSITION = convert(int,0),
  4403.             IS_NULLABLE = convert(varchar(254),'NO'),
  4404.             SS_DATA_TYPE = convert(tinyint,56)
  4405.         FROM
  4406.             syscolumns c,
  4407.             sysobjects o
  4408.         WHERE
  4409.             o.id = @procedure_id
  4410.             AND c.id =* o.id
  4411.             AND c.colid = 1
  4412.             AND o.type = 'P'                        /* Just Procedures */
  4413.             AND 'RETURN_VALUE' like @column_name
  4414.             AND c.number = @group_num
  4415.         ORDER BY 1, 2, 3, 18
  4416.     end
  4417.     else
  4418.     begin
  4419.         /* this block is for the case where there IS pattern
  4420.             matching done on the procedure name */
  4421.         if @procedure_owner is null
  4422.             select @procedure_owner = '%'
  4423.         SELECT
  4424.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  4425.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  4426.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ ltrim(str(c.number,5))),
  4427.             COLUMN_NAME = convert(varchar(32),c.name),
  4428.             COLUMN_TYPE = convert(smallint, 1+((c.status/64)&1)),
  4429.             d.DATA_TYPE,
  4430.             TYPE_NAME = t.name,
  4431.             convert(int,case
  4432.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  4433.                 else isnull(convert(int,c.prec), 2147483647)
  4434.             end) "PRECISION",
  4435.             convert(int,case
  4436.                 when d.ss_dtype IN (106, 108, 55, 63) then    /* decimal/numeric types */
  4437.                     c.prec+2
  4438.                 else
  4439.                     isnull(d.length, c.length)
  4440.             end) LENGTH,
  4441.             SCALE = convert(smallint, c.scale),
  4442.             d.RADIX,
  4443.             d.NULLABLE,
  4444.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  4445.             COLUMN_DEF = convert(varchar(254),NULL),
  4446.             d.SQL_DATA_TYPE,
  4447.             d.SQL_DATETIME_SUB,
  4448.             CHAR_OCTET_LENGTH = isnull(convert(int,c.prec), 2147483647)+d.charbin,
  4449.             ORDINAL_POSITION = convert(int,c.colid),
  4450.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  4451.             SS_DATA_TYPE = c.type
  4452.         FROM
  4453.             syscolumns c,
  4454.             sysobjects o,
  4455.             master.dbo.spt_datatype_info d,
  4456.             systypes t
  4457.         WHERE
  4458.             o.name like @procedure_name
  4459.             AND user_name(o.uid) like @procedure_owner
  4460.             AND o.id = c.id
  4461.             AND c.type = d.ss_dtype
  4462.             AND c.length = isnull(d.fixlen, c.length)
  4463.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  4464.             AND isnull(d.AUTO_INCREMENT,0) = 0
  4465.             AND c.usertype = t.usertype
  4466.             AND o.type = 'P'                            /* Just Procedures */
  4467.             AND c.name like @column_name
  4468.         UNION ALL
  4469.         SELECT           /* return value row*/
  4470.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  4471.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  4472.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ isnull(ltrim(str(c.number,5)),'1')),
  4473.             COLUMN_NAME = convert(varchar(32),'RETURN_VALUE'),
  4474.             COLUMN_TYPE = convert(smallint, 5),
  4475.             DATA_TYPE = convert(smallint, 4),
  4476.             TYPE_NAME = convert(varchar(32),'int'),
  4477.             "PRECISION" = convert(int,10),
  4478.             LENGTH = convert(int,4),
  4479.             SCALE = convert(smallint,0),
  4480.             RADIX = convert(smallint,10),
  4481.             NULLABLE = convert(smallint,0),
  4482.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  4483.             COLUMN_DEF = convert(varchar(254),NULL),
  4484.             SQL_DATA_TYPE = convert(smallint, 4),
  4485.             SQL_DATETIME_SUB = convert(smallint,null),
  4486.             CHAR_OCTET_LENGTH = convert(int,null),
  4487.             ORDINAL_POSITION = convert(int,0),
  4488.             IS_NULLABLE = convert(varchar(254),'NO'),
  4489.             SS_DATA_TYPE = convert(tinyint,56)
  4490.         FROM
  4491.             syscolumns c,
  4492.             sysobjects o
  4493.         WHERE
  4494.             o.name like @procedure_name
  4495.             AND user_name(o.uid) like @procedure_owner
  4496.             AND c.id =* o.id
  4497.             AND c.colid = 1
  4498.             AND o.type = 'P'                        /* Just Procedures */
  4499.             AND 'RETURN_VALUE' like @column_name
  4500.         ORDER BY 1, 2, 3, 18
  4501.     end
  4502. go
  4503.  
  4504. if (charindex('7.00', @@version) = 0)
  4505. begin
  4506.     print ''
  4507.     print ''
  4508.     print 'Warning:'
  4509.     print 'you are installing the stored procedures '
  4510.     print 'on a pre 7.0 SQL Server.'
  4511.     print 'Ignore the following errors.'
  4512. end
  4513. else
  4514.     drop proc sp_sproc_columns
  4515. go
  4516.  
  4517. /*    Procedure for 7.0 server */
  4518. CREATE PROCEDURE sp_sproc_columns (
  4519.                  @procedure_name        varchar(390) = '%',
  4520.                  @procedure_owner        varchar(384) = null,
  4521.                  @procedure_qualifier    sysname = null,
  4522.                  @column_name            varchar(384) = null,
  4523.                  @ODBCVer                int = 2)
  4524. AS
  4525.     DECLARE @group_num int
  4526.     DECLARE @semi_position int
  4527.     DECLARE @full_procedure_name    varchar(774)
  4528.     DECLARE @procedure_id int
  4529.  
  4530.     if @column_name is null /*    If column name not supplied, match all */
  4531.         select @column_name = '%'
  4532.     if @procedure_qualifier is not null
  4533.     begin
  4534.         if db_name() <> @procedure_qualifier
  4535.         begin
  4536.             if @procedure_qualifier = ''
  4537.             begin
  4538.                 /* in this case, we need to return an empty result set */
  4539.                 /* because the user has requested a database with an empty name */
  4540.                 select @procedure_name = ''
  4541.                 select @procedure_owner = ''
  4542.             end
  4543.             else
  4544.             begin    /* If qualifier doesn't match current database */
  4545.                 raiserror (15250, -1,-1,'Procedure')
  4546.                 return
  4547.             end
  4548.         end
  4549.     end
  4550.  
  4551.     if @procedure_name is null
  4552.     begin    /*    If procedure name not supplied, match all */
  4553.         select @procedure_name = '%'
  4554.     end
  4555.  
  4556.     /* first we need to extract the procedure group number, if one exists */
  4557.     select @semi_position = charindex(';',@procedure_name)
  4558.     if (@semi_position > 0)
  4559.     begin    /* If group number separator (;) found */
  4560.         select @group_num = convert(int,substring(@procedure_name, @semi_position + 1, 2))
  4561.         select @procedure_name = substring(@procedure_name, 1, @semi_position -1)
  4562.     end
  4563.     else
  4564.     begin    /* No group separator, so default to group number of 1 */
  4565.         select @group_num = 1
  4566.     end
  4567.  
  4568.     if @procedure_owner is null
  4569.     begin    /* If unqualified procedure name */
  4570.         SELECT @full_procedure_name = @procedure_name
  4571.     end
  4572.     else
  4573.     begin    /* Qualified procedure name */
  4574.         if @procedure_owner = ''
  4575.         begin    /* If empty owner name */
  4576.             SELECT @full_procedure_name = @procedure_owner
  4577.         end
  4578.         else
  4579.         begin
  4580.             SELECT @full_procedure_name = @procedure_owner + '.' + @procedure_name
  4581.         end
  4582.     end
  4583.  
  4584.     /*    Get Object ID */
  4585.     SELECT @procedure_id = object_id(@full_procedure_name)
  4586.     if ((charindex('%',@full_procedure_name) = 0) and
  4587.         (charindex('_',@full_procedure_name) = 0)  and
  4588.         @procedure_id <> 0)
  4589.     begin
  4590.         /* this block is for the case where there is no pattern
  4591.             matching required for the procedure name */
  4592.         SELECT
  4593.             PROCEDURE_QUALIFIER = convert(sysname,DB_NAME()),
  4594.             PROCEDURE_OWNER = convert(sysname,USER_NAME(o.uid)),
  4595.             PROCEDURE_NAME = convert(varchar(134),o.name +';'+ ltrim(str(c.number,5))),
  4596.             COLUMN_NAME = convert(sysname,c.name),
  4597.             COLUMN_TYPE = convert(smallint,
  4598.                 1+ColumnProperty (c.id, c.name, 'IsOutParam')),
  4599.             d.DATA_TYPE,
  4600.             TYPE_NAME = t.name,
  4601.             convert(int,case
  4602.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  4603.                 else ColumnProperty (c.id, c.name, 'Precision')
  4604.             end) "PRECISION",
  4605.             convert(int,case
  4606.                 when d.ss_dtype IN (139, 140) then    /* decimal/numeric types */
  4607.                     ColumnProperty (c.id, c.name, 'Precision')+2
  4608.                 else
  4609.                     isnull(d.length, c.length)
  4610.             end) LENGTH,
  4611.             SCALE = convert(smallint, ColumnProperty (c.id, c.name, 'Scale')),
  4612.             d.RADIX,
  4613.             d.NULLABLE,
  4614.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  4615.             COLUMN_DEF = convert(varchar(254),NULL),
  4616.             d.SQL_DATA_TYPE,
  4617.             d.SQL_DATETIME_SUB,
  4618.             CHAR_OCTET_LENGTH = isnull(convert(int,c.prec), 2147483647)+d.charbin,
  4619.             ORDINAL_POSITION = convert(int,c.colid),
  4620.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  4621.             SS_DATA_TYPE = c.type
  4622.         FROM
  4623.             syscolumns c,
  4624.             sysobjects o,
  4625.             master.dbo.spt_datatype_info d,
  4626.             systypes t
  4627.         WHERE
  4628.             o.id = @procedure_id
  4629.             AND c.id = o.id
  4630.             AND c.xtype = d.ss_dtype
  4631.             AND c.length = isnull(d.fixlen, c.length)
  4632.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  4633.             AND isnull(d.AUTO_INCREMENT,0) = 0
  4634.             AND c.xusertype = t.xusertype
  4635.             AND c.name like @column_name
  4636.             AND c.number = @group_num
  4637.         UNION ALL
  4638.         SELECT           /* return value row*/
  4639.             PROCEDURE_QUALIFIER = convert(sysname,DB_NAME()),
  4640.             PROCEDURE_OWNER = convert(sysname,USER_NAME(o.uid)),
  4641.             PROCEDURE_NAME = convert(varchar(134),o.name +';'+ isnull(ltrim(str(c.number,5)),'1')),
  4642.             COLUMN_NAME = convert(sysname,'RETURN_VALUE'),
  4643.             COLUMN_TYPE = convert(smallint, 5),
  4644.             DATA_TYPE = convert(smallint, 4),
  4645.             TYPE_NAME = convert(sysname,'int'),
  4646.             "PRECISION" = convert(int,10),
  4647.             LENGTH = convert(int,4),
  4648.             SCALE = convert(smallint,0),
  4649.             RADIX = convert(smallint,10),
  4650.             NULLABLE = convert(smallint,0),
  4651.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  4652.             COLUMN_DEF = convert(varchar(254),NULL),
  4653.             SQL_DATA_TYPE = convert(smallint, 4),
  4654.             SQL_DATETIME_SUB = convert(smallint,null),
  4655.             CHAR_OCTET_LENGTH = convert(int,null),
  4656.             ORDINAL_POSITION = convert(int,0),
  4657.             IS_NULLABLE = convert(varchar(254),'NO'),
  4658.             SS_DATA_TYPE = convert(tinyint,56)
  4659.         FROM
  4660.             syscolumns c
  4661.             RIGHT OUTER JOIN sysobjects o on c.id = o.id
  4662.         WHERE
  4663.             o.id = @procedure_id
  4664.             AND c.colid = 1
  4665.             AND o.type = 'P'                        /* Just Procedures */
  4666.             AND 'RETURN_VALUE' like @column_name
  4667.             AND c.number = @group_num
  4668.         ORDER BY 1, 2, 3, 18
  4669.     end
  4670.     else
  4671.     begin
  4672.         /* this block is for the case where there IS pattern
  4673.             matching done on the procedure name */
  4674.         if @procedure_owner is null
  4675.             select @procedure_owner = '%'
  4676.         SELECT
  4677.             PROCEDURE_QUALIFIER = convert(sysname,DB_NAME()),
  4678.             PROCEDURE_OWNER = convert(sysname,USER_NAME(o.uid)),
  4679.             PROCEDURE_NAME = convert(varchar(134),o.name +';'+ ltrim(str(c.number,5))),
  4680.             COLUMN_NAME = convert(sysname,c.name),
  4681.             COLUMN_TYPE = convert(smallint,
  4682.                 1+ColumnProperty (c.id, c.name, 'IsOutParam')),
  4683.             d.DATA_TYPE,
  4684.             TYPE_NAME = t.name,
  4685.             convert(int,case
  4686.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  4687.                 else ColumnProperty (c.id, c.name, 'Precision')
  4688.             end) "PRECISION",
  4689.             convert(int,case
  4690.                 when d.ss_dtype IN (139, 140) then    /* decimal/numeric types */
  4691.                     ColumnProperty (c.id, c.name, 'Precision')+2
  4692.                 else
  4693.                     isnull(d.length, c.length)
  4694.             end) LENGTH,
  4695.             SCALE = convert(smallint, ColumnProperty (c.id, c.name, 'Scale')),
  4696.             d.RADIX,
  4697.             d.NULLABLE,
  4698.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  4699.             COLUMN_DEF = convert(varchar(254),NULL),
  4700.             d.SQL_DATA_TYPE,
  4701.             d.SQL_DATETIME_SUB,
  4702.             CHAR_OCTET_LENGTH = isnull(convert(int,c.prec), 2147483647)+d.charbin,
  4703.             ORDINAL_POSITION = convert(int,c.colid),
  4704.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  4705.             SS_DATA_TYPE = c.type
  4706.         FROM
  4707.             syscolumns c,
  4708.             sysobjects o,
  4709.             master.dbo.spt_datatype_info d,
  4710.             systypes t
  4711.         WHERE
  4712.             o.name like @procedure_name
  4713.             AND user_name(o.uid) like @procedure_owner
  4714.             AND o.id = c.id
  4715.             AND c.xtype = d.ss_dtype
  4716.             AND c.length = isnull(d.fixlen, c.length)
  4717.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  4718.             AND isnull(d.AUTO_INCREMENT,0) = 0
  4719.             AND c.xusertype = t.xusertype
  4720.             AND o.type = 'P'                            /* Just Procedures */
  4721.             AND c.name like @column_name
  4722.         UNION ALL
  4723.         SELECT           /* return value row*/
  4724.             PROCEDURE_QUALIFIER = convert(sysname,DB_NAME()),
  4725.             PROCEDURE_OWNER = convert(sysname,USER_NAME(o.uid)),
  4726.             PROCEDURE_NAME = convert(varchar(134),o.name +';'+ isnull(ltrim(str(c.number,5)),'1')),
  4727.             COLUMN_NAME = convert(sysname,'RETURN_VALUE'),
  4728.             COLUMN_TYPE = convert(smallint, 5),
  4729.             DATA_TYPE = convert(smallint, 4),
  4730.             TYPE_NAME = convert(sysname,'int'),
  4731.             "PRECISION" = convert(int,10),
  4732.             LENGTH = convert(int,4),
  4733.             SCALE = convert(smallint,0),
  4734.             RADIX = convert(smallint,10),
  4735.             NULLABLE = convert(smallint,0),
  4736.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  4737.             COLUMN_DEF = convert(varchar(254),NULL),
  4738.             SQL_DATA_TYPE = convert(smallint, 4),
  4739.             SQL_DATETIME_SUB = convert(smallint,null),
  4740.             CHAR_OCTET_LENGTH = convert(int,null),
  4741.             ORDINAL_POSITION = convert(int,0),
  4742.             IS_NULLABLE = convert(varchar(254),'NO'),
  4743.             SS_DATA_TYPE = convert(tinyint,56)
  4744.         FROM
  4745.             syscolumns c
  4746.             RIGHT OUTER JOIN sysobjects o on c.id = o.id
  4747.         WHERE
  4748.             o.name like @procedure_name
  4749.             AND user_name(o.uid) like @procedure_owner
  4750.             AND c.colid = 1
  4751.             AND o.type = 'P'                        /* Just Procedures */
  4752.             AND 'RETURN_VALUE' like @column_name
  4753.         ORDER BY 1, 2, 3, 18
  4754.     end
  4755. go
  4756.  
  4757. grant execute on sp_sproc_columns to public
  4758. go
  4759.  
  4760. dump tran master with no_log
  4761. go
  4762.  
  4763. print 'creating sp_statistics'
  4764. go
  4765.  
  4766. /*    Procedure for pre-7.0 server */
  4767. CREATE PROCEDURE sp_statistics (
  4768.                  @table_name        varchar(32),
  4769.                  @table_owner        varchar(32) = null,
  4770.                  @table_qualifier    varchar(32) = null,
  4771.                  @index_name        varchar(32) = '%',
  4772.                  @is_unique         char(1) = 'N',
  4773.                  @accuracy            char(1) = 'Q')
  4774. AS
  4775.     DECLARE @indid                int
  4776.     DECLARE @lastindid            int
  4777.     DECLARE @table_id            int
  4778.     DECLARE @full_table_name    varchar(65) /* 2*32+1 */
  4779.  
  4780.     create table #TmpIndex(
  4781.         TABLE_QUALIFIER varchar(32) NULL,
  4782.         TABLE_OWNER     varchar(32) NULL,
  4783.         TABLE_NAME        varchar(32) NOT NULL,
  4784.         INDEX_QUALIFIER varchar(32) null,
  4785.         INDEX_NAME        varchar(32) null,
  4786.         NON_UNIQUE        smallint null,
  4787.         TYPE            smallint NOT NULL,
  4788.         SEQ_IN_INDEX    smallint null,
  4789.         COLUMN_NAME     varchar(32) null,
  4790.         COLLATION        char(1) null,
  4791.         index_id        int null,
  4792.         CARDINALITY     int null,
  4793.         PAGES            int null,
  4794.         status            smallint NOT NULL)
  4795.  
  4796.     if @table_qualifier is not null
  4797.     begin
  4798.         if db_name() <> @table_qualifier
  4799.         begin    /* If qualifier doesn't match current database */
  4800.             raiserror 20001 'Table qualifier must be name of current database'
  4801.             return
  4802.         end
  4803.     end
  4804.  
  4805.     if @accuracy not in ('Q','E')
  4806.         begin
  4807.             raiserror 20002 'Illegal ''accuracy'' specified -- must be ''Q'' or ''E''.'
  4808.             return
  4809.         end
  4810.  
  4811.     if (@@trancount <> 0 and
  4812.         charindex('6.50', @@version) = 0 and
  4813.         charindex('7.00', @@version) = 0)
  4814.     begin    /* If inside a transaction */
  4815.         raiserror 20003 'The procedure ''sp_statistics'' cannot be executed from within a transaction.'
  4816.         return
  4817.     end
  4818.  
  4819.     if @table_owner is null
  4820.     begin    /* If unqualified table name */
  4821.         SELECT @full_table_name = @table_name
  4822.     end
  4823.     else
  4824.     begin    /* Qualified table name */
  4825.         if @table_owner = ''
  4826.         begin    /* If empty owner name */
  4827.             SELECT @full_table_name = @table_owner
  4828.         end
  4829.         else
  4830.         begin
  4831.             SELECT @full_table_name = @table_owner + '.' + @table_name
  4832.         end
  4833.     end
  4834.     /*    Get Object ID */
  4835.     SELECT @table_id = object_id(@full_table_name)
  4836.  
  4837.     /*    Start at lowest index id */
  4838.     SELECT @indid = min(indid)
  4839.     FROM sysindexes
  4840.     WHERE id = @table_id
  4841.         AND indid > 0
  4842.         AND indid < 255
  4843.  
  4844.     WHILE @indid <> NULL
  4845.     BEGIN
  4846.         INSERT #TmpIndex    /* Add all columns that are in index */
  4847.             SELECT
  4848.                 DB_NAME(),                                /* TABLE_QUALIFIER */
  4849.                 USER_NAME(o.uid),                        /* TABLE_OWNER       */
  4850.                 o.name,                                 /* TABLE_NAME       */
  4851.                 o.name,                                 /* INDEX_QUALIFIER */
  4852.                 x.name,                                 /* INDEX_NAME       */
  4853.                 0,                                        /* NON_UNIQUE       */
  4854.                 1,                                        /* SQL_INDEX_CLUSTERED */
  4855.                 colid,                                    /* SEQ_IN_INDEX    */
  4856.                 INDEX_COL(@full_table_name,indid,colid),/* COLUMN_NAME       */
  4857.                 'A',                                    /* COLLATION       */
  4858.                 @indid,                                 /* index_id        */
  4859.                 x.rows,                                 /* CARDINALITY       */
  4860.                 x.dpages,                                /* PAGES           */
  4861.                 x.status                                /* status            */
  4862.             FROM sysindexes x, syscolumns c, sysobjects o
  4863.             WHERE
  4864.                 x.id = @table_id
  4865.                 AND x.id = o.id
  4866.                 AND x.id = c.id
  4867.                 AND c.colid < keycnt+(x.status&18)/18    /* all but Unique Clust indices have an extra key */
  4868.                 AND x.indid = @indid
  4869.         /*
  4870.         **      Now move @indid to the next index.
  4871.         */
  4872.         SELECT @lastindid = @indid
  4873.         SELECT @indid = NULL
  4874.  
  4875.         SELECT @indid = min(indid)
  4876.         FROM sysindexes
  4877.         WHERE id = @table_id
  4878.             AND indid > @lastindid
  4879.             AND indid < 255
  4880.     END
  4881.  
  4882.     UPDATE #TmpIndex
  4883.         SET NON_UNIQUE = 1
  4884.         WHERE status&2 <> 2 /* If non-unique index */
  4885.     UPDATE #TmpIndex
  4886.         SET
  4887.             TYPE = 3,            /* SQL_INDEX_OTHER */
  4888.             CARDINALITY = NULL,
  4889.             PAGES = NULL
  4890.         WHERE index_id > 1    /* If non-clustered index */
  4891.  
  4892.     /* now add row for table statistics */
  4893.     INSERT #TmpIndex
  4894.         SELECT
  4895.             DB_NAME(),                /* TABLE_QUALIFIER */
  4896.             USER_NAME(o.uid),        /* TABLE_OWNER       */
  4897.             o.name,                 /* TABLE_NAME       */
  4898.             null,                    /* INDEX_QUALIFIER */
  4899.             null,                    /* INDEX_NAME       */
  4900.             null,                    /* NON_UNIQUE       */
  4901.             0,                        /* SQL_TABLE_STAT  */
  4902.             null,                    /* SEQ_IN_INDEX    */
  4903.             null,                    /* COLUMN_NAME       */
  4904.             null,                    /* COLLATION       */
  4905.             0,                        /* index_id        */
  4906.             x.rows,                 /* CARDINALITY       */
  4907.             x.dpages,                /* PAGES           */
  4908.             0                        /* status           */
  4909.         FROM sysindexes x, sysobjects o
  4910.         WHERE o.id = @table_id
  4911.             AND x.id = o.id
  4912.             AND (x.indid = 0 or x.indid = 1)    /*    If there are no indexes */
  4913.                                                 /*    then table stats are in */
  4914.                                                 /*    a row with indid =0        */
  4915.  
  4916.     if @is_unique <> 'Y'    /* If all indexes desired */
  4917.         SELECT
  4918.             TABLE_QUALIFIER,
  4919.             TABLE_OWNER,
  4920.             TABLE_NAME,
  4921.             NON_UNIQUE,
  4922.             INDEX_QUALIFIER,
  4923.             INDEX_NAME,
  4924.             TYPE,
  4925.             SEQ_IN_INDEX,
  4926.             COLUMN_NAME,
  4927.             COLLATION,
  4928.             CARDINALITY,
  4929.             PAGES,
  4930.             FILTER_CONDITION = convert(varchar(128),null)
  4931.         FROM #TmpIndex
  4932.         WHERE
  4933.             INDEX_NAME like @index_name /* If matching name */
  4934.             or INDEX_NAME is null        /* If SQL_TABLE_STAT row */
  4935.         ORDER BY 4, 7, 6, 8
  4936.     else                    /* If only unique indexes desired */
  4937.         SELECT
  4938.             TABLE_QUALIFIER,
  4939.             TABLE_OWNER,
  4940.             TABLE_NAME,
  4941.             NON_UNIQUE,
  4942.             INDEX_QUALIFIER,
  4943.             INDEX_NAME,
  4944.             TYPE,
  4945.             SEQ_IN_INDEX,
  4946.             COLUMN_NAME,
  4947.             COLLATION,
  4948.             CARDINALITY,
  4949.             PAGES,
  4950.             FILTER_CONDITION = convert(varchar(128),null)
  4951.         FROM #TmpIndex
  4952.         WHERE
  4953.             (NON_UNIQUE = 0             /* If unique */
  4954.                 or NON_UNIQUE is NULL)    /* If SQL_TABLE_STAT row */
  4955.             and (INDEX_NAME like @index_name    /* If matching name */
  4956.                 or INDEX_NAME is null)    /* If SQL_TABLE_STAT row */
  4957.         ORDER BY 4, 7, 6, 8
  4958.  
  4959.     DROP TABLE #TmpIndex
  4960. go
  4961.  
  4962. if (charindex('7.00', @@version) = 0)
  4963. begin
  4964.     print ''
  4965.     print ''
  4966.     print 'Warning:'
  4967.     print 'you are installing the stored procedures '
  4968.     print 'on a pre 7.0 SQL Server.'
  4969.     print 'Ignore the following errors.'
  4970. end
  4971. else
  4972.     drop proc sp_statistics
  4973. go
  4974.  
  4975. /*    Procedure for 7.0 server */
  4976. CREATE PROCEDURE sp_statistics (
  4977.                  @table_name        sysname,
  4978.                  @table_owner        sysname = null,
  4979.                  @table_qualifier    sysname = null,
  4980.                  @index_name        sysname = '%',
  4981.                  @is_unique         char(1) = 'N',
  4982.                  @accuracy            char(1) = 'Q')
  4983. AS
  4984.     DECLARE @indid                int
  4985.     DECLARE @lastindid            int
  4986.     DECLARE @table_id            int
  4987.     DECLARE @full_table_name    varchar(257)
  4988.  
  4989.     create table #TmpIndex(
  4990.         TABLE_QUALIFIER sysname NULL,
  4991.         TABLE_OWNER     sysname NULL,
  4992.         TABLE_NAME        sysname NOT NULL,
  4993.         INDEX_QUALIFIER sysname null,
  4994.         INDEX_NAME        sysname null,
  4995.         NON_UNIQUE        smallint null,
  4996.         TYPE            smallint NOT NULL,
  4997.         SEQ_IN_INDEX    smallint null,
  4998.         COLUMN_NAME     sysname null,
  4999.         COLLATION        char(1) null,
  5000.         index_id        int null,
  5001.         CARDINALITY     int null,
  5002.         PAGES            int null,
  5003.         status            int NOT NULL)
  5004.  
  5005.     if @table_qualifier is not null
  5006.     begin
  5007.         if db_name() <> @table_qualifier
  5008.         begin    /* If qualifier doesn't match current database */
  5009.             raiserror 20001 'Table qualifier must be name of current database'
  5010.             return
  5011.         end
  5012.     end
  5013.  
  5014.     if @accuracy not in ('Q','E')
  5015.         begin
  5016.             raiserror 20002 'Illegal ''accuracy'' specified -- must be ''Q'' or ''E''.'
  5017.             return
  5018.         end
  5019.  
  5020.     if (@@trancount <> 0 and
  5021.         charindex('6.50', @@version) = 0 and
  5022.         charindex('7.00', @@version) = 0)
  5023.     begin    /* If inside a transaction */
  5024.         raiserror 20003 'The procedure ''sp_statistics'' cannot be executed from within a transaction.'
  5025.         return
  5026.     end
  5027.  
  5028.     if @table_owner is null
  5029.     begin    /* If unqualified table name */
  5030.         SELECT @full_table_name = @table_name
  5031.     end
  5032.     else
  5033.     begin    /* Qualified table name */
  5034.         if @table_owner = ''
  5035.         begin    /* If empty owner name */
  5036.             SELECT @full_table_name = @table_owner
  5037.         end
  5038.         else
  5039.         begin
  5040.             SELECT @full_table_name = @table_owner + '.' + @table_name
  5041.         end
  5042.     end
  5043.     /*    Get Object ID */
  5044.     SELECT @table_id = object_id(@full_table_name)
  5045.  
  5046.     /*    Start at lowest index id */
  5047.     SELECT @indid = min(indid)
  5048.     FROM sysindexes
  5049.     WHERE id = @table_id
  5050.         AND indid > 0
  5051.         AND indid < 255
  5052.  
  5053.     WHILE @indid <> NULL
  5054.     BEGIN
  5055.         INSERT #TmpIndex    /* Add all columns that are in index */
  5056.             SELECT
  5057.                 DB_NAME(),                                /* TABLE_QUALIFIER */
  5058.                 USER_NAME(o.uid),                        /* TABLE_OWNER       */
  5059.                 o.name,                                 /* TABLE_NAME       */
  5060.                 o.name,                                 /* INDEX_QUALIFIER */
  5061.                 x.name,                                 /* INDEX_NAME       */
  5062.                 0,                                        /* NON_UNIQUE       */
  5063.                 1,                                        /* SQL_INDEX_CLUSTERED */
  5064.                 colid,                                    /* SEQ_IN_INDEX    */
  5065.                 INDEX_COL(@full_table_name,indid,colid),/* COLUMN_NAME       */
  5066.                 'A',                                    /* COLLATION       */
  5067.                 @indid,                                 /* index_id        */
  5068.                 x.rows,                                 /* CARDINALITY       */
  5069.                 x.dpages,                                /* PAGES           */
  5070.                 x.status                                /* status            */
  5071.             FROM sysindexes x, syscolumns c, sysobjects o
  5072.             WHERE
  5073.                 x.id = @table_id
  5074.                 AND x.id = o.id
  5075.                 AND x.id = c.id
  5076.                 AND c.colid < keycnt+(x.status&18)/18    /* all but Unique Clust indices have an extra key */
  5077.                 AND x.indid = @indid
  5078.         /*
  5079.         **      Now move @indid to the next index.
  5080.         */
  5081.         SELECT @lastindid = @indid
  5082.         SELECT @indid = NULL
  5083.  
  5084.         SELECT @indid = min(indid)
  5085.         FROM sysindexes
  5086.         WHERE id = @table_id
  5087.             AND indid > @lastindid
  5088.             AND indid < 255
  5089.     END
  5090.  
  5091.     UPDATE #TmpIndex
  5092.         SET NON_UNIQUE = 1
  5093.         WHERE status&2 <> 2 /* If non-unique index */
  5094.     UPDATE #TmpIndex
  5095.         SET
  5096.             TYPE = 3,            /* SQL_INDEX_OTHER */
  5097.             CARDINALITY = NULL,
  5098.             PAGES = NULL
  5099.         WHERE index_id > 1    /* If non-clustered index */
  5100.  
  5101.     /* now add row for table statistics */
  5102.     INSERT #TmpIndex
  5103.         SELECT
  5104.             DB_NAME(),                /* TABLE_QUALIFIER */
  5105.             USER_NAME(o.uid),        /* TABLE_OWNER       */
  5106.             o.name,                 /* TABLE_NAME       */
  5107.             null,                    /* INDEX_QUALIFIER */
  5108.             null,                    /* INDEX_NAME       */
  5109.             null,                    /* NON_UNIQUE       */
  5110.             0,                        /* SQL_TABLE_STAT  */
  5111.             null,                    /* SEQ_IN_INDEX    */
  5112.             null,                    /* COLUMN_NAME       */
  5113.             null,                    /* COLLATION       */
  5114.             0,                        /* index_id        */
  5115.             x.rows,                 /* CARDINALITY       */
  5116.             x.dpages,                /* PAGES           */
  5117.             0                        /* status           */
  5118.         FROM sysindexes x, sysobjects o
  5119.         WHERE o.id = @table_id
  5120.             AND x.id = o.id
  5121.             AND (x.indid = 0 or x.indid = 1)    /*    If there are no indexes */
  5122.                                                 /*    then table stats are in */
  5123.                                                 /*    a row with indid =0        */
  5124.  
  5125.     if @is_unique <> 'Y'    /* If all indexes desired */
  5126.         SELECT
  5127.             TABLE_QUALIFIER,
  5128.             TABLE_OWNER,
  5129.             TABLE_NAME,
  5130.             NON_UNIQUE,
  5131.             INDEX_QUALIFIER,
  5132.             INDEX_NAME,
  5133.             TYPE,
  5134.             SEQ_IN_INDEX,
  5135.             COLUMN_NAME,
  5136.             COLLATION,
  5137.             CARDINALITY,
  5138.             PAGES,
  5139.             FILTER_CONDITION = convert(varchar(128),null)
  5140.         FROM #TmpIndex
  5141.         WHERE
  5142.             INDEX_NAME like @index_name /* If matching name */
  5143.             or INDEX_NAME is null        /* If SQL_TABLE_STAT row */
  5144.         ORDER BY 4, 7, 6, 8
  5145.     else                    /* If only unique indexes desired */
  5146.         SELECT
  5147.             TABLE_QUALIFIER,
  5148.             TABLE_OWNER,
  5149.             TABLE_NAME,
  5150.             NON_UNIQUE,
  5151.             INDEX_QUALIFIER,
  5152.             INDEX_NAME,
  5153.             TYPE,
  5154.             SEQ_IN_INDEX,
  5155.             COLUMN_NAME,
  5156.             COLLATION,
  5157.             CARDINALITY,
  5158.             PAGES,
  5159.             FILTER_CONDITION = convert(varchar(128),null)
  5160.         FROM #TmpIndex
  5161.         WHERE
  5162.             (NON_UNIQUE = 0             /* If unique */
  5163.                 or NON_UNIQUE is NULL)    /* If SQL_TABLE_STAT row */
  5164.             and (INDEX_NAME like @index_name    /* If matching name */
  5165.                 or INDEX_NAME is null)    /* If SQL_TABLE_STAT row */
  5166.         ORDER BY 4, 7, 6, 8
  5167.  
  5168.     DROP TABLE #TmpIndex
  5169. go
  5170.  
  5171. grant execute on sp_statistics to public
  5172. go
  5173.  
  5174. dump tran master with no_log
  5175. go
  5176.  
  5177. print 'creating sp_stored_procedures'
  5178. go
  5179.  
  5180.  
  5181. /* pre 7.00 version */
  5182. create procedure sp_stored_procedures(
  5183.                         @sp_name        varchar(102) = null,
  5184.                         @sp_owner        varchar(96) = null,
  5185.                         @sp_qualifier    varchar(32) = null)
  5186. as
  5187.     declare @proc_type smallint
  5188.  
  5189.     if @sp_qualifier is not null
  5190.     begin
  5191.         if db_name() <> @sp_qualifier
  5192.         begin
  5193.             if @sp_qualifier = ''
  5194.             begin
  5195.                 /* in this case, we need to return an empty result set */
  5196.                 /* because the user has requested a database with an empty name */
  5197.                 select @sp_name = ''
  5198.                 select @sp_owner = ''
  5199.             end else
  5200.             begin    /* If qualifier doesn't match current database */
  5201.                 raiserror 20001 'Procedure qualifier must be name of current database'
  5202.                 return
  5203.             end
  5204.         end
  5205.     end
  5206.  
  5207.     if @sp_name is null
  5208.     begin  /*  If procedure name not supplied, match all */
  5209.         select @sp_name = '%'
  5210.     end
  5211.     else begin
  5212.         if (@sp_owner is null) and (charindex('%', @sp_name) = 0)
  5213.         begin
  5214.             if exists (select * from sysobjects
  5215.                 where uid = user_id()
  5216.                     and name = @sp_name
  5217.                     and type = 'P') /* Object type of Procedure */
  5218.             begin
  5219.                 select @sp_owner = user_name()
  5220.             end
  5221.         end
  5222.     end
  5223.     if @sp_owner is null    /*    If procedure owner not supplied, match all */
  5224.         select @sp_owner = '%'
  5225.  
  5226.     select @proc_type=2        /* Return 2 for 4.2 and later servers. */
  5227.  
  5228.     select
  5229.         PROCEDURE_QUALIFIER = convert(varchar(32),db_name()),
  5230.         PROCEDURE_OWNER = convert(varchar(32),user_name(o.uid)),
  5231.         PROCEDURE_NAME = convert(varchar(36),o.name +';'+ ltrim(str(c.number,5))),
  5232.         NUM_INPUT_PARAMS = -1,    /* Constant since value unknown */
  5233.         NUM_OUTPUT_PARAMS = -1, /* Constant since value unknown */
  5234.         NUM_RESULT_SETS = -1,    /* Constant since value unknown */
  5235.         REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  5236.         PROCEDURE_TYPE = @proc_type
  5237.     from
  5238.         sysobjects o,syscomments c,sysusers u
  5239.     where
  5240.         o.name like @sp_name
  5241.         and c.colid = 1
  5242.         and user_name(o.uid) like @sp_owner
  5243.         and o.type = 'P'        /* Object type of Procedure */
  5244.         and c.id = o.id
  5245.         and u.uid = user_id()    /* constrain sysusers uid for use in subquery */
  5246.         and (suser_id() = 1     /* User is the System Administrator */
  5247.             or o.uid = user_id()    /* User created the object */
  5248.             /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  5249.             or ((select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  5250.              from sysprotects p
  5251.              /* outer join to correlate with all rows in sysobjects */
  5252.              where p.id =* o.id
  5253.                  /*  get rows for public,current user,user's group */
  5254.                  and (p.uid = 0 or p.uid = user_id() or p.uid =* u.gid)
  5255.                  /* check for SELECT,EXECUTE privilege */
  5256.                  and (action in (193,224)))&1    /* more magic...normalize GRANT */
  5257.             ) = 1     /* final magic...compare Grants    */
  5258.         )
  5259.     order by 1, 2, 3
  5260. go
  5261.  
  5262. grant execute on sp_stored_procedures to public
  5263. go
  5264.  
  5265.  
  5266. if (charindex('7.00', @@version) > 0)
  5267.     drop procedure sp_stored_procedures
  5268. else
  5269. begin
  5270.     print ''
  5271.     print ''
  5272.     print 'Warning:'
  5273.     print 'you are installing the stored procedures '
  5274.     print 'on a pre 7.0 SQL Server.'
  5275.     print 'Ignore the following errors.'
  5276. end
  5277. go
  5278.  
  5279.  
  5280. /* 7.00 version */
  5281. create procedure sp_stored_procedures(
  5282.                         @sp_name        varchar(390) = null,
  5283.                         @sp_owner        varchar(384) = null,
  5284.                         @sp_qualifier    sysname = null)
  5285. as
  5286.     declare @proc_type smallint
  5287.  
  5288.     if @sp_qualifier is not null
  5289.     begin
  5290.         if db_name() <> @sp_qualifier
  5291.         begin
  5292.             if @sp_qualifier = ''
  5293.             begin
  5294.                 /* in this case, we need to return an empty result set */
  5295.                 /* because the user has requested a database with an empty name */
  5296.                 select @sp_name = ''
  5297.                 select @sp_owner = ''
  5298.             end else
  5299.             begin    /* If qualifier doesn't match current database */
  5300.                 raiserror 20001 'Procedure qualifier must be name of current database'
  5301.                 return
  5302.             end
  5303.         end
  5304.     end
  5305.  
  5306.     if @sp_name is null
  5307.     begin  /*  If procedure name not supplied, match all */
  5308.         select @sp_name = '%'
  5309.     end
  5310.     else begin
  5311.         if (@sp_owner is null) and (charindex('%', @sp_name) = 0)
  5312.         begin
  5313.             if exists (select * from sysobjects
  5314.                 where uid = user_id()
  5315.                     and name = @sp_name
  5316.                     and type = 'P') /* Object type of Procedure */
  5317.             begin
  5318.                 select @sp_owner = user_name()
  5319.             end
  5320.         end
  5321.     end
  5322.     if @sp_owner is null    /*    If procedure owner not supplied, match all */
  5323.         select @sp_owner = '%'
  5324.  
  5325.     select @proc_type=2        /* Return 2 for 4.2 and later servers. */
  5326.  
  5327.     select
  5328.         PROCEDURE_QUALIFIER = convert(sysname,db_name()),
  5329.         PROCEDURE_OWNER = convert(sysname,user_name(o.uid)),
  5330.         PROCEDURE_NAME = convert(varchar(134),o.name +';'+ ltrim(str(c.number,5))),
  5331.         NUM_INPUT_PARAMS = -1,    /* Constant since value unknown */
  5332.         NUM_OUTPUT_PARAMS = -1, /* Constant since value unknown */
  5333.         NUM_RESULT_SETS = -1,    /* Constant since value unknown */
  5334.         REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  5335.         PROCEDURE_TYPE = @proc_type
  5336.     from
  5337.         sysobjects o,syscomments c
  5338.     where
  5339.         o.name like @sp_name
  5340.         and c.colid = 1
  5341.         and user_name(o.uid) like @sp_owner
  5342.         and o.type = 'P'        /* Object type of Procedure */
  5343.         and c.id = o.id
  5344.         and permissions (o.id)&32 <> 0
  5345.     order by 1, 2, 3
  5346. go
  5347.  
  5348. grant execute on sp_stored_procedures to public
  5349. go
  5350.  
  5351. dump tran master with no_log
  5352. go
  5353.  
  5354.  
  5355. print 'creating sp_table_privileges'
  5356. go
  5357.  
  5358. /*    Procedure for pre 6.50 server */
  5359. CREATE PROCEDURE sp_table_privileges (
  5360.             @table_name         varchar(96),        /* 3*32 */
  5361.             @table_owner        varchar(96) = null,
  5362.             @table_qualifier    varchar(32) = null)
  5363. as
  5364.  
  5365.     if @table_qualifier is not null
  5366.     begin
  5367.         if db_name() <> @table_qualifier
  5368.         begin    /* If qualifier doesn't match current database */
  5369.             raiserror 20001 'Table qualifier must be name of current database'
  5370.             return
  5371.         end
  5372.     end
  5373.     if (@@trancount <> 0)
  5374.     begin    /* If inside a transaction */
  5375.         raiserror 20003 'The procedure ''sp_table_privileges'' cannot be executed from within a transaction.'
  5376.         return
  5377.     end
  5378.     if @table_name is null
  5379.         select @table_name = '%'
  5380.     if @table_owner is null /* If no owner supplied, force wildcard */
  5381.         select @table_owner = '%'
  5382.  
  5383.     create table #table_priv1(
  5384.         id                        int NOT NULL,
  5385.         grantor                 smallint NOT NULL,
  5386.         grantee                 smallint NOT NULL,
  5387.         select_privilege        bit,
  5388.         insert_privilege        bit,
  5389.         update_privilege        bit,
  5390.         delete_privilege        bit,
  5391.         references_privilege    bit,
  5392.         select_privilege_grant    bit,
  5393.         insert_privilege_grant    bit,
  5394.         update_privilege_grant    bit,
  5395.         delete_privilege_grant    bit,
  5396.         references_privilege_grant    bit,
  5397.         uid                     smallint NOT NULL,
  5398.         gid                     smallint NOT NULL)
  5399.  
  5400.     insert into #table_priv1
  5401.         select distinct
  5402.             o.id,
  5403.             o.uid,
  5404.             u.uid,
  5405.             0,
  5406.             0,
  5407.             0,
  5408.             0,
  5409.             0,
  5410.             0,
  5411.             0,
  5412.             0,
  5413.             0,
  5414.             0,
  5415.             u.uid,
  5416.             u.gid
  5417.         from sysusers u, sysobjects o, sysprotects p
  5418.         where
  5419.             o.name like @table_name
  5420.             and user_name(o.uid) like @table_owner
  5421.             and u.uid <> u.gid
  5422.             and u.uid *= p.uid and o.id *= p.id
  5423.             and p.protecttype in (204, 205)
  5424.             and type in ('S ','U ','V ')    /* only valid for system tables,
  5425.                                             ** user tables, and views. */
  5426.  
  5427.     /*
  5428.     ** now add/update row for table owner
  5429.     */
  5430.     if exists (
  5431.         select *
  5432.             from #table_priv1
  5433.             where grantor = grantee)
  5434.     begin
  5435.         update #table_priv1
  5436.         set
  5437.             select_privilege_grant = 1,
  5438.             update_privilege_grant = 1,
  5439.             insert_privilege_grant = 1,
  5440.             delete_privilege_grant = 1,
  5441.             references_privilege_grant = 1,
  5442.             grantor = (select uid from sysusers where suid = 1)
  5443.         where grantor = grantee
  5444.     end
  5445.     else
  5446.     begin
  5447.         insert into #table_priv1
  5448.             select
  5449.                 o.id,
  5450.                 u1.uid,
  5451.                 o.uid,
  5452.                 0,
  5453.                 0,
  5454.                 0,
  5455.                 0,
  5456.                 0,
  5457.                 1,
  5458.                 1,
  5459.                 1,
  5460.                 1,
  5461.                 1,
  5462.                 o.uid,
  5463.                 u.gid
  5464.             from sysobjects o, sysusers u, sysusers u1
  5465.             where o.name like @table_name
  5466.             and user_name(o.uid) like @table_owner
  5467.             and u.uid = o.uid
  5468.             and type in ('S ','U ','V ')    /* only valid for system tables,
  5469.                                            ** user tables, and views. */
  5470.             and u1.suid = 1        /* grantor is dbo of database */
  5471.  
  5472.     end
  5473.  
  5474.     update #table_priv1
  5475.     set select_privilege = 1
  5476.         from sysprotects p
  5477.     where
  5478.         #table_priv1.id = p.id
  5479.         and (#table_priv1.uid = p.uid
  5480.             or #table_priv1.gid = p.uid
  5481.             or p.uid = 0)
  5482.         and protecttype = 205
  5483.         and action = 193
  5484.         and not exists (
  5485.             select * from sysprotects
  5486.             where
  5487.                 #table_priv1.id = sysprotects.id
  5488.                 and (#table_priv1.uid = uid
  5489.                     or #table_priv1.gid = uid
  5490.                     or uid = 0)
  5491.                 and protecttype = 206
  5492.                 and action = 193)
  5493.  
  5494.     update #table_priv1
  5495.     set insert_privilege = 1
  5496.         from sysprotects p
  5497.     where
  5498.         #table_priv1.id = p.id
  5499.         and (#table_priv1.uid = p.uid
  5500.             or #table_priv1.gid = p.uid
  5501.             or p.uid = 0)
  5502.         and protecttype = 205
  5503.         and action = 195
  5504.         and not exists (
  5505.             select * from sysprotects
  5506.             where
  5507.                 #table_priv1.id = sysprotects.id
  5508.                 and (#table_priv1.uid = uid
  5509.                     or #table_priv1.gid = uid
  5510.                     or uid = 0)
  5511.                 and protecttype = 206
  5512.                 and action = 195)
  5513.  
  5514.     update #table_priv1
  5515.     set delete_privilege = 1
  5516.         from sysprotects p
  5517.     where
  5518.         exists (
  5519.             select * from sysprotects
  5520.             where
  5521.                 #table_priv1.id = sysprotects.id
  5522.                 and (#table_priv1.uid = uid
  5523.                     or #table_priv1.gid = uid
  5524.                     or uid = 0)
  5525.                 and protecttype = 205
  5526.                 and action = 196)
  5527.         and not exists (select * from sysprotects
  5528.             where
  5529.                 #table_priv1.id = sysprotects.id
  5530.                 and (#table_priv1.uid = uid
  5531.                     or #table_priv1.gid = uid
  5532.                     or uid = 0)
  5533.                 and protecttype = 206
  5534.                 and action = 196)
  5535.  
  5536.     update #table_priv1
  5537.     set update_privilege = 1
  5538.         from sysprotects p
  5539.     where
  5540.         #table_priv1.id = p.id
  5541.         and (#table_priv1.uid = p.uid
  5542.             or #table_priv1.gid = p.uid
  5543.             or p.uid = 0)
  5544.         and protecttype = 205
  5545.         and action = 197
  5546.         and not exists (
  5547.             select * from sysprotects
  5548.             where
  5549.                 #table_priv1.id = sysprotects.id
  5550.                 and (#table_priv1.uid = uid
  5551.                     or #table_priv1.gid = uid
  5552.                     or uid = 0)
  5553.                 and protecttype = 206
  5554.                 and action = 197)
  5555.  
  5556.     update #table_priv1
  5557.     set references_privilege = 1
  5558.         from sysprotects p
  5559.     where
  5560.         #table_priv1.id = p.id
  5561.         and (#table_priv1.uid = p.uid
  5562.             or #table_priv1.gid = p.uid
  5563.             or p.uid = 0)
  5564.         and protecttype = 205
  5565.         and action = 26
  5566.         and not exists (
  5567.             select * from sysprotects
  5568.             where
  5569.                 #table_priv1.id = sysprotects.id
  5570.                 and (#table_priv1.uid = uid
  5571.                     or #table_priv1.gid = uid
  5572.                     or uid = 0)
  5573.                 and protecttype = 206
  5574.                 and action = 26)
  5575.  
  5576.     create table #table_priv2(
  5577.         id                int NOT NULL,
  5578.         grantor         smallint NOT NULL,
  5579.         grantee         smallint NOT NULL,
  5580.         PRIVILEGE        varchar(32) NOT NULL,
  5581.         IS_GRANTABLE    varchar(3) NULL)
  5582.  
  5583.     insert into #table_priv2
  5584.         select
  5585.             id,
  5586.             grantor,
  5587.             grantee,
  5588.             'SELECT',
  5589.             'NO'
  5590.         from #table_priv1
  5591.         where select_privilege = 1 and select_privilege_grant = 0
  5592.  
  5593.     insert into #table_priv2
  5594.         select
  5595.             id,
  5596.             grantor,
  5597.             grantee,
  5598.             'INSERT',
  5599.             'NO'
  5600.         from #table_priv1
  5601.         where insert_privilege = 1 and insert_privilege_grant = 0
  5602.  
  5603.     insert into #table_priv2
  5604.         select
  5605.             id,
  5606.             grantor,
  5607.             grantee,
  5608.             'DELETE',
  5609.             'NO'
  5610.         from #table_priv1
  5611.         where delete_privilege = 1 and delete_privilege_grant = 0
  5612.  
  5613.     insert into #table_priv2
  5614.         select
  5615.             id,
  5616.             grantor,
  5617.             grantee,
  5618.             'UPDATE',
  5619.             'NO'
  5620.         from #table_priv1
  5621.         where update_privilege = 1 and update_privilege_grant = 0
  5622.  
  5623.     insert into #table_priv2
  5624.         select
  5625.             id,
  5626.             grantor,
  5627.             grantee,
  5628.             'REFERENCES',
  5629.             'NO'
  5630.         from #table_priv1
  5631.         where references_privilege = 1 and references_privilege_grant = 0
  5632.  
  5633.     insert into #table_priv2
  5634.         select
  5635.             id,
  5636.             grantor,
  5637.             grantee,
  5638.             'SELECT',
  5639.             'YES'
  5640.         from #table_priv1
  5641.         where select_privilege_grant = 1
  5642.  
  5643.     insert into #table_priv2
  5644.         select
  5645.             id,
  5646.             grantor,
  5647.             grantee,
  5648.             'INSERT',
  5649.             'YES'
  5650.         from #table_priv1
  5651.         where insert_privilege_grant = 1
  5652.  
  5653.     insert into #table_priv2
  5654.         select
  5655.             id,
  5656.             grantor,
  5657.             grantee,
  5658.             'DELETE',
  5659.             'YES'
  5660.         from #table_priv1
  5661.         where delete_privilege_grant = 1
  5662.  
  5663.     insert into #table_priv2
  5664.         select
  5665.             id,
  5666.             grantor,
  5667.             grantee,
  5668.             'UPDATE',
  5669.             'YES'
  5670.         from #table_priv1
  5671.         where update_privilege_grant = 1
  5672.  
  5673.     insert into #table_priv2
  5674.         select
  5675.             id,
  5676.             grantor,
  5677.             grantee,
  5678.             'REFERENCES',
  5679.             'YES'
  5680.         from #table_priv1
  5681.         where references_privilege_grant = 1
  5682.  
  5683.  
  5684.     select
  5685.         convert(varchar(32),db_name()) TABLE_QUALIFIER,
  5686.         convert(varchar(32),user_name(o.uid)) TABLE_OWNER,
  5687.         convert(varchar(32),o.name) TABLE_NAME,
  5688.         convert(varchar(32),user_name(grantor)) GRANTOR,
  5689.         convert(varchar(32),user_name(grantee)) GRANTEE,
  5690.         PRIVILEGE,
  5691.         IS_GRANTABLE
  5692.     from #table_priv2 t, sysobjects o where o.id = t.id
  5693.     order by 2,3,6,5
  5694. go
  5695.  
  5696. if (charindex('6.50', @@version) = 0 and
  5697.     charindex('7.00', @@version) = 0)
  5698. begin
  5699.     print ''
  5700.     print ''
  5701.     print 'Warning:'
  5702.     print 'you are installing the stored procedures '
  5703.     print 'on a pre 6.50 SQL Server.'
  5704.     print 'Ignore the following errors.'
  5705. end
  5706. else
  5707.     drop proc sp_table_privileges
  5708. go
  5709.  
  5710.  
  5711. /*    Procedure for 6.50 server */
  5712. CREATE PROCEDURE sp_table_privileges (
  5713.             @table_name         varchar(96),
  5714.             @table_owner        varchar(96) = null,
  5715.             @table_qualifier    varchar(32) = null)
  5716. as
  5717.  
  5718.     if @table_qualifier is not null
  5719.     begin
  5720.         if db_name() <> @table_qualifier
  5721.         begin    /* If qualifier doesn't match current database */
  5722.             raiserror 20001 'Table qualifier must be name of current database'
  5723.             return
  5724.         end
  5725.     end
  5726.     if @table_name is null
  5727.         select @table_name = '%'
  5728.     if @table_owner is null /* If no owner supplied, force wildcard */
  5729.         select @table_owner = '%'
  5730.  
  5731.     select
  5732.         convert(varchar(32),db_name()) TABLE_QUALIFIER,
  5733.         convert(varchar(32),user_name(o.uid)) TABLE_OWNER,
  5734.         convert(varchar(32),object_name(o.id)) TABLE_NAME,
  5735.         convert(varchar(32),user_name(p.grantor)) GRANTOR,
  5736.         convert(varchar(32),user_name(u.uid)) GRANTEE,
  5737.         convert(varchar(32),case p.action
  5738.              when 193 then 'SELECT'
  5739.              when 195 then 'INSERT'
  5740.              when 196 then 'DELETE'
  5741.              when 197 then 'UPDATE'
  5742.              else 'REFERENCES'
  5743.         end) PRIVILEGE,
  5744.         convert(varchar(3),case when p.protecttype = 205 then 'NO'
  5745.             else 'YES'
  5746.         end) IS_GRANTABLE
  5747.     from sysprotects p, sysobjects o, sysusers u
  5748.     where
  5749.         p.id = o.id
  5750.         and o.type in ('U','V','S')
  5751.         and object_name(o.id) like @table_name
  5752.         and user_name(o.uid) like @table_owner
  5753.             /* expand groups */
  5754.         and ((p.uid = u.uid and u.uid <> u.gid) or
  5755.              (p.uid = u.gid and u.uid <> u.gid))
  5756.         and p.protecttype <> 206    /* only grant rows */
  5757.         and p.action in (26,193,195,196,197)
  5758.         and o.uid <> u.uid            /* no rows for owner */
  5759.         and not exists (            /* exclude revoke'd privileges */
  5760.             select *
  5761.             from sysprotects p1
  5762.             where
  5763.                 p1.protecttype = 206
  5764.                 and p1.action = p.action
  5765.                 and p1.id = p.id
  5766.                 and p1.uid = u.uid)
  5767.     union all
  5768.     select    /*    Add rows for table owner */
  5769.         convert(varchar(32),db_name()) TABLE_QUALIFIER,
  5770.         convert(varchar(32),user_name(o.uid)) TABLE_OWNER,
  5771.         convert(varchar(32),object_name(o.id)) TABLE_NAME,
  5772.         convert(varchar(32),user_name(u.uid)) GRANTOR,
  5773.         convert(varchar(32),user_name(o.uid)) GRANTEE,
  5774.         convert(varchar(32),case v.number
  5775.             when 193 then 'SELECT'
  5776.             when 195 then 'INSERT'
  5777.             when 196 then 'DELETE'
  5778.             when 197 then 'UPDATE'
  5779.             else 'REFERENCES'
  5780.         end) PRIVILEGE,
  5781.         convert(varchar(3),'YES') IS_GRANTABLE
  5782.     from sysobjects o, master.dbo.spt_values v, sysusers u
  5783.     where
  5784.         object_name(o.id) like @table_name
  5785.         and o.type in ('U','V','S')
  5786.         and user_name(o.uid) like @table_owner
  5787.         and u.suid = 1        /* grantor is dbo of database */
  5788.         and v.type = 'P'    /* cross product to get all exposed privileges */
  5789.         and v.number in (26,193,195,196,197)
  5790.         and not exists (    /* exclude revoke'd privileges */
  5791.             select *
  5792.             from sysprotects p1
  5793.             where
  5794.                 p1.protecttype = 206
  5795.                 and p1.action = v.number
  5796.                 and p1.id = o.id
  5797.                 and p1.uid = o.uid)
  5798.     order by 2,3,6,5
  5799. go
  5800.  
  5801.  
  5802. if (charindex('7.00', @@version) > 0)
  5803.     drop proc sp_table_privileges
  5804. else
  5805. begin
  5806.     print ''
  5807.     print ''
  5808.     print 'Warning:'
  5809.     print 'you are installing the stored procedures '
  5810.     print 'on a pre 7.0 SQL Server.'
  5811.     print 'Ignore the following errors.'
  5812. end
  5813. go
  5814.  
  5815. /*    Procedure for 7.00 server */
  5816. CREATE PROCEDURE sp_table_privileges (
  5817.             @table_name         varchar(384),
  5818.             @table_owner        varchar(384) = null,
  5819.             @table_qualifier    sysname = null)
  5820. as
  5821.  
  5822.     if @table_qualifier is not null
  5823.     begin
  5824.         if db_name() <> @table_qualifier
  5825.         begin    /* If qualifier doesn't match current database */
  5826.             raiserror 20001 'Table qualifier must be name of current database'
  5827.             return
  5828.         end
  5829.     end
  5830.     if @table_name is null
  5831.         select @table_name = '%'
  5832.     if @table_owner is null /* If no owner supplied, force wildcard */
  5833.         select @table_owner = '%'
  5834.  
  5835.     select
  5836.         convert(sysname,db_name()) TABLE_QUALIFIER,
  5837.         convert(sysname,user_name(o.uid)) TABLE_OWNER,
  5838.         convert(sysname,object_name(o.id)) TABLE_NAME,
  5839.         convert(sysname,user_name(p.grantor)) GRANTOR,
  5840.         convert(sysname,user_name(u.uid)) GRANTEE,
  5841.         convert(varchar(32),case p.action
  5842.              when 193 then 'SELECT'
  5843.              when 195 then 'INSERT'
  5844.              when 196 then 'DELETE'
  5845.              when 197 then 'UPDATE'
  5846.              else 'REFERENCES'
  5847.         end) PRIVILEGE,
  5848.         convert(varchar(3),case when p.protecttype = 205 then 'NO'
  5849.             else 'YES'
  5850.         end) IS_GRANTABLE
  5851.     from sysprotects p, sysobjects o, sysusers u, sysmembers m
  5852.     where
  5853.         p.id = o.id
  5854.         and o.type in ('U','V','S')
  5855.         and object_name(o.id) like @table_name
  5856.         and user_name(o.uid) like @table_owner
  5857.             /* expand groups - AKUNDONE: only 1 level of grp unrolling. */
  5858.         and (u.uid > 0 and u.uid < 16384)
  5859.         and ((p.uid = u.uid) or
  5860.              (p.uid = m.groupuid and u.uid = m.memberuid))
  5861.         and p.protecttype <> 206    /* only grant rows */
  5862.         and p.action in (26,193,195,196,197)
  5863.         and o.uid <> u.uid            /* no rows for owner */
  5864.         and not exists (            /* exclude revoke'd privileges */
  5865.             select *
  5866.             from sysprotects p1
  5867.             where
  5868.                 p1.protecttype = 206
  5869.                 and p1.action = p.action
  5870.                 and p1.id = p.id
  5871.                 and p1.uid = u.uid)
  5872.     union all
  5873.     select    /*    Add rows for table owner */
  5874.         convert(sysname,db_name()) TABLE_QUALIFIER,
  5875.         convert(sysname,user_name(o.uid)) TABLE_OWNER,
  5876.         convert(sysname,object_name(o.id)) TABLE_NAME,
  5877.         convert(sysname,user_name(u.uid)) GRANTOR,
  5878.         convert(sysname,user_name(o.uid)) GRANTEE,
  5879.         convert(varchar(32),case v.number
  5880.             when 193 then 'SELECT'
  5881.             when 195 then 'INSERT'
  5882.             when 196 then 'DELETE'
  5883.             when 197 then 'UPDATE'
  5884.             else 'REFERENCES'
  5885.         end) PRIVILEGE,
  5886.         convert(varchar(3),'YES') IS_GRANTABLE
  5887.     from sysobjects o, master.dbo.spt_values v, sysusers u
  5888.     where
  5889.         object_name(o.id) like @table_name
  5890.         and o.type in ('U','V','S')
  5891.         and user_name(o.uid) like @table_owner
  5892.         and u.uid = 1        /* grantor is 'dbo' of database */
  5893.         and v.type = 'P'    /* cross product to get all exposed privileges */
  5894.         and v.number in (26,193,195,196,197)
  5895.         and not exists (    /* exclude revoke'd privileges */
  5896.             select *
  5897.             from sysprotects p1
  5898.             where
  5899.                 p1.protecttype = 206
  5900.                 and p1.action = v.number
  5901.                 and p1.id = o.id
  5902.                 and p1.uid = o.uid)
  5903.     order by 2,3,6,5
  5904. go
  5905.  
  5906. grant execute on sp_table_privileges to public
  5907. go
  5908.  
  5909. dump tran master with no_log
  5910. go
  5911.  
  5912. print 'creating sp_tables'
  5913. go
  5914.  
  5915. /*    Procedure for 6.50 and earlier servers */
  5916. create procedure sp_tables(
  5917.                @table_name        varchar(96)    = null,
  5918.                @table_owner     varchar(96)    = null,
  5919.                @table_qualifier varchar(32)    = null,
  5920.                @table_type        varchar(100) = null)
  5921. as
  5922.     declare @type1 varchar(3)
  5923.     declare @tableindex int
  5924.  
  5925.  
  5926.     /* Special feature #1:    enumerate databases when owner and name
  5927.          are blank but qualifier is explicitly '%'.  */
  5928.     if @table_qualifier = '%' and
  5929.         @table_owner = '' and
  5930.         @table_name = ''
  5931.     begin    /* If enumerating databases */
  5932.         select
  5933.             TABLE_QUALIFIER = convert(varchar(32),d.name),
  5934.             TABLE_OWNER = convert(varchar(32),null),
  5935.             TABLE_NAME = convert(varchar(32),null),
  5936.             TABLE_TYPE = convert(varchar(32),null),
  5937.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  5938.         from master.dbo.sysdatabases d
  5939.         where d.name <> 'model'    /* eliminate MODEL database */
  5940.         order by 1
  5941.     end
  5942.  
  5943.     /* Special feature #2:    enumerate owners when qualifier and name
  5944.          are blank but owner is explicitly '%'.  */
  5945.     else if @table_qualifier = '' and
  5946.         @table_owner = '%' and
  5947.         @table_name = ''
  5948.     begin    /* If enumerating owners */
  5949.         select distinct
  5950.             TABLE_QUALIFIER = convert(varchar(32),null),
  5951.             TABLE_OWNER = convert(varchar(32),user_name(uid)),
  5952.             TABLE_NAME = convert(varchar(32),null),
  5953.             TABLE_TYPE = convert(varchar(32),null),
  5954.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  5955.         from sysobjects
  5956.         order by 2
  5957.     end
  5958.  
  5959.     /* Special feature #3:    enumerate table types when qualifier, owner and
  5960.          name are blank but table type is explicitly '%'.    */
  5961.     else if @table_qualifier = '' and
  5962.         @table_owner = '' and
  5963.         @table_name = '' and
  5964.         @table_type = '%'
  5965.     begin    /* If enumerating table types */
  5966.         select
  5967.             TABLE_QUALIFIER = convert(varchar(32),null),
  5968.             TABLE_OWNER = convert(varchar(32),null),
  5969.             TABLE_NAME = convert(varchar(32),null),
  5970.             TABLE_TYPE = convert(varchar(32),rtrim(substring('SYSTEM TABLETABLE       VIEW',(colid-1)*12+1,12))),
  5971.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  5972.         from sysobjects o, syscolumns c
  5973.         where o.id=c.id and o.name='sysusers' and colid<=3
  5974.     end
  5975.  
  5976.     else
  5977.     begin /* end of special features - do normal processing */
  5978.         if @table_qualifier is not null
  5979.         begin
  5980.             if db_name() <> @table_qualifier
  5981.             begin
  5982.                 if @table_qualifier = ''
  5983.                 begin  /* If empty qualifier supplied */
  5984.                     /* Force an empty result set */
  5985.                     select @table_name = ''
  5986.                     select @table_owner = ''
  5987.                 end
  5988.                 else
  5989.                 begin    /* If qualifier doesn't match current database */
  5990.                     raiserror 20001 'Table qualifier must be name of current database'
  5991.                     return
  5992.                 end
  5993.             end
  5994.         end
  5995.         if @table_type is null
  5996.         begin    /* Select all ODBC supported table types */
  5997.             select @type1 = 'SUV'
  5998.         end
  5999.         else
  6000.         begin
  6001.             /*    TableType is case sensitive if CS server */
  6002.             select @type1 = null
  6003.             if (charindex('''SYSTEM TABLE''',@table_type) <> 0)
  6004.                 select @type1 = @type1 + 'S'    /* Add System Tables */
  6005.             if (charindex('''TABLE''',@table_type) <> 0)
  6006.                 select @type1 = @type1 + 'U'    /* Add User Tables */
  6007.             if (charindex('''VIEW''',@table_type) <> 0)
  6008.                 select @type1 = @type1 + 'V'    /* Add Views */
  6009.         end
  6010.         if @table_name is null
  6011.         begin    /*    If table name not supplied, match all */
  6012.             select @table_name = '%'
  6013.         end
  6014.         else
  6015.         begin
  6016.             if (@table_owner is null) and (charindex('%', @table_name) = 0)
  6017.             begin    /* If owner not specified and table is specified */
  6018.                 if exists (select * from sysobjects
  6019.                     where uid = user_id()
  6020.                     and name = @table_name
  6021.                     and (type = 'U' or type = 'V' or type = 'S'))
  6022.                 begin    /* Override supplied owner w/owner of table */
  6023.                     select @table_owner = user_name()
  6024.                 end
  6025.             end
  6026.         end
  6027.         if @table_owner is null /* If no owner supplied, force wildcard */
  6028.             select @table_owner = '%'
  6029.         select
  6030.             TABLE_QUALIFIER = convert(varchar(32),db_name()),
  6031.             TABLE_OWNER = convert(varchar(32),user_name(o.uid)),
  6032.             TABLE_NAME = convert(varchar(32),o.name),    /* make nullable */
  6033.             TABLE_TYPE = convert(varchar(32),rtrim(
  6034.                 substring('SYSTEM TABLE            TABLE       VIEW       ',
  6035.                     (ascii(o.type)-83)*12+1,12))),    /* 'S'=0,'U'=2,'V'=3 */
  6036.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  6037.         from sysusers u, sysobjects o
  6038.         where
  6039.             o.name like @table_name
  6040.             and user_name(o.uid) like @table_owner
  6041.             and charindex(substring(o.type,1,1),@type1) <> 0 /* Only desired types */
  6042.             and u.uid = user_id() /* constrain sysusers uid for use in subquery */
  6043.             and (
  6044.                 suser_id() = 1     /* User is the System Administrator */
  6045.                 or o.uid = user_id()     /* User created the object */
  6046.                 /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  6047.                 or ((select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  6048.                     from sysprotects p
  6049.                     /* outer join to correlate with all rows in sysobjects */
  6050.                     where p.id =* o.id
  6051.                         /* get rows for public,current user,user's group */
  6052.                         and (p.uid = 0 or p.uid = user_id() or p.uid =* u.gid)
  6053.                         /* check for SELECT,EXECUTE privilege */
  6054.                         and (action in (193,224)))&1     /* more magic...normalize GRANT */
  6055.                     ) = 1    /* final magic...compare Grants      */
  6056.             )
  6057.         order by 4, 1, 2, 3
  6058.     end
  6059. go
  6060.  
  6061. if (charindex('7.00', @@version) > 0)
  6062.     drop procedure sp_tables
  6063. else
  6064. begin
  6065.     print ''
  6066.     print ''
  6067.     print 'Warning:'
  6068.     print 'you are installing the stored procedures '
  6069.     print 'on a pre 7.0 SQL Server.'
  6070.     print 'Ignore the following errors.'
  6071. end
  6072. go
  6073.  
  6074. /*    Procedure for 7.00 server */
  6075. create procedure sp_tables(
  6076.                @table_name        varchar(384)    = null,
  6077.                @table_owner     varchar(384)    = null,
  6078.                @table_qualifier sysname    = null,
  6079.                @table_type        varchar(100) = null)
  6080. as
  6081.     declare @type1 varchar(3)
  6082.     declare @tableindex int
  6083.  
  6084.  
  6085.     /* Special feature #1:    enumerate databases when owner and name
  6086.          are blank but qualifier is explicitly '%'.  */
  6087.     if @table_qualifier = '%' and
  6088.         @table_owner = '' and
  6089.         @table_name = ''
  6090.     begin    /* If enumerating databases */
  6091.         select
  6092.             TABLE_QUALIFIER = convert(sysname,d.name),
  6093.             TABLE_OWNER = convert(sysname,null),
  6094.             TABLE_NAME = convert(sysname,null),
  6095.             TABLE_TYPE = convert(sysname,null),
  6096.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  6097.         from master.dbo.sysdatabases d
  6098.         where d.name <> 'model'    /* eliminate MODEL database */
  6099.         order by 1
  6100.     end
  6101.  
  6102.     /* Special feature #2:    enumerate owners when qualifier and name
  6103.          are blank but owner is explicitly '%'.  */
  6104.     else if @table_qualifier = '' and
  6105.         @table_owner = '%' and
  6106.         @table_name = ''
  6107.     begin    /* If enumerating owners */
  6108.         select distinct
  6109.             TABLE_QUALIFIER = convert(sysname,null),
  6110.             TABLE_OWNER = convert(sysname,user_name(uid)),
  6111.             TABLE_NAME = convert(sysname,null),
  6112.             TABLE_TYPE = convert(sysname,null),
  6113.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  6114.         from sysobjects
  6115.         order by 2
  6116.     end
  6117.  
  6118.     /* Special feature #3:    enumerate table types when qualifier, owner and
  6119.          name are blank but table type is explicitly '%'.    */
  6120.     else if @table_qualifier = '' and
  6121.         @table_owner = '' and
  6122.         @table_name = '' and
  6123.         @table_type = '%'
  6124.     begin    /* If enumerating table types */
  6125.         select
  6126.             TABLE_QUALIFIER = convert(sysname,null),
  6127.             TABLE_OWNER = convert(sysname,null),
  6128.             TABLE_NAME = convert(sysname,null),
  6129.             TABLE_TYPE = convert(sysname,rtrim(substring('SYSTEM TABLETABLE       VIEW',(colid-1)*12+1,12))),
  6130.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  6131.         from sysobjects o, syscolumns c
  6132.         where o.id=c.id and o.name='sysusers' and colid<=3
  6133.     end
  6134.  
  6135.     else
  6136.     begin /* end of special features - do normal processing */
  6137.         if @table_qualifier is not null
  6138.         begin
  6139.             if db_name() <> @table_qualifier
  6140.             begin
  6141.                 if @table_qualifier = ''
  6142.                 begin  /* If empty qualifier supplied */
  6143.                     /* Force an empty result set */
  6144.                     select @table_name = ''
  6145.                     select @table_owner = ''
  6146.                 end
  6147.                 else
  6148.                 begin    /* If qualifier doesn't match current database */
  6149.                     raiserror 20001 'Table qualifier must be name of current database'
  6150.                     return
  6151.                 end
  6152.             end
  6153.         end
  6154.         if @table_type is null
  6155.         begin    /* Select all ODBC supported table types */
  6156.             select @type1 = 'SUV'
  6157.         end
  6158.         else
  6159.         begin
  6160.             /*    TableType is case sensitive if CS server */
  6161.             select @type1 = null
  6162.             if (charindex('''SYSTEM TABLE''',@table_type) <> 0)
  6163.                 select @type1 = @type1 + 'S'    /* Add System Tables */
  6164.             if (charindex('''TABLE''',@table_type) <> 0)
  6165.                 select @type1 = @type1 + 'U'    /* Add User Tables */
  6166.             if (charindex('''VIEW''',@table_type) <> 0)
  6167.                 select @type1 = @type1 + 'V'    /* Add Views */
  6168.         end
  6169.         if @table_name is null
  6170.         begin    /*    If table name not supplied, match all */
  6171.             select @table_name = '%'
  6172.         end
  6173.         else
  6174.         begin
  6175.             if (@table_owner is null) and (charindex('%', @table_name) = 0)
  6176.             begin    /* If owner not specified and table is specified */
  6177.                 if exists (select * from sysobjects
  6178.                     where uid = user_id()
  6179.                     and name = @table_name
  6180.                     and (type = 'U' or type = 'V' or type = 'S'))
  6181.                 begin    /* Override supplied owner w/owner of table */
  6182.                     select @table_owner = user_name()
  6183.                 end
  6184.             end
  6185.         end
  6186.         if @table_owner is null /* If no owner supplied, force wildcard */
  6187.             select @table_owner = '%'
  6188.         select
  6189.             TABLE_QUALIFIER = convert(sysname,db_name()),
  6190.             TABLE_OWNER = convert(sysname,user_name(o.uid)),
  6191.             TABLE_NAME = convert(sysname,o.name),    /* make nullable */
  6192.             TABLE_TYPE = convert(sysname,rtrim(
  6193.                 substring('SYSTEM TABLE            TABLE       VIEW       ',
  6194.                     (ascii(o.type)-83)*12+1,12))),    /* 'S'=0,'U'=2,'V'=3 */
  6195.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  6196.         from sysobjects o
  6197.         where
  6198.             o.name like @table_name
  6199.             and user_name(o.uid) like @table_owner
  6200.             and (o.type in ('U','S') or
  6201.              (o.type = 'V' and ObjectProperty (o.id, 'IsMSShipped') = 0))
  6202.             and charindex(substring(o.type,1,1),@type1) <> 0 /* Only desired types */
  6203.             and permissions (o.id)&4096 <> 0
  6204.         order by 4, 1, 2, 3
  6205.     end
  6206. go
  6207.  
  6208.  
  6209. grant execute on sp_tables to public
  6210. go
  6211.  
  6212. dump tran master with no_log
  6213. go
  6214.  
  6215. if (charindex('6.50', @@version) = 0 and
  6216.     charindex('7.00', @@version) = 0)
  6217. begin
  6218.     print ''
  6219.     print ''
  6220.     print 'Warning:'
  6221.     print 'you are installing the stored procedures '
  6222.     print 'on a pre 6.50 SQL Server.'
  6223.     print 'Ignore the following errors.'
  6224. end
  6225.  
  6226. print 'creating sp_ddopen'
  6227. go
  6228.  
  6229. /*    Procedure for pre-7.00 server */
  6230. create procedure sp_ddopen(
  6231.                @handle            int output,
  6232.                @procname        sysname,
  6233.                @scrollopt        int output,
  6234.                @ccopt            int output,
  6235.                @rows            int output,
  6236.                @p1                varchar(255) = null,
  6237.                @p2                varchar(255) = null,
  6238.                @p3                varchar(255) = null,
  6239.                @p4                varchar(255) = null,
  6240.                @p5                varchar(255) = null,
  6241.                @p6                varchar(255) = null,
  6242.                @p7                int = null,
  6243.                @ODBCVer         int = 2)
  6244. as
  6245.     declare @ret int
  6246.  
  6247.     if @procname = 'sp_column_privileges'
  6248.     begin
  6249.         create table #spcolpriv (
  6250.             TABLE_QUALIFIER varchar(32) null,
  6251.             TABLE_OWNER varchar(32) null,
  6252.             TABLE_NAME varchar(32)    not null,
  6253.             COLUMN_NAME varchar(32) not null,
  6254.             GRANTOR varchar(32) null,
  6255.             GRANTEE varchar(32) not null,
  6256.             PRIVILEGE varchar(32) not null,
  6257.             IS_GRANTABLE varchar(3) null
  6258.             )
  6259.         insert into #spcolpriv exec sp_column_privileges @p1,@p2,@p3,@p4
  6260.         exec @ret = sp_cursoropen @handle output,
  6261.             'select * from #spcolpriv',
  6262.             @scrollopt output, @ccopt output, @rows output
  6263.         drop table #spcolpriv
  6264.     end
  6265.     else if @procname = 'sp_columns'
  6266.     begin
  6267.         create table #spcolumns (
  6268.             TABLE_QUALIFIER varchar(32) null,
  6269.             TABLE_OWNER varchar(32) null,
  6270.             TABLE_NAME varchar(32)    not null,
  6271.             COLUMN_NAME varchar(32) not null,
  6272.             DATA_TYPE smallint not null,
  6273.             TYPE_NAME varchar(32) not null,
  6274.             "PRECISION" int null,
  6275.             LENGTH int null,
  6276.             SCALE smallint null,
  6277.             RADIX smallint null,
  6278.             NULLABLE smallint not null,
  6279.             REMARKS varchar(254) null,
  6280.             COLUMN_DEF varchar(254) null,
  6281.             SQL_DATA_TYPE smallint not null,
  6282.             SQL_DATETIME_SUB smallint null,
  6283.             CHAR_OCTET_LENGTH int null,
  6284.             ORDINAL_POSITION int not null,
  6285.             IS_NULLABLE varchar(254) null,
  6286.             SS_DATA_TYPE tinyint null
  6287.             )
  6288.         insert into #spcolumns exec sp_columns @p1,@p2,@p3,@p4,@ODBCVer
  6289.         exec @ret = sp_cursoropen @handle output,
  6290.             'select * from #spcolumns',
  6291.             @scrollopt output, @ccopt output, @rows output
  6292.         drop table #spcolumns
  6293.     end
  6294.     else if @procname = 'sp_datatype_info'
  6295.     begin
  6296.         create table #spdatatypeinfo (
  6297.             TYPE_NAME            varchar(32)  not null,
  6298.             DATA_TYPE            smallint not null,
  6299.             "PRECISION"            int null,
  6300.             LITERAL_PREFIX        varchar(32)    null,
  6301.             LITERAL_SUFFIX        varchar(32)    null,
  6302.             CREATE_PARAMS        varchar(32)    null,
  6303.             NULLABLE            smallint   not null,
  6304.             CASE_SENSITIVE        smallint   not null,
  6305.             SEARCHABLE            smallint   not null,
  6306.             UNSIGNED_ATTRIBUTE    smallint   null,
  6307.             MONEY    smallint    not null,
  6308.             AUTO_INCREMENT        smallint    null,
  6309.             LOCAL_TYPE_NAME     varchar(32) null,
  6310.             MINIMUM_SCALE        smallint     null,
  6311.             MAXIMUM_SCALE        smallint   null,
  6312.             SQL_DATA_TYPE        smallint      not null,
  6313.             SQL_DATETIME_SUB    smallint   null,
  6314.             NUM_PREC_RADIX        int     null,
  6315.             INTERVAL_PRECISION    smallint    NULL,
  6316.             USERTYPE            smallint not null)
  6317.         insert into #spdatatypeinfo exec sp_datatype_info @p7,@ODBCVer
  6318.         exec @ret = sp_cursoropen @handle output,
  6319.             'select * from #spdatatypeinfo',
  6320.             @scrollopt output, @ccopt output, @rows output
  6321.         drop table #spdatatypeinfo
  6322.     end
  6323.     else if @procname = 'sp_fkeys'
  6324.     begin
  6325.         create table #spfkeys (
  6326.             PKTABLE_QUALIFIER varchar(32)     null,
  6327.             PKTABLE_OWNER varchar(32)    null,
  6328.             PKTABLE_NAME varchar(32)  not null,
  6329.             PKCOLUMN_NAME varchar(32)    not null,
  6330.             FKTABLE_QUALIFIER varchar(32)    null,
  6331.             FKTABLE_OWNER varchar(32)    null,
  6332.             FKTABLE_NAME varchar(32)  not null,
  6333.             FKCOLUMN_NAME varchar(32)    not null,
  6334.             KEY_SEQ smallint not null,
  6335.             UPDATE_RULE smallint null,
  6336.             DELETE_RULE smallint null,
  6337.             FK_NAME varchar(32) null,
  6338.             PK_NAME varchar(32) null
  6339.             )
  6340.         insert into #spfkeys exec sp_fkeys @p1,@p2,@p3,@p4,@p5,@p6
  6341.         exec @ret = sp_cursoropen @handle output,
  6342.             'select * from #spfkeys',
  6343.             @scrollopt output, @ccopt output, @rows output
  6344.         drop table #spfkeys
  6345.     end
  6346.     else if @procname = 'sp_pkeys'
  6347.     begin
  6348.         create table #sppkeys (
  6349.             TABLE_QUALIFIER varchar(32)   null,
  6350.             TABLE_OWNER varchar(32)   null,
  6351.             TABLE_NAME varchar(32)    not null,
  6352.             COLUMN_NAME varchar(32)  not null,
  6353.             KEY_SEQ smallint not null,
  6354.             PK_NAME varchar(32) null
  6355.             )
  6356.         insert into #sppkeys exec sp_pkeys @p1,@p2,@p3
  6357.         exec @ret = sp_cursoropen @handle output,
  6358.             'select * from #sppkeys',
  6359.             @scrollopt output, @ccopt output, @rows output
  6360.         drop table #sppkeys
  6361.     end
  6362.     else if @procname = 'sp_special_columns'
  6363.     begin
  6364.         create table #spspeccol (
  6365.             SCOPE smallint null,
  6366.             COLUMN_NAME varchar(32) not null,
  6367.             DATA_TYPE smallint not null,
  6368.             TYPE_NAME varchar(32) not null,
  6369.             "PRECISION" int null,
  6370.             LENGTH int null,
  6371.             SCALE smallint null,
  6372.             PSEUDO_COLUMN smallint null
  6373.             )
  6374.         insert into #spspeccol exec sp_special_columns @p1,@p2,@p3,@p4,@p5,@p6,@ODBCVer
  6375.         exec @ret = sp_cursoropen @handle output,
  6376.             'select * from #spspeccol',
  6377.             @scrollopt output, @ccopt output, @rows output
  6378.         drop table #spspeccol
  6379.     end
  6380.     else if @procname = 'sp_sproc_columns'
  6381.     begin
  6382.         create table #spproccol (
  6383.             PROCEDURE_QUALIFIER varchar(32)  null,
  6384.             PROCEDURE_OWNER varchar(32)  null,
  6385.             PROCEDURE_NAME varchar(32)    not null,
  6386.             COLUMN_NAME varchar(32) not null,
  6387.             COLUMN_TYPE smallint not null,
  6388.             DATA_TYPE smallint not null,
  6389.             TYPE_NAME varchar(32) not null,
  6390.             "PRECISION" int null,
  6391.             LENGTH int null,
  6392.             SCALE smallint null,
  6393.             RADIX smallint null,
  6394.             NULLABLE smallint not null,
  6395.             REMARKS varchar(254) null,
  6396.             COLUMN_DEF varchar(254) null,
  6397.             SQL_DATA_TYPE smallint not null,
  6398.             SQL_DATETIME_SUB smallint null,
  6399.             CHAR_OCTET_LENGTH int null,
  6400.             ORDINAL_POSITION int not null,
  6401.             IS_NULLABLE varchar(254) null,
  6402.             SS_DATA_TYPE tinyint null
  6403.             )
  6404.         insert into #spproccol exec sp_sproc_columns @p1,@p2,@p3,@p4,@ODBCVer
  6405.         exec @ret = sp_cursoropen @handle output,
  6406.             'select * from #spproccol',
  6407.             @scrollopt output, @ccopt output, @rows output
  6408.         drop table #spproccol
  6409.     end
  6410.     else if @procname = 'sp_statistics'
  6411.     begin
  6412.         create table #spstatistics (
  6413.             TABLE_QUALIFIER varchar(32)   null,
  6414.             TABLE_OWNER varchar(32)   null,
  6415.             TABLE_NAME varchar(32)    not null,
  6416.             NON_UNIQUE smallint null,
  6417.             INDEX_QUALIFIER varchar(32) null,
  6418.             INDEX_NAME varchar(32)    null,
  6419.             TYPE smallint not null,
  6420.             SEQ_IN_INDEX smallint null,
  6421.             COLUMN_NAME varchar(32) null,
  6422.             COLLATION char(1) null,
  6423.             CARDINALITY int null,
  6424.             PAGES int null,
  6425.             FILTER_CONDITION varchar(128) null
  6426.             )
  6427.         insert into #spstatistics exec sp_statistics @p1,@p2,@p3,@p4,@p5,@p6
  6428.         exec @ret = sp_cursoropen @handle output,
  6429.             'select * from #spstatistics',
  6430.             @scrollopt output, @ccopt output, @rows output
  6431.         drop table #spstatistics
  6432.     end
  6433.     else if @procname = 'sp_stored_procedures'
  6434.     begin
  6435.         create table #spprocedures (
  6436.             PROCEDURE_QUALIFIER varchar(32)  null,
  6437.             PROCEDURE_OWNER varchar(32)  null,
  6438.             PROCEDURE_NAME varchar(32)    not null,
  6439.             NUM_INPUT_PARAMS int null,
  6440.             NUM_OUTPUT_PARAMS int null,
  6441.             NUM_RESULT_SETS int null,
  6442.             REMARKS varchar(254) null,
  6443.             PROCEDURE_TYPE smallint null
  6444.             )
  6445.         insert into #spprocedures exec sp_stored_procedures @p1,@p2,@p3
  6446.         exec @ret = sp_cursoropen @handle output,
  6447.             'select * from #spprocedures',
  6448.             @scrollopt output, @ccopt output, @rows output
  6449.         drop table #spprocedures
  6450.     end
  6451.     else if @procname = 'sp_table_privileges'
  6452.     begin
  6453.         create table #sptabpriv (
  6454.             TABLE_QUALIFIER varchar(32) null,
  6455.             TABLE_OWNER varchar(32) null,
  6456.             TABLE_NAME varchar(32)    not null,
  6457.             GRANTOR varchar(32) null,
  6458.             GRANTEE varchar(32) not null,
  6459.             PRIVILEGE varchar(32) not null,
  6460.             IS_GRANTABLE varchar(3) null
  6461.             )
  6462.         insert into #sptabpriv exec sp_table_privileges @p1,@p2,@p3
  6463.         exec @ret = sp_cursoropen @handle output,
  6464.             'select * from #sptabpriv',
  6465.             @scrollopt output, @ccopt output, @rows output
  6466.         drop table #sptabpriv
  6467.     end
  6468.     else if @procname = 'sp_tables'
  6469.     begin
  6470.         create table #sptables (
  6471.             TABLE_QUALIFIER varchar(32) null,
  6472.             TABLE_OWNER varchar(32) null,
  6473.             TABLE_NAME varchar(32)    null,
  6474.             TABLE_TYPE     varchar(32) null,
  6475.             REMARKS varchar(254) null)
  6476.         insert into #sptables exec sp_tables @p1,@p2,@p3,@p4
  6477.         exec @ret = sp_cursoropen @handle output,
  6478.             'select * from #sptables',
  6479.             @scrollopt output, @ccopt output, @rows output
  6480.         drop table #sptables
  6481.     end
  6482.     select @ret = isnull(@ret,0)
  6483.     return isnull(@ret,0)
  6484. go
  6485.  
  6486. if (charindex('7.00', @@version) > 0)
  6487.     drop procedure sp_ddopen
  6488. else
  6489. begin
  6490.     print ''
  6491.     print ''
  6492.     print 'Warning:'
  6493.     print 'you are installing the stored procedures '
  6494.     print 'on a pre 7.0 SQL Server.'
  6495.     print 'Ignore the following errors.'
  6496. end
  6497. go
  6498.  
  6499. /*    Procedure for 7.00 server */
  6500. create procedure sp_ddopen(
  6501.                @handle            int output,
  6502.                @procname        sysname,
  6503.                @scrollopt        int output,
  6504.                @ccopt            int output,
  6505.                @rows            int output,
  6506.                @p1                varchar(255) = null,
  6507.                @p2                varchar(255) = null,
  6508.                @p3                varchar(255) = null,
  6509.                @p4                varchar(255) = null,
  6510.                @p5                varchar(255) = null,
  6511.                @p6                varchar(255) = null,
  6512.                @p7                int = null,
  6513.                @ODBCVer         int = 2)
  6514. as
  6515.     declare @ret int
  6516.  
  6517.     if @procname = 'sp_column_privileges'
  6518.     begin
  6519.         create table #spcolpriv (
  6520.             TABLE_QUALIFIER sysname null,
  6521.             TABLE_OWNER sysname null,
  6522.             TABLE_NAME sysname not null,
  6523.             COLUMN_NAME sysname not null,
  6524.             GRANTOR sysname null,
  6525.             GRANTEE sysname not null,
  6526.             PRIVILEGE varchar(32) not null,
  6527.             IS_GRANTABLE varchar(3) null
  6528.             )
  6529.         insert into #spcolpriv exec sp_column_privileges @p1,@p2,@p3,@p4
  6530.         exec @ret = sp_cursoropen @handle output,
  6531.             'select * from #spcolpriv',
  6532.             @scrollopt output, @ccopt output, @rows output
  6533.         drop table #spcolpriv
  6534.     end
  6535.     else if @procname = 'sp_columns'
  6536.     begin
  6537.         create table #spcolumns (
  6538.             TABLE_QUALIFIER sysname null,
  6539.             TABLE_OWNER sysname null,
  6540.             TABLE_NAME sysname not null,
  6541.             COLUMN_NAME sysname not null,
  6542.             DATA_TYPE smallint not null,
  6543.             TYPE_NAME sysname not null,
  6544.             "PRECISION" int null,
  6545.             LENGTH int null,
  6546.             SCALE smallint null,
  6547.             RADIX smallint null,
  6548.             NULLABLE smallint not null,
  6549.             REMARKS varchar(254) null,
  6550.             COLUMN_DEF varchar(254) null,
  6551.             SQL_DATA_TYPE smallint not null,
  6552.             SQL_DATETIME_SUB smallint null,
  6553.             CHAR_OCTET_LENGTH int null,
  6554.             ORDINAL_POSITION int not null,
  6555.             IS_NULLABLE varchar(254) null,
  6556.             SS_DATA_TYPE tinyint null
  6557.             )
  6558.         insert into #spcolumns exec sp_columns @p1,@p2,@p3,@p4,@ODBCVer
  6559.         exec @ret = sp_cursoropen @handle output,
  6560.             'select * from #spcolumns',
  6561.             @scrollopt output, @ccopt output, @rows output
  6562.         drop table #spcolumns
  6563.     end
  6564.     else if @procname = 'sp_datatype_info'
  6565.     begin
  6566.         create table #spdatatypeinfo (
  6567.             TYPE_NAME            sysname  not null,
  6568.             DATA_TYPE            smallint not null,
  6569.             "PRECISION"            int null,
  6570.             LITERAL_PREFIX        varchar(32)    null,
  6571.             LITERAL_SUFFIX        varchar(32)    null,
  6572.             CREATE_PARAMS        varchar(32)    null,
  6573.             NULLABLE            smallint   not null,
  6574.             CASE_SENSITIVE        smallint   not null,
  6575.             SEARCHABLE            smallint   not null,
  6576.             UNSIGNED_ATTRIBUTE    smallint   null,
  6577.             MONEY    smallint    not null,
  6578.             AUTO_INCREMENT        smallint    null,
  6579.             LOCAL_TYPE_NAME     varchar(128) null,
  6580.             MINIMUM_SCALE        smallint     null,
  6581.             MAXIMUM_SCALE        smallint   null,
  6582.             SQL_DATA_TYPE        smallint      not null,
  6583.             SQL_DATETIME_SUB    smallint   null,
  6584.             NUM_PREC_RADIX        int     null,
  6585.             INTERVAL_PRECISION    smallint    NULL,
  6586.             USERTYPE            smallint not null)
  6587.         insert into #spdatatypeinfo exec sp_datatype_info @p7,@ODBCVer
  6588.         exec @ret = sp_cursoropen @handle output,
  6589.             'select * from #spdatatypeinfo',
  6590.             @scrollopt output, @ccopt output, @rows output
  6591.         drop table #spdatatypeinfo
  6592.     end
  6593.     else if @procname = 'sp_fkeys'
  6594.     begin
  6595.         create table #spfkeys (
  6596.             PKTABLE_QUALIFIER sysname     null,
  6597.             PKTABLE_OWNER sysname    null,
  6598.             PKTABLE_NAME sysname  not null,
  6599.             PKCOLUMN_NAME sysname  not null,
  6600.             FKTABLE_QUALIFIER sysname    null,
  6601.             FKTABLE_OWNER sysname    null,
  6602.             FKTABLE_NAME sysname  not null,
  6603.             FKCOLUMN_NAME sysname  not null,
  6604.             KEY_SEQ smallint not null,
  6605.             UPDATE_RULE smallint null,
  6606.             DELETE_RULE smallint null,
  6607.             FK_NAME sysname null,
  6608.             PK_NAME sysname null
  6609.             )
  6610.         insert into #spfkeys exec sp_fkeys @p1,@p2,@p3,@p4,@p5,@p6
  6611.         exec @ret = sp_cursoropen @handle output,
  6612.             'select * from #spfkeys',
  6613.             @scrollopt output, @ccopt output, @rows output
  6614.         drop table #spfkeys
  6615.     end
  6616.     else if @procname = 'sp_pkeys'
  6617.     begin
  6618.         create table #sppkeys (
  6619.             TABLE_QUALIFIER sysname   null,
  6620.             TABLE_OWNER sysname   null,
  6621.             TABLE_NAME sysname    not null,
  6622.             COLUMN_NAME sysname  not null,
  6623.             KEY_SEQ smallint not null,
  6624.             PK_NAME sysname null
  6625.             )
  6626.         insert into #sppkeys exec sp_pkeys @p1,@p2,@p3
  6627.         exec @ret = sp_cursoropen @handle output,
  6628.             'select * from #sppkeys',
  6629.             @scrollopt output, @ccopt output, @rows output
  6630.         drop table #sppkeys
  6631.     end
  6632.     else if @procname = 'sp_special_columns'
  6633.     begin
  6634.         create table #spspeccol (
  6635.             SCOPE smallint null,
  6636.             COLUMN_NAME sysname not null,
  6637.             DATA_TYPE smallint not null,
  6638.             TYPE_NAME sysname not null,
  6639.             "PRECISION" int null,
  6640.             LENGTH int null,
  6641.             SCALE smallint null,
  6642.             PSEUDO_COLUMN smallint null
  6643.             )
  6644.         insert into #spspeccol exec sp_special_columns @p1,@p2,@p3,@p4,@p5,@p6,@ODBCVer
  6645.         exec @ret = sp_cursoropen @handle output,
  6646.             'select * from #spspeccol',
  6647.             @scrollopt output, @ccopt output, @rows output
  6648.         drop table #spspeccol
  6649.     end
  6650.     else if @procname = 'sp_sproc_columns'
  6651.     begin
  6652.         create table #spproccol (
  6653.             PROCEDURE_QUALIFIER sysname  null,
  6654.             PROCEDURE_OWNER sysname  null,
  6655.             PROCEDURE_NAME sysname not null,
  6656.             COLUMN_NAME sysname not null,
  6657.             COLUMN_TYPE smallint not null,
  6658.             DATA_TYPE smallint not null,
  6659.             TYPE_NAME sysname not null,
  6660.             "PRECISION" int null,
  6661.             LENGTH int null,
  6662.             SCALE smallint null,
  6663.             RADIX smallint null,
  6664.             NULLABLE smallint not null,
  6665.             REMARKS varchar(254) null,
  6666.             COLUMN_DEF varchar(254) null,
  6667.             SQL_DATA_TYPE smallint not null,
  6668.             SQL_DATETIME_SUB smallint null,
  6669.             CHAR_OCTET_LENGTH int null,
  6670.             ORDINAL_POSITION int not null,
  6671.             IS_NULLABLE varchar(254) null,
  6672.             SS_DATA_TYPE tinyint null
  6673.             )
  6674.         insert into #spproccol exec sp_sproc_columns @p1,@p2,@p3,@p4,@ODBCVer
  6675.         exec @ret = sp_cursoropen @handle output,
  6676.             'select * from #spproccol',
  6677.             @scrollopt output, @ccopt output, @rows output
  6678.         drop table #spproccol
  6679.     end
  6680.     else if @procname = 'sp_statistics'
  6681.     begin
  6682.         create table #spstatistics (
  6683.             TABLE_QUALIFIER sysname   null,
  6684.             TABLE_OWNER sysname   null,
  6685.             TABLE_NAME sysname    not null,
  6686.             NON_UNIQUE smallint null,
  6687.             INDEX_QUALIFIER sysname null,
  6688.             INDEX_NAME sysname null,
  6689.             TYPE smallint not null,
  6690.             SEQ_IN_INDEX smallint null,
  6691.             COLUMN_NAME sysname null,
  6692.             COLLATION char(1) null,
  6693.             CARDINALITY int null,
  6694.             PAGES int null,
  6695.             FILTER_CONDITION varchar(128) null
  6696.             )
  6697.         insert into #spstatistics exec sp_statistics @p1,@p2,@p3,@p4,@p5,@p6
  6698.         exec @ret = sp_cursoropen @handle output,
  6699.             'select * from #spstatistics',
  6700.             @scrollopt output, @ccopt output, @rows output
  6701.         drop table #spstatistics
  6702.     end
  6703.     else if @procname = 'sp_stored_procedures'
  6704.     begin
  6705.         create table #spprocedures (
  6706.             PROCEDURE_QUALIFIER sysname  null,
  6707.             PROCEDURE_OWNER sysname  null,
  6708.             PROCEDURE_NAME sysname not null,
  6709.             NUM_INPUT_PARAMS int null,
  6710.             NUM_OUTPUT_PARAMS int null,
  6711.             NUM_RESULT_SETS int null,
  6712.             REMARKS varchar(254) null,
  6713.             PROCEDURE_TYPE smallint null
  6714.             )
  6715.         insert into #spprocedures exec sp_stored_procedures @p1,@p2,@p3
  6716.         exec @ret = sp_cursoropen @handle output,
  6717.             'select * from #spprocedures',
  6718.             @scrollopt output, @ccopt output, @rows output
  6719.         drop table #spprocedures
  6720.     end
  6721.     else if @procname = 'sp_table_privileges'
  6722.     begin
  6723.         create table #sptabpriv (
  6724.             TABLE_QUALIFIER sysname null,
  6725.             TABLE_OWNER sysname null,
  6726.             TABLE_NAME sysname not null,
  6727.             GRANTOR sysname null,
  6728.             GRANTEE sysname not null,
  6729.             PRIVILEGE varchar(32) not null,
  6730.             IS_GRANTABLE varchar(3) null
  6731.             )
  6732.         insert into #sptabpriv exec sp_table_privileges @p1,@p2,@p3
  6733.         exec @ret = sp_cursoropen @handle output,
  6734.             'select * from #sptabpriv',
  6735.             @scrollopt output, @ccopt output, @rows output
  6736.         drop table #sptabpriv
  6737.     end
  6738.     else if @procname = 'sp_tables'
  6739.     begin
  6740.         create table #sptables (
  6741.             TABLE_QUALIFIER sysname null,
  6742.             TABLE_OWNER sysname null,
  6743.             TABLE_NAME sysname null,
  6744.             TABLE_TYPE    sysname null,
  6745.             REMARKS varchar(254) null)
  6746.         insert into #sptables exec sp_tables @p1,@p2,@p3,@p4
  6747.         exec @ret = sp_cursoropen @handle output,
  6748.             'select * from #sptables',
  6749.             @scrollopt output, @ccopt output, @rows output
  6750.         drop table #sptables
  6751.     end
  6752.     select @ret = isnull(@ret,0)
  6753.     return isnull(@ret,0)
  6754. go
  6755.  
  6756. grant execute on sp_ddopen to public
  6757. go
  6758.  
  6759. dump tran master with no_log
  6760. go
  6761.  
  6762. print 'creating spt_provider_types'
  6763. go
  6764.  
  6765. create table spt_provider_types
  6766.     (
  6767.     ss_dtype        tinyint     not null,
  6768.     fixlen            int         null,        /* datatype len for variable, else null */
  6769.     type_name        sysname    not null,
  6770.     oledb_data_type        smallint    not null,
  6771.     best_match        bit        not null,
  6772.     is_nullable        bit        not null,
  6773.     case_sensitive        bit        not null,
  6774.     fixed_prec_scale    bit        not null,
  6775.     is_long            bit        not null,
  6776.     auto_unique_value    tinyint        not null,
  6777.     data_precision        int         null,
  6778.     numeric_scale        smallint    null,        /* min scale if 6.0 */
  6779.     column_size        int         null,
  6780.     literal_prefix        varchar(32)    null,
  6781.     literal_suffix        varchar(32)     null,
  6782.     searchable        int        not null,
  6783.     unsigned_attribute    tinyint        null,
  6784.     local_type_name     sysname    null
  6785.     )
  6786. go
  6787.  
  6788. grant select on spt_provider_types to public
  6789. go
  6790.  
  6791. /*
  6792. ** Insert the spt_provider_types rows for DBTYTPE_STR data types
  6793. */
  6794. begin tran
  6795.  
  6796. /* Get case sensitivity */
  6797. declare @case_sensitive bit
  6798. select @case_sensitive = case when 'a' <> 'A' then 1 else 0 end
  6799. /* Local Char */
  6800. insert into spt_provider_types values
  6801.     (
  6802.     47 /*SQLCHARACTER*/,        /* ss_dtype */
  6803.     0,                /* fixlen */
  6804.     'char',                /* type_name */
  6805.     129 /*DBTYPE_STR*/,        /* oledb_data_type */
  6806.     0,                /* best_match */
  6807.     1,                /* is_nullable */
  6808.     @case_sensitive,        /* case_sensitive */
  6809.     0,                /* fixed_prec_scale */
  6810.     0,                /* is_long */
  6811.     0,                /* auto_unique_value */
  6812.     null,                /* data_precision */
  6813.     null,                /* numeric_scale */
  6814.     null,                /* column_size */
  6815.     '''',                /* literal_prefix */
  6816.     '''',                /* literal_suffix */
  6817.     4 /*DB_SEARCHABLE*/,        /* searchable */
  6818.     null,                /* unsigned_attribute */
  6819.     'char'                /* local_type_name */
  6820.     )
  6821.  
  6822. /* Local Varchar */
  6823. insert into spt_provider_types values
  6824.     (
  6825.     39 /*SQLVARCHAR*/,        /* ss_dtype */
  6826.     null,                /* fixlen */
  6827.     'varchar',            /* type_name */
  6828.     129 /*DBTYPE_STR*/,        /* oledb_data_type */
  6829.     0,                /* best_match */
  6830.     1,                /* is_nullable */
  6831.     @case_sensitive,        /* case_sensitive */
  6832.     0,                /* fixed_prec_scale */
  6833.     0,                /* is_long */
  6834.     0,                /* auto_unique_value */
  6835.     null,                /* data_precision */
  6836.     null,                /* numeric_scale */
  6837.     null,                /* column_size */
  6838.     '''',                /* literal_prefix */
  6839.     '''',                /* literal_suffix */
  6840.     4 /*DB_SEARCHABLE*/,        /* searchable */
  6841.     null,                /* unsigned_attribute */
  6842.     'varchar'            /* local_type_name */
  6843.     )
  6844.  
  6845. /* Local Text */
  6846. insert into spt_provider_types values
  6847.     (
  6848.     35  /*SQLTEXT*/,        /* ss_dtype */
  6849.     null,                /* fixlen */
  6850.     'text',                /* type_name */
  6851.     129 /*DBTYPE_STR*/,        /* oledb_data_type */
  6852.     0,                /* best_match */
  6853.     1,                /* is_nullable */
  6854.     @case_sensitive,        /* case_sensitive */
  6855.     0,                /* fixed_prec_scale */
  6856.     1,                /* is_long */
  6857.     0,                /* auto_unique_value */
  6858.     null,                /* data_precision */
  6859.     null,                /* numeric_scale */
  6860.     2147483647,            /* column_size */
  6861.     '''',                /* literal_prefix */
  6862.     '''',                /* literal_suffix */
  6863.     2 /*DB_LIKE_ONLY*/,        /* searchable */
  6864.     null,                /* unsigned_attribute */
  6865.     'text'                /* local_type_name */
  6866.     )
  6867. commit tran
  6868. go
  6869.  
  6870. /*
  6871. ** Insert the spt_provider_types rows for DBTYTPE_BYTES data types
  6872. */
  6873. begin tran
  6874. /* Local Binary */
  6875. insert into spt_provider_types values
  6876.     (
  6877.     45 /*SQLBINARY*/,        /* ss_dtype */
  6878.     0,                /* fixlen */
  6879.     'binary',            /* type_name */
  6880.     128 /*DBTYPE_BYTES*/,        /* oledb_data_type */
  6881.     0,                /* best_match */
  6882.     1,                /* is_nullable */
  6883.     0,                /* case_sensitive */
  6884.     0,                /* fixed_prec_scale */
  6885.     0,                /* is_long */
  6886.     0,                /* auto_unique_value */
  6887.     null,                /* data_precision */
  6888.     null,                /* numeric_scale */
  6889.     null,                /* column_size */
  6890.     '0x',                /* literal_prefix */
  6891.     null,                /* literal_suffix */
  6892.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  6893.     null,                /* unsigned_attribute */
  6894.     'binary'            /* local_type_name */
  6895.     )
  6896.  
  6897. /* Local Varbinary */
  6898. insert into spt_provider_types values
  6899.     (
  6900.     37 /*SQLVARBINARY*/,        /* ss_dtype */
  6901.     null,                /* fixlen */
  6902.     'varbinary',            /* type_name */
  6903.     128 /*DBTYPE_BYTES*/,        /* oledb_data_type */
  6904.     0,                /* best_match */
  6905.     1,                /* is_nullable */
  6906.     0,                /* case_sensitive */
  6907.     0,                /* fixed_prec_scale */
  6908.     0,                /* is_long */
  6909.     0,                /* auto_unique_value */
  6910.     null,                /* data_precision */
  6911.     null,                /* numeric_scale */
  6912.     null,                /* column_size */
  6913.     '0x',                /* literal_prefix */
  6914.     null,                /* literal_suffix */
  6915.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  6916.     null,                /* unsigned_attribute */
  6917.     'varbinary'            /* local_type_name */
  6918.     )
  6919.  
  6920. /* Local Image */
  6921. insert into spt_provider_types values
  6922.     (
  6923.     34 /*SQLIMAGE*/,        /* ss_dtype */
  6924.     null,                /* fixlen */
  6925.     'image',            /* type_name */
  6926.     128 /*DBTYPE_BYTES*/,        /* oledb_data_type */
  6927.     0,                /* best_match */
  6928.     1,                /* is_nullable */
  6929.     0,                /* case_sensitive */
  6930.     0,                /* fixed_prec_scale */
  6931.     1,                /* is_long */
  6932.     0,                /* auto_unique_value */
  6933.     null,                /* data_precision */
  6934.     null,                /* numeric_scale */
  6935.     2147483647,            /* column_size */
  6936.     '0x',                /* literal_prefix */
  6937.     null,                /* literal_suffix */
  6938.     1 /*DB_UNSEARCHABLE*/,        /* searchable */
  6939.     null,                /* unsigned_attribute */
  6940.     'text'                /* local_type_name */
  6941.     )
  6942.  
  6943. commit tran
  6944. go
  6945.  
  6946. /*
  6947. ** Insert the spt_provider_types rows for DBTYPE_DBTIMESTAMP data types
  6948. */
  6949. begin tran
  6950.  
  6951. /* Local Datetime */
  6952. insert into spt_provider_types values
  6953.     (
  6954.     61 /*SQLDATETIME*/,        /* ss_dtype */
  6955.     8,                /* fixlen */
  6956.     'datetime',            /* type_name */
  6957.     135 /*DBTYPE_DBTIMESTAMP*/,    /* oledb_data_type */
  6958.     1,                /* best_match */
  6959.     1,                /* is_nullable */
  6960.     0,                /* case_sensitive */
  6961.     0,                /* fixed_prec_scale */
  6962.     0,                /* is_long */
  6963.     0,                /* auto_unique_value */
  6964.     23,                /* data_precision */
  6965.     null,                /* numeric_scale */
  6966.     null,                /* column_size */
  6967.     '''',                /* literal_prefix */
  6968.     '''',                /* literal_suffix */
  6969.     4 /*DB_SEARCHABLE*/,        /* searchable */
  6970.     null,                /* unsigned_attribute */
  6971.     'datetime'            /* local_type_name */
  6972.     )
  6973.  
  6974. /* Local Smalldatetime */
  6975. insert into spt_provider_types values
  6976.     (
  6977.     58 /*SQLDATETIM4*/,        /* ss_dtype */
  6978.     4,                /* fixlen */
  6979.     'smalldatetime',        /* type_name */
  6980.     135 /*DBTYPE_DBTIMESTAMP*/,    /* oledb_data_type */
  6981.     0,                /* best_match */
  6982.     1,                /* is_nullable */
  6983.     0,                /* case_sensitive */
  6984.     0,                /* fixed_prec_scale */
  6985.     0,                /* is_long */
  6986.     0,                /* auto_unique_value */
  6987.     16,                /* data_precision */
  6988.     null,                /* numeric_scale */
  6989.     null,                /* column_size */
  6990.     '''',                /* literal_prefix */
  6991.     '''',                /* literal_suffix */
  6992.     4 /*DB_SEARCHABLE*/,        /* searchable */
  6993.     null,                /* unsigned_attribute */
  6994.     'smalldatetime'            /* local_type_name */
  6995.     )
  6996.  
  6997. if (charindex('7.00', @@version) = 0)
  6998.     begin    /*    Add nullable type for non-Sphinx server */
  6999.     
  7000.     /* Local Datetimn */
  7001.     insert into spt_provider_types values
  7002.         (
  7003.         111 /*SQLDATETIMN*/,        /* ss_dtype */
  7004.         4,                /* fixlen */
  7005.         'smalldatetime',        /* type_name */
  7006.         135 /*DBTYPE_DBTIMESTAMP*/,    /* oledb_data_type */
  7007.         0,                /* best_match */
  7008.         1,                /* is_nullable */
  7009.         0,                /* case_sensitive */
  7010.         0,                /* fixed_prec_scale */
  7011.         0,                /* is_long */
  7012.         0,                /* auto_unique_value */
  7013.         16,                /* data_precision */
  7014.         null,                /* numeric_scale */
  7015.         null,                /* column_size */
  7016.         '''',                /* literal_prefix */
  7017.         '''',                /* literal_suffix */
  7018.         4 /*DB_SEARCHABLE*/,        /* searchable */
  7019.         null,                /* unsigned_attribute */
  7020.         'smalldatetime'            /* local_type_name */
  7021.         )    
  7022.     insert into spt_provider_types values
  7023.         (
  7024.         111 /*SQLDATETIMN*/,        /* ss_dtype */
  7025.         8,                /* fixlen */
  7026.         'datetime',            /* type_name */
  7027.         135 /*DBTYPE_DBTIMESTAMP*/,    /* oledb_data_type */
  7028.         0,                /* best_match */
  7029.         1,                /* is_nullable */
  7030.         0,                /* case_sensitive */
  7031.         0,                /* fixed_prec_scale */
  7032.         0,                /* is_long */
  7033.         0,                /* auto_unique_value */
  7034.         23,                /* data_precision */
  7035.         null,                /* numeric_scale */
  7036.         null,                /* column_size */
  7037.         '''',                /* literal_prefix */
  7038.         '''',                /* literal_suffix */
  7039.         4 /*DB_SEARCHABLE*/,        /* searchable */
  7040.         null,                /* unsigned_attribute */
  7041.         'datetime'            /* local_type_name */
  7042.         )
  7043.     end
  7044.  
  7045. commit tran
  7046. go
  7047.  
  7048. /*
  7049. ** Insert the spt_provider_types rows for DBTYPE_CY data types
  7050. */
  7051. begin tran
  7052.  
  7053. /* Local Smallmoney */
  7054. insert into spt_provider_types values
  7055.     (
  7056.     122 /*SQLMONEY4*/,        /* ss_dtype */
  7057.     4,                /* fixlen */
  7058.     'smallmoney',            /* type_name */
  7059.     6 /*DBTYPE_CY*/,        /* oledb_data_type */
  7060.     0,                /* best_match */
  7061.     1,                /* is_nullable */
  7062.     0,                /* case_sensitive */
  7063.     1,                /* fixed_prec_scale */
  7064.     0,                /* is_long */
  7065.     0,                /* auto_unique_value */
  7066.     10,                /* data_precision */
  7067.     null,                /* numeric_scale */
  7068.     null,                /* column_size */
  7069.     '$',                /* literal_prefix */
  7070.     null,                /* literal_suffix */
  7071.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  7072.     0,                /* unsigned_attribute */
  7073.     'smallmoney'            /* local_type_name */
  7074.     )    
  7075.     
  7076. /* Local Money */
  7077. insert into spt_provider_types values
  7078.     (
  7079.     60 /*SQLMONEY*/,        /* ss_dtype */
  7080.     8,                /* fixlen */
  7081.     'money',            /* type_name */
  7082.     6 /*DBTYPE_CY*/,        /* oledb_data_type */
  7083.     1,                /* best_match */
  7084.     1,                /* is_nullable */
  7085.     0,                /* case_sensitive */
  7086.     1,                /* fixed_prec_scale */
  7087.     0,                /* is_long */
  7088.     0,                /* auto_unique_value */
  7089.     19,                /* data_precision */
  7090.     null,                /* numeric_scale */
  7091.     null,                /* column_size */
  7092.     '$',                /* literal_prefix */
  7093.     null,                /* literal_suffix */
  7094.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  7095.     0,                /* unsigned_attribute */
  7096.     'money'                /* local_type_name */
  7097.     )    
  7098.  
  7099. if (charindex('7.00', @@version) = 0)
  7100.     begin    /*    Add nullable type for non-Sphinx server */
  7101.  
  7102.     /* Local Moneyn */
  7103.     insert into spt_provider_types values
  7104.         (
  7105.         110 /*SQLMONEYN*/,    /* ss_dtype */
  7106.         4,            /* fixlen */
  7107.         'smallmoney',        /* type_name */
  7108.         6 /*DBTYPE_CY*/,    /* oledb_data_type */
  7109.         0,            /* best_match */
  7110.         1,            /* is_nullable */
  7111.         0,            /* case_sensitive */
  7112.         1,            /* fixed_prec_scale */
  7113.         0,            /* is_long */
  7114.         0,            /* auto_unique_value */
  7115.         10,            /* data_precision */
  7116.         null,            /* numeric_scale */
  7117.         null,            /* column_size */
  7118.         '$',            /* literal_prefix */
  7119.         null,            /* literal_suffix */
  7120.         3 /*DB_ALL_EXCEPT_LIKE*/, /* searchable */
  7121.         0,            /* unsigned_attribute */
  7122.         'smallmoney'        /* local_type_name */
  7123.         )    
  7124.     insert into spt_provider_types values
  7125.         (
  7126.         110 /*SQLMONEYN*/,    /* ss_dtype */
  7127.         8,            /* fixlen */
  7128.         'money',        /* type_name */
  7129.         6 /*DBTYPE_CY*/,    /* oledb_data_type */
  7130.         0,            /* best_match */
  7131.         1,            /* is_nullable */
  7132.         0,            /* case_sensitive */
  7133.         1,            /* fixed_prec_scale */
  7134.         0,            /* is_long */
  7135.         0,            /* auto_unique_value */
  7136.         19,            /* data_precision */
  7137.         null,            /* numeric_scale */
  7138.         null,            /* column_size */
  7139.         '$',            /* literal_prefix */
  7140.         null,            /* literal_suffix */
  7141.         3 /*DB_ALL_EXCEPT_LIKE*/, /* searchable */
  7142.         null,            /* unsigned_attribute */
  7143.         'money'            /* local_type_name */
  7144.         )    
  7145.     end
  7146.  
  7147. commit tran
  7148. go
  7149.  
  7150. /*
  7151. ** Insert the spt_provider_types rows for the numeric data types
  7152. */
  7153. begin tran
  7154.  
  7155. /* Local Float */
  7156. insert into spt_provider_types values
  7157.     (
  7158.     62 /*SQLFLT8*/,            /* ss_dtype */
  7159.     8,                /* fixlen */
  7160.     'float',            /* type_name */
  7161.     5 /*DBTYPE_R8*/,        /* oledb_data_type */
  7162.     1,                /* best_match */
  7163.     1,                /* is_nullable */
  7164.     0,                /* case_sensitive */
  7165.     0,                /* fixed_prec_scale */
  7166.     0,                /* is_long */
  7167.     0,                /* auto_unique_value */
  7168.     15,                /* data_precision */
  7169.     null,                /* numeric_scale */
  7170.     null,                /* column_size */
  7171.     null,                /* literal_prefix */
  7172.     null,                /* literal_suffix */
  7173.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  7174.     0,                /* unsigned_attribute */
  7175.     'float'                /* local_type_name */
  7176.     )    
  7177.  
  7178. /* Local Real */
  7179. insert into spt_provider_types values
  7180.     (
  7181.     59 /*SQLFLT4*/,            /* ss_dtype */
  7182.     4,                /* fixlen */
  7183.     'real',                /* type_name */
  7184.     4 /*DBTYPE_R4*/,        /* oledb_data_type */
  7185.     1,                /* best_match */
  7186.     1,                /* is_nullable */
  7187.     0,                /* case_sensitive */
  7188.     0,                /* fixed_prec_scale */
  7189.     0,                /* is_long */
  7190.     0,                /* auto_unique_value */
  7191.     7,                /* data_precision */
  7192.     null,                /* numeric_scale */
  7193.     null,                /* column_size */
  7194.     null,                /* literal_prefix */
  7195.     null,                /* literal_suffix */
  7196.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  7197.     0,                /* unsigned_attribute */
  7198.     'real'                /* local_type_name */
  7199.     )    
  7200.  
  7201. /* Local Int */
  7202. insert into spt_provider_types values
  7203.     (
  7204.     56 /*SQLINT4*/,            /* ss_dtype */
  7205.     4,                /* fixlen */
  7206.     'int',                /* type_name */
  7207.     3 /*DBTYPE_I4*/,        /* oledb_data_type */
  7208.     1,                /* best_match */
  7209.     1,                /* is_nullable */
  7210.     0,                /* case_sensitive */
  7211.     1,                /* fixed_prec_scale */
  7212.     0,                /* is_long */
  7213.     0,                /* auto_unique_value */
  7214.     10,                /* data_precision */
  7215.     null,                /* numeric_scale */
  7216.     null,                /* column_size */
  7217.     null,                /* literal_prefix */
  7218.     null,                /* literal_suffix */
  7219.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  7220.     0,                /* unsigned_attribute */
  7221.     'int'                /* local_type_name */
  7222.     )    
  7223.  
  7224. /* Local Smallint */
  7225. insert into spt_provider_types values
  7226.     (
  7227.     52 /*SQLINT2*/,            /* ss_dtype */
  7228.     2,                /* fixlen */
  7229.     'smallint',            /* type_name */
  7230.     2 /*DBTYPE_I2*/,        /* oledb_data_type */
  7231.     1,                /* best_match */
  7232.     1,                /* is_nullable */
  7233.     0,                /* case_sensitive */
  7234.     1,                /* fixed_prec_scale */
  7235.     0,                /* is_long */
  7236.     0,                /* auto_unique_value */
  7237.     5,                /* data_precision */
  7238.     null,                /* numeric_scale */
  7239.     null,                /* column_size */
  7240.     null,                /* literal_prefix */
  7241.     null,                /* literal_suffix */
  7242.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  7243.     0,                /* unsigned_attribute */
  7244.     'smallint'            /* local_type_name */
  7245.     )    
  7246.  
  7247. /* Local Tinyint */
  7248. insert into spt_provider_types values
  7249.     (
  7250.     48 /*SQLINT1*/,            /* ss_dtype */
  7251.     1,                /* fixlen */
  7252.     'tinyint',            /* type_name */
  7253.     17 /*DBTYPE_UI1*/,        /* oledb_data_type */
  7254.     1,                /* best_match */
  7255.     1,                /* is_nullable */
  7256.     0,                /* case_sensitive */
  7257.     1,                /* fixed_prec_scale */
  7258.     0,                /* is_long */
  7259.     0,                /* auto_unique_value */
  7260.     3,                /* data_precision */
  7261.     null,                /* numeric_scale */
  7262.     null,                /* column_size */
  7263.     null,                /* literal_prefix */
  7264.     null,                /* literal_suffix */
  7265.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  7266.     1,                /* unsigned_attribute */
  7267.     'tinyint'            /* local_type_name */
  7268.     )    
  7269. commit tran
  7270. go
  7271. begin tran    
  7272. if (charindex('6.00', @@version) > 0 or
  7273.     charindex('6.50', @@version) > 0 or
  7274.     charindex('7.00', @@version) > 0)
  7275.     begin    /*    Add 6.0 data types */
  7276.     
  7277.     /* Local Decimal */
  7278.     insert into spt_provider_types values
  7279.         (
  7280.         55 /*SQLDECIMAL*/,        /* ss_dtype */
  7281.         0,                /* fixlen */
  7282.         'decimal',            /* type_name */
  7283.         131 /*DBTYPE_NUMERIC*/,        /* oledb_data_type */
  7284.         0,                /* best_match */
  7285.         1,                /* is_nullable */
  7286.         0,                /* case_sensitive */
  7287.         1,                /* fixed_prec_scale */
  7288.         0,                /* is_long */
  7289.         0,                /* auto_unique_value */
  7290.         28,                /* data_precision */
  7291.         null,                /* numeric_scale */
  7292.         null,                /* column_size */
  7293.         null,                /* literal_prefix */
  7294.         null,                /* literal_suffix */
  7295.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  7296.         0,                /* unsigned_attribute */
  7297.         'decimal'            /* local_type_name */
  7298.         )    
  7299.  
  7300.     /* Local Numeric */
  7301.     insert into spt_provider_types values
  7302.         (
  7303.         63 /*SQLNUMERIC*/,        /* ss_dtype */
  7304.         0,                /* fixlen */
  7305.         'numeric',            /* type_name */
  7306.         131 /*DBTYPE_NUMERIC*/,        /* oledb_data_type */
  7307.         1,                /* best_match */
  7308.         1,                /* is_nullable */
  7309.         0,                /* case_sensitive */
  7310.         1,                /* fixed_prec_scale */
  7311.         0,                /* is_long */
  7312.         0,                /* auto_unique_value */
  7313.         28,                /* data_precision */
  7314.         null,                /* numeric_scale */
  7315.         null,                /* column_size */
  7316.         null,                /* literal_prefix */
  7317.         null,                /* literal_suffix */
  7318.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  7319.         0,                /* unsigned_attribute */
  7320.         'numeric'            /* local_type_name */
  7321.         )    
  7322.  
  7323.     /* Identity attribute data types */
  7324.  
  7325.     /* Identity Int */
  7326.     insert into spt_provider_types values
  7327.         (
  7328.         56 /*SQLINT4*/,            /* ss_dtype */
  7329.         4,                /* fixlen */
  7330.         'int identity',            /* type_name */
  7331.         3 /*DBTYPE_I4*/,        /* oledb_data_type */
  7332.         0,                /* best_match */
  7333.         0,                /* is_nullable */
  7334.         0,                /* case_sensitive */
  7335.         1,                /* fixed_prec_scale */
  7336.         0,                /* is_long */
  7337.         1,                /* auto_unique_value */
  7338.         10,                /* data_precision */
  7339.         null,                /* numeric_scale */
  7340.         null,                /* column_size */
  7341.         null,                /* literal_prefix */
  7342.         null,                /* literal_suffix */
  7343.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  7344.         0,                /* unsigned_attribute */
  7345.         'int identity'            /* local_type_name */
  7346.         )    
  7347.  
  7348.     /* Identity Smallint */
  7349.     insert into spt_provider_types values
  7350.         (
  7351.         52 /*SQLINT2*/,            /* ss_dtype */
  7352.         2,                /* fixlen */
  7353.         'smallint identity',        /* type_name */
  7354.         2 /*DBTYPE_I2*/,        /* oledb_data_type */
  7355.         0,                /* best_match */
  7356.         0,                /* is_nullable */
  7357.         0,                /* case_sensitive */
  7358.         1,                /* fixed_prec_scale */
  7359.         0,                /* is_long */
  7360.         1,                /* auto_unique_value */
  7361.         5,                /* data_precision */
  7362.         null,                /* numeric_scale */
  7363.         null,                /* column_size */
  7364.         null,                /* literal_prefix */
  7365.         null,                /* literal_suffix */
  7366.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  7367.         0,                /* unsigned_attribute */
  7368.         'smallint identity'        /* local_type_name */
  7369.         )
  7370.  
  7371.     /* Identity Tinyint */
  7372.     insert into spt_provider_types values
  7373.         (
  7374.         48 /*SQLINT1*/,            /* ss_dtype */
  7375.         1,                /* fixlen */
  7376.         'tinyint identity',        /* type_name */
  7377.         17 /*DBTYPE_UI1*/,        /* oledb_data_type */
  7378.         0,                /* best_match */
  7379.         0,                /* is_nullable */
  7380.         0,                /* case_sensitive */
  7381.         1,                /* fixed_prec_scale */
  7382.         0,                /* is_long */
  7383.         1,                /* auto_unique_value */
  7384.         3,                /* data_precision */
  7385.         null,                /* numeric_scale */
  7386.         null,                /* column_size */
  7387.         null,                /* literal_prefix */
  7388.         null,                /* literal_suffix */
  7389.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  7390.         1,                /* unsigned_attribute */
  7391.         'tinyint identity'        /* local_type_name */
  7392.         )    
  7393.  
  7394.     /* Identity Decimal */
  7395.     insert into spt_provider_types values
  7396.         (
  7397.         55 /*SQLDECIMAL*/,        /* ss_dtype */
  7398.         0,                /* fixlen */
  7399.         'decimal() identity',        /* type_name */
  7400.         131 /*DBTYPE_NUMERIC*/,        /* oledb_data_type */
  7401.         0,                /* best_match */
  7402.         0,                /* is_nullable */
  7403.         0,                /* case_sensitive */
  7404.         1,                /* fixed_prec_scale */
  7405.         0,                /* is_long */
  7406.         1,                /* auto_unique_value */
  7407.         28,                /* data_precision */
  7408.         null,                /* numeric_scale */
  7409.         null,                /* column_size */
  7410.         null,                /* literal_prefix */
  7411.         null,                /* literal_suffix */
  7412.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  7413.         0,                /* unsigned_attribute */
  7414.         'decimal() identity'        /* local_type_name */
  7415.         )    
  7416.  
  7417.     /* Identity Numeric */
  7418.     insert into spt_provider_types values
  7419.         (
  7420.         63 /*SQLNUMERIC*/,        /* ss_dtype */
  7421.         0,                /* fixlen */
  7422.         'numeric() identity',        /* type_name */
  7423.         131 /*DBTYPE_NUMERIC*/,        /* oledb_data_type */
  7424.         0,                /* best_match */
  7425.         0,                /* is_nullable */
  7426.         0,                /* case_sensitive */
  7427.         1,                /* fixed_prec_scale */
  7428.         0,                /* is_long */
  7429.         1,                /* auto_unique_value */
  7430.         28,                /* data_precision */
  7431.         null,                /* numeric_scale */
  7432.         null,                /* column_size */
  7433.         null,                /* literal_prefix */
  7434.         null,                /* literal_suffix */
  7435.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  7436.         0,                /* unsigned_attribute */
  7437.         'numeric() identity'        /* local_type_name */
  7438.         )        
  7439.     end
  7440. commit tran
  7441. go
  7442. begin tran
  7443. if (charindex('7.00', @@version) = 0)
  7444.     begin    /*    Add nullable type for non-Sphinx server */
  7445.     
  7446.     /* Local Floatn */
  7447.     insert into spt_provider_types values
  7448.         (
  7449.         109 /*SQLFLTN*/,        /* ss_dtype */
  7450.         8,                /* fixlen */
  7451.         'float',            /* type_name */
  7452.         5 /*DBTYPE_R8*/,        /* oledb_data_type */
  7453.         0,                /* best_match */
  7454.         1,                /* is_nullable */
  7455.         0,                /* case_sensitive */
  7456.         0,                /* fixed_prec_scale */
  7457.         0,                /* is_long */
  7458.         0,                /* auto_unique_value */
  7459.         15,                /* data_precision */
  7460.         null,                /* numeric_scale */
  7461.         null,                /* column_size */
  7462.         null,                /* literal_prefix */
  7463.         null,                /* literal_suffix */
  7464.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  7465.         null,                /* unsigned_attribute */
  7466.         'float'                /* local_type_name */
  7467.         )    
  7468.     insert into spt_provider_types values
  7469.         (
  7470.         109 /*SQLFLT4*/,        /* ss_dtype */
  7471.         4,                /* fixlen */
  7472.         'real',                /* type_name */
  7473.         4 /*DBTYPE_R4*/,        /* oledb_data_type */
  7474.         0,                /* best_match */
  7475.         1,                /* is_nullable */
  7476.         0,                /* case_sensitive */
  7477.         0,                /* fixed_prec_scale */
  7478.         0,                /* is_long */
  7479.         0,                /* auto_unique_value */
  7480.         7,                /* data_precision */
  7481.         null,                /* numeric_scale */
  7482.         null,                /* column_size */
  7483.         null,                /* literal_prefix */
  7484.         null,                /* literal_suffix */
  7485.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  7486.         null,                /* unsigned_attribute */
  7487.         'real'                /* local_type_name */
  7488.         )    
  7489.  
  7490.     /* Local Intn */
  7491.     insert into spt_provider_types values
  7492.         (
  7493.         38 /*SQLINTN*/,            /* ss_dtype */
  7494.         4,                /* fixlen */
  7495.         'int',                /* type_name */
  7496.         3 /*DBTYPE_I4*/,        /* oledb_data_type */
  7497.         1,                /* best_match */
  7498.         1,                /* is_nullable */
  7499.         0,                /* case_sensitive */
  7500.         1,                /* fixed_prec_scale */
  7501.         0,                /* is_long */
  7502.         0,                /* auto_unique_value */
  7503.         10,                /* data_precision */
  7504.         null,                /* numeric_scale */
  7505.         null,                /* column_size */
  7506.         null,                /* literal_prefix */
  7507.         null,                /* literal_suffix */
  7508.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  7509.         0,                /* unsigned_attribute */
  7510.         'int'                /* local_type_name */
  7511.         )    
  7512.     insert into spt_provider_types values
  7513.         (
  7514.         38 /*SQLINTN*/,            /* ss_dtype */
  7515.         2,                /* fixlen */
  7516.         'smallint',            /* type_name */
  7517.         2 /*DBTYPE_I2*/,        /* oledb_data_type */
  7518.         0,                /* best_match */
  7519.         1,                /* is_nullable */
  7520.         0,                /* case_sensitive */
  7521.         1,                /* fixed_prec_scale */
  7522.         0,                /* is_long */
  7523.         0,                /* auto_unique_value */
  7524.         5,                /* data_precision */
  7525.         null,                /* numeric_scale */
  7526.         null,                /* column_size */
  7527.         null,                /* literal_prefix */
  7528.         null,                /* literal_suffix */
  7529.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  7530.         0,                /* unsigned_attribute */
  7531.         'smallint'            /* local_type_name */
  7532.         )    
  7533.     insert into spt_provider_types values
  7534.         (
  7535.         38 /*SQLINTN*/,            /* ss_dtype */
  7536.         1,                /* fixlen */
  7537.         'tinyint',            /* type_name */
  7538.         17 /*DBTYPE_UI1*/,        /* oledb_data_type */
  7539.         0,                /* best_match */
  7540.         1,                /* is_nullable */
  7541.         0,                /* case_sensitive */
  7542.         1,                /* fixed_prec_scale */
  7543.         0,                /* is_long */
  7544.         0,                /* auto_unique_value */
  7545.         3,                /* data_precision */
  7546.         null,                /* numeric_scale */
  7547.         null,                /* column_size */
  7548.         null,                /* literal_prefix */
  7549.         null,                /* literal_suffix */
  7550.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  7551.         1,                /* unsigned_attribute */
  7552.         'tinyint'            /* local_type_name */
  7553.         )    
  7554.  
  7555.     if (charindex('6.00', @@version) > 0 or    charindex('6.50', @@version) > 0)
  7556.         begin    /*    Add 6.0 data types */
  7557.         
  7558.         /* Local Decimaln */
  7559.         insert into spt_provider_types values
  7560.             (
  7561.             106 /*SQLDECIMALN*/,        /* ss_dtype */
  7562.             0,                /* fixlen */
  7563.             'decimal',            /* type_name */
  7564.             131 /*DBTYPE_NUMERIC*/,        /* oledb_data_type */
  7565.             0,                /* best_match */
  7566.             1,                /* is_nullable */
  7567.             0,                /* case_sensitive */
  7568.             1,                /* fixed_prec_scale */
  7569.             0,                /* is_long */
  7570.             0,                /* auto_unique_value */
  7571.             28,                /* data_precision */
  7572.             null,                /* numeric_scale */
  7573.             null,                /* column_size */
  7574.             null,                /* literal_prefix */
  7575.             null,                /* literal_suffix */
  7576.             3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  7577.             0,                /* unsigned_attribute */
  7578.             'decimal'            /* local_type_name */
  7579.             )    
  7580.         insert into spt_provider_types values
  7581.             (
  7582.             106 /*SQLDECIMALN*/,        /* ss_dtype */
  7583.             0,                /* fixlen */
  7584.             'decimal() identity',        /* type_name */
  7585.             131 /*DBTYPE_NUMERIC*/,        /* oledb_data_type */
  7586.             0,                /* best_match */
  7587.             0,                /* is_nullable */
  7588.             0,                /* case_sensitive */
  7589.             1,                /* fixed_prec_scale */
  7590.             0,                /* is_long */
  7591.             1,                /* auto_unique_value */
  7592.             28,                /* data_precision */
  7593.             null,                /* numeric_scale */
  7594.             null,                /* column_size */
  7595.             null,                /* literal_prefix */
  7596.             null,                /* literal_suffix */
  7597.             3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  7598.             0,                /* unsigned_attribute */
  7599.             'decimal() identity'        /* local_type_name */
  7600.             )    
  7601.  
  7602.         /* Local Numericn */
  7603.         insert into spt_provider_types values
  7604.             (
  7605.             108 /*SQLNUMERICN*/,        /* ss_dtype */
  7606.             0,                /* fixlen */
  7607.             'numeric',            /* type_name */
  7608.             131 /*DBTYPE_NUMERIC*/,        /* oledb_data_type */
  7609.             0,                /* best_match */
  7610.             1,                /* is_nullable */
  7611.             0,                /* case_sensitive */
  7612.             1,                /* fixed_prec_scale */
  7613.             0,                /* is_long */
  7614.             0,                /* auto_unique_value */
  7615.             28,                /* data_precision */
  7616.             null,                /* numeric_scale */
  7617.             null,                /* column_size */
  7618.             null,                /* literal_prefix */
  7619.             null,                /* literal_suffix */
  7620.             3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  7621.             0,                /* unsigned_attribute */
  7622.             'numeric'            /* local_type_name */
  7623.             )    
  7624.         insert into spt_provider_types values
  7625.             (
  7626.             108 /*SQLNUMERICN*/,        /* ss_dtype */
  7627.             0,                /* fixlen */
  7628.             'numeric() identity',        /* type_name */
  7629.             131 /*DBTYPE_NUMERIC*/,        /* oledb_data_type */
  7630.             0,                /* best_match */
  7631.             0,                /* is_nullable */
  7632.             0,                /* case_sensitive */
  7633.             1,                /* fixed_prec_scale */
  7634.             0,                /* is_long */
  7635.             1,                /* auto_unique_value */
  7636.             28,                /* data_precision */
  7637.             null,                /* numeric_scale */
  7638.             null,                /* column_size */
  7639.             null,                /* literal_prefix */
  7640.             null,                /* literal_suffix */
  7641.             3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  7642.             0,                /* unsigned_attribute */
  7643.             'numeric() identity'        /* local_type_name */
  7644.             )    
  7645.         end
  7646.     end
  7647. commit tran
  7648. go
  7649.  
  7650. /*
  7651. **    Remaining data types
  7652. */
  7653. begin tran
  7654.  
  7655. if (charindex('7.00', @@version) > 0)
  7656.     begin
  7657.     /* Local Bit */
  7658.     insert into spt_provider_types values
  7659.         (
  7660.         50 /*SQLBIT*/,            /* ss_dtype */
  7661.         0,                /* fixlen */
  7662.         'bit',                /* type_name */
  7663.         11 /*DBTYPE_BOOL*/,        /* oledb_data_type */
  7664.         1,                /* best_match */
  7665.         1,                /* is_nullable */
  7666.         0,                /* case_sensitive */
  7667.         0,                /* fixed_prec_scale */
  7668.         0,                /* is_long */
  7669.         0,                /* auto_unique_value */
  7670.         1,                /* data_precision */
  7671.         null,                /* numeric_scale */
  7672.         null,                /* column_size */
  7673.         null,                /* literal_prefix */
  7674.         null,                /* literal_suffix */
  7675.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  7676.         null,                /* unsigned_attribute */
  7677.         'bit'                /* local_type_name */
  7678.         )        
  7679.     
  7680.     /* data types changed in Sphinx */    
  7681.     update spt_provider_types set ss_dtype = (select xtype from systypes    where type_name like name+'%')
  7682.  
  7683.     /* Local Timestamp */
  7684.     insert into spt_provider_types values
  7685.         (
  7686.         152,                /* ss_dtype */
  7687.         8,                /* fixlen */
  7688.         'timestamp',            /* type_name */
  7689.         21 /*DBTYPE_UI8*/,        /* oledb_data_type */
  7690.         1,                /* best_match */
  7691.         0,                /* is_nullable */
  7692.         0,                /* case_sensitive */
  7693.         1,                /* fixed_prec_scale */
  7694.         0,                /* is_long */
  7695.         0,                /* auto_unique_value */
  7696.         20,                /* data_precision */
  7697.         null,                /* numeric_scale */
  7698.         null,                /* column_size */
  7699.         '0x',                /* literal_prefix */
  7700.         null,                /* literal_suffix */
  7701.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  7702.         1,                /* unsigned_attribute */
  7703.         'timestamp'            /* local_type_name */
  7704.         )    
  7705.     /* Local GUID */
  7706.     insert into spt_provider_types values
  7707.         (
  7708.         72,                /* ss_dtype */
  7709.         16,                /* fixlen */
  7710.         'uniqueidentifier',        /* type_name */
  7711.         72 /*DBTYPE_GUID*/,        /* oledb_data_type */
  7712.         1,                /* best_match */
  7713.         0,                /* is_nullable */
  7714.         0,                /* case_sensitive */
  7715.         0,                /* fixed_prec_scale */
  7716.         0,                /* is_long */
  7717.         0,                /* auto_unique_value */
  7718.         null,                /* data_precision */
  7719.         null,                /* numeric_scale */
  7720.         null,                /* column_size */
  7721.         '''',                /* literal_prefix */
  7722.         '''',                /* literal_suffix */
  7723.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  7724.         null,                /* unsigned_attribute */
  7725.         'uniqueidentifier'        /* local_type_name */
  7726.         )    
  7727.     end
  7728. commit tran
  7729. go
  7730. begin tran
  7731. if (charindex('7.00', @@version) = 0)
  7732.     begin
  7733.     /* Local Bit */
  7734.     insert into spt_provider_types values
  7735.         (
  7736.         50 /*SQLBIT*/,            /* ss_dtype */
  7737.         0,                /* fixlen */
  7738.         'bit',                /* type_name */
  7739.         11 /*DBTYPE_BOOL*/,        /* oledb_data_type */
  7740.         1,                /* best_match */
  7741.         0,                /* is_nullable */
  7742.         0,                /* case_sensitive */
  7743.         0,                /* fixed_prec_scale */
  7744.         0,                /* is_long */
  7745.         0,                /* auto_unique_value */
  7746.         1,                /* data_precision */
  7747.         null,                /* numeric_scale */
  7748.         null,                /* column_size */
  7749.         null,                /* literal_prefix */
  7750.         null,                /* literal_suffix */
  7751.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  7752.         null,                /* unsigned_attribute */
  7753.         'bit'                /* local_type_name */
  7754.         )    
  7755.  
  7756.     /* Local Timestamp */
  7757.     insert into spt_provider_types values
  7758.         (
  7759.         45 /*SQLBINARY*/,        /* ss_dtype */
  7760.         8,                /* fixlen */
  7761.         'timestamp',            /* type_name */
  7762.         21 /*DBTYPE_UI8*/,        /* oledb_data_type */
  7763.         1,                /* best_match */
  7764.         0,                /* is_nullable */
  7765.         0,                /* case_sensitive */
  7766.         1,                /* fixed_prec_scale */
  7767.         0,                /* is_long */
  7768.         0,                /* auto_unique_value */
  7769.         20,                /* data_precision */
  7770.         null,                /* numeric_scale */
  7771.         null,                /* column_size */
  7772.         '0x',                /* literal_prefix */
  7773.         null,                /* literal_suffix */
  7774.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  7775.         1,                /* unsigned_attribute */
  7776.         'timestamp'            /* local_type_name */
  7777.         )        
  7778. end
  7779.  
  7780. commit tran
  7781. go
  7782.  
  7783. create unique clustered index datatypeinfoclust on spt_provider_types(ss_dtype,fixlen,auto_unique_value )
  7784. go
  7785.  
  7786. dump tran master with no_log
  7787. go
  7788.  
  7789. print 'creating sp_catalogs_rowset'
  7790. go
  7791.  
  7792. create procedure sp_catalogs_rowset
  7793.     (
  7794.     @catalog_name        varchar(255)
  7795.     )        
  7796. as
  7797.     select
  7798.         CATALOG_NAME    = name,
  7799.         DESCRIPTION    = convert(varchar(1),null)
  7800.     from     master.dbo.sysdatabases
  7801.     where    name = @catalog_name
  7802. go
  7803. create procedure sp_catalogs_rowset;2
  7804.     (
  7805.     @dummy        int /* remove when Hydra bug 17032 is fixed */
  7806.     )
  7807. as
  7808.     select
  7809.         CATALOG_NAME    = name,
  7810.         DESCRIPTION    = convert(varchar(1),null)
  7811.     from     master.dbo.sysdatabases
  7812.     order by 1
  7813. go
  7814.  
  7815. grant execute on sp_catalogs_rowset to public
  7816. go
  7817.  
  7818. dump tran master with no_log
  7819. go
  7820.  
  7821.  
  7822. print 'creating sp_column_privileges_rowset'
  7823. go
  7824.  
  7825. /*    Procedure for 6.0 and 6.5 server */
  7826. CREATE PROCEDURE sp_column_privileges_rowset
  7827.     (
  7828.     @table_name     varchar(255) = null,
  7829.     @table_schema    varchar(255) = null,
  7830.     @column_name    varchar(255) = null,
  7831.     @grantor    varchar(255) = null,
  7832.     @grantee    varchar(255) = null
  7833.     )
  7834. as
  7835. IF @table_name is not null
  7836.     BEGIN
  7837.     select
  7838.         GRANTOR        = user_name(p.grantor),
  7839.         GRANTEE        = user_name(u.uid),
  7840.         TABLE_CATALOG    = db_name(),
  7841.         TABLE_SCHEMA    = user_name(o.uid),
  7842.         TABLE_NAME    = o.name,
  7843.         COLUMN_NAME    = c.name,
  7844.         COLUMN_GUID    = convert(binary(16),null),
  7845.         COLUMN_PROPID    = convert(int,null),
  7846.         PRIVILEGE_TYPE    = convert(varchar(30),
  7847.                     case p.action
  7848.                     when 193 then 'SELECT'
  7849.                     when 195 then 'INSERT'
  7850.                     when 197 then 'UPDATE'
  7851.                     else 'REFERENCES'
  7852.                     end),
  7853.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  7854.     from 
  7855.         sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c
  7856.     where
  7857.         o.name = @table_name
  7858.     and     o.type in ('U','V')
  7859.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  7860.     and    c.id = o.id
  7861.     and     (@column_name is null or @column_name = c.name)
  7862.     and     c.id = p.id
  7863.     and    (@grantor is null or @grantor = user_name(p.grantor))
  7864.     and     case 
  7865.             when substring(p.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  7866.             when substring(p.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p.columns, v.low, 1))
  7867.             else (~convert(tinyint, coalesce(substring(p.columns, v.low, 1),0)))
  7868.             end & v.high <> 0            /* permission applies to this column */
  7869.     and     v.number <= (select count(*) from syscolumns where id = o.id)    /* ranges from 1 to # of columns in table */
  7870.     and     v.type = 'P'
  7871.     and     v.number = c.colid
  7872.     and    (@grantee is null or @grantee = user_name(u.uid))
  7873.             /* expand groups */
  7874.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  7875.     and     p.protecttype <> 206    /* only grant rows */
  7876.     and     p.action in (26,193,195,197)
  7877.     and     o.uid <> u.uid            /* no rows for owner */
  7878.     and     not exists (            /* exclude revoke'd privileges */
  7879.             select *
  7880.             from sysprotects p1
  7881.             where
  7882.                 p1.protecttype = 206
  7883.             and     p1.action = p.action
  7884.             and     p1.id = p.id
  7885.             and     p1.uid = u.uid
  7886.             and     case 
  7887.                 when substring(p1.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  7888.                 when substring(p1.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  7889.                                 else (~convert(tinyint,coalesce(substring(p.columns, v.low, 1),0)))
  7890.                 end & v.high <> 0)            /* permission applies to this column */
  7891.     union all
  7892.     select    /*    Add rows for table owner */
  7893.         GRANTOR        = user_name(u.uid),
  7894.         GRANTEE        = user_name(o.uid),
  7895.         TABLE_CATALOG    = db_name(),
  7896.         TABLE_SCHEMA    = user_name(o.uid),
  7897.         TABLE_NAME    = o.name,
  7898.         COLUMN_NAME    = c.name,
  7899.         COLUMN_GUID    = convert(binary(16),null),
  7900.         COLUMN_PROPID    = convert(int,null),
  7901.         PRIVILEGE_TYPE    = convert(varchar(30),
  7902.                     case v.number
  7903.                     when 193 then 'SELECT'
  7904.                     when 195 then 'INSERT'
  7905.                     when 197 then 'UPDATE'
  7906.                     else 'REFERENCES'
  7907.                     end),
  7908.         IS_GRANTABLE    = convert(bit,1)    
  7909.     from 
  7910.         sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  7911.     where
  7912.         o.name = @table_name
  7913.     and     o.type in ('U','V')
  7914.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  7915.     and    (@grantee is null or @grantee = user_name(o.uid))
  7916.     and    c.id = o.id
  7917.     and     (@column_name is null or @column_name = c.name)
  7918.     and     u.suid = 1        /* grantor is dbo of database */
  7919.     and    (@grantor is null or @grantor = user_name(u.uid))
  7920.     and     v.type = 'P'        /* cross product to get all exposed privileges */
  7921.     and     v.number in (26,193,195,197)
  7922.     and     not exists (        /* exclude revoke'd privileges */
  7923.             select *
  7924.             from     sysprotects p1
  7925.             where    p1.protecttype = 206
  7926.             and     p1.action = v.number
  7927.             and     p1.id = o.id
  7928.             and     p1.uid = o.uid)
  7929.     order by 4, 5, 6, 9, 1, 2
  7930.     END
  7931. ELSE
  7932.     BEGIN
  7933.     select
  7934.         GRANTOR        = user_name(p.grantor),
  7935.         GRANTEE        = user_name(u.uid),
  7936.         TABLE_CATALOG    = db_name(),
  7937.         TABLE_SCHEMA    = user_name(o.uid),
  7938.         TABLE_NAME    = o.name,
  7939.         COLUMN_NAME    = c.name,
  7940.         COLUMN_GUID    = convert(binary(16),null),
  7941.         COLUMN_PROPID    = convert(int,null),
  7942.         PRIVILEGE_TYPE    = convert(varchar(30),
  7943.                     case p.action
  7944.                     when 193 then 'SELECT'
  7945.                     when 195 then 'INSERT'
  7946.                     when 197 then 'UPDATE'
  7947.                     else 'REFERENCES'
  7948.                     end),
  7949.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  7950.     from 
  7951.         sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c
  7952.     where
  7953.         o.type in ('U','V')
  7954.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  7955.     and    c.id = o.id
  7956.     and     (@column_name is null or @column_name = c.name)
  7957.     and     c.id = p.id
  7958.     and    (@grantor is null or @grantor = user_name(p.grantor))
  7959.     and     case 
  7960.             when substring(p.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  7961.             when substring(p.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p.columns, v.low, 1))
  7962.             else (~convert(tinyint, coalesce(substring(p.columns, v.low, 1),0)))
  7963.             end & v.high <> 0            /* permission applies to this column */
  7964.     and     v.number <= (select count(*) from syscolumns where id = o.id)    /* ranges from 1 to # of columns in table */
  7965.     and     v.type = 'P'
  7966.     and     v.number = c.colid
  7967.     and    (@grantee is null or @grantee = user_name(u.uid))
  7968.             /* expand groups */
  7969.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  7970.     and     p.protecttype <> 206    /* only grant rows */
  7971.     and     p.action in (26,193,195,197)
  7972.     and     o.uid <> u.uid            /* no rows for owner */
  7973.     and     not exists (            /* exclude revoke'd privileges */
  7974.             select *
  7975.             from sysprotects p1
  7976.             where
  7977.                 p1.protecttype = 206
  7978.             and     p1.action = p.action
  7979.             and     p1.id = p.id
  7980.             and     p1.uid = u.uid
  7981.             and     case 
  7982.                 when substring(p1.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  7983.                 when substring(p1.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  7984.                                 else (~convert(tinyint,coalesce(substring(p.columns, v.low, 1),0)))
  7985.                 end & v.high <> 0)            /* permission applies to this column */
  7986.     union all
  7987.     select    /*    Add rows for table owner */
  7988.         GRANTOR        = user_name(u.uid),
  7989.         GRANTEE        = user_name(o.uid),
  7990.         TABLE_CATALOG    = db_name(),
  7991.         TABLE_SCHEMA    = user_name(o.uid),
  7992.         TABLE_NAME    = o.name,
  7993.         COLUMN_NAME    = c.name,
  7994.         COLUMN_GUID    = convert(binary(16),null),
  7995.         COLUMN_PROPID    = convert(int,null),
  7996.         PRIVILEGE_TYPE    = convert(varchar(30),
  7997.                     case v.number
  7998.                     when 193 then 'SELECT'
  7999.                     when 195 then 'INSERT'
  8000.                     when 197 then 'UPDATE'
  8001.                     else 'REFERENCES'
  8002.                     end),
  8003.         IS_GRANTABLE    = convert(bit,1)    
  8004.     from 
  8005.         sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  8006.     where
  8007.          o.type in ('U','V')
  8008.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  8009.     and    (@grantee is null or @grantee = user_name(o.uid))
  8010.     and    c.id = o.id
  8011.     and     (@column_name is null or @column_name = c.name)
  8012.     and     u.suid = 1        /* grantor is dbo of database */
  8013.     and    (@grantor is null or @grantor = user_name(u.uid))
  8014.     and     v.type = 'P'        /* cross product to get all exposed privileges */
  8015.     and     v.number in (26,193,195,197)
  8016.     and     not exists (        /* exclude revoke'd privileges */
  8017.             select *
  8018.             from     sysprotects p1
  8019.             where    p1.protecttype = 206
  8020.             and     p1.action = v.number
  8021.             and     p1.id = o.id
  8022.             and     p1.uid = o.uid)
  8023.     order by 4, 5, 6, 9, 1, 2
  8024.     END
  8025. go
  8026. CREATE PROCEDURE sp_column_privileges_rowset;2
  8027.     (
  8028.        @handle        int output,
  8029.        @scrollopt    int output,
  8030.     @ccopt        int output,
  8031.     @rows        int output,
  8032.     @table_name     varchar(255) = null,
  8033.     @table_schema    varchar(255) = null,
  8034.     @column_name    varchar(255) = null,
  8035.     @grantor    varchar(255) = null,
  8036.     @grantee    varchar(255) = null
  8037.     )
  8038. as
  8039.  
  8040. declare @ret int
  8041.     
  8042. create table #spcprivsrowset1
  8043.     (
  8044.     GRANTOR        varchar(30) not null,
  8045.     GRANTEE        varchar(30) not null,
  8046.     TABLE_CATALOG    varchar(30) not null,
  8047.     TABLE_SCHEMA    varchar(30) not null,
  8048.     TABLE_NAME    varchar(30) not null,
  8049.     COLUMN_NAME    varchar(30) not null,
  8050.     COLUMN_GUID    binary(16) null,
  8051.     COLUMN_PROPID    int null,
  8052.     PRIVILEGE_TYPE    varchar(30) not null,
  8053.     IS_GRANTABLE    bit not null
  8054.     )
  8055.  
  8056. IF @table_name is not null
  8057.     BEGIN
  8058.     insert into #spcprivsrowset1
  8059.     select
  8060.         GRANTOR        = user_name(p.grantor),
  8061.         GRANTEE        = user_name(u.uid),
  8062.         TABLE_CATALOG    = db_name(),
  8063.         TABLE_SCHEMA    = user_name(o.uid),
  8064.         TABLE_NAME    = o.name,
  8065.         COLUMN_NAME    = c.name,
  8066.         COLUMN_GUID    = convert(binary(16),null),
  8067.         COLUMN_PROPID    = convert(int,null),
  8068.         PRIVILEGE_TYPE    = convert(varchar(30),
  8069.                     case p.action
  8070.                     when 193 then 'SELECT'
  8071.                     when 195 then 'INSERT'
  8072.                     when 197 then 'UPDATE'
  8073.                     else 'REFERENCES'
  8074.                     end),
  8075.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  8076.     from 
  8077.         sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c
  8078.     where
  8079.         o.name = @table_name
  8080.     and     o.type in ('U','V')
  8081.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  8082.     and    c.id = o.id
  8083.     and     (@column_name is null or @column_name = c.name)
  8084.     and     c.id = p.id
  8085.     and    (@grantor is null or @grantor = user_name(p.grantor))
  8086.     and     case 
  8087.             when substring(p.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  8088.             when substring(p.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p.columns, v.low, 1))
  8089.             else (~convert(tinyint, coalesce(substring(p.columns, v.low, 1),0)))
  8090.             end & v.high <> 0            /* permission applies to this column */
  8091.     and     v.number <= (select count(*) from syscolumns where id = o.id)    /* ranges from 1 to # of columns in table */
  8092.     and     v.type = 'P'
  8093.     and     v.number = c.colid
  8094.     and    (@grantee is null or @grantee = user_name(u.uid))
  8095.             /* expand groups */
  8096.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  8097.     and     p.protecttype <> 206    /* only grant rows */
  8098.     and     p.action in (26,193,195,197)
  8099.     and     o.uid <> u.uid            /* no rows for owner */
  8100.     and     not exists (            /* exclude revoke'd privileges */
  8101.             select *
  8102.             from sysprotects p1
  8103.             where
  8104.                 p1.protecttype = 206
  8105.             and     p1.action = p.action
  8106.             and     p1.id = p.id
  8107.             and     p1.uid = u.uid
  8108.             and     case 
  8109.                 when substring(p1.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  8110.                 when substring(p1.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  8111.                                 else (~convert(tinyint,coalesce(substring(p.columns, v.low, 1),0)))
  8112.                 end & v.high <> 0)            /* permission applies to this column */
  8113.     union all
  8114.     select    /*    Add rows for table owner */
  8115.         GRANTOR        = user_name(u.uid),
  8116.         GRANTEE        = user_name(o.uid),
  8117.         TABLE_CATALOG    = db_name(),
  8118.         TABLE_SCHEMA    = user_name(o.uid),
  8119.         TABLE_NAME    = o.name,
  8120.         COLUMN_NAME    = c.name,
  8121.         COLUMN_GUID    = convert(binary(16),null),
  8122.         COLUMN_PROPID    = convert(int,null),
  8123.         PRIVILEGE_TYPE    = convert(varchar(30),
  8124.                     case v.number
  8125.                     when 193 then 'SELECT'
  8126.                     when 195 then 'INSERT'
  8127.                     when 197 then 'UPDATE'
  8128.                     else 'REFERENCES'
  8129.                     end),
  8130.         IS_GRANTABLE    = convert(bit,1)    
  8131.     from 
  8132.         sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  8133.     where
  8134.         o.name = @table_name
  8135.     and     o.type in ('U','V')
  8136.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  8137.     and    (@grantee is null or @grantee = user_name(o.uid))
  8138.     and    c.id = o.id
  8139.     and     (@column_name is null or @column_name = c.name)
  8140.     and     u.suid = 1        /* grantor is dbo of database */
  8141.     and    (@grantor is null or @grantor = user_name(u.uid))
  8142.     and     v.type = 'P'        /* cross product to get all exposed privileges */
  8143.     and     v.number in (26,193,195,197)
  8144.     and     not exists (        /* exclude revoke'd privileges */
  8145.             select *
  8146.             from     sysprotects p1
  8147.             where    p1.protecttype = 206
  8148.             and     p1.action = v.number
  8149.             and     p1.id = o.id
  8150.             and     p1.uid = o.uid)
  8151.     order by 4, 5, 6, 9, 1, 2
  8152.     END
  8153. ELSE
  8154.     BEGIN
  8155.     insert into #spcprivsrowset1
  8156.     select
  8157.         GRANTOR        = user_name(p.grantor),
  8158.         GRANTEE        = user_name(u.uid),
  8159.         TABLE_CATALOG    = db_name(),
  8160.         TABLE_SCHEMA    = user_name(o.uid),
  8161.         TABLE_NAME    = o.name,
  8162.         COLUMN_NAME    = c.name,
  8163.         COLUMN_GUID    = convert(binary(16),null),
  8164.         COLUMN_PROPID    = convert(int,null),
  8165.         PRIVILEGE_TYPE    = convert(varchar(30),
  8166.                     case p.action
  8167.                     when 193 then 'SELECT'
  8168.                     when 195 then 'INSERT'
  8169.                     when 197 then 'UPDATE'
  8170.                     else 'REFERENCES'
  8171.                     end),
  8172.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  8173.     from 
  8174.         sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c
  8175.     where
  8176.         o.type in ('U','V')
  8177.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  8178.     and    c.id = o.id
  8179.     and     (@column_name is null or @column_name = c.name)
  8180.     and     c.id = p.id
  8181.     and    (@grantor is null or @grantor = user_name(p.grantor))
  8182.     and     case 
  8183.             when substring(p.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  8184.             when substring(p.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p.columns, v.low, 1))
  8185.             else (~convert(tinyint, coalesce(substring(p.columns, v.low, 1),0)))
  8186.             end & v.high <> 0            /* permission applies to this column */
  8187.     and     v.number <= (select count(*) from syscolumns where id = o.id)    /* ranges from 1 to # of columns in table */
  8188.     and     v.type = 'P'
  8189.     and     v.number = c.colid
  8190.     and    (@grantee is null or @grantee = user_name(u.uid))
  8191.             /* expand groups */
  8192.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  8193.     and     p.protecttype <> 206    /* only grant rows */
  8194.     and     p.action in (26,193,195,197)
  8195.     and     o.uid <> u.uid            /* no rows for owner */
  8196.     and     not exists (            /* exclude revoke'd privileges */
  8197.             select *
  8198.             from sysprotects p1
  8199.             where
  8200.                 p1.protecttype = 206
  8201.             and     p1.action = p.action
  8202.             and     p1.id = p.id
  8203.             and     p1.uid = u.uid
  8204.             and     case 
  8205.                 when substring(p1.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  8206.                 when substring(p1.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  8207.                                 else (~convert(tinyint,coalesce(substring(p.columns, v.low, 1),0)))
  8208.                 end & v.high <> 0)            /* permission applies to this column */
  8209.     union all
  8210.     select    /*    Add rows for table owner */
  8211.         GRANTOR        = user_name(u.uid),
  8212.         GRANTEE        = user_name(o.uid),
  8213.         TABLE_CATALOG    = db_name(),
  8214.         TABLE_SCHEMA    = user_name(o.uid),
  8215.         TABLE_NAME    = o.name,
  8216.         COLUMN_NAME    = c.name,
  8217.         COLUMN_GUID    = convert(binary(16),null),
  8218.         COLUMN_PROPID    = convert(int,null),
  8219.         PRIVILEGE_TYPE    = convert(varchar(30),
  8220.                     case v.number
  8221.                     when 193 then 'SELECT'
  8222.                     when 195 then 'INSERT'
  8223.                     when 197 then 'UPDATE'
  8224.                     else 'REFERENCES'
  8225.                     end),
  8226.         IS_GRANTABLE    = convert(bit,1)    
  8227.     from 
  8228.         sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  8229.     where
  8230.          o.type in ('U','V')
  8231.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  8232.     and    (@grantee is null or @grantee = user_name(o.uid))
  8233.     and    c.id = o.id
  8234.     and     (@column_name is null or @column_name = c.name)
  8235.     and     u.suid = 1        /* grantor is dbo of database */
  8236.     and    (@grantor is null or @grantor = user_name(u.uid))
  8237.     and     v.type = 'P'        /* cross product to get all exposed privileges */
  8238.     and     v.number in (26,193,195,197)
  8239.     and     not exists (        /* exclude revoke'd privileges */
  8240.             select *
  8241.             from     sysprotects p1
  8242.             where    p1.protecttype = 206
  8243.             and     p1.action = v.number
  8244.             and     p1.id = o.id
  8245.             and     p1.uid = o.uid)
  8246.     order by 4, 5, 6, 9, 1, 2
  8247.     END
  8248.  
  8249. exec @ret = sp_cursoropen @handle output, 'select * from #spcprivsrowset1',
  8250.     @scrollopt output, @ccopt output, @rows output
  8251.  
  8252. drop table #spcprivsrowset1
  8253.  
  8254. return isnull(@ret,0)
  8255. go
  8256.  
  8257. create procedure sp_column_privileges_rowset;4
  8258.     (
  8259.        @handle        int output,
  8260.        @scrollopt    int output,
  8261.     @ccopt        int output,
  8262.     @rows        int output,
  8263.     @cmd        varchar(255)
  8264.     )
  8265. as
  8266.     declare @ret int
  8267.     declare @database_name varchar(30)
  8268.  
  8269.     create table #spcprivsrowset2
  8270.         (
  8271.         GRANTOR        varchar(30) not null,
  8272.         GRANTEE        varchar(30) not null,
  8273.         TABLE_CATALOG    varchar(30) not null,
  8274.         TABLE_SCHEMA    varchar(30) not null,
  8275.         TABLE_NAME    varchar(30) not null,
  8276.         COLUMN_NAME    varchar(30) not null,
  8277.         COLUMN_GUID    binary(16) null,
  8278.         COLUMN_PROPID    int null,
  8279.         PRIVILEGE_TYPE    varchar(30) not null,
  8280.         IS_GRANTABLE    bit not null
  8281.         )
  8282.  
  8283.     declare sysdatabases_cursor insensitive cursor for 
  8284.         select name from master.dbo.sysdatabases order by name
  8285.   
  8286.     set nocount on
  8287.     open sysdatabases_cursor
  8288.       fetch next from sysdatabases_cursor into @database_name 
  8289.     while @@fetch_status = 0
  8290.         begin
  8291.         exec('use '+@database_name+' insert into #spcprivsrowset2 '+@cmd)
  8292.         fetch next from sysdatabases_cursor into @database_name 
  8293.         end
  8294.       close sysdatabases_cursor
  8295.     deallocate sysdatabases_cursor
  8296.  
  8297.     if @scrollopt is null
  8298.         begin
  8299.         select * from #spcprivsrowset2
  8300.         end
  8301.     else
  8302.         begin
  8303.         exec @ret = sp_cursoropen @handle output,
  8304.             'select * from #spcprivsrowset2',
  8305.                 @scrollopt output, @ccopt output, @rows output
  8306.         end
  8307.     drop table #spcprivsrowset2
  8308.     return isnull(@ret,0)
  8309. go
  8310.  
  8311.  
  8312. if (charindex('7.00', @@version) = 0)
  8313. begin
  8314.     print ''
  8315.     print ''
  8316.     print 'Warning:'
  8317.     print 'you are installing the stored procedures '
  8318.     print 'on a pre 7.0 SQL Server.'
  8319.     print 'Ignore the following errors.'
  8320. end
  8321. else
  8322.     drop proc sp_column_privileges_rowset
  8323. go
  8324.  
  8325.  
  8326. /*    Procedure for 7.0 server */
  8327. CREATE PROCEDURE sp_column_privileges_rowset
  8328.     (
  8329.     @table_name         varchar(255),
  8330.     @table_schema        varchar(255) = null,
  8331.     @column_name        varchar(255) = null,
  8332.     @grantor        varchar(255) = null,
  8333.     @grantee        varchar(255) = null
  8334.     )
  8335. as
  8336.     select
  8337.         GRANTOR        = user_name(p.grantor),
  8338.         GRANTEE        = user_name(u.uid),
  8339.         TABLE_CATALOG    = db_name(),
  8340.         TABLE_SCHEMA    = user_name(o.uid),
  8341.         TABLE_NAME    = o.name,
  8342.         COLUMN_NAME    = c.name,
  8343.         COLUMN_GUID    = convert(binary(16),null),
  8344.         COLUMN_PROPID    = convert(int,null),
  8345.         PRIVILEGE_TYPE    = convert(varchar(30),
  8346.                     case p.action
  8347.                     when 193 then 'SELECT'
  8348.                     when 195 then 'INSERT'
  8349.                     when 197 then 'UPDATE'
  8350.                     else 'REFERENCES'
  8351.                     end),
  8352.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  8353.     from 
  8354.         sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c, sysmembers m
  8355.     where
  8356.         o.name = @table_name
  8357.     and     o.type in ('U','V')
  8358.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  8359.     and    c.id = o.id
  8360.     and     (@column_name is null or @column_name = c.name)
  8361.     and     c.id = p.id
  8362.     and    (@grantor is null or @grantor = user_name(p.grantor))
  8363.     and     case 
  8364.             when substring(p.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  8365.             when substring(p.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p.columns, v.low, 1))
  8366.             else (~convert(tinyint, coalesce(substring(p.columns, v.low, 1),0)))
  8367.             end & v.high <> 0            /* permission applies to this column */
  8368.     and     v.number <= (select count(*) from syscolumns where id = o.id)    /* ranges from 1 to # of columns in table */
  8369.     and     v.type = 'P'
  8370.     and     v.number = c.colid
  8371.     and    (@grantee is null or @grantee = user_name(u.uid))
  8372.         /* expand groups - AKUNDONE: only 1 level of group unrolling here. Need more?? */
  8373.     and     (u.uid > 0 and u.uid < 16384)
  8374.     and     ((p.uid = u.uid) or  (p.uid = m.groupuid and u.uid = m.memberuid))    
  8375.     and     p.protecttype <> 206    /* only grant rows */
  8376.     and     p.action in (26,193,195,197)
  8377.     and     o.uid <> u.uid            /* no rows for owner */
  8378.     and     not exists (            /* exclude revoke'd privileges */
  8379.             select *
  8380.             from sysprotects p1
  8381.             where
  8382.                 p1.protecttype = 206
  8383.             and     p1.action = p.action
  8384.             and     p1.id = p.id
  8385.             and     p1.uid = u.uid
  8386.             and     case 
  8387.                 when substring(p1.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  8388.                 when substring(p1.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  8389.                                 else (~convert(tinyint,coalesce(substring(p.columns, v.low, 1),0)))
  8390.                 end & v.high <> 0)            /* permission applies to this column */
  8391.     union all
  8392.     select    /*    Add rows for table owner */
  8393.         GRANTOR        = user_name(u.uid),
  8394.         GRANTEE        = user_name(o.uid),
  8395.         TABLE_CATALOG    = db_name(),
  8396.         TABLE_SCHEMA    = user_name(o.uid),
  8397.         TABLE_NAME    = o.name,
  8398.         COLUMN_NAME    = c.name,
  8399.         COLUMN_GUID    = convert(binary(16),null),
  8400.         COLUMN_PROPID    = convert(int,null),
  8401.         PRIVILEGE_TYPE    = convert(varchar(30),
  8402.                     case v.number
  8403.                     when 193 then 'SELECT'
  8404.                     when 195 then 'INSERT'
  8405.                     when 197 then 'UPDATE'
  8406.                     else 'REFERENCES'
  8407.                     end),
  8408.         IS_GRANTABLE    = convert(bit,1)    
  8409.     from 
  8410.         sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  8411.     where
  8412.         o.name = @table_name
  8413.     and     o.type in ('U','V')
  8414.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  8415.     and    (@grantee is null or @grantee = user_name(o.uid))
  8416.     and    c.id = o.id
  8417.     and     (@column_name is null or @column_name = c.name)
  8418.     and     u.suid = 1        /* grantor is dbo of database */
  8419.     and    (@grantor is null or @grantor = user_name(u.uid))
  8420.     and     v.type = 'P'        /* cross product to get all exposed privileges */
  8421.     and     v.number in (26,193,195,197)
  8422.     and     not exists (        /* exclude revoke'd privileges */
  8423.             select *
  8424.             from     sysprotects p1
  8425.             where    p1.protecttype = 206
  8426.             and     p1.action = v.number
  8427.             and     p1.id = o.id
  8428.             and     p1.uid = o.uid)
  8429.     order by 4, 5, 6, 9, 1, 2
  8430. go
  8431. CREATE PROCEDURE sp_column_privileges_rowset;2
  8432.     (
  8433.     @table_schema        varchar(255) = null,
  8434.     @column_name        varchar(255) = null,
  8435.     @grantor        varchar(255) = null,
  8436.     @grantee        varchar(255) = null
  8437.     )
  8438. as
  8439.     select
  8440.         GRANTOR        = user_name(p.grantor),
  8441.         GRANTEE        = user_name(u.uid),
  8442.         TABLE_CATALOG    = db_name(),
  8443.         TABLE_SCHEMA    = user_name(o.uid),
  8444.         TABLE_NAME    = o.name,
  8445.         COLUMN_NAME    = c.name,
  8446.         COLUMN_GUID    = convert(binary(16),null),
  8447.         COLUMN_PROPID    = convert(int,null),
  8448.         PRIVILEGE_TYPE    = convert(varchar(30),
  8449.                     case p.action
  8450.                     when 193 then 'SELECT'
  8451.                     when 195 then 'INSERT'
  8452.                     when 197 then 'UPDATE'
  8453.                     else 'REFERENCES'
  8454.                     end),
  8455.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  8456.     from 
  8457.         sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c, sysmembers m
  8458.     where
  8459.         o.type in ('U','V')
  8460.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  8461.     and    c.id = o.id
  8462.     and     (@column_name is null or @column_name = c.name)
  8463.     and     c.id = p.id
  8464.     and    (@grantor is null or @grantor = user_name(p.grantor))
  8465.     and     case 
  8466.             when substring(p.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  8467.             when substring(p.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p.columns, v.low, 1))
  8468.             else (~convert(tinyint, coalesce(substring(p.columns, v.low, 1),0)))
  8469.             end & v.high <> 0            /* permission applies to this column */
  8470.     and     v.number <= (select count(*) from syscolumns where id = o.id)    /* ranges from 1 to # of columns in table */
  8471.     and     v.type = 'P'
  8472.     and     v.number = c.colid
  8473.     and    (@grantee is null or @grantee = user_name(u.uid))
  8474.         /* expand groups - AKUNDONE: only 1 level of group unrolling here. Need more?? */
  8475.     and     (u.uid > 0 and u.uid < 16384)
  8476.     and     ((p.uid = u.uid) or  (p.uid = m.groupuid and u.uid = m.memberuid))    
  8477.     and     p.protecttype <> 206    /* only grant rows */
  8478.     and     p.action in (26,193,195,197)
  8479.     and     o.uid <> u.uid            /* no rows for owner */
  8480.     and     not exists (            /* exclude revoke'd privileges */
  8481.             select *
  8482.             from sysprotects p1
  8483.             where
  8484.                 p1.protecttype = 206
  8485.             and     p1.action = p.action
  8486.             and     p1.id = p.id
  8487.             and     p1.uid = u.uid
  8488.             and     case 
  8489.                 when substring(p1.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  8490.                 when substring(p1.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  8491.                                 else (~convert(tinyint,coalesce(substring(p.columns, v.low, 1),0)))
  8492.                 end & v.high <> 0)            /* permission applies to this column */
  8493.     union all
  8494.     select    /*    Add rows for table owner */
  8495.         GRANTOR        = user_name(u.uid),
  8496.         GRANTEE        = user_name(o.uid),
  8497.         TABLE_CATALOG    = db_name(),
  8498.         TABLE_SCHEMA    = user_name(o.uid),
  8499.         TABLE_NAME    = o.name,
  8500.         COLUMN_NAME    = c.name,
  8501.         COLUMN_GUID    = convert(binary(16),null),
  8502.         COLUMN_PROPID    = convert(int,null),
  8503.         PRIVILEGE_TYPE    = convert(varchar(30),
  8504.                     case v.number
  8505.                     when 193 then 'SELECT'
  8506.                     when 195 then 'INSERT'
  8507.                     when 197 then 'UPDATE'
  8508.                     else 'REFERENCES'
  8509.                     end),
  8510.         IS_GRANTABLE    = convert(bit,1)    
  8511.     from 
  8512.         sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  8513.     where
  8514.         o.type in ('U','V')
  8515.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  8516.     and    (@grantee is null or @grantee = user_name(o.uid))
  8517.     and    c.id = o.id
  8518.     and     (@column_name is null or @column_name = c.name)
  8519.     and     u.suid = 1        /* grantor is dbo of database */
  8520.     and    (@grantor is null or @grantor = user_name(u.uid))
  8521.     and     v.type = 'P'        /* cross product to get all exposed privileges */
  8522.     and     v.number in (26,193,195,197)
  8523.     and     not exists (        /* exclude revoke'd privileges */
  8524.             select *
  8525.             from     sysprotects p1
  8526.             where    p1.protecttype = 206
  8527.             and     p1.action = v.number
  8528.             and     p1.id = o.id
  8529.             and     p1.uid = o.uid)
  8530.     order by 4, 5, 6, 9, 1, 2
  8531. go
  8532. CREATE PROCEDURE sp_column_privileges_rowset;3
  8533.     (
  8534.     @table_name         varchar(255) = null,
  8535.     @table_schema        varchar(255) = null,
  8536.     @column_name        varchar(255) = null,
  8537.     @grantor        varchar(255) = null,
  8538.     @grantee        varchar(255) = null
  8539.     )
  8540. as
  8541. IF @table_name is not null
  8542.     BEGIN
  8543.     select
  8544.         GRANTOR        = user_name(p.grantor),
  8545.         GRANTEE        = user_name(u.uid),
  8546.         TABLE_CATALOG    = db_name(),
  8547.         TABLE_SCHEMA    = user_name(o.uid),
  8548.         TABLE_NAME    = o.name,
  8549.         COLUMN_NAME    = c.name,
  8550.         COLUMN_GUID    = convert(binary(16),null),
  8551.         COLUMN_PROPID    = convert(int,null),
  8552.         PRIVILEGE_TYPE    = convert(varchar(30),
  8553.                     case p.action
  8554.                     when 193 then 'SELECT'
  8555.                     when 195 then 'INSERT'
  8556.                     when 197 then 'UPDATE'
  8557.                     else 'REFERENCES'
  8558.                     end),
  8559.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  8560.     from 
  8561.         sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c, sysmembers m
  8562.     where
  8563.         o.name = @table_name
  8564.     and     o.type in ('U','V')
  8565.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  8566.     and    c.id = o.id
  8567.     and     (@column_name is null or @column_name = c.name)
  8568.     and     c.id = p.id
  8569.     and    (@grantor is null or @grantor = user_name(p.grantor))
  8570.     and     case 
  8571.             when substring(p.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  8572.             when substring(p.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p.columns, v.low, 1))
  8573.             else (~convert(tinyint, coalesce(substring(p.columns, v.low, 1),0)))
  8574.             end & v.high <> 0            /* permission applies to this column */
  8575.     and     v.number <= (select count(*) from syscolumns where id = o.id)    /* ranges from 1 to # of columns in table */
  8576.     and     v.type = 'P'
  8577.     and     v.number = c.colid
  8578.     and    (@grantee is null or @grantee = user_name(u.uid))
  8579.         /* expand groups - AKUNDONE: only 1 level of group unrolling here. Need more?? */
  8580.     and     (u.uid > 0 and u.uid < 16384)
  8581.     and     ((p.uid = u.uid) or  (p.uid = m.groupuid and u.uid = m.memberuid))    
  8582.     and     p.protecttype <> 206    /* only grant rows */
  8583.     and     p.action in (26,193,195,197)
  8584.     and     o.uid <> u.uid            /* no rows for owner */
  8585.     and     not exists (            /* exclude revoke'd privileges */
  8586.             select *
  8587.             from sysprotects p1
  8588.             where
  8589.                 p1.protecttype = 206
  8590.             and     p1.action = p.action
  8591.             and     p1.id = p.id
  8592.             and     p1.uid = u.uid
  8593.             and     case 
  8594.                 when substring(p1.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  8595.                 when substring(p1.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  8596.                                 else (~convert(tinyint,coalesce(substring(p.columns, v.low, 1),0)))
  8597.                 end & v.high <> 0)            /* permission applies to this column */
  8598.     union all
  8599.     select    /*    Add rows for table owner */
  8600.         GRANTOR        = user_name(u.uid),
  8601.         GRANTEE        = user_name(o.uid),
  8602.         TABLE_CATALOG    = db_name(),
  8603.         TABLE_SCHEMA    = user_name(o.uid),
  8604.         TABLE_NAME    = o.name,
  8605.         COLUMN_NAME    = c.name,
  8606.         COLUMN_GUID    = convert(binary(16),null),
  8607.         COLUMN_PROPID    = convert(int,null),
  8608.         PRIVILEGE_TYPE    = convert(varchar(30),
  8609.                     case v.number
  8610.                     when 193 then 'SELECT'
  8611.                     when 195 then 'INSERT'
  8612.                     when 197 then 'UPDATE'
  8613.                     else 'REFERENCES'
  8614.                     end),
  8615.         IS_GRANTABLE    = convert(bit,1)    
  8616.     from 
  8617.         sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  8618.     where
  8619.         o.name = @table_name
  8620.     and     o.type in ('U','V')
  8621.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  8622.     and    (@grantee is null or @grantee = user_name(o.uid))
  8623.     and    c.id = o.id
  8624.     and     (@column_name is null or @column_name = c.name)
  8625.     and     u.suid = 1        /* grantor is dbo of database */
  8626.     and    (@grantor is null or @grantor = user_name(u.uid))
  8627.     and     v.type = 'P'        /* cross product to get all exposed privileges */
  8628.     and     v.number in (26,193,195,197)
  8629.     and     not exists (        /* exclude revoke'd privileges */
  8630.             select *
  8631.             from     sysprotects p1
  8632.             where    p1.protecttype = 206
  8633.             and     p1.action = v.number
  8634.             and     p1.id = o.id
  8635.             and     p1.uid = o.uid)
  8636.     END
  8637. ELSE
  8638.     BEGIN
  8639.     select
  8640.         GRANTOR        = user_name(p.grantor),
  8641.         GRANTEE        = user_name(u.uid),
  8642.         TABLE_CATALOG    = db_name(),
  8643.         TABLE_SCHEMA    = user_name(o.uid),
  8644.         TABLE_NAME    = o.name,
  8645.         COLUMN_NAME    = c.name,
  8646.         COLUMN_GUID    = convert(binary(16),null),
  8647.         COLUMN_PROPID    = convert(int,null),
  8648.         PRIVILEGE_TYPE    = convert(varchar(30),
  8649.                     case p.action
  8650.                     when 193 then 'SELECT'
  8651.                     when 195 then 'INSERT'
  8652.                     when 197 then 'UPDATE'
  8653.                     else 'REFERENCES'
  8654.                     end),
  8655.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  8656.     from 
  8657.         sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c, sysmembers m
  8658.     where
  8659.         o.type in ('U','V')
  8660.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  8661.     and    c.id = o.id
  8662.     and     (@column_name is null or @column_name = c.name)
  8663.     and     c.id = p.id
  8664.     and    (@grantor is null or @grantor = user_name(p.grantor))
  8665.     and     case 
  8666.             when substring(p.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  8667.             when substring(p.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p.columns, v.low, 1))
  8668.             else (~convert(tinyint, coalesce(substring(p.columns, v.low, 1),0)))
  8669.             end & v.high <> 0            /* permission applies to this column */
  8670.     and     v.number <= (select count(*) from syscolumns where id = o.id)    /* ranges from 1 to # of columns in table */
  8671.     and     v.type = 'P'
  8672.     and     v.number = c.colid
  8673.     and    (@grantee is null or @grantee = user_name(u.uid))
  8674.         /* expand groups - AKUNDONE: only 1 level of group unrolling here. Need more?? */
  8675.     and     (u.uid > 0 and u.uid < 16384)
  8676.     and     ((p.uid = u.uid) or  (p.uid = m.groupuid and u.uid = m.memberuid))    
  8677.     and     p.protecttype <> 206    /* only grant rows */
  8678.     and     p.action in (26,193,195,197)
  8679.     and     o.uid <> u.uid            /* no rows for owner */
  8680.     and     not exists (            /* exclude revoke'd privileges */
  8681.             select *
  8682.             from sysprotects p1
  8683.             where
  8684.                 p1.protecttype = 206
  8685.             and     p1.action = p.action
  8686.             and     p1.id = p.id
  8687.             and     p1.uid = u.uid
  8688.             and     case 
  8689.                 when substring(p1.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  8690.                 when substring(p1.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  8691.                                 else (~convert(tinyint,coalesce(substring(p.columns, v.low, 1),0)))
  8692.                 end & v.high <> 0)            /* permission applies to this column */
  8693.     union all
  8694.     select    /*    Add rows for table owner */
  8695.         GRANTOR        = user_name(u.uid),
  8696.         GRANTEE        = user_name(o.uid),
  8697.         TABLE_CATALOG    = db_name(),
  8698.         TABLE_SCHEMA    = user_name(o.uid),
  8699.         TABLE_NAME    = o.name,
  8700.         COLUMN_NAME    = c.name,
  8701.         COLUMN_GUID    = convert(binary(16),null),
  8702.         COLUMN_PROPID    = convert(int,null),
  8703.         PRIVILEGE_TYPE    = convert(varchar(30),
  8704.                     case v.number
  8705.                     when 193 then 'SELECT'
  8706.                     when 195 then 'INSERT'
  8707.                     when 197 then 'UPDATE'
  8708.                     else 'REFERENCES'
  8709.                     end),
  8710.         IS_GRANTABLE    = convert(bit,1)    
  8711.     from 
  8712.         sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  8713.     where
  8714.          o.type in ('U','V')
  8715.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  8716.     and    (@grantee is null or @grantee = user_name(o.uid))
  8717.     and    c.id = o.id
  8718.     and     (@column_name is null or @column_name = c.name)
  8719.     and     u.suid = 1        /* grantor is dbo of database */
  8720.     and    (@grantor is null or @grantor = user_name(u.uid))
  8721.     and     v.type = 'P'        /* cross product to get all exposed privileges */
  8722.     and     v.number in (26,193,195,197)
  8723.     and     not exists (        /* exclude revoke'd privileges */
  8724.             select *
  8725.             from     sysprotects p1
  8726.             where    p1.protecttype = 206
  8727.             and     p1.action = v.number
  8728.             and     p1.id = o.id
  8729.             and     p1.uid = o.uid)
  8730.     END
  8731. go
  8732.  
  8733.  
  8734. create procedure sp_column_privileges_rowset;4
  8735.     (
  8736.        @handle        int output,
  8737.        @scrollopt    int output,
  8738.     @ccopt        int output,
  8739.     @rows        int output,
  8740.     @cmd        varchar(255)
  8741.     )
  8742. as
  8743.     declare @ret int
  8744.     declare @database_name varchar(30)
  8745.  
  8746.     create table #spcprivsrowset
  8747.         (
  8748.         GRANTOR        varchar(30) not null,
  8749.         GRANTEE        varchar(30) not null,
  8750.         TABLE_CATALOG    varchar(30) not null,
  8751.         TABLE_SCHEMA    varchar(30) not null,
  8752.         TABLE_NAME    varchar(30) not null,
  8753.         COLUMN_NAME    varchar(30) not null,
  8754.         COLUMN_GUID    binary(16) null,
  8755.         COLUMN_PROPID    int null,
  8756.         PRIVILEGE_TYPE    varchar(30) not null,
  8757.         IS_GRANTABLE    bit not null
  8758.         )
  8759.  
  8760.     declare sysdatabases_cursor insensitive cursor for 
  8761.         select name from master.dbo.sysdatabases
  8762.   
  8763.     set nocount on
  8764.     open sysdatabases_cursor
  8765.       fetch next from sysdatabases_cursor into @database_name 
  8766.     while @@fetch_status = 0
  8767.         begin
  8768.         exec('use '+@database_name+' insert into #spcprivsrowset '+@cmd)
  8769.         fetch next from sysdatabases_cursor into @database_name 
  8770.         end
  8771.       close sysdatabases_cursor
  8772.     deallocate sysdatabases_cursor
  8773.  
  8774.     if @scrollopt is null
  8775.         begin
  8776.         select * from #spcprivsrowset order by 3,4,5,6,9,1,2
  8777.         end
  8778.     else
  8779.         begin
  8780.         exec @ret = sp_cursoropen @handle output,
  8781.             'select * from #spcprivsrowset order by 3,4,5,6,9,1,2',
  8782.                 @scrollopt output, @ccopt output, @rows output
  8783.         end
  8784.     drop table #spcprivsrowset
  8785.     return isnull(@ret,0)
  8786. go
  8787.  
  8788.  
  8789. grant execute on sp_column_privileges_rowset to public
  8790. go
  8791.  
  8792. dump tran master with no_log
  8793. go
  8794.  
  8795.  
  8796. print 'creating sp_columns_rowset'
  8797. go
  8798.  
  8799. /*    Procedure for 6.0 and 6.50 server */
  8800. create procedure sp_columns_rowset
  8801.     (
  8802.        @table_name    varchar(255),
  8803.     @table_schema     varchar(255) = null,
  8804.     @column_name    varchar(255) = null
  8805.     )
  8806. as     
  8807.     select
  8808.         TABLE_CATALOG        = db_name(),
  8809.         TABLE_SCHEMA        = user_name(o.uid),
  8810.         TABLE_NAME        = o.name,
  8811.         COLUMN_NAME        = c.name,
  8812.         COLUMN_GUID        = convert(binary(16),null),
  8813.         COLUMN_PROPID        = convert(int,null),
  8814.         ORDINAL_POSITION    = convert(int,c.colid),
  8815.         COLUMN_HASDEFAULT    = convert(bit, case when m.text is null then 1 else 0 end),
  8816.         COLUMN_DEFAULT        = substring(m.text,2,datalength(m.text)-2),
  8817.         COLUMN_FLAGS        = convert(int,
  8818.                         case when d.is_long = 1 
  8819.                         then 0x82 /*DBCOLUMNFLAGS_ISLONG|DBCOLUMNFLAGS_MAYDEFER*/
  8820.                         else 0
  8821.                         end
  8822.                     |    case when d.fixlen is not null
  8823.                         then 0x10 /*DBCOLUMNFLAGS_ISFIXEDLENGTH*/
  8824.                         else 0
  8825.                         end
  8826.                     |    case when c.status&8 = 8
  8827.                         then 0x20 /*DBCOLUMNFLAGS_ISNULLABLE*/
  8828.                         else 0
  8829.                         end
  8830.                     |    case 
  8831.                         when d.type_name = 'timestamp'
  8832.                             then 0x200 /*DBCOLUMNFLAGS_ISROWVER*/
  8833.                         when d.auto_unique_value = 0
  8834.                             then 0x8 /*DBCOLUMNFLAGS_WRITEUNKNOWN*/ 
  8835.                         else 0 
  8836.                         end),
  8837.         IS_NULLABLE        = convert(bit,c.status&8),
  8838.         DATA_TYPE        = d.oledb_data_type,
  8839.         TYPE_GUID        = convert(binary(16),null),
  8840.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  8841.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  8842.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  8843.                         then coalesce(d.column_size,c.length)
  8844.                         else null 
  8845.                         end),
  8846.         CHARACTER_OCTET_LENGTH    = convert(int,
  8847.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  8848.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  8849.                         then coalesce(d.column_size,c.length)
  8850.                         else null 
  8851.                         end),
  8852.         NUMERIC_PRECISION    = convert(smallint,
  8853.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec else null end),
  8854.         NUMERIC_SCALE        = convert(smallint, 
  8855.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  8856.         DATETIME_PRECISION    = convert(int,
  8857.                         case when d.oledb_data_type = 135 /*DBTYPE_DBTIMESTAMP*/ then 3 else null end),
  8858.         CHARACTER_SET_CATALOG    = convert(varchar(30),
  8859.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  8860.                         then 'master' 
  8861.                         else null 
  8862.                         end),
  8863.         CHARACTER_SET_SCHEMA    = convert(varchar(30),
  8864.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  8865.                         then 'dbo' 
  8866.                         else null 
  8867.                         end),
  8868.         CHARACTER_SET_NAME    = convert(varchar(30),
  8869.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  8870.                         then a_cha.name 
  8871.                         else null 
  8872.                         end),
  8873.         COLLATION_CATALOG    = convert(varchar(30),
  8874.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  8875.                         then 'master' 
  8876.                         else null 
  8877.                         end),
  8878.         COLLATION_SCHEMA    = convert(varchar(30),
  8879.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  8880.                         then 'dbo' 
  8881.                         else null 
  8882.                         end),
  8883.         COLLATION_NAME        = convert(varchar(30),
  8884.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  8885.                         then b_cha.name 
  8886.                         else null 
  8887.                         end),
  8888.         DOMAIN_CATALOG        = case when t.usertype < 256 then null else db_name() end,
  8889.         DOMAIN_SCHEMA        = case when t.usertype < 256 then null else user_name(o.uid) end,
  8890.         DOMAIN_NAME        = case when t.usertype < 256 then null else t.name end,
  8891.         DESCRIPTION        = convert(varchar(1),null)    
  8892.     
  8893.     from
  8894.         syscolumns c,
  8895.         syscomments m,
  8896.         sysobjects o,
  8897.         master.dbo.spt_provider_types d,
  8898.         systypes t,
  8899.         sysusers u,
  8900.         master.dbo.sysconfigures    cfg,
  8901.         master.dbo.syscharsets        a_cha, /* charset/1001, not sortorder. */
  8902.         master.dbo.syscharsets        b_cha  /* sortorder/2001, not charset. */
  8903.     where
  8904.         o.name = @table_name
  8905.     and     o.type in ('U','V')
  8906.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  8907.     and     (
  8908.         suser_id() = 1     /* User is the System Administrator */
  8909.         or o.uid = user_id()     /* User created the object */
  8910.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  8911.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  8912.             from sysprotects p
  8913.             where p.id = o.id
  8914.             /* get rows for public,current user,user's group */
  8915.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  8916.             /* check for SELECT,EXECUTE privilege */
  8917.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  8918.             ) = 1    /* final magic...compare Grants      */
  8919.            )
  8920.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  8921.     and    (@column_name is null or @column_name = c.name)
  8922.     and     o.id = c.id
  8923.     and     t.type = d.ss_dtype
  8924.     and     c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  8925.     and     coalesce(d.auto_unique_value,0) = (c.status&128)/128
  8926.     and     c.usertype = t.usertype
  8927.     and    c.cdefault *= m.id
  8928.     and    m.colid = 1
  8929.     and    cfg.comment = 'default sortorder ID' 
  8930.     and    a_cha.type = 1001 /* type is charset */
  8931.     and    b_cha.type = 2001 /* type is sortorder */
  8932.     and    a_cha.id = b_cha.csid
  8933.     and     b_cha.id = cfg.value
  8934.     order by 2, 3, 7
  8935. go
  8936. create procedure sp_columns_rowset;2
  8937.     (
  8938.     @table_schema     varchar(255) = null,
  8939.     @column_name    varchar(255) = null
  8940.     )
  8941. as     
  8942.     select
  8943.         TABLE_CATALOG        = db_name(),
  8944.         TABLE_SCHEMA        = user_name(o.uid),
  8945.         TABLE_NAME        = o.name,
  8946.         COLUMN_NAME        = c.name,
  8947.         COLUMN_GUID        = convert(binary(16),null),
  8948.         COLUMN_PROPID        = convert(int,null),
  8949.         ORDINAL_POSITION    = convert(int,c.colid),
  8950.         COLUMN_HASDEFAULT    = convert(bit, case when m.text is null then 1 else 0 end),
  8951.         COLUMN_DEFAULT        = substring(m.text,2,datalength(m.text)-2),
  8952.         COLUMN_FLAGS        = convert(int,
  8953.                         case when d.is_long = 1 
  8954.                         then 0x82 /*DBCOLUMNFLAGS_ISLONG|DBCOLUMNFLAGS_MAYDEFER*/
  8955.                         else 0
  8956.                         end
  8957.                     |    case when d.fixlen is not null
  8958.                         then 0x10 /*DBCOLUMNFLAGS_ISFIXEDLENGTH*/
  8959.                         else 0
  8960.                         end
  8961.                     |    case when c.status&8 = 8
  8962.                         then 0x20 /*DBCOLUMNFLAGS_ISNULLABLE*/
  8963.                         else 0
  8964.                         end
  8965.                     |    case 
  8966.                         when d.type_name = 'timestamp'
  8967.                             then 0x200 /*DBCOLUMNFLAGS_ISROWVER*/
  8968.                         when d.auto_unique_value = 0
  8969.                             then 0x8 /*DBCOLUMNFLAGS_WRITEUNKNOWN*/ 
  8970.                         else 0 
  8971.                         end),
  8972.         IS_NULLABLE        = convert(bit,c.status&8),
  8973.         DATA_TYPE        = d.oledb_data_type,
  8974.         TYPE_GUID        = convert(binary(16),null),
  8975.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  8976.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  8977.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  8978.                         then coalesce(d.column_size,c.length)
  8979.                         else null 
  8980.                         end),
  8981.         CHARACTER_OCTET_LENGTH    = convert(int,
  8982.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  8983.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  8984.                         then coalesce(d.column_size,c.length)
  8985.                         else null 
  8986.                         end),
  8987.         NUMERIC_PRECISION    = convert(smallint,
  8988.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec else null end),
  8989.         NUMERIC_SCALE        = convert(smallint, 
  8990.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  8991.         DATETIME_PRECISION    = convert(int,
  8992.                         case when d.oledb_data_type = 135 /*DBTYPE_DBTIMESTAMP*/ then 3 else null end),
  8993.         CHARACTER_SET_CATALOG    = convert(varchar(30),
  8994.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  8995.                         then 'master' 
  8996.                         else null 
  8997.                         end),
  8998.         CHARACTER_SET_SCHEMA    = convert(varchar(30),
  8999.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9000.                         then 'dbo' 
  9001.                         else null 
  9002.                         end),
  9003.         CHARACTER_SET_NAME    = convert(varchar(30),
  9004.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9005.                         then a_cha.name 
  9006.                         else null 
  9007.                         end),
  9008.         COLLATION_CATALOG    = convert(varchar(30),
  9009.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9010.                         then 'master' 
  9011.                         else null 
  9012.                         end),
  9013.         COLLATION_SCHEMA    = convert(varchar(30),
  9014.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9015.                         then 'dbo' 
  9016.                         else null 
  9017.                         end),
  9018.         COLLATION_NAME        = convert(varchar(30),
  9019.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9020.                         then b_cha.name 
  9021.                         else null 
  9022.                         end),
  9023.         DOMAIN_CATALOG        = case when t.usertype < 256 then null else db_name() end,
  9024.         DOMAIN_SCHEMA        = case when t.usertype < 256 then null else user_name(o.uid) end,
  9025.         DOMAIN_NAME        = case when t.usertype < 256 then null else t.name end,
  9026.         DESCRIPTION        = convert(varchar(1),null)    
  9027.     
  9028.     from
  9029.         syscolumns c,
  9030.         syscomments m,
  9031.         sysobjects o,
  9032.         master.dbo.spt_provider_types d,
  9033.         systypes t,
  9034.         sysusers u,
  9035.         master.dbo.sysconfigures    cfg,
  9036.         master.dbo.syscharsets        a_cha, /* charset/1001, not sortorder. */
  9037.         master.dbo.syscharsets        b_cha /* sortorder/2001, not charset. */
  9038.     where
  9039.          o.type in ('U','V')
  9040.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  9041.     and     (
  9042.         suser_id() = 1     /* User is the System Administrator */
  9043.         or o.uid = user_id()     /* User created the object */
  9044.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  9045.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  9046.             from sysprotects p
  9047.             where p.id = o.id
  9048.             /* get rows for public,current user,user's group */
  9049.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  9050.             /* check for SELECT,EXECUTE privilege */
  9051.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  9052.             ) = 1    /* final magic...compare Grants      */
  9053.            )
  9054.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  9055.     and    (@column_name is null or @column_name = c.name)
  9056.     and     o.id = c.id
  9057.     and     t.type = d.ss_dtype
  9058.     and     c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  9059.     and     coalesce(d.auto_unique_value,0) = (c.status&128)/128
  9060.     and     c.usertype = t.usertype
  9061.     and    c.cdefault *= m.id
  9062.     and     m.colid = 1
  9063.     and    cfg.comment = 'default sortorder ID' 
  9064.     and    a_cha.type = 1001 /* type is charset */
  9065.     and    b_cha.type = 2001 /* type is sortorder */
  9066.     and    a_cha.id = b_cha.csid
  9067.     and     b_cha.id = cfg.value
  9068.     order by 2, 3, 7
  9069. go
  9070. create procedure sp_columns_rowset;3
  9071.     (
  9072.        @table_name    varchar(255) = null,
  9073.     @table_schema     varchar(255) = null,
  9074.     @column_name    varchar(255) = null
  9075.     )
  9076. as     
  9077. IF @table_name is not null
  9078.     BEGIN
  9079.     select
  9080.         TABLE_CATALOG        = db_name(),
  9081.         TABLE_SCHEMA        = user_name(o.uid),
  9082.         TABLE_NAME        = o.name,
  9083.         COLUMN_NAME        = c.name,
  9084.         COLUMN_GUID        = convert(binary(16),null),
  9085.         COLUMN_PROPID        = convert(int,null),
  9086.         ORDINAL_POSITION    = convert(int,c.colid),
  9087.         COLUMN_HASDEFAULT    = convert(bit, case when m.text is null then 1 else 0 end),
  9088.         COLUMN_DEFAULT        = substring(m.text,2,datalength(m.text)-2),
  9089.         COLUMN_FLAGS        = convert(int,
  9090.                         case when d.is_long = 1 
  9091.                         then 0x82 /*DBCOLUMNFLAGS_ISLONG|DBCOLUMNFLAGS_MAYDEFER*/
  9092.                         else 0
  9093.                         end
  9094.                     |    case when d.fixlen is not null
  9095.                         then 0x10 /*DBCOLUMNFLAGS_ISFIXEDLENGTH*/
  9096.                         else 0
  9097.                         end
  9098.                     |    case when c.status&8 = 8
  9099.                         then 0x20 /*DBCOLUMNFLAGS_ISNULLABLE*/
  9100.                         else 0
  9101.                         end
  9102.                     |    case 
  9103.                         when d.type_name = 'timestamp'
  9104.                             then 0x200 /*DBCOLUMNFLAGS_ISROWVER*/
  9105.                         when d.auto_unique_value = 0
  9106.                             then 0x8 /*DBCOLUMNFLAGS_WRITEUNKNOWN*/ 
  9107.                         else 0 
  9108.                         end),
  9109.         IS_NULLABLE        = convert(bit,c.status&8),
  9110.         DATA_TYPE        = d.oledb_data_type,
  9111.         TYPE_GUID        = convert(binary(16),null),
  9112.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  9113.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  9114.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  9115.                         then coalesce(d.column_size,c.length)
  9116.                         else null 
  9117.                         end),
  9118.         CHARACTER_OCTET_LENGTH    = convert(int,
  9119.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  9120.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  9121.                         then coalesce(d.column_size,c.length)
  9122.                         else null 
  9123.                         end),
  9124.         NUMERIC_PRECISION    = convert(smallint,
  9125.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec else null end),
  9126.         NUMERIC_SCALE        = convert(smallint, 
  9127.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  9128.         DATETIME_PRECISION    = convert(int,
  9129.                         case when d.oledb_data_type = 135 /*DBTYPE_DBTIMESTAMP*/ then 3 else null end),
  9130.         CHARACTER_SET_CATALOG    = convert(varchar(30),
  9131.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9132.                         then 'master' 
  9133.                         else null 
  9134.                         end),
  9135.         CHARACTER_SET_SCHEMA    = convert(varchar(30),
  9136.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9137.                         then 'dbo' 
  9138.                         else null 
  9139.                         end),
  9140.         CHARACTER_SET_NAME    = convert(varchar(30),
  9141.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9142.                         then a_cha.name 
  9143.                         else null 
  9144.                         end),
  9145.         COLLATION_CATALOG    = convert(varchar(30),
  9146.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9147.                         then 'master' 
  9148.                         else null 
  9149.                         end),
  9150.         COLLATION_SCHEMA    = convert(varchar(30),
  9151.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9152.                         then 'dbo' 
  9153.                         else null 
  9154.                         end),
  9155.         COLLATION_NAME        = convert(varchar(30),
  9156.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9157.                         then b_cha.name 
  9158.                         else null 
  9159.                         end),
  9160.         DOMAIN_CATALOG        = case when t.usertype < 256 then null else db_name() end,
  9161.         DOMAIN_SCHEMA        = case when t.usertype < 256 then null else user_name(o.uid) end,
  9162.         DOMAIN_NAME        = case when t.usertype < 256 then null else t.name end,
  9163.         DESCRIPTION        = convert(varchar(1),null)    
  9164.     
  9165.     from
  9166.         syscolumns c,
  9167.         syscomments m,
  9168.         sysobjects o,
  9169.         master.dbo.spt_provider_types d,
  9170.         systypes t,
  9171.         sysusers u,
  9172.         master.dbo.sysconfigures    cfg,
  9173.         master.dbo.syscharsets        a_cha, /* charset/1001, not sortorder. */
  9174.         master.dbo.syscharsets        b_cha  /* sortorder/2001, not charset. */
  9175.     where
  9176.         o.name = @table_name
  9177.     and     o.type in ('U','V')
  9178.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  9179.     and     (
  9180.         suser_id() = 1     /* User is the System Administrator */
  9181.         or o.uid = user_id()     /* User created the object */
  9182.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  9183.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  9184.             from sysprotects p
  9185.             where p.id = o.id
  9186.             /* get rows for public,current user,user's group */
  9187.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  9188.             /* check for SELECT,EXECUTE privilege */
  9189.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  9190.             ) = 1    /* final magic...compare Grants      */
  9191.            )
  9192.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  9193.     and    (@column_name is null or @column_name = c.name)
  9194.     and     o.id = c.id
  9195.     and     t.type = d.ss_dtype
  9196.     and     c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  9197.     and     coalesce(d.auto_unique_value,0) = (c.status&128)/128
  9198.     and     c.usertype = t.usertype
  9199.     and    c.cdefault *= m.id
  9200.     and     m.colid = 1
  9201.     and    cfg.comment = 'default sortorder ID' 
  9202.     and    a_cha.type = 1001 /* type is charset */
  9203.     and    b_cha.type = 2001 /* type is sortorder */
  9204.     and    a_cha.id = b_cha.csid
  9205.     and     b_cha.id = cfg.value
  9206.     END
  9207. ELSE 
  9208.     BEGIN
  9209.     select
  9210.         TABLE_CATALOG        = db_name(),
  9211.         TABLE_SCHEMA        = user_name(o.uid),
  9212.         TABLE_NAME        = o.name,
  9213.         COLUMN_NAME        = c.name,
  9214.         COLUMN_GUID        = convert(binary(16),null),
  9215.         COLUMN_PROPID        = convert(int,null),
  9216.         ORDINAL_POSITION    = convert(int,c.colid),
  9217.         COLUMN_HASDEFAULT    = convert(bit, case when m.text is null then 1 else 0 end),
  9218.         COLUMN_DEFAULT        = substring(m.text,2,datalength(m.text)-2),
  9219.         COLUMN_FLAGS        = convert(int,
  9220.                         case when d.is_long = 1 
  9221.                         then 0x82 /*DBCOLUMNFLAGS_ISLONG|DBCOLUMNFLAGS_MAYDEFER*/
  9222.                         else 0
  9223.                         end
  9224.                     |    case when d.fixlen is not null
  9225.                         then 0x10 /*DBCOLUMNFLAGS_ISFIXEDLENGTH*/
  9226.                         else 0
  9227.                         end
  9228.                     |    case when c.status&8 = 8
  9229.                         then 0x20 /*DBCOLUMNFLAGS_ISNULLABLE*/
  9230.                         else 0
  9231.                         end
  9232.                     |    case 
  9233.                         when d.type_name = 'timestamp'
  9234.                             then 0x200 /*DBCOLUMNFLAGS_ISROWVER*/
  9235.                         when d.auto_unique_value = 0
  9236.                             then 0x8 /*DBCOLUMNFLAGS_WRITEUNKNOWN*/ 
  9237.                         else 0 
  9238.                         end),
  9239.         IS_NULLABLE        = convert(bit,c.status&8),
  9240.         DATA_TYPE        = d.oledb_data_type,
  9241.         TYPE_GUID        = convert(binary(16),null),
  9242.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  9243.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  9244.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  9245.                         then coalesce(d.column_size,c.length)
  9246.                         else null 
  9247.                         end),
  9248.         CHARACTER_OCTET_LENGTH    = convert(int,
  9249.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  9250.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  9251.                         then coalesce(d.column_size,c.length)
  9252.                         else null 
  9253.                         end),
  9254.         NUMERIC_PRECISION    = convert(smallint,
  9255.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec else null end),
  9256.         NUMERIC_SCALE        = convert(smallint, 
  9257.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  9258.         DATETIME_PRECISION    = convert(int,
  9259.                         case when d.oledb_data_type = 135 /*DBTYPE_DBTIMESTAMP*/ then 3 else null end),
  9260.         CHARACTER_SET_CATALOG    = convert(varchar(30),
  9261.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9262.                         then 'master' 
  9263.                         else null 
  9264.                         end),
  9265.         CHARACTER_SET_SCHEMA    = convert(varchar(30),
  9266.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9267.                         then 'dbo' 
  9268.                         else null 
  9269.                         end),
  9270.         CHARACTER_SET_NAME    = convert(varchar(30),
  9271.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9272.                         then a_cha.name 
  9273.                         else null 
  9274.                         end),
  9275.         COLLATION_CATALOG    = convert(varchar(30),
  9276.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9277.                         then 'master' 
  9278.                         else null 
  9279.                         end),
  9280.         COLLATION_SCHEMA    = convert(varchar(30),
  9281.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9282.                         then 'dbo' 
  9283.                         else null 
  9284.                         end),
  9285.         COLLATION_NAME        = convert(varchar(30),
  9286.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9287.                         then b_cha.name 
  9288.                         else null 
  9289.                         end),
  9290.         DOMAIN_CATALOG        = case when t.usertype < 256 then null else db_name() end,
  9291.         DOMAIN_SCHEMA        = case when t.usertype < 256 then null else user_name(o.uid) end,
  9292.         DOMAIN_NAME        = case when t.usertype < 256 then null else t.name end,
  9293.         DESCRIPTION        = convert(varchar(1),null)    
  9294.     
  9295.     from
  9296.         syscolumns c,
  9297.         syscomments m,
  9298.         sysobjects o,
  9299.         master.dbo.spt_provider_types d,
  9300.         systypes t,
  9301.         sysusers u,
  9302.         master.dbo.sysconfigures    cfg,
  9303.         master.dbo.syscharsets        a_cha, /* charset/1001, not sortorder. */
  9304.         master.dbo.syscharsets        b_cha  /* sortorder/2001, not charset. */
  9305.     where
  9306.          o.type in ('U','V')
  9307.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  9308.     and     (
  9309.         suser_id() = 1     /* User is the System Administrator */
  9310.         or o.uid = user_id()     /* User created the object */
  9311.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  9312.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  9313.             from sysprotects p
  9314.             where p.id = o.id
  9315.             /* get rows for public,current user,user's group */
  9316.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  9317.             /* check for SELECT,EXECUTE privilege */
  9318.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  9319.             ) = 1    /* final magic...compare Grants      */
  9320.            )
  9321.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  9322.     and    (@column_name is null or @column_name = c.name)
  9323.     and     o.id = c.id
  9324.     and     t.type = d.ss_dtype
  9325.     and     c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  9326.     and     coalesce(d.auto_unique_value,0) = (c.status&128)/128
  9327.     and     c.usertype = t.usertype
  9328.     and    c.cdefault *= m.id
  9329.     and     m.colid = 1
  9330.     and    cfg.comment = 'default sortorder ID' 
  9331.     and    a_cha.type = 1001 /* type is charset */
  9332.     and    b_cha.type = 2001 /* type is sortorder */
  9333.     and    a_cha.id = b_cha.csid
  9334.     and     b_cha.id = cfg.value
  9335.     END
  9336. go
  9337.  
  9338. if (charindex('7.00', @@version) = 0)
  9339. begin
  9340.     print ''
  9341.     print ''
  9342.     print 'Warning:'
  9343.     print 'you are installing the stored procedures '
  9344.     print 'on a pre 7.0 SQL Server.'
  9345.     print 'Ignore the following errors.'
  9346. end
  9347. else
  9348.     drop proc sp_columns_rowset
  9349. go
  9350.  
  9351. /*    Procedure for 7.0 server */
  9352. create procedure sp_columns_rowset
  9353.     (
  9354.        @table_name    varchar(255),
  9355.     @table_schema     varchar(255) = NULL,
  9356.     @column_name    varchar(255) = NULL
  9357.     )
  9358. as     
  9359.     select
  9360.         TABLE_CATALOG        = db_name(),
  9361.         TABLE_SCHEMA        = user_name(o.uid),
  9362.         TABLE_NAME        = o.name,
  9363.         COLUMN_NAME        = c.name,
  9364.         COLUMN_GUID        = convert(binary(16),null),
  9365.         COLUMN_PROPID        = convert(int,null),
  9366.         ORDINAL_POSITION    = convert(int,c.colid),
  9367.         COLUMN_HASDEFAULT    = convert(bit, case when m.text is null then 1 else 0 end),
  9368.         COLUMN_DEFAULT        = substring(m.text,2,datalength(m.text)-2),
  9369.         COLUMN_FLAGS        = convert(int,
  9370.                         case when d.is_long = 1 
  9371.                         then 0x82 /*DBCOLUMNFLAGS_ISLONG|DBCOLUMNFLAGS_MAYDEFER*/
  9372.                         else 0
  9373.                         end
  9374.                     |    case when d.fixlen is not null
  9375.                         then 0x10 /*DBCOLUMNFLAGS_ISFIXEDLENGTH*/
  9376.                         else 0
  9377.                         end
  9378.                     |    case when ColumnProperty(c.id,c.name,'AllowsNull') = 1
  9379.                         then 0x20 /*DBCOLUMNFLAGS_ISNULLABLE*/
  9380.                         else 0
  9381.                         end
  9382.                     |    case 
  9383.                         when d.type_name = 'timestamp'
  9384.                             then 0x200 /*DBCOLUMNFLAGS_ISROWVER*/
  9385.                         when d.auto_unique_value = 0  and permissions(o.id,c.name)&2 = 2
  9386.                             then 0x4 /*DBCOLUMNFLAGS_WRITE*/ 
  9387.                         else 0 
  9388.                         end),
  9389.         IS_NULLABLE        = convert(bit,ColumnProperty(c.id,c.name,'AllowsNull')),
  9390.         DATA_TYPE        = d.oledb_data_type,
  9391.         TYPE_GUID        = convert(binary(16),null),
  9392.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  9393.                         case 
  9394.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  9395.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  9396.                         then coalesce(d.column_size,c.length)
  9397.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  9398.                         then coalesce(d.column_size,c.length/2)
  9399.                         else null 
  9400.                         end),
  9401.         CHARACTER_OCTET_LENGTH    = convert(int,
  9402.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  9403.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  9404.                             or d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  9405.                         then coalesce(d.column_size,c.length)
  9406.                         else null 
  9407.                         end),
  9408.         NUMERIC_PRECISION    = convert(smallint,
  9409.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.xprec else null end),
  9410.         NUMERIC_SCALE        = convert(smallint, 
  9411.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  9412.         DATETIME_PRECISION    = convert(int,
  9413.                         case when d.oledb_data_type = 135 /*DBTYPE_DBTIMESTAMP*/ then 3 else null end),
  9414.         CHARACTER_SET_CATALOG    = convert(varchar(30),
  9415.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9416.                         then 'master' 
  9417.                         else null 
  9418.                         end),
  9419.         CHARACTER_SET_SCHEMA    = convert(varchar(30),
  9420.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9421.                         then 'dbo' 
  9422.                         else null 
  9423.                         end),
  9424.         CHARACTER_SET_NAME    = convert(varchar(30),
  9425.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9426.                         then a_cha.name 
  9427.                         else null 
  9428.                         end),
  9429.         COLLATION_CATALOG    = convert(varchar(30),
  9430.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9431.                         then 'master' 
  9432.                         else null 
  9433.                         end),
  9434.         COLLATION_SCHEMA    = convert(varchar(30),
  9435.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9436.                         then 'dbo' 
  9437.                         else null 
  9438.                         end),
  9439.         COLLATION_NAME        = convert(varchar(30),
  9440.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9441.                         then b_cha.name 
  9442.                         else null 
  9443.                         end),
  9444.         DOMAIN_CATALOG        = case when t.usertype < 256 then null else db_name() end,
  9445.         DOMAIN_SCHEMA        = case when t.usertype < 256 then null else user_name(o.uid) end,
  9446.         DOMAIN_NAME        = case when t.usertype < 256 then null else t.name end,
  9447.         DESCRIPTION        = convert(varchar(1),null)    
  9448.     
  9449.     from
  9450.         syscolumns c left join syscomments m on c.cdefault = m.id and m.colid = 1,
  9451.         sysobjects o,
  9452.         master.dbo.spt_provider_types d,
  9453.         systypes t,
  9454.         master.dbo.sysconfigures    cfg,
  9455.         master.dbo.syscharsets        a_cha, --charset/1001, not sortorder.
  9456.         master.dbo.syscharsets        b_cha --sortorder/2001, not charset.
  9457.     where
  9458.         permissions(o.id, c.name) <> 0
  9459.     and    o.name = @table_name
  9460.     and     o.type in ('U','V')
  9461.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  9462.     and    (@column_name is null or @column_name = c.name)
  9463.     and     o.id = c.id
  9464.     and     t.xtype = d.ss_dtype
  9465.     and     c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  9466.     and     coalesce(d.auto_unique_value,0) = ColumnProperty(c.id,c.name,'IsIdentity')
  9467.     and     c.usertype = t.usertype
  9468.     and    cfg.comment = 'default sortorder ID' 
  9469.     and    a_cha.type = 1001 --- type is charset
  9470.     and    b_cha.type = 2001 --- type is sortorder
  9471.     and    a_cha.id = b_cha.csid
  9472.     and     b_cha.id = cfg.value
  9473.     order by 2, 3, 7
  9474. go
  9475. create procedure sp_columns_rowset;2
  9476.     (
  9477.     @table_schema     varchar(255) = NULL,
  9478.     @column_name    varchar(255) = NULL
  9479.     )
  9480. as     
  9481.     select
  9482.         TABLE_CATALOG        = db_name(),
  9483.         TABLE_SCHEMA        = user_name(o.uid),
  9484.         TABLE_NAME        = o.name,
  9485.         COLUMN_NAME        = c.name,
  9486.         COLUMN_GUID        = convert(binary(16),null),
  9487.         COLUMN_PROPID        = convert(int,null),
  9488.         ORDINAL_POSITION    = convert(int,c.colid),
  9489.         COLUMN_HASDEFAULT    = convert(bit, case when m.text is null then 1 else 0 end),
  9490.         COLUMN_DEFAULT        = substring(m.text,2,datalength(m.text)-2),
  9491.         COLUMN_FLAGS        = convert(int,
  9492.                         case when d.is_long = 1 
  9493.                         then 0x82 /*DBCOLUMNFLAGS_ISLONG|DBCOLUMNFLAGS_MAYDEFER*/
  9494.                         else 0
  9495.                         end
  9496.                     |    case when d.fixlen is not null
  9497.                         then 0x10 /*DBCOLUMNFLAGS_ISFIXEDLENGTH*/
  9498.                         else 0
  9499.                         end
  9500.                     |    case when ColumnProperty(c.id,c.name,'AllowsNull') = 1
  9501.                         then 0x20 /*DBCOLUMNFLAGS_ISNULLABLE*/
  9502.                         else 0
  9503.                         end
  9504.                     |    case 
  9505.                         when d.type_name = 'timestamp'
  9506.                             then 0x200 /*DBCOLUMNFLAGS_ISROWVER*/
  9507.                         when d.auto_unique_value = 0  and permissions(o.id,c.name)&2 = 2
  9508.                             then 0x4 /*DBCOLUMNFLAGS_WRITE*/ 
  9509.                         else 0 
  9510.                         end),
  9511.         IS_NULLABLE        = convert(bit,ColumnProperty(c.id,c.name,'AllowsNull')),
  9512.         DATA_TYPE        = d.oledb_data_type,
  9513.         TYPE_GUID        = convert(binary(16),null),
  9514.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  9515.                         case 
  9516.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  9517.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  9518.                         then coalesce(d.column_size,c.length)
  9519.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  9520.                         then coalesce(d.column_size,c.length/2)
  9521.                         else null 
  9522.                         end),
  9523.         CHARACTER_OCTET_LENGTH    = convert(int,
  9524.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  9525.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  9526.                             or d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  9527.                         then coalesce(d.column_size,c.length)
  9528.                         else null 
  9529.                         end),
  9530.         NUMERIC_PRECISION    = convert(smallint,
  9531.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.xprec else null end),
  9532.         NUMERIC_SCALE        = convert(smallint, 
  9533.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  9534.         DATETIME_PRECISION    = convert(int,
  9535.                         case when d.oledb_data_type = 135 /*DBTYPE_DBTIMESTAMP*/ then 3 else null end),
  9536.         CHARACTER_SET_CATALOG    = convert(varchar(30),
  9537.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9538.                         then 'master' 
  9539.                         else null 
  9540.                         end),
  9541.         CHARACTER_SET_SCHEMA    = convert(varchar(30),
  9542.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9543.                         then 'dbo' 
  9544.                         else null 
  9545.                         end),
  9546.         CHARACTER_SET_NAME    = convert(varchar(30),
  9547.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9548.                         then a_cha.name 
  9549.                         else null 
  9550.                         end),
  9551.         COLLATION_CATALOG    = convert(varchar(30),
  9552.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9553.                         then 'master' 
  9554.                         else null 
  9555.                         end),
  9556.         COLLATION_SCHEMA    = convert(varchar(30),
  9557.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9558.                         then 'dbo' 
  9559.                         else null 
  9560.                         end),
  9561.         COLLATION_NAME        = convert(varchar(30),
  9562.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9563.                         then b_cha.name 
  9564.                         else null 
  9565.                         end),
  9566.         DOMAIN_CATALOG        = case when t.usertype < 256 then null else db_name() end,
  9567.         DOMAIN_SCHEMA        = case when t.usertype < 256 then null else user_name(o.uid) end,
  9568.         DOMAIN_NAME        = case when t.usertype < 256 then null else t.name end,
  9569.         DESCRIPTION        = convert(varchar(1),null)    
  9570.     
  9571.     from
  9572.         syscolumns c left join syscomments m on c.cdefault = m.id and m.colid = 1,
  9573.         sysobjects o,
  9574.         master.dbo.spt_provider_types d,
  9575.         systypes t,
  9576.         master.dbo.sysconfigures    cfg,
  9577.         master.dbo.syscharsets        a_cha, --charset/1001, not sortorder.
  9578.         master.dbo.syscharsets        b_cha --sortorder/2001, not charset.
  9579.     where
  9580.         permissions(o.id, c.name) <> 0
  9581.     and     o.type in ('U','V')
  9582.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  9583.     and    (@column_name is null or @column_name = c.name)
  9584.     and     o.id = c.id
  9585.     and     t.xtype = d.ss_dtype
  9586.     and     c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  9587.     and     coalesce(d.auto_unique_value,0) = ColumnProperty(c.id,c.name,'IsIdentity')
  9588.     and     c.usertype = t.usertype
  9589.     and    cfg.comment = 'default sortorder ID' 
  9590.     and    a_cha.type = 1001 --- type is charset
  9591.     and    b_cha.type = 2001 --- type is sortorder
  9592.     and    a_cha.id = b_cha.csid
  9593.     and     b_cha.id = cfg.value
  9594.     order by 2, 3, 7
  9595. go
  9596. create procedure sp_columns_rowset;3
  9597.     (
  9598.        @table_name    varchar(255) = NULL,
  9599.     @table_schema     varchar(255) = NULL,
  9600.     @column_name    varchar(255) = NULL
  9601.     )
  9602. as     
  9603. IF @table_name is not null
  9604.     BEGIN
  9605.     select
  9606.         TABLE_CATALOG        = db_name(),
  9607.         TABLE_SCHEMA        = user_name(o.uid),
  9608.         TABLE_NAME        = o.name,
  9609.         COLUMN_NAME        = c.name,
  9610.         COLUMN_GUID        = convert(binary(16),null),
  9611.         COLUMN_PROPID        = convert(int,null),
  9612.         ORDINAL_POSITION    = convert(int,c.colid),
  9613.         COLUMN_HASDEFAULT    = convert(bit, case when m.text is null then 1 else 0 end),
  9614.         COLUMN_DEFAULT        = substring(m.text,2,datalength(m.text)-2),
  9615.         COLUMN_FLAGS        = convert(int,
  9616.                         case when d.is_long = 1 
  9617.                         then 0x82 /*DBCOLUMNFLAGS_ISLONG|DBCOLUMNFLAGS_MAYDEFER*/
  9618.                         else 0
  9619.                         end
  9620.                     |    case when d.fixlen is not null
  9621.                         then 0x10 /*DBCOLUMNFLAGS_ISFIXEDLENGTH*/
  9622.                         else 0
  9623.                         end
  9624.                     |    case when ColumnProperty(c.id,c.name,'AllowsNull') = 1
  9625.                         then 0x20 /*DBCOLUMNFLAGS_ISNULLABLE*/
  9626.                         else 0
  9627.                         end
  9628.                     |    case 
  9629.                         when d.type_name = 'timestamp'
  9630.                             then 0x200 /*DBCOLUMNFLAGS_ISROWVER*/
  9631.                         when d.auto_unique_value = 0  and permissions(o.id,c.name)&2 = 2
  9632.                             then 0x4 /*DBCOLUMNFLAGS_WRITE*/ 
  9633.                         else 0 
  9634.                         end),
  9635.         IS_NULLABLE        = convert(bit,ColumnProperty(c.id,c.name,'AllowsNull')),
  9636.         DATA_TYPE        = d.oledb_data_type,
  9637.         TYPE_GUID        = convert(binary(16),null),
  9638.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  9639.                         case 
  9640.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  9641.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  9642.                         then coalesce(d.column_size,c.length)
  9643.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  9644.                         then coalesce(d.column_size,c.length/2)
  9645.                         else null 
  9646.                         end),
  9647.         CHARACTER_OCTET_LENGTH    = convert(int,
  9648.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  9649.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  9650.                             or d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  9651.                         then coalesce(d.column_size,c.length)
  9652.                         else null 
  9653.                         end),
  9654.         NUMERIC_PRECISION    = convert(smallint,
  9655.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.xprec else null end),
  9656.         NUMERIC_SCALE        = convert(smallint, 
  9657.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  9658.         DATETIME_PRECISION    = convert(int,
  9659.                         case when d.oledb_data_type = 135 /*DBTYPE_DBTIMESTAMP*/ then 3 else null end),
  9660.         CHARACTER_SET_CATALOG    = convert(varchar(30),
  9661.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9662.                         then 'master' 
  9663.                         else null 
  9664.                         end),
  9665.         CHARACTER_SET_SCHEMA    = convert(varchar(30),
  9666.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9667.                         then 'dbo' 
  9668.                         else null 
  9669.                         end),
  9670.         CHARACTER_SET_NAME    = convert(varchar(30),
  9671.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9672.                         then a_cha.name 
  9673.                         else null 
  9674.                         end),
  9675.         COLLATION_CATALOG    = convert(varchar(30),
  9676.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9677.                         then 'master' 
  9678.                         else null 
  9679.                         end),
  9680.         COLLATION_SCHEMA    = convert(varchar(30),
  9681.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9682.                         then 'dbo' 
  9683.                         else null 
  9684.                         end),
  9685.         COLLATION_NAME        = convert(varchar(30),
  9686.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9687.                         then b_cha.name 
  9688.                         else null 
  9689.                         end),
  9690.         DOMAIN_CATALOG        = case when t.usertype < 256 then null else db_name() end,
  9691.         DOMAIN_SCHEMA        = case when t.usertype < 256 then null else user_name(o.uid) end,
  9692.         DOMAIN_NAME        = case when t.usertype < 256 then null else t.name end,
  9693.         DESCRIPTION        = convert(varchar(1),null)    
  9694.     
  9695.     from
  9696.         syscolumns c left join syscomments m on c.cdefault = m.id and m.colid = 1,
  9697.         sysobjects o,
  9698.         master.dbo.spt_provider_types d,
  9699.         systypes t,
  9700.         master.dbo.sysconfigures    cfg,
  9701.         master.dbo.syscharsets        a_cha, --charset/1001, not sortorder.
  9702.         master.dbo.syscharsets        b_cha --sortorder/2001, not charset.
  9703.     where
  9704.         permissions(o.id, c.name) <> 0
  9705.     and    o.name = @table_name
  9706.     and     o.type in ('U','V')
  9707.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  9708.     and    (@column_name is null or @column_name = c.name)
  9709.     and     o.id = c.id
  9710.     and     t.xtype = d.ss_dtype
  9711.     and     c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  9712.     and     coalesce(d.auto_unique_value,0) = ColumnProperty(c.id,c.name,'IsIdentity')
  9713.     and     c.usertype = t.usertype
  9714.     and    cfg.comment = 'default sortorder ID' 
  9715.     and    a_cha.type = 1001 --- type is charset
  9716.     and    b_cha.type = 2001 --- type is sortorder
  9717.     and    a_cha.id = b_cha.csid
  9718.     and     b_cha.id = cfg.value
  9719.     END    
  9720. ELSE 
  9721.     BEGIN
  9722.     select
  9723.         TABLE_CATALOG        = db_name(),
  9724.         TABLE_SCHEMA        = user_name(o.uid),
  9725.         TABLE_NAME        = o.name,
  9726.         COLUMN_NAME        = c.name,
  9727.         COLUMN_GUID        = convert(binary(16),null),
  9728.         COLUMN_PROPID        = convert(int,null),
  9729.         ORDINAL_POSITION    = convert(int,c.colid),
  9730.         COLUMN_HASDEFAULT    = convert(bit, case when m.text is null then 1 else 0 end),
  9731.         COLUMN_DEFAULT        = substring(m.text,2,datalength(m.text)-2),
  9732.         COLUMN_FLAGS        = convert(int,
  9733.                         case when d.is_long = 1 
  9734.                         then 0x82 /*DBCOLUMNFLAGS_ISLONG|DBCOLUMNFLAGS_MAYDEFER*/
  9735.                         else 0
  9736.                         end
  9737.                     |    case when d.fixlen is not null
  9738.                         then 0x10 /*DBCOLUMNFLAGS_ISFIXEDLENGTH*/
  9739.                         else 0
  9740.                         end
  9741.                     |    case when ColumnProperty(c.id,c.name,'AllowsNull') = 1
  9742.                         then 0x20 /*DBCOLUMNFLAGS_ISNULLABLE*/
  9743.                         else 0
  9744.                         end
  9745.                     |    case 
  9746.                         when d.type_name = 'timestamp'
  9747.                             then 0x200 /*DBCOLUMNFLAGS_ISROWVER*/
  9748.                         when d.auto_unique_value = 0  and permissions(o.id,c.name)&2 = 2
  9749.                             then 0x4 /*DBCOLUMNFLAGS_WRITE*/ 
  9750.                         else 0 
  9751.                         end),
  9752.         IS_NULLABLE        = convert(bit,ColumnProperty(c.id,c.name,'AllowsNull')),
  9753.         DATA_TYPE        = d.oledb_data_type,
  9754.         TYPE_GUID        = convert(binary(16),null),
  9755.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  9756.                         case 
  9757.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  9758.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  9759.                         then coalesce(d.column_size,c.length)
  9760.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  9761.                         then coalesce(d.column_size,c.length/2)
  9762.                         else null 
  9763.                         end),
  9764.         CHARACTER_OCTET_LENGTH    = convert(int,
  9765.                         case 
  9766.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  9767.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  9768.                             or d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  9769.                         then coalesce(d.column_size,c.length)
  9770.                         else null 
  9771.                         end),
  9772.         NUMERIC_PRECISION    = convert(smallint,
  9773.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.xprec else null end),
  9774.         NUMERIC_SCALE        = convert(smallint, 
  9775.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  9776.         DATETIME_PRECISION    = convert(int,
  9777.                         case when d.oledb_data_type = 135 /*DBTYPE_DBTIMESTAMP*/ then 3 else null end),
  9778.         CHARACTER_SET_CATALOG    = convert(varchar(30),
  9779.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9780.                         then 'master' 
  9781.                         else null 
  9782.                         end),
  9783.         CHARACTER_SET_SCHEMA    = convert(varchar(30),
  9784.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9785.                         then 'dbo' 
  9786.                         else null 
  9787.                         end),
  9788.         CHARACTER_SET_NAME    = convert(varchar(30),
  9789.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9790.                         then a_cha.name 
  9791.                         else null 
  9792.                         end),
  9793.         COLLATION_CATALOG    = convert(varchar(30),
  9794.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9795.                         then 'master' 
  9796.                         else null 
  9797.                         end),
  9798.         COLLATION_SCHEMA    = convert(varchar(30),
  9799.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9800.                         then 'dbo' 
  9801.                         else null 
  9802.                         end),
  9803.         COLLATION_NAME        = convert(varchar(30),
  9804.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9805.                         then b_cha.name 
  9806.                         else null 
  9807.                         end),
  9808.         DOMAIN_CATALOG        = case when t.usertype < 256 then null else db_name() end,
  9809.         DOMAIN_SCHEMA        = case when t.usertype < 256 then null else user_name(o.uid) end,
  9810.         DOMAIN_NAME        = case when t.usertype < 256 then null else t.name end,
  9811.         DESCRIPTION        = convert(varchar(1),null)    
  9812.     
  9813.     from
  9814.         syscolumns c left join syscomments m on c.cdefault = m.id and m.colid = 1,
  9815.         sysobjects o,
  9816.         master.dbo.spt_provider_types d,
  9817.         systypes t,
  9818.         master.dbo.sysconfigures    cfg,
  9819.         master.dbo.syscharsets        a_cha, --charset/1001, not sortorder.
  9820.         master.dbo.syscharsets        b_cha --sortorder/2001, not charset.
  9821.     where
  9822.         permissions(o.id, c.name) <> 0
  9823.     and     o.type in ('U','V')
  9824.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  9825.     and    (@column_name is null or @column_name = c.name)
  9826.     and     o.id = c.id
  9827.     and     t.xtype = d.ss_dtype
  9828.     and     c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  9829.     and     coalesce(d.auto_unique_value,0) = ColumnProperty(c.id,c.name,'IsIdentity')
  9830.     and     c.usertype = t.usertype
  9831.     and    cfg.comment = 'default sortorder ID' 
  9832.     and    a_cha.type = 1001 --- type is charset
  9833.     and    b_cha.type = 2001 --- type is sortorder
  9834.     and    a_cha.id = b_cha.csid
  9835.     and     b_cha.id = cfg.value
  9836. END
  9837. go
  9838. create procedure sp_columns_rowset;4
  9839.     (
  9840.  
  9841.        @handle        int output,
  9842.        @scrollopt    int output,
  9843.     @ccopt        int output,
  9844.     @rows        int output,
  9845.     @cmd        varchar(255)
  9846.     )
  9847. as
  9848.     declare @ret int
  9849.     declare @database_name varchar(30)
  9850.  
  9851.     create table #spcolumnsrowset 
  9852.         (
  9853.         TABLE_CATALOG        varchar(30) not null,
  9854.         TABLE_SCHEMA        varchar(30) not null,
  9855.         TABLE_NAME        varchar(30) not null,
  9856.         COLUMN_NAME        varchar(30) not null,
  9857.         COLUMN_GUID        binary(16) null,
  9858.         COLUMN_PROPID        int null,
  9859.         ORDINAL_POSITION    int not null,
  9860.         COLUMN_HASDEFAULT    bit not null,
  9861.         COLUMN_DEFAULT        varchar(255) null,
  9862.         COLUMN_FLAGS        int not null,
  9863.         IS_NULLABLE        bit not null,
  9864.         DATA_TYPE        smallint not null,
  9865.         TYPE_GUID        binary(16) null,
  9866.         CHARACTER_MAXIMUM_LENGTH int null,
  9867.         CHARACTER_OCTET_LENGTH    int null,
  9868.         NUMERIC_PRECISION    smallint null,
  9869.         NUMERIC_SCALE        smallint null,
  9870.         DATETIME_PRECISION    int null,
  9871.         CHARACTER_SET_CATALOG    varchar(30) null,
  9872.         CHARACTER_SET_SCHEMA    varchar(30) null,
  9873.         CHARACTER_SET_NAME    varchar(30) null,
  9874.         COLLATION_CATALOG    varchar(30) null,
  9875.         COLLATION_SCHEMA    varchar(30) null,
  9876.         COLLATION_NAME        varchar(30) null,
  9877.         DOMAIN_CATALOG        varchar(30) null,
  9878.         DOMAIN_SCHEMA        varchar(30) null,
  9879.         DOMAIN_NAME        varchar(30) null,
  9880.         DESCRIPTION        varchar(30) null    
  9881.         )
  9882.  
  9883.     declare sysdatabases_cursor insensitive cursor for 
  9884.         select name from master.dbo.sysdatabases
  9885.   
  9886.     set nocount on
  9887.     open sysdatabases_cursor
  9888.       fetch next from sysdatabases_cursor into @database_name 
  9889.     while @@fetch_status = 0
  9890.         begin
  9891.         exec('use '+@database_name+' insert into #spcolumnsrowset '+@cmd)
  9892.         fetch next from sysdatabases_cursor into @database_name 
  9893.         end
  9894.       close sysdatabases_cursor
  9895.     deallocate sysdatabases_cursor
  9896.  
  9897.     if @scrollopt is null
  9898.         begin
  9899.         select * from #spcolumnsrowset order by 1,2,3,7
  9900.         end
  9901.     else
  9902.         begin
  9903.         exec @ret = sp_cursoropen @handle output,
  9904.             'select * from #spcolumnsrowset order by 1,2,3,7',
  9905.                 @scrollopt output, @ccopt output, @rows output
  9906.         end
  9907.     drop table #spcolumnsrowset 
  9908.     return coalesce(@ret,0)
  9909. go
  9910.  
  9911. grant execute on sp_columns_rowset to public
  9912. go
  9913.  
  9914. dump tran master with no_log
  9915. go
  9916.  
  9917.  
  9918. print 'creating sp_foreign_keys_rowset'
  9919. go
  9920.  
  9921. /*    Procedure for 6.0 and 6.5 server */
  9922. CREATE PROCEDURE sp_foreign_keys_rowset
  9923.     (
  9924.      @pk_table_name        varchar(255) = null,
  9925.     @pk_table_schema    varchar(255) = null,
  9926.     @pk_table_catalog    varchar(255) = null,
  9927.     @fk_table_name        varchar(255) = null,
  9928.     @fk_table_schema    varchar(255) = null,
  9929.     @fk_table_catalog    varchar(255) = null
  9930.     )
  9931. as
  9932. IF @pk_table_name is not null
  9933.     BEGIN
  9934.     select
  9935.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  9936.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  9937.         PK_TABLE_NAME         = o1.name,
  9938.         PK_COLUMN_NAME         = c1.name,
  9939.         PK_COLUMN_GUID        = convert(binary(16),null),
  9940.         PK_COLUMN_PROPID    = convert(int,null),
  9941.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  9942.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  9943.         FK_TABLE_NAME         = o2.name,
  9944.         FK_COLUMN_NAME         = c2.name,
  9945.         FK_COLUMN_GUID        = convert(binary(16),null),
  9946.         FK_COLUMN_PROPID    = convert(int,null),
  9947.         ORDINAL            = convert(int,1),
  9948.         UPDATE_RULE        = 'NO ACTION',
  9949.         DELETE_RULE         = 'NO ACTION'        
  9950.     from
  9951.         sysobjects o1, sysobjects o2,
  9952.         syscolumns c1, syscolumns c2,
  9953.         sysreferences r
  9954.     where    
  9955.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  9956.     and    o1.name = @pk_table_name
  9957.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  9958.     and    o1.id = r.rkeyid
  9959.     and    o1.id = c1.id
  9960.     and    c1.colid = r.rkey1
  9961.     and     r.fkeyid = o2.id
  9962.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  9963.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  9964.     and    o2.id = c2.id    
  9965.     and    c2.colid = r.fkey1
  9966.     union all
  9967.     select
  9968.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  9969.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  9970.         PK_TABLE_NAME         = o1.name,
  9971.         PK_COLUMN_NAME         = c1.name,
  9972.         PK_COLUMN_GUID        = convert(binary(16),null),
  9973.         PK_COLUMN_PROPID    = convert(int,null),
  9974.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  9975.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  9976.         FK_TABLE_NAME         = o2.name,
  9977.         FK_COLUMN_NAME         = c2.name,
  9978.         FK_COLUMN_GUID        = convert(binary(16),null),
  9979.         FK_COLUMN_PROPID    = convert(int,null),
  9980.         ORDINAL            = convert(int,2),
  9981.         UPDATE_RULE        = 'NO ACTION',
  9982.         DELETE_RULE         = 'NO ACTION'        
  9983.     from
  9984.         sysobjects o1, sysobjects o2,
  9985.         syscolumns c1, syscolumns c2,
  9986.         sysreferences r
  9987.     where    
  9988.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  9989.     and    o1.name = @pk_table_name
  9990.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  9991.     and    o1.id = r.rkeyid
  9992.     and    o1.id = c1.id
  9993.     and    c1.colid = r.rkey2
  9994.     and    r.keycnt >= 2
  9995.     and     r.fkeyid = o2.id
  9996.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  9997.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  9998.     and    o2.id = c2.id    
  9999.     and    c2.colid = r.fkey2
  10000.     union all
  10001.     select
  10002.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10003.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10004.         PK_TABLE_NAME         = o1.name,
  10005.         PK_COLUMN_NAME         = c1.name,
  10006.         PK_COLUMN_GUID        = convert(binary(16),null),
  10007.         PK_COLUMN_PROPID    = convert(int,null),
  10008.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10009.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  10010.         FK_TABLE_NAME         = o2.name,
  10011.         FK_COLUMN_NAME         = c2.name,
  10012.         FK_COLUMN_GUID        = convert(binary(16),null),
  10013.         FK_COLUMN_PROPID    = convert(int,null),
  10014.         ORDINAL            = convert(int,3),
  10015.         UPDATE_RULE        = 'NO ACTION',
  10016.         DELETE_RULE         = 'NO ACTION'        
  10017.     from
  10018.         sysobjects o1, sysobjects o2,
  10019.         syscolumns c1, syscolumns c2,
  10020.         sysreferences r
  10021.     where    
  10022.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  10023.     and    o1.name = @pk_table_name
  10024.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10025.     and    o1.id = r.rkeyid
  10026.     and    o1.id = c1.id
  10027.     and    c1.colid = r.rkey3
  10028.     and    r.keycnt >= 3
  10029.     and     r.fkeyid = o2.id
  10030.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  10031.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10032.     and    o2.id = c2.id    
  10033.     and    c2.colid = r.fkey3
  10034.     union all
  10035.     select
  10036.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10037.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10038.         PK_TABLE_NAME         = o1.name,
  10039.         PK_COLUMN_NAME         = c1.name,
  10040.         PK_COLUMN_GUID        = convert(binary(16),null),
  10041.         PK_COLUMN_PROPID    = convert(int,null),
  10042.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10043.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  10044.         FK_TABLE_NAME         = o2.name,
  10045.         FK_COLUMN_NAME         = c2.name,
  10046.         FK_COLUMN_GUID        = convert(binary(16),null),
  10047.         FK_COLUMN_PROPID    = convert(int,null),
  10048.         ORDINAL            = convert(int,4),
  10049.         UPDATE_RULE        = 'NO ACTION',
  10050.         DELETE_RULE         = 'NO ACTION'        
  10051.     from
  10052.         sysobjects o1, sysobjects o2,
  10053.         syscolumns c1, syscolumns c2,
  10054.         sysreferences r
  10055.     where    
  10056.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  10057.     and    o1.name = @pk_table_name
  10058.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10059.     and    o1.id = r.rkeyid
  10060.     and    r.keycnt >= 4
  10061.     and    o1.id = c1.id
  10062.     and    c1.colid = r.rkey4
  10063.     and     r.fkeyid = o2.id
  10064.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  10065.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10066.     and    o2.id = c2.id    
  10067.     and    c2.colid = r.fkey4
  10068.     union all
  10069.     select
  10070.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10071.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10072.         PK_TABLE_NAME         = o1.name,
  10073.         PK_COLUMN_NAME         = c1.name,
  10074.         PK_COLUMN_GUID        = convert(binary(16),null),
  10075.         PK_COLUMN_PROPID    = convert(int,null),
  10076.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10077.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  10078.         FK_TABLE_NAME         = o2.name,
  10079.         FK_COLUMN_NAME         = c2.name,
  10080.         FK_COLUMN_GUID        = convert(binary(16),null),
  10081.         FK_COLUMN_PROPID    = convert(int,null),
  10082.         ORDINAL            = convert(int,5),
  10083.         UPDATE_RULE        = 'NO ACTION',
  10084.         DELETE_RULE         = 'NO ACTION'        
  10085.     from
  10086.         sysobjects o1, sysobjects o2,
  10087.         syscolumns c1, syscolumns c2,
  10088.         sysreferences r
  10089.     where    
  10090.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  10091.     and    o1.name = @pk_table_name
  10092.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10093.     and    o1.id = r.rkeyid
  10094.     and    r.keycnt >= 5
  10095.     and    o1.id = c1.id
  10096.     and    c1.colid = r.rkey5
  10097.     and     r.fkeyid = o2.id
  10098.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  10099.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10100.     and    o2.id = c2.id    
  10101.     and    c2.colid = r.fkey5
  10102.     union all
  10103.     select
  10104.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10105.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10106.         PK_TABLE_NAME         = o1.name,
  10107.         PK_COLUMN_NAME         = c1.name,
  10108.         PK_COLUMN_GUID        = convert(binary(16),null),
  10109.         PK_COLUMN_PROPID    = convert(int,null),
  10110.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10111.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  10112.         FK_TABLE_NAME         = o2.name,
  10113.         FK_COLUMN_NAME         = c2.name,
  10114.         FK_COLUMN_GUID        = convert(binary(16),null),
  10115.         FK_COLUMN_PROPID    = convert(int,null),
  10116.         ORDINAL            = convert(int,6),
  10117.         UPDATE_RULE        = 'NO ACTION',
  10118.         DELETE_RULE         = 'NO ACTION'        
  10119.     from
  10120.         sysobjects o1, sysobjects o2,
  10121.         syscolumns c1, syscolumns c2,
  10122.         sysreferences r
  10123.     where    
  10124.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  10125.     and    o1.name = @pk_table_name
  10126.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10127.     and    o1.id = r.rkeyid
  10128.     and    r.keycnt >= 6
  10129.     and    o1.id = c1.id
  10130.     and    c1.colid = r.rkey6
  10131.     and     r.fkeyid = o2.id
  10132.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  10133.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10134.     and    o2.id = c2.id    
  10135.     and    c2.colid = r.fkey6
  10136.     union all
  10137.     select
  10138.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10139.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10140.         PK_TABLE_NAME         = o1.name,
  10141.         PK_COLUMN_NAME         = c1.name,
  10142.         PK_COLUMN_GUID        = convert(binary(16),null),
  10143.         PK_COLUMN_PROPID    = convert(int,null),
  10144.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10145.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  10146.         FK_TABLE_NAME         = o2.name,
  10147.         FK_COLUMN_NAME         = c2.name,
  10148.         FK_COLUMN_GUID        = convert(binary(16),null),
  10149.         FK_COLUMN_PROPID    = convert(int,null),
  10150.         ORDINAL            = convert(int,7),
  10151.         UPDATE_RULE        = 'NO ACTION',
  10152.         DELETE_RULE         = 'NO ACTION'        
  10153.     from
  10154.         sysobjects o1, sysobjects o2,
  10155.         syscolumns c1, syscolumns c2,
  10156.         sysreferences r
  10157.     where    
  10158.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  10159.     and    o1.name = @pk_table_name
  10160.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10161.     and    o1.id = r.rkeyid
  10162.     and    r.keycnt >= 7
  10163.     and    o1.id = c1.id
  10164.     and    c1.colid = r.rkey7
  10165.     and     r.fkeyid = o2.id
  10166.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  10167.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10168.     and    o2.id = c2.id    
  10169.     and    c2.colid = r.fkey7
  10170.     union all
  10171.     select
  10172.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10173.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10174.         PK_TABLE_NAME         = o1.name,
  10175.         PK_COLUMN_NAME         = c1.name,
  10176.         PK_COLUMN_GUID        = convert(binary(16),null),
  10177.         PK_COLUMN_PROPID    = convert(int,null),
  10178.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10179.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  10180.         FK_TABLE_NAME         = o2.name,
  10181.         FK_COLUMN_NAME         = c2.name,
  10182.         FK_COLUMN_GUID        = convert(binary(16),null),
  10183.         FK_COLUMN_PROPID    = convert(int,null),
  10184.         ORDINAL            = convert(int,8),
  10185.         UPDATE_RULE        = 'NO ACTION',
  10186.         DELETE_RULE         = 'NO ACTION'        
  10187.     from
  10188.         sysobjects o1, sysobjects o2,
  10189.         syscolumns c1, syscolumns c2,
  10190.         sysreferences r
  10191.     where    
  10192.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  10193.     and    o1.name = @pk_table_name
  10194.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10195.     and    o1.id = r.rkeyid
  10196.     and    r.keycnt >= 8
  10197.     and    o1.id = c1.id
  10198.     and    c1.colid = r.rkey8
  10199.     and     r.fkeyid = o2.id
  10200.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  10201.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10202.     and    o2.id = c2.id    
  10203.     and    c2.colid = r.fkey8
  10204.     union all
  10205.     select
  10206.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10207.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10208.         PK_TABLE_NAME         = o1.name,
  10209.         PK_COLUMN_NAME         = c1.name,
  10210.         PK_COLUMN_GUID        = convert(binary(16),null),
  10211.         PK_COLUMN_PROPID    = convert(int,null),
  10212.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10213.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  10214.         FK_TABLE_NAME         = o2.name,
  10215.         FK_COLUMN_NAME         = c2.name,
  10216.         FK_COLUMN_GUID        = convert(binary(16),null),
  10217.         FK_COLUMN_PROPID    = convert(int,null),
  10218.         ORDINAL            = convert(int,9),
  10219.         UPDATE_RULE        = 'NO ACTION',
  10220.         DELETE_RULE         = 'NO ACTION'        
  10221.     from
  10222.         sysobjects o1, sysobjects o2,
  10223.         syscolumns c1, syscolumns c2,
  10224.         sysreferences r
  10225.     where    
  10226.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  10227.     and    o1.name = @pk_table_name
  10228.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10229.     and    o1.id = r.rkeyid
  10230.     and    r.keycnt >= 9
  10231.     and    o1.id = c1.id
  10232.     and    c1.colid = r.rkey9
  10233.     and     r.fkeyid = o2.id
  10234.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  10235.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10236.     and    o2.id = c2.id    
  10237.     and    c2.colid = r.fkey9
  10238.     union all
  10239.     select
  10240.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10241.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10242.         PK_TABLE_NAME         = o1.name,
  10243.         PK_COLUMN_NAME         = c1.name,
  10244.         PK_COLUMN_GUID        = convert(binary(16),null),
  10245.         PK_COLUMN_PROPID    = convert(int,null),
  10246.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10247.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  10248.         FK_TABLE_NAME         = o2.name,
  10249.         FK_COLUMN_NAME         = c2.name,
  10250.         FK_COLUMN_GUID        = convert(binary(16),null),
  10251.         FK_COLUMN_PROPID    = convert(int,null),
  10252.         ORDINAL            = convert(int,10),
  10253.         UPDATE_RULE        = 'NO ACTION',
  10254.         DELETE_RULE         = 'NO ACTION'        
  10255.     from
  10256.         sysobjects o1, sysobjects o2,
  10257.         syscolumns c1, syscolumns c2,
  10258.         sysreferences r
  10259.     where    
  10260.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  10261.     and    o1.name = @pk_table_name
  10262.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10263.     and    o1.id = r.rkeyid
  10264.     and    r.keycnt >= 10
  10265.     and    o1.id = c1.id
  10266.     and    c1.colid = r.rkey10
  10267.     and     r.fkeyid = o2.id
  10268.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  10269.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10270.     and    o2.id = c2.id    
  10271.     and    c2.colid = r.fkey10
  10272.     union all
  10273.     select
  10274.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10275.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10276.         PK_TABLE_NAME         = o1.name,
  10277.         PK_COLUMN_NAME         = c1.name,
  10278.         PK_COLUMN_GUID        = convert(binary(16),null),
  10279.         PK_COLUMN_PROPID    = convert(int,null),
  10280.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10281.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  10282.         FK_TABLE_NAME         = o2.name,
  10283.         FK_COLUMN_NAME         = c2.name,
  10284.         FK_COLUMN_GUID        = convert(binary(16),null),
  10285.         FK_COLUMN_PROPID    = convert(int,null),
  10286.         ORDINAL            = convert(int,11),
  10287.         UPDATE_RULE        = 'NO ACTION',
  10288.         DELETE_RULE         = 'NO ACTION'        
  10289.     from
  10290.         sysobjects o1, sysobjects o2,
  10291.         syscolumns c1, syscolumns c2,
  10292.         sysreferences r
  10293.     where    
  10294.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  10295.     and    o1.name = @pk_table_name
  10296.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10297.     and    o1.id = r.rkeyid
  10298.     and    r.keycnt >= 11
  10299.     and    o1.id = c1.id
  10300.     and    c1.colid = r.rkey11
  10301.     and     r.fkeyid = o2.id
  10302.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  10303.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10304.     and    o2.id = c2.id    
  10305.     and    c2.colid = r.fkey11
  10306.     union all
  10307.     select
  10308.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10309.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10310.         PK_TABLE_NAME         = o1.name,
  10311.         PK_COLUMN_NAME         = c1.name,
  10312.         PK_COLUMN_GUID        = convert(binary(16),null),
  10313.         PK_COLUMN_PROPID    = convert(int,null),
  10314.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10315.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  10316.         FK_TABLE_NAME         = o2.name,
  10317.         FK_COLUMN_NAME         = c2.name,
  10318.         FK_COLUMN_GUID        = convert(binary(16),null),
  10319.         FK_COLUMN_PROPID    = convert(int,null),
  10320.         ORDINAL            = convert(int,12),
  10321.         UPDATE_RULE        = 'NO ACTION',
  10322.         DELETE_RULE         = 'NO ACTION'        
  10323.     from
  10324.         sysobjects o1, sysobjects o2,
  10325.         syscolumns c1, syscolumns c2,
  10326.         sysreferences r
  10327.     where    
  10328.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  10329.     and    o1.name = @pk_table_name
  10330.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10331.     and    o1.id = r.rkeyid
  10332.     and    r.keycnt >= 12
  10333.     and    o1.id = c1.id
  10334.     and    c1.colid = r.rkey12
  10335.     and     r.fkeyid = o2.id
  10336.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  10337.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10338.     and    o2.id = c2.id    
  10339.     and    c2.colid = r.fkey12
  10340.     union all
  10341.     select
  10342.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10343.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10344.         PK_TABLE_NAME         = o1.name,
  10345.         PK_COLUMN_NAME         = c1.name,
  10346.         PK_COLUMN_GUID        = convert(binary(16),null),
  10347.         PK_COLUMN_PROPID    = convert(int,null),
  10348.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10349.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  10350.         FK_TABLE_NAME         = o2.name,
  10351.         FK_COLUMN_NAME         = c2.name,
  10352.         FK_COLUMN_GUID        = convert(binary(16),null),
  10353.         FK_COLUMN_PROPID    = convert(int,null),
  10354.         ORDINAL            = convert(int,13),
  10355.         UPDATE_RULE        = 'NO ACTION',
  10356.         DELETE_RULE         = 'NO ACTION'        
  10357.     from
  10358.         sysobjects o1, sysobjects o2,
  10359.         syscolumns c1, syscolumns c2,
  10360.         sysreferences r
  10361.     where    
  10362.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  10363.     and    o1.name = @pk_table_name
  10364.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10365.     and    o1.id = r.rkeyid
  10366.     and    r.keycnt >= 13
  10367.     and    o1.id = c1.id
  10368.     and    c1.colid = r.rkey13
  10369.     and     r.fkeyid = o2.id
  10370.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  10371.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10372.     and    o2.id = c2.id    
  10373.     and    c2.colid = r.fkey13
  10374.     union all
  10375.     select
  10376.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10377.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10378.         PK_TABLE_NAME         = o1.name,
  10379.         PK_COLUMN_NAME         = c1.name,
  10380.         PK_COLUMN_GUID        = convert(binary(16),null),
  10381.         PK_COLUMN_PROPID    = convert(int,null),
  10382.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10383.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  10384.         FK_TABLE_NAME         = o2.name,
  10385.         FK_COLUMN_NAME         = c2.name,
  10386.         FK_COLUMN_GUID        = convert(binary(16),null),
  10387.         FK_COLUMN_PROPID    = convert(int,null),
  10388.         ORDINAL            = convert(int,14),
  10389.         UPDATE_RULE        = 'NO ACTION',
  10390.         DELETE_RULE         = 'NO ACTION'        
  10391.     from
  10392.         sysobjects o1, sysobjects o2,
  10393.         syscolumns c1, syscolumns c2,
  10394.         sysreferences r
  10395.     where    
  10396.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  10397.     and    o1.name = @pk_table_name
  10398.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10399.     and    o1.id = r.rkeyid
  10400.     and    r.keycnt >= 14
  10401.     and    o1.id = c1.id
  10402.     and    c1.colid = r.rkey14
  10403.     and     r.fkeyid = o2.id
  10404.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  10405.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10406.     and    o2.id = c2.id    
  10407.     and    c2.colid = r.fkey14
  10408.     union all
  10409.     select
  10410.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10411.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10412.         PK_TABLE_NAME         = o1.name,
  10413.         PK_COLUMN_NAME         = c1.name,
  10414.         PK_COLUMN_GUID        = convert(binary(16),null),
  10415.         PK_COLUMN_PROPID    = convert(int,null),
  10416.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10417.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  10418.         FK_TABLE_NAME         = o2.name,
  10419.         FK_COLUMN_NAME         = c2.name,
  10420.         FK_COLUMN_GUID        = convert(binary(16),null),
  10421.         FK_COLUMN_PROPID    = convert(int,null),
  10422.         ORDINAL            = convert(int,15),
  10423.         UPDATE_RULE        = 'NO ACTION',
  10424.         DELETE_RULE         = 'NO ACTION'        
  10425.     from
  10426.         sysobjects o1, sysobjects o2,
  10427.         syscolumns c1, syscolumns c2,
  10428.         sysreferences r
  10429.     where    
  10430.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  10431.     and    o1.name = @pk_table_name
  10432.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10433.     and    o1.id = r.rkeyid
  10434.     and    r.keycnt >= 15
  10435.     and    o1.id = c1.id
  10436.     and    c1.colid = r.rkey15
  10437.     and     r.fkeyid = o2.id
  10438.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  10439.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10440.     and    o2.id = c2.id    
  10441.     and    c2.colid = r.fkey15
  10442.     union all
  10443.     select
  10444.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10445.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10446.         PK_TABLE_NAME         = o1.name,
  10447.         PK_COLUMN_NAME         = c1.name,
  10448.         PK_COLUMN_GUID        = convert(binary(16),null),
  10449.         PK_COLUMN_PROPID    = convert(int,null),
  10450.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10451.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  10452.         FK_TABLE_NAME         = o2.name,
  10453.         FK_COLUMN_NAME         = c2.name,
  10454.         FK_COLUMN_GUID        = convert(binary(16),null),
  10455.         FK_COLUMN_PROPID    = convert(int,null),
  10456.         ORDINAL            = convert(int,16),
  10457.         UPDATE_RULE        = 'NO ACTION',
  10458.         DELETE_RULE         = 'NO ACTION'        
  10459.     from
  10460.         sysobjects o1, sysobjects o2,
  10461.         syscolumns c1, syscolumns c2,
  10462.         sysreferences r
  10463.     where    
  10464.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  10465.     and    o1.name = @pk_table_name
  10466.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10467.     and    o1.id = r.rkeyid
  10468.     and    r.keycnt >= 16
  10469.     and    o1.id = c1.id
  10470.     and    c1.colid = r.rkey16
  10471.     and     r.fkeyid = o2.id
  10472.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  10473.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10474.     and    o2.id = c2.id    
  10475.     and    c2.colid = r.fkey16
  10476.     order by 8,9,2,3,13
  10477.     END
  10478. ELSE IF @fk_table_name is not null
  10479.     BEGIN
  10480.     select
  10481.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10482.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10483.         PK_TABLE_NAME         = o1.name,
  10484.         PK_COLUMN_NAME         = c1.name,
  10485.         PK_COLUMN_GUID        = convert(binary(16),null),
  10486.         PK_COLUMN_PROPID    = convert(int,null),
  10487.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10488.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  10489.         FK_TABLE_NAME         = o2.name,
  10490.         FK_COLUMN_NAME         = c2.name,
  10491.         FK_COLUMN_GUID        = convert(binary(16),null),
  10492.         FK_COLUMN_PROPID    = convert(int,null),
  10493.         ORDINAL            = convert(int,1),
  10494.         UPDATE_RULE        = 'NO ACTION',
  10495.         DELETE_RULE         = 'NO ACTION'        
  10496.     from
  10497.         sysobjects o1, sysobjects o2,
  10498.         syscolumns c1, syscolumns c2,
  10499.         sysreferences r
  10500.     where    
  10501.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  10502.     and    o2.name = @fk_table_name
  10503.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10504.     and    o2.id = r.fkeyid
  10505.     and    o2.id = c2.id
  10506.     and    c2.colid = r.fkey1
  10507.     and     r.rkeyid = o1.id
  10508.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  10509.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10510.     and    o1.id = c1.id    
  10511.     and    c1.colid = r.rkey1
  10512.     union    all
  10513.     select
  10514.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10515.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10516.         PK_TABLE_NAME         = o1.name,
  10517.         PK_COLUMN_NAME         = c1.name,
  10518.         PK_COLUMN_GUID        = convert(binary(16),null),
  10519.         PK_COLUMN_PROPID    = convert(int,null),
  10520.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10521.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  10522.         FK_TABLE_NAME         = o2.name,
  10523.         FK_COLUMN_NAME         = c2.name,
  10524.         FK_COLUMN_GUID        = convert(binary(16),null),
  10525.         FK_COLUMN_PROPID    = convert(int,null),
  10526.         ORDINAL            = convert(int,2),
  10527.         UPDATE_RULE        = 'NO ACTION',
  10528.         DELETE_RULE         = 'NO ACTION'        
  10529.     from
  10530.         sysobjects o1, sysobjects o2,
  10531.         syscolumns c1, syscolumns c2,
  10532.         sysreferences r
  10533.     where    
  10534.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  10535.     and    o2.name = @fk_table_name
  10536.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10537.     and    o2.id = r.fkeyid
  10538.     and    r.keycnt >= 2
  10539.     and    o2.id = c2.id
  10540.     and    c2.colid = r.fkey2
  10541.     and     r.rkeyid = o1.id
  10542.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  10543.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10544.     and    o1.id = c1.id    
  10545.     and    c1.colid = r.rkey2
  10546.     union    all
  10547.     select
  10548.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10549.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10550.         PK_TABLE_NAME         = o1.name,
  10551.         PK_COLUMN_NAME         = c1.name,
  10552.         PK_COLUMN_GUID        = convert(binary(16),null),
  10553.         PK_COLUMN_PROPID    = convert(int,null),
  10554.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10555.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  10556.         FK_TABLE_NAME         = o2.name,
  10557.         FK_COLUMN_NAME         = c2.name,
  10558.         FK_COLUMN_GUID        = convert(binary(16),null),
  10559.         FK_COLUMN_PROPID    = convert(int,null),
  10560.         ORDINAL            = convert(int,3),
  10561.         UPDATE_RULE        = 'NO ACTION',
  10562.         DELETE_RULE         = 'NO ACTION'        
  10563.     from
  10564.         sysobjects o1, sysobjects o2,
  10565.         syscolumns c1, syscolumns c2,
  10566.         sysreferences r
  10567.     where    
  10568.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  10569.     and    o2.name = @fk_table_name
  10570.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10571.     and    o2.id = r.fkeyid
  10572.     and    r.keycnt >= 3
  10573.     and    o2.id = c2.id
  10574.     and    c2.colid = r.fkey3
  10575.     and     r.rkeyid = o1.id
  10576.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  10577.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10578.     and    o1.id = c1.id    
  10579.     and    c1.colid = r.rkey3
  10580.     union    all
  10581.     select
  10582.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10583.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10584.         PK_TABLE_NAME         = o1.name,
  10585.         PK_COLUMN_NAME         = c1.name,
  10586.         PK_COLUMN_GUID        = convert(binary(16),null),
  10587.         PK_COLUMN_PROPID    = convert(int,null),
  10588.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10589.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  10590.         FK_TABLE_NAME         = o2.name,
  10591.         FK_COLUMN_NAME         = c2.name,
  10592.         FK_COLUMN_GUID        = convert(binary(16),null),
  10593.         FK_COLUMN_PROPID    = convert(int,null),
  10594.         ORDINAL            = convert(int,4),
  10595.         UPDATE_RULE        = 'NO ACTION',
  10596.         DELETE_RULE         = 'NO ACTION'        
  10597.     from
  10598.         sysobjects o1, sysobjects o2,
  10599.         syscolumns c1, syscolumns c2,
  10600.         sysreferences r
  10601.     where    
  10602.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  10603.     and    o2.name = @fk_table_name
  10604.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10605.     and    o2.id = r.fkeyid
  10606.     and    r.keycnt >= 4
  10607.     and    o2.id = c2.id
  10608.     and    c2.colid = r.fkey4
  10609.     and     r.rkeyid = o1.id
  10610.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  10611.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10612.     and    o1.id = c1.id    
  10613.     and    c1.colid = r.rkey4
  10614.     union    all
  10615.     select
  10616.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10617.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10618.         PK_TABLE_NAME         = o1.name,
  10619.         PK_COLUMN_NAME         = c1.name,
  10620.         PK_COLUMN_GUID        = convert(binary(16),null),
  10621.         PK_COLUMN_PROPID    = convert(int,null),
  10622.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10623.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  10624.         FK_TABLE_NAME         = o2.name,
  10625.         FK_COLUMN_NAME         = c2.name,
  10626.         FK_COLUMN_GUID        = convert(binary(16),null),
  10627.         FK_COLUMN_PROPID    = convert(int,null),
  10628.         ORDINAL            = convert(int,5),
  10629.         UPDATE_RULE        = 'NO ACTION',
  10630.         DELETE_RULE         = 'NO ACTION'        
  10631.     from
  10632.         sysobjects o1, sysobjects o2,
  10633.         syscolumns c1, syscolumns c2,
  10634.         sysreferences r
  10635.     where    
  10636.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  10637.     and    o2.name = @fk_table_name
  10638.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10639.     and    o2.id = r.fkeyid
  10640.     and    r.keycnt >= 5
  10641.     and    o2.id = c2.id
  10642.     and    c2.colid = r.fkey5
  10643.     and     r.rkeyid = o1.id
  10644.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  10645.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10646.     and    o1.id = c1.id    
  10647.     and    c1.colid = r.rkey5
  10648.     union    all
  10649.     select
  10650.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10651.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10652.         PK_TABLE_NAME         = o1.name,
  10653.         PK_COLUMN_NAME         = c1.name,
  10654.         PK_COLUMN_GUID        = convert(binary(16),null),
  10655.         PK_COLUMN_PROPID    = convert(int,null),
  10656.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10657.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  10658.         FK_TABLE_NAME         = o2.name,
  10659.         FK_COLUMN_NAME         = c2.name,
  10660.         FK_COLUMN_GUID        = convert(binary(16),null),
  10661.         FK_COLUMN_PROPID    = convert(int,null),
  10662.         ORDINAL            = convert(int,6),
  10663.         UPDATE_RULE        = 'NO ACTION',
  10664.         DELETE_RULE         = 'NO ACTION'        
  10665.     from
  10666.         sysobjects o1, sysobjects o2,
  10667.         syscolumns c1, syscolumns c2,
  10668.         sysreferences r
  10669.     where    
  10670.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  10671.     and    o2.name = @fk_table_name
  10672.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10673.     and    o2.id = r.fkeyid
  10674.     and    r.keycnt >= 6
  10675.     and    o2.id = c2.id
  10676.     and    c2.colid = r.fkey6
  10677.     and     r.rkeyid = o1.id
  10678.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  10679.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10680.     and    o1.id = c1.id    
  10681.     and    c1.colid = r.rkey6
  10682.     union    all
  10683.     select
  10684.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10685.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10686.         PK_TABLE_NAME         = o1.name,
  10687.         PK_COLUMN_NAME         = c1.name,
  10688.         PK_COLUMN_GUID        = convert(binary(16),null),
  10689.         PK_COLUMN_PROPID    = convert(int,null),
  10690.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10691.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  10692.         FK_TABLE_NAME         = o2.name,
  10693.         FK_COLUMN_NAME         = c2.name,
  10694.         FK_COLUMN_GUID        = convert(binary(16),null),
  10695.         FK_COLUMN_PROPID    = convert(int,null),
  10696.         ORDINAL            = convert(int,7),
  10697.         UPDATE_RULE        = 'NO ACTION',
  10698.         DELETE_RULE         = 'NO ACTION'        
  10699.     from
  10700.         sysobjects o1, sysobjects o2,
  10701.         syscolumns c1, syscolumns c2,
  10702.         sysreferences r
  10703.     where    
  10704.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  10705.     and    o2.name = @fk_table_name
  10706.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10707.     and    o2.id = r.fkeyid
  10708.     and    r.keycnt >= 7
  10709.     and    o2.id = c2.id
  10710.     and    c2.colid = r.fkey7
  10711.     and     r.rkeyid = o1.id
  10712.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  10713.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10714.     and    o1.id = c1.id    
  10715.     and    c1.colid = r.rkey7
  10716.     union    all
  10717.     select
  10718.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10719.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10720.         PK_TABLE_NAME         = o1.name,
  10721.         PK_COLUMN_NAME         = c1.name,
  10722.         PK_COLUMN_GUID        = convert(binary(16),null),
  10723.         PK_COLUMN_PROPID    = convert(int,null),
  10724.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10725.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  10726.         FK_TABLE_NAME         = o2.name,
  10727.         FK_COLUMN_NAME         = c2.name,
  10728.         FK_COLUMN_GUID        = convert(binary(16),null),
  10729.         FK_COLUMN_PROPID    = convert(int,null),
  10730.         ORDINAL            = convert(int,8),
  10731.         UPDATE_RULE        = 'NO ACTION',
  10732.         DELETE_RULE         = 'NO ACTION'        
  10733.     from
  10734.         sysobjects o1, sysobjects o2,
  10735.         syscolumns c1, syscolumns c2,
  10736.         sysreferences r
  10737.     where    
  10738.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  10739.     and    o2.name = @fk_table_name
  10740.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10741.     and    o2.id = r.fkeyid
  10742.     and    r.keycnt >= 8
  10743.     and    o2.id = c2.id
  10744.     and    c2.colid = r.fkey8
  10745.     and     r.rkeyid = o1.id
  10746.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  10747.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10748.     and    o1.id = c1.id    
  10749.     and    c1.colid = r.rkey8
  10750.     union    all
  10751.     select
  10752.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10753.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10754.         PK_TABLE_NAME         = o1.name,
  10755.         PK_COLUMN_NAME         = c1.name,
  10756.         PK_COLUMN_GUID        = convert(binary(16),null),
  10757.         PK_COLUMN_PROPID    = convert(int,null),
  10758.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10759.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  10760.         FK_TABLE_NAME         = o2.name,
  10761.         FK_COLUMN_NAME         = c2.name,
  10762.         FK_COLUMN_GUID        = convert(binary(16),null),
  10763.         FK_COLUMN_PROPID    = convert(int,null),
  10764.         ORDINAL            = convert(int,9),
  10765.         UPDATE_RULE        = 'NO ACTION',
  10766.         DELETE_RULE         = 'NO ACTION'        
  10767.     from
  10768.         sysobjects o1, sysobjects o2,
  10769.         syscolumns c1, syscolumns c2,
  10770.         sysreferences r
  10771.     where    
  10772.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  10773.     and    o2.name = @fk_table_name
  10774.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10775.     and    o2.id = r.fkeyid
  10776.     and    r.keycnt >= 9
  10777.     and    o2.id = c2.id
  10778.     and    c2.colid = r.fkey9
  10779.     and     r.rkeyid = o1.id
  10780.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  10781.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10782.     and    o1.id = c1.id    
  10783.     and    c1.colid = r.rkey9
  10784.     union    all
  10785.     select
  10786.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10787.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10788.         PK_TABLE_NAME         = o1.name,
  10789.         PK_COLUMN_NAME         = c1.name,
  10790.         PK_COLUMN_GUID        = convert(binary(16),null),
  10791.         PK_COLUMN_PROPID    = convert(int,null),
  10792.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10793.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  10794.         FK_TABLE_NAME         = o2.name,
  10795.         FK_COLUMN_NAME         = c2.name,
  10796.         FK_COLUMN_GUID        = convert(binary(16),null),
  10797.         FK_COLUMN_PROPID    = convert(int,null),
  10798.         ORDINAL            = convert(int,10),
  10799.         UPDATE_RULE        = 'NO ACTION',
  10800.         DELETE_RULE         = 'NO ACTION'        
  10801.     from
  10802.         sysobjects o1, sysobjects o2,
  10803.         syscolumns c1, syscolumns c2,
  10804.         sysreferences r
  10805.     where    
  10806.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  10807.     and    o2.name = @fk_table_name
  10808.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10809.     and    o2.id = r.fkeyid
  10810.     and    r.keycnt >= 10
  10811.     and    o2.id = c2.id
  10812.     and    c2.colid = r.fkey10
  10813.     and     r.rkeyid = o1.id
  10814.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  10815.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10816.     and    o1.id = c1.id    
  10817.     and    c1.colid = r.rkey10
  10818.     union    all
  10819.     select
  10820.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10821.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10822.         PK_TABLE_NAME         = o1.name,
  10823.         PK_COLUMN_NAME         = c1.name,
  10824.         PK_COLUMN_GUID        = convert(binary(16),null),
  10825.         PK_COLUMN_PROPID    = convert(int,null),
  10826.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10827.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  10828.         FK_TABLE_NAME         = o2.name,
  10829.         FK_COLUMN_NAME         = c2.name,
  10830.         FK_COLUMN_GUID        = convert(binary(16),null),
  10831.         FK_COLUMN_PROPID    = convert(int,null),
  10832.         ORDINAL            = convert(int,11),
  10833.         UPDATE_RULE        = 'NO ACTION',
  10834.         DELETE_RULE         = 'NO ACTION'        
  10835.     from
  10836.         sysobjects o1, sysobjects o2,
  10837.         syscolumns c1, syscolumns c2,
  10838.         sysreferences r
  10839.     where    
  10840.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  10841.     and    o2.name = @fk_table_name
  10842.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10843.     and    o2.id = r.fkeyid
  10844.     and    r.keycnt >= 11
  10845.     and    o2.id = c2.id
  10846.     and    c2.colid = r.fkey11
  10847.     and     r.rkeyid = o1.id
  10848.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  10849.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10850.     and    o1.id = c1.id    
  10851.     and    c1.colid = r.rkey11
  10852.     union    all
  10853.     select
  10854.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10855.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10856.         PK_TABLE_NAME         = o1.name,
  10857.         PK_COLUMN_NAME         = c1.name,
  10858.         PK_COLUMN_GUID        = convert(binary(16),null),
  10859.         PK_COLUMN_PROPID    = convert(int,null),
  10860.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10861.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  10862.         FK_TABLE_NAME         = o2.name,
  10863.         FK_COLUMN_NAME         = c2.name,
  10864.         FK_COLUMN_GUID        = convert(binary(16),null),
  10865.         FK_COLUMN_PROPID    = convert(int,null),
  10866.         ORDINAL            = convert(int,12),
  10867.         UPDATE_RULE        = 'NO ACTION',
  10868.         DELETE_RULE         = 'NO ACTION'        
  10869.     from
  10870.         sysobjects o1, sysobjects o2,
  10871.         syscolumns c1, syscolumns c2,
  10872.         sysreferences r
  10873.     where    
  10874.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  10875.     and    o2.name = @fk_table_name
  10876.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10877.     and    o2.id = r.fkeyid
  10878.     and    r.keycnt >= 12
  10879.     and    o2.id = c2.id
  10880.     and    c2.colid = r.fkey12
  10881.     and     r.rkeyid = o1.id
  10882.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  10883.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10884.     and    o1.id = c1.id    
  10885.     and    c1.colid = r.rkey12
  10886.     union    all
  10887.     select
  10888.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10889.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10890.         PK_TABLE_NAME         = o1.name,
  10891.         PK_COLUMN_NAME         = c1.name,
  10892.         PK_COLUMN_GUID        = convert(binary(16),null),
  10893.         PK_COLUMN_PROPID    = convert(int,null),
  10894.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10895.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  10896.         FK_TABLE_NAME         = o2.name,
  10897.         FK_COLUMN_NAME         = c2.name,
  10898.         FK_COLUMN_GUID        = convert(binary(16),null),
  10899.         FK_COLUMN_PROPID    = convert(int,null),
  10900.         ORDINAL            = convert(int,13),
  10901.         UPDATE_RULE        = 'NO ACTION',
  10902.         DELETE_RULE         = 'NO ACTION'        
  10903.     from
  10904.         sysobjects o1, sysobjects o2,
  10905.         syscolumns c1, syscolumns c2,
  10906.         sysreferences r
  10907.     where    
  10908.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  10909.     and    o2.name = @fk_table_name
  10910.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10911.     and    o2.id = r.fkeyid
  10912.     and    r.keycnt >= 13
  10913.     and    o2.id = c2.id
  10914.     and    c2.colid = r.fkey13
  10915.     and     r.rkeyid = o1.id
  10916.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  10917.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10918.     and    o1.id = c1.id    
  10919.     and    c1.colid = r.rkey13
  10920.     union    all
  10921.     select
  10922.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10923.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10924.         PK_TABLE_NAME         = o1.name,
  10925.         PK_COLUMN_NAME         = c1.name,
  10926.         PK_COLUMN_GUID        = convert(binary(16),null),
  10927.         PK_COLUMN_PROPID    = convert(int,null),
  10928.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10929.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  10930.         FK_TABLE_NAME         = o2.name,
  10931.         FK_COLUMN_NAME         = c2.name,
  10932.         FK_COLUMN_GUID        = convert(binary(16),null),
  10933.         FK_COLUMN_PROPID    = convert(int,null),
  10934.         ORDINAL            = convert(int,14),
  10935.         UPDATE_RULE        = 'NO ACTION',
  10936.         DELETE_RULE         = 'NO ACTION'        
  10937.     from
  10938.         sysobjects o1, sysobjects o2,
  10939.         syscolumns c1, syscolumns c2,
  10940.         sysreferences r
  10941.     where    
  10942.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  10943.     and    o2.name = @fk_table_name
  10944.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10945.     and    o2.id = r.fkeyid
  10946.     and    r.keycnt >= 14
  10947.     and    o2.id = c2.id
  10948.     and    c2.colid = r.fkey14
  10949.     and     r.rkeyid = o1.id
  10950.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  10951.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10952.     and    o1.id = c1.id    
  10953.     and    c1.colid = r.rkey14
  10954.     union    all
  10955.     select
  10956.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10957.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10958.         PK_TABLE_NAME         = o1.name,
  10959.         PK_COLUMN_NAME         = c1.name,
  10960.         PK_COLUMN_GUID        = convert(binary(16),null),
  10961.         PK_COLUMN_PROPID    = convert(int,null),
  10962.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10963.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  10964.         FK_TABLE_NAME         = o2.name,
  10965.         FK_COLUMN_NAME         = c2.name,
  10966.         FK_COLUMN_GUID        = convert(binary(16),null),
  10967.         FK_COLUMN_PROPID    = convert(int,null),
  10968.         ORDINAL            = convert(int,15),
  10969.         UPDATE_RULE        = 'NO ACTION',
  10970.         DELETE_RULE         = 'NO ACTION'        
  10971.     from
  10972.         sysobjects o1, sysobjects o2,
  10973.         syscolumns c1, syscolumns c2,
  10974.         sysreferences r
  10975.     where    
  10976.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  10977.     and    o2.name = @fk_table_name
  10978.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10979.     and    o2.id = r.fkeyid
  10980.     and    r.keycnt >= 15
  10981.     and    o2.id = c2.id
  10982.     and    c2.colid = r.fkey15
  10983.     and     r.rkeyid = o1.id
  10984.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  10985.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10986.     and    o1.id = c1.id    
  10987.     and    c1.colid = r.rkey15
  10988.     union    all
  10989.     select
  10990.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10991.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10992.         PK_TABLE_NAME         = o1.name,
  10993.         PK_COLUMN_NAME         = c1.name,
  10994.         PK_COLUMN_GUID        = convert(binary(16),null),
  10995.         PK_COLUMN_PROPID    = convert(int,null),
  10996.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10997.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  10998.         FK_TABLE_NAME         = o2.name,
  10999.         FK_COLUMN_NAME         = c2.name,
  11000.         FK_COLUMN_GUID        = convert(binary(16),null),
  11001.         FK_COLUMN_PROPID    = convert(int,null),
  11002.         ORDINAL            = convert(int,16),
  11003.         UPDATE_RULE        = 'NO ACTION',
  11004.         DELETE_RULE         = 'NO ACTION'        
  11005.     from
  11006.         sysobjects o1, sysobjects o2,
  11007.         syscolumns c1, syscolumns c2,
  11008.         sysreferences r
  11009.     where    
  11010.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  11011.     and    o2.name = @fk_table_name
  11012.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11013.     and    o2.id = r.fkeyid
  11014.     and    r.keycnt >= 16
  11015.     and    o2.id = c2.id
  11016.     and    c2.colid = r.fkey16
  11017.     and     r.rkeyid = o1.id
  11018.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  11019.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11020.     and    o1.id = c1.id    
  11021.     and    c1.colid = r.rkey16
  11022.     order by 8,9,2,3,13
  11023.     END
  11024. ELSE
  11025.     BEGIN
  11026.     select
  11027.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11028.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11029.         PK_TABLE_NAME         = o1.name,
  11030.         PK_COLUMN_NAME         = c1.name,
  11031.         PK_COLUMN_GUID        = convert(binary(16),null),
  11032.         PK_COLUMN_PROPID    = convert(int,null),
  11033.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11034.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  11035.         FK_TABLE_NAME         = o2.name,
  11036.         FK_COLUMN_NAME         = c2.name,
  11037.         FK_COLUMN_GUID        = convert(binary(16),null),
  11038.         FK_COLUMN_PROPID    = convert(int,null),
  11039.         ORDINAL            = convert(int,1),
  11040.         UPDATE_RULE        = 'NO ACTION',
  11041.         DELETE_RULE         = 'NO ACTION'        
  11042.     from
  11043.         sysobjects o1, sysobjects o2,
  11044.         syscolumns c1, syscolumns c2,
  11045.         sysreferences r
  11046.     where    
  11047.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  11048.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11049.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11050.     and    o1.id = r.rkeyid
  11051.     and    o1.id = c1.id
  11052.     and    c1.colid = r.rkey1
  11053.     and     r.fkeyid = o2.id
  11054.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11055.     and    o2.id = c2.id    
  11056.     and    c2.colid = r.fkey1
  11057.     union all
  11058.     select
  11059.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11060.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11061.         PK_TABLE_NAME         = o1.name,
  11062.         PK_COLUMN_NAME         = c1.name,
  11063.         PK_COLUMN_GUID        = convert(binary(16),null),
  11064.         PK_COLUMN_PROPID    = convert(int,null),
  11065.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11066.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  11067.         FK_TABLE_NAME         = o2.name,
  11068.         FK_COLUMN_NAME         = c2.name,
  11069.         FK_COLUMN_GUID        = convert(binary(16),null),
  11070.         FK_COLUMN_PROPID    = convert(int,null),
  11071.         ORDINAL            = convert(int,2),
  11072.         UPDATE_RULE        = 'NO ACTION',
  11073.         DELETE_RULE         = 'NO ACTION'        
  11074.     from
  11075.         sysobjects o1, sysobjects o2,
  11076.         syscolumns c1, syscolumns c2,
  11077.         sysreferences r
  11078.     where    
  11079.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  11080.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11081.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11082.     and    o1.id = r.rkeyid
  11083.     and    r.keycnt >= 2
  11084.     and    o1.id = c1.id
  11085.     and    c1.colid = r.rkey2
  11086.     and     r.fkeyid = o2.id
  11087.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11088.     and    o2.id = c2.id    
  11089.     and    c2.colid = r.fkey2
  11090.     union all
  11091.     select
  11092.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11093.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11094.         PK_TABLE_NAME         = o1.name,
  11095.         PK_COLUMN_NAME         = c1.name,
  11096.         PK_COLUMN_GUID        = convert(binary(16),null),
  11097.         PK_COLUMN_PROPID    = convert(int,null),
  11098.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11099.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  11100.         FK_TABLE_NAME         = o2.name,
  11101.         FK_COLUMN_NAME         = c2.name,
  11102.         FK_COLUMN_GUID        = convert(binary(16),null),
  11103.         FK_COLUMN_PROPID    = convert(int,null),
  11104.         ORDINAL            = convert(int,3),
  11105.         UPDATE_RULE        = 'NO ACTION',
  11106.         DELETE_RULE         = 'NO ACTION'        
  11107.     from
  11108.         sysobjects o1, sysobjects o2,
  11109.         syscolumns c1, syscolumns c2,
  11110.         sysreferences r
  11111.     where    
  11112.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  11113.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11114.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11115.     and    o1.id = r.rkeyid
  11116.     and    r.keycnt >= 3
  11117.     and    o1.id = c1.id
  11118.     and    c1.colid = r.rkey3
  11119.     and     r.fkeyid = o2.id
  11120.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11121.     and    o2.id = c2.id    
  11122.     and    c2.colid = r.fkey3
  11123.     union all
  11124.     select
  11125.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11126.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11127.         PK_TABLE_NAME         = o1.name,
  11128.         PK_COLUMN_NAME         = c1.name,
  11129.         PK_COLUMN_GUID        = convert(binary(16),null),
  11130.         PK_COLUMN_PROPID    = convert(int,null),
  11131.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11132.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  11133.         FK_TABLE_NAME         = o2.name,
  11134.         FK_COLUMN_NAME         = c2.name,
  11135.         FK_COLUMN_GUID        = convert(binary(16),null),
  11136.         FK_COLUMN_PROPID    = convert(int,null),
  11137.         ORDINAL            = convert(int,4),
  11138.         UPDATE_RULE        = 'NO ACTION',
  11139.         DELETE_RULE         = 'NO ACTION'        
  11140.     from
  11141.         sysobjects o1, sysobjects o2,
  11142.         syscolumns c1, syscolumns c2,
  11143.         sysreferences r
  11144.     where    
  11145.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  11146.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11147.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11148.     and    o1.id = r.rkeyid
  11149.     and    r.keycnt >= 4
  11150.     and    o1.id = c1.id
  11151.     and    c1.colid = r.rkey4
  11152.     and     r.fkeyid = o2.id
  11153.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11154.     and    o2.id = c2.id    
  11155.     and    c2.colid = r.fkey4
  11156.     union all
  11157.     select
  11158.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11159.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11160.         PK_TABLE_NAME         = o1.name,
  11161.         PK_COLUMN_NAME         = c1.name,
  11162.         PK_COLUMN_GUID        = convert(binary(16),null),
  11163.         PK_COLUMN_PROPID    = convert(int,null),
  11164.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11165.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  11166.         FK_TABLE_NAME         = o2.name,
  11167.         FK_COLUMN_NAME         = c2.name,
  11168.         FK_COLUMN_GUID        = convert(binary(16),null),
  11169.         FK_COLUMN_PROPID    = convert(int,null),
  11170.         ORDINAL            = convert(int,5),
  11171.         UPDATE_RULE        = 'NO ACTION',
  11172.         DELETE_RULE         = 'NO ACTION'        
  11173.     from
  11174.         sysobjects o1, sysobjects o2,
  11175.         syscolumns c1, syscolumns c2,
  11176.         sysreferences r
  11177.     where    
  11178.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  11179.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11180.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11181.     and    o1.id = r.rkeyid
  11182.     and    r.keycnt >= 5
  11183.     and    o1.id = c1.id
  11184.     and    c1.colid = r.rkey5
  11185.     and     r.fkeyid = o2.id
  11186.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11187.     and    o2.id = c2.id    
  11188.     and    c2.colid = r.fkey5
  11189.     union all
  11190.     select
  11191.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11192.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11193.         PK_TABLE_NAME         = o1.name,
  11194.         PK_COLUMN_NAME         = c1.name,
  11195.         PK_COLUMN_GUID        = convert(binary(16),null),
  11196.         PK_COLUMN_PROPID    = convert(int,null),
  11197.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11198.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  11199.         FK_TABLE_NAME         = o2.name,
  11200.         FK_COLUMN_NAME         = c2.name,
  11201.         FK_COLUMN_GUID        = convert(binary(16),null),
  11202.         FK_COLUMN_PROPID    = convert(int,null),
  11203.         ORDINAL            = convert(int,6),
  11204.         UPDATE_RULE        = 'NO ACTION',
  11205.         DELETE_RULE         = 'NO ACTION'        
  11206.     from
  11207.         sysobjects o1, sysobjects o2,
  11208.         syscolumns c1, syscolumns c2,
  11209.         sysreferences r
  11210.     where    
  11211.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  11212.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11213.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11214.     and    o1.id = r.rkeyid
  11215.     and    r.keycnt >= 6
  11216.     and    o1.id = c1.id
  11217.     and    c1.colid = r.rkey6
  11218.     and     r.fkeyid = o2.id
  11219.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11220.     and    o2.id = c2.id    
  11221.     and    c2.colid = r.fkey6
  11222.     union all
  11223.     select
  11224.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11225.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11226.         PK_TABLE_NAME         = o1.name,
  11227.         PK_COLUMN_NAME         = c1.name,
  11228.         PK_COLUMN_GUID        = convert(binary(16),null),
  11229.         PK_COLUMN_PROPID    = convert(int,null),
  11230.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11231.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  11232.         FK_TABLE_NAME         = o2.name,
  11233.         FK_COLUMN_NAME         = c2.name,
  11234.         FK_COLUMN_GUID        = convert(binary(16),null),
  11235.         FK_COLUMN_PROPID    = convert(int,null),
  11236.         ORDINAL            = convert(int,7),
  11237.         UPDATE_RULE        = 'NO ACTION',
  11238.         DELETE_RULE         = 'NO ACTION'        
  11239.     from
  11240.         sysobjects o1, sysobjects o2,
  11241.         syscolumns c1, syscolumns c2,
  11242.         sysreferences r
  11243.     where    
  11244.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  11245.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11246.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11247.     and    o1.id = r.rkeyid
  11248.     and    r.keycnt >= 7
  11249.     and    o1.id = c1.id
  11250.     and    c1.colid = r.rkey7
  11251.     and     r.fkeyid = o2.id
  11252.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11253.     and    o2.id = c2.id    
  11254.     and    c2.colid = r.fkey7
  11255.     union all
  11256.     select
  11257.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11258.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11259.         PK_TABLE_NAME         = o1.name,
  11260.         PK_COLUMN_NAME         = c1.name,
  11261.         PK_COLUMN_GUID        = convert(binary(16),null),
  11262.         PK_COLUMN_PROPID    = convert(int,null),
  11263.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11264.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  11265.         FK_TABLE_NAME         = o2.name,
  11266.         FK_COLUMN_NAME         = c2.name,
  11267.         FK_COLUMN_GUID        = convert(binary(16),null),
  11268.         FK_COLUMN_PROPID    = convert(int,null),
  11269.         ORDINAL            = convert(int,8),
  11270.         UPDATE_RULE        = 'NO ACTION',
  11271.         DELETE_RULE         = 'NO ACTION'        
  11272.     from
  11273.         sysobjects o1, sysobjects o2,
  11274.         syscolumns c1, syscolumns c2,
  11275.         sysreferences r
  11276.     where    
  11277.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  11278.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11279.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11280.     and    o1.id = r.rkeyid
  11281.     and    r.keycnt >= 8
  11282.     and    o1.id = c1.id
  11283.     and    c1.colid = r.rkey8
  11284.     and     r.fkeyid = o2.id
  11285.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11286.     and    o2.id = c2.id    
  11287.     and    c2.colid = r.fkey8
  11288.     union all
  11289.     select
  11290.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11291.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11292.         PK_TABLE_NAME         = o1.name,
  11293.         PK_COLUMN_NAME         = c1.name,
  11294.         PK_COLUMN_GUID        = convert(binary(16),null),
  11295.         PK_COLUMN_PROPID    = convert(int,null),
  11296.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11297.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  11298.         FK_TABLE_NAME         = o2.name,
  11299.         FK_COLUMN_NAME         = c2.name,
  11300.         FK_COLUMN_GUID        = convert(binary(16),null),
  11301.         FK_COLUMN_PROPID    = convert(int,null),
  11302.         ORDINAL            = convert(int,9),
  11303.         UPDATE_RULE        = 'NO ACTION',
  11304.         DELETE_RULE         = 'NO ACTION'        
  11305.     from
  11306.         sysobjects o1, sysobjects o2,
  11307.         syscolumns c1, syscolumns c2,
  11308.         sysreferences r
  11309.     where    
  11310.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  11311.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11312.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11313.     and    o1.id = r.rkeyid
  11314.     and    r.keycnt >= 9
  11315.     and    o1.id = c1.id
  11316.     and    c1.colid = r.rkey9
  11317.     and     r.fkeyid = o2.id
  11318.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11319.     and    o2.id = c2.id    
  11320.     and    c2.colid = r.fkey9
  11321.     union all
  11322.     select
  11323.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11324.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11325.         PK_TABLE_NAME         = o1.name,
  11326.         PK_COLUMN_NAME         = c1.name,
  11327.         PK_COLUMN_GUID        = convert(binary(16),null),
  11328.         PK_COLUMN_PROPID    = convert(int,null),
  11329.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11330.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  11331.         FK_TABLE_NAME         = o2.name,
  11332.         FK_COLUMN_NAME         = c2.name,
  11333.         FK_COLUMN_GUID        = convert(binary(16),null),
  11334.         FK_COLUMN_PROPID    = convert(int,null),
  11335.         ORDINAL            = convert(int,10),
  11336.         UPDATE_RULE        = 'NO ACTION',
  11337.         DELETE_RULE         = 'NO ACTION'        
  11338.     from
  11339.         sysobjects o1, sysobjects o2,
  11340.         syscolumns c1, syscolumns c2,
  11341.         sysreferences r
  11342.     where    
  11343.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  11344.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11345.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11346.     and    o1.id = r.rkeyid
  11347.     and    r.keycnt >= 10
  11348.     and    o1.id = c1.id
  11349.     and    c1.colid = r.rkey10
  11350.     and     r.fkeyid = o2.id
  11351.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11352.     and    o2.id = c2.id    
  11353.     and    c2.colid = r.fkey10
  11354.     union all
  11355.     select
  11356.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11357.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11358.         PK_TABLE_NAME         = o1.name,
  11359.         PK_COLUMN_NAME         = c1.name,
  11360.         PK_COLUMN_GUID        = convert(binary(16),null),
  11361.         PK_COLUMN_PROPID    = convert(int,null),
  11362.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11363.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  11364.         FK_TABLE_NAME         = o2.name,
  11365.         FK_COLUMN_NAME         = c2.name,
  11366.         FK_COLUMN_GUID        = convert(binary(16),null),
  11367.         FK_COLUMN_PROPID    = convert(int,null),
  11368.         ORDINAL            = convert(int,11),
  11369.         UPDATE_RULE        = 'NO ACTION',
  11370.         DELETE_RULE         = 'NO ACTION'        
  11371.     from
  11372.         sysobjects o1, sysobjects o2,
  11373.         syscolumns c1, syscolumns c2,
  11374.         sysreferences r
  11375.     where    
  11376.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  11377.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11378.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11379.     and    o1.id = r.rkeyid
  11380.     and    r.keycnt >= 11
  11381.     and    o1.id = c1.id
  11382.     and    c1.colid = r.rkey11
  11383.     and     r.fkeyid = o2.id
  11384.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11385.     and    o2.id = c2.id    
  11386.     and    c2.colid = r.fkey11
  11387.     union all
  11388.     select
  11389.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11390.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11391.         PK_TABLE_NAME         = o1.name,
  11392.         PK_COLUMN_NAME         = c1.name,
  11393.         PK_COLUMN_GUID        = convert(binary(16),null),
  11394.         PK_COLUMN_PROPID    = convert(int,null),
  11395.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11396.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  11397.         FK_TABLE_NAME         = o2.name,
  11398.         FK_COLUMN_NAME         = c2.name,
  11399.         FK_COLUMN_GUID        = convert(binary(16),null),
  11400.         FK_COLUMN_PROPID    = convert(int,null),
  11401.         ORDINAL            = convert(int,12),
  11402.         UPDATE_RULE        = 'NO ACTION',
  11403.         DELETE_RULE         = 'NO ACTION'        
  11404.     from
  11405.         sysobjects o1, sysobjects o2,
  11406.         syscolumns c1, syscolumns c2,
  11407.         sysreferences r
  11408.     where    
  11409.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  11410.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11411.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11412.     and    o1.id = r.rkeyid
  11413.     and    r.keycnt >= 12
  11414.     and    o1.id = c1.id
  11415.     and    c1.colid = r.rkey12
  11416.     and     r.fkeyid = o2.id
  11417.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11418.     and    o2.id = c2.id    
  11419.     and    c2.colid = r.fkey12
  11420.     union all
  11421.     select
  11422.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11423.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11424.         PK_TABLE_NAME         = o1.name,
  11425.         PK_COLUMN_NAME         = c1.name,
  11426.         PK_COLUMN_GUID        = convert(binary(16),null),
  11427.         PK_COLUMN_PROPID    = convert(int,null),
  11428.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11429.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  11430.         FK_TABLE_NAME         = o2.name,
  11431.         FK_COLUMN_NAME         = c2.name,
  11432.         FK_COLUMN_GUID        = convert(binary(16),null),
  11433.         FK_COLUMN_PROPID    = convert(int,null),
  11434.         ORDINAL            = convert(int,13),
  11435.         UPDATE_RULE        = 'NO ACTION',
  11436.         DELETE_RULE         = 'NO ACTION'        
  11437.     from
  11438.         sysobjects o1, sysobjects o2,
  11439.         syscolumns c1, syscolumns c2,
  11440.         sysreferences r
  11441.     where    
  11442.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  11443.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11444.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11445.     and    o1.id = r.rkeyid
  11446.     and    r.keycnt >= 13
  11447.     and    o1.id = c1.id
  11448.     and    c1.colid = r.rkey13
  11449.     and     r.fkeyid = o2.id
  11450.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11451.     and    o2.id = c2.id    
  11452.     and    c2.colid = r.fkey13
  11453.     union all
  11454.     select
  11455.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11456.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11457.         PK_TABLE_NAME         = o1.name,
  11458.         PK_COLUMN_NAME         = c1.name,
  11459.         PK_COLUMN_GUID        = convert(binary(16),null),
  11460.         PK_COLUMN_PROPID    = convert(int,null),
  11461.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11462.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  11463.         FK_TABLE_NAME         = o2.name,
  11464.         FK_COLUMN_NAME         = c2.name,
  11465.         FK_COLUMN_GUID        = convert(binary(16),null),
  11466.         FK_COLUMN_PROPID    = convert(int,null),
  11467.         ORDINAL            = convert(int,14),
  11468.         UPDATE_RULE        = 'NO ACTION',
  11469.         DELETE_RULE         = 'NO ACTION'        
  11470.     from
  11471.         sysobjects o1, sysobjects o2,
  11472.         syscolumns c1, syscolumns c2,
  11473.         sysreferences r
  11474.     where    
  11475.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  11476.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11477.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11478.     and    o1.id = r.rkeyid
  11479.     and    r.keycnt >= 14
  11480.     and    o1.id = c1.id
  11481.     and    c1.colid = r.rkey14
  11482.     and     r.fkeyid = o2.id
  11483.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11484.     and    o2.id = c2.id    
  11485.     and    c2.colid = r.fkey14
  11486.     union all
  11487.     select
  11488.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11489.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11490.         PK_TABLE_NAME         = o1.name,
  11491.         PK_COLUMN_NAME         = c1.name,
  11492.         PK_COLUMN_GUID        = convert(binary(16),null),
  11493.         PK_COLUMN_PROPID    = convert(int,null),
  11494.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11495.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  11496.         FK_TABLE_NAME         = o2.name,
  11497.         FK_COLUMN_NAME         = c2.name,
  11498.         FK_COLUMN_GUID        = convert(binary(16),null),
  11499.         FK_COLUMN_PROPID    = convert(int,null),
  11500.         ORDINAL            = convert(int,15),
  11501.         UPDATE_RULE        = 'NO ACTION',
  11502.         DELETE_RULE         = 'NO ACTION'        
  11503.     from
  11504.         sysobjects o1, sysobjects o2,
  11505.         syscolumns c1, syscolumns c2,
  11506.         sysreferences r
  11507.     where    
  11508.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  11509.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11510.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11511.     and    o1.id = r.rkeyid
  11512.     and    r.keycnt >= 15
  11513.     and    o1.id = c1.id
  11514.     and    c1.colid = r.rkey15
  11515.     and     r.fkeyid = o2.id
  11516.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11517.     and    o2.id = c2.id    
  11518.     and    c2.colid = r.fkey15
  11519.     union all
  11520.     select
  11521.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11522.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11523.         PK_TABLE_NAME         = o1.name,
  11524.         PK_COLUMN_NAME         = c1.name,
  11525.         PK_COLUMN_GUID        = convert(binary(16),null),
  11526.         PK_COLUMN_PROPID    = convert(int,null),
  11527.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11528.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  11529.         FK_TABLE_NAME         = o2.name,
  11530.         FK_COLUMN_NAME         = c2.name,
  11531.         FK_COLUMN_GUID        = convert(binary(16),null),
  11532.         FK_COLUMN_PROPID    = convert(int,null),
  11533.         ORDINAL            = convert(int,16),
  11534.         UPDATE_RULE        = 'NO ACTION',
  11535.         DELETE_RULE         = 'NO ACTION'        
  11536.     from
  11537.         sysobjects o1, sysobjects o2,
  11538.         syscolumns c1, syscolumns c2,
  11539.         sysreferences r
  11540.     where    
  11541.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  11542.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11543.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11544.     and    o1.id = r.rkeyid
  11545.     and    o1.id = c1.id
  11546.     and    r.keycnt >= 16
  11547.     and    c1.colid = r.rkey16
  11548.     and     r.fkeyid = o2.id
  11549.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11550.     and    o2.id = c2.id    
  11551.     and    c2.colid = r.fkey16
  11552.     order by 8,9,2,3,13
  11553.     END
  11554. go
  11555. CREATE PROCEDURE sp_foreign_keys_rowset;2
  11556.     (
  11557.        @handle            int output,
  11558.        @scrollopt        int output,
  11559.     @ccopt            int output,
  11560.     @rows            int output,
  11561.      @pk_table_name        varchar(255) = null,
  11562.     @pk_table_schema    varchar(255) = null,
  11563.     @pk_table_catalog    varchar(255) = null,
  11564.     @fk_table_name        varchar(255) = null,
  11565.     @fk_table_schema    varchar(255) = null,
  11566.     @fk_table_catalog    varchar(255) = null
  11567.     )
  11568. as
  11569.     declare @ret int
  11570.     
  11571.     create table #spfkeysrowset1
  11572.         (
  11573.         PK_TABLE_CATALOG    varchar(30) not null,
  11574.         PK_TABLE_SCHEMA        varchar(30) not null,
  11575.         PK_TABLE_NAME         varchar(30) not null,
  11576.         PK_COLUMN_NAME         varchar(30) not null,
  11577.         PK_COLUMN_GUID        binary(16) null,
  11578.         PK_COLUMN_PROPID    int null,
  11579.         FK_TABLE_CATALOG    varchar(30) not null,
  11580.         FK_TABLE_SCHEMA         varchar(30) not null,
  11581.         FK_TABLE_NAME         varchar(30) not null,
  11582.         FK_COLUMN_NAME         varchar(30) not null,
  11583.         FK_COLUMN_GUID        binary(16) null,
  11584.         FK_COLUMN_PROPID    int null,
  11585.         ORDINAL            int not null,
  11586.         UPDATE_RULE        varchar(30) not null,
  11587.         DELETE_RULE         varchar(30) not null        
  11588.         )
  11589.  
  11590. IF @pk_table_name is not null
  11591.     BEGIN
  11592.     insert into #spfkeysrowset1
  11593.     select
  11594.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11595.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11596.         PK_TABLE_NAME         = o1.name,
  11597.         PK_COLUMN_NAME         = c1.name,
  11598.         PK_COLUMN_GUID        = convert(binary(16),null),
  11599.         PK_COLUMN_PROPID    = convert(int,null),
  11600.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11601.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  11602.         FK_TABLE_NAME         = o2.name,
  11603.         FK_COLUMN_NAME         = c2.name,
  11604.         FK_COLUMN_GUID        = convert(binary(16),null),
  11605.         FK_COLUMN_PROPID    = convert(int,null),
  11606.         ORDINAL            = convert(int,1),
  11607.         UPDATE_RULE        = 'NO ACTION',
  11608.         DELETE_RULE         = 'NO ACTION'        
  11609.     from
  11610.         sysobjects o1, sysobjects o2,
  11611.         syscolumns c1, syscolumns c2,
  11612.         sysreferences r
  11613.     where    
  11614.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11615.     and    o1.name = @pk_table_name
  11616.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11617.     and    o1.id = r.rkeyid
  11618.     and    o1.id = c1.id
  11619.     and    c1.colid = r.rkey1
  11620.     and     r.fkeyid = o2.id
  11621.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  11622.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11623.     and    o2.id = c2.id    
  11624.     and    c2.colid = r.fkey1
  11625.     union all
  11626.     select
  11627.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11628.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11629.         PK_TABLE_NAME         = o1.name,
  11630.         PK_COLUMN_NAME         = c1.name,
  11631.         PK_COLUMN_GUID        = convert(binary(16),null),
  11632.         PK_COLUMN_PROPID    = convert(int,null),
  11633.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11634.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  11635.         FK_TABLE_NAME         = o2.name,
  11636.         FK_COLUMN_NAME         = c2.name,
  11637.         FK_COLUMN_GUID        = convert(binary(16),null),
  11638.         FK_COLUMN_PROPID    = convert(int,null),
  11639.         ORDINAL            = convert(int,2),
  11640.         UPDATE_RULE        = 'NO ACTION',
  11641.         DELETE_RULE         = 'NO ACTION'        
  11642.     from
  11643.         sysobjects o1, sysobjects o2,
  11644.         syscolumns c1, syscolumns c2,
  11645.         sysreferences r
  11646.     where    
  11647.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11648.     and    o1.name = @pk_table_name
  11649.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11650.     and    o1.id = r.rkeyid
  11651.     and    o1.id = c1.id
  11652.     and    c1.colid = r.rkey2
  11653.     and    r.keycnt >= 2
  11654.     and     r.fkeyid = o2.id
  11655.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  11656.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11657.     and    o2.id = c2.id    
  11658.     and    c2.colid = r.fkey2
  11659.     union all
  11660.     select
  11661.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11662.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11663.         PK_TABLE_NAME         = o1.name,
  11664.         PK_COLUMN_NAME         = c1.name,
  11665.         PK_COLUMN_GUID        = convert(binary(16),null),
  11666.         PK_COLUMN_PROPID    = convert(int,null),
  11667.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11668.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  11669.         FK_TABLE_NAME         = o2.name,
  11670.         FK_COLUMN_NAME         = c2.name,
  11671.         FK_COLUMN_GUID        = convert(binary(16),null),
  11672.         FK_COLUMN_PROPID    = convert(int,null),
  11673.         ORDINAL            = convert(int,3),
  11674.         UPDATE_RULE        = 'NO ACTION',
  11675.         DELETE_RULE         = 'NO ACTION'        
  11676.     from
  11677.         sysobjects o1, sysobjects o2,
  11678.         syscolumns c1, syscolumns c2,
  11679.         sysreferences r
  11680.     where    
  11681.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11682.     and    o1.name = @pk_table_name
  11683.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11684.     and    o1.id = r.rkeyid
  11685.     and    o1.id = c1.id
  11686.     and    c1.colid = r.rkey3
  11687.     and    r.keycnt >= 3
  11688.     and     r.fkeyid = o2.id
  11689.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  11690.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11691.     and    o2.id = c2.id    
  11692.     and    c2.colid = r.fkey3
  11693.     union all
  11694.     select
  11695.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11696.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11697.         PK_TABLE_NAME         = o1.name,
  11698.         PK_COLUMN_NAME         = c1.name,
  11699.         PK_COLUMN_GUID        = convert(binary(16),null),
  11700.         PK_COLUMN_PROPID    = convert(int,null),
  11701.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11702.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  11703.         FK_TABLE_NAME         = o2.name,
  11704.         FK_COLUMN_NAME         = c2.name,
  11705.         FK_COLUMN_GUID        = convert(binary(16),null),
  11706.         FK_COLUMN_PROPID    = convert(int,null),
  11707.         ORDINAL            = convert(int,4),
  11708.         UPDATE_RULE        = 'NO ACTION',
  11709.         DELETE_RULE         = 'NO ACTION'        
  11710.     from
  11711.         sysobjects o1, sysobjects o2,
  11712.         syscolumns c1, syscolumns c2,
  11713.         sysreferences r
  11714.     where    
  11715.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11716.     and    o1.name = @pk_table_name
  11717.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11718.     and    o1.id = r.rkeyid
  11719.     and    r.keycnt >= 4
  11720.     and    o1.id = c1.id
  11721.     and    c1.colid = r.rkey4
  11722.     and     r.fkeyid = o2.id
  11723.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  11724.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11725.     and    o2.id = c2.id    
  11726.     and    c2.colid = r.fkey4
  11727.     union all
  11728.     select
  11729.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11730.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11731.         PK_TABLE_NAME         = o1.name,
  11732.         PK_COLUMN_NAME         = c1.name,
  11733.         PK_COLUMN_GUID        = convert(binary(16),null),
  11734.         PK_COLUMN_PROPID    = convert(int,null),
  11735.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11736.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  11737.         FK_TABLE_NAME         = o2.name,
  11738.         FK_COLUMN_NAME         = c2.name,
  11739.         FK_COLUMN_GUID        = convert(binary(16),null),
  11740.         FK_COLUMN_PROPID    = convert(int,null),
  11741.         ORDINAL            = convert(int,5),
  11742.         UPDATE_RULE        = 'NO ACTION',
  11743.         DELETE_RULE         = 'NO ACTION'        
  11744.     from
  11745.         sysobjects o1, sysobjects o2,
  11746.         syscolumns c1, syscolumns c2,
  11747.         sysreferences r
  11748.     where    
  11749.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11750.     and    o1.name = @pk_table_name
  11751.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11752.     and    o1.id = r.rkeyid
  11753.     and    r.keycnt >= 5
  11754.     and    o1.id = c1.id
  11755.     and    c1.colid = r.rkey5
  11756.     and     r.fkeyid = o2.id
  11757.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  11758.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11759.     and    o2.id = c2.id    
  11760.     and    c2.colid = r.fkey5
  11761.     union all
  11762.     select
  11763.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11764.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11765.         PK_TABLE_NAME         = o1.name,
  11766.         PK_COLUMN_NAME         = c1.name,
  11767.         PK_COLUMN_GUID        = convert(binary(16),null),
  11768.         PK_COLUMN_PROPID    = convert(int,null),
  11769.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11770.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  11771.         FK_TABLE_NAME         = o2.name,
  11772.         FK_COLUMN_NAME         = c2.name,
  11773.         FK_COLUMN_GUID        = convert(binary(16),null),
  11774.         FK_COLUMN_PROPID    = convert(int,null),
  11775.         ORDINAL            = convert(int,6),
  11776.         UPDATE_RULE        = 'NO ACTION',
  11777.         DELETE_RULE         = 'NO ACTION'        
  11778.     from
  11779.         sysobjects o1, sysobjects o2,
  11780.         syscolumns c1, syscolumns c2,
  11781.         sysreferences r
  11782.     where    
  11783.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11784.     and    o1.name = @pk_table_name
  11785.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11786.     and    o1.id = r.rkeyid
  11787.     and    r.keycnt >= 6
  11788.     and    o1.id = c1.id
  11789.     and    c1.colid = r.rkey6
  11790.     and     r.fkeyid = o2.id
  11791.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  11792.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11793.     and    o2.id = c2.id    
  11794.     and    c2.colid = r.fkey6
  11795.     union all
  11796.     select
  11797.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11798.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11799.         PK_TABLE_NAME         = o1.name,
  11800.         PK_COLUMN_NAME         = c1.name,
  11801.         PK_COLUMN_GUID        = convert(binary(16),null),
  11802.         PK_COLUMN_PROPID    = convert(int,null),
  11803.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11804.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  11805.         FK_TABLE_NAME         = o2.name,
  11806.         FK_COLUMN_NAME         = c2.name,
  11807.         FK_COLUMN_GUID        = convert(binary(16),null),
  11808.         FK_COLUMN_PROPID    = convert(int,null),
  11809.         ORDINAL            = convert(int,7),
  11810.         UPDATE_RULE        = 'NO ACTION',
  11811.         DELETE_RULE         = 'NO ACTION'        
  11812.     from
  11813.         sysobjects o1, sysobjects o2,
  11814.         syscolumns c1, syscolumns c2,
  11815.         sysreferences r
  11816.     where    
  11817.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11818.     and    o1.name = @pk_table_name
  11819.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11820.     and    o1.id = r.rkeyid
  11821.     and    r.keycnt >= 7
  11822.     and    o1.id = c1.id
  11823.     and    c1.colid = r.rkey7
  11824.     and     r.fkeyid = o2.id
  11825.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  11826.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11827.     and    o2.id = c2.id    
  11828.     and    c2.colid = r.fkey7
  11829.     union all
  11830.     select
  11831.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11832.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11833.         PK_TABLE_NAME         = o1.name,
  11834.         PK_COLUMN_NAME         = c1.name,
  11835.         PK_COLUMN_GUID        = convert(binary(16),null),
  11836.         PK_COLUMN_PROPID    = convert(int,null),
  11837.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11838.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  11839.         FK_TABLE_NAME         = o2.name,
  11840.         FK_COLUMN_NAME         = c2.name,
  11841.         FK_COLUMN_GUID        = convert(binary(16),null),
  11842.         FK_COLUMN_PROPID    = convert(int,null),
  11843.         ORDINAL            = convert(int,8),
  11844.         UPDATE_RULE        = 'NO ACTION',
  11845.         DELETE_RULE         = 'NO ACTION'        
  11846.     from
  11847.         sysobjects o1, sysobjects o2,
  11848.         syscolumns c1, syscolumns c2,
  11849.         sysreferences r
  11850.     where    
  11851.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11852.     and    o1.name = @pk_table_name
  11853.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11854.     and    o1.id = r.rkeyid
  11855.     and    r.keycnt >= 8
  11856.     and    o1.id = c1.id
  11857.     and    c1.colid = r.rkey8
  11858.     and     r.fkeyid = o2.id
  11859.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  11860.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11861.     and    o2.id = c2.id    
  11862.     and    c2.colid = r.fkey8
  11863.     union all
  11864.     select
  11865.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11866.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11867.         PK_TABLE_NAME         = o1.name,
  11868.         PK_COLUMN_NAME         = c1.name,
  11869.         PK_COLUMN_GUID        = convert(binary(16),null),
  11870.         PK_COLUMN_PROPID    = convert(int,null),
  11871.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11872.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  11873.         FK_TABLE_NAME         = o2.name,
  11874.         FK_COLUMN_NAME         = c2.name,
  11875.         FK_COLUMN_GUID        = convert(binary(16),null),
  11876.         FK_COLUMN_PROPID    = convert(int,null),
  11877.         ORDINAL            = convert(int,9),
  11878.         UPDATE_RULE        = 'NO ACTION',
  11879.         DELETE_RULE         = 'NO ACTION'        
  11880.     from
  11881.         sysobjects o1, sysobjects o2,
  11882.         syscolumns c1, syscolumns c2,
  11883.         sysreferences r
  11884.     where    
  11885.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11886.     and    o1.name = @pk_table_name
  11887.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11888.     and    o1.id = r.rkeyid
  11889.     and    r.keycnt >= 9
  11890.     and    o1.id = c1.id
  11891.     and    c1.colid = r.rkey9
  11892.     and     r.fkeyid = o2.id
  11893.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  11894.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11895.     and    o2.id = c2.id    
  11896.     and    c2.colid = r.fkey9
  11897.     union all
  11898.     select
  11899.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11900.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11901.         PK_TABLE_NAME         = o1.name,
  11902.         PK_COLUMN_NAME         = c1.name,
  11903.         PK_COLUMN_GUID        = convert(binary(16),null),
  11904.         PK_COLUMN_PROPID    = convert(int,null),
  11905.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11906.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  11907.         FK_TABLE_NAME         = o2.name,
  11908.         FK_COLUMN_NAME         = c2.name,
  11909.         FK_COLUMN_GUID        = convert(binary(16),null),
  11910.         FK_COLUMN_PROPID    = convert(int,null),
  11911.         ORDINAL            = convert(int,10),
  11912.         UPDATE_RULE        = 'NO ACTION',
  11913.         DELETE_RULE         = 'NO ACTION'        
  11914.     from
  11915.         sysobjects o1, sysobjects o2,
  11916.         syscolumns c1, syscolumns c2,
  11917.         sysreferences r
  11918.     where    
  11919.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11920.     and    o1.name = @pk_table_name
  11921.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11922.     and    o1.id = r.rkeyid
  11923.     and    r.keycnt >= 10
  11924.     and    o1.id = c1.id
  11925.     and    c1.colid = r.rkey10
  11926.     and     r.fkeyid = o2.id
  11927.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  11928.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11929.     and    o2.id = c2.id    
  11930.     and    c2.colid = r.fkey10
  11931.     union all
  11932.     select
  11933.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11934.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11935.         PK_TABLE_NAME         = o1.name,
  11936.         PK_COLUMN_NAME         = c1.name,
  11937.         PK_COLUMN_GUID        = convert(binary(16),null),
  11938.         PK_COLUMN_PROPID    = convert(int,null),
  11939.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11940.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  11941.         FK_TABLE_NAME         = o2.name,
  11942.         FK_COLUMN_NAME         = c2.name,
  11943.         FK_COLUMN_GUID        = convert(binary(16),null),
  11944.         FK_COLUMN_PROPID    = convert(int,null),
  11945.         ORDINAL            = convert(int,11),
  11946.         UPDATE_RULE        = 'NO ACTION',
  11947.         DELETE_RULE         = 'NO ACTION'        
  11948.     from
  11949.         sysobjects o1, sysobjects o2,
  11950.         syscolumns c1, syscolumns c2,
  11951.         sysreferences r
  11952.     where    
  11953.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11954.     and    o1.name = @pk_table_name
  11955.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11956.     and    o1.id = r.rkeyid
  11957.     and    r.keycnt >= 11
  11958.     and    o1.id = c1.id
  11959.     and    c1.colid = r.rkey11
  11960.     and     r.fkeyid = o2.id
  11961.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  11962.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11963.     and    o2.id = c2.id    
  11964.     and    c2.colid = r.fkey11
  11965.     union all
  11966.     select
  11967.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11968.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11969.         PK_TABLE_NAME         = o1.name,
  11970.         PK_COLUMN_NAME         = c1.name,
  11971.         PK_COLUMN_GUID        = convert(binary(16),null),
  11972.         PK_COLUMN_PROPID    = convert(int,null),
  11973.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11974.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  11975.         FK_TABLE_NAME         = o2.name,
  11976.         FK_COLUMN_NAME         = c2.name,
  11977.         FK_COLUMN_GUID        = convert(binary(16),null),
  11978.         FK_COLUMN_PROPID    = convert(int,null),
  11979.         ORDINAL            = convert(int,12),
  11980.         UPDATE_RULE        = 'NO ACTION',
  11981.         DELETE_RULE         = 'NO ACTION'        
  11982.     from
  11983.         sysobjects o1, sysobjects o2,
  11984.         syscolumns c1, syscolumns c2,
  11985.         sysreferences r
  11986.     where    
  11987.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11988.     and    o1.name = @pk_table_name
  11989.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11990.     and    o1.id = r.rkeyid
  11991.     and    r.keycnt >= 12
  11992.     and    o1.id = c1.id
  11993.     and    c1.colid = r.rkey12
  11994.     and     r.fkeyid = o2.id
  11995.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  11996.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11997.     and    o2.id = c2.id    
  11998.     and    c2.colid = r.fkey12
  11999.     union all
  12000.     select
  12001.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12002.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12003.         PK_TABLE_NAME         = o1.name,
  12004.         PK_COLUMN_NAME         = c1.name,
  12005.         PK_COLUMN_GUID        = convert(binary(16),null),
  12006.         PK_COLUMN_PROPID    = convert(int,null),
  12007.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12008.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  12009.         FK_TABLE_NAME         = o2.name,
  12010.         FK_COLUMN_NAME         = c2.name,
  12011.         FK_COLUMN_GUID        = convert(binary(16),null),
  12012.         FK_COLUMN_PROPID    = convert(int,null),
  12013.         ORDINAL            = convert(int,13),
  12014.         UPDATE_RULE        = 'NO ACTION',
  12015.         DELETE_RULE         = 'NO ACTION'        
  12016.     from
  12017.         sysobjects o1, sysobjects o2,
  12018.         syscolumns c1, syscolumns c2,
  12019.         sysreferences r
  12020.     where    
  12021.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12022.     and    o1.name = @pk_table_name
  12023.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12024.     and    o1.id = r.rkeyid
  12025.     and    r.keycnt >= 13
  12026.     and    o1.id = c1.id
  12027.     and    c1.colid = r.rkey13
  12028.     and     r.fkeyid = o2.id
  12029.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  12030.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12031.     and    o2.id = c2.id    
  12032.     and    c2.colid = r.fkey13
  12033.     union all
  12034.     select
  12035.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12036.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12037.         PK_TABLE_NAME         = o1.name,
  12038.         PK_COLUMN_NAME         = c1.name,
  12039.         PK_COLUMN_GUID        = convert(binary(16),null),
  12040.         PK_COLUMN_PROPID    = convert(int,null),
  12041.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12042.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  12043.         FK_TABLE_NAME         = o2.name,
  12044.         FK_COLUMN_NAME         = c2.name,
  12045.         FK_COLUMN_GUID        = convert(binary(16),null),
  12046.         FK_COLUMN_PROPID    = convert(int,null),
  12047.         ORDINAL            = convert(int,14),
  12048.         UPDATE_RULE        = 'NO ACTION',
  12049.         DELETE_RULE         = 'NO ACTION'        
  12050.     from
  12051.         sysobjects o1, sysobjects o2,
  12052.         syscolumns c1, syscolumns c2,
  12053.         sysreferences r
  12054.     where    
  12055.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12056.     and    o1.name = @pk_table_name
  12057.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12058.     and    o1.id = r.rkeyid
  12059.     and    r.keycnt >= 14
  12060.     and    o1.id = c1.id
  12061.     and    c1.colid = r.rkey14
  12062.     and     r.fkeyid = o2.id
  12063.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  12064.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12065.     and    o2.id = c2.id    
  12066.     and    c2.colid = r.fkey14
  12067.     union all
  12068.     select
  12069.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12070.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12071.         PK_TABLE_NAME         = o1.name,
  12072.         PK_COLUMN_NAME         = c1.name,
  12073.         PK_COLUMN_GUID        = convert(binary(16),null),
  12074.         PK_COLUMN_PROPID    = convert(int,null),
  12075.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12076.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  12077.         FK_TABLE_NAME         = o2.name,
  12078.         FK_COLUMN_NAME         = c2.name,
  12079.         FK_COLUMN_GUID        = convert(binary(16),null),
  12080.         FK_COLUMN_PROPID    = convert(int,null),
  12081.         ORDINAL            = convert(int,15),
  12082.         UPDATE_RULE        = 'NO ACTION',
  12083.         DELETE_RULE         = 'NO ACTION'        
  12084.     from
  12085.         sysobjects o1, sysobjects o2,
  12086.         syscolumns c1, syscolumns c2,
  12087.         sysreferences r
  12088.     where    
  12089.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12090.     and    o1.name = @pk_table_name
  12091.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12092.     and    o1.id = r.rkeyid
  12093.     and    r.keycnt >= 15
  12094.     and    o1.id = c1.id
  12095.     and    c1.colid = r.rkey15
  12096.     and     r.fkeyid = o2.id
  12097.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  12098.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12099.     and    o2.id = c2.id    
  12100.     and    c2.colid = r.fkey15
  12101.     union all
  12102.     select
  12103.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12104.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12105.         PK_TABLE_NAME         = o1.name,
  12106.         PK_COLUMN_NAME         = c1.name,
  12107.         PK_COLUMN_GUID        = convert(binary(16),null),
  12108.         PK_COLUMN_PROPID    = convert(int,null),
  12109.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12110.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  12111.         FK_TABLE_NAME         = o2.name,
  12112.         FK_COLUMN_NAME         = c2.name,
  12113.         FK_COLUMN_GUID        = convert(binary(16),null),
  12114.         FK_COLUMN_PROPID    = convert(int,null),
  12115.         ORDINAL            = convert(int,16),
  12116.         UPDATE_RULE        = 'NO ACTION',
  12117.         DELETE_RULE         = 'NO ACTION'        
  12118.     from
  12119.         sysobjects o1, sysobjects o2,
  12120.         syscolumns c1, syscolumns c2,
  12121.         sysreferences r
  12122.     where    
  12123.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12124.     and    o1.name = @pk_table_name
  12125.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12126.     and    o1.id = r.rkeyid
  12127.     and    r.keycnt >= 16
  12128.     and    o1.id = c1.id
  12129.     and    c1.colid = r.rkey16
  12130.     and     r.fkeyid = o2.id
  12131.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  12132.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12133.     and    o2.id = c2.id    
  12134.     and    c2.colid = r.fkey16
  12135.     order by 8,9,2,3,13
  12136.     END
  12137. ELSE IF @fk_table_name is not null
  12138.     BEGIN
  12139.     insert into #spfkeysrowset1
  12140.     select
  12141.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12142.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12143.         PK_TABLE_NAME         = o1.name,
  12144.         PK_COLUMN_NAME         = c1.name,
  12145.         PK_COLUMN_GUID        = convert(binary(16),null),
  12146.         PK_COLUMN_PROPID    = convert(int,null),
  12147.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12148.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  12149.         FK_TABLE_NAME         = o2.name,
  12150.         FK_COLUMN_NAME         = c2.name,
  12151.         FK_COLUMN_GUID        = convert(binary(16),null),
  12152.         FK_COLUMN_PROPID    = convert(int,null),
  12153.         ORDINAL            = convert(int,1),
  12154.         UPDATE_RULE        = 'NO ACTION',
  12155.         DELETE_RULE         = 'NO ACTION'        
  12156.     from
  12157.         sysobjects o1, sysobjects o2,
  12158.         syscolumns c1, syscolumns c2,
  12159.         sysreferences r
  12160.     where    
  12161.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12162.     and    o2.name = @fk_table_name
  12163.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12164.     and    o2.id = r.fkeyid
  12165.     and    o2.id = c2.id
  12166.     and    c2.colid = r.fkey1
  12167.     and     r.rkeyid = o1.id
  12168.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  12169.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12170.     and    o1.id = c1.id    
  12171.     and    c1.colid = r.rkey1
  12172.     union    all
  12173.     select
  12174.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12175.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12176.         PK_TABLE_NAME         = o1.name,
  12177.         PK_COLUMN_NAME         = c1.name,
  12178.         PK_COLUMN_GUID        = convert(binary(16),null),
  12179.         PK_COLUMN_PROPID    = convert(int,null),
  12180.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12181.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  12182.         FK_TABLE_NAME         = o2.name,
  12183.         FK_COLUMN_NAME         = c2.name,
  12184.         FK_COLUMN_GUID        = convert(binary(16),null),
  12185.         FK_COLUMN_PROPID    = convert(int,null),
  12186.         ORDINAL            = convert(int,2),
  12187.         UPDATE_RULE        = 'NO ACTION',
  12188.         DELETE_RULE         = 'NO ACTION'        
  12189.     from
  12190.         sysobjects o1, sysobjects o2,
  12191.         syscolumns c1, syscolumns c2,
  12192.         sysreferences r
  12193.     where    
  12194.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12195.     and    o2.name = @fk_table_name
  12196.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12197.     and    o2.id = r.fkeyid
  12198.     and    r.keycnt >= 2
  12199.     and    o2.id = c2.id
  12200.     and    c2.colid = r.fkey2
  12201.     and     r.rkeyid = o1.id
  12202.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  12203.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12204.     and    o1.id = c1.id    
  12205.     and    c1.colid = r.rkey2
  12206.     union    all
  12207.     select
  12208.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12209.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12210.         PK_TABLE_NAME         = o1.name,
  12211.         PK_COLUMN_NAME         = c1.name,
  12212.         PK_COLUMN_GUID        = convert(binary(16),null),
  12213.         PK_COLUMN_PROPID    = convert(int,null),
  12214.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12215.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  12216.         FK_TABLE_NAME         = o2.name,
  12217.         FK_COLUMN_NAME         = c2.name,
  12218.         FK_COLUMN_GUID        = convert(binary(16),null),
  12219.         FK_COLUMN_PROPID    = convert(int,null),
  12220.         ORDINAL            = convert(int,3),
  12221.         UPDATE_RULE        = 'NO ACTION',
  12222.         DELETE_RULE         = 'NO ACTION'        
  12223.     from
  12224.         sysobjects o1, sysobjects o2,
  12225.         syscolumns c1, syscolumns c2,
  12226.         sysreferences r
  12227.     where    
  12228.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12229.     and    o2.name = @fk_table_name
  12230.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12231.     and    o2.id = r.fkeyid
  12232.     and    r.keycnt >= 3
  12233.     and    o2.id = c2.id
  12234.     and    c2.colid = r.fkey3
  12235.     and     r.rkeyid = o1.id
  12236.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  12237.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12238.     and    o1.id = c1.id    
  12239.     and    c1.colid = r.rkey3
  12240.     union    all
  12241.     select
  12242.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12243.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12244.         PK_TABLE_NAME         = o1.name,
  12245.         PK_COLUMN_NAME         = c1.name,
  12246.         PK_COLUMN_GUID        = convert(binary(16),null),
  12247.         PK_COLUMN_PROPID    = convert(int,null),
  12248.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12249.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  12250.         FK_TABLE_NAME         = o2.name,
  12251.         FK_COLUMN_NAME         = c2.name,
  12252.         FK_COLUMN_GUID        = convert(binary(16),null),
  12253.         FK_COLUMN_PROPID    = convert(int,null),
  12254.         ORDINAL            = convert(int,4),
  12255.         UPDATE_RULE        = 'NO ACTION',
  12256.         DELETE_RULE         = 'NO ACTION'        
  12257.     from
  12258.         sysobjects o1, sysobjects o2,
  12259.         syscolumns c1, syscolumns c2,
  12260.         sysreferences r
  12261.     where    
  12262.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12263.     and    o2.name = @fk_table_name
  12264.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12265.     and    o2.id = r.fkeyid
  12266.     and    r.keycnt >= 4
  12267.     and    o2.id = c2.id
  12268.     and    c2.colid = r.fkey4
  12269.     and     r.rkeyid = o1.id
  12270.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  12271.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12272.     and    o1.id = c1.id    
  12273.     and    c1.colid = r.rkey4
  12274.     union    all
  12275.     select
  12276.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12277.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12278.         PK_TABLE_NAME         = o1.name,
  12279.         PK_COLUMN_NAME         = c1.name,
  12280.         PK_COLUMN_GUID        = convert(binary(16),null),
  12281.         PK_COLUMN_PROPID    = convert(int,null),
  12282.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12283.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  12284.         FK_TABLE_NAME         = o2.name,
  12285.         FK_COLUMN_NAME         = c2.name,
  12286.         FK_COLUMN_GUID        = convert(binary(16),null),
  12287.         FK_COLUMN_PROPID    = convert(int,null),
  12288.         ORDINAL            = convert(int,5),
  12289.         UPDATE_RULE        = 'NO ACTION',
  12290.         DELETE_RULE         = 'NO ACTION'        
  12291.     from
  12292.         sysobjects o1, sysobjects o2,
  12293.         syscolumns c1, syscolumns c2,
  12294.         sysreferences r
  12295.     where    
  12296.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12297.     and    o2.name = @fk_table_name
  12298.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12299.     and    o2.id = r.fkeyid
  12300.     and    r.keycnt >= 5
  12301.     and    o2.id = c2.id
  12302.     and    c2.colid = r.fkey5
  12303.     and     r.rkeyid = o1.id
  12304.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  12305.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12306.     and    o1.id = c1.id    
  12307.     and    c1.colid = r.rkey5
  12308.     union    all
  12309.     select
  12310.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12311.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12312.         PK_TABLE_NAME         = o1.name,
  12313.         PK_COLUMN_NAME         = c1.name,
  12314.         PK_COLUMN_GUID        = convert(binary(16),null),
  12315.         PK_COLUMN_PROPID    = convert(int,null),
  12316.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12317.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  12318.         FK_TABLE_NAME         = o2.name,
  12319.         FK_COLUMN_NAME         = c2.name,
  12320.         FK_COLUMN_GUID        = convert(binary(16),null),
  12321.         FK_COLUMN_PROPID    = convert(int,null),
  12322.         ORDINAL            = convert(int,6),
  12323.         UPDATE_RULE        = 'NO ACTION',
  12324.         DELETE_RULE         = 'NO ACTION'        
  12325.     from
  12326.         sysobjects o1, sysobjects o2,
  12327.         syscolumns c1, syscolumns c2,
  12328.         sysreferences r
  12329.     where    
  12330.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12331.     and    o2.name = @fk_table_name
  12332.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12333.     and    o2.id = r.fkeyid
  12334.     and    r.keycnt >= 6
  12335.     and    o2.id = c2.id
  12336.     and    c2.colid = r.fkey6
  12337.     and     r.rkeyid = o1.id
  12338.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  12339.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12340.     and    o1.id = c1.id    
  12341.     and    c1.colid = r.rkey6
  12342.     union    all
  12343.     select
  12344.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12345.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12346.         PK_TABLE_NAME         = o1.name,
  12347.         PK_COLUMN_NAME         = c1.name,
  12348.         PK_COLUMN_GUID        = convert(binary(16),null),
  12349.         PK_COLUMN_PROPID    = convert(int,null),
  12350.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12351.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  12352.         FK_TABLE_NAME         = o2.name,
  12353.         FK_COLUMN_NAME         = c2.name,
  12354.         FK_COLUMN_GUID        = convert(binary(16),null),
  12355.         FK_COLUMN_PROPID    = convert(int,null),
  12356.         ORDINAL            = convert(int,7),
  12357.         UPDATE_RULE        = 'NO ACTION',
  12358.         DELETE_RULE         = 'NO ACTION'        
  12359.     from
  12360.         sysobjects o1, sysobjects o2,
  12361.         syscolumns c1, syscolumns c2,
  12362.         sysreferences r
  12363.     where    
  12364.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12365.     and    o2.name = @fk_table_name
  12366.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12367.     and    o2.id = r.fkeyid
  12368.     and    r.keycnt >= 7
  12369.     and    o2.id = c2.id
  12370.     and    c2.colid = r.fkey7
  12371.     and     r.rkeyid = o1.id
  12372.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  12373.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12374.     and    o1.id = c1.id    
  12375.     and    c1.colid = r.rkey7
  12376.     union    all
  12377.     select
  12378.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12379.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12380.         PK_TABLE_NAME         = o1.name,
  12381.         PK_COLUMN_NAME         = c1.name,
  12382.         PK_COLUMN_GUID        = convert(binary(16),null),
  12383.         PK_COLUMN_PROPID    = convert(int,null),
  12384.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12385.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  12386.         FK_TABLE_NAME         = o2.name,
  12387.         FK_COLUMN_NAME         = c2.name,
  12388.         FK_COLUMN_GUID        = convert(binary(16),null),
  12389.         FK_COLUMN_PROPID    = convert(int,null),
  12390.         ORDINAL            = convert(int,8),
  12391.         UPDATE_RULE        = 'NO ACTION',
  12392.         DELETE_RULE         = 'NO ACTION'        
  12393.     from
  12394.         sysobjects o1, sysobjects o2,
  12395.         syscolumns c1, syscolumns c2,
  12396.         sysreferences r
  12397.     where    
  12398.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12399.     and    o2.name = @fk_table_name
  12400.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12401.     and    o2.id = r.fkeyid
  12402.     and    r.keycnt >= 8
  12403.     and    o2.id = c2.id
  12404.     and    c2.colid = r.fkey8
  12405.     and     r.rkeyid = o1.id
  12406.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  12407.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12408.     and    o1.id = c1.id    
  12409.     and    c1.colid = r.rkey8
  12410.     union    all
  12411.     select
  12412.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12413.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12414.         PK_TABLE_NAME         = o1.name,
  12415.         PK_COLUMN_NAME         = c1.name,
  12416.         PK_COLUMN_GUID        = convert(binary(16),null),
  12417.         PK_COLUMN_PROPID    = convert(int,null),
  12418.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12419.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  12420.         FK_TABLE_NAME         = o2.name,
  12421.         FK_COLUMN_NAME         = c2.name,
  12422.         FK_COLUMN_GUID        = convert(binary(16),null),
  12423.         FK_COLUMN_PROPID    = convert(int,null),
  12424.         ORDINAL            = convert(int,9),
  12425.         UPDATE_RULE        = 'NO ACTION',
  12426.         DELETE_RULE         = 'NO ACTION'        
  12427.     from
  12428.         sysobjects o1, sysobjects o2,
  12429.         syscolumns c1, syscolumns c2,
  12430.         sysreferences r
  12431.     where    
  12432.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12433.     and    o2.name = @fk_table_name
  12434.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12435.     and    o2.id = r.fkeyid
  12436.     and    r.keycnt >= 9
  12437.     and    o2.id = c2.id
  12438.     and    c2.colid = r.fkey9
  12439.     and     r.rkeyid = o1.id
  12440.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  12441.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12442.     and    o1.id = c1.id    
  12443.     and    c1.colid = r.rkey9
  12444.     union    all
  12445.     select
  12446.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12447.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12448.         PK_TABLE_NAME         = o1.name,
  12449.         PK_COLUMN_NAME         = c1.name,
  12450.         PK_COLUMN_GUID        = convert(binary(16),null),
  12451.         PK_COLUMN_PROPID    = convert(int,null),
  12452.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12453.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  12454.         FK_TABLE_NAME         = o2.name,
  12455.         FK_COLUMN_NAME         = c2.name,
  12456.         FK_COLUMN_GUID        = convert(binary(16),null),
  12457.         FK_COLUMN_PROPID    = convert(int,null),
  12458.         ORDINAL            = convert(int,10),
  12459.         UPDATE_RULE        = 'NO ACTION',
  12460.         DELETE_RULE         = 'NO ACTION'        
  12461.     from
  12462.         sysobjects o1, sysobjects o2,
  12463.         syscolumns c1, syscolumns c2,
  12464.         sysreferences r
  12465.     where    
  12466.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12467.     and    o2.name = @fk_table_name
  12468.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12469.     and    o2.id = r.fkeyid
  12470.     and    r.keycnt >= 10
  12471.     and    o2.id = c2.id
  12472.     and    c2.colid = r.fkey10
  12473.     and     r.rkeyid = o1.id
  12474.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  12475.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12476.     and    o1.id = c1.id    
  12477.     and    c1.colid = r.rkey10
  12478.     union    all
  12479.     select
  12480.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12481.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12482.         PK_TABLE_NAME         = o1.name,
  12483.         PK_COLUMN_NAME         = c1.name,
  12484.         PK_COLUMN_GUID        = convert(binary(16),null),
  12485.         PK_COLUMN_PROPID    = convert(int,null),
  12486.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12487.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  12488.         FK_TABLE_NAME         = o2.name,
  12489.         FK_COLUMN_NAME         = c2.name,
  12490.         FK_COLUMN_GUID        = convert(binary(16),null),
  12491.         FK_COLUMN_PROPID    = convert(int,null),
  12492.         ORDINAL            = convert(int,11),
  12493.         UPDATE_RULE        = 'NO ACTION',
  12494.         DELETE_RULE         = 'NO ACTION'        
  12495.     from
  12496.         sysobjects o1, sysobjects o2,
  12497.         syscolumns c1, syscolumns c2,
  12498.         sysreferences r
  12499.     where    
  12500.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12501.     and    o2.name = @fk_table_name
  12502.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12503.     and    o2.id = r.fkeyid
  12504.     and    r.keycnt >= 11
  12505.     and    o2.id = c2.id
  12506.     and    c2.colid = r.fkey11
  12507.     and     r.rkeyid = o1.id
  12508.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  12509.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12510.     and    o1.id = c1.id    
  12511.     and    c1.colid = r.rkey11
  12512.     union    all
  12513.     select
  12514.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12515.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12516.         PK_TABLE_NAME         = o1.name,
  12517.         PK_COLUMN_NAME         = c1.name,
  12518.         PK_COLUMN_GUID        = convert(binary(16),null),
  12519.         PK_COLUMN_PROPID    = convert(int,null),
  12520.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12521.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  12522.         FK_TABLE_NAME         = o2.name,
  12523.         FK_COLUMN_NAME         = c2.name,
  12524.         FK_COLUMN_GUID        = convert(binary(16),null),
  12525.         FK_COLUMN_PROPID    = convert(int,null),
  12526.         ORDINAL            = convert(int,12),
  12527.         UPDATE_RULE        = 'NO ACTION',
  12528.         DELETE_RULE         = 'NO ACTION'        
  12529.     from
  12530.         sysobjects o1, sysobjects o2,
  12531.         syscolumns c1, syscolumns c2,
  12532.         sysreferences r
  12533.     where    
  12534.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12535.     and    o2.name = @fk_table_name
  12536.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12537.     and    o2.id = r.fkeyid
  12538.     and    r.keycnt >= 12
  12539.     and    o2.id = c2.id
  12540.     and    c2.colid = r.fkey12
  12541.     and     r.rkeyid = o1.id
  12542.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  12543.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12544.     and    o1.id = c1.id    
  12545.     and    c1.colid = r.rkey12
  12546.     union    all
  12547.     select
  12548.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12549.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12550.         PK_TABLE_NAME         = o1.name,
  12551.         PK_COLUMN_NAME         = c1.name,
  12552.         PK_COLUMN_GUID        = convert(binary(16),null),
  12553.         PK_COLUMN_PROPID    = convert(int,null),
  12554.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12555.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  12556.         FK_TABLE_NAME         = o2.name,
  12557.         FK_COLUMN_NAME         = c2.name,
  12558.         FK_COLUMN_GUID        = convert(binary(16),null),
  12559.         FK_COLUMN_PROPID    = convert(int,null),
  12560.         ORDINAL            = convert(int,13),
  12561.         UPDATE_RULE        = 'NO ACTION',
  12562.         DELETE_RULE         = 'NO ACTION'        
  12563.     from
  12564.         sysobjects o1, sysobjects o2,
  12565.         syscolumns c1, syscolumns c2,
  12566.         sysreferences r
  12567.     where    
  12568.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12569.     and    o2.name = @fk_table_name
  12570.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12571.     and    o2.id = r.fkeyid
  12572.     and    r.keycnt >= 13
  12573.     and    o2.id = c2.id
  12574.     and    c2.colid = r.fkey13
  12575.     and     r.rkeyid = o1.id
  12576.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  12577.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12578.     and    o1.id = c1.id    
  12579.     and    c1.colid = r.rkey13
  12580.     union    all
  12581.     select
  12582.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12583.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12584.         PK_TABLE_NAME         = o1.name,
  12585.         PK_COLUMN_NAME         = c1.name,
  12586.         PK_COLUMN_GUID        = convert(binary(16),null),
  12587.         PK_COLUMN_PROPID    = convert(int,null),
  12588.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12589.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  12590.         FK_TABLE_NAME         = o2.name,
  12591.         FK_COLUMN_NAME         = c2.name,
  12592.         FK_COLUMN_GUID        = convert(binary(16),null),
  12593.         FK_COLUMN_PROPID    = convert(int,null),
  12594.         ORDINAL            = convert(int,14),
  12595.         UPDATE_RULE        = 'NO ACTION',
  12596.         DELETE_RULE         = 'NO ACTION'        
  12597.     from
  12598.         sysobjects o1, sysobjects o2,
  12599.         syscolumns c1, syscolumns c2,
  12600.         sysreferences r
  12601.     where    
  12602.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12603.     and    o2.name = @fk_table_name
  12604.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12605.     and    o2.id = r.fkeyid
  12606.     and    r.keycnt >= 14
  12607.     and    o2.id = c2.id
  12608.     and    c2.colid = r.fkey14
  12609.     and     r.rkeyid = o1.id
  12610.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  12611.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12612.     and    o1.id = c1.id    
  12613.     and    c1.colid = r.rkey14
  12614.     union    all
  12615.     select
  12616.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12617.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12618.         PK_TABLE_NAME         = o1.name,
  12619.         PK_COLUMN_NAME         = c1.name,
  12620.         PK_COLUMN_GUID        = convert(binary(16),null),
  12621.         PK_COLUMN_PROPID    = convert(int,null),
  12622.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12623.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  12624.         FK_TABLE_NAME         = o2.name,
  12625.         FK_COLUMN_NAME         = c2.name,
  12626.         FK_COLUMN_GUID        = convert(binary(16),null),
  12627.         FK_COLUMN_PROPID    = convert(int,null),
  12628.         ORDINAL            = convert(int,15),
  12629.         UPDATE_RULE        = 'NO ACTION',
  12630.         DELETE_RULE         = 'NO ACTION'        
  12631.     from
  12632.         sysobjects o1, sysobjects o2,
  12633.         syscolumns c1, syscolumns c2,
  12634.         sysreferences r
  12635.     where    
  12636.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12637.     and    o2.name = @fk_table_name
  12638.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12639.     and    o2.id = r.fkeyid
  12640.     and    r.keycnt >= 15
  12641.     and    o2.id = c2.id
  12642.     and    c2.colid = r.fkey15
  12643.     and     r.rkeyid = o1.id
  12644.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  12645.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12646.     and    o1.id = c1.id    
  12647.     and    c1.colid = r.rkey15
  12648.     union    all
  12649.     select
  12650.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12651.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12652.         PK_TABLE_NAME         = o1.name,
  12653.         PK_COLUMN_NAME         = c1.name,
  12654.         PK_COLUMN_GUID        = convert(binary(16),null),
  12655.         PK_COLUMN_PROPID    = convert(int,null),
  12656.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12657.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  12658.         FK_TABLE_NAME         = o2.name,
  12659.         FK_COLUMN_NAME         = c2.name,
  12660.         FK_COLUMN_GUID        = convert(binary(16),null),
  12661.         FK_COLUMN_PROPID    = convert(int,null),
  12662.         ORDINAL            = convert(int,16),
  12663.         UPDATE_RULE        = 'NO ACTION',
  12664.         DELETE_RULE         = 'NO ACTION'        
  12665.     from
  12666.         sysobjects o1, sysobjects o2,
  12667.         syscolumns c1, syscolumns c2,
  12668.         sysreferences r
  12669.     where    
  12670.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12671.     and    o2.name = @fk_table_name
  12672.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12673.     and    o2.id = r.fkeyid
  12674.     and    r.keycnt >= 16
  12675.     and    o2.id = c2.id
  12676.     and    c2.colid = r.fkey16
  12677.     and     r.rkeyid = o1.id
  12678.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  12679.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12680.     and    o1.id = c1.id    
  12681.     and    c1.colid = r.rkey16
  12682.     order by 8,9,2,3,13
  12683.     END
  12684. ELSE
  12685.     BEGIN
  12686.     insert into #spfkeysrowset1
  12687.     select
  12688.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12689.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12690.         PK_TABLE_NAME         = o1.name,
  12691.         PK_COLUMN_NAME         = c1.name,
  12692.         PK_COLUMN_GUID        = convert(binary(16),null),
  12693.         PK_COLUMN_PROPID    = convert(int,null),
  12694.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12695.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  12696.         FK_TABLE_NAME         = o2.name,
  12697.         FK_COLUMN_NAME         = c2.name,
  12698.         FK_COLUMN_GUID        = convert(binary(16),null),
  12699.         FK_COLUMN_PROPID    = convert(int,null),
  12700.         ORDINAL            = convert(int,1),
  12701.         UPDATE_RULE        = 'NO ACTION',
  12702.         DELETE_RULE         = 'NO ACTION'        
  12703.     from
  12704.         sysobjects o1, sysobjects o2,
  12705.         syscolumns c1, syscolumns c2,
  12706.         sysreferences r
  12707.     where    
  12708.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12709.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12710.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12711.     and    o1.id = r.rkeyid
  12712.     and    o1.id = c1.id
  12713.     and    c1.colid = r.rkey1
  12714.     and     r.fkeyid = o2.id
  12715.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12716.     and    o2.id = c2.id    
  12717.     and    c2.colid = r.fkey1
  12718.     union all
  12719.     select
  12720.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12721.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12722.         PK_TABLE_NAME         = o1.name,
  12723.         PK_COLUMN_NAME         = c1.name,
  12724.         PK_COLUMN_GUID        = convert(binary(16),null),
  12725.         PK_COLUMN_PROPID    = convert(int,null),
  12726.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12727.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  12728.         FK_TABLE_NAME         = o2.name,
  12729.         FK_COLUMN_NAME         = c2.name,
  12730.         FK_COLUMN_GUID        = convert(binary(16),null),
  12731.         FK_COLUMN_PROPID    = convert(int,null),
  12732.         ORDINAL            = convert(int,2),
  12733.         UPDATE_RULE        = 'NO ACTION',
  12734.         DELETE_RULE         = 'NO ACTION'        
  12735.     from
  12736.         sysobjects o1, sysobjects o2,
  12737.         syscolumns c1, syscolumns c2,
  12738.         sysreferences r
  12739.     where    
  12740.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12741.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12742.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12743.     and    o1.id = r.rkeyid
  12744.     and    r.keycnt >= 2
  12745.     and    o1.id = c1.id
  12746.     and    c1.colid = r.rkey2
  12747.     and     r.fkeyid = o2.id
  12748.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12749.     and    o2.id = c2.id    
  12750.     and    c2.colid = r.fkey2
  12751.     union all
  12752.     select
  12753.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12754.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12755.         PK_TABLE_NAME         = o1.name,
  12756.         PK_COLUMN_NAME         = c1.name,
  12757.         PK_COLUMN_GUID        = convert(binary(16),null),
  12758.         PK_COLUMN_PROPID    = convert(int,null),
  12759.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12760.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  12761.         FK_TABLE_NAME         = o2.name,
  12762.         FK_COLUMN_NAME         = c2.name,
  12763.         FK_COLUMN_GUID        = convert(binary(16),null),
  12764.         FK_COLUMN_PROPID    = convert(int,null),
  12765.         ORDINAL            = convert(int,3),
  12766.         UPDATE_RULE        = 'NO ACTION',
  12767.         DELETE_RULE         = 'NO ACTION'        
  12768.     from
  12769.         sysobjects o1, sysobjects o2,
  12770.         syscolumns c1, syscolumns c2,
  12771.         sysreferences r
  12772.     where    
  12773.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12774.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12775.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12776.     and    o1.id = r.rkeyid
  12777.     and    r.keycnt >= 3
  12778.     and    o1.id = c1.id
  12779.     and    c1.colid = r.rkey3
  12780.     and     r.fkeyid = o2.id
  12781.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12782.     and    o2.id = c2.id    
  12783.     and    c2.colid = r.fkey3
  12784.     union all
  12785.     select
  12786.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12787.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12788.         PK_TABLE_NAME         = o1.name,
  12789.         PK_COLUMN_NAME         = c1.name,
  12790.         PK_COLUMN_GUID        = convert(binary(16),null),
  12791.         PK_COLUMN_PROPID    = convert(int,null),
  12792.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12793.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  12794.         FK_TABLE_NAME         = o2.name,
  12795.         FK_COLUMN_NAME         = c2.name,
  12796.         FK_COLUMN_GUID        = convert(binary(16),null),
  12797.         FK_COLUMN_PROPID    = convert(int,null),
  12798.         ORDINAL            = convert(int,4),
  12799.         UPDATE_RULE        = 'NO ACTION',
  12800.         DELETE_RULE         = 'NO ACTION'        
  12801.     from
  12802.         sysobjects o1, sysobjects o2,
  12803.         syscolumns c1, syscolumns c2,
  12804.         sysreferences r
  12805.     where    
  12806.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12807.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12808.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12809.     and    o1.id = r.rkeyid
  12810.     and    r.keycnt >= 4
  12811.     and    o1.id = c1.id
  12812.     and    c1.colid = r.rkey4
  12813.     and     r.fkeyid = o2.id
  12814.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12815.     and    o2.id = c2.id    
  12816.     and    c2.colid = r.fkey4
  12817.     union all
  12818.     select
  12819.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12820.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12821.         PK_TABLE_NAME         = o1.name,
  12822.         PK_COLUMN_NAME         = c1.name,
  12823.         PK_COLUMN_GUID        = convert(binary(16),null),
  12824.         PK_COLUMN_PROPID    = convert(int,null),
  12825.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12826.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  12827.         FK_TABLE_NAME         = o2.name,
  12828.         FK_COLUMN_NAME         = c2.name,
  12829.         FK_COLUMN_GUID        = convert(binary(16),null),
  12830.         FK_COLUMN_PROPID    = convert(int,null),
  12831.         ORDINAL            = convert(int,5),
  12832.         UPDATE_RULE        = 'NO ACTION',
  12833.         DELETE_RULE         = 'NO ACTION'        
  12834.     from
  12835.         sysobjects o1, sysobjects o2,
  12836.         syscolumns c1, syscolumns c2,
  12837.         sysreferences r
  12838.     where    
  12839.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12840.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12841.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12842.     and    o1.id = r.rkeyid
  12843.     and    r.keycnt >= 5
  12844.     and    o1.id = c1.id
  12845.     and    c1.colid = r.rkey5
  12846.     and     r.fkeyid = o2.id
  12847.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12848.     and    o2.id = c2.id    
  12849.     and    c2.colid = r.fkey5
  12850.     union all
  12851.     select
  12852.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12853.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12854.         PK_TABLE_NAME         = o1.name,
  12855.         PK_COLUMN_NAME         = c1.name,
  12856.         PK_COLUMN_GUID        = convert(binary(16),null),
  12857.         PK_COLUMN_PROPID    = convert(int,null),
  12858.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12859.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  12860.         FK_TABLE_NAME         = o2.name,
  12861.         FK_COLUMN_NAME         = c2.name,
  12862.         FK_COLUMN_GUID        = convert(binary(16),null),
  12863.         FK_COLUMN_PROPID    = convert(int,null),
  12864.         ORDINAL            = convert(int,6),
  12865.         UPDATE_RULE        = 'NO ACTION',
  12866.         DELETE_RULE         = 'NO ACTION'        
  12867.     from
  12868.         sysobjects o1, sysobjects o2,
  12869.         syscolumns c1, syscolumns c2,
  12870.         sysreferences r
  12871.     where    
  12872.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12873.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12874.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12875.     and    o1.id = r.rkeyid
  12876.     and    r.keycnt >= 6
  12877.     and    o1.id = c1.id
  12878.     and    c1.colid = r.rkey6
  12879.     and     r.fkeyid = o2.id
  12880.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12881.     and    o2.id = c2.id    
  12882.     and    c2.colid = r.fkey6
  12883.     union all
  12884.     select
  12885.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12886.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12887.         PK_TABLE_NAME         = o1.name,
  12888.         PK_COLUMN_NAME         = c1.name,
  12889.         PK_COLUMN_GUID        = convert(binary(16),null),
  12890.         PK_COLUMN_PROPID    = convert(int,null),
  12891.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12892.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  12893.         FK_TABLE_NAME         = o2.name,
  12894.         FK_COLUMN_NAME         = c2.name,
  12895.         FK_COLUMN_GUID        = convert(binary(16),null),
  12896.         FK_COLUMN_PROPID    = convert(int,null),
  12897.         ORDINAL            = convert(int,7),
  12898.         UPDATE_RULE        = 'NO ACTION',
  12899.         DELETE_RULE         = 'NO ACTION'        
  12900.     from
  12901.         sysobjects o1, sysobjects o2,
  12902.         syscolumns c1, syscolumns c2,
  12903.         sysreferences r
  12904.     where    
  12905.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12906.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12907.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12908.     and    o1.id = r.rkeyid
  12909.     and    r.keycnt >= 7
  12910.     and    o1.id = c1.id
  12911.     and    c1.colid = r.rkey7
  12912.     and     r.fkeyid = o2.id
  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.fkey7
  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(binary(16),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(binary(16),null),
  12929.         FK_COLUMN_PROPID    = convert(int,null),
  12930.         ORDINAL            = convert(int,8),
  12931.         UPDATE_RULE        = 'NO ACTION',
  12932.         DELETE_RULE         = 'NO ACTION'        
  12933.     from
  12934.         sysobjects o1, sysobjects o2,
  12935.         syscolumns c1, syscolumns c2,
  12936.         sysreferences r
  12937.     where    
  12938.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12939.     and     (@fk_table_catalog is null or @fk_table_catalog = db_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 >= 8
  12943.     and    o1.id = c1.id
  12944.     and    c1.colid = r.rkey8
  12945.     and     r.fkeyid = o2.id
  12946.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12947.     and    o2.id = c2.id    
  12948.     and    c2.colid = r.fkey8
  12949.     union all
  12950.     select
  12951.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12952.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12953.         PK_TABLE_NAME         = o1.name,
  12954.         PK_COLUMN_NAME         = c1.name,
  12955.         PK_COLUMN_GUID        = convert(binary(16),null),
  12956.         PK_COLUMN_PROPID    = convert(int,null),
  12957.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12958.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  12959.         FK_TABLE_NAME         = o2.name,
  12960.         FK_COLUMN_NAME         = c2.name,
  12961.         FK_COLUMN_GUID        = convert(binary(16),null),
  12962.         FK_COLUMN_PROPID    = convert(int,null),
  12963.         ORDINAL            = convert(int,9),
  12964.         UPDATE_RULE        = 'NO ACTION',
  12965.         DELETE_RULE         = 'NO ACTION'        
  12966.     from
  12967.         sysobjects o1, sysobjects o2,
  12968.         syscolumns c1, syscolumns c2,
  12969.         sysreferences r
  12970.     where    
  12971.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12972.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12973.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12974.     and    o1.id = r.rkeyid
  12975.     and    r.keycnt >= 9
  12976.     and    o1.id = c1.id
  12977.     and    c1.colid = r.rkey9
  12978.     and     r.fkeyid = o2.id
  12979.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12980.     and    o2.id = c2.id    
  12981.     and    c2.colid = r.fkey9
  12982.     union all
  12983.     select
  12984.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12985.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12986.         PK_TABLE_NAME         = o1.name,
  12987.         PK_COLUMN_NAME         = c1.name,
  12988.         PK_COLUMN_GUID        = convert(binary(16),null),
  12989.         PK_COLUMN_PROPID    = convert(int,null),
  12990.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12991.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  12992.         FK_TABLE_NAME         = o2.name,
  12993.         FK_COLUMN_NAME         = c2.name,
  12994.         FK_COLUMN_GUID        = convert(binary(16),null),
  12995.         FK_COLUMN_PROPID    = convert(int,null),
  12996.         ORDINAL            = convert(int,10),
  12997.         UPDATE_RULE        = 'NO ACTION',
  12998.         DELETE_RULE         = 'NO ACTION'        
  12999.     from
  13000.         sysobjects o1, sysobjects o2,
  13001.         syscolumns c1, syscolumns c2,
  13002.         sysreferences r
  13003.     where    
  13004.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13005.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13006.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13007.     and    o1.id = r.rkeyid
  13008.     and    r.keycnt >= 10
  13009.     and    o1.id = c1.id
  13010.     and    c1.colid = r.rkey10
  13011.     and     r.fkeyid = o2.id
  13012.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13013.     and    o2.id = c2.id    
  13014.     and    c2.colid = r.fkey10
  13015.     union all
  13016.     select
  13017.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  13018.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  13019.         PK_TABLE_NAME         = o1.name,
  13020.         PK_COLUMN_NAME         = c1.name,
  13021.         PK_COLUMN_GUID        = convert(binary(16),null),
  13022.         PK_COLUMN_PROPID    = convert(int,null),
  13023.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  13024.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  13025.         FK_TABLE_NAME         = o2.name,
  13026.         FK_COLUMN_NAME         = c2.name,
  13027.         FK_COLUMN_GUID        = convert(binary(16),null),
  13028.         FK_COLUMN_PROPID    = convert(int,null),
  13029.         ORDINAL            = convert(int,11),
  13030.         UPDATE_RULE        = 'NO ACTION',
  13031.         DELETE_RULE         = 'NO ACTION'        
  13032.     from
  13033.         sysobjects o1, sysobjects o2,
  13034.         syscolumns c1, syscolumns c2,
  13035.         sysreferences r
  13036.     where    
  13037.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13038.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13039.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13040.     and    o1.id = r.rkeyid
  13041.     and    r.keycnt >= 11
  13042.     and    o1.id = c1.id
  13043.     and    c1.colid = r.rkey11
  13044.     and     r.fkeyid = o2.id
  13045.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13046.     and    o2.id = c2.id    
  13047.     and    c2.colid = r.fkey11
  13048.     union all
  13049.     select
  13050.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  13051.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  13052.         PK_TABLE_NAME         = o1.name,
  13053.         PK_COLUMN_NAME         = c1.name,
  13054.         PK_COLUMN_GUID        = convert(binary(16),null),
  13055.         PK_COLUMN_PROPID    = convert(int,null),
  13056.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  13057.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  13058.         FK_TABLE_NAME         = o2.name,
  13059.         FK_COLUMN_NAME         = c2.name,
  13060.         FK_COLUMN_GUID        = convert(binary(16),null),
  13061.         FK_COLUMN_PROPID    = convert(int,null),
  13062.         ORDINAL            = convert(int,12),
  13063.         UPDATE_RULE        = 'NO ACTION',
  13064.         DELETE_RULE         = 'NO ACTION'        
  13065.     from
  13066.         sysobjects o1, sysobjects o2,
  13067.         syscolumns c1, syscolumns c2,
  13068.         sysreferences r
  13069.     where    
  13070.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13071.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13072.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13073.     and    o1.id = r.rkeyid
  13074.     and    r.keycnt >= 12
  13075.     and    o1.id = c1.id
  13076.     and    c1.colid = r.rkey12
  13077.     and     r.fkeyid = o2.id
  13078.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13079.     and    o2.id = c2.id    
  13080.     and    c2.colid = r.fkey12
  13081.     union all
  13082.     select
  13083.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  13084.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  13085.         PK_TABLE_NAME         = o1.name,
  13086.         PK_COLUMN_NAME         = c1.name,
  13087.         PK_COLUMN_GUID        = convert(binary(16),null),
  13088.         PK_COLUMN_PROPID    = convert(int,null),
  13089.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  13090.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  13091.         FK_TABLE_NAME         = o2.name,
  13092.         FK_COLUMN_NAME         = c2.name,
  13093.         FK_COLUMN_GUID        = convert(binary(16),null),
  13094.         FK_COLUMN_PROPID    = convert(int,null),
  13095.         ORDINAL            = convert(int,13),
  13096.         UPDATE_RULE        = 'NO ACTION',
  13097.         DELETE_RULE         = 'NO ACTION'        
  13098.     from
  13099.         sysobjects o1, sysobjects o2,
  13100.         syscolumns c1, syscolumns c2,
  13101.         sysreferences r
  13102.     where    
  13103.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13104.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13105.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13106.     and    o1.id = r.rkeyid
  13107.     and    r.keycnt >= 13
  13108.     and    o1.id = c1.id
  13109.     and    c1.colid = r.rkey13
  13110.     and     r.fkeyid = o2.id
  13111.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13112.     and    o2.id = c2.id    
  13113.     and    c2.colid = r.fkey13
  13114.     union all
  13115.     select
  13116.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  13117.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  13118.         PK_TABLE_NAME         = o1.name,
  13119.         PK_COLUMN_NAME         = c1.name,
  13120.         PK_COLUMN_GUID        = convert(binary(16),null),
  13121.         PK_COLUMN_PROPID    = convert(int,null),
  13122.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  13123.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  13124.         FK_TABLE_NAME         = o2.name,
  13125.         FK_COLUMN_NAME         = c2.name,
  13126.         FK_COLUMN_GUID        = convert(binary(16),null),
  13127.         FK_COLUMN_PROPID    = convert(int,null),
  13128.         ORDINAL            = convert(int,14),
  13129.         UPDATE_RULE        = 'NO ACTION',
  13130.         DELETE_RULE         = 'NO ACTION'        
  13131.     from
  13132.         sysobjects o1, sysobjects o2,
  13133.         syscolumns c1, syscolumns c2,
  13134.         sysreferences r
  13135.     where    
  13136.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13137.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13138.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13139.     and    o1.id = r.rkeyid
  13140.     and    r.keycnt >= 14
  13141.     and    o1.id = c1.id
  13142.     and    c1.colid = r.rkey14
  13143.     and     r.fkeyid = o2.id
  13144.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13145.     and    o2.id = c2.id    
  13146.     and    c2.colid = r.fkey14
  13147.     union all
  13148.     select
  13149.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  13150.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  13151.         PK_TABLE_NAME         = o1.name,
  13152.         PK_COLUMN_NAME         = c1.name,
  13153.         PK_COLUMN_GUID        = convert(binary(16),null),
  13154.         PK_COLUMN_PROPID    = convert(int,null),
  13155.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  13156.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  13157.         FK_TABLE_NAME         = o2.name,
  13158.         FK_COLUMN_NAME         = c2.name,
  13159.         FK_COLUMN_GUID        = convert(binary(16),null),
  13160.         FK_COLUMN_PROPID    = convert(int,null),
  13161.         ORDINAL            = convert(int,15),
  13162.         UPDATE_RULE        = 'NO ACTION',
  13163.         DELETE_RULE         = 'NO ACTION'        
  13164.     from
  13165.         sysobjects o1, sysobjects o2,
  13166.         syscolumns c1, syscolumns c2,
  13167.         sysreferences r
  13168.     where    
  13169.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13170.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13171.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13172.     and    o1.id = r.rkeyid
  13173.     and    r.keycnt >= 15
  13174.     and    o1.id = c1.id
  13175.     and    c1.colid = r.rkey15
  13176.     and     r.fkeyid = o2.id
  13177.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13178.     and    o2.id = c2.id    
  13179.     and    c2.colid = r.fkey15
  13180.     union all
  13181.     select
  13182.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  13183.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  13184.         PK_TABLE_NAME         = o1.name,
  13185.         PK_COLUMN_NAME         = c1.name,
  13186.         PK_COLUMN_GUID        = convert(binary(16),null),
  13187.         PK_COLUMN_PROPID    = convert(int,null),
  13188.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  13189.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  13190.         FK_TABLE_NAME         = o2.name,
  13191.         FK_COLUMN_NAME         = c2.name,
  13192.         FK_COLUMN_GUID        = convert(binary(16),null),
  13193.         FK_COLUMN_PROPID    = convert(int,null),
  13194.         ORDINAL            = convert(int,16),
  13195.         UPDATE_RULE        = 'NO ACTION',
  13196.         DELETE_RULE         = 'NO ACTION'        
  13197.     from
  13198.         sysobjects o1, sysobjects o2,
  13199.         syscolumns c1, syscolumns c2,
  13200.         sysreferences r
  13201.     where    
  13202.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13203.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13204.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13205.     and    o1.id = r.rkeyid
  13206.     and    o1.id = c1.id
  13207.     and    r.keycnt >= 16
  13208.     and    c1.colid = r.rkey16
  13209.     and     r.fkeyid = o2.id
  13210.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13211.     and    o2.id = c2.id    
  13212.     and    c2.colid = r.fkey16
  13213.     order by 8,9,2,3,13
  13214.     END
  13215.  
  13216.     exec @ret = sp_cursoropen @handle output, 'select * from #spfkeysrowset1',
  13217.         @scrollopt output, @ccopt output, @rows output
  13218.  
  13219.     drop table #spfkeysrowset1
  13220.        return isnull(@ret,0)
  13221. go
  13222.  
  13223.  
  13224. create procedure sp_foreign_keys_rowset;5
  13225.     (
  13226.        @handle        int output,
  13227.        @scrollopt    int output,
  13228.     @ccopt        int output,
  13229.     @rows        int output,
  13230.     @cmd        varchar(255)
  13231.     )
  13232. as
  13233.     declare @ret int
  13234.     declare @database_name varchar(30)
  13235.  
  13236.     create table #spfkeysrowset2
  13237.         (
  13238.         PK_TABLE_CATALOG    varchar(30) not null,
  13239.         PK_TABLE_SCHEMA        varchar(30) not null,
  13240.         PK_TABLE_NAME         varchar(30) not null,
  13241.         PK_COLUMN_NAME         varchar(30) not null,
  13242.         PK_COLUMN_GUID        binary(16) null,
  13243.         PK_COLUMN_PROPID    int null,
  13244.         FK_TABLE_CATALOG    varchar(30) not null,
  13245.         FK_TABLE_SCHEMA     varchar(30) not null,
  13246.         FK_TABLE_NAME         varchar(30) not null,
  13247.         FK_COLUMN_NAME         varchar(30) not null,
  13248.         FK_COLUMN_GUID        binary(16) null,
  13249.         FK_COLUMN_PROPID    int null,
  13250.         ORDINAL            int not null,
  13251.         UPDATE_RULE        varchar(30) not null,
  13252.         DELETE_RULE         varchar(30) not null        
  13253.         )
  13254.  
  13255.     declare sysdatabases_cursor insensitive cursor for 
  13256.         select name from master.dbo.sysdatabases order by name
  13257.   
  13258.     set nocount on
  13259.     open sysdatabases_cursor
  13260.       fetch next from sysdatabases_cursor into @database_name 
  13261.     while @@fetch_status = 0
  13262.         begin
  13263.         exec('use '+@database_name+' insert into #spfkeysrowset2 '+@cmd)
  13264.         fetch next from sysdatabases_cursor into @database_name 
  13265.         end
  13266.       close sysdatabases_cursor
  13267.     deallocate sysdatabases_cursor
  13268.  
  13269.     if @scrollopt is null
  13270.         begin
  13271.         select * from #spfkeysrowset2
  13272.         end
  13273.     else
  13274.         begin
  13275.         exec @ret = sp_cursoropen @handle output,'select * from #spfkeysrowset2',
  13276.             @scrollopt output, @ccopt output, @rows output
  13277.         end
  13278.     drop table #spfkeysrowset2
  13279.     return isnull(@ret,0)
  13280. go
  13281.  
  13282.  
  13283.  
  13284. if (charindex('7.00', @@version) = 0)
  13285. begin
  13286.     print ''
  13287.     print ''
  13288.     print 'Warning:'
  13289.     print 'you are installing the stored procedures '
  13290.     print 'on a pre 7.0 SQL Server.'
  13291.     print 'Ignore the following errors.'
  13292. end
  13293. else
  13294.     drop proc sp_foreign_keys_rowset
  13295. go
  13296.  
  13297.  
  13298. /*    Procedure for 7.0 server */
  13299. CREATE PROCEDURE sp_foreign_keys_rowset
  13300.     (
  13301.     @pk_table_name        varchar(255),
  13302.     @pk_table_schema    varchar(255) = null,
  13303.     @fk_table_name        varchar(255) = null,
  13304.     @fk_table_schema    varchar(255) = null,
  13305.     @fk_table_catalog    varchar(255) = null
  13306.     )
  13307. as
  13308.     select
  13309.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  13310.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  13311.         PK_TABLE_NAME         = o1.name,
  13312.         PK_COLUMN_NAME         = c1.name,
  13313.         PK_COLUMN_GUID        = convert(binary(16),null),
  13314.         PK_COLUMN_PROPID    = convert(int,null),
  13315.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  13316.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  13317.         FK_TABLE_NAME         = o2.name,
  13318.         FK_COLUMN_NAME         = c2.name,
  13319.         FK_COLUMN_GUID        = convert(binary(16),null),
  13320.         FK_COLUMN_PROPID    = convert(int,null),
  13321.         ORDINAL            = convert(int,1),
  13322.         UPDATE_RULE        = 'NO ACTION',
  13323.         DELETE_RULE         = 'NO ACTION'        
  13324.     from
  13325.         sysobjects o1, sysobjects o2,
  13326.         syscolumns c1, syscolumns c2,
  13327.         sysreferences r
  13328.     where    
  13329.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13330.     and    o1.name = @pk_table_name
  13331.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13332.     and    o1.id = r.rkeyid
  13333.     and    o1.id = c1.id
  13334.     and    c1.colid = r.rkey1
  13335.     and     r.fkeyid = o2.id
  13336.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  13337.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13338.     and    o2.id = c2.id    
  13339.     and    c2.colid = r.fkey1
  13340.     union all
  13341.     select
  13342.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  13343.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  13344.         PK_TABLE_NAME         = o1.name,
  13345.         PK_COLUMN_NAME         = c1.name,
  13346.         PK_COLUMN_GUID        = convert(binary(16),null),
  13347.         PK_COLUMN_PROPID    = convert(int,null),
  13348.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  13349.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  13350.         FK_TABLE_NAME         = o2.name,
  13351.         FK_COLUMN_NAME         = c2.name,
  13352.         FK_COLUMN_GUID        = convert(binary(16),null),
  13353.         FK_COLUMN_PROPID    = convert(int,null),
  13354.         ORDINAL            = convert(int,2),
  13355.         UPDATE_RULE        = 'NO ACTION',
  13356.         DELETE_RULE         = 'NO ACTION'        
  13357.     from
  13358.         sysobjects o1, sysobjects o2,
  13359.         syscolumns c1, syscolumns c2,
  13360.         sysreferences r
  13361.     where    
  13362.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13363.     and    o1.name = @pk_table_name
  13364.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13365.     and    o1.id = r.rkeyid
  13366.     and    o1.id = c1.id
  13367.     and    c1.colid = r.rkey2
  13368.     and    r.keycnt >= 2
  13369.     and     r.fkeyid = o2.id
  13370.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  13371.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13372.     and    o2.id = c2.id    
  13373.     and    c2.colid = r.fkey2
  13374.     union all
  13375.     select
  13376.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  13377.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  13378.         PK_TABLE_NAME         = o1.name,
  13379.         PK_COLUMN_NAME         = c1.name,
  13380.         PK_COLUMN_GUID        = convert(binary(16),null),
  13381.         PK_COLUMN_PROPID    = convert(int,null),
  13382.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  13383.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  13384.         FK_TABLE_NAME         = o2.name,
  13385.         FK_COLUMN_NAME         = c2.name,
  13386.         FK_COLUMN_GUID        = convert(binary(16),null),
  13387.         FK_COLUMN_PROPID    = convert(int,null),
  13388.         ORDINAL            = convert(int,3),
  13389.         UPDATE_RULE        = 'NO ACTION',
  13390.         DELETE_RULE         = 'NO ACTION'        
  13391.     from
  13392.         sysobjects o1, sysobjects o2,
  13393.         syscolumns c1, syscolumns c2,
  13394.         sysreferences r
  13395.     where    
  13396.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13397.     and    o1.name = @pk_table_name
  13398.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13399.     and    o1.id = r.rkeyid
  13400.     and    o1.id = c1.id
  13401.     and    c1.colid = r.rkey3
  13402.     and    r.keycnt >= 3
  13403.     and     r.fkeyid = o2.id
  13404.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  13405.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13406.     and    o2.id = c2.id    
  13407.     and    c2.colid = r.fkey3
  13408.     union all
  13409.     select
  13410.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  13411.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  13412.         PK_TABLE_NAME         = o1.name,
  13413.         PK_COLUMN_NAME         = c1.name,
  13414.         PK_COLUMN_GUID        = convert(binary(16),null),
  13415.         PK_COLUMN_PROPID    = convert(int,null),
  13416.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  13417.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  13418.         FK_TABLE_NAME         = o2.name,
  13419.         FK_COLUMN_NAME         = c2.name,
  13420.         FK_COLUMN_GUID        = convert(binary(16),null),
  13421.         FK_COLUMN_PROPID    = convert(int,null),
  13422.         ORDINAL            = convert(int,4),
  13423.         UPDATE_RULE        = 'NO ACTION',
  13424.         DELETE_RULE         = 'NO ACTION'        
  13425.     from
  13426.         sysobjects o1, sysobjects o2,
  13427.         syscolumns c1, syscolumns c2,
  13428.         sysreferences r
  13429.     where    
  13430.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13431.     and    o1.name = @pk_table_name
  13432.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13433.     and    o1.id = r.rkeyid
  13434.     and    r.keycnt >= 4
  13435.     and    o1.id = c1.id
  13436.     and    c1.colid = r.rkey4
  13437.     and     r.fkeyid = o2.id
  13438.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  13439.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13440.     and    o2.id = c2.id    
  13441.     and    c2.colid = r.fkey4
  13442.     union all
  13443.     select
  13444.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  13445.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  13446.         PK_TABLE_NAME         = o1.name,
  13447.         PK_COLUMN_NAME         = c1.name,
  13448.         PK_COLUMN_GUID        = convert(binary(16),null),
  13449.         PK_COLUMN_PROPID    = convert(int,null),
  13450.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  13451.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  13452.         FK_TABLE_NAME         = o2.name,
  13453.         FK_COLUMN_NAME         = c2.name,
  13454.         FK_COLUMN_GUID        = convert(binary(16),null),
  13455.         FK_COLUMN_PROPID    = convert(int,null),
  13456.         ORDINAL            = convert(int,5),
  13457.         UPDATE_RULE        = 'NO ACTION',
  13458.         DELETE_RULE         = 'NO ACTION'        
  13459.     from
  13460.         sysobjects o1, sysobjects o2,
  13461.         syscolumns c1, syscolumns c2,
  13462.         sysreferences r
  13463.     where    
  13464.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13465.     and    o1.name = @pk_table_name
  13466.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13467.     and    o1.id = r.rkeyid
  13468.     and    r.keycnt >= 5
  13469.     and    o1.id = c1.id
  13470.     and    c1.colid = r.rkey5
  13471.     and     r.fkeyid = o2.id
  13472.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  13473.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13474.     and    o2.id = c2.id    
  13475.     and    c2.colid = r.fkey5
  13476.     union all
  13477.     select
  13478.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  13479.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  13480.         PK_TABLE_NAME         = o1.name,
  13481.         PK_COLUMN_NAME         = c1.name,
  13482.         PK_COLUMN_GUID        = convert(binary(16),null),
  13483.         PK_COLUMN_PROPID    = convert(int,null),
  13484.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  13485.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  13486.         FK_TABLE_NAME         = o2.name,
  13487.         FK_COLUMN_NAME         = c2.name,
  13488.         FK_COLUMN_GUID        = convert(binary(16),null),
  13489.         FK_COLUMN_PROPID    = convert(int,null),
  13490.         ORDINAL            = convert(int,6),
  13491.         UPDATE_RULE        = 'NO ACTION',
  13492.         DELETE_RULE         = 'NO ACTION'        
  13493.     from
  13494.         sysobjects o1, sysobjects o2,
  13495.         syscolumns c1, syscolumns c2,
  13496.         sysreferences r
  13497.     where    
  13498.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13499.     and    o1.name = @pk_table_name
  13500.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13501.     and    o1.id = r.rkeyid
  13502.     and    r.keycnt >= 6
  13503.     and    o1.id = c1.id
  13504.     and    c1.colid = r.rkey6
  13505.     and     r.fkeyid = o2.id
  13506.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  13507.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13508.     and    o2.id = c2.id    
  13509.     and    c2.colid = r.fkey6
  13510.     union all
  13511.     select
  13512.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  13513.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  13514.         PK_TABLE_NAME         = o1.name,
  13515.         PK_COLUMN_NAME         = c1.name,
  13516.         PK_COLUMN_GUID        = convert(binary(16),null),
  13517.         PK_COLUMN_PROPID    = convert(int,null),
  13518.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  13519.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  13520.         FK_TABLE_NAME         = o2.name,
  13521.         FK_COLUMN_NAME         = c2.name,
  13522.         FK_COLUMN_GUID        = convert(binary(16),null),
  13523.         FK_COLUMN_PROPID    = convert(int,null),
  13524.         ORDINAL            = convert(int,7),
  13525.         UPDATE_RULE        = 'NO ACTION',
  13526.         DELETE_RULE         = 'NO ACTION'        
  13527.     from
  13528.         sysobjects o1, sysobjects o2,
  13529.         syscolumns c1, syscolumns c2,
  13530.         sysreferences r
  13531.     where    
  13532.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13533.     and    o1.name = @pk_table_name
  13534.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13535.     and    o1.id = r.rkeyid
  13536.     and    r.keycnt >= 7
  13537.     and    o1.id = c1.id
  13538.     and    c1.colid = r.rkey7
  13539.     and     r.fkeyid = o2.id
  13540.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  13541.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13542.     and    o2.id = c2.id    
  13543.     and    c2.colid = r.fkey7
  13544.     union all
  13545.     select
  13546.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  13547.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  13548.         PK_TABLE_NAME         = o1.name,
  13549.         PK_COLUMN_NAME         = c1.name,
  13550.         PK_COLUMN_GUID        = convert(binary(16),null),
  13551.         PK_COLUMN_PROPID    = convert(int,null),
  13552.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  13553.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  13554.         FK_TABLE_NAME         = o2.name,
  13555.         FK_COLUMN_NAME         = c2.name,
  13556.         FK_COLUMN_GUID        = convert(binary(16),null),
  13557.         FK_COLUMN_PROPID    = convert(int,null),
  13558.         ORDINAL            = convert(int,8),
  13559.         UPDATE_RULE        = 'NO ACTION',
  13560.         DELETE_RULE         = 'NO ACTION'        
  13561.     from
  13562.         sysobjects o1, sysobjects o2,
  13563.         syscolumns c1, syscolumns c2,
  13564.         sysreferences r
  13565.     where    
  13566.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13567.     and    o1.name = @pk_table_name
  13568.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13569.     and    o1.id = r.rkeyid
  13570.     and    r.keycnt >= 8
  13571.     and    o1.id = c1.id
  13572.     and    c1.colid = r.rkey8
  13573.     and     r.fkeyid = o2.id
  13574.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  13575.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13576.     and    o2.id = c2.id    
  13577.     and    c2.colid = r.fkey8
  13578.     union all
  13579.     select
  13580.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  13581.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  13582.         PK_TABLE_NAME         = o1.name,
  13583.         PK_COLUMN_NAME         = c1.name,
  13584.         PK_COLUMN_GUID        = convert(binary(16),null),
  13585.         PK_COLUMN_PROPID    = convert(int,null),
  13586.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  13587.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  13588.         FK_TABLE_NAME         = o2.name,
  13589.         FK_COLUMN_NAME         = c2.name,
  13590.         FK_COLUMN_GUID        = convert(binary(16),null),
  13591.         FK_COLUMN_PROPID    = convert(int,null),
  13592.         ORDINAL            = convert(int,9),
  13593.         UPDATE_RULE        = 'NO ACTION',
  13594.         DELETE_RULE         = 'NO ACTION'        
  13595.     from
  13596.         sysobjects o1, sysobjects o2,
  13597.         syscolumns c1, syscolumns c2,
  13598.         sysreferences r
  13599.     where    
  13600.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13601.     and    o1.name = @pk_table_name
  13602.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13603.     and    o1.id = r.rkeyid
  13604.     and    r.keycnt >= 9
  13605.     and    o1.id = c1.id
  13606.     and    c1.colid = r.rkey9
  13607.     and     r.fkeyid = o2.id
  13608.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  13609.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13610.     and    o2.id = c2.id    
  13611.     and    c2.colid = r.fkey9
  13612.     union all
  13613.     select
  13614.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  13615.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  13616.         PK_TABLE_NAME         = o1.name,
  13617.         PK_COLUMN_NAME         = c1.name,
  13618.         PK_COLUMN_GUID        = convert(binary(16),null),
  13619.         PK_COLUMN_PROPID    = convert(int,null),
  13620.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  13621.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  13622.         FK_TABLE_NAME         = o2.name,
  13623.         FK_COLUMN_NAME         = c2.name,
  13624.         FK_COLUMN_GUID        = convert(binary(16),null),
  13625.         FK_COLUMN_PROPID    = convert(int,null),
  13626.         ORDINAL            = convert(int,10),
  13627.         UPDATE_RULE        = 'NO ACTION',
  13628.         DELETE_RULE         = 'NO ACTION'        
  13629.     from
  13630.         sysobjects o1, sysobjects o2,
  13631.         syscolumns c1, syscolumns c2,
  13632.         sysreferences r
  13633.     where    
  13634.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13635.     and    o1.name = @pk_table_name
  13636.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13637.     and    o1.id = r.rkeyid
  13638.     and    r.keycnt >= 10
  13639.     and    o1.id = c1.id
  13640.     and    c1.colid = r.rkey10
  13641.     and     r.fkeyid = o2.id
  13642.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  13643.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13644.     and    o2.id = c2.id    
  13645.     and    c2.colid = r.fkey10
  13646.     union all
  13647.     select
  13648.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  13649.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  13650.         PK_TABLE_NAME         = o1.name,
  13651.         PK_COLUMN_NAME         = c1.name,
  13652.         PK_COLUMN_GUID        = convert(binary(16),null),
  13653.         PK_COLUMN_PROPID    = convert(int,null),
  13654.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  13655.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  13656.         FK_TABLE_NAME         = o2.name,
  13657.         FK_COLUMN_NAME         = c2.name,
  13658.         FK_COLUMN_GUID        = convert(binary(16),null),
  13659.         FK_COLUMN_PROPID    = convert(int,null),
  13660.         ORDINAL            = convert(int,11),
  13661.         UPDATE_RULE        = 'NO ACTION',
  13662.         DELETE_RULE         = 'NO ACTION'        
  13663.     from
  13664.         sysobjects o1, sysobjects o2,
  13665.         syscolumns c1, syscolumns c2,
  13666.         sysreferences r
  13667.     where    
  13668.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13669.     and    o1.name = @pk_table_name
  13670.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13671.     and    o1.id = r.rkeyid
  13672.     and    r.keycnt >= 11
  13673.     and    o1.id = c1.id
  13674.     and    c1.colid = r.rkey11
  13675.     and     r.fkeyid = o2.id
  13676.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  13677.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13678.     and    o2.id = c2.id    
  13679.     and    c2.colid = r.fkey11
  13680.     union all
  13681.     select
  13682.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  13683.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  13684.         PK_TABLE_NAME         = o1.name,
  13685.         PK_COLUMN_NAME         = c1.name,
  13686.         PK_COLUMN_GUID        = convert(binary(16),null),
  13687.         PK_COLUMN_PROPID    = convert(int,null),
  13688.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  13689.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  13690.         FK_TABLE_NAME         = o2.name,
  13691.         FK_COLUMN_NAME         = c2.name,
  13692.         FK_COLUMN_GUID        = convert(binary(16),null),
  13693.         FK_COLUMN_PROPID    = convert(int,null),
  13694.         ORDINAL            = convert(int,12),
  13695.         UPDATE_RULE        = 'NO ACTION',
  13696.         DELETE_RULE         = 'NO ACTION'        
  13697.     from
  13698.         sysobjects o1, sysobjects o2,
  13699.         syscolumns c1, syscolumns c2,
  13700.         sysreferences r
  13701.     where    
  13702.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13703.     and    o1.name = @pk_table_name
  13704.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13705.     and    o1.id = r.rkeyid
  13706.     and    r.keycnt >= 12
  13707.     and    o1.id = c1.id
  13708.     and    c1.colid = r.rkey12
  13709.     and     r.fkeyid = o2.id
  13710.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  13711.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13712.     and    o2.id = c2.id    
  13713.     and    c2.colid = r.fkey12
  13714.     union all
  13715.     select
  13716.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  13717.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  13718.         PK_TABLE_NAME         = o1.name,
  13719.         PK_COLUMN_NAME         = c1.name,
  13720.         PK_COLUMN_GUID        = convert(binary(16),null),
  13721.         PK_COLUMN_PROPID    = convert(int,null),
  13722.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  13723.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  13724.         FK_TABLE_NAME         = o2.name,
  13725.         FK_COLUMN_NAME         = c2.name,
  13726.         FK_COLUMN_GUID        = convert(binary(16),null),
  13727.         FK_COLUMN_PROPID    = convert(int,null),
  13728.         ORDINAL            = convert(int,13),
  13729.         UPDATE_RULE        = 'NO ACTION',
  13730.         DELETE_RULE         = 'NO ACTION'        
  13731.     from
  13732.         sysobjects o1, sysobjects o2,
  13733.         syscolumns c1, syscolumns c2,
  13734.         sysreferences r
  13735.     where    
  13736.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13737.     and    o1.name = @pk_table_name
  13738.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13739.     and    o1.id = r.rkeyid
  13740.     and    r.keycnt >= 13
  13741.     and    o1.id = c1.id
  13742.     and    c1.colid = r.rkey13
  13743.     and     r.fkeyid = o2.id
  13744.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  13745.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13746.     and    o2.id = c2.id    
  13747.     and    c2.colid = r.fkey13
  13748.     union all
  13749.     select
  13750.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  13751.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  13752.         PK_TABLE_NAME         = o1.name,
  13753.         PK_COLUMN_NAME         = c1.name,
  13754.         PK_COLUMN_GUID        = convert(binary(16),null),
  13755.         PK_COLUMN_PROPID    = convert(int,null),
  13756.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  13757.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  13758.         FK_TABLE_NAME         = o2.name,
  13759.         FK_COLUMN_NAME         = c2.name,
  13760.         FK_COLUMN_GUID        = convert(binary(16),null),
  13761.         FK_COLUMN_PROPID    = convert(int,null),
  13762.         ORDINAL            = convert(int,14),
  13763.         UPDATE_RULE        = 'NO ACTION',
  13764.         DELETE_RULE         = 'NO ACTION'        
  13765.     from
  13766.         sysobjects o1, sysobjects o2,
  13767.         syscolumns c1, syscolumns c2,
  13768.         sysreferences r
  13769.     where    
  13770.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13771.     and    o1.name = @pk_table_name
  13772.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13773.     and    o1.id = r.rkeyid
  13774.     and    r.keycnt >= 14
  13775.     and    o1.id = c1.id
  13776.     and    c1.colid = r.rkey14
  13777.     and     r.fkeyid = o2.id
  13778.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  13779.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13780.     and    o2.id = c2.id    
  13781.     and    c2.colid = r.fkey14
  13782.     union all
  13783.     select
  13784.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  13785.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  13786.         PK_TABLE_NAME         = o1.name,
  13787.         PK_COLUMN_NAME         = c1.name,
  13788.         PK_COLUMN_GUID        = convert(binary(16),null),
  13789.         PK_COLUMN_PROPID    = convert(int,null),
  13790.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  13791.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  13792.         FK_TABLE_NAME         = o2.name,
  13793.         FK_COLUMN_NAME         = c2.name,
  13794.         FK_COLUMN_GUID        = convert(binary(16),null),
  13795.         FK_COLUMN_PROPID    = convert(int,null),
  13796.         ORDINAL            = convert(int,15),
  13797.         UPDATE_RULE        = 'NO ACTION',
  13798.         DELETE_RULE         = 'NO ACTION'        
  13799.     from
  13800.         sysobjects o1, sysobjects o2,
  13801.         syscolumns c1, syscolumns c2,
  13802.         sysreferences r
  13803.     where    
  13804.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13805.     and    o1.name = @pk_table_name
  13806.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13807.     and    o1.id = r.rkeyid
  13808.     and    r.keycnt >= 15
  13809.     and    o1.id = c1.id
  13810.     and    c1.colid = r.rkey15
  13811.     and     r.fkeyid = o2.id
  13812.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  13813.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13814.     and    o2.id = c2.id    
  13815.     and    c2.colid = r.fkey15
  13816.     union all
  13817.     select
  13818.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  13819.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  13820.         PK_TABLE_NAME         = o1.name,
  13821.         PK_COLUMN_NAME         = c1.name,
  13822.         PK_COLUMN_GUID        = convert(binary(16),null),
  13823.         PK_COLUMN_PROPID    = convert(int,null),
  13824.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  13825.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  13826.         FK_TABLE_NAME         = o2.name,
  13827.         FK_COLUMN_NAME         = c2.name,
  13828.         FK_COLUMN_GUID        = convert(binary(16),null),
  13829.         FK_COLUMN_PROPID    = convert(int,null),
  13830.         ORDINAL            = convert(int,16),
  13831.         UPDATE_RULE        = 'NO ACTION',
  13832.         DELETE_RULE         = 'NO ACTION'        
  13833.     from
  13834.         sysobjects o1, sysobjects o2,
  13835.         syscolumns c1, syscolumns c2,
  13836.         sysreferences r
  13837.     where    
  13838.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13839.     and    o1.name = @pk_table_name
  13840.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13841.     and    o1.id = r.rkeyid
  13842.     and    r.keycnt >= 16
  13843.     and    o1.id = c1.id
  13844.     and    c1.colid = r.rkey16
  13845.     and     r.fkeyid = o2.id
  13846.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  13847.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13848.     and    o2.id = c2.id    
  13849.     and    c2.colid = r.fkey16
  13850.     order by 8,9,2,3,13
  13851. go
  13852. CREATE PROCEDURE sp_foreign_keys_rowset;2
  13853.     (
  13854.     @fk_table_name        varchar(255),
  13855.     @fk_table_schema    varchar(255) = null,
  13856.     @pk_table_name        varchar(255) = null,
  13857.     @pk_table_schema    varchar(255) = null,
  13858.     @pk_table_catalog    varchar(255) = null
  13859.     )
  13860. as
  13861.     select
  13862.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  13863.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  13864.         PK_TABLE_NAME         = o1.name,
  13865.         PK_COLUMN_NAME         = c1.name,
  13866.         PK_COLUMN_GUID        = convert(binary(16),null),
  13867.         PK_COLUMN_PROPID    = convert(int,null),
  13868.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  13869.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  13870.         FK_TABLE_NAME         = o2.name,
  13871.         FK_COLUMN_NAME         = c2.name,
  13872.         FK_COLUMN_GUID        = convert(binary(16),null),
  13873.         FK_COLUMN_PROPID    = convert(int,null),
  13874.         ORDINAL            = convert(int,1),
  13875.         UPDATE_RULE        = 'NO ACTION',
  13876.         DELETE_RULE         = 'NO ACTION'        
  13877.     from
  13878.         sysobjects o1, sysobjects o2,
  13879.         syscolumns c1, syscolumns c2,
  13880.         sysreferences r
  13881.     where    
  13882.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13883.     and    o2.name = @fk_table_name
  13884.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13885.     and    o2.id = r.fkeyid
  13886.     and    o2.id = c2.id
  13887.     and    c2.colid = r.fkey1
  13888.     and     r.rkeyid = o1.id
  13889.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  13890.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13891.     and    o1.id = c1.id    
  13892.     and    c1.colid = r.rkey1
  13893.     union    all
  13894.     select
  13895.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  13896.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  13897.         PK_TABLE_NAME         = o1.name,
  13898.         PK_COLUMN_NAME         = c1.name,
  13899.         PK_COLUMN_GUID        = convert(binary(16),null),
  13900.         PK_COLUMN_PROPID    = convert(int,null),
  13901.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  13902.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  13903.         FK_TABLE_NAME         = o2.name,
  13904.         FK_COLUMN_NAME         = c2.name,
  13905.         FK_COLUMN_GUID        = convert(binary(16),null),
  13906.         FK_COLUMN_PROPID    = convert(int,null),
  13907.         ORDINAL            = convert(int,2),
  13908.         UPDATE_RULE        = 'NO ACTION',
  13909.         DELETE_RULE         = 'NO ACTION'        
  13910.     from
  13911.         sysobjects o1, sysobjects o2,
  13912.         syscolumns c1, syscolumns c2,
  13913.         sysreferences r
  13914.     where    
  13915.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13916.     and    o2.name = @fk_table_name
  13917.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13918.     and    o2.id = r.fkeyid
  13919.     and    r.keycnt >= 2
  13920.     and    o2.id = c2.id
  13921.     and    c2.colid = r.fkey2
  13922.     and     r.rkeyid = o1.id
  13923.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  13924.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13925.     and    o1.id = c1.id    
  13926.     and    c1.colid = r.rkey2
  13927.     union    all
  13928.     select
  13929.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  13930.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  13931.         PK_TABLE_NAME         = o1.name,
  13932.         PK_COLUMN_NAME         = c1.name,
  13933.         PK_COLUMN_GUID        = convert(binary(16),null),
  13934.         PK_COLUMN_PROPID    = convert(int,null),
  13935.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  13936.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  13937.         FK_TABLE_NAME         = o2.name,
  13938.         FK_COLUMN_NAME         = c2.name,
  13939.         FK_COLUMN_GUID        = convert(binary(16),null),
  13940.         FK_COLUMN_PROPID    = convert(int,null),
  13941.         ORDINAL            = convert(int,3),
  13942.         UPDATE_RULE        = 'NO ACTION',
  13943.         DELETE_RULE         = 'NO ACTION'        
  13944.     from
  13945.         sysobjects o1, sysobjects o2,
  13946.         syscolumns c1, syscolumns c2,
  13947.         sysreferences r
  13948.     where    
  13949.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13950.     and    o2.name = @fk_table_name
  13951.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13952.     and    o2.id = r.fkeyid
  13953.     and    r.keycnt >= 3
  13954.     and    o2.id = c2.id
  13955.     and    c2.colid = r.fkey3
  13956.     and     r.rkeyid = o1.id
  13957.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  13958.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13959.     and    o1.id = c1.id    
  13960.     and    c1.colid = r.rkey3
  13961.     union    all
  13962.     select
  13963.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  13964.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  13965.         PK_TABLE_NAME         = o1.name,
  13966.         PK_COLUMN_NAME         = c1.name,
  13967.         PK_COLUMN_GUID        = convert(binary(16),null),
  13968.         PK_COLUMN_PROPID    = convert(int,null),
  13969.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  13970.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  13971.         FK_TABLE_NAME         = o2.name,
  13972.         FK_COLUMN_NAME         = c2.name,
  13973.         FK_COLUMN_GUID        = convert(binary(16),null),
  13974.         FK_COLUMN_PROPID    = convert(int,null),
  13975.         ORDINAL            = convert(int,4),
  13976.         UPDATE_RULE        = 'NO ACTION',
  13977.         DELETE_RULE         = 'NO ACTION'        
  13978.     from
  13979.         sysobjects o1, sysobjects o2,
  13980.         syscolumns c1, syscolumns c2,
  13981.         sysreferences r
  13982.     where    
  13983.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13984.     and    o2.name = @fk_table_name
  13985.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13986.     and    o2.id = r.fkeyid
  13987.     and    r.keycnt >= 4
  13988.     and    o2.id = c2.id
  13989.     and    c2.colid = r.fkey4
  13990.     and     r.rkeyid = o1.id
  13991.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  13992.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13993.     and    o1.id = c1.id    
  13994.     and    c1.colid = r.rkey4
  13995.     union    all
  13996.     select
  13997.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  13998.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  13999.         PK_TABLE_NAME         = o1.name,
  14000.         PK_COLUMN_NAME         = c1.name,
  14001.         PK_COLUMN_GUID        = convert(binary(16),null),
  14002.         PK_COLUMN_PROPID    = convert(int,null),
  14003.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14004.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  14005.         FK_TABLE_NAME         = o2.name,
  14006.         FK_COLUMN_NAME         = c2.name,
  14007.         FK_COLUMN_GUID        = convert(binary(16),null),
  14008.         FK_COLUMN_PROPID    = convert(int,null),
  14009.         ORDINAL            = convert(int,5),
  14010.         UPDATE_RULE        = 'NO ACTION',
  14011.         DELETE_RULE         = 'NO ACTION'        
  14012.     from
  14013.         sysobjects o1, sysobjects o2,
  14014.         syscolumns c1, syscolumns c2,
  14015.         sysreferences r
  14016.     where    
  14017.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14018.     and    o2.name = @fk_table_name
  14019.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14020.     and    o2.id = r.fkeyid
  14021.     and    r.keycnt >= 5
  14022.     and    o2.id = c2.id
  14023.     and    c2.colid = r.fkey5
  14024.     and     r.rkeyid = o1.id
  14025.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  14026.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14027.     and    o1.id = c1.id    
  14028.     and    c1.colid = r.rkey5
  14029.     union    all
  14030.     select
  14031.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14032.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14033.         PK_TABLE_NAME         = o1.name,
  14034.         PK_COLUMN_NAME         = c1.name,
  14035.         PK_COLUMN_GUID        = convert(binary(16),null),
  14036.         PK_COLUMN_PROPID    = convert(int,null),
  14037.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14038.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  14039.         FK_TABLE_NAME         = o2.name,
  14040.         FK_COLUMN_NAME         = c2.name,
  14041.         FK_COLUMN_GUID        = convert(binary(16),null),
  14042.         FK_COLUMN_PROPID    = convert(int,null),
  14043.         ORDINAL            = convert(int,6),
  14044.         UPDATE_RULE        = 'NO ACTION',
  14045.         DELETE_RULE         = 'NO ACTION'        
  14046.     from
  14047.         sysobjects o1, sysobjects o2,
  14048.         syscolumns c1, syscolumns c2,
  14049.         sysreferences r
  14050.     where    
  14051.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14052.     and    o2.name = @fk_table_name
  14053.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14054.     and    o2.id = r.fkeyid
  14055.     and    r.keycnt >= 6
  14056.     and    o2.id = c2.id
  14057.     and    c2.colid = r.fkey6
  14058.     and     r.rkeyid = o1.id
  14059.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  14060.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14061.     and    o1.id = c1.id    
  14062.     and    c1.colid = r.rkey6
  14063.     union    all
  14064.     select
  14065.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14066.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14067.         PK_TABLE_NAME         = o1.name,
  14068.         PK_COLUMN_NAME         = c1.name,
  14069.         PK_COLUMN_GUID        = convert(binary(16),null),
  14070.         PK_COLUMN_PROPID    = convert(int,null),
  14071.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14072.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  14073.         FK_TABLE_NAME         = o2.name,
  14074.         FK_COLUMN_NAME         = c2.name,
  14075.         FK_COLUMN_GUID        = convert(binary(16),null),
  14076.         FK_COLUMN_PROPID    = convert(int,null),
  14077.         ORDINAL            = convert(int,7),
  14078.         UPDATE_RULE        = 'NO ACTION',
  14079.         DELETE_RULE         = 'NO ACTION'        
  14080.     from
  14081.         sysobjects o1, sysobjects o2,
  14082.         syscolumns c1, syscolumns c2,
  14083.         sysreferences r
  14084.     where    
  14085.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14086.     and    o2.name = @fk_table_name
  14087.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14088.     and    o2.id = r.fkeyid
  14089.     and    r.keycnt >= 7
  14090.     and    o2.id = c2.id
  14091.     and    c2.colid = r.fkey7
  14092.     and     r.rkeyid = o1.id
  14093.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  14094.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14095.     and    o1.id = c1.id    
  14096.     and    c1.colid = r.rkey7
  14097.     union    all
  14098.     select
  14099.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14100.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14101.         PK_TABLE_NAME         = o1.name,
  14102.         PK_COLUMN_NAME         = c1.name,
  14103.         PK_COLUMN_GUID        = convert(binary(16),null),
  14104.         PK_COLUMN_PROPID    = convert(int,null),
  14105.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14106.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  14107.         FK_TABLE_NAME         = o2.name,
  14108.         FK_COLUMN_NAME         = c2.name,
  14109.         FK_COLUMN_GUID        = convert(binary(16),null),
  14110.         FK_COLUMN_PROPID    = convert(int,null),
  14111.         ORDINAL            = convert(int,8),
  14112.         UPDATE_RULE        = 'NO ACTION',
  14113.         DELETE_RULE         = 'NO ACTION'        
  14114.     from
  14115.         sysobjects o1, sysobjects o2,
  14116.         syscolumns c1, syscolumns c2,
  14117.         sysreferences r
  14118.     where    
  14119.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14120.     and    o2.name = @fk_table_name
  14121.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14122.     and    o2.id = r.fkeyid
  14123.     and    r.keycnt >= 8
  14124.     and    o2.id = c2.id
  14125.     and    c2.colid = r.fkey8
  14126.     and     r.rkeyid = o1.id
  14127.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  14128.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14129.     and    o1.id = c1.id    
  14130.     and    c1.colid = r.rkey8
  14131.     union    all
  14132.     select
  14133.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14134.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14135.         PK_TABLE_NAME         = o1.name,
  14136.         PK_COLUMN_NAME         = c1.name,
  14137.         PK_COLUMN_GUID        = convert(binary(16),null),
  14138.         PK_COLUMN_PROPID    = convert(int,null),
  14139.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14140.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  14141.         FK_TABLE_NAME         = o2.name,
  14142.         FK_COLUMN_NAME         = c2.name,
  14143.         FK_COLUMN_GUID        = convert(binary(16),null),
  14144.         FK_COLUMN_PROPID    = convert(int,null),
  14145.         ORDINAL            = convert(int,9),
  14146.         UPDATE_RULE        = 'NO ACTION',
  14147.         DELETE_RULE         = 'NO ACTION'        
  14148.     from
  14149.         sysobjects o1, sysobjects o2,
  14150.         syscolumns c1, syscolumns c2,
  14151.         sysreferences r
  14152.     where    
  14153.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14154.     and    o2.name = @fk_table_name
  14155.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14156.     and    o2.id = r.fkeyid
  14157.     and    r.keycnt >= 9
  14158.     and    o2.id = c2.id
  14159.     and    c2.colid = r.fkey9
  14160.     and     r.rkeyid = o1.id
  14161.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  14162.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14163.     and    o1.id = c1.id    
  14164.     and    c1.colid = r.rkey9
  14165.     union    all
  14166.     select
  14167.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14168.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14169.         PK_TABLE_NAME         = o1.name,
  14170.         PK_COLUMN_NAME         = c1.name,
  14171.         PK_COLUMN_GUID        = convert(binary(16),null),
  14172.         PK_COLUMN_PROPID    = convert(int,null),
  14173.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14174.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  14175.         FK_TABLE_NAME         = o2.name,
  14176.         FK_COLUMN_NAME         = c2.name,
  14177.         FK_COLUMN_GUID        = convert(binary(16),null),
  14178.         FK_COLUMN_PROPID    = convert(int,null),
  14179.         ORDINAL            = convert(int,10),
  14180.         UPDATE_RULE        = 'NO ACTION',
  14181.         DELETE_RULE         = 'NO ACTION'        
  14182.     from
  14183.         sysobjects o1, sysobjects o2,
  14184.         syscolumns c1, syscolumns c2,
  14185.         sysreferences r
  14186.     where    
  14187.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14188.     and    o2.name = @fk_table_name
  14189.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14190.     and    o2.id = r.fkeyid
  14191.     and    r.keycnt >= 10
  14192.     and    o2.id = c2.id
  14193.     and    c2.colid = r.fkey10
  14194.     and     r.rkeyid = o1.id
  14195.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  14196.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14197.     and    o1.id = c1.id    
  14198.     and    c1.colid = r.rkey10
  14199.     union    all
  14200.     select
  14201.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14202.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14203.         PK_TABLE_NAME         = o1.name,
  14204.         PK_COLUMN_NAME         = c1.name,
  14205.         PK_COLUMN_GUID        = convert(binary(16),null),
  14206.         PK_COLUMN_PROPID    = convert(int,null),
  14207.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14208.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  14209.         FK_TABLE_NAME         = o2.name,
  14210.         FK_COLUMN_NAME         = c2.name,
  14211.         FK_COLUMN_GUID        = convert(binary(16),null),
  14212.         FK_COLUMN_PROPID    = convert(int,null),
  14213.         ORDINAL            = convert(int,11),
  14214.         UPDATE_RULE        = 'NO ACTION',
  14215.         DELETE_RULE         = 'NO ACTION'        
  14216.     from
  14217.         sysobjects o1, sysobjects o2,
  14218.         syscolumns c1, syscolumns c2,
  14219.         sysreferences r
  14220.     where    
  14221.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14222.     and    o2.name = @fk_table_name
  14223.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14224.     and    o2.id = r.fkeyid
  14225.     and    r.keycnt >= 11
  14226.     and    o2.id = c2.id
  14227.     and    c2.colid = r.fkey11
  14228.     and     r.rkeyid = o1.id
  14229.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  14230.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14231.     and    o1.id = c1.id    
  14232.     and    c1.colid = r.rkey11
  14233.     union    all
  14234.     select
  14235.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14236.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14237.         PK_TABLE_NAME         = o1.name,
  14238.         PK_COLUMN_NAME         = c1.name,
  14239.         PK_COLUMN_GUID        = convert(binary(16),null),
  14240.         PK_COLUMN_PROPID    = convert(int,null),
  14241.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14242.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  14243.         FK_TABLE_NAME         = o2.name,
  14244.         FK_COLUMN_NAME         = c2.name,
  14245.         FK_COLUMN_GUID        = convert(binary(16),null),
  14246.         FK_COLUMN_PROPID    = convert(int,null),
  14247.         ORDINAL            = convert(int,12),
  14248.         UPDATE_RULE        = 'NO ACTION',
  14249.         DELETE_RULE         = 'NO ACTION'        
  14250.     from
  14251.         sysobjects o1, sysobjects o2,
  14252.         syscolumns c1, syscolumns c2,
  14253.         sysreferences r
  14254.     where    
  14255.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14256.     and    o2.name = @fk_table_name
  14257.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14258.     and    o2.id = r.fkeyid
  14259.     and    r.keycnt >= 12
  14260.     and    o2.id = c2.id
  14261.     and    c2.colid = r.fkey12
  14262.     and     r.rkeyid = o1.id
  14263.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  14264.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14265.     and    o1.id = c1.id    
  14266.     and    c1.colid = r.rkey12
  14267.     union    all
  14268.     select
  14269.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14270.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14271.         PK_TABLE_NAME         = o1.name,
  14272.         PK_COLUMN_NAME         = c1.name,
  14273.         PK_COLUMN_GUID        = convert(binary(16),null),
  14274.         PK_COLUMN_PROPID    = convert(int,null),
  14275.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14276.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  14277.         FK_TABLE_NAME         = o2.name,
  14278.         FK_COLUMN_NAME         = c2.name,
  14279.         FK_COLUMN_GUID        = convert(binary(16),null),
  14280.         FK_COLUMN_PROPID    = convert(int,null),
  14281.         ORDINAL            = convert(int,13),
  14282.         UPDATE_RULE        = 'NO ACTION',
  14283.         DELETE_RULE         = 'NO ACTION'        
  14284.     from
  14285.         sysobjects o1, sysobjects o2,
  14286.         syscolumns c1, syscolumns c2,
  14287.         sysreferences r
  14288.     where    
  14289.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14290.     and    o2.name = @fk_table_name
  14291.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14292.     and    o2.id = r.fkeyid
  14293.     and    r.keycnt >= 13
  14294.     and    o2.id = c2.id
  14295.     and    c2.colid = r.fkey13
  14296.     and     r.rkeyid = o1.id
  14297.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  14298.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14299.     and    o1.id = c1.id    
  14300.     and    c1.colid = r.rkey13
  14301.     union    all
  14302.     select
  14303.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14304.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14305.         PK_TABLE_NAME         = o1.name,
  14306.         PK_COLUMN_NAME         = c1.name,
  14307.         PK_COLUMN_GUID        = convert(binary(16),null),
  14308.         PK_COLUMN_PROPID    = convert(int,null),
  14309.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14310.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  14311.         FK_TABLE_NAME         = o2.name,
  14312.         FK_COLUMN_NAME         = c2.name,
  14313.         FK_COLUMN_GUID        = convert(binary(16),null),
  14314.         FK_COLUMN_PROPID    = convert(int,null),
  14315.         ORDINAL            = convert(int,14),
  14316.         UPDATE_RULE        = 'NO ACTION',
  14317.         DELETE_RULE         = 'NO ACTION'        
  14318.     from
  14319.         sysobjects o1, sysobjects o2,
  14320.         syscolumns c1, syscolumns c2,
  14321.         sysreferences r
  14322.     where    
  14323.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14324.     and    o2.name = @fk_table_name
  14325.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14326.     and    o2.id = r.fkeyid
  14327.     and    r.keycnt >= 14
  14328.     and    o2.id = c2.id
  14329.     and    c2.colid = r.fkey14
  14330.     and     r.rkeyid = o1.id
  14331.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  14332.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14333.     and    o1.id = c1.id    
  14334.     and    c1.colid = r.rkey14
  14335.     union    all
  14336.     select
  14337.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14338.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14339.         PK_TABLE_NAME         = o1.name,
  14340.         PK_COLUMN_NAME         = c1.name,
  14341.         PK_COLUMN_GUID        = convert(binary(16),null),
  14342.         PK_COLUMN_PROPID    = convert(int,null),
  14343.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14344.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  14345.         FK_TABLE_NAME         = o2.name,
  14346.         FK_COLUMN_NAME         = c2.name,
  14347.         FK_COLUMN_GUID        = convert(binary(16),null),
  14348.         FK_COLUMN_PROPID    = convert(int,null),
  14349.         ORDINAL            = convert(int,15),
  14350.         UPDATE_RULE        = 'NO ACTION',
  14351.         DELETE_RULE         = 'NO ACTION'        
  14352.     from
  14353.         sysobjects o1, sysobjects o2,
  14354.         syscolumns c1, syscolumns c2,
  14355.         sysreferences r
  14356.     where    
  14357.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14358.     and    o2.name = @fk_table_name
  14359.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14360.     and    o2.id = r.fkeyid
  14361.     and    r.keycnt >= 15
  14362.     and    o2.id = c2.id
  14363.     and    c2.colid = r.fkey15
  14364.     and     r.rkeyid = o1.id
  14365.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  14366.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14367.     and    o1.id = c1.id    
  14368.     and    c1.colid = r.rkey15
  14369.     union    all
  14370.     select
  14371.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14372.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14373.         PK_TABLE_NAME         = o1.name,
  14374.         PK_COLUMN_NAME         = c1.name,
  14375.         PK_COLUMN_GUID        = convert(binary(16),null),
  14376.         PK_COLUMN_PROPID    = convert(int,null),
  14377.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14378.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  14379.         FK_TABLE_NAME         = o2.name,
  14380.         FK_COLUMN_NAME         = c2.name,
  14381.         FK_COLUMN_GUID        = convert(binary(16),null),
  14382.         FK_COLUMN_PROPID    = convert(int,null),
  14383.         ORDINAL            = convert(int,16),
  14384.         UPDATE_RULE        = 'NO ACTION',
  14385.         DELETE_RULE         = 'NO ACTION'        
  14386.     from
  14387.         sysobjects o1, sysobjects o2,
  14388.         syscolumns c1, syscolumns c2,
  14389.         sysreferences r
  14390.     where    
  14391.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14392.     and    o2.name = @fk_table_name
  14393.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14394.     and    o2.id = r.fkeyid
  14395.     and    r.keycnt >= 16
  14396.     and    o2.id = c2.id
  14397.     and    c2.colid = r.fkey16
  14398.     and     r.rkeyid = o1.id
  14399.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  14400.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14401.     and    o1.id = c1.id    
  14402.     and    c1.colid = r.rkey16
  14403.     order by 8,9,2,3,13
  14404. go
  14405. CREATE PROCEDURE sp_foreign_keys_rowset;3
  14406.     (
  14407.     @pk_table_schema    varchar(255) = null,
  14408.     @pk_table_catalog    varchar(255) = null,
  14409.     @fk_table_schema    varchar(255) = null,
  14410.     @fk_table_catalog    varchar(255) = null
  14411.     )
  14412. as
  14413.     select
  14414.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14415.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14416.         PK_TABLE_NAME         = o1.name,
  14417.         PK_COLUMN_NAME         = c1.name,
  14418.         PK_COLUMN_GUID        = convert(binary(16),null),
  14419.         PK_COLUMN_PROPID    = convert(int,null),
  14420.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14421.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  14422.         FK_TABLE_NAME         = o2.name,
  14423.         FK_COLUMN_NAME         = c2.name,
  14424.         FK_COLUMN_GUID        = convert(binary(16),null),
  14425.         FK_COLUMN_PROPID    = convert(int,null),
  14426.         ORDINAL            = convert(int,1),
  14427.         UPDATE_RULE        = 'NO ACTION',
  14428.         DELETE_RULE         = 'NO ACTION'        
  14429.     from
  14430.         sysobjects o1, sysobjects o2,
  14431.         syscolumns c1, syscolumns c2,
  14432.         sysreferences r
  14433.     where    
  14434.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14435.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14436.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14437.     and    o1.id = r.rkeyid
  14438.     and    o1.id = c1.id
  14439.     and    c1.colid = r.rkey1
  14440.     and     r.fkeyid = o2.id
  14441.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14442.     and    o2.id = c2.id    
  14443.     and    c2.colid = r.fkey1
  14444.     union all
  14445.     select
  14446.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14447.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14448.         PK_TABLE_NAME         = o1.name,
  14449.         PK_COLUMN_NAME         = c1.name,
  14450.         PK_COLUMN_GUID        = convert(binary(16),null),
  14451.         PK_COLUMN_PROPID    = convert(int,null),
  14452.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14453.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  14454.         FK_TABLE_NAME         = o2.name,
  14455.         FK_COLUMN_NAME         = c2.name,
  14456.         FK_COLUMN_GUID        = convert(binary(16),null),
  14457.         FK_COLUMN_PROPID    = convert(int,null),
  14458.         ORDINAL            = convert(int,2),
  14459.         UPDATE_RULE        = 'NO ACTION',
  14460.         DELETE_RULE         = 'NO ACTION'        
  14461.     from
  14462.         sysobjects o1, sysobjects o2,
  14463.         syscolumns c1, syscolumns c2,
  14464.         sysreferences r
  14465.     where    
  14466.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14467.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14468.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14469.     and    o1.id = r.rkeyid
  14470.     and    r.keycnt >= 2
  14471.     and    o1.id = c1.id
  14472.     and    c1.colid = r.rkey2
  14473.     and     r.fkeyid = o2.id
  14474.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14475.     and    o2.id = c2.id    
  14476.     and    c2.colid = r.fkey2
  14477.     union all
  14478.     select
  14479.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14480.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14481.         PK_TABLE_NAME         = o1.name,
  14482.         PK_COLUMN_NAME         = c1.name,
  14483.         PK_COLUMN_GUID        = convert(binary(16),null),
  14484.         PK_COLUMN_PROPID    = convert(int,null),
  14485.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14486.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  14487.         FK_TABLE_NAME         = o2.name,
  14488.         FK_COLUMN_NAME         = c2.name,
  14489.         FK_COLUMN_GUID        = convert(binary(16),null),
  14490.         FK_COLUMN_PROPID    = convert(int,null),
  14491.         ORDINAL            = convert(int,3),
  14492.         UPDATE_RULE        = 'NO ACTION',
  14493.         DELETE_RULE         = 'NO ACTION'        
  14494.     from
  14495.         sysobjects o1, sysobjects o2,
  14496.         syscolumns c1, syscolumns c2,
  14497.         sysreferences r
  14498.     where    
  14499.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14500.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14501.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14502.     and    o1.id = r.rkeyid
  14503.     and    r.keycnt >= 3
  14504.     and    o1.id = c1.id
  14505.     and    c1.colid = r.rkey3
  14506.     and     r.fkeyid = o2.id
  14507.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14508.     and    o2.id = c2.id    
  14509.     and    c2.colid = r.fkey3
  14510.     union all
  14511.     select
  14512.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14513.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14514.         PK_TABLE_NAME         = o1.name,
  14515.         PK_COLUMN_NAME         = c1.name,
  14516.         PK_COLUMN_GUID        = convert(binary(16),null),
  14517.         PK_COLUMN_PROPID    = convert(int,null),
  14518.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14519.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  14520.         FK_TABLE_NAME         = o2.name,
  14521.         FK_COLUMN_NAME         = c2.name,
  14522.         FK_COLUMN_GUID        = convert(binary(16),null),
  14523.         FK_COLUMN_PROPID    = convert(int,null),
  14524.         ORDINAL            = convert(int,4),
  14525.         UPDATE_RULE        = 'NO ACTION',
  14526.         DELETE_RULE         = 'NO ACTION'        
  14527.     from
  14528.         sysobjects o1, sysobjects o2,
  14529.         syscolumns c1, syscolumns c2,
  14530.         sysreferences r
  14531.     where    
  14532.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14533.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14534.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14535.     and    o1.id = r.rkeyid
  14536.     and    r.keycnt >= 4
  14537.     and    o1.id = c1.id
  14538.     and    c1.colid = r.rkey4
  14539.     and     r.fkeyid = o2.id
  14540.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14541.     and    o2.id = c2.id    
  14542.     and    c2.colid = r.fkey4
  14543.     union all
  14544.     select
  14545.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14546.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14547.         PK_TABLE_NAME         = o1.name,
  14548.         PK_COLUMN_NAME         = c1.name,
  14549.         PK_COLUMN_GUID        = convert(binary(16),null),
  14550.         PK_COLUMN_PROPID    = convert(int,null),
  14551.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14552.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  14553.         FK_TABLE_NAME         = o2.name,
  14554.         FK_COLUMN_NAME         = c2.name,
  14555.         FK_COLUMN_GUID        = convert(binary(16),null),
  14556.         FK_COLUMN_PROPID    = convert(int,null),
  14557.         ORDINAL            = convert(int,5),
  14558.         UPDATE_RULE        = 'NO ACTION',
  14559.         DELETE_RULE         = 'NO ACTION'        
  14560.     from
  14561.         sysobjects o1, sysobjects o2,
  14562.         syscolumns c1, syscolumns c2,
  14563.         sysreferences r
  14564.     where    
  14565.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14566.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14567.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14568.     and    o1.id = r.rkeyid
  14569.     and    r.keycnt >= 5
  14570.     and    o1.id = c1.id
  14571.     and    c1.colid = r.rkey5
  14572.     and     r.fkeyid = o2.id
  14573.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14574.     and    o2.id = c2.id    
  14575.     and    c2.colid = r.fkey5
  14576.     union all
  14577.     select
  14578.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14579.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14580.         PK_TABLE_NAME         = o1.name,
  14581.         PK_COLUMN_NAME         = c1.name,
  14582.         PK_COLUMN_GUID        = convert(binary(16),null),
  14583.         PK_COLUMN_PROPID    = convert(int,null),
  14584.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14585.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  14586.         FK_TABLE_NAME         = o2.name,
  14587.         FK_COLUMN_NAME         = c2.name,
  14588.         FK_COLUMN_GUID        = convert(binary(16),null),
  14589.         FK_COLUMN_PROPID    = convert(int,null),
  14590.         ORDINAL            = convert(int,6),
  14591.         UPDATE_RULE        = 'NO ACTION',
  14592.         DELETE_RULE         = 'NO ACTION'        
  14593.     from
  14594.         sysobjects o1, sysobjects o2,
  14595.         syscolumns c1, syscolumns c2,
  14596.         sysreferences r
  14597.     where    
  14598.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14599.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14600.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14601.     and    o1.id = r.rkeyid
  14602.     and    r.keycnt >= 6
  14603.     and    o1.id = c1.id
  14604.     and    c1.colid = r.rkey6
  14605.     and     r.fkeyid = o2.id
  14606.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14607.     and    o2.id = c2.id    
  14608.     and    c2.colid = r.fkey6
  14609.     union all
  14610.     select
  14611.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14612.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14613.         PK_TABLE_NAME         = o1.name,
  14614.         PK_COLUMN_NAME         = c1.name,
  14615.         PK_COLUMN_GUID        = convert(binary(16),null),
  14616.         PK_COLUMN_PROPID    = convert(int,null),
  14617.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14618.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  14619.         FK_TABLE_NAME         = o2.name,
  14620.         FK_COLUMN_NAME         = c2.name,
  14621.         FK_COLUMN_GUID        = convert(binary(16),null),
  14622.         FK_COLUMN_PROPID    = convert(int,null),
  14623.         ORDINAL            = convert(int,7),
  14624.         UPDATE_RULE        = 'NO ACTION',
  14625.         DELETE_RULE         = 'NO ACTION'        
  14626.     from
  14627.         sysobjects o1, sysobjects o2,
  14628.         syscolumns c1, syscolumns c2,
  14629.         sysreferences r
  14630.     where    
  14631.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14632.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14633.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14634.     and    o1.id = r.rkeyid
  14635.     and    r.keycnt >= 7
  14636.     and    o1.id = c1.id
  14637.     and    c1.colid = r.rkey7
  14638.     and     r.fkeyid = o2.id
  14639.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14640.     and    o2.id = c2.id    
  14641.     and    c2.colid = r.fkey7
  14642.     union all
  14643.     select
  14644.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14645.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14646.         PK_TABLE_NAME         = o1.name,
  14647.         PK_COLUMN_NAME         = c1.name,
  14648.         PK_COLUMN_GUID        = convert(binary(16),null),
  14649.         PK_COLUMN_PROPID    = convert(int,null),
  14650.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14651.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  14652.         FK_TABLE_NAME         = o2.name,
  14653.         FK_COLUMN_NAME         = c2.name,
  14654.         FK_COLUMN_GUID        = convert(binary(16),null),
  14655.         FK_COLUMN_PROPID    = convert(int,null),
  14656.         ORDINAL            = convert(int,8),
  14657.         UPDATE_RULE        = 'NO ACTION',
  14658.         DELETE_RULE         = 'NO ACTION'        
  14659.     from
  14660.         sysobjects o1, sysobjects o2,
  14661.         syscolumns c1, syscolumns c2,
  14662.         sysreferences r
  14663.     where    
  14664.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14665.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14666.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14667.     and    o1.id = r.rkeyid
  14668.     and    r.keycnt >= 8
  14669.     and    o1.id = c1.id
  14670.     and    c1.colid = r.rkey8
  14671.     and     r.fkeyid = o2.id
  14672.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14673.     and    o2.id = c2.id    
  14674.     and    c2.colid = r.fkey8
  14675.     union all
  14676.     select
  14677.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14678.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14679.         PK_TABLE_NAME         = o1.name,
  14680.         PK_COLUMN_NAME         = c1.name,
  14681.         PK_COLUMN_GUID        = convert(binary(16),null),
  14682.         PK_COLUMN_PROPID    = convert(int,null),
  14683.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14684.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  14685.         FK_TABLE_NAME         = o2.name,
  14686.         FK_COLUMN_NAME         = c2.name,
  14687.         FK_COLUMN_GUID        = convert(binary(16),null),
  14688.         FK_COLUMN_PROPID    = convert(int,null),
  14689.         ORDINAL            = convert(int,9),
  14690.         UPDATE_RULE        = 'NO ACTION',
  14691.         DELETE_RULE         = 'NO ACTION'        
  14692.     from
  14693.         sysobjects o1, sysobjects o2,
  14694.         syscolumns c1, syscolumns c2,
  14695.         sysreferences r
  14696.     where    
  14697.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14698.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14699.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14700.     and    o1.id = r.rkeyid
  14701.     and    r.keycnt >= 9
  14702.     and    o1.id = c1.id
  14703.     and    c1.colid = r.rkey9
  14704.     and     r.fkeyid = o2.id
  14705.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14706.     and    o2.id = c2.id    
  14707.     and    c2.colid = r.fkey9
  14708.     union all
  14709.     select
  14710.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14711.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14712.         PK_TABLE_NAME         = o1.name,
  14713.         PK_COLUMN_NAME         = c1.name,
  14714.         PK_COLUMN_GUID        = convert(binary(16),null),
  14715.         PK_COLUMN_PROPID    = convert(int,null),
  14716.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14717.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  14718.         FK_TABLE_NAME         = o2.name,
  14719.         FK_COLUMN_NAME         = c2.name,
  14720.         FK_COLUMN_GUID        = convert(binary(16),null),
  14721.         FK_COLUMN_PROPID    = convert(int,null),
  14722.         ORDINAL            = convert(int,10),
  14723.         UPDATE_RULE        = 'NO ACTION',
  14724.         DELETE_RULE         = 'NO ACTION'        
  14725.     from
  14726.         sysobjects o1, sysobjects o2,
  14727.         syscolumns c1, syscolumns c2,
  14728.         sysreferences r
  14729.     where    
  14730.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14731.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14732.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14733.     and    o1.id = r.rkeyid
  14734.     and    r.keycnt >= 10
  14735.     and    o1.id = c1.id
  14736.     and    c1.colid = r.rkey10
  14737.     and     r.fkeyid = o2.id
  14738.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14739.     and    o2.id = c2.id    
  14740.     and    c2.colid = r.fkey10
  14741.     union all
  14742.     select
  14743.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14744.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14745.         PK_TABLE_NAME         = o1.name,
  14746.         PK_COLUMN_NAME         = c1.name,
  14747.         PK_COLUMN_GUID        = convert(binary(16),null),
  14748.         PK_COLUMN_PROPID    = convert(int,null),
  14749.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14750.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  14751.         FK_TABLE_NAME         = o2.name,
  14752.         FK_COLUMN_NAME         = c2.name,
  14753.         FK_COLUMN_GUID        = convert(binary(16),null),
  14754.         FK_COLUMN_PROPID    = convert(int,null),
  14755.         ORDINAL            = convert(int,11),
  14756.         UPDATE_RULE        = 'NO ACTION',
  14757.         DELETE_RULE         = 'NO ACTION'        
  14758.     from
  14759.         sysobjects o1, sysobjects o2,
  14760.         syscolumns c1, syscolumns c2,
  14761.         sysreferences r
  14762.     where    
  14763.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14764.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14765.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14766.     and    o1.id = r.rkeyid
  14767.     and    r.keycnt >= 11
  14768.     and    o1.id = c1.id
  14769.     and    c1.colid = r.rkey11
  14770.     and     r.fkeyid = o2.id
  14771.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14772.     and    o2.id = c2.id    
  14773.     and    c2.colid = r.fkey11
  14774.     union all
  14775.     select
  14776.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14777.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14778.         PK_TABLE_NAME         = o1.name,
  14779.         PK_COLUMN_NAME         = c1.name,
  14780.         PK_COLUMN_GUID        = convert(binary(16),null),
  14781.         PK_COLUMN_PROPID    = convert(int,null),
  14782.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14783.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  14784.         FK_TABLE_NAME         = o2.name,
  14785.         FK_COLUMN_NAME         = c2.name,
  14786.         FK_COLUMN_GUID        = convert(binary(16),null),
  14787.         FK_COLUMN_PROPID    = convert(int,null),
  14788.         ORDINAL            = convert(int,12),
  14789.         UPDATE_RULE        = 'NO ACTION',
  14790.         DELETE_RULE         = 'NO ACTION'        
  14791.     from
  14792.         sysobjects o1, sysobjects o2,
  14793.         syscolumns c1, syscolumns c2,
  14794.         sysreferences r
  14795.     where    
  14796.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14797.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14798.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14799.     and    o1.id = r.rkeyid
  14800.     and    r.keycnt >= 12
  14801.     and    o1.id = c1.id
  14802.     and    c1.colid = r.rkey12
  14803.     and     r.fkeyid = o2.id
  14804.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14805.     and    o2.id = c2.id    
  14806.     and    c2.colid = r.fkey12
  14807.     union all
  14808.     select
  14809.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14810.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14811.         PK_TABLE_NAME         = o1.name,
  14812.         PK_COLUMN_NAME         = c1.name,
  14813.         PK_COLUMN_GUID        = convert(binary(16),null),
  14814.         PK_COLUMN_PROPID    = convert(int,null),
  14815.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14816.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  14817.         FK_TABLE_NAME         = o2.name,
  14818.         FK_COLUMN_NAME         = c2.name,
  14819.         FK_COLUMN_GUID        = convert(binary(16),null),
  14820.         FK_COLUMN_PROPID    = convert(int,null),
  14821.         ORDINAL            = convert(int,13),
  14822.         UPDATE_RULE        = 'NO ACTION',
  14823.         DELETE_RULE         = 'NO ACTION'        
  14824.     from
  14825.         sysobjects o1, sysobjects o2,
  14826.         syscolumns c1, syscolumns c2,
  14827.         sysreferences r
  14828.     where    
  14829.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14830.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14831.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14832.     and    o1.id = r.rkeyid
  14833.     and    r.keycnt >= 13
  14834.     and    o1.id = c1.id
  14835.     and    c1.colid = r.rkey13
  14836.     and     r.fkeyid = o2.id
  14837.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14838.     and    o2.id = c2.id    
  14839.     and    c2.colid = r.fkey13
  14840.     union all
  14841.     select
  14842.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14843.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14844.         PK_TABLE_NAME         = o1.name,
  14845.         PK_COLUMN_NAME         = c1.name,
  14846.         PK_COLUMN_GUID        = convert(binary(16),null),
  14847.         PK_COLUMN_PROPID    = convert(int,null),
  14848.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14849.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  14850.         FK_TABLE_NAME         = o2.name,
  14851.         FK_COLUMN_NAME         = c2.name,
  14852.         FK_COLUMN_GUID        = convert(binary(16),null),
  14853.         FK_COLUMN_PROPID    = convert(int,null),
  14854.         ORDINAL            = convert(int,14),
  14855.         UPDATE_RULE        = 'NO ACTION',
  14856.         DELETE_RULE         = 'NO ACTION'        
  14857.     from
  14858.         sysobjects o1, sysobjects o2,
  14859.         syscolumns c1, syscolumns c2,
  14860.         sysreferences r
  14861.     where    
  14862.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14863.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14864.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14865.     and    o1.id = r.rkeyid
  14866.     and    r.keycnt >= 14
  14867.     and    o1.id = c1.id
  14868.     and    c1.colid = r.rkey14
  14869.     and     r.fkeyid = o2.id
  14870.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14871.     and    o2.id = c2.id    
  14872.     and    c2.colid = r.fkey14
  14873.     union all
  14874.     select
  14875.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14876.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14877.         PK_TABLE_NAME         = o1.name,
  14878.         PK_COLUMN_NAME         = c1.name,
  14879.         PK_COLUMN_GUID        = convert(binary(16),null),
  14880.         PK_COLUMN_PROPID    = convert(int,null),
  14881.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14882.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  14883.         FK_TABLE_NAME         = o2.name,
  14884.         FK_COLUMN_NAME         = c2.name,
  14885.         FK_COLUMN_GUID        = convert(binary(16),null),
  14886.         FK_COLUMN_PROPID    = convert(int,null),
  14887.         ORDINAL            = convert(int,15),
  14888.         UPDATE_RULE        = 'NO ACTION',
  14889.         DELETE_RULE         = 'NO ACTION'        
  14890.     from
  14891.         sysobjects o1, sysobjects o2,
  14892.         syscolumns c1, syscolumns c2,
  14893.         sysreferences r
  14894.     where    
  14895.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14896.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14897.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14898.     and    o1.id = r.rkeyid
  14899.     and    r.keycnt >= 15
  14900.     and    o1.id = c1.id
  14901.     and    c1.colid = r.rkey15
  14902.     and     r.fkeyid = o2.id
  14903.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14904.     and    o2.id = c2.id    
  14905.     and    c2.colid = r.fkey15
  14906.     union all
  14907.     select
  14908.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14909.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14910.         PK_TABLE_NAME         = o1.name,
  14911.         PK_COLUMN_NAME         = c1.name,
  14912.         PK_COLUMN_GUID        = convert(binary(16),null),
  14913.         PK_COLUMN_PROPID    = convert(int,null),
  14914.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14915.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  14916.         FK_TABLE_NAME         = o2.name,
  14917.         FK_COLUMN_NAME         = c2.name,
  14918.         FK_COLUMN_GUID        = convert(binary(16),null),
  14919.         FK_COLUMN_PROPID    = convert(int,null),
  14920.         ORDINAL            = convert(int,16),
  14921.         UPDATE_RULE        = 'NO ACTION',
  14922.         DELETE_RULE         = 'NO ACTION'        
  14923.     from
  14924.         sysobjects o1, sysobjects o2,
  14925.         syscolumns c1, syscolumns c2,
  14926.         sysreferences r
  14927.     where    
  14928.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14929.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14930.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14931.     and    o1.id = r.rkeyid
  14932.     and    o1.id = c1.id
  14933.     and    r.keycnt >= 16
  14934.     and    c1.colid = r.rkey16
  14935.     and     r.fkeyid = o2.id
  14936.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14937.     and    o2.id = c2.id    
  14938.     and    c2.colid = r.fkey16
  14939.     order by 8,9,2,3,13
  14940. go
  14941. CREATE PROCEDURE sp_foreign_keys_rowset;4
  14942.     (
  14943.     @pk_table_name        varchar(255) = null,
  14944.     @pk_table_schema    varchar(255) = null,
  14945.     @fk_table_name        varchar(255) = null,
  14946.     @fk_table_schema    varchar(255) = null
  14947.     )
  14948. as
  14949. IF @pk_table_name is not null
  14950.     BEGIN
  14951.     select
  14952.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14953.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14954.         PK_TABLE_NAME         = o1.name,
  14955.         PK_COLUMN_NAME         = c1.name,
  14956.         PK_COLUMN_GUID        = convert(binary(16),null),
  14957.         PK_COLUMN_PROPID    = convert(int,null),
  14958.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14959.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  14960.         FK_TABLE_NAME         = o2.name,
  14961.         FK_COLUMN_NAME         = c2.name,
  14962.         FK_COLUMN_GUID        = convert(binary(16),null),
  14963.         FK_COLUMN_PROPID    = convert(int,null),
  14964.         ORDINAL            = convert(int,1),
  14965.         UPDATE_RULE        = 'NO ACTION',
  14966.         DELETE_RULE         = 'NO ACTION'        
  14967.     from
  14968.         sysobjects o1, sysobjects o2,
  14969.         syscolumns c1, syscolumns c2,
  14970.         sysreferences r
  14971.     where    
  14972.         o1.name = @pk_table_name
  14973.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14974.     and    o1.id = r.rkeyid
  14975.     and    o1.id = c1.id
  14976.     and    c1.colid = r.rkey1
  14977.     and     r.fkeyid = o2.id
  14978.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  14979.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14980.     and    o2.id = c2.id    
  14981.     and    c2.colid = r.fkey1
  14982.     union all
  14983.     select
  14984.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14985.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14986.         PK_TABLE_NAME         = o1.name,
  14987.         PK_COLUMN_NAME         = c1.name,
  14988.         PK_COLUMN_GUID        = convert(binary(16),null),
  14989.         PK_COLUMN_PROPID    = convert(int,null),
  14990.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14991.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  14992.         FK_TABLE_NAME         = o2.name,
  14993.         FK_COLUMN_NAME         = c2.name,
  14994.         FK_COLUMN_GUID        = convert(binary(16),null),
  14995.         FK_COLUMN_PROPID    = convert(int,null),
  14996.         ORDINAL            = convert(int,2),
  14997.         UPDATE_RULE        = 'NO ACTION',
  14998.         DELETE_RULE         = 'NO ACTION'        
  14999.     from
  15000.         sysobjects o1, sysobjects o2,
  15001.         syscolumns c1, syscolumns c2,
  15002.         sysreferences r
  15003.     where    
  15004.         o1.name = @pk_table_name
  15005.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15006.     and    o1.id = r.rkeyid
  15007.     and    o1.id = c1.id
  15008.     and    c1.colid = r.rkey2
  15009.     and    r.keycnt >= 2
  15010.     and     r.fkeyid = o2.id
  15011.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  15012.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15013.     and    o2.id = c2.id    
  15014.     and    c2.colid = r.fkey2
  15015.     union all
  15016.     select
  15017.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15018.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15019.         PK_TABLE_NAME         = o1.name,
  15020.         PK_COLUMN_NAME         = c1.name,
  15021.         PK_COLUMN_GUID        = convert(binary(16),null),
  15022.         PK_COLUMN_PROPID    = convert(int,null),
  15023.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15024.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  15025.         FK_TABLE_NAME         = o2.name,
  15026.         FK_COLUMN_NAME         = c2.name,
  15027.         FK_COLUMN_GUID        = convert(binary(16),null),
  15028.         FK_COLUMN_PROPID    = convert(int,null),
  15029.         ORDINAL            = convert(int,3),
  15030.         UPDATE_RULE        = 'NO ACTION',
  15031.         DELETE_RULE         = 'NO ACTION'        
  15032.     from
  15033.         sysobjects o1, sysobjects o2,
  15034.         syscolumns c1, syscolumns c2,
  15035.         sysreferences r
  15036.     where    
  15037.         o1.name = @pk_table_name
  15038.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15039.     and    o1.id = r.rkeyid
  15040.     and    o1.id = c1.id
  15041.     and    c1.colid = r.rkey3
  15042.     and    r.keycnt >= 3
  15043.     and     r.fkeyid = o2.id
  15044.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  15045.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15046.     and    o2.id = c2.id    
  15047.     and    c2.colid = r.fkey3
  15048.     union all
  15049.     select
  15050.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15051.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15052.         PK_TABLE_NAME         = o1.name,
  15053.         PK_COLUMN_NAME         = c1.name,
  15054.         PK_COLUMN_GUID        = convert(binary(16),null),
  15055.         PK_COLUMN_PROPID    = convert(int,null),
  15056.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15057.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  15058.         FK_TABLE_NAME         = o2.name,
  15059.         FK_COLUMN_NAME         = c2.name,
  15060.         FK_COLUMN_GUID        = convert(binary(16),null),
  15061.         FK_COLUMN_PROPID    = convert(int,null),
  15062.         ORDINAL            = convert(int,4),
  15063.         UPDATE_RULE        = 'NO ACTION',
  15064.         DELETE_RULE         = 'NO ACTION'        
  15065.     from
  15066.         sysobjects o1, sysobjects o2,
  15067.         syscolumns c1, syscolumns c2,
  15068.         sysreferences r
  15069.     where    
  15070.         o1.name = @pk_table_name
  15071.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15072.     and    o1.id = r.rkeyid
  15073.     and    r.keycnt >= 4
  15074.     and    o1.id = c1.id
  15075.     and    c1.colid = r.rkey4
  15076.     and     r.fkeyid = o2.id
  15077.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  15078.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15079.     and    o2.id = c2.id    
  15080.     and    c2.colid = r.fkey4
  15081.     union all
  15082.     select
  15083.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15084.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15085.         PK_TABLE_NAME         = o1.name,
  15086.         PK_COLUMN_NAME         = c1.name,
  15087.         PK_COLUMN_GUID        = convert(binary(16),null),
  15088.         PK_COLUMN_PROPID    = convert(int,null),
  15089.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15090.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  15091.         FK_TABLE_NAME         = o2.name,
  15092.         FK_COLUMN_NAME         = c2.name,
  15093.         FK_COLUMN_GUID        = convert(binary(16),null),
  15094.         FK_COLUMN_PROPID    = convert(int,null),
  15095.         ORDINAL            = convert(int,5),
  15096.         UPDATE_RULE        = 'NO ACTION',
  15097.         DELETE_RULE         = 'NO ACTION'        
  15098.     from
  15099.         sysobjects o1, sysobjects o2,
  15100.         syscolumns c1, syscolumns c2,
  15101.         sysreferences r
  15102.     where    
  15103.         o1.name = @pk_table_name
  15104.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15105.     and    o1.id = r.rkeyid
  15106.     and    r.keycnt >= 5
  15107.     and    o1.id = c1.id
  15108.     and    c1.colid = r.rkey5
  15109.     and     r.fkeyid = o2.id
  15110.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  15111.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15112.     and    o2.id = c2.id    
  15113.     and    c2.colid = r.fkey5
  15114.     union all
  15115.     select
  15116.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15117.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15118.         PK_TABLE_NAME         = o1.name,
  15119.         PK_COLUMN_NAME         = c1.name,
  15120.         PK_COLUMN_GUID        = convert(binary(16),null),
  15121.         PK_COLUMN_PROPID    = convert(int,null),
  15122.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15123.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  15124.         FK_TABLE_NAME         = o2.name,
  15125.         FK_COLUMN_NAME         = c2.name,
  15126.         FK_COLUMN_GUID        = convert(binary(16),null),
  15127.         FK_COLUMN_PROPID    = convert(int,null),
  15128.         ORDINAL            = convert(int,6),
  15129.         UPDATE_RULE        = 'NO ACTION',
  15130.         DELETE_RULE         = 'NO ACTION'        
  15131.     from
  15132.         sysobjects o1, sysobjects o2,
  15133.         syscolumns c1, syscolumns c2,
  15134.         sysreferences r
  15135.     where    
  15136.         o1.name = @pk_table_name
  15137.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15138.     and    o1.id = r.rkeyid
  15139.     and    r.keycnt >= 6
  15140.     and    o1.id = c1.id
  15141.     and    c1.colid = r.rkey6
  15142.     and     r.fkeyid = o2.id
  15143.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  15144.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15145.     and    o2.id = c2.id    
  15146.     and    c2.colid = r.fkey6
  15147.     union all
  15148.     select
  15149.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15150.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15151.         PK_TABLE_NAME         = o1.name,
  15152.         PK_COLUMN_NAME         = c1.name,
  15153.         PK_COLUMN_GUID        = convert(binary(16),null),
  15154.         PK_COLUMN_PROPID    = convert(int,null),
  15155.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15156.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  15157.         FK_TABLE_NAME         = o2.name,
  15158.         FK_COLUMN_NAME         = c2.name,
  15159.         FK_COLUMN_GUID        = convert(binary(16),null),
  15160.         FK_COLUMN_PROPID    = convert(int,null),
  15161.         ORDINAL            = convert(int,7),
  15162.         UPDATE_RULE        = 'NO ACTION',
  15163.         DELETE_RULE         = 'NO ACTION'        
  15164.     from
  15165.         sysobjects o1, sysobjects o2,
  15166.         syscolumns c1, syscolumns c2,
  15167.         sysreferences r
  15168.     where    
  15169.         o1.name = @pk_table_name
  15170.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15171.     and    o1.id = r.rkeyid
  15172.     and    r.keycnt >= 7
  15173.     and    o1.id = c1.id
  15174.     and    c1.colid = r.rkey7
  15175.     and     r.fkeyid = o2.id
  15176.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  15177.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15178.     and    o2.id = c2.id    
  15179.     and    c2.colid = r.fkey7
  15180.     union all
  15181.     select
  15182.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15183.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15184.         PK_TABLE_NAME         = o1.name,
  15185.         PK_COLUMN_NAME         = c1.name,
  15186.         PK_COLUMN_GUID        = convert(binary(16),null),
  15187.         PK_COLUMN_PROPID    = convert(int,null),
  15188.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15189.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  15190.         FK_TABLE_NAME         = o2.name,
  15191.         FK_COLUMN_NAME         = c2.name,
  15192.         FK_COLUMN_GUID        = convert(binary(16),null),
  15193.         FK_COLUMN_PROPID    = convert(int,null),
  15194.         ORDINAL            = convert(int,8),
  15195.         UPDATE_RULE        = 'NO ACTION',
  15196.         DELETE_RULE         = 'NO ACTION'        
  15197.     from
  15198.         sysobjects o1, sysobjects o2,
  15199.         syscolumns c1, syscolumns c2,
  15200.         sysreferences r
  15201.     where    
  15202.         o1.name = @pk_table_name
  15203.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15204.     and    o1.id = r.rkeyid
  15205.     and    r.keycnt >= 8
  15206.     and    o1.id = c1.id
  15207.     and    c1.colid = r.rkey8
  15208.     and     r.fkeyid = o2.id
  15209.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  15210.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15211.     and    o2.id = c2.id    
  15212.     and    c2.colid = r.fkey8
  15213.     union all
  15214.     select
  15215.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15216.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15217.         PK_TABLE_NAME         = o1.name,
  15218.         PK_COLUMN_NAME         = c1.name,
  15219.         PK_COLUMN_GUID        = convert(binary(16),null),
  15220.         PK_COLUMN_PROPID    = convert(int,null),
  15221.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15222.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  15223.         FK_TABLE_NAME         = o2.name,
  15224.         FK_COLUMN_NAME         = c2.name,
  15225.         FK_COLUMN_GUID        = convert(binary(16),null),
  15226.         FK_COLUMN_PROPID    = convert(int,null),
  15227.         ORDINAL            = convert(int,9),
  15228.         UPDATE_RULE        = 'NO ACTION',
  15229.         DELETE_RULE         = 'NO ACTION'        
  15230.     from
  15231.         sysobjects o1, sysobjects o2,
  15232.         syscolumns c1, syscolumns c2,
  15233.         sysreferences r
  15234.     where    
  15235.         o1.name = @pk_table_name
  15236.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15237.     and    o1.id = r.rkeyid
  15238.     and    r.keycnt >= 9
  15239.     and    o1.id = c1.id
  15240.     and    c1.colid = r.rkey9
  15241.     and     r.fkeyid = o2.id
  15242.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  15243.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15244.     and    o2.id = c2.id    
  15245.     and    c2.colid = r.fkey9
  15246.     union all
  15247.     select
  15248.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15249.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15250.         PK_TABLE_NAME         = o1.name,
  15251.         PK_COLUMN_NAME         = c1.name,
  15252.         PK_COLUMN_GUID        = convert(binary(16),null),
  15253.         PK_COLUMN_PROPID    = convert(int,null),
  15254.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15255.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  15256.         FK_TABLE_NAME         = o2.name,
  15257.         FK_COLUMN_NAME         = c2.name,
  15258.         FK_COLUMN_GUID        = convert(binary(16),null),
  15259.         FK_COLUMN_PROPID    = convert(int,null),
  15260.         ORDINAL            = convert(int,10),
  15261.         UPDATE_RULE        = 'NO ACTION',
  15262.         DELETE_RULE         = 'NO ACTION'        
  15263.     from
  15264.         sysobjects o1, sysobjects o2,
  15265.         syscolumns c1, syscolumns c2,
  15266.         sysreferences r
  15267.     where    
  15268.         o1.name = @pk_table_name
  15269.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15270.     and    o1.id = r.rkeyid
  15271.     and    r.keycnt >= 10
  15272.     and    o1.id = c1.id
  15273.     and    c1.colid = r.rkey10
  15274.     and     r.fkeyid = o2.id
  15275.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  15276.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15277.     and    o2.id = c2.id    
  15278.     and    c2.colid = r.fkey10
  15279.     union all
  15280.     select
  15281.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15282.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15283.         PK_TABLE_NAME         = o1.name,
  15284.         PK_COLUMN_NAME         = c1.name,
  15285.         PK_COLUMN_GUID        = convert(binary(16),null),
  15286.         PK_COLUMN_PROPID    = convert(int,null),
  15287.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15288.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  15289.         FK_TABLE_NAME         = o2.name,
  15290.         FK_COLUMN_NAME         = c2.name,
  15291.         FK_COLUMN_GUID        = convert(binary(16),null),
  15292.         FK_COLUMN_PROPID    = convert(int,null),
  15293.         ORDINAL            = convert(int,11),
  15294.         UPDATE_RULE        = 'NO ACTION',
  15295.         DELETE_RULE         = 'NO ACTION'        
  15296.     from
  15297.         sysobjects o1, sysobjects o2,
  15298.         syscolumns c1, syscolumns c2,
  15299.         sysreferences r
  15300.     where    
  15301.         o1.name = @pk_table_name
  15302.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15303.     and    o1.id = r.rkeyid
  15304.     and    r.keycnt >= 11
  15305.     and    o1.id = c1.id
  15306.     and    c1.colid = r.rkey11
  15307.     and     r.fkeyid = o2.id
  15308.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  15309.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15310.     and    o2.id = c2.id    
  15311.     and    c2.colid = r.fkey11
  15312.     union all
  15313.     select
  15314.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15315.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15316.         PK_TABLE_NAME         = o1.name,
  15317.         PK_COLUMN_NAME         = c1.name,
  15318.         PK_COLUMN_GUID        = convert(binary(16),null),
  15319.         PK_COLUMN_PROPID    = convert(int,null),
  15320.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15321.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  15322.         FK_TABLE_NAME         = o2.name,
  15323.         FK_COLUMN_NAME         = c2.name,
  15324.         FK_COLUMN_GUID        = convert(binary(16),null),
  15325.         FK_COLUMN_PROPID    = convert(int,null),
  15326.         ORDINAL            = convert(int,12),
  15327.         UPDATE_RULE        = 'NO ACTION',
  15328.         DELETE_RULE         = 'NO ACTION'        
  15329.     from
  15330.         sysobjects o1, sysobjects o2,
  15331.         syscolumns c1, syscolumns c2,
  15332.         sysreferences r
  15333.     where    
  15334.         o1.name = @pk_table_name
  15335.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15336.     and    o1.id = r.rkeyid
  15337.     and    r.keycnt >= 12
  15338.     and    o1.id = c1.id
  15339.     and    c1.colid = r.rkey12
  15340.     and     r.fkeyid = o2.id
  15341.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  15342.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15343.     and    o2.id = c2.id    
  15344.     and    c2.colid = r.fkey12
  15345.     union all
  15346.     select
  15347.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15348.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15349.         PK_TABLE_NAME         = o1.name,
  15350.         PK_COLUMN_NAME         = c1.name,
  15351.         PK_COLUMN_GUID        = convert(binary(16),null),
  15352.         PK_COLUMN_PROPID    = convert(int,null),
  15353.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15354.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  15355.         FK_TABLE_NAME         = o2.name,
  15356.         FK_COLUMN_NAME         = c2.name,
  15357.         FK_COLUMN_GUID        = convert(binary(16),null),
  15358.         FK_COLUMN_PROPID    = convert(int,null),
  15359.         ORDINAL            = convert(int,13),
  15360.         UPDATE_RULE        = 'NO ACTION',
  15361.         DELETE_RULE         = 'NO ACTION'        
  15362.     from
  15363.         sysobjects o1, sysobjects o2,
  15364.         syscolumns c1, syscolumns c2,
  15365.         sysreferences r
  15366.     where    
  15367.         o1.name = @pk_table_name
  15368.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15369.     and    o1.id = r.rkeyid
  15370.     and    r.keycnt >= 13
  15371.     and    o1.id = c1.id
  15372.     and    c1.colid = r.rkey13
  15373.     and     r.fkeyid = o2.id
  15374.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  15375.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15376.     and    o2.id = c2.id    
  15377.     and    c2.colid = r.fkey13
  15378.     union all
  15379.     select
  15380.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15381.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15382.         PK_TABLE_NAME         = o1.name,
  15383.         PK_COLUMN_NAME         = c1.name,
  15384.         PK_COLUMN_GUID        = convert(binary(16),null),
  15385.         PK_COLUMN_PROPID    = convert(int,null),
  15386.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15387.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  15388.         FK_TABLE_NAME         = o2.name,
  15389.         FK_COLUMN_NAME         = c2.name,
  15390.         FK_COLUMN_GUID        = convert(binary(16),null),
  15391.         FK_COLUMN_PROPID    = convert(int,null),
  15392.         ORDINAL            = convert(int,14),
  15393.         UPDATE_RULE        = 'NO ACTION',
  15394.         DELETE_RULE         = 'NO ACTION'        
  15395.     from
  15396.         sysobjects o1, sysobjects o2,
  15397.         syscolumns c1, syscolumns c2,
  15398.         sysreferences r
  15399.     where    
  15400.         o1.name = @pk_table_name
  15401.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15402.     and    o1.id = r.rkeyid
  15403.     and    r.keycnt >= 14
  15404.     and    o1.id = c1.id
  15405.     and    c1.colid = r.rkey14
  15406.     and     r.fkeyid = o2.id
  15407.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  15408.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15409.     and    o2.id = c2.id    
  15410.     and    c2.colid = r.fkey14
  15411.     union all
  15412.     select
  15413.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15414.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15415.         PK_TABLE_NAME         = o1.name,
  15416.         PK_COLUMN_NAME         = c1.name,
  15417.         PK_COLUMN_GUID        = convert(binary(16),null),
  15418.         PK_COLUMN_PROPID    = convert(int,null),
  15419.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15420.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  15421.         FK_TABLE_NAME         = o2.name,
  15422.         FK_COLUMN_NAME         = c2.name,
  15423.         FK_COLUMN_GUID        = convert(binary(16),null),
  15424.         FK_COLUMN_PROPID    = convert(int,null),
  15425.         ORDINAL            = convert(int,15),
  15426.         UPDATE_RULE        = 'NO ACTION',
  15427.         DELETE_RULE         = 'NO ACTION'        
  15428.     from
  15429.         sysobjects o1, sysobjects o2,
  15430.         syscolumns c1, syscolumns c2,
  15431.         sysreferences r
  15432.     where    
  15433.         o1.name = @pk_table_name
  15434.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15435.     and    o1.id = r.rkeyid
  15436.     and    r.keycnt >= 15
  15437.     and    o1.id = c1.id
  15438.     and    c1.colid = r.rkey15
  15439.     and     r.fkeyid = o2.id
  15440.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  15441.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15442.     and    o2.id = c2.id    
  15443.     and    c2.colid = r.fkey15
  15444.     union all
  15445.     select
  15446.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15447.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15448.         PK_TABLE_NAME         = o1.name,
  15449.         PK_COLUMN_NAME         = c1.name,
  15450.         PK_COLUMN_GUID        = convert(binary(16),null),
  15451.         PK_COLUMN_PROPID    = convert(int,null),
  15452.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15453.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  15454.         FK_TABLE_NAME         = o2.name,
  15455.         FK_COLUMN_NAME         = c2.name,
  15456.         FK_COLUMN_GUID        = convert(binary(16),null),
  15457.         FK_COLUMN_PROPID    = convert(int,null),
  15458.         ORDINAL            = convert(int,16),
  15459.         UPDATE_RULE        = 'NO ACTION',
  15460.         DELETE_RULE         = 'NO ACTION'        
  15461.     from
  15462.         sysobjects o1, sysobjects o2,
  15463.         syscolumns c1, syscolumns c2,
  15464.         sysreferences r
  15465.     where    
  15466.         o1.name = @pk_table_name
  15467.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15468.     and    o1.id = r.rkeyid
  15469.     and    r.keycnt >= 16
  15470.     and    o1.id = c1.id
  15471.     and    c1.colid = r.rkey16
  15472.     and     r.fkeyid = o2.id
  15473.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  15474.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15475.     and    o2.id = c2.id    
  15476.     and    c2.colid = r.fkey16
  15477.     END
  15478. ELSE IF @fk_table_name is not null
  15479.     BEGIN
  15480.     select
  15481.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15482.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15483.         PK_TABLE_NAME         = o1.name,
  15484.         PK_COLUMN_NAME         = c1.name,
  15485.         PK_COLUMN_GUID        = convert(binary(16),null),
  15486.         PK_COLUMN_PROPID    = convert(int,null),
  15487.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15488.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  15489.         FK_TABLE_NAME         = o2.name,
  15490.         FK_COLUMN_NAME         = c2.name,
  15491.         FK_COLUMN_GUID        = convert(binary(16),null),
  15492.         FK_COLUMN_PROPID    = convert(int,null),
  15493.         ORDINAL            = convert(int,1),
  15494.         UPDATE_RULE        = 'NO ACTION',
  15495.         DELETE_RULE         = 'NO ACTION'        
  15496.     from
  15497.         sysobjects o1, sysobjects o2,
  15498.         syscolumns c1, syscolumns c2,
  15499.         sysreferences r
  15500.     where    
  15501.         o2.name = @fk_table_name
  15502.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15503.     and    o2.id = r.fkeyid
  15504.     and    o2.id = c2.id
  15505.     and    c2.colid = r.fkey1
  15506.     and     r.rkeyid = o1.id
  15507.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  15508.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15509.     and    o1.id = c1.id    
  15510.     and    c1.colid = r.rkey1
  15511.     union    all
  15512.     select
  15513.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15514.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15515.         PK_TABLE_NAME         = o1.name,
  15516.         PK_COLUMN_NAME         = c1.name,
  15517.         PK_COLUMN_GUID        = convert(binary(16),null),
  15518.         PK_COLUMN_PROPID    = convert(int,null),
  15519.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15520.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  15521.         FK_TABLE_NAME         = o2.name,
  15522.         FK_COLUMN_NAME         = c2.name,
  15523.         FK_COLUMN_GUID        = convert(binary(16),null),
  15524.         FK_COLUMN_PROPID    = convert(int,null),
  15525.         ORDINAL            = convert(int,2),
  15526.         UPDATE_RULE        = 'NO ACTION',
  15527.         DELETE_RULE         = 'NO ACTION'        
  15528.     from
  15529.         sysobjects o1, sysobjects o2,
  15530.         syscolumns c1, syscolumns c2,
  15531.         sysreferences r
  15532.     where    
  15533.         o2.name = @fk_table_name
  15534.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15535.     and    o2.id = r.fkeyid
  15536.     and    r.keycnt >= 2
  15537.     and    o2.id = c2.id
  15538.     and    c2.colid = r.fkey2
  15539.     and     r.rkeyid = o1.id
  15540.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  15541.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15542.     and    o1.id = c1.id    
  15543.     and    c1.colid = r.rkey2
  15544.     union    all
  15545.     select
  15546.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15547.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15548.         PK_TABLE_NAME         = o1.name,
  15549.         PK_COLUMN_NAME         = c1.name,
  15550.         PK_COLUMN_GUID        = convert(binary(16),null),
  15551.         PK_COLUMN_PROPID    = convert(int,null),
  15552.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15553.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  15554.         FK_TABLE_NAME         = o2.name,
  15555.         FK_COLUMN_NAME         = c2.name,
  15556.         FK_COLUMN_GUID        = convert(binary(16),null),
  15557.         FK_COLUMN_PROPID    = convert(int,null),
  15558.         ORDINAL            = convert(int,3),
  15559.         UPDATE_RULE        = 'NO ACTION',
  15560.         DELETE_RULE         = 'NO ACTION'        
  15561.     from
  15562.         sysobjects o1, sysobjects o2,
  15563.         syscolumns c1, syscolumns c2,
  15564.         sysreferences r
  15565.     where    
  15566.         o2.name = @fk_table_name
  15567.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15568.     and    o2.id = r.fkeyid
  15569.     and    r.keycnt >= 3
  15570.     and    o2.id = c2.id
  15571.     and    c2.colid = r.fkey3
  15572.     and     r.rkeyid = o1.id
  15573.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  15574.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15575.     and    o1.id = c1.id    
  15576.     and    c1.colid = r.rkey3
  15577.     union    all
  15578.     select
  15579.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15580.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15581.         PK_TABLE_NAME         = o1.name,
  15582.         PK_COLUMN_NAME         = c1.name,
  15583.         PK_COLUMN_GUID        = convert(binary(16),null),
  15584.         PK_COLUMN_PROPID    = convert(int,null),
  15585.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15586.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  15587.         FK_TABLE_NAME         = o2.name,
  15588.         FK_COLUMN_NAME         = c2.name,
  15589.         FK_COLUMN_GUID        = convert(binary(16),null),
  15590.         FK_COLUMN_PROPID    = convert(int,null),
  15591.         ORDINAL            = convert(int,4),
  15592.         UPDATE_RULE        = 'NO ACTION',
  15593.         DELETE_RULE         = 'NO ACTION'        
  15594.     from
  15595.         sysobjects o1, sysobjects o2,
  15596.         syscolumns c1, syscolumns c2,
  15597.         sysreferences r
  15598.     where    
  15599.         o2.name = @fk_table_name
  15600.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15601.     and    o2.id = r.fkeyid
  15602.     and    r.keycnt >= 4
  15603.     and    o2.id = c2.id
  15604.     and    c2.colid = r.fkey4
  15605.     and     r.rkeyid = o1.id
  15606.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  15607.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15608.     and    o1.id = c1.id    
  15609.     and    c1.colid = r.rkey4
  15610.     union    all
  15611.     select
  15612.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15613.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15614.         PK_TABLE_NAME         = o1.name,
  15615.         PK_COLUMN_NAME         = c1.name,
  15616.         PK_COLUMN_GUID        = convert(binary(16),null),
  15617.         PK_COLUMN_PROPID    = convert(int,null),
  15618.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15619.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  15620.         FK_TABLE_NAME         = o2.name,
  15621.         FK_COLUMN_NAME         = c2.name,
  15622.         FK_COLUMN_GUID        = convert(binary(16),null),
  15623.         FK_COLUMN_PROPID    = convert(int,null),
  15624.         ORDINAL            = convert(int,5),
  15625.         UPDATE_RULE        = 'NO ACTION',
  15626.         DELETE_RULE         = 'NO ACTION'        
  15627.     from
  15628.         sysobjects o1, sysobjects o2,
  15629.         syscolumns c1, syscolumns c2,
  15630.         sysreferences r
  15631.     where    
  15632.         o2.name = @fk_table_name
  15633.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15634.     and    o2.id = r.fkeyid
  15635.     and    r.keycnt >= 5
  15636.     and    o2.id = c2.id
  15637.     and    c2.colid = r.fkey5
  15638.     and     r.rkeyid = o1.id
  15639.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  15640.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15641.     and    o1.id = c1.id    
  15642.     and    c1.colid = r.rkey5
  15643.     union    all
  15644.     select
  15645.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15646.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15647.         PK_TABLE_NAME         = o1.name,
  15648.         PK_COLUMN_NAME         = c1.name,
  15649.         PK_COLUMN_GUID        = convert(binary(16),null),
  15650.         PK_COLUMN_PROPID    = convert(int,null),
  15651.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15652.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  15653.         FK_TABLE_NAME         = o2.name,
  15654.         FK_COLUMN_NAME         = c2.name,
  15655.         FK_COLUMN_GUID        = convert(binary(16),null),
  15656.         FK_COLUMN_PROPID    = convert(int,null),
  15657.         ORDINAL            = convert(int,6),
  15658.         UPDATE_RULE        = 'NO ACTION',
  15659.         DELETE_RULE         = 'NO ACTION'        
  15660.     from
  15661.         sysobjects o1, sysobjects o2,
  15662.         syscolumns c1, syscolumns c2,
  15663.         sysreferences r
  15664.     where    
  15665.         o2.name = @fk_table_name
  15666.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15667.     and    o2.id = r.fkeyid
  15668.     and    r.keycnt >= 6
  15669.     and    o2.id = c2.id
  15670.     and    c2.colid = r.fkey6
  15671.     and     r.rkeyid = o1.id
  15672.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  15673.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15674.     and    o1.id = c1.id    
  15675.     and    c1.colid = r.rkey6
  15676.     union    all
  15677.     select
  15678.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15679.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15680.         PK_TABLE_NAME         = o1.name,
  15681.         PK_COLUMN_NAME         = c1.name,
  15682.         PK_COLUMN_GUID        = convert(binary(16),null),
  15683.         PK_COLUMN_PROPID    = convert(int,null),
  15684.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15685.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  15686.         FK_TABLE_NAME         = o2.name,
  15687.         FK_COLUMN_NAME         = c2.name,
  15688.         FK_COLUMN_GUID        = convert(binary(16),null),
  15689.         FK_COLUMN_PROPID    = convert(int,null),
  15690.         ORDINAL            = convert(int,7),
  15691.         UPDATE_RULE        = 'NO ACTION',
  15692.         DELETE_RULE         = 'NO ACTION'        
  15693.     from
  15694.         sysobjects o1, sysobjects o2,
  15695.         syscolumns c1, syscolumns c2,
  15696.         sysreferences r
  15697.     where    
  15698.         o2.name = @fk_table_name
  15699.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15700.     and    o2.id = r.fkeyid
  15701.     and    r.keycnt >= 7
  15702.     and    o2.id = c2.id
  15703.     and    c2.colid = r.fkey7
  15704.     and     r.rkeyid = o1.id
  15705.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  15706.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15707.     and    o1.id = c1.id    
  15708.     and    c1.colid = r.rkey7
  15709.     union    all
  15710.     select
  15711.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15712.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15713.         PK_TABLE_NAME         = o1.name,
  15714.         PK_COLUMN_NAME         = c1.name,
  15715.         PK_COLUMN_GUID        = convert(binary(16),null),
  15716.         PK_COLUMN_PROPID    = convert(int,null),
  15717.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15718.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  15719.         FK_TABLE_NAME         = o2.name,
  15720.         FK_COLUMN_NAME         = c2.name,
  15721.         FK_COLUMN_GUID        = convert(binary(16),null),
  15722.         FK_COLUMN_PROPID    = convert(int,null),
  15723.         ORDINAL            = convert(int,8),
  15724.         UPDATE_RULE        = 'NO ACTION',
  15725.         DELETE_RULE         = 'NO ACTION'        
  15726.     from
  15727.         sysobjects o1, sysobjects o2,
  15728.         syscolumns c1, syscolumns c2,
  15729.         sysreferences r
  15730.     where    
  15731.         o2.name = @fk_table_name
  15732.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15733.     and    o2.id = r.fkeyid
  15734.     and    r.keycnt >= 8
  15735.     and    o2.id = c2.id
  15736.     and    c2.colid = r.fkey8
  15737.     and     r.rkeyid = o1.id
  15738.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  15739.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15740.     and    o1.id = c1.id    
  15741.     and    c1.colid = r.rkey8
  15742.     union    all
  15743.     select
  15744.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15745.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15746.         PK_TABLE_NAME         = o1.name,
  15747.         PK_COLUMN_NAME         = c1.name,
  15748.         PK_COLUMN_GUID        = convert(binary(16),null),
  15749.         PK_COLUMN_PROPID    = convert(int,null),
  15750.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15751.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  15752.         FK_TABLE_NAME         = o2.name,
  15753.         FK_COLUMN_NAME         = c2.name,
  15754.         FK_COLUMN_GUID        = convert(binary(16),null),
  15755.         FK_COLUMN_PROPID    = convert(int,null),
  15756.         ORDINAL            = convert(int,9),
  15757.         UPDATE_RULE        = 'NO ACTION',
  15758.         DELETE_RULE         = 'NO ACTION'        
  15759.     from
  15760.         sysobjects o1, sysobjects o2,
  15761.         syscolumns c1, syscolumns c2,
  15762.         sysreferences r
  15763.     where    
  15764.         o2.name = @fk_table_name
  15765.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15766.     and    o2.id = r.fkeyid
  15767.     and    r.keycnt >= 9
  15768.     and    o2.id = c2.id
  15769.     and    c2.colid = r.fkey9
  15770.     and     r.rkeyid = o1.id
  15771.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  15772.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15773.     and    o1.id = c1.id    
  15774.     and    c1.colid = r.rkey9
  15775.     union    all
  15776.     select
  15777.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15778.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15779.         PK_TABLE_NAME         = o1.name,
  15780.         PK_COLUMN_NAME         = c1.name,
  15781.         PK_COLUMN_GUID        = convert(binary(16),null),
  15782.         PK_COLUMN_PROPID    = convert(int,null),
  15783.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15784.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  15785.         FK_TABLE_NAME         = o2.name,
  15786.         FK_COLUMN_NAME         = c2.name,
  15787.         FK_COLUMN_GUID        = convert(binary(16),null),
  15788.         FK_COLUMN_PROPID    = convert(int,null),
  15789.         ORDINAL            = convert(int,10),
  15790.         UPDATE_RULE        = 'NO ACTION',
  15791.         DELETE_RULE         = 'NO ACTION'        
  15792.     from
  15793.         sysobjects o1, sysobjects o2,
  15794.         syscolumns c1, syscolumns c2,
  15795.         sysreferences r
  15796.     where    
  15797.         o2.name = @fk_table_name
  15798.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15799.     and    o2.id = r.fkeyid
  15800.     and    r.keycnt >= 10
  15801.     and    o2.id = c2.id
  15802.     and    c2.colid = r.fkey10
  15803.     and     r.rkeyid = o1.id
  15804.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  15805.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15806.     and    o1.id = c1.id    
  15807.     and    c1.colid = r.rkey10
  15808.     union    all
  15809.     select
  15810.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15811.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15812.         PK_TABLE_NAME         = o1.name,
  15813.         PK_COLUMN_NAME         = c1.name,
  15814.         PK_COLUMN_GUID        = convert(binary(16),null),
  15815.         PK_COLUMN_PROPID    = convert(int,null),
  15816.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15817.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  15818.         FK_TABLE_NAME         = o2.name,
  15819.         FK_COLUMN_NAME         = c2.name,
  15820.         FK_COLUMN_GUID        = convert(binary(16),null),
  15821.         FK_COLUMN_PROPID    = convert(int,null),
  15822.         ORDINAL            = convert(int,11),
  15823.         UPDATE_RULE        = 'NO ACTION',
  15824.         DELETE_RULE         = 'NO ACTION'        
  15825.     from
  15826.         sysobjects o1, sysobjects o2,
  15827.         syscolumns c1, syscolumns c2,
  15828.         sysreferences r
  15829.     where    
  15830.         o2.name = @fk_table_name
  15831.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15832.     and    o2.id = r.fkeyid
  15833.     and    r.keycnt >= 11
  15834.     and    o2.id = c2.id
  15835.     and    c2.colid = r.fkey11
  15836.     and     r.rkeyid = o1.id
  15837.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  15838.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15839.     and    o1.id = c1.id    
  15840.     and    c1.colid = r.rkey11
  15841.     union    all
  15842.     select
  15843.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15844.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15845.         PK_TABLE_NAME         = o1.name,
  15846.         PK_COLUMN_NAME         = c1.name,
  15847.         PK_COLUMN_GUID        = convert(binary(16),null),
  15848.         PK_COLUMN_PROPID    = convert(int,null),
  15849.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15850.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  15851.         FK_TABLE_NAME         = o2.name,
  15852.         FK_COLUMN_NAME         = c2.name,
  15853.         FK_COLUMN_GUID        = convert(binary(16),null),
  15854.         FK_COLUMN_PROPID    = convert(int,null),
  15855.         ORDINAL            = convert(int,12),
  15856.         UPDATE_RULE        = 'NO ACTION',
  15857.         DELETE_RULE         = 'NO ACTION'        
  15858.     from
  15859.         sysobjects o1, sysobjects o2,
  15860.         syscolumns c1, syscolumns c2,
  15861.         sysreferences r
  15862.     where    
  15863.         o2.name = @fk_table_name
  15864.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15865.     and    o2.id = r.fkeyid
  15866.     and    r.keycnt >= 12
  15867.     and    o2.id = c2.id
  15868.     and    c2.colid = r.fkey12
  15869.     and     r.rkeyid = o1.id
  15870.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  15871.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15872.     and    o1.id = c1.id    
  15873.     and    c1.colid = r.rkey12
  15874.     union    all
  15875.     select
  15876.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15877.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15878.         PK_TABLE_NAME         = o1.name,
  15879.         PK_COLUMN_NAME         = c1.name,
  15880.         PK_COLUMN_GUID        = convert(binary(16),null),
  15881.         PK_COLUMN_PROPID    = convert(int,null),
  15882.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15883.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  15884.         FK_TABLE_NAME         = o2.name,
  15885.         FK_COLUMN_NAME         = c2.name,
  15886.         FK_COLUMN_GUID        = convert(binary(16),null),
  15887.         FK_COLUMN_PROPID    = convert(int,null),
  15888.         ORDINAL            = convert(int,13),
  15889.         UPDATE_RULE        = 'NO ACTION',
  15890.         DELETE_RULE         = 'NO ACTION'        
  15891.     from
  15892.         sysobjects o1, sysobjects o2,
  15893.         syscolumns c1, syscolumns c2,
  15894.         sysreferences r
  15895.     where    
  15896.         o2.name = @fk_table_name
  15897.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15898.     and    o2.id = r.fkeyid
  15899.     and    r.keycnt >= 13
  15900.     and    o2.id = c2.id
  15901.     and    c2.colid = r.fkey13
  15902.     and     r.rkeyid = o1.id
  15903.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  15904.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15905.     and    o1.id = c1.id    
  15906.     and    c1.colid = r.rkey13
  15907.     union    all
  15908.     select
  15909.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15910.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15911.         PK_TABLE_NAME         = o1.name,
  15912.         PK_COLUMN_NAME         = c1.name,
  15913.         PK_COLUMN_GUID        = convert(binary(16),null),
  15914.         PK_COLUMN_PROPID    = convert(int,null),
  15915.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15916.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  15917.         FK_TABLE_NAME         = o2.name,
  15918.         FK_COLUMN_NAME         = c2.name,
  15919.         FK_COLUMN_GUID        = convert(binary(16),null),
  15920.         FK_COLUMN_PROPID    = convert(int,null),
  15921.         ORDINAL            = convert(int,14),
  15922.         UPDATE_RULE        = 'NO ACTION',
  15923.         DELETE_RULE         = 'NO ACTION'        
  15924.     from
  15925.         sysobjects o1, sysobjects o2,
  15926.         syscolumns c1, syscolumns c2,
  15927.         sysreferences r
  15928.     where    
  15929.         o2.name = @fk_table_name
  15930.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15931.     and    o2.id = r.fkeyid
  15932.     and    r.keycnt >= 14
  15933.     and    o2.id = c2.id
  15934.     and    c2.colid = r.fkey14
  15935.     and     r.rkeyid = o1.id
  15936.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  15937.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15938.     and    o1.id = c1.id    
  15939.     and    c1.colid = r.rkey14
  15940.     union    all
  15941.     select
  15942.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15943.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15944.         PK_TABLE_NAME         = o1.name,
  15945.         PK_COLUMN_NAME         = c1.name,
  15946.         PK_COLUMN_GUID        = convert(binary(16),null),
  15947.         PK_COLUMN_PROPID    = convert(int,null),
  15948.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15949.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  15950.         FK_TABLE_NAME         = o2.name,
  15951.         FK_COLUMN_NAME         = c2.name,
  15952.         FK_COLUMN_GUID        = convert(binary(16),null),
  15953.         FK_COLUMN_PROPID    = convert(int,null),
  15954.         ORDINAL            = convert(int,15),
  15955.         UPDATE_RULE        = 'NO ACTION',
  15956.         DELETE_RULE         = 'NO ACTION'        
  15957.     from
  15958.         sysobjects o1, sysobjects o2,
  15959.         syscolumns c1, syscolumns c2,
  15960.         sysreferences r
  15961.     where    
  15962.         o2.name = @fk_table_name
  15963.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15964.     and    o2.id = r.fkeyid
  15965.     and    r.keycnt >= 15
  15966.     and    o2.id = c2.id
  15967.     and    c2.colid = r.fkey15
  15968.     and     r.rkeyid = o1.id
  15969.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  15970.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15971.     and    o1.id = c1.id    
  15972.     and    c1.colid = r.rkey15
  15973.     union    all
  15974.     select
  15975.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15976.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15977.         PK_TABLE_NAME         = o1.name,
  15978.         PK_COLUMN_NAME         = c1.name,
  15979.         PK_COLUMN_GUID        = convert(binary(16),null),
  15980.         PK_COLUMN_PROPID    = convert(int,null),
  15981.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15982.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  15983.         FK_TABLE_NAME         = o2.name,
  15984.         FK_COLUMN_NAME         = c2.name,
  15985.         FK_COLUMN_GUID        = convert(binary(16),null),
  15986.         FK_COLUMN_PROPID    = convert(int,null),
  15987.         ORDINAL            = convert(int,16),
  15988.         UPDATE_RULE        = 'NO ACTION',
  15989.         DELETE_RULE         = 'NO ACTION'        
  15990.     from
  15991.         sysobjects o1, sysobjects o2,
  15992.         syscolumns c1, syscolumns c2,
  15993.         sysreferences r
  15994.     where    
  15995.         o2.name = @fk_table_name
  15996.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15997.     and    o2.id = r.fkeyid
  15998.     and    r.keycnt >= 16
  15999.     and    o2.id = c2.id
  16000.     and    c2.colid = r.fkey16
  16001.     and     r.rkeyid = o1.id
  16002.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  16003.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16004.     and    o1.id = c1.id    
  16005.     and    c1.colid = r.rkey16
  16006.     END
  16007. ELSE
  16008.     BEGIN
  16009.     select
  16010.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16011.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16012.         PK_TABLE_NAME         = o1.name,
  16013.         PK_COLUMN_NAME         = c1.name,
  16014.         PK_COLUMN_GUID        = convert(binary(16),null),
  16015.         PK_COLUMN_PROPID    = convert(int,null),
  16016.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16017.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  16018.         FK_TABLE_NAME         = o2.name,
  16019.         FK_COLUMN_NAME         = c2.name,
  16020.         FK_COLUMN_GUID        = convert(binary(16),null),
  16021.         FK_COLUMN_PROPID    = convert(int,null),
  16022.         ORDINAL            = convert(int,1),
  16023.         UPDATE_RULE        = 'NO ACTION',
  16024.         DELETE_RULE         = 'NO ACTION'        
  16025.     from
  16026.         sysobjects o1, sysobjects o2,
  16027.         syscolumns c1, syscolumns c2,
  16028.         sysreferences r
  16029.     where    
  16030.         (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16031.     and    o1.id = r.rkeyid
  16032.     and    o1.id = c1.id
  16033.     and    c1.colid = r.rkey1
  16034.     and     r.fkeyid = o2.id
  16035.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16036.     and    o2.id = c2.id    
  16037.     and    c2.colid = r.fkey1
  16038.     union all
  16039.     select
  16040.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16041.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16042.         PK_TABLE_NAME         = o1.name,
  16043.         PK_COLUMN_NAME         = c1.name,
  16044.         PK_COLUMN_GUID        = convert(binary(16),null),
  16045.         PK_COLUMN_PROPID    = convert(int,null),
  16046.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16047.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  16048.         FK_TABLE_NAME         = o2.name,
  16049.         FK_COLUMN_NAME         = c2.name,
  16050.         FK_COLUMN_GUID        = convert(binary(16),null),
  16051.         FK_COLUMN_PROPID    = convert(int,null),
  16052.         ORDINAL            = convert(int,2),
  16053.         UPDATE_RULE        = 'NO ACTION',
  16054.         DELETE_RULE         = 'NO ACTION'        
  16055.     from
  16056.         sysobjects o1, sysobjects o2,
  16057.         syscolumns c1, syscolumns c2,
  16058.         sysreferences r
  16059.     where    
  16060.         (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16061.     and    o1.id = r.rkeyid
  16062.     and    r.keycnt >= 2
  16063.     and    o1.id = c1.id
  16064.     and    c1.colid = r.rkey2
  16065.     and     r.fkeyid = o2.id
  16066.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16067.     and    o2.id = c2.id    
  16068.     and    c2.colid = r.fkey2
  16069.     union all
  16070.     select
  16071.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16072.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16073.         PK_TABLE_NAME         = o1.name,
  16074.         PK_COLUMN_NAME         = c1.name,
  16075.         PK_COLUMN_GUID        = convert(binary(16),null),
  16076.         PK_COLUMN_PROPID    = convert(int,null),
  16077.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16078.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  16079.         FK_TABLE_NAME         = o2.name,
  16080.         FK_COLUMN_NAME         = c2.name,
  16081.         FK_COLUMN_GUID        = convert(binary(16),null),
  16082.         FK_COLUMN_PROPID    = convert(int,null),
  16083.         ORDINAL            = convert(int,3),
  16084.         UPDATE_RULE        = 'NO ACTION',
  16085.         DELETE_RULE         = 'NO ACTION'        
  16086.     from
  16087.         sysobjects o1, sysobjects o2,
  16088.         syscolumns c1, syscolumns c2,
  16089.         sysreferences r
  16090.     where    
  16091.         (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16092.     and    o1.id = r.rkeyid
  16093.     and    r.keycnt >= 3
  16094.     and    o1.id = c1.id
  16095.     and    c1.colid = r.rkey3
  16096.     and     r.fkeyid = o2.id
  16097.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16098.     and    o2.id = c2.id    
  16099.     and    c2.colid = r.fkey3
  16100.     union all
  16101.     select
  16102.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16103.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16104.         PK_TABLE_NAME         = o1.name,
  16105.         PK_COLUMN_NAME         = c1.name,
  16106.         PK_COLUMN_GUID        = convert(binary(16),null),
  16107.         PK_COLUMN_PROPID    = convert(int,null),
  16108.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16109.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  16110.         FK_TABLE_NAME         = o2.name,
  16111.         FK_COLUMN_NAME         = c2.name,
  16112.         FK_COLUMN_GUID        = convert(binary(16),null),
  16113.         FK_COLUMN_PROPID    = convert(int,null),
  16114.         ORDINAL            = convert(int,4),
  16115.         UPDATE_RULE        = 'NO ACTION',
  16116.         DELETE_RULE         = 'NO ACTION'        
  16117.     from
  16118.         sysobjects o1, sysobjects o2,
  16119.         syscolumns c1, syscolumns c2,
  16120.         sysreferences r
  16121.     where    
  16122.         (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16123.     and    o1.id = r.rkeyid
  16124.     and    r.keycnt >= 4
  16125.     and    o1.id = c1.id
  16126.     and    c1.colid = r.rkey4
  16127.     and     r.fkeyid = o2.id
  16128.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16129.     and    o2.id = c2.id    
  16130.     and    c2.colid = r.fkey4
  16131.     union all
  16132.     select
  16133.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16134.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16135.         PK_TABLE_NAME         = o1.name,
  16136.         PK_COLUMN_NAME         = c1.name,
  16137.         PK_COLUMN_GUID        = convert(binary(16),null),
  16138.         PK_COLUMN_PROPID    = convert(int,null),
  16139.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16140.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  16141.         FK_TABLE_NAME         = o2.name,
  16142.         FK_COLUMN_NAME         = c2.name,
  16143.         FK_COLUMN_GUID        = convert(binary(16),null),
  16144.         FK_COLUMN_PROPID    = convert(int,null),
  16145.         ORDINAL            = convert(int,5),
  16146.         UPDATE_RULE        = 'NO ACTION',
  16147.         DELETE_RULE         = 'NO ACTION'        
  16148.     from
  16149.         sysobjects o1, sysobjects o2,
  16150.         syscolumns c1, syscolumns c2,
  16151.         sysreferences r
  16152.     where    
  16153.         (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16154.     and    o1.id = r.rkeyid
  16155.     and    r.keycnt >= 5
  16156.     and    o1.id = c1.id
  16157.     and    c1.colid = r.rkey5
  16158.     and     r.fkeyid = o2.id
  16159.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16160.     and    o2.id = c2.id    
  16161.     and    c2.colid = r.fkey5
  16162.     union all
  16163.     select
  16164.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16165.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16166.         PK_TABLE_NAME         = o1.name,
  16167.         PK_COLUMN_NAME         = c1.name,
  16168.         PK_COLUMN_GUID        = convert(binary(16),null),
  16169.         PK_COLUMN_PROPID    = convert(int,null),
  16170.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16171.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  16172.         FK_TABLE_NAME         = o2.name,
  16173.         FK_COLUMN_NAME         = c2.name,
  16174.         FK_COLUMN_GUID        = convert(binary(16),null),
  16175.         FK_COLUMN_PROPID    = convert(int,null),
  16176.         ORDINAL            = convert(int,6),
  16177.         UPDATE_RULE        = 'NO ACTION',
  16178.         DELETE_RULE         = 'NO ACTION'        
  16179.     from
  16180.         sysobjects o1, sysobjects o2,
  16181.         syscolumns c1, syscolumns c2,
  16182.         sysreferences r
  16183.     where    
  16184.         (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16185.     and    o1.id = r.rkeyid
  16186.     and    r.keycnt >= 6
  16187.     and    o1.id = c1.id
  16188.     and    c1.colid = r.rkey6
  16189.     and     r.fkeyid = o2.id
  16190.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16191.     and    o2.id = c2.id    
  16192.     and    c2.colid = r.fkey6
  16193.     union all
  16194.     select
  16195.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16196.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16197.         PK_TABLE_NAME         = o1.name,
  16198.         PK_COLUMN_NAME         = c1.name,
  16199.         PK_COLUMN_GUID        = convert(binary(16),null),
  16200.         PK_COLUMN_PROPID    = convert(int,null),
  16201.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16202.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  16203.         FK_TABLE_NAME         = o2.name,
  16204.         FK_COLUMN_NAME         = c2.name,
  16205.         FK_COLUMN_GUID        = convert(binary(16),null),
  16206.         FK_COLUMN_PROPID    = convert(int,null),
  16207.         ORDINAL            = convert(int,7),
  16208.         UPDATE_RULE        = 'NO ACTION',
  16209.         DELETE_RULE         = 'NO ACTION'        
  16210.     from
  16211.         sysobjects o1, sysobjects o2,
  16212.         syscolumns c1, syscolumns c2,
  16213.         sysreferences r
  16214.     where    
  16215.         (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16216.     and    o1.id = r.rkeyid
  16217.     and    r.keycnt >= 7
  16218.     and    o1.id = c1.id
  16219.     and    c1.colid = r.rkey7
  16220.     and     r.fkeyid = o2.id
  16221.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16222.     and    o2.id = c2.id    
  16223.     and    c2.colid = r.fkey7
  16224.     union all
  16225.     select
  16226.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16227.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16228.         PK_TABLE_NAME         = o1.name,
  16229.         PK_COLUMN_NAME         = c1.name,
  16230.         PK_COLUMN_GUID        = convert(binary(16),null),
  16231.         PK_COLUMN_PROPID    = convert(int,null),
  16232.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16233.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  16234.         FK_TABLE_NAME         = o2.name,
  16235.         FK_COLUMN_NAME         = c2.name,
  16236.         FK_COLUMN_GUID        = convert(binary(16),null),
  16237.         FK_COLUMN_PROPID    = convert(int,null),
  16238.         ORDINAL            = convert(int,8),
  16239.         UPDATE_RULE        = 'NO ACTION',
  16240.         DELETE_RULE         = 'NO ACTION'        
  16241.     from
  16242.         sysobjects o1, sysobjects o2,
  16243.         syscolumns c1, syscolumns c2,
  16244.         sysreferences r
  16245.     where    
  16246.         (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16247.     and    o1.id = r.rkeyid
  16248.     and    r.keycnt >= 8
  16249.     and    o1.id = c1.id
  16250.     and    c1.colid = r.rkey8
  16251.     and     r.fkeyid = o2.id
  16252.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16253.     and    o2.id = c2.id    
  16254.     and    c2.colid = r.fkey8
  16255.     union all
  16256.     select
  16257.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16258.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16259.         PK_TABLE_NAME         = o1.name,
  16260.         PK_COLUMN_NAME         = c1.name,
  16261.         PK_COLUMN_GUID        = convert(binary(16),null),
  16262.         PK_COLUMN_PROPID    = convert(int,null),
  16263.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16264.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  16265.         FK_TABLE_NAME         = o2.name,
  16266.         FK_COLUMN_NAME         = c2.name,
  16267.         FK_COLUMN_GUID        = convert(binary(16),null),
  16268.         FK_COLUMN_PROPID    = convert(int,null),
  16269.         ORDINAL            = convert(int,9),
  16270.         UPDATE_RULE        = 'NO ACTION',
  16271.         DELETE_RULE         = 'NO ACTION'        
  16272.     from
  16273.         sysobjects o1, sysobjects o2,
  16274.         syscolumns c1, syscolumns c2,
  16275.         sysreferences r
  16276.     where    
  16277.         (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16278.     and    o1.id = r.rkeyid
  16279.     and    r.keycnt >= 9
  16280.     and    o1.id = c1.id
  16281.     and    c1.colid = r.rkey9
  16282.     and     r.fkeyid = o2.id
  16283.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16284.     and    o2.id = c2.id    
  16285.     and    c2.colid = r.fkey9
  16286.     union all
  16287.     select
  16288.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16289.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16290.         PK_TABLE_NAME         = o1.name,
  16291.         PK_COLUMN_NAME         = c1.name,
  16292.         PK_COLUMN_GUID        = convert(binary(16),null),
  16293.         PK_COLUMN_PROPID    = convert(int,null),
  16294.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16295.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  16296.         FK_TABLE_NAME         = o2.name,
  16297.         FK_COLUMN_NAME         = c2.name,
  16298.         FK_COLUMN_GUID        = convert(binary(16),null),
  16299.         FK_COLUMN_PROPID    = convert(int,null),
  16300.         ORDINAL            = convert(int,10),
  16301.         UPDATE_RULE        = 'NO ACTION',
  16302.         DELETE_RULE         = 'NO ACTION'        
  16303.     from
  16304.         sysobjects o1, sysobjects o2,
  16305.         syscolumns c1, syscolumns c2,
  16306.         sysreferences r
  16307.     where    
  16308.         (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16309.     and    o1.id = r.rkeyid
  16310.     and    r.keycnt >= 10
  16311.     and    o1.id = c1.id
  16312.     and    c1.colid = r.rkey10
  16313.     and     r.fkeyid = o2.id
  16314.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16315.     and    o2.id = c2.id    
  16316.     and    c2.colid = r.fkey10
  16317.     union all
  16318.     select
  16319.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16320.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16321.         PK_TABLE_NAME         = o1.name,
  16322.         PK_COLUMN_NAME         = c1.name,
  16323.         PK_COLUMN_GUID        = convert(binary(16),null),
  16324.         PK_COLUMN_PROPID    = convert(int,null),
  16325.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16326.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  16327.         FK_TABLE_NAME         = o2.name,
  16328.         FK_COLUMN_NAME         = c2.name,
  16329.         FK_COLUMN_GUID        = convert(binary(16),null),
  16330.         FK_COLUMN_PROPID    = convert(int,null),
  16331.         ORDINAL            = convert(int,11),
  16332.         UPDATE_RULE        = 'NO ACTION',
  16333.         DELETE_RULE         = 'NO ACTION'        
  16334.     from
  16335.         sysobjects o1, sysobjects o2,
  16336.         syscolumns c1, syscolumns c2,
  16337.         sysreferences r
  16338.     where    
  16339.         (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16340.     and    o1.id = r.rkeyid
  16341.     and    r.keycnt >= 11
  16342.     and    o1.id = c1.id
  16343.     and    c1.colid = r.rkey11
  16344.     and     r.fkeyid = o2.id
  16345.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16346.     and    o2.id = c2.id    
  16347.     and    c2.colid = r.fkey11
  16348.     union all
  16349.     select
  16350.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16351.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16352.         PK_TABLE_NAME         = o1.name,
  16353.         PK_COLUMN_NAME         = c1.name,
  16354.         PK_COLUMN_GUID        = convert(binary(16),null),
  16355.         PK_COLUMN_PROPID    = convert(int,null),
  16356.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16357.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  16358.         FK_TABLE_NAME         = o2.name,
  16359.         FK_COLUMN_NAME         = c2.name,
  16360.         FK_COLUMN_GUID        = convert(binary(16),null),
  16361.         FK_COLUMN_PROPID    = convert(int,null),
  16362.         ORDINAL            = convert(int,12),
  16363.         UPDATE_RULE        = 'NO ACTION',
  16364.         DELETE_RULE         = 'NO ACTION'        
  16365.     from
  16366.         sysobjects o1, sysobjects o2,
  16367.         syscolumns c1, syscolumns c2,
  16368.         sysreferences r
  16369.     where    
  16370.         (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16371.     and    o1.id = r.rkeyid
  16372.     and    r.keycnt >= 12
  16373.     and    o1.id = c1.id
  16374.     and    c1.colid = r.rkey12
  16375.     and     r.fkeyid = o2.id
  16376.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16377.     and    o2.id = c2.id    
  16378.     and    c2.colid = r.fkey12
  16379.     union all
  16380.     select
  16381.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16382.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16383.         PK_TABLE_NAME         = o1.name,
  16384.         PK_COLUMN_NAME         = c1.name,
  16385.         PK_COLUMN_GUID        = convert(binary(16),null),
  16386.         PK_COLUMN_PROPID    = convert(int,null),
  16387.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16388.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  16389.         FK_TABLE_NAME         = o2.name,
  16390.         FK_COLUMN_NAME         = c2.name,
  16391.         FK_COLUMN_GUID        = convert(binary(16),null),
  16392.         FK_COLUMN_PROPID    = convert(int,null),
  16393.         ORDINAL            = convert(int,13),
  16394.         UPDATE_RULE        = 'NO ACTION',
  16395.         DELETE_RULE         = 'NO ACTION'        
  16396.     from
  16397.         sysobjects o1, sysobjects o2,
  16398.         syscolumns c1, syscolumns c2,
  16399.         sysreferences r
  16400.     where    
  16401.         (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16402.     and    o1.id = r.rkeyid
  16403.     and    r.keycnt >= 13
  16404.     and    o1.id = c1.id
  16405.     and    c1.colid = r.rkey13
  16406.     and     r.fkeyid = o2.id
  16407.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16408.     and    o2.id = c2.id    
  16409.     and    c2.colid = r.fkey13
  16410.     union all
  16411.     select
  16412.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16413.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16414.         PK_TABLE_NAME         = o1.name,
  16415.         PK_COLUMN_NAME         = c1.name,
  16416.         PK_COLUMN_GUID        = convert(binary(16),null),
  16417.         PK_COLUMN_PROPID    = convert(int,null),
  16418.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16419.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  16420.         FK_TABLE_NAME         = o2.name,
  16421.         FK_COLUMN_NAME         = c2.name,
  16422.         FK_COLUMN_GUID        = convert(binary(16),null),
  16423.         FK_COLUMN_PROPID    = convert(int,null),
  16424.         ORDINAL            = convert(int,14),
  16425.         UPDATE_RULE        = 'NO ACTION',
  16426.         DELETE_RULE         = 'NO ACTION'        
  16427.     from
  16428.         sysobjects o1, sysobjects o2,
  16429.         syscolumns c1, syscolumns c2,
  16430.         sysreferences r
  16431.     where    
  16432.         (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16433.     and    o1.id = r.rkeyid
  16434.     and    r.keycnt >= 14
  16435.     and    o1.id = c1.id
  16436.     and    c1.colid = r.rkey14
  16437.     and     r.fkeyid = o2.id
  16438.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16439.     and    o2.id = c2.id    
  16440.     and    c2.colid = r.fkey14
  16441.     union all
  16442.     select
  16443.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16444.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16445.         PK_TABLE_NAME         = o1.name,
  16446.         PK_COLUMN_NAME         = c1.name,
  16447.         PK_COLUMN_GUID        = convert(binary(16),null),
  16448.         PK_COLUMN_PROPID    = convert(int,null),
  16449.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16450.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  16451.         FK_TABLE_NAME         = o2.name,
  16452.         FK_COLUMN_NAME         = c2.name,
  16453.         FK_COLUMN_GUID        = convert(binary(16),null),
  16454.         FK_COLUMN_PROPID    = convert(int,null),
  16455.         ORDINAL            = convert(int,15),
  16456.         UPDATE_RULE        = 'NO ACTION',
  16457.         DELETE_RULE         = 'NO ACTION'        
  16458.     from
  16459.         sysobjects o1, sysobjects o2,
  16460.         syscolumns c1, syscolumns c2,
  16461.         sysreferences r
  16462.     where    
  16463.         (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16464.     and    o1.id = r.rkeyid
  16465.     and    r.keycnt >= 15
  16466.     and    o1.id = c1.id
  16467.     and    c1.colid = r.rkey15
  16468.     and     r.fkeyid = o2.id
  16469.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16470.     and    o2.id = c2.id    
  16471.     and    c2.colid = r.fkey15
  16472.     union all
  16473.     select
  16474.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16475.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16476.         PK_TABLE_NAME         = o1.name,
  16477.         PK_COLUMN_NAME         = c1.name,
  16478.         PK_COLUMN_GUID        = convert(binary(16),null),
  16479.         PK_COLUMN_PROPID    = convert(int,null),
  16480.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16481.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  16482.         FK_TABLE_NAME         = o2.name,
  16483.         FK_COLUMN_NAME         = c2.name,
  16484.         FK_COLUMN_GUID        = convert(binary(16),null),
  16485.         FK_COLUMN_PROPID    = convert(int,null),
  16486.         ORDINAL            = convert(int,16),
  16487.         UPDATE_RULE        = 'NO ACTION',
  16488.         DELETE_RULE         = 'NO ACTION'        
  16489.     from
  16490.         sysobjects o1, sysobjects o2,
  16491.         syscolumns c1, syscolumns c2,
  16492.         sysreferences r
  16493.     where    
  16494.         (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16495.     and    o1.id = r.rkeyid
  16496.     and    o1.id = c1.id
  16497.     and    r.keycnt >= 16
  16498.     and    c1.colid = r.rkey16
  16499.     and     r.fkeyid = o2.id
  16500.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16501.     and    o2.id = c2.id    
  16502.     and    c2.colid = r.fkey16
  16503.     END
  16504. go
  16505.  
  16506.  
  16507. create procedure sp_foreign_keys_rowset;5
  16508.     (
  16509.        @handle        int output,
  16510.        @scrollopt    int output,
  16511.     @ccopt        int output,
  16512.     @rows        int output,
  16513.     @cmd        varchar(255)
  16514.     )
  16515. as
  16516.     declare @ret int
  16517.     declare @database_name varchar(30)
  16518.  
  16519.     create table #spfkeysrowset
  16520.         (
  16521.         PK_TABLE_CATALOG    varchar(30) not null,
  16522.         PK_TABLE_SCHEMA        varchar(30) not null,
  16523.         PK_TABLE_NAME         varchar(30) not null,
  16524.         PK_COLUMN_NAME         varchar(30) not null,
  16525.         PK_COLUMN_GUID        binary(16) null,
  16526.         PK_COLUMN_PROPID    int null,
  16527.         FK_TABLE_CATALOG    varchar(30) not null,
  16528.         FK_TABLE_SCHEMA         varchar(30) not null,
  16529.         FK_TABLE_NAME         varchar(30) not null,
  16530.         FK_COLUMN_NAME         varchar(30) not null,
  16531.         FK_COLUMN_GUID        binary(16) null,
  16532.         FK_COLUMN_PROPID    int null,
  16533.         ORDINAL            int not null,
  16534.         UPDATE_RULE        varchar(30) not null,
  16535.         DELETE_RULE         varchar(30) not null        
  16536.         )
  16537.  
  16538.     declare sysdatabases_cursor insensitive cursor for 
  16539.         select name from master.dbo.sysdatabases
  16540.   
  16541.     set nocount on
  16542.     open sysdatabases_cursor
  16543.       fetch next from sysdatabases_cursor into @database_name 
  16544.     while @@fetch_status = 0
  16545.         begin
  16546.         exec('use '+@database_name+' insert into #spfkeysrowset '+@cmd)
  16547.         fetch next from sysdatabases_cursor into @database_name 
  16548.         end
  16549.       close sysdatabases_cursor
  16550.     deallocate sysdatabases_cursor
  16551.  
  16552.     if @scrollopt is null
  16553.         begin
  16554.         select * from #spfkeysrowset order by 7,8,9,1,2,3,13
  16555.         end
  16556.     else
  16557.         begin
  16558.         exec @ret = sp_cursoropen @handle output,
  16559.             'select * from #spfkeysrowset order by 7,8,9,1,2,3,13',
  16560.                 @scrollopt output, @ccopt output, @rows output
  16561.         end
  16562.     drop table #spfkeysrowset
  16563.     return isnull(@ret,0)
  16564. go
  16565.  
  16566.  
  16567. grant execute on sp_foreign_keys_rowset to public
  16568. go
  16569.  
  16570. dump tran master with no_log
  16571. go
  16572.  
  16573. print 'creating sp_indexes_rowset'
  16574. go
  16575.  
  16576. /*    6.0 and 6.5 version */
  16577. create procedure sp_indexes_rowset
  16578.     (
  16579.     @table_name    varchar(255), 
  16580.     @index_name    varchar(255) = null,
  16581.     @table_schema    varchar(255) = null     
  16582.     )
  16583. as
  16584.     select    TABLE_CATALOG        = db_name(),
  16585.         TABLE_SCHEMA        = user_name(o.uid),
  16586.         TABLE_NAME        = o.name,
  16587.         INDEX_CATALOG        = db_name(),        
  16588.         INDEX_SCHEMA        = user_name(o.uid),
  16589.         INDEX_NAME        = x.name,
  16590.         PRIMARY_KEY        = convert(bit,(x.status & 0x800)/0x800),
  16591.         "UNIQUE"        = convert(bit,(x.status & 2)/2),
  16592.         "CLUSTERED"        = convert(bit,(x.status & 16)/16),
  16593.         "TYPE"            = convert(smallint, 1 /*DBPROPVAL_IT_BTREE*/),
  16594.         FILL_FACTOR        = convert(int, x.OrigFillFactor),
  16595.         INITIAL_SIZE        = convert(int,null),
  16596.         NULLS            = convert(int,null),
  16597.         SORT_BOOKMARKS        = convert(bit,0),
  16598.         AUTO_UPDATE        = convert(bit,1),
  16599.         NULL_COLLATION        = convert(int,4 /*DBPROPVAL_NC_LOW*/),
  16600.         ORDINAL_POSITION    = convert(int,c.colid),
  16601.         COLUMN_NAME        = index_col(user_name(o.uid)+'.'+o.name, x.indid, c.colid),
  16602.         COLUMN_GUID        = convert(binary(16),null),
  16603.         COLUMN_PROPID        = convert(int,null),
  16604.         COLLATION        = convert(smallint, 1 /*DB_COLLATION_ASC*/),
  16605.         CARDINALITY        = case when (x.status & 2) = 2 then x.rows else null end,
  16606.         PAGES            = convert(int, x.dpages),
  16607.         FILTER_CONDITION    = convert(varchar(1),null) 
  16608.         
  16609.     from    sysobjects o, sysindexes x, syscolumns c, sysusers u
  16610.     where    o.type in ('U')
  16611.     and     o.name = @table_name
  16612.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  16613.     and    (@index_name is null or @index_name = x.name)
  16614.     and     x.id = o.id
  16615.     and     x.id = c.id
  16616.     and      c.colid < x.keycnt+(x.status&16)/16
  16617.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  16618.     and     (
  16619.         suser_id() = 1     /* User is the System Administrator */
  16620.         or o.uid = user_id()     /* User created the object */
  16621.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16622.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16623.             from sysprotects p
  16624.             where p.id = o.id
  16625.             /* get rows for public,current user,user's group */
  16626.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16627.             /* check for SELECT,EXECUTE privilege */
  16628.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16629.             ) = 1    /* final magic...compare Grants      */
  16630.            )
  16631.     order by 8 desc, 4, 5, 6, 16
  16632. go
  16633. create procedure sp_indexes_rowset;2
  16634.     (
  16635.     @index_name    varchar(255) = null,
  16636.     @table_schema    varchar(255) = null     
  16637.     )
  16638. as
  16639.     select    TABLE_CATALOG        = db_name(),
  16640.         TABLE_SCHEMA        = user_name(o.uid),
  16641.         TABLE_NAME        = o.name,
  16642.         INDEX_CATALOG        = db_name(),        
  16643.         INDEX_SCHEMA        = user_name(o.uid),
  16644.         INDEX_NAME        = x.name,
  16645.         PRIMARY_KEY        = convert(bit,(x.status & 0x800)/0x800),
  16646.         "UNIQUE"        = convert(bit,(x.status & 2)/2),
  16647.         "CLUSTERED"        = convert(bit,(x.status & 16)/16),
  16648.         "TYPE"            = convert(smallint, 1 /*DBPROPVAL_IT_BTREE*/),
  16649.         FILL_FACTOR        = convert(int, x.OrigFillFactor),
  16650.         INITIAL_SIZE        = convert(int,null),
  16651.         NULLS            = convert(int,null),
  16652.         SORT_BOOKMARKS        = convert(bit,0),
  16653.         AUTO_UPDATE        = convert(bit,1),
  16654.         NULL_COLLATION        = convert(int,4 /*DBPROPVAL_NC_LOW*/),
  16655.         ORDINAL_POSITION    = convert(int,c.colid),
  16656.         COLUMN_NAME        = index_col(user_name(o.uid)+'.'+o.name, x.indid, c.colid),
  16657.         COLUMN_GUID        = convert(binary(16),null),
  16658.         COLUMN_PROPID        = convert(int,null),
  16659.         COLLATION        = convert(smallint, 1 /*DB_COLLATION_ASC*/),
  16660.         CARDINALITY        = case when (x.status & 2) = 2 then x.rows else null end,
  16661.         PAGES            = convert(int, x.dpages),
  16662.         FILTER_CONDITION    = convert(varchar(1),null) 
  16663.         
  16664.     from    sysobjects o, sysindexes x, syscolumns c, sysusers u
  16665.     where    o.type in ('U')
  16666.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  16667.     and    (@index_name is null or @index_name = x.name)
  16668.     and     x.id = o.id
  16669.     and     x.id = c.id
  16670.     and      c.colid < x.keycnt+(x.status&16)/16
  16671.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  16672.     and     (
  16673.         suser_id() = 1     /* User is the System Administrator */
  16674.         or o.uid = user_id()     /* User created the object */
  16675.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16676.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16677.             from sysprotects p
  16678.             where p.id = o.id
  16679.             /* get rows for public,current user,user's group */
  16680.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16681.             /* check for SELECT,EXECUTE privilege */
  16682.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16683.             ) = 1    /* final magic...compare Grants      */
  16684.            )
  16685.     order by 8 desc, 4, 5, 6, 16
  16686. go
  16687. create procedure sp_indexes_rowset;3
  16688.     (
  16689.     @table_name    varchar(255) = null, 
  16690.     @index_name    varchar(255) = null,
  16691.     @table_schema    varchar(255) = null     
  16692.     )
  16693. as
  16694. IF @table_name is not null
  16695.     BEGIN
  16696.     select    TABLE_CATALOG        = db_name(),
  16697.         TABLE_SCHEMA        = user_name(o.uid),
  16698.         TABLE_NAME        = o.name,
  16699.         INDEX_CATALOG        = db_name(),        
  16700.         INDEX_SCHEMA        = user_name(o.uid),
  16701.         INDEX_NAME        = x.name,
  16702.         PRIMARY_KEY        = convert(bit,(x.status & 0x800)/0x800),
  16703.         "UNIQUE"        = convert(bit,(x.status & 2)/2),
  16704.         "CLUSTERED"        = convert(bit,(x.status & 16)/16),
  16705.         "TYPE"            = convert(smallint, 1 /*DBPROPVAL_IT_BTREE*/),
  16706.         FILL_FACTOR        = convert(int, x.OrigFillFactor),
  16707.         INITIAL_SIZE        = convert(int,null),
  16708.         NULLS            = convert(int,null),
  16709.         SORT_BOOKMARKS        = convert(bit,0),
  16710.         AUTO_UPDATE        = convert(bit,1),
  16711.         NULL_COLLATION        = convert(int,4 /*DBPROPVAL_NC_LOW*/),
  16712.         ORDINAL_POSITION    = convert(int,c.colid),
  16713.         COLUMN_NAME        = index_col(user_name(o.uid)+'.'+o.name, x.indid, c.colid),
  16714.         COLUMN_GUID        = convert(binary(16),null),
  16715.         COLUMN_PROPID        = convert(int,null),
  16716.         COLLATION        = convert(smallint, 1 /*DB_COLLATION_ASC*/),
  16717.         CARDINALITY        = case when (x.status & 2) = 2 then x.rows else null end,
  16718.         PAGES            = convert(int, x.dpages),
  16719.         FILTER_CONDITION    = convert(varchar(1),null) 
  16720.         
  16721.     from    sysobjects o, sysindexes x, syscolumns c, sysusers u
  16722.     where    o.type in ('U')
  16723.     and     o.name = @table_name
  16724.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  16725.     and    (@index_name is null or @index_name = x.name)
  16726.     and     x.id = o.id
  16727.     and     x.id = c.id
  16728.     and      c.colid < x.keycnt+(x.status&16)/16
  16729.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  16730.     and     (
  16731.         suser_id() = 1     /* User is the System Administrator */
  16732.         or o.uid = user_id()     /* User created the object */
  16733.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16734.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16735.             from sysprotects p
  16736.             where p.id = o.id
  16737.             /* get rows for public,current user,user's group */
  16738.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16739.             /* check for SELECT,EXECUTE privilege */
  16740.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16741.             ) = 1    /* final magic...compare Grants      */
  16742.            )
  16743.     END
  16744. ELSE
  16745.     BEGIN
  16746.     select    TABLE_CATALOG        = db_name(),
  16747.         TABLE_SCHEMA        = user_name(o.uid),
  16748.         TABLE_NAME        = o.name,
  16749.         INDEX_CATALOG        = db_name(),        
  16750.         INDEX_SCHEMA        = user_name(o.uid),
  16751.         INDEX_NAME        = x.name,
  16752.         PRIMARY_KEY        = convert(bit,(x.status & 0x800)/0x800),
  16753.         "UNIQUE"        = convert(bit,(x.status & 2)/2),
  16754.         "CLUSTERED"        = convert(bit,(x.status & 16)/16),
  16755.         "TYPE"            = convert(smallint, 1 /*DBPROPVAL_IT_BTREE*/),
  16756.         FILL_FACTOR        = convert(int, x.OrigFillFactor),
  16757.         INITIAL_SIZE        = convert(int,null),
  16758.         NULLS            = convert(int,null),
  16759.         SORT_BOOKMARKS        = convert(bit,0),
  16760.         AUTO_UPDATE        = convert(bit,1),
  16761.         NULL_COLLATION        = convert(int,4 /*DBPROPVAL_NC_LOW*/),
  16762.         ORDINAL_POSITION    = convert(int,c.colid),
  16763.         COLUMN_NAME        = index_col(user_name(o.uid)+'.'+o.name, x.indid, c.colid),
  16764.         COLUMN_GUID        = convert(binary(16),null),
  16765.         COLUMN_PROPID        = convert(int,null),
  16766.         COLLATION        = convert(smallint, 1 /*DB_COLLATION_ASC*/),
  16767.         CARDINALITY        = case when (x.status & 2) = 2 then x.rows else null end,
  16768.         PAGES            = convert(int, x.dpages),
  16769.         FILTER_CONDITION    = convert(varchar(1),null) 
  16770.         
  16771.     from    sysobjects o, sysindexes x, syscolumns c, sysusers u
  16772.     where    o.type in ('U')
  16773.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  16774.     and    (@index_name is null or @index_name = x.name)
  16775.     and     x.id = o.id
  16776.     and     x.id = c.id
  16777.     and      c.colid < x.keycnt+(x.status&16)/16
  16778.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  16779.     and     (
  16780.         suser_id() = 1     /* User is the System Administrator */
  16781.         or o.uid = user_id()     /* User created the object */
  16782.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16783.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16784.             from sysprotects p
  16785.             where p.id = o.id
  16786.             /* get rows for public,current user,user's group */
  16787.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16788.             /* check for SELECT,EXECUTE privilege */
  16789.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16790.             ) = 1    /* final magic...compare Grants      */
  16791.            )
  16792.     END
  16793. go
  16794.  
  16795.  
  16796. if (charindex('7.00', @@version) > 0)
  16797.     drop procedure sp_indexes_rowset
  16798. else
  16799. begin
  16800.     print ''
  16801.     print ''
  16802.     print 'Warning:'
  16803.     print 'you are installing the stored procedures '
  16804.     print 'on a pre 7.0 SQL Server.'
  16805.     print 'Ignore the following errors.'
  16806. end
  16807. go
  16808.  
  16809. /*     7.0 version */
  16810. create procedure sp_indexes_rowset
  16811.     (
  16812.     @table_name    varchar(255), 
  16813.     @index_name    varchar(255) = null,
  16814.     @table_schema    varchar(255) = null     
  16815.     )
  16816. as
  16817.     select    TABLE_CATALOG        = db_name(),
  16818.         TABLE_SCHEMA        = user_name(o.uid),
  16819.         TABLE_NAME        = o.name,
  16820.         INDEX_CATALOG        = db_name(),        
  16821.         INDEX_SCHEMA        = user_name(o.uid),
  16822.         INDEX_NAME        = x.name,
  16823.         PRIMARY_KEY        = convert(bit,(x.status & 0x800)/0x800),
  16824.         "UNIQUE"        = convert(bit,(x.status & 2)/2),
  16825.         "CLUSTERED"        = convert(bit,(x.status & 16)/16),
  16826.         "TYPE"            = convert(smallint, 1 /*DBPROPVAL_IT_BTREE*/),
  16827.         FILL_FACTOR        = convert(int, x.OrigFillFactor),
  16828.         INITIAL_SIZE        = convert(int,null),
  16829.         NULLS            = convert(int,null),
  16830.         SORT_BOOKMARKS        = convert(bit,0),
  16831.         AUTO_UPDATE        = convert(bit,1),
  16832.         NULL_COLLATION        = convert(int,4 /*DBPROPVAL_NC_LOW*/),
  16833.         ORDINAL_POSITION    = convert(int,c.colid),
  16834.         COLUMN_NAME        = index_col(user_name(o.uid)+'.'+o.name, x.indid, c.colid),
  16835.         COLUMN_GUID        = convert(binary(16),null),
  16836.         COLUMN_PROPID        = convert(int,null),
  16837.         COLLATION        = convert(smallint, 1 /*DB_COLLATION_ASC*/),
  16838.         CARDINALITY        = case when (x.status & 2) = 2 then x.rows else null end,
  16839.         PAGES            = convert(int, x.dpages),
  16840.         FILTER_CONDITION    = convert(varchar(1),null) 
  16841.         
  16842.     from    sysobjects o, sysindexes x, syscolumns c
  16843.     where    o.type in ('U')
  16844.     and     o.name = @table_name
  16845.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  16846.     and    (@index_name is null or @index_name = x.name)
  16847.     and     x.id = o.id
  16848.     and     x.id = c.id
  16849.     and      c.colid < x.keycnt+(x.status&16)/16
  16850.     and     permissions(o.id) <> 0
  16851.     order by 8 desc, 4, 5, 6, 16
  16852. go
  16853. create procedure sp_indexes_rowset;2
  16854.     (
  16855.     @index_name    varchar(255) = null,
  16856.     @table_schema    varchar(255) = null     
  16857.     )
  16858. as
  16859.     select    TABLE_CATALOG        = db_name(),
  16860.         TABLE_SCHEMA        = user_name(o.uid),
  16861.         TABLE_NAME        = o.name,
  16862.         INDEX_CATALOG        = db_name(),        
  16863.         INDEX_SCHEMA        = user_name(o.uid),
  16864.         INDEX_NAME        = x.name,
  16865.         PRIMARY_KEY        = convert(bit,(x.status & 0x800)/0x800),
  16866.         "UNIQUE"        = convert(bit,(x.status & 2)/2),
  16867.         "CLUSTERED"        = convert(bit,(x.status & 16)/16),
  16868.         "TYPE"            = convert(smallint, 1 /*DBPROPVAL_IT_BTREE*/),
  16869.         FILL_FACTOR        = convert(int, x.OrigFillFactor),
  16870.         INITIAL_SIZE        = convert(int,null),
  16871.         NULLS            = convert(int,null),
  16872.         SORT_BOOKMARKS        = convert(bit,0),
  16873.         AUTO_UPDATE        = convert(bit,1),
  16874.         NULL_COLLATION        = convert(int,4 /*DBPROPVAL_NC_LOW*/),
  16875.         ORDINAL_POSITION    = convert(int,c.colid),
  16876.         COLUMN_NAME        = index_col(user_name(o.uid)+'.'+o.name, x.indid, c.colid),
  16877.         COLUMN_GUID        = convert(binary(16),null),
  16878.         COLUMN_PROPID        = convert(int,null),
  16879.         COLLATION        = convert(smallint, 1 /*DB_COLLATION_ASC*/),
  16880.         CARDINALITY        = case when (x.status & 2) = 2 then x.rows else null end,
  16881.         PAGES            = convert(int, x.dpages),
  16882.         FILTER_CONDITION    = convert(varchar(1),null) 
  16883.         
  16884.     from    sysobjects o, sysindexes x, syscolumns c
  16885.     where    o.type in ('U')
  16886.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  16887.     and    (@index_name is null or @index_name = x.name)
  16888.     and     x.id = o.id
  16889.     and     x.id = c.id
  16890.     and      c.colid < x.keycnt+(x.status&16)/16
  16891.     and     permissions(o.id) <> 0
  16892.     order by 8 desc, 4, 5, 6, 16
  16893. go
  16894. create procedure sp_indexes_rowset;3
  16895.     (
  16896.     @table_name    varchar(255) = null, 
  16897.     @index_name    varchar(255) = null,
  16898.     @table_schema    varchar(255) = null     
  16899.     )
  16900. as
  16901. IF @table_name is not null
  16902.     BEGIN
  16903.     select    TABLE_CATALOG        = db_name(),
  16904.         TABLE_SCHEMA        = user_name(o.uid),
  16905.         TABLE_NAME        = o.name,
  16906.         INDEX_CATALOG        = db_name(),        
  16907.         INDEX_SCHEMA        = user_name(o.uid),
  16908.         INDEX_NAME        = x.name,
  16909.         PRIMARY_KEY        = convert(bit,(x.status & 0x800)/0x800),
  16910.         "UNIQUE"        = convert(bit,(x.status & 2)/2),
  16911.         "CLUSTERED"        = convert(bit,(x.status & 16)/16),
  16912.         "TYPE"            = convert(smallint, 1 /*DBPROPVAL_IT_BTREE*/),
  16913.         FILL_FACTOR        = convert(int, x.OrigFillFactor),
  16914.         INITIAL_SIZE        = convert(int,null),
  16915.         NULLS            = convert(int,null),
  16916.         SORT_BOOKMARKS        = convert(bit,0),
  16917.         AUTO_UPDATE        = convert(bit,1),
  16918.         NULL_COLLATION        = convert(int,4 /*DBPROPVAL_NC_LOW*/),
  16919.         ORDINAL_POSITION    = convert(int,c.colid),
  16920.         COLUMN_NAME        = index_col(user_name(o.uid)+'.'+o.name, x.indid, c.colid),
  16921.         COLUMN_GUID        = convert(binary(16),null),
  16922.         COLUMN_PROPID        = convert(int,null),
  16923.         COLLATION        = convert(smallint, 1 /*DB_COLLATION_ASC*/),
  16924.         CARDINALITY        = case when (x.status & 2) = 2 then x.rows else null end,
  16925.         PAGES            = convert(int, x.dpages),
  16926.         FILTER_CONDITION    = convert(varchar(1),null) 
  16927.         
  16928.     from    sysobjects o, sysindexes x, syscolumns c
  16929.     where    o.type in ('U')
  16930.     and     o.name = @table_name
  16931.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  16932.     and    (@index_name is null or @index_name = x.name)
  16933.     and     x.id = o.id
  16934.     and     x.id = c.id
  16935.     and      c.colid < x.keycnt+(x.status&16)/16
  16936.     and     permissions(o.id) <> 0
  16937.     END
  16938. ELSE
  16939.     BEGIN
  16940.     select    TABLE_CATALOG        = db_name(),
  16941.         TABLE_SCHEMA        = user_name(o.uid),
  16942.         TABLE_NAME        = o.name,
  16943.         INDEX_CATALOG        = db_name(),        
  16944.         INDEX_SCHEMA        = user_name(o.uid),
  16945.         INDEX_NAME        = x.name,
  16946.         PRIMARY_KEY        = convert(bit,(x.status & 0x800)/0x800),
  16947.         "UNIQUE"        = convert(bit,(x.status & 2)/2),
  16948.         "CLUSTERED"        = convert(bit,(x.status & 16)/16),
  16949.         "TYPE"            = convert(smallint, 1 /*DBPROPVAL_IT_BTREE*/),
  16950.         FILL_FACTOR        = convert(int, x.OrigFillFactor),
  16951.         INITIAL_SIZE        = convert(int,null),
  16952.         NULLS            = convert(int,null),
  16953.         SORT_BOOKMARKS        = convert(bit,0),
  16954.         AUTO_UPDATE        = convert(bit,1),
  16955.         NULL_COLLATION        = convert(int,4 /*DBPROPVAL_NC_LOW*/),
  16956.         ORDINAL_POSITION    = convert(int,c.colid),
  16957.         COLUMN_NAME        = index_col(user_name(o.uid)+'.'+o.name, x.indid, c.colid),
  16958.         COLUMN_GUID        = convert(binary(16),null),
  16959.         COLUMN_PROPID        = convert(int,null),
  16960.         COLLATION        = convert(smallint, 1 /*DB_COLLATION_ASC*/),
  16961.         CARDINALITY        = case when (x.status & 2) = 2 then x.rows else null end,
  16962.         PAGES            = convert(int, x.dpages),
  16963.         FILTER_CONDITION    = convert(varchar(1),null) 
  16964.         
  16965.     from    sysobjects o, sysindexes x, syscolumns c
  16966.     where    o.type in ('U')
  16967.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  16968.     and    (@index_name is null or @index_name = x.name)
  16969.     and     x.id = o.id
  16970.     and     x.id = c.id
  16971.     and      c.colid < x.keycnt+(x.status&16)/16
  16972.     and     permissions(o.id) <> 0
  16973.     END
  16974. go
  16975.  
  16976.  
  16977. create procedure sp_indexes_rowset;4
  16978.     (
  16979.  
  16980.        @handle        int output,
  16981.        @scrollopt    int output,
  16982.     @ccopt        int output,
  16983.     @rows        int output,
  16984.     @cmd        varchar(255)
  16985.     )
  16986. as
  16987.     declare @ret int
  16988.     declare @database_name varchar(30)
  16989.  
  16990.     create table #sppindxrowset
  16991.         (
  16992.         TABLE_CATALOG        varchar(30) not null,
  16993.         TABLE_SCHEMA        varchar(30) not null,
  16994.         TABLE_NAME        varchar(30) not null,
  16995.         INDEX_CATALOG        varchar(30) not null,        
  16996.         INDEX_SCHEMA        varchar(30) not null,
  16997.         INDEX_NAME        varchar(30) not null,
  16998.         PRIMARY_KEY        bit not null,
  16999.         "UNIQUE"        bit not null,
  17000.         "CLUSTERED"        bit not null,
  17001.         "TYPE"            smallint not null,
  17002.         FILL_FACTOR        int not null,
  17003.         INITIAL_SIZE        int null,
  17004.         NULLS            int null,
  17005.         SORT_BOOKMARKS        bit not null,
  17006.         AUTO_UPDATE        bit not null,
  17007.         NULL_COLLATION        int not null,
  17008.         ORDINAL_POSITION    int not null,
  17009.         COLUMN_NAME        varchar(30) not null,
  17010.         COLUMN_GUID        binary(16) null,
  17011.         COLUMN_PROPID        int null,
  17012.         COLLATION        smallint not null,
  17013.         CARDINALITY        int null,
  17014.         PAGES            int not null,
  17015.         FILTER_CONDITION    varchar(1) null         
  17016.         )
  17017.  
  17018.     declare sysdatabases_cursor insensitive cursor for 
  17019.         select name from master.dbo.sysdatabases
  17020.   
  17021.     set nocount on
  17022.     open sysdatabases_cursor
  17023.       fetch next from sysdatabases_cursor into @database_name 
  17024.     while @@fetch_status = 0
  17025.         begin
  17026.         exec('use '+@database_name+' insert into #sppindxrowset '+@cmd)
  17027.         fetch next from sysdatabases_cursor into @database_name 
  17028.         end
  17029.       close sysdatabases_cursor
  17030.     deallocate sysdatabases_cursor
  17031.  
  17032.     if @scrollopt is null
  17033.         begin
  17034.         select * from #sppindxrowset order by 8 desc, 4, 5, 6, 16
  17035.         end
  17036.     else
  17037.         begin
  17038.         exec @ret = sp_cursoropen @handle output,
  17039.             'select * from #sppindxrowset order by 8 desc, 4, 5, 6, 16',
  17040.                 @scrollopt output, @ccopt output, @rows output
  17041.         end
  17042.     drop table #sppindxrowset
  17043.     return isnull(@ret,0)
  17044. go
  17045.  
  17046. grant execute on sp_indexes_rowset to public
  17047. go
  17048.  
  17049. dump tran master with no_log
  17050. go
  17051.  
  17052. print 'creating sp_primary_keys_rowset'
  17053. go
  17054.  
  17055. /*    Procedure for 6.0 and 6.5 servers */
  17056. create procedure sp_primary_keys_rowset
  17057.     (
  17058.        @table_name        varchar(255),
  17059.     @table_schema         varchar(244) = null
  17060.     )
  17061. as
  17062.     select    TABLE_CATALOG    = db_name(),
  17063.         TABLE_SCHEMA    = user_name(o.uid),
  17064.         TABLE_NAME    = o.name,    
  17065.         COLUMN_NAME    = c.name,
  17066.         COLUMN_GUID    = convert(binary(16),null),
  17067.         COLUMN_PROPID    = convert(int,null),
  17068.         ORDINAL        = convert(int,c1.colid)
  17069.     from    sysindexes i, syscolumns c, sysobjects o, syscolumns c1, sysusers u
  17070.     where    o.type in ('U')
  17071.     and     o.name = @table_name
  17072.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  17073.     and     o.id = c.id
  17074.     and     o.id = i.id
  17075.     and     (i.status & 0x800) = 0x800
  17076.     and     c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, c1.colid)
  17077.     and     c1.id = c.id
  17078.     and     c1.colid <= i.keycnt    /* create rows from 1 to keycnt */
  17079.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  17080.     and     (
  17081.         suser_id() = 1     /* User is the System Administrator */
  17082.         or o.uid = user_id()     /* User created the object */
  17083.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  17084.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  17085.             from sysprotects p
  17086.             where p.id = o.id
  17087.             /* get rows for public,current user,user's group */
  17088.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  17089.             /* check for SELECT,EXECUTE privilege */
  17090.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  17091.             ) = 1    /* final magic...compare Grants      */
  17092.            )
  17093.     order by 2, 3
  17094. go
  17095. create procedure sp_primary_keys_rowset;2
  17096.     (
  17097.      @table_schema         varchar(244) = null
  17098.     )
  17099. as
  17100.     select    TABLE_CATALOG    = db_name(),
  17101.         TABLE_SCHEMA    = user_name(o.uid),
  17102.         TABLE_NAME    = o.name,    
  17103.         COLUMN_NAME    = c.name,
  17104.         COLUMN_GUID    = convert(binary(16),null),
  17105.         COLUMN_PROPID    = convert(int,null),
  17106.         ORDINAL        = convert(int,c1.colid)
  17107.     from    sysindexes i, syscolumns c, sysobjects o, syscolumns c1, sysusers u
  17108.     where    o.type in ('U')
  17109.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  17110.     and     o.id = c.id
  17111.     and     o.id = i.id
  17112.     and     (i.status & 0x800) = 0x800
  17113.     and     c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, c1.colid)
  17114.     and     c1.id = c.id
  17115.     and     c1.colid <= i.keycnt    /* create rows from 1 to keycnt */
  17116.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  17117.     and     (
  17118.         suser_id() = 1     /* User is the System Administrator */
  17119.         or o.uid = user_id()     /* User created the object */
  17120.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  17121.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  17122.             from sysprotects p
  17123.             where p.id = o.id
  17124.             /* get rows for public,current user,user's group */
  17125.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  17126.             /* check for SELECT,EXECUTE privilege */
  17127.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  17128.             ) = 1    /* final magic...compare Grants      */
  17129.            )
  17130.     order by 2, 3
  17131. go
  17132. create procedure sp_primary_keys_rowset;3
  17133.     (
  17134.        @table_name        varchar(255) = null,
  17135.     @table_schema         varchar(244) = null
  17136.     )
  17137. as
  17138. IF @table_name is not NULL
  17139.     BEGIN
  17140.     select    TABLE_CATALOG    = db_name(),
  17141.         TABLE_SCHEMA    = user_name(o.uid),
  17142.         TABLE_NAME    = o.name,    
  17143.         COLUMN_NAME    = c.name,
  17144.         COLUMN_GUID    = convert(binary(16),null),
  17145.         COLUMN_PROPID    = convert(int,null),
  17146.         ORDINAL        = convert(int,c1.colid)
  17147.     from    sysindexes i, syscolumns c, sysobjects o, syscolumns c1, sysusers u
  17148.     where    o.type in ('U')
  17149.     and     o.name = @table_name
  17150.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  17151.     and     o.id = c.id
  17152.     and     o.id = i.id
  17153.     and     (i.status & 0x800) = 0x800
  17154.     and     c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, c1.colid)
  17155.     and     c1.id = c.id
  17156.     and     c1.colid <= i.keycnt    /* create rows from 1 to keycnt */
  17157.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  17158.     and     (
  17159.         suser_id() = 1     /* User is the System Administrator */
  17160.         or o.uid = user_id()     /* User created the object */
  17161.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  17162.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  17163.             from sysprotects p
  17164.             where p.id = o.id
  17165.             /* get rows for public,current user,user's group */
  17166.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  17167.             /* check for SELECT,EXECUTE privilege */
  17168.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  17169.             ) = 1    /* final magic...compare Grants      */
  17170.            )
  17171.     END
  17172. ELSE
  17173.     BEGIN
  17174.     select    TABLE_CATALOG    = db_name(),
  17175.         TABLE_SCHEMA    = user_name(o.uid),
  17176.         TABLE_NAME    = o.name,    
  17177.         COLUMN_NAME    = c.name,
  17178.         COLUMN_GUID    = convert(binary(16),null),
  17179.         COLUMN_PROPID    = convert(int,null),
  17180.         ORDINAL        = convert(int,c1.colid)
  17181.     from    sysindexes i, syscolumns c, sysobjects o, syscolumns c1, sysusers u
  17182.     where    o.type in ('U')
  17183.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  17184.     and     o.id = c.id
  17185.     and     o.id = i.id
  17186.     and     (i.status & 0x800) = 0x800
  17187.     and     c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, c1.colid)
  17188.     and     c1.id = c.id
  17189.     and     c1.colid <= i.keycnt    /* create rows from 1 to keycnt */
  17190.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  17191.     and     (
  17192.         suser_id() = 1     /* User is the System Administrator */
  17193.         or o.uid = user_id()     /* User created the object */
  17194.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  17195.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  17196.             from sysprotects p
  17197.             where p.id = o.id
  17198.             /* get rows for public,current user,user's group */
  17199.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  17200.             /* check for SELECT,EXECUTE privilege */
  17201.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  17202.             ) = 1    /* final magic...compare Grants      */
  17203.            )
  17204.     END
  17205. go
  17206.  
  17207. if (charindex('7.00', @@version) = 0)
  17208. begin
  17209.     print ''
  17210.     print ''
  17211.     print 'Warning:'
  17212.     print 'you are installing the stored procedures '
  17213.     print 'on a pre 7.0 SQL Server.'
  17214.     print 'Ignore the following errors.'
  17215. end
  17216. else
  17217.     drop proc sp_primary_keys_rowset
  17218. go
  17219.  
  17220. /*    Procedure for 7.0 servers */
  17221. create procedure sp_primary_keys_rowset
  17222.     (
  17223.        @table_name        varchar(255),
  17224.     @table_schema         varchar(244) = null
  17225.     )
  17226. as
  17227.     select    TABLE_CATALOG    = db_name(),
  17228.         TABLE_SCHEMA    = user_name(o.uid),
  17229.         TABLE_NAME    = o.name,    
  17230.         COLUMN_NAME    = c.name,
  17231.         COLUMN_GUID    = convert(binary(16),null),
  17232.         COLUMN_PROPID    = convert(int,null),
  17233.         ORDINAL        = convert(int,c1.colid)
  17234.     from    sysindexes i, syscolumns c, sysobjects o, syscolumns c1
  17235.     where    o.type in ('U')
  17236.     and     o.name = @table_name
  17237.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  17238.     and     o.id = c.id
  17239.     and     o.id = i.id
  17240.     and     (i.status & 0x800) = 0x800
  17241.     and     c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, c1.colid)
  17242.     and     c1.id = c.id
  17243.     and     c1.colid <= i.keycnt    /* create rows from 1 to keycnt */
  17244.     and    permissions(o.id) <> 0
  17245.     order by 2, 3
  17246. go
  17247. create procedure sp_primary_keys_rowset;2
  17248.     (
  17249.      @table_schema         varchar(244) = null
  17250.     )
  17251. as
  17252.     select    TABLE_CATALOG    = db_name(),
  17253.         TABLE_SCHEMA    = user_name(o.uid),
  17254.         TABLE_NAME    = o.name,    
  17255.         COLUMN_NAME    = c.name,
  17256.         COLUMN_GUID    = convert(binary(16),null),
  17257.         COLUMN_PROPID    = convert(int,null),
  17258.         ORDINAL        = convert(int,c1.colid)
  17259.     from    sysindexes i, syscolumns c, sysobjects o, syscolumns c1
  17260.     where    o.type in ('U')
  17261.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  17262.     and     o.id = c.id
  17263.     and     o.id = i.id
  17264.     and     (i.status & 0x800) = 0x800
  17265.     and     c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, c1.colid)
  17266.     and     c1.id = c.id
  17267.     and     c1.colid <= i.keycnt    /* create rows from 1 to keycnt */
  17268.     and    permissions(o.id) <> 0
  17269.     order by 2, 3
  17270. go
  17271. create procedure sp_primary_keys_rowset;3
  17272.     (
  17273.        @table_name        varchar(255) = null,
  17274.     @table_schema         varchar(244) = null
  17275.     )
  17276. as
  17277. IF @table_name is not NULL
  17278.     BEGIN
  17279.     select    TABLE_CATALOG    = db_name(),
  17280.         TABLE_SCHEMA    = user_name(o.uid),
  17281.         TABLE_NAME    = o.name,    
  17282.         COLUMN_NAME    = c.name,
  17283.         COLUMN_GUID    = convert(binary(16),null),
  17284.         COLUMN_PROPID    = convert(int,null),
  17285.         ORDINAL        = convert(int,c1.colid)
  17286.     from    sysindexes i, syscolumns c, sysobjects o, syscolumns c1
  17287.     where    o.type in ('U')
  17288.     and     o.name = @table_name
  17289.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  17290.     and     o.id = c.id
  17291.     and     o.id = i.id
  17292.     and     (i.status & 0x800) = 0x800
  17293.     and     c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, c1.colid)
  17294.     and     c1.id = c.id
  17295.     and     c1.colid <= i.keycnt    /* create rows from 1 to keycnt */
  17296.     and    permissions(o.id) <> 0
  17297.     END
  17298. ELSE
  17299.     BEGIN
  17300.     select    TABLE_CATALOG    = db_name(),
  17301.         TABLE_SCHEMA    = user_name(o.uid),
  17302.         TABLE_NAME    = o.name,    
  17303.         COLUMN_NAME    = c.name,
  17304.         COLUMN_GUID    = convert(binary(16),null),
  17305.         COLUMN_PROPID    = convert(int,null),
  17306.         ORDINAL        = convert(int,c1.colid)
  17307.     from    sysindexes i, syscolumns c, sysobjects o, syscolumns c1
  17308.     where    o.type in ('U')
  17309.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  17310.     and     o.id = c.id
  17311.     and     o.id = i.id
  17312.     and     (i.status & 0x800) = 0x800
  17313.     and     c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, c1.colid)
  17314.     and     c1.id = c.id
  17315.     and     c1.colid <= i.keycnt    /* create rows from 1 to keycnt */
  17316.     and    permissions(o.id) <> 0
  17317.     END
  17318. go
  17319.  
  17320.  
  17321. create procedure sp_primary_keys_rowset;4
  17322.     (
  17323.  
  17324.        @handle        int output,
  17325.        @scrollopt    int output,
  17326.     @ccopt        int output,
  17327.     @rows        int output,
  17328.     @cmd        varchar(255)
  17329.     )
  17330. as
  17331.     declare @ret int
  17332.     declare @database_name varchar(30)
  17333.  
  17334.     create table #sppkeysrowset
  17335.         (
  17336.         TABLE_CATALOG        varchar(30) not null,
  17337.         TABLE_SCHEMA        varchar(30) not null,
  17338.         TABLE_NAME        varchar(30) not null,
  17339.         COLUMN_NAME        varchar(30) not null,
  17340.         COLUMN_GUID        binary(16) null,
  17341.         COLUMN_PROPID        int null,
  17342.         ORDINAL            int null
  17343.         )
  17344.  
  17345.     declare sysdatabases_cursor insensitive cursor for 
  17346.         select name from master.dbo.sysdatabases
  17347.   
  17348.     set nocount on
  17349.     open sysdatabases_cursor
  17350.       fetch next from sysdatabases_cursor into @database_name 
  17351.     while @@fetch_status = 0
  17352.         begin
  17353.         exec('use '+@database_name+' insert into #sppkeysrowset '+@cmd)
  17354.         fetch next from sysdatabases_cursor into @database_name 
  17355.         end
  17356.       close sysdatabases_cursor
  17357.     deallocate sysdatabases_cursor
  17358.  
  17359.     if @scrollopt is null
  17360.         begin
  17361.         select * from #sppkeysrowset order by 1,2,3
  17362.         end
  17363.     else
  17364.         begin
  17365.         exec @ret = sp_cursoropen @handle output,
  17366.             'select * from #sppkeysrowset order by 1,2,3',
  17367.                 @scrollopt output, @ccopt output, @rows output
  17368.         end
  17369.     drop table #sppkeysrowset
  17370.     return isnull(@ret,0)
  17371. go
  17372.  
  17373. grant execute on sp_primary_keys_rowset to public
  17374. go
  17375.  
  17376. dump tran master with no_log
  17377. go
  17378.  
  17379. print 'creating sp_provider_types_rowset'
  17380. go
  17381.  
  17382. /*    Procedure for 6.0 and 6.50 servers */
  17383. create proc sp_provider_types_rowset
  17384.     (
  17385.     @data_type     smallint = null,
  17386.     @best_match    tinyint  = null
  17387.     )
  17388. as
  17389.     select
  17390.         TYPE_NAME         = case    when t.usertype in (18,80) then t.name 
  17391.                         else d.type_name 
  17392.                         end,
  17393.         DATA_TYPE         = d.oledb_data_type,                            
  17394.         COLUMN_SIZE        = coalesce(d.column_size,d.data_precision,t.prec),
  17395.         LITERAL_PREFIX         = d.literal_prefix,
  17396.         LITERAL_SUFFIX         = d.literal_suffix,
  17397.         CREATE_PARAMS         = e.create_params,
  17398.         IS_NULLABLE        = convert(bit, 
  17399.                         case 
  17400.                         when d.auto_unique_value = 1 then convert(bit,0) /* IDENTITY*/
  17401.                         else t.allownulls
  17402.                         end),
  17403.         CASE_SENSITIVE        = d.case_sensitive,
  17404.         SEARCHABLE         = d.searchable,
  17405.         UNSIGNED_ATTRIBUTE    = d.unsigned_attribute,
  17406.         FIXED_PREC_SCALE    = d.fixed_prec_scale,
  17407.         AUTO_UNIQUE_VALUE    = d.auto_unique_value, 
  17408.         LOCAL_TYPE_NAME        = d.local_type_name,
  17409.         MINIMUM_SCALE        = convert(smallint,
  17410.                         case 
  17411.                         when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then 0 
  17412.                         else null 
  17413.                         end),
  17414.         MAXIMUM_SCALE        = convert(smallint,
  17415.                         case 
  17416.                         when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ 
  17417.                             then case 
  17418.                                 when d.auto_unique_value = 1 then 0 
  17419.                                 else d.data_precision 
  17420.                                 end
  17421.                         else null 
  17422.                         end),
  17423.         GUID            = convert(binary(16),null),
  17424.         TYPELIB            = convert(varchar(1),null),
  17425.         VERSION            = convert(varchar(1),null),
  17426.         IS_LONG            = d.is_long,
  17427.         BEST_MATCH        = d.best_match
  17428.  
  17429.     from     master.dbo.spt_provider_types d, master.dbo.spt_datatype_info_ext e, systypes t
  17430.     where    d.ss_dtype = t.type 
  17431.     and    t.usertype <= 100
  17432.     and    (case when t.usertype = 80 /* TIMESTAMP */ then 1 else 0 end 
  17433.             = case when d.oledb_data_type = 21 /*DBTYPE_UI8*/ then 1 else 0 end)
  17434.     and     t.usertype *= e.user_type 
  17435.     and     d.auto_unique_value *= e.AUTO_INCREMENT
  17436.     and     t.type not in (111,109,38,110,55,63)    /* get rid of nullable types */
  17437.     and    (@data_type is null or d.oledb_data_type = @data_type)
  17438.     and    (@best_match is null or d.best_match = @best_match)
  17439.     order by 2
  17440. go
  17441.  
  17442. if (charindex('7.00', @@version) = 0)
  17443. begin
  17444.     print ''
  17445.     print ''
  17446.     print 'Warning:'
  17447.     print 'you are installing the stored procedures '
  17448.     print 'on a pre 7.0 SQL Server.'
  17449.     print 'Ignore the following errors.'
  17450. end
  17451. else
  17452.     drop proc sp_provider_types_rowset
  17453. go
  17454.  
  17455. /*    Procedure for 7.0 server */
  17456. create proc sp_provider_types_rowset
  17457.     (
  17458.     @data_type     smallint = null,
  17459.     @best_match    tinyint  = null
  17460.     )
  17461. as
  17462.     select
  17463.         TYPE_NAME         = case    when t.usertype in (18,80) then t.name 
  17464.                         else d.type_name 
  17465.                         end,
  17466.         DATA_TYPE         = d.oledb_data_type,                            
  17467.         COLUMN_SIZE        = coalesce(d.column_size,d.data_precision,t.prec),
  17468.         LITERAL_PREFIX         = d.literal_prefix,
  17469.         LITERAL_SUFFIX         = d.literal_suffix,
  17470.         CREATE_PARAMS         = e.create_params,
  17471.         IS_NULLABLE        = convert(bit, 
  17472.                         case 
  17473.                         when d.auto_unique_value = 1 then 0 /* IDENTITY*/
  17474.                         else TypeProperty (t.name, 'AllowsNull')
  17475.                         end),
  17476.         CASE_SENSITIVE        = d.case_sensitive,
  17477.         SEARCHABLE         = d.searchable,
  17478.         UNSIGNED_ATTRIBUTE    = d.unsigned_attribute,
  17479.         FIXED_PREC_SCALE    = d.fixed_prec_scale,
  17480.         AUTO_UNIQUE_VALUE    = d.auto_unique_value, 
  17481.         LOCAL_TYPE_NAME        = d.local_type_name,
  17482.         MINIMUM_SCALE        = convert(smallint,
  17483.                         case 
  17484.                         when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then 0 
  17485.                         else null 
  17486.                         end),
  17487.         MAXIMUM_SCALE        = convert(smallint,
  17488.                         case 
  17489.                         when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ 
  17490.                             then case 
  17491.                                 when d.auto_unique_value = 1 then 0 
  17492.                                 else d.data_precision 
  17493.                                 end
  17494.                         else null 
  17495.                         end),
  17496.         GUID            = convert(binary(16),null),
  17497.         TYPELIB            = convert(varchar(1),null),
  17498.         VERSION            = convert(varchar(1),null),
  17499.         IS_LONG            = d.is_long,
  17500.         BEST_MATCH        = d.best_match
  17501.     from master.dbo.spt_provider_types d
  17502.         INNER JOIN model.dbo.systypes t on d.ss_dtype = t.xtype
  17503.         LEFT OUTER JOIN master.dbo.spt_datatype_info_ext e on
  17504.             t.xusertype = e.user_type
  17505.             and d.auto_unique_value = e.AUTO_INCREMENT
  17506.     where    (@data_type is null or d.oledb_data_type = @data_type)
  17507.     and    (@best_match is null or d.best_match = @best_match)    
  17508.     and    t.usertype <= 255
  17509.     order by 2
  17510. go
  17511.  
  17512. grant execute on sp_provider_types_rowset to public
  17513. go
  17514.  
  17515. dump tran master with no_log
  17516. go
  17517.  
  17518. print 'creating sp_procedure_params_rowset'
  17519. go
  17520.  
  17521. /*    Procedure for 6.0 and 6.50 servers */
  17522. create procedure sp_procedure_params_rowset
  17523.     (
  17524.        @procedure_name        varchar(255) = null,
  17525.     @group_number        int = null,
  17526.     @procedure_schema     varchar(255) = null,
  17527.     @parameter_name        varchar(255) = null
  17528.     )
  17529. as
  17530. IF @procedure_name is not null
  17531.     BEGIN
  17532.     select
  17533.         PROCEDURE_CATALOG     = db_name(),
  17534.         PROCEDURE_SCHEMA     = user_name(),
  17535.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  17536.         PARAMETER_NAME         = c.name,
  17537.         ORDINAL_POSITION     = convert(smallint,c.colid),
  17538.         PARAMETER_TYPE         = convert(smallint, 1+((c.status/64)&1)),
  17539.         PARAMETER_HASDEFAULT    = convert(tinyint, null),
  17540.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  17541.         IS_NULLABLE        = d.is_nullable,
  17542.         DATA_TYPE        = d.oledb_data_type,
  17543.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  17544.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  17545.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  17546.                         then coalesce(d.column_size,c.length)
  17547.                         else null 
  17548.                         end),
  17549.         CHARACTER_OCTET_LENGTH    = convert(int,
  17550.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  17551.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  17552.                         then coalesce(d.column_size,c.length)
  17553.                         else null 
  17554.                         end),
  17555.         NUMERIC_PRECISION    = convert(smallint,
  17556.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec else null end),
  17557.         NUMERIC_SCALE        = convert(smallint, 
  17558.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  17559.         DESCRIPTION        = convert(varchar(1),null)
  17560.     
  17561.     from
  17562.         syscolumns c,
  17563.         sysobjects o,
  17564.         master.dbo.spt_provider_types d,
  17565.         systypes t,
  17566.         sysusers u
  17567.     where
  17568.         o.name = @procedure_name
  17569.     and    o.type = 'P'                            /* Just Procedures */
  17570.     and    (@procedure_schema = null or @procedure_schema = user_name(o.uid))
  17571.     and    o.id = c.id
  17572.     and    c.number = @group_number
  17573.     and    c.type = d.ss_dtype
  17574.     and    c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  17575.     and    coalesce(d.auto_unique_value,0) = 0
  17576.     and    c.usertype = t.usertype
  17577.     and    (@parameter_name is null or @parameter_name = c.name)
  17578.     and     u.uid = user_id()    /* constrain sysusers uid for use in subquery */
  17579.     and     (    suser_id() = 1     /* User is the System Administrator */
  17580.         or     o.uid = user_id()    /* User created the object */
  17581.             /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  17582.         or     ((select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  17583.              from sysprotects p
  17584.              where p.id = o.id
  17585.                  /*  get rows for public,current user,user's group */
  17586.                  and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  17587.                  /* check for SELECT,EXECUTE privilege */
  17588.              and (action in (193,224)))&1    /* more magic...normalize GRANT */
  17589.             ) = 1     /* final magic...compare Grants    */
  17590.         )
  17591.     UNION ALL
  17592.     SELECT           /* return value row*/
  17593.         PROCEDURE_CATALOG     = db_name(),
  17594.         PROCEDURE_SCHEMA     = user_name(),
  17595.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  17596.         PARAMETER_NAME         = convert(varchar(30),'RETURN_VALUE'),
  17597.         ORDINAL_POSITION     = convert(smallint,0),
  17598.         PARAMETER_TYPE         = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  17599.         PARAMETER_HASDEFAULT    = convert(tinyint,null),
  17600.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  17601.         IS_NULLABLE        = convert(bit,0),
  17602.         DATA_TYPE        = convert(smallint, 3 /*DBTYPE_I4*/),
  17603.         CHARACTER_MAXIMUM_LENGTH= convert(int,null),
  17604.         CHARACTER_OCTET_LENGTH    = convert(int,null),
  17605.         NUMERIC_PRECISION    = convert(smallint,null),
  17606.         NUMERIC_SCALE        = convert(smallint,null),
  17607.         DESCRIPTION        = convert(varchar(1),null)
  17608.     from
  17609.         syscolumns c,
  17610.         sysobjects o,
  17611.         sysusers u
  17612.     where
  17613.         o.name = @procedure_name
  17614.     and    o.type = 'P'                        /* Just Procedures */
  17615.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  17616.     and    (@parameter_name is null or @parameter_name = 'RETURN_VALUE')
  17617.     and    c.id =* o.id
  17618.     and     c.colid = 1
  17619.     and     u.uid = user_id()    /* constrain sysusers uid for use in subquery */
  17620.     and     (    suser_id() = 1     /* User is the System Administrator */
  17621.         or     o.uid = user_id()    /* User created the object */
  17622.             /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  17623.         or     ((select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  17624.              from sysprotects p
  17625.              where p.id = o.id
  17626.                  /*  get rows for public,current user,user's group */
  17627.                  and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  17628.                  /* check for SELECT,EXECUTE privilege */
  17629.              and (action in (193,224)))&1    /* more magic...normalize GRANT */
  17630.             ) = 1     /* final magic...compare Grants    */
  17631.         )
  17632.     order by 2, 3, 5
  17633.     END
  17634. ELSE
  17635.     BEGIN
  17636.     select
  17637.         PROCEDURE_CATALOG     = db_name(),
  17638.         PROCEDURE_SCHEMA     = user_name(),
  17639.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  17640.         PARAMETER_NAME         = c.name,
  17641.         ORDINAL_POSITION     = convert(smallint,c.colid),
  17642.         PARAMETER_TYPE         = convert(smallint, 1+((c.status/64)&1)),
  17643.         PARAMETER_HASDEFAULT    = convert(tinyint,null),
  17644.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  17645.         IS_NULLABLE        = d.is_nullable,
  17646.         DATA_TYPE        = d.oledb_data_type,
  17647.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  17648.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  17649.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  17650.                         then coalesce(d.column_size,c.length)
  17651.                         else null 
  17652.                         end),
  17653.         CHARACTER_OCTET_LENGTH    = convert(int,
  17654.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  17655.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  17656.                         then coalesce(d.column_size,c.length)
  17657.                         else null 
  17658.                         end),
  17659.         NUMERIC_PRECISION    = convert(smallint,
  17660.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec else null end),
  17661.         NUMERIC_SCALE        = convert(smallint, 
  17662.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  17663.         DESCRIPTION        = convert(varchar(1),null)
  17664.     
  17665.     from
  17666.         syscolumns c,
  17667.         sysobjects o,
  17668.         master.dbo.spt_provider_types d,
  17669.         systypes t,
  17670.         sysusers u
  17671.     where
  17672.         o.type = 'P'                            /* Just Procedures */
  17673.     and    (@procedure_schema = null or @procedure_schema = user_name(o.uid))
  17674.     and    o.id = c.id
  17675.     and    c.type = d.ss_dtype
  17676.     and    c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  17677.     and    coalesce(d.auto_unique_value,0) = 0
  17678.     and    c.usertype = t.usertype
  17679.     and    (@parameter_name is null or @parameter_name = c.name)
  17680.     and     u.uid = user_id()    /* constrain sysusers uid for use in subquery */
  17681.     and     (    suser_id() = 1     /* User is the System Administrator */
  17682.         or     o.uid = user_id()    /* User created the object */
  17683.             /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  17684.         or     ((select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  17685.              from sysprotects p
  17686.              where p.id = o.id
  17687.                  /*  get rows for public,current user,user's group */
  17688.                  and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  17689.                  /* check for SELECT,EXECUTE privilege */
  17690.              and (action in (193,224)))&1    /* more magic...normalize GRANT */
  17691.             ) = 1     /* final magic...compare Grants    */
  17692.         )
  17693.     UNION ALL
  17694.     SELECT           /* return value row*/
  17695.         PROCEDURE_CATALOG     = db_name(),
  17696.         PROCEDURE_SCHEMA     = user_name(),
  17697.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  17698.         PARAMETER_NAME         = convert(varchar(30),'RETURN_VALUE'),
  17699.         ORDINAL_POSITION     = convert(smallint,0),
  17700.         PARAMETER_TYPE         = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  17701.         PARAMETER_HASDEFAULT    = convert(tinyint,null),
  17702.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  17703.         IS_NULLABLE        = convert(bit,0),
  17704.         DATA_TYPE        = convert(smallint, 3 /*DBTYPE_I4*/),
  17705.         CHARACTER_MAXIMUM_LENGTH= convert(int,null),
  17706.         CHARACTER_OCTET_LENGTH    = convert(int,null),
  17707.         NUMERIC_PRECISION    = convert(smallint,null),
  17708.         NUMERIC_SCALE        = convert(smallint,null),
  17709.         DESCRIPTION        = convert(varchar(1),null)
  17710.     from
  17711.         syscolumns c,
  17712.         sysobjects o,
  17713.         sysusers u
  17714.     where
  17715.         o.type = 'P'                        /* Just Procedures */
  17716.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  17717.     and    (@parameter_name is null or @parameter_name = 'RETURN_VALUE')
  17718.     and    c.id =* o.id
  17719.     and     c.colid = 1
  17720.     and     u.uid = user_id()    /* constrain sysusers uid for use in subquery */
  17721.     and     (    suser_id() = 1     /* User is the System Administrator */
  17722.         or     o.uid = user_id()    /* User created the object */
  17723.             /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  17724.         or     ((select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  17725.              from sysprotects p
  17726.              where p.id = o.id
  17727.                  /*  get rows for public,current user,user's group */
  17728.                  and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  17729.                  /* check for SELECT,EXECUTE privilege */
  17730.              and (action in (193,224)))&1    /* more magic...normalize GRANT */
  17731.             ) = 1     /* final magic...compare Grants    */
  17732.         )
  17733.     order by 2, 3, 5
  17734.     END
  17735. go
  17736. create procedure sp_procedure_params_rowset;2
  17737.     (
  17738.       @handle            int output,
  17739.        @scrollopt        int output,
  17740.     @ccopt            int output,
  17741.     @rows            int output,
  17742.        @procedure_name        varchar(255) = null,
  17743.     @group_number        int = null,
  17744.     @procedure_schema     varchar(255) = null,
  17745.     @parameter_name        varchar(255) = null
  17746.     )
  17747. as
  17748.     declare @ret int
  17749.  
  17750.  
  17751.     create table #spprocparamrowset1
  17752.         (
  17753.         PROCEDURE_CATALOG     varchar(30) not null,
  17754.         PROCEDURE_SCHEMA     varchar(30) not null,
  17755.         PROCEDURE_NAME         varchar(35) not null,
  17756.         PARAMETER_NAME         varchar(30) not null,
  17757.         ORDINAL_POSITION     smallint not null,
  17758.         PARAMETER_TYPE         smallint null,
  17759.         PARAMETER_HASDEFAULT    tinyint null,
  17760.         PARAMETER_DEFAULT    varchar(255) null,
  17761.         IS_NULLABLE        bit not null,
  17762.         DATA_TYPE        smallint null,
  17763.         CHARACTER_MAXIMUM_LENGTH int null,
  17764.         CHARACTER_OCTET_LENGTH    int null,
  17765.         NUMERIC_PRECISION    smallint null,
  17766.         NUMERIC_SCALE        smallint null,
  17767.         DESCRIPTION        varchar(1) null
  17768.         )
  17769.  
  17770. IF @procedure_name is not null
  17771.     BEGIN
  17772.     insert into #spprocparamrowset1
  17773.     select
  17774.         PROCEDURE_CATALOG     = db_name(),
  17775.         PROCEDURE_SCHEMA     = user_name(),
  17776.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  17777.         PARAMETER_NAME         = c.name,
  17778.         ORDINAL_POSITION     = convert(smallint,c.colid),
  17779.         PARAMETER_TYPE         = convert(smallint, 1+((c.status/64)&1)),
  17780.         PARAMETER_HASDEFAULT    = convert(tinyint, null),
  17781.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  17782.         IS_NULLABLE        = d.is_nullable,
  17783.         DATA_TYPE        = d.oledb_data_type,
  17784.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  17785.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  17786.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  17787.                         then coalesce(d.column_size,c.length)
  17788.                         else null 
  17789.                         end),
  17790.         CHARACTER_OCTET_LENGTH    = convert(int,
  17791.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  17792.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  17793.                         then coalesce(d.column_size,c.length)
  17794.                         else null 
  17795.                         end),
  17796.         NUMERIC_PRECISION    = convert(smallint,
  17797.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec else null end),
  17798.         NUMERIC_SCALE        = convert(smallint, 
  17799.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  17800.         DESCRIPTION        = convert(varchar(1),null)
  17801.     
  17802.     from
  17803.         syscolumns c,
  17804.         sysobjects o,
  17805.         master.dbo.spt_provider_types d,
  17806.         systypes t,
  17807.         sysusers u
  17808.     where
  17809.         o.name = @procedure_name
  17810.     and    o.type = 'P'                            /* Just Procedures */
  17811.     and    (@procedure_schema = null or @procedure_schema = user_name(o.uid))
  17812.     and    o.id = c.id
  17813.     and    c.number = @group_number
  17814.     and    c.type = d.ss_dtype
  17815.     and    c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  17816.     and    coalesce(d.auto_unique_value,0) = 0
  17817.     and    c.usertype = t.usertype
  17818.     and    (@parameter_name is null or @parameter_name = c.name)
  17819.     and     u.uid = user_id()    /* constrain sysusers uid for use in subquery */
  17820.     and     (    suser_id() = 1     /* User is the System Administrator */
  17821.         or     o.uid = user_id()    /* User created the object */
  17822.             /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  17823.         or     ((select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  17824.              from sysprotects p
  17825.              where p.id = o.id
  17826.                  /*  get rows for public,current user,user's group */
  17827.                  and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  17828.                  /* check for SELECT,EXECUTE privilege */
  17829.              and (action in (193,224)))&1    /* more magic...normalize GRANT */
  17830.             ) = 1     /* final magic...compare Grants    */
  17831.         )
  17832.     UNION ALL
  17833.     SELECT           /* return value row*/
  17834.         PROCEDURE_CATALOG     = db_name(),
  17835.         PROCEDURE_SCHEMA     = user_name(),
  17836.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  17837.         PARAMETER_NAME         = convert(varchar(30),'RETURN_VALUE'),
  17838.         ORDINAL_POSITION     = convert(smallint,0),
  17839.         PARAMETER_TYPE         = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  17840.         PARAMETER_HASDEFAULT    = convert(tinyint,null),
  17841.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  17842.         IS_NULLABLE        = convert(bit,0),
  17843.         DATA_TYPE        = convert(smallint, 3 /*DBTYPE_I4*/),
  17844.         CHARACTER_MAXIMUM_LENGTH= convert(int,null),
  17845.         CHARACTER_OCTET_LENGTH    = convert(int,null),
  17846.         NUMERIC_PRECISION    = convert(smallint,null),
  17847.         NUMERIC_SCALE        = convert(smallint,null),
  17848.         DESCRIPTION        = convert(varchar(1),null)
  17849.     from
  17850.         syscolumns c,
  17851.         sysobjects o,
  17852.         sysusers u
  17853.     where
  17854.         o.name = @procedure_name
  17855.     and    o.type = 'P'                        /* Just Procedures */
  17856.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  17857.     and    (@parameter_name is null or @parameter_name = 'RETURN_VALUE')
  17858.     and    c.id =* o.id
  17859.     and     c.colid = 1
  17860.     and     u.uid = user_id()    /* constrain sysusers uid for use in subquery */
  17861.     and     (    suser_id() = 1     /* User is the System Administrator */
  17862.         or     o.uid = user_id()    /* User created the object */
  17863.             /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  17864.         or     ((select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  17865.              from sysprotects p
  17866.              where p.id = o.id
  17867.                  /*  get rows for public,current user,user's group */
  17868.                  and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  17869.                  /* check for SELECT,EXECUTE privilege */
  17870.              and (action in (193,224)))&1    /* more magic...normalize GRANT */
  17871.             ) = 1     /* final magic...compare Grants    */
  17872.         )
  17873.     order by 2, 3, 5
  17874.     END
  17875. ELSE
  17876.     BEGIN
  17877.     insert into #spprocparamrowset1
  17878.     select
  17879.         PROCEDURE_CATALOG     = db_name(),
  17880.         PROCEDURE_SCHEMA     = user_name(),
  17881.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  17882.         PARAMETER_NAME         = c.name,
  17883.         ORDINAL_POSITION     = convert(smallint,c.colid),
  17884.         PARAMETER_TYPE         = convert(smallint, 1+((c.status/64)&1)),
  17885.         PARAMETER_HASDEFAULT    = convert(tinyint,null),
  17886.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  17887.         IS_NULLABLE        = d.is_nullable,
  17888.         DATA_TYPE        = d.oledb_data_type,
  17889.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  17890.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  17891.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  17892.                         then coalesce(d.column_size,c.length)
  17893.                         else null 
  17894.                         end),
  17895.         CHARACTER_OCTET_LENGTH    = convert(int,
  17896.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  17897.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  17898.                         then coalesce(d.column_size,c.length)
  17899.                         else null 
  17900.                         end),
  17901.         NUMERIC_PRECISION    = convert(smallint,
  17902.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec else null end),
  17903.         NUMERIC_SCALE        = convert(smallint, 
  17904.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  17905.         DESCRIPTION        = convert(varchar(1),null)
  17906.     
  17907.     from
  17908.         syscolumns c,
  17909.         sysobjects o,
  17910.         master.dbo.spt_provider_types d,
  17911.         systypes t,
  17912.         sysusers u
  17913.     where
  17914.         o.type = 'P'                            /* Just Procedures */
  17915.     and    (@procedure_schema = null or @procedure_schema = user_name(o.uid))
  17916.     and    o.id = c.id
  17917.     and    c.type = d.ss_dtype
  17918.     and    c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  17919.     and    coalesce(d.auto_unique_value,0) = 0
  17920.     and    c.usertype = t.usertype
  17921.     and    (@parameter_name is null or @parameter_name = c.name)
  17922.     and     u.uid = user_id()    /* constrain sysusers uid for use in subquery */
  17923.     and     (    suser_id() = 1     /* User is the System Administrator */
  17924.         or     o.uid = user_id()    /* User created the object */
  17925.             /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  17926.         or     ((select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  17927.              from sysprotects p
  17928.              where p.id = o.id
  17929.                  /*  get rows for public,current user,user's group */
  17930.                  and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  17931.                  /* check for SELECT,EXECUTE privilege */
  17932.              and (action in (193,224)))&1    /* more magic...normalize GRANT */
  17933.             ) = 1     /* final magic...compare Grants    */
  17934.         )
  17935.     UNION ALL
  17936.     SELECT           /* return value row*/
  17937.         PROCEDURE_CATALOG     = db_name(),
  17938.         PROCEDURE_SCHEMA     = user_name(),
  17939.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  17940.         PARAMETER_NAME         = convert(varchar(30),'RETURN_VALUE'),
  17941.         ORDINAL_POSITION     = convert(smallint,0),
  17942.         PARAMETER_TYPE         = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  17943.         PARAMETER_HASDEFAULT    = convert(tinyint,null),
  17944.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  17945.         IS_NULLABLE        = convert(bit,0),
  17946.         DATA_TYPE        = convert(smallint, 3 /*DBTYPE_I4*/),
  17947.         CHARACTER_MAXIMUM_LENGTH= convert(int,null),
  17948.         CHARACTER_OCTET_LENGTH    = convert(int,null),
  17949.         NUMERIC_PRECISION    = convert(smallint,null),
  17950.         NUMERIC_SCALE        = convert(smallint,null),
  17951.         DESCRIPTION        = convert(varchar(1),null)
  17952.     from
  17953.         syscolumns c,
  17954.         sysobjects o,
  17955.         sysusers u
  17956.     where
  17957.         o.type = 'P'                        /* Just Procedures */
  17958.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  17959.     and    (@parameter_name is null or @parameter_name = 'RETURN_VALUE')
  17960.     and    c.id =* o.id
  17961.     and     c.colid = 1
  17962.     and     u.uid = user_id()    /* constrain sysusers uid for use in subquery */
  17963.     and     (    suser_id() = 1     /* User is the System Administrator */
  17964.         or     o.uid = user_id()    /* User created the object */
  17965.             /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  17966.         or     ((select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  17967.              from sysprotects p
  17968.              where p.id = o.id
  17969.                  /*  get rows for public,current user,user's group */
  17970.                  and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  17971.                  /* check for SELECT,EXECUTE privilege */
  17972.              and (action in (193,224)))&1    /* more magic...normalize GRANT */
  17973.             ) = 1     /* final magic...compare Grants    */
  17974.         )
  17975.     order by 2, 3, 5
  17976.     END
  17977.  
  17978.     exec @ret = sp_cursoropen @handle output, 'select * from #spprocparamrowset1',
  17979.         @scrollopt output, @ccopt output, @rows output
  17980.  
  17981.     drop table #spprocparamrowset1
  17982.        return isnull(@ret,0)
  17983. go
  17984.  
  17985.  
  17986. create procedure sp_procedure_params_rowset;4
  17987.     (
  17988.        @handle        int output,
  17989.        @scrollopt    int output,
  17990.     @ccopt        int output,
  17991.     @rows        int output,
  17992.     @cmd        varchar(255)
  17993.     )
  17994. as
  17995.     declare @ret int
  17996.     declare @database_name varchar(30)
  17997.  
  17998.     create table #spprocparamrowset2
  17999.         (
  18000.         PROCEDURE_CATALOG     varchar(30) not null,
  18001.         PROCEDURE_SCHEMA     varchar(30) not null,
  18002.         PROCEDURE_NAME         varchar(35) not null,
  18003.         PARAMETER_NAME         varchar(30) not null,
  18004.         ORDINAL_POSITION     smallint not null,
  18005.         PARAMETER_TYPE         smallint null,
  18006.         PARAMETER_HASDEFAULT    tinyint null,
  18007.         PARAMETER_DEFAULT    varchar(255) null,
  18008.         IS_NULLABLE        bit not null,
  18009.         DATA_TYPE        smallint null,
  18010.         CHARACTER_MAXIMUM_LENGTH int null,
  18011.         CHARACTER_OCTET_LENGTH    int null,
  18012.         NUMERIC_PRECISION    smallint null,
  18013.         NUMERIC_SCALE        smallint null,
  18014.         DESCRIPTION        varchar(1) null
  18015.         )
  18016.  
  18017.     declare sysdatabases_cursor insensitive cursor for 
  18018.         select name from master.dbo.sysdatabases order by name
  18019.   
  18020.     set nocount on
  18021.     open sysdatabases_cursor
  18022.       fetch next from sysdatabases_cursor into @database_name 
  18023.     while @@fetch_status = 0
  18024.         begin
  18025.         exec('use '+@database_name+' insert into #spprocparamrowset2 '+@cmd)
  18026.         fetch next from sysdatabases_cursor into @database_name 
  18027.         end
  18028.       close sysdatabases_cursor
  18029.     deallocate sysdatabases_cursor
  18030.  
  18031.     if @scrollopt is null
  18032.         begin
  18033.         select * from #spprocparamrowset2
  18034.         end
  18035.     else
  18036.         begin
  18037.         exec @ret = sp_cursoropen @handle output,
  18038.             'select * from #spprocparamrowset2',
  18039.                 @scrollopt output, @ccopt output, @rows output
  18040.         end
  18041.     drop table #spprocparamrowset2
  18042.     return isnull(@ret,0)
  18043. go
  18044.  
  18045.  
  18046.  
  18047. if (charindex('7.00', @@version) > 0)
  18048.     drop procedure sp_procedure_params_rowset
  18049. else
  18050. begin
  18051.     print ''
  18052.     print ''
  18053.     print 'Warning:'
  18054.     print 'you are installing the stored procedures '
  18055.     print 'on a pre 7.0 SQL Server.'
  18056.     print 'Ignore the following errors.'
  18057. end
  18058. go
  18059.  
  18060. /*    Procedure for 7.0 servers */
  18061. create procedure sp_procedure_params_rowset
  18062.     (
  18063.        @procedure_name        varchar(255),
  18064.     @group_number        int = 1,
  18065.     @procedure_schema     varchar(255) = null,
  18066.     @parameter_name        varchar(255) = null
  18067.     )
  18068. as
  18069.     select
  18070.         PROCEDURE_CATALOG     = db_name(),
  18071.         PROCEDURE_SCHEMA     = user_name(),
  18072.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  18073.         PARAMETER_NAME         = c.name,
  18074.         ORDINAL_POSITION     = convert(smallint,c.colid),
  18075.         PARAMETER_TYPE         = convert(smallint, 1+ColumnProperty(c.id,c.name,'IsOutParam')),
  18076.         PARAMETER_HASDEFAULT    = convert(tinyint, null),
  18077.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  18078.         IS_NULLABLE        = convert(tinyint,d.is_nullable),     /* Until Sphinx bug 24918 is fixed */
  18079.         DATA_TYPE        = convert(smallint,d.oledb_data_type),    /* Until Sphinx bug 24918 is fixed */
  18080.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  18081.                         case 
  18082.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  18083.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  18084.                         then coalesce(d.column_size,c.length)
  18085.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  18086.                         then coalesce(d.column_size,c.length/2)
  18087.                         else null 
  18088.                         end),
  18089.         CHARACTER_OCTET_LENGTH    = convert(int,
  18090.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  18091.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  18092.                             or d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  18093.                         then coalesce(d.column_size,c.length)
  18094.                         else null 
  18095.                         end),
  18096.         NUMERIC_PRECISION    = convert(smallint,
  18097.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec else null end),
  18098.         NUMERIC_SCALE        = convert(smallint, 
  18099.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  18100.         DESCRIPTION        = convert(varchar(1),null)
  18101.     
  18102.     from
  18103.         sysobjects o,
  18104.         syscolumns c,
  18105.         master.dbo.spt_provider_types d,
  18106.         systypes t
  18107.     where
  18108.         o.name = @procedure_name
  18109.     and    o.type = 'P'                            /* Just Procedures */
  18110.     and    (@procedure_schema = null or @procedure_schema = user_name(o.uid))
  18111.     and    o.id = c.id
  18112.     and    c.number = @group_number
  18113.     and    c.xtype = d.ss_dtype
  18114.     and    c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  18115.     and    coalesce(d.auto_unique_value,0) = 0
  18116.     and    c.usertype = t.usertype
  18117.     and    (@parameter_name is null or @parameter_name = c.name)
  18118.     and     permissions(o.id) <> 0
  18119.     UNION ALL
  18120.     SELECT           /* return value row*/
  18121.         PROCEDURE_CATALOG     = db_name(),
  18122.         PROCEDURE_SCHEMA     = user_name(),
  18123.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  18124.         PARAMETER_NAME         = convert(varchar(30),'RETURN_VALUE'),
  18125.         ORDINAL_POSITION     = convert(smallint,0),
  18126.         PARAMETER_TYPE         = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  18127.         PARAMETER_HASDEFAULT    = convert(tinyint, null),
  18128.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  18129.         IS_NULLABLE        = convert(tinyint,0),        /* Until Sphinx bug 24918 is fixed */
  18130.         DATA_TYPE        = convert(smallint, 3 /*DBTYPE_I4*/),
  18131.         CHARACTER_MAXIMUM_LENGTH= convert(int,null),
  18132.         CHARACTER_OCTET_LENGTH    = convert(int,null),
  18133.         NUMERIC_PRECISION    = convert(smallint,null),
  18134.         NUMERIC_SCALE        = convert(smallint,null),
  18135.         DESCRIPTION        = convert(varchar(1),null)
  18136.     from
  18137.         sysobjects o left join syscolumns c on o.id = c.id and c.colid = 1
  18138.     where
  18139.         o.name = @procedure_name
  18140.     and    o.type = 'P'                        /* Just Procedures */
  18141.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  18142.     and    (@parameter_name is null or @parameter_name = 'RETURN_VALUE')
  18143.     and    permissions(o.id) <> 1
  18144.     order by 2, 3, 5
  18145. go
  18146. create procedure sp_procedure_params_rowset;2
  18147.     (
  18148.     @procedure_schema     varchar(255) = null,
  18149.     @parameter_name        varchar(255) = null
  18150.     )
  18151. as
  18152.     select
  18153.         PROCEDURE_CATALOG     = db_name(),
  18154.         PROCEDURE_SCHEMA     = user_name(),
  18155.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  18156.         PARAMETER_NAME         = c.name,
  18157.         ORDINAL_POSITION     = convert(smallint,c.colid),
  18158.         PARAMETER_TYPE         = convert(smallint, 1+ColumnProperty(c.id,c.name,'IsOutParam')),
  18159.         PARAMETER_HASDEFAULT    = convert(tinyint, null),
  18160.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  18161.         IS_NULLABLE        = convert(tinyint,d.is_nullable),     /* Until Sphinx bug 24918 is fixed */
  18162.         DATA_TYPE        = convert(smallint,d.oledb_data_type),    /* Until Sphinx bug 24918 is fixed */
  18163.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  18164.                         case 
  18165.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  18166.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  18167.                         then coalesce(d.column_size,c.length)
  18168.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  18169.                         then coalesce(d.column_size,c.length/2)
  18170.                         else null 
  18171.                         end),
  18172.         CHARACTER_OCTET_LENGTH    = convert(int,
  18173.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  18174.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  18175.                             or d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  18176.                         then coalesce(d.column_size,c.length)
  18177.                         else null 
  18178.                         end),
  18179.         NUMERIC_PRECISION    = convert(smallint,
  18180.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec else null end),
  18181.         NUMERIC_SCALE        = convert(smallint, 
  18182.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  18183.         DESCRIPTION        = convert(varchar(1),null)
  18184.     
  18185.     from
  18186.         sysobjects o,
  18187.         syscolumns c,
  18188.         master.dbo.spt_provider_types d,
  18189.         systypes t
  18190.     where
  18191.         o.type = 'P'                            /* Just Procedures */
  18192.     and    (@procedure_schema = null or @procedure_schema = user_name(o.uid))
  18193.     and    o.id = c.id
  18194.     and    c.xtype = d.ss_dtype
  18195.     and    c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  18196.     and    coalesce(d.auto_unique_value,0) = 0
  18197.     and    c.usertype = t.usertype
  18198.     and    (@parameter_name is null or @parameter_name = c.name)
  18199.     and     permissions(o.id) <> 0
  18200.     UNION ALL
  18201.     SELECT           /* return value row*/
  18202.         PROCEDURE_CATALOG     = db_name(),
  18203.         PROCEDURE_SCHEMA     = user_name(),
  18204.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  18205.         PARAMETER_NAME         = convert(varchar(30),'RETURN_VALUE'),
  18206.         ORDINAL_POSITION     = convert(smallint,0),
  18207.         PARAMETER_TYPE         = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  18208.         PARAMETER_HASDEFAULT    = convert(tinyint, null),
  18209.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  18210.         IS_NULLABLE        = convert(tinyint,0),        /* Until Sphinx bug 24918 is fixed */
  18211.         DATA_TYPE        = convert(smallint, 3 /*DBTYPE_I4*/),
  18212.         CHARACTER_MAXIMUM_LENGTH= convert(int,null),
  18213.         CHARACTER_OCTET_LENGTH    = convert(int,null),
  18214.         NUMERIC_PRECISION    = convert(smallint,null),
  18215.         NUMERIC_SCALE        = convert(smallint,null),
  18216.         DESCRIPTION        = convert(varchar(1),null)
  18217.     from
  18218.         sysobjects o left join syscolumns c on o.id = c.id and c.colid = 1
  18219.     where
  18220.         o.type = 'P'                        /* Just Procedures */
  18221.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  18222.     and    (@parameter_name is null or @parameter_name = 'RETURN_VALUE')
  18223.     and    permissions(o.id) <> 1
  18224.     order by 2, 3, 5
  18225. go
  18226. create procedure sp_procedure_params_rowset;3
  18227.     (
  18228.        @procedure_name        varchar(255) = null,
  18229.     @group_number        int = null,
  18230.     @procedure_schema     varchar(255) = null,
  18231.     @parameter_name        varchar(255) = null
  18232.     )
  18233. as
  18234. IF @procedure_name is not null 
  18235.     BEGIN
  18236.     select
  18237.         PROCEDURE_CATALOG     = db_name(),
  18238.         PROCEDURE_SCHEMA     = user_name(),
  18239.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  18240.         PARAMETER_NAME         = c.name,
  18241.         ORDINAL_POSITION     = convert(smallint,c.colid),
  18242.         PARAMETER_TYPE         = convert(smallint, 1+ColumnProperty(c.id,c.name,'IsOutParam')),
  18243.         PARAMETER_HASDEFAULT    = convert(tinyint, null),
  18244.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  18245.         IS_NULLABLE        = convert(tinyint,d.is_nullable),     /* Until Sphinx bug 24918 is fixed */
  18246.         DATA_TYPE        = convert(smallint,d.oledb_data_type),    /* Until Sphinx bug 24918 is fixed */
  18247.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  18248.                         case 
  18249.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  18250.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  18251.                         then coalesce(d.column_size,c.length)
  18252.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  18253.                         then coalesce(d.column_size,c.length/2)
  18254.                         else null 
  18255.                         end),
  18256.         CHARACTER_OCTET_LENGTH    = convert(int,
  18257.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  18258.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  18259.                             or d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  18260.                         then coalesce(d.column_size,c.length*2)
  18261.                         else null 
  18262.                         end),
  18263.         NUMERIC_PRECISION    = convert(smallint,
  18264.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec else null end),
  18265.         NUMERIC_SCALE        = convert(smallint, 
  18266.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  18267.         DESCRIPTION        = convert(varchar(1),null)
  18268.     
  18269.     from
  18270.         sysobjects o,
  18271.         syscolumns c,
  18272.         master.dbo.spt_provider_types d,
  18273.         systypes t
  18274.     where
  18275.         o.name = @procedure_name
  18276.     and    o.type = 'P'                            /* Just Procedures */
  18277.     and    (@procedure_schema = null or @procedure_schema = user_name(o.uid))
  18278.     and    o.id = c.id
  18279.     and    c.number = @group_number
  18280.     and    c.xtype = d.ss_dtype
  18281.     and    c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  18282.     and    coalesce(d.auto_unique_value,0) = 0
  18283.     and    c.usertype = t.usertype
  18284.     and    (@parameter_name is null or @parameter_name = c.name)
  18285.     and     permissions(o.id) <> 0
  18286.     UNION ALL
  18287.     SELECT           /* return value row*/
  18288.         PROCEDURE_CATALOG     = db_name(),
  18289.         PROCEDURE_SCHEMA     = user_name(),
  18290.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  18291.         PARAMETER_NAME         = convert(varchar(30),'RETURN_VALUE'),
  18292.         ORDINAL_POSITION     = convert(smallint,0),
  18293.         PARAMETER_TYPE         = convert(smallint,4 /*DBPARAMTYPE_RETURNVALUE*/),
  18294.         PARAMETER_HASDEFAULT    = convert(tinyint, null),
  18295.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  18296.         IS_NULLABLE        = convert(tinyint,0),        /* Until Sphinx bug 24918 is fixed */
  18297.         DATA_TYPE        = convert(smallint,3 /*DBTYPE_I4*/),
  18298.         CHARACTER_MAXIMUM_LENGTH= convert(int,null),
  18299.         CHARACTER_OCTET_LENGTH    = convert(int,null),
  18300.         NUMERIC_PRECISION    = convert(smallint,null),
  18301.         NUMERIC_SCALE        = convert(smallint,null),
  18302.         DESCRIPTION        = convert(varchar(1),null)
  18303.     from
  18304.         sysobjects o left join syscolumns c on o.id = c.id and c.colid = 1
  18305.     where
  18306.         o.name = @procedure_name
  18307.     and    o.type = 'P'                        /* Just Procedures */
  18308.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  18309.     and    (@parameter_name is null or @parameter_name = 'RETURN_VALUE')
  18310.     and    permissions(o.id) <> 1
  18311.     END
  18312. ELSE
  18313.     BEGIN
  18314.     select
  18315.         PROCEDURE_CATALOG     = db_name(),
  18316.         PROCEDURE_SCHEMA     = user_name(),
  18317.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  18318.         PARAMETER_NAME         = c.name,
  18319.         ORDINAL_POSITION     = convert(smallint,c.colid),
  18320.         PARAMETER_TYPE         = convert(smallint, 1+ColumnProperty(c.id,c.name,'IsOutParam')),
  18321.         PARAMETER_HASDEFAULT    = convert(tinyint, null),
  18322.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  18323.         IS_NULLABLE        = convert(tinyint,d.is_nullable),     /* Until Sphinx bug 24918 is fixed */
  18324.         DATA_TYPE        = convert(smallint,d.oledb_data_type),    /* Until Sphinx bug 24918 is fixed */
  18325.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  18326.                         case 
  18327.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  18328.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  18329.                         then coalesce(d.column_size,c.length)
  18330.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  18331.                         then coalesce(d.column_size,c.length/2)
  18332.                         else null 
  18333.                         end),
  18334.         CHARACTER_OCTET_LENGTH    = convert(int,
  18335.                         case 
  18336.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  18337.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  18338.                             or d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  18339.                         then coalesce(d.column_size,c.length)
  18340.                         else null 
  18341.                         end),
  18342.         NUMERIC_PRECISION    = convert(smallint,
  18343.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec else null end),
  18344.         NUMERIC_SCALE        = convert(smallint, 
  18345.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  18346.         DESCRIPTION        = convert(varchar(1),null)
  18347.     
  18348.     from
  18349.         sysobjects o,
  18350.         syscolumns c,
  18351.         master.dbo.spt_provider_types d,
  18352.         systypes t
  18353.     where
  18354.         o.type = 'P'                            /* Just Procedures */
  18355.     and    (@procedure_schema = null or @procedure_schema = user_name(o.uid))
  18356.     and    o.id = c.id
  18357.     and    c.xtype = d.ss_dtype
  18358.     and    c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  18359.     and    coalesce(d.auto_unique_value,0) = 0
  18360.     and    c.usertype = t.usertype
  18361.     and    (@parameter_name is null or @parameter_name = c.name)
  18362.     and     permissions(o.id) <> 0
  18363.     UNION ALL
  18364.     SELECT           /* return value row*/
  18365.         PROCEDURE_CATALOG     = db_name(),
  18366.         PROCEDURE_SCHEMA     = user_name(),
  18367.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  18368.         PARAMETER_NAME         = convert(varchar(30),'RETURN_VALUE'),
  18369.         ORDINAL_POSITION     = convert(smallint,0),
  18370.         PARAMETER_TYPE         = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  18371.         PARAMETER_HASDEFAULT    = convert(tinyint, null),
  18372.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  18373.         IS_NULLABLE        = convert(tinyint,0),        /* Until Sphinx bug 24918 is fixed */
  18374.         DATA_TYPE        = convert(smallint, 3 /*DBTYPE_I4*/),
  18375.         CHARACTER_MAXIMUM_LENGTH= convert(int,null),
  18376.         CHARACTER_OCTET_LENGTH    = convert(int,null),
  18377.         NUMERIC_PRECISION    = convert(smallint,null),
  18378.         NUMERIC_SCALE        = convert(smallint,null),
  18379.         DESCRIPTION        = convert(varchar(1),null)
  18380.     from
  18381.         sysobjects o left join syscolumns c on o.id = c.id and c.colid = 1
  18382.     where
  18383.         o.type = 'P'                        /* Just Procedures */
  18384.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  18385.     and    (@parameter_name is null or @parameter_name = 'RETURN_VALUE')
  18386.     and    permissions(o.id) <> 1
  18387.     END
  18388. go
  18389.  
  18390.  
  18391.  
  18392. create procedure sp_procedure_params_rowset;4
  18393.     (
  18394.        @handle        int output,
  18395.        @scrollopt    int output,
  18396.     @ccopt        int output,
  18397.     @rows        int output,
  18398.     @cmd        varchar(255)
  18399.     )
  18400. as
  18401.     declare @ret int
  18402.     declare @database_name varchar(30)
  18403.  
  18404.     create table #spsprocparamrowset
  18405.         (
  18406.         PROCEDURE_CATALOG     varchar(30) not null,
  18407.         PROCEDURE_SCHEMA     varchar(30) not null,
  18408.         PROCEDURE_NAME         varchar(35) not null,
  18409.         PARAMETER_NAME         varchar(30) not null,
  18410.         ORDINAL_POSITION     smallint not null,
  18411.         PARAMETER_TYPE         smallint null,
  18412.         PARAMETER_HASDEFAULT    tinyint null,
  18413.         PARAMETER_DEFAULT    varchar(255) null,
  18414.         IS_NULLABLE        bit not null,
  18415.         DATA_TYPE        smallint null,
  18416.         CHARACTER_MAXIMUM_LENGTH int null,
  18417.         CHARACTER_OCTET_LENGTH    int null,
  18418.         NUMERIC_PRECISION    smallint null,
  18419.         NUMERIC_SCALE        smallint null,
  18420.         DESCRIPTION        varchar(1) null
  18421.         )
  18422.  
  18423.     declare sysdatabases_cursor insensitive cursor for 
  18424.         select name from master.dbo.sysdatabases
  18425.   
  18426.     set nocount on
  18427.     open sysdatabases_cursor
  18428.       fetch next from sysdatabases_cursor into @database_name 
  18429.     while @@fetch_status = 0
  18430.         begin
  18431.         exec('use '+@database_name+' insert into #spsprocparamrowset '+@cmd)
  18432.         fetch next from sysdatabases_cursor into @database_name 
  18433.         end
  18434.       close sysdatabases_cursor
  18435.     deallocate sysdatabases_cursor
  18436.  
  18437.     if @scrollopt is null
  18438.         begin
  18439.         select * from #spsprocparamrowset order by 1,2,3
  18440.         end
  18441.     else
  18442.         begin
  18443.         exec @ret = sp_cursoropen @handle output,
  18444.             'select * from #spsprocparamrowset order by 1,2,3,5',
  18445.                 @scrollopt output, @ccopt output, @rows output
  18446.         end
  18447.     drop table #spsprocparamrowset
  18448.     return isnull(@ret,0)
  18449. go
  18450.  
  18451.  
  18452. grant execute on sp_procedure_params_rowset to public
  18453. go
  18454.  
  18455. dump tran master with no_log
  18456. go
  18457.  
  18458. print 'creating sp_procedures_rowset'
  18459. go
  18460.  
  18461.  
  18462. /* pre 7.00 version */
  18463. create procedure sp_procedures_rowset
  18464.     (
  18465.     @procedure_name        varchar(255), 
  18466.     @group_number        int = 1,
  18467.     @procedure_schema    varchar(255) = null
  18468.     )        
  18469. as
  18470.     select
  18471.         PROCEDURE_CATALOG    = db_name(),
  18472.         PROCEDURE_SCHEMA    = user_name(o.uid),
  18473.         PROCEDURE_NAME        = convert(varchar(35),o.name +';'+ ltrim(str(p.number,5))),
  18474.         PROCEDURE_TYPE        = convert(smallint, 3 /*DB_PT_FUNCTION*/),
  18475.         PROCEDURE_DEFINITION    = convert(varchar(1),null),
  18476.         DESCRIPTION        = convert(varchar(1),null)
  18477.     from     
  18478.         sysobjects o, 
  18479.         syscomments p,
  18480.         sysusers u
  18481.     where
  18482.             o.name = @procedure_name
  18483.         and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  18484.     and     o.type = 'P'        /* Object type of Procedure */
  18485.     and     p.colid = 1
  18486.     and     p.id = o.id
  18487.     and    p.number = @group_number
  18488.     and     u.uid = user_id()    /* constrain sysusers uid for use in subquery */
  18489.     and     (    suser_id() = 1     /* User is the System Administrator */
  18490.         or     o.uid = user_id()    /* User created the object */
  18491.             /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  18492.         or     ((select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  18493.              from sysprotects p
  18494.              where p.id = o.id
  18495.                  /*  get rows for public,current user,user's group */
  18496.                  and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  18497.                  /* check for SELECT,EXECUTE privilege */
  18498.              and (action in (193,224)))&1    /* more magic...normalize GRANT */
  18499.             ) = 1     /* final magic...compare Grants    */
  18500.         )
  18501.     order by 2, 3
  18502. go
  18503. create procedure sp_procedures_rowset;2
  18504.     (
  18505.     @procedure_schema    varchar(255) = null
  18506.     )        
  18507. as
  18508.     select
  18509.         PROCEDURE_CATALOG    = db_name(),
  18510.         PROCEDURE_SCHEMA    = user_name(o.uid),
  18511.         PROCEDURE_NAME        = convert(varchar(35),o.name +';'+ ltrim(str(p.number,5))),
  18512.         PROCEDURE_TYPE        = convert(smallint, 3 /*DB_PT_FUNCTION*/),
  18513.         PROCEDURE_DEFINITION    = convert(varchar(1),null),
  18514.         DESCRIPTION        = convert(varchar(1),null)
  18515.     from     
  18516.         sysobjects o, 
  18517.         syscomments p,
  18518.         sysusers u
  18519.     where
  18520.             (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  18521.     and     o.type = 'P'        /* Object type of Procedure */
  18522.     and     p.colid = 1
  18523.     and     p.id = o.id
  18524.     and     u.uid = user_id()    /* constrain sysusers uid for use in subquery */
  18525.     and     (    suser_id() = 1     /* User is the System Administrator */
  18526.         or     o.uid = user_id()    /* User created the object */
  18527.             /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  18528.         or     ((select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  18529.              from sysprotects p
  18530.              where p.id = o.id
  18531.                  /*  get rows for public,current user,user's group */
  18532.                  and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  18533.                  /* check for SELECT,EXECUTE privilege */
  18534.              and (action in (193,224)))&1    /* more magic...normalize GRANT */
  18535.             ) = 1     /* final magic...compare Grants    */
  18536.         )
  18537.     order by 2, 3
  18538. go
  18539. create procedure sp_procedures_rowset;3
  18540.     (
  18541.     @procedure_name        varchar(255) = null, 
  18542.     @group_number        int = null,
  18543.     @procedure_schema    varchar(255) = null
  18544.     )        
  18545. as
  18546. IF @procedure_name is not null
  18547.     BEGIN
  18548.     select
  18549.         PROCEDURE_CATALOG    = db_name(),
  18550.         PROCEDURE_SCHEMA    = user_name(o.uid),
  18551.         PROCEDURE_NAME        = convert(varchar(35),o.name +';'+ ltrim(str(p.number,5))),
  18552.         PROCEDURE_TYPE        = convert(smallint, 3 /*DB_PT_FUNCTION*/),
  18553.         PROCEDURE_DEFINITION    = convert(varchar(1),null),
  18554.         DESCRIPTION        = convert(varchar(1),null)
  18555.     from     
  18556.         sysobjects o, 
  18557.         syscomments p,
  18558.         sysusers u
  18559.     where
  18560.             o.name = @procedure_name
  18561.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  18562.     and     o.type = 'P'        /* Object type of Procedure */
  18563.     and     p.colid = 1
  18564.     and     p.id = o.id
  18565.     and    p.number = @group_number
  18566.     and     u.uid = user_id()    /* constrain sysusers uid for use in subquery */
  18567.     and     (    suser_id() = 1     /* User is the System Administrator */
  18568.         or     o.uid = user_id()    /* User created the object */
  18569.             /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  18570.         or     ((select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  18571.              from sysprotects p
  18572.              where p.id = o.id
  18573.                  /*  get rows for public,current user,user's group */
  18574.                  and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  18575.                  /* check for SELECT,EXECUTE privilege */
  18576.              and (action in (193,224)))&1    /* more magic...normalize GRANT */
  18577.             ) = 1     /* final magic...compare Grants    */
  18578.         )    
  18579.     END
  18580. ELSE
  18581.     BEGIN
  18582.     select
  18583.         PROCEDURE_CATALOG    = db_name(),
  18584.         PROCEDURE_SCHEMA    = user_name(o.uid),
  18585.         PROCEDURE_NAME        = convert(varchar(35),o.name +';'+ ltrim(str(p.number,5))),
  18586.         PROCEDURE_TYPE        = convert(smallint, 3 /*DB_PT_FUNCTION*/),
  18587.         PROCEDURE_DEFINITION    = convert(varchar(1),null),
  18588.         DESCRIPTION        = convert(varchar(1),null)
  18589.     from     
  18590.         sysobjects o, 
  18591.         syscomments p,
  18592.         sysusers u
  18593.     where
  18594.             (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  18595.     and     o.type = 'P'        /* Object type of Procedure */
  18596.     and     p.colid = 1
  18597.     and     p.id = o.id
  18598.     and     u.uid = user_id()    /* constrain sysusers uid for use in subquery */
  18599.     and     (    suser_id() = 1     /* User is the System Administrator */
  18600.         or     o.uid = user_id()    /* User created the object */
  18601.             /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  18602.         or     ((select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  18603.              from sysprotects p
  18604.              where p.id = o.id
  18605.                  /*  get rows for public,current user,user's group */
  18606.                  and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  18607.                  /* check for SELECT,EXECUTE privilege */
  18608.              and (action in (193,224)))&1    /* more magic...normalize GRANT */
  18609.             ) = 1     /* final magic...compare Grants    */
  18610.         )    
  18611.     END
  18612. go
  18613.  
  18614.  
  18615. if (charindex('7.00', @@version) > 0)
  18616.     drop procedure sp_procedures_rowset
  18617. else
  18618. begin
  18619.     print ''
  18620.     print ''
  18621.     print 'Warning:'
  18622.     print 'you are installing the stored procedures '
  18623.     print 'on a pre 7.0 SQL Server.'
  18624.     print 'Ignore the following errors.'
  18625. end
  18626. go
  18627.  
  18628. /* 7.00 version */
  18629. create procedure sp_procedures_rowset
  18630.     (
  18631.     @procedure_name        varchar(255), 
  18632.     @group_number        int = 1,
  18633.     @procedure_schema    varchar(255) = null
  18634.     )        
  18635. as
  18636.     select
  18637.         PROCEDURE_CATALOG    = db_name(),
  18638.         PROCEDURE_SCHEMA    = user_name(o.uid),
  18639.         PROCEDURE_NAME        = convert(varchar(35),o.name +';'+ ltrim(str(p.number,5))),
  18640.         PROCEDURE_TYPE        = convert(smallint, 3 /*DB_PT_FUNCTION*/),
  18641.         PROCEDURE_DEFINITION    = convert(varchar(1),null),
  18642.         DESCRIPTION        = convert(varchar(1),null)
  18643.     from     
  18644.         sysobjects o, 
  18645.         syscomments p
  18646.     where
  18647.         permissions(o.id) <> 0
  18648.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  18649.     and     o.type = 'P'        /* Object type of Procedure */
  18650.     and     p.colid = 1
  18651.     and     p.id = o.id
  18652.     and    p.number = @group_number
  18653.     order by 2, 3
  18654. go
  18655. create procedure sp_procedures_rowset;2
  18656.     (
  18657.     @procedure_schema    varchar(255) = null
  18658.     )        
  18659. as
  18660.     select
  18661.         PROCEDURE_CATALOG    = db_name(),
  18662.         PROCEDURE_SCHEMA    = user_name(o.uid),
  18663.         PROCEDURE_NAME        = convert(varchar(35),o.name +';'+ ltrim(str(p.number,5))),
  18664.         PROCEDURE_TYPE        = convert(smallint, 3 /*DB_PT_FUNCTION*/),
  18665.         PROCEDURE_DEFINITION    = convert(varchar(1),null),
  18666.         DESCRIPTION        = convert(varchar(1),null)
  18667.     from     sysobjects o, syscomments p
  18668.     where
  18669.         permissions(o.id) <> 0
  18670.     and        (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  18671.     and     o.type = 'P'        /* Object type of Procedure */
  18672.     and p.colid = 1
  18673.     and p.id = o.id
  18674.     order by 2, 3
  18675. go
  18676. create procedure sp_procedures_rowset;3
  18677.     (
  18678.     @procedure_name        varchar(255) = null, 
  18679.     @group_number        int = null,
  18680.     @procedure_schema    varchar(255) = null
  18681.     )        
  18682. as
  18683. IF @procedure_name is not null
  18684.     BEGIN
  18685.     select
  18686.         PROCEDURE_CATALOG    = db_name(),
  18687.         PROCEDURE_SCHEMA    = user_name(o.uid),
  18688.         PROCEDURE_NAME        = convert(varchar(35),o.name +';'+ ltrim(str(p.number,5))),
  18689.         PROCEDURE_TYPE        = convert(smallint, 3 /*DB_PT_FUNCTION*/),
  18690.         PROCEDURE_DEFINITION    = convert(varchar(1),null),
  18691.         DESCRIPTION        = convert(varchar(1),null)
  18692.     from     
  18693.         sysobjects o, 
  18694.         syscomments p
  18695.     where
  18696.         permissions(o.id) <> 0
  18697.     and    o.name = @procedure_name
  18698.      and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  18699.     and     o.type = 'P'        /* Object type of Procedure */
  18700.     and     p.colid = 1
  18701.     and     p.id = o.id
  18702.     and    p.number = @group_number
  18703.     END
  18704. ELSE
  18705.     BEGIN
  18706.     select
  18707.         PROCEDURE_CATALOG    = db_name(),
  18708.         PROCEDURE_SCHEMA    = user_name(o.uid),
  18709.         PROCEDURE_NAME        = convert(varchar(35),o.name +';'+ ltrim(str(p.number,5))),
  18710.         PROCEDURE_TYPE        = convert(smallint, 3 /*DB_PT_FUNCTION*/),
  18711.         PROCEDURE_DEFINITION    = convert(varchar(1),null),
  18712.         DESCRIPTION        = convert(varchar(1),null)
  18713.     from     
  18714.         sysobjects o, 
  18715.         syscomments p
  18716.     where
  18717.         permissions(o.id) <> 0
  18718.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  18719.     and    o.type = 'P'        /* Object type of Procedure */
  18720.     and     p.colid = 1
  18721.         and     p.id = o.id
  18722.     END
  18723. go
  18724.  
  18725.  
  18726. create procedure sp_procedures_rowset;4
  18727.     (
  18728.  
  18729.        @handle        int output,
  18730.        @scrollopt    int output,
  18731.     @ccopt        int output,
  18732.     @rows        int output,
  18733.     @cmd        varchar(255)
  18734.     )
  18735. as
  18736.     declare @ret int
  18737.     declare @database_name varchar(30)
  18738.  
  18739.     create table #spsprocrowset
  18740.         (
  18741.         PROCEDURE_CATALOG        varchar(30) not null,
  18742.         PROCEDURE_SCHEMA        varchar(30) not null,
  18743.         PROCEDURE_NAME            varchar(35) not null,
  18744.         PROCEDURE_TYPE            smallint not null,
  18745.         PROCEDURE_DEFINITION        varchar(1) null,
  18746.         DESCRIPTION            varchar(1) null
  18747.         )
  18748.  
  18749.     declare sysdatabases_cursor insensitive cursor for 
  18750.         select name from master.dbo.sysdatabases
  18751.   
  18752.     set nocount on
  18753.     open sysdatabases_cursor
  18754.       fetch next from sysdatabases_cursor into @database_name 
  18755.     while @@fetch_status = 0
  18756.         begin
  18757.         exec('use '+@database_name+' insert into #spsprocrowset '+@cmd)
  18758.         fetch next from sysdatabases_cursor into @database_name 
  18759.         end
  18760.       close sysdatabases_cursor
  18761.     deallocate sysdatabases_cursor
  18762.  
  18763.     if @scrollopt is null
  18764.         begin
  18765.         select * from #spsprocrowset order by 1,2,3
  18766.         end
  18767.     else
  18768.         begin
  18769.         exec @ret = sp_cursoropen @handle output,
  18770.             'select * from #spsprocrowset order by 1,2,3',
  18771.                 @scrollopt output, @ccopt output, @rows output
  18772.         end
  18773.     drop table #spsprocrowset
  18774.     return isnull(@ret,0)
  18775. go
  18776.  
  18777. grant execute on sp_procedures_rowset to public
  18778. go
  18779.  
  18780. dump tran master with no_log
  18781. go
  18782.  
  18783. print 'creating sp_schemata_rowset'
  18784. go
  18785.  
  18786. create procedure sp_schemata_rowset
  18787.     (
  18788.     @schema_name    varchar(90) = null, 
  18789.     @schema_owner    varchar(90) = null
  18790.     )        
  18791. as
  18792.     select    distinct
  18793.         CATALOG_NAME            = db_name(),
  18794.         SCHEMA_NAME            = user_name(o.uid),    
  18795.         SCHEMA_OWNER            = user_name(o.uid),    
  18796.         DEFAULT_CHARACTER_SET_CATALOG    = convert(varchar(30),'master'),
  18797.         DEFAULT_CHARACTER_SET_SCHEMA    = convert(varchar(30),'dbo'),
  18798.         DEFAULT_CHARACTER_SET_NAME    = convert(varchar(30),a_cha.name)
  18799.     from    
  18800.         sysobjects o,
  18801.         master.dbo.sysconfigures    cfg,
  18802.         master.dbo.syscharsets        a_cha, /* charset/1001, not sortorder */
  18803.         master.dbo.syscharsets        b_cha  /* sortorder/2001, not charset */
  18804.     where   
  18805.         (@schema_name is null or @schema_name = user_name(o.uid))
  18806.     and     (@schema_owner is null or @schema_owner = user_name(o.uid))
  18807.     and    a_cha.type = 1001 /* type is charset */
  18808.     and     b_cha.type = 2001 /* type is sortorder */
  18809.     and     a_cha.id = b_cha.csid
  18810.     and     b_cha.id = cfg.value
  18811.     order by 2
  18812. go
  18813. create procedure sp_schemata_rowset;2
  18814.     (
  18815.  
  18816.        @handle        int output,
  18817.        @scrollopt    int output,
  18818.     @ccopt        int output,
  18819.     @rows        int output,
  18820.     @cmd        varchar(255)
  18821.     )
  18822. as
  18823.     declare @ret int
  18824.     declare @database_name varchar(30)
  18825.  
  18826.     create table #spschematarowset
  18827.         (
  18828.         CATALOG_NAME            varchar(30) not null,
  18829.         SCHEMA_NAME            varchar(30) not null,
  18830.         SCHEMA_OWNER            varchar(30) not null,
  18831.         DEFAULT_CHARACTER_SET_CATALOG    varchar(30) null,
  18832.         DEFAULT_CHARACTER_SET_SCHEMA    varchar(30) null,
  18833.         DEFAULT_CHARACTER_SET_NAME    varchar(30) null
  18834.         )
  18835.  
  18836.     declare sysdatabases_cursor insensitive cursor for 
  18837.         select name from master.dbo.sysdatabases
  18838.   
  18839.     set nocount on
  18840.     open sysdatabases_cursor
  18841.       fetch next from sysdatabases_cursor into @database_name 
  18842.     while @@fetch_status = 0
  18843.         begin
  18844.         exec('use '+@database_name+' insert into #spschematarowset '+@cmd)
  18845.         fetch next from sysdatabases_cursor into @database_name 
  18846.         end
  18847.       close sysdatabases_cursor
  18848.     deallocate sysdatabases_cursor
  18849.  
  18850.     if @scrollopt is null
  18851.         begin
  18852.         select * from #spschematarowset order by 1,2,3
  18853.         end
  18854.     else
  18855.         begin
  18856.         exec @ret = sp_cursoropen @handle output,
  18857.             'select * from #spschematarowset order by 1,2,3',
  18858.                 @scrollopt output, @ccopt output, @rows output
  18859.         end
  18860.     drop table #spschematarowset 
  18861.     return isnull(@ret,0)
  18862. go
  18863.  
  18864. grant execute on sp_schemata_rowset to public
  18865. go
  18866.  
  18867. dump tran master with no_log
  18868. go
  18869.  
  18870. print 'creating sp_statistics_rowset'
  18871. go
  18872.  
  18873.  
  18874. /*    6.0 and 6.5 version */
  18875. create procedure sp_statistics_rowset
  18876.     (
  18877.     @table_name    varchar(255),
  18878.     @table_schema    varchar(255) = null     
  18879.     )
  18880. as
  18881.     select    db_name()                as TABLE_CATALOG,        
  18882.         user_name(o.uid)            as TABLE_SCHEMA,
  18883.         o.name                    as TABLE_NAME,
  18884.         x.rows                    as CARDINALITY
  18885.     from    sysobjects o, sysindexes x, sysusers u
  18886.     where    o.type in ('U')
  18887.     and     o.name = @table_name
  18888.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  18889.     and     x.id = o.id
  18890.     and     x.indid in (0,1)    /*If there are no indexes then table stats are in a row with indid =0 */
  18891.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  18892.     and     (
  18893.         suser_id() = 1     /* User is the System Administrator */
  18894.         or o.uid = user_id()     /* User created the object */
  18895.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  18896.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  18897.             from sysprotects p
  18898.             where p.id = o.id
  18899.             /* get rows for public,current user,user's group */
  18900.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  18901.             /* check for SELECT,EXECUTE privilege */
  18902.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  18903.             ) = 1    /* final magic...compare Grants      */
  18904.            )
  18905.     order by 2, 3
  18906. go
  18907. create procedure sp_statistics_rowset;2
  18908.     (
  18909.     @table_schema    varchar(255) = null     
  18910.     )
  18911. as
  18912.     select    db_name()                as TABLE_CATALOG,        
  18913.         user_name(o.uid)            as TABLE_SCHEMA,
  18914.         o.name                    as TABLE_NAME,
  18915.         x.rows                    as CARDINALITY
  18916.     from    sysobjects o, sysindexes x, sysusers u
  18917.     where    o.type in ('U')
  18918.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  18919.     and     x.id = o.id
  18920.     and     x.indid in (0,1)    /*If there are no indexes then table stats are in a row with indid =0 */
  18921.     and     (
  18922.         suser_id() = 1         /* User is the System Administrator */
  18923.         or o.uid = user_id()    /* User created the object */
  18924.         )        
  18925.     order by 2, 3
  18926. go
  18927. create procedure sp_statistics_rowset;3
  18928.     (
  18929.     @table_name    varchar(255) = null,
  18930.     @table_schema    varchar(255) = null     
  18931.     )
  18932. as
  18933. IF @table_name is not null
  18934.     BEGIN
  18935.     select    db_name()                as TABLE_CATALOG,        
  18936.         user_name(o.uid)            as TABLE_SCHEMA,
  18937.         o.name                    as TABLE_NAME,
  18938.         x.rows                    as CARDINALITY
  18939.     from    sysobjects o, sysindexes x, sysusers u
  18940.     where    o.type in ('U')
  18941.     and     o.name = @table_name
  18942.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  18943.     and     x.id = o.id
  18944.     and     x.indid in (0,1)    /*If there are no indexes then table stats are in a row with indid =0 */
  18945.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  18946.     and     (
  18947.         suser_id() = 1     /* User is the System Administrator */
  18948.         or o.uid = user_id()     /* User created the object */
  18949.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  18950.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  18951.             from sysprotects p
  18952.             where p.id = o.id
  18953.             /* get rows for public,current user,user's group */
  18954.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  18955.             /* check for SELECT,EXECUTE privilege */
  18956.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  18957.             ) = 1    /* final magic...compare Grants      */
  18958.            )
  18959.     END
  18960. ELSE
  18961.     BEGIN
  18962.     select    db_name()                as TABLE_CATALOG,        
  18963.         user_name(o.uid)            as TABLE_SCHEMA,
  18964.         o.name                    as TABLE_NAME,
  18965.         x.rows                    as CARDINALITY
  18966.     from    sysobjects o, sysindexes x, sysusers u
  18967.     where    o.type in ('U')
  18968.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  18969.     and     x.id = o.id
  18970.     and     x.indid in (0,1)    /*If there are no indexes then table stats are in a row with indid =0 */
  18971.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  18972.     and     (
  18973.         suser_id() = 1     /* User is the System Administrator */
  18974.         or o.uid = user_id()     /* User created the object */
  18975.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  18976.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  18977.             from sysprotects p
  18978.             where p.id = o.id
  18979.             /* get rows for public,current user,user's group */
  18980.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  18981.             /* check for SELECT,EXECUTE privilege */
  18982.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  18983.             ) = 1    /* final magic...compare Grants      */
  18984.            )
  18985.     END
  18986. go
  18987.  
  18988. if (charindex('7.00', @@version) > 0)
  18989.     drop procedure sp_statistics_rowset
  18990. else
  18991. begin
  18992.     print ''
  18993.     print ''
  18994.     print 'Warning:'
  18995.     print 'you are installing the stored procedures '
  18996.     print 'on a pre 7.0 SQL Server.'
  18997.     print 'Ignore the following errors.'
  18998. end
  18999. go
  19000.  
  19001. /*     7.0 version */
  19002. create procedure sp_statistics_rowset
  19003.     (
  19004.     @table_name    varchar(255),
  19005.     @table_schema    varchar(255) = null     
  19006.     )
  19007. as
  19008.     select    db_name()                as TABLE_CATALOG,        
  19009.         user_name(o.uid)            as TABLE_SCHEMA,
  19010.         o.name                    as TABLE_NAME,
  19011.         x.rows                    as CARDINALITY
  19012.     from    sysobjects o, sysindexes x
  19013.     where    o.type in ('U')
  19014.     and     o.name = @table_name
  19015.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  19016.     and     x.id = o.id
  19017.     and     x.indid in (0,1)    /*If there are no indexes then table stats are in a row with indid =0 */
  19018.     and     permissions(o.id) <> 0
  19019.     order by 2, 3
  19020. go
  19021. create procedure sp_statistics_rowset;2
  19022.     (
  19023.     @table_schema    varchar(255) = null     
  19024.     )
  19025. as
  19026.     select    db_name()                as TABLE_CATALOG,        
  19027.         user_name(o.uid)            as TABLE_SCHEMA,
  19028.         o.name                    as TABLE_NAME,
  19029.         x.rows                    as CARDINALITY
  19030.     from    sysobjects o, sysindexes x
  19031.     where    o.type in ('U')
  19032.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  19033.     and     x.id = o.id
  19034.     and     x.indid in (0,1)    /*If there are no indexes then table stats are in a row with indid =0 */
  19035.     and     permissions(o.id) <> 0
  19036.     order by 2, 3
  19037. go
  19038. create procedure sp_statistics_rowset;3
  19039.     (
  19040.     @table_name    varchar(255) = null,
  19041.     @table_schema    varchar(255) = null     
  19042.     )
  19043. as
  19044. IF @table_name is not null
  19045.     BEGIN
  19046.     select    db_name()                as TABLE_CATALOG,        
  19047.         user_name(o.uid)            as TABLE_SCHEMA,
  19048.         o.name                    as TABLE_NAME,
  19049.         x.rows                    as CARDINALITY
  19050.     from    sysobjects o, sysindexes x
  19051.     where    o.type in ('U')
  19052.     and     o.name = @table_name
  19053.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  19054.     and     x.id = o.id
  19055.     and     x.indid in (0,1)    /*If there are no indexes then table stats are in a row with indid =0 */
  19056.     and     permissions(o.id) <> 0
  19057.     END
  19058. ELSE
  19059.     BEGIN
  19060.     select    db_name()                as TABLE_CATALOG,        
  19061.         user_name(o.uid)            as TABLE_SCHEMA,
  19062.         o.name                    as TABLE_NAME,
  19063.         x.rows                    as CARDINALITY
  19064.     from    sysobjects o, sysindexes x
  19065.     where    o.type in ('U')
  19066.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  19067.     and     x.id = o.id
  19068.     and     x.indid in (0,1)    /*If there are no indexes then table stats are in a row with indid =0 */
  19069.     and     permissions(o.id) <> 0
  19070.     END
  19071. go
  19072.  
  19073. create procedure sp_statistics_rowset;4
  19074.     (
  19075.  
  19076.        @handle        int output,
  19077.        @scrollopt    int output,
  19078.     @ccopt        int output,
  19079.     @rows        int output,
  19080.     @cmd        varchar(255)
  19081.     )
  19082. as
  19083.     declare @ret int
  19084.     declare @database_name varchar(30)
  19085.  
  19086.     create table #spstatisticsrowset
  19087.         (
  19088.         TABLE_CATALOG            varchar(30) not null,
  19089.         TABLE_SCHEMA            varchar(30) not null,
  19090.         TABLE_NAME            varchar(30) not null,
  19091.         CARDINALITY            int not null
  19092.         )
  19093.  
  19094.     declare sysdatabases_cursor insensitive cursor for 
  19095.         select name from master.dbo.sysdatabases
  19096.   
  19097.     set nocount on
  19098.     open sysdatabases_cursor
  19099.       fetch next from sysdatabases_cursor into @database_name 
  19100.     while @@fetch_status = 0
  19101.         begin
  19102.         exec('use '+@database_name+' insert into #spstatisticsrowset '+@cmd)
  19103.         fetch next from sysdatabases_cursor into @database_name 
  19104.         end
  19105.       close sysdatabases_cursor
  19106.     deallocate sysdatabases_cursor
  19107.  
  19108.     if @scrollopt is null
  19109.         begin
  19110.         select * from #spstatisticsrowset order by 1,2,3
  19111.         end
  19112.     else
  19113.         begin
  19114.         exec @ret = sp_cursoropen @handle output,
  19115.             'select * from #spstatisticsrowset order by 1,2,3',
  19116.                 @scrollopt output, @ccopt output, @rows output
  19117.         end
  19118.     drop table #spstatisticsrowset 
  19119.     return isnull(@ret,0)
  19120. go
  19121.  
  19122. grant execute on sp_statistics_rowset to public
  19123. go
  19124.  
  19125. dump tran master with no_log
  19126. go
  19127.  
  19128. print 'creating sp_tables_rowset'
  19129. go
  19130.  
  19131.  
  19132. /*    Procedure for 6.50 and earlier servers */
  19133. create procedure sp_tables_rowset
  19134.     (
  19135.     @table_name    varchar(255), 
  19136.     @table_schema    varchar(255) = null,    
  19137.     @table_type    varchar(255) = null 
  19138.     )
  19139. as
  19140.     select    TABLE_CATALOG    = db_name(),
  19141.         TABLE_SCHEMA    = user_name(o.uid),
  19142.         TABLE_NAME    = o.name,
  19143.         TABLE_TYPE    = convert(varchar(30),
  19144.                     case o.type 
  19145.                     when 'U' then 'TABLE'
  19146.                     when 'V' then 'VIEW'
  19147.                     when 'S' then 'SYSTEM TABLE'
  19148.                     end),
  19149.         TABLE_GUID    = convert(binary(16), null),
  19150.         DESCRIPTION    = convert(varchar(1), null)
  19151.     from    sysusers u, sysobjects o
  19152.     where    o.type in ('U','V','S')
  19153.     and     o.name = @table_name
  19154.     and     (    @table_schema is null
  19155.         or    @table_schema = user_name(o.uid)
  19156.         )
  19157.     and     (
  19158.             @table_type is null
  19159.         or    @table_type = case o.type 
  19160.                     when 'U' then 'TABLE'
  19161.                     when 'V' then 'VIEW'
  19162.                     when 'S' then 'SYSTEM TABLE'
  19163.                     end
  19164.         )
  19165.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  19166.     and     (
  19167.         suser_id() = 1     /* User is the System Administrator */
  19168.         or o.uid = user_id()     /* User created the object */
  19169.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  19170.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  19171.             from sysprotects p
  19172.             /* join to correlate with all rows in sysobjects */
  19173.             where p.id = o.id
  19174.             /* get rows for public,current user,user's group */
  19175.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  19176.             /* check for SELECT,EXECUTE privilege */
  19177.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  19178.             ) = 1    /* final magic...compare Grants      */
  19179.         )        
  19180.     order by 4, 2, 3
  19181. go
  19182. create procedure sp_tables_rowset;2
  19183.     (
  19184.     @table_schema    varchar(255) = null,    
  19185.     @table_type    varchar(255) = null 
  19186.     )
  19187. as
  19188.     select    TABLE_CATALOG    = db_name(),
  19189.         TABLE_SCHEMA    = user_name(o.uid),
  19190.         TABLE_NAME    = o.name,
  19191.         TABLE_TYPE    = convert(varchar(30),
  19192.                     case o.type 
  19193.                     when 'U' then 'TABLE'
  19194.                     when 'V' then 'VIEW'
  19195.                     when 'S' then 'SYSTEM TABLE'
  19196.                     end),
  19197.         TABLE_GUID    = convert(binary(16), null),
  19198.         DESCRIPTION    = convert(varchar(1), null)
  19199.     from    sysusers u, sysobjects o
  19200.     where    o.type in ('U','V','S')
  19201.     and     (    @table_schema is null
  19202.         or    @table_schema = user_name(o.uid)
  19203.         )
  19204.     and     (
  19205.             @table_type is null
  19206.         or    @table_type = case o.type 
  19207.                     when 'U' then 'TABLE'
  19208.                     when 'V' then 'VIEW'
  19209.                     when 'S' then 'SYSTEM TABLE'
  19210.                     end
  19211.         )
  19212.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  19213.     and     (
  19214.         suser_id() = 1     /* User is the System Administrator */
  19215.         or o.uid = user_id()     /* User created the object */
  19216.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  19217.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  19218.             from sysprotects p
  19219.             /* join to correlate with all rows in sysobjects */
  19220.             where p.id = o.id
  19221.             /* get rows for public,current user,user's group */
  19222.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  19223.             /* check for SELECT,EXECUTE privilege */
  19224.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  19225.             ) = 1    /* final magic...compare Grants      */
  19226.         )        
  19227.     order by 4, 2, 3
  19228. go
  19229. create procedure sp_tables_rowset;3
  19230.     (
  19231.     @table_name    varchar(255) = null, 
  19232.     @table_schema    varchar(255) = null,    
  19233.     @table_type    varchar(255) = null 
  19234.     )
  19235. as
  19236. IF @table_name is not null
  19237.     BEGIN
  19238.     select    TABLE_CATALOG    = db_name(),
  19239.         TABLE_SCHEMA    = user_name(o.uid),
  19240.         TABLE_NAME    = o.name,
  19241.         TABLE_TYPE    = convert(varchar(30),
  19242.                     case o.type 
  19243.                     when 'U' then 'TABLE'
  19244.                     when 'V' then 'VIEW'
  19245.                     when 'S' then 'SYSTEM TABLE'
  19246.                     end),
  19247.         TABLE_GUID    = convert(binary(16), null),
  19248.         DESCRIPTION    = convert(varchar(1), null)
  19249.     from    sysusers u, sysobjects o
  19250.     where    o.type in ('U','V','S')
  19251.     and     o.name = @table_name
  19252.     and     (    @table_schema is null
  19253.         or    @table_schema = user_name(o.uid)
  19254.         )
  19255.     and     (
  19256.             @table_type is null
  19257.         or    @table_type = case o.type 
  19258.                     when 'U' then 'TABLE'
  19259.                     when 'V' then 'VIEW'
  19260.                     when 'S' then 'SYSTEM TABLE'
  19261.                     end
  19262.         )
  19263.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  19264.     and     (
  19265.         suser_id() = 1     /* User is the System Administrator */
  19266.         or o.uid = user_id()     /* User created the object */
  19267.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  19268.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  19269.             from sysprotects p
  19270.             /* join to correlate with all rows in sysobjects */
  19271.             where p.id = o.id
  19272.             /* get rows for public,current user,user's group */
  19273.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  19274.             /* check for SELECT,EXECUTE privilege */
  19275.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  19276.             ) = 1    /* final magic...compare Grants      */
  19277.         )        
  19278.     END
  19279.  
  19280. ELSE
  19281.     BEGIN
  19282.     select    TABLE_CATALOG    = db_name(),
  19283.         TABLE_SCHEMA    = user_name(o.uid),
  19284.         TABLE_NAME    = o.name,
  19285.         TABLE_TYPE    = convert(varchar(30),
  19286.                     case o.type 
  19287.                     when 'U' then 'TABLE'
  19288.                     when 'V' then 'VIEW'
  19289.                     when 'S' then 'SYSTEM TABLE'
  19290.                     end),
  19291.         TABLE_GUID    = convert(binary(16), null),
  19292.         DESCRIPTION    = convert(varchar(1), null)
  19293.     from    sysusers u, sysobjects o
  19294.     where    o.type in ('U','V','S')
  19295.     and     (    @table_schema is null
  19296.         or    @table_schema = user_name(o.uid)
  19297.         )
  19298.     and     (
  19299.             @table_type is null
  19300.         or    @table_type = case o.type 
  19301.                     when 'U' then 'TABLE'
  19302.                     when 'V' then 'VIEW'
  19303.                     when 'S' then 'SYSTEM TABLE'
  19304.                     end
  19305.         )
  19306.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  19307.     and     (
  19308.         suser_id() = 1     /* User is the System Administrator */
  19309.         or o.uid = user_id()     /* User created the object */
  19310.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  19311.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  19312.             from sysprotects p
  19313.             /* join to correlate with all rows in sysobjects */
  19314.             where p.id = o.id
  19315.             /* get rows for public,current user,user's group */
  19316.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  19317.             /* check for SELECT,EXECUTE privilege */
  19318.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  19319.             ) = 1    /* final magic...compare Grants      */
  19320.         )        
  19321.     END
  19322. go
  19323.  
  19324. if (charindex('7.00', @@version) > 0)
  19325.     drop procedure sp_tables_rowset
  19326. else
  19327. begin
  19328.     print ''
  19329.     print ''
  19330.     print 'Warning:'
  19331.     print 'you are installing the stored procedures '
  19332.     print 'on a pre 7.0 SQL Server.'
  19333.     print 'Ignore the following errors.'
  19334. end
  19335. go
  19336.  
  19337. /*    Procedure for 7.00 server */
  19338. create procedure sp_tables_rowset
  19339.     (
  19340.     @table_name    varchar(255), 
  19341.     @table_schema    varchar(255) = null,    
  19342.     @table_type    varchar(255) = null 
  19343.     )
  19344. as
  19345.     select    TABLE_CATALOG    = db_name(),
  19346.         TABLE_SCHEMA    = user_name(o.uid),
  19347.         TABLE_NAME    = o.name,
  19348.         TABLE_TYPE    = convert(varchar(30),
  19349.                     case o.type 
  19350.                     when 'U' then 'TABLE'
  19351.                     when 'V' then 'VIEW'
  19352.                     when 'S' then 'SYSTEM TABLE'
  19353.                     end),
  19354.         TABLE_GUID    = convert(binary(16), null),
  19355.         DESCRIPTION    = convert(varchar(1), null)
  19356.     from    sysobjects o
  19357.     where    o.name = @table_name
  19358.     and     o.type in ('U','V','S')
  19359.     and    permissions(o.id) <> 0
  19360.     and     (    @table_schema is null
  19361.         or    @table_schema = user_name(o.uid)
  19362.         )
  19363.     and     (
  19364.             @table_type is null
  19365.         or    @table_type = case o.type 
  19366.                     when 'U' then 'TABLE'
  19367.                     when 'V' then 'VIEW'
  19368.                     when 'S' then 'SYSTEM TABLE'
  19369.                     end
  19370.         )    
  19371.     order by 4, 2, 3
  19372. go
  19373. create procedure sp_tables_rowset;2
  19374.     (
  19375.     @table_schema    varchar(255) = null,    
  19376.     @table_type    varchar(255) = null 
  19377.     )
  19378. as
  19379.     select    TABLE_CATALOG    = db_name(),
  19380.         TABLE_SCHEMA    = user_name(o.uid),
  19381.         TABLE_NAME    = o.name,
  19382.         TABLE_TYPE    = convert(varchar(30),
  19383.                     case o.type 
  19384.                     when 'U' then 'TABLE'
  19385.                     when 'V' then 'VIEW'
  19386.                     when 'S' then 'SYSTEM TABLE'
  19387.                     end),
  19388.         TABLE_GUID    = convert(binary(16), null),
  19389.         DESCRIPTION    = convert(varchar(1), null)
  19390.     from    sysobjects o
  19391.     where    o.type in ('U','V','S')
  19392.     and    permissions(o.id) <> 0
  19393.     and     (    @table_schema is null
  19394.         or    @table_schema = user_name(o.uid)
  19395.         )
  19396.     and     (
  19397.             @table_type is null
  19398.         or    @table_type = case o.type 
  19399.                     when 'U' then 'TABLE'
  19400.                     when 'V' then 'VIEW'
  19401.                     when 'S' then 'SYSTEM TABLE'
  19402.                     end
  19403.         )    
  19404.     order by 4, 2, 3
  19405. go
  19406. create procedure sp_tables_rowset;3
  19407.     (
  19408.     @table_name    varchar(255) = null, 
  19409.     @table_schema    varchar(255) = null,    
  19410.     @table_type    varchar(255) = null 
  19411.     )
  19412. as
  19413. IF @table_name is not null
  19414.     BEGIN
  19415.     select    TABLE_CATALOG    = db_name(),
  19416.         TABLE_SCHEMA    = user_name(o.uid),
  19417.         TABLE_NAME    = o.name,
  19418.         TABLE_TYPE    = convert(varchar(30),
  19419.                     case o.type 
  19420.                     when 'U' then 'TABLE'
  19421.                     when 'V' then 'VIEW'
  19422.                     when 'S' then 'SYSTEM TABLE'
  19423.                     end),
  19424.         TABLE_GUID    = convert(binary(16), null),
  19425.         DESCRIPTION    = convert(varchar(1), null)
  19426.     from    sysobjects o
  19427.     where    o.name = @table_name
  19428.     and     o.type in ('U','V','S')
  19429.     and    permissions(o.id) <> 0
  19430.     and     (    @table_schema is null
  19431.         or    @table_schema = user_name(o.uid)
  19432.         )
  19433.     and     (
  19434.             @table_type is null
  19435.         or    @table_type = case o.type 
  19436.                     when 'U' then 'TABLE'
  19437.                     when 'V' then 'VIEW'
  19438.                     when 'S' then 'SYSTEM TABLE'
  19439.                     end
  19440.         )    
  19441.     END
  19442. ELSE
  19443.     BEGIN
  19444.     select    TABLE_CATALOG    = db_name(),
  19445.         TABLE_SCHEMA    = user_name(o.uid),
  19446.         TABLE_NAME    = o.name,
  19447.         TABLE_TYPE    = convert(varchar(30),
  19448.                     case o.type 
  19449.                     when 'U' then 'TABLE'
  19450.                     when 'V' then 'VIEW'
  19451.                     when 'S' then 'SYSTEM TABLE'
  19452.                     end),
  19453.         TABLE_GUID    = convert(binary(16), null),
  19454.         DESCRIPTION    = convert(varchar(1), null)
  19455.     from    sysobjects o
  19456.     where    o.type in ('U','V','S')
  19457.     and    permissions(o.id) <> 0
  19458.     and     (    @table_schema is null
  19459.         or    @table_schema = user_name(o.uid)
  19460.         )
  19461.     and     (
  19462.             @table_type is null
  19463.         or    @table_type = case o.type 
  19464.                     when 'U' then 'TABLE'
  19465.                     when 'V' then 'VIEW'
  19466.                     when 'S' then 'SYSTEM TABLE'
  19467.                     end
  19468.         )    
  19469.     END
  19470. go
  19471.  
  19472.  
  19473. create procedure sp_tables_rowset;4
  19474.     (
  19475.  
  19476.        @handle        int output,
  19477.        @scrollopt    int output,
  19478.     @ccopt        int output,
  19479.     @rows        int output,
  19480.     @cmd        varchar(255)
  19481.     )
  19482. as
  19483.     declare @ret int
  19484.     declare @database_name varchar(30)
  19485.  
  19486.     create table #sptablesrowset
  19487.         (
  19488.         TABLE_CATALOG        varchar(30) not null,
  19489.         TABLE_SCHEMA        varchar(30) not null,
  19490.         TABLE_NAME        varchar(30) not null,
  19491.         TABLE_TYPE        varchar(30) not null,
  19492.         TABLE_GUID        binary(16) null,
  19493.         DESCRIPTION        varchar(1) null
  19494.         )
  19495.  
  19496.     declare sysdatabases_cursor insensitive cursor for 
  19497.         select name from master.dbo.sysdatabases
  19498.   
  19499.     set nocount on
  19500.     open sysdatabases_cursor
  19501.       fetch next from sysdatabases_cursor into @database_name 
  19502.     while @@fetch_status = 0
  19503.         begin
  19504.         exec('use '+@database_name+' insert into #sptablesrowset '+@cmd)
  19505.         fetch next from sysdatabases_cursor into @database_name 
  19506.         end
  19507.       close sysdatabases_cursor
  19508.     deallocate sysdatabases_cursor
  19509.  
  19510.     if @scrollopt is null
  19511.         begin
  19512.         select * from #sptablesrowset order by 4,1,2,3
  19513.         end
  19514.     else
  19515.         begin
  19516.         exec @ret = sp_cursoropen @handle output,
  19517.             'select * from #sptablesrowset order by 4,1,2,3',
  19518.                 @scrollopt output, @ccopt output, @rows output
  19519.         end
  19520.     drop table #sptablesrowset
  19521.     return isnull(@ret,0)
  19522. go
  19523.  
  19524. grant execute on sp_tables_rowset to public
  19525. go
  19526.  
  19527. dump tran master with no_log
  19528. go
  19529.  
  19530.  
  19531.  
  19532.  
  19533. print 'creating sp_table_privileges_rowset'
  19534. go
  19535.  
  19536. /*    Procedure for 6.0 and 6.5 server */
  19537. CREATE PROCEDURE sp_table_privileges_rowset
  19538.     (
  19539.     @table_name    varchar(255) = null,
  19540.     @table_schema    varchar(255) = null,
  19541.     @grantor    varchar(255) = null,
  19542.     @grantee    varchar(255) = null
  19543.     )
  19544. as
  19545. IF @table_name is not null
  19546.     BEGIN
  19547.     select
  19548.         GRANTOR        = user_name(p.grantor),
  19549.         GRANTEE        = user_name(u.uid),
  19550.         TABLE_CATALOG    = db_name(),
  19551.         TABLE_SCHEMA    = user_name(o.uid),
  19552.         TABLE_NAME    = o.name,
  19553.         PRIVILEGE_TYPE    = convert(varchar(30),
  19554.                     case p.action
  19555.                     when 193 then 'SELECT'
  19556.                     when 195 then 'INSERT'
  19557.                     when 197 then 'UPDATE'
  19558.                     else 'REFERENCES'
  19559.                     end),
  19560.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  19561.     from 
  19562.         sysprotects p, sysobjects o, sysusers u
  19563.     where
  19564.         o.name = @table_name
  19565.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  19566.     and    (@grantee is null or @grantee = user_name(u.uid))
  19567.     and     o.type in ('U','V')
  19568.     and    p.id = o.id
  19569.     and     (@grantor is null or @grantor = user_name(p.grantor))
  19570.             /* expand groups */
  19571.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  19572.     and     p.protecttype <> 206    /* only grant rows */
  19573.     and     p.action in (26,193,195,196,197)
  19574.     and     o.uid <> u.uid            /* no rows for owner */
  19575.     and     not exists (            /* exclude revoke'd privileges */
  19576.             select     *
  19577.             from     sysprotects p1
  19578.             where    p1.protecttype = 206
  19579.             and     p1.action = p.action
  19580.             and     p1.id = p.id
  19581.             and     p1.uid = u.uid)
  19582.     union all
  19583.     select    /*    Add rows for table owner */
  19584.         GRANTOR        = user_name(u.uid),
  19585.         GRANTEE        = user_name(o.uid),
  19586.         TABLE_CATALOG    = db_name(),
  19587.         TABLE_SCHEMA    = user_name(o.uid),
  19588.         TABLE_NAME    = o.name,
  19589.         PRIVILEGE_TYPE    = convert(varchar(30),
  19590.                     case v.number
  19591.                     when 193 then 'SELECT'
  19592.                     when 195 then 'INSERT'
  19593.                     when 197 then 'UPDATE'
  19594.                     else 'REFERENCES'
  19595.                     end),
  19596.         IS_GRANTABLE    = convert(bit,1)    
  19597.     from 
  19598.         sysobjects o, master.dbo.spt_values v, sysusers u
  19599.     where
  19600.         o.name = @table_name
  19601.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  19602.     and    (@grantee is null or @grantee = user_name(u.uid))
  19603.     and     o.type in ('U','V')
  19604.     and     u.suid = 1        /* grantor is dbo of database */
  19605.     and    (@grantor is null or @grantor = user_name(u.uid))
  19606.     and     v.type = 'P'    /* cross product to get all exposed privileges */
  19607.     and     v.number in (26,193,195,196,197)
  19608.     and     not exists (    /* exclude revoke'd privileges */
  19609.             select     *
  19610.             from     sysprotects p1
  19611.             where    p1.protecttype = 206
  19612.             and     p1.action = v.number
  19613.             and     p1.id = o.id
  19614.             and     p1.uid = o.uid)
  19615.     order by 4,5,6,1,2
  19616.     END
  19617. ELSE
  19618.     BEGIN
  19619.     select
  19620.         GRANTOR        = user_name(p.grantor),
  19621.         GRANTEE        = user_name(u.uid),
  19622.         TABLE_CATALOG    = db_name(),
  19623.         TABLE_SCHEMA    = user_name(o.uid),
  19624.         TABLE_NAME    = o.name,
  19625.         PRIVILEGE_TYPE    = convert(varchar(30),
  19626.                     case p.action
  19627.                     when 193 then 'SELECT'
  19628.                     when 195 then 'INSERT'
  19629.                     when 197 then 'UPDATE'
  19630.                     else 'REFERENCES'
  19631.                     end),
  19632.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  19633.     from 
  19634.         sysprotects p, sysobjects o, sysusers u
  19635.     where
  19636.         o.type in ('U','V')
  19637.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  19638.     and    (@grantee is null or @grantee = user_name(u.uid))
  19639.     and    p.id = o.id
  19640.     and     (@grantor is null or @grantor = user_name(p.grantor))
  19641.             /* expand groups */
  19642.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  19643.     and     p.protecttype <> 206    /* only grant rows */
  19644.     and     p.action in (26,193,195,196,197)
  19645.     and     o.uid <> u.uid            /* no rows for owner */
  19646.     and     not exists (            /* exclude revoke'd privileges */
  19647.             select     *
  19648.             from     sysprotects p1
  19649.             where    p1.protecttype = 206
  19650.             and     p1.action = p.action
  19651.             and     p1.id = p.id
  19652.             and     p1.uid = u.uid)
  19653.     union all
  19654.     select    /*    Add rows for table owner */
  19655.         GRANTOR        = user_name(u.uid),
  19656.         GRANTEE        = user_name(o.uid),
  19657.         TABLE_CATALOG    = db_name(),
  19658.         TABLE_SCHEMA    = user_name(o.uid),
  19659.         TABLE_NAME    = o.name,
  19660.         PRIVILEGE_TYPE    = convert(varchar(30),
  19661.                     case v.number
  19662.                     when 193 then 'SELECT'
  19663.                     when 195 then 'INSERT'
  19664.                     when 197 then 'UPDATE'
  19665.                     else 'REFERENCES'
  19666.                     end),
  19667.         IS_GRANTABLE    = convert(bit,1)    
  19668.     from 
  19669.         sysobjects o, master.dbo.spt_values v, sysusers u
  19670.     where
  19671.         o.type in ('U','V')
  19672.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  19673.     and    (@grantee is null or @grantee = user_name(u.uid))
  19674.     and     u.suid = 1        /* grantor is dbo of database */
  19675.     and    (@grantor is null or @grantor = user_name(u.uid))
  19676.     and     v.type = 'P'    /* cross product to get all exposed privileges */
  19677.     and     v.number in (26,193,195,196,197)
  19678.     and     not exists (    /* exclude revoke'd privileges */
  19679.             select     *
  19680.             from     sysprotects p1
  19681.             where    p1.protecttype = 206
  19682.             and     p1.action = v.number
  19683.             and     p1.id = o.id
  19684.             and     p1.uid = o.uid)
  19685.     order by 4,5,6,1,2
  19686.     END
  19687. go
  19688. CREATE PROCEDURE sp_table_privileges_rowset;2
  19689.     (
  19690.        @handle        int output,
  19691.        @scrollopt    int output,
  19692.     @ccopt        int output,
  19693.     @rows        int output,
  19694.     @table_name     varchar(255) = null,
  19695.     @table_schema    varchar(255) = null,
  19696.     @grantor    varchar(255) = null,
  19697.     @grantee    varchar(255) = null
  19698.     )
  19699. as
  19700. declare @ret int
  19701. create table #sptprivsrowset1
  19702.     (
  19703.     GRANTOR        varchar(30) not null,
  19704.     GRANTEE        varchar(30) not null,
  19705.     TABLE_CATALOG    varchar(30) not null,
  19706.     TABLE_SCHEMA    varchar(30) not null,
  19707.     TABLE_NAME    varchar(30) not null,
  19708.     PRIVILEGE_TYPE    varchar(30) not null,
  19709.     IS_GRANTABLE    bit not null
  19710.     )
  19711.     
  19712. IF @table_name is not null
  19713.     BEGIN
  19714.     insert into #sptprivsrowset1
  19715.     select
  19716.         GRANTOR        = user_name(p.grantor),
  19717.         GRANTEE        = user_name(u.uid),
  19718.         TABLE_CATALOG    = db_name(),
  19719.         TABLE_SCHEMA    = user_name(o.uid),
  19720.         TABLE_NAME    = o.name,
  19721.         PRIVILEGE_TYPE    = convert(varchar(30),
  19722.                     case p.action
  19723.                     when 193 then 'SELECT'
  19724.                     when 195 then 'INSERT'
  19725.                     when 197 then 'UPDATE'
  19726.                     else 'REFERENCES'
  19727.                     end),
  19728.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  19729.     from 
  19730.         sysprotects p, sysobjects o, sysusers u
  19731.     where
  19732.         o.name = @table_name
  19733.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  19734.     and    (@grantee is null or @grantee = user_name(u.uid))
  19735.     and     o.type in ('U','V')
  19736.     and    p.id = o.id
  19737.     and     (@grantor is null or @grantor = user_name(p.grantor))
  19738.             /* expand groups */
  19739.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  19740.     and     p.protecttype <> 206    /* only grant rows */
  19741.     and     p.action in (26,193,195,196,197)
  19742.     and     o.uid <> u.uid            /* no rows for owner */
  19743.     and     not exists (            /* exclude revoke'd privileges */
  19744.             select     *
  19745.             from     sysprotects p1
  19746.             where    p1.protecttype = 206
  19747.             and     p1.action = p.action
  19748.             and     p1.id = p.id
  19749.             and     p1.uid = u.uid)
  19750.     union all
  19751.     select    /*    Add rows for table owner */
  19752.         GRANTOR        = user_name(u.uid),
  19753.         GRANTEE        = user_name(o.uid),
  19754.         TABLE_CATALOG    = db_name(),
  19755.         TABLE_SCHEMA    = user_name(o.uid),
  19756.         TABLE_NAME    = o.name,
  19757.         PRIVILEGE_TYPE    = convert(varchar(30),
  19758.                     case v.number
  19759.                     when 193 then 'SELECT'
  19760.                     when 195 then 'INSERT'
  19761.                     when 197 then 'UPDATE'
  19762.                     else 'REFERENCES'
  19763.                     end),
  19764.         IS_GRANTABLE    = convert(bit,1)    
  19765.     from 
  19766.         sysobjects o, master.dbo.spt_values v, sysusers u
  19767.     where
  19768.         o.name = @table_name
  19769.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  19770.     and    (@grantee is null or @grantee = user_name(u.uid))
  19771.     and     o.type in ('U','V')
  19772.     and     u.suid = 1        /* grantor is dbo of database */
  19773.     and    (@grantor is null or @grantor = user_name(u.uid))
  19774.     and     v.type = 'P'    /* cross product to get all exposed privileges */
  19775.     and     v.number in (26,193,195,196,197)
  19776.     and     not exists (    /* exclude revoke'd privileges */
  19777.             select     *
  19778.             from     sysprotects p1
  19779.             where    p1.protecttype = 206
  19780.             and     p1.action = v.number
  19781.             and     p1.id = o.id
  19782.             and     p1.uid = o.uid)
  19783.     order by 4,5,6,1,2
  19784.     END
  19785. ELSE
  19786.     BEGIN
  19787.     insert into #sptprivsrowset1
  19788.     select
  19789.         GRANTOR        = user_name(p.grantor),
  19790.         GRANTEE        = user_name(u.uid),
  19791.         TABLE_CATALOG    = db_name(),
  19792.         TABLE_SCHEMA    = user_name(o.uid),
  19793.         TABLE_NAME    = o.name,
  19794.         PRIVILEGE_TYPE    = convert(varchar(30),
  19795.                     case p.action
  19796.                     when 193 then 'SELECT'
  19797.                     when 195 then 'INSERT'
  19798.                     when 197 then 'UPDATE'
  19799.                     else 'REFERENCES'
  19800.                     end),
  19801.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  19802.     from 
  19803.         sysprotects p, sysobjects o, sysusers u
  19804.     where
  19805.         o.type in ('U','V')
  19806.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  19807.     and    (@grantee is null or @grantee = user_name(u.uid))
  19808.     and    p.id = o.id
  19809.     and     (@grantor is null or @grantor = user_name(p.grantor))
  19810.             /* expand groups */
  19811.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  19812.     and     p.protecttype <> 206    /* only grant rows */
  19813.     and     p.action in (26,193,195,196,197)
  19814.     and     o.uid <> u.uid            /* no rows for owner */
  19815.     and     not exists (            /* exclude revoke'd privileges */
  19816.             select     *
  19817.             from     sysprotects p1
  19818.             where    p1.protecttype = 206
  19819.             and     p1.action = p.action
  19820.             and     p1.id = p.id
  19821.             and     p1.uid = u.uid)
  19822.     union all
  19823.     select    /*    Add rows for table owner */
  19824.         GRANTOR        = user_name(u.uid),
  19825.         GRANTEE        = user_name(o.uid),
  19826.         TABLE_CATALOG    = db_name(),
  19827.         TABLE_SCHEMA    = user_name(o.uid),
  19828.         TABLE_NAME    = o.name,
  19829.         PRIVILEGE_TYPE    = convert(varchar(30),
  19830.                     case v.number
  19831.                     when 193 then 'SELECT'
  19832.                     when 195 then 'INSERT'
  19833.                     when 197 then 'UPDATE'
  19834.                     else 'REFERENCES'
  19835.                     end),
  19836.         IS_GRANTABLE    = convert(bit,1)    
  19837.     from 
  19838.         sysobjects o, master.dbo.spt_values v, sysusers u
  19839.     where
  19840.         o.type in ('U','V')
  19841.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  19842.     and    (@grantee is null or @grantee = user_name(u.uid))
  19843.     and     u.suid = 1        /* grantor is dbo of database */
  19844.     and    (@grantor is null or @grantor = user_name(u.uid))
  19845.     and     v.type = 'P'    /* cross product to get all exposed privileges */
  19846.     and     v.number in (26,193,195,196,197)
  19847.     and     not exists (    /* exclude revoke'd privileges */
  19848.             select     *
  19849.             from     sysprotects p1
  19850.             where    p1.protecttype = 206
  19851.             and     p1.action = v.number
  19852.             and     p1.id = o.id
  19853.             and     p1.uid = o.uid)
  19854.     order by 4,5,6,1,2
  19855.     END
  19856.  
  19857. exec @ret = sp_cursoropen @handle output, 'select * from #sptprivsrowset1',
  19858.     @scrollopt output, @ccopt output, @rows output
  19859.  
  19860. drop table #sptprivsrowset1
  19861. return isnull(@ret,0)
  19862. go
  19863.  
  19864.  
  19865. create procedure sp_table_privileges_rowset;4
  19866.     (
  19867.        @handle        int output,
  19868.        @scrollopt    int output,
  19869.     @ccopt        int output,
  19870.     @rows        int output,
  19871.     @cmd        varchar(255)
  19872.     )
  19873. as
  19874.     declare @ret int
  19875.     declare @database_name varchar(30)
  19876.  
  19877.     create table #sptprivsrowset2
  19878.         (
  19879.         GRANTOR        varchar(30) not null,
  19880.         GRANTEE        varchar(30) not null,
  19881.         TABLE_CATALOG    varchar(30) not null,
  19882.         TABLE_SCHEMA    varchar(30) not null,
  19883.         TABLE_NAME    varchar(30) not null,
  19884.         PRIVILEGE_TYPE    varchar(30) not null,
  19885.         IS_GRANTABLE    bit not null
  19886.         )
  19887.  
  19888.     declare sysdatabases_cursor insensitive cursor for 
  19889.         select name from master.dbo.sysdatabases order by name
  19890.   
  19891.     set nocount on
  19892.     open sysdatabases_cursor
  19893.       fetch next from sysdatabases_cursor into @database_name 
  19894.     while @@fetch_status = 0
  19895.         begin
  19896.         exec('use '+@database_name+' insert into #sptprivsrowset2 '+@cmd)
  19897.         fetch next from sysdatabases_cursor into @database_name 
  19898.         end
  19899.       close sysdatabases_cursor
  19900.     deallocate sysdatabases_cursor
  19901.  
  19902.     if @scrollopt is null
  19903.         begin
  19904.         select * from #sptprivsrowset2
  19905.         end
  19906.     else
  19907.         begin
  19908.         exec @ret = sp_cursoropen @handle output,
  19909.             'select * from #sptprivsrowset2',
  19910.                 @scrollopt output, @ccopt output, @rows output
  19911.         end
  19912.     drop table #sptprivsrowset2
  19913.     return isnull(@ret,0)
  19914. go
  19915.  
  19916.  
  19917.  
  19918.  
  19919. if (charindex('7.00', @@version) = 0)
  19920. begin
  19921.     print ''
  19922.     print ''
  19923.     print 'Warning:'
  19924.     print 'you are installing the stored procedures '
  19925.     print 'on a pre 7.0 SQL Server.'
  19926.     print 'Ignore the following errors.'
  19927. end
  19928. else
  19929.     drop proc sp_table_privileges_rowset
  19930. go
  19931.  
  19932.  
  19933. /*    Procedure for 7.0 server */
  19934. CREATE PROCEDURE sp_table_privileges_rowset
  19935.     (
  19936.     @table_name         varchar(255),
  19937.     @table_schema        varchar(255) = null,
  19938.     @grantor        varchar(255) = null,
  19939.     @grantee        varchar(255) = null
  19940.     )
  19941. as
  19942.     select
  19943.         GRANTOR        = user_name(p.grantor),
  19944.         GRANTEE        = user_name(u.uid),
  19945.         TABLE_CATALOG    = db_name(),
  19946.         TABLE_SCHEMA    = user_name(o.uid),
  19947.         TABLE_NAME    = o.name,
  19948.         PRIVILEGE_TYPE    = convert(varchar(30),
  19949.                     case p.action
  19950.                     when 193 then 'SELECT'
  19951.                     when 195 then 'INSERT'
  19952.                     when 197 then 'UPDATE'
  19953.                     else 'REFERENCES'
  19954.                     end),
  19955.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  19956.     from 
  19957.         sysprotects p, sysobjects o, sysusers u, sysmembers m
  19958.     where
  19959.         o.name = @table_name
  19960.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  19961.     and    (@grantee is null or @grantee = user_name(u.uid))
  19962.     and     o.type in ('U','V')
  19963.     and    p.id = o.id
  19964.     and     (@grantor is null or @grantor = user_name(p.grantor))
  19965.         /* expand groups - AKUNDONE: only 1 level of grp unrolling. */
  19966.     and     (u.uid > 0 and u.uid < 16384)
  19967.     and     ((p.uid = u.uid) or (p.uid = m.groupuid and u.uid = m.memberuid))
  19968.     and     p.protecttype <> 206    /* only grant rows */
  19969.     and     p.action in (26,193,195,196,197)
  19970.     and     o.uid <> u.uid            /* no rows for owner */
  19971.     and     not exists (            /* exclude revoke'd privileges */
  19972.             select     *
  19973.             from     sysprotects p1
  19974.             where    p1.protecttype = 206
  19975.             and     p1.action = p.action
  19976.             and     p1.id = p.id
  19977.             and     p1.uid = u.uid)
  19978.     union all
  19979.     select    /*    Add rows for table owner */
  19980.         GRANTOR        = user_name(u.uid),
  19981.         GRANTEE        = user_name(o.uid),
  19982.         TABLE_CATALOG    = db_name(),
  19983.         TABLE_SCHEMA    = user_name(o.uid),
  19984.         TABLE_NAME    = o.name,
  19985.         PRIVILEGE_TYPE    = convert(varchar(30),
  19986.                     case v.number
  19987.                     when 193 then 'SELECT'
  19988.                     when 195 then 'INSERT'
  19989.                     when 197 then 'UPDATE'
  19990.                     else 'REFERENCES'
  19991.                     end),
  19992.         IS_GRANTABLE    = convert(bit,1)    
  19993.     from 
  19994.         sysobjects o, master.dbo.spt_values v, sysusers u
  19995.     where
  19996.         o.name = @table_name
  19997.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  19998.     and    (@grantee is null or @grantee = user_name(u.uid))
  19999.     and     o.type in ('U','V')
  20000.     and     u.suid = 1        /* grantor is dbo of database */
  20001.     and    (@grantor is null or @grantor = user_name(u.uid))
  20002.     and     v.type = 'P'    /* cross product to get all exposed privileges */
  20003.     and     v.number in (26,193,195,196,197)
  20004.     and     not exists (    /* exclude revoke'd privileges */
  20005.             select     *
  20006.             from     sysprotects p1
  20007.             where    p1.protecttype = 206
  20008.             and     p1.action = v.number
  20009.             and     p1.id = o.id
  20010.             and     p1.uid = o.uid)
  20011.     order by 4,5,6,1,2
  20012. go
  20013. CREATE PROCEDURE sp_table_privileges_rowset;2
  20014.     (
  20015.     @table_schema        varchar(255) = null,
  20016.     @grantor        varchar(255) = null,
  20017.     @grantee        varchar(255) = null
  20018.     )
  20019. as
  20020.     select
  20021.         GRANTOR        = user_name(p.grantor),
  20022.         GRANTEE        = user_name(u.uid),
  20023.         TABLE_CATALOG    = db_name(),
  20024.         TABLE_SCHEMA    = user_name(o.uid),
  20025.         TABLE_NAME    = o.name,
  20026.         PRIVILEGE_TYPE    = convert(varchar(30),
  20027.                     case p.action
  20028.                     when 193 then 'SELECT'
  20029.                     when 195 then 'INSERT'
  20030.                     when 197 then 'UPDATE'
  20031.                     else 'REFERENCES'
  20032.                     end),
  20033.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  20034.     from 
  20035.         sysprotects p, sysobjects o, sysusers u, sysmembers m
  20036.     where
  20037.         o.type in ('U','V')
  20038.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  20039.     and    (@grantee is null or @grantee = user_name(u.uid))
  20040.     and    p.id = o.id
  20041.     and     (@grantor is null or @grantor = user_name(p.grantor))
  20042.         /* expand groups - AKUNDONE: only 1 level of grp unrolling. */
  20043.     and     (u.uid > 0 and u.uid < 16384)
  20044.     and     ((p.uid = u.uid) or (p.uid = m.groupuid and u.uid = m.memberuid))
  20045.     and     p.protecttype <> 206    /* only grant rows */
  20046.     and     p.action in (26,193,195,196,197)
  20047.     and     o.uid <> u.uid            /* no rows for owner */
  20048.     and     not exists (            /* exclude revoke'd privileges */
  20049.             select     *
  20050.             from     sysprotects p1
  20051.             where    p1.protecttype = 206
  20052.             and     p1.action = p.action
  20053.             and     p1.id = p.id
  20054.             and     p1.uid = u.uid)
  20055.     union all
  20056.     select    /*    Add rows for table owner */
  20057.         GRANTOR        = user_name(u.uid),
  20058.         GRANTEE        = user_name(o.uid),
  20059.         TABLE_CATALOG    = db_name(),
  20060.         TABLE_SCHEMA    = user_name(o.uid),
  20061.         TABLE_NAME    = o.name,
  20062.         PRIVILEGE_TYPE    = convert(varchar(30),
  20063.                     case v.number
  20064.                     when 193 then 'SELECT'
  20065.                     when 195 then 'INSERT'
  20066.                     when 197 then 'UPDATE'
  20067.                     else 'REFERENCES'
  20068.                     end),
  20069.         IS_GRANTABLE    = convert(bit,1)    
  20070.     from 
  20071.         sysobjects o, master.dbo.spt_values v, sysusers u
  20072.     where
  20073.         o.type in ('U','V')
  20074.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  20075.     and    (@grantee is null or @grantee = user_name(u.uid))
  20076.     and     u.suid = 1        /* grantor is dbo of database */
  20077.     and    (@grantor is null or @grantor = user_name(u.uid))
  20078.     and     v.type = 'P'    /* cross product to get all exposed privileges */
  20079.     and     v.number in (26,193,195,196,197)
  20080.     and     not exists (    /* exclude revoke'd privileges */
  20081.             select     *
  20082.             from     sysprotects p1
  20083.             where    p1.protecttype = 206
  20084.             and     p1.action = v.number
  20085.             and     p1.id = o.id
  20086.             and     p1.uid = o.uid)
  20087.     order by 4,5,6,1,2
  20088. go
  20089. CREATE PROCEDURE sp_table_privileges_rowset;3
  20090.     (
  20091.     @table_name         varchar(255) = null,
  20092.     @table_schema        varchar(255) = null,
  20093.     @grantor        varchar(255) = null,
  20094.     @grantee        varchar(255) = null
  20095.     )
  20096. as
  20097. IF @table_name is not null
  20098.     BEGIN
  20099.     select
  20100.         GRANTOR        = user_name(p.grantor),
  20101.         GRANTEE        = user_name(u.uid),
  20102.         TABLE_CATALOG    = db_name(),
  20103.         TABLE_SCHEMA    = user_name(o.uid),
  20104.         TABLE_NAME    = o.name,
  20105.         PRIVILEGE_TYPE    = convert(varchar(30),
  20106.                     case p.action
  20107.                     when 193 then 'SELECT'
  20108.                     when 195 then 'INSERT'
  20109.                     when 197 then 'UPDATE'
  20110.                     else 'REFERENCES'
  20111.                     end),
  20112.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  20113.     from 
  20114.         sysprotects p, sysobjects o, sysusers u, sysmembers m
  20115.     where
  20116.         o.name = @table_name
  20117.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  20118.     and    (@grantee is null or @grantee = user_name(u.uid))
  20119.     and     o.type in ('U','V')
  20120.     and    p.id = o.id
  20121.     and     (@grantor is null or @grantor = user_name(p.grantor))
  20122.         /* expand groups - AKUNDONE: only 1 level of grp unrolling. */
  20123.     and     (u.uid > 0 and u.uid < 16384)
  20124.     and     ((p.uid = u.uid) or (p.uid = m.groupuid and u.uid = m.memberuid))
  20125.     and     p.protecttype <> 206    /* only grant rows */
  20126.     and     p.action in (26,193,195,196,197)
  20127.     and     o.uid <> u.uid            /* no rows for owner */
  20128.     and     not exists (            /* exclude revoke'd privileges */
  20129.             select     *
  20130.             from     sysprotects p1
  20131.             where    p1.protecttype = 206
  20132.             and     p1.action = p.action
  20133.             and     p1.id = p.id
  20134.             and     p1.uid = u.uid)
  20135.     union all
  20136.     select    /*    Add rows for table owner */
  20137.         GRANTOR        = user_name(u.uid),
  20138.         GRANTEE        = user_name(o.uid),
  20139.         TABLE_CATALOG    = db_name(),
  20140.         TABLE_SCHEMA    = user_name(o.uid),
  20141.         TABLE_NAME    = o.name,
  20142.         PRIVILEGE_TYPE    = convert(varchar(30),
  20143.                     case v.number
  20144.                     when 193 then 'SELECT'
  20145.                     when 195 then 'INSERT'
  20146.                     when 197 then 'UPDATE'
  20147.                     else 'REFERENCES'
  20148.                     end),
  20149.         IS_GRANTABLE    = convert(bit,1)    
  20150.     from 
  20151.         sysobjects o, master.dbo.spt_values v, sysusers u
  20152.     where
  20153.         o.name = @table_name
  20154.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  20155.     and    (@grantee is null or @grantee = user_name(u.uid))
  20156.     and     o.type in ('U','V')
  20157.     and     u.suid = 1        /* grantor is dbo of database */
  20158.     and    (@grantor is null or @grantor = user_name(u.uid))
  20159.     and     v.type = 'P'    /* cross product to get all exposed privileges */
  20160.     and     v.number in (26,193,195,196,197)
  20161.     and     not exists (    /* exclude revoke'd privileges */
  20162.             select     *
  20163.             from     sysprotects p1
  20164.             where    p1.protecttype = 206
  20165.             and     p1.action = v.number
  20166.             and     p1.id = o.id
  20167.             and     p1.uid = o.uid)
  20168.     END
  20169. ELSE
  20170.     BEGIN
  20171.     select
  20172.         GRANTOR        = user_name(p.grantor),
  20173.         GRANTEE        = user_name(u.uid),
  20174.         TABLE_CATALOG    = db_name(),
  20175.         TABLE_SCHEMA    = user_name(o.uid),
  20176.         TABLE_NAME    = o.name,
  20177.         PRIVILEGE_TYPE    = convert(varchar(30),
  20178.                     case p.action
  20179.                     when 193 then 'SELECT'
  20180.                     when 195 then 'INSERT'
  20181.                     when 197 then 'UPDATE'
  20182.                     else 'REFERENCES'
  20183.                     end),
  20184.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  20185.     from 
  20186.         sysprotects p, sysobjects o, sysusers u, sysmembers m
  20187.     where
  20188.         o.type in ('U','V')
  20189.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  20190.     and    (@grantee is null or @grantee = user_name(u.uid))
  20191.     and    p.id = o.id
  20192.     and     (@grantor is null or @grantor = user_name(p.grantor))
  20193.         /* expand groups - AKUNDONE: only 1 level of grp unrolling. */
  20194.     and     (u.uid > 0 and u.uid < 16384)
  20195.     and     ((p.uid = u.uid) or (p.uid = m.groupuid and u.uid = m.memberuid))
  20196.     and     p.protecttype <> 206    /* only grant rows */
  20197.     and     p.action in (26,193,195,196,197)
  20198.     and     o.uid <> u.uid            /* no rows for owner */
  20199.     and     not exists (            /* exclude revoke'd privileges */
  20200.             select     *
  20201.             from     sysprotects p1
  20202.             where    p1.protecttype = 206
  20203.             and     p1.action = p.action
  20204.             and     p1.id = p.id
  20205.             and     p1.uid = u.uid)
  20206.     union all
  20207.     select    /*    Add rows for table owner */
  20208.         GRANTOR        = user_name(u.uid),
  20209.         GRANTEE        = user_name(o.uid),
  20210.         TABLE_CATALOG    = db_name(),
  20211.         TABLE_SCHEMA    = user_name(o.uid),
  20212.         TABLE_NAME    = o.name,
  20213.         PRIVILEGE_TYPE    = convert(varchar(30),
  20214.                     case v.number
  20215.                     when 193 then 'SELECT'
  20216.                     when 195 then 'INSERT'
  20217.                     when 197 then 'UPDATE'
  20218.                     else 'REFERENCES'
  20219.                     end),
  20220.         IS_GRANTABLE    = convert(bit,1)    
  20221.     from 
  20222.         sysobjects o, master.dbo.spt_values v, sysusers u
  20223.     where
  20224.         o.type in ('U','V')
  20225.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  20226.     and    (@grantee is null or @grantee = user_name(u.uid))
  20227.     and     u.suid = 1        /* grantor is dbo of database */
  20228.     and    (@grantor is null or @grantor = user_name(u.uid))
  20229.     and     v.type = 'P'    /* cross product to get all exposed privileges */
  20230.     and     v.number in (26,193,195,196,197)
  20231.     and     not exists (    /* exclude revoke'd privileges */
  20232.             select     *
  20233.             from     sysprotects p1
  20234.             where    p1.protecttype = 206
  20235.             and     p1.action = v.number
  20236.             and     p1.id = o.id
  20237.             and     p1.uid = o.uid)
  20238.     END
  20239. go
  20240.  
  20241.  
  20242. create procedure sp_table_privileges_rowset;4
  20243.     (
  20244.        @handle        int output,
  20245.        @scrollopt    int output,
  20246.     @ccopt        int output,
  20247.     @rows        int output,
  20248.     @cmd        varchar(255)
  20249.     )
  20250. as
  20251.     declare @ret int
  20252.     declare @database_name varchar(30)
  20253.  
  20254.     create table #sptprivsrowset
  20255.         (
  20256.         GRANTOR        varchar(30) not null,
  20257.         GRANTEE        varchar(30) not null,
  20258.         TABLE_CATALOG    varchar(30) not null,
  20259.         TABLE_SCHEMA    varchar(30) not null,
  20260.         TABLE_NAME    varchar(30) not null,
  20261.         PRIVILEGE_TYPE    varchar(30) not null,
  20262.         IS_GRANTABLE    bit not null
  20263.         )
  20264.  
  20265.     declare sysdatabases_cursor insensitive cursor for 
  20266.         select name from master.dbo.sysdatabases
  20267.   
  20268.     set nocount on
  20269.     open sysdatabases_cursor
  20270.       fetch next from sysdatabases_cursor into @database_name 
  20271.     while @@fetch_status = 0
  20272.         begin
  20273.         exec('use '+@database_name+' insert into #sptprivsrowset '+@cmd)
  20274.         fetch next from sysdatabases_cursor into @database_name 
  20275.         end
  20276.       close sysdatabases_cursor
  20277.     deallocate sysdatabases_cursor
  20278.  
  20279.     if @scrollopt is null
  20280.         begin
  20281.         select * from #sptprivsrowset order by 3,4,5,6,1,2
  20282.         end
  20283.     else
  20284.         begin
  20285.         exec @ret = sp_cursoropen @handle output,
  20286.             'select * from #sptprivsrowset order by 3,4,5,6,1,2',
  20287.                 @scrollopt output, @ccopt output, @rows output
  20288.         end
  20289.     drop table #sptprivsrowset
  20290.     return isnull(@ret,0)
  20291. go
  20292.  
  20293.  
  20294. grant execute on sp_table_privileges_rowset to public
  20295. go
  20296.  
  20297. -------------------------------------------------------------------------------
  20298. -------------- CATALOG STORED PROCEDURES FOR OLEDB SOURCES --------------------
  20299. -------------------------------------------------------------------------------
  20300.  
  20301. if object_id('sp_linkedservers', 'P') is not null
  20302.     drop proc sp_linkedservers
  20303. go
  20304. raiserror(15339,-1,-1,'sp_linkedservers')
  20305. go
  20306. create proc sp_linkedservers as
  20307.     select
  20308.         SRV_NAME = srvname,
  20309.         SRV_PROVIDER = convert(sysname, 'MSDASQL'),
  20310.         SRV_PRODUCTNAME = convert(sysname, 'SQL Server 7.0'),
  20311.         SRV_DATASOURCE = convert(nvarchar(255), srvnetname),
  20312.         SRV_LOCATION = convert(nvarchar(255), null)
  20313.     from sysservers
  20314. go
  20315. grant execute on sp_linkedservers to public
  20316. go
  20317.  
  20318.  
  20319. if object_id('sp_catalogs', 'P') is not null
  20320.     drop proc sp_catalogs
  20321. go
  20322. raiserror(15339,-1,-1,'sp_catalogs')
  20323. go
  20324. create procedure sp_catalogs(
  20325.     @server_name        sysname)
  20326. as
  20327.     select
  20328.         CATALOG_NAME,
  20329.         convert (nvarchar(255),DESCRIPTION)
  20330.     from master.dbo.SYSREMOTE_CATALOGS < @server_name >
  20331.     order by CATALOG_NAME
  20332. go
  20333. grant execute on sp_catalogs to public
  20334. go
  20335.  
  20336.  
  20337. if object_id('sp_tables_ex', 'P') is not null
  20338.     drop proc sp_tables_ex
  20339. go
  20340. raiserror(15339,-1,-1,'sp_tables_ex')
  20341. go
  20342. create procedure sp_tables_ex(
  20343.     @table_server        sysname,
  20344.     @table_name            sysname = null,
  20345.     @table_schema        sysname = null,
  20346.     @table_catalog        sysname = null,
  20347.     @table_type            sysname = null)
  20348. as
  20349.     select
  20350.         TABLE_CAT = TABLE_CATALOG,
  20351.         TABLE_SCHEM = TABLE_SCHEMA,
  20352.         TABLE_NAME = TABLE_NAME,
  20353.         TABLE_TYPE = TABLE_TYPE,
  20354.         REMARKS = convert(nvarchar(255),DESCRIPTION)
  20355.     from master.dbo.SYSREMOTE_TABLES <
  20356.                 @table_server,
  20357.                 @table_catalog,
  20358.                 @table_schema,
  20359.                 @table_name,
  20360.                 @table_type >
  20361.     order by TABLE_TYPE, TABLE_CAT, TABLE_SCHEM, TABLE_NAME
  20362. go
  20363. grant execute on sp_tables_ex to public
  20364. go
  20365.  
  20366.  
  20367. if object_id('sp_columns_ex', 'P') is not null
  20368.     drop proc sp_columns_ex
  20369. go
  20370. raiserror(15339,-1,-1,'sp_columns_ex')
  20371. go
  20372. create procedure sp_columns_ex(
  20373.     @table_server        sysname,
  20374.     @table_name            sysname = null,
  20375.     @table_schema        sysname = null,
  20376.     @table_catalog        sysname = null,
  20377.     @column_name        sysname = null)
  20378. as
  20379.     select
  20380.         TABLE_CAT = c.TABLE_CATALOG,
  20381.         TABLE_SCHEM = c.TABLE_SCHEMA,
  20382.         TABLE_NAME = c.TABLE_NAME,
  20383.         COLUMN_NAME = c.COLUMN_NAME,
  20384.         DATA_TYPE = c.DATA_TYPE,
  20385.         TYPE_NAME = p.TYPE_NAME,
  20386.         COLUMN_SIZE = c.NUMERIC_PRECISION,
  20387.         BUFFER_LENGTH = c.CHARACTER_OCTET_LENGTH,
  20388.         DECIMAL_DIGITS = c.NUMERIC_SCALE,
  20389.         NUM_PREC_RADIX = convert(smallint, 10),
  20390.         NULLABLE = convert(smallint, c.IS_NULLABLE),
  20391.         REMARKS = convert(nvarchar(255),c.DESCRIPTION),
  20392.         COLUMN_DEF = c.COLUMN_DEFAULT,
  20393.         SQL_DATA_TYPE = c.DATA_TYPE,
  20394.         SQL_DATETIME_SUB = convert(smallint, c.DATETIME_PRECISION),
  20395.         CHAR_OCTET_LENGTH = c.CHARACTER_OCTET_LENGTH,
  20396.         ORDINAL_POSITION = c.ORDINAL_POSITION,
  20397.         IS_NULLABLE = c.IS_NULLABLE,
  20398.         SS_DATA_TYPE = convert(tinyint, XTypeToTds(c.DATA_TYPE, 0))
  20399.     from master.dbo.SYSREMOTE_COLUMNS <
  20400.                 @table_server,
  20401.                 @table_catalog,
  20402.                 @table_schema,
  20403.                 @table_name,
  20404.                 @column_name > c
  20405.          ,master.dbo.SYSREMOTE_PROVIDER_TYPES <
  20406.                 @table_server,
  20407.                 NULL,           -- DATA_TYPE
  20408.                 NULL    -- 1> p            -- BEST_MATCH
  20409.                         -- UNDONE: BUGBUG: Kagera needs to fix BEST_MATCH so
  20410.                         --  we get only one PROVIDER_TYPES row for each column!
  20411.                 > p
  20412.     where c.DATA_TYPE = p.DATA_TYPE
  20413.     order by TABLE_CAT, TABLE_SCHEM, TABLE_NAME, ORDINAL_POSITION
  20414. go
  20415. grant execute on sp_columns_ex to public
  20416. go
  20417.  
  20418.  
  20419. if object_id('sp_table_privileges_ex', 'P') is not null
  20420.     drop proc sp_table_privileges_ex
  20421. go
  20422. raiserror(15339,-1,-1,'sp_table_privileges_ex')
  20423. go
  20424. create procedure sp_table_privileges_ex(
  20425.     @table_server        sysname,
  20426.     @table_name            sysname = null,
  20427.     @table_schema        sysname = null,
  20428.     @table_catalog        sysname = null)
  20429. as
  20430.     select
  20431.         TABLE_CAT = TABLE_CATALOG,
  20432.         TABLE_SCHEM = TABLE_SCHEMA,
  20433.         TABLE_NAME = TABLE_NAME,
  20434.         GRANTOR = GRANTOR,
  20435.         GRANTEE = GRANTEE,
  20436.         PRIVILEGE = PRIVILEGE_TYPE,
  20437.         IS_GRANTABLE = IS_GRANTABLE
  20438.     from master.dbo.SYSREMOTE_TABLE_PRIVILEGES <
  20439.                 @table_server,
  20440.                 @table_catalog,
  20441.                 @table_schema,
  20442.                 @table_name >
  20443.     order by TABLE_CAT, TABLE_SCHEM, TABLE_NAME, PRIVILEGE
  20444. go
  20445. grant execute on sp_table_privileges_ex to public
  20446. go
  20447.  
  20448.  
  20449. if object_id('sp_column_privileges_ex', 'P') is not null
  20450.     drop proc sp_column_privileges_ex
  20451. go
  20452. raiserror(15339,-1,-1,'sp_column_privileges_ex')
  20453. go
  20454. create procedure sp_column_privileges_ex(
  20455.     @table_server        sysname,
  20456.     @table_name            sysname = null,
  20457.     @table_schema        sysname = null,
  20458.     @table_catalog        sysname = null,
  20459.     @column_name        sysname = null)
  20460. as
  20461.     select
  20462.         TABLE_CAT = TABLE_CATALOG,
  20463.         TABLE_SCHEM = TABLE_SCHEMA,
  20464.         TABLE_NAME = TABLE_NAME,
  20465.         COLUMN_NAME = COLUMN_NAME,
  20466.         GRANTOR = GRANTOR,
  20467.         GRANTEE = GRANTEE,
  20468.         PRIVILEGE = PRIVILEGE_TYPE,
  20469.         IS_GRANTABLE = IS_GRANTABLE
  20470.     from master.dbo.SYSREMOTE_COLUMN_PRIVILEGES <
  20471.                 @table_server,
  20472.                 @table_catalog,
  20473.                 @table_schema,
  20474.                 @table_name,
  20475.                 @column_name >
  20476.     order by TABLE_CAT, TABLE_SCHEM, TABLE_NAME, COLUMN_NAME, PRIVILEGE
  20477. go
  20478. grant execute on sp_column_privileges_ex to public
  20479. go
  20480.  
  20481.  
  20482. if object_id('sp_indexes', 'P') is not null
  20483.     drop proc sp_indexes
  20484. go
  20485. raiserror(15339,-1,-1,'sp_indexes')
  20486. go
  20487. create procedure sp_indexes(
  20488.     @table_server        sysname,
  20489.     @table_name            sysname = null,
  20490.     @table_schema        sysname = null,
  20491.     @table_catalog        sysname = null,
  20492.     @index_name            sysname = null,
  20493.     @is_unique          bit = null)
  20494. as
  20495.     select
  20496.         TABLE_CAT = TABLE_CATALOG,
  20497.         TABLE_SCHEM = TABLE_SCHEMA,
  20498.         TABLE_NAME = TABLE_NAME,
  20499.         NON_UNIQUE = convert(smallint, 1 - [UNIQUE]),
  20500.         INDEX_NAME = INDEX_NAME,
  20501.         INDEX_QUALIFIER = INDEX_SCHEMA,
  20502.         TYPE = TYPE,
  20503.         ORDINAL_POSITION = ORDINAL_POSITION,
  20504.         COLUMN_NAME = COLUMN_NAME,
  20505.         ASC_OR_DESC = [COLLATION],
  20506.         CARDINALITY = CARDINALITY,
  20507.         PAGES = PAGES,
  20508.         FILTER_CONDITION = FILTER_CONDITION
  20509.     from master.dbo.SYSREMOTE_INDEXES <
  20510.                 @table_server,
  20511.                 @table_catalog,
  20512.                 @table_schema,
  20513.                 @index_name,
  20514.                 NULL,           -- TYPE (index type)
  20515.                 @table_name >
  20516.     where @is_unique is null or @is_unique = [UNIQUE]
  20517.     order by NON_UNIQUE, TYPE, INDEX_QUALIFIER, INDEX_NAME, ORDINAL_POSITION
  20518. go
  20519. grant execute on sp_indexes to public
  20520. go
  20521.  
  20522.  
  20523. if object_id('sp_foreignkeys', 'P') is not null
  20524.     drop proc sp_foreignkeys
  20525. go
  20526. raiserror(15339,-1,-1,'sp_foreignkeys')
  20527. go
  20528. create procedure sp_foreignkeys(
  20529.     @table_server        sysname,
  20530.     @pktab_name         sysname = null,
  20531.     @pktab_schema       sysname = null,
  20532.     @pktab_catalog      sysname = null,
  20533.     @fktab_name         sysname = null,
  20534.     @fktab_schema       sysname = null,
  20535.     @fktab_catalog      sysname = null)
  20536. as
  20537.     select
  20538.         PKTABLE_CAT = PK_TABLE_CATALOG,
  20539.         PKTABLE_SCHEM = PK_TABLE_SCHEMA,
  20540.         PKTABLE_NAME = PK_TABLE_NAME,
  20541.         PKCOLUMN_NAME = PK_COLUMN_NAME,
  20542.         FKTABLE_CAT = FK_TABLE_CATALOG,
  20543.         FKTABLE_SCHEM = FK_TABLE_SCHEMA,
  20544.         FKTABLE_NAME = FK_TABLE_NAME,
  20545.         FKCOLUMN_NAME = FK_COLUMN_NAME,
  20546.         KEY_SEQ = ORDINAL,
  20547.         UPDATE_RULE = UPDATE_RULE,
  20548.         DELETE_RULE = DELETE_RULE,
  20549.         FK_NAME = convert(sysname, NULL),
  20550.         PK_NAME = convert(sysname, NULL)
  20551.     from master.dbo.SYSREMOTE_FOREIGN_KEYS <
  20552.                 @table_server,
  20553.                 @pktab_catalog,
  20554.                 @pktab_schema,
  20555.                 @pktab_name,
  20556.                 @fktab_catalog,
  20557.                 @fktab_schema,
  20558.                 @fktab_name >
  20559.     order by FKTABLE_CAT, FKTABLE_SCHEM, FKTABLE_NAME, KEY_SEQ
  20560. go
  20561. grant execute on sp_foreignkeys to public
  20562. go
  20563.  
  20564.  
  20565. if object_id('sp_primarykeys', 'P') is not null
  20566.     drop proc sp_primarykeys
  20567. go
  20568. raiserror(15339,-1,-1,'sp_primarykeys')
  20569. go
  20570. create procedure sp_primarykeys(
  20571.     @table_server        sysname,
  20572.     @table_name         sysname = null,
  20573.     @table_schema       sysname = null,
  20574.     @table_catalog      sysname = null)
  20575. as
  20576.     select
  20577.         TABLE_CAT = TABLE_CATALOG,
  20578.         TABLE_SCHEM = TABLE_SCHEMA,
  20579.         TABLE_NAME = TABLE_NAME,
  20580.         COLUMN_NAME = COLUMN_NAME,
  20581.         KEY_SEQ = ORDINAL,
  20582.         PK_NAME = convert(sysname, NULL)
  20583.     from master.dbo.SYSREMOTE_PRIMARY_KEYS <
  20584.                 @table_server,
  20585.                 @table_catalog,
  20586.                 @table_schema,
  20587.                 @table_name >
  20588.     order by TABLE_CAT, TABLE_SCHEM, TABLE_NAME, KEY_SEQ
  20589. go
  20590. grant execute on sp_primarykeys to public
  20591. go
  20592.  
  20593. ---------------------------- END OLEDB CATALOG PROCS --------------------------
  20594.  
  20595.  
  20596. dump tran master with no_log
  20597. go
  20598.  
  20599.  
  20600.  
  20601.  
  20602. if (exists (select * from sysobjects
  20603.         where name = 'sp_configure' and type = 'P '))
  20604.     begin
  20605.         exec sp_configure 'allow updates',0
  20606.         reconfigure with override
  20607.     end
  20608. go
  20609.  
  20610. exec sp_MS_upd_sysobj_category 2 /* set category | 2 based on crdate. */
  20611.  
  20612. go
  20613.  
  20614. if exists (select * from sysobjects where name = 'sp_check_objects'
  20615.             and type = 'P ')
  20616.     begin
  20617.         /* Only supported on 6.0 servers */
  20618.         print ''
  20619.         print 'Checking objects created by instcat.sql.'
  20620.  
  20621.         exec sp_check_objects 'catalog'
  20622.     end
  20623. go
  20624.  
  20625. print ''
  20626. print 'instcat.sql completed successfully.'
  20627. go
  20628.  
  20629. set quoted_identifier off
  20630. go
  20631.  
  20632. dump tran master with no_log
  20633. go
  20634. checkpoint
  20635. go
  20636. /**/
  20637.