home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 9 / IOPROG_9.ISO / contrib / iis4 / sqlsrv.cab / instcat.sql < prev    next >
Encoding:
Text File  |  1997-08-11  |  639.2 KB  |  20,636 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.297')
  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.              where p.id = o.id
  5252.                  /*  get rows for public,current user,user's group */
  5253.                  and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  5254.                  /* check for SELECT,EXECUTE privilege */
  5255.                  and (action in (193,224)))&1    /* more magic...normalize GRANT */
  5256.             ) = 1     /* final magic...compare Grants    */
  5257.         )
  5258.     order by 1, 2, 3
  5259. go
  5260.  
  5261. grant execute on sp_stored_procedures to public
  5262. go
  5263.  
  5264.  
  5265. if (charindex('7.00', @@version) > 0)
  5266.     drop procedure sp_stored_procedures
  5267. else
  5268. begin
  5269.     print ''
  5270.     print ''
  5271.     print 'Warning:'
  5272.     print 'you are installing the stored procedures '
  5273.     print 'on a pre 7.0 SQL Server.'
  5274.     print 'Ignore the following errors.'
  5275. end
  5276. go
  5277.  
  5278.  
  5279. /* 7.00 version */
  5280. create procedure sp_stored_procedures(
  5281.                         @sp_name        varchar(390) = null,
  5282.                         @sp_owner        varchar(384) = null,
  5283.                         @sp_qualifier    sysname = null)
  5284. as
  5285.     declare @proc_type smallint
  5286.  
  5287.     if @sp_qualifier is not null
  5288.     begin
  5289.         if db_name() <> @sp_qualifier
  5290.         begin
  5291.             if @sp_qualifier = ''
  5292.             begin
  5293.                 /* in this case, we need to return an empty result set */
  5294.                 /* because the user has requested a database with an empty name */
  5295.                 select @sp_name = ''
  5296.                 select @sp_owner = ''
  5297.             end else
  5298.             begin    /* If qualifier doesn't match current database */
  5299.                 raiserror 20001 'Procedure qualifier must be name of current database'
  5300.                 return
  5301.             end
  5302.         end
  5303.     end
  5304.  
  5305.     if @sp_name is null
  5306.     begin  /*  If procedure name not supplied, match all */
  5307.         select @sp_name = '%'
  5308.     end
  5309.     else begin
  5310.         if (@sp_owner is null) and (charindex('%', @sp_name) = 0)
  5311.         begin
  5312.             if exists (select * from sysobjects
  5313.                 where uid = user_id()
  5314.                     and name = @sp_name
  5315.                     and type = 'P') /* Object type of Procedure */
  5316.             begin
  5317.                 select @sp_owner = user_name()
  5318.             end
  5319.         end
  5320.     end
  5321.     if @sp_owner is null    /*    If procedure owner not supplied, match all */
  5322.         select @sp_owner = '%'
  5323.  
  5324.     select @proc_type=2        /* Return 2 for 4.2 and later servers. */
  5325.  
  5326.     select
  5327.         PROCEDURE_QUALIFIER = convert(sysname,db_name()),
  5328.         PROCEDURE_OWNER = convert(sysname,user_name(o.uid)),
  5329.         PROCEDURE_NAME = convert(varchar(134),o.name +';'+ ltrim(str(c.number,5))),
  5330.         NUM_INPUT_PARAMS = -1,    /* Constant since value unknown */
  5331.         NUM_OUTPUT_PARAMS = -1, /* Constant since value unknown */
  5332.         NUM_RESULT_SETS = -1,    /* Constant since value unknown */
  5333.         REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  5334.         PROCEDURE_TYPE = @proc_type
  5335.     from
  5336.         sysobjects o,syscomments c
  5337.     where
  5338.         o.name like @sp_name
  5339.         and c.colid = 1
  5340.         and user_name(o.uid) like @sp_owner
  5341.         and o.type = 'P'        /* Object type of Procedure */
  5342.         and c.id = o.id
  5343.         and permissions (o.id)&32 <> 0
  5344.     order by 1, 2, 3
  5345. go
  5346.  
  5347. grant execute on sp_stored_procedures to public
  5348. go
  5349.  
  5350. dump tran master with no_log
  5351. go
  5352.  
  5353.  
  5354. print 'creating sp_table_privileges'
  5355. go
  5356.  
  5357. /*    Procedure for pre 6.50 server */
  5358. CREATE PROCEDURE sp_table_privileges (
  5359.             @table_name         varchar(96),        /* 3*32 */
  5360.             @table_owner        varchar(96) = null,
  5361.             @table_qualifier    varchar(32) = null)
  5362. as
  5363.  
  5364.     if @table_qualifier is not null
  5365.     begin
  5366.         if db_name() <> @table_qualifier
  5367.         begin    /* If qualifier doesn't match current database */
  5368.             raiserror 20001 'Table qualifier must be name of current database'
  5369.             return
  5370.         end
  5371.     end
  5372.     if (@@trancount <> 0)
  5373.     begin    /* If inside a transaction */
  5374.         raiserror 20003 'The procedure ''sp_table_privileges'' cannot be executed from within a transaction.'
  5375.         return
  5376.     end
  5377.     if @table_name is null
  5378.         select @table_name = '%'
  5379.     if @table_owner is null /* If no owner supplied, force wildcard */
  5380.         select @table_owner = '%'
  5381.  
  5382.     create table #table_priv1(
  5383.         id                        int NOT NULL,
  5384.         grantor                 smallint NOT NULL,
  5385.         grantee                 smallint NOT NULL,
  5386.         select_privilege        bit,
  5387.         insert_privilege        bit,
  5388.         update_privilege        bit,
  5389.         delete_privilege        bit,
  5390.         references_privilege    bit,
  5391.         select_privilege_grant    bit,
  5392.         insert_privilege_grant    bit,
  5393.         update_privilege_grant    bit,
  5394.         delete_privilege_grant    bit,
  5395.         references_privilege_grant    bit,
  5396.         uid                     smallint NOT NULL,
  5397.         gid                     smallint NOT NULL)
  5398.  
  5399.     insert into #table_priv1
  5400.         select distinct
  5401.             o.id,
  5402.             o.uid,
  5403.             u.uid,
  5404.             0,
  5405.             0,
  5406.             0,
  5407.             0,
  5408.             0,
  5409.             0,
  5410.             0,
  5411.             0,
  5412.             0,
  5413.             0,
  5414.             u.uid,
  5415.             u.gid
  5416.         from sysusers u, sysobjects o, sysprotects p
  5417.         where
  5418.             o.name like @table_name
  5419.             and user_name(o.uid) like @table_owner
  5420.             and u.uid <> u.gid
  5421.             and u.uid *= p.uid and o.id *= p.id
  5422.             and p.protecttype in (204, 205)
  5423.             and type in ('S ','U ','V ')    /* only valid for system tables,
  5424.                                             ** user tables, and views. */
  5425.  
  5426.     /*
  5427.     ** now add/update row for table owner
  5428.     */
  5429.     if exists (
  5430.         select *
  5431.             from #table_priv1
  5432.             where grantor = grantee)
  5433.     begin
  5434.         update #table_priv1
  5435.         set
  5436.             select_privilege_grant = 1,
  5437.             update_privilege_grant = 1,
  5438.             insert_privilege_grant = 1,
  5439.             delete_privilege_grant = 1,
  5440.             references_privilege_grant = 1,
  5441.             grantor = (select uid from sysusers where suid = 1)
  5442.         where grantor = grantee
  5443.     end
  5444.     else
  5445.     begin
  5446.         insert into #table_priv1
  5447.             select
  5448.                 o.id,
  5449.                 u1.uid,
  5450.                 o.uid,
  5451.                 0,
  5452.                 0,
  5453.                 0,
  5454.                 0,
  5455.                 0,
  5456.                 1,
  5457.                 1,
  5458.                 1,
  5459.                 1,
  5460.                 1,
  5461.                 o.uid,
  5462.                 u.gid
  5463.             from sysobjects o, sysusers u, sysusers u1
  5464.             where o.name like @table_name
  5465.             and user_name(o.uid) like @table_owner
  5466.             and u.uid = o.uid
  5467.             and type in ('S ','U ','V ')    /* only valid for system tables,
  5468.                                            ** user tables, and views. */
  5469.             and u1.suid = 1        /* grantor is dbo of database */
  5470.  
  5471.     end
  5472.  
  5473.     update #table_priv1
  5474.     set select_privilege = 1
  5475.         from sysprotects p
  5476.     where
  5477.         #table_priv1.id = p.id
  5478.         and (#table_priv1.uid = p.uid
  5479.             or #table_priv1.gid = p.uid
  5480.             or p.uid = 0)
  5481.         and protecttype = 205
  5482.         and action = 193
  5483.         and not exists (
  5484.             select * from sysprotects
  5485.             where
  5486.                 #table_priv1.id = sysprotects.id
  5487.                 and (#table_priv1.uid = uid
  5488.                     or #table_priv1.gid = uid
  5489.                     or uid = 0)
  5490.                 and protecttype = 206
  5491.                 and action = 193)
  5492.  
  5493.     update #table_priv1
  5494.     set insert_privilege = 1
  5495.         from sysprotects p
  5496.     where
  5497.         #table_priv1.id = p.id
  5498.         and (#table_priv1.uid = p.uid
  5499.             or #table_priv1.gid = p.uid
  5500.             or p.uid = 0)
  5501.         and protecttype = 205
  5502.         and action = 195
  5503.         and not exists (
  5504.             select * from sysprotects
  5505.             where
  5506.                 #table_priv1.id = sysprotects.id
  5507.                 and (#table_priv1.uid = uid
  5508.                     or #table_priv1.gid = uid
  5509.                     or uid = 0)
  5510.                 and protecttype = 206
  5511.                 and action = 195)
  5512.  
  5513.     update #table_priv1
  5514.     set delete_privilege = 1
  5515.         from sysprotects p
  5516.     where
  5517.         exists (
  5518.             select * from sysprotects
  5519.             where
  5520.                 #table_priv1.id = sysprotects.id
  5521.                 and (#table_priv1.uid = uid
  5522.                     or #table_priv1.gid = uid
  5523.                     or uid = 0)
  5524.                 and protecttype = 205
  5525.                 and action = 196)
  5526.         and not exists (select * from sysprotects
  5527.             where
  5528.                 #table_priv1.id = sysprotects.id
  5529.                 and (#table_priv1.uid = uid
  5530.                     or #table_priv1.gid = uid
  5531.                     or uid = 0)
  5532.                 and protecttype = 206
  5533.                 and action = 196)
  5534.  
  5535.     update #table_priv1
  5536.     set update_privilege = 1
  5537.         from sysprotects p
  5538.     where
  5539.         #table_priv1.id = p.id
  5540.         and (#table_priv1.uid = p.uid
  5541.             or #table_priv1.gid = p.uid
  5542.             or p.uid = 0)
  5543.         and protecttype = 205
  5544.         and action = 197
  5545.         and not exists (
  5546.             select * from sysprotects
  5547.             where
  5548.                 #table_priv1.id = sysprotects.id
  5549.                 and (#table_priv1.uid = uid
  5550.                     or #table_priv1.gid = uid
  5551.                     or uid = 0)
  5552.                 and protecttype = 206
  5553.                 and action = 197)
  5554.  
  5555.     update #table_priv1
  5556.     set references_privilege = 1
  5557.         from sysprotects p
  5558.     where
  5559.         #table_priv1.id = p.id
  5560.         and (#table_priv1.uid = p.uid
  5561.             or #table_priv1.gid = p.uid
  5562.             or p.uid = 0)
  5563.         and protecttype = 205
  5564.         and action = 26
  5565.         and not exists (
  5566.             select * from sysprotects
  5567.             where
  5568.                 #table_priv1.id = sysprotects.id
  5569.                 and (#table_priv1.uid = uid
  5570.                     or #table_priv1.gid = uid
  5571.                     or uid = 0)
  5572.                 and protecttype = 206
  5573.                 and action = 26)
  5574.  
  5575.     create table #table_priv2(
  5576.         id                int NOT NULL,
  5577.         grantor         smallint NOT NULL,
  5578.         grantee         smallint NOT NULL,
  5579.         PRIVILEGE        varchar(32) NOT NULL,
  5580.         IS_GRANTABLE    varchar(3) NULL)
  5581.  
  5582.     insert into #table_priv2
  5583.         select
  5584.             id,
  5585.             grantor,
  5586.             grantee,
  5587.             'SELECT',
  5588.             'NO'
  5589.         from #table_priv1
  5590.         where select_privilege = 1 and select_privilege_grant = 0
  5591.  
  5592.     insert into #table_priv2
  5593.         select
  5594.             id,
  5595.             grantor,
  5596.             grantee,
  5597.             'INSERT',
  5598.             'NO'
  5599.         from #table_priv1
  5600.         where insert_privilege = 1 and insert_privilege_grant = 0
  5601.  
  5602.     insert into #table_priv2
  5603.         select
  5604.             id,
  5605.             grantor,
  5606.             grantee,
  5607.             'DELETE',
  5608.             'NO'
  5609.         from #table_priv1
  5610.         where delete_privilege = 1 and delete_privilege_grant = 0
  5611.  
  5612.     insert into #table_priv2
  5613.         select
  5614.             id,
  5615.             grantor,
  5616.             grantee,
  5617.             'UPDATE',
  5618.             'NO'
  5619.         from #table_priv1
  5620.         where update_privilege = 1 and update_privilege_grant = 0
  5621.  
  5622.     insert into #table_priv2
  5623.         select
  5624.             id,
  5625.             grantor,
  5626.             grantee,
  5627.             'REFERENCES',
  5628.             'NO'
  5629.         from #table_priv1
  5630.         where references_privilege = 1 and references_privilege_grant = 0
  5631.  
  5632.     insert into #table_priv2
  5633.         select
  5634.             id,
  5635.             grantor,
  5636.             grantee,
  5637.             'SELECT',
  5638.             'YES'
  5639.         from #table_priv1
  5640.         where select_privilege_grant = 1
  5641.  
  5642.     insert into #table_priv2
  5643.         select
  5644.             id,
  5645.             grantor,
  5646.             grantee,
  5647.             'INSERT',
  5648.             'YES'
  5649.         from #table_priv1
  5650.         where insert_privilege_grant = 1
  5651.  
  5652.     insert into #table_priv2
  5653.         select
  5654.             id,
  5655.             grantor,
  5656.             grantee,
  5657.             'DELETE',
  5658.             'YES'
  5659.         from #table_priv1
  5660.         where delete_privilege_grant = 1
  5661.  
  5662.     insert into #table_priv2
  5663.         select
  5664.             id,
  5665.             grantor,
  5666.             grantee,
  5667.             'UPDATE',
  5668.             'YES'
  5669.         from #table_priv1
  5670.         where update_privilege_grant = 1
  5671.  
  5672.     insert into #table_priv2
  5673.         select
  5674.             id,
  5675.             grantor,
  5676.             grantee,
  5677.             'REFERENCES',
  5678.             'YES'
  5679.         from #table_priv1
  5680.         where references_privilege_grant = 1
  5681.  
  5682.  
  5683.     select
  5684.         convert(varchar(32),db_name()) TABLE_QUALIFIER,
  5685.         convert(varchar(32),user_name(o.uid)) TABLE_OWNER,
  5686.         convert(varchar(32),o.name) TABLE_NAME,
  5687.         convert(varchar(32),user_name(grantor)) GRANTOR,
  5688.         convert(varchar(32),user_name(grantee)) GRANTEE,
  5689.         PRIVILEGE,
  5690.         IS_GRANTABLE
  5691.     from #table_priv2 t, sysobjects o where o.id = t.id
  5692.     order by 2,3,6,5
  5693. go
  5694.  
  5695. if (charindex('6.50', @@version) = 0 and
  5696.     charindex('7.00', @@version) = 0)
  5697. begin
  5698.     print ''
  5699.     print ''
  5700.     print 'Warning:'
  5701.     print 'you are installing the stored procedures '
  5702.     print 'on a pre 6.50 SQL Server.'
  5703.     print 'Ignore the following errors.'
  5704. end
  5705. else
  5706.     drop proc sp_table_privileges
  5707. go
  5708.  
  5709.  
  5710. /*    Procedure for 6.50 server */
  5711. CREATE PROCEDURE sp_table_privileges (
  5712.             @table_name         varchar(96),
  5713.             @table_owner        varchar(96) = null,
  5714.             @table_qualifier    varchar(32) = null)
  5715. as
  5716.  
  5717.     if @table_qualifier is not null
  5718.     begin
  5719.         if db_name() <> @table_qualifier
  5720.         begin    /* If qualifier doesn't match current database */
  5721.             raiserror 20001 'Table qualifier must be name of current database'
  5722.             return
  5723.         end
  5724.     end
  5725.     if @table_name is null
  5726.         select @table_name = '%'
  5727.     if @table_owner is null /* If no owner supplied, force wildcard */
  5728.         select @table_owner = '%'
  5729.  
  5730.     select
  5731.         convert(varchar(32),db_name()) TABLE_QUALIFIER,
  5732.         convert(varchar(32),user_name(o.uid)) TABLE_OWNER,
  5733.         convert(varchar(32),object_name(o.id)) TABLE_NAME,
  5734.         convert(varchar(32),user_name(p.grantor)) GRANTOR,
  5735.         convert(varchar(32),user_name(u.uid)) GRANTEE,
  5736.         convert(varchar(32),case p.action
  5737.              when 193 then 'SELECT'
  5738.              when 195 then 'INSERT'
  5739.              when 196 then 'DELETE'
  5740.              when 197 then 'UPDATE'
  5741.              else 'REFERENCES'
  5742.         end) PRIVILEGE,
  5743.         convert(varchar(3),case when p.protecttype = 205 then 'NO'
  5744.             else 'YES'
  5745.         end) IS_GRANTABLE
  5746.     from sysprotects p, sysobjects o, sysusers u
  5747.     where
  5748.         p.id = o.id
  5749.         and o.type in ('U','V','S')
  5750.         and object_name(o.id) like @table_name
  5751.         and user_name(o.uid) like @table_owner
  5752.             /* expand groups */
  5753.         and ((p.uid = u.uid and u.uid <> u.gid) or
  5754.              (p.uid = u.gid and u.uid <> u.gid))
  5755.         and p.protecttype <> 206    /* only grant rows */
  5756.         and p.action in (26,193,195,196,197)
  5757.         and o.uid <> u.uid            /* no rows for owner */
  5758.         and not exists (            /* exclude revoke'd privileges */
  5759.             select *
  5760.             from sysprotects p1
  5761.             where
  5762.                 p1.protecttype = 206
  5763.                 and p1.action = p.action
  5764.                 and p1.id = p.id
  5765.                 and p1.uid = u.uid)
  5766.     union all
  5767.     select    /*    Add rows for table owner */
  5768.         convert(varchar(32),db_name()) TABLE_QUALIFIER,
  5769.         convert(varchar(32),user_name(o.uid)) TABLE_OWNER,
  5770.         convert(varchar(32),object_name(o.id)) TABLE_NAME,
  5771.         convert(varchar(32),user_name(u.uid)) GRANTOR,
  5772.         convert(varchar(32),user_name(o.uid)) GRANTEE,
  5773.         convert(varchar(32),case v.number
  5774.             when 193 then 'SELECT'
  5775.             when 195 then 'INSERT'
  5776.             when 196 then 'DELETE'
  5777.             when 197 then 'UPDATE'
  5778.             else 'REFERENCES'
  5779.         end) PRIVILEGE,
  5780.         convert(varchar(3),'YES') IS_GRANTABLE
  5781.     from sysobjects o, master.dbo.spt_values v, sysusers u
  5782.     where
  5783.         object_name(o.id) like @table_name
  5784.         and o.type in ('U','V','S')
  5785.         and user_name(o.uid) like @table_owner
  5786.         and u.suid = 1        /* grantor is dbo of database */
  5787.         and v.type = 'P'    /* cross product to get all exposed privileges */
  5788.         and v.number in (26,193,195,196,197)
  5789.         and not exists (    /* exclude revoke'd privileges */
  5790.             select *
  5791.             from sysprotects p1
  5792.             where
  5793.                 p1.protecttype = 206
  5794.                 and p1.action = v.number
  5795.                 and p1.id = o.id
  5796.                 and p1.uid = o.uid)
  5797.     order by 2,3,6,5
  5798. go
  5799.  
  5800.  
  5801. if (charindex('7.00', @@version) > 0)
  5802.     drop proc sp_table_privileges
  5803. else
  5804. begin
  5805.     print ''
  5806.     print ''
  5807.     print 'Warning:'
  5808.     print 'you are installing the stored procedures '
  5809.     print 'on a pre 7.0 SQL Server.'
  5810.     print 'Ignore the following errors.'
  5811. end
  5812. go
  5813.  
  5814. /*    Procedure for 7.00 server */
  5815. CREATE PROCEDURE sp_table_privileges (
  5816.             @table_name         varchar(384),
  5817.             @table_owner        varchar(384) = null,
  5818.             @table_qualifier    sysname = null)
  5819. as
  5820.  
  5821.     if @table_qualifier is not null
  5822.     begin
  5823.         if db_name() <> @table_qualifier
  5824.         begin    /* If qualifier doesn't match current database */
  5825.             raiserror 20001 'Table qualifier must be name of current database'
  5826.             return
  5827.         end
  5828.     end
  5829.     if @table_name is null
  5830.         select @table_name = '%'
  5831.     if @table_owner is null /* If no owner supplied, force wildcard */
  5832.         select @table_owner = '%'
  5833.  
  5834.     select
  5835.         convert(sysname,db_name()) TABLE_QUALIFIER,
  5836.         convert(sysname,user_name(o.uid)) TABLE_OWNER,
  5837.         convert(sysname,object_name(o.id)) TABLE_NAME,
  5838.         convert(sysname,user_name(p.grantor)) GRANTOR,
  5839.         convert(sysname,user_name(u.uid)) GRANTEE,
  5840.         convert(varchar(32),case p.action
  5841.              when 193 then 'SELECT'
  5842.              when 195 then 'INSERT'
  5843.              when 196 then 'DELETE'
  5844.              when 197 then 'UPDATE'
  5845.              else 'REFERENCES'
  5846.         end) PRIVILEGE,
  5847.         convert(varchar(3),case when p.protecttype = 205 then 'NO'
  5848.             else 'YES'
  5849.         end) IS_GRANTABLE
  5850.     from sysprotects p, sysobjects o, sysusers u, sysmembers m
  5851.     where
  5852.         p.id = o.id
  5853.         and o.type in ('U','V','S')
  5854.         and object_name(o.id) like @table_name
  5855.         and user_name(o.uid) like @table_owner
  5856.             /* expand groups - AKUNDONE: only 1 level of grp unrolling. */
  5857.         and (u.uid > 0 and u.uid < 16384)
  5858.         and ((p.uid = u.uid) or
  5859.              (p.uid = m.groupuid and u.uid = m.memberuid))
  5860.         and p.protecttype <> 206    /* only grant rows */
  5861.         and p.action in (26,193,195,196,197)
  5862.         and o.uid <> u.uid            /* no rows for owner */
  5863.         and not exists (            /* exclude revoke'd privileges */
  5864.             select *
  5865.             from sysprotects p1
  5866.             where
  5867.                 p1.protecttype = 206
  5868.                 and p1.action = p.action
  5869.                 and p1.id = p.id
  5870.                 and p1.uid = u.uid)
  5871.     union all
  5872.     select    /*    Add rows for table owner */
  5873.         convert(sysname,db_name()) TABLE_QUALIFIER,
  5874.         convert(sysname,user_name(o.uid)) TABLE_OWNER,
  5875.         convert(sysname,object_name(o.id)) TABLE_NAME,
  5876.         convert(sysname,user_name(u.uid)) GRANTOR,
  5877.         convert(sysname,user_name(o.uid)) GRANTEE,
  5878.         convert(varchar(32),case v.number
  5879.             when 193 then 'SELECT'
  5880.             when 195 then 'INSERT'
  5881.             when 196 then 'DELETE'
  5882.             when 197 then 'UPDATE'
  5883.             else 'REFERENCES'
  5884.         end) PRIVILEGE,
  5885.         convert(varchar(3),'YES') IS_GRANTABLE
  5886.     from sysobjects o, master.dbo.spt_values v, sysusers u
  5887.     where
  5888.         object_name(o.id) like @table_name
  5889.         and o.type in ('U','V','S')
  5890.         and user_name(o.uid) like @table_owner
  5891.         and u.uid = 1        /* grantor is 'dbo' of database */
  5892.         and v.type = 'P'    /* cross product to get all exposed privileges */
  5893.         and v.number in (26,193,195,196,197)
  5894.         and not exists (    /* exclude revoke'd privileges */
  5895.             select *
  5896.             from sysprotects p1
  5897.             where
  5898.                 p1.protecttype = 206
  5899.                 and p1.action = v.number
  5900.                 and p1.id = o.id
  5901.                 and p1.uid = o.uid)
  5902.     order by 2,3,6,5
  5903. go
  5904.  
  5905. grant execute on sp_table_privileges to public
  5906. go
  5907.  
  5908. dump tran master with no_log
  5909. go
  5910.  
  5911. print 'creating sp_tables'
  5912. go
  5913.  
  5914. /*    Procedure for 6.50 and earlier servers */
  5915. create procedure sp_tables(
  5916.                @table_name        varchar(96)    = null,
  5917.                @table_owner     varchar(96)    = null,
  5918.                @table_qualifier varchar(32)    = null,
  5919.                @table_type        varchar(100) = null)
  5920. as
  5921.     declare @type1 varchar(3)
  5922.     declare @tableindex int
  5923.  
  5924.  
  5925.     /* Special feature #1:    enumerate databases when owner and name
  5926.          are blank but qualifier is explicitly '%'.  */
  5927.     if @table_qualifier = '%' and
  5928.         @table_owner = '' and
  5929.         @table_name = ''
  5930.     begin    /* If enumerating databases */
  5931.         select
  5932.             TABLE_QUALIFIER = convert(varchar(32),d.name),
  5933.             TABLE_OWNER = convert(varchar(32),null),
  5934.             TABLE_NAME = convert(varchar(32),null),
  5935.             TABLE_TYPE = convert(varchar(32),null),
  5936.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  5937.         from master.dbo.sysdatabases d
  5938.         where d.name <> 'model'    /* eliminate MODEL database */
  5939.         order by 1
  5940.     end
  5941.  
  5942.     /* Special feature #2:    enumerate owners when qualifier and name
  5943.          are blank but owner is explicitly '%'.  */
  5944.     else if @table_qualifier = '' and
  5945.         @table_owner = '%' and
  5946.         @table_name = ''
  5947.     begin    /* If enumerating owners */
  5948.         select distinct
  5949.             TABLE_QUALIFIER = convert(varchar(32),null),
  5950.             TABLE_OWNER = convert(varchar(32),user_name(uid)),
  5951.             TABLE_NAME = convert(varchar(32),null),
  5952.             TABLE_TYPE = convert(varchar(32),null),
  5953.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  5954.         from sysobjects
  5955.         order by 2
  5956.     end
  5957.  
  5958.     /* Special feature #3:    enumerate table types when qualifier, owner and
  5959.          name are blank but table type is explicitly '%'.    */
  5960.     else if @table_qualifier = '' and
  5961.         @table_owner = '' and
  5962.         @table_name = '' and
  5963.         @table_type = '%'
  5964.     begin    /* If enumerating table types */
  5965.         select
  5966.             TABLE_QUALIFIER = convert(varchar(32),null),
  5967.             TABLE_OWNER = convert(varchar(32),null),
  5968.             TABLE_NAME = convert(varchar(32),null),
  5969.             TABLE_TYPE = convert(varchar(32),rtrim(substring('SYSTEM TABLETABLE       VIEW',(colid-1)*12+1,12))),
  5970.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  5971.         from sysobjects o, syscolumns c
  5972.         where o.id=c.id and o.name='sysusers' and colid<=3
  5973.     end
  5974.  
  5975.     else
  5976.     begin /* end of special features - do normal processing */
  5977.         if @table_qualifier is not null
  5978.         begin
  5979.             if db_name() <> @table_qualifier
  5980.             begin
  5981.                 if @table_qualifier = ''
  5982.                 begin  /* If empty qualifier supplied */
  5983.                     /* Force an empty result set */
  5984.                     select @table_name = ''
  5985.                     select @table_owner = ''
  5986.                 end
  5987.                 else
  5988.                 begin    /* If qualifier doesn't match current database */
  5989.                     raiserror 20001 'Table qualifier must be name of current database'
  5990.                     return
  5991.                 end
  5992.             end
  5993.         end
  5994.         if @table_type is null
  5995.         begin    /* Select all ODBC supported table types */
  5996.             select @type1 = 'SUV'
  5997.         end
  5998.         else
  5999.         begin
  6000.             /*    TableType is case sensitive if CS server */
  6001.             select @type1 = null
  6002.             if (charindex('''SYSTEM TABLE''',@table_type) <> 0)
  6003.                 select @type1 = @type1 + 'S'    /* Add System Tables */
  6004.             if (charindex('''TABLE''',@table_type) <> 0)
  6005.                 select @type1 = @type1 + 'U'    /* Add User Tables */
  6006.             if (charindex('''VIEW''',@table_type) <> 0)
  6007.                 select @type1 = @type1 + 'V'    /* Add Views */
  6008.         end
  6009.         if @table_name is null
  6010.         begin    /*    If table name not supplied, match all */
  6011.             select @table_name = '%'
  6012.         end
  6013.         else
  6014.         begin
  6015.             if (@table_owner is null) and (charindex('%', @table_name) = 0)
  6016.             begin    /* If owner not specified and table is specified */
  6017.                 if exists (select * from sysobjects
  6018.                     where uid = user_id()
  6019.                     and name = @table_name
  6020.                     and (type = 'U' or type = 'V' or type = 'S'))
  6021.                 begin    /* Override supplied owner w/owner of table */
  6022.                     select @table_owner = user_name()
  6023.                 end
  6024.             end
  6025.         end
  6026.         if @table_owner is null /* If no owner supplied, force wildcard */
  6027.             select @table_owner = '%'
  6028.         select
  6029.             TABLE_QUALIFIER = convert(varchar(32),db_name()),
  6030.             TABLE_OWNER = convert(varchar(32),user_name(o.uid)),
  6031.             TABLE_NAME = convert(varchar(32),o.name),    /* make nullable */
  6032.             TABLE_TYPE = convert(varchar(32),rtrim(
  6033.                 substring('SYSTEM TABLE            TABLE       VIEW       ',
  6034.                     (ascii(o.type)-83)*12+1,12))),    /* 'S'=0,'U'=2,'V'=3 */
  6035.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  6036.         from sysusers u, sysobjects o
  6037.         where
  6038.             o.name like @table_name
  6039.             and user_name(o.uid) like @table_owner
  6040.             and charindex(substring(o.type,1,1),@type1) <> 0 /* Only desired types */
  6041.             and u.uid = user_id() /* constrain sysusers uid for use in subquery */
  6042.             and (
  6043.                 suser_id() = 1     /* User is the System Administrator */
  6044.                 or o.uid = user_id()     /* User created the object */
  6045.                 /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  6046.                 or ((select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  6047.                     from sysprotects p
  6048.                     /* outer join to correlate with all rows in sysobjects */
  6049.                     where p.id =* o.id
  6050.                         /* get rows for public,current user,user's group */
  6051.                         and (p.uid = 0 or p.uid = user_id() or p.uid =* u.gid)
  6052.                         /* check for SELECT,EXECUTE privilege */
  6053.                         and (action in (193,224)))&1     /* more magic...normalize GRANT */
  6054.                     ) = 1    /* final magic...compare Grants      */
  6055.             )
  6056.         order by 4, 1, 2, 3
  6057.     end
  6058. go
  6059.  
  6060. if (charindex('7.00', @@version) > 0)
  6061.     drop procedure sp_tables
  6062. else
  6063. begin
  6064.     print ''
  6065.     print ''
  6066.     print 'Warning:'
  6067.     print 'you are installing the stored procedures '
  6068.     print 'on a pre 7.0 SQL Server.'
  6069.     print 'Ignore the following errors.'
  6070. end
  6071. go
  6072.  
  6073. /*    Procedure for 7.00 server */
  6074. create procedure sp_tables(
  6075.                @table_name        varchar(384)    = null,
  6076.                @table_owner     varchar(384)    = null,
  6077.                @table_qualifier sysname    = null,
  6078.                @table_type        varchar(100) = null)
  6079. as
  6080.     declare @type1 varchar(3)
  6081.     declare @tableindex int
  6082.  
  6083.  
  6084.     /* Special feature #1:    enumerate databases when owner and name
  6085.          are blank but qualifier is explicitly '%'.  */
  6086.     if @table_qualifier = '%' and
  6087.         @table_owner = '' and
  6088.         @table_name = ''
  6089.     begin    /* If enumerating databases */
  6090.         select
  6091.             TABLE_QUALIFIER = convert(sysname,d.name),
  6092.             TABLE_OWNER = convert(sysname,null),
  6093.             TABLE_NAME = convert(sysname,null),
  6094.             TABLE_TYPE = convert(sysname,null),
  6095.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  6096.         from master.dbo.sysdatabases d
  6097.         where d.name <> 'model'    /* eliminate MODEL database */
  6098.         order by 1
  6099.     end
  6100.  
  6101.     /* Special feature #2:    enumerate owners when qualifier and name
  6102.          are blank but owner is explicitly '%'.  */
  6103.     else if @table_qualifier = '' and
  6104.         @table_owner = '%' and
  6105.         @table_name = ''
  6106.     begin    /* If enumerating owners */
  6107.         select distinct
  6108.             TABLE_QUALIFIER = convert(sysname,null),
  6109.             TABLE_OWNER = convert(sysname,user_name(uid)),
  6110.             TABLE_NAME = convert(sysname,null),
  6111.             TABLE_TYPE = convert(sysname,null),
  6112.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  6113.         from sysobjects
  6114.         order by 2
  6115.     end
  6116.  
  6117.     /* Special feature #3:    enumerate table types when qualifier, owner and
  6118.          name are blank but table type is explicitly '%'.    */
  6119.     else if @table_qualifier = '' and
  6120.         @table_owner = '' and
  6121.         @table_name = '' and
  6122.         @table_type = '%'
  6123.     begin    /* If enumerating table types */
  6124.         select
  6125.             TABLE_QUALIFIER = convert(sysname,null),
  6126.             TABLE_OWNER = convert(sysname,null),
  6127.             TABLE_NAME = convert(sysname,null),
  6128.             TABLE_TYPE = convert(sysname,rtrim(substring('SYSTEM TABLETABLE       VIEW',(colid-1)*12+1,12))),
  6129.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  6130.         from sysobjects o, syscolumns c
  6131.         where o.id=c.id and o.name='sysusers' and colid<=3
  6132.     end
  6133.  
  6134.     else
  6135.     begin /* end of special features - do normal processing */
  6136.         if @table_qualifier is not null
  6137.         begin
  6138.             if db_name() <> @table_qualifier
  6139.             begin
  6140.                 if @table_qualifier = ''
  6141.                 begin  /* If empty qualifier supplied */
  6142.                     /* Force an empty result set */
  6143.                     select @table_name = ''
  6144.                     select @table_owner = ''
  6145.                 end
  6146.                 else
  6147.                 begin    /* If qualifier doesn't match current database */
  6148.                     raiserror 20001 'Table qualifier must be name of current database'
  6149.                     return
  6150.                 end
  6151.             end
  6152.         end
  6153.         if @table_type is null
  6154.         begin    /* Select all ODBC supported table types */
  6155.             select @type1 = 'SUV'
  6156.         end
  6157.         else
  6158.         begin
  6159.             /*    TableType is case sensitive if CS server */
  6160.             select @type1 = null
  6161.             if (charindex('''SYSTEM TABLE''',@table_type) <> 0)
  6162.                 select @type1 = @type1 + 'S'    /* Add System Tables */
  6163.             if (charindex('''TABLE''',@table_type) <> 0)
  6164.                 select @type1 = @type1 + 'U'    /* Add User Tables */
  6165.             if (charindex('''VIEW''',@table_type) <> 0)
  6166.                 select @type1 = @type1 + 'V'    /* Add Views */
  6167.         end
  6168.         if @table_name is null
  6169.         begin    /*    If table name not supplied, match all */
  6170.             select @table_name = '%'
  6171.         end
  6172.         else
  6173.         begin
  6174.             if (@table_owner is null) and (charindex('%', @table_name) = 0)
  6175.             begin    /* If owner not specified and table is specified */
  6176.                 if exists (select * from sysobjects
  6177.                     where uid = user_id()
  6178.                     and name = @table_name
  6179.                     and (type = 'U' or type = 'V' or type = 'S'))
  6180.                 begin    /* Override supplied owner w/owner of table */
  6181.                     select @table_owner = user_name()
  6182.                 end
  6183.             end
  6184.         end
  6185.         if @table_owner is null /* If no owner supplied, force wildcard */
  6186.             select @table_owner = '%'
  6187.         select
  6188.             TABLE_QUALIFIER = convert(sysname,db_name()),
  6189.             TABLE_OWNER = convert(sysname,user_name(o.uid)),
  6190.             TABLE_NAME = convert(sysname,o.name),    /* make nullable */
  6191.             TABLE_TYPE = convert(sysname,rtrim(
  6192.                 substring('SYSTEM TABLE            TABLE       VIEW       ',
  6193.                     (ascii(o.type)-83)*12+1,12))),    /* 'S'=0,'U'=2,'V'=3 */
  6194.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  6195.         from sysobjects o
  6196.         where
  6197.             o.name like @table_name
  6198.             and user_name(o.uid) like @table_owner
  6199.             and (o.type in ('U','S') or
  6200.              (o.type = 'V' and ObjectProperty (o.id, 'IsMSShipped') = 0))
  6201.             and charindex(substring(o.type,1,1),@type1) <> 0 /* Only desired types */
  6202.             and permissions (o.id)&4096 <> 0
  6203.         order by 4, 1, 2, 3
  6204.     end
  6205. go
  6206.  
  6207.  
  6208. grant execute on sp_tables to public
  6209. go
  6210.  
  6211. dump tran master with no_log
  6212. go
  6213.  
  6214. if (charindex('6.50', @@version) = 0 and
  6215.     charindex('7.00', @@version) = 0)
  6216. begin
  6217.     print ''
  6218.     print ''
  6219.     print 'Warning:'
  6220.     print 'you are installing the stored procedures '
  6221.     print 'on a pre 6.50 SQL Server.'
  6222.     print 'Ignore the following errors.'
  6223. end
  6224.  
  6225. print 'creating sp_ddopen'
  6226. go
  6227.  
  6228. /*    Procedure for pre-7.00 server */
  6229. create procedure sp_ddopen(
  6230.                @handle            int output,
  6231.                @procname        sysname,
  6232.                @scrollopt        int output,
  6233.                @ccopt            int output,
  6234.                @rows            int output,
  6235.                @p1                varchar(255) = null,
  6236.                @p2                varchar(255) = null,
  6237.                @p3                varchar(255) = null,
  6238.                @p4                varchar(255) = null,
  6239.                @p5                varchar(255) = null,
  6240.                @p6                varchar(255) = null,
  6241.                @p7                int = null,
  6242.                @ODBCVer         int = 2)
  6243. as
  6244.     declare @ret int
  6245.  
  6246.     if @procname = 'sp_column_privileges'
  6247.     begin
  6248.         create table #spcolpriv (
  6249.             TABLE_QUALIFIER varchar(32) null,
  6250.             TABLE_OWNER varchar(32) null,
  6251.             TABLE_NAME varchar(32)    not null,
  6252.             COLUMN_NAME varchar(32) not null,
  6253.             GRANTOR varchar(32) null,
  6254.             GRANTEE varchar(32) not null,
  6255.             PRIVILEGE varchar(32) not null,
  6256.             IS_GRANTABLE varchar(3) null
  6257.             )
  6258.         insert into #spcolpriv exec sp_column_privileges @p1,@p2,@p3,@p4
  6259.         exec @ret = sp_cursoropen @handle output,
  6260.             'select * from #spcolpriv',
  6261.             @scrollopt output, @ccopt output, @rows output
  6262.         drop table #spcolpriv
  6263.     end
  6264.     else if @procname = 'sp_columns'
  6265.     begin
  6266.         create table #spcolumns (
  6267.             TABLE_QUALIFIER varchar(32) null,
  6268.             TABLE_OWNER varchar(32) null,
  6269.             TABLE_NAME varchar(32)    not null,
  6270.             COLUMN_NAME varchar(32) not null,
  6271.             DATA_TYPE smallint not null,
  6272.             TYPE_NAME varchar(32) not null,
  6273.             "PRECISION" int null,
  6274.             LENGTH int null,
  6275.             SCALE smallint null,
  6276.             RADIX smallint null,
  6277.             NULLABLE smallint not null,
  6278.             REMARKS varchar(254) null,
  6279.             COLUMN_DEF varchar(254) null,
  6280.             SQL_DATA_TYPE smallint not null,
  6281.             SQL_DATETIME_SUB smallint null,
  6282.             CHAR_OCTET_LENGTH int null,
  6283.             ORDINAL_POSITION int not null,
  6284.             IS_NULLABLE varchar(254) null,
  6285.             SS_DATA_TYPE tinyint null
  6286.             )
  6287.         insert into #spcolumns exec sp_columns @p1,@p2,@p3,@p4,@ODBCVer
  6288.         exec @ret = sp_cursoropen @handle output,
  6289.             'select * from #spcolumns',
  6290.             @scrollopt output, @ccopt output, @rows output
  6291.         drop table #spcolumns
  6292.     end
  6293.     else if @procname = 'sp_datatype_info'
  6294.     begin
  6295.         create table #spdatatypeinfo (
  6296.             TYPE_NAME            varchar(32)  not null,
  6297.             DATA_TYPE            smallint not null,
  6298.             "PRECISION"            int null,
  6299.             LITERAL_PREFIX        varchar(32)    null,
  6300.             LITERAL_SUFFIX        varchar(32)    null,
  6301.             CREATE_PARAMS        varchar(32)    null,
  6302.             NULLABLE            smallint   not null,
  6303.             CASE_SENSITIVE        smallint   not null,
  6304.             SEARCHABLE            smallint   not null,
  6305.             UNSIGNED_ATTRIBUTE    smallint   null,
  6306.             MONEY    smallint    not null,
  6307.             AUTO_INCREMENT        smallint    null,
  6308.             LOCAL_TYPE_NAME     varchar(32) null,
  6309.             MINIMUM_SCALE        smallint     null,
  6310.             MAXIMUM_SCALE        smallint   null,
  6311.             SQL_DATA_TYPE        smallint      not null,
  6312.             SQL_DATETIME_SUB    smallint   null,
  6313.             NUM_PREC_RADIX        int     null,
  6314.             INTERVAL_PRECISION    smallint    NULL,
  6315.             USERTYPE            smallint not null)
  6316.         insert into #spdatatypeinfo exec sp_datatype_info @p7,@ODBCVer
  6317.         exec @ret = sp_cursoropen @handle output,
  6318.             'select * from #spdatatypeinfo',
  6319.             @scrollopt output, @ccopt output, @rows output
  6320.         drop table #spdatatypeinfo
  6321.     end
  6322.     else if @procname = 'sp_fkeys'
  6323.     begin
  6324.         create table #spfkeys (
  6325.             PKTABLE_QUALIFIER varchar(32)     null,
  6326.             PKTABLE_OWNER varchar(32)    null,
  6327.             PKTABLE_NAME varchar(32)  not null,
  6328.             PKCOLUMN_NAME varchar(32)    not null,
  6329.             FKTABLE_QUALIFIER varchar(32)    null,
  6330.             FKTABLE_OWNER varchar(32)    null,
  6331.             FKTABLE_NAME varchar(32)  not null,
  6332.             FKCOLUMN_NAME varchar(32)    not null,
  6333.             KEY_SEQ smallint not null,
  6334.             UPDATE_RULE smallint null,
  6335.             DELETE_RULE smallint null,
  6336.             FK_NAME varchar(32) null,
  6337.             PK_NAME varchar(32) null
  6338.             )
  6339.         insert into #spfkeys exec sp_fkeys @p1,@p2,@p3,@p4,@p5,@p6
  6340.         exec @ret = sp_cursoropen @handle output,
  6341.             'select * from #spfkeys',
  6342.             @scrollopt output, @ccopt output, @rows output
  6343.         drop table #spfkeys
  6344.     end
  6345.     else if @procname = 'sp_pkeys'
  6346.     begin
  6347.         create table #sppkeys (
  6348.             TABLE_QUALIFIER varchar(32)   null,
  6349.             TABLE_OWNER varchar(32)   null,
  6350.             TABLE_NAME varchar(32)    not null,
  6351.             COLUMN_NAME varchar(32)  not null,
  6352.             KEY_SEQ smallint not null,
  6353.             PK_NAME varchar(32) null
  6354.             )
  6355.         insert into #sppkeys exec sp_pkeys @p1,@p2,@p3
  6356.         exec @ret = sp_cursoropen @handle output,
  6357.             'select * from #sppkeys',
  6358.             @scrollopt output, @ccopt output, @rows output
  6359.         drop table #sppkeys
  6360.     end
  6361.     else if @procname = 'sp_special_columns'
  6362.     begin
  6363.         create table #spspeccol (
  6364.             SCOPE smallint null,
  6365.             COLUMN_NAME varchar(32) not null,
  6366.             DATA_TYPE smallint not null,
  6367.             TYPE_NAME varchar(32) not null,
  6368.             "PRECISION" int null,
  6369.             LENGTH int null,
  6370.             SCALE smallint null,
  6371.             PSEUDO_COLUMN smallint null
  6372.             )
  6373.         insert into #spspeccol exec sp_special_columns @p1,@p2,@p3,@p4,@p5,@p6,@ODBCVer
  6374.         exec @ret = sp_cursoropen @handle output,
  6375.             'select * from #spspeccol',
  6376.             @scrollopt output, @ccopt output, @rows output
  6377.         drop table #spspeccol
  6378.     end
  6379.     else if @procname = 'sp_sproc_columns'
  6380.     begin
  6381.         create table #spproccol (
  6382.             PROCEDURE_QUALIFIER varchar(32)  null,
  6383.             PROCEDURE_OWNER varchar(32)  null,
  6384.             PROCEDURE_NAME varchar(32)    not null,
  6385.             COLUMN_NAME varchar(32) not null,
  6386.             COLUMN_TYPE smallint not null,
  6387.             DATA_TYPE smallint not null,
  6388.             TYPE_NAME varchar(32) not null,
  6389.             "PRECISION" int null,
  6390.             LENGTH int null,
  6391.             SCALE smallint null,
  6392.             RADIX smallint null,
  6393.             NULLABLE smallint not null,
  6394.             REMARKS varchar(254) null,
  6395.             COLUMN_DEF varchar(254) null,
  6396.             SQL_DATA_TYPE smallint not null,
  6397.             SQL_DATETIME_SUB smallint null,
  6398.             CHAR_OCTET_LENGTH int null,
  6399.             ORDINAL_POSITION int not null,
  6400.             IS_NULLABLE varchar(254) null,
  6401.             SS_DATA_TYPE tinyint null
  6402.             )
  6403.         insert into #spproccol exec sp_sproc_columns @p1,@p2,@p3,@p4,@ODBCVer
  6404.         exec @ret = sp_cursoropen @handle output,
  6405.             'select * from #spproccol',
  6406.             @scrollopt output, @ccopt output, @rows output
  6407.         drop table #spproccol
  6408.     end
  6409.     else if @procname = 'sp_statistics'
  6410.     begin
  6411.         create table #spstatistics (
  6412.             TABLE_QUALIFIER varchar(32)   null,
  6413.             TABLE_OWNER varchar(32)   null,
  6414.             TABLE_NAME varchar(32)    not null,
  6415.             NON_UNIQUE smallint null,
  6416.             INDEX_QUALIFIER varchar(32) null,
  6417.             INDEX_NAME varchar(32)    null,
  6418.             TYPE smallint not null,
  6419.             SEQ_IN_INDEX smallint null,
  6420.             COLUMN_NAME varchar(32) null,
  6421.             COLLATION char(1) null,
  6422.             CARDINALITY int null,
  6423.             PAGES int null,
  6424.             FILTER_CONDITION varchar(128) null
  6425.             )
  6426.         insert into #spstatistics exec sp_statistics @p1,@p2,@p3,@p4,@p5,@p6
  6427.         exec @ret = sp_cursoropen @handle output,
  6428.             'select * from #spstatistics',
  6429.             @scrollopt output, @ccopt output, @rows output
  6430.         drop table #spstatistics
  6431.     end
  6432.     else if @procname = 'sp_stored_procedures'
  6433.     begin
  6434.         create table #spprocedures (
  6435.             PROCEDURE_QUALIFIER varchar(32)  null,
  6436.             PROCEDURE_OWNER varchar(32)  null,
  6437.             PROCEDURE_NAME varchar(32)    not null,
  6438.             NUM_INPUT_PARAMS int null,
  6439.             NUM_OUTPUT_PARAMS int null,
  6440.             NUM_RESULT_SETS int null,
  6441.             REMARKS varchar(254) null,
  6442.             PROCEDURE_TYPE smallint null
  6443.             )
  6444.         insert into #spprocedures exec sp_stored_procedures @p1,@p2,@p3
  6445.         exec @ret = sp_cursoropen @handle output,
  6446.             'select * from #spprocedures',
  6447.             @scrollopt output, @ccopt output, @rows output
  6448.         drop table #spprocedures
  6449.     end
  6450.     else if @procname = 'sp_table_privileges'
  6451.     begin
  6452.         create table #sptabpriv (
  6453.             TABLE_QUALIFIER varchar(32) null,
  6454.             TABLE_OWNER varchar(32) null,
  6455.             TABLE_NAME varchar(32)    not null,
  6456.             GRANTOR varchar(32) null,
  6457.             GRANTEE varchar(32) not null,
  6458.             PRIVILEGE varchar(32) not null,
  6459.             IS_GRANTABLE varchar(3) null
  6460.             )
  6461.         insert into #sptabpriv exec sp_table_privileges @p1,@p2,@p3
  6462.         exec @ret = sp_cursoropen @handle output,
  6463.             'select * from #sptabpriv',
  6464.             @scrollopt output, @ccopt output, @rows output
  6465.         drop table #sptabpriv
  6466.     end
  6467.     else if @procname = 'sp_tables'
  6468.     begin
  6469.         create table #sptables (
  6470.             TABLE_QUALIFIER varchar(32) null,
  6471.             TABLE_OWNER varchar(32) null,
  6472.             TABLE_NAME varchar(32)    null,
  6473.             TABLE_TYPE     varchar(32) null,
  6474.             REMARKS varchar(254) null)
  6475.         insert into #sptables exec sp_tables @p1,@p2,@p3,@p4
  6476.         exec @ret = sp_cursoropen @handle output,
  6477.             'select * from #sptables',
  6478.             @scrollopt output, @ccopt output, @rows output
  6479.         drop table #sptables
  6480.     end
  6481.     select @ret = isnull(@ret,0)
  6482.     return isnull(@ret,0)
  6483. go
  6484.  
  6485. if (charindex('7.00', @@version) > 0)
  6486.     drop procedure sp_ddopen
  6487. else
  6488. begin
  6489.     print ''
  6490.     print ''
  6491.     print 'Warning:'
  6492.     print 'you are installing the stored procedures '
  6493.     print 'on a pre 7.0 SQL Server.'
  6494.     print 'Ignore the following errors.'
  6495. end
  6496. go
  6497.  
  6498. /*    Procedure for 7.00 server */
  6499. create procedure sp_ddopen(
  6500.                @handle            int output,
  6501.                @procname        sysname,
  6502.                @scrollopt        int output,
  6503.                @ccopt            int output,
  6504.                @rows            int output,
  6505.                @p1                varchar(255) = null,
  6506.                @p2                varchar(255) = null,
  6507.                @p3                varchar(255) = null,
  6508.                @p4                varchar(255) = null,
  6509.                @p5                varchar(255) = null,
  6510.                @p6                varchar(255) = null,
  6511.                @p7                int = null,
  6512.                @ODBCVer         int = 2)
  6513. as
  6514.     declare @ret int
  6515.  
  6516.     if @procname = 'sp_column_privileges'
  6517.     begin
  6518.         create table #spcolpriv (
  6519.             TABLE_QUALIFIER sysname null,
  6520.             TABLE_OWNER sysname null,
  6521.             TABLE_NAME sysname not null,
  6522.             COLUMN_NAME sysname not null,
  6523.             GRANTOR sysname null,
  6524.             GRANTEE sysname not null,
  6525.             PRIVILEGE varchar(32) not null,
  6526.             IS_GRANTABLE varchar(3) null
  6527.             )
  6528.         insert into #spcolpriv exec sp_column_privileges @p1,@p2,@p3,@p4
  6529.         exec @ret = sp_cursoropen @handle output,
  6530.             'select * from #spcolpriv',
  6531.             @scrollopt output, @ccopt output, @rows output
  6532.         drop table #spcolpriv
  6533.     end
  6534.     else if @procname = 'sp_columns'
  6535.     begin
  6536.         create table #spcolumns (
  6537.             TABLE_QUALIFIER sysname null,
  6538.             TABLE_OWNER sysname null,
  6539.             TABLE_NAME sysname not null,
  6540.             COLUMN_NAME sysname not null,
  6541.             DATA_TYPE smallint not null,
  6542.             TYPE_NAME sysname not null,
  6543.             "PRECISION" int null,
  6544.             LENGTH int null,
  6545.             SCALE smallint null,
  6546.             RADIX smallint null,
  6547.             NULLABLE smallint not null,
  6548.             REMARKS varchar(254) null,
  6549.             COLUMN_DEF varchar(254) null,
  6550.             SQL_DATA_TYPE smallint not null,
  6551.             SQL_DATETIME_SUB smallint null,
  6552.             CHAR_OCTET_LENGTH int null,
  6553.             ORDINAL_POSITION int not null,
  6554.             IS_NULLABLE varchar(254) null,
  6555.             SS_DATA_TYPE tinyint null
  6556.             )
  6557.         insert into #spcolumns exec sp_columns @p1,@p2,@p3,@p4,@ODBCVer
  6558.         exec @ret = sp_cursoropen @handle output,
  6559.             'select * from #spcolumns',
  6560.             @scrollopt output, @ccopt output, @rows output
  6561.         drop table #spcolumns
  6562.     end
  6563.     else if @procname = 'sp_datatype_info'
  6564.     begin
  6565.         create table #spdatatypeinfo (
  6566.             TYPE_NAME            sysname  not null,
  6567.             DATA_TYPE            smallint not null,
  6568.             "PRECISION"            int null,
  6569.             LITERAL_PREFIX        varchar(32)    null,
  6570.             LITERAL_SUFFIX        varchar(32)    null,
  6571.             CREATE_PARAMS        varchar(32)    null,
  6572.             NULLABLE            smallint   not null,
  6573.             CASE_SENSITIVE        smallint   not null,
  6574.             SEARCHABLE            smallint   not null,
  6575.             UNSIGNED_ATTRIBUTE    smallint   null,
  6576.             MONEY    smallint    not null,
  6577.             AUTO_INCREMENT        smallint    null,
  6578.             LOCAL_TYPE_NAME     varchar(128) null,
  6579.             MINIMUM_SCALE        smallint     null,
  6580.             MAXIMUM_SCALE        smallint   null,
  6581.             SQL_DATA_TYPE        smallint      not null,
  6582.             SQL_DATETIME_SUB    smallint   null,
  6583.             NUM_PREC_RADIX        int     null,
  6584.             INTERVAL_PRECISION    smallint    NULL,
  6585.             USERTYPE            smallint not null)
  6586.         insert into #spdatatypeinfo exec sp_datatype_info @p7,@ODBCVer
  6587.         exec @ret = sp_cursoropen @handle output,
  6588.             'select * from #spdatatypeinfo',
  6589.             @scrollopt output, @ccopt output, @rows output
  6590.         drop table #spdatatypeinfo
  6591.     end
  6592.     else if @procname = 'sp_fkeys'
  6593.     begin
  6594.         create table #spfkeys (
  6595.             PKTABLE_QUALIFIER sysname     null,
  6596.             PKTABLE_OWNER sysname    null,
  6597.             PKTABLE_NAME sysname  not null,
  6598.             PKCOLUMN_NAME sysname  not null,
  6599.             FKTABLE_QUALIFIER sysname    null,
  6600.             FKTABLE_OWNER sysname    null,
  6601.             FKTABLE_NAME sysname  not null,
  6602.             FKCOLUMN_NAME sysname  not null,
  6603.             KEY_SEQ smallint not null,
  6604.             UPDATE_RULE smallint null,
  6605.             DELETE_RULE smallint null,
  6606.             FK_NAME sysname null,
  6607.             PK_NAME sysname null
  6608.             )
  6609.         insert into #spfkeys exec sp_fkeys @p1,@p2,@p3,@p4,@p5,@p6
  6610.         exec @ret = sp_cursoropen @handle output,
  6611.             'select * from #spfkeys',
  6612.             @scrollopt output, @ccopt output, @rows output
  6613.         drop table #spfkeys
  6614.     end
  6615.     else if @procname = 'sp_pkeys'
  6616.     begin
  6617.         create table #sppkeys (
  6618.             TABLE_QUALIFIER sysname   null,
  6619.             TABLE_OWNER sysname   null,
  6620.             TABLE_NAME sysname    not null,
  6621.             COLUMN_NAME sysname  not null,
  6622.             KEY_SEQ smallint not null,
  6623.             PK_NAME sysname null
  6624.             )
  6625.         insert into #sppkeys exec sp_pkeys @p1,@p2,@p3
  6626.         exec @ret = sp_cursoropen @handle output,
  6627.             'select * from #sppkeys',
  6628.             @scrollopt output, @ccopt output, @rows output
  6629.         drop table #sppkeys
  6630.     end
  6631.     else if @procname = 'sp_special_columns'
  6632.     begin
  6633.         create table #spspeccol (
  6634.             SCOPE smallint null,
  6635.             COLUMN_NAME sysname not null,
  6636.             DATA_TYPE smallint not null,
  6637.             TYPE_NAME sysname not null,
  6638.             "PRECISION" int null,
  6639.             LENGTH int null,
  6640.             SCALE smallint null,
  6641.             PSEUDO_COLUMN smallint null
  6642.             )
  6643.         insert into #spspeccol exec sp_special_columns @p1,@p2,@p3,@p4,@p5,@p6,@ODBCVer
  6644.         exec @ret = sp_cursoropen @handle output,
  6645.             'select * from #spspeccol',
  6646.             @scrollopt output, @ccopt output, @rows output
  6647.         drop table #spspeccol
  6648.     end
  6649.     else if @procname = 'sp_sproc_columns'
  6650.     begin
  6651.         create table #spproccol (
  6652.             PROCEDURE_QUALIFIER sysname  null,
  6653.             PROCEDURE_OWNER sysname  null,
  6654.             PROCEDURE_NAME sysname not null,
  6655.             COLUMN_NAME sysname not null,
  6656.             COLUMN_TYPE smallint not null,
  6657.             DATA_TYPE smallint not null,
  6658.             TYPE_NAME sysname not null,
  6659.             "PRECISION" int null,
  6660.             LENGTH int null,
  6661.             SCALE smallint null,
  6662.             RADIX smallint null,
  6663.             NULLABLE smallint not null,
  6664.             REMARKS varchar(254) null,
  6665.             COLUMN_DEF varchar(254) null,
  6666.             SQL_DATA_TYPE smallint not null,
  6667.             SQL_DATETIME_SUB smallint null,
  6668.             CHAR_OCTET_LENGTH int null,
  6669.             ORDINAL_POSITION int not null,
  6670.             IS_NULLABLE varchar(254) null,
  6671.             SS_DATA_TYPE tinyint null
  6672.             )
  6673.         insert into #spproccol exec sp_sproc_columns @p1,@p2,@p3,@p4,@ODBCVer
  6674.         exec @ret = sp_cursoropen @handle output,
  6675.             'select * from #spproccol',
  6676.             @scrollopt output, @ccopt output, @rows output
  6677.         drop table #spproccol
  6678.     end
  6679.     else if @procname = 'sp_statistics'
  6680.     begin
  6681.         create table #spstatistics (
  6682.             TABLE_QUALIFIER sysname   null,
  6683.             TABLE_OWNER sysname   null,
  6684.             TABLE_NAME sysname    not null,
  6685.             NON_UNIQUE smallint null,
  6686.             INDEX_QUALIFIER sysname null,
  6687.             INDEX_NAME sysname null,
  6688.             TYPE smallint not null,
  6689.             SEQ_IN_INDEX smallint null,
  6690.             COLUMN_NAME sysname null,
  6691.             COLLATION char(1) null,
  6692.             CARDINALITY int null,
  6693.             PAGES int null,
  6694.             FILTER_CONDITION varchar(128) null
  6695.             )
  6696.         insert into #spstatistics exec sp_statistics @p1,@p2,@p3,@p4,@p5,@p6
  6697.         exec @ret = sp_cursoropen @handle output,
  6698.             'select * from #spstatistics',
  6699.             @scrollopt output, @ccopt output, @rows output
  6700.         drop table #spstatistics
  6701.     end
  6702.     else if @procname = 'sp_stored_procedures'
  6703.     begin
  6704.         create table #spprocedures (
  6705.             PROCEDURE_QUALIFIER sysname  null,
  6706.             PROCEDURE_OWNER sysname  null,
  6707.             PROCEDURE_NAME sysname not null,
  6708.             NUM_INPUT_PARAMS int null,
  6709.             NUM_OUTPUT_PARAMS int null,
  6710.             NUM_RESULT_SETS int null,
  6711.             REMARKS varchar(254) null,
  6712.             PROCEDURE_TYPE smallint null
  6713.             )
  6714.         insert into #spprocedures exec sp_stored_procedures @p1,@p2,@p3
  6715.         exec @ret = sp_cursoropen @handle output,
  6716.             'select * from #spprocedures',
  6717.             @scrollopt output, @ccopt output, @rows output
  6718.         drop table #spprocedures
  6719.     end
  6720.     else if @procname = 'sp_table_privileges'
  6721.     begin
  6722.         create table #sptabpriv (
  6723.             TABLE_QUALIFIER sysname null,
  6724.             TABLE_OWNER sysname null,
  6725.             TABLE_NAME sysname not null,
  6726.             GRANTOR sysname null,
  6727.             GRANTEE sysname not null,
  6728.             PRIVILEGE varchar(32) not null,
  6729.             IS_GRANTABLE varchar(3) null
  6730.             )
  6731.         insert into #sptabpriv exec sp_table_privileges @p1,@p2,@p3
  6732.         exec @ret = sp_cursoropen @handle output,
  6733.             'select * from #sptabpriv',
  6734.             @scrollopt output, @ccopt output, @rows output
  6735.         drop table #sptabpriv
  6736.     end
  6737.     else if @procname = 'sp_tables'
  6738.     begin
  6739.         create table #sptables (
  6740.             TABLE_QUALIFIER sysname null,
  6741.             TABLE_OWNER sysname null,
  6742.             TABLE_NAME sysname null,
  6743.             TABLE_TYPE    sysname null,
  6744.             REMARKS varchar(254) null)
  6745.         insert into #sptables exec sp_tables @p1,@p2,@p3,@p4
  6746.         exec @ret = sp_cursoropen @handle output,
  6747.             'select * from #sptables',
  6748.             @scrollopt output, @ccopt output, @rows output
  6749.         drop table #sptables
  6750.     end
  6751.     select @ret = isnull(@ret,0)
  6752.     return isnull(@ret,0)
  6753. go
  6754.  
  6755. grant execute on sp_ddopen to public
  6756. go
  6757.  
  6758. dump tran master with no_log
  6759. go
  6760.  
  6761. print 'creating spt_provider_types'
  6762. go
  6763.  
  6764. create table spt_provider_types
  6765.     (
  6766.     ss_dtype        tinyint     not null,
  6767.     fixlen            int         null,        /* datatype len for variable, else null */
  6768.     type_name        sysname    not null,
  6769.     oledb_data_type        smallint    not null,
  6770.     best_match        bit        not null,
  6771.     is_nullable        bit        not null,
  6772.     case_sensitive        bit        not null,
  6773.     fixed_prec_scale    bit        not null,
  6774.     is_long            bit        not null,
  6775.     auto_unique_value    tinyint        not null,
  6776.     data_precision        int         null,
  6777.     numeric_scale        smallint    null,        /* min scale if 6.0 */
  6778.     column_size        int         null,
  6779.     literal_prefix        varchar(32)    null,
  6780.     literal_suffix        varchar(32)     null,
  6781.     searchable        int        not null,
  6782.     unsigned_attribute    tinyint        null,
  6783.     local_type_name     sysname    null
  6784.     )
  6785. go
  6786.  
  6787. grant select on spt_provider_types to public
  6788. go
  6789.  
  6790. /*
  6791. ** Insert the spt_provider_types rows for DBTYTPE_STR data types
  6792. */
  6793. begin tran
  6794.  
  6795. /* Get case sensitivity */
  6796. declare @case_sensitive bit
  6797. select @case_sensitive = case when 'a' <> 'A' then 1 else 0 end
  6798. /* Local Char */
  6799. insert into spt_provider_types values
  6800.     (
  6801.     47 /*SQLCHARACTER*/,        /* ss_dtype */
  6802.     0,                /* fixlen */
  6803.     'char',                /* type_name */
  6804.     129 /*DBTYPE_STR*/,        /* oledb_data_type */
  6805.     0,                /* best_match */
  6806.     1,                /* is_nullable */
  6807.     @case_sensitive,        /* case_sensitive */
  6808.     0,                /* fixed_prec_scale */
  6809.     0,                /* is_long */
  6810.     0,                /* auto_unique_value */
  6811.     null,                /* data_precision */
  6812.     null,                /* numeric_scale */
  6813.     null,                /* column_size */
  6814.     '''',                /* literal_prefix */
  6815.     '''',                /* literal_suffix */
  6816.     4 /*DB_SEARCHABLE*/,        /* searchable */
  6817.     null,                /* unsigned_attribute */
  6818.     'char'                /* local_type_name */
  6819.     )
  6820.  
  6821. /* Local Varchar */
  6822. insert into spt_provider_types values
  6823.     (
  6824.     39 /*SQLVARCHAR*/,        /* ss_dtype */
  6825.     null,                /* fixlen */
  6826.     'varchar',            /* type_name */
  6827.     129 /*DBTYPE_STR*/,        /* oledb_data_type */
  6828.     0,                /* best_match */
  6829.     1,                /* is_nullable */
  6830.     @case_sensitive,        /* case_sensitive */
  6831.     0,                /* fixed_prec_scale */
  6832.     0,                /* is_long */
  6833.     0,                /* auto_unique_value */
  6834.     null,                /* data_precision */
  6835.     null,                /* numeric_scale */
  6836.     null,                /* column_size */
  6837.     '''',                /* literal_prefix */
  6838.     '''',                /* literal_suffix */
  6839.     4 /*DB_SEARCHABLE*/,        /* searchable */
  6840.     null,                /* unsigned_attribute */
  6841.     'varchar'            /* local_type_name */
  6842.     )
  6843.  
  6844. /* Local Text */
  6845. insert into spt_provider_types values
  6846.     (
  6847.     35  /*SQLTEXT*/,        /* ss_dtype */
  6848.     null,                /* fixlen */
  6849.     'text',                /* type_name */
  6850.     129 /*DBTYPE_STR*/,        /* oledb_data_type */
  6851.     0,                /* best_match */
  6852.     1,                /* is_nullable */
  6853.     @case_sensitive,        /* case_sensitive */
  6854.     0,                /* fixed_prec_scale */
  6855.     1,                /* is_long */
  6856.     0,                /* auto_unique_value */
  6857.     null,                /* data_precision */
  6858.     null,                /* numeric_scale */
  6859.     2147483647,            /* column_size */
  6860.     '''',                /* literal_prefix */
  6861.     '''',                /* literal_suffix */
  6862.     2 /*DB_LIKE_ONLY*/,        /* searchable */
  6863.     null,                /* unsigned_attribute */
  6864.     'text'                /* local_type_name */
  6865.     )
  6866. commit tran
  6867. go
  6868.  
  6869. /*
  6870. ** Insert the spt_provider_types rows for DBTYTPE_BYTES data types
  6871. */
  6872. begin tran
  6873. /* Local Binary */
  6874. insert into spt_provider_types values
  6875.     (
  6876.     45 /*SQLBINARY*/,        /* ss_dtype */
  6877.     0,                /* fixlen */
  6878.     'binary',            /* type_name */
  6879.     128 /*DBTYPE_BYTES*/,        /* oledb_data_type */
  6880.     0,                /* best_match */
  6881.     1,                /* is_nullable */
  6882.     0,                /* case_sensitive */
  6883.     0,                /* fixed_prec_scale */
  6884.     0,                /* is_long */
  6885.     0,                /* auto_unique_value */
  6886.     null,                /* data_precision */
  6887.     null,                /* numeric_scale */
  6888.     null,                /* column_size */
  6889.     '0x',                /* literal_prefix */
  6890.     null,                /* literal_suffix */
  6891.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  6892.     null,                /* unsigned_attribute */
  6893.     'binary'            /* local_type_name */
  6894.     )
  6895.  
  6896. /* Local Varbinary */
  6897. insert into spt_provider_types values
  6898.     (
  6899.     37 /*SQLVARBINARY*/,        /* ss_dtype */
  6900.     null,                /* fixlen */
  6901.     'varbinary',            /* type_name */
  6902.     128 /*DBTYPE_BYTES*/,        /* oledb_data_type */
  6903.     0,                /* best_match */
  6904.     1,                /* is_nullable */
  6905.     0,                /* case_sensitive */
  6906.     0,                /* fixed_prec_scale */
  6907.     0,                /* is_long */
  6908.     0,                /* auto_unique_value */
  6909.     null,                /* data_precision */
  6910.     null,                /* numeric_scale */
  6911.     null,                /* column_size */
  6912.     '0x',                /* literal_prefix */
  6913.     null,                /* literal_suffix */
  6914.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  6915.     null,                /* unsigned_attribute */
  6916.     'varbinary'            /* local_type_name */
  6917.     )
  6918.  
  6919. /* Local Image */
  6920. insert into spt_provider_types values
  6921.     (
  6922.     34 /*SQLIMAGE*/,        /* ss_dtype */
  6923.     null,                /* fixlen */
  6924.     'image',            /* type_name */
  6925.     128 /*DBTYPE_BYTES*/,        /* oledb_data_type */
  6926.     0,                /* best_match */
  6927.     1,                /* is_nullable */
  6928.     0,                /* case_sensitive */
  6929.     0,                /* fixed_prec_scale */
  6930.     1,                /* is_long */
  6931.     0,                /* auto_unique_value */
  6932.     null,                /* data_precision */
  6933.     null,                /* numeric_scale */
  6934.     2147483647,            /* column_size */
  6935.     '0x',                /* literal_prefix */
  6936.     null,                /* literal_suffix */
  6937.     1 /*DB_UNSEARCHABLE*/,        /* searchable */
  6938.     null,                /* unsigned_attribute */
  6939.     'text'                /* local_type_name */
  6940.     )
  6941.  
  6942. commit tran
  6943. go
  6944.  
  6945. /*
  6946. ** Insert the spt_provider_types rows for DBTYPE_DBTIMESTAMP data types
  6947. */
  6948. begin tran
  6949.  
  6950. /* Local Datetime */
  6951. insert into spt_provider_types values
  6952.     (
  6953.     61 /*SQLDATETIME*/,        /* ss_dtype */
  6954.     8,                /* fixlen */
  6955.     'datetime',            /* type_name */
  6956.     135 /*DBTYPE_DBTIMESTAMP*/,    /* oledb_data_type */
  6957.     1,                /* best_match */
  6958.     1,                /* is_nullable */
  6959.     0,                /* case_sensitive */
  6960.     0,                /* fixed_prec_scale */
  6961.     0,                /* is_long */
  6962.     0,                /* auto_unique_value */
  6963.     23,                /* data_precision */
  6964.     null,                /* numeric_scale */
  6965.     null,                /* column_size */
  6966.     '''',                /* literal_prefix */
  6967.     '''',                /* literal_suffix */
  6968.     4 /*DB_SEARCHABLE*/,        /* searchable */
  6969.     null,                /* unsigned_attribute */
  6970.     'datetime'            /* local_type_name */
  6971.     )
  6972.  
  6973. /* Local Smalldatetime */
  6974. insert into spt_provider_types values
  6975.     (
  6976.     58 /*SQLDATETIM4*/,        /* ss_dtype */
  6977.     4,                /* fixlen */
  6978.     'smalldatetime',        /* type_name */
  6979.     135 /*DBTYPE_DBTIMESTAMP*/,    /* oledb_data_type */
  6980.     0,                /* best_match */
  6981.     1,                /* is_nullable */
  6982.     0,                /* case_sensitive */
  6983.     0,                /* fixed_prec_scale */
  6984.     0,                /* is_long */
  6985.     0,                /* auto_unique_value */
  6986.     16,                /* data_precision */
  6987.     null,                /* numeric_scale */
  6988.     null,                /* column_size */
  6989.     '''',                /* literal_prefix */
  6990.     '''',                /* literal_suffix */
  6991.     4 /*DB_SEARCHABLE*/,        /* searchable */
  6992.     null,                /* unsigned_attribute */
  6993.     'smalldatetime'            /* local_type_name */
  6994.     )
  6995.  
  6996. if (charindex('7.00', @@version) = 0)
  6997.     begin    /*    Add nullable type for non-Sphinx server */
  6998.     
  6999.     /* Local Datetimn */
  7000.     insert into spt_provider_types values
  7001.         (
  7002.         111 /*SQLDATETIMN*/,        /* ss_dtype */
  7003.         4,                /* fixlen */
  7004.         'smalldatetime',        /* type_name */
  7005.         135 /*DBTYPE_DBTIMESTAMP*/,    /* oledb_data_type */
  7006.         0,                /* best_match */
  7007.         1,                /* is_nullable */
  7008.         0,                /* case_sensitive */
  7009.         0,                /* fixed_prec_scale */
  7010.         0,                /* is_long */
  7011.         0,                /* auto_unique_value */
  7012.         16,                /* data_precision */
  7013.         null,                /* numeric_scale */
  7014.         null,                /* column_size */
  7015.         '''',                /* literal_prefix */
  7016.         '''',                /* literal_suffix */
  7017.         4 /*DB_SEARCHABLE*/,        /* searchable */
  7018.         null,                /* unsigned_attribute */
  7019.         'smalldatetime'            /* local_type_name */
  7020.         )    
  7021.     insert into spt_provider_types values
  7022.         (
  7023.         111 /*SQLDATETIMN*/,        /* ss_dtype */
  7024.         8,                /* fixlen */
  7025.         'datetime',            /* type_name */
  7026.         135 /*DBTYPE_DBTIMESTAMP*/,    /* oledb_data_type */
  7027.         0,                /* best_match */
  7028.         1,                /* is_nullable */
  7029.         0,                /* case_sensitive */
  7030.         0,                /* fixed_prec_scale */
  7031.         0,                /* is_long */
  7032.         0,                /* auto_unique_value */
  7033.         23,                /* data_precision */
  7034.         null,                /* numeric_scale */
  7035.         null,                /* column_size */
  7036.         '''',                /* literal_prefix */
  7037.         '''',                /* literal_suffix */
  7038.         4 /*DB_SEARCHABLE*/,        /* searchable */
  7039.         null,                /* unsigned_attribute */
  7040.         'datetime'            /* local_type_name */
  7041.         )
  7042.     end
  7043.  
  7044. commit tran
  7045. go
  7046.  
  7047. /*
  7048. ** Insert the spt_provider_types rows for DBTYPE_CY data types
  7049. */
  7050. begin tran
  7051.  
  7052. /* Local Smallmoney */
  7053. insert into spt_provider_types values
  7054.     (
  7055.     122 /*SQLMONEY4*/,        /* ss_dtype */
  7056.     4,                /* fixlen */
  7057.     'smallmoney',            /* type_name */
  7058.     6 /*DBTYPE_CY*/,        /* oledb_data_type */
  7059.     0,                /* best_match */
  7060.     1,                /* is_nullable */
  7061.     0,                /* case_sensitive */
  7062.     1,                /* fixed_prec_scale */
  7063.     0,                /* is_long */
  7064.     0,                /* auto_unique_value */
  7065.     10,                /* data_precision */
  7066.     null,                /* numeric_scale */
  7067.     null,                /* column_size */
  7068.     '$',                /* literal_prefix */
  7069.     null,                /* literal_suffix */
  7070.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  7071.     0,                /* unsigned_attribute */
  7072.     'smallmoney'            /* local_type_name */
  7073.     )    
  7074.     
  7075. /* Local Money */
  7076. insert into spt_provider_types values
  7077.     (
  7078.     60 /*SQLMONEY*/,        /* ss_dtype */
  7079.     8,                /* fixlen */
  7080.     'money',            /* type_name */
  7081.     6 /*DBTYPE_CY*/,        /* oledb_data_type */
  7082.     1,                /* best_match */
  7083.     1,                /* is_nullable */
  7084.     0,                /* case_sensitive */
  7085.     1,                /* fixed_prec_scale */
  7086.     0,                /* is_long */
  7087.     0,                /* auto_unique_value */
  7088.     19,                /* data_precision */
  7089.     null,                /* numeric_scale */
  7090.     null,                /* column_size */
  7091.     '$',                /* literal_prefix */
  7092.     null,                /* literal_suffix */
  7093.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  7094.     0,                /* unsigned_attribute */
  7095.     'money'                /* local_type_name */
  7096.     )    
  7097.  
  7098. if (charindex('7.00', @@version) = 0)
  7099.     begin    /*    Add nullable type for non-Sphinx server */
  7100.  
  7101.     /* Local Moneyn */
  7102.     insert into spt_provider_types values
  7103.         (
  7104.         110 /*SQLMONEYN*/,    /* ss_dtype */
  7105.         4,            /* fixlen */
  7106.         'smallmoney',        /* type_name */
  7107.         6 /*DBTYPE_CY*/,    /* oledb_data_type */
  7108.         0,            /* best_match */
  7109.         1,            /* is_nullable */
  7110.         0,            /* case_sensitive */
  7111.         1,            /* fixed_prec_scale */
  7112.         0,            /* is_long */
  7113.         0,            /* auto_unique_value */
  7114.         10,            /* data_precision */
  7115.         null,            /* numeric_scale */
  7116.         null,            /* column_size */
  7117.         '$',            /* literal_prefix */
  7118.         null,            /* literal_suffix */
  7119.         3 /*DB_ALL_EXCEPT_LIKE*/, /* searchable */
  7120.         0,            /* unsigned_attribute */
  7121.         'smallmoney'        /* local_type_name */
  7122.         )    
  7123.     insert into spt_provider_types values
  7124.         (
  7125.         110 /*SQLMONEYN*/,    /* ss_dtype */
  7126.         8,            /* fixlen */
  7127.         'money',        /* type_name */
  7128.         6 /*DBTYPE_CY*/,    /* oledb_data_type */
  7129.         0,            /* best_match */
  7130.         1,            /* is_nullable */
  7131.         0,            /* case_sensitive */
  7132.         1,            /* fixed_prec_scale */
  7133.         0,            /* is_long */
  7134.         0,            /* auto_unique_value */
  7135.         19,            /* data_precision */
  7136.         null,            /* numeric_scale */
  7137.         null,            /* column_size */
  7138.         '$',            /* literal_prefix */
  7139.         null,            /* literal_suffix */
  7140.         3 /*DB_ALL_EXCEPT_LIKE*/, /* searchable */
  7141.         null,            /* unsigned_attribute */
  7142.         'money'            /* local_type_name */
  7143.         )    
  7144.     end
  7145.  
  7146. commit tran
  7147. go
  7148.  
  7149. /*
  7150. ** Insert the spt_provider_types rows for the numeric data types
  7151. */
  7152. begin tran
  7153.  
  7154. /* Local Float */
  7155. insert into spt_provider_types values
  7156.     (
  7157.     62 /*SQLFLT8*/,            /* ss_dtype */
  7158.     8,                /* fixlen */
  7159.     'float',            /* type_name */
  7160.     5 /*DBTYPE_R8*/,        /* oledb_data_type */
  7161.     1,                /* best_match */
  7162.     1,                /* is_nullable */
  7163.     0,                /* case_sensitive */
  7164.     0,                /* fixed_prec_scale */
  7165.     0,                /* is_long */
  7166.     0,                /* auto_unique_value */
  7167.     15,                /* data_precision */
  7168.     null,                /* numeric_scale */
  7169.     null,                /* column_size */
  7170.     null,                /* literal_prefix */
  7171.     null,                /* literal_suffix */
  7172.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  7173.     0,                /* unsigned_attribute */
  7174.     'float'                /* local_type_name */
  7175.     )    
  7176.  
  7177. /* Local Real */
  7178. insert into spt_provider_types values
  7179.     (
  7180.     59 /*SQLFLT4*/,            /* ss_dtype */
  7181.     4,                /* fixlen */
  7182.     'real',                /* type_name */
  7183.     4 /*DBTYPE_R4*/,        /* oledb_data_type */
  7184.     1,                /* best_match */
  7185.     1,                /* is_nullable */
  7186.     0,                /* case_sensitive */
  7187.     0,                /* fixed_prec_scale */
  7188.     0,                /* is_long */
  7189.     0,                /* auto_unique_value */
  7190.     7,                /* data_precision */
  7191.     null,                /* numeric_scale */
  7192.     null,                /* column_size */
  7193.     null,                /* literal_prefix */
  7194.     null,                /* literal_suffix */
  7195.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  7196.     0,                /* unsigned_attribute */
  7197.     'real'                /* local_type_name */
  7198.     )    
  7199.  
  7200. /* Local Int */
  7201. insert into spt_provider_types values
  7202.     (
  7203.     56 /*SQLINT4*/,            /* ss_dtype */
  7204.     4,                /* fixlen */
  7205.     'int',                /* type_name */
  7206.     3 /*DBTYPE_I4*/,        /* oledb_data_type */
  7207.     1,                /* best_match */
  7208.     1,                /* is_nullable */
  7209.     0,                /* case_sensitive */
  7210.     1,                /* fixed_prec_scale */
  7211.     0,                /* is_long */
  7212.     0,                /* auto_unique_value */
  7213.     10,                /* data_precision */
  7214.     null,                /* numeric_scale */
  7215.     null,                /* column_size */
  7216.     null,                /* literal_prefix */
  7217.     null,                /* literal_suffix */
  7218.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  7219.     0,                /* unsigned_attribute */
  7220.     'int'                /* local_type_name */
  7221.     )    
  7222.  
  7223. /* Local Smallint */
  7224. insert into spt_provider_types values
  7225.     (
  7226.     52 /*SQLINT2*/,            /* ss_dtype */
  7227.     2,                /* fixlen */
  7228.     'smallint',            /* type_name */
  7229.     2 /*DBTYPE_I2*/,        /* oledb_data_type */
  7230.     1,                /* best_match */
  7231.     1,                /* is_nullable */
  7232.     0,                /* case_sensitive */
  7233.     1,                /* fixed_prec_scale */
  7234.     0,                /* is_long */
  7235.     0,                /* auto_unique_value */
  7236.     5,                /* data_precision */
  7237.     null,                /* numeric_scale */
  7238.     null,                /* column_size */
  7239.     null,                /* literal_prefix */
  7240.     null,                /* literal_suffix */
  7241.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  7242.     0,                /* unsigned_attribute */
  7243.     'smallint'            /* local_type_name */
  7244.     )    
  7245.  
  7246. /* Local Tinyint */
  7247. insert into spt_provider_types values
  7248.     (
  7249.     48 /*SQLINT1*/,            /* ss_dtype */
  7250.     1,                /* fixlen */
  7251.     'tinyint',            /* type_name */
  7252.     17 /*DBTYPE_UI1*/,        /* oledb_data_type */
  7253.     1,                /* best_match */
  7254.     1,                /* is_nullable */
  7255.     0,                /* case_sensitive */
  7256.     1,                /* fixed_prec_scale */
  7257.     0,                /* is_long */
  7258.     0,                /* auto_unique_value */
  7259.     3,                /* data_precision */
  7260.     null,                /* numeric_scale */
  7261.     null,                /* column_size */
  7262.     null,                /* literal_prefix */
  7263.     null,                /* literal_suffix */
  7264.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  7265.     1,                /* unsigned_attribute */
  7266.     'tinyint'            /* local_type_name */
  7267.     )    
  7268. commit tran
  7269. go
  7270. begin tran    
  7271. if (charindex('6.00', @@version) > 0 or
  7272.     charindex('6.50', @@version) > 0 or
  7273.     charindex('7.00', @@version) > 0)
  7274.     begin    /*    Add 6.0 data types */
  7275.     
  7276.     /* Local Decimal */
  7277.     insert into spt_provider_types values
  7278.         (
  7279.         55 /*SQLDECIMAL*/,        /* ss_dtype */
  7280.         0,                /* fixlen */
  7281.         'decimal',            /* type_name */
  7282.         131 /*DBTYPE_NUMERIC*/,        /* oledb_data_type */
  7283.         0,                /* best_match */
  7284.         1,                /* is_nullable */
  7285.         0,                /* case_sensitive */
  7286.         1,                /* fixed_prec_scale */
  7287.         0,                /* is_long */
  7288.         0,                /* auto_unique_value */
  7289.         28,                /* data_precision */
  7290.         null,                /* numeric_scale */
  7291.         null,                /* column_size */
  7292.         null,                /* literal_prefix */
  7293.         null,                /* literal_suffix */
  7294.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  7295.         0,                /* unsigned_attribute */
  7296.         'decimal'            /* local_type_name */
  7297.         )    
  7298.  
  7299.     /* Local Numeric */
  7300.     insert into spt_provider_types values
  7301.         (
  7302.         63 /*SQLNUMERIC*/,        /* ss_dtype */
  7303.         0,                /* fixlen */
  7304.         'numeric',            /* type_name */
  7305.         131 /*DBTYPE_NUMERIC*/,        /* oledb_data_type */
  7306.         1,                /* best_match */
  7307.         1,                /* is_nullable */
  7308.         0,                /* case_sensitive */
  7309.         1,                /* fixed_prec_scale */
  7310.         0,                /* is_long */
  7311.         0,                /* auto_unique_value */
  7312.         28,                /* data_precision */
  7313.         null,                /* numeric_scale */
  7314.         null,                /* column_size */
  7315.         null,                /* literal_prefix */
  7316.         null,                /* literal_suffix */
  7317.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  7318.         0,                /* unsigned_attribute */
  7319.         'numeric'            /* local_type_name */
  7320.         )    
  7321.  
  7322.     /* Identity attribute data types */
  7323.  
  7324.     /* Identity Int */
  7325.     insert into spt_provider_types values
  7326.         (
  7327.         56 /*SQLINT4*/,            /* ss_dtype */
  7328.         4,                /* fixlen */
  7329.         'int identity',            /* type_name */
  7330.         3 /*DBTYPE_I4*/,        /* oledb_data_type */
  7331.         0,                /* best_match */
  7332.         0,                /* is_nullable */
  7333.         0,                /* case_sensitive */
  7334.         1,                /* fixed_prec_scale */
  7335.         0,                /* is_long */
  7336.         1,                /* auto_unique_value */
  7337.         10,                /* data_precision */
  7338.         null,                /* numeric_scale */
  7339.         null,                /* column_size */
  7340.         null,                /* literal_prefix */
  7341.         null,                /* literal_suffix */
  7342.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  7343.         0,                /* unsigned_attribute */
  7344.         'int identity'            /* local_type_name */
  7345.         )    
  7346.  
  7347.     /* Identity Smallint */
  7348.     insert into spt_provider_types values
  7349.         (
  7350.         52 /*SQLINT2*/,            /* ss_dtype */
  7351.         2,                /* fixlen */
  7352.         'smallint identity',        /* type_name */
  7353.         2 /*DBTYPE_I2*/,        /* oledb_data_type */
  7354.         0,                /* best_match */
  7355.         0,                /* is_nullable */
  7356.         0,                /* case_sensitive */
  7357.         1,                /* fixed_prec_scale */
  7358.         0,                /* is_long */
  7359.         1,                /* auto_unique_value */
  7360.         5,                /* data_precision */
  7361.         null,                /* numeric_scale */
  7362.         null,                /* column_size */
  7363.         null,                /* literal_prefix */
  7364.         null,                /* literal_suffix */
  7365.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  7366.         0,                /* unsigned_attribute */
  7367.         'smallint identity'        /* local_type_name */
  7368.         )
  7369.  
  7370.     /* Identity Tinyint */
  7371.     insert into spt_provider_types values
  7372.         (
  7373.         48 /*SQLINT1*/,            /* ss_dtype */
  7374.         1,                /* fixlen */
  7375.         'tinyint identity',        /* type_name */
  7376.         17 /*DBTYPE_UI1*/,        /* oledb_data_type */
  7377.         0,                /* best_match */
  7378.         0,                /* is_nullable */
  7379.         0,                /* case_sensitive */
  7380.         1,                /* fixed_prec_scale */
  7381.         0,                /* is_long */
  7382.         1,                /* auto_unique_value */
  7383.         3,                /* data_precision */
  7384.         null,                /* numeric_scale */
  7385.         null,                /* column_size */
  7386.         null,                /* literal_prefix */
  7387.         null,                /* literal_suffix */
  7388.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  7389.         1,                /* unsigned_attribute */
  7390.         'tinyint identity'        /* local_type_name */
  7391.         )    
  7392.  
  7393.     /* Identity Decimal */
  7394.     insert into spt_provider_types values
  7395.         (
  7396.         55 /*SQLDECIMAL*/,        /* ss_dtype */
  7397.         0,                /* fixlen */
  7398.         'decimal() identity',        /* type_name */
  7399.         131 /*DBTYPE_NUMERIC*/,        /* oledb_data_type */
  7400.         0,                /* best_match */
  7401.         0,                /* is_nullable */
  7402.         0,                /* case_sensitive */
  7403.         1,                /* fixed_prec_scale */
  7404.         0,                /* is_long */
  7405.         1,                /* auto_unique_value */
  7406.         28,                /* data_precision */
  7407.         null,                /* numeric_scale */
  7408.         null,                /* column_size */
  7409.         null,                /* literal_prefix */
  7410.         null,                /* literal_suffix */
  7411.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  7412.         0,                /* unsigned_attribute */
  7413.         'decimal() identity'        /* local_type_name */
  7414.         )    
  7415.  
  7416.     /* Identity Numeric */
  7417.     insert into spt_provider_types values
  7418.         (
  7419.         63 /*SQLNUMERIC*/,        /* ss_dtype */
  7420.         0,                /* fixlen */
  7421.         'numeric() identity',        /* type_name */
  7422.         131 /*DBTYPE_NUMERIC*/,        /* oledb_data_type */
  7423.         0,                /* best_match */
  7424.         0,                /* is_nullable */
  7425.         0,                /* case_sensitive */
  7426.         1,                /* fixed_prec_scale */
  7427.         0,                /* is_long */
  7428.         1,                /* auto_unique_value */
  7429.         28,                /* data_precision */
  7430.         null,                /* numeric_scale */
  7431.         null,                /* column_size */
  7432.         null,                /* literal_prefix */
  7433.         null,                /* literal_suffix */
  7434.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  7435.         0,                /* unsigned_attribute */
  7436.         'numeric() identity'        /* local_type_name */
  7437.         )        
  7438.     end
  7439. commit tran
  7440. go
  7441. begin tran
  7442. if (charindex('7.00', @@version) = 0)
  7443.     begin    /*    Add nullable type for non-Sphinx server */
  7444.     
  7445.     /* Local Floatn */
  7446.     insert into spt_provider_types values
  7447.         (
  7448.         109 /*SQLFLTN*/,        /* ss_dtype */
  7449.         8,                /* fixlen */
  7450.         'float',            /* type_name */
  7451.         5 /*DBTYPE_R8*/,        /* oledb_data_type */
  7452.         0,                /* best_match */
  7453.         1,                /* is_nullable */
  7454.         0,                /* case_sensitive */
  7455.         0,                /* fixed_prec_scale */
  7456.         0,                /* is_long */
  7457.         0,                /* auto_unique_value */
  7458.         15,                /* data_precision */
  7459.         null,                /* numeric_scale */
  7460.         null,                /* column_size */
  7461.         null,                /* literal_prefix */
  7462.         null,                /* literal_suffix */
  7463.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  7464.         null,                /* unsigned_attribute */
  7465.         'float'                /* local_type_name */
  7466.         )    
  7467.     insert into spt_provider_types values
  7468.         (
  7469.         109 /*SQLFLT4*/,        /* ss_dtype */
  7470.         4,                /* fixlen */
  7471.         'real',                /* type_name */
  7472.         4 /*DBTYPE_R4*/,        /* oledb_data_type */
  7473.         0,                /* best_match */
  7474.         1,                /* is_nullable */
  7475.         0,                /* case_sensitive */
  7476.         0,                /* fixed_prec_scale */
  7477.         0,                /* is_long */
  7478.         0,                /* auto_unique_value */
  7479.         7,                /* data_precision */
  7480.         null,                /* numeric_scale */
  7481.         null,                /* column_size */
  7482.         null,                /* literal_prefix */
  7483.         null,                /* literal_suffix */
  7484.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  7485.         null,                /* unsigned_attribute */
  7486.         'real'                /* local_type_name */
  7487.         )    
  7488.  
  7489.     /* Local Intn */
  7490.     insert into spt_provider_types values
  7491.         (
  7492.         38 /*SQLINTN*/,            /* ss_dtype */
  7493.         4,                /* fixlen */
  7494.         'int',                /* type_name */
  7495.         3 /*DBTYPE_I4*/,        /* oledb_data_type */
  7496.         1,                /* best_match */
  7497.         1,                /* is_nullable */
  7498.         0,                /* case_sensitive */
  7499.         1,                /* fixed_prec_scale */
  7500.         0,                /* is_long */
  7501.         0,                /* auto_unique_value */
  7502.         10,                /* data_precision */
  7503.         null,                /* numeric_scale */
  7504.         null,                /* column_size */
  7505.         null,                /* literal_prefix */
  7506.         null,                /* literal_suffix */
  7507.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  7508.         0,                /* unsigned_attribute */
  7509.         'int'                /* local_type_name */
  7510.         )    
  7511.     insert into spt_provider_types values
  7512.         (
  7513.         38 /*SQLINTN*/,            /* ss_dtype */
  7514.         2,                /* fixlen */
  7515.         'smallint',            /* type_name */
  7516.         2 /*DBTYPE_I2*/,        /* oledb_data_type */
  7517.         0,                /* best_match */
  7518.         1,                /* is_nullable */
  7519.         0,                /* case_sensitive */
  7520.         1,                /* fixed_prec_scale */
  7521.         0,                /* is_long */
  7522.         0,                /* auto_unique_value */
  7523.         5,                /* data_precision */
  7524.         null,                /* numeric_scale */
  7525.         null,                /* column_size */
  7526.         null,                /* literal_prefix */
  7527.         null,                /* literal_suffix */
  7528.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  7529.         0,                /* unsigned_attribute */
  7530.         'smallint'            /* local_type_name */
  7531.         )    
  7532.     insert into spt_provider_types values
  7533.         (
  7534.         38 /*SQLINTN*/,            /* ss_dtype */
  7535.         1,                /* fixlen */
  7536.         'tinyint',            /* type_name */
  7537.         17 /*DBTYPE_UI1*/,        /* oledb_data_type */
  7538.         0,                /* best_match */
  7539.         1,                /* is_nullable */
  7540.         0,                /* case_sensitive */
  7541.         1,                /* fixed_prec_scale */
  7542.         0,                /* is_long */
  7543.         0,                /* auto_unique_value */
  7544.         3,                /* data_precision */
  7545.         null,                /* numeric_scale */
  7546.         null,                /* column_size */
  7547.         null,                /* literal_prefix */
  7548.         null,                /* literal_suffix */
  7549.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  7550.         1,                /* unsigned_attribute */
  7551.         'tinyint'            /* local_type_name */
  7552.         )    
  7553.  
  7554.     if (charindex('6.00', @@version) > 0 or    charindex('6.50', @@version) > 0)
  7555.         begin    /*    Add 6.0 data types */
  7556.         
  7557.         /* Local Decimaln */
  7558.         insert into spt_provider_types values
  7559.             (
  7560.             106 /*SQLDECIMALN*/,        /* ss_dtype */
  7561.             0,                /* fixlen */
  7562.             'decimal',            /* type_name */
  7563.             131 /*DBTYPE_NUMERIC*/,        /* oledb_data_type */
  7564.             0,                /* best_match */
  7565.             1,                /* is_nullable */
  7566.             0,                /* case_sensitive */
  7567.             1,                /* fixed_prec_scale */
  7568.             0,                /* is_long */
  7569.             0,                /* auto_unique_value */
  7570.             28,                /* data_precision */
  7571.             null,                /* numeric_scale */
  7572.             null,                /* column_size */
  7573.             null,                /* literal_prefix */
  7574.             null,                /* literal_suffix */
  7575.             3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  7576.             0,                /* unsigned_attribute */
  7577.             'decimal'            /* local_type_name */
  7578.             )    
  7579.         insert into spt_provider_types values
  7580.             (
  7581.             106 /*SQLDECIMALN*/,        /* ss_dtype */
  7582.             0,                /* fixlen */
  7583.             'decimal() identity',        /* type_name */
  7584.             131 /*DBTYPE_NUMERIC*/,        /* oledb_data_type */
  7585.             0,                /* best_match */
  7586.             0,                /* is_nullable */
  7587.             0,                /* case_sensitive */
  7588.             1,                /* fixed_prec_scale */
  7589.             0,                /* is_long */
  7590.             1,                /* auto_unique_value */
  7591.             28,                /* data_precision */
  7592.             null,                /* numeric_scale */
  7593.             null,                /* column_size */
  7594.             null,                /* literal_prefix */
  7595.             null,                /* literal_suffix */
  7596.             3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  7597.             0,                /* unsigned_attribute */
  7598.             'decimal() identity'        /* local_type_name */
  7599.             )    
  7600.  
  7601.         /* Local Numericn */
  7602.         insert into spt_provider_types values
  7603.             (
  7604.             108 /*SQLNUMERICN*/,        /* ss_dtype */
  7605.             0,                /* fixlen */
  7606.             'numeric',            /* type_name */
  7607.             131 /*DBTYPE_NUMERIC*/,        /* oledb_data_type */
  7608.             0,                /* best_match */
  7609.             1,                /* is_nullable */
  7610.             0,                /* case_sensitive */
  7611.             1,                /* fixed_prec_scale */
  7612.             0,                /* is_long */
  7613.             0,                /* auto_unique_value */
  7614.             28,                /* data_precision */
  7615.             null,                /* numeric_scale */
  7616.             null,                /* column_size */
  7617.             null,                /* literal_prefix */
  7618.             null,                /* literal_suffix */
  7619.             3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  7620.             0,                /* unsigned_attribute */
  7621.             'numeric'            /* local_type_name */
  7622.             )    
  7623.         insert into spt_provider_types values
  7624.             (
  7625.             108 /*SQLNUMERICN*/,        /* ss_dtype */
  7626.             0,                /* fixlen */
  7627.             'numeric() identity',        /* type_name */
  7628.             131 /*DBTYPE_NUMERIC*/,        /* oledb_data_type */
  7629.             0,                /* best_match */
  7630.             0,                /* is_nullable */
  7631.             0,                /* case_sensitive */
  7632.             1,                /* fixed_prec_scale */
  7633.             0,                /* is_long */
  7634.             1,                /* auto_unique_value */
  7635.             28,                /* data_precision */
  7636.             null,                /* numeric_scale */
  7637.             null,                /* column_size */
  7638.             null,                /* literal_prefix */
  7639.             null,                /* literal_suffix */
  7640.             3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  7641.             0,                /* unsigned_attribute */
  7642.             'numeric() identity'        /* local_type_name */
  7643.             )    
  7644.         end
  7645.     end
  7646. commit tran
  7647. go
  7648.  
  7649. /*
  7650. **    Remaining data types
  7651. */
  7652. begin tran
  7653.  
  7654. if (charindex('7.00', @@version) > 0)
  7655.     begin
  7656.     /* Local Bit */
  7657.     insert into spt_provider_types values
  7658.         (
  7659.         50 /*SQLBIT*/,            /* ss_dtype */
  7660.         0,                /* fixlen */
  7661.         'bit',                /* type_name */
  7662.         11 /*DBTYPE_BOOL*/,        /* oledb_data_type */
  7663.         1,                /* best_match */
  7664.         1,                /* is_nullable */
  7665.         0,                /* case_sensitive */
  7666.         0,                /* fixed_prec_scale */
  7667.         0,                /* is_long */
  7668.         0,                /* auto_unique_value */
  7669.         1,                /* data_precision */
  7670.         null,                /* numeric_scale */
  7671.         null,                /* column_size */
  7672.         null,                /* literal_prefix */
  7673.         null,                /* literal_suffix */
  7674.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  7675.         null,                /* unsigned_attribute */
  7676.         'bit'                /* local_type_name */
  7677.         )        
  7678.     
  7679.     /* data types changed in Sphinx */    
  7680.     update spt_provider_types set ss_dtype = (select xtype from systypes    where type_name like name+'%')
  7681.  
  7682.     /* Local Timestamp */
  7683.     insert into spt_provider_types values
  7684.         (
  7685.         152,                /* ss_dtype */
  7686.         8,                /* fixlen */
  7687.         'timestamp',            /* type_name */
  7688.         21 /*DBTYPE_UI8*/,        /* oledb_data_type */
  7689.         1,                /* best_match */
  7690.         0,                /* is_nullable */
  7691.         0,                /* case_sensitive */
  7692.         1,                /* fixed_prec_scale */
  7693.         0,                /* is_long */
  7694.         0,                /* auto_unique_value */
  7695.         20,                /* data_precision */
  7696.         null,                /* numeric_scale */
  7697.         null,                /* column_size */
  7698.         '0x',                /* literal_prefix */
  7699.         null,                /* literal_suffix */
  7700.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  7701.         1,                /* unsigned_attribute */
  7702.         'timestamp'            /* local_type_name */
  7703.         )    
  7704.     /* Local GUID */
  7705.     insert into spt_provider_types values
  7706.         (
  7707.         72,                /* ss_dtype */
  7708.         16,                /* fixlen */
  7709.         'uniqueidentifier',        /* type_name */
  7710.         72 /*DBTYPE_GUID*/,        /* oledb_data_type */
  7711.         1,                /* best_match */
  7712.         0,                /* is_nullable */
  7713.         0,                /* case_sensitive */
  7714.         0,                /* fixed_prec_scale */
  7715.         0,                /* is_long */
  7716.         0,                /* auto_unique_value */
  7717.         null,                /* data_precision */
  7718.         null,                /* numeric_scale */
  7719.         null,                /* column_size */
  7720.         '''',                /* literal_prefix */
  7721.         '''',                /* literal_suffix */
  7722.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  7723.         null,                /* unsigned_attribute */
  7724.         'uniqueidentifier'        /* local_type_name */
  7725.         )    
  7726.     end
  7727. commit tran
  7728. go
  7729. begin tran
  7730. if (charindex('7.00', @@version) = 0)
  7731.     begin
  7732.     /* Local Bit */
  7733.     insert into spt_provider_types values
  7734.         (
  7735.         50 /*SQLBIT*/,            /* ss_dtype */
  7736.         0,                /* fixlen */
  7737.         'bit',                /* type_name */
  7738.         11 /*DBTYPE_BOOL*/,        /* oledb_data_type */
  7739.         1,                /* best_match */
  7740.         0,                /* is_nullable */
  7741.         0,                /* case_sensitive */
  7742.         0,                /* fixed_prec_scale */
  7743.         0,                /* is_long */
  7744.         0,                /* auto_unique_value */
  7745.         1,                /* data_precision */
  7746.         null,                /* numeric_scale */
  7747.         null,                /* column_size */
  7748.         null,                /* literal_prefix */
  7749.         null,                /* literal_suffix */
  7750.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  7751.         null,                /* unsigned_attribute */
  7752.         'bit'                /* local_type_name */
  7753.         )    
  7754.  
  7755.     /* Local Timestamp */
  7756.     insert into spt_provider_types values
  7757.         (
  7758.         45 /*SQLBINARY*/,        /* ss_dtype */
  7759.         8,                /* fixlen */
  7760.         'timestamp',            /* type_name */
  7761.         21 /*DBTYPE_UI8*/,        /* oledb_data_type */
  7762.         1,                /* best_match */
  7763.         0,                /* is_nullable */
  7764.         0,                /* case_sensitive */
  7765.         1,                /* fixed_prec_scale */
  7766.         0,                /* is_long */
  7767.         0,                /* auto_unique_value */
  7768.         20,                /* data_precision */
  7769.         null,                /* numeric_scale */
  7770.         null,                /* column_size */
  7771.         '0x',                /* literal_prefix */
  7772.         null,                /* literal_suffix */
  7773.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  7774.         1,                /* unsigned_attribute */
  7775.         'timestamp'            /* local_type_name */
  7776.         )        
  7777. end
  7778.  
  7779. commit tran
  7780. go
  7781.  
  7782. create unique clustered index datatypeinfoclust on spt_provider_types(ss_dtype,fixlen,auto_unique_value )
  7783. go
  7784.  
  7785. dump tran master with no_log
  7786. go
  7787.  
  7788. print 'creating sp_catalogs_rowset'
  7789. go
  7790.  
  7791. create procedure sp_catalogs_rowset
  7792.     (
  7793.     @catalog_name        varchar(255)
  7794.     )        
  7795. as
  7796.     select
  7797.         CATALOG_NAME    = name,
  7798.         DESCRIPTION    = convert(varchar(1),null)
  7799.     from     master.dbo.sysdatabases
  7800.     where    name = @catalog_name
  7801. go
  7802. create procedure sp_catalogs_rowset;2
  7803.     (
  7804.     @dummy        int /* remove when Hydra bug 17032 is fixed */
  7805.     )
  7806. as
  7807.     select
  7808.         CATALOG_NAME    = name,
  7809.         DESCRIPTION    = convert(varchar(1),null)
  7810.     from     master.dbo.sysdatabases
  7811.     order by 1
  7812. go
  7813.  
  7814. grant execute on sp_catalogs_rowset to public
  7815. go
  7816.  
  7817. dump tran master with no_log
  7818. go
  7819.  
  7820.  
  7821. print 'creating sp_column_privileges_rowset'
  7822. go
  7823.  
  7824. /*    Procedure for 6.0 and 6.5 server */
  7825. CREATE PROCEDURE sp_column_privileges_rowset
  7826.     (
  7827.     @table_name     varchar(255) = null,
  7828.     @table_schema    varchar(255) = null,
  7829.     @column_name    varchar(255) = null,
  7830.     @grantor    varchar(255) = null,
  7831.     @grantee    varchar(255) = null
  7832.     )
  7833. as
  7834. IF @table_name is not null
  7835.     BEGIN
  7836.     select
  7837.         GRANTOR        = user_name(p.grantor),
  7838.         GRANTEE        = user_name(u.uid),
  7839.         TABLE_CATALOG    = db_name(),
  7840.         TABLE_SCHEMA    = user_name(o.uid),
  7841.         TABLE_NAME    = o.name,
  7842.         COLUMN_NAME    = c.name,
  7843.         COLUMN_GUID    = convert(binary(16),null),
  7844.         COLUMN_PROPID    = convert(int,null),
  7845.         PRIVILEGE_TYPE    = convert(varchar(30),
  7846.                     case p.action
  7847.                     when 193 then 'SELECT'
  7848.                     when 195 then 'INSERT'
  7849.                     when 197 then 'UPDATE'
  7850.                     else 'REFERENCES'
  7851.                     end),
  7852.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  7853.     from 
  7854.         sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c
  7855.     where
  7856.         o.name = @table_name
  7857.     and     o.type in ('U','V')
  7858.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  7859.     and    c.id = o.id
  7860.     and     (@column_name is null or @column_name = c.name)
  7861.     and     c.id = p.id
  7862.     and    (@grantor is null or @grantor = user_name(p.grantor))
  7863.     and     case 
  7864.             when substring(p.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  7865.             when substring(p.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p.columns, v.low, 1))
  7866.             else (~convert(tinyint, coalesce(substring(p.columns, v.low, 1),0)))
  7867.             end & v.high <> 0            /* permission applies to this column */
  7868.     and     v.number <= (select count(*) from syscolumns where id = o.id)    /* ranges from 1 to # of columns in table */
  7869.     and     v.type = 'P'
  7870.     and     v.number = c.colid
  7871.     and    (@grantee is null or @grantee = user_name(u.uid))
  7872.             /* expand groups */
  7873.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  7874.     and     p.protecttype <> 206    /* only grant rows */
  7875.     and     p.action in (26,193,195,197)
  7876.     and     o.uid <> u.uid            /* no rows for owner */
  7877.     and     not exists (            /* exclude revoke'd privileges */
  7878.             select *
  7879.             from sysprotects p1
  7880.             where
  7881.                 p1.protecttype = 206
  7882.             and     p1.action = p.action
  7883.             and     p1.id = p.id
  7884.             and     p1.uid = u.uid
  7885.             and     case 
  7886.                 when substring(p1.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  7887.                 when substring(p1.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  7888.                                 else (~convert(tinyint,coalesce(substring(p.columns, v.low, 1),0)))
  7889.                 end & v.high <> 0)            /* permission applies to this column */
  7890.     union all
  7891.     select    /*    Add rows for table owner */
  7892.         GRANTOR        = user_name(u.uid),
  7893.         GRANTEE        = user_name(o.uid),
  7894.         TABLE_CATALOG    = db_name(),
  7895.         TABLE_SCHEMA    = user_name(o.uid),
  7896.         TABLE_NAME    = o.name,
  7897.         COLUMN_NAME    = c.name,
  7898.         COLUMN_GUID    = convert(binary(16),null),
  7899.         COLUMN_PROPID    = convert(int,null),
  7900.         PRIVILEGE_TYPE    = convert(varchar(30),
  7901.                     case v.number
  7902.                     when 193 then 'SELECT'
  7903.                     when 195 then 'INSERT'
  7904.                     when 197 then 'UPDATE'
  7905.                     else 'REFERENCES'
  7906.                     end),
  7907.         IS_GRANTABLE    = convert(bit,1)    
  7908.     from 
  7909.         sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  7910.     where
  7911.         o.name = @table_name
  7912.     and     o.type in ('U','V')
  7913.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  7914.     and    (@grantee is null or @grantee = user_name(o.uid))
  7915.     and    c.id = o.id
  7916.     and     (@column_name is null or @column_name = c.name)
  7917.     and     u.suid = 1        /* grantor is dbo of database */
  7918.     and    (@grantor is null or @grantor = user_name(u.uid))
  7919.     and     v.type = 'P'        /* cross product to get all exposed privileges */
  7920.     and     v.number in (26,193,195,197)
  7921.     and     not exists (        /* exclude revoke'd privileges */
  7922.             select *
  7923.             from     sysprotects p1
  7924.             where    p1.protecttype = 206
  7925.             and     p1.action = v.number
  7926.             and     p1.id = o.id
  7927.             and     p1.uid = o.uid)
  7928.     order by 4, 5, 6, 9, 1, 2
  7929.     END
  7930. ELSE
  7931.     BEGIN
  7932.     select
  7933.         GRANTOR        = user_name(p.grantor),
  7934.         GRANTEE        = user_name(u.uid),
  7935.         TABLE_CATALOG    = db_name(),
  7936.         TABLE_SCHEMA    = user_name(o.uid),
  7937.         TABLE_NAME    = o.name,
  7938.         COLUMN_NAME    = c.name,
  7939.         COLUMN_GUID    = convert(binary(16),null),
  7940.         COLUMN_PROPID    = convert(int,null),
  7941.         PRIVILEGE_TYPE    = convert(varchar(30),
  7942.                     case p.action
  7943.                     when 193 then 'SELECT'
  7944.                     when 195 then 'INSERT'
  7945.                     when 197 then 'UPDATE'
  7946.                     else 'REFERENCES'
  7947.                     end),
  7948.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  7949.     from 
  7950.         sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c
  7951.     where
  7952.         o.type in ('U','V')
  7953.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  7954.     and    c.id = o.id
  7955.     and     (@column_name is null or @column_name = c.name)
  7956.     and     c.id = p.id
  7957.     and    (@grantor is null or @grantor = user_name(p.grantor))
  7958.     and     case 
  7959.             when substring(p.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  7960.             when substring(p.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p.columns, v.low, 1))
  7961.             else (~convert(tinyint, coalesce(substring(p.columns, v.low, 1),0)))
  7962.             end & v.high <> 0            /* permission applies to this column */
  7963.     and     v.number <= (select count(*) from syscolumns where id = o.id)    /* ranges from 1 to # of columns in table */
  7964.     and     v.type = 'P'
  7965.     and     v.number = c.colid
  7966.     and    (@grantee is null or @grantee = user_name(u.uid))
  7967.             /* expand groups */
  7968.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  7969.     and     p.protecttype <> 206    /* only grant rows */
  7970.     and     p.action in (26,193,195,197)
  7971.     and     o.uid <> u.uid            /* no rows for owner */
  7972.     and     not exists (            /* exclude revoke'd privileges */
  7973.             select *
  7974.             from sysprotects p1
  7975.             where
  7976.                 p1.protecttype = 206
  7977.             and     p1.action = p.action
  7978.             and     p1.id = p.id
  7979.             and     p1.uid = u.uid
  7980.             and     case 
  7981.                 when substring(p1.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  7982.                 when substring(p1.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  7983.                                 else (~convert(tinyint,coalesce(substring(p.columns, v.low, 1),0)))
  7984.                 end & v.high <> 0)            /* permission applies to this column */
  7985.     union all
  7986.     select    /*    Add rows for table owner */
  7987.         GRANTOR        = user_name(u.uid),
  7988.         GRANTEE        = user_name(o.uid),
  7989.         TABLE_CATALOG    = db_name(),
  7990.         TABLE_SCHEMA    = user_name(o.uid),
  7991.         TABLE_NAME    = o.name,
  7992.         COLUMN_NAME    = c.name,
  7993.         COLUMN_GUID    = convert(binary(16),null),
  7994.         COLUMN_PROPID    = convert(int,null),
  7995.         PRIVILEGE_TYPE    = convert(varchar(30),
  7996.                     case v.number
  7997.                     when 193 then 'SELECT'
  7998.                     when 195 then 'INSERT'
  7999.                     when 197 then 'UPDATE'
  8000.                     else 'REFERENCES'
  8001.                     end),
  8002.         IS_GRANTABLE    = convert(bit,1)    
  8003.     from 
  8004.         sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  8005.     where
  8006.          o.type in ('U','V')
  8007.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  8008.     and    (@grantee is null or @grantee = user_name(o.uid))
  8009.     and    c.id = o.id
  8010.     and     (@column_name is null or @column_name = c.name)
  8011.     and     u.suid = 1        /* grantor is dbo of database */
  8012.     and    (@grantor is null or @grantor = user_name(u.uid))
  8013.     and     v.type = 'P'        /* cross product to get all exposed privileges */
  8014.     and     v.number in (26,193,195,197)
  8015.     and     not exists (        /* exclude revoke'd privileges */
  8016.             select *
  8017.             from     sysprotects p1
  8018.             where    p1.protecttype = 206
  8019.             and     p1.action = v.number
  8020.             and     p1.id = o.id
  8021.             and     p1.uid = o.uid)
  8022.     order by 4, 5, 6, 9, 1, 2
  8023.     END
  8024. go
  8025. CREATE PROCEDURE sp_column_privileges_rowset;2
  8026.     (
  8027.        @handle        int output,
  8028.        @scrollopt    int output,
  8029.     @ccopt        int output,
  8030.     @rows        int output,
  8031.     @table_name     varchar(255) = null,
  8032.     @table_schema    varchar(255) = null,
  8033.     @column_name    varchar(255) = null,
  8034.     @grantor    varchar(255) = null,
  8035.     @grantee    varchar(255) = null
  8036.     )
  8037. as
  8038.  
  8039. declare @ret int
  8040.     
  8041. create table #spcprivsrowset1
  8042.     (
  8043.     GRANTOR        varchar(30) not null,
  8044.     GRANTEE        varchar(30) not null,
  8045.     TABLE_CATALOG    varchar(30) not null,
  8046.     TABLE_SCHEMA    varchar(30) not null,
  8047.     TABLE_NAME    varchar(30) not null,
  8048.     COLUMN_NAME    varchar(30) not null,
  8049.     COLUMN_GUID    binary(16) null,
  8050.     COLUMN_PROPID    int null,
  8051.     PRIVILEGE_TYPE    varchar(30) not null,
  8052.     IS_GRANTABLE    bit not null
  8053.     )
  8054.  
  8055. IF @table_name is not null
  8056.     BEGIN
  8057.     insert into #spcprivsrowset1
  8058.     select
  8059.         GRANTOR        = user_name(p.grantor),
  8060.         GRANTEE        = user_name(u.uid),
  8061.         TABLE_CATALOG    = db_name(),
  8062.         TABLE_SCHEMA    = user_name(o.uid),
  8063.         TABLE_NAME    = o.name,
  8064.         COLUMN_NAME    = c.name,
  8065.         COLUMN_GUID    = convert(binary(16),null),
  8066.         COLUMN_PROPID    = convert(int,null),
  8067.         PRIVILEGE_TYPE    = convert(varchar(30),
  8068.                     case p.action
  8069.                     when 193 then 'SELECT'
  8070.                     when 195 then 'INSERT'
  8071.                     when 197 then 'UPDATE'
  8072.                     else 'REFERENCES'
  8073.                     end),
  8074.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  8075.     from 
  8076.         sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c
  8077.     where
  8078.         o.name = @table_name
  8079.     and     o.type in ('U','V')
  8080.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  8081.     and    c.id = o.id
  8082.     and     (@column_name is null or @column_name = c.name)
  8083.     and     c.id = p.id
  8084.     and    (@grantor is null or @grantor = user_name(p.grantor))
  8085.     and     case 
  8086.             when substring(p.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  8087.             when substring(p.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p.columns, v.low, 1))
  8088.             else (~convert(tinyint, coalesce(substring(p.columns, v.low, 1),0)))
  8089.             end & v.high <> 0            /* permission applies to this column */
  8090.     and     v.number <= (select count(*) from syscolumns where id = o.id)    /* ranges from 1 to # of columns in table */
  8091.     and     v.type = 'P'
  8092.     and     v.number = c.colid
  8093.     and    (@grantee is null or @grantee = user_name(u.uid))
  8094.             /* expand groups */
  8095.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  8096.     and     p.protecttype <> 206    /* only grant rows */
  8097.     and     p.action in (26,193,195,197)
  8098.     and     o.uid <> u.uid            /* no rows for owner */
  8099.     and     not exists (            /* exclude revoke'd privileges */
  8100.             select *
  8101.             from sysprotects p1
  8102.             where
  8103.                 p1.protecttype = 206
  8104.             and     p1.action = p.action
  8105.             and     p1.id = p.id
  8106.             and     p1.uid = u.uid
  8107.             and     case 
  8108.                 when substring(p1.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  8109.                 when substring(p1.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  8110.                                 else (~convert(tinyint,coalesce(substring(p.columns, v.low, 1),0)))
  8111.                 end & v.high <> 0)            /* permission applies to this column */
  8112.     union all
  8113.     select    /*    Add rows for table owner */
  8114.         GRANTOR        = user_name(u.uid),
  8115.         GRANTEE        = user_name(o.uid),
  8116.         TABLE_CATALOG    = db_name(),
  8117.         TABLE_SCHEMA    = user_name(o.uid),
  8118.         TABLE_NAME    = o.name,
  8119.         COLUMN_NAME    = c.name,
  8120.         COLUMN_GUID    = convert(binary(16),null),
  8121.         COLUMN_PROPID    = convert(int,null),
  8122.         PRIVILEGE_TYPE    = convert(varchar(30),
  8123.                     case v.number
  8124.                     when 193 then 'SELECT'
  8125.                     when 195 then 'INSERT'
  8126.                     when 197 then 'UPDATE'
  8127.                     else 'REFERENCES'
  8128.                     end),
  8129.         IS_GRANTABLE    = convert(bit,1)    
  8130.     from 
  8131.         sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  8132.     where
  8133.         o.name = @table_name
  8134.     and     o.type in ('U','V')
  8135.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  8136.     and    (@grantee is null or @grantee = user_name(o.uid))
  8137.     and    c.id = o.id
  8138.     and     (@column_name is null or @column_name = c.name)
  8139.     and     u.suid = 1        /* grantor is dbo of database */
  8140.     and    (@grantor is null or @grantor = user_name(u.uid))
  8141.     and     v.type = 'P'        /* cross product to get all exposed privileges */
  8142.     and     v.number in (26,193,195,197)
  8143.     and     not exists (        /* exclude revoke'd privileges */
  8144.             select *
  8145.             from     sysprotects p1
  8146.             where    p1.protecttype = 206
  8147.             and     p1.action = v.number
  8148.             and     p1.id = o.id
  8149.             and     p1.uid = o.uid)
  8150.     order by 4, 5, 6, 9, 1, 2
  8151.     END
  8152. ELSE
  8153.     BEGIN
  8154.     insert into #spcprivsrowset1
  8155.     select
  8156.         GRANTOR        = user_name(p.grantor),
  8157.         GRANTEE        = user_name(u.uid),
  8158.         TABLE_CATALOG    = db_name(),
  8159.         TABLE_SCHEMA    = user_name(o.uid),
  8160.         TABLE_NAME    = o.name,
  8161.         COLUMN_NAME    = c.name,
  8162.         COLUMN_GUID    = convert(binary(16),null),
  8163.         COLUMN_PROPID    = convert(int,null),
  8164.         PRIVILEGE_TYPE    = convert(varchar(30),
  8165.                     case p.action
  8166.                     when 193 then 'SELECT'
  8167.                     when 195 then 'INSERT'
  8168.                     when 197 then 'UPDATE'
  8169.                     else 'REFERENCES'
  8170.                     end),
  8171.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  8172.     from 
  8173.         sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c
  8174.     where
  8175.         o.type in ('U','V')
  8176.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  8177.     and    c.id = o.id
  8178.     and     (@column_name is null or @column_name = c.name)
  8179.     and     c.id = p.id
  8180.     and    (@grantor is null or @grantor = user_name(p.grantor))
  8181.     and     case 
  8182.             when substring(p.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  8183.             when substring(p.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p.columns, v.low, 1))
  8184.             else (~convert(tinyint, coalesce(substring(p.columns, v.low, 1),0)))
  8185.             end & v.high <> 0            /* permission applies to this column */
  8186.     and     v.number <= (select count(*) from syscolumns where id = o.id)    /* ranges from 1 to # of columns in table */
  8187.     and     v.type = 'P'
  8188.     and     v.number = c.colid
  8189.     and    (@grantee is null or @grantee = user_name(u.uid))
  8190.             /* expand groups */
  8191.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  8192.     and     p.protecttype <> 206    /* only grant rows */
  8193.     and     p.action in (26,193,195,197)
  8194.     and     o.uid <> u.uid            /* no rows for owner */
  8195.     and     not exists (            /* exclude revoke'd privileges */
  8196.             select *
  8197.             from sysprotects p1
  8198.             where
  8199.                 p1.protecttype = 206
  8200.             and     p1.action = p.action
  8201.             and     p1.id = p.id
  8202.             and     p1.uid = u.uid
  8203.             and     case 
  8204.                 when substring(p1.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  8205.                 when substring(p1.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  8206.                                 else (~convert(tinyint,coalesce(substring(p.columns, v.low, 1),0)))
  8207.                 end & v.high <> 0)            /* permission applies to this column */
  8208.     union all
  8209.     select    /*    Add rows for table owner */
  8210.         GRANTOR        = user_name(u.uid),
  8211.         GRANTEE        = user_name(o.uid),
  8212.         TABLE_CATALOG    = db_name(),
  8213.         TABLE_SCHEMA    = user_name(o.uid),
  8214.         TABLE_NAME    = o.name,
  8215.         COLUMN_NAME    = c.name,
  8216.         COLUMN_GUID    = convert(binary(16),null),
  8217.         COLUMN_PROPID    = convert(int,null),
  8218.         PRIVILEGE_TYPE    = convert(varchar(30),
  8219.                     case v.number
  8220.                     when 193 then 'SELECT'
  8221.                     when 195 then 'INSERT'
  8222.                     when 197 then 'UPDATE'
  8223.                     else 'REFERENCES'
  8224.                     end),
  8225.         IS_GRANTABLE    = convert(bit,1)    
  8226.     from 
  8227.         sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  8228.     where
  8229.          o.type in ('U','V')
  8230.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  8231.     and    (@grantee is null or @grantee = user_name(o.uid))
  8232.     and    c.id = o.id
  8233.     and     (@column_name is null or @column_name = c.name)
  8234.     and     u.suid = 1        /* grantor is dbo of database */
  8235.     and    (@grantor is null or @grantor = user_name(u.uid))
  8236.     and     v.type = 'P'        /* cross product to get all exposed privileges */
  8237.     and     v.number in (26,193,195,197)
  8238.     and     not exists (        /* exclude revoke'd privileges */
  8239.             select *
  8240.             from     sysprotects p1
  8241.             where    p1.protecttype = 206
  8242.             and     p1.action = v.number
  8243.             and     p1.id = o.id
  8244.             and     p1.uid = o.uid)
  8245.     order by 4, 5, 6, 9, 1, 2
  8246.     END
  8247.  
  8248. exec @ret = sp_cursoropen @handle output, 'select * from #spcprivsrowset1',
  8249.     @scrollopt output, @ccopt output, @rows output
  8250.  
  8251. drop table #spcprivsrowset1
  8252.  
  8253. return isnull(@ret,0)
  8254. go
  8255.  
  8256. create procedure sp_column_privileges_rowset;4
  8257.     (
  8258.        @handle        int output,
  8259.        @scrollopt    int output,
  8260.     @ccopt        int output,
  8261.     @rows        int output,
  8262.     @cmd        varchar(255)
  8263.     )
  8264. as
  8265.     declare @ret int
  8266.     declare @database_name varchar(30)
  8267.  
  8268.     create table #spcprivsrowset2
  8269.         (
  8270.         GRANTOR        varchar(30) not null,
  8271.         GRANTEE        varchar(30) not null,
  8272.         TABLE_CATALOG    varchar(30) not null,
  8273.         TABLE_SCHEMA    varchar(30) not null,
  8274.         TABLE_NAME    varchar(30) not null,
  8275.         COLUMN_NAME    varchar(30) not null,
  8276.         COLUMN_GUID    binary(16) null,
  8277.         COLUMN_PROPID    int null,
  8278.         PRIVILEGE_TYPE    varchar(30) not null,
  8279.         IS_GRANTABLE    bit not null
  8280.         )
  8281.  
  8282.     declare sysdatabases_cursor insensitive cursor for 
  8283.         select name from master.dbo.sysdatabases order by name
  8284.   
  8285.     set nocount on
  8286.     open sysdatabases_cursor
  8287.       fetch next from sysdatabases_cursor into @database_name 
  8288.     while @@fetch_status = 0
  8289.         begin
  8290.         exec('use '+@database_name+' insert into #spcprivsrowset2 '+@cmd)
  8291.         fetch next from sysdatabases_cursor into @database_name 
  8292.         end
  8293.       close sysdatabases_cursor
  8294.     deallocate sysdatabases_cursor
  8295.  
  8296.     if @scrollopt is null
  8297.         begin
  8298.         select * from #spcprivsrowset2
  8299.         end
  8300.     else
  8301.         begin
  8302.         exec @ret = sp_cursoropen @handle output,
  8303.             'select * from #spcprivsrowset2',
  8304.                 @scrollopt output, @ccopt output, @rows output
  8305.         end
  8306.     drop table #spcprivsrowset2
  8307.     return isnull(@ret,0)
  8308. go
  8309.  
  8310.  
  8311. if (charindex('7.00', @@version) = 0)
  8312. begin
  8313.     print ''
  8314.     print ''
  8315.     print 'Warning:'
  8316.     print 'you are installing the stored procedures '
  8317.     print 'on a pre 7.0 SQL Server.'
  8318.     print 'Ignore the following errors.'
  8319. end
  8320. else
  8321.     drop proc sp_column_privileges_rowset
  8322. go
  8323.  
  8324.  
  8325. /*    Procedure for 7.0 server */
  8326. CREATE PROCEDURE sp_column_privileges_rowset
  8327.     (
  8328.     @table_name         varchar(255),
  8329.     @table_schema        varchar(255) = null,
  8330.     @column_name        varchar(255) = null,
  8331.     @grantor        varchar(255) = null,
  8332.     @grantee        varchar(255) = null
  8333.     )
  8334. as
  8335.     select
  8336.         GRANTOR        = user_name(p.grantor),
  8337.         GRANTEE        = user_name(u.uid),
  8338.         TABLE_CATALOG    = db_name(),
  8339.         TABLE_SCHEMA    = user_name(o.uid),
  8340.         TABLE_NAME    = o.name,
  8341.         COLUMN_NAME    = c.name,
  8342.         COLUMN_GUID    = convert(binary(16),null),
  8343.         COLUMN_PROPID    = convert(int,null),
  8344.         PRIVILEGE_TYPE    = convert(varchar(30),
  8345.                     case p.action
  8346.                     when 193 then 'SELECT'
  8347.                     when 195 then 'INSERT'
  8348.                     when 197 then 'UPDATE'
  8349.                     else 'REFERENCES'
  8350.                     end),
  8351.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  8352.     from 
  8353.         sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c, sysmembers m
  8354.     where
  8355.         o.name = @table_name
  8356.     and     o.type in ('U','V')
  8357.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  8358.     and    c.id = o.id
  8359.     and     (@column_name is null or @column_name = c.name)
  8360.     and     c.id = p.id
  8361.     and    (@grantor is null or @grantor = user_name(p.grantor))
  8362.     and     case 
  8363.             when substring(p.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  8364.             when substring(p.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p.columns, v.low, 1))
  8365.             else (~convert(tinyint, coalesce(substring(p.columns, v.low, 1),0)))
  8366.             end & v.high <> 0            /* permission applies to this column */
  8367.     and     v.number <= (select count(*) from syscolumns where id = o.id)    /* ranges from 1 to # of columns in table */
  8368.     and     v.type = 'P'
  8369.     and     v.number = c.colid
  8370.     and    (@grantee is null or @grantee = user_name(u.uid))
  8371.         /* expand groups - AKUNDONE: only 1 level of group unrolling here. Need more?? */
  8372.     and     (u.uid > 0 and u.uid < 16384)
  8373.     and     ((p.uid = u.uid) or  (p.uid = m.groupuid and u.uid = m.memberuid))    
  8374.     and     p.protecttype <> 206    /* only grant rows */
  8375.     and     p.action in (26,193,195,197)
  8376.     and     o.uid <> u.uid            /* no rows for owner */
  8377.     and     not exists (            /* exclude revoke'd privileges */
  8378.             select *
  8379.             from sysprotects p1
  8380.             where
  8381.                 p1.protecttype = 206
  8382.             and     p1.action = p.action
  8383.             and     p1.id = p.id
  8384.             and     p1.uid = u.uid
  8385.             and     case 
  8386.                 when substring(p1.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  8387.                 when substring(p1.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  8388.                                 else (~convert(tinyint,coalesce(substring(p.columns, v.low, 1),0)))
  8389.                 end & v.high <> 0)            /* permission applies to this column */
  8390.     union all
  8391.     select    /*    Add rows for table owner */
  8392.         GRANTOR        = user_name(u.uid),
  8393.         GRANTEE        = user_name(o.uid),
  8394.         TABLE_CATALOG    = db_name(),
  8395.         TABLE_SCHEMA    = user_name(o.uid),
  8396.         TABLE_NAME    = o.name,
  8397.         COLUMN_NAME    = c.name,
  8398.         COLUMN_GUID    = convert(binary(16),null),
  8399.         COLUMN_PROPID    = convert(int,null),
  8400.         PRIVILEGE_TYPE    = convert(varchar(30),
  8401.                     case v.number
  8402.                     when 193 then 'SELECT'
  8403.                     when 195 then 'INSERT'
  8404.                     when 197 then 'UPDATE'
  8405.                     else 'REFERENCES'
  8406.                     end),
  8407.         IS_GRANTABLE    = convert(bit,1)    
  8408.     from 
  8409.         sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  8410.     where
  8411.         o.name = @table_name
  8412.     and     o.type in ('U','V')
  8413.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  8414.     and    (@grantee is null or @grantee = user_name(o.uid))
  8415.     and    c.id = o.id
  8416.     and     (@column_name is null or @column_name = c.name)
  8417.     and     u.suid = 1        /* grantor is dbo of database */
  8418.     and    (@grantor is null or @grantor = user_name(u.uid))
  8419.     and     v.type = 'P'        /* cross product to get all exposed privileges */
  8420.     and     v.number in (26,193,195,197)
  8421.     and     not exists (        /* exclude revoke'd privileges */
  8422.             select *
  8423.             from     sysprotects p1
  8424.             where    p1.protecttype = 206
  8425.             and     p1.action = v.number
  8426.             and     p1.id = o.id
  8427.             and     p1.uid = o.uid)
  8428.     order by 4, 5, 6, 9, 1, 2
  8429. go
  8430. CREATE PROCEDURE sp_column_privileges_rowset;2
  8431.     (
  8432.     @table_schema        varchar(255) = null,
  8433.     @column_name        varchar(255) = null,
  8434.     @grantor        varchar(255) = null,
  8435.     @grantee        varchar(255) = null
  8436.     )
  8437. as
  8438.     select
  8439.         GRANTOR        = user_name(p.grantor),
  8440.         GRANTEE        = user_name(u.uid),
  8441.         TABLE_CATALOG    = db_name(),
  8442.         TABLE_SCHEMA    = user_name(o.uid),
  8443.         TABLE_NAME    = o.name,
  8444.         COLUMN_NAME    = c.name,
  8445.         COLUMN_GUID    = convert(binary(16),null),
  8446.         COLUMN_PROPID    = convert(int,null),
  8447.         PRIVILEGE_TYPE    = convert(varchar(30),
  8448.                     case p.action
  8449.                     when 193 then 'SELECT'
  8450.                     when 195 then 'INSERT'
  8451.                     when 197 then 'UPDATE'
  8452.                     else 'REFERENCES'
  8453.                     end),
  8454.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  8455.     from 
  8456.         sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c, sysmembers m
  8457.     where
  8458.         o.type in ('U','V')
  8459.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  8460.     and    c.id = o.id
  8461.     and     (@column_name is null or @column_name = c.name)
  8462.     and     c.id = p.id
  8463.     and    (@grantor is null or @grantor = user_name(p.grantor))
  8464.     and     case 
  8465.             when substring(p.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  8466.             when substring(p.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p.columns, v.low, 1))
  8467.             else (~convert(tinyint, coalesce(substring(p.columns, v.low, 1),0)))
  8468.             end & v.high <> 0            /* permission applies to this column */
  8469.     and     v.number <= (select count(*) from syscolumns where id = o.id)    /* ranges from 1 to # of columns in table */
  8470.     and     v.type = 'P'
  8471.     and     v.number = c.colid
  8472.     and    (@grantee is null or @grantee = user_name(u.uid))
  8473.         /* expand groups - AKUNDONE: only 1 level of group unrolling here. Need more?? */
  8474.     and     (u.uid > 0 and u.uid < 16384)
  8475.     and     ((p.uid = u.uid) or  (p.uid = m.groupuid and u.uid = m.memberuid))    
  8476.     and     p.protecttype <> 206    /* only grant rows */
  8477.     and     p.action in (26,193,195,197)
  8478.     and     o.uid <> u.uid            /* no rows for owner */
  8479.     and     not exists (            /* exclude revoke'd privileges */
  8480.             select *
  8481.             from sysprotects p1
  8482.             where
  8483.                 p1.protecttype = 206
  8484.             and     p1.action = p.action
  8485.             and     p1.id = p.id
  8486.             and     p1.uid = u.uid
  8487.             and     case 
  8488.                 when substring(p1.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  8489.                 when substring(p1.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  8490.                                 else (~convert(tinyint,coalesce(substring(p.columns, v.low, 1),0)))
  8491.                 end & v.high <> 0)            /* permission applies to this column */
  8492.     union all
  8493.     select    /*    Add rows for table owner */
  8494.         GRANTOR        = user_name(u.uid),
  8495.         GRANTEE        = user_name(o.uid),
  8496.         TABLE_CATALOG    = db_name(),
  8497.         TABLE_SCHEMA    = user_name(o.uid),
  8498.         TABLE_NAME    = o.name,
  8499.         COLUMN_NAME    = c.name,
  8500.         COLUMN_GUID    = convert(binary(16),null),
  8501.         COLUMN_PROPID    = convert(int,null),
  8502.         PRIVILEGE_TYPE    = convert(varchar(30),
  8503.                     case v.number
  8504.                     when 193 then 'SELECT'
  8505.                     when 195 then 'INSERT'
  8506.                     when 197 then 'UPDATE'
  8507.                     else 'REFERENCES'
  8508.                     end),
  8509.         IS_GRANTABLE    = convert(bit,1)    
  8510.     from 
  8511.         sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  8512.     where
  8513.         o.type in ('U','V')
  8514.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  8515.     and    (@grantee is null or @grantee = user_name(o.uid))
  8516.     and    c.id = o.id
  8517.     and     (@column_name is null or @column_name = c.name)
  8518.     and     u.suid = 1        /* grantor is dbo of database */
  8519.     and    (@grantor is null or @grantor = user_name(u.uid))
  8520.     and     v.type = 'P'        /* cross product to get all exposed privileges */
  8521.     and     v.number in (26,193,195,197)
  8522.     and     not exists (        /* exclude revoke'd privileges */
  8523.             select *
  8524.             from     sysprotects p1
  8525.             where    p1.protecttype = 206
  8526.             and     p1.action = v.number
  8527.             and     p1.id = o.id
  8528.             and     p1.uid = o.uid)
  8529.     order by 4, 5, 6, 9, 1, 2
  8530. go
  8531. CREATE PROCEDURE sp_column_privileges_rowset;3
  8532.     (
  8533.     @table_name         varchar(255) = null,
  8534.     @table_schema        varchar(255) = null,
  8535.     @column_name        varchar(255) = null,
  8536.     @grantor        varchar(255) = null,
  8537.     @grantee        varchar(255) = null
  8538.     )
  8539. as
  8540. IF @table_name is not null
  8541.     BEGIN
  8542.     select
  8543.         GRANTOR        = user_name(p.grantor),
  8544.         GRANTEE        = user_name(u.uid),
  8545.         TABLE_CATALOG    = db_name(),
  8546.         TABLE_SCHEMA    = user_name(o.uid),
  8547.         TABLE_NAME    = o.name,
  8548.         COLUMN_NAME    = c.name,
  8549.         COLUMN_GUID    = convert(binary(16),null),
  8550.         COLUMN_PROPID    = convert(int,null),
  8551.         PRIVILEGE_TYPE    = convert(varchar(30),
  8552.                     case p.action
  8553.                     when 193 then 'SELECT'
  8554.                     when 195 then 'INSERT'
  8555.                     when 197 then 'UPDATE'
  8556.                     else 'REFERENCES'
  8557.                     end),
  8558.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  8559.     from 
  8560.         sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c, sysmembers m
  8561.     where
  8562.         o.name = @table_name
  8563.     and     o.type in ('U','V')
  8564.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  8565.     and    c.id = o.id
  8566.     and     (@column_name is null or @column_name = c.name)
  8567.     and     c.id = p.id
  8568.     and    (@grantor is null or @grantor = user_name(p.grantor))
  8569.     and     case 
  8570.             when substring(p.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  8571.             when substring(p.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p.columns, v.low, 1))
  8572.             else (~convert(tinyint, coalesce(substring(p.columns, v.low, 1),0)))
  8573.             end & v.high <> 0            /* permission applies to this column */
  8574.     and     v.number <= (select count(*) from syscolumns where id = o.id)    /* ranges from 1 to # of columns in table */
  8575.     and     v.type = 'P'
  8576.     and     v.number = c.colid
  8577.     and    (@grantee is null or @grantee = user_name(u.uid))
  8578.         /* expand groups - AKUNDONE: only 1 level of group unrolling here. Need more?? */
  8579.     and     (u.uid > 0 and u.uid < 16384)
  8580.     and     ((p.uid = u.uid) or  (p.uid = m.groupuid and u.uid = m.memberuid))    
  8581.     and     p.protecttype <> 206    /* only grant rows */
  8582.     and     p.action in (26,193,195,197)
  8583.     and     o.uid <> u.uid            /* no rows for owner */
  8584.     and     not exists (            /* exclude revoke'd privileges */
  8585.             select *
  8586.             from sysprotects p1
  8587.             where
  8588.                 p1.protecttype = 206
  8589.             and     p1.action = p.action
  8590.             and     p1.id = p.id
  8591.             and     p1.uid = u.uid
  8592.             and     case 
  8593.                 when substring(p1.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  8594.                 when substring(p1.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  8595.                                 else (~convert(tinyint,coalesce(substring(p.columns, v.low, 1),0)))
  8596.                 end & v.high <> 0)            /* permission applies to this column */
  8597.     union all
  8598.     select    /*    Add rows for table owner */
  8599.         GRANTOR        = user_name(u.uid),
  8600.         GRANTEE        = user_name(o.uid),
  8601.         TABLE_CATALOG    = db_name(),
  8602.         TABLE_SCHEMA    = user_name(o.uid),
  8603.         TABLE_NAME    = o.name,
  8604.         COLUMN_NAME    = c.name,
  8605.         COLUMN_GUID    = convert(binary(16),null),
  8606.         COLUMN_PROPID    = convert(int,null),
  8607.         PRIVILEGE_TYPE    = convert(varchar(30),
  8608.                     case v.number
  8609.                     when 193 then 'SELECT'
  8610.                     when 195 then 'INSERT'
  8611.                     when 197 then 'UPDATE'
  8612.                     else 'REFERENCES'
  8613.                     end),
  8614.         IS_GRANTABLE    = convert(bit,1)    
  8615.     from 
  8616.         sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  8617.     where
  8618.         o.name = @table_name
  8619.     and     o.type in ('U','V')
  8620.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  8621.     and    (@grantee is null or @grantee = user_name(o.uid))
  8622.     and    c.id = o.id
  8623.     and     (@column_name is null or @column_name = c.name)
  8624.     and     u.suid = 1        /* grantor is dbo of database */
  8625.     and    (@grantor is null or @grantor = user_name(u.uid))
  8626.     and     v.type = 'P'        /* cross product to get all exposed privileges */
  8627.     and     v.number in (26,193,195,197)
  8628.     and     not exists (        /* exclude revoke'd privileges */
  8629.             select *
  8630.             from     sysprotects p1
  8631.             where    p1.protecttype = 206
  8632.             and     p1.action = v.number
  8633.             and     p1.id = o.id
  8634.             and     p1.uid = o.uid)
  8635.     END
  8636. ELSE
  8637.     BEGIN
  8638.     select
  8639.         GRANTOR        = user_name(p.grantor),
  8640.         GRANTEE        = user_name(u.uid),
  8641.         TABLE_CATALOG    = db_name(),
  8642.         TABLE_SCHEMA    = user_name(o.uid),
  8643.         TABLE_NAME    = o.name,
  8644.         COLUMN_NAME    = c.name,
  8645.         COLUMN_GUID    = convert(binary(16),null),
  8646.         COLUMN_PROPID    = convert(int,null),
  8647.         PRIVILEGE_TYPE    = convert(varchar(30),
  8648.                     case p.action
  8649.                     when 193 then 'SELECT'
  8650.                     when 195 then 'INSERT'
  8651.                     when 197 then 'UPDATE'
  8652.                     else 'REFERENCES'
  8653.                     end),
  8654.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  8655.     from 
  8656.         sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c, sysmembers m
  8657.     where
  8658.         o.type in ('U','V')
  8659.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  8660.     and    c.id = o.id
  8661.     and     (@column_name is null or @column_name = c.name)
  8662.     and     c.id = p.id
  8663.     and    (@grantor is null or @grantor = user_name(p.grantor))
  8664.     and     case 
  8665.             when substring(p.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  8666.             when substring(p.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p.columns, v.low, 1))
  8667.             else (~convert(tinyint, coalesce(substring(p.columns, v.low, 1),0)))
  8668.             end & v.high <> 0            /* permission applies to this column */
  8669.     and     v.number <= (select count(*) from syscolumns where id = o.id)    /* ranges from 1 to # of columns in table */
  8670.     and     v.type = 'P'
  8671.     and     v.number = c.colid
  8672.     and    (@grantee is null or @grantee = user_name(u.uid))
  8673.         /* expand groups - AKUNDONE: only 1 level of group unrolling here. Need more?? */
  8674.     and     (u.uid > 0 and u.uid < 16384)
  8675.     and     ((p.uid = u.uid) or  (p.uid = m.groupuid and u.uid = m.memberuid))    
  8676.     and     p.protecttype <> 206    /* only grant rows */
  8677.     and     p.action in (26,193,195,197)
  8678.     and     o.uid <> u.uid            /* no rows for owner */
  8679.     and     not exists (            /* exclude revoke'd privileges */
  8680.             select *
  8681.             from sysprotects p1
  8682.             where
  8683.                 p1.protecttype = 206
  8684.             and     p1.action = p.action
  8685.             and     p1.id = p.id
  8686.             and     p1.uid = u.uid
  8687.             and     case 
  8688.                 when substring(p1.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  8689.                 when substring(p1.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  8690.                                 else (~convert(tinyint,coalesce(substring(p.columns, v.low, 1),0)))
  8691.                 end & v.high <> 0)            /* permission applies to this column */
  8692.     union all
  8693.     select    /*    Add rows for table owner */
  8694.         GRANTOR        = user_name(u.uid),
  8695.         GRANTEE        = user_name(o.uid),
  8696.         TABLE_CATALOG    = db_name(),
  8697.         TABLE_SCHEMA    = user_name(o.uid),
  8698.         TABLE_NAME    = o.name,
  8699.         COLUMN_NAME    = c.name,
  8700.         COLUMN_GUID    = convert(binary(16),null),
  8701.         COLUMN_PROPID    = convert(int,null),
  8702.         PRIVILEGE_TYPE    = convert(varchar(30),
  8703.                     case v.number
  8704.                     when 193 then 'SELECT'
  8705.                     when 195 then 'INSERT'
  8706.                     when 197 then 'UPDATE'
  8707.                     else 'REFERENCES'
  8708.                     end),
  8709.         IS_GRANTABLE    = convert(bit,1)    
  8710.     from 
  8711.         sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  8712.     where
  8713.          o.type in ('U','V')
  8714.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  8715.     and    (@grantee is null or @grantee = user_name(o.uid))
  8716.     and    c.id = o.id
  8717.     and     (@column_name is null or @column_name = c.name)
  8718.     and     u.suid = 1        /* grantor is dbo of database */
  8719.     and    (@grantor is null or @grantor = user_name(u.uid))
  8720.     and     v.type = 'P'        /* cross product to get all exposed privileges */
  8721.     and     v.number in (26,193,195,197)
  8722.     and     not exists (        /* exclude revoke'd privileges */
  8723.             select *
  8724.             from     sysprotects p1
  8725.             where    p1.protecttype = 206
  8726.             and     p1.action = v.number
  8727.             and     p1.id = o.id
  8728.             and     p1.uid = o.uid)
  8729.     END
  8730. go
  8731.  
  8732.  
  8733. create procedure sp_column_privileges_rowset;4
  8734.     (
  8735.        @handle        int output,
  8736.        @scrollopt    int output,
  8737.     @ccopt        int output,
  8738.     @rows        int output,
  8739.     @cmd        varchar(255)
  8740.     )
  8741. as
  8742.     declare @ret int
  8743.     declare @database_name varchar(30)
  8744.  
  8745.     create table #spcprivsrowset
  8746.         (
  8747.         GRANTOR        varchar(30) not null,
  8748.         GRANTEE        varchar(30) not null,
  8749.         TABLE_CATALOG    varchar(30) not null,
  8750.         TABLE_SCHEMA    varchar(30) not null,
  8751.         TABLE_NAME    varchar(30) not null,
  8752.         COLUMN_NAME    varchar(30) not null,
  8753.         COLUMN_GUID    binary(16) null,
  8754.         COLUMN_PROPID    int null,
  8755.         PRIVILEGE_TYPE    varchar(30) not null,
  8756.         IS_GRANTABLE    bit not null
  8757.         )
  8758.  
  8759.     declare sysdatabases_cursor insensitive cursor for 
  8760.         select name from master.dbo.sysdatabases
  8761.   
  8762.     set nocount on
  8763.     open sysdatabases_cursor
  8764.       fetch next from sysdatabases_cursor into @database_name 
  8765.     while @@fetch_status = 0
  8766.         begin
  8767.         exec('use '+@database_name+' insert into #spcprivsrowset '+@cmd)
  8768.         fetch next from sysdatabases_cursor into @database_name 
  8769.         end
  8770.       close sysdatabases_cursor
  8771.     deallocate sysdatabases_cursor
  8772.  
  8773.     if @scrollopt is null
  8774.         begin
  8775.         select * from #spcprivsrowset order by 3,4,5,6,9,1,2
  8776.         end
  8777.     else
  8778.         begin
  8779.         exec @ret = sp_cursoropen @handle output,
  8780.             'select * from #spcprivsrowset order by 3,4,5,6,9,1,2',
  8781.                 @scrollopt output, @ccopt output, @rows output
  8782.         end
  8783.     drop table #spcprivsrowset
  8784.     return isnull(@ret,0)
  8785. go
  8786.  
  8787.  
  8788. grant execute on sp_column_privileges_rowset to public
  8789. go
  8790.  
  8791. dump tran master with no_log
  8792. go
  8793.  
  8794.  
  8795. print 'creating sp_columns_rowset'
  8796. go
  8797.  
  8798. /*    Procedure for 6.0 and 6.50 server */
  8799. create procedure sp_columns_rowset
  8800.     (
  8801.        @table_name    varchar(255),
  8802.     @table_schema     varchar(255) = null,
  8803.     @column_name    varchar(255) = null
  8804.     )
  8805. as     
  8806.     select
  8807.         TABLE_CATALOG        = db_name(),
  8808.         TABLE_SCHEMA        = user_name(o.uid),
  8809.         TABLE_NAME        = o.name,
  8810.         COLUMN_NAME        = c.name,
  8811.         COLUMN_GUID        = convert(binary(16),null),
  8812.         COLUMN_PROPID        = convert(int,null),
  8813.         ORDINAL_POSITION    = convert(int,c.colid),
  8814.         COLUMN_HASDEFAULT    = convert(bit, case when m.text is null then 1 else 0 end),
  8815.         COLUMN_DEFAULT        = substring(m.text,2,datalength(m.text)-2),
  8816.         COLUMN_FLAGS        = convert(int,
  8817.                         case when d.is_long = 1 
  8818.                         then 0x82 /*DBCOLUMNFLAGS_ISLONG|DBCOLUMNFLAGS_MAYDEFER*/
  8819.                         else 0
  8820.                         end
  8821.                     |    case when d.fixlen is not null
  8822.                         then 0x10 /*DBCOLUMNFLAGS_ISFIXEDLENGTH*/
  8823.                         else 0
  8824.                         end
  8825.                     |    case when c.status&8 = 8
  8826.                         then 0x20 /*DBCOLUMNFLAGS_ISNULLABLE*/
  8827.                         else 0
  8828.                         end
  8829.                     |    case 
  8830.                         when d.type_name = 'timestamp'
  8831.                             then 0x200 /*DBCOLUMNFLAGS_ISROWVER*/
  8832.                         when d.auto_unique_value = 0
  8833.                             then 0x8 /*DBCOLUMNFLAGS_WRITEUNKNOWN*/ 
  8834.                         else 0 
  8835.                         end),
  8836.         IS_NULLABLE        = convert(bit,c.status&8),
  8837.         DATA_TYPE        = d.oledb_data_type,
  8838.         TYPE_GUID        = convert(binary(16),null),
  8839.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  8840.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  8841.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  8842.                         then coalesce(d.column_size,c.length)
  8843.                         else null 
  8844.                         end),
  8845.         CHARACTER_OCTET_LENGTH    = convert(int,
  8846.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  8847.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  8848.                         then coalesce(d.column_size,c.length)
  8849.                         else null 
  8850.                         end),
  8851.         NUMERIC_PRECISION    = convert(smallint,
  8852.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec else null end),
  8853.         NUMERIC_SCALE        = convert(smallint, 
  8854.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  8855.         DATETIME_PRECISION    = convert(int,
  8856.                         case when d.oledb_data_type = 135 /*DBTYPE_DBTIMESTAMP*/ then 3 else null end),
  8857.         CHARACTER_SET_CATALOG    = convert(varchar(30),
  8858.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  8859.                         then 'master' 
  8860.                         else null 
  8861.                         end),
  8862.         CHARACTER_SET_SCHEMA    = convert(varchar(30),
  8863.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  8864.                         then 'dbo' 
  8865.                         else null 
  8866.                         end),
  8867.         CHARACTER_SET_NAME    = convert(varchar(30),
  8868.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  8869.                         then a_cha.name 
  8870.                         else null 
  8871.                         end),
  8872.         COLLATION_CATALOG    = convert(varchar(30),
  8873.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  8874.                         then 'master' 
  8875.                         else null 
  8876.                         end),
  8877.         COLLATION_SCHEMA    = convert(varchar(30),
  8878.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  8879.                         then 'dbo' 
  8880.                         else null 
  8881.                         end),
  8882.         COLLATION_NAME        = convert(varchar(30),
  8883.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  8884.                         then b_cha.name 
  8885.                         else null 
  8886.                         end),
  8887.         DOMAIN_CATALOG        = case when t.usertype < 256 then null else db_name() end,
  8888.         DOMAIN_SCHEMA        = case when t.usertype < 256 then null else user_name(o.uid) end,
  8889.         DOMAIN_NAME        = case when t.usertype < 256 then null else t.name end,
  8890.         DESCRIPTION        = convert(varchar(1),null)    
  8891.     
  8892.     from
  8893.         syscolumns c,
  8894.         syscomments m,
  8895.         sysobjects o,
  8896.         master.dbo.spt_provider_types d,
  8897.         systypes t,
  8898.         sysusers u,
  8899.         master.dbo.sysconfigures    cfg,
  8900.         master.dbo.syscharsets        a_cha, /* charset/1001, not sortorder. */
  8901.         master.dbo.syscharsets        b_cha  /* sortorder/2001, not charset. */
  8902.     where
  8903.         o.name = @table_name
  8904.     and     o.type in ('U','V')
  8905.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  8906.     and     (
  8907.         suser_id() = 1     /* User is the System Administrator */
  8908.         or o.uid = user_id()     /* User created the object */
  8909.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  8910.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  8911.             from sysprotects p
  8912.             where p.id = o.id
  8913.             /* get rows for public,current user,user's group */
  8914.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  8915.             /* check for SELECT,EXECUTE privilege */
  8916.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  8917.             ) = 1    /* final magic...compare Grants      */
  8918.            )
  8919.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  8920.     and    (@column_name is null or @column_name = c.name)
  8921.     and     o.id = c.id
  8922.     and     t.type = d.ss_dtype
  8923.     and     c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  8924.     and     coalesce(d.auto_unique_value,0) = (c.status&128)/128
  8925.     and     c.usertype = t.usertype
  8926.     and    c.cdefault *= m.id
  8927.     and    m.colid = 1
  8928.     and    cfg.comment = 'default sortorder ID' 
  8929.     and    a_cha.type = 1001 /* type is charset */
  8930.     and    b_cha.type = 2001 /* type is sortorder */
  8931.     and    a_cha.id = b_cha.csid
  8932.     and     b_cha.id = cfg.value
  8933.     order by 2, 3, 7
  8934. go
  8935. create procedure sp_columns_rowset;2
  8936.     (
  8937.     @table_schema     varchar(255) = null,
  8938.     @column_name    varchar(255) = null
  8939.     )
  8940. as     
  8941.     select
  8942.         TABLE_CATALOG        = db_name(),
  8943.         TABLE_SCHEMA        = user_name(o.uid),
  8944.         TABLE_NAME        = o.name,
  8945.         COLUMN_NAME        = c.name,
  8946.         COLUMN_GUID        = convert(binary(16),null),
  8947.         COLUMN_PROPID        = convert(int,null),
  8948.         ORDINAL_POSITION    = convert(int,c.colid),
  8949.         COLUMN_HASDEFAULT    = convert(bit, case when m.text is null then 1 else 0 end),
  8950.         COLUMN_DEFAULT        = substring(m.text,2,datalength(m.text)-2),
  8951.         COLUMN_FLAGS        = convert(int,
  8952.                         case when d.is_long = 1 
  8953.                         then 0x82 /*DBCOLUMNFLAGS_ISLONG|DBCOLUMNFLAGS_MAYDEFER*/
  8954.                         else 0
  8955.                         end
  8956.                     |    case when d.fixlen is not null
  8957.                         then 0x10 /*DBCOLUMNFLAGS_ISFIXEDLENGTH*/
  8958.                         else 0
  8959.                         end
  8960.                     |    case when c.status&8 = 8
  8961.                         then 0x20 /*DBCOLUMNFLAGS_ISNULLABLE*/
  8962.                         else 0
  8963.                         end
  8964.                     |    case 
  8965.                         when d.type_name = 'timestamp'
  8966.                             then 0x200 /*DBCOLUMNFLAGS_ISROWVER*/
  8967.                         when d.auto_unique_value = 0
  8968.                             then 0x8 /*DBCOLUMNFLAGS_WRITEUNKNOWN*/ 
  8969.                         else 0 
  8970.                         end),
  8971.         IS_NULLABLE        = convert(bit,c.status&8),
  8972.         DATA_TYPE        = d.oledb_data_type,
  8973.         TYPE_GUID        = convert(binary(16),null),
  8974.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  8975.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  8976.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  8977.                         then coalesce(d.column_size,c.length)
  8978.                         else null 
  8979.                         end),
  8980.         CHARACTER_OCTET_LENGTH    = convert(int,
  8981.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  8982.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  8983.                         then coalesce(d.column_size,c.length)
  8984.                         else null 
  8985.                         end),
  8986.         NUMERIC_PRECISION    = convert(smallint,
  8987.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec else null end),
  8988.         NUMERIC_SCALE        = convert(smallint, 
  8989.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  8990.         DATETIME_PRECISION    = convert(int,
  8991.                         case when d.oledb_data_type = 135 /*DBTYPE_DBTIMESTAMP*/ then 3 else null end),
  8992.         CHARACTER_SET_CATALOG    = convert(varchar(30),
  8993.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  8994.                         then 'master' 
  8995.                         else null 
  8996.                         end),
  8997.         CHARACTER_SET_SCHEMA    = convert(varchar(30),
  8998.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  8999.                         then 'dbo' 
  9000.                         else null 
  9001.                         end),
  9002.         CHARACTER_SET_NAME    = convert(varchar(30),
  9003.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9004.                         then a_cha.name 
  9005.                         else null 
  9006.                         end),
  9007.         COLLATION_CATALOG    = convert(varchar(30),
  9008.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9009.                         then 'master' 
  9010.                         else null 
  9011.                         end),
  9012.         COLLATION_SCHEMA    = convert(varchar(30),
  9013.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9014.                         then 'dbo' 
  9015.                         else null 
  9016.                         end),
  9017.         COLLATION_NAME        = convert(varchar(30),
  9018.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9019.                         then b_cha.name 
  9020.                         else null 
  9021.                         end),
  9022.         DOMAIN_CATALOG        = case when t.usertype < 256 then null else db_name() end,
  9023.         DOMAIN_SCHEMA        = case when t.usertype < 256 then null else user_name(o.uid) end,
  9024.         DOMAIN_NAME        = case when t.usertype < 256 then null else t.name end,
  9025.         DESCRIPTION        = convert(varchar(1),null)    
  9026.     
  9027.     from
  9028.         syscolumns c,
  9029.         syscomments m,
  9030.         sysobjects o,
  9031.         master.dbo.spt_provider_types d,
  9032.         systypes t,
  9033.         sysusers u,
  9034.         master.dbo.sysconfigures    cfg,
  9035.         master.dbo.syscharsets        a_cha, /* charset/1001, not sortorder. */
  9036.         master.dbo.syscharsets        b_cha /* sortorder/2001, not charset. */
  9037.     where
  9038.          o.type in ('U','V')
  9039.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  9040.     and     (
  9041.         suser_id() = 1     /* User is the System Administrator */
  9042.         or o.uid = user_id()     /* User created the object */
  9043.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  9044.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  9045.             from sysprotects p
  9046.             where p.id = o.id
  9047.             /* get rows for public,current user,user's group */
  9048.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  9049.             /* check for SELECT,EXECUTE privilege */
  9050.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  9051.             ) = 1    /* final magic...compare Grants      */
  9052.            )
  9053.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  9054.     and    (@column_name is null or @column_name = c.name)
  9055.     and     o.id = c.id
  9056.     and     t.type = d.ss_dtype
  9057.     and     c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  9058.     and     coalesce(d.auto_unique_value,0) = (c.status&128)/128
  9059.     and     c.usertype = t.usertype
  9060.     and    c.cdefault *= m.id
  9061.     and     m.colid = 1
  9062.     and    cfg.comment = 'default sortorder ID' 
  9063.     and    a_cha.type = 1001 /* type is charset */
  9064.     and    b_cha.type = 2001 /* type is sortorder */
  9065.     and    a_cha.id = b_cha.csid
  9066.     and     b_cha.id = cfg.value
  9067.     order by 2, 3, 7
  9068. go
  9069. create procedure sp_columns_rowset;3
  9070.     (
  9071.        @table_name    varchar(255) = null,
  9072.     @table_schema     varchar(255) = null,
  9073.     @column_name    varchar(255) = null
  9074.     )
  9075. as     
  9076. IF @table_name is not null
  9077.     BEGIN
  9078.     select
  9079.         TABLE_CATALOG        = db_name(),
  9080.         TABLE_SCHEMA        = user_name(o.uid),
  9081.         TABLE_NAME        = o.name,
  9082.         COLUMN_NAME        = c.name,
  9083.         COLUMN_GUID        = convert(binary(16),null),
  9084.         COLUMN_PROPID        = convert(int,null),
  9085.         ORDINAL_POSITION    = convert(int,c.colid),
  9086.         COLUMN_HASDEFAULT    = convert(bit, case when m.text is null then 1 else 0 end),
  9087.         COLUMN_DEFAULT        = substring(m.text,2,datalength(m.text)-2),
  9088.         COLUMN_FLAGS        = convert(int,
  9089.                         case when d.is_long = 1 
  9090.                         then 0x82 /*DBCOLUMNFLAGS_ISLONG|DBCOLUMNFLAGS_MAYDEFER*/
  9091.                         else 0
  9092.                         end
  9093.                     |    case when d.fixlen is not null
  9094.                         then 0x10 /*DBCOLUMNFLAGS_ISFIXEDLENGTH*/
  9095.                         else 0
  9096.                         end
  9097.                     |    case when c.status&8 = 8
  9098.                         then 0x20 /*DBCOLUMNFLAGS_ISNULLABLE*/
  9099.                         else 0
  9100.                         end
  9101.                     |    case 
  9102.                         when d.type_name = 'timestamp'
  9103.                             then 0x200 /*DBCOLUMNFLAGS_ISROWVER*/
  9104.                         when d.auto_unique_value = 0
  9105.                             then 0x8 /*DBCOLUMNFLAGS_WRITEUNKNOWN*/ 
  9106.                         else 0 
  9107.                         end),
  9108.         IS_NULLABLE        = convert(bit,c.status&8),
  9109.         DATA_TYPE        = d.oledb_data_type,
  9110.         TYPE_GUID        = convert(binary(16),null),
  9111.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  9112.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  9113.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  9114.                         then coalesce(d.column_size,c.length)
  9115.                         else null 
  9116.                         end),
  9117.         CHARACTER_OCTET_LENGTH    = convert(int,
  9118.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  9119.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  9120.                         then coalesce(d.column_size,c.length)
  9121.                         else null 
  9122.                         end),
  9123.         NUMERIC_PRECISION    = convert(smallint,
  9124.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec else null end),
  9125.         NUMERIC_SCALE        = convert(smallint, 
  9126.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  9127.         DATETIME_PRECISION    = convert(int,
  9128.                         case when d.oledb_data_type = 135 /*DBTYPE_DBTIMESTAMP*/ then 3 else null end),
  9129.         CHARACTER_SET_CATALOG    = convert(varchar(30),
  9130.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9131.                         then 'master' 
  9132.                         else null 
  9133.                         end),
  9134.         CHARACTER_SET_SCHEMA    = convert(varchar(30),
  9135.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9136.                         then 'dbo' 
  9137.                         else null 
  9138.                         end),
  9139.         CHARACTER_SET_NAME    = convert(varchar(30),
  9140.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9141.                         then a_cha.name 
  9142.                         else null 
  9143.                         end),
  9144.         COLLATION_CATALOG    = convert(varchar(30),
  9145.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9146.                         then 'master' 
  9147.                         else null 
  9148.                         end),
  9149.         COLLATION_SCHEMA    = convert(varchar(30),
  9150.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9151.                         then 'dbo' 
  9152.                         else null 
  9153.                         end),
  9154.         COLLATION_NAME        = convert(varchar(30),
  9155.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9156.                         then b_cha.name 
  9157.                         else null 
  9158.                         end),
  9159.         DOMAIN_CATALOG        = case when t.usertype < 256 then null else db_name() end,
  9160.         DOMAIN_SCHEMA        = case when t.usertype < 256 then null else user_name(o.uid) end,
  9161.         DOMAIN_NAME        = case when t.usertype < 256 then null else t.name end,
  9162.         DESCRIPTION        = convert(varchar(1),null)    
  9163.     
  9164.     from
  9165.         syscolumns c,
  9166.         syscomments m,
  9167.         sysobjects o,
  9168.         master.dbo.spt_provider_types d,
  9169.         systypes t,
  9170.         sysusers u,
  9171.         master.dbo.sysconfigures    cfg,
  9172.         master.dbo.syscharsets        a_cha, /* charset/1001, not sortorder. */
  9173.         master.dbo.syscharsets        b_cha  /* sortorder/2001, not charset. */
  9174.     where
  9175.         o.name = @table_name
  9176.     and     o.type in ('U','V')
  9177.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  9178.     and     (
  9179.         suser_id() = 1     /* User is the System Administrator */
  9180.         or o.uid = user_id()     /* User created the object */
  9181.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  9182.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  9183.             from sysprotects p
  9184.             where p.id = o.id
  9185.             /* get rows for public,current user,user's group */
  9186.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  9187.             /* check for SELECT,EXECUTE privilege */
  9188.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  9189.             ) = 1    /* final magic...compare Grants      */
  9190.            )
  9191.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  9192.     and    (@column_name is null or @column_name = c.name)
  9193.     and     o.id = c.id
  9194.     and     t.type = d.ss_dtype
  9195.     and     c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  9196.     and     coalesce(d.auto_unique_value,0) = (c.status&128)/128
  9197.     and     c.usertype = t.usertype
  9198.     and    c.cdefault *= m.id
  9199.     and     m.colid = 1
  9200.     and    cfg.comment = 'default sortorder ID' 
  9201.     and    a_cha.type = 1001 /* type is charset */
  9202.     and    b_cha.type = 2001 /* type is sortorder */
  9203.     and    a_cha.id = b_cha.csid
  9204.     and     b_cha.id = cfg.value
  9205.     END
  9206. ELSE 
  9207.     BEGIN
  9208.     select
  9209.         TABLE_CATALOG        = db_name(),
  9210.         TABLE_SCHEMA        = user_name(o.uid),
  9211.         TABLE_NAME        = o.name,
  9212.         COLUMN_NAME        = c.name,
  9213.         COLUMN_GUID        = convert(binary(16),null),
  9214.         COLUMN_PROPID        = convert(int,null),
  9215.         ORDINAL_POSITION    = convert(int,c.colid),
  9216.         COLUMN_HASDEFAULT    = convert(bit, case when m.text is null then 1 else 0 end),
  9217.         COLUMN_DEFAULT        = substring(m.text,2,datalength(m.text)-2),
  9218.         COLUMN_FLAGS        = convert(int,
  9219.                         case when d.is_long = 1 
  9220.                         then 0x82 /*DBCOLUMNFLAGS_ISLONG|DBCOLUMNFLAGS_MAYDEFER*/
  9221.                         else 0
  9222.                         end
  9223.                     |    case when d.fixlen is not null
  9224.                         then 0x10 /*DBCOLUMNFLAGS_ISFIXEDLENGTH*/
  9225.                         else 0
  9226.                         end
  9227.                     |    case when c.status&8 = 8
  9228.                         then 0x20 /*DBCOLUMNFLAGS_ISNULLABLE*/
  9229.                         else 0
  9230.                         end
  9231.                     |    case 
  9232.                         when d.type_name = 'timestamp'
  9233.                             then 0x200 /*DBCOLUMNFLAGS_ISROWVER*/
  9234.                         when d.auto_unique_value = 0
  9235.                             then 0x8 /*DBCOLUMNFLAGS_WRITEUNKNOWN*/ 
  9236.                         else 0 
  9237.                         end),
  9238.         IS_NULLABLE        = convert(bit,c.status&8),
  9239.         DATA_TYPE        = d.oledb_data_type,
  9240.         TYPE_GUID        = convert(binary(16),null),
  9241.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  9242.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  9243.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  9244.                         then coalesce(d.column_size,c.length)
  9245.                         else null 
  9246.                         end),
  9247.         CHARACTER_OCTET_LENGTH    = convert(int,
  9248.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  9249.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  9250.                         then coalesce(d.column_size,c.length)
  9251.                         else null 
  9252.                         end),
  9253.         NUMERIC_PRECISION    = convert(smallint,
  9254.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec else null end),
  9255.         NUMERIC_SCALE        = convert(smallint, 
  9256.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  9257.         DATETIME_PRECISION    = convert(int,
  9258.                         case when d.oledb_data_type = 135 /*DBTYPE_DBTIMESTAMP*/ then 3 else null end),
  9259.         CHARACTER_SET_CATALOG    = convert(varchar(30),
  9260.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9261.                         then 'master' 
  9262.                         else null 
  9263.                         end),
  9264.         CHARACTER_SET_SCHEMA    = convert(varchar(30),
  9265.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9266.                         then 'dbo' 
  9267.                         else null 
  9268.                         end),
  9269.         CHARACTER_SET_NAME    = convert(varchar(30),
  9270.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9271.                         then a_cha.name 
  9272.                         else null 
  9273.                         end),
  9274.         COLLATION_CATALOG    = convert(varchar(30),
  9275.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9276.                         then 'master' 
  9277.                         else null 
  9278.                         end),
  9279.         COLLATION_SCHEMA    = convert(varchar(30),
  9280.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9281.                         then 'dbo' 
  9282.                         else null 
  9283.                         end),
  9284.         COLLATION_NAME        = convert(varchar(30),
  9285.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9286.                         then b_cha.name 
  9287.                         else null 
  9288.                         end),
  9289.         DOMAIN_CATALOG        = case when t.usertype < 256 then null else db_name() end,
  9290.         DOMAIN_SCHEMA        = case when t.usertype < 256 then null else user_name(o.uid) end,
  9291.         DOMAIN_NAME        = case when t.usertype < 256 then null else t.name end,
  9292.         DESCRIPTION        = convert(varchar(1),null)    
  9293.     
  9294.     from
  9295.         syscolumns c,
  9296.         syscomments m,
  9297.         sysobjects o,
  9298.         master.dbo.spt_provider_types d,
  9299.         systypes t,
  9300.         sysusers u,
  9301.         master.dbo.sysconfigures    cfg,
  9302.         master.dbo.syscharsets        a_cha, /* charset/1001, not sortorder. */
  9303.         master.dbo.syscharsets        b_cha  /* sortorder/2001, not charset. */
  9304.     where
  9305.          o.type in ('U','V')
  9306.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  9307.     and     (
  9308.         suser_id() = 1     /* User is the System Administrator */
  9309.         or o.uid = user_id()     /* User created the object */
  9310.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  9311.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  9312.             from sysprotects p
  9313.             where p.id = o.id
  9314.             /* get rows for public,current user,user's group */
  9315.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  9316.             /* check for SELECT,EXECUTE privilege */
  9317.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  9318.             ) = 1    /* final magic...compare Grants      */
  9319.            )
  9320.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  9321.     and    (@column_name is null or @column_name = c.name)
  9322.     and     o.id = c.id
  9323.     and     t.type = d.ss_dtype
  9324.     and     c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  9325.     and     coalesce(d.auto_unique_value,0) = (c.status&128)/128
  9326.     and     c.usertype = t.usertype
  9327.     and    c.cdefault *= m.id
  9328.     and     m.colid = 1
  9329.     and    cfg.comment = 'default sortorder ID' 
  9330.     and    a_cha.type = 1001 /* type is charset */
  9331.     and    b_cha.type = 2001 /* type is sortorder */
  9332.     and    a_cha.id = b_cha.csid
  9333.     and     b_cha.id = cfg.value
  9334.     END
  9335. go
  9336.  
  9337. if (charindex('7.00', @@version) = 0)
  9338. begin
  9339.     print ''
  9340.     print ''
  9341.     print 'Warning:'
  9342.     print 'you are installing the stored procedures '
  9343.     print 'on a pre 7.0 SQL Server.'
  9344.     print 'Ignore the following errors.'
  9345. end
  9346. else
  9347.     drop proc sp_columns_rowset
  9348. go
  9349.  
  9350. /*    Procedure for 7.0 server */
  9351. create procedure sp_columns_rowset
  9352.     (
  9353.        @table_name    varchar(255),
  9354.     @table_schema     varchar(255) = NULL,
  9355.     @column_name    varchar(255) = NULL
  9356.     )
  9357. as     
  9358.     select
  9359.         TABLE_CATALOG        = db_name(),
  9360.         TABLE_SCHEMA        = user_name(o.uid),
  9361.         TABLE_NAME        = o.name,
  9362.         COLUMN_NAME        = c.name,
  9363.         COLUMN_GUID        = convert(binary(16),null),
  9364.         COLUMN_PROPID        = convert(int,null),
  9365.         ORDINAL_POSITION    = convert(int,c.colid),
  9366.         COLUMN_HASDEFAULT    = convert(bit, case when m.text is null then 1 else 0 end),
  9367.         COLUMN_DEFAULT        = substring(m.text,2,datalength(m.text)-2),
  9368.         COLUMN_FLAGS        = convert(int,
  9369.                         case when d.is_long = 1 
  9370.                         then 0x82 /*DBCOLUMNFLAGS_ISLONG|DBCOLUMNFLAGS_MAYDEFER*/
  9371.                         else 0
  9372.                         end
  9373.                     |    case when d.fixlen is not null
  9374.                         then 0x10 /*DBCOLUMNFLAGS_ISFIXEDLENGTH*/
  9375.                         else 0
  9376.                         end
  9377.                     |    case when ColumnProperty(c.id,c.name,'AllowsNull') = 1
  9378.                         then 0x20 /*DBCOLUMNFLAGS_ISNULLABLE*/
  9379.                         else 0
  9380.                         end
  9381.                     |    case 
  9382.                         when d.type_name = 'timestamp'
  9383.                             then 0x200 /*DBCOLUMNFLAGS_ISROWVER*/
  9384.                         when d.auto_unique_value = 0  and permissions(o.id,c.name)&2 = 2
  9385.                             then 0x4 /*DBCOLUMNFLAGS_WRITE*/ 
  9386.                         else 0 
  9387.                         end),
  9388.         IS_NULLABLE        = convert(bit,ColumnProperty(c.id,c.name,'AllowsNull')),
  9389.         DATA_TYPE        = d.oledb_data_type,
  9390.         TYPE_GUID        = convert(binary(16),null),
  9391.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  9392.                         case 
  9393.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  9394.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  9395.                         then coalesce(d.column_size,c.length)
  9396.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  9397.                         then coalesce(d.column_size,c.length/2)
  9398.                         else null 
  9399.                         end),
  9400.         CHARACTER_OCTET_LENGTH    = convert(int,
  9401.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  9402.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  9403.                             or d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  9404.                         then coalesce(d.column_size,c.length)
  9405.                         else null 
  9406.                         end),
  9407.         NUMERIC_PRECISION    = convert(smallint,
  9408.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.xprec else null end),
  9409.         NUMERIC_SCALE        = convert(smallint, 
  9410.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  9411.         DATETIME_PRECISION    = convert(int,
  9412.                         case when d.oledb_data_type = 135 /*DBTYPE_DBTIMESTAMP*/ then 3 else null end),
  9413.         CHARACTER_SET_CATALOG    = convert(varchar(30),
  9414.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9415.                         then 'master' 
  9416.                         else null 
  9417.                         end),
  9418.         CHARACTER_SET_SCHEMA    = convert(varchar(30),
  9419.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9420.                         then 'dbo' 
  9421.                         else null 
  9422.                         end),
  9423.         CHARACTER_SET_NAME    = convert(varchar(30),
  9424.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9425.                         then a_cha.name 
  9426.                         else null 
  9427.                         end),
  9428.         COLLATION_CATALOG    = convert(varchar(30),
  9429.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9430.                         then 'master' 
  9431.                         else null 
  9432.                         end),
  9433.         COLLATION_SCHEMA    = convert(varchar(30),
  9434.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9435.                         then 'dbo' 
  9436.                         else null 
  9437.                         end),
  9438.         COLLATION_NAME        = convert(varchar(30),
  9439.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9440.                         then b_cha.name 
  9441.                         else null 
  9442.                         end),
  9443.         DOMAIN_CATALOG        = case when t.usertype < 256 then null else db_name() end,
  9444.         DOMAIN_SCHEMA        = case when t.usertype < 256 then null else user_name(o.uid) end,
  9445.         DOMAIN_NAME        = case when t.usertype < 256 then null else t.name end,
  9446.         DESCRIPTION        = convert(varchar(1),null)    
  9447.     
  9448.     from
  9449.         syscolumns c left join syscomments m on c.cdefault = m.id and m.colid = 1,
  9450.         sysobjects o,
  9451.         master.dbo.spt_provider_types d,
  9452.         systypes t,
  9453.         master.dbo.sysconfigures    cfg,
  9454.         master.dbo.syscharsets        a_cha, --charset/1001, not sortorder.
  9455.         master.dbo.syscharsets        b_cha --sortorder/2001, not charset.
  9456.     where
  9457.         permissions(o.id, c.name) <> 0
  9458.     and    o.name = @table_name
  9459.     and     o.type in ('U','V')
  9460.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  9461.     and    (@column_name is null or @column_name = c.name)
  9462.     and     o.id = c.id
  9463.     and     t.xtype = d.ss_dtype
  9464.     and     c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  9465.     and     coalesce(d.auto_unique_value,0) = ColumnProperty(c.id,c.name,'IsIdentity')
  9466.     and     c.usertype = t.usertype
  9467.     and    cfg.comment = 'default sortorder ID' 
  9468.     and    a_cha.type = 1001 --- type is charset
  9469.     and    b_cha.type = 2001 --- type is sortorder
  9470.     and    a_cha.id = b_cha.csid
  9471.     and     b_cha.id = cfg.value
  9472.     order by 2, 3, 7
  9473. go
  9474. create procedure sp_columns_rowset;2
  9475.     (
  9476.     @table_schema     varchar(255) = NULL,
  9477.     @column_name    varchar(255) = NULL
  9478.     )
  9479. as     
  9480.     select
  9481.         TABLE_CATALOG        = db_name(),
  9482.         TABLE_SCHEMA        = user_name(o.uid),
  9483.         TABLE_NAME        = o.name,
  9484.         COLUMN_NAME        = c.name,
  9485.         COLUMN_GUID        = convert(binary(16),null),
  9486.         COLUMN_PROPID        = convert(int,null),
  9487.         ORDINAL_POSITION    = convert(int,c.colid),
  9488.         COLUMN_HASDEFAULT    = convert(bit, case when m.text is null then 1 else 0 end),
  9489.         COLUMN_DEFAULT        = substring(m.text,2,datalength(m.text)-2),
  9490.         COLUMN_FLAGS        = convert(int,
  9491.                         case when d.is_long = 1 
  9492.                         then 0x82 /*DBCOLUMNFLAGS_ISLONG|DBCOLUMNFLAGS_MAYDEFER*/
  9493.                         else 0
  9494.                         end
  9495.                     |    case when d.fixlen is not null
  9496.                         then 0x10 /*DBCOLUMNFLAGS_ISFIXEDLENGTH*/
  9497.                         else 0
  9498.                         end
  9499.                     |    case when ColumnProperty(c.id,c.name,'AllowsNull') = 1
  9500.                         then 0x20 /*DBCOLUMNFLAGS_ISNULLABLE*/
  9501.                         else 0
  9502.                         end
  9503.                     |    case 
  9504.                         when d.type_name = 'timestamp'
  9505.                             then 0x200 /*DBCOLUMNFLAGS_ISROWVER*/
  9506.                         when d.auto_unique_value = 0  and permissions(o.id,c.name)&2 = 2
  9507.                             then 0x4 /*DBCOLUMNFLAGS_WRITE*/ 
  9508.                         else 0 
  9509.                         end),
  9510.         IS_NULLABLE        = convert(bit,ColumnProperty(c.id,c.name,'AllowsNull')),
  9511.         DATA_TYPE        = d.oledb_data_type,
  9512.         TYPE_GUID        = convert(binary(16),null),
  9513.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  9514.                         case 
  9515.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  9516.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  9517.                         then coalesce(d.column_size,c.length)
  9518.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  9519.                         then coalesce(d.column_size,c.length/2)
  9520.                         else null 
  9521.                         end),
  9522.         CHARACTER_OCTET_LENGTH    = convert(int,
  9523.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  9524.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  9525.                             or d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  9526.                         then coalesce(d.column_size,c.length)
  9527.                         else null 
  9528.                         end),
  9529.         NUMERIC_PRECISION    = convert(smallint,
  9530.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.xprec else null end),
  9531.         NUMERIC_SCALE        = convert(smallint, 
  9532.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  9533.         DATETIME_PRECISION    = convert(int,
  9534.                         case when d.oledb_data_type = 135 /*DBTYPE_DBTIMESTAMP*/ then 3 else null end),
  9535.         CHARACTER_SET_CATALOG    = convert(varchar(30),
  9536.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9537.                         then 'master' 
  9538.                         else null 
  9539.                         end),
  9540.         CHARACTER_SET_SCHEMA    = convert(varchar(30),
  9541.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9542.                         then 'dbo' 
  9543.                         else null 
  9544.                         end),
  9545.         CHARACTER_SET_NAME    = convert(varchar(30),
  9546.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9547.                         then a_cha.name 
  9548.                         else null 
  9549.                         end),
  9550.         COLLATION_CATALOG    = convert(varchar(30),
  9551.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9552.                         then 'master' 
  9553.                         else null 
  9554.                         end),
  9555.         COLLATION_SCHEMA    = convert(varchar(30),
  9556.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9557.                         then 'dbo' 
  9558.                         else null 
  9559.                         end),
  9560.         COLLATION_NAME        = convert(varchar(30),
  9561.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9562.                         then b_cha.name 
  9563.                         else null 
  9564.                         end),
  9565.         DOMAIN_CATALOG        = case when t.usertype < 256 then null else db_name() end,
  9566.         DOMAIN_SCHEMA        = case when t.usertype < 256 then null else user_name(o.uid) end,
  9567.         DOMAIN_NAME        = case when t.usertype < 256 then null else t.name end,
  9568.         DESCRIPTION        = convert(varchar(1),null)    
  9569.     
  9570.     from
  9571.         syscolumns c left join syscomments m on c.cdefault = m.id and m.colid = 1,
  9572.         sysobjects o,
  9573.         master.dbo.spt_provider_types d,
  9574.         systypes t,
  9575.         master.dbo.sysconfigures    cfg,
  9576.         master.dbo.syscharsets        a_cha, --charset/1001, not sortorder.
  9577.         master.dbo.syscharsets        b_cha --sortorder/2001, not charset.
  9578.     where
  9579.         permissions(o.id, c.name) <> 0
  9580.     and     o.type in ('U','V')
  9581.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  9582.     and    (@column_name is null or @column_name = c.name)
  9583.     and     o.id = c.id
  9584.     and     t.xtype = d.ss_dtype
  9585.     and     c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  9586.     and     coalesce(d.auto_unique_value,0) = ColumnProperty(c.id,c.name,'IsIdentity')
  9587.     and     c.usertype = t.usertype
  9588.     and    cfg.comment = 'default sortorder ID' 
  9589.     and    a_cha.type = 1001 --- type is charset
  9590.     and    b_cha.type = 2001 --- type is sortorder
  9591.     and    a_cha.id = b_cha.csid
  9592.     and     b_cha.id = cfg.value
  9593.     order by 2, 3, 7
  9594. go
  9595. create procedure sp_columns_rowset;3
  9596.     (
  9597.        @table_name    varchar(255) = NULL,
  9598.     @table_schema     varchar(255) = NULL,
  9599.     @column_name    varchar(255) = NULL
  9600.     )
  9601. as     
  9602. IF @table_name is not null
  9603.     BEGIN
  9604.     select
  9605.         TABLE_CATALOG        = db_name(),
  9606.         TABLE_SCHEMA        = user_name(o.uid),
  9607.         TABLE_NAME        = o.name,
  9608.         COLUMN_NAME        = c.name,
  9609.         COLUMN_GUID        = convert(binary(16),null),
  9610.         COLUMN_PROPID        = convert(int,null),
  9611.         ORDINAL_POSITION    = convert(int,c.colid),
  9612.         COLUMN_HASDEFAULT    = convert(bit, case when m.text is null then 1 else 0 end),
  9613.         COLUMN_DEFAULT        = substring(m.text,2,datalength(m.text)-2),
  9614.         COLUMN_FLAGS        = convert(int,
  9615.                         case when d.is_long = 1 
  9616.                         then 0x82 /*DBCOLUMNFLAGS_ISLONG|DBCOLUMNFLAGS_MAYDEFER*/
  9617.                         else 0
  9618.                         end
  9619.                     |    case when d.fixlen is not null
  9620.                         then 0x10 /*DBCOLUMNFLAGS_ISFIXEDLENGTH*/
  9621.                         else 0
  9622.                         end
  9623.                     |    case when ColumnProperty(c.id,c.name,'AllowsNull') = 1
  9624.                         then 0x20 /*DBCOLUMNFLAGS_ISNULLABLE*/
  9625.                         else 0
  9626.                         end
  9627.                     |    case 
  9628.                         when d.type_name = 'timestamp'
  9629.                             then 0x200 /*DBCOLUMNFLAGS_ISROWVER*/
  9630.                         when d.auto_unique_value = 0  and permissions(o.id,c.name)&2 = 2
  9631.                             then 0x4 /*DBCOLUMNFLAGS_WRITE*/ 
  9632.                         else 0 
  9633.                         end),
  9634.         IS_NULLABLE        = convert(bit,ColumnProperty(c.id,c.name,'AllowsNull')),
  9635.         DATA_TYPE        = d.oledb_data_type,
  9636.         TYPE_GUID        = convert(binary(16),null),
  9637.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  9638.                         case 
  9639.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  9640.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  9641.                         then coalesce(d.column_size,c.length)
  9642.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  9643.                         then coalesce(d.column_size,c.length/2)
  9644.                         else null 
  9645.                         end),
  9646.         CHARACTER_OCTET_LENGTH    = convert(int,
  9647.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  9648.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  9649.                             or d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  9650.                         then coalesce(d.column_size,c.length)
  9651.                         else null 
  9652.                         end),
  9653.         NUMERIC_PRECISION    = convert(smallint,
  9654.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.xprec else null end),
  9655.         NUMERIC_SCALE        = convert(smallint, 
  9656.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  9657.         DATETIME_PRECISION    = convert(int,
  9658.                         case when d.oledb_data_type = 135 /*DBTYPE_DBTIMESTAMP*/ then 3 else null end),
  9659.         CHARACTER_SET_CATALOG    = convert(varchar(30),
  9660.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9661.                         then 'master' 
  9662.                         else null 
  9663.                         end),
  9664.         CHARACTER_SET_SCHEMA    = convert(varchar(30),
  9665.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9666.                         then 'dbo' 
  9667.                         else null 
  9668.                         end),
  9669.         CHARACTER_SET_NAME    = convert(varchar(30),
  9670.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9671.                         then a_cha.name 
  9672.                         else null 
  9673.                         end),
  9674.         COLLATION_CATALOG    = convert(varchar(30),
  9675.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9676.                         then 'master' 
  9677.                         else null 
  9678.                         end),
  9679.         COLLATION_SCHEMA    = convert(varchar(30),
  9680.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9681.                         then 'dbo' 
  9682.                         else null 
  9683.                         end),
  9684.         COLLATION_NAME        = convert(varchar(30),
  9685.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9686.                         then b_cha.name 
  9687.                         else null 
  9688.                         end),
  9689.         DOMAIN_CATALOG        = case when t.usertype < 256 then null else db_name() end,
  9690.         DOMAIN_SCHEMA        = case when t.usertype < 256 then null else user_name(o.uid) end,
  9691.         DOMAIN_NAME        = case when t.usertype < 256 then null else t.name end,
  9692.         DESCRIPTION        = convert(varchar(1),null)    
  9693.     
  9694.     from
  9695.         syscolumns c left join syscomments m on c.cdefault = m.id and m.colid = 1,
  9696.         sysobjects o,
  9697.         master.dbo.spt_provider_types d,
  9698.         systypes t,
  9699.         master.dbo.sysconfigures    cfg,
  9700.         master.dbo.syscharsets        a_cha, --charset/1001, not sortorder.
  9701.         master.dbo.syscharsets        b_cha --sortorder/2001, not charset.
  9702.     where
  9703.         permissions(o.id, c.name) <> 0
  9704.     and    o.name = @table_name
  9705.     and     o.type in ('U','V')
  9706.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  9707.     and    (@column_name is null or @column_name = c.name)
  9708.     and     o.id = c.id
  9709.     and     t.xtype = d.ss_dtype
  9710.     and     c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  9711.     and     coalesce(d.auto_unique_value,0) = ColumnProperty(c.id,c.name,'IsIdentity')
  9712.     and     c.usertype = t.usertype
  9713.     and    cfg.comment = 'default sortorder ID' 
  9714.     and    a_cha.type = 1001 --- type is charset
  9715.     and    b_cha.type = 2001 --- type is sortorder
  9716.     and    a_cha.id = b_cha.csid
  9717.     and     b_cha.id = cfg.value
  9718.     END    
  9719. ELSE 
  9720.     BEGIN
  9721.     select
  9722.         TABLE_CATALOG        = db_name(),
  9723.         TABLE_SCHEMA        = user_name(o.uid),
  9724.         TABLE_NAME        = o.name,
  9725.         COLUMN_NAME        = c.name,
  9726.         COLUMN_GUID        = convert(binary(16),null),
  9727.         COLUMN_PROPID        = convert(int,null),
  9728.         ORDINAL_POSITION    = convert(int,c.colid),
  9729.         COLUMN_HASDEFAULT    = convert(bit, case when m.text is null then 1 else 0 end),
  9730.         COLUMN_DEFAULT        = substring(m.text,2,datalength(m.text)-2),
  9731.         COLUMN_FLAGS        = convert(int,
  9732.                         case when d.is_long = 1 
  9733.                         then 0x82 /*DBCOLUMNFLAGS_ISLONG|DBCOLUMNFLAGS_MAYDEFER*/
  9734.                         else 0
  9735.                         end
  9736.                     |    case when d.fixlen is not null
  9737.                         then 0x10 /*DBCOLUMNFLAGS_ISFIXEDLENGTH*/
  9738.                         else 0
  9739.                         end
  9740.                     |    case when ColumnProperty(c.id,c.name,'AllowsNull') = 1
  9741.                         then 0x20 /*DBCOLUMNFLAGS_ISNULLABLE*/
  9742.                         else 0
  9743.                         end
  9744.                     |    case 
  9745.                         when d.type_name = 'timestamp'
  9746.                             then 0x200 /*DBCOLUMNFLAGS_ISROWVER*/
  9747.                         when d.auto_unique_value = 0  and permissions(o.id,c.name)&2 = 2
  9748.                             then 0x4 /*DBCOLUMNFLAGS_WRITE*/ 
  9749.                         else 0 
  9750.                         end),
  9751.         IS_NULLABLE        = convert(bit,ColumnProperty(c.id,c.name,'AllowsNull')),
  9752.         DATA_TYPE        = d.oledb_data_type,
  9753.         TYPE_GUID        = convert(binary(16),null),
  9754.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  9755.                         case 
  9756.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  9757.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  9758.                         then coalesce(d.column_size,c.length)
  9759.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  9760.                         then coalesce(d.column_size,c.length/2)
  9761.                         else null 
  9762.                         end),
  9763.         CHARACTER_OCTET_LENGTH    = convert(int,
  9764.                         case 
  9765.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  9766.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  9767.                             or d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  9768.                         then coalesce(d.column_size,c.length)
  9769.                         else null 
  9770.                         end),
  9771.         NUMERIC_PRECISION    = convert(smallint,
  9772.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.xprec else null end),
  9773.         NUMERIC_SCALE        = convert(smallint, 
  9774.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  9775.         DATETIME_PRECISION    = convert(int,
  9776.                         case when d.oledb_data_type = 135 /*DBTYPE_DBTIMESTAMP*/ then 3 else null end),
  9777.         CHARACTER_SET_CATALOG    = convert(varchar(30),
  9778.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9779.                         then 'master' 
  9780.                         else null 
  9781.                         end),
  9782.         CHARACTER_SET_SCHEMA    = convert(varchar(30),
  9783.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9784.                         then 'dbo' 
  9785.                         else null 
  9786.                         end),
  9787.         CHARACTER_SET_NAME    = convert(varchar(30),
  9788.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9789.                         then a_cha.name 
  9790.                         else null 
  9791.                         end),
  9792.         COLLATION_CATALOG    = convert(varchar(30),
  9793.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9794.                         then 'master' 
  9795.                         else null 
  9796.                         end),
  9797.         COLLATION_SCHEMA    = convert(varchar(30),
  9798.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9799.                         then 'dbo' 
  9800.                         else null 
  9801.                         end),
  9802.         COLLATION_NAME        = convert(varchar(30),
  9803.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  9804.                         then b_cha.name 
  9805.                         else null 
  9806.                         end),
  9807.         DOMAIN_CATALOG        = case when t.usertype < 256 then null else db_name() end,
  9808.         DOMAIN_SCHEMA        = case when t.usertype < 256 then null else user_name(o.uid) end,
  9809.         DOMAIN_NAME        = case when t.usertype < 256 then null else t.name end,
  9810.         DESCRIPTION        = convert(varchar(1),null)    
  9811.     
  9812.     from
  9813.         syscolumns c left join syscomments m on c.cdefault = m.id and m.colid = 1,
  9814.         sysobjects o,
  9815.         master.dbo.spt_provider_types d,
  9816.         systypes t,
  9817.         master.dbo.sysconfigures    cfg,
  9818.         master.dbo.syscharsets        a_cha, --charset/1001, not sortorder.
  9819.         master.dbo.syscharsets        b_cha --sortorder/2001, not charset.
  9820.     where
  9821.         permissions(o.id, c.name) <> 0
  9822.     and     o.type in ('U','V')
  9823.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  9824.     and    (@column_name is null or @column_name = c.name)
  9825.     and     o.id = c.id
  9826.     and     t.xtype = d.ss_dtype
  9827.     and     c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  9828.     and     coalesce(d.auto_unique_value,0) = ColumnProperty(c.id,c.name,'IsIdentity')
  9829.     and     c.usertype = t.usertype
  9830.     and    cfg.comment = 'default sortorder ID' 
  9831.     and    a_cha.type = 1001 --- type is charset
  9832.     and    b_cha.type = 2001 --- type is sortorder
  9833.     and    a_cha.id = b_cha.csid
  9834.     and     b_cha.id = cfg.value
  9835. END
  9836. go
  9837. create procedure sp_columns_rowset;4
  9838.     (
  9839.  
  9840.        @handle        int output,
  9841.        @scrollopt    int output,
  9842.     @ccopt        int output,
  9843.     @rows        int output,
  9844.     @cmd        varchar(255)
  9845.     )
  9846. as
  9847.     declare @ret int
  9848.     declare @database_name varchar(30)
  9849.  
  9850.     create table #spcolumnsrowset 
  9851.         (
  9852.         TABLE_CATALOG        varchar(30) not null,
  9853.         TABLE_SCHEMA        varchar(30) not null,
  9854.         TABLE_NAME        varchar(30) not null,
  9855.         COLUMN_NAME        varchar(30) not null,
  9856.         COLUMN_GUID        binary(16) null,
  9857.         COLUMN_PROPID        int null,
  9858.         ORDINAL_POSITION    int not null,
  9859.         COLUMN_HASDEFAULT    bit not null,
  9860.         COLUMN_DEFAULT        varchar(255) null,
  9861.         COLUMN_FLAGS        int not null,
  9862.         IS_NULLABLE        bit not null,
  9863.         DATA_TYPE        smallint not null,
  9864.         TYPE_GUID        binary(16) null,
  9865.         CHARACTER_MAXIMUM_LENGTH int null,
  9866.         CHARACTER_OCTET_LENGTH    int null,
  9867.         NUMERIC_PRECISION    smallint null,
  9868.         NUMERIC_SCALE        smallint null,
  9869.         DATETIME_PRECISION    int null,
  9870.         CHARACTER_SET_CATALOG    varchar(30) null,
  9871.         CHARACTER_SET_SCHEMA    varchar(30) null,
  9872.         CHARACTER_SET_NAME    varchar(30) null,
  9873.         COLLATION_CATALOG    varchar(30) null,
  9874.         COLLATION_SCHEMA    varchar(30) null,
  9875.         COLLATION_NAME        varchar(30) null,
  9876.         DOMAIN_CATALOG        varchar(30) null,
  9877.         DOMAIN_SCHEMA        varchar(30) null,
  9878.         DOMAIN_NAME        varchar(30) null,
  9879.         DESCRIPTION        varchar(30) null    
  9880.         )
  9881.  
  9882.     declare sysdatabases_cursor insensitive cursor for 
  9883.         select name from master.dbo.sysdatabases
  9884.   
  9885.     set nocount on
  9886.     open sysdatabases_cursor
  9887.       fetch next from sysdatabases_cursor into @database_name 
  9888.     while @@fetch_status = 0
  9889.         begin
  9890.         exec('use '+@database_name+' insert into #spcolumnsrowset '+@cmd)
  9891.         fetch next from sysdatabases_cursor into @database_name 
  9892.         end
  9893.       close sysdatabases_cursor
  9894.     deallocate sysdatabases_cursor
  9895.  
  9896.     if @scrollopt is null
  9897.         begin
  9898.         select * from #spcolumnsrowset order by 1,2,3,7
  9899.         end
  9900.     else
  9901.         begin
  9902.         exec @ret = sp_cursoropen @handle output,
  9903.             'select * from #spcolumnsrowset order by 1,2,3,7',
  9904.                 @scrollopt output, @ccopt output, @rows output
  9905.         end
  9906.     drop table #spcolumnsrowset 
  9907.     return coalesce(@ret,0)
  9908. go
  9909.  
  9910. grant execute on sp_columns_rowset to public
  9911. go
  9912.  
  9913. dump tran master with no_log
  9914. go
  9915.  
  9916.  
  9917. print 'creating sp_foreign_keys_rowset'
  9918. go
  9919.  
  9920. /*    Procedure for 6.0 and 6.5 server */
  9921. CREATE PROCEDURE sp_foreign_keys_rowset
  9922.     (
  9923.      @pk_table_name        varchar(255) = null,
  9924.     @pk_table_schema    varchar(255) = null,
  9925.     @pk_table_catalog    varchar(255) = null,
  9926.     @fk_table_name        varchar(255) = null,
  9927.     @fk_table_schema    varchar(255) = null,
  9928.     @fk_table_catalog    varchar(255) = null
  9929.     )
  9930. as
  9931. IF @pk_table_name is not null
  9932.     BEGIN
  9933.     select
  9934.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  9935.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  9936.         PK_TABLE_NAME         = o1.name,
  9937.         PK_COLUMN_NAME         = c1.name,
  9938.         PK_COLUMN_GUID        = convert(binary(16),null),
  9939.         PK_COLUMN_PROPID    = convert(int,null),
  9940.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  9941.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  9942.         FK_TABLE_NAME         = o2.name,
  9943.         FK_COLUMN_NAME         = c2.name,
  9944.         FK_COLUMN_GUID        = convert(binary(16),null),
  9945.         FK_COLUMN_PROPID    = convert(int,null),
  9946.         ORDINAL            = convert(int,1),
  9947.         UPDATE_RULE        = 'NO ACTION',
  9948.         DELETE_RULE         = 'NO ACTION'        
  9949.     from
  9950.         sysobjects o1, sysobjects o2,
  9951.         syscolumns c1, syscolumns c2,
  9952.         sysreferences r
  9953.     where    
  9954.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  9955.     and    o1.name = @pk_table_name
  9956.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  9957.     and    o1.id = r.rkeyid
  9958.     and    o1.id = c1.id
  9959.     and    c1.colid = r.rkey1
  9960.     and     r.fkeyid = o2.id
  9961.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  9962.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  9963.     and    o2.id = c2.id    
  9964.     and    c2.colid = r.fkey1
  9965.     union all
  9966.     select
  9967.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  9968.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  9969.         PK_TABLE_NAME         = o1.name,
  9970.         PK_COLUMN_NAME         = c1.name,
  9971.         PK_COLUMN_GUID        = convert(binary(16),null),
  9972.         PK_COLUMN_PROPID    = convert(int,null),
  9973.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  9974.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  9975.         FK_TABLE_NAME         = o2.name,
  9976.         FK_COLUMN_NAME         = c2.name,
  9977.         FK_COLUMN_GUID        = convert(binary(16),null),
  9978.         FK_COLUMN_PROPID    = convert(int,null),
  9979.         ORDINAL            = convert(int,2),
  9980.         UPDATE_RULE        = 'NO ACTION',
  9981.         DELETE_RULE         = 'NO ACTION'        
  9982.     from
  9983.         sysobjects o1, sysobjects o2,
  9984.         syscolumns c1, syscolumns c2,
  9985.         sysreferences r
  9986.     where    
  9987.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  9988.     and    o1.name = @pk_table_name
  9989.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  9990.     and    o1.id = r.rkeyid
  9991.     and    o1.id = c1.id
  9992.     and    c1.colid = r.rkey2
  9993.     and    r.keycnt >= 2
  9994.     and     r.fkeyid = o2.id
  9995.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  9996.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  9997.     and    o2.id = c2.id    
  9998.     and    c2.colid = r.fkey2
  9999.     union all
  10000.     select
  10001.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10002.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10003.         PK_TABLE_NAME         = o1.name,
  10004.         PK_COLUMN_NAME         = c1.name,
  10005.         PK_COLUMN_GUID        = convert(binary(16),null),
  10006.         PK_COLUMN_PROPID    = convert(int,null),
  10007.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10008.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  10009.         FK_TABLE_NAME         = o2.name,
  10010.         FK_COLUMN_NAME         = c2.name,
  10011.         FK_COLUMN_GUID        = convert(binary(16),null),
  10012.         FK_COLUMN_PROPID    = convert(int,null),
  10013.         ORDINAL            = convert(int,3),
  10014.         UPDATE_RULE        = 'NO ACTION',
  10015.         DELETE_RULE         = 'NO ACTION'        
  10016.     from
  10017.         sysobjects o1, sysobjects o2,
  10018.         syscolumns c1, syscolumns c2,
  10019.         sysreferences r
  10020.     where    
  10021.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  10022.     and    o1.name = @pk_table_name
  10023.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10024.     and    o1.id = r.rkeyid
  10025.     and    o1.id = c1.id
  10026.     and    c1.colid = r.rkey3
  10027.     and    r.keycnt >= 3
  10028.     and     r.fkeyid = o2.id
  10029.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  10030.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10031.     and    o2.id = c2.id    
  10032.     and    c2.colid = r.fkey3
  10033.     union all
  10034.     select
  10035.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10036.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10037.         PK_TABLE_NAME         = o1.name,
  10038.         PK_COLUMN_NAME         = c1.name,
  10039.         PK_COLUMN_GUID        = convert(binary(16),null),
  10040.         PK_COLUMN_PROPID    = convert(int,null),
  10041.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10042.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  10043.         FK_TABLE_NAME         = o2.name,
  10044.         FK_COLUMN_NAME         = c2.name,
  10045.         FK_COLUMN_GUID        = convert(binary(16),null),
  10046.         FK_COLUMN_PROPID    = convert(int,null),
  10047.         ORDINAL            = convert(int,4),
  10048.         UPDATE_RULE        = 'NO ACTION',
  10049.         DELETE_RULE         = 'NO ACTION'        
  10050.     from
  10051.         sysobjects o1, sysobjects o2,
  10052.         syscolumns c1, syscolumns c2,
  10053.         sysreferences r
  10054.     where    
  10055.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  10056.     and    o1.name = @pk_table_name
  10057.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10058.     and    o1.id = r.rkeyid
  10059.     and    r.keycnt >= 4
  10060.     and    o1.id = c1.id
  10061.     and    c1.colid = r.rkey4
  10062.     and     r.fkeyid = o2.id
  10063.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  10064.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10065.     and    o2.id = c2.id    
  10066.     and    c2.colid = r.fkey4
  10067.     union all
  10068.     select
  10069.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10070.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10071.         PK_TABLE_NAME         = o1.name,
  10072.         PK_COLUMN_NAME         = c1.name,
  10073.         PK_COLUMN_GUID        = convert(binary(16),null),
  10074.         PK_COLUMN_PROPID    = convert(int,null),
  10075.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10076.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  10077.         FK_TABLE_NAME         = o2.name,
  10078.         FK_COLUMN_NAME         = c2.name,
  10079.         FK_COLUMN_GUID        = convert(binary(16),null),
  10080.         FK_COLUMN_PROPID    = convert(int,null),
  10081.         ORDINAL            = convert(int,5),
  10082.         UPDATE_RULE        = 'NO ACTION',
  10083.         DELETE_RULE         = 'NO ACTION'        
  10084.     from
  10085.         sysobjects o1, sysobjects o2,
  10086.         syscolumns c1, syscolumns c2,
  10087.         sysreferences r
  10088.     where    
  10089.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  10090.     and    o1.name = @pk_table_name
  10091.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10092.     and    o1.id = r.rkeyid
  10093.     and    r.keycnt >= 5
  10094.     and    o1.id = c1.id
  10095.     and    c1.colid = r.rkey5
  10096.     and     r.fkeyid = o2.id
  10097.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  10098.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10099.     and    o2.id = c2.id    
  10100.     and    c2.colid = r.fkey5
  10101.     union all
  10102.     select
  10103.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10104.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10105.         PK_TABLE_NAME         = o1.name,
  10106.         PK_COLUMN_NAME         = c1.name,
  10107.         PK_COLUMN_GUID        = convert(binary(16),null),
  10108.         PK_COLUMN_PROPID    = convert(int,null),
  10109.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10110.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  10111.         FK_TABLE_NAME         = o2.name,
  10112.         FK_COLUMN_NAME         = c2.name,
  10113.         FK_COLUMN_GUID        = convert(binary(16),null),
  10114.         FK_COLUMN_PROPID    = convert(int,null),
  10115.         ORDINAL            = convert(int,6),
  10116.         UPDATE_RULE        = 'NO ACTION',
  10117.         DELETE_RULE         = 'NO ACTION'        
  10118.     from
  10119.         sysobjects o1, sysobjects o2,
  10120.         syscolumns c1, syscolumns c2,
  10121.         sysreferences r
  10122.     where    
  10123.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  10124.     and    o1.name = @pk_table_name
  10125.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10126.     and    o1.id = r.rkeyid
  10127.     and    r.keycnt >= 6
  10128.     and    o1.id = c1.id
  10129.     and    c1.colid = r.rkey6
  10130.     and     r.fkeyid = o2.id
  10131.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  10132.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10133.     and    o2.id = c2.id    
  10134.     and    c2.colid = r.fkey6
  10135.     union all
  10136.     select
  10137.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10138.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10139.         PK_TABLE_NAME         = o1.name,
  10140.         PK_COLUMN_NAME         = c1.name,
  10141.         PK_COLUMN_GUID        = convert(binary(16),null),
  10142.         PK_COLUMN_PROPID    = convert(int,null),
  10143.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10144.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  10145.         FK_TABLE_NAME         = o2.name,
  10146.         FK_COLUMN_NAME         = c2.name,
  10147.         FK_COLUMN_GUID        = convert(binary(16),null),
  10148.         FK_COLUMN_PROPID    = convert(int,null),
  10149.         ORDINAL            = convert(int,7),
  10150.         UPDATE_RULE        = 'NO ACTION',
  10151.         DELETE_RULE         = 'NO ACTION'        
  10152.     from
  10153.         sysobjects o1, sysobjects o2,
  10154.         syscolumns c1, syscolumns c2,
  10155.         sysreferences r
  10156.     where    
  10157.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  10158.     and    o1.name = @pk_table_name
  10159.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10160.     and    o1.id = r.rkeyid
  10161.     and    r.keycnt >= 7
  10162.     and    o1.id = c1.id
  10163.     and    c1.colid = r.rkey7
  10164.     and     r.fkeyid = o2.id
  10165.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  10166.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10167.     and    o2.id = c2.id    
  10168.     and    c2.colid = r.fkey7
  10169.     union all
  10170.     select
  10171.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10172.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10173.         PK_TABLE_NAME         = o1.name,
  10174.         PK_COLUMN_NAME         = c1.name,
  10175.         PK_COLUMN_GUID        = convert(binary(16),null),
  10176.         PK_COLUMN_PROPID    = convert(int,null),
  10177.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10178.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  10179.         FK_TABLE_NAME         = o2.name,
  10180.         FK_COLUMN_NAME         = c2.name,
  10181.         FK_COLUMN_GUID        = convert(binary(16),null),
  10182.         FK_COLUMN_PROPID    = convert(int,null),
  10183.         ORDINAL            = convert(int,8),
  10184.         UPDATE_RULE        = 'NO ACTION',
  10185.         DELETE_RULE         = 'NO ACTION'        
  10186.     from
  10187.         sysobjects o1, sysobjects o2,
  10188.         syscolumns c1, syscolumns c2,
  10189.         sysreferences r
  10190.     where    
  10191.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  10192.     and    o1.name = @pk_table_name
  10193.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10194.     and    o1.id = r.rkeyid
  10195.     and    r.keycnt >= 8
  10196.     and    o1.id = c1.id
  10197.     and    c1.colid = r.rkey8
  10198.     and     r.fkeyid = o2.id
  10199.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  10200.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10201.     and    o2.id = c2.id    
  10202.     and    c2.colid = r.fkey8
  10203.     union all
  10204.     select
  10205.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10206.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10207.         PK_TABLE_NAME         = o1.name,
  10208.         PK_COLUMN_NAME         = c1.name,
  10209.         PK_COLUMN_GUID        = convert(binary(16),null),
  10210.         PK_COLUMN_PROPID    = convert(int,null),
  10211.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10212.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  10213.         FK_TABLE_NAME         = o2.name,
  10214.         FK_COLUMN_NAME         = c2.name,
  10215.         FK_COLUMN_GUID        = convert(binary(16),null),
  10216.         FK_COLUMN_PROPID    = convert(int,null),
  10217.         ORDINAL            = convert(int,9),
  10218.         UPDATE_RULE        = 'NO ACTION',
  10219.         DELETE_RULE         = 'NO ACTION'        
  10220.     from
  10221.         sysobjects o1, sysobjects o2,
  10222.         syscolumns c1, syscolumns c2,
  10223.         sysreferences r
  10224.     where    
  10225.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  10226.     and    o1.name = @pk_table_name
  10227.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10228.     and    o1.id = r.rkeyid
  10229.     and    r.keycnt >= 9
  10230.     and    o1.id = c1.id
  10231.     and    c1.colid = r.rkey9
  10232.     and     r.fkeyid = o2.id
  10233.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  10234.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10235.     and    o2.id = c2.id    
  10236.     and    c2.colid = r.fkey9
  10237.     union all
  10238.     select
  10239.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10240.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10241.         PK_TABLE_NAME         = o1.name,
  10242.         PK_COLUMN_NAME         = c1.name,
  10243.         PK_COLUMN_GUID        = convert(binary(16),null),
  10244.         PK_COLUMN_PROPID    = convert(int,null),
  10245.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10246.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  10247.         FK_TABLE_NAME         = o2.name,
  10248.         FK_COLUMN_NAME         = c2.name,
  10249.         FK_COLUMN_GUID        = convert(binary(16),null),
  10250.         FK_COLUMN_PROPID    = convert(int,null),
  10251.         ORDINAL            = convert(int,10),
  10252.         UPDATE_RULE        = 'NO ACTION',
  10253.         DELETE_RULE         = 'NO ACTION'        
  10254.     from
  10255.         sysobjects o1, sysobjects o2,
  10256.         syscolumns c1, syscolumns c2,
  10257.         sysreferences r
  10258.     where    
  10259.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  10260.     and    o1.name = @pk_table_name
  10261.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10262.     and    o1.id = r.rkeyid
  10263.     and    r.keycnt >= 10
  10264.     and    o1.id = c1.id
  10265.     and    c1.colid = r.rkey10
  10266.     and     r.fkeyid = o2.id
  10267.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  10268.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10269.     and    o2.id = c2.id    
  10270.     and    c2.colid = r.fkey10
  10271.     union all
  10272.     select
  10273.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10274.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10275.         PK_TABLE_NAME         = o1.name,
  10276.         PK_COLUMN_NAME         = c1.name,
  10277.         PK_COLUMN_GUID        = convert(binary(16),null),
  10278.         PK_COLUMN_PROPID    = convert(int,null),
  10279.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10280.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  10281.         FK_TABLE_NAME         = o2.name,
  10282.         FK_COLUMN_NAME         = c2.name,
  10283.         FK_COLUMN_GUID        = convert(binary(16),null),
  10284.         FK_COLUMN_PROPID    = convert(int,null),
  10285.         ORDINAL            = convert(int,11),
  10286.         UPDATE_RULE        = 'NO ACTION',
  10287.         DELETE_RULE         = 'NO ACTION'        
  10288.     from
  10289.         sysobjects o1, sysobjects o2,
  10290.         syscolumns c1, syscolumns c2,
  10291.         sysreferences r
  10292.     where    
  10293.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  10294.     and    o1.name = @pk_table_name
  10295.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10296.     and    o1.id = r.rkeyid
  10297.     and    r.keycnt >= 11
  10298.     and    o1.id = c1.id
  10299.     and    c1.colid = r.rkey11
  10300.     and     r.fkeyid = o2.id
  10301.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  10302.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10303.     and    o2.id = c2.id    
  10304.     and    c2.colid = r.fkey11
  10305.     union all
  10306.     select
  10307.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10308.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10309.         PK_TABLE_NAME         = o1.name,
  10310.         PK_COLUMN_NAME         = c1.name,
  10311.         PK_COLUMN_GUID        = convert(binary(16),null),
  10312.         PK_COLUMN_PROPID    = convert(int,null),
  10313.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10314.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  10315.         FK_TABLE_NAME         = o2.name,
  10316.         FK_COLUMN_NAME         = c2.name,
  10317.         FK_COLUMN_GUID        = convert(binary(16),null),
  10318.         FK_COLUMN_PROPID    = convert(int,null),
  10319.         ORDINAL            = convert(int,12),
  10320.         UPDATE_RULE        = 'NO ACTION',
  10321.         DELETE_RULE         = 'NO ACTION'        
  10322.     from
  10323.         sysobjects o1, sysobjects o2,
  10324.         syscolumns c1, syscolumns c2,
  10325.         sysreferences r
  10326.     where    
  10327.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  10328.     and    o1.name = @pk_table_name
  10329.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10330.     and    o1.id = r.rkeyid
  10331.     and    r.keycnt >= 12
  10332.     and    o1.id = c1.id
  10333.     and    c1.colid = r.rkey12
  10334.     and     r.fkeyid = o2.id
  10335.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  10336.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10337.     and    o2.id = c2.id    
  10338.     and    c2.colid = r.fkey12
  10339.     union all
  10340.     select
  10341.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10342.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10343.         PK_TABLE_NAME         = o1.name,
  10344.         PK_COLUMN_NAME         = c1.name,
  10345.         PK_COLUMN_GUID        = convert(binary(16),null),
  10346.         PK_COLUMN_PROPID    = convert(int,null),
  10347.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10348.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  10349.         FK_TABLE_NAME         = o2.name,
  10350.         FK_COLUMN_NAME         = c2.name,
  10351.         FK_COLUMN_GUID        = convert(binary(16),null),
  10352.         FK_COLUMN_PROPID    = convert(int,null),
  10353.         ORDINAL            = convert(int,13),
  10354.         UPDATE_RULE        = 'NO ACTION',
  10355.         DELETE_RULE         = 'NO ACTION'        
  10356.     from
  10357.         sysobjects o1, sysobjects o2,
  10358.         syscolumns c1, syscolumns c2,
  10359.         sysreferences r
  10360.     where    
  10361.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  10362.     and    o1.name = @pk_table_name
  10363.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10364.     and    o1.id = r.rkeyid
  10365.     and    r.keycnt >= 13
  10366.     and    o1.id = c1.id
  10367.     and    c1.colid = r.rkey13
  10368.     and     r.fkeyid = o2.id
  10369.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  10370.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10371.     and    o2.id = c2.id    
  10372.     and    c2.colid = r.fkey13
  10373.     union all
  10374.     select
  10375.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10376.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10377.         PK_TABLE_NAME         = o1.name,
  10378.         PK_COLUMN_NAME         = c1.name,
  10379.         PK_COLUMN_GUID        = convert(binary(16),null),
  10380.         PK_COLUMN_PROPID    = convert(int,null),
  10381.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10382.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  10383.         FK_TABLE_NAME         = o2.name,
  10384.         FK_COLUMN_NAME         = c2.name,
  10385.         FK_COLUMN_GUID        = convert(binary(16),null),
  10386.         FK_COLUMN_PROPID    = convert(int,null),
  10387.         ORDINAL            = convert(int,14),
  10388.         UPDATE_RULE        = 'NO ACTION',
  10389.         DELETE_RULE         = 'NO ACTION'        
  10390.     from
  10391.         sysobjects o1, sysobjects o2,
  10392.         syscolumns c1, syscolumns c2,
  10393.         sysreferences r
  10394.     where    
  10395.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  10396.     and    o1.name = @pk_table_name
  10397.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10398.     and    o1.id = r.rkeyid
  10399.     and    r.keycnt >= 14
  10400.     and    o1.id = c1.id
  10401.     and    c1.colid = r.rkey14
  10402.     and     r.fkeyid = o2.id
  10403.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  10404.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10405.     and    o2.id = c2.id    
  10406.     and    c2.colid = r.fkey14
  10407.     union all
  10408.     select
  10409.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10410.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10411.         PK_TABLE_NAME         = o1.name,
  10412.         PK_COLUMN_NAME         = c1.name,
  10413.         PK_COLUMN_GUID        = convert(binary(16),null),
  10414.         PK_COLUMN_PROPID    = convert(int,null),
  10415.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10416.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  10417.         FK_TABLE_NAME         = o2.name,
  10418.         FK_COLUMN_NAME         = c2.name,
  10419.         FK_COLUMN_GUID        = convert(binary(16),null),
  10420.         FK_COLUMN_PROPID    = convert(int,null),
  10421.         ORDINAL            = convert(int,15),
  10422.         UPDATE_RULE        = 'NO ACTION',
  10423.         DELETE_RULE         = 'NO ACTION'        
  10424.     from
  10425.         sysobjects o1, sysobjects o2,
  10426.         syscolumns c1, syscolumns c2,
  10427.         sysreferences r
  10428.     where    
  10429.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  10430.     and    o1.name = @pk_table_name
  10431.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10432.     and    o1.id = r.rkeyid
  10433.     and    r.keycnt >= 15
  10434.     and    o1.id = c1.id
  10435.     and    c1.colid = r.rkey15
  10436.     and     r.fkeyid = o2.id
  10437.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  10438.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10439.     and    o2.id = c2.id    
  10440.     and    c2.colid = r.fkey15
  10441.     union all
  10442.     select
  10443.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10444.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10445.         PK_TABLE_NAME         = o1.name,
  10446.         PK_COLUMN_NAME         = c1.name,
  10447.         PK_COLUMN_GUID        = convert(binary(16),null),
  10448.         PK_COLUMN_PROPID    = convert(int,null),
  10449.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10450.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  10451.         FK_TABLE_NAME         = o2.name,
  10452.         FK_COLUMN_NAME         = c2.name,
  10453.         FK_COLUMN_GUID        = convert(binary(16),null),
  10454.         FK_COLUMN_PROPID    = convert(int,null),
  10455.         ORDINAL            = convert(int,16),
  10456.         UPDATE_RULE        = 'NO ACTION',
  10457.         DELETE_RULE         = 'NO ACTION'        
  10458.     from
  10459.         sysobjects o1, sysobjects o2,
  10460.         syscolumns c1, syscolumns c2,
  10461.         sysreferences r
  10462.     where    
  10463.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  10464.     and    o1.name = @pk_table_name
  10465.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10466.     and    o1.id = r.rkeyid
  10467.     and    r.keycnt >= 16
  10468.     and    o1.id = c1.id
  10469.     and    c1.colid = r.rkey16
  10470.     and     r.fkeyid = o2.id
  10471.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  10472.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10473.     and    o2.id = c2.id    
  10474.     and    c2.colid = r.fkey16
  10475.     order by 8,9,2,3,13
  10476.     END
  10477. ELSE IF @fk_table_name is not null
  10478.     BEGIN
  10479.     select
  10480.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10481.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10482.         PK_TABLE_NAME         = o1.name,
  10483.         PK_COLUMN_NAME         = c1.name,
  10484.         PK_COLUMN_GUID        = convert(binary(16),null),
  10485.         PK_COLUMN_PROPID    = convert(int,null),
  10486.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10487.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  10488.         FK_TABLE_NAME         = o2.name,
  10489.         FK_COLUMN_NAME         = c2.name,
  10490.         FK_COLUMN_GUID        = convert(binary(16),null),
  10491.         FK_COLUMN_PROPID    = convert(int,null),
  10492.         ORDINAL            = convert(int,1),
  10493.         UPDATE_RULE        = 'NO ACTION',
  10494.         DELETE_RULE         = 'NO ACTION'        
  10495.     from
  10496.         sysobjects o1, sysobjects o2,
  10497.         syscolumns c1, syscolumns c2,
  10498.         sysreferences r
  10499.     where    
  10500.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  10501.     and    o2.name = @fk_table_name
  10502.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10503.     and    o2.id = r.fkeyid
  10504.     and    o2.id = c2.id
  10505.     and    c2.colid = r.fkey1
  10506.     and     r.rkeyid = o1.id
  10507.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  10508.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10509.     and    o1.id = c1.id    
  10510.     and    c1.colid = r.rkey1
  10511.     union    all
  10512.     select
  10513.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10514.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10515.         PK_TABLE_NAME         = o1.name,
  10516.         PK_COLUMN_NAME         = c1.name,
  10517.         PK_COLUMN_GUID        = convert(binary(16),null),
  10518.         PK_COLUMN_PROPID    = convert(int,null),
  10519.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10520.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  10521.         FK_TABLE_NAME         = o2.name,
  10522.         FK_COLUMN_NAME         = c2.name,
  10523.         FK_COLUMN_GUID        = convert(binary(16),null),
  10524.         FK_COLUMN_PROPID    = convert(int,null),
  10525.         ORDINAL            = convert(int,2),
  10526.         UPDATE_RULE        = 'NO ACTION',
  10527.         DELETE_RULE         = 'NO ACTION'        
  10528.     from
  10529.         sysobjects o1, sysobjects o2,
  10530.         syscolumns c1, syscolumns c2,
  10531.         sysreferences r
  10532.     where    
  10533.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  10534.     and    o2.name = @fk_table_name
  10535.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10536.     and    o2.id = r.fkeyid
  10537.     and    r.keycnt >= 2
  10538.     and    o2.id = c2.id
  10539.     and    c2.colid = r.fkey2
  10540.     and     r.rkeyid = o1.id
  10541.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  10542.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10543.     and    o1.id = c1.id    
  10544.     and    c1.colid = r.rkey2
  10545.     union    all
  10546.     select
  10547.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10548.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10549.         PK_TABLE_NAME         = o1.name,
  10550.         PK_COLUMN_NAME         = c1.name,
  10551.         PK_COLUMN_GUID        = convert(binary(16),null),
  10552.         PK_COLUMN_PROPID    = convert(int,null),
  10553.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10554.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  10555.         FK_TABLE_NAME         = o2.name,
  10556.         FK_COLUMN_NAME         = c2.name,
  10557.         FK_COLUMN_GUID        = convert(binary(16),null),
  10558.         FK_COLUMN_PROPID    = convert(int,null),
  10559.         ORDINAL            = convert(int,3),
  10560.         UPDATE_RULE        = 'NO ACTION',
  10561.         DELETE_RULE         = 'NO ACTION'        
  10562.     from
  10563.         sysobjects o1, sysobjects o2,
  10564.         syscolumns c1, syscolumns c2,
  10565.         sysreferences r
  10566.     where    
  10567.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  10568.     and    o2.name = @fk_table_name
  10569.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10570.     and    o2.id = r.fkeyid
  10571.     and    r.keycnt >= 3
  10572.     and    o2.id = c2.id
  10573.     and    c2.colid = r.fkey3
  10574.     and     r.rkeyid = o1.id
  10575.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  10576.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10577.     and    o1.id = c1.id    
  10578.     and    c1.colid = r.rkey3
  10579.     union    all
  10580.     select
  10581.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10582.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10583.         PK_TABLE_NAME         = o1.name,
  10584.         PK_COLUMN_NAME         = c1.name,
  10585.         PK_COLUMN_GUID        = convert(binary(16),null),
  10586.         PK_COLUMN_PROPID    = convert(int,null),
  10587.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10588.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  10589.         FK_TABLE_NAME         = o2.name,
  10590.         FK_COLUMN_NAME         = c2.name,
  10591.         FK_COLUMN_GUID        = convert(binary(16),null),
  10592.         FK_COLUMN_PROPID    = convert(int,null),
  10593.         ORDINAL            = convert(int,4),
  10594.         UPDATE_RULE        = 'NO ACTION',
  10595.         DELETE_RULE         = 'NO ACTION'        
  10596.     from
  10597.         sysobjects o1, sysobjects o2,
  10598.         syscolumns c1, syscolumns c2,
  10599.         sysreferences r
  10600.     where    
  10601.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  10602.     and    o2.name = @fk_table_name
  10603.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10604.     and    o2.id = r.fkeyid
  10605.     and    r.keycnt >= 4
  10606.     and    o2.id = c2.id
  10607.     and    c2.colid = r.fkey4
  10608.     and     r.rkeyid = o1.id
  10609.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  10610.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10611.     and    o1.id = c1.id    
  10612.     and    c1.colid = r.rkey4
  10613.     union    all
  10614.     select
  10615.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10616.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10617.         PK_TABLE_NAME         = o1.name,
  10618.         PK_COLUMN_NAME         = c1.name,
  10619.         PK_COLUMN_GUID        = convert(binary(16),null),
  10620.         PK_COLUMN_PROPID    = convert(int,null),
  10621.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10622.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  10623.         FK_TABLE_NAME         = o2.name,
  10624.         FK_COLUMN_NAME         = c2.name,
  10625.         FK_COLUMN_GUID        = convert(binary(16),null),
  10626.         FK_COLUMN_PROPID    = convert(int,null),
  10627.         ORDINAL            = convert(int,5),
  10628.         UPDATE_RULE        = 'NO ACTION',
  10629.         DELETE_RULE         = 'NO ACTION'        
  10630.     from
  10631.         sysobjects o1, sysobjects o2,
  10632.         syscolumns c1, syscolumns c2,
  10633.         sysreferences r
  10634.     where    
  10635.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  10636.     and    o2.name = @fk_table_name
  10637.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10638.     and    o2.id = r.fkeyid
  10639.     and    r.keycnt >= 5
  10640.     and    o2.id = c2.id
  10641.     and    c2.colid = r.fkey5
  10642.     and     r.rkeyid = o1.id
  10643.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  10644.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10645.     and    o1.id = c1.id    
  10646.     and    c1.colid = r.rkey5
  10647.     union    all
  10648.     select
  10649.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10650.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10651.         PK_TABLE_NAME         = o1.name,
  10652.         PK_COLUMN_NAME         = c1.name,
  10653.         PK_COLUMN_GUID        = convert(binary(16),null),
  10654.         PK_COLUMN_PROPID    = convert(int,null),
  10655.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10656.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  10657.         FK_TABLE_NAME         = o2.name,
  10658.         FK_COLUMN_NAME         = c2.name,
  10659.         FK_COLUMN_GUID        = convert(binary(16),null),
  10660.         FK_COLUMN_PROPID    = convert(int,null),
  10661.         ORDINAL            = convert(int,6),
  10662.         UPDATE_RULE        = 'NO ACTION',
  10663.         DELETE_RULE         = 'NO ACTION'        
  10664.     from
  10665.         sysobjects o1, sysobjects o2,
  10666.         syscolumns c1, syscolumns c2,
  10667.         sysreferences r
  10668.     where    
  10669.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  10670.     and    o2.name = @fk_table_name
  10671.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10672.     and    o2.id = r.fkeyid
  10673.     and    r.keycnt >= 6
  10674.     and    o2.id = c2.id
  10675.     and    c2.colid = r.fkey6
  10676.     and     r.rkeyid = o1.id
  10677.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  10678.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10679.     and    o1.id = c1.id    
  10680.     and    c1.colid = r.rkey6
  10681.     union    all
  10682.     select
  10683.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10684.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10685.         PK_TABLE_NAME         = o1.name,
  10686.         PK_COLUMN_NAME         = c1.name,
  10687.         PK_COLUMN_GUID        = convert(binary(16),null),
  10688.         PK_COLUMN_PROPID    = convert(int,null),
  10689.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10690.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  10691.         FK_TABLE_NAME         = o2.name,
  10692.         FK_COLUMN_NAME         = c2.name,
  10693.         FK_COLUMN_GUID        = convert(binary(16),null),
  10694.         FK_COLUMN_PROPID    = convert(int,null),
  10695.         ORDINAL            = convert(int,7),
  10696.         UPDATE_RULE        = 'NO ACTION',
  10697.         DELETE_RULE         = 'NO ACTION'        
  10698.     from
  10699.         sysobjects o1, sysobjects o2,
  10700.         syscolumns c1, syscolumns c2,
  10701.         sysreferences r
  10702.     where    
  10703.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  10704.     and    o2.name = @fk_table_name
  10705.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10706.     and    o2.id = r.fkeyid
  10707.     and    r.keycnt >= 7
  10708.     and    o2.id = c2.id
  10709.     and    c2.colid = r.fkey7
  10710.     and     r.rkeyid = o1.id
  10711.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  10712.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10713.     and    o1.id = c1.id    
  10714.     and    c1.colid = r.rkey7
  10715.     union    all
  10716.     select
  10717.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10718.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10719.         PK_TABLE_NAME         = o1.name,
  10720.         PK_COLUMN_NAME         = c1.name,
  10721.         PK_COLUMN_GUID        = convert(binary(16),null),
  10722.         PK_COLUMN_PROPID    = convert(int,null),
  10723.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10724.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  10725.         FK_TABLE_NAME         = o2.name,
  10726.         FK_COLUMN_NAME         = c2.name,
  10727.         FK_COLUMN_GUID        = convert(binary(16),null),
  10728.         FK_COLUMN_PROPID    = convert(int,null),
  10729.         ORDINAL            = convert(int,8),
  10730.         UPDATE_RULE        = 'NO ACTION',
  10731.         DELETE_RULE         = 'NO ACTION'        
  10732.     from
  10733.         sysobjects o1, sysobjects o2,
  10734.         syscolumns c1, syscolumns c2,
  10735.         sysreferences r
  10736.     where    
  10737.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  10738.     and    o2.name = @fk_table_name
  10739.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10740.     and    o2.id = r.fkeyid
  10741.     and    r.keycnt >= 8
  10742.     and    o2.id = c2.id
  10743.     and    c2.colid = r.fkey8
  10744.     and     r.rkeyid = o1.id
  10745.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  10746.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10747.     and    o1.id = c1.id    
  10748.     and    c1.colid = r.rkey8
  10749.     union    all
  10750.     select
  10751.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10752.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10753.         PK_TABLE_NAME         = o1.name,
  10754.         PK_COLUMN_NAME         = c1.name,
  10755.         PK_COLUMN_GUID        = convert(binary(16),null),
  10756.         PK_COLUMN_PROPID    = convert(int,null),
  10757.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10758.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  10759.         FK_TABLE_NAME         = o2.name,
  10760.         FK_COLUMN_NAME         = c2.name,
  10761.         FK_COLUMN_GUID        = convert(binary(16),null),
  10762.         FK_COLUMN_PROPID    = convert(int,null),
  10763.         ORDINAL            = convert(int,9),
  10764.         UPDATE_RULE        = 'NO ACTION',
  10765.         DELETE_RULE         = 'NO ACTION'        
  10766.     from
  10767.         sysobjects o1, sysobjects o2,
  10768.         syscolumns c1, syscolumns c2,
  10769.         sysreferences r
  10770.     where    
  10771.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  10772.     and    o2.name = @fk_table_name
  10773.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10774.     and    o2.id = r.fkeyid
  10775.     and    r.keycnt >= 9
  10776.     and    o2.id = c2.id
  10777.     and    c2.colid = r.fkey9
  10778.     and     r.rkeyid = o1.id
  10779.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  10780.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10781.     and    o1.id = c1.id    
  10782.     and    c1.colid = r.rkey9
  10783.     union    all
  10784.     select
  10785.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10786.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10787.         PK_TABLE_NAME         = o1.name,
  10788.         PK_COLUMN_NAME         = c1.name,
  10789.         PK_COLUMN_GUID        = convert(binary(16),null),
  10790.         PK_COLUMN_PROPID    = convert(int,null),
  10791.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10792.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  10793.         FK_TABLE_NAME         = o2.name,
  10794.         FK_COLUMN_NAME         = c2.name,
  10795.         FK_COLUMN_GUID        = convert(binary(16),null),
  10796.         FK_COLUMN_PROPID    = convert(int,null),
  10797.         ORDINAL            = convert(int,10),
  10798.         UPDATE_RULE        = 'NO ACTION',
  10799.         DELETE_RULE         = 'NO ACTION'        
  10800.     from
  10801.         sysobjects o1, sysobjects o2,
  10802.         syscolumns c1, syscolumns c2,
  10803.         sysreferences r
  10804.     where    
  10805.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  10806.     and    o2.name = @fk_table_name
  10807.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10808.     and    o2.id = r.fkeyid
  10809.     and    r.keycnt >= 10
  10810.     and    o2.id = c2.id
  10811.     and    c2.colid = r.fkey10
  10812.     and     r.rkeyid = o1.id
  10813.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  10814.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10815.     and    o1.id = c1.id    
  10816.     and    c1.colid = r.rkey10
  10817.     union    all
  10818.     select
  10819.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10820.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10821.         PK_TABLE_NAME         = o1.name,
  10822.         PK_COLUMN_NAME         = c1.name,
  10823.         PK_COLUMN_GUID        = convert(binary(16),null),
  10824.         PK_COLUMN_PROPID    = convert(int,null),
  10825.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10826.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  10827.         FK_TABLE_NAME         = o2.name,
  10828.         FK_COLUMN_NAME         = c2.name,
  10829.         FK_COLUMN_GUID        = convert(binary(16),null),
  10830.         FK_COLUMN_PROPID    = convert(int,null),
  10831.         ORDINAL            = convert(int,11),
  10832.         UPDATE_RULE        = 'NO ACTION',
  10833.         DELETE_RULE         = 'NO ACTION'        
  10834.     from
  10835.         sysobjects o1, sysobjects o2,
  10836.         syscolumns c1, syscolumns c2,
  10837.         sysreferences r
  10838.     where    
  10839.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  10840.     and    o2.name = @fk_table_name
  10841.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10842.     and    o2.id = r.fkeyid
  10843.     and    r.keycnt >= 11
  10844.     and    o2.id = c2.id
  10845.     and    c2.colid = r.fkey11
  10846.     and     r.rkeyid = o1.id
  10847.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  10848.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10849.     and    o1.id = c1.id    
  10850.     and    c1.colid = r.rkey11
  10851.     union    all
  10852.     select
  10853.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10854.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10855.         PK_TABLE_NAME         = o1.name,
  10856.         PK_COLUMN_NAME         = c1.name,
  10857.         PK_COLUMN_GUID        = convert(binary(16),null),
  10858.         PK_COLUMN_PROPID    = convert(int,null),
  10859.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10860.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  10861.         FK_TABLE_NAME         = o2.name,
  10862.         FK_COLUMN_NAME         = c2.name,
  10863.         FK_COLUMN_GUID        = convert(binary(16),null),
  10864.         FK_COLUMN_PROPID    = convert(int,null),
  10865.         ORDINAL            = convert(int,12),
  10866.         UPDATE_RULE        = 'NO ACTION',
  10867.         DELETE_RULE         = 'NO ACTION'        
  10868.     from
  10869.         sysobjects o1, sysobjects o2,
  10870.         syscolumns c1, syscolumns c2,
  10871.         sysreferences r
  10872.     where    
  10873.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  10874.     and    o2.name = @fk_table_name
  10875.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10876.     and    o2.id = r.fkeyid
  10877.     and    r.keycnt >= 12
  10878.     and    o2.id = c2.id
  10879.     and    c2.colid = r.fkey12
  10880.     and     r.rkeyid = o1.id
  10881.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  10882.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10883.     and    o1.id = c1.id    
  10884.     and    c1.colid = r.rkey12
  10885.     union    all
  10886.     select
  10887.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10888.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10889.         PK_TABLE_NAME         = o1.name,
  10890.         PK_COLUMN_NAME         = c1.name,
  10891.         PK_COLUMN_GUID        = convert(binary(16),null),
  10892.         PK_COLUMN_PROPID    = convert(int,null),
  10893.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10894.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  10895.         FK_TABLE_NAME         = o2.name,
  10896.         FK_COLUMN_NAME         = c2.name,
  10897.         FK_COLUMN_GUID        = convert(binary(16),null),
  10898.         FK_COLUMN_PROPID    = convert(int,null),
  10899.         ORDINAL            = convert(int,13),
  10900.         UPDATE_RULE        = 'NO ACTION',
  10901.         DELETE_RULE         = 'NO ACTION'        
  10902.     from
  10903.         sysobjects o1, sysobjects o2,
  10904.         syscolumns c1, syscolumns c2,
  10905.         sysreferences r
  10906.     where    
  10907.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  10908.     and    o2.name = @fk_table_name
  10909.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10910.     and    o2.id = r.fkeyid
  10911.     and    r.keycnt >= 13
  10912.     and    o2.id = c2.id
  10913.     and    c2.colid = r.fkey13
  10914.     and     r.rkeyid = o1.id
  10915.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  10916.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10917.     and    o1.id = c1.id    
  10918.     and    c1.colid = r.rkey13
  10919.     union    all
  10920.     select
  10921.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10922.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10923.         PK_TABLE_NAME         = o1.name,
  10924.         PK_COLUMN_NAME         = c1.name,
  10925.         PK_COLUMN_GUID        = convert(binary(16),null),
  10926.         PK_COLUMN_PROPID    = convert(int,null),
  10927.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10928.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  10929.         FK_TABLE_NAME         = o2.name,
  10930.         FK_COLUMN_NAME         = c2.name,
  10931.         FK_COLUMN_GUID        = convert(binary(16),null),
  10932.         FK_COLUMN_PROPID    = convert(int,null),
  10933.         ORDINAL            = convert(int,14),
  10934.         UPDATE_RULE        = 'NO ACTION',
  10935.         DELETE_RULE         = 'NO ACTION'        
  10936.     from
  10937.         sysobjects o1, sysobjects o2,
  10938.         syscolumns c1, syscolumns c2,
  10939.         sysreferences r
  10940.     where    
  10941.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  10942.     and    o2.name = @fk_table_name
  10943.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10944.     and    o2.id = r.fkeyid
  10945.     and    r.keycnt >= 14
  10946.     and    o2.id = c2.id
  10947.     and    c2.colid = r.fkey14
  10948.     and     r.rkeyid = o1.id
  10949.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  10950.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10951.     and    o1.id = c1.id    
  10952.     and    c1.colid = r.rkey14
  10953.     union    all
  10954.     select
  10955.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10956.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10957.         PK_TABLE_NAME         = o1.name,
  10958.         PK_COLUMN_NAME         = c1.name,
  10959.         PK_COLUMN_GUID        = convert(binary(16),null),
  10960.         PK_COLUMN_PROPID    = convert(int,null),
  10961.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10962.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  10963.         FK_TABLE_NAME         = o2.name,
  10964.         FK_COLUMN_NAME         = c2.name,
  10965.         FK_COLUMN_GUID        = convert(binary(16),null),
  10966.         FK_COLUMN_PROPID    = convert(int,null),
  10967.         ORDINAL            = convert(int,15),
  10968.         UPDATE_RULE        = 'NO ACTION',
  10969.         DELETE_RULE         = 'NO ACTION'        
  10970.     from
  10971.         sysobjects o1, sysobjects o2,
  10972.         syscolumns c1, syscolumns c2,
  10973.         sysreferences r
  10974.     where    
  10975.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  10976.     and    o2.name = @fk_table_name
  10977.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10978.     and    o2.id = r.fkeyid
  10979.     and    r.keycnt >= 15
  10980.     and    o2.id = c2.id
  10981.     and    c2.colid = r.fkey15
  10982.     and     r.rkeyid = o1.id
  10983.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  10984.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10985.     and    o1.id = c1.id    
  10986.     and    c1.colid = r.rkey15
  10987.     union    all
  10988.     select
  10989.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10990.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10991.         PK_TABLE_NAME         = o1.name,
  10992.         PK_COLUMN_NAME         = c1.name,
  10993.         PK_COLUMN_GUID        = convert(binary(16),null),
  10994.         PK_COLUMN_PROPID    = convert(int,null),
  10995.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10996.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  10997.         FK_TABLE_NAME         = o2.name,
  10998.         FK_COLUMN_NAME         = c2.name,
  10999.         FK_COLUMN_GUID        = convert(binary(16),null),
  11000.         FK_COLUMN_PROPID    = convert(int,null),
  11001.         ORDINAL            = convert(int,16),
  11002.         UPDATE_RULE        = 'NO ACTION',
  11003.         DELETE_RULE         = 'NO ACTION'        
  11004.     from
  11005.         sysobjects o1, sysobjects o2,
  11006.         syscolumns c1, syscolumns c2,
  11007.         sysreferences r
  11008.     where    
  11009.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  11010.     and    o2.name = @fk_table_name
  11011.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11012.     and    o2.id = r.fkeyid
  11013.     and    r.keycnt >= 16
  11014.     and    o2.id = c2.id
  11015.     and    c2.colid = r.fkey16
  11016.     and     r.rkeyid = o1.id
  11017.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  11018.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11019.     and    o1.id = c1.id    
  11020.     and    c1.colid = r.rkey16
  11021.     order by 8,9,2,3,13
  11022.     END
  11023. ELSE
  11024.     BEGIN
  11025.     select
  11026.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11027.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11028.         PK_TABLE_NAME         = o1.name,
  11029.         PK_COLUMN_NAME         = c1.name,
  11030.         PK_COLUMN_GUID        = convert(binary(16),null),
  11031.         PK_COLUMN_PROPID    = convert(int,null),
  11032.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11033.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  11034.         FK_TABLE_NAME         = o2.name,
  11035.         FK_COLUMN_NAME         = c2.name,
  11036.         FK_COLUMN_GUID        = convert(binary(16),null),
  11037.         FK_COLUMN_PROPID    = convert(int,null),
  11038.         ORDINAL            = convert(int,1),
  11039.         UPDATE_RULE        = 'NO ACTION',
  11040.         DELETE_RULE         = 'NO ACTION'        
  11041.     from
  11042.         sysobjects o1, sysobjects o2,
  11043.         syscolumns c1, syscolumns c2,
  11044.         sysreferences r
  11045.     where    
  11046.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  11047.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11048.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11049.     and    o1.id = r.rkeyid
  11050.     and    o1.id = c1.id
  11051.     and    c1.colid = r.rkey1
  11052.     and     r.fkeyid = o2.id
  11053.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11054.     and    o2.id = c2.id    
  11055.     and    c2.colid = r.fkey1
  11056.     union all
  11057.     select
  11058.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11059.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11060.         PK_TABLE_NAME         = o1.name,
  11061.         PK_COLUMN_NAME         = c1.name,
  11062.         PK_COLUMN_GUID        = convert(binary(16),null),
  11063.         PK_COLUMN_PROPID    = convert(int,null),
  11064.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11065.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  11066.         FK_TABLE_NAME         = o2.name,
  11067.         FK_COLUMN_NAME         = c2.name,
  11068.         FK_COLUMN_GUID        = convert(binary(16),null),
  11069.         FK_COLUMN_PROPID    = convert(int,null),
  11070.         ORDINAL            = convert(int,2),
  11071.         UPDATE_RULE        = 'NO ACTION',
  11072.         DELETE_RULE         = 'NO ACTION'        
  11073.     from
  11074.         sysobjects o1, sysobjects o2,
  11075.         syscolumns c1, syscolumns c2,
  11076.         sysreferences r
  11077.     where    
  11078.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  11079.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11080.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11081.     and    o1.id = r.rkeyid
  11082.     and    r.keycnt >= 2
  11083.     and    o1.id = c1.id
  11084.     and    c1.colid = r.rkey2
  11085.     and     r.fkeyid = o2.id
  11086.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11087.     and    o2.id = c2.id    
  11088.     and    c2.colid = r.fkey2
  11089.     union all
  11090.     select
  11091.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11092.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11093.         PK_TABLE_NAME         = o1.name,
  11094.         PK_COLUMN_NAME         = c1.name,
  11095.         PK_COLUMN_GUID        = convert(binary(16),null),
  11096.         PK_COLUMN_PROPID    = convert(int,null),
  11097.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11098.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  11099.         FK_TABLE_NAME         = o2.name,
  11100.         FK_COLUMN_NAME         = c2.name,
  11101.         FK_COLUMN_GUID        = convert(binary(16),null),
  11102.         FK_COLUMN_PROPID    = convert(int,null),
  11103.         ORDINAL            = convert(int,3),
  11104.         UPDATE_RULE        = 'NO ACTION',
  11105.         DELETE_RULE         = 'NO ACTION'        
  11106.     from
  11107.         sysobjects o1, sysobjects o2,
  11108.         syscolumns c1, syscolumns c2,
  11109.         sysreferences r
  11110.     where    
  11111.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  11112.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11113.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11114.     and    o1.id = r.rkeyid
  11115.     and    r.keycnt >= 3
  11116.     and    o1.id = c1.id
  11117.     and    c1.colid = r.rkey3
  11118.     and     r.fkeyid = o2.id
  11119.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11120.     and    o2.id = c2.id    
  11121.     and    c2.colid = r.fkey3
  11122.     union all
  11123.     select
  11124.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11125.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11126.         PK_TABLE_NAME         = o1.name,
  11127.         PK_COLUMN_NAME         = c1.name,
  11128.         PK_COLUMN_GUID        = convert(binary(16),null),
  11129.         PK_COLUMN_PROPID    = convert(int,null),
  11130.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11131.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  11132.         FK_TABLE_NAME         = o2.name,
  11133.         FK_COLUMN_NAME         = c2.name,
  11134.         FK_COLUMN_GUID        = convert(binary(16),null),
  11135.         FK_COLUMN_PROPID    = convert(int,null),
  11136.         ORDINAL            = convert(int,4),
  11137.         UPDATE_RULE        = 'NO ACTION',
  11138.         DELETE_RULE         = 'NO ACTION'        
  11139.     from
  11140.         sysobjects o1, sysobjects o2,
  11141.         syscolumns c1, syscolumns c2,
  11142.         sysreferences r
  11143.     where    
  11144.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  11145.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11146.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11147.     and    o1.id = r.rkeyid
  11148.     and    r.keycnt >= 4
  11149.     and    o1.id = c1.id
  11150.     and    c1.colid = r.rkey4
  11151.     and     r.fkeyid = o2.id
  11152.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11153.     and    o2.id = c2.id    
  11154.     and    c2.colid = r.fkey4
  11155.     union all
  11156.     select
  11157.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11158.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11159.         PK_TABLE_NAME         = o1.name,
  11160.         PK_COLUMN_NAME         = c1.name,
  11161.         PK_COLUMN_GUID        = convert(binary(16),null),
  11162.         PK_COLUMN_PROPID    = convert(int,null),
  11163.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11164.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  11165.         FK_TABLE_NAME         = o2.name,
  11166.         FK_COLUMN_NAME         = c2.name,
  11167.         FK_COLUMN_GUID        = convert(binary(16),null),
  11168.         FK_COLUMN_PROPID    = convert(int,null),
  11169.         ORDINAL            = convert(int,5),
  11170.         UPDATE_RULE        = 'NO ACTION',
  11171.         DELETE_RULE         = 'NO ACTION'        
  11172.     from
  11173.         sysobjects o1, sysobjects o2,
  11174.         syscolumns c1, syscolumns c2,
  11175.         sysreferences r
  11176.     where    
  11177.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  11178.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11179.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11180.     and    o1.id = r.rkeyid
  11181.     and    r.keycnt >= 5
  11182.     and    o1.id = c1.id
  11183.     and    c1.colid = r.rkey5
  11184.     and     r.fkeyid = o2.id
  11185.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11186.     and    o2.id = c2.id    
  11187.     and    c2.colid = r.fkey5
  11188.     union all
  11189.     select
  11190.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11191.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11192.         PK_TABLE_NAME         = o1.name,
  11193.         PK_COLUMN_NAME         = c1.name,
  11194.         PK_COLUMN_GUID        = convert(binary(16),null),
  11195.         PK_COLUMN_PROPID    = convert(int,null),
  11196.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11197.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  11198.         FK_TABLE_NAME         = o2.name,
  11199.         FK_COLUMN_NAME         = c2.name,
  11200.         FK_COLUMN_GUID        = convert(binary(16),null),
  11201.         FK_COLUMN_PROPID    = convert(int,null),
  11202.         ORDINAL            = convert(int,6),
  11203.         UPDATE_RULE        = 'NO ACTION',
  11204.         DELETE_RULE         = 'NO ACTION'        
  11205.     from
  11206.         sysobjects o1, sysobjects o2,
  11207.         syscolumns c1, syscolumns c2,
  11208.         sysreferences r
  11209.     where    
  11210.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  11211.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11212.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11213.     and    o1.id = r.rkeyid
  11214.     and    r.keycnt >= 6
  11215.     and    o1.id = c1.id
  11216.     and    c1.colid = r.rkey6
  11217.     and     r.fkeyid = o2.id
  11218.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11219.     and    o2.id = c2.id    
  11220.     and    c2.colid = r.fkey6
  11221.     union all
  11222.     select
  11223.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11224.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11225.         PK_TABLE_NAME         = o1.name,
  11226.         PK_COLUMN_NAME         = c1.name,
  11227.         PK_COLUMN_GUID        = convert(binary(16),null),
  11228.         PK_COLUMN_PROPID    = convert(int,null),
  11229.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11230.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  11231.         FK_TABLE_NAME         = o2.name,
  11232.         FK_COLUMN_NAME         = c2.name,
  11233.         FK_COLUMN_GUID        = convert(binary(16),null),
  11234.         FK_COLUMN_PROPID    = convert(int,null),
  11235.         ORDINAL            = convert(int,7),
  11236.         UPDATE_RULE        = 'NO ACTION',
  11237.         DELETE_RULE         = 'NO ACTION'        
  11238.     from
  11239.         sysobjects o1, sysobjects o2,
  11240.         syscolumns c1, syscolumns c2,
  11241.         sysreferences r
  11242.     where    
  11243.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  11244.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11245.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11246.     and    o1.id = r.rkeyid
  11247.     and    r.keycnt >= 7
  11248.     and    o1.id = c1.id
  11249.     and    c1.colid = r.rkey7
  11250.     and     r.fkeyid = o2.id
  11251.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11252.     and    o2.id = c2.id    
  11253.     and    c2.colid = r.fkey7
  11254.     union all
  11255.     select
  11256.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11257.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11258.         PK_TABLE_NAME         = o1.name,
  11259.         PK_COLUMN_NAME         = c1.name,
  11260.         PK_COLUMN_GUID        = convert(binary(16),null),
  11261.         PK_COLUMN_PROPID    = convert(int,null),
  11262.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11263.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  11264.         FK_TABLE_NAME         = o2.name,
  11265.         FK_COLUMN_NAME         = c2.name,
  11266.         FK_COLUMN_GUID        = convert(binary(16),null),
  11267.         FK_COLUMN_PROPID    = convert(int,null),
  11268.         ORDINAL            = convert(int,8),
  11269.         UPDATE_RULE        = 'NO ACTION',
  11270.         DELETE_RULE         = 'NO ACTION'        
  11271.     from
  11272.         sysobjects o1, sysobjects o2,
  11273.         syscolumns c1, syscolumns c2,
  11274.         sysreferences r
  11275.     where    
  11276.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  11277.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11278.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11279.     and    o1.id = r.rkeyid
  11280.     and    r.keycnt >= 8
  11281.     and    o1.id = c1.id
  11282.     and    c1.colid = r.rkey8
  11283.     and     r.fkeyid = o2.id
  11284.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11285.     and    o2.id = c2.id    
  11286.     and    c2.colid = r.fkey8
  11287.     union all
  11288.     select
  11289.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11290.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11291.         PK_TABLE_NAME         = o1.name,
  11292.         PK_COLUMN_NAME         = c1.name,
  11293.         PK_COLUMN_GUID        = convert(binary(16),null),
  11294.         PK_COLUMN_PROPID    = convert(int,null),
  11295.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11296.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  11297.         FK_TABLE_NAME         = o2.name,
  11298.         FK_COLUMN_NAME         = c2.name,
  11299.         FK_COLUMN_GUID        = convert(binary(16),null),
  11300.         FK_COLUMN_PROPID    = convert(int,null),
  11301.         ORDINAL            = convert(int,9),
  11302.         UPDATE_RULE        = 'NO ACTION',
  11303.         DELETE_RULE         = 'NO ACTION'        
  11304.     from
  11305.         sysobjects o1, sysobjects o2,
  11306.         syscolumns c1, syscolumns c2,
  11307.         sysreferences r
  11308.     where    
  11309.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  11310.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11311.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11312.     and    o1.id = r.rkeyid
  11313.     and    r.keycnt >= 9
  11314.     and    o1.id = c1.id
  11315.     and    c1.colid = r.rkey9
  11316.     and     r.fkeyid = o2.id
  11317.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11318.     and    o2.id = c2.id    
  11319.     and    c2.colid = r.fkey9
  11320.     union all
  11321.     select
  11322.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11323.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11324.         PK_TABLE_NAME         = o1.name,
  11325.         PK_COLUMN_NAME         = c1.name,
  11326.         PK_COLUMN_GUID        = convert(binary(16),null),
  11327.         PK_COLUMN_PROPID    = convert(int,null),
  11328.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11329.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  11330.         FK_TABLE_NAME         = o2.name,
  11331.         FK_COLUMN_NAME         = c2.name,
  11332.         FK_COLUMN_GUID        = convert(binary(16),null),
  11333.         FK_COLUMN_PROPID    = convert(int,null),
  11334.         ORDINAL            = convert(int,10),
  11335.         UPDATE_RULE        = 'NO ACTION',
  11336.         DELETE_RULE         = 'NO ACTION'        
  11337.     from
  11338.         sysobjects o1, sysobjects o2,
  11339.         syscolumns c1, syscolumns c2,
  11340.         sysreferences r
  11341.     where    
  11342.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  11343.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11344.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11345.     and    o1.id = r.rkeyid
  11346.     and    r.keycnt >= 10
  11347.     and    o1.id = c1.id
  11348.     and    c1.colid = r.rkey10
  11349.     and     r.fkeyid = o2.id
  11350.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11351.     and    o2.id = c2.id    
  11352.     and    c2.colid = r.fkey10
  11353.     union all
  11354.     select
  11355.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11356.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11357.         PK_TABLE_NAME         = o1.name,
  11358.         PK_COLUMN_NAME         = c1.name,
  11359.         PK_COLUMN_GUID        = convert(binary(16),null),
  11360.         PK_COLUMN_PROPID    = convert(int,null),
  11361.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11362.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  11363.         FK_TABLE_NAME         = o2.name,
  11364.         FK_COLUMN_NAME         = c2.name,
  11365.         FK_COLUMN_GUID        = convert(binary(16),null),
  11366.         FK_COLUMN_PROPID    = convert(int,null),
  11367.         ORDINAL            = convert(int,11),
  11368.         UPDATE_RULE        = 'NO ACTION',
  11369.         DELETE_RULE         = 'NO ACTION'        
  11370.     from
  11371.         sysobjects o1, sysobjects o2,
  11372.         syscolumns c1, syscolumns c2,
  11373.         sysreferences r
  11374.     where    
  11375.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  11376.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11377.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11378.     and    o1.id = r.rkeyid
  11379.     and    r.keycnt >= 11
  11380.     and    o1.id = c1.id
  11381.     and    c1.colid = r.rkey11
  11382.     and     r.fkeyid = o2.id
  11383.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11384.     and    o2.id = c2.id    
  11385.     and    c2.colid = r.fkey11
  11386.     union all
  11387.     select
  11388.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11389.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11390.         PK_TABLE_NAME         = o1.name,
  11391.         PK_COLUMN_NAME         = c1.name,
  11392.         PK_COLUMN_GUID        = convert(binary(16),null),
  11393.         PK_COLUMN_PROPID    = convert(int,null),
  11394.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11395.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  11396.         FK_TABLE_NAME         = o2.name,
  11397.         FK_COLUMN_NAME         = c2.name,
  11398.         FK_COLUMN_GUID        = convert(binary(16),null),
  11399.         FK_COLUMN_PROPID    = convert(int,null),
  11400.         ORDINAL            = convert(int,12),
  11401.         UPDATE_RULE        = 'NO ACTION',
  11402.         DELETE_RULE         = 'NO ACTION'        
  11403.     from
  11404.         sysobjects o1, sysobjects o2,
  11405.         syscolumns c1, syscolumns c2,
  11406.         sysreferences r
  11407.     where    
  11408.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  11409.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11410.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11411.     and    o1.id = r.rkeyid
  11412.     and    r.keycnt >= 12
  11413.     and    o1.id = c1.id
  11414.     and    c1.colid = r.rkey12
  11415.     and     r.fkeyid = o2.id
  11416.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11417.     and    o2.id = c2.id    
  11418.     and    c2.colid = r.fkey12
  11419.     union all
  11420.     select
  11421.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11422.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11423.         PK_TABLE_NAME         = o1.name,
  11424.         PK_COLUMN_NAME         = c1.name,
  11425.         PK_COLUMN_GUID        = convert(binary(16),null),
  11426.         PK_COLUMN_PROPID    = convert(int,null),
  11427.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11428.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  11429.         FK_TABLE_NAME         = o2.name,
  11430.         FK_COLUMN_NAME         = c2.name,
  11431.         FK_COLUMN_GUID        = convert(binary(16),null),
  11432.         FK_COLUMN_PROPID    = convert(int,null),
  11433.         ORDINAL            = convert(int,13),
  11434.         UPDATE_RULE        = 'NO ACTION',
  11435.         DELETE_RULE         = 'NO ACTION'        
  11436.     from
  11437.         sysobjects o1, sysobjects o2,
  11438.         syscolumns c1, syscolumns c2,
  11439.         sysreferences r
  11440.     where    
  11441.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  11442.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11443.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11444.     and    o1.id = r.rkeyid
  11445.     and    r.keycnt >= 13
  11446.     and    o1.id = c1.id
  11447.     and    c1.colid = r.rkey13
  11448.     and     r.fkeyid = o2.id
  11449.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11450.     and    o2.id = c2.id    
  11451.     and    c2.colid = r.fkey13
  11452.     union all
  11453.     select
  11454.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11455.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11456.         PK_TABLE_NAME         = o1.name,
  11457.         PK_COLUMN_NAME         = c1.name,
  11458.         PK_COLUMN_GUID        = convert(binary(16),null),
  11459.         PK_COLUMN_PROPID    = convert(int,null),
  11460.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11461.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  11462.         FK_TABLE_NAME         = o2.name,
  11463.         FK_COLUMN_NAME         = c2.name,
  11464.         FK_COLUMN_GUID        = convert(binary(16),null),
  11465.         FK_COLUMN_PROPID    = convert(int,null),
  11466.         ORDINAL            = convert(int,14),
  11467.         UPDATE_RULE        = 'NO ACTION',
  11468.         DELETE_RULE         = 'NO ACTION'        
  11469.     from
  11470.         sysobjects o1, sysobjects o2,
  11471.         syscolumns c1, syscolumns c2,
  11472.         sysreferences r
  11473.     where    
  11474.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  11475.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11476.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11477.     and    o1.id = r.rkeyid
  11478.     and    r.keycnt >= 14
  11479.     and    o1.id = c1.id
  11480.     and    c1.colid = r.rkey14
  11481.     and     r.fkeyid = o2.id
  11482.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11483.     and    o2.id = c2.id    
  11484.     and    c2.colid = r.fkey14
  11485.     union all
  11486.     select
  11487.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11488.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11489.         PK_TABLE_NAME         = o1.name,
  11490.         PK_COLUMN_NAME         = c1.name,
  11491.         PK_COLUMN_GUID        = convert(binary(16),null),
  11492.         PK_COLUMN_PROPID    = convert(int,null),
  11493.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11494.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  11495.         FK_TABLE_NAME         = o2.name,
  11496.         FK_COLUMN_NAME         = c2.name,
  11497.         FK_COLUMN_GUID        = convert(binary(16),null),
  11498.         FK_COLUMN_PROPID    = convert(int,null),
  11499.         ORDINAL            = convert(int,15),
  11500.         UPDATE_RULE        = 'NO ACTION',
  11501.         DELETE_RULE         = 'NO ACTION'        
  11502.     from
  11503.         sysobjects o1, sysobjects o2,
  11504.         syscolumns c1, syscolumns c2,
  11505.         sysreferences r
  11506.     where    
  11507.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  11508.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11509.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11510.     and    o1.id = r.rkeyid
  11511.     and    r.keycnt >= 15
  11512.     and    o1.id = c1.id
  11513.     and    c1.colid = r.rkey15
  11514.     and     r.fkeyid = o2.id
  11515.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11516.     and    o2.id = c2.id    
  11517.     and    c2.colid = r.fkey15
  11518.     union all
  11519.     select
  11520.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11521.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11522.         PK_TABLE_NAME         = o1.name,
  11523.         PK_COLUMN_NAME         = c1.name,
  11524.         PK_COLUMN_GUID        = convert(binary(16),null),
  11525.         PK_COLUMN_PROPID    = convert(int,null),
  11526.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11527.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  11528.         FK_TABLE_NAME         = o2.name,
  11529.         FK_COLUMN_NAME         = c2.name,
  11530.         FK_COLUMN_GUID        = convert(binary(16),null),
  11531.         FK_COLUMN_PROPID    = convert(int,null),
  11532.         ORDINAL            = convert(int,16),
  11533.         UPDATE_RULE        = 'NO ACTION',
  11534.         DELETE_RULE         = 'NO ACTION'        
  11535.     from
  11536.         sysobjects o1, sysobjects o2,
  11537.         syscolumns c1, syscolumns c2,
  11538.         sysreferences r
  11539.     where    
  11540.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  11541.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11542.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11543.     and    o1.id = r.rkeyid
  11544.     and    o1.id = c1.id
  11545.     and    r.keycnt >= 16
  11546.     and    c1.colid = r.rkey16
  11547.     and     r.fkeyid = o2.id
  11548.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11549.     and    o2.id = c2.id    
  11550.     and    c2.colid = r.fkey16
  11551.     order by 8,9,2,3,13
  11552.     END
  11553. go
  11554. CREATE PROCEDURE sp_foreign_keys_rowset;2
  11555.     (
  11556.        @handle            int output,
  11557.        @scrollopt        int output,
  11558.     @ccopt            int output,
  11559.     @rows            int output,
  11560.      @pk_table_name        varchar(255) = null,
  11561.     @pk_table_schema    varchar(255) = null,
  11562.     @pk_table_catalog    varchar(255) = null,
  11563.     @fk_table_name        varchar(255) = null,
  11564.     @fk_table_schema    varchar(255) = null,
  11565.     @fk_table_catalog    varchar(255) = null
  11566.     )
  11567. as
  11568.     declare @ret int
  11569.     
  11570.     create table #spfkeysrowset1
  11571.         (
  11572.         PK_TABLE_CATALOG    varchar(30) not null,
  11573.         PK_TABLE_SCHEMA        varchar(30) not null,
  11574.         PK_TABLE_NAME         varchar(30) not null,
  11575.         PK_COLUMN_NAME         varchar(30) not null,
  11576.         PK_COLUMN_GUID        binary(16) null,
  11577.         PK_COLUMN_PROPID    int null,
  11578.         FK_TABLE_CATALOG    varchar(30) not null,
  11579.         FK_TABLE_SCHEMA         varchar(30) not null,
  11580.         FK_TABLE_NAME         varchar(30) not null,
  11581.         FK_COLUMN_NAME         varchar(30) not null,
  11582.         FK_COLUMN_GUID        binary(16) null,
  11583.         FK_COLUMN_PROPID    int null,
  11584.         ORDINAL            int not null,
  11585.         UPDATE_RULE        varchar(30) not null,
  11586.         DELETE_RULE         varchar(30) not null        
  11587.         )
  11588.  
  11589. IF @pk_table_name is not null
  11590.     BEGIN
  11591.     insert into #spfkeysrowset1
  11592.     select
  11593.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11594.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11595.         PK_TABLE_NAME         = o1.name,
  11596.         PK_COLUMN_NAME         = c1.name,
  11597.         PK_COLUMN_GUID        = convert(binary(16),null),
  11598.         PK_COLUMN_PROPID    = convert(int,null),
  11599.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11600.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  11601.         FK_TABLE_NAME         = o2.name,
  11602.         FK_COLUMN_NAME         = c2.name,
  11603.         FK_COLUMN_GUID        = convert(binary(16),null),
  11604.         FK_COLUMN_PROPID    = convert(int,null),
  11605.         ORDINAL            = convert(int,1),
  11606.         UPDATE_RULE        = 'NO ACTION',
  11607.         DELETE_RULE         = 'NO ACTION'        
  11608.     from
  11609.         sysobjects o1, sysobjects o2,
  11610.         syscolumns c1, syscolumns c2,
  11611.         sysreferences r
  11612.     where    
  11613.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11614.     and    o1.name = @pk_table_name
  11615.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11616.     and    o1.id = r.rkeyid
  11617.     and    o1.id = c1.id
  11618.     and    c1.colid = r.rkey1
  11619.     and     r.fkeyid = o2.id
  11620.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  11621.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11622.     and    o2.id = c2.id    
  11623.     and    c2.colid = r.fkey1
  11624.     union all
  11625.     select
  11626.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11627.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11628.         PK_TABLE_NAME         = o1.name,
  11629.         PK_COLUMN_NAME         = c1.name,
  11630.         PK_COLUMN_GUID        = convert(binary(16),null),
  11631.         PK_COLUMN_PROPID    = convert(int,null),
  11632.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11633.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  11634.         FK_TABLE_NAME         = o2.name,
  11635.         FK_COLUMN_NAME         = c2.name,
  11636.         FK_COLUMN_GUID        = convert(binary(16),null),
  11637.         FK_COLUMN_PROPID    = convert(int,null),
  11638.         ORDINAL            = convert(int,2),
  11639.         UPDATE_RULE        = 'NO ACTION',
  11640.         DELETE_RULE         = 'NO ACTION'        
  11641.     from
  11642.         sysobjects o1, sysobjects o2,
  11643.         syscolumns c1, syscolumns c2,
  11644.         sysreferences r
  11645.     where    
  11646.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11647.     and    o1.name = @pk_table_name
  11648.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11649.     and    o1.id = r.rkeyid
  11650.     and    o1.id = c1.id
  11651.     and    c1.colid = r.rkey2
  11652.     and    r.keycnt >= 2
  11653.     and     r.fkeyid = o2.id
  11654.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  11655.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11656.     and    o2.id = c2.id    
  11657.     and    c2.colid = r.fkey2
  11658.     union all
  11659.     select
  11660.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11661.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11662.         PK_TABLE_NAME         = o1.name,
  11663.         PK_COLUMN_NAME         = c1.name,
  11664.         PK_COLUMN_GUID        = convert(binary(16),null),
  11665.         PK_COLUMN_PROPID    = convert(int,null),
  11666.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11667.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  11668.         FK_TABLE_NAME         = o2.name,
  11669.         FK_COLUMN_NAME         = c2.name,
  11670.         FK_COLUMN_GUID        = convert(binary(16),null),
  11671.         FK_COLUMN_PROPID    = convert(int,null),
  11672.         ORDINAL            = convert(int,3),
  11673.         UPDATE_RULE        = 'NO ACTION',
  11674.         DELETE_RULE         = 'NO ACTION'        
  11675.     from
  11676.         sysobjects o1, sysobjects o2,
  11677.         syscolumns c1, syscolumns c2,
  11678.         sysreferences r
  11679.     where    
  11680.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11681.     and    o1.name = @pk_table_name
  11682.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11683.     and    o1.id = r.rkeyid
  11684.     and    o1.id = c1.id
  11685.     and    c1.colid = r.rkey3
  11686.     and    r.keycnt >= 3
  11687.     and     r.fkeyid = o2.id
  11688.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  11689.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11690.     and    o2.id = c2.id    
  11691.     and    c2.colid = r.fkey3
  11692.     union all
  11693.     select
  11694.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11695.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11696.         PK_TABLE_NAME         = o1.name,
  11697.         PK_COLUMN_NAME         = c1.name,
  11698.         PK_COLUMN_GUID        = convert(binary(16),null),
  11699.         PK_COLUMN_PROPID    = convert(int,null),
  11700.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11701.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  11702.         FK_TABLE_NAME         = o2.name,
  11703.         FK_COLUMN_NAME         = c2.name,
  11704.         FK_COLUMN_GUID        = convert(binary(16),null),
  11705.         FK_COLUMN_PROPID    = convert(int,null),
  11706.         ORDINAL            = convert(int,4),
  11707.         UPDATE_RULE        = 'NO ACTION',
  11708.         DELETE_RULE         = 'NO ACTION'        
  11709.     from
  11710.         sysobjects o1, sysobjects o2,
  11711.         syscolumns c1, syscolumns c2,
  11712.         sysreferences r
  11713.     where    
  11714.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11715.     and    o1.name = @pk_table_name
  11716.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11717.     and    o1.id = r.rkeyid
  11718.     and    r.keycnt >= 4
  11719.     and    o1.id = c1.id
  11720.     and    c1.colid = r.rkey4
  11721.     and     r.fkeyid = o2.id
  11722.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  11723.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11724.     and    o2.id = c2.id    
  11725.     and    c2.colid = r.fkey4
  11726.     union all
  11727.     select
  11728.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11729.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11730.         PK_TABLE_NAME         = o1.name,
  11731.         PK_COLUMN_NAME         = c1.name,
  11732.         PK_COLUMN_GUID        = convert(binary(16),null),
  11733.         PK_COLUMN_PROPID    = convert(int,null),
  11734.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11735.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  11736.         FK_TABLE_NAME         = o2.name,
  11737.         FK_COLUMN_NAME         = c2.name,
  11738.         FK_COLUMN_GUID        = convert(binary(16),null),
  11739.         FK_COLUMN_PROPID    = convert(int,null),
  11740.         ORDINAL            = convert(int,5),
  11741.         UPDATE_RULE        = 'NO ACTION',
  11742.         DELETE_RULE         = 'NO ACTION'        
  11743.     from
  11744.         sysobjects o1, sysobjects o2,
  11745.         syscolumns c1, syscolumns c2,
  11746.         sysreferences r
  11747.     where    
  11748.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11749.     and    o1.name = @pk_table_name
  11750.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11751.     and    o1.id = r.rkeyid
  11752.     and    r.keycnt >= 5
  11753.     and    o1.id = c1.id
  11754.     and    c1.colid = r.rkey5
  11755.     and     r.fkeyid = o2.id
  11756.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  11757.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11758.     and    o2.id = c2.id    
  11759.     and    c2.colid = r.fkey5
  11760.     union all
  11761.     select
  11762.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11763.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11764.         PK_TABLE_NAME         = o1.name,
  11765.         PK_COLUMN_NAME         = c1.name,
  11766.         PK_COLUMN_GUID        = convert(binary(16),null),
  11767.         PK_COLUMN_PROPID    = convert(int,null),
  11768.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11769.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  11770.         FK_TABLE_NAME         = o2.name,
  11771.         FK_COLUMN_NAME         = c2.name,
  11772.         FK_COLUMN_GUID        = convert(binary(16),null),
  11773.         FK_COLUMN_PROPID    = convert(int,null),
  11774.         ORDINAL            = convert(int,6),
  11775.         UPDATE_RULE        = 'NO ACTION',
  11776.         DELETE_RULE         = 'NO ACTION'        
  11777.     from
  11778.         sysobjects o1, sysobjects o2,
  11779.         syscolumns c1, syscolumns c2,
  11780.         sysreferences r
  11781.     where    
  11782.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11783.     and    o1.name = @pk_table_name
  11784.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11785.     and    o1.id = r.rkeyid
  11786.     and    r.keycnt >= 6
  11787.     and    o1.id = c1.id
  11788.     and    c1.colid = r.rkey6
  11789.     and     r.fkeyid = o2.id
  11790.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  11791.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11792.     and    o2.id = c2.id    
  11793.     and    c2.colid = r.fkey6
  11794.     union all
  11795.     select
  11796.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11797.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11798.         PK_TABLE_NAME         = o1.name,
  11799.         PK_COLUMN_NAME         = c1.name,
  11800.         PK_COLUMN_GUID        = convert(binary(16),null),
  11801.         PK_COLUMN_PROPID    = convert(int,null),
  11802.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11803.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  11804.         FK_TABLE_NAME         = o2.name,
  11805.         FK_COLUMN_NAME         = c2.name,
  11806.         FK_COLUMN_GUID        = convert(binary(16),null),
  11807.         FK_COLUMN_PROPID    = convert(int,null),
  11808.         ORDINAL            = convert(int,7),
  11809.         UPDATE_RULE        = 'NO ACTION',
  11810.         DELETE_RULE         = 'NO ACTION'        
  11811.     from
  11812.         sysobjects o1, sysobjects o2,
  11813.         syscolumns c1, syscolumns c2,
  11814.         sysreferences r
  11815.     where    
  11816.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11817.     and    o1.name = @pk_table_name
  11818.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11819.     and    o1.id = r.rkeyid
  11820.     and    r.keycnt >= 7
  11821.     and    o1.id = c1.id
  11822.     and    c1.colid = r.rkey7
  11823.     and     r.fkeyid = o2.id
  11824.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  11825.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11826.     and    o2.id = c2.id    
  11827.     and    c2.colid = r.fkey7
  11828.     union all
  11829.     select
  11830.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11831.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11832.         PK_TABLE_NAME         = o1.name,
  11833.         PK_COLUMN_NAME         = c1.name,
  11834.         PK_COLUMN_GUID        = convert(binary(16),null),
  11835.         PK_COLUMN_PROPID    = convert(int,null),
  11836.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11837.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  11838.         FK_TABLE_NAME         = o2.name,
  11839.         FK_COLUMN_NAME         = c2.name,
  11840.         FK_COLUMN_GUID        = convert(binary(16),null),
  11841.         FK_COLUMN_PROPID    = convert(int,null),
  11842.         ORDINAL            = convert(int,8),
  11843.         UPDATE_RULE        = 'NO ACTION',
  11844.         DELETE_RULE         = 'NO ACTION'        
  11845.     from
  11846.         sysobjects o1, sysobjects o2,
  11847.         syscolumns c1, syscolumns c2,
  11848.         sysreferences r
  11849.     where    
  11850.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11851.     and    o1.name = @pk_table_name
  11852.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11853.     and    o1.id = r.rkeyid
  11854.     and    r.keycnt >= 8
  11855.     and    o1.id = c1.id
  11856.     and    c1.colid = r.rkey8
  11857.     and     r.fkeyid = o2.id
  11858.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  11859.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11860.     and    o2.id = c2.id    
  11861.     and    c2.colid = r.fkey8
  11862.     union all
  11863.     select
  11864.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11865.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11866.         PK_TABLE_NAME         = o1.name,
  11867.         PK_COLUMN_NAME         = c1.name,
  11868.         PK_COLUMN_GUID        = convert(binary(16),null),
  11869.         PK_COLUMN_PROPID    = convert(int,null),
  11870.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11871.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  11872.         FK_TABLE_NAME         = o2.name,
  11873.         FK_COLUMN_NAME         = c2.name,
  11874.         FK_COLUMN_GUID        = convert(binary(16),null),
  11875.         FK_COLUMN_PROPID    = convert(int,null),
  11876.         ORDINAL            = convert(int,9),
  11877.         UPDATE_RULE        = 'NO ACTION',
  11878.         DELETE_RULE         = 'NO ACTION'        
  11879.     from
  11880.         sysobjects o1, sysobjects o2,
  11881.         syscolumns c1, syscolumns c2,
  11882.         sysreferences r
  11883.     where    
  11884.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11885.     and    o1.name = @pk_table_name
  11886.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11887.     and    o1.id = r.rkeyid
  11888.     and    r.keycnt >= 9
  11889.     and    o1.id = c1.id
  11890.     and    c1.colid = r.rkey9
  11891.     and     r.fkeyid = o2.id
  11892.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  11893.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11894.     and    o2.id = c2.id    
  11895.     and    c2.colid = r.fkey9
  11896.     union all
  11897.     select
  11898.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11899.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11900.         PK_TABLE_NAME         = o1.name,
  11901.         PK_COLUMN_NAME         = c1.name,
  11902.         PK_COLUMN_GUID        = convert(binary(16),null),
  11903.         PK_COLUMN_PROPID    = convert(int,null),
  11904.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11905.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  11906.         FK_TABLE_NAME         = o2.name,
  11907.         FK_COLUMN_NAME         = c2.name,
  11908.         FK_COLUMN_GUID        = convert(binary(16),null),
  11909.         FK_COLUMN_PROPID    = convert(int,null),
  11910.         ORDINAL            = convert(int,10),
  11911.         UPDATE_RULE        = 'NO ACTION',
  11912.         DELETE_RULE         = 'NO ACTION'        
  11913.     from
  11914.         sysobjects o1, sysobjects o2,
  11915.         syscolumns c1, syscolumns c2,
  11916.         sysreferences r
  11917.     where    
  11918.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11919.     and    o1.name = @pk_table_name
  11920.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11921.     and    o1.id = r.rkeyid
  11922.     and    r.keycnt >= 10
  11923.     and    o1.id = c1.id
  11924.     and    c1.colid = r.rkey10
  11925.     and     r.fkeyid = o2.id
  11926.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  11927.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11928.     and    o2.id = c2.id    
  11929.     and    c2.colid = r.fkey10
  11930.     union all
  11931.     select
  11932.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11933.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11934.         PK_TABLE_NAME         = o1.name,
  11935.         PK_COLUMN_NAME         = c1.name,
  11936.         PK_COLUMN_GUID        = convert(binary(16),null),
  11937.         PK_COLUMN_PROPID    = convert(int,null),
  11938.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11939.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  11940.         FK_TABLE_NAME         = o2.name,
  11941.         FK_COLUMN_NAME         = c2.name,
  11942.         FK_COLUMN_GUID        = convert(binary(16),null),
  11943.         FK_COLUMN_PROPID    = convert(int,null),
  11944.         ORDINAL            = convert(int,11),
  11945.         UPDATE_RULE        = 'NO ACTION',
  11946.         DELETE_RULE         = 'NO ACTION'        
  11947.     from
  11948.         sysobjects o1, sysobjects o2,
  11949.         syscolumns c1, syscolumns c2,
  11950.         sysreferences r
  11951.     where    
  11952.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11953.     and    o1.name = @pk_table_name
  11954.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11955.     and    o1.id = r.rkeyid
  11956.     and    r.keycnt >= 11
  11957.     and    o1.id = c1.id
  11958.     and    c1.colid = r.rkey11
  11959.     and     r.fkeyid = o2.id
  11960.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  11961.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11962.     and    o2.id = c2.id    
  11963.     and    c2.colid = r.fkey11
  11964.     union all
  11965.     select
  11966.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11967.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11968.         PK_TABLE_NAME         = o1.name,
  11969.         PK_COLUMN_NAME         = c1.name,
  11970.         PK_COLUMN_GUID        = convert(binary(16),null),
  11971.         PK_COLUMN_PROPID    = convert(int,null),
  11972.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11973.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  11974.         FK_TABLE_NAME         = o2.name,
  11975.         FK_COLUMN_NAME         = c2.name,
  11976.         FK_COLUMN_GUID        = convert(binary(16),null),
  11977.         FK_COLUMN_PROPID    = convert(int,null),
  11978.         ORDINAL            = convert(int,12),
  11979.         UPDATE_RULE        = 'NO ACTION',
  11980.         DELETE_RULE         = 'NO ACTION'        
  11981.     from
  11982.         sysobjects o1, sysobjects o2,
  11983.         syscolumns c1, syscolumns c2,
  11984.         sysreferences r
  11985.     where    
  11986.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11987.     and    o1.name = @pk_table_name
  11988.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11989.     and    o1.id = r.rkeyid
  11990.     and    r.keycnt >= 12
  11991.     and    o1.id = c1.id
  11992.     and    c1.colid = r.rkey12
  11993.     and     r.fkeyid = o2.id
  11994.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  11995.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11996.     and    o2.id = c2.id    
  11997.     and    c2.colid = r.fkey12
  11998.     union all
  11999.     select
  12000.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12001.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12002.         PK_TABLE_NAME         = o1.name,
  12003.         PK_COLUMN_NAME         = c1.name,
  12004.         PK_COLUMN_GUID        = convert(binary(16),null),
  12005.         PK_COLUMN_PROPID    = convert(int,null),
  12006.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12007.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  12008.         FK_TABLE_NAME         = o2.name,
  12009.         FK_COLUMN_NAME         = c2.name,
  12010.         FK_COLUMN_GUID        = convert(binary(16),null),
  12011.         FK_COLUMN_PROPID    = convert(int,null),
  12012.         ORDINAL            = convert(int,13),
  12013.         UPDATE_RULE        = 'NO ACTION',
  12014.         DELETE_RULE         = 'NO ACTION'        
  12015.     from
  12016.         sysobjects o1, sysobjects o2,
  12017.         syscolumns c1, syscolumns c2,
  12018.         sysreferences r
  12019.     where    
  12020.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12021.     and    o1.name = @pk_table_name
  12022.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12023.     and    o1.id = r.rkeyid
  12024.     and    r.keycnt >= 13
  12025.     and    o1.id = c1.id
  12026.     and    c1.colid = r.rkey13
  12027.     and     r.fkeyid = o2.id
  12028.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  12029.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12030.     and    o2.id = c2.id    
  12031.     and    c2.colid = r.fkey13
  12032.     union all
  12033.     select
  12034.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12035.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12036.         PK_TABLE_NAME         = o1.name,
  12037.         PK_COLUMN_NAME         = c1.name,
  12038.         PK_COLUMN_GUID        = convert(binary(16),null),
  12039.         PK_COLUMN_PROPID    = convert(int,null),
  12040.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12041.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  12042.         FK_TABLE_NAME         = o2.name,
  12043.         FK_COLUMN_NAME         = c2.name,
  12044.         FK_COLUMN_GUID        = convert(binary(16),null),
  12045.         FK_COLUMN_PROPID    = convert(int,null),
  12046.         ORDINAL            = convert(int,14),
  12047.         UPDATE_RULE        = 'NO ACTION',
  12048.         DELETE_RULE         = 'NO ACTION'        
  12049.     from
  12050.         sysobjects o1, sysobjects o2,
  12051.         syscolumns c1, syscolumns c2,
  12052.         sysreferences r
  12053.     where    
  12054.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12055.     and    o1.name = @pk_table_name
  12056.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12057.     and    o1.id = r.rkeyid
  12058.     and    r.keycnt >= 14
  12059.     and    o1.id = c1.id
  12060.     and    c1.colid = r.rkey14
  12061.     and     r.fkeyid = o2.id
  12062.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  12063.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12064.     and    o2.id = c2.id    
  12065.     and    c2.colid = r.fkey14
  12066.     union all
  12067.     select
  12068.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12069.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12070.         PK_TABLE_NAME         = o1.name,
  12071.         PK_COLUMN_NAME         = c1.name,
  12072.         PK_COLUMN_GUID        = convert(binary(16),null),
  12073.         PK_COLUMN_PROPID    = convert(int,null),
  12074.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12075.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  12076.         FK_TABLE_NAME         = o2.name,
  12077.         FK_COLUMN_NAME         = c2.name,
  12078.         FK_COLUMN_GUID        = convert(binary(16),null),
  12079.         FK_COLUMN_PROPID    = convert(int,null),
  12080.         ORDINAL            = convert(int,15),
  12081.         UPDATE_RULE        = 'NO ACTION',
  12082.         DELETE_RULE         = 'NO ACTION'        
  12083.     from
  12084.         sysobjects o1, sysobjects o2,
  12085.         syscolumns c1, syscolumns c2,
  12086.         sysreferences r
  12087.     where    
  12088.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12089.     and    o1.name = @pk_table_name
  12090.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12091.     and    o1.id = r.rkeyid
  12092.     and    r.keycnt >= 15
  12093.     and    o1.id = c1.id
  12094.     and    c1.colid = r.rkey15
  12095.     and     r.fkeyid = o2.id
  12096.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  12097.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12098.     and    o2.id = c2.id    
  12099.     and    c2.colid = r.fkey15
  12100.     union all
  12101.     select
  12102.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12103.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12104.         PK_TABLE_NAME         = o1.name,
  12105.         PK_COLUMN_NAME         = c1.name,
  12106.         PK_COLUMN_GUID        = convert(binary(16),null),
  12107.         PK_COLUMN_PROPID    = convert(int,null),
  12108.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12109.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  12110.         FK_TABLE_NAME         = o2.name,
  12111.         FK_COLUMN_NAME         = c2.name,
  12112.         FK_COLUMN_GUID        = convert(binary(16),null),
  12113.         FK_COLUMN_PROPID    = convert(int,null),
  12114.         ORDINAL            = convert(int,16),
  12115.         UPDATE_RULE        = 'NO ACTION',
  12116.         DELETE_RULE         = 'NO ACTION'        
  12117.     from
  12118.         sysobjects o1, sysobjects o2,
  12119.         syscolumns c1, syscolumns c2,
  12120.         sysreferences r
  12121.     where    
  12122.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12123.     and    o1.name = @pk_table_name
  12124.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12125.     and    o1.id = r.rkeyid
  12126.     and    r.keycnt >= 16
  12127.     and    o1.id = c1.id
  12128.     and    c1.colid = r.rkey16
  12129.     and     r.fkeyid = o2.id
  12130.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  12131.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12132.     and    o2.id = c2.id    
  12133.     and    c2.colid = r.fkey16
  12134.     order by 8,9,2,3,13
  12135.     END
  12136. ELSE IF @fk_table_name is not null
  12137.     BEGIN
  12138.     insert into #spfkeysrowset1
  12139.     select
  12140.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12141.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12142.         PK_TABLE_NAME         = o1.name,
  12143.         PK_COLUMN_NAME         = c1.name,
  12144.         PK_COLUMN_GUID        = convert(binary(16),null),
  12145.         PK_COLUMN_PROPID    = convert(int,null),
  12146.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12147.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  12148.         FK_TABLE_NAME         = o2.name,
  12149.         FK_COLUMN_NAME         = c2.name,
  12150.         FK_COLUMN_GUID        = convert(binary(16),null),
  12151.         FK_COLUMN_PROPID    = convert(int,null),
  12152.         ORDINAL            = convert(int,1),
  12153.         UPDATE_RULE        = 'NO ACTION',
  12154.         DELETE_RULE         = 'NO ACTION'        
  12155.     from
  12156.         sysobjects o1, sysobjects o2,
  12157.         syscolumns c1, syscolumns c2,
  12158.         sysreferences r
  12159.     where    
  12160.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12161.     and    o2.name = @fk_table_name
  12162.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12163.     and    o2.id = r.fkeyid
  12164.     and    o2.id = c2.id
  12165.     and    c2.colid = r.fkey1
  12166.     and     r.rkeyid = o1.id
  12167.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  12168.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12169.     and    o1.id = c1.id    
  12170.     and    c1.colid = r.rkey1
  12171.     union    all
  12172.     select
  12173.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12174.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12175.         PK_TABLE_NAME         = o1.name,
  12176.         PK_COLUMN_NAME         = c1.name,
  12177.         PK_COLUMN_GUID        = convert(binary(16),null),
  12178.         PK_COLUMN_PROPID    = convert(int,null),
  12179.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12180.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  12181.         FK_TABLE_NAME         = o2.name,
  12182.         FK_COLUMN_NAME         = c2.name,
  12183.         FK_COLUMN_GUID        = convert(binary(16),null),
  12184.         FK_COLUMN_PROPID    = convert(int,null),
  12185.         ORDINAL            = convert(int,2),
  12186.         UPDATE_RULE        = 'NO ACTION',
  12187.         DELETE_RULE         = 'NO ACTION'        
  12188.     from
  12189.         sysobjects o1, sysobjects o2,
  12190.         syscolumns c1, syscolumns c2,
  12191.         sysreferences r
  12192.     where    
  12193.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12194.     and    o2.name = @fk_table_name
  12195.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12196.     and    o2.id = r.fkeyid
  12197.     and    r.keycnt >= 2
  12198.     and    o2.id = c2.id
  12199.     and    c2.colid = r.fkey2
  12200.     and     r.rkeyid = o1.id
  12201.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  12202.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12203.     and    o1.id = c1.id    
  12204.     and    c1.colid = r.rkey2
  12205.     union    all
  12206.     select
  12207.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12208.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12209.         PK_TABLE_NAME         = o1.name,
  12210.         PK_COLUMN_NAME         = c1.name,
  12211.         PK_COLUMN_GUID        = convert(binary(16),null),
  12212.         PK_COLUMN_PROPID    = convert(int,null),
  12213.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12214.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  12215.         FK_TABLE_NAME         = o2.name,
  12216.         FK_COLUMN_NAME         = c2.name,
  12217.         FK_COLUMN_GUID        = convert(binary(16),null),
  12218.         FK_COLUMN_PROPID    = convert(int,null),
  12219.         ORDINAL            = convert(int,3),
  12220.         UPDATE_RULE        = 'NO ACTION',
  12221.         DELETE_RULE         = 'NO ACTION'        
  12222.     from
  12223.         sysobjects o1, sysobjects o2,
  12224.         syscolumns c1, syscolumns c2,
  12225.         sysreferences r
  12226.     where    
  12227.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12228.     and    o2.name = @fk_table_name
  12229.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12230.     and    o2.id = r.fkeyid
  12231.     and    r.keycnt >= 3
  12232.     and    o2.id = c2.id
  12233.     and    c2.colid = r.fkey3
  12234.     and     r.rkeyid = o1.id
  12235.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  12236.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12237.     and    o1.id = c1.id    
  12238.     and    c1.colid = r.rkey3
  12239.     union    all
  12240.     select
  12241.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12242.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12243.         PK_TABLE_NAME         = o1.name,
  12244.         PK_COLUMN_NAME         = c1.name,
  12245.         PK_COLUMN_GUID        = convert(binary(16),null),
  12246.         PK_COLUMN_PROPID    = convert(int,null),
  12247.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12248.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  12249.         FK_TABLE_NAME         = o2.name,
  12250.         FK_COLUMN_NAME         = c2.name,
  12251.         FK_COLUMN_GUID        = convert(binary(16),null),
  12252.         FK_COLUMN_PROPID    = convert(int,null),
  12253.         ORDINAL            = convert(int,4),
  12254.         UPDATE_RULE        = 'NO ACTION',
  12255.         DELETE_RULE         = 'NO ACTION'        
  12256.     from
  12257.         sysobjects o1, sysobjects o2,
  12258.         syscolumns c1, syscolumns c2,
  12259.         sysreferences r
  12260.     where    
  12261.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12262.     and    o2.name = @fk_table_name
  12263.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12264.     and    o2.id = r.fkeyid
  12265.     and    r.keycnt >= 4
  12266.     and    o2.id = c2.id
  12267.     and    c2.colid = r.fkey4
  12268.     and     r.rkeyid = o1.id
  12269.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  12270.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12271.     and    o1.id = c1.id    
  12272.     and    c1.colid = r.rkey4
  12273.     union    all
  12274.     select
  12275.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12276.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12277.         PK_TABLE_NAME         = o1.name,
  12278.         PK_COLUMN_NAME         = c1.name,
  12279.         PK_COLUMN_GUID        = convert(binary(16),null),
  12280.         PK_COLUMN_PROPID    = convert(int,null),
  12281.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12282.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  12283.         FK_TABLE_NAME         = o2.name,
  12284.         FK_COLUMN_NAME         = c2.name,
  12285.         FK_COLUMN_GUID        = convert(binary(16),null),
  12286.         FK_COLUMN_PROPID    = convert(int,null),
  12287.         ORDINAL            = convert(int,5),
  12288.         UPDATE_RULE        = 'NO ACTION',
  12289.         DELETE_RULE         = 'NO ACTION'        
  12290.     from
  12291.         sysobjects o1, sysobjects o2,
  12292.         syscolumns c1, syscolumns c2,
  12293.         sysreferences r
  12294.     where    
  12295.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12296.     and    o2.name = @fk_table_name
  12297.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12298.     and    o2.id = r.fkeyid
  12299.     and    r.keycnt >= 5
  12300.     and    o2.id = c2.id
  12301.     and    c2.colid = r.fkey5
  12302.     and     r.rkeyid = o1.id
  12303.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  12304.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12305.     and    o1.id = c1.id    
  12306.     and    c1.colid = r.rkey5
  12307.     union    all
  12308.     select
  12309.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12310.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12311.         PK_TABLE_NAME         = o1.name,
  12312.         PK_COLUMN_NAME         = c1.name,
  12313.         PK_COLUMN_GUID        = convert(binary(16),null),
  12314.         PK_COLUMN_PROPID    = convert(int,null),
  12315.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12316.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  12317.         FK_TABLE_NAME         = o2.name,
  12318.         FK_COLUMN_NAME         = c2.name,
  12319.         FK_COLUMN_GUID        = convert(binary(16),null),
  12320.         FK_COLUMN_PROPID    = convert(int,null),
  12321.         ORDINAL            = convert(int,6),
  12322.         UPDATE_RULE        = 'NO ACTION',
  12323.         DELETE_RULE         = 'NO ACTION'        
  12324.     from
  12325.         sysobjects o1, sysobjects o2,
  12326.         syscolumns c1, syscolumns c2,
  12327.         sysreferences r
  12328.     where    
  12329.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12330.     and    o2.name = @fk_table_name
  12331.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12332.     and    o2.id = r.fkeyid
  12333.     and    r.keycnt >= 6
  12334.     and    o2.id = c2.id
  12335.     and    c2.colid = r.fkey6
  12336.     and     r.rkeyid = o1.id
  12337.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  12338.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12339.     and    o1.id = c1.id    
  12340.     and    c1.colid = r.rkey6
  12341.     union    all
  12342.     select
  12343.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12344.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12345.         PK_TABLE_NAME         = o1.name,
  12346.         PK_COLUMN_NAME         = c1.name,
  12347.         PK_COLUMN_GUID        = convert(binary(16),null),
  12348.         PK_COLUMN_PROPID    = convert(int,null),
  12349.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12350.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  12351.         FK_TABLE_NAME         = o2.name,
  12352.         FK_COLUMN_NAME         = c2.name,
  12353.         FK_COLUMN_GUID        = convert(binary(16),null),
  12354.         FK_COLUMN_PROPID    = convert(int,null),
  12355.         ORDINAL            = convert(int,7),
  12356.         UPDATE_RULE        = 'NO ACTION',
  12357.         DELETE_RULE         = 'NO ACTION'        
  12358.     from
  12359.         sysobjects o1, sysobjects o2,
  12360.         syscolumns c1, syscolumns c2,
  12361.         sysreferences r
  12362.     where    
  12363.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12364.     and    o2.name = @fk_table_name
  12365.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12366.     and    o2.id = r.fkeyid
  12367.     and    r.keycnt >= 7
  12368.     and    o2.id = c2.id
  12369.     and    c2.colid = r.fkey7
  12370.     and     r.rkeyid = o1.id
  12371.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  12372.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12373.     and    o1.id = c1.id    
  12374.     and    c1.colid = r.rkey7
  12375.     union    all
  12376.     select
  12377.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12378.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12379.         PK_TABLE_NAME         = o1.name,
  12380.         PK_COLUMN_NAME         = c1.name,
  12381.         PK_COLUMN_GUID        = convert(binary(16),null),
  12382.         PK_COLUMN_PROPID    = convert(int,null),
  12383.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12384.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  12385.         FK_TABLE_NAME         = o2.name,
  12386.         FK_COLUMN_NAME         = c2.name,
  12387.         FK_COLUMN_GUID        = convert(binary(16),null),
  12388.         FK_COLUMN_PROPID    = convert(int,null),
  12389.         ORDINAL            = convert(int,8),
  12390.         UPDATE_RULE        = 'NO ACTION',
  12391.         DELETE_RULE         = 'NO ACTION'        
  12392.     from
  12393.         sysobjects o1, sysobjects o2,
  12394.         syscolumns c1, syscolumns c2,
  12395.         sysreferences r
  12396.     where    
  12397.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12398.     and    o2.name = @fk_table_name
  12399.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12400.     and    o2.id = r.fkeyid
  12401.     and    r.keycnt >= 8
  12402.     and    o2.id = c2.id
  12403.     and    c2.colid = r.fkey8
  12404.     and     r.rkeyid = o1.id
  12405.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  12406.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12407.     and    o1.id = c1.id    
  12408.     and    c1.colid = r.rkey8
  12409.     union    all
  12410.     select
  12411.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12412.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12413.         PK_TABLE_NAME         = o1.name,
  12414.         PK_COLUMN_NAME         = c1.name,
  12415.         PK_COLUMN_GUID        = convert(binary(16),null),
  12416.         PK_COLUMN_PROPID    = convert(int,null),
  12417.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12418.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  12419.         FK_TABLE_NAME         = o2.name,
  12420.         FK_COLUMN_NAME         = c2.name,
  12421.         FK_COLUMN_GUID        = convert(binary(16),null),
  12422.         FK_COLUMN_PROPID    = convert(int,null),
  12423.         ORDINAL            = convert(int,9),
  12424.         UPDATE_RULE        = 'NO ACTION',
  12425.         DELETE_RULE         = 'NO ACTION'        
  12426.     from
  12427.         sysobjects o1, sysobjects o2,
  12428.         syscolumns c1, syscolumns c2,
  12429.         sysreferences r
  12430.     where    
  12431.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12432.     and    o2.name = @fk_table_name
  12433.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12434.     and    o2.id = r.fkeyid
  12435.     and    r.keycnt >= 9
  12436.     and    o2.id = c2.id
  12437.     and    c2.colid = r.fkey9
  12438.     and     r.rkeyid = o1.id
  12439.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  12440.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12441.     and    o1.id = c1.id    
  12442.     and    c1.colid = r.rkey9
  12443.     union    all
  12444.     select
  12445.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12446.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12447.         PK_TABLE_NAME         = o1.name,
  12448.         PK_COLUMN_NAME         = c1.name,
  12449.         PK_COLUMN_GUID        = convert(binary(16),null),
  12450.         PK_COLUMN_PROPID    = convert(int,null),
  12451.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12452.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  12453.         FK_TABLE_NAME         = o2.name,
  12454.         FK_COLUMN_NAME         = c2.name,
  12455.         FK_COLUMN_GUID        = convert(binary(16),null),
  12456.         FK_COLUMN_PROPID    = convert(int,null),
  12457.         ORDINAL            = convert(int,10),
  12458.         UPDATE_RULE        = 'NO ACTION',
  12459.         DELETE_RULE         = 'NO ACTION'        
  12460.     from
  12461.         sysobjects o1, sysobjects o2,
  12462.         syscolumns c1, syscolumns c2,
  12463.         sysreferences r
  12464.     where    
  12465.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12466.     and    o2.name = @fk_table_name
  12467.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12468.     and    o2.id = r.fkeyid
  12469.     and    r.keycnt >= 10
  12470.     and    o2.id = c2.id
  12471.     and    c2.colid = r.fkey10
  12472.     and     r.rkeyid = o1.id
  12473.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  12474.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12475.     and    o1.id = c1.id    
  12476.     and    c1.colid = r.rkey10
  12477.     union    all
  12478.     select
  12479.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12480.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12481.         PK_TABLE_NAME         = o1.name,
  12482.         PK_COLUMN_NAME         = c1.name,
  12483.         PK_COLUMN_GUID        = convert(binary(16),null),
  12484.         PK_COLUMN_PROPID    = convert(int,null),
  12485.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12486.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  12487.         FK_TABLE_NAME         = o2.name,
  12488.         FK_COLUMN_NAME         = c2.name,
  12489.         FK_COLUMN_GUID        = convert(binary(16),null),
  12490.         FK_COLUMN_PROPID    = convert(int,null),
  12491.         ORDINAL            = convert(int,11),
  12492.         UPDATE_RULE        = 'NO ACTION',
  12493.         DELETE_RULE         = 'NO ACTION'        
  12494.     from
  12495.         sysobjects o1, sysobjects o2,
  12496.         syscolumns c1, syscolumns c2,
  12497.         sysreferences r
  12498.     where    
  12499.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12500.     and    o2.name = @fk_table_name
  12501.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12502.     and    o2.id = r.fkeyid
  12503.     and    r.keycnt >= 11
  12504.     and    o2.id = c2.id
  12505.     and    c2.colid = r.fkey11
  12506.     and     r.rkeyid = o1.id
  12507.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  12508.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12509.     and    o1.id = c1.id    
  12510.     and    c1.colid = r.rkey11
  12511.     union    all
  12512.     select
  12513.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12514.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12515.         PK_TABLE_NAME         = o1.name,
  12516.         PK_COLUMN_NAME         = c1.name,
  12517.         PK_COLUMN_GUID        = convert(binary(16),null),
  12518.         PK_COLUMN_PROPID    = convert(int,null),
  12519.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12520.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  12521.         FK_TABLE_NAME         = o2.name,
  12522.         FK_COLUMN_NAME         = c2.name,
  12523.         FK_COLUMN_GUID        = convert(binary(16),null),
  12524.         FK_COLUMN_PROPID    = convert(int,null),
  12525.         ORDINAL            = convert(int,12),
  12526.         UPDATE_RULE        = 'NO ACTION',
  12527.         DELETE_RULE         = 'NO ACTION'        
  12528.     from
  12529.         sysobjects o1, sysobjects o2,
  12530.         syscolumns c1, syscolumns c2,
  12531.         sysreferences r
  12532.     where    
  12533.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12534.     and    o2.name = @fk_table_name
  12535.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12536.     and    o2.id = r.fkeyid
  12537.     and    r.keycnt >= 12
  12538.     and    o2.id = c2.id
  12539.     and    c2.colid = r.fkey12
  12540.     and     r.rkeyid = o1.id
  12541.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  12542.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12543.     and    o1.id = c1.id    
  12544.     and    c1.colid = r.rkey12
  12545.     union    all
  12546.     select
  12547.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12548.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12549.         PK_TABLE_NAME         = o1.name,
  12550.         PK_COLUMN_NAME         = c1.name,
  12551.         PK_COLUMN_GUID        = convert(binary(16),null),
  12552.         PK_COLUMN_PROPID    = convert(int,null),
  12553.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12554.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  12555.         FK_TABLE_NAME         = o2.name,
  12556.         FK_COLUMN_NAME         = c2.name,
  12557.         FK_COLUMN_GUID        = convert(binary(16),null),
  12558.         FK_COLUMN_PROPID    = convert(int,null),
  12559.         ORDINAL            = convert(int,13),
  12560.         UPDATE_RULE        = 'NO ACTION',
  12561.         DELETE_RULE         = 'NO ACTION'        
  12562.     from
  12563.         sysobjects o1, sysobjects o2,
  12564.         syscolumns c1, syscolumns c2,
  12565.         sysreferences r
  12566.     where    
  12567.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12568.     and    o2.name = @fk_table_name
  12569.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12570.     and    o2.id = r.fkeyid
  12571.     and    r.keycnt >= 13
  12572.     and    o2.id = c2.id
  12573.     and    c2.colid = r.fkey13
  12574.     and     r.rkeyid = o1.id
  12575.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  12576.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12577.     and    o1.id = c1.id    
  12578.     and    c1.colid = r.rkey13
  12579.     union    all
  12580.     select
  12581.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12582.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12583.         PK_TABLE_NAME         = o1.name,
  12584.         PK_COLUMN_NAME         = c1.name,
  12585.         PK_COLUMN_GUID        = convert(binary(16),null),
  12586.         PK_COLUMN_PROPID    = convert(int,null),
  12587.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12588.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  12589.         FK_TABLE_NAME         = o2.name,
  12590.         FK_COLUMN_NAME         = c2.name,
  12591.         FK_COLUMN_GUID        = convert(binary(16),null),
  12592.         FK_COLUMN_PROPID    = convert(int,null),
  12593.         ORDINAL            = convert(int,14),
  12594.         UPDATE_RULE        = 'NO ACTION',
  12595.         DELETE_RULE         = 'NO ACTION'        
  12596.     from
  12597.         sysobjects o1, sysobjects o2,
  12598.         syscolumns c1, syscolumns c2,
  12599.         sysreferences r
  12600.     where    
  12601.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12602.     and    o2.name = @fk_table_name
  12603.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12604.     and    o2.id = r.fkeyid
  12605.     and    r.keycnt >= 14
  12606.     and    o2.id = c2.id
  12607.     and    c2.colid = r.fkey14
  12608.     and     r.rkeyid = o1.id
  12609.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  12610.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12611.     and    o1.id = c1.id    
  12612.     and    c1.colid = r.rkey14
  12613.     union    all
  12614.     select
  12615.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12616.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12617.         PK_TABLE_NAME         = o1.name,
  12618.         PK_COLUMN_NAME         = c1.name,
  12619.         PK_COLUMN_GUID        = convert(binary(16),null),
  12620.         PK_COLUMN_PROPID    = convert(int,null),
  12621.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12622.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  12623.         FK_TABLE_NAME         = o2.name,
  12624.         FK_COLUMN_NAME         = c2.name,
  12625.         FK_COLUMN_GUID        = convert(binary(16),null),
  12626.         FK_COLUMN_PROPID    = convert(int,null),
  12627.         ORDINAL            = convert(int,15),
  12628.         UPDATE_RULE        = 'NO ACTION',
  12629.         DELETE_RULE         = 'NO ACTION'        
  12630.     from
  12631.         sysobjects o1, sysobjects o2,
  12632.         syscolumns c1, syscolumns c2,
  12633.         sysreferences r
  12634.     where    
  12635.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12636.     and    o2.name = @fk_table_name
  12637.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12638.     and    o2.id = r.fkeyid
  12639.     and    r.keycnt >= 15
  12640.     and    o2.id = c2.id
  12641.     and    c2.colid = r.fkey15
  12642.     and     r.rkeyid = o1.id
  12643.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  12644.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12645.     and    o1.id = c1.id    
  12646.     and    c1.colid = r.rkey15
  12647.     union    all
  12648.     select
  12649.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12650.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12651.         PK_TABLE_NAME         = o1.name,
  12652.         PK_COLUMN_NAME         = c1.name,
  12653.         PK_COLUMN_GUID        = convert(binary(16),null),
  12654.         PK_COLUMN_PROPID    = convert(int,null),
  12655.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12656.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  12657.         FK_TABLE_NAME         = o2.name,
  12658.         FK_COLUMN_NAME         = c2.name,
  12659.         FK_COLUMN_GUID        = convert(binary(16),null),
  12660.         FK_COLUMN_PROPID    = convert(int,null),
  12661.         ORDINAL            = convert(int,16),
  12662.         UPDATE_RULE        = 'NO ACTION',
  12663.         DELETE_RULE         = 'NO ACTION'        
  12664.     from
  12665.         sysobjects o1, sysobjects o2,
  12666.         syscolumns c1, syscolumns c2,
  12667.         sysreferences r
  12668.     where    
  12669.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12670.     and    o2.name = @fk_table_name
  12671.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12672.     and    o2.id = r.fkeyid
  12673.     and    r.keycnt >= 16
  12674.     and    o2.id = c2.id
  12675.     and    c2.colid = r.fkey16
  12676.     and     r.rkeyid = o1.id
  12677.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  12678.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12679.     and    o1.id = c1.id    
  12680.     and    c1.colid = r.rkey16
  12681.     order by 8,9,2,3,13
  12682.     END
  12683. ELSE
  12684.     BEGIN
  12685.     insert into #spfkeysrowset1
  12686.     select
  12687.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12688.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12689.         PK_TABLE_NAME         = o1.name,
  12690.         PK_COLUMN_NAME         = c1.name,
  12691.         PK_COLUMN_GUID        = convert(binary(16),null),
  12692.         PK_COLUMN_PROPID    = convert(int,null),
  12693.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12694.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  12695.         FK_TABLE_NAME         = o2.name,
  12696.         FK_COLUMN_NAME         = c2.name,
  12697.         FK_COLUMN_GUID        = convert(binary(16),null),
  12698.         FK_COLUMN_PROPID    = convert(int,null),
  12699.         ORDINAL            = convert(int,1),
  12700.         UPDATE_RULE        = 'NO ACTION',
  12701.         DELETE_RULE         = 'NO ACTION'        
  12702.     from
  12703.         sysobjects o1, sysobjects o2,
  12704.         syscolumns c1, syscolumns c2,
  12705.         sysreferences r
  12706.     where    
  12707.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12708.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12709.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12710.     and    o1.id = r.rkeyid
  12711.     and    o1.id = c1.id
  12712.     and    c1.colid = r.rkey1
  12713.     and     r.fkeyid = o2.id
  12714.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12715.     and    o2.id = c2.id    
  12716.     and    c2.colid = r.fkey1
  12717.     union all
  12718.     select
  12719.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12720.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12721.         PK_TABLE_NAME         = o1.name,
  12722.         PK_COLUMN_NAME         = c1.name,
  12723.         PK_COLUMN_GUID        = convert(binary(16),null),
  12724.         PK_COLUMN_PROPID    = convert(int,null),
  12725.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12726.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  12727.         FK_TABLE_NAME         = o2.name,
  12728.         FK_COLUMN_NAME         = c2.name,
  12729.         FK_COLUMN_GUID        = convert(binary(16),null),
  12730.         FK_COLUMN_PROPID    = convert(int,null),
  12731.         ORDINAL            = convert(int,2),
  12732.         UPDATE_RULE        = 'NO ACTION',
  12733.         DELETE_RULE         = 'NO ACTION'        
  12734.     from
  12735.         sysobjects o1, sysobjects o2,
  12736.         syscolumns c1, syscolumns c2,
  12737.         sysreferences r
  12738.     where    
  12739.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12740.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12741.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12742.     and    o1.id = r.rkeyid
  12743.     and    r.keycnt >= 2
  12744.     and    o1.id = c1.id
  12745.     and    c1.colid = r.rkey2
  12746.     and     r.fkeyid = o2.id
  12747.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12748.     and    o2.id = c2.id    
  12749.     and    c2.colid = r.fkey2
  12750.     union all
  12751.     select
  12752.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12753.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12754.         PK_TABLE_NAME         = o1.name,
  12755.         PK_COLUMN_NAME         = c1.name,
  12756.         PK_COLUMN_GUID        = convert(binary(16),null),
  12757.         PK_COLUMN_PROPID    = convert(int,null),
  12758.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12759.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  12760.         FK_TABLE_NAME         = o2.name,
  12761.         FK_COLUMN_NAME         = c2.name,
  12762.         FK_COLUMN_GUID        = convert(binary(16),null),
  12763.         FK_COLUMN_PROPID    = convert(int,null),
  12764.         ORDINAL            = convert(int,3),
  12765.         UPDATE_RULE        = 'NO ACTION',
  12766.         DELETE_RULE         = 'NO ACTION'        
  12767.     from
  12768.         sysobjects o1, sysobjects o2,
  12769.         syscolumns c1, syscolumns c2,
  12770.         sysreferences r
  12771.     where    
  12772.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12773.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12774.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12775.     and    o1.id = r.rkeyid
  12776.     and    r.keycnt >= 3
  12777.     and    o1.id = c1.id
  12778.     and    c1.colid = r.rkey3
  12779.     and     r.fkeyid = o2.id
  12780.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12781.     and    o2.id = c2.id    
  12782.     and    c2.colid = r.fkey3
  12783.     union all
  12784.     select
  12785.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12786.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12787.         PK_TABLE_NAME         = o1.name,
  12788.         PK_COLUMN_NAME         = c1.name,
  12789.         PK_COLUMN_GUID        = convert(binary(16),null),
  12790.         PK_COLUMN_PROPID    = convert(int,null),
  12791.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12792.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  12793.         FK_TABLE_NAME         = o2.name,
  12794.         FK_COLUMN_NAME         = c2.name,
  12795.         FK_COLUMN_GUID        = convert(binary(16),null),
  12796.         FK_COLUMN_PROPID    = convert(int,null),
  12797.         ORDINAL            = convert(int,4),
  12798.         UPDATE_RULE        = 'NO ACTION',
  12799.         DELETE_RULE         = 'NO ACTION'        
  12800.     from
  12801.         sysobjects o1, sysobjects o2,
  12802.         syscolumns c1, syscolumns c2,
  12803.         sysreferences r
  12804.     where    
  12805.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12806.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12807.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12808.     and    o1.id = r.rkeyid
  12809.     and    r.keycnt >= 4
  12810.     and    o1.id = c1.id
  12811.     and    c1.colid = r.rkey4
  12812.     and     r.fkeyid = o2.id
  12813.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12814.     and    o2.id = c2.id    
  12815.     and    c2.colid = r.fkey4
  12816.     union all
  12817.     select
  12818.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12819.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12820.         PK_TABLE_NAME         = o1.name,
  12821.         PK_COLUMN_NAME         = c1.name,
  12822.         PK_COLUMN_GUID        = convert(binary(16),null),
  12823.         PK_COLUMN_PROPID    = convert(int,null),
  12824.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12825.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  12826.         FK_TABLE_NAME         = o2.name,
  12827.         FK_COLUMN_NAME         = c2.name,
  12828.         FK_COLUMN_GUID        = convert(binary(16),null),
  12829.         FK_COLUMN_PROPID    = convert(int,null),
  12830.         ORDINAL            = convert(int,5),
  12831.         UPDATE_RULE        = 'NO ACTION',
  12832.         DELETE_RULE         = 'NO ACTION'        
  12833.     from
  12834.         sysobjects o1, sysobjects o2,
  12835.         syscolumns c1, syscolumns c2,
  12836.         sysreferences r
  12837.     where    
  12838.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12839.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12840.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12841.     and    o1.id = r.rkeyid
  12842.     and    r.keycnt >= 5
  12843.     and    o1.id = c1.id
  12844.     and    c1.colid = r.rkey5
  12845.     and     r.fkeyid = o2.id
  12846.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12847.     and    o2.id = c2.id    
  12848.     and    c2.colid = r.fkey5
  12849.     union all
  12850.     select
  12851.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12852.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12853.         PK_TABLE_NAME         = o1.name,
  12854.         PK_COLUMN_NAME         = c1.name,
  12855.         PK_COLUMN_GUID        = convert(binary(16),null),
  12856.         PK_COLUMN_PROPID    = convert(int,null),
  12857.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12858.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  12859.         FK_TABLE_NAME         = o2.name,
  12860.         FK_COLUMN_NAME         = c2.name,
  12861.         FK_COLUMN_GUID        = convert(binary(16),null),
  12862.         FK_COLUMN_PROPID    = convert(int,null),
  12863.         ORDINAL            = convert(int,6),
  12864.         UPDATE_RULE        = 'NO ACTION',
  12865.         DELETE_RULE         = 'NO ACTION'        
  12866.     from
  12867.         sysobjects o1, sysobjects o2,
  12868.         syscolumns c1, syscolumns c2,
  12869.         sysreferences r
  12870.     where    
  12871.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12872.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12873.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12874.     and    o1.id = r.rkeyid
  12875.     and    r.keycnt >= 6
  12876.     and    o1.id = c1.id
  12877.     and    c1.colid = r.rkey6
  12878.     and     r.fkeyid = o2.id
  12879.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12880.     and    o2.id = c2.id    
  12881.     and    c2.colid = r.fkey6
  12882.     union all
  12883.     select
  12884.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12885.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12886.         PK_TABLE_NAME         = o1.name,
  12887.         PK_COLUMN_NAME         = c1.name,
  12888.         PK_COLUMN_GUID        = convert(binary(16),null),
  12889.         PK_COLUMN_PROPID    = convert(int,null),
  12890.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12891.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  12892.         FK_TABLE_NAME         = o2.name,
  12893.         FK_COLUMN_NAME         = c2.name,
  12894.         FK_COLUMN_GUID        = convert(binary(16),null),
  12895.         FK_COLUMN_PROPID    = convert(int,null),
  12896.         ORDINAL            = convert(int,7),
  12897.         UPDATE_RULE        = 'NO ACTION',
  12898.         DELETE_RULE         = 'NO ACTION'        
  12899.     from
  12900.         sysobjects o1, sysobjects o2,
  12901.         syscolumns c1, syscolumns c2,
  12902.         sysreferences r
  12903.     where    
  12904.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12905.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12906.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12907.     and    o1.id = r.rkeyid
  12908.     and    r.keycnt >= 7
  12909.     and    o1.id = c1.id
  12910.     and    c1.colid = r.rkey7
  12911.     and     r.fkeyid = o2.id
  12912.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12913.     and    o2.id = c2.id    
  12914.     and    c2.colid = r.fkey7
  12915.     union all
  12916.     select
  12917.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12918.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12919.         PK_TABLE_NAME         = o1.name,
  12920.         PK_COLUMN_NAME         = c1.name,
  12921.         PK_COLUMN_GUID        = convert(binary(16),null),
  12922.         PK_COLUMN_PROPID    = convert(int,null),
  12923.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12924.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  12925.         FK_TABLE_NAME         = o2.name,
  12926.         FK_COLUMN_NAME         = c2.name,
  12927.         FK_COLUMN_GUID        = convert(binary(16),null),
  12928.         FK_COLUMN_PROPID    = convert(int,null),
  12929.         ORDINAL            = convert(int,8),
  12930.         UPDATE_RULE        = 'NO ACTION',
  12931.         DELETE_RULE         = 'NO ACTION'        
  12932.     from
  12933.         sysobjects o1, sysobjects o2,
  12934.         syscolumns c1, syscolumns c2,
  12935.         sysreferences r
  12936.     where    
  12937.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12938.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12939.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12940.     and    o1.id = r.rkeyid
  12941.     and    r.keycnt >= 8
  12942.     and    o1.id = c1.id
  12943.     and    c1.colid = r.rkey8
  12944.     and     r.fkeyid = o2.id
  12945.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12946.     and    o2.id = c2.id    
  12947.     and    c2.colid = r.fkey8
  12948.     union all
  12949.     select
  12950.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12951.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12952.         PK_TABLE_NAME         = o1.name,
  12953.         PK_COLUMN_NAME         = c1.name,
  12954.         PK_COLUMN_GUID        = convert(binary(16),null),
  12955.         PK_COLUMN_PROPID    = convert(int,null),
  12956.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12957.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  12958.         FK_TABLE_NAME         = o2.name,
  12959.         FK_COLUMN_NAME         = c2.name,
  12960.         FK_COLUMN_GUID        = convert(binary(16),null),
  12961.         FK_COLUMN_PROPID    = convert(int,null),
  12962.         ORDINAL            = convert(int,9),
  12963.         UPDATE_RULE        = 'NO ACTION',
  12964.         DELETE_RULE         = 'NO ACTION'        
  12965.     from
  12966.         sysobjects o1, sysobjects o2,
  12967.         syscolumns c1, syscolumns c2,
  12968.         sysreferences r
  12969.     where    
  12970.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12971.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12972.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12973.     and    o1.id = r.rkeyid
  12974.     and    r.keycnt >= 9
  12975.     and    o1.id = c1.id
  12976.     and    c1.colid = r.rkey9
  12977.     and     r.fkeyid = o2.id
  12978.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12979.     and    o2.id = c2.id    
  12980.     and    c2.colid = r.fkey9
  12981.     union all
  12982.     select
  12983.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12984.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12985.         PK_TABLE_NAME         = o1.name,
  12986.         PK_COLUMN_NAME         = c1.name,
  12987.         PK_COLUMN_GUID        = convert(binary(16),null),
  12988.         PK_COLUMN_PROPID    = convert(int,null),
  12989.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12990.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  12991.         FK_TABLE_NAME         = o2.name,
  12992.         FK_COLUMN_NAME         = c2.name,
  12993.         FK_COLUMN_GUID        = convert(binary(16),null),
  12994.         FK_COLUMN_PROPID    = convert(int,null),
  12995.         ORDINAL            = convert(int,10),
  12996.         UPDATE_RULE        = 'NO ACTION',
  12997.         DELETE_RULE         = 'NO ACTION'        
  12998.     from
  12999.         sysobjects o1, sysobjects o2,
  13000.         syscolumns c1, syscolumns c2,
  13001.         sysreferences r
  13002.     where    
  13003.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13004.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13005.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13006.     and    o1.id = r.rkeyid
  13007.     and    r.keycnt >= 10
  13008.     and    o1.id = c1.id
  13009.     and    c1.colid = r.rkey10
  13010.     and     r.fkeyid = o2.id
  13011.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13012.     and    o2.id = c2.id    
  13013.     and    c2.colid = r.fkey10
  13014.     union all
  13015.     select
  13016.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  13017.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  13018.         PK_TABLE_NAME         = o1.name,
  13019.         PK_COLUMN_NAME         = c1.name,
  13020.         PK_COLUMN_GUID        = convert(binary(16),null),
  13021.         PK_COLUMN_PROPID    = convert(int,null),
  13022.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  13023.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  13024.         FK_TABLE_NAME         = o2.name,
  13025.         FK_COLUMN_NAME         = c2.name,
  13026.         FK_COLUMN_GUID        = convert(binary(16),null),
  13027.         FK_COLUMN_PROPID    = convert(int,null),
  13028.         ORDINAL            = convert(int,11),
  13029.         UPDATE_RULE        = 'NO ACTION',
  13030.         DELETE_RULE         = 'NO ACTION'        
  13031.     from
  13032.         sysobjects o1, sysobjects o2,
  13033.         syscolumns c1, syscolumns c2,
  13034.         sysreferences r
  13035.     where    
  13036.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13037.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13038.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13039.     and    o1.id = r.rkeyid
  13040.     and    r.keycnt >= 11
  13041.     and    o1.id = c1.id
  13042.     and    c1.colid = r.rkey11
  13043.     and     r.fkeyid = o2.id
  13044.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13045.     and    o2.id = c2.id    
  13046.     and    c2.colid = r.fkey11
  13047.     union all
  13048.     select
  13049.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  13050.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  13051.         PK_TABLE_NAME         = o1.name,
  13052.         PK_COLUMN_NAME         = c1.name,
  13053.         PK_COLUMN_GUID        = convert(binary(16),null),
  13054.         PK_COLUMN_PROPID    = convert(int,null),
  13055.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  13056.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  13057.         FK_TABLE_NAME         = o2.name,
  13058.         FK_COLUMN_NAME         = c2.name,
  13059.         FK_COLUMN_GUID        = convert(binary(16),null),
  13060.         FK_COLUMN_PROPID    = convert(int,null),
  13061.         ORDINAL            = convert(int,12),
  13062.         UPDATE_RULE        = 'NO ACTION',
  13063.         DELETE_RULE         = 'NO ACTION'        
  13064.     from
  13065.         sysobjects o1, sysobjects o2,
  13066.         syscolumns c1, syscolumns c2,
  13067.         sysreferences r
  13068.     where    
  13069.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13070.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13071.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13072.     and    o1.id = r.rkeyid
  13073.     and    r.keycnt >= 12
  13074.     and    o1.id = c1.id
  13075.     and    c1.colid = r.rkey12
  13076.     and     r.fkeyid = o2.id
  13077.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13078.     and    o2.id = c2.id    
  13079.     and    c2.colid = r.fkey12
  13080.     union all
  13081.     select
  13082.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  13083.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  13084.         PK_TABLE_NAME         = o1.name,
  13085.         PK_COLUMN_NAME         = c1.name,
  13086.         PK_COLUMN_GUID        = convert(binary(16),null),
  13087.         PK_COLUMN_PROPID    = convert(int,null),
  13088.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  13089.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  13090.         FK_TABLE_NAME         = o2.name,
  13091.         FK_COLUMN_NAME         = c2.name,
  13092.         FK_COLUMN_GUID        = convert(binary(16),null),
  13093.         FK_COLUMN_PROPID    = convert(int,null),
  13094.         ORDINAL            = convert(int,13),
  13095.         UPDATE_RULE        = 'NO ACTION',
  13096.         DELETE_RULE         = 'NO ACTION'        
  13097.     from
  13098.         sysobjects o1, sysobjects o2,
  13099.         syscolumns c1, syscolumns c2,
  13100.         sysreferences r
  13101.     where    
  13102.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13103.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13104.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13105.     and    o1.id = r.rkeyid
  13106.     and    r.keycnt >= 13
  13107.     and    o1.id = c1.id
  13108.     and    c1.colid = r.rkey13
  13109.     and     r.fkeyid = o2.id
  13110.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13111.     and    o2.id = c2.id    
  13112.     and    c2.colid = r.fkey13
  13113.     union all
  13114.     select
  13115.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  13116.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  13117.         PK_TABLE_NAME         = o1.name,
  13118.         PK_COLUMN_NAME         = c1.name,
  13119.         PK_COLUMN_GUID        = convert(binary(16),null),
  13120.         PK_COLUMN_PROPID    = convert(int,null),
  13121.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  13122.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  13123.         FK_TABLE_NAME         = o2.name,
  13124.         FK_COLUMN_NAME         = c2.name,
  13125.         FK_COLUMN_GUID        = convert(binary(16),null),
  13126.         FK_COLUMN_PROPID    = convert(int,null),
  13127.         ORDINAL            = convert(int,14),
  13128.         UPDATE_RULE        = 'NO ACTION',
  13129.         DELETE_RULE         = 'NO ACTION'        
  13130.     from
  13131.         sysobjects o1, sysobjects o2,
  13132.         syscolumns c1, syscolumns c2,
  13133.         sysreferences r
  13134.     where    
  13135.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13136.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13137.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13138.     and    o1.id = r.rkeyid
  13139.     and    r.keycnt >= 14
  13140.     and    o1.id = c1.id
  13141.     and    c1.colid = r.rkey14
  13142.     and     r.fkeyid = o2.id
  13143.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13144.     and    o2.id = c2.id    
  13145.     and    c2.colid = r.fkey14
  13146.     union all
  13147.     select
  13148.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  13149.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  13150.         PK_TABLE_NAME         = o1.name,
  13151.         PK_COLUMN_NAME         = c1.name,
  13152.         PK_COLUMN_GUID        = convert(binary(16),null),
  13153.         PK_COLUMN_PROPID    = convert(int,null),
  13154.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  13155.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  13156.         FK_TABLE_NAME         = o2.name,
  13157.         FK_COLUMN_NAME         = c2.name,
  13158.         FK_COLUMN_GUID        = convert(binary(16),null),
  13159.         FK_COLUMN_PROPID    = convert(int,null),
  13160.         ORDINAL            = convert(int,15),
  13161.         UPDATE_RULE        = 'NO ACTION',
  13162.         DELETE_RULE         = 'NO ACTION'        
  13163.     from
  13164.         sysobjects o1, sysobjects o2,
  13165.         syscolumns c1, syscolumns c2,
  13166.         sysreferences r
  13167.     where    
  13168.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13169.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13170.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13171.     and    o1.id = r.rkeyid
  13172.     and    r.keycnt >= 15
  13173.     and    o1.id = c1.id
  13174.     and    c1.colid = r.rkey15
  13175.     and     r.fkeyid = o2.id
  13176.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13177.     and    o2.id = c2.id    
  13178.     and    c2.colid = r.fkey15
  13179.     union all
  13180.     select
  13181.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  13182.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  13183.         PK_TABLE_NAME         = o1.name,
  13184.         PK_COLUMN_NAME         = c1.name,
  13185.         PK_COLUMN_GUID        = convert(binary(16),null),
  13186.         PK_COLUMN_PROPID    = convert(int,null),
  13187.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  13188.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  13189.         FK_TABLE_NAME         = o2.name,
  13190.         FK_COLUMN_NAME         = c2.name,
  13191.         FK_COLUMN_GUID        = convert(binary(16),null),
  13192.         FK_COLUMN_PROPID    = convert(int,null),
  13193.         ORDINAL            = convert(int,16),
  13194.         UPDATE_RULE        = 'NO ACTION',
  13195.         DELETE_RULE         = 'NO ACTION'        
  13196.     from
  13197.         sysobjects o1, sysobjects o2,
  13198.         syscolumns c1, syscolumns c2,
  13199.         sysreferences r
  13200.     where    
  13201.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13202.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13203.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13204.     and    o1.id = r.rkeyid
  13205.     and    o1.id = c1.id
  13206.     and    r.keycnt >= 16
  13207.     and    c1.colid = r.rkey16
  13208.     and     r.fkeyid = o2.id
  13209.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13210.     and    o2.id = c2.id    
  13211.     and    c2.colid = r.fkey16
  13212.     order by 8,9,2,3,13
  13213.     END
  13214.  
  13215.     exec @ret = sp_cursoropen @handle output, 'select * from #spfkeysrowset1',
  13216.         @scrollopt output, @ccopt output, @rows output
  13217.  
  13218.     drop table #spfkeysrowset1
  13219.        return isnull(@ret,0)
  13220. go
  13221.  
  13222.  
  13223. create procedure sp_foreign_keys_rowset;5
  13224.     (
  13225.        @handle        int output,
  13226.        @scrollopt    int output,
  13227.     @ccopt        int output,
  13228.     @rows        int output,
  13229.     @cmd        varchar(255)
  13230.     )
  13231. as
  13232.     declare @ret int
  13233.     declare @database_name varchar(30)
  13234.  
  13235.     create table #spfkeysrowset2
  13236.         (
  13237.         PK_TABLE_CATALOG    varchar(30) not null,
  13238.         PK_TABLE_SCHEMA        varchar(30) not null,
  13239.         PK_TABLE_NAME         varchar(30) not null,
  13240.         PK_COLUMN_NAME         varchar(30) not null,
  13241.         PK_COLUMN_GUID        binary(16) null,
  13242.         PK_COLUMN_PROPID    int null,
  13243.         FK_TABLE_CATALOG    varchar(30) not null,
  13244.         FK_TABLE_SCHEMA     varchar(30) not null,
  13245.         FK_TABLE_NAME         varchar(30) not null,
  13246.         FK_COLUMN_NAME         varchar(30) not null,
  13247.         FK_COLUMN_GUID        binary(16) null,
  13248.         FK_COLUMN_PROPID    int null,
  13249.         ORDINAL            int not null,
  13250.         UPDATE_RULE        varchar(30) not null,
  13251.         DELETE_RULE         varchar(30) not null        
  13252.         )
  13253.  
  13254.     declare sysdatabases_cursor insensitive cursor for 
  13255.         select name from master.dbo.sysdatabases order by name
  13256.   
  13257.     set nocount on
  13258.     open sysdatabases_cursor
  13259.       fetch next from sysdatabases_cursor into @database_name 
  13260.     while @@fetch_status = 0
  13261.         begin
  13262.         exec('use '+@database_name+' insert into #spfkeysrowset2 '+@cmd)
  13263.         fetch next from sysdatabases_cursor into @database_name 
  13264.         end
  13265.       close sysdatabases_cursor
  13266.     deallocate sysdatabases_cursor
  13267.  
  13268.     if @scrollopt is null
  13269.         begin
  13270.         select * from #spfkeysrowset2
  13271.         end
  13272.     else
  13273.         begin
  13274.         exec @ret = sp_cursoropen @handle output,'select * from #spfkeysrowset2',
  13275.             @scrollopt output, @ccopt output, @rows output
  13276.         end
  13277.     drop table #spfkeysrowset2
  13278.     return isnull(@ret,0)
  13279. go
  13280.  
  13281.  
  13282.  
  13283. if (charindex('7.00', @@version) = 0)
  13284. begin
  13285.     print ''
  13286.     print ''
  13287.     print 'Warning:'
  13288.     print 'you are installing the stored procedures '
  13289.     print 'on a pre 7.0 SQL Server.'
  13290.     print 'Ignore the following errors.'
  13291. end
  13292. else
  13293.     drop proc sp_foreign_keys_rowset
  13294. go
  13295.  
  13296.  
  13297. /*    Procedure for 7.0 server */
  13298. CREATE PROCEDURE sp_foreign_keys_rowset
  13299.     (
  13300.     @pk_table_name        varchar(255),
  13301.     @pk_table_schema    varchar(255) = null,
  13302.     @fk_table_name        varchar(255) = null,
  13303.     @fk_table_schema    varchar(255) = null,
  13304.     @fk_table_catalog    varchar(255) = null
  13305.     )
  13306. as
  13307.     select
  13308.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  13309.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  13310.         PK_TABLE_NAME         = o1.name,
  13311.         PK_COLUMN_NAME         = c1.name,
  13312.         PK_COLUMN_GUID        = convert(binary(16),null),
  13313.         PK_COLUMN_PROPID    = convert(int,null),
  13314.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  13315.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  13316.         FK_TABLE_NAME         = o2.name,
  13317.         FK_COLUMN_NAME         = c2.name,
  13318.         FK_COLUMN_GUID        = convert(binary(16),null),
  13319.         FK_COLUMN_PROPID    = convert(int,null),
  13320.         ORDINAL            = convert(int,1),
  13321.         UPDATE_RULE        = 'NO ACTION',
  13322.         DELETE_RULE         = 'NO ACTION'        
  13323.     from
  13324.         sysobjects o1, sysobjects o2,
  13325.         syscolumns c1, syscolumns c2,
  13326.         sysreferences r
  13327.     where    
  13328.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13329.     and    o1.name = @pk_table_name
  13330.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13331.     and    o1.id = r.rkeyid
  13332.     and    o1.id = c1.id
  13333.     and    c1.colid = r.rkey1
  13334.     and     r.fkeyid = o2.id
  13335.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  13336.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13337.     and    o2.id = c2.id    
  13338.     and    c2.colid = r.fkey1
  13339.     union all
  13340.     select
  13341.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  13342.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  13343.         PK_TABLE_NAME         = o1.name,
  13344.         PK_COLUMN_NAME         = c1.name,
  13345.         PK_COLUMN_GUID        = convert(binary(16),null),
  13346.         PK_COLUMN_PROPID    = convert(int,null),
  13347.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  13348.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  13349.         FK_TABLE_NAME         = o2.name,
  13350.         FK_COLUMN_NAME         = c2.name,
  13351.         FK_COLUMN_GUID        = convert(binary(16),null),
  13352.         FK_COLUMN_PROPID    = convert(int,null),
  13353.         ORDINAL            = convert(int,2),
  13354.         UPDATE_RULE        = 'NO ACTION',
  13355.         DELETE_RULE         = 'NO ACTION'        
  13356.     from
  13357.         sysobjects o1, sysobjects o2,
  13358.         syscolumns c1, syscolumns c2,
  13359.         sysreferences r
  13360.     where    
  13361.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13362.     and    o1.name = @pk_table_name
  13363.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13364.     and    o1.id = r.rkeyid
  13365.     and    o1.id = c1.id
  13366.     and    c1.colid = r.rkey2
  13367.     and    r.keycnt >= 2
  13368.     and     r.fkeyid = o2.id
  13369.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  13370.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13371.     and    o2.id = c2.id    
  13372.     and    c2.colid = r.fkey2
  13373.     union all
  13374.     select
  13375.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  13376.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  13377.         PK_TABLE_NAME         = o1.name,
  13378.         PK_COLUMN_NAME         = c1.name,
  13379.         PK_COLUMN_GUID        = convert(binary(16),null),
  13380.         PK_COLUMN_PROPID    = convert(int,null),
  13381.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  13382.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  13383.         FK_TABLE_NAME         = o2.name,
  13384.         FK_COLUMN_NAME         = c2.name,
  13385.         FK_COLUMN_GUID        = convert(binary(16),null),
  13386.         FK_COLUMN_PROPID    = convert(int,null),
  13387.         ORDINAL            = convert(int,3),
  13388.         UPDATE_RULE        = 'NO ACTION',
  13389.         DELETE_RULE         = 'NO ACTION'        
  13390.     from
  13391.         sysobjects o1, sysobjects o2,
  13392.         syscolumns c1, syscolumns c2,
  13393.         sysreferences r
  13394.     where    
  13395.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13396.     and    o1.name = @pk_table_name
  13397.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13398.     and    o1.id = r.rkeyid
  13399.     and    o1.id = c1.id
  13400.     and    c1.colid = r.rkey3
  13401.     and    r.keycnt >= 3
  13402.     and     r.fkeyid = o2.id
  13403.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  13404.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13405.     and    o2.id = c2.id    
  13406.     and    c2.colid = r.fkey3
  13407.     union all
  13408.     select
  13409.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  13410.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  13411.         PK_TABLE_NAME         = o1.name,
  13412.         PK_COLUMN_NAME         = c1.name,
  13413.         PK_COLUMN_GUID        = convert(binary(16),null),
  13414.         PK_COLUMN_PROPID    = convert(int,null),
  13415.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  13416.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  13417.         FK_TABLE_NAME         = o2.name,
  13418.         FK_COLUMN_NAME         = c2.name,
  13419.         FK_COLUMN_GUID        = convert(binary(16),null),
  13420.         FK_COLUMN_PROPID    = convert(int,null),
  13421.         ORDINAL            = convert(int,4),
  13422.         UPDATE_RULE        = 'NO ACTION',
  13423.         DELETE_RULE         = 'NO ACTION'        
  13424.     from
  13425.         sysobjects o1, sysobjects o2,
  13426.         syscolumns c1, syscolumns c2,
  13427.         sysreferences r
  13428.     where    
  13429.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13430.     and    o1.name = @pk_table_name
  13431.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13432.     and    o1.id = r.rkeyid
  13433.     and    r.keycnt >= 4
  13434.     and    o1.id = c1.id
  13435.     and    c1.colid = r.rkey4
  13436.     and     r.fkeyid = o2.id
  13437.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  13438.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13439.     and    o2.id = c2.id    
  13440.     and    c2.colid = r.fkey4
  13441.     union all
  13442.     select
  13443.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  13444.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  13445.         PK_TABLE_NAME         = o1.name,
  13446.         PK_COLUMN_NAME         = c1.name,
  13447.         PK_COLUMN_GUID        = convert(binary(16),null),
  13448.         PK_COLUMN_PROPID    = convert(int,null),
  13449.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  13450.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  13451.         FK_TABLE_NAME         = o2.name,
  13452.         FK_COLUMN_NAME         = c2.name,
  13453.         FK_COLUMN_GUID        = convert(binary(16),null),
  13454.         FK_COLUMN_PROPID    = convert(int,null),
  13455.         ORDINAL            = convert(int,5),
  13456.         UPDATE_RULE        = 'NO ACTION',
  13457.         DELETE_RULE         = 'NO ACTION'        
  13458.     from
  13459.         sysobjects o1, sysobjects o2,
  13460.         syscolumns c1, syscolumns c2,
  13461.         sysreferences r
  13462.     where    
  13463.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13464.     and    o1.name = @pk_table_name
  13465.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13466.     and    o1.id = r.rkeyid
  13467.     and    r.keycnt >= 5
  13468.     and    o1.id = c1.id
  13469.     and    c1.colid = r.rkey5
  13470.     and     r.fkeyid = o2.id
  13471.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  13472.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13473.     and    o2.id = c2.id    
  13474.     and    c2.colid = r.fkey5
  13475.     union all
  13476.     select
  13477.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  13478.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  13479.         PK_TABLE_NAME         = o1.name,
  13480.         PK_COLUMN_NAME         = c1.name,
  13481.         PK_COLUMN_GUID        = convert(binary(16),null),
  13482.         PK_COLUMN_PROPID    = convert(int,null),
  13483.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  13484.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  13485.         FK_TABLE_NAME         = o2.name,
  13486.         FK_COLUMN_NAME         = c2.name,
  13487.         FK_COLUMN_GUID        = convert(binary(16),null),
  13488.         FK_COLUMN_PROPID    = convert(int,null),
  13489.         ORDINAL            = convert(int,6),
  13490.         UPDATE_RULE        = 'NO ACTION',
  13491.         DELETE_RULE         = 'NO ACTION'        
  13492.     from
  13493.         sysobjects o1, sysobjects o2,
  13494.         syscolumns c1, syscolumns c2,
  13495.         sysreferences r
  13496.     where    
  13497.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13498.     and    o1.name = @pk_table_name
  13499.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13500.     and    o1.id = r.rkeyid
  13501.     and    r.keycnt >= 6
  13502.     and    o1.id = c1.id
  13503.     and    c1.colid = r.rkey6
  13504.     and     r.fkeyid = o2.id
  13505.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  13506.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13507.     and    o2.id = c2.id    
  13508.     and    c2.colid = r.fkey6
  13509.     union all
  13510.     select
  13511.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  13512.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  13513.         PK_TABLE_NAME         = o1.name,
  13514.         PK_COLUMN_NAME         = c1.name,
  13515.         PK_COLUMN_GUID        = convert(binary(16),null),
  13516.         PK_COLUMN_PROPID    = convert(int,null),
  13517.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  13518.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  13519.         FK_TABLE_NAME         = o2.name,
  13520.         FK_COLUMN_NAME         = c2.name,
  13521.         FK_COLUMN_GUID        = convert(binary(16),null),
  13522.         FK_COLUMN_PROPID    = convert(int,null),
  13523.         ORDINAL            = convert(int,7),
  13524.         UPDATE_RULE        = 'NO ACTION',
  13525.         DELETE_RULE         = 'NO ACTION'        
  13526.     from
  13527.         sysobjects o1, sysobjects o2,
  13528.         syscolumns c1, syscolumns c2,
  13529.         sysreferences r
  13530.     where    
  13531.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13532.     and    o1.name = @pk_table_name
  13533.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13534.     and    o1.id = r.rkeyid
  13535.     and    r.keycnt >= 7
  13536.     and    o1.id = c1.id
  13537.     and    c1.colid = r.rkey7
  13538.     and     r.fkeyid = o2.id
  13539.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  13540.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13541.     and    o2.id = c2.id    
  13542.     and    c2.colid = r.fkey7
  13543.     union all
  13544.     select
  13545.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  13546.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  13547.         PK_TABLE_NAME         = o1.name,
  13548.         PK_COLUMN_NAME         = c1.name,
  13549.         PK_COLUMN_GUID        = convert(binary(16),null),
  13550.         PK_COLUMN_PROPID    = convert(int,null),
  13551.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  13552.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  13553.         FK_TABLE_NAME         = o2.name,
  13554.         FK_COLUMN_NAME         = c2.name,
  13555.         FK_COLUMN_GUID        = convert(binary(16),null),
  13556.         FK_COLUMN_PROPID    = convert(int,null),
  13557.         ORDINAL            = convert(int,8),
  13558.         UPDATE_RULE        = 'NO ACTION',
  13559.         DELETE_RULE         = 'NO ACTION'        
  13560.     from
  13561.         sysobjects o1, sysobjects o2,
  13562.         syscolumns c1, syscolumns c2,
  13563.         sysreferences r
  13564.     where    
  13565.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13566.     and    o1.name = @pk_table_name
  13567.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13568.     and    o1.id = r.rkeyid
  13569.     and    r.keycnt >= 8
  13570.     and    o1.id = c1.id
  13571.     and    c1.colid = r.rkey8
  13572.     and     r.fkeyid = o2.id
  13573.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  13574.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13575.     and    o2.id = c2.id    
  13576.     and    c2.colid = r.fkey8
  13577.     union all
  13578.     select
  13579.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  13580.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  13581.         PK_TABLE_NAME         = o1.name,
  13582.         PK_COLUMN_NAME         = c1.name,
  13583.         PK_COLUMN_GUID        = convert(binary(16),null),
  13584.         PK_COLUMN_PROPID    = convert(int,null),
  13585.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  13586.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  13587.         FK_TABLE_NAME         = o2.name,
  13588.         FK_COLUMN_NAME         = c2.name,
  13589.         FK_COLUMN_GUID        = convert(binary(16),null),
  13590.         FK_COLUMN_PROPID    = convert(int,null),
  13591.         ORDINAL            = convert(int,9),
  13592.         UPDATE_RULE        = 'NO ACTION',
  13593.         DELETE_RULE         = 'NO ACTION'        
  13594.     from
  13595.         sysobjects o1, sysobjects o2,
  13596.         syscolumns c1, syscolumns c2,
  13597.         sysreferences r
  13598.     where    
  13599.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13600.     and    o1.name = @pk_table_name
  13601.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13602.     and    o1.id = r.rkeyid
  13603.     and    r.keycnt >= 9
  13604.     and    o1.id = c1.id
  13605.     and    c1.colid = r.rkey9
  13606.     and     r.fkeyid = o2.id
  13607.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  13608.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13609.     and    o2.id = c2.id    
  13610.     and    c2.colid = r.fkey9
  13611.     union all
  13612.     select
  13613.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  13614.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  13615.         PK_TABLE_NAME         = o1.name,
  13616.         PK_COLUMN_NAME         = c1.name,
  13617.         PK_COLUMN_GUID        = convert(binary(16),null),
  13618.         PK_COLUMN_PROPID    = convert(int,null),
  13619.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  13620.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  13621.         FK_TABLE_NAME         = o2.name,
  13622.         FK_COLUMN_NAME         = c2.name,
  13623.         FK_COLUMN_GUID        = convert(binary(16),null),
  13624.         FK_COLUMN_PROPID    = convert(int,null),
  13625.         ORDINAL            = convert(int,10),
  13626.         UPDATE_RULE        = 'NO ACTION',
  13627.         DELETE_RULE         = 'NO ACTION'        
  13628.     from
  13629.         sysobjects o1, sysobjects o2,
  13630.         syscolumns c1, syscolumns c2,
  13631.         sysreferences r
  13632.     where    
  13633.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13634.     and    o1.name = @pk_table_name
  13635.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13636.     and    o1.id = r.rkeyid
  13637.     and    r.keycnt >= 10
  13638.     and    o1.id = c1.id
  13639.     and    c1.colid = r.rkey10
  13640.     and     r.fkeyid = o2.id
  13641.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  13642.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13643.     and    o2.id = c2.id    
  13644.     and    c2.colid = r.fkey10
  13645.     union all
  13646.     select
  13647.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  13648.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  13649.         PK_TABLE_NAME         = o1.name,
  13650.         PK_COLUMN_NAME         = c1.name,
  13651.         PK_COLUMN_GUID        = convert(binary(16),null),
  13652.         PK_COLUMN_PROPID    = convert(int,null),
  13653.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  13654.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  13655.         FK_TABLE_NAME         = o2.name,
  13656.         FK_COLUMN_NAME         = c2.name,
  13657.         FK_COLUMN_GUID        = convert(binary(16),null),
  13658.         FK_COLUMN_PROPID    = convert(int,null),
  13659.         ORDINAL            = convert(int,11),
  13660.         UPDATE_RULE        = 'NO ACTION',
  13661.         DELETE_RULE         = 'NO ACTION'        
  13662.     from
  13663.         sysobjects o1, sysobjects o2,
  13664.         syscolumns c1, syscolumns c2,
  13665.         sysreferences r
  13666.     where    
  13667.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13668.     and    o1.name = @pk_table_name
  13669.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13670.     and    o1.id = r.rkeyid
  13671.     and    r.keycnt >= 11
  13672.     and    o1.id = c1.id
  13673.     and    c1.colid = r.rkey11
  13674.     and     r.fkeyid = o2.id
  13675.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  13676.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13677.     and    o2.id = c2.id    
  13678.     and    c2.colid = r.fkey11
  13679.     union all
  13680.     select
  13681.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  13682.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  13683.         PK_TABLE_NAME         = o1.name,
  13684.         PK_COLUMN_NAME         = c1.name,
  13685.         PK_COLUMN_GUID        = convert(binary(16),null),
  13686.         PK_COLUMN_PROPID    = convert(int,null),
  13687.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  13688.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  13689.         FK_TABLE_NAME         = o2.name,
  13690.         FK_COLUMN_NAME         = c2.name,
  13691.         FK_COLUMN_GUID        = convert(binary(16),null),
  13692.         FK_COLUMN_PROPID    = convert(int,null),
  13693.         ORDINAL            = convert(int,12),
  13694.         UPDATE_RULE        = 'NO ACTION',
  13695.         DELETE_RULE         = 'NO ACTION'        
  13696.     from
  13697.         sysobjects o1, sysobjects o2,
  13698.         syscolumns c1, syscolumns c2,
  13699.         sysreferences r
  13700.     where    
  13701.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13702.     and    o1.name = @pk_table_name
  13703.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13704.     and    o1.id = r.rkeyid
  13705.     and    r.keycnt >= 12
  13706.     and    o1.id = c1.id
  13707.     and    c1.colid = r.rkey12
  13708.     and     r.fkeyid = o2.id
  13709.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  13710.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13711.     and    o2.id = c2.id    
  13712.     and    c2.colid = r.fkey12
  13713.     union all
  13714.     select
  13715.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  13716.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  13717.         PK_TABLE_NAME         = o1.name,
  13718.         PK_COLUMN_NAME         = c1.name,
  13719.         PK_COLUMN_GUID        = convert(binary(16),null),
  13720.         PK_COLUMN_PROPID    = convert(int,null),
  13721.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  13722.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  13723.         FK_TABLE_NAME         = o2.name,
  13724.         FK_COLUMN_NAME         = c2.name,
  13725.         FK_COLUMN_GUID        = convert(binary(16),null),
  13726.         FK_COLUMN_PROPID    = convert(int,null),
  13727.         ORDINAL            = convert(int,13),
  13728.         UPDATE_RULE        = 'NO ACTION',
  13729.         DELETE_RULE         = 'NO ACTION'        
  13730.     from
  13731.         sysobjects o1, sysobjects o2,
  13732.         syscolumns c1, syscolumns c2,
  13733.         sysreferences r
  13734.     where    
  13735.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13736.     and    o1.name = @pk_table_name
  13737.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13738.     and    o1.id = r.rkeyid
  13739.     and    r.keycnt >= 13
  13740.     and    o1.id = c1.id
  13741.     and    c1.colid = r.rkey13
  13742.     and     r.fkeyid = o2.id
  13743.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  13744.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13745.     and    o2.id = c2.id    
  13746.     and    c2.colid = r.fkey13
  13747.     union all
  13748.     select
  13749.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  13750.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  13751.         PK_TABLE_NAME         = o1.name,
  13752.         PK_COLUMN_NAME         = c1.name,
  13753.         PK_COLUMN_GUID        = convert(binary(16),null),
  13754.         PK_COLUMN_PROPID    = convert(int,null),
  13755.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  13756.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  13757.         FK_TABLE_NAME         = o2.name,
  13758.         FK_COLUMN_NAME         = c2.name,
  13759.         FK_COLUMN_GUID        = convert(binary(16),null),
  13760.         FK_COLUMN_PROPID    = convert(int,null),
  13761.         ORDINAL            = convert(int,14),
  13762.         UPDATE_RULE        = 'NO ACTION',
  13763.         DELETE_RULE         = 'NO ACTION'        
  13764.     from
  13765.         sysobjects o1, sysobjects o2,
  13766.         syscolumns c1, syscolumns c2,
  13767.         sysreferences r
  13768.     where    
  13769.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13770.     and    o1.name = @pk_table_name
  13771.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13772.     and    o1.id = r.rkeyid
  13773.     and    r.keycnt >= 14
  13774.     and    o1.id = c1.id
  13775.     and    c1.colid = r.rkey14
  13776.     and     r.fkeyid = o2.id
  13777.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  13778.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13779.     and    o2.id = c2.id    
  13780.     and    c2.colid = r.fkey14
  13781.     union all
  13782.     select
  13783.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  13784.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  13785.         PK_TABLE_NAME         = o1.name,
  13786.         PK_COLUMN_NAME         = c1.name,
  13787.         PK_COLUMN_GUID        = convert(binary(16),null),
  13788.         PK_COLUMN_PROPID    = convert(int,null),
  13789.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  13790.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  13791.         FK_TABLE_NAME         = o2.name,
  13792.         FK_COLUMN_NAME         = c2.name,
  13793.         FK_COLUMN_GUID        = convert(binary(16),null),
  13794.         FK_COLUMN_PROPID    = convert(int,null),
  13795.         ORDINAL            = convert(int,15),
  13796.         UPDATE_RULE        = 'NO ACTION',
  13797.         DELETE_RULE         = 'NO ACTION'        
  13798.     from
  13799.         sysobjects o1, sysobjects o2,
  13800.         syscolumns c1, syscolumns c2,
  13801.         sysreferences r
  13802.     where    
  13803.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13804.     and    o1.name = @pk_table_name
  13805.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13806.     and    o1.id = r.rkeyid
  13807.     and    r.keycnt >= 15
  13808.     and    o1.id = c1.id
  13809.     and    c1.colid = r.rkey15
  13810.     and     r.fkeyid = o2.id
  13811.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  13812.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13813.     and    o2.id = c2.id    
  13814.     and    c2.colid = r.fkey15
  13815.     union all
  13816.     select
  13817.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  13818.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  13819.         PK_TABLE_NAME         = o1.name,
  13820.         PK_COLUMN_NAME         = c1.name,
  13821.         PK_COLUMN_GUID        = convert(binary(16),null),
  13822.         PK_COLUMN_PROPID    = convert(int,null),
  13823.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  13824.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  13825.         FK_TABLE_NAME         = o2.name,
  13826.         FK_COLUMN_NAME         = c2.name,
  13827.         FK_COLUMN_GUID        = convert(binary(16),null),
  13828.         FK_COLUMN_PROPID    = convert(int,null),
  13829.         ORDINAL            = convert(int,16),
  13830.         UPDATE_RULE        = 'NO ACTION',
  13831.         DELETE_RULE         = 'NO ACTION'        
  13832.     from
  13833.         sysobjects o1, sysobjects o2,
  13834.         syscolumns c1, syscolumns c2,
  13835.         sysreferences r
  13836.     where    
  13837.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13838.     and    o1.name = @pk_table_name
  13839.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13840.     and    o1.id = r.rkeyid
  13841.     and    r.keycnt >= 16
  13842.     and    o1.id = c1.id
  13843.     and    c1.colid = r.rkey16
  13844.     and     r.fkeyid = o2.id
  13845.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  13846.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13847.     and    o2.id = c2.id    
  13848.     and    c2.colid = r.fkey16
  13849.     order by 8,9,2,3,13
  13850. go
  13851. CREATE PROCEDURE sp_foreign_keys_rowset;2
  13852.     (
  13853.     @fk_table_name        varchar(255),
  13854.     @fk_table_schema    varchar(255) = null,
  13855.     @pk_table_name        varchar(255) = null,
  13856.     @pk_table_schema    varchar(255) = null,
  13857.     @pk_table_catalog    varchar(255) = null
  13858.     )
  13859. as
  13860.     select
  13861.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  13862.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  13863.         PK_TABLE_NAME         = o1.name,
  13864.         PK_COLUMN_NAME         = c1.name,
  13865.         PK_COLUMN_GUID        = convert(binary(16),null),
  13866.         PK_COLUMN_PROPID    = convert(int,null),
  13867.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  13868.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  13869.         FK_TABLE_NAME         = o2.name,
  13870.         FK_COLUMN_NAME         = c2.name,
  13871.         FK_COLUMN_GUID        = convert(binary(16),null),
  13872.         FK_COLUMN_PROPID    = convert(int,null),
  13873.         ORDINAL            = convert(int,1),
  13874.         UPDATE_RULE        = 'NO ACTION',
  13875.         DELETE_RULE         = 'NO ACTION'        
  13876.     from
  13877.         sysobjects o1, sysobjects o2,
  13878.         syscolumns c1, syscolumns c2,
  13879.         sysreferences r
  13880.     where    
  13881.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13882.     and    o2.name = @fk_table_name
  13883.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13884.     and    o2.id = r.fkeyid
  13885.     and    o2.id = c2.id
  13886.     and    c2.colid = r.fkey1
  13887.     and     r.rkeyid = o1.id
  13888.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  13889.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13890.     and    o1.id = c1.id    
  13891.     and    c1.colid = r.rkey1
  13892.     union    all
  13893.     select
  13894.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  13895.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  13896.         PK_TABLE_NAME         = o1.name,
  13897.         PK_COLUMN_NAME         = c1.name,
  13898.         PK_COLUMN_GUID        = convert(binary(16),null),
  13899.         PK_COLUMN_PROPID    = convert(int,null),
  13900.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  13901.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  13902.         FK_TABLE_NAME         = o2.name,
  13903.         FK_COLUMN_NAME         = c2.name,
  13904.         FK_COLUMN_GUID        = convert(binary(16),null),
  13905.         FK_COLUMN_PROPID    = convert(int,null),
  13906.         ORDINAL            = convert(int,2),
  13907.         UPDATE_RULE        = 'NO ACTION',
  13908.         DELETE_RULE         = 'NO ACTION'        
  13909.     from
  13910.         sysobjects o1, sysobjects o2,
  13911.         syscolumns c1, syscolumns c2,
  13912.         sysreferences r
  13913.     where    
  13914.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13915.     and    o2.name = @fk_table_name
  13916.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13917.     and    o2.id = r.fkeyid
  13918.     and    r.keycnt >= 2
  13919.     and    o2.id = c2.id
  13920.     and    c2.colid = r.fkey2
  13921.     and     r.rkeyid = o1.id
  13922.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  13923.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13924.     and    o1.id = c1.id    
  13925.     and    c1.colid = r.rkey2
  13926.     union    all
  13927.     select
  13928.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  13929.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  13930.         PK_TABLE_NAME         = o1.name,
  13931.         PK_COLUMN_NAME         = c1.name,
  13932.         PK_COLUMN_GUID        = convert(binary(16),null),
  13933.         PK_COLUMN_PROPID    = convert(int,null),
  13934.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  13935.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  13936.         FK_TABLE_NAME         = o2.name,
  13937.         FK_COLUMN_NAME         = c2.name,
  13938.         FK_COLUMN_GUID        = convert(binary(16),null),
  13939.         FK_COLUMN_PROPID    = convert(int,null),
  13940.         ORDINAL            = convert(int,3),
  13941.         UPDATE_RULE        = 'NO ACTION',
  13942.         DELETE_RULE         = 'NO ACTION'        
  13943.     from
  13944.         sysobjects o1, sysobjects o2,
  13945.         syscolumns c1, syscolumns c2,
  13946.         sysreferences r
  13947.     where    
  13948.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13949.     and    o2.name = @fk_table_name
  13950.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13951.     and    o2.id = r.fkeyid
  13952.     and    r.keycnt >= 3
  13953.     and    o2.id = c2.id
  13954.     and    c2.colid = r.fkey3
  13955.     and     r.rkeyid = o1.id
  13956.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  13957.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13958.     and    o1.id = c1.id    
  13959.     and    c1.colid = r.rkey3
  13960.     union    all
  13961.     select
  13962.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  13963.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  13964.         PK_TABLE_NAME         = o1.name,
  13965.         PK_COLUMN_NAME         = c1.name,
  13966.         PK_COLUMN_GUID        = convert(binary(16),null),
  13967.         PK_COLUMN_PROPID    = convert(int,null),
  13968.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  13969.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  13970.         FK_TABLE_NAME         = o2.name,
  13971.         FK_COLUMN_NAME         = c2.name,
  13972.         FK_COLUMN_GUID        = convert(binary(16),null),
  13973.         FK_COLUMN_PROPID    = convert(int,null),
  13974.         ORDINAL            = convert(int,4),
  13975.         UPDATE_RULE        = 'NO ACTION',
  13976.         DELETE_RULE         = 'NO ACTION'        
  13977.     from
  13978.         sysobjects o1, sysobjects o2,
  13979.         syscolumns c1, syscolumns c2,
  13980.         sysreferences r
  13981.     where    
  13982.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13983.     and    o2.name = @fk_table_name
  13984.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13985.     and    o2.id = r.fkeyid
  13986.     and    r.keycnt >= 4
  13987.     and    o2.id = c2.id
  13988.     and    c2.colid = r.fkey4
  13989.     and     r.rkeyid = o1.id
  13990.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  13991.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13992.     and    o1.id = c1.id    
  13993.     and    c1.colid = r.rkey4
  13994.     union    all
  13995.     select
  13996.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  13997.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  13998.         PK_TABLE_NAME         = o1.name,
  13999.         PK_COLUMN_NAME         = c1.name,
  14000.         PK_COLUMN_GUID        = convert(binary(16),null),
  14001.         PK_COLUMN_PROPID    = convert(int,null),
  14002.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14003.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  14004.         FK_TABLE_NAME         = o2.name,
  14005.         FK_COLUMN_NAME         = c2.name,
  14006.         FK_COLUMN_GUID        = convert(binary(16),null),
  14007.         FK_COLUMN_PROPID    = convert(int,null),
  14008.         ORDINAL            = convert(int,5),
  14009.         UPDATE_RULE        = 'NO ACTION',
  14010.         DELETE_RULE         = 'NO ACTION'        
  14011.     from
  14012.         sysobjects o1, sysobjects o2,
  14013.         syscolumns c1, syscolumns c2,
  14014.         sysreferences r
  14015.     where    
  14016.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14017.     and    o2.name = @fk_table_name
  14018.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14019.     and    o2.id = r.fkeyid
  14020.     and    r.keycnt >= 5
  14021.     and    o2.id = c2.id
  14022.     and    c2.colid = r.fkey5
  14023.     and     r.rkeyid = o1.id
  14024.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  14025.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14026.     and    o1.id = c1.id    
  14027.     and    c1.colid = r.rkey5
  14028.     union    all
  14029.     select
  14030.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14031.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14032.         PK_TABLE_NAME         = o1.name,
  14033.         PK_COLUMN_NAME         = c1.name,
  14034.         PK_COLUMN_GUID        = convert(binary(16),null),
  14035.         PK_COLUMN_PROPID    = convert(int,null),
  14036.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14037.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  14038.         FK_TABLE_NAME         = o2.name,
  14039.         FK_COLUMN_NAME         = c2.name,
  14040.         FK_COLUMN_GUID        = convert(binary(16),null),
  14041.         FK_COLUMN_PROPID    = convert(int,null),
  14042.         ORDINAL            = convert(int,6),
  14043.         UPDATE_RULE        = 'NO ACTION',
  14044.         DELETE_RULE         = 'NO ACTION'        
  14045.     from
  14046.         sysobjects o1, sysobjects o2,
  14047.         syscolumns c1, syscolumns c2,
  14048.         sysreferences r
  14049.     where    
  14050.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14051.     and    o2.name = @fk_table_name
  14052.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14053.     and    o2.id = r.fkeyid
  14054.     and    r.keycnt >= 6
  14055.     and    o2.id = c2.id
  14056.     and    c2.colid = r.fkey6
  14057.     and     r.rkeyid = o1.id
  14058.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  14059.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14060.     and    o1.id = c1.id    
  14061.     and    c1.colid = r.rkey6
  14062.     union    all
  14063.     select
  14064.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14065.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14066.         PK_TABLE_NAME         = o1.name,
  14067.         PK_COLUMN_NAME         = c1.name,
  14068.         PK_COLUMN_GUID        = convert(binary(16),null),
  14069.         PK_COLUMN_PROPID    = convert(int,null),
  14070.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14071.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  14072.         FK_TABLE_NAME         = o2.name,
  14073.         FK_COLUMN_NAME         = c2.name,
  14074.         FK_COLUMN_GUID        = convert(binary(16),null),
  14075.         FK_COLUMN_PROPID    = convert(int,null),
  14076.         ORDINAL            = convert(int,7),
  14077.         UPDATE_RULE        = 'NO ACTION',
  14078.         DELETE_RULE         = 'NO ACTION'        
  14079.     from
  14080.         sysobjects o1, sysobjects o2,
  14081.         syscolumns c1, syscolumns c2,
  14082.         sysreferences r
  14083.     where    
  14084.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14085.     and    o2.name = @fk_table_name
  14086.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14087.     and    o2.id = r.fkeyid
  14088.     and    r.keycnt >= 7
  14089.     and    o2.id = c2.id
  14090.     and    c2.colid = r.fkey7
  14091.     and     r.rkeyid = o1.id
  14092.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  14093.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14094.     and    o1.id = c1.id    
  14095.     and    c1.colid = r.rkey7
  14096.     union    all
  14097.     select
  14098.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14099.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14100.         PK_TABLE_NAME         = o1.name,
  14101.         PK_COLUMN_NAME         = c1.name,
  14102.         PK_COLUMN_GUID        = convert(binary(16),null),
  14103.         PK_COLUMN_PROPID    = convert(int,null),
  14104.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14105.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  14106.         FK_TABLE_NAME         = o2.name,
  14107.         FK_COLUMN_NAME         = c2.name,
  14108.         FK_COLUMN_GUID        = convert(binary(16),null),
  14109.         FK_COLUMN_PROPID    = convert(int,null),
  14110.         ORDINAL            = convert(int,8),
  14111.         UPDATE_RULE        = 'NO ACTION',
  14112.         DELETE_RULE         = 'NO ACTION'        
  14113.     from
  14114.         sysobjects o1, sysobjects o2,
  14115.         syscolumns c1, syscolumns c2,
  14116.         sysreferences r
  14117.     where    
  14118.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14119.     and    o2.name = @fk_table_name
  14120.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14121.     and    o2.id = r.fkeyid
  14122.     and    r.keycnt >= 8
  14123.     and    o2.id = c2.id
  14124.     and    c2.colid = r.fkey8
  14125.     and     r.rkeyid = o1.id
  14126.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  14127.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14128.     and    o1.id = c1.id    
  14129.     and    c1.colid = r.rkey8
  14130.     union    all
  14131.     select
  14132.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14133.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14134.         PK_TABLE_NAME         = o1.name,
  14135.         PK_COLUMN_NAME         = c1.name,
  14136.         PK_COLUMN_GUID        = convert(binary(16),null),
  14137.         PK_COLUMN_PROPID    = convert(int,null),
  14138.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14139.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  14140.         FK_TABLE_NAME         = o2.name,
  14141.         FK_COLUMN_NAME         = c2.name,
  14142.         FK_COLUMN_GUID        = convert(binary(16),null),
  14143.         FK_COLUMN_PROPID    = convert(int,null),
  14144.         ORDINAL            = convert(int,9),
  14145.         UPDATE_RULE        = 'NO ACTION',
  14146.         DELETE_RULE         = 'NO ACTION'        
  14147.     from
  14148.         sysobjects o1, sysobjects o2,
  14149.         syscolumns c1, syscolumns c2,
  14150.         sysreferences r
  14151.     where    
  14152.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14153.     and    o2.name = @fk_table_name
  14154.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14155.     and    o2.id = r.fkeyid
  14156.     and    r.keycnt >= 9
  14157.     and    o2.id = c2.id
  14158.     and    c2.colid = r.fkey9
  14159.     and     r.rkeyid = o1.id
  14160.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  14161.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14162.     and    o1.id = c1.id    
  14163.     and    c1.colid = r.rkey9
  14164.     union    all
  14165.     select
  14166.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14167.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14168.         PK_TABLE_NAME         = o1.name,
  14169.         PK_COLUMN_NAME         = c1.name,
  14170.         PK_COLUMN_GUID        = convert(binary(16),null),
  14171.         PK_COLUMN_PROPID    = convert(int,null),
  14172.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14173.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  14174.         FK_TABLE_NAME         = o2.name,
  14175.         FK_COLUMN_NAME         = c2.name,
  14176.         FK_COLUMN_GUID        = convert(binary(16),null),
  14177.         FK_COLUMN_PROPID    = convert(int,null),
  14178.         ORDINAL            = convert(int,10),
  14179.         UPDATE_RULE        = 'NO ACTION',
  14180.         DELETE_RULE         = 'NO ACTION'        
  14181.     from
  14182.         sysobjects o1, sysobjects o2,
  14183.         syscolumns c1, syscolumns c2,
  14184.         sysreferences r
  14185.     where    
  14186.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14187.     and    o2.name = @fk_table_name
  14188.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14189.     and    o2.id = r.fkeyid
  14190.     and    r.keycnt >= 10
  14191.     and    o2.id = c2.id
  14192.     and    c2.colid = r.fkey10
  14193.     and     r.rkeyid = o1.id
  14194.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  14195.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14196.     and    o1.id = c1.id    
  14197.     and    c1.colid = r.rkey10
  14198.     union    all
  14199.     select
  14200.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14201.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14202.         PK_TABLE_NAME         = o1.name,
  14203.         PK_COLUMN_NAME         = c1.name,
  14204.         PK_COLUMN_GUID        = convert(binary(16),null),
  14205.         PK_COLUMN_PROPID    = convert(int,null),
  14206.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14207.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  14208.         FK_TABLE_NAME         = o2.name,
  14209.         FK_COLUMN_NAME         = c2.name,
  14210.         FK_COLUMN_GUID        = convert(binary(16),null),
  14211.         FK_COLUMN_PROPID    = convert(int,null),
  14212.         ORDINAL            = convert(int,11),
  14213.         UPDATE_RULE        = 'NO ACTION',
  14214.         DELETE_RULE         = 'NO ACTION'        
  14215.     from
  14216.         sysobjects o1, sysobjects o2,
  14217.         syscolumns c1, syscolumns c2,
  14218.         sysreferences r
  14219.     where    
  14220.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14221.     and    o2.name = @fk_table_name
  14222.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14223.     and    o2.id = r.fkeyid
  14224.     and    r.keycnt >= 11
  14225.     and    o2.id = c2.id
  14226.     and    c2.colid = r.fkey11
  14227.     and     r.rkeyid = o1.id
  14228.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  14229.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14230.     and    o1.id = c1.id    
  14231.     and    c1.colid = r.rkey11
  14232.     union    all
  14233.     select
  14234.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14235.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14236.         PK_TABLE_NAME         = o1.name,
  14237.         PK_COLUMN_NAME         = c1.name,
  14238.         PK_COLUMN_GUID        = convert(binary(16),null),
  14239.         PK_COLUMN_PROPID    = convert(int,null),
  14240.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14241.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  14242.         FK_TABLE_NAME         = o2.name,
  14243.         FK_COLUMN_NAME         = c2.name,
  14244.         FK_COLUMN_GUID        = convert(binary(16),null),
  14245.         FK_COLUMN_PROPID    = convert(int,null),
  14246.         ORDINAL            = convert(int,12),
  14247.         UPDATE_RULE        = 'NO ACTION',
  14248.         DELETE_RULE         = 'NO ACTION'        
  14249.     from
  14250.         sysobjects o1, sysobjects o2,
  14251.         syscolumns c1, syscolumns c2,
  14252.         sysreferences r
  14253.     where    
  14254.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14255.     and    o2.name = @fk_table_name
  14256.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14257.     and    o2.id = r.fkeyid
  14258.     and    r.keycnt >= 12
  14259.     and    o2.id = c2.id
  14260.     and    c2.colid = r.fkey12
  14261.     and     r.rkeyid = o1.id
  14262.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  14263.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14264.     and    o1.id = c1.id    
  14265.     and    c1.colid = r.rkey12
  14266.     union    all
  14267.     select
  14268.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14269.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14270.         PK_TABLE_NAME         = o1.name,
  14271.         PK_COLUMN_NAME         = c1.name,
  14272.         PK_COLUMN_GUID        = convert(binary(16),null),
  14273.         PK_COLUMN_PROPID    = convert(int,null),
  14274.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14275.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  14276.         FK_TABLE_NAME         = o2.name,
  14277.         FK_COLUMN_NAME         = c2.name,
  14278.         FK_COLUMN_GUID        = convert(binary(16),null),
  14279.         FK_COLUMN_PROPID    = convert(int,null),
  14280.         ORDINAL            = convert(int,13),
  14281.         UPDATE_RULE        = 'NO ACTION',
  14282.         DELETE_RULE         = 'NO ACTION'        
  14283.     from
  14284.         sysobjects o1, sysobjects o2,
  14285.         syscolumns c1, syscolumns c2,
  14286.         sysreferences r
  14287.     where    
  14288.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14289.     and    o2.name = @fk_table_name
  14290.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14291.     and    o2.id = r.fkeyid
  14292.     and    r.keycnt >= 13
  14293.     and    o2.id = c2.id
  14294.     and    c2.colid = r.fkey13
  14295.     and     r.rkeyid = o1.id
  14296.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  14297.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14298.     and    o1.id = c1.id    
  14299.     and    c1.colid = r.rkey13
  14300.     union    all
  14301.     select
  14302.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14303.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14304.         PK_TABLE_NAME         = o1.name,
  14305.         PK_COLUMN_NAME         = c1.name,
  14306.         PK_COLUMN_GUID        = convert(binary(16),null),
  14307.         PK_COLUMN_PROPID    = convert(int,null),
  14308.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14309.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  14310.         FK_TABLE_NAME         = o2.name,
  14311.         FK_COLUMN_NAME         = c2.name,
  14312.         FK_COLUMN_GUID        = convert(binary(16),null),
  14313.         FK_COLUMN_PROPID    = convert(int,null),
  14314.         ORDINAL            = convert(int,14),
  14315.         UPDATE_RULE        = 'NO ACTION',
  14316.         DELETE_RULE         = 'NO ACTION'        
  14317.     from
  14318.         sysobjects o1, sysobjects o2,
  14319.         syscolumns c1, syscolumns c2,
  14320.         sysreferences r
  14321.     where    
  14322.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14323.     and    o2.name = @fk_table_name
  14324.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14325.     and    o2.id = r.fkeyid
  14326.     and    r.keycnt >= 14
  14327.     and    o2.id = c2.id
  14328.     and    c2.colid = r.fkey14
  14329.     and     r.rkeyid = o1.id
  14330.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  14331.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14332.     and    o1.id = c1.id    
  14333.     and    c1.colid = r.rkey14
  14334.     union    all
  14335.     select
  14336.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14337.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14338.         PK_TABLE_NAME         = o1.name,
  14339.         PK_COLUMN_NAME         = c1.name,
  14340.         PK_COLUMN_GUID        = convert(binary(16),null),
  14341.         PK_COLUMN_PROPID    = convert(int,null),
  14342.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14343.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  14344.         FK_TABLE_NAME         = o2.name,
  14345.         FK_COLUMN_NAME         = c2.name,
  14346.         FK_COLUMN_GUID        = convert(binary(16),null),
  14347.         FK_COLUMN_PROPID    = convert(int,null),
  14348.         ORDINAL            = convert(int,15),
  14349.         UPDATE_RULE        = 'NO ACTION',
  14350.         DELETE_RULE         = 'NO ACTION'        
  14351.     from
  14352.         sysobjects o1, sysobjects o2,
  14353.         syscolumns c1, syscolumns c2,
  14354.         sysreferences r
  14355.     where    
  14356.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14357.     and    o2.name = @fk_table_name
  14358.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14359.     and    o2.id = r.fkeyid
  14360.     and    r.keycnt >= 15
  14361.     and    o2.id = c2.id
  14362.     and    c2.colid = r.fkey15
  14363.     and     r.rkeyid = o1.id
  14364.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  14365.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14366.     and    o1.id = c1.id    
  14367.     and    c1.colid = r.rkey15
  14368.     union    all
  14369.     select
  14370.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14371.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14372.         PK_TABLE_NAME         = o1.name,
  14373.         PK_COLUMN_NAME         = c1.name,
  14374.         PK_COLUMN_GUID        = convert(binary(16),null),
  14375.         PK_COLUMN_PROPID    = convert(int,null),
  14376.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14377.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  14378.         FK_TABLE_NAME         = o2.name,
  14379.         FK_COLUMN_NAME         = c2.name,
  14380.         FK_COLUMN_GUID        = convert(binary(16),null),
  14381.         FK_COLUMN_PROPID    = convert(int,null),
  14382.         ORDINAL            = convert(int,16),
  14383.         UPDATE_RULE        = 'NO ACTION',
  14384.         DELETE_RULE         = 'NO ACTION'        
  14385.     from
  14386.         sysobjects o1, sysobjects o2,
  14387.         syscolumns c1, syscolumns c2,
  14388.         sysreferences r
  14389.     where    
  14390.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14391.     and    o2.name = @fk_table_name
  14392.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14393.     and    o2.id = r.fkeyid
  14394.     and    r.keycnt >= 16
  14395.     and    o2.id = c2.id
  14396.     and    c2.colid = r.fkey16
  14397.     and     r.rkeyid = o1.id
  14398.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  14399.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14400.     and    o1.id = c1.id    
  14401.     and    c1.colid = r.rkey16
  14402.     order by 8,9,2,3,13
  14403. go
  14404. CREATE PROCEDURE sp_foreign_keys_rowset;3
  14405.     (
  14406.     @pk_table_schema    varchar(255) = null,
  14407.     @pk_table_catalog    varchar(255) = null,
  14408.     @fk_table_schema    varchar(255) = null,
  14409.     @fk_table_catalog    varchar(255) = null
  14410.     )
  14411. as
  14412.     select
  14413.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14414.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14415.         PK_TABLE_NAME         = o1.name,
  14416.         PK_COLUMN_NAME         = c1.name,
  14417.         PK_COLUMN_GUID        = convert(binary(16),null),
  14418.         PK_COLUMN_PROPID    = convert(int,null),
  14419.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14420.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  14421.         FK_TABLE_NAME         = o2.name,
  14422.         FK_COLUMN_NAME         = c2.name,
  14423.         FK_COLUMN_GUID        = convert(binary(16),null),
  14424.         FK_COLUMN_PROPID    = convert(int,null),
  14425.         ORDINAL            = convert(int,1),
  14426.         UPDATE_RULE        = 'NO ACTION',
  14427.         DELETE_RULE         = 'NO ACTION'        
  14428.     from
  14429.         sysobjects o1, sysobjects o2,
  14430.         syscolumns c1, syscolumns c2,
  14431.         sysreferences r
  14432.     where    
  14433.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14434.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14435.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14436.     and    o1.id = r.rkeyid
  14437.     and    o1.id = c1.id
  14438.     and    c1.colid = r.rkey1
  14439.     and     r.fkeyid = o2.id
  14440.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14441.     and    o2.id = c2.id    
  14442.     and    c2.colid = r.fkey1
  14443.     union all
  14444.     select
  14445.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14446.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14447.         PK_TABLE_NAME         = o1.name,
  14448.         PK_COLUMN_NAME         = c1.name,
  14449.         PK_COLUMN_GUID        = convert(binary(16),null),
  14450.         PK_COLUMN_PROPID    = convert(int,null),
  14451.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14452.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  14453.         FK_TABLE_NAME         = o2.name,
  14454.         FK_COLUMN_NAME         = c2.name,
  14455.         FK_COLUMN_GUID        = convert(binary(16),null),
  14456.         FK_COLUMN_PROPID    = convert(int,null),
  14457.         ORDINAL            = convert(int,2),
  14458.         UPDATE_RULE        = 'NO ACTION',
  14459.         DELETE_RULE         = 'NO ACTION'        
  14460.     from
  14461.         sysobjects o1, sysobjects o2,
  14462.         syscolumns c1, syscolumns c2,
  14463.         sysreferences r
  14464.     where    
  14465.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14466.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14467.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14468.     and    o1.id = r.rkeyid
  14469.     and    r.keycnt >= 2
  14470.     and    o1.id = c1.id
  14471.     and    c1.colid = r.rkey2
  14472.     and     r.fkeyid = o2.id
  14473.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14474.     and    o2.id = c2.id    
  14475.     and    c2.colid = r.fkey2
  14476.     union all
  14477.     select
  14478.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14479.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14480.         PK_TABLE_NAME         = o1.name,
  14481.         PK_COLUMN_NAME         = c1.name,
  14482.         PK_COLUMN_GUID        = convert(binary(16),null),
  14483.         PK_COLUMN_PROPID    = convert(int,null),
  14484.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14485.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  14486.         FK_TABLE_NAME         = o2.name,
  14487.         FK_COLUMN_NAME         = c2.name,
  14488.         FK_COLUMN_GUID        = convert(binary(16),null),
  14489.         FK_COLUMN_PROPID    = convert(int,null),
  14490.         ORDINAL            = convert(int,3),
  14491.         UPDATE_RULE        = 'NO ACTION',
  14492.         DELETE_RULE         = 'NO ACTION'        
  14493.     from
  14494.         sysobjects o1, sysobjects o2,
  14495.         syscolumns c1, syscolumns c2,
  14496.         sysreferences r
  14497.     where    
  14498.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14499.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14500.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14501.     and    o1.id = r.rkeyid
  14502.     and    r.keycnt >= 3
  14503.     and    o1.id = c1.id
  14504.     and    c1.colid = r.rkey3
  14505.     and     r.fkeyid = o2.id
  14506.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14507.     and    o2.id = c2.id    
  14508.     and    c2.colid = r.fkey3
  14509.     union all
  14510.     select
  14511.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14512.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14513.         PK_TABLE_NAME         = o1.name,
  14514.         PK_COLUMN_NAME         = c1.name,
  14515.         PK_COLUMN_GUID        = convert(binary(16),null),
  14516.         PK_COLUMN_PROPID    = convert(int,null),
  14517.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14518.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  14519.         FK_TABLE_NAME         = o2.name,
  14520.         FK_COLUMN_NAME         = c2.name,
  14521.         FK_COLUMN_GUID        = convert(binary(16),null),
  14522.         FK_COLUMN_PROPID    = convert(int,null),
  14523.         ORDINAL            = convert(int,4),
  14524.         UPDATE_RULE        = 'NO ACTION',
  14525.         DELETE_RULE         = 'NO ACTION'        
  14526.     from
  14527.         sysobjects o1, sysobjects o2,
  14528.         syscolumns c1, syscolumns c2,
  14529.         sysreferences r
  14530.     where    
  14531.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14532.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14533.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14534.     and    o1.id = r.rkeyid
  14535.     and    r.keycnt >= 4
  14536.     and    o1.id = c1.id
  14537.     and    c1.colid = r.rkey4
  14538.     and     r.fkeyid = o2.id
  14539.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14540.     and    o2.id = c2.id    
  14541.     and    c2.colid = r.fkey4
  14542.     union all
  14543.     select
  14544.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14545.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14546.         PK_TABLE_NAME         = o1.name,
  14547.         PK_COLUMN_NAME         = c1.name,
  14548.         PK_COLUMN_GUID        = convert(binary(16),null),
  14549.         PK_COLUMN_PROPID    = convert(int,null),
  14550.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14551.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  14552.         FK_TABLE_NAME         = o2.name,
  14553.         FK_COLUMN_NAME         = c2.name,
  14554.         FK_COLUMN_GUID        = convert(binary(16),null),
  14555.         FK_COLUMN_PROPID    = convert(int,null),
  14556.         ORDINAL            = convert(int,5),
  14557.         UPDATE_RULE        = 'NO ACTION',
  14558.         DELETE_RULE         = 'NO ACTION'        
  14559.     from
  14560.         sysobjects o1, sysobjects o2,
  14561.         syscolumns c1, syscolumns c2,
  14562.         sysreferences r
  14563.     where    
  14564.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14565.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14566.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14567.     and    o1.id = r.rkeyid
  14568.     and    r.keycnt >= 5
  14569.     and    o1.id = c1.id
  14570.     and    c1.colid = r.rkey5
  14571.     and     r.fkeyid = o2.id
  14572.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14573.     and    o2.id = c2.id    
  14574.     and    c2.colid = r.fkey5
  14575.     union all
  14576.     select
  14577.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14578.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14579.         PK_TABLE_NAME         = o1.name,
  14580.         PK_COLUMN_NAME         = c1.name,
  14581.         PK_COLUMN_GUID        = convert(binary(16),null),
  14582.         PK_COLUMN_PROPID    = convert(int,null),
  14583.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14584.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  14585.         FK_TABLE_NAME         = o2.name,
  14586.         FK_COLUMN_NAME         = c2.name,
  14587.         FK_COLUMN_GUID        = convert(binary(16),null),
  14588.         FK_COLUMN_PROPID    = convert(int,null),
  14589.         ORDINAL            = convert(int,6),
  14590.         UPDATE_RULE        = 'NO ACTION',
  14591.         DELETE_RULE         = 'NO ACTION'        
  14592.     from
  14593.         sysobjects o1, sysobjects o2,
  14594.         syscolumns c1, syscolumns c2,
  14595.         sysreferences r
  14596.     where    
  14597.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14598.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14599.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14600.     and    o1.id = r.rkeyid
  14601.     and    r.keycnt >= 6
  14602.     and    o1.id = c1.id
  14603.     and    c1.colid = r.rkey6
  14604.     and     r.fkeyid = o2.id
  14605.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14606.     and    o2.id = c2.id    
  14607.     and    c2.colid = r.fkey6
  14608.     union all
  14609.     select
  14610.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14611.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14612.         PK_TABLE_NAME         = o1.name,
  14613.         PK_COLUMN_NAME         = c1.name,
  14614.         PK_COLUMN_GUID        = convert(binary(16),null),
  14615.         PK_COLUMN_PROPID    = convert(int,null),
  14616.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14617.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  14618.         FK_TABLE_NAME         = o2.name,
  14619.         FK_COLUMN_NAME         = c2.name,
  14620.         FK_COLUMN_GUID        = convert(binary(16),null),
  14621.         FK_COLUMN_PROPID    = convert(int,null),
  14622.         ORDINAL            = convert(int,7),
  14623.         UPDATE_RULE        = 'NO ACTION',
  14624.         DELETE_RULE         = 'NO ACTION'        
  14625.     from
  14626.         sysobjects o1, sysobjects o2,
  14627.         syscolumns c1, syscolumns c2,
  14628.         sysreferences r
  14629.     where    
  14630.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14631.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14632.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14633.     and    o1.id = r.rkeyid
  14634.     and    r.keycnt >= 7
  14635.     and    o1.id = c1.id
  14636.     and    c1.colid = r.rkey7
  14637.     and     r.fkeyid = o2.id
  14638.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14639.     and    o2.id = c2.id    
  14640.     and    c2.colid = r.fkey7
  14641.     union all
  14642.     select
  14643.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14644.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14645.         PK_TABLE_NAME         = o1.name,
  14646.         PK_COLUMN_NAME         = c1.name,
  14647.         PK_COLUMN_GUID        = convert(binary(16),null),
  14648.         PK_COLUMN_PROPID    = convert(int,null),
  14649.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14650.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  14651.         FK_TABLE_NAME         = o2.name,
  14652.         FK_COLUMN_NAME         = c2.name,
  14653.         FK_COLUMN_GUID        = convert(binary(16),null),
  14654.         FK_COLUMN_PROPID    = convert(int,null),
  14655.         ORDINAL            = convert(int,8),
  14656.         UPDATE_RULE        = 'NO ACTION',
  14657.         DELETE_RULE         = 'NO ACTION'        
  14658.     from
  14659.         sysobjects o1, sysobjects o2,
  14660.         syscolumns c1, syscolumns c2,
  14661.         sysreferences r
  14662.     where    
  14663.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14664.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14665.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14666.     and    o1.id = r.rkeyid
  14667.     and    r.keycnt >= 8
  14668.     and    o1.id = c1.id
  14669.     and    c1.colid = r.rkey8
  14670.     and     r.fkeyid = o2.id
  14671.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14672.     and    o2.id = c2.id    
  14673.     and    c2.colid = r.fkey8
  14674.     union all
  14675.     select
  14676.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14677.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14678.         PK_TABLE_NAME         = o1.name,
  14679.         PK_COLUMN_NAME         = c1.name,
  14680.         PK_COLUMN_GUID        = convert(binary(16),null),
  14681.         PK_COLUMN_PROPID    = convert(int,null),
  14682.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14683.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  14684.         FK_TABLE_NAME         = o2.name,
  14685.         FK_COLUMN_NAME         = c2.name,
  14686.         FK_COLUMN_GUID        = convert(binary(16),null),
  14687.         FK_COLUMN_PROPID    = convert(int,null),
  14688.         ORDINAL            = convert(int,9),
  14689.         UPDATE_RULE        = 'NO ACTION',
  14690.         DELETE_RULE         = 'NO ACTION'        
  14691.     from
  14692.         sysobjects o1, sysobjects o2,
  14693.         syscolumns c1, syscolumns c2,
  14694.         sysreferences r
  14695.     where    
  14696.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14697.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14698.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14699.     and    o1.id = r.rkeyid
  14700.     and    r.keycnt >= 9
  14701.     and    o1.id = c1.id
  14702.     and    c1.colid = r.rkey9
  14703.     and     r.fkeyid = o2.id
  14704.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14705.     and    o2.id = c2.id    
  14706.     and    c2.colid = r.fkey9
  14707.     union all
  14708.     select
  14709.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14710.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14711.         PK_TABLE_NAME         = o1.name,
  14712.         PK_COLUMN_NAME         = c1.name,
  14713.         PK_COLUMN_GUID        = convert(binary(16),null),
  14714.         PK_COLUMN_PROPID    = convert(int,null),
  14715.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14716.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  14717.         FK_TABLE_NAME         = o2.name,
  14718.         FK_COLUMN_NAME         = c2.name,
  14719.         FK_COLUMN_GUID        = convert(binary(16),null),
  14720.         FK_COLUMN_PROPID    = convert(int,null),
  14721.         ORDINAL            = convert(int,10),
  14722.         UPDATE_RULE        = 'NO ACTION',
  14723.         DELETE_RULE         = 'NO ACTION'        
  14724.     from
  14725.         sysobjects o1, sysobjects o2,
  14726.         syscolumns c1, syscolumns c2,
  14727.         sysreferences r
  14728.     where    
  14729.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14730.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14731.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14732.     and    o1.id = r.rkeyid
  14733.     and    r.keycnt >= 10
  14734.     and    o1.id = c1.id
  14735.     and    c1.colid = r.rkey10
  14736.     and     r.fkeyid = o2.id
  14737.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14738.     and    o2.id = c2.id    
  14739.     and    c2.colid = r.fkey10
  14740.     union all
  14741.     select
  14742.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14743.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14744.         PK_TABLE_NAME         = o1.name,
  14745.         PK_COLUMN_NAME         = c1.name,
  14746.         PK_COLUMN_GUID        = convert(binary(16),null),
  14747.         PK_COLUMN_PROPID    = convert(int,null),
  14748.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14749.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  14750.         FK_TABLE_NAME         = o2.name,
  14751.         FK_COLUMN_NAME         = c2.name,
  14752.         FK_COLUMN_GUID        = convert(binary(16),null),
  14753.         FK_COLUMN_PROPID    = convert(int,null),
  14754.         ORDINAL            = convert(int,11),
  14755.         UPDATE_RULE        = 'NO ACTION',
  14756.         DELETE_RULE         = 'NO ACTION'        
  14757.     from
  14758.         sysobjects o1, sysobjects o2,
  14759.         syscolumns c1, syscolumns c2,
  14760.         sysreferences r
  14761.     where    
  14762.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14763.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14764.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14765.     and    o1.id = r.rkeyid
  14766.     and    r.keycnt >= 11
  14767.     and    o1.id = c1.id
  14768.     and    c1.colid = r.rkey11
  14769.     and     r.fkeyid = o2.id
  14770.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14771.     and    o2.id = c2.id    
  14772.     and    c2.colid = r.fkey11
  14773.     union all
  14774.     select
  14775.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14776.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14777.         PK_TABLE_NAME         = o1.name,
  14778.         PK_COLUMN_NAME         = c1.name,
  14779.         PK_COLUMN_GUID        = convert(binary(16),null),
  14780.         PK_COLUMN_PROPID    = convert(int,null),
  14781.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14782.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  14783.         FK_TABLE_NAME         = o2.name,
  14784.         FK_COLUMN_NAME         = c2.name,
  14785.         FK_COLUMN_GUID        = convert(binary(16),null),
  14786.         FK_COLUMN_PROPID    = convert(int,null),
  14787.         ORDINAL            = convert(int,12),
  14788.         UPDATE_RULE        = 'NO ACTION',
  14789.         DELETE_RULE         = 'NO ACTION'        
  14790.     from
  14791.         sysobjects o1, sysobjects o2,
  14792.         syscolumns c1, syscolumns c2,
  14793.         sysreferences r
  14794.     where    
  14795.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14796.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14797.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14798.     and    o1.id = r.rkeyid
  14799.     and    r.keycnt >= 12
  14800.     and    o1.id = c1.id
  14801.     and    c1.colid = r.rkey12
  14802.     and     r.fkeyid = o2.id
  14803.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14804.     and    o2.id = c2.id    
  14805.     and    c2.colid = r.fkey12
  14806.     union all
  14807.     select
  14808.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14809.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14810.         PK_TABLE_NAME         = o1.name,
  14811.         PK_COLUMN_NAME         = c1.name,
  14812.         PK_COLUMN_GUID        = convert(binary(16),null),
  14813.         PK_COLUMN_PROPID    = convert(int,null),
  14814.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14815.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  14816.         FK_TABLE_NAME         = o2.name,
  14817.         FK_COLUMN_NAME         = c2.name,
  14818.         FK_COLUMN_GUID        = convert(binary(16),null),
  14819.         FK_COLUMN_PROPID    = convert(int,null),
  14820.         ORDINAL            = convert(int,13),
  14821.         UPDATE_RULE        = 'NO ACTION',
  14822.         DELETE_RULE         = 'NO ACTION'        
  14823.     from
  14824.         sysobjects o1, sysobjects o2,
  14825.         syscolumns c1, syscolumns c2,
  14826.         sysreferences r
  14827.     where    
  14828.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14829.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14830.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14831.     and    o1.id = r.rkeyid
  14832.     and    r.keycnt >= 13
  14833.     and    o1.id = c1.id
  14834.     and    c1.colid = r.rkey13
  14835.     and     r.fkeyid = o2.id
  14836.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14837.     and    o2.id = c2.id    
  14838.     and    c2.colid = r.fkey13
  14839.     union all
  14840.     select
  14841.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14842.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14843.         PK_TABLE_NAME         = o1.name,
  14844.         PK_COLUMN_NAME         = c1.name,
  14845.         PK_COLUMN_GUID        = convert(binary(16),null),
  14846.         PK_COLUMN_PROPID    = convert(int,null),
  14847.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14848.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  14849.         FK_TABLE_NAME         = o2.name,
  14850.         FK_COLUMN_NAME         = c2.name,
  14851.         FK_COLUMN_GUID        = convert(binary(16),null),
  14852.         FK_COLUMN_PROPID    = convert(int,null),
  14853.         ORDINAL            = convert(int,14),
  14854.         UPDATE_RULE        = 'NO ACTION',
  14855.         DELETE_RULE         = 'NO ACTION'        
  14856.     from
  14857.         sysobjects o1, sysobjects o2,
  14858.         syscolumns c1, syscolumns c2,
  14859.         sysreferences r
  14860.     where    
  14861.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14862.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14863.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14864.     and    o1.id = r.rkeyid
  14865.     and    r.keycnt >= 14
  14866.     and    o1.id = c1.id
  14867.     and    c1.colid = r.rkey14
  14868.     and     r.fkeyid = o2.id
  14869.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14870.     and    o2.id = c2.id    
  14871.     and    c2.colid = r.fkey14
  14872.     union all
  14873.     select
  14874.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14875.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14876.         PK_TABLE_NAME         = o1.name,
  14877.         PK_COLUMN_NAME         = c1.name,
  14878.         PK_COLUMN_GUID        = convert(binary(16),null),
  14879.         PK_COLUMN_PROPID    = convert(int,null),
  14880.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14881.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  14882.         FK_TABLE_NAME         = o2.name,
  14883.         FK_COLUMN_NAME         = c2.name,
  14884.         FK_COLUMN_GUID        = convert(binary(16),null),
  14885.         FK_COLUMN_PROPID    = convert(int,null),
  14886.         ORDINAL            = convert(int,15),
  14887.         UPDATE_RULE        = 'NO ACTION',
  14888.         DELETE_RULE         = 'NO ACTION'        
  14889.     from
  14890.         sysobjects o1, sysobjects o2,
  14891.         syscolumns c1, syscolumns c2,
  14892.         sysreferences r
  14893.     where    
  14894.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14895.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14896.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14897.     and    o1.id = r.rkeyid
  14898.     and    r.keycnt >= 15
  14899.     and    o1.id = c1.id
  14900.     and    c1.colid = r.rkey15
  14901.     and     r.fkeyid = o2.id
  14902.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14903.     and    o2.id = c2.id    
  14904.     and    c2.colid = r.fkey15
  14905.     union all
  14906.     select
  14907.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14908.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14909.         PK_TABLE_NAME         = o1.name,
  14910.         PK_COLUMN_NAME         = c1.name,
  14911.         PK_COLUMN_GUID        = convert(binary(16),null),
  14912.         PK_COLUMN_PROPID    = convert(int,null),
  14913.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14914.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  14915.         FK_TABLE_NAME         = o2.name,
  14916.         FK_COLUMN_NAME         = c2.name,
  14917.         FK_COLUMN_GUID        = convert(binary(16),null),
  14918.         FK_COLUMN_PROPID    = convert(int,null),
  14919.         ORDINAL            = convert(int,16),
  14920.         UPDATE_RULE        = 'NO ACTION',
  14921.         DELETE_RULE         = 'NO ACTION'        
  14922.     from
  14923.         sysobjects o1, sysobjects o2,
  14924.         syscolumns c1, syscolumns c2,
  14925.         sysreferences r
  14926.     where    
  14927.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14928.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14929.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14930.     and    o1.id = r.rkeyid
  14931.     and    o1.id = c1.id
  14932.     and    r.keycnt >= 16
  14933.     and    c1.colid = r.rkey16
  14934.     and     r.fkeyid = o2.id
  14935.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14936.     and    o2.id = c2.id    
  14937.     and    c2.colid = r.fkey16
  14938.     order by 8,9,2,3,13
  14939. go
  14940. CREATE PROCEDURE sp_foreign_keys_rowset;4
  14941.     (
  14942.     @pk_table_name        varchar(255) = null,
  14943.     @pk_table_schema    varchar(255) = null,
  14944.     @fk_table_name        varchar(255) = null,
  14945.     @fk_table_schema    varchar(255) = null
  14946.     )
  14947. as
  14948. IF @pk_table_name is not null
  14949.     BEGIN
  14950.     select
  14951.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14952.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14953.         PK_TABLE_NAME         = o1.name,
  14954.         PK_COLUMN_NAME         = c1.name,
  14955.         PK_COLUMN_GUID        = convert(binary(16),null),
  14956.         PK_COLUMN_PROPID    = convert(int,null),
  14957.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14958.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  14959.         FK_TABLE_NAME         = o2.name,
  14960.         FK_COLUMN_NAME         = c2.name,
  14961.         FK_COLUMN_GUID        = convert(binary(16),null),
  14962.         FK_COLUMN_PROPID    = convert(int,null),
  14963.         ORDINAL            = convert(int,1),
  14964.         UPDATE_RULE        = 'NO ACTION',
  14965.         DELETE_RULE         = 'NO ACTION'        
  14966.     from
  14967.         sysobjects o1, sysobjects o2,
  14968.         syscolumns c1, syscolumns c2,
  14969.         sysreferences r
  14970.     where    
  14971.         o1.name = @pk_table_name
  14972.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14973.     and    o1.id = r.rkeyid
  14974.     and    o1.id = c1.id
  14975.     and    c1.colid = r.rkey1
  14976.     and     r.fkeyid = o2.id
  14977.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  14978.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14979.     and    o2.id = c2.id    
  14980.     and    c2.colid = r.fkey1
  14981.     union all
  14982.     select
  14983.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14984.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14985.         PK_TABLE_NAME         = o1.name,
  14986.         PK_COLUMN_NAME         = c1.name,
  14987.         PK_COLUMN_GUID        = convert(binary(16),null),
  14988.         PK_COLUMN_PROPID    = convert(int,null),
  14989.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14990.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  14991.         FK_TABLE_NAME         = o2.name,
  14992.         FK_COLUMN_NAME         = c2.name,
  14993.         FK_COLUMN_GUID        = convert(binary(16),null),
  14994.         FK_COLUMN_PROPID    = convert(int,null),
  14995.         ORDINAL            = convert(int,2),
  14996.         UPDATE_RULE        = 'NO ACTION',
  14997.         DELETE_RULE         = 'NO ACTION'        
  14998.     from
  14999.         sysobjects o1, sysobjects o2,
  15000.         syscolumns c1, syscolumns c2,
  15001.         sysreferences r
  15002.     where    
  15003.         o1.name = @pk_table_name
  15004.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15005.     and    o1.id = r.rkeyid
  15006.     and    o1.id = c1.id
  15007.     and    c1.colid = r.rkey2
  15008.     and    r.keycnt >= 2
  15009.     and     r.fkeyid = o2.id
  15010.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  15011.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15012.     and    o2.id = c2.id    
  15013.     and    c2.colid = r.fkey2
  15014.     union all
  15015.     select
  15016.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15017.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15018.         PK_TABLE_NAME         = o1.name,
  15019.         PK_COLUMN_NAME         = c1.name,
  15020.         PK_COLUMN_GUID        = convert(binary(16),null),
  15021.         PK_COLUMN_PROPID    = convert(int,null),
  15022.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15023.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  15024.         FK_TABLE_NAME         = o2.name,
  15025.         FK_COLUMN_NAME         = c2.name,
  15026.         FK_COLUMN_GUID        = convert(binary(16),null),
  15027.         FK_COLUMN_PROPID    = convert(int,null),
  15028.         ORDINAL            = convert(int,3),
  15029.         UPDATE_RULE        = 'NO ACTION',
  15030.         DELETE_RULE         = 'NO ACTION'        
  15031.     from
  15032.         sysobjects o1, sysobjects o2,
  15033.         syscolumns c1, syscolumns c2,
  15034.         sysreferences r
  15035.     where    
  15036.         o1.name = @pk_table_name
  15037.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15038.     and    o1.id = r.rkeyid
  15039.     and    o1.id = c1.id
  15040.     and    c1.colid = r.rkey3
  15041.     and    r.keycnt >= 3
  15042.     and     r.fkeyid = o2.id
  15043.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  15044.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15045.     and    o2.id = c2.id    
  15046.     and    c2.colid = r.fkey3
  15047.     union all
  15048.     select
  15049.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15050.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15051.         PK_TABLE_NAME         = o1.name,
  15052.         PK_COLUMN_NAME         = c1.name,
  15053.         PK_COLUMN_GUID        = convert(binary(16),null),
  15054.         PK_COLUMN_PROPID    = convert(int,null),
  15055.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15056.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  15057.         FK_TABLE_NAME         = o2.name,
  15058.         FK_COLUMN_NAME         = c2.name,
  15059.         FK_COLUMN_GUID        = convert(binary(16),null),
  15060.         FK_COLUMN_PROPID    = convert(int,null),
  15061.         ORDINAL            = convert(int,4),
  15062.         UPDATE_RULE        = 'NO ACTION',
  15063.         DELETE_RULE         = 'NO ACTION'        
  15064.     from
  15065.         sysobjects o1, sysobjects o2,
  15066.         syscolumns c1, syscolumns c2,
  15067.         sysreferences r
  15068.     where    
  15069.         o1.name = @pk_table_name
  15070.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15071.     and    o1.id = r.rkeyid
  15072.     and    r.keycnt >= 4
  15073.     and    o1.id = c1.id
  15074.     and    c1.colid = r.rkey4
  15075.     and     r.fkeyid = o2.id
  15076.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  15077.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15078.     and    o2.id = c2.id    
  15079.     and    c2.colid = r.fkey4
  15080.     union all
  15081.     select
  15082.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15083.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15084.         PK_TABLE_NAME         = o1.name,
  15085.         PK_COLUMN_NAME         = c1.name,
  15086.         PK_COLUMN_GUID        = convert(binary(16),null),
  15087.         PK_COLUMN_PROPID    = convert(int,null),
  15088.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15089.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  15090.         FK_TABLE_NAME         = o2.name,
  15091.         FK_COLUMN_NAME         = c2.name,
  15092.         FK_COLUMN_GUID        = convert(binary(16),null),
  15093.         FK_COLUMN_PROPID    = convert(int,null),
  15094.         ORDINAL            = convert(int,5),
  15095.         UPDATE_RULE        = 'NO ACTION',
  15096.         DELETE_RULE         = 'NO ACTION'        
  15097.     from
  15098.         sysobjects o1, sysobjects o2,
  15099.         syscolumns c1, syscolumns c2,
  15100.         sysreferences r
  15101.     where    
  15102.         o1.name = @pk_table_name
  15103.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15104.     and    o1.id = r.rkeyid
  15105.     and    r.keycnt >= 5
  15106.     and    o1.id = c1.id
  15107.     and    c1.colid = r.rkey5
  15108.     and     r.fkeyid = o2.id
  15109.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  15110.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15111.     and    o2.id = c2.id    
  15112.     and    c2.colid = r.fkey5
  15113.     union all
  15114.     select
  15115.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15116.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15117.         PK_TABLE_NAME         = o1.name,
  15118.         PK_COLUMN_NAME         = c1.name,
  15119.         PK_COLUMN_GUID        = convert(binary(16),null),
  15120.         PK_COLUMN_PROPID    = convert(int,null),
  15121.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15122.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  15123.         FK_TABLE_NAME         = o2.name,
  15124.         FK_COLUMN_NAME         = c2.name,
  15125.         FK_COLUMN_GUID        = convert(binary(16),null),
  15126.         FK_COLUMN_PROPID    = convert(int,null),
  15127.         ORDINAL            = convert(int,6),
  15128.         UPDATE_RULE        = 'NO ACTION',
  15129.         DELETE_RULE         = 'NO ACTION'        
  15130.     from
  15131.         sysobjects o1, sysobjects o2,
  15132.         syscolumns c1, syscolumns c2,
  15133.         sysreferences r
  15134.     where    
  15135.         o1.name = @pk_table_name
  15136.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15137.     and    o1.id = r.rkeyid
  15138.     and    r.keycnt >= 6
  15139.     and    o1.id = c1.id
  15140.     and    c1.colid = r.rkey6
  15141.     and     r.fkeyid = o2.id
  15142.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  15143.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15144.     and    o2.id = c2.id    
  15145.     and    c2.colid = r.fkey6
  15146.     union all
  15147.     select
  15148.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15149.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15150.         PK_TABLE_NAME         = o1.name,
  15151.         PK_COLUMN_NAME         = c1.name,
  15152.         PK_COLUMN_GUID        = convert(binary(16),null),
  15153.         PK_COLUMN_PROPID    = convert(int,null),
  15154.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15155.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  15156.         FK_TABLE_NAME         = o2.name,
  15157.         FK_COLUMN_NAME         = c2.name,
  15158.         FK_COLUMN_GUID        = convert(binary(16),null),
  15159.         FK_COLUMN_PROPID    = convert(int,null),
  15160.         ORDINAL            = convert(int,7),
  15161.         UPDATE_RULE        = 'NO ACTION',
  15162.         DELETE_RULE         = 'NO ACTION'        
  15163.     from
  15164.         sysobjects o1, sysobjects o2,
  15165.         syscolumns c1, syscolumns c2,
  15166.         sysreferences r
  15167.     where    
  15168.         o1.name = @pk_table_name
  15169.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15170.     and    o1.id = r.rkeyid
  15171.     and    r.keycnt >= 7
  15172.     and    o1.id = c1.id
  15173.     and    c1.colid = r.rkey7
  15174.     and     r.fkeyid = o2.id
  15175.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  15176.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15177.     and    o2.id = c2.id    
  15178.     and    c2.colid = r.fkey7
  15179.     union all
  15180.     select
  15181.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15182.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15183.         PK_TABLE_NAME         = o1.name,
  15184.         PK_COLUMN_NAME         = c1.name,
  15185.         PK_COLUMN_GUID        = convert(binary(16),null),
  15186.         PK_COLUMN_PROPID    = convert(int,null),
  15187.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15188.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  15189.         FK_TABLE_NAME         = o2.name,
  15190.         FK_COLUMN_NAME         = c2.name,
  15191.         FK_COLUMN_GUID        = convert(binary(16),null),
  15192.         FK_COLUMN_PROPID    = convert(int,null),
  15193.         ORDINAL            = convert(int,8),
  15194.         UPDATE_RULE        = 'NO ACTION',
  15195.         DELETE_RULE         = 'NO ACTION'        
  15196.     from
  15197.         sysobjects o1, sysobjects o2,
  15198.         syscolumns c1, syscolumns c2,
  15199.         sysreferences r
  15200.     where    
  15201.         o1.name = @pk_table_name
  15202.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15203.     and    o1.id = r.rkeyid
  15204.     and    r.keycnt >= 8
  15205.     and    o1.id = c1.id
  15206.     and    c1.colid = r.rkey8
  15207.     and     r.fkeyid = o2.id
  15208.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  15209.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15210.     and    o2.id = c2.id    
  15211.     and    c2.colid = r.fkey8
  15212.     union all
  15213.     select
  15214.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15215.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15216.         PK_TABLE_NAME         = o1.name,
  15217.         PK_COLUMN_NAME         = c1.name,
  15218.         PK_COLUMN_GUID        = convert(binary(16),null),
  15219.         PK_COLUMN_PROPID    = convert(int,null),
  15220.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15221.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  15222.         FK_TABLE_NAME         = o2.name,
  15223.         FK_COLUMN_NAME         = c2.name,
  15224.         FK_COLUMN_GUID        = convert(binary(16),null),
  15225.         FK_COLUMN_PROPID    = convert(int,null),
  15226.         ORDINAL            = convert(int,9),
  15227.         UPDATE_RULE        = 'NO ACTION',
  15228.         DELETE_RULE         = 'NO ACTION'        
  15229.     from
  15230.         sysobjects o1, sysobjects o2,
  15231.         syscolumns c1, syscolumns c2,
  15232.         sysreferences r
  15233.     where    
  15234.         o1.name = @pk_table_name
  15235.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15236.     and    o1.id = r.rkeyid
  15237.     and    r.keycnt >= 9
  15238.     and    o1.id = c1.id
  15239.     and    c1.colid = r.rkey9
  15240.     and     r.fkeyid = o2.id
  15241.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  15242.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15243.     and    o2.id = c2.id    
  15244.     and    c2.colid = r.fkey9
  15245.     union all
  15246.     select
  15247.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15248.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15249.         PK_TABLE_NAME         = o1.name,
  15250.         PK_COLUMN_NAME         = c1.name,
  15251.         PK_COLUMN_GUID        = convert(binary(16),null),
  15252.         PK_COLUMN_PROPID    = convert(int,null),
  15253.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15254.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  15255.         FK_TABLE_NAME         = o2.name,
  15256.         FK_COLUMN_NAME         = c2.name,
  15257.         FK_COLUMN_GUID        = convert(binary(16),null),
  15258.         FK_COLUMN_PROPID    = convert(int,null),
  15259.         ORDINAL            = convert(int,10),
  15260.         UPDATE_RULE        = 'NO ACTION',
  15261.         DELETE_RULE         = 'NO ACTION'        
  15262.     from
  15263.         sysobjects o1, sysobjects o2,
  15264.         syscolumns c1, syscolumns c2,
  15265.         sysreferences r
  15266.     where    
  15267.         o1.name = @pk_table_name
  15268.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15269.     and    o1.id = r.rkeyid
  15270.     and    r.keycnt >= 10
  15271.     and    o1.id = c1.id
  15272.     and    c1.colid = r.rkey10
  15273.     and     r.fkeyid = o2.id
  15274.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  15275.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15276.     and    o2.id = c2.id    
  15277.     and    c2.colid = r.fkey10
  15278.     union all
  15279.     select
  15280.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15281.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15282.         PK_TABLE_NAME         = o1.name,
  15283.         PK_COLUMN_NAME         = c1.name,
  15284.         PK_COLUMN_GUID        = convert(binary(16),null),
  15285.         PK_COLUMN_PROPID    = convert(int,null),
  15286.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15287.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  15288.         FK_TABLE_NAME         = o2.name,
  15289.         FK_COLUMN_NAME         = c2.name,
  15290.         FK_COLUMN_GUID        = convert(binary(16),null),
  15291.         FK_COLUMN_PROPID    = convert(int,null),
  15292.         ORDINAL            = convert(int,11),
  15293.         UPDATE_RULE        = 'NO ACTION',
  15294.         DELETE_RULE         = 'NO ACTION'        
  15295.     from
  15296.         sysobjects o1, sysobjects o2,
  15297.         syscolumns c1, syscolumns c2,
  15298.         sysreferences r
  15299.     where    
  15300.         o1.name = @pk_table_name
  15301.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15302.     and    o1.id = r.rkeyid
  15303.     and    r.keycnt >= 11
  15304.     and    o1.id = c1.id
  15305.     and    c1.colid = r.rkey11
  15306.     and     r.fkeyid = o2.id
  15307.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  15308.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15309.     and    o2.id = c2.id    
  15310.     and    c2.colid = r.fkey11
  15311.     union all
  15312.     select
  15313.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15314.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15315.         PK_TABLE_NAME         = o1.name,
  15316.         PK_COLUMN_NAME         = c1.name,
  15317.         PK_COLUMN_GUID        = convert(binary(16),null),
  15318.         PK_COLUMN_PROPID    = convert(int,null),
  15319.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15320.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  15321.         FK_TABLE_NAME         = o2.name,
  15322.         FK_COLUMN_NAME         = c2.name,
  15323.         FK_COLUMN_GUID        = convert(binary(16),null),
  15324.         FK_COLUMN_PROPID    = convert(int,null),
  15325.         ORDINAL            = convert(int,12),
  15326.         UPDATE_RULE        = 'NO ACTION',
  15327.         DELETE_RULE         = 'NO ACTION'        
  15328.     from
  15329.         sysobjects o1, sysobjects o2,
  15330.         syscolumns c1, syscolumns c2,
  15331.         sysreferences r
  15332.     where    
  15333.         o1.name = @pk_table_name
  15334.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15335.     and    o1.id = r.rkeyid
  15336.     and    r.keycnt >= 12
  15337.     and    o1.id = c1.id
  15338.     and    c1.colid = r.rkey12
  15339.     and     r.fkeyid = o2.id
  15340.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  15341.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15342.     and    o2.id = c2.id    
  15343.     and    c2.colid = r.fkey12
  15344.     union all
  15345.     select
  15346.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15347.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15348.         PK_TABLE_NAME         = o1.name,
  15349.         PK_COLUMN_NAME         = c1.name,
  15350.         PK_COLUMN_GUID        = convert(binary(16),null),
  15351.         PK_COLUMN_PROPID    = convert(int,null),
  15352.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15353.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  15354.         FK_TABLE_NAME         = o2.name,
  15355.         FK_COLUMN_NAME         = c2.name,
  15356.         FK_COLUMN_GUID        = convert(binary(16),null),
  15357.         FK_COLUMN_PROPID    = convert(int,null),
  15358.         ORDINAL            = convert(int,13),
  15359.         UPDATE_RULE        = 'NO ACTION',
  15360.         DELETE_RULE         = 'NO ACTION'        
  15361.     from
  15362.         sysobjects o1, sysobjects o2,
  15363.         syscolumns c1, syscolumns c2,
  15364.         sysreferences r
  15365.     where    
  15366.         o1.name = @pk_table_name
  15367.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15368.     and    o1.id = r.rkeyid
  15369.     and    r.keycnt >= 13
  15370.     and    o1.id = c1.id
  15371.     and    c1.colid = r.rkey13
  15372.     and     r.fkeyid = o2.id
  15373.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  15374.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15375.     and    o2.id = c2.id    
  15376.     and    c2.colid = r.fkey13
  15377.     union all
  15378.     select
  15379.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15380.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15381.         PK_TABLE_NAME         = o1.name,
  15382.         PK_COLUMN_NAME         = c1.name,
  15383.         PK_COLUMN_GUID        = convert(binary(16),null),
  15384.         PK_COLUMN_PROPID    = convert(int,null),
  15385.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15386.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  15387.         FK_TABLE_NAME         = o2.name,
  15388.         FK_COLUMN_NAME         = c2.name,
  15389.         FK_COLUMN_GUID        = convert(binary(16),null),
  15390.         FK_COLUMN_PROPID    = convert(int,null),
  15391.         ORDINAL            = convert(int,14),
  15392.         UPDATE_RULE        = 'NO ACTION',
  15393.         DELETE_RULE         = 'NO ACTION'        
  15394.     from
  15395.         sysobjects o1, sysobjects o2,
  15396.         syscolumns c1, syscolumns c2,
  15397.         sysreferences r
  15398.     where    
  15399.         o1.name = @pk_table_name
  15400.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15401.     and    o1.id = r.rkeyid
  15402.     and    r.keycnt >= 14
  15403.     and    o1.id = c1.id
  15404.     and    c1.colid = r.rkey14
  15405.     and     r.fkeyid = o2.id
  15406.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  15407.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15408.     and    o2.id = c2.id    
  15409.     and    c2.colid = r.fkey14
  15410.     union all
  15411.     select
  15412.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15413.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15414.         PK_TABLE_NAME         = o1.name,
  15415.         PK_COLUMN_NAME         = c1.name,
  15416.         PK_COLUMN_GUID        = convert(binary(16),null),
  15417.         PK_COLUMN_PROPID    = convert(int,null),
  15418.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15419.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  15420.         FK_TABLE_NAME         = o2.name,
  15421.         FK_COLUMN_NAME         = c2.name,
  15422.         FK_COLUMN_GUID        = convert(binary(16),null),
  15423.         FK_COLUMN_PROPID    = convert(int,null),
  15424.         ORDINAL            = convert(int,15),
  15425.         UPDATE_RULE        = 'NO ACTION',
  15426.         DELETE_RULE         = 'NO ACTION'        
  15427.     from
  15428.         sysobjects o1, sysobjects o2,
  15429.         syscolumns c1, syscolumns c2,
  15430.         sysreferences r
  15431.     where    
  15432.         o1.name = @pk_table_name
  15433.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15434.     and    o1.id = r.rkeyid
  15435.     and    r.keycnt >= 15
  15436.     and    o1.id = c1.id
  15437.     and    c1.colid = r.rkey15
  15438.     and     r.fkeyid = o2.id
  15439.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  15440.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15441.     and    o2.id = c2.id    
  15442.     and    c2.colid = r.fkey15
  15443.     union all
  15444.     select
  15445.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15446.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15447.         PK_TABLE_NAME         = o1.name,
  15448.         PK_COLUMN_NAME         = c1.name,
  15449.         PK_COLUMN_GUID        = convert(binary(16),null),
  15450.         PK_COLUMN_PROPID    = convert(int,null),
  15451.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15452.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  15453.         FK_TABLE_NAME         = o2.name,
  15454.         FK_COLUMN_NAME         = c2.name,
  15455.         FK_COLUMN_GUID        = convert(binary(16),null),
  15456.         FK_COLUMN_PROPID    = convert(int,null),
  15457.         ORDINAL            = convert(int,16),
  15458.         UPDATE_RULE        = 'NO ACTION',
  15459.         DELETE_RULE         = 'NO ACTION'        
  15460.     from
  15461.         sysobjects o1, sysobjects o2,
  15462.         syscolumns c1, syscolumns c2,
  15463.         sysreferences r
  15464.     where    
  15465.         o1.name = @pk_table_name
  15466.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15467.     and    o1.id = r.rkeyid
  15468.     and    r.keycnt >= 16
  15469.     and    o1.id = c1.id
  15470.     and    c1.colid = r.rkey16
  15471.     and     r.fkeyid = o2.id
  15472.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  15473.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15474.     and    o2.id = c2.id    
  15475.     and    c2.colid = r.fkey16
  15476.     END
  15477. ELSE IF @fk_table_name is not null
  15478.     BEGIN
  15479.     select
  15480.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15481.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15482.         PK_TABLE_NAME         = o1.name,
  15483.         PK_COLUMN_NAME         = c1.name,
  15484.         PK_COLUMN_GUID        = convert(binary(16),null),
  15485.         PK_COLUMN_PROPID    = convert(int,null),
  15486.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15487.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  15488.         FK_TABLE_NAME         = o2.name,
  15489.         FK_COLUMN_NAME         = c2.name,
  15490.         FK_COLUMN_GUID        = convert(binary(16),null),
  15491.         FK_COLUMN_PROPID    = convert(int,null),
  15492.         ORDINAL            = convert(int,1),
  15493.         UPDATE_RULE        = 'NO ACTION',
  15494.         DELETE_RULE         = 'NO ACTION'        
  15495.     from
  15496.         sysobjects o1, sysobjects o2,
  15497.         syscolumns c1, syscolumns c2,
  15498.         sysreferences r
  15499.     where    
  15500.         o2.name = @fk_table_name
  15501.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15502.     and    o2.id = r.fkeyid
  15503.     and    o2.id = c2.id
  15504.     and    c2.colid = r.fkey1
  15505.     and     r.rkeyid = o1.id
  15506.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  15507.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15508.     and    o1.id = c1.id    
  15509.     and    c1.colid = r.rkey1
  15510.     union    all
  15511.     select
  15512.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15513.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15514.         PK_TABLE_NAME         = o1.name,
  15515.         PK_COLUMN_NAME         = c1.name,
  15516.         PK_COLUMN_GUID        = convert(binary(16),null),
  15517.         PK_COLUMN_PROPID    = convert(int,null),
  15518.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15519.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  15520.         FK_TABLE_NAME         = o2.name,
  15521.         FK_COLUMN_NAME         = c2.name,
  15522.         FK_COLUMN_GUID        = convert(binary(16),null),
  15523.         FK_COLUMN_PROPID    = convert(int,null),
  15524.         ORDINAL            = convert(int,2),
  15525.         UPDATE_RULE        = 'NO ACTION',
  15526.         DELETE_RULE         = 'NO ACTION'        
  15527.     from
  15528.         sysobjects o1, sysobjects o2,
  15529.         syscolumns c1, syscolumns c2,
  15530.         sysreferences r
  15531.     where    
  15532.         o2.name = @fk_table_name
  15533.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15534.     and    o2.id = r.fkeyid
  15535.     and    r.keycnt >= 2
  15536.     and    o2.id = c2.id
  15537.     and    c2.colid = r.fkey2
  15538.     and     r.rkeyid = o1.id
  15539.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  15540.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15541.     and    o1.id = c1.id    
  15542.     and    c1.colid = r.rkey2
  15543.     union    all
  15544.     select
  15545.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15546.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15547.         PK_TABLE_NAME         = o1.name,
  15548.         PK_COLUMN_NAME         = c1.name,
  15549.         PK_COLUMN_GUID        = convert(binary(16),null),
  15550.         PK_COLUMN_PROPID    = convert(int,null),
  15551.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15552.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  15553.         FK_TABLE_NAME         = o2.name,
  15554.         FK_COLUMN_NAME         = c2.name,
  15555.         FK_COLUMN_GUID        = convert(binary(16),null),
  15556.         FK_COLUMN_PROPID    = convert(int,null),
  15557.         ORDINAL            = convert(int,3),
  15558.         UPDATE_RULE        = 'NO ACTION',
  15559.         DELETE_RULE         = 'NO ACTION'        
  15560.     from
  15561.         sysobjects o1, sysobjects o2,
  15562.         syscolumns c1, syscolumns c2,
  15563.         sysreferences r
  15564.     where    
  15565.         o2.name = @fk_table_name
  15566.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15567.     and    o2.id = r.fkeyid
  15568.     and    r.keycnt >= 3
  15569.     and    o2.id = c2.id
  15570.     and    c2.colid = r.fkey3
  15571.     and     r.rkeyid = o1.id
  15572.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  15573.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15574.     and    o1.id = c1.id    
  15575.     and    c1.colid = r.rkey3
  15576.     union    all
  15577.     select
  15578.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15579.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15580.         PK_TABLE_NAME         = o1.name,
  15581.         PK_COLUMN_NAME         = c1.name,
  15582.         PK_COLUMN_GUID        = convert(binary(16),null),
  15583.         PK_COLUMN_PROPID    = convert(int,null),
  15584.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15585.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  15586.         FK_TABLE_NAME         = o2.name,
  15587.         FK_COLUMN_NAME         = c2.name,
  15588.         FK_COLUMN_GUID        = convert(binary(16),null),
  15589.         FK_COLUMN_PROPID    = convert(int,null),
  15590.         ORDINAL            = convert(int,4),
  15591.         UPDATE_RULE        = 'NO ACTION',
  15592.         DELETE_RULE         = 'NO ACTION'        
  15593.     from
  15594.         sysobjects o1, sysobjects o2,
  15595.         syscolumns c1, syscolumns c2,
  15596.         sysreferences r
  15597.     where    
  15598.         o2.name = @fk_table_name
  15599.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15600.     and    o2.id = r.fkeyid
  15601.     and    r.keycnt >= 4
  15602.     and    o2.id = c2.id
  15603.     and    c2.colid = r.fkey4
  15604.     and     r.rkeyid = o1.id
  15605.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  15606.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15607.     and    o1.id = c1.id    
  15608.     and    c1.colid = r.rkey4
  15609.     union    all
  15610.     select
  15611.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15612.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15613.         PK_TABLE_NAME         = o1.name,
  15614.         PK_COLUMN_NAME         = c1.name,
  15615.         PK_COLUMN_GUID        = convert(binary(16),null),
  15616.         PK_COLUMN_PROPID    = convert(int,null),
  15617.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15618.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  15619.         FK_TABLE_NAME         = o2.name,
  15620.         FK_COLUMN_NAME         = c2.name,
  15621.         FK_COLUMN_GUID        = convert(binary(16),null),
  15622.         FK_COLUMN_PROPID    = convert(int,null),
  15623.         ORDINAL            = convert(int,5),
  15624.         UPDATE_RULE        = 'NO ACTION',
  15625.         DELETE_RULE         = 'NO ACTION'        
  15626.     from
  15627.         sysobjects o1, sysobjects o2,
  15628.         syscolumns c1, syscolumns c2,
  15629.         sysreferences r
  15630.     where    
  15631.         o2.name = @fk_table_name
  15632.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15633.     and    o2.id = r.fkeyid
  15634.     and    r.keycnt >= 5
  15635.     and    o2.id = c2.id
  15636.     and    c2.colid = r.fkey5
  15637.     and     r.rkeyid = o1.id
  15638.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  15639.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15640.     and    o1.id = c1.id    
  15641.     and    c1.colid = r.rkey5
  15642.     union    all
  15643.     select
  15644.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15645.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15646.         PK_TABLE_NAME         = o1.name,
  15647.         PK_COLUMN_NAME         = c1.name,
  15648.         PK_COLUMN_GUID        = convert(binary(16),null),
  15649.         PK_COLUMN_PROPID    = convert(int,null),
  15650.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15651.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  15652.         FK_TABLE_NAME         = o2.name,
  15653.         FK_COLUMN_NAME         = c2.name,
  15654.         FK_COLUMN_GUID        = convert(binary(16),null),
  15655.         FK_COLUMN_PROPID    = convert(int,null),
  15656.         ORDINAL            = convert(int,6),
  15657.         UPDATE_RULE        = 'NO ACTION',
  15658.         DELETE_RULE         = 'NO ACTION'        
  15659.     from
  15660.         sysobjects o1, sysobjects o2,
  15661.         syscolumns c1, syscolumns c2,
  15662.         sysreferences r
  15663.     where    
  15664.         o2.name = @fk_table_name
  15665.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15666.     and    o2.id = r.fkeyid
  15667.     and    r.keycnt >= 6
  15668.     and    o2.id = c2.id
  15669.     and    c2.colid = r.fkey6
  15670.     and     r.rkeyid = o1.id
  15671.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  15672.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15673.     and    o1.id = c1.id    
  15674.     and    c1.colid = r.rkey6
  15675.     union    all
  15676.     select
  15677.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15678.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15679.         PK_TABLE_NAME         = o1.name,
  15680.         PK_COLUMN_NAME         = c1.name,
  15681.         PK_COLUMN_GUID        = convert(binary(16),null),
  15682.         PK_COLUMN_PROPID    = convert(int,null),
  15683.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15684.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  15685.         FK_TABLE_NAME         = o2.name,
  15686.         FK_COLUMN_NAME         = c2.name,
  15687.         FK_COLUMN_GUID        = convert(binary(16),null),
  15688.         FK_COLUMN_PROPID    = convert(int,null),
  15689.         ORDINAL            = convert(int,7),
  15690.         UPDATE_RULE        = 'NO ACTION',
  15691.         DELETE_RULE         = 'NO ACTION'        
  15692.     from
  15693.         sysobjects o1, sysobjects o2,
  15694.         syscolumns c1, syscolumns c2,
  15695.         sysreferences r
  15696.     where    
  15697.         o2.name = @fk_table_name
  15698.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15699.     and    o2.id = r.fkeyid
  15700.     and    r.keycnt >= 7
  15701.     and    o2.id = c2.id
  15702.     and    c2.colid = r.fkey7
  15703.     and     r.rkeyid = o1.id
  15704.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  15705.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15706.     and    o1.id = c1.id    
  15707.     and    c1.colid = r.rkey7
  15708.     union    all
  15709.     select
  15710.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15711.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15712.         PK_TABLE_NAME         = o1.name,
  15713.         PK_COLUMN_NAME         = c1.name,
  15714.         PK_COLUMN_GUID        = convert(binary(16),null),
  15715.         PK_COLUMN_PROPID    = convert(int,null),
  15716.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15717.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  15718.         FK_TABLE_NAME         = o2.name,
  15719.         FK_COLUMN_NAME         = c2.name,
  15720.         FK_COLUMN_GUID        = convert(binary(16),null),
  15721.         FK_COLUMN_PROPID    = convert(int,null),
  15722.         ORDINAL            = convert(int,8),
  15723.         UPDATE_RULE        = 'NO ACTION',
  15724.         DELETE_RULE         = 'NO ACTION'        
  15725.     from
  15726.         sysobjects o1, sysobjects o2,
  15727.         syscolumns c1, syscolumns c2,
  15728.         sysreferences r
  15729.     where    
  15730.         o2.name = @fk_table_name
  15731.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15732.     and    o2.id = r.fkeyid
  15733.     and    r.keycnt >= 8
  15734.     and    o2.id = c2.id
  15735.     and    c2.colid = r.fkey8
  15736.     and     r.rkeyid = o1.id
  15737.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  15738.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15739.     and    o1.id = c1.id    
  15740.     and    c1.colid = r.rkey8
  15741.     union    all
  15742.     select
  15743.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15744.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15745.         PK_TABLE_NAME         = o1.name,
  15746.         PK_COLUMN_NAME         = c1.name,
  15747.         PK_COLUMN_GUID        = convert(binary(16),null),
  15748.         PK_COLUMN_PROPID    = convert(int,null),
  15749.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15750.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  15751.         FK_TABLE_NAME         = o2.name,
  15752.         FK_COLUMN_NAME         = c2.name,
  15753.         FK_COLUMN_GUID        = convert(binary(16),null),
  15754.         FK_COLUMN_PROPID    = convert(int,null),
  15755.         ORDINAL            = convert(int,9),
  15756.         UPDATE_RULE        = 'NO ACTION',
  15757.         DELETE_RULE         = 'NO ACTION'        
  15758.     from
  15759.         sysobjects o1, sysobjects o2,
  15760.         syscolumns c1, syscolumns c2,
  15761.         sysreferences r
  15762.     where    
  15763.         o2.name = @fk_table_name
  15764.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15765.     and    o2.id = r.fkeyid
  15766.     and    r.keycnt >= 9
  15767.     and    o2.id = c2.id
  15768.     and    c2.colid = r.fkey9
  15769.     and     r.rkeyid = o1.id
  15770.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  15771.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15772.     and    o1.id = c1.id    
  15773.     and    c1.colid = r.rkey9
  15774.     union    all
  15775.     select
  15776.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15777.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15778.         PK_TABLE_NAME         = o1.name,
  15779.         PK_COLUMN_NAME         = c1.name,
  15780.         PK_COLUMN_GUID        = convert(binary(16),null),
  15781.         PK_COLUMN_PROPID    = convert(int,null),
  15782.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15783.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  15784.         FK_TABLE_NAME         = o2.name,
  15785.         FK_COLUMN_NAME         = c2.name,
  15786.         FK_COLUMN_GUID        = convert(binary(16),null),
  15787.         FK_COLUMN_PROPID    = convert(int,null),
  15788.         ORDINAL            = convert(int,10),
  15789.         UPDATE_RULE        = 'NO ACTION',
  15790.         DELETE_RULE         = 'NO ACTION'        
  15791.     from
  15792.         sysobjects o1, sysobjects o2,
  15793.         syscolumns c1, syscolumns c2,
  15794.         sysreferences r
  15795.     where    
  15796.         o2.name = @fk_table_name
  15797.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15798.     and    o2.id = r.fkeyid
  15799.     and    r.keycnt >= 10
  15800.     and    o2.id = c2.id
  15801.     and    c2.colid = r.fkey10
  15802.     and     r.rkeyid = o1.id
  15803.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  15804.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15805.     and    o1.id = c1.id    
  15806.     and    c1.colid = r.rkey10
  15807.     union    all
  15808.     select
  15809.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15810.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15811.         PK_TABLE_NAME         = o1.name,
  15812.         PK_COLUMN_NAME         = c1.name,
  15813.         PK_COLUMN_GUID        = convert(binary(16),null),
  15814.         PK_COLUMN_PROPID    = convert(int,null),
  15815.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15816.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  15817.         FK_TABLE_NAME         = o2.name,
  15818.         FK_COLUMN_NAME         = c2.name,
  15819.         FK_COLUMN_GUID        = convert(binary(16),null),
  15820.         FK_COLUMN_PROPID    = convert(int,null),
  15821.         ORDINAL            = convert(int,11),
  15822.         UPDATE_RULE        = 'NO ACTION',
  15823.         DELETE_RULE         = 'NO ACTION'        
  15824.     from
  15825.         sysobjects o1, sysobjects o2,
  15826.         syscolumns c1, syscolumns c2,
  15827.         sysreferences r
  15828.     where    
  15829.         o2.name = @fk_table_name
  15830.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15831.     and    o2.id = r.fkeyid
  15832.     and    r.keycnt >= 11
  15833.     and    o2.id = c2.id
  15834.     and    c2.colid = r.fkey11
  15835.     and     r.rkeyid = o1.id
  15836.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  15837.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15838.     and    o1.id = c1.id    
  15839.     and    c1.colid = r.rkey11
  15840.     union    all
  15841.     select
  15842.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15843.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15844.         PK_TABLE_NAME         = o1.name,
  15845.         PK_COLUMN_NAME         = c1.name,
  15846.         PK_COLUMN_GUID        = convert(binary(16),null),
  15847.         PK_COLUMN_PROPID    = convert(int,null),
  15848.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15849.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  15850.         FK_TABLE_NAME         = o2.name,
  15851.         FK_COLUMN_NAME         = c2.name,
  15852.         FK_COLUMN_GUID        = convert(binary(16),null),
  15853.         FK_COLUMN_PROPID    = convert(int,null),
  15854.         ORDINAL            = convert(int,12),
  15855.         UPDATE_RULE        = 'NO ACTION',
  15856.         DELETE_RULE         = 'NO ACTION'        
  15857.     from
  15858.         sysobjects o1, sysobjects o2,
  15859.         syscolumns c1, syscolumns c2,
  15860.         sysreferences r
  15861.     where    
  15862.         o2.name = @fk_table_name
  15863.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15864.     and    o2.id = r.fkeyid
  15865.     and    r.keycnt >= 12
  15866.     and    o2.id = c2.id
  15867.     and    c2.colid = r.fkey12
  15868.     and     r.rkeyid = o1.id
  15869.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  15870.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15871.     and    o1.id = c1.id    
  15872.     and    c1.colid = r.rkey12
  15873.     union    all
  15874.     select
  15875.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15876.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15877.         PK_TABLE_NAME         = o1.name,
  15878.         PK_COLUMN_NAME         = c1.name,
  15879.         PK_COLUMN_GUID        = convert(binary(16),null),
  15880.         PK_COLUMN_PROPID    = convert(int,null),
  15881.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15882.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  15883.         FK_TABLE_NAME         = o2.name,
  15884.         FK_COLUMN_NAME         = c2.name,
  15885.         FK_COLUMN_GUID        = convert(binary(16),null),
  15886.         FK_COLUMN_PROPID    = convert(int,null),
  15887.         ORDINAL            = convert(int,13),
  15888.         UPDATE_RULE        = 'NO ACTION',
  15889.         DELETE_RULE         = 'NO ACTION'        
  15890.     from
  15891.         sysobjects o1, sysobjects o2,
  15892.         syscolumns c1, syscolumns c2,
  15893.         sysreferences r
  15894.     where    
  15895.         o2.name = @fk_table_name
  15896.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15897.     and    o2.id = r.fkeyid
  15898.     and    r.keycnt >= 13
  15899.     and    o2.id = c2.id
  15900.     and    c2.colid = r.fkey13
  15901.     and     r.rkeyid = o1.id
  15902.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  15903.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15904.     and    o1.id = c1.id    
  15905.     and    c1.colid = r.rkey13
  15906.     union    all
  15907.     select
  15908.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15909.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15910.         PK_TABLE_NAME         = o1.name,
  15911.         PK_COLUMN_NAME         = c1.name,
  15912.         PK_COLUMN_GUID        = convert(binary(16),null),
  15913.         PK_COLUMN_PROPID    = convert(int,null),
  15914.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15915.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  15916.         FK_TABLE_NAME         = o2.name,
  15917.         FK_COLUMN_NAME         = c2.name,
  15918.         FK_COLUMN_GUID        = convert(binary(16),null),
  15919.         FK_COLUMN_PROPID    = convert(int,null),
  15920.         ORDINAL            = convert(int,14),
  15921.         UPDATE_RULE        = 'NO ACTION',
  15922.         DELETE_RULE         = 'NO ACTION'        
  15923.     from
  15924.         sysobjects o1, sysobjects o2,
  15925.         syscolumns c1, syscolumns c2,
  15926.         sysreferences r
  15927.     where    
  15928.         o2.name = @fk_table_name
  15929.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15930.     and    o2.id = r.fkeyid
  15931.     and    r.keycnt >= 14
  15932.     and    o2.id = c2.id
  15933.     and    c2.colid = r.fkey14
  15934.     and     r.rkeyid = o1.id
  15935.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  15936.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15937.     and    o1.id = c1.id    
  15938.     and    c1.colid = r.rkey14
  15939.     union    all
  15940.     select
  15941.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15942.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15943.         PK_TABLE_NAME         = o1.name,
  15944.         PK_COLUMN_NAME         = c1.name,
  15945.         PK_COLUMN_GUID        = convert(binary(16),null),
  15946.         PK_COLUMN_PROPID    = convert(int,null),
  15947.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15948.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  15949.         FK_TABLE_NAME         = o2.name,
  15950.         FK_COLUMN_NAME         = c2.name,
  15951.         FK_COLUMN_GUID        = convert(binary(16),null),
  15952.         FK_COLUMN_PROPID    = convert(int,null),
  15953.         ORDINAL            = convert(int,15),
  15954.         UPDATE_RULE        = 'NO ACTION',
  15955.         DELETE_RULE         = 'NO ACTION'        
  15956.     from
  15957.         sysobjects o1, sysobjects o2,
  15958.         syscolumns c1, syscolumns c2,
  15959.         sysreferences r
  15960.     where    
  15961.         o2.name = @fk_table_name
  15962.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15963.     and    o2.id = r.fkeyid
  15964.     and    r.keycnt >= 15
  15965.     and    o2.id = c2.id
  15966.     and    c2.colid = r.fkey15
  15967.     and     r.rkeyid = o1.id
  15968.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  15969.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15970.     and    o1.id = c1.id    
  15971.     and    c1.colid = r.rkey15
  15972.     union    all
  15973.     select
  15974.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15975.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15976.         PK_TABLE_NAME         = o1.name,
  15977.         PK_COLUMN_NAME         = c1.name,
  15978.         PK_COLUMN_GUID        = convert(binary(16),null),
  15979.         PK_COLUMN_PROPID    = convert(int,null),
  15980.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15981.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  15982.         FK_TABLE_NAME         = o2.name,
  15983.         FK_COLUMN_NAME         = c2.name,
  15984.         FK_COLUMN_GUID        = convert(binary(16),null),
  15985.         FK_COLUMN_PROPID    = convert(int,null),
  15986.         ORDINAL            = convert(int,16),
  15987.         UPDATE_RULE        = 'NO ACTION',
  15988.         DELETE_RULE         = 'NO ACTION'        
  15989.     from
  15990.         sysobjects o1, sysobjects o2,
  15991.         syscolumns c1, syscolumns c2,
  15992.         sysreferences r
  15993.     where    
  15994.         o2.name = @fk_table_name
  15995.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15996.     and    o2.id = r.fkeyid
  15997.     and    r.keycnt >= 16
  15998.     and    o2.id = c2.id
  15999.     and    c2.colid = r.fkey16
  16000.     and     r.rkeyid = o1.id
  16001.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  16002.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16003.     and    o1.id = c1.id    
  16004.     and    c1.colid = r.rkey16
  16005.     END
  16006. ELSE
  16007.     BEGIN
  16008.     select
  16009.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16010.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16011.         PK_TABLE_NAME         = o1.name,
  16012.         PK_COLUMN_NAME         = c1.name,
  16013.         PK_COLUMN_GUID        = convert(binary(16),null),
  16014.         PK_COLUMN_PROPID    = convert(int,null),
  16015.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16016.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  16017.         FK_TABLE_NAME         = o2.name,
  16018.         FK_COLUMN_NAME         = c2.name,
  16019.         FK_COLUMN_GUID        = convert(binary(16),null),
  16020.         FK_COLUMN_PROPID    = convert(int,null),
  16021.         ORDINAL            = convert(int,1),
  16022.         UPDATE_RULE        = 'NO ACTION',
  16023.         DELETE_RULE         = 'NO ACTION'        
  16024.     from
  16025.         sysobjects o1, sysobjects o2,
  16026.         syscolumns c1, syscolumns c2,
  16027.         sysreferences r
  16028.     where    
  16029.         (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16030.     and    o1.id = r.rkeyid
  16031.     and    o1.id = c1.id
  16032.     and    c1.colid = r.rkey1
  16033.     and     r.fkeyid = o2.id
  16034.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16035.     and    o2.id = c2.id    
  16036.     and    c2.colid = r.fkey1
  16037.     union all
  16038.     select
  16039.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16040.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16041.         PK_TABLE_NAME         = o1.name,
  16042.         PK_COLUMN_NAME         = c1.name,
  16043.         PK_COLUMN_GUID        = convert(binary(16),null),
  16044.         PK_COLUMN_PROPID    = convert(int,null),
  16045.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16046.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  16047.         FK_TABLE_NAME         = o2.name,
  16048.         FK_COLUMN_NAME         = c2.name,
  16049.         FK_COLUMN_GUID        = convert(binary(16),null),
  16050.         FK_COLUMN_PROPID    = convert(int,null),
  16051.         ORDINAL            = convert(int,2),
  16052.         UPDATE_RULE        = 'NO ACTION',
  16053.         DELETE_RULE         = 'NO ACTION'        
  16054.     from
  16055.         sysobjects o1, sysobjects o2,
  16056.         syscolumns c1, syscolumns c2,
  16057.         sysreferences r
  16058.     where    
  16059.         (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16060.     and    o1.id = r.rkeyid
  16061.     and    r.keycnt >= 2
  16062.     and    o1.id = c1.id
  16063.     and    c1.colid = r.rkey2
  16064.     and     r.fkeyid = o2.id
  16065.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16066.     and    o2.id = c2.id    
  16067.     and    c2.colid = r.fkey2
  16068.     union all
  16069.     select
  16070.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16071.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16072.         PK_TABLE_NAME         = o1.name,
  16073.         PK_COLUMN_NAME         = c1.name,
  16074.         PK_COLUMN_GUID        = convert(binary(16),null),
  16075.         PK_COLUMN_PROPID    = convert(int,null),
  16076.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16077.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  16078.         FK_TABLE_NAME         = o2.name,
  16079.         FK_COLUMN_NAME         = c2.name,
  16080.         FK_COLUMN_GUID        = convert(binary(16),null),
  16081.         FK_COLUMN_PROPID    = convert(int,null),
  16082.         ORDINAL            = convert(int,3),
  16083.         UPDATE_RULE        = 'NO ACTION',
  16084.         DELETE_RULE         = 'NO ACTION'        
  16085.     from
  16086.         sysobjects o1, sysobjects o2,
  16087.         syscolumns c1, syscolumns c2,
  16088.         sysreferences r
  16089.     where    
  16090.         (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16091.     and    o1.id = r.rkeyid
  16092.     and    r.keycnt >= 3
  16093.     and    o1.id = c1.id
  16094.     and    c1.colid = r.rkey3
  16095.     and     r.fkeyid = o2.id
  16096.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16097.     and    o2.id = c2.id    
  16098.     and    c2.colid = r.fkey3
  16099.     union all
  16100.     select
  16101.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16102.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16103.         PK_TABLE_NAME         = o1.name,
  16104.         PK_COLUMN_NAME         = c1.name,
  16105.         PK_COLUMN_GUID        = convert(binary(16),null),
  16106.         PK_COLUMN_PROPID    = convert(int,null),
  16107.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16108.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  16109.         FK_TABLE_NAME         = o2.name,
  16110.         FK_COLUMN_NAME         = c2.name,
  16111.         FK_COLUMN_GUID        = convert(binary(16),null),
  16112.         FK_COLUMN_PROPID    = convert(int,null),
  16113.         ORDINAL            = convert(int,4),
  16114.         UPDATE_RULE        = 'NO ACTION',
  16115.         DELETE_RULE         = 'NO ACTION'        
  16116.     from
  16117.         sysobjects o1, sysobjects o2,
  16118.         syscolumns c1, syscolumns c2,
  16119.         sysreferences r
  16120.     where    
  16121.         (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16122.     and    o1.id = r.rkeyid
  16123.     and    r.keycnt >= 4
  16124.     and    o1.id = c1.id
  16125.     and    c1.colid = r.rkey4
  16126.     and     r.fkeyid = o2.id
  16127.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16128.     and    o2.id = c2.id    
  16129.     and    c2.colid = r.fkey4
  16130.     union all
  16131.     select
  16132.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16133.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16134.         PK_TABLE_NAME         = o1.name,
  16135.         PK_COLUMN_NAME         = c1.name,
  16136.         PK_COLUMN_GUID        = convert(binary(16),null),
  16137.         PK_COLUMN_PROPID    = convert(int,null),
  16138.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16139.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  16140.         FK_TABLE_NAME         = o2.name,
  16141.         FK_COLUMN_NAME         = c2.name,
  16142.         FK_COLUMN_GUID        = convert(binary(16),null),
  16143.         FK_COLUMN_PROPID    = convert(int,null),
  16144.         ORDINAL            = convert(int,5),
  16145.         UPDATE_RULE        = 'NO ACTION',
  16146.         DELETE_RULE         = 'NO ACTION'        
  16147.     from
  16148.         sysobjects o1, sysobjects o2,
  16149.         syscolumns c1, syscolumns c2,
  16150.         sysreferences r
  16151.     where    
  16152.         (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16153.     and    o1.id = r.rkeyid
  16154.     and    r.keycnt >= 5
  16155.     and    o1.id = c1.id
  16156.     and    c1.colid = r.rkey5
  16157.     and     r.fkeyid = o2.id
  16158.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16159.     and    o2.id = c2.id    
  16160.     and    c2.colid = r.fkey5
  16161.     union all
  16162.     select
  16163.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16164.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16165.         PK_TABLE_NAME         = o1.name,
  16166.         PK_COLUMN_NAME         = c1.name,
  16167.         PK_COLUMN_GUID        = convert(binary(16),null),
  16168.         PK_COLUMN_PROPID    = convert(int,null),
  16169.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16170.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  16171.         FK_TABLE_NAME         = o2.name,
  16172.         FK_COLUMN_NAME         = c2.name,
  16173.         FK_COLUMN_GUID        = convert(binary(16),null),
  16174.         FK_COLUMN_PROPID    = convert(int,null),
  16175.         ORDINAL            = convert(int,6),
  16176.         UPDATE_RULE        = 'NO ACTION',
  16177.         DELETE_RULE         = 'NO ACTION'        
  16178.     from
  16179.         sysobjects o1, sysobjects o2,
  16180.         syscolumns c1, syscolumns c2,
  16181.         sysreferences r
  16182.     where    
  16183.         (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16184.     and    o1.id = r.rkeyid
  16185.     and    r.keycnt >= 6
  16186.     and    o1.id = c1.id
  16187.     and    c1.colid = r.rkey6
  16188.     and     r.fkeyid = o2.id
  16189.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16190.     and    o2.id = c2.id    
  16191.     and    c2.colid = r.fkey6
  16192.     union all
  16193.     select
  16194.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16195.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16196.         PK_TABLE_NAME         = o1.name,
  16197.         PK_COLUMN_NAME         = c1.name,
  16198.         PK_COLUMN_GUID        = convert(binary(16),null),
  16199.         PK_COLUMN_PROPID    = convert(int,null),
  16200.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16201.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  16202.         FK_TABLE_NAME         = o2.name,
  16203.         FK_COLUMN_NAME         = c2.name,
  16204.         FK_COLUMN_GUID        = convert(binary(16),null),
  16205.         FK_COLUMN_PROPID    = convert(int,null),
  16206.         ORDINAL            = convert(int,7),
  16207.         UPDATE_RULE        = 'NO ACTION',
  16208.         DELETE_RULE         = 'NO ACTION'        
  16209.     from
  16210.         sysobjects o1, sysobjects o2,
  16211.         syscolumns c1, syscolumns c2,
  16212.         sysreferences r
  16213.     where    
  16214.         (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16215.     and    o1.id = r.rkeyid
  16216.     and    r.keycnt >= 7
  16217.     and    o1.id = c1.id
  16218.     and    c1.colid = r.rkey7
  16219.     and     r.fkeyid = o2.id
  16220.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16221.     and    o2.id = c2.id    
  16222.     and    c2.colid = r.fkey7
  16223.     union all
  16224.     select
  16225.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16226.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16227.         PK_TABLE_NAME         = o1.name,
  16228.         PK_COLUMN_NAME         = c1.name,
  16229.         PK_COLUMN_GUID        = convert(binary(16),null),
  16230.         PK_COLUMN_PROPID    = convert(int,null),
  16231.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16232.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  16233.         FK_TABLE_NAME         = o2.name,
  16234.         FK_COLUMN_NAME         = c2.name,
  16235.         FK_COLUMN_GUID        = convert(binary(16),null),
  16236.         FK_COLUMN_PROPID    = convert(int,null),
  16237.         ORDINAL            = convert(int,8),
  16238.         UPDATE_RULE        = 'NO ACTION',
  16239.         DELETE_RULE         = 'NO ACTION'        
  16240.     from
  16241.         sysobjects o1, sysobjects o2,
  16242.         syscolumns c1, syscolumns c2,
  16243.         sysreferences r
  16244.     where    
  16245.         (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16246.     and    o1.id = r.rkeyid
  16247.     and    r.keycnt >= 8
  16248.     and    o1.id = c1.id
  16249.     and    c1.colid = r.rkey8
  16250.     and     r.fkeyid = o2.id
  16251.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16252.     and    o2.id = c2.id    
  16253.     and    c2.colid = r.fkey8
  16254.     union all
  16255.     select
  16256.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16257.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16258.         PK_TABLE_NAME         = o1.name,
  16259.         PK_COLUMN_NAME         = c1.name,
  16260.         PK_COLUMN_GUID        = convert(binary(16),null),
  16261.         PK_COLUMN_PROPID    = convert(int,null),
  16262.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16263.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  16264.         FK_TABLE_NAME         = o2.name,
  16265.         FK_COLUMN_NAME         = c2.name,
  16266.         FK_COLUMN_GUID        = convert(binary(16),null),
  16267.         FK_COLUMN_PROPID    = convert(int,null),
  16268.         ORDINAL            = convert(int,9),
  16269.         UPDATE_RULE        = 'NO ACTION',
  16270.         DELETE_RULE         = 'NO ACTION'        
  16271.     from
  16272.         sysobjects o1, sysobjects o2,
  16273.         syscolumns c1, syscolumns c2,
  16274.         sysreferences r
  16275.     where    
  16276.         (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16277.     and    o1.id = r.rkeyid
  16278.     and    r.keycnt >= 9
  16279.     and    o1.id = c1.id
  16280.     and    c1.colid = r.rkey9
  16281.     and     r.fkeyid = o2.id
  16282.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16283.     and    o2.id = c2.id    
  16284.     and    c2.colid = r.fkey9
  16285.     union all
  16286.     select
  16287.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16288.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16289.         PK_TABLE_NAME         = o1.name,
  16290.         PK_COLUMN_NAME         = c1.name,
  16291.         PK_COLUMN_GUID        = convert(binary(16),null),
  16292.         PK_COLUMN_PROPID    = convert(int,null),
  16293.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16294.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  16295.         FK_TABLE_NAME         = o2.name,
  16296.         FK_COLUMN_NAME         = c2.name,
  16297.         FK_COLUMN_GUID        = convert(binary(16),null),
  16298.         FK_COLUMN_PROPID    = convert(int,null),
  16299.         ORDINAL            = convert(int,10),
  16300.         UPDATE_RULE        = 'NO ACTION',
  16301.         DELETE_RULE         = 'NO ACTION'        
  16302.     from
  16303.         sysobjects o1, sysobjects o2,
  16304.         syscolumns c1, syscolumns c2,
  16305.         sysreferences r
  16306.     where    
  16307.         (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16308.     and    o1.id = r.rkeyid
  16309.     and    r.keycnt >= 10
  16310.     and    o1.id = c1.id
  16311.     and    c1.colid = r.rkey10
  16312.     and     r.fkeyid = o2.id
  16313.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16314.     and    o2.id = c2.id    
  16315.     and    c2.colid = r.fkey10
  16316.     union all
  16317.     select
  16318.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16319.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16320.         PK_TABLE_NAME         = o1.name,
  16321.         PK_COLUMN_NAME         = c1.name,
  16322.         PK_COLUMN_GUID        = convert(binary(16),null),
  16323.         PK_COLUMN_PROPID    = convert(int,null),
  16324.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16325.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  16326.         FK_TABLE_NAME         = o2.name,
  16327.         FK_COLUMN_NAME         = c2.name,
  16328.         FK_COLUMN_GUID        = convert(binary(16),null),
  16329.         FK_COLUMN_PROPID    = convert(int,null),
  16330.         ORDINAL            = convert(int,11),
  16331.         UPDATE_RULE        = 'NO ACTION',
  16332.         DELETE_RULE         = 'NO ACTION'        
  16333.     from
  16334.         sysobjects o1, sysobjects o2,
  16335.         syscolumns c1, syscolumns c2,
  16336.         sysreferences r
  16337.     where    
  16338.         (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16339.     and    o1.id = r.rkeyid
  16340.     and    r.keycnt >= 11
  16341.     and    o1.id = c1.id
  16342.     and    c1.colid = r.rkey11
  16343.     and     r.fkeyid = o2.id
  16344.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16345.     and    o2.id = c2.id    
  16346.     and    c2.colid = r.fkey11
  16347.     union all
  16348.     select
  16349.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16350.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16351.         PK_TABLE_NAME         = o1.name,
  16352.         PK_COLUMN_NAME         = c1.name,
  16353.         PK_COLUMN_GUID        = convert(binary(16),null),
  16354.         PK_COLUMN_PROPID    = convert(int,null),
  16355.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16356.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  16357.         FK_TABLE_NAME         = o2.name,
  16358.         FK_COLUMN_NAME         = c2.name,
  16359.         FK_COLUMN_GUID        = convert(binary(16),null),
  16360.         FK_COLUMN_PROPID    = convert(int,null),
  16361.         ORDINAL            = convert(int,12),
  16362.         UPDATE_RULE        = 'NO ACTION',
  16363.         DELETE_RULE         = 'NO ACTION'        
  16364.     from
  16365.         sysobjects o1, sysobjects o2,
  16366.         syscolumns c1, syscolumns c2,
  16367.         sysreferences r
  16368.     where    
  16369.         (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16370.     and    o1.id = r.rkeyid
  16371.     and    r.keycnt >= 12
  16372.     and    o1.id = c1.id
  16373.     and    c1.colid = r.rkey12
  16374.     and     r.fkeyid = o2.id
  16375.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16376.     and    o2.id = c2.id    
  16377.     and    c2.colid = r.fkey12
  16378.     union all
  16379.     select
  16380.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16381.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16382.         PK_TABLE_NAME         = o1.name,
  16383.         PK_COLUMN_NAME         = c1.name,
  16384.         PK_COLUMN_GUID        = convert(binary(16),null),
  16385.         PK_COLUMN_PROPID    = convert(int,null),
  16386.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16387.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  16388.         FK_TABLE_NAME         = o2.name,
  16389.         FK_COLUMN_NAME         = c2.name,
  16390.         FK_COLUMN_GUID        = convert(binary(16),null),
  16391.         FK_COLUMN_PROPID    = convert(int,null),
  16392.         ORDINAL            = convert(int,13),
  16393.         UPDATE_RULE        = 'NO ACTION',
  16394.         DELETE_RULE         = 'NO ACTION'        
  16395.     from
  16396.         sysobjects o1, sysobjects o2,
  16397.         syscolumns c1, syscolumns c2,
  16398.         sysreferences r
  16399.     where    
  16400.         (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16401.     and    o1.id = r.rkeyid
  16402.     and    r.keycnt >= 13
  16403.     and    o1.id = c1.id
  16404.     and    c1.colid = r.rkey13
  16405.     and     r.fkeyid = o2.id
  16406.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16407.     and    o2.id = c2.id    
  16408.     and    c2.colid = r.fkey13
  16409.     union all
  16410.     select
  16411.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16412.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16413.         PK_TABLE_NAME         = o1.name,
  16414.         PK_COLUMN_NAME         = c1.name,
  16415.         PK_COLUMN_GUID        = convert(binary(16),null),
  16416.         PK_COLUMN_PROPID    = convert(int,null),
  16417.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16418.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  16419.         FK_TABLE_NAME         = o2.name,
  16420.         FK_COLUMN_NAME         = c2.name,
  16421.         FK_COLUMN_GUID        = convert(binary(16),null),
  16422.         FK_COLUMN_PROPID    = convert(int,null),
  16423.         ORDINAL            = convert(int,14),
  16424.         UPDATE_RULE        = 'NO ACTION',
  16425.         DELETE_RULE         = 'NO ACTION'        
  16426.     from
  16427.         sysobjects o1, sysobjects o2,
  16428.         syscolumns c1, syscolumns c2,
  16429.         sysreferences r
  16430.     where    
  16431.         (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16432.     and    o1.id = r.rkeyid
  16433.     and    r.keycnt >= 14
  16434.     and    o1.id = c1.id
  16435.     and    c1.colid = r.rkey14
  16436.     and     r.fkeyid = o2.id
  16437.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16438.     and    o2.id = c2.id    
  16439.     and    c2.colid = r.fkey14
  16440.     union all
  16441.     select
  16442.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16443.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16444.         PK_TABLE_NAME         = o1.name,
  16445.         PK_COLUMN_NAME         = c1.name,
  16446.         PK_COLUMN_GUID        = convert(binary(16),null),
  16447.         PK_COLUMN_PROPID    = convert(int,null),
  16448.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16449.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  16450.         FK_TABLE_NAME         = o2.name,
  16451.         FK_COLUMN_NAME         = c2.name,
  16452.         FK_COLUMN_GUID        = convert(binary(16),null),
  16453.         FK_COLUMN_PROPID    = convert(int,null),
  16454.         ORDINAL            = convert(int,15),
  16455.         UPDATE_RULE        = 'NO ACTION',
  16456.         DELETE_RULE         = 'NO ACTION'        
  16457.     from
  16458.         sysobjects o1, sysobjects o2,
  16459.         syscolumns c1, syscolumns c2,
  16460.         sysreferences r
  16461.     where    
  16462.         (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16463.     and    o1.id = r.rkeyid
  16464.     and    r.keycnt >= 15
  16465.     and    o1.id = c1.id
  16466.     and    c1.colid = r.rkey15
  16467.     and     r.fkeyid = o2.id
  16468.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16469.     and    o2.id = c2.id    
  16470.     and    c2.colid = r.fkey15
  16471.     union all
  16472.     select
  16473.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16474.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16475.         PK_TABLE_NAME         = o1.name,
  16476.         PK_COLUMN_NAME         = c1.name,
  16477.         PK_COLUMN_GUID        = convert(binary(16),null),
  16478.         PK_COLUMN_PROPID    = convert(int,null),
  16479.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16480.         FK_TABLE_SCHEMA         = user_name(o2.uid),
  16481.         FK_TABLE_NAME         = o2.name,
  16482.         FK_COLUMN_NAME         = c2.name,
  16483.         FK_COLUMN_GUID        = convert(binary(16),null),
  16484.         FK_COLUMN_PROPID    = convert(int,null),
  16485.         ORDINAL            = convert(int,16),
  16486.         UPDATE_RULE        = 'NO ACTION',
  16487.         DELETE_RULE         = 'NO ACTION'        
  16488.     from
  16489.         sysobjects o1, sysobjects o2,
  16490.         syscolumns c1, syscolumns c2,
  16491.         sysreferences r
  16492.     where    
  16493.         (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16494.     and    o1.id = r.rkeyid
  16495.     and    o1.id = c1.id
  16496.     and    r.keycnt >= 16
  16497.     and    c1.colid = r.rkey16
  16498.     and     r.fkeyid = o2.id
  16499.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16500.     and    o2.id = c2.id    
  16501.     and    c2.colid = r.fkey16
  16502.     END
  16503. go
  16504.  
  16505.  
  16506. create procedure sp_foreign_keys_rowset;5
  16507.     (
  16508.        @handle        int output,
  16509.        @scrollopt    int output,
  16510.     @ccopt        int output,
  16511.     @rows        int output,
  16512.     @cmd        varchar(255)
  16513.     )
  16514. as
  16515.     declare @ret int
  16516.     declare @database_name varchar(30)
  16517.  
  16518.     create table #spfkeysrowset
  16519.         (
  16520.         PK_TABLE_CATALOG    varchar(30) not null,
  16521.         PK_TABLE_SCHEMA        varchar(30) not null,
  16522.         PK_TABLE_NAME         varchar(30) not null,
  16523.         PK_COLUMN_NAME         varchar(30) not null,
  16524.         PK_COLUMN_GUID        binary(16) null,
  16525.         PK_COLUMN_PROPID    int null,
  16526.         FK_TABLE_CATALOG    varchar(30) not null,
  16527.         FK_TABLE_SCHEMA         varchar(30) not null,
  16528.         FK_TABLE_NAME         varchar(30) not null,
  16529.         FK_COLUMN_NAME         varchar(30) not null,
  16530.         FK_COLUMN_GUID        binary(16) null,
  16531.         FK_COLUMN_PROPID    int null,
  16532.         ORDINAL            int not null,
  16533.         UPDATE_RULE        varchar(30) not null,
  16534.         DELETE_RULE         varchar(30) not null        
  16535.         )
  16536.  
  16537.     declare sysdatabases_cursor insensitive cursor for 
  16538.         select name from master.dbo.sysdatabases
  16539.   
  16540.     set nocount on
  16541.     open sysdatabases_cursor
  16542.       fetch next from sysdatabases_cursor into @database_name 
  16543.     while @@fetch_status = 0
  16544.         begin
  16545.         exec('use '+@database_name+' insert into #spfkeysrowset '+@cmd)
  16546.         fetch next from sysdatabases_cursor into @database_name 
  16547.         end
  16548.       close sysdatabases_cursor
  16549.     deallocate sysdatabases_cursor
  16550.  
  16551.     if @scrollopt is null
  16552.         begin
  16553.         select * from #spfkeysrowset order by 7,8,9,1,2,3,13
  16554.         end
  16555.     else
  16556.         begin
  16557.         exec @ret = sp_cursoropen @handle output,
  16558.             'select * from #spfkeysrowset order by 7,8,9,1,2,3,13',
  16559.                 @scrollopt output, @ccopt output, @rows output
  16560.         end
  16561.     drop table #spfkeysrowset
  16562.     return isnull(@ret,0)
  16563. go
  16564.  
  16565.  
  16566. grant execute on sp_foreign_keys_rowset to public
  16567. go
  16568.  
  16569. dump tran master with no_log
  16570. go
  16571.  
  16572. print 'creating sp_indexes_rowset'
  16573. go
  16574.  
  16575. /*    6.0 and 6.5 version */
  16576. create procedure sp_indexes_rowset
  16577.     (
  16578.     @table_name    varchar(255), 
  16579.     @index_name    varchar(255) = null,
  16580.     @table_schema    varchar(255) = null     
  16581.     )
  16582. as
  16583.     select    TABLE_CATALOG        = db_name(),
  16584.         TABLE_SCHEMA        = user_name(o.uid),
  16585.         TABLE_NAME        = o.name,
  16586.         INDEX_CATALOG        = db_name(),        
  16587.         INDEX_SCHEMA        = user_name(o.uid),
  16588.         INDEX_NAME        = x.name,
  16589.         PRIMARY_KEY        = convert(bit,(x.status & 0x800)/0x800),
  16590.         "UNIQUE"        = convert(bit,(x.status & 2)/2),
  16591.         "CLUSTERED"        = convert(bit,(x.status & 16)/16),
  16592.         "TYPE"            = convert(smallint, 1 /*DBPROPVAL_IT_BTREE*/),
  16593.         FILL_FACTOR        = convert(int, x.OrigFillFactor),
  16594.         INITIAL_SIZE        = convert(int,null),
  16595.         NULLS            = convert(int,null),
  16596.         SORT_BOOKMARKS        = convert(bit,0),
  16597.         AUTO_UPDATE        = convert(bit,1),
  16598.         NULL_COLLATION        = convert(int,4 /*DBPROPVAL_NC_LOW*/),
  16599.         ORDINAL_POSITION    = convert(int,c.colid),
  16600.         COLUMN_NAME        = index_col(user_name(o.uid)+'.'+o.name, x.indid, c.colid),
  16601.         COLUMN_GUID        = convert(binary(16),null),
  16602.         COLUMN_PROPID        = convert(int,null),
  16603.         COLLATION        = convert(smallint, 1 /*DB_COLLATION_ASC*/),
  16604.         CARDINALITY        = case when (x.status & 2) = 2 then x.rows else null end,
  16605.         PAGES            = convert(int, x.dpages),
  16606.         FILTER_CONDITION    = convert(varchar(1),null) 
  16607.         
  16608.     from    sysobjects o, sysindexes x, syscolumns c, sysusers u
  16609.     where    o.type in ('U')
  16610.     and     o.name = @table_name
  16611.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  16612.     and    (@index_name is null or @index_name = x.name)
  16613.     and     x.id = o.id
  16614.     and     x.id = c.id
  16615.     and      c.colid < x.keycnt+(x.status&16)/16
  16616.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  16617.     and     (
  16618.         suser_id() = 1     /* User is the System Administrator */
  16619.         or o.uid = user_id()     /* User created the object */
  16620.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16621.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16622.             from sysprotects p
  16623.             where p.id = o.id
  16624.             /* get rows for public,current user,user's group */
  16625.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16626.             /* check for SELECT,EXECUTE privilege */
  16627.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16628.             ) = 1    /* final magic...compare Grants      */
  16629.            )
  16630.     order by 8 desc, 4, 5, 6, 16
  16631. go
  16632. create procedure sp_indexes_rowset;2
  16633.     (
  16634.     @index_name    varchar(255) = null,
  16635.     @table_schema    varchar(255) = null     
  16636.     )
  16637. as
  16638.     select    TABLE_CATALOG        = db_name(),
  16639.         TABLE_SCHEMA        = user_name(o.uid),
  16640.         TABLE_NAME        = o.name,
  16641.         INDEX_CATALOG        = db_name(),        
  16642.         INDEX_SCHEMA        = user_name(o.uid),
  16643.         INDEX_NAME        = x.name,
  16644.         PRIMARY_KEY        = convert(bit,(x.status & 0x800)/0x800),
  16645.         "UNIQUE"        = convert(bit,(x.status & 2)/2),
  16646.         "CLUSTERED"        = convert(bit,(x.status & 16)/16),
  16647.         "TYPE"            = convert(smallint, 1 /*DBPROPVAL_IT_BTREE*/),
  16648.         FILL_FACTOR        = convert(int, x.OrigFillFactor),
  16649.         INITIAL_SIZE        = convert(int,null),
  16650.         NULLS            = convert(int,null),
  16651.         SORT_BOOKMARKS        = convert(bit,0),
  16652.         AUTO_UPDATE        = convert(bit,1),
  16653.         NULL_COLLATION        = convert(int,4 /*DBPROPVAL_NC_LOW*/),
  16654.         ORDINAL_POSITION    = convert(int,c.colid),
  16655.         COLUMN_NAME        = index_col(user_name(o.uid)+'.'+o.name, x.indid, c.colid),
  16656.         COLUMN_GUID        = convert(binary(16),null),
  16657.         COLUMN_PROPID        = convert(int,null),
  16658.         COLLATION        = convert(smallint, 1 /*DB_COLLATION_ASC*/),
  16659.         CARDINALITY        = case when (x.status & 2) = 2 then x.rows else null end,
  16660.         PAGES            = convert(int, x.dpages),
  16661.         FILTER_CONDITION    = convert(varchar(1),null) 
  16662.         
  16663.     from    sysobjects o, sysindexes x, syscolumns c, sysusers u
  16664.     where    o.type in ('U')
  16665.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  16666.     and    (@index_name is null or @index_name = x.name)
  16667.     and     x.id = o.id
  16668.     and     x.id = c.id
  16669.     and      c.colid < x.keycnt+(x.status&16)/16
  16670.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  16671.     and     (
  16672.         suser_id() = 1     /* User is the System Administrator */
  16673.         or o.uid = user_id()     /* User created the object */
  16674.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16675.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16676.             from sysprotects p
  16677.             where p.id = o.id
  16678.             /* get rows for public,current user,user's group */
  16679.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16680.             /* check for SELECT,EXECUTE privilege */
  16681.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16682.             ) = 1    /* final magic...compare Grants      */
  16683.            )
  16684.     order by 8 desc, 4, 5, 6, 16
  16685. go
  16686. create procedure sp_indexes_rowset;3
  16687.     (
  16688.     @table_name    varchar(255) = null, 
  16689.     @index_name    varchar(255) = null,
  16690.     @table_schema    varchar(255) = null     
  16691.     )
  16692. as
  16693. IF @table_name is not null
  16694.     BEGIN
  16695.     select    TABLE_CATALOG        = db_name(),
  16696.         TABLE_SCHEMA        = user_name(o.uid),
  16697.         TABLE_NAME        = o.name,
  16698.         INDEX_CATALOG        = db_name(),        
  16699.         INDEX_SCHEMA        = user_name(o.uid),
  16700.         INDEX_NAME        = x.name,
  16701.         PRIMARY_KEY        = convert(bit,(x.status & 0x800)/0x800),
  16702.         "UNIQUE"        = convert(bit,(x.status & 2)/2),
  16703.         "CLUSTERED"        = convert(bit,(x.status & 16)/16),
  16704.         "TYPE"            = convert(smallint, 1 /*DBPROPVAL_IT_BTREE*/),
  16705.         FILL_FACTOR        = convert(int, x.OrigFillFactor),
  16706.         INITIAL_SIZE        = convert(int,null),
  16707.         NULLS            = convert(int,null),
  16708.         SORT_BOOKMARKS        = convert(bit,0),
  16709.         AUTO_UPDATE        = convert(bit,1),
  16710.         NULL_COLLATION        = convert(int,4 /*DBPROPVAL_NC_LOW*/),
  16711.         ORDINAL_POSITION    = convert(int,c.colid),
  16712.         COLUMN_NAME        = index_col(user_name(o.uid)+'.'+o.name, x.indid, c.colid),
  16713.         COLUMN_GUID        = convert(binary(16),null),
  16714.         COLUMN_PROPID        = convert(int,null),
  16715.         COLLATION        = convert(smallint, 1 /*DB_COLLATION_ASC*/),
  16716.         CARDINALITY        = case when (x.status & 2) = 2 then x.rows else null end,
  16717.         PAGES            = convert(int, x.dpages),
  16718.         FILTER_CONDITION    = convert(varchar(1),null) 
  16719.         
  16720.     from    sysobjects o, sysindexes x, syscolumns c, sysusers u
  16721.     where    o.type in ('U')
  16722.     and     o.name = @table_name
  16723.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  16724.     and    (@index_name is null or @index_name = x.name)
  16725.     and     x.id = o.id
  16726.     and     x.id = c.id
  16727.     and      c.colid < x.keycnt+(x.status&16)/16
  16728.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  16729.     and     (
  16730.         suser_id() = 1     /* User is the System Administrator */
  16731.         or o.uid = user_id()     /* User created the object */
  16732.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16733.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16734.             from sysprotects p
  16735.             where p.id = o.id
  16736.             /* get rows for public,current user,user's group */
  16737.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16738.             /* check for SELECT,EXECUTE privilege */
  16739.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16740.             ) = 1    /* final magic...compare Grants      */
  16741.            )
  16742.     END
  16743. ELSE
  16744.     BEGIN
  16745.     select    TABLE_CATALOG        = db_name(),
  16746.         TABLE_SCHEMA        = user_name(o.uid),
  16747.         TABLE_NAME        = o.name,
  16748.         INDEX_CATALOG        = db_name(),        
  16749.         INDEX_SCHEMA        = user_name(o.uid),
  16750.         INDEX_NAME        = x.name,
  16751.         PRIMARY_KEY        = convert(bit,(x.status & 0x800)/0x800),
  16752.         "UNIQUE"        = convert(bit,(x.status & 2)/2),
  16753.         "CLUSTERED"        = convert(bit,(x.status & 16)/16),
  16754.         "TYPE"            = convert(smallint, 1 /*DBPROPVAL_IT_BTREE*/),
  16755.         FILL_FACTOR        = convert(int, x.OrigFillFactor),
  16756.         INITIAL_SIZE        = convert(int,null),
  16757.         NULLS            = convert(int,null),
  16758.         SORT_BOOKMARKS        = convert(bit,0),
  16759.         AUTO_UPDATE        = convert(bit,1),
  16760.         NULL_COLLATION        = convert(int,4 /*DBPROPVAL_NC_LOW*/),
  16761.         ORDINAL_POSITION    = convert(int,c.colid),
  16762.         COLUMN_NAME        = index_col(user_name(o.uid)+'.'+o.name, x.indid, c.colid),
  16763.         COLUMN_GUID        = convert(binary(16),null),
  16764.         COLUMN_PROPID        = convert(int,null),
  16765.         COLLATION        = convert(smallint, 1 /*DB_COLLATION_ASC*/),
  16766.         CARDINALITY        = case when (x.status & 2) = 2 then x.rows else null end,
  16767.         PAGES            = convert(int, x.dpages),
  16768.         FILTER_CONDITION    = convert(varchar(1),null) 
  16769.         
  16770.     from    sysobjects o, sysindexes x, syscolumns c, sysusers u
  16771.     where    o.type in ('U')
  16772.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  16773.     and    (@index_name is null or @index_name = x.name)
  16774.     and     x.id = o.id
  16775.     and     x.id = c.id
  16776.     and      c.colid < x.keycnt+(x.status&16)/16
  16777.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  16778.     and     (
  16779.         suser_id() = 1     /* User is the System Administrator */
  16780.         or o.uid = user_id()     /* User created the object */
  16781.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16782.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16783.             from sysprotects p
  16784.             where p.id = o.id
  16785.             /* get rows for public,current user,user's group */
  16786.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16787.             /* check for SELECT,EXECUTE privilege */
  16788.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16789.             ) = 1    /* final magic...compare Grants      */
  16790.            )
  16791.     END
  16792. go
  16793.  
  16794.  
  16795. if (charindex('7.00', @@version) > 0)
  16796.     drop procedure sp_indexes_rowset
  16797. else
  16798. begin
  16799.     print ''
  16800.     print ''
  16801.     print 'Warning:'
  16802.     print 'you are installing the stored procedures '
  16803.     print 'on a pre 7.0 SQL Server.'
  16804.     print 'Ignore the following errors.'
  16805. end
  16806. go
  16807.  
  16808. /*     7.0 version */
  16809. create procedure sp_indexes_rowset
  16810.     (
  16811.     @table_name    varchar(255), 
  16812.     @index_name    varchar(255) = null,
  16813.     @table_schema    varchar(255) = null     
  16814.     )
  16815. as
  16816.     select    TABLE_CATALOG        = db_name(),
  16817.         TABLE_SCHEMA        = user_name(o.uid),
  16818.         TABLE_NAME        = o.name,
  16819.         INDEX_CATALOG        = db_name(),        
  16820.         INDEX_SCHEMA        = user_name(o.uid),
  16821.         INDEX_NAME        = x.name,
  16822.         PRIMARY_KEY        = convert(bit,(x.status & 0x800)/0x800),
  16823.         "UNIQUE"        = convert(bit,(x.status & 2)/2),
  16824.         "CLUSTERED"        = convert(bit,(x.status & 16)/16),
  16825.         "TYPE"            = convert(smallint, 1 /*DBPROPVAL_IT_BTREE*/),
  16826.         FILL_FACTOR        = convert(int, x.OrigFillFactor),
  16827.         INITIAL_SIZE        = convert(int,null),
  16828.         NULLS            = convert(int,null),
  16829.         SORT_BOOKMARKS        = convert(bit,0),
  16830.         AUTO_UPDATE        = convert(bit,1),
  16831.         NULL_COLLATION        = convert(int,4 /*DBPROPVAL_NC_LOW*/),
  16832.         ORDINAL_POSITION    = convert(int,c.colid),
  16833.         COLUMN_NAME        = index_col(user_name(o.uid)+'.'+o.name, x.indid, c.colid),
  16834.         COLUMN_GUID        = convert(binary(16),null),
  16835.         COLUMN_PROPID        = convert(int,null),
  16836.         COLLATION        = convert(smallint, 1 /*DB_COLLATION_ASC*/),
  16837.         CARDINALITY        = case when (x.status & 2) = 2 then x.rows else null end,
  16838.         PAGES            = convert(int, x.dpages),
  16839.         FILTER_CONDITION    = convert(varchar(1),null) 
  16840.         
  16841.     from    sysobjects o, sysindexes x, syscolumns c
  16842.     where    o.type in ('U')
  16843.     and     o.name = @table_name
  16844.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  16845.     and    (@index_name is null or @index_name = x.name)
  16846.     and     x.id = o.id
  16847.     and     x.id = c.id
  16848.     and      c.colid < x.keycnt+(x.status&16)/16
  16849.     and     permissions(o.id) <> 0
  16850.     order by 8 desc, 4, 5, 6, 16
  16851. go
  16852. create procedure sp_indexes_rowset;2
  16853.     (
  16854.     @index_name    varchar(255) = null,
  16855.     @table_schema    varchar(255) = null     
  16856.     )
  16857. as
  16858.     select    TABLE_CATALOG        = db_name(),
  16859.         TABLE_SCHEMA        = user_name(o.uid),
  16860.         TABLE_NAME        = o.name,
  16861.         INDEX_CATALOG        = db_name(),        
  16862.         INDEX_SCHEMA        = user_name(o.uid),
  16863.         INDEX_NAME        = x.name,
  16864.         PRIMARY_KEY        = convert(bit,(x.status & 0x800)/0x800),
  16865.         "UNIQUE"        = convert(bit,(x.status & 2)/2),
  16866.         "CLUSTERED"        = convert(bit,(x.status & 16)/16),
  16867.         "TYPE"            = convert(smallint, 1 /*DBPROPVAL_IT_BTREE*/),
  16868.         FILL_FACTOR        = convert(int, x.OrigFillFactor),
  16869.         INITIAL_SIZE        = convert(int,null),
  16870.         NULLS            = convert(int,null),
  16871.         SORT_BOOKMARKS        = convert(bit,0),
  16872.         AUTO_UPDATE        = convert(bit,1),
  16873.         NULL_COLLATION        = convert(int,4 /*DBPROPVAL_NC_LOW*/),
  16874.         ORDINAL_POSITION    = convert(int,c.colid),
  16875.         COLUMN_NAME        = index_col(user_name(o.uid)+'.'+o.name, x.indid, c.colid),
  16876.         COLUMN_GUID        = convert(binary(16),null),
  16877.         COLUMN_PROPID        = convert(int,null),
  16878.         COLLATION        = convert(smallint, 1 /*DB_COLLATION_ASC*/),
  16879.         CARDINALITY        = case when (x.status & 2) = 2 then x.rows else null end,
  16880.         PAGES            = convert(int, x.dpages),
  16881.         FILTER_CONDITION    = convert(varchar(1),null) 
  16882.         
  16883.     from    sysobjects o, sysindexes x, syscolumns c
  16884.     where    o.type in ('U')
  16885.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  16886.     and    (@index_name is null or @index_name = x.name)
  16887.     and     x.id = o.id
  16888.     and     x.id = c.id
  16889.     and      c.colid < x.keycnt+(x.status&16)/16
  16890.     and     permissions(o.id) <> 0
  16891.     order by 8 desc, 4, 5, 6, 16
  16892. go
  16893. create procedure sp_indexes_rowset;3
  16894.     (
  16895.     @table_name    varchar(255) = null, 
  16896.     @index_name    varchar(255) = null,
  16897.     @table_schema    varchar(255) = null     
  16898.     )
  16899. as
  16900. IF @table_name is not null
  16901.     BEGIN
  16902.     select    TABLE_CATALOG        = db_name(),
  16903.         TABLE_SCHEMA        = user_name(o.uid),
  16904.         TABLE_NAME        = o.name,
  16905.         INDEX_CATALOG        = db_name(),        
  16906.         INDEX_SCHEMA        = user_name(o.uid),
  16907.         INDEX_NAME        = x.name,
  16908.         PRIMARY_KEY        = convert(bit,(x.status & 0x800)/0x800),
  16909.         "UNIQUE"        = convert(bit,(x.status & 2)/2),
  16910.         "CLUSTERED"        = convert(bit,(x.status & 16)/16),
  16911.         "TYPE"            = convert(smallint, 1 /*DBPROPVAL_IT_BTREE*/),
  16912.         FILL_FACTOR        = convert(int, x.OrigFillFactor),
  16913.         INITIAL_SIZE        = convert(int,null),
  16914.         NULLS            = convert(int,null),
  16915.         SORT_BOOKMARKS        = convert(bit,0),
  16916.         AUTO_UPDATE        = convert(bit,1),
  16917.         NULL_COLLATION        = convert(int,4 /*DBPROPVAL_NC_LOW*/),
  16918.         ORDINAL_POSITION    = convert(int,c.colid),
  16919.         COLUMN_NAME        = index_col(user_name(o.uid)+'.'+o.name, x.indid, c.colid),
  16920.         COLUMN_GUID        = convert(binary(16),null),
  16921.         COLUMN_PROPID        = convert(int,null),
  16922.         COLLATION        = convert(smallint, 1 /*DB_COLLATION_ASC*/),
  16923.         CARDINALITY        = case when (x.status & 2) = 2 then x.rows else null end,
  16924.         PAGES            = convert(int, x.dpages),
  16925.         FILTER_CONDITION    = convert(varchar(1),null) 
  16926.         
  16927.     from    sysobjects o, sysindexes x, syscolumns c
  16928.     where    o.type in ('U')
  16929.     and     o.name = @table_name
  16930.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  16931.     and    (@index_name is null or @index_name = x.name)
  16932.     and     x.id = o.id
  16933.     and     x.id = c.id
  16934.     and      c.colid < x.keycnt+(x.status&16)/16
  16935.     and     permissions(o.id) <> 0
  16936.     END
  16937. ELSE
  16938.     BEGIN
  16939.     select    TABLE_CATALOG        = db_name(),
  16940.         TABLE_SCHEMA        = user_name(o.uid),
  16941.         TABLE_NAME        = o.name,
  16942.         INDEX_CATALOG        = db_name(),        
  16943.         INDEX_SCHEMA        = user_name(o.uid),
  16944.         INDEX_NAME        = x.name,
  16945.         PRIMARY_KEY        = convert(bit,(x.status & 0x800)/0x800),
  16946.         "UNIQUE"        = convert(bit,(x.status & 2)/2),
  16947.         "CLUSTERED"        = convert(bit,(x.status & 16)/16),
  16948.         "TYPE"            = convert(smallint, 1 /*DBPROPVAL_IT_BTREE*/),
  16949.         FILL_FACTOR        = convert(int, x.OrigFillFactor),
  16950.         INITIAL_SIZE        = convert(int,null),
  16951.         NULLS            = convert(int,null),
  16952.         SORT_BOOKMARKS        = convert(bit,0),
  16953.         AUTO_UPDATE        = convert(bit,1),
  16954.         NULL_COLLATION        = convert(int,4 /*DBPROPVAL_NC_LOW*/),
  16955.         ORDINAL_POSITION    = convert(int,c.colid),
  16956.         COLUMN_NAME        = index_col(user_name(o.uid)+'.'+o.name, x.indid, c.colid),
  16957.         COLUMN_GUID        = convert(binary(16),null),
  16958.         COLUMN_PROPID        = convert(int,null),
  16959.         COLLATION        = convert(smallint, 1 /*DB_COLLATION_ASC*/),
  16960.         CARDINALITY        = case when (x.status & 2) = 2 then x.rows else null end,
  16961.         PAGES            = convert(int, x.dpages),
  16962.         FILTER_CONDITION    = convert(varchar(1),null) 
  16963.         
  16964.     from    sysobjects o, sysindexes x, syscolumns c
  16965.     where    o.type in ('U')
  16966.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  16967.     and    (@index_name is null or @index_name = x.name)
  16968.     and     x.id = o.id
  16969.     and     x.id = c.id
  16970.     and      c.colid < x.keycnt+(x.status&16)/16
  16971.     and     permissions(o.id) <> 0
  16972.     END
  16973. go
  16974.  
  16975.  
  16976. create procedure sp_indexes_rowset;4
  16977.     (
  16978.  
  16979.        @handle        int output,
  16980.        @scrollopt    int output,
  16981.     @ccopt        int output,
  16982.     @rows        int output,
  16983.     @cmd        varchar(255)
  16984.     )
  16985. as
  16986.     declare @ret int
  16987.     declare @database_name varchar(30)
  16988.  
  16989.     create table #sppindxrowset
  16990.         (
  16991.         TABLE_CATALOG        varchar(30) not null,
  16992.         TABLE_SCHEMA        varchar(30) not null,
  16993.         TABLE_NAME        varchar(30) not null,
  16994.         INDEX_CATALOG        varchar(30) not null,        
  16995.         INDEX_SCHEMA        varchar(30) not null,
  16996.         INDEX_NAME        varchar(30) not null,
  16997.         PRIMARY_KEY        bit not null,
  16998.         "UNIQUE"        bit not null,
  16999.         "CLUSTERED"        bit not null,
  17000.         "TYPE"            smallint not null,
  17001.         FILL_FACTOR        int not null,
  17002.         INITIAL_SIZE        int null,
  17003.         NULLS            int null,
  17004.         SORT_BOOKMARKS        bit not null,
  17005.         AUTO_UPDATE        bit not null,
  17006.         NULL_COLLATION        int not null,
  17007.         ORDINAL_POSITION    int not null,
  17008.         COLUMN_NAME        varchar(30) not null,
  17009.         COLUMN_GUID        binary(16) null,
  17010.         COLUMN_PROPID        int null,
  17011.         COLLATION        smallint not null,
  17012.         CARDINALITY        int null,
  17013.         PAGES            int not null,
  17014.         FILTER_CONDITION    varchar(1) null         
  17015.         )
  17016.  
  17017.     declare sysdatabases_cursor insensitive cursor for 
  17018.         select name from master.dbo.sysdatabases
  17019.   
  17020.     set nocount on
  17021.     open sysdatabases_cursor
  17022.       fetch next from sysdatabases_cursor into @database_name 
  17023.     while @@fetch_status = 0
  17024.         begin
  17025.         exec('use '+@database_name+' insert into #sppindxrowset '+@cmd)
  17026.         fetch next from sysdatabases_cursor into @database_name 
  17027.         end
  17028.       close sysdatabases_cursor
  17029.     deallocate sysdatabases_cursor
  17030.  
  17031.     if @scrollopt is null
  17032.         begin
  17033.         select * from #sppindxrowset order by 8 desc, 4, 5, 6, 16
  17034.         end
  17035.     else
  17036.         begin
  17037.         exec @ret = sp_cursoropen @handle output,
  17038.             'select * from #sppindxrowset order by 8 desc, 4, 5, 6, 16',
  17039.                 @scrollopt output, @ccopt output, @rows output
  17040.         end
  17041.     drop table #sppindxrowset
  17042.     return isnull(@ret,0)
  17043. go
  17044.  
  17045. grant execute on sp_indexes_rowset to public
  17046. go
  17047.  
  17048. dump tran master with no_log
  17049. go
  17050.  
  17051. print 'creating sp_primary_keys_rowset'
  17052. go
  17053.  
  17054. /*    Procedure for 6.0 and 6.5 servers */
  17055. create procedure sp_primary_keys_rowset
  17056.     (
  17057.        @table_name        varchar(255),
  17058.     @table_schema         varchar(244) = null
  17059.     )
  17060. as
  17061.     select    TABLE_CATALOG    = db_name(),
  17062.         TABLE_SCHEMA    = user_name(o.uid),
  17063.         TABLE_NAME    = o.name,    
  17064.         COLUMN_NAME    = c.name,
  17065.         COLUMN_GUID    = convert(binary(16),null),
  17066.         COLUMN_PROPID    = convert(int,null),
  17067.         ORDINAL        = convert(int,c1.colid)
  17068.     from    sysindexes i, syscolumns c, sysobjects o, syscolumns c1, sysusers u
  17069.     where    o.type in ('U')
  17070.     and     o.name = @table_name
  17071.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  17072.     and     o.id = c.id
  17073.     and     o.id = i.id
  17074.     and     (i.status & 0x800) = 0x800
  17075.     and     c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, c1.colid)
  17076.     and     c1.id = c.id
  17077.     and     c1.colid <= i.keycnt    /* create rows from 1 to keycnt */
  17078.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  17079.     and     (
  17080.         suser_id() = 1     /* User is the System Administrator */
  17081.         or o.uid = user_id()     /* User created the object */
  17082.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  17083.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  17084.             from sysprotects p
  17085.             where p.id = o.id
  17086.             /* get rows for public,current user,user's group */
  17087.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  17088.             /* check for SELECT,EXECUTE privilege */
  17089.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  17090.             ) = 1    /* final magic...compare Grants      */
  17091.            )
  17092.     order by 2, 3
  17093. go
  17094. create procedure sp_primary_keys_rowset;2
  17095.     (
  17096.      @table_schema         varchar(244) = null
  17097.     )
  17098. as
  17099.     select    TABLE_CATALOG    = db_name(),
  17100.         TABLE_SCHEMA    = user_name(o.uid),
  17101.         TABLE_NAME    = o.name,    
  17102.         COLUMN_NAME    = c.name,
  17103.         COLUMN_GUID    = convert(binary(16),null),
  17104.         COLUMN_PROPID    = convert(int,null),
  17105.         ORDINAL        = convert(int,c1.colid)
  17106.     from    sysindexes i, syscolumns c, sysobjects o, syscolumns c1, sysusers u
  17107.     where    o.type in ('U')
  17108.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  17109.     and     o.id = c.id
  17110.     and     o.id = i.id
  17111.     and     (i.status & 0x800) = 0x800
  17112.     and     c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, c1.colid)
  17113.     and     c1.id = c.id
  17114.     and     c1.colid <= i.keycnt    /* create rows from 1 to keycnt */
  17115.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  17116.     and     (
  17117.         suser_id() = 1     /* User is the System Administrator */
  17118.         or o.uid = user_id()     /* User created the object */
  17119.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  17120.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  17121.             from sysprotects p
  17122.             where p.id = o.id
  17123.             /* get rows for public,current user,user's group */
  17124.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  17125.             /* check for SELECT,EXECUTE privilege */
  17126.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  17127.             ) = 1    /* final magic...compare Grants      */
  17128.            )
  17129.     order by 2, 3
  17130. go
  17131. create procedure sp_primary_keys_rowset;3
  17132.     (
  17133.        @table_name        varchar(255) = null,
  17134.     @table_schema         varchar(244) = null
  17135.     )
  17136. as
  17137. IF @table_name is not NULL
  17138.     BEGIN
  17139.     select    TABLE_CATALOG    = db_name(),
  17140.         TABLE_SCHEMA    = user_name(o.uid),
  17141.         TABLE_NAME    = o.name,    
  17142.         COLUMN_NAME    = c.name,
  17143.         COLUMN_GUID    = convert(binary(16),null),
  17144.         COLUMN_PROPID    = convert(int,null),
  17145.         ORDINAL        = convert(int,c1.colid)
  17146.     from    sysindexes i, syscolumns c, sysobjects o, syscolumns c1, sysusers u
  17147.     where    o.type in ('U')
  17148.     and     o.name = @table_name
  17149.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  17150.     and     o.id = c.id
  17151.     and     o.id = i.id
  17152.     and     (i.status & 0x800) = 0x800
  17153.     and     c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, c1.colid)
  17154.     and     c1.id = c.id
  17155.     and     c1.colid <= i.keycnt    /* create rows from 1 to keycnt */
  17156.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  17157.     and     (
  17158.         suser_id() = 1     /* User is the System Administrator */
  17159.         or o.uid = user_id()     /* User created the object */
  17160.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  17161.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  17162.             from sysprotects p
  17163.             where p.id = o.id
  17164.             /* get rows for public,current user,user's group */
  17165.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  17166.             /* check for SELECT,EXECUTE privilege */
  17167.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  17168.             ) = 1    /* final magic...compare Grants      */
  17169.            )
  17170.     END
  17171. ELSE
  17172.     BEGIN
  17173.     select    TABLE_CATALOG    = db_name(),
  17174.         TABLE_SCHEMA    = user_name(o.uid),
  17175.         TABLE_NAME    = o.name,    
  17176.         COLUMN_NAME    = c.name,
  17177.         COLUMN_GUID    = convert(binary(16),null),
  17178.         COLUMN_PROPID    = convert(int,null),
  17179.         ORDINAL        = convert(int,c1.colid)
  17180.     from    sysindexes i, syscolumns c, sysobjects o, syscolumns c1, sysusers u
  17181.     where    o.type in ('U')
  17182.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  17183.     and     o.id = c.id
  17184.     and     o.id = i.id
  17185.     and     (i.status & 0x800) = 0x800
  17186.     and     c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, c1.colid)
  17187.     and     c1.id = c.id
  17188.     and     c1.colid <= i.keycnt    /* create rows from 1 to keycnt */
  17189.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  17190.     and     (
  17191.         suser_id() = 1     /* User is the System Administrator */
  17192.         or o.uid = user_id()     /* User created the object */
  17193.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  17194.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  17195.             from sysprotects p
  17196.             where p.id = o.id
  17197.             /* get rows for public,current user,user's group */
  17198.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  17199.             /* check for SELECT,EXECUTE privilege */
  17200.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  17201.             ) = 1    /* final magic...compare Grants      */
  17202.            )
  17203.     END
  17204. go
  17205.  
  17206. if (charindex('7.00', @@version) = 0)
  17207. begin
  17208.     print ''
  17209.     print ''
  17210.     print 'Warning:'
  17211.     print 'you are installing the stored procedures '
  17212.     print 'on a pre 7.0 SQL Server.'
  17213.     print 'Ignore the following errors.'
  17214. end
  17215. else
  17216.     drop proc sp_primary_keys_rowset
  17217. go
  17218.  
  17219. /*    Procedure for 7.0 servers */
  17220. create procedure sp_primary_keys_rowset
  17221.     (
  17222.        @table_name        varchar(255),
  17223.     @table_schema         varchar(244) = null
  17224.     )
  17225. as
  17226.     select    TABLE_CATALOG    = db_name(),
  17227.         TABLE_SCHEMA    = user_name(o.uid),
  17228.         TABLE_NAME    = o.name,    
  17229.         COLUMN_NAME    = c.name,
  17230.         COLUMN_GUID    = convert(binary(16),null),
  17231.         COLUMN_PROPID    = convert(int,null),
  17232.         ORDINAL        = convert(int,c1.colid)
  17233.     from    sysindexes i, syscolumns c, sysobjects o, syscolumns c1
  17234.     where    o.type in ('U')
  17235.     and     o.name = @table_name
  17236.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  17237.     and     o.id = c.id
  17238.     and     o.id = i.id
  17239.     and     (i.status & 0x800) = 0x800
  17240.     and     c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, c1.colid)
  17241.     and     c1.id = c.id
  17242.     and     c1.colid <= i.keycnt    /* create rows from 1 to keycnt */
  17243.     and    permissions(o.id) <> 0
  17244.     order by 2, 3
  17245. go
  17246. create procedure sp_primary_keys_rowset;2
  17247.     (
  17248.      @table_schema         varchar(244) = null
  17249.     )
  17250. as
  17251.     select    TABLE_CATALOG    = db_name(),
  17252.         TABLE_SCHEMA    = user_name(o.uid),
  17253.         TABLE_NAME    = o.name,    
  17254.         COLUMN_NAME    = c.name,
  17255.         COLUMN_GUID    = convert(binary(16),null),
  17256.         COLUMN_PROPID    = convert(int,null),
  17257.         ORDINAL        = convert(int,c1.colid)
  17258.     from    sysindexes i, syscolumns c, sysobjects o, syscolumns c1
  17259.     where    o.type in ('U')
  17260.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  17261.     and     o.id = c.id
  17262.     and     o.id = i.id
  17263.     and     (i.status & 0x800) = 0x800
  17264.     and     c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, c1.colid)
  17265.     and     c1.id = c.id
  17266.     and     c1.colid <= i.keycnt    /* create rows from 1 to keycnt */
  17267.     and    permissions(o.id) <> 0
  17268.     order by 2, 3
  17269. go
  17270. create procedure sp_primary_keys_rowset;3
  17271.     (
  17272.        @table_name        varchar(255) = null,
  17273.     @table_schema         varchar(244) = null
  17274.     )
  17275. as
  17276. IF @table_name is not NULL
  17277.     BEGIN
  17278.     select    TABLE_CATALOG    = db_name(),
  17279.         TABLE_SCHEMA    = user_name(o.uid),
  17280.         TABLE_NAME    = o.name,    
  17281.         COLUMN_NAME    = c.name,
  17282.         COLUMN_GUID    = convert(binary(16),null),
  17283.         COLUMN_PROPID    = convert(int,null),
  17284.         ORDINAL        = convert(int,c1.colid)
  17285.     from    sysindexes i, syscolumns c, sysobjects o, syscolumns c1
  17286.     where    o.type in ('U')
  17287.     and     o.name = @table_name
  17288.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  17289.     and     o.id = c.id
  17290.     and     o.id = i.id
  17291.     and     (i.status & 0x800) = 0x800
  17292.     and     c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, c1.colid)
  17293.     and     c1.id = c.id
  17294.     and     c1.colid <= i.keycnt    /* create rows from 1 to keycnt */
  17295.     and    permissions(o.id) <> 0
  17296.     END
  17297. ELSE
  17298.     BEGIN
  17299.     select    TABLE_CATALOG    = db_name(),
  17300.         TABLE_SCHEMA    = user_name(o.uid),
  17301.         TABLE_NAME    = o.name,    
  17302.         COLUMN_NAME    = c.name,
  17303.         COLUMN_GUID    = convert(binary(16),null),
  17304.         COLUMN_PROPID    = convert(int,null),
  17305.         ORDINAL        = convert(int,c1.colid)
  17306.     from    sysindexes i, syscolumns c, sysobjects o, syscolumns c1
  17307.     where    o.type in ('U')
  17308.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  17309.     and     o.id = c.id
  17310.     and     o.id = i.id
  17311.     and     (i.status & 0x800) = 0x800
  17312.     and     c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, c1.colid)
  17313.     and     c1.id = c.id
  17314.     and     c1.colid <= i.keycnt    /* create rows from 1 to keycnt */
  17315.     and    permissions(o.id) <> 0
  17316.     END
  17317. go
  17318.  
  17319.  
  17320. create procedure sp_primary_keys_rowset;4
  17321.     (
  17322.  
  17323.        @handle        int output,
  17324.        @scrollopt    int output,
  17325.     @ccopt        int output,
  17326.     @rows        int output,
  17327.     @cmd        varchar(255)
  17328.     )
  17329. as
  17330.     declare @ret int
  17331.     declare @database_name varchar(30)
  17332.  
  17333.     create table #sppkeysrowset
  17334.         (
  17335.         TABLE_CATALOG        varchar(30) not null,
  17336.         TABLE_SCHEMA        varchar(30) not null,
  17337.         TABLE_NAME        varchar(30) not null,
  17338.         COLUMN_NAME        varchar(30) not null,
  17339.         COLUMN_GUID        binary(16) null,
  17340.         COLUMN_PROPID        int null,
  17341.         ORDINAL            int null
  17342.         )
  17343.  
  17344.     declare sysdatabases_cursor insensitive cursor for 
  17345.         select name from master.dbo.sysdatabases
  17346.   
  17347.     set nocount on
  17348.     open sysdatabases_cursor
  17349.       fetch next from sysdatabases_cursor into @database_name 
  17350.     while @@fetch_status = 0
  17351.         begin
  17352.         exec('use '+@database_name+' insert into #sppkeysrowset '+@cmd)
  17353.         fetch next from sysdatabases_cursor into @database_name 
  17354.         end
  17355.       close sysdatabases_cursor
  17356.     deallocate sysdatabases_cursor
  17357.  
  17358.     if @scrollopt is null
  17359.         begin
  17360.         select * from #sppkeysrowset order by 1,2,3
  17361.         end
  17362.     else
  17363.         begin
  17364.         exec @ret = sp_cursoropen @handle output,
  17365.             'select * from #sppkeysrowset order by 1,2,3',
  17366.                 @scrollopt output, @ccopt output, @rows output
  17367.         end
  17368.     drop table #sppkeysrowset
  17369.     return isnull(@ret,0)
  17370. go
  17371.  
  17372. grant execute on sp_primary_keys_rowset to public
  17373. go
  17374.  
  17375. dump tran master with no_log
  17376. go
  17377.  
  17378. print 'creating sp_provider_types_rowset'
  17379. go
  17380.  
  17381. /*    Procedure for 6.0 and 6.50 servers */
  17382. create proc sp_provider_types_rowset
  17383.     (
  17384.     @data_type     smallint = null,
  17385.     @best_match    tinyint  = null
  17386.     )
  17387. as
  17388.     select
  17389.         TYPE_NAME         = case    when t.usertype in (18,80) then t.name 
  17390.                         else d.type_name 
  17391.                         end,
  17392.         DATA_TYPE         = d.oledb_data_type,                            
  17393.         COLUMN_SIZE        = coalesce(d.column_size,d.data_precision,t.prec),
  17394.         LITERAL_PREFIX         = d.literal_prefix,
  17395.         LITERAL_SUFFIX         = d.literal_suffix,
  17396.         CREATE_PARAMS         = e.create_params,
  17397.         IS_NULLABLE        = convert(bit, 
  17398.                         case 
  17399.                         when d.auto_unique_value = 1 then convert(bit,0) /* IDENTITY*/
  17400.                         else t.allownulls
  17401.                         end),
  17402.         CASE_SENSITIVE        = d.case_sensitive,
  17403.         SEARCHABLE         = d.searchable,
  17404.         UNSIGNED_ATTRIBUTE    = d.unsigned_attribute,
  17405.         FIXED_PREC_SCALE    = d.fixed_prec_scale,
  17406.         AUTO_UNIQUE_VALUE    = d.auto_unique_value, 
  17407.         LOCAL_TYPE_NAME        = d.local_type_name,
  17408.         MINIMUM_SCALE        = convert(smallint,
  17409.                         case 
  17410.                         when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then 0 
  17411.                         else null 
  17412.                         end),
  17413.         MAXIMUM_SCALE        = convert(smallint,
  17414.                         case 
  17415.                         when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ 
  17416.                             then case 
  17417.                                 when d.auto_unique_value = 1 then 0 
  17418.                                 else d.data_precision 
  17419.                                 end
  17420.                         else null 
  17421.                         end),
  17422.         GUID            = convert(binary(16),null),
  17423.         TYPELIB            = convert(varchar(1),null),
  17424.         VERSION            = convert(varchar(1),null),
  17425.         IS_LONG            = d.is_long,
  17426.         BEST_MATCH        = d.best_match
  17427.  
  17428.     from     master.dbo.spt_provider_types d, master.dbo.spt_datatype_info_ext e, systypes t
  17429.     where    d.ss_dtype = t.type 
  17430.     and    t.usertype <= 100
  17431.     and    (case when t.usertype = 80 /* TIMESTAMP */ then 1 else 0 end 
  17432.             = case when d.oledb_data_type = 21 /*DBTYPE_UI8*/ then 1 else 0 end)
  17433.     and     t.usertype *= e.user_type 
  17434.     and     d.auto_unique_value *= e.AUTO_INCREMENT
  17435.     and     t.type not in (111,109,38,110,55,63)    /* get rid of nullable types */
  17436.     and    (@data_type is null or d.oledb_data_type = @data_type)
  17437.     and    (@best_match is null or d.best_match = @best_match)
  17438.     order by 2
  17439. go
  17440.  
  17441. if (charindex('7.00', @@version) = 0)
  17442. begin
  17443.     print ''
  17444.     print ''
  17445.     print 'Warning:'
  17446.     print 'you are installing the stored procedures '
  17447.     print 'on a pre 7.0 SQL Server.'
  17448.     print 'Ignore the following errors.'
  17449. end
  17450. else
  17451.     drop proc sp_provider_types_rowset
  17452. go
  17453.  
  17454. /*    Procedure for 7.0 server */
  17455. create proc sp_provider_types_rowset
  17456.     (
  17457.     @data_type     smallint = null,
  17458.     @best_match    tinyint  = null
  17459.     )
  17460. as
  17461.     select
  17462.         TYPE_NAME         = case    when t.usertype in (18,80) then t.name 
  17463.                         else d.type_name 
  17464.                         end,
  17465.         DATA_TYPE         = d.oledb_data_type,                            
  17466.         COLUMN_SIZE        = coalesce(d.column_size,d.data_precision,t.prec),
  17467.         LITERAL_PREFIX         = d.literal_prefix,
  17468.         LITERAL_SUFFIX         = d.literal_suffix,
  17469.         CREATE_PARAMS         = e.create_params,
  17470.         IS_NULLABLE        = convert(bit, 
  17471.                         case 
  17472.                         when d.auto_unique_value = 1 then 0 /* IDENTITY*/
  17473.                         else TypeProperty (t.name, 'AllowsNull')
  17474.                         end),
  17475.         CASE_SENSITIVE        = d.case_sensitive,
  17476.         SEARCHABLE         = d.searchable,
  17477.         UNSIGNED_ATTRIBUTE    = d.unsigned_attribute,
  17478.         FIXED_PREC_SCALE    = d.fixed_prec_scale,
  17479.         AUTO_UNIQUE_VALUE    = d.auto_unique_value, 
  17480.         LOCAL_TYPE_NAME        = d.local_type_name,
  17481.         MINIMUM_SCALE        = convert(smallint,
  17482.                         case 
  17483.                         when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then 0 
  17484.                         else null 
  17485.                         end),
  17486.         MAXIMUM_SCALE        = convert(smallint,
  17487.                         case 
  17488.                         when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ 
  17489.                             then case 
  17490.                                 when d.auto_unique_value = 1 then 0 
  17491.                                 else d.data_precision 
  17492.                                 end
  17493.                         else null 
  17494.                         end),
  17495.         GUID            = convert(binary(16),null),
  17496.         TYPELIB            = convert(varchar(1),null),
  17497.         VERSION            = convert(varchar(1),null),
  17498.         IS_LONG            = d.is_long,
  17499.         BEST_MATCH        = d.best_match
  17500.     from master.dbo.spt_provider_types d
  17501.         INNER JOIN model.dbo.systypes t on d.ss_dtype = t.xtype
  17502.         LEFT OUTER JOIN master.dbo.spt_datatype_info_ext e on
  17503.             t.xusertype = e.user_type
  17504.             and d.auto_unique_value = e.AUTO_INCREMENT
  17505.     where    (@data_type is null or d.oledb_data_type = @data_type)
  17506.     and    (@best_match is null or d.best_match = @best_match)    
  17507.     and    t.usertype <= 255
  17508.     order by 2
  17509. go
  17510.  
  17511. grant execute on sp_provider_types_rowset to public
  17512. go
  17513.  
  17514. dump tran master with no_log
  17515. go
  17516.  
  17517. print 'creating sp_procedure_params_rowset'
  17518. go
  17519.  
  17520. /*    Procedure for 6.0 and 6.50 servers */
  17521. create procedure sp_procedure_params_rowset
  17522.     (
  17523.        @procedure_name        varchar(255) = null,
  17524.     @group_number        int = null,
  17525.     @procedure_schema     varchar(255) = null,
  17526.     @parameter_name        varchar(255) = null
  17527.     )
  17528. as
  17529. IF @procedure_name is not null
  17530.     BEGIN
  17531.     select
  17532.         PROCEDURE_CATALOG     = db_name(),
  17533.         PROCEDURE_SCHEMA     = user_name(),
  17534.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  17535.         PARAMETER_NAME         = c.name,
  17536.         ORDINAL_POSITION     = convert(smallint,c.colid),
  17537.         PARAMETER_TYPE         = convert(smallint, 1+((c.status/64)&1)),
  17538.         PARAMETER_HASDEFAULT    = convert(tinyint, null),
  17539.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  17540.         IS_NULLABLE        = d.is_nullable,
  17541.         DATA_TYPE        = d.oledb_data_type,
  17542.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  17543.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  17544.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  17545.                         then coalesce(d.column_size,c.length)
  17546.                         else null 
  17547.                         end),
  17548.         CHARACTER_OCTET_LENGTH    = convert(int,
  17549.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  17550.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  17551.                         then coalesce(d.column_size,c.length)
  17552.                         else null 
  17553.                         end),
  17554.         NUMERIC_PRECISION    = convert(smallint,
  17555.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec else null end),
  17556.         NUMERIC_SCALE        = convert(smallint, 
  17557.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  17558.         DESCRIPTION        = convert(varchar(1),null)
  17559.     
  17560.     from
  17561.         syscolumns c,
  17562.         sysobjects o,
  17563.         master.dbo.spt_provider_types d,
  17564.         systypes t,
  17565.         sysusers u
  17566.     where
  17567.         o.name = @procedure_name
  17568.     and    o.type = 'P'                            /* Just Procedures */
  17569.     and    (@procedure_schema = null or @procedure_schema = user_name(o.uid))
  17570.     and    o.id = c.id
  17571.     and    c.number = @group_number
  17572.     and    c.type = d.ss_dtype
  17573.     and    c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  17574.     and    coalesce(d.auto_unique_value,0) = 0
  17575.     and    c.usertype = t.usertype
  17576.     and    (@parameter_name is null or @parameter_name = c.name)
  17577.     and     u.uid = user_id()    /* constrain sysusers uid for use in subquery */
  17578.     and     (    suser_id() = 1     /* User is the System Administrator */
  17579.         or     o.uid = user_id()    /* User created the object */
  17580.             /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  17581.         or     ((select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  17582.              from sysprotects p
  17583.              where p.id = o.id
  17584.                  /*  get rows for public,current user,user's group */
  17585.                  and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  17586.                  /* check for SELECT,EXECUTE privilege */
  17587.              and (action in (193,224)))&1    /* more magic...normalize GRANT */
  17588.             ) = 1     /* final magic...compare Grants    */
  17589.         )
  17590.     UNION ALL
  17591.     SELECT           /* return value row*/
  17592.         PROCEDURE_CATALOG     = db_name(),
  17593.         PROCEDURE_SCHEMA     = user_name(),
  17594.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  17595.         PARAMETER_NAME         = convert(varchar(30),'RETURN_VALUE'),
  17596.         ORDINAL_POSITION     = convert(smallint,0),
  17597.         PARAMETER_TYPE         = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  17598.         PARAMETER_HASDEFAULT    = convert(tinyint,null),
  17599.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  17600.         IS_NULLABLE        = convert(bit,0),
  17601.         DATA_TYPE        = convert(smallint, 3 /*DBTYPE_I4*/),
  17602.         CHARACTER_MAXIMUM_LENGTH= convert(int,null),
  17603.         CHARACTER_OCTET_LENGTH    = convert(int,null),
  17604.         NUMERIC_PRECISION    = convert(smallint,null),
  17605.         NUMERIC_SCALE        = convert(smallint,null),
  17606.         DESCRIPTION        = convert(varchar(1),null)
  17607.     from
  17608.         syscolumns c,
  17609.         sysobjects o,
  17610.         sysusers u
  17611.     where
  17612.         o.name = @procedure_name
  17613.     and    o.type = 'P'                        /* Just Procedures */
  17614.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  17615.     and    (@parameter_name is null or @parameter_name = 'RETURN_VALUE')
  17616.     and    c.id =* o.id
  17617.     and     c.colid = 1
  17618.     and     u.uid = user_id()    /* constrain sysusers uid for use in subquery */
  17619.     and     (    suser_id() = 1     /* User is the System Administrator */
  17620.         or     o.uid = user_id()    /* User created the object */
  17621.             /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  17622.         or     ((select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  17623.              from sysprotects p
  17624.              where p.id = o.id
  17625.                  /*  get rows for public,current user,user's group */
  17626.                  and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  17627.                  /* check for SELECT,EXECUTE privilege */
  17628.              and (action in (193,224)))&1    /* more magic...normalize GRANT */
  17629.             ) = 1     /* final magic...compare Grants    */
  17630.         )
  17631.     order by 2, 3, 5
  17632.     END
  17633. ELSE
  17634.     BEGIN
  17635.     select
  17636.         PROCEDURE_CATALOG     = db_name(),
  17637.         PROCEDURE_SCHEMA     = user_name(),
  17638.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  17639.         PARAMETER_NAME         = c.name,
  17640.         ORDINAL_POSITION     = convert(smallint,c.colid),
  17641.         PARAMETER_TYPE         = convert(smallint, 1+((c.status/64)&1)),
  17642.         PARAMETER_HASDEFAULT    = convert(tinyint,null),
  17643.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  17644.         IS_NULLABLE        = d.is_nullable,
  17645.         DATA_TYPE        = d.oledb_data_type,
  17646.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  17647.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  17648.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  17649.                         then coalesce(d.column_size,c.length)
  17650.                         else null 
  17651.                         end),
  17652.         CHARACTER_OCTET_LENGTH    = convert(int,
  17653.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  17654.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  17655.                         then coalesce(d.column_size,c.length)
  17656.                         else null 
  17657.                         end),
  17658.         NUMERIC_PRECISION    = convert(smallint,
  17659.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec else null end),
  17660.         NUMERIC_SCALE        = convert(smallint, 
  17661.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  17662.         DESCRIPTION        = convert(varchar(1),null)
  17663.     
  17664.     from
  17665.         syscolumns c,
  17666.         sysobjects o,
  17667.         master.dbo.spt_provider_types d,
  17668.         systypes t,
  17669.         sysusers u
  17670.     where
  17671.         o.type = 'P'                            /* Just Procedures */
  17672.     and    (@procedure_schema = null or @procedure_schema = user_name(o.uid))
  17673.     and    o.id = c.id
  17674.     and    c.type = d.ss_dtype
  17675.     and    c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  17676.     and    coalesce(d.auto_unique_value,0) = 0
  17677.     and    c.usertype = t.usertype
  17678.     and    (@parameter_name is null or @parameter_name = c.name)
  17679.     and     u.uid = user_id()    /* constrain sysusers uid for use in subquery */
  17680.     and     (    suser_id() = 1     /* User is the System Administrator */
  17681.         or     o.uid = user_id()    /* User created the object */
  17682.             /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  17683.         or     ((select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  17684.              from sysprotects p
  17685.              where p.id = o.id
  17686.                  /*  get rows for public,current user,user's group */
  17687.                  and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  17688.                  /* check for SELECT,EXECUTE privilege */
  17689.              and (action in (193,224)))&1    /* more magic...normalize GRANT */
  17690.             ) = 1     /* final magic...compare Grants    */
  17691.         )
  17692.     UNION ALL
  17693.     SELECT           /* return value row*/
  17694.         PROCEDURE_CATALOG     = db_name(),
  17695.         PROCEDURE_SCHEMA     = user_name(),
  17696.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  17697.         PARAMETER_NAME         = convert(varchar(30),'RETURN_VALUE'),
  17698.         ORDINAL_POSITION     = convert(smallint,0),
  17699.         PARAMETER_TYPE         = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  17700.         PARAMETER_HASDEFAULT    = convert(tinyint,null),
  17701.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  17702.         IS_NULLABLE        = convert(bit,0),
  17703.         DATA_TYPE        = convert(smallint, 3 /*DBTYPE_I4*/),
  17704.         CHARACTER_MAXIMUM_LENGTH= convert(int,null),
  17705.         CHARACTER_OCTET_LENGTH    = convert(int,null),
  17706.         NUMERIC_PRECISION    = convert(smallint,null),
  17707.         NUMERIC_SCALE        = convert(smallint,null),
  17708.         DESCRIPTION        = convert(varchar(1),null)
  17709.     from
  17710.         syscolumns c,
  17711.         sysobjects o,
  17712.         sysusers u
  17713.     where
  17714.         o.type = 'P'                        /* Just Procedures */
  17715.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  17716.     and    (@parameter_name is null or @parameter_name = 'RETURN_VALUE')
  17717.     and    c.id =* o.id
  17718.     and     c.colid = 1
  17719.     and     u.uid = user_id()    /* constrain sysusers uid for use in subquery */
  17720.     and     (    suser_id() = 1     /* User is the System Administrator */
  17721.         or     o.uid = user_id()    /* User created the object */
  17722.             /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  17723.         or     ((select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  17724.              from sysprotects p
  17725.              where p.id = o.id
  17726.                  /*  get rows for public,current user,user's group */
  17727.                  and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  17728.                  /* check for SELECT,EXECUTE privilege */
  17729.              and (action in (193,224)))&1    /* more magic...normalize GRANT */
  17730.             ) = 1     /* final magic...compare Grants    */
  17731.         )
  17732.     order by 2, 3, 5
  17733.     END
  17734. go
  17735. create procedure sp_procedure_params_rowset;2
  17736.     (
  17737.       @handle            int output,
  17738.        @scrollopt        int output,
  17739.     @ccopt            int output,
  17740.     @rows            int output,
  17741.        @procedure_name        varchar(255) = null,
  17742.     @group_number        int = null,
  17743.     @procedure_schema     varchar(255) = null,
  17744.     @parameter_name        varchar(255) = null
  17745.     )
  17746. as
  17747.     declare @ret int
  17748.  
  17749.  
  17750.     create table #spprocparamrowset1
  17751.         (
  17752.         PROCEDURE_CATALOG     varchar(30) not null,
  17753.         PROCEDURE_SCHEMA     varchar(30) not null,
  17754.         PROCEDURE_NAME         varchar(35) not null,
  17755.         PARAMETER_NAME         varchar(30) not null,
  17756.         ORDINAL_POSITION     smallint not null,
  17757.         PARAMETER_TYPE         smallint null,
  17758.         PARAMETER_HASDEFAULT    tinyint null,
  17759.         PARAMETER_DEFAULT    varchar(255) null,
  17760.         IS_NULLABLE        bit not null,
  17761.         DATA_TYPE        smallint null,
  17762.         CHARACTER_MAXIMUM_LENGTH int null,
  17763.         CHARACTER_OCTET_LENGTH    int null,
  17764.         NUMERIC_PRECISION    smallint null,
  17765.         NUMERIC_SCALE        smallint null,
  17766.         DESCRIPTION        varchar(1) null
  17767.         )
  17768.  
  17769. IF @procedure_name is not null
  17770.     BEGIN
  17771.     insert into #spprocparamrowset1
  17772.     select
  17773.         PROCEDURE_CATALOG     = db_name(),
  17774.         PROCEDURE_SCHEMA     = user_name(),
  17775.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  17776.         PARAMETER_NAME         = c.name,
  17777.         ORDINAL_POSITION     = convert(smallint,c.colid),
  17778.         PARAMETER_TYPE         = convert(smallint, 1+((c.status/64)&1)),
  17779.         PARAMETER_HASDEFAULT    = convert(tinyint, null),
  17780.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  17781.         IS_NULLABLE        = d.is_nullable,
  17782.         DATA_TYPE        = d.oledb_data_type,
  17783.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  17784.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  17785.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  17786.                         then coalesce(d.column_size,c.length)
  17787.                         else null 
  17788.                         end),
  17789.         CHARACTER_OCTET_LENGTH    = convert(int,
  17790.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  17791.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  17792.                         then coalesce(d.column_size,c.length)
  17793.                         else null 
  17794.                         end),
  17795.         NUMERIC_PRECISION    = convert(smallint,
  17796.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec else null end),
  17797.         NUMERIC_SCALE        = convert(smallint, 
  17798.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  17799.         DESCRIPTION        = convert(varchar(1),null)
  17800.     
  17801.     from
  17802.         syscolumns c,
  17803.         sysobjects o,
  17804.         master.dbo.spt_provider_types d,
  17805.         systypes t,
  17806.         sysusers u
  17807.     where
  17808.         o.name = @procedure_name
  17809.     and    o.type = 'P'                            /* Just Procedures */
  17810.     and    (@procedure_schema = null or @procedure_schema = user_name(o.uid))
  17811.     and    o.id = c.id
  17812.     and    c.number = @group_number
  17813.     and    c.type = d.ss_dtype
  17814.     and    c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  17815.     and    coalesce(d.auto_unique_value,0) = 0
  17816.     and    c.usertype = t.usertype
  17817.     and    (@parameter_name is null or @parameter_name = c.name)
  17818.     and     u.uid = user_id()    /* constrain sysusers uid for use in subquery */
  17819.     and     (    suser_id() = 1     /* User is the System Administrator */
  17820.         or     o.uid = user_id()    /* User created the object */
  17821.             /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  17822.         or     ((select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  17823.              from sysprotects p
  17824.              where p.id = o.id
  17825.                  /*  get rows for public,current user,user's group */
  17826.                  and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  17827.                  /* check for SELECT,EXECUTE privilege */
  17828.              and (action in (193,224)))&1    /* more magic...normalize GRANT */
  17829.             ) = 1     /* final magic...compare Grants    */
  17830.         )
  17831.     UNION ALL
  17832.     SELECT           /* return value row*/
  17833.         PROCEDURE_CATALOG     = db_name(),
  17834.         PROCEDURE_SCHEMA     = user_name(),
  17835.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  17836.         PARAMETER_NAME         = convert(varchar(30),'RETURN_VALUE'),
  17837.         ORDINAL_POSITION     = convert(smallint,0),
  17838.         PARAMETER_TYPE         = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  17839.         PARAMETER_HASDEFAULT    = convert(tinyint,null),
  17840.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  17841.         IS_NULLABLE        = convert(bit,0),
  17842.         DATA_TYPE        = convert(smallint, 3 /*DBTYPE_I4*/),
  17843.         CHARACTER_MAXIMUM_LENGTH= convert(int,null),
  17844.         CHARACTER_OCTET_LENGTH    = convert(int,null),
  17845.         NUMERIC_PRECISION    = convert(smallint,null),
  17846.         NUMERIC_SCALE        = convert(smallint,null),
  17847.         DESCRIPTION        = convert(varchar(1),null)
  17848.     from
  17849.         syscolumns c,
  17850.         sysobjects o,
  17851.         sysusers u
  17852.     where
  17853.         o.name = @procedure_name
  17854.     and    o.type = 'P'                        /* Just Procedures */
  17855.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  17856.     and    (@parameter_name is null or @parameter_name = 'RETURN_VALUE')
  17857.     and    c.id =* o.id
  17858.     and     c.colid = 1
  17859.     and     u.uid = user_id()    /* constrain sysusers uid for use in subquery */
  17860.     and     (    suser_id() = 1     /* User is the System Administrator */
  17861.         or     o.uid = user_id()    /* User created the object */
  17862.             /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  17863.         or     ((select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  17864.              from sysprotects p
  17865.              where p.id = o.id
  17866.                  /*  get rows for public,current user,user's group */
  17867.                  and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  17868.                  /* check for SELECT,EXECUTE privilege */
  17869.              and (action in (193,224)))&1    /* more magic...normalize GRANT */
  17870.             ) = 1     /* final magic...compare Grants    */
  17871.         )
  17872.     order by 2, 3, 5
  17873.     END
  17874. ELSE
  17875.     BEGIN
  17876.     insert into #spprocparamrowset1
  17877.     select
  17878.         PROCEDURE_CATALOG     = db_name(),
  17879.         PROCEDURE_SCHEMA     = user_name(),
  17880.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  17881.         PARAMETER_NAME         = c.name,
  17882.         ORDINAL_POSITION     = convert(smallint,c.colid),
  17883.         PARAMETER_TYPE         = convert(smallint, 1+((c.status/64)&1)),
  17884.         PARAMETER_HASDEFAULT    = convert(tinyint,null),
  17885.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  17886.         IS_NULLABLE        = d.is_nullable,
  17887.         DATA_TYPE        = d.oledb_data_type,
  17888.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  17889.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  17890.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  17891.                         then coalesce(d.column_size,c.length)
  17892.                         else null 
  17893.                         end),
  17894.         CHARACTER_OCTET_LENGTH    = convert(int,
  17895.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  17896.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  17897.                         then coalesce(d.column_size,c.length)
  17898.                         else null 
  17899.                         end),
  17900.         NUMERIC_PRECISION    = convert(smallint,
  17901.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec else null end),
  17902.         NUMERIC_SCALE        = convert(smallint, 
  17903.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  17904.         DESCRIPTION        = convert(varchar(1),null)
  17905.     
  17906.     from
  17907.         syscolumns c,
  17908.         sysobjects o,
  17909.         master.dbo.spt_provider_types d,
  17910.         systypes t,
  17911.         sysusers u
  17912.     where
  17913.         o.type = 'P'                            /* Just Procedures */
  17914.     and    (@procedure_schema = null or @procedure_schema = user_name(o.uid))
  17915.     and    o.id = c.id
  17916.     and    c.type = d.ss_dtype
  17917.     and    c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  17918.     and    coalesce(d.auto_unique_value,0) = 0
  17919.     and    c.usertype = t.usertype
  17920.     and    (@parameter_name is null or @parameter_name = c.name)
  17921.     and     u.uid = user_id()    /* constrain sysusers uid for use in subquery */
  17922.     and     (    suser_id() = 1     /* User is the System Administrator */
  17923.         or     o.uid = user_id()    /* User created the object */
  17924.             /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  17925.         or     ((select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  17926.              from sysprotects p
  17927.              where p.id = o.id
  17928.                  /*  get rows for public,current user,user's group */
  17929.                  and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  17930.                  /* check for SELECT,EXECUTE privilege */
  17931.              and (action in (193,224)))&1    /* more magic...normalize GRANT */
  17932.             ) = 1     /* final magic...compare Grants    */
  17933.         )
  17934.     UNION ALL
  17935.     SELECT           /* return value row*/
  17936.         PROCEDURE_CATALOG     = db_name(),
  17937.         PROCEDURE_SCHEMA     = user_name(),
  17938.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  17939.         PARAMETER_NAME         = convert(varchar(30),'RETURN_VALUE'),
  17940.         ORDINAL_POSITION     = convert(smallint,0),
  17941.         PARAMETER_TYPE         = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  17942.         PARAMETER_HASDEFAULT    = convert(tinyint,null),
  17943.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  17944.         IS_NULLABLE        = convert(bit,0),
  17945.         DATA_TYPE        = convert(smallint, 3 /*DBTYPE_I4*/),
  17946.         CHARACTER_MAXIMUM_LENGTH= convert(int,null),
  17947.         CHARACTER_OCTET_LENGTH    = convert(int,null),
  17948.         NUMERIC_PRECISION    = convert(smallint,null),
  17949.         NUMERIC_SCALE        = convert(smallint,null),
  17950.         DESCRIPTION        = convert(varchar(1),null)
  17951.     from
  17952.         syscolumns c,
  17953.         sysobjects o,
  17954.         sysusers u
  17955.     where
  17956.         o.type = 'P'                        /* Just Procedures */
  17957.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  17958.     and    (@parameter_name is null or @parameter_name = 'RETURN_VALUE')
  17959.     and    c.id =* o.id
  17960.     and     c.colid = 1
  17961.     and     u.uid = user_id()    /* constrain sysusers uid for use in subquery */
  17962.     and     (    suser_id() = 1     /* User is the System Administrator */
  17963.         or     o.uid = user_id()    /* User created the object */
  17964.             /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  17965.         or     ((select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  17966.              from sysprotects p
  17967.              where p.id = o.id
  17968.                  /*  get rows for public,current user,user's group */
  17969.                  and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  17970.                  /* check for SELECT,EXECUTE privilege */
  17971.              and (action in (193,224)))&1    /* more magic...normalize GRANT */
  17972.             ) = 1     /* final magic...compare Grants    */
  17973.         )
  17974.     order by 2, 3, 5
  17975.     END
  17976.  
  17977.     exec @ret = sp_cursoropen @handle output, 'select * from #spprocparamrowset1',
  17978.         @scrollopt output, @ccopt output, @rows output
  17979.  
  17980.     drop table #spprocparamrowset1
  17981.        return isnull(@ret,0)
  17982. go
  17983.  
  17984.  
  17985. create procedure sp_procedure_params_rowset;4
  17986.     (
  17987.        @handle        int output,
  17988.        @scrollopt    int output,
  17989.     @ccopt        int output,
  17990.     @rows        int output,
  17991.     @cmd        varchar(255)
  17992.     )
  17993. as
  17994.     declare @ret int
  17995.     declare @database_name varchar(30)
  17996.  
  17997.     create table #spprocparamrowset2
  17998.         (
  17999.         PROCEDURE_CATALOG     varchar(30) not null,
  18000.         PROCEDURE_SCHEMA     varchar(30) not null,
  18001.         PROCEDURE_NAME         varchar(35) not null,
  18002.         PARAMETER_NAME         varchar(30) not null,
  18003.         ORDINAL_POSITION     smallint not null,
  18004.         PARAMETER_TYPE         smallint null,
  18005.         PARAMETER_HASDEFAULT    tinyint null,
  18006.         PARAMETER_DEFAULT    varchar(255) null,
  18007.         IS_NULLABLE        bit not null,
  18008.         DATA_TYPE        smallint null,
  18009.         CHARACTER_MAXIMUM_LENGTH int null,
  18010.         CHARACTER_OCTET_LENGTH    int null,
  18011.         NUMERIC_PRECISION    smallint null,
  18012.         NUMERIC_SCALE        smallint null,
  18013.         DESCRIPTION        varchar(1) null
  18014.         )
  18015.  
  18016.     declare sysdatabases_cursor insensitive cursor for 
  18017.         select name from master.dbo.sysdatabases order by name
  18018.   
  18019.     set nocount on
  18020.     open sysdatabases_cursor
  18021.       fetch next from sysdatabases_cursor into @database_name 
  18022.     while @@fetch_status = 0
  18023.         begin
  18024.         exec('use '+@database_name+' insert into #spprocparamrowset2 '+@cmd)
  18025.         fetch next from sysdatabases_cursor into @database_name 
  18026.         end
  18027.       close sysdatabases_cursor
  18028.     deallocate sysdatabases_cursor
  18029.  
  18030.     if @scrollopt is null
  18031.         begin
  18032.         select * from #spprocparamrowset2
  18033.         end
  18034.     else
  18035.         begin
  18036.         exec @ret = sp_cursoropen @handle output,
  18037.             'select * from #spprocparamrowset2',
  18038.                 @scrollopt output, @ccopt output, @rows output
  18039.         end
  18040.     drop table #spprocparamrowset2
  18041.     return isnull(@ret,0)
  18042. go
  18043.  
  18044.  
  18045.  
  18046. if (charindex('7.00', @@version) > 0)
  18047.     drop procedure sp_procedure_params_rowset
  18048. else
  18049. begin
  18050.     print ''
  18051.     print ''
  18052.     print 'Warning:'
  18053.     print 'you are installing the stored procedures '
  18054.     print 'on a pre 7.0 SQL Server.'
  18055.     print 'Ignore the following errors.'
  18056. end
  18057. go
  18058.  
  18059. /*    Procedure for 7.0 servers */
  18060. create procedure sp_procedure_params_rowset
  18061.     (
  18062.        @procedure_name        varchar(255),
  18063.     @group_number        int = 1,
  18064.     @procedure_schema     varchar(255) = null,
  18065.     @parameter_name        varchar(255) = null
  18066.     )
  18067. as
  18068.     select
  18069.         PROCEDURE_CATALOG     = db_name(),
  18070.         PROCEDURE_SCHEMA     = user_name(),
  18071.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  18072.         PARAMETER_NAME         = c.name,
  18073.         ORDINAL_POSITION     = convert(smallint,c.colid),
  18074.         PARAMETER_TYPE         = convert(smallint, 1+ColumnProperty(c.id,c.name,'IsOutParam')),
  18075.         PARAMETER_HASDEFAULT    = convert(tinyint, null),
  18076.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  18077.         IS_NULLABLE        = convert(tinyint,d.is_nullable),     /* Until Sphinx bug 24918 is fixed */
  18078.         DATA_TYPE        = convert(smallint,d.oledb_data_type),    /* Until Sphinx bug 24918 is fixed */
  18079.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  18080.                         case 
  18081.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  18082.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  18083.                         then coalesce(d.column_size,c.length)
  18084.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  18085.                         then coalesce(d.column_size,c.length/2)
  18086.                         else null 
  18087.                         end),
  18088.         CHARACTER_OCTET_LENGTH    = convert(int,
  18089.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  18090.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  18091.                             or d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  18092.                         then coalesce(d.column_size,c.length)
  18093.                         else null 
  18094.                         end),
  18095.         NUMERIC_PRECISION    = convert(smallint,
  18096.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec else null end),
  18097.         NUMERIC_SCALE        = convert(smallint, 
  18098.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  18099.         DESCRIPTION        = convert(varchar(1),null)
  18100.     
  18101.     from
  18102.         sysobjects o,
  18103.         syscolumns c,
  18104.         master.dbo.spt_provider_types d,
  18105.         systypes t
  18106.     where
  18107.         o.name = @procedure_name
  18108.     and    o.type = 'P'                            /* Just Procedures */
  18109.     and    (@procedure_schema = null or @procedure_schema = user_name(o.uid))
  18110.     and    o.id = c.id
  18111.     and    c.number = @group_number
  18112.     and    c.xtype = d.ss_dtype
  18113.     and    c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  18114.     and    coalesce(d.auto_unique_value,0) = 0
  18115.     and    c.usertype = t.usertype
  18116.     and    (@parameter_name is null or @parameter_name = c.name)
  18117.     and     permissions(o.id) <> 0
  18118.     UNION ALL
  18119.     SELECT           /* return value row*/
  18120.         PROCEDURE_CATALOG     = db_name(),
  18121.         PROCEDURE_SCHEMA     = user_name(),
  18122.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  18123.         PARAMETER_NAME         = convert(varchar(30),'RETURN_VALUE'),
  18124.         ORDINAL_POSITION     = convert(smallint,0),
  18125.         PARAMETER_TYPE         = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  18126.         PARAMETER_HASDEFAULT    = convert(tinyint, null),
  18127.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  18128.         IS_NULLABLE        = convert(tinyint,0),        /* Until Sphinx bug 24918 is fixed */
  18129.         DATA_TYPE        = convert(smallint, 3 /*DBTYPE_I4*/),
  18130.         CHARACTER_MAXIMUM_LENGTH= convert(int,null),
  18131.         CHARACTER_OCTET_LENGTH    = convert(int,null),
  18132.         NUMERIC_PRECISION    = convert(smallint,null),
  18133.         NUMERIC_SCALE        = convert(smallint,null),
  18134.         DESCRIPTION        = convert(varchar(1),null)
  18135.     from
  18136.         sysobjects o left join syscolumns c on o.id = c.id and c.colid = 1
  18137.     where
  18138.         o.name = @procedure_name
  18139.     and    o.type = 'P'                        /* Just Procedures */
  18140.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  18141.     and    (@parameter_name is null or @parameter_name = 'RETURN_VALUE')
  18142.     and    permissions(o.id) <> 1
  18143.     order by 2, 3, 5
  18144. go
  18145. create procedure sp_procedure_params_rowset;2
  18146.     (
  18147.     @procedure_schema     varchar(255) = null,
  18148.     @parameter_name        varchar(255) = null
  18149.     )
  18150. as
  18151.     select
  18152.         PROCEDURE_CATALOG     = db_name(),
  18153.         PROCEDURE_SCHEMA     = user_name(),
  18154.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  18155.         PARAMETER_NAME         = c.name,
  18156.         ORDINAL_POSITION     = convert(smallint,c.colid),
  18157.         PARAMETER_TYPE         = convert(smallint, 1+ColumnProperty(c.id,c.name,'IsOutParam')),
  18158.         PARAMETER_HASDEFAULT    = convert(tinyint, null),
  18159.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  18160.         IS_NULLABLE        = convert(tinyint,d.is_nullable),     /* Until Sphinx bug 24918 is fixed */
  18161.         DATA_TYPE        = convert(smallint,d.oledb_data_type),    /* Until Sphinx bug 24918 is fixed */
  18162.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  18163.                         case 
  18164.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  18165.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  18166.                         then coalesce(d.column_size,c.length)
  18167.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  18168.                         then coalesce(d.column_size,c.length/2)
  18169.                         else null 
  18170.                         end),
  18171.         CHARACTER_OCTET_LENGTH    = convert(int,
  18172.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  18173.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  18174.                             or d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  18175.                         then coalesce(d.column_size,c.length)
  18176.                         else null 
  18177.                         end),
  18178.         NUMERIC_PRECISION    = convert(smallint,
  18179.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec else null end),
  18180.         NUMERIC_SCALE        = convert(smallint, 
  18181.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  18182.         DESCRIPTION        = convert(varchar(1),null)
  18183.     
  18184.     from
  18185.         sysobjects o,
  18186.         syscolumns c,
  18187.         master.dbo.spt_provider_types d,
  18188.         systypes t
  18189.     where
  18190.         o.type = 'P'                            /* Just Procedures */
  18191.     and    (@procedure_schema = null or @procedure_schema = user_name(o.uid))
  18192.     and    o.id = c.id
  18193.     and    c.xtype = d.ss_dtype
  18194.     and    c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  18195.     and    coalesce(d.auto_unique_value,0) = 0
  18196.     and    c.usertype = t.usertype
  18197.     and    (@parameter_name is null or @parameter_name = c.name)
  18198.     and     permissions(o.id) <> 0
  18199.     UNION ALL
  18200.     SELECT           /* return value row*/
  18201.         PROCEDURE_CATALOG     = db_name(),
  18202.         PROCEDURE_SCHEMA     = user_name(),
  18203.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  18204.         PARAMETER_NAME         = convert(varchar(30),'RETURN_VALUE'),
  18205.         ORDINAL_POSITION     = convert(smallint,0),
  18206.         PARAMETER_TYPE         = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  18207.         PARAMETER_HASDEFAULT    = convert(tinyint, null),
  18208.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  18209.         IS_NULLABLE        = convert(tinyint,0),        /* Until Sphinx bug 24918 is fixed */
  18210.         DATA_TYPE        = convert(smallint, 3 /*DBTYPE_I4*/),
  18211.         CHARACTER_MAXIMUM_LENGTH= convert(int,null),
  18212.         CHARACTER_OCTET_LENGTH    = convert(int,null),
  18213.         NUMERIC_PRECISION    = convert(smallint,null),
  18214.         NUMERIC_SCALE        = convert(smallint,null),
  18215.         DESCRIPTION        = convert(varchar(1),null)
  18216.     from
  18217.         sysobjects o left join syscolumns c on o.id = c.id and c.colid = 1
  18218.     where
  18219.         o.type = 'P'                        /* Just Procedures */
  18220.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  18221.     and    (@parameter_name is null or @parameter_name = 'RETURN_VALUE')
  18222.     and    permissions(o.id) <> 1
  18223.     order by 2, 3, 5
  18224. go
  18225. create procedure sp_procedure_params_rowset;3
  18226.     (
  18227.        @procedure_name        varchar(255) = null,
  18228.     @group_number        int = null,
  18229.     @procedure_schema     varchar(255) = null,
  18230.     @parameter_name        varchar(255) = null
  18231.     )
  18232. as
  18233. IF @procedure_name is not null 
  18234.     BEGIN
  18235.     select
  18236.         PROCEDURE_CATALOG     = db_name(),
  18237.         PROCEDURE_SCHEMA     = user_name(),
  18238.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  18239.         PARAMETER_NAME         = c.name,
  18240.         ORDINAL_POSITION     = convert(smallint,c.colid),
  18241.         PARAMETER_TYPE         = convert(smallint, 1+ColumnProperty(c.id,c.name,'IsOutParam')),
  18242.         PARAMETER_HASDEFAULT    = convert(tinyint, null),
  18243.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  18244.         IS_NULLABLE        = convert(tinyint,d.is_nullable),     /* Until Sphinx bug 24918 is fixed */
  18245.         DATA_TYPE        = convert(smallint,d.oledb_data_type),    /* Until Sphinx bug 24918 is fixed */
  18246.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  18247.                         case 
  18248.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  18249.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  18250.                         then coalesce(d.column_size,c.length)
  18251.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  18252.                         then coalesce(d.column_size,c.length/2)
  18253.                         else null 
  18254.                         end),
  18255.         CHARACTER_OCTET_LENGTH    = convert(int,
  18256.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  18257.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  18258.                             or d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  18259.                         then coalesce(d.column_size,c.length*2)
  18260.                         else null 
  18261.                         end),
  18262.         NUMERIC_PRECISION    = convert(smallint,
  18263.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec else null end),
  18264.         NUMERIC_SCALE        = convert(smallint, 
  18265.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  18266.         DESCRIPTION        = convert(varchar(1),null)
  18267.     
  18268.     from
  18269.         sysobjects o,
  18270.         syscolumns c,
  18271.         master.dbo.spt_provider_types d,
  18272.         systypes t
  18273.     where
  18274.         o.name = @procedure_name
  18275.     and    o.type = 'P'                            /* Just Procedures */
  18276.     and    (@procedure_schema = null or @procedure_schema = user_name(o.uid))
  18277.     and    o.id = c.id
  18278.     and    c.number = @group_number
  18279.     and    c.xtype = d.ss_dtype
  18280.     and    c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  18281.     and    coalesce(d.auto_unique_value,0) = 0
  18282.     and    c.usertype = t.usertype
  18283.     and    (@parameter_name is null or @parameter_name = c.name)
  18284.     and     permissions(o.id) <> 0
  18285.     UNION ALL
  18286.     SELECT           /* return value row*/
  18287.         PROCEDURE_CATALOG     = db_name(),
  18288.         PROCEDURE_SCHEMA     = user_name(),
  18289.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  18290.         PARAMETER_NAME         = convert(varchar(30),'RETURN_VALUE'),
  18291.         ORDINAL_POSITION     = convert(smallint,0),
  18292.         PARAMETER_TYPE         = convert(smallint,4 /*DBPARAMTYPE_RETURNVALUE*/),
  18293.         PARAMETER_HASDEFAULT    = convert(tinyint, null),
  18294.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  18295.         IS_NULLABLE        = convert(tinyint,0),        /* Until Sphinx bug 24918 is fixed */
  18296.         DATA_TYPE        = convert(smallint,3 /*DBTYPE_I4*/),
  18297.         CHARACTER_MAXIMUM_LENGTH= convert(int,null),
  18298.         CHARACTER_OCTET_LENGTH    = convert(int,null),
  18299.         NUMERIC_PRECISION    = convert(smallint,null),
  18300.         NUMERIC_SCALE        = convert(smallint,null),
  18301.         DESCRIPTION        = convert(varchar(1),null)
  18302.     from
  18303.         sysobjects o left join syscolumns c on o.id = c.id and c.colid = 1
  18304.     where
  18305.         o.name = @procedure_name
  18306.     and    o.type = 'P'                        /* Just Procedures */
  18307.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  18308.     and    (@parameter_name is null or @parameter_name = 'RETURN_VALUE')
  18309.     and    permissions(o.id) <> 1
  18310.     END
  18311. ELSE
  18312.     BEGIN
  18313.     select
  18314.         PROCEDURE_CATALOG     = db_name(),
  18315.         PROCEDURE_SCHEMA     = user_name(),
  18316.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  18317.         PARAMETER_NAME         = c.name,
  18318.         ORDINAL_POSITION     = convert(smallint,c.colid),
  18319.         PARAMETER_TYPE         = convert(smallint, 1+ColumnProperty(c.id,c.name,'IsOutParam')),
  18320.         PARAMETER_HASDEFAULT    = convert(tinyint, null),
  18321.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  18322.         IS_NULLABLE        = convert(tinyint,d.is_nullable),     /* Until Sphinx bug 24918 is fixed */
  18323.         DATA_TYPE        = convert(smallint,d.oledb_data_type),    /* Until Sphinx bug 24918 is fixed */
  18324.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  18325.                         case 
  18326.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  18327.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  18328.                         then coalesce(d.column_size,c.length)
  18329.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  18330.                         then coalesce(d.column_size,c.length/2)
  18331.                         else null 
  18332.                         end),
  18333.         CHARACTER_OCTET_LENGTH    = convert(int,
  18334.                         case 
  18335.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  18336.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  18337.                             or d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  18338.                         then coalesce(d.column_size,c.length)
  18339.                         else null 
  18340.                         end),
  18341.         NUMERIC_PRECISION    = convert(smallint,
  18342.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec else null end),
  18343.         NUMERIC_SCALE        = convert(smallint, 
  18344.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  18345.         DESCRIPTION        = convert(varchar(1),null)
  18346.     
  18347.     from
  18348.         sysobjects o,
  18349.         syscolumns c,
  18350.         master.dbo.spt_provider_types d,
  18351.         systypes t
  18352.     where
  18353.         o.type = 'P'                            /* Just Procedures */
  18354.     and    (@procedure_schema = null or @procedure_schema = user_name(o.uid))
  18355.     and    o.id = c.id
  18356.     and    c.xtype = d.ss_dtype
  18357.     and    c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  18358.     and    coalesce(d.auto_unique_value,0) = 0
  18359.     and    c.usertype = t.usertype
  18360.     and    (@parameter_name is null or @parameter_name = c.name)
  18361.     and     permissions(o.id) <> 0
  18362.     UNION ALL
  18363.     SELECT           /* return value row*/
  18364.         PROCEDURE_CATALOG     = db_name(),
  18365.         PROCEDURE_SCHEMA     = user_name(),
  18366.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  18367.         PARAMETER_NAME         = convert(varchar(30),'RETURN_VALUE'),
  18368.         ORDINAL_POSITION     = convert(smallint,0),
  18369.         PARAMETER_TYPE         = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  18370.         PARAMETER_HASDEFAULT    = convert(tinyint, null),
  18371.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  18372.         IS_NULLABLE        = convert(tinyint,0),        /* Until Sphinx bug 24918 is fixed */
  18373.         DATA_TYPE        = convert(smallint, 3 /*DBTYPE_I4*/),
  18374.         CHARACTER_MAXIMUM_LENGTH= convert(int,null),
  18375.         CHARACTER_OCTET_LENGTH    = convert(int,null),
  18376.         NUMERIC_PRECISION    = convert(smallint,null),
  18377.         NUMERIC_SCALE        = convert(smallint,null),
  18378.         DESCRIPTION        = convert(varchar(1),null)
  18379.     from
  18380.         sysobjects o left join syscolumns c on o.id = c.id and c.colid = 1
  18381.     where
  18382.         o.type = 'P'                        /* Just Procedures */
  18383.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  18384.     and    (@parameter_name is null or @parameter_name = 'RETURN_VALUE')
  18385.     and    permissions(o.id) <> 1
  18386.     END
  18387. go
  18388.  
  18389.  
  18390.  
  18391. create procedure sp_procedure_params_rowset;4
  18392.     (
  18393.        @handle        int output,
  18394.        @scrollopt    int output,
  18395.     @ccopt        int output,
  18396.     @rows        int output,
  18397.     @cmd        varchar(255)
  18398.     )
  18399. as
  18400.     declare @ret int
  18401.     declare @database_name varchar(30)
  18402.  
  18403.     create table #spsprocparamrowset
  18404.         (
  18405.         PROCEDURE_CATALOG     varchar(30) not null,
  18406.         PROCEDURE_SCHEMA     varchar(30) not null,
  18407.         PROCEDURE_NAME         varchar(35) not null,
  18408.         PARAMETER_NAME         varchar(30) not null,
  18409.         ORDINAL_POSITION     smallint not null,
  18410.         PARAMETER_TYPE         smallint null,
  18411.         PARAMETER_HASDEFAULT    tinyint null,
  18412.         PARAMETER_DEFAULT    varchar(255) null,
  18413.         IS_NULLABLE        bit not null,
  18414.         DATA_TYPE        smallint null,
  18415.         CHARACTER_MAXIMUM_LENGTH int null,
  18416.         CHARACTER_OCTET_LENGTH    int null,
  18417.         NUMERIC_PRECISION    smallint null,
  18418.         NUMERIC_SCALE        smallint null,
  18419.         DESCRIPTION        varchar(1) null
  18420.         )
  18421.  
  18422.     declare sysdatabases_cursor insensitive cursor for 
  18423.         select name from master.dbo.sysdatabases
  18424.   
  18425.     set nocount on
  18426.     open sysdatabases_cursor
  18427.       fetch next from sysdatabases_cursor into @database_name 
  18428.     while @@fetch_status = 0
  18429.         begin
  18430.         exec('use '+@database_name+' insert into #spsprocparamrowset '+@cmd)
  18431.         fetch next from sysdatabases_cursor into @database_name 
  18432.         end
  18433.       close sysdatabases_cursor
  18434.     deallocate sysdatabases_cursor
  18435.  
  18436.     if @scrollopt is null
  18437.         begin
  18438.         select * from #spsprocparamrowset order by 1,2,3
  18439.         end
  18440.     else
  18441.         begin
  18442.         exec @ret = sp_cursoropen @handle output,
  18443.             'select * from #spsprocparamrowset order by 1,2,3,5',
  18444.                 @scrollopt output, @ccopt output, @rows output
  18445.         end
  18446.     drop table #spsprocparamrowset
  18447.     return isnull(@ret,0)
  18448. go
  18449.  
  18450.  
  18451. grant execute on sp_procedure_params_rowset to public
  18452. go
  18453.  
  18454. dump tran master with no_log
  18455. go
  18456.  
  18457. print 'creating sp_procedures_rowset'
  18458. go
  18459.  
  18460.  
  18461. /* pre 7.00 version */
  18462. create procedure sp_procedures_rowset
  18463.     (
  18464.     @procedure_name        varchar(255), 
  18465.     @group_number        int = 1,
  18466.     @procedure_schema    varchar(255) = null
  18467.     )        
  18468. as
  18469.     select
  18470.         PROCEDURE_CATALOG    = db_name(),
  18471.         PROCEDURE_SCHEMA    = user_name(o.uid),
  18472.         PROCEDURE_NAME        = convert(varchar(35),o.name +';'+ ltrim(str(p.number,5))),
  18473.         PROCEDURE_TYPE        = convert(smallint, 3 /*DB_PT_FUNCTION*/),
  18474.         PROCEDURE_DEFINITION    = convert(varchar(1),null),
  18475.         DESCRIPTION        = convert(varchar(1),null)
  18476.     from     
  18477.         sysobjects o, 
  18478.         syscomments p,
  18479.         sysusers u
  18480.     where
  18481.             o.name = @procedure_name
  18482.         and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  18483.     and     o.type = 'P'        /* Object type of Procedure */
  18484.     and     p.colid = 1
  18485.     and     p.id = o.id
  18486.     and    p.number = @group_number
  18487.     and     u.uid = user_id()    /* constrain sysusers uid for use in subquery */
  18488.     and     (    suser_id() = 1     /* User is the System Administrator */
  18489.         or     o.uid = user_id()    /* User created the object */
  18490.             /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  18491.         or     ((select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  18492.              from sysprotects p
  18493.              where p.id = o.id
  18494.                  /*  get rows for public,current user,user's group */
  18495.                  and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  18496.                  /* check for SELECT,EXECUTE privilege */
  18497.              and (action in (193,224)))&1    /* more magic...normalize GRANT */
  18498.             ) = 1     /* final magic...compare Grants    */
  18499.         )
  18500.     order by 2, 3
  18501. go
  18502. create procedure sp_procedures_rowset;2
  18503.     (
  18504.     @procedure_schema    varchar(255) = null
  18505.     )        
  18506. as
  18507.     select
  18508.         PROCEDURE_CATALOG    = db_name(),
  18509.         PROCEDURE_SCHEMA    = user_name(o.uid),
  18510.         PROCEDURE_NAME        = convert(varchar(35),o.name +';'+ ltrim(str(p.number,5))),
  18511.         PROCEDURE_TYPE        = convert(smallint, 3 /*DB_PT_FUNCTION*/),
  18512.         PROCEDURE_DEFINITION    = convert(varchar(1),null),
  18513.         DESCRIPTION        = convert(varchar(1),null)
  18514.     from     
  18515.         sysobjects o, 
  18516.         syscomments p,
  18517.         sysusers u
  18518.     where
  18519.             (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  18520.     and     o.type = 'P'        /* Object type of Procedure */
  18521.     and     p.colid = 1
  18522.     and     p.id = o.id
  18523.     and     u.uid = user_id()    /* constrain sysusers uid for use in subquery */
  18524.     and     (    suser_id() = 1     /* User is the System Administrator */
  18525.         or     o.uid = user_id()    /* User created the object */
  18526.             /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  18527.         or     ((select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  18528.              from sysprotects p
  18529.              where p.id = o.id
  18530.                  /*  get rows for public,current user,user's group */
  18531.                  and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  18532.                  /* check for SELECT,EXECUTE privilege */
  18533.              and (action in (193,224)))&1    /* more magic...normalize GRANT */
  18534.             ) = 1     /* final magic...compare Grants    */
  18535.         )
  18536.     order by 2, 3
  18537. go
  18538. create procedure sp_procedures_rowset;3
  18539.     (
  18540.     @procedure_name        varchar(255) = null, 
  18541.     @group_number        int = null,
  18542.     @procedure_schema    varchar(255) = null
  18543.     )        
  18544. as
  18545. IF @procedure_name is not null
  18546.     BEGIN
  18547.     select
  18548.         PROCEDURE_CATALOG    = db_name(),
  18549.         PROCEDURE_SCHEMA    = user_name(o.uid),
  18550.         PROCEDURE_NAME        = convert(varchar(35),o.name +';'+ ltrim(str(p.number,5))),
  18551.         PROCEDURE_TYPE        = convert(smallint, 3 /*DB_PT_FUNCTION*/),
  18552.         PROCEDURE_DEFINITION    = convert(varchar(1),null),
  18553.         DESCRIPTION        = convert(varchar(1),null)
  18554.     from     
  18555.         sysobjects o, 
  18556.         syscomments p,
  18557.         sysusers u
  18558.     where
  18559.             o.name = @procedure_name
  18560.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  18561.     and     o.type = 'P'        /* Object type of Procedure */
  18562.     and     p.colid = 1
  18563.     and     p.id = o.id
  18564.     and    p.number = @group_number
  18565.     and     u.uid = user_id()    /* constrain sysusers uid for use in subquery */
  18566.     and     (    suser_id() = 1     /* User is the System Administrator */
  18567.         or     o.uid = user_id()    /* User created the object */
  18568.             /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  18569.         or     ((select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  18570.              from sysprotects p
  18571.              where p.id = o.id
  18572.                  /*  get rows for public,current user,user's group */
  18573.                  and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  18574.                  /* check for SELECT,EXECUTE privilege */
  18575.              and (action in (193,224)))&1    /* more magic...normalize GRANT */
  18576.             ) = 1     /* final magic...compare Grants    */
  18577.         )    
  18578.     END
  18579. ELSE
  18580.     BEGIN
  18581.     select
  18582.         PROCEDURE_CATALOG    = db_name(),
  18583.         PROCEDURE_SCHEMA    = user_name(o.uid),
  18584.         PROCEDURE_NAME        = convert(varchar(35),o.name +';'+ ltrim(str(p.number,5))),
  18585.         PROCEDURE_TYPE        = convert(smallint, 3 /*DB_PT_FUNCTION*/),
  18586.         PROCEDURE_DEFINITION    = convert(varchar(1),null),
  18587.         DESCRIPTION        = convert(varchar(1),null)
  18588.     from     
  18589.         sysobjects o, 
  18590.         syscomments p,
  18591.         sysusers u
  18592.     where
  18593.             (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  18594.     and     o.type = 'P'        /* Object type of Procedure */
  18595.     and     p.colid = 1
  18596.     and     p.id = o.id
  18597.     and     u.uid = user_id()    /* constrain sysusers uid for use in subquery */
  18598.     and     (    suser_id() = 1     /* User is the System Administrator */
  18599.         or     o.uid = user_id()    /* User created the object */
  18600.             /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  18601.         or     ((select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  18602.              from sysprotects p
  18603.              where p.id = o.id
  18604.                  /*  get rows for public,current user,user's group */
  18605.                  and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  18606.                  /* check for SELECT,EXECUTE privilege */
  18607.              and (action in (193,224)))&1    /* more magic...normalize GRANT */
  18608.             ) = 1     /* final magic...compare Grants    */
  18609.         )    
  18610.     END
  18611. go
  18612.  
  18613.  
  18614. if (charindex('7.00', @@version) > 0)
  18615.     drop procedure sp_procedures_rowset
  18616. else
  18617. begin
  18618.     print ''
  18619.     print ''
  18620.     print 'Warning:'
  18621.     print 'you are installing the stored procedures '
  18622.     print 'on a pre 7.0 SQL Server.'
  18623.     print 'Ignore the following errors.'
  18624. end
  18625. go
  18626.  
  18627. /* 7.00 version */
  18628. create procedure sp_procedures_rowset
  18629.     (
  18630.     @procedure_name        varchar(255), 
  18631.     @group_number        int = 1,
  18632.     @procedure_schema    varchar(255) = null
  18633.     )        
  18634. as
  18635.     select
  18636.         PROCEDURE_CATALOG    = db_name(),
  18637.         PROCEDURE_SCHEMA    = user_name(o.uid),
  18638.         PROCEDURE_NAME        = convert(varchar(35),o.name +';'+ ltrim(str(p.number,5))),
  18639.         PROCEDURE_TYPE        = convert(smallint, 3 /*DB_PT_FUNCTION*/),
  18640.         PROCEDURE_DEFINITION    = convert(varchar(1),null),
  18641.         DESCRIPTION        = convert(varchar(1),null)
  18642.     from     
  18643.         sysobjects o, 
  18644.         syscomments p
  18645.     where
  18646.         permissions(o.id) <> 0
  18647.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  18648.     and     o.type = 'P'        /* Object type of Procedure */
  18649.     and     p.colid = 1
  18650.     and     p.id = o.id
  18651.     and    p.number = @group_number
  18652.     order by 2, 3
  18653. go
  18654. create procedure sp_procedures_rowset;2
  18655.     (
  18656.     @procedure_schema    varchar(255) = null
  18657.     )        
  18658. as
  18659.     select
  18660.         PROCEDURE_CATALOG    = db_name(),
  18661.         PROCEDURE_SCHEMA    = user_name(o.uid),
  18662.         PROCEDURE_NAME        = convert(varchar(35),o.name +';'+ ltrim(str(p.number,5))),
  18663.         PROCEDURE_TYPE        = convert(smallint, 3 /*DB_PT_FUNCTION*/),
  18664.         PROCEDURE_DEFINITION    = convert(varchar(1),null),
  18665.         DESCRIPTION        = convert(varchar(1),null)
  18666.     from     sysobjects o, syscomments p
  18667.     where
  18668.         permissions(o.id) <> 0
  18669.     and        (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  18670.     and     o.type = 'P'        /* Object type of Procedure */
  18671.     and p.colid = 1
  18672.     and p.id = o.id
  18673.     order by 2, 3
  18674. go
  18675. create procedure sp_procedures_rowset;3
  18676.     (
  18677.     @procedure_name        varchar(255) = null, 
  18678.     @group_number        int = null,
  18679.     @procedure_schema    varchar(255) = null
  18680.     )        
  18681. as
  18682. IF @procedure_name is not null
  18683.     BEGIN
  18684.     select
  18685.         PROCEDURE_CATALOG    = db_name(),
  18686.         PROCEDURE_SCHEMA    = user_name(o.uid),
  18687.         PROCEDURE_NAME        = convert(varchar(35),o.name +';'+ ltrim(str(p.number,5))),
  18688.         PROCEDURE_TYPE        = convert(smallint, 3 /*DB_PT_FUNCTION*/),
  18689.         PROCEDURE_DEFINITION    = convert(varchar(1),null),
  18690.         DESCRIPTION        = convert(varchar(1),null)
  18691.     from     
  18692.         sysobjects o, 
  18693.         syscomments p
  18694.     where
  18695.         permissions(o.id) <> 0
  18696.     and    o.name = @procedure_name
  18697.      and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  18698.     and     o.type = 'P'        /* Object type of Procedure */
  18699.     and     p.colid = 1
  18700.     and     p.id = o.id
  18701.     and    p.number = @group_number
  18702.     END
  18703. ELSE
  18704.     BEGIN
  18705.     select
  18706.         PROCEDURE_CATALOG    = db_name(),
  18707.         PROCEDURE_SCHEMA    = user_name(o.uid),
  18708.         PROCEDURE_NAME        = convert(varchar(35),o.name +';'+ ltrim(str(p.number,5))),
  18709.         PROCEDURE_TYPE        = convert(smallint, 3 /*DB_PT_FUNCTION*/),
  18710.         PROCEDURE_DEFINITION    = convert(varchar(1),null),
  18711.         DESCRIPTION        = convert(varchar(1),null)
  18712.     from     
  18713.         sysobjects o, 
  18714.         syscomments p
  18715.     where
  18716.         permissions(o.id) <> 0
  18717.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  18718.     and    o.type = 'P'        /* Object type of Procedure */
  18719.     and     p.colid = 1
  18720.         and     p.id = o.id
  18721.     END
  18722. go
  18723.  
  18724.  
  18725. create procedure sp_procedures_rowset;4
  18726.     (
  18727.  
  18728.        @handle        int output,
  18729.        @scrollopt    int output,
  18730.     @ccopt        int output,
  18731.     @rows        int output,
  18732.     @cmd        varchar(255)
  18733.     )
  18734. as
  18735.     declare @ret int
  18736.     declare @database_name varchar(30)
  18737.  
  18738.     create table #spsprocrowset
  18739.         (
  18740.         PROCEDURE_CATALOG        varchar(30) not null,
  18741.         PROCEDURE_SCHEMA        varchar(30) not null,
  18742.         PROCEDURE_NAME            varchar(35) not null,
  18743.         PROCEDURE_TYPE            smallint not null,
  18744.         PROCEDURE_DEFINITION        varchar(1) null,
  18745.         DESCRIPTION            varchar(1) null
  18746.         )
  18747.  
  18748.     declare sysdatabases_cursor insensitive cursor for 
  18749.         select name from master.dbo.sysdatabases
  18750.   
  18751.     set nocount on
  18752.     open sysdatabases_cursor
  18753.       fetch next from sysdatabases_cursor into @database_name 
  18754.     while @@fetch_status = 0
  18755.         begin
  18756.         exec('use '+@database_name+' insert into #spsprocrowset '+@cmd)
  18757.         fetch next from sysdatabases_cursor into @database_name 
  18758.         end
  18759.       close sysdatabases_cursor
  18760.     deallocate sysdatabases_cursor
  18761.  
  18762.     if @scrollopt is null
  18763.         begin
  18764.         select * from #spsprocrowset order by 1,2,3
  18765.         end
  18766.     else
  18767.         begin
  18768.         exec @ret = sp_cursoropen @handle output,
  18769.             'select * from #spsprocrowset order by 1,2,3',
  18770.                 @scrollopt output, @ccopt output, @rows output
  18771.         end
  18772.     drop table #spsprocrowset
  18773.     return isnull(@ret,0)
  18774. go
  18775.  
  18776. grant execute on sp_procedures_rowset to public
  18777. go
  18778.  
  18779. dump tran master with no_log
  18780. go
  18781.  
  18782. print 'creating sp_schemata_rowset'
  18783. go
  18784.  
  18785. create procedure sp_schemata_rowset
  18786.     (
  18787.     @schema_name    varchar(90) = null, 
  18788.     @schema_owner    varchar(90) = null
  18789.     )        
  18790. as
  18791.     select    distinct
  18792.         CATALOG_NAME            = db_name(),
  18793.         SCHEMA_NAME            = user_name(o.uid),    
  18794.         SCHEMA_OWNER            = user_name(o.uid),    
  18795.         DEFAULT_CHARACTER_SET_CATALOG    = convert(varchar(30),'master'),
  18796.         DEFAULT_CHARACTER_SET_SCHEMA    = convert(varchar(30),'dbo'),
  18797.         DEFAULT_CHARACTER_SET_NAME    = convert(varchar(30),a_cha.name)
  18798.     from    
  18799.         sysobjects o,
  18800.         master.dbo.sysconfigures    cfg,
  18801.         master.dbo.syscharsets        a_cha, /* charset/1001, not sortorder */
  18802.         master.dbo.syscharsets        b_cha  /* sortorder/2001, not charset */
  18803.     where   
  18804.         (@schema_name is null or @schema_name = user_name(o.uid))
  18805.     and     (@schema_owner is null or @schema_owner = user_name(o.uid))
  18806.     and    a_cha.type = 1001 /* type is charset */
  18807.     and     b_cha.type = 2001 /* type is sortorder */
  18808.     and     a_cha.id = b_cha.csid
  18809.     and     b_cha.id = cfg.value
  18810.     order by 2
  18811. go
  18812. create procedure sp_schemata_rowset;2
  18813.     (
  18814.  
  18815.        @handle        int output,
  18816.        @scrollopt    int output,
  18817.     @ccopt        int output,
  18818.     @rows        int output,
  18819.     @cmd        varchar(255)
  18820.     )
  18821. as
  18822.     declare @ret int
  18823.     declare @database_name varchar(30)
  18824.  
  18825.     create table #spschematarowset
  18826.         (
  18827.         CATALOG_NAME            varchar(30) not null,
  18828.         SCHEMA_NAME            varchar(30) not null,
  18829.         SCHEMA_OWNER            varchar(30) not null,
  18830.         DEFAULT_CHARACTER_SET_CATALOG    varchar(30) null,
  18831.         DEFAULT_CHARACTER_SET_SCHEMA    varchar(30) null,
  18832.         DEFAULT_CHARACTER_SET_NAME    varchar(30) null
  18833.         )
  18834.  
  18835.     declare sysdatabases_cursor insensitive cursor for 
  18836.         select name from master.dbo.sysdatabases
  18837.   
  18838.     set nocount on
  18839.     open sysdatabases_cursor
  18840.       fetch next from sysdatabases_cursor into @database_name 
  18841.     while @@fetch_status = 0
  18842.         begin
  18843.         exec('use '+@database_name+' insert into #spschematarowset '+@cmd)
  18844.         fetch next from sysdatabases_cursor into @database_name 
  18845.         end
  18846.       close sysdatabases_cursor
  18847.     deallocate sysdatabases_cursor
  18848.  
  18849.     if @scrollopt is null
  18850.         begin
  18851.         select * from #spschematarowset order by 1,2,3
  18852.         end
  18853.     else
  18854.         begin
  18855.         exec @ret = sp_cursoropen @handle output,
  18856.             'select * from #spschematarowset order by 1,2,3',
  18857.                 @scrollopt output, @ccopt output, @rows output
  18858.         end
  18859.     drop table #spschematarowset 
  18860.     return isnull(@ret,0)
  18861. go
  18862.  
  18863. grant execute on sp_schemata_rowset to public
  18864. go
  18865.  
  18866. dump tran master with no_log
  18867. go
  18868.  
  18869. print 'creating sp_statistics_rowset'
  18870. go
  18871.  
  18872.  
  18873. /*    6.0 and 6.5 version */
  18874. create procedure sp_statistics_rowset
  18875.     (
  18876.     @table_name    varchar(255),
  18877.     @table_schema    varchar(255) = null     
  18878.     )
  18879. as
  18880.     select    db_name()                as TABLE_CATALOG,        
  18881.         user_name(o.uid)            as TABLE_SCHEMA,
  18882.         o.name                    as TABLE_NAME,
  18883.         x.rows                    as CARDINALITY
  18884.     from    sysobjects o, sysindexes x, sysusers u
  18885.     where    o.type in ('U')
  18886.     and     o.name = @table_name
  18887.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  18888.     and     x.id = o.id
  18889.     and     x.indid in (0,1)    /*If there are no indexes then table stats are in a row with indid =0 */
  18890.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  18891.     and     (
  18892.         suser_id() = 1     /* User is the System Administrator */
  18893.         or o.uid = user_id()     /* User created the object */
  18894.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  18895.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  18896.             from sysprotects p
  18897.             where p.id = o.id
  18898.             /* get rows for public,current user,user's group */
  18899.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  18900.             /* check for SELECT,EXECUTE privilege */
  18901.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  18902.             ) = 1    /* final magic...compare Grants      */
  18903.            )
  18904.     order by 2, 3
  18905. go
  18906. create procedure sp_statistics_rowset;2
  18907.     (
  18908.     @table_schema    varchar(255) = null     
  18909.     )
  18910. as
  18911.     select    db_name()                as TABLE_CATALOG,        
  18912.         user_name(o.uid)            as TABLE_SCHEMA,
  18913.         o.name                    as TABLE_NAME,
  18914.         x.rows                    as CARDINALITY
  18915.     from    sysobjects o, sysindexes x, sysusers u
  18916.     where    o.type in ('U')
  18917.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  18918.     and     x.id = o.id
  18919.     and     x.indid in (0,1)    /*If there are no indexes then table stats are in a row with indid =0 */
  18920.     and     (
  18921.         suser_id() = 1         /* User is the System Administrator */
  18922.         or o.uid = user_id()    /* User created the object */
  18923.         )        
  18924.     order by 2, 3
  18925. go
  18926. create procedure sp_statistics_rowset;3
  18927.     (
  18928.     @table_name    varchar(255) = null,
  18929.     @table_schema    varchar(255) = null     
  18930.     )
  18931. as
  18932. IF @table_name is not null
  18933.     BEGIN
  18934.     select    db_name()                as TABLE_CATALOG,        
  18935.         user_name(o.uid)            as TABLE_SCHEMA,
  18936.         o.name                    as TABLE_NAME,
  18937.         x.rows                    as CARDINALITY
  18938.     from    sysobjects o, sysindexes x, sysusers u
  18939.     where    o.type in ('U')
  18940.     and     o.name = @table_name
  18941.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  18942.     and     x.id = o.id
  18943.     and     x.indid in (0,1)    /*If there are no indexes then table stats are in a row with indid =0 */
  18944.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  18945.     and     (
  18946.         suser_id() = 1     /* User is the System Administrator */
  18947.         or o.uid = user_id()     /* User created the object */
  18948.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  18949.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  18950.             from sysprotects p
  18951.             where p.id = o.id
  18952.             /* get rows for public,current user,user's group */
  18953.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  18954.             /* check for SELECT,EXECUTE privilege */
  18955.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  18956.             ) = 1    /* final magic...compare Grants      */
  18957.            )
  18958.     END
  18959. ELSE
  18960.     BEGIN
  18961.     select    db_name()                as TABLE_CATALOG,        
  18962.         user_name(o.uid)            as TABLE_SCHEMA,
  18963.         o.name                    as TABLE_NAME,
  18964.         x.rows                    as CARDINALITY
  18965.     from    sysobjects o, sysindexes x, sysusers u
  18966.     where    o.type in ('U')
  18967.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  18968.     and     x.id = o.id
  18969.     and     x.indid in (0,1)    /*If there are no indexes then table stats are in a row with indid =0 */
  18970.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  18971.     and     (
  18972.         suser_id() = 1     /* User is the System Administrator */
  18973.         or o.uid = user_id()     /* User created the object */
  18974.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  18975.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  18976.             from sysprotects p
  18977.             where p.id = o.id
  18978.             /* get rows for public,current user,user's group */
  18979.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  18980.             /* check for SELECT,EXECUTE privilege */
  18981.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  18982.             ) = 1    /* final magic...compare Grants      */
  18983.            )
  18984.     END
  18985. go
  18986.  
  18987. if (charindex('7.00', @@version) > 0)
  18988.     drop procedure sp_statistics_rowset
  18989. else
  18990. begin
  18991.     print ''
  18992.     print ''
  18993.     print 'Warning:'
  18994.     print 'you are installing the stored procedures '
  18995.     print 'on a pre 7.0 SQL Server.'
  18996.     print 'Ignore the following errors.'
  18997. end
  18998. go
  18999.  
  19000. /*     7.0 version */
  19001. create procedure sp_statistics_rowset
  19002.     (
  19003.     @table_name    varchar(255),
  19004.     @table_schema    varchar(255) = null     
  19005.     )
  19006. as
  19007.     select    db_name()                as TABLE_CATALOG,        
  19008.         user_name(o.uid)            as TABLE_SCHEMA,
  19009.         o.name                    as TABLE_NAME,
  19010.         x.rows                    as CARDINALITY
  19011.     from    sysobjects o, sysindexes x
  19012.     where    o.type in ('U')
  19013.     and     o.name = @table_name
  19014.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  19015.     and     x.id = o.id
  19016.     and     x.indid in (0,1)    /*If there are no indexes then table stats are in a row with indid =0 */
  19017.     and     permissions(o.id) <> 0
  19018.     order by 2, 3
  19019. go
  19020. create procedure sp_statistics_rowset;2
  19021.     (
  19022.     @table_schema    varchar(255) = null     
  19023.     )
  19024. as
  19025.     select    db_name()                as TABLE_CATALOG,        
  19026.         user_name(o.uid)            as TABLE_SCHEMA,
  19027.         o.name                    as TABLE_NAME,
  19028.         x.rows                    as CARDINALITY
  19029.     from    sysobjects o, sysindexes x
  19030.     where    o.type in ('U')
  19031.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  19032.     and     x.id = o.id
  19033.     and     x.indid in (0,1)    /*If there are no indexes then table stats are in a row with indid =0 */
  19034.     and     permissions(o.id) <> 0
  19035.     order by 2, 3
  19036. go
  19037. create procedure sp_statistics_rowset;3
  19038.     (
  19039.     @table_name    varchar(255) = null,
  19040.     @table_schema    varchar(255) = null     
  19041.     )
  19042. as
  19043. IF @table_name is not null
  19044.     BEGIN
  19045.     select    db_name()                as TABLE_CATALOG,        
  19046.         user_name(o.uid)            as TABLE_SCHEMA,
  19047.         o.name                    as TABLE_NAME,
  19048.         x.rows                    as CARDINALITY
  19049.     from    sysobjects o, sysindexes x
  19050.     where    o.type in ('U')
  19051.     and     o.name = @table_name
  19052.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  19053.     and     x.id = o.id
  19054.     and     x.indid in (0,1)    /*If there are no indexes then table stats are in a row with indid =0 */
  19055.     and     permissions(o.id) <> 0
  19056.     END
  19057. ELSE
  19058.     BEGIN
  19059.     select    db_name()                as TABLE_CATALOG,        
  19060.         user_name(o.uid)            as TABLE_SCHEMA,
  19061.         o.name                    as TABLE_NAME,
  19062.         x.rows                    as CARDINALITY
  19063.     from    sysobjects o, sysindexes x
  19064.     where    o.type in ('U')
  19065.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  19066.     and     x.id = o.id
  19067.     and     x.indid in (0,1)    /*If there are no indexes then table stats are in a row with indid =0 */
  19068.     and     permissions(o.id) <> 0
  19069.     END
  19070. go
  19071.  
  19072. create procedure sp_statistics_rowset;4
  19073.     (
  19074.  
  19075.        @handle        int output,
  19076.        @scrollopt    int output,
  19077.     @ccopt        int output,
  19078.     @rows        int output,
  19079.     @cmd        varchar(255)
  19080.     )
  19081. as
  19082.     declare @ret int
  19083.     declare @database_name varchar(30)
  19084.  
  19085.     create table #spstatisticsrowset
  19086.         (
  19087.         TABLE_CATALOG            varchar(30) not null,
  19088.         TABLE_SCHEMA            varchar(30) not null,
  19089.         TABLE_NAME            varchar(30) not null,
  19090.         CARDINALITY            int not null
  19091.         )
  19092.  
  19093.     declare sysdatabases_cursor insensitive cursor for 
  19094.         select name from master.dbo.sysdatabases
  19095.   
  19096.     set nocount on
  19097.     open sysdatabases_cursor
  19098.       fetch next from sysdatabases_cursor into @database_name 
  19099.     while @@fetch_status = 0
  19100.         begin
  19101.         exec('use '+@database_name+' insert into #spstatisticsrowset '+@cmd)
  19102.         fetch next from sysdatabases_cursor into @database_name 
  19103.         end
  19104.       close sysdatabases_cursor
  19105.     deallocate sysdatabases_cursor
  19106.  
  19107.     if @scrollopt is null
  19108.         begin
  19109.         select * from #spstatisticsrowset order by 1,2,3
  19110.         end
  19111.     else
  19112.         begin
  19113.         exec @ret = sp_cursoropen @handle output,
  19114.             'select * from #spstatisticsrowset order by 1,2,3',
  19115.                 @scrollopt output, @ccopt output, @rows output
  19116.         end
  19117.     drop table #spstatisticsrowset 
  19118.     return isnull(@ret,0)
  19119. go
  19120.  
  19121. grant execute on sp_statistics_rowset to public
  19122. go
  19123.  
  19124. dump tran master with no_log
  19125. go
  19126.  
  19127. print 'creating sp_tables_rowset'
  19128. go
  19129.  
  19130.  
  19131. /*    Procedure for 6.50 and earlier servers */
  19132. create procedure sp_tables_rowset
  19133.     (
  19134.     @table_name    varchar(255), 
  19135.     @table_schema    varchar(255) = null,    
  19136.     @table_type    varchar(255) = null 
  19137.     )
  19138. as
  19139.     select    TABLE_CATALOG    = db_name(),
  19140.         TABLE_SCHEMA    = user_name(o.uid),
  19141.         TABLE_NAME    = o.name,
  19142.         TABLE_TYPE    = convert(varchar(30),
  19143.                     case o.type 
  19144.                     when 'U' then 'TABLE'
  19145.                     when 'V' then 'VIEW'
  19146.                     when 'S' then 'SYSTEM TABLE'
  19147.                     end),
  19148.         TABLE_GUID    = convert(binary(16), null),
  19149.         DESCRIPTION    = convert(varchar(1), null)
  19150.     from    sysusers u, sysobjects o
  19151.     where    o.type in ('U','V','S')
  19152.     and     o.name = @table_name
  19153.     and     (    @table_schema is null
  19154.         or    @table_schema = user_name(o.uid)
  19155.         )
  19156.     and     (
  19157.             @table_type is null
  19158.         or    @table_type = case o.type 
  19159.                     when 'U' then 'TABLE'
  19160.                     when 'V' then 'VIEW'
  19161.                     when 'S' then 'SYSTEM TABLE'
  19162.                     end
  19163.         )
  19164.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  19165.     and     (
  19166.         suser_id() = 1     /* User is the System Administrator */
  19167.         or o.uid = user_id()     /* User created the object */
  19168.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  19169.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  19170.             from sysprotects p
  19171.             /* join to correlate with all rows in sysobjects */
  19172.             where p.id = o.id
  19173.             /* get rows for public,current user,user's group */
  19174.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  19175.             /* check for SELECT,EXECUTE privilege */
  19176.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  19177.             ) = 1    /* final magic...compare Grants      */
  19178.         )        
  19179.     order by 4, 2, 3
  19180. go
  19181. create procedure sp_tables_rowset;2
  19182.     (
  19183.     @table_schema    varchar(255) = null,    
  19184.     @table_type    varchar(255) = null 
  19185.     )
  19186. as
  19187.     select    TABLE_CATALOG    = db_name(),
  19188.         TABLE_SCHEMA    = user_name(o.uid),
  19189.         TABLE_NAME    = o.name,
  19190.         TABLE_TYPE    = convert(varchar(30),
  19191.                     case o.type 
  19192.                     when 'U' then 'TABLE'
  19193.                     when 'V' then 'VIEW'
  19194.                     when 'S' then 'SYSTEM TABLE'
  19195.                     end),
  19196.         TABLE_GUID    = convert(binary(16), null),
  19197.         DESCRIPTION    = convert(varchar(1), null)
  19198.     from    sysusers u, sysobjects o
  19199.     where    o.type in ('U','V','S')
  19200.     and     (    @table_schema is null
  19201.         or    @table_schema = user_name(o.uid)
  19202.         )
  19203.     and     (
  19204.             @table_type is null
  19205.         or    @table_type = case o.type 
  19206.                     when 'U' then 'TABLE'
  19207.                     when 'V' then 'VIEW'
  19208.                     when 'S' then 'SYSTEM TABLE'
  19209.                     end
  19210.         )
  19211.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  19212.     and     (
  19213.         suser_id() = 1     /* User is the System Administrator */
  19214.         or o.uid = user_id()     /* User created the object */
  19215.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  19216.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  19217.             from sysprotects p
  19218.             /* join to correlate with all rows in sysobjects */
  19219.             where p.id = o.id
  19220.             /* get rows for public,current user,user's group */
  19221.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  19222.             /* check for SELECT,EXECUTE privilege */
  19223.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  19224.             ) = 1    /* final magic...compare Grants      */
  19225.         )        
  19226.     order by 4, 2, 3
  19227. go
  19228. create procedure sp_tables_rowset;3
  19229.     (
  19230.     @table_name    varchar(255) = null, 
  19231.     @table_schema    varchar(255) = null,    
  19232.     @table_type    varchar(255) = null 
  19233.     )
  19234. as
  19235. IF @table_name is not null
  19236.     BEGIN
  19237.     select    TABLE_CATALOG    = db_name(),
  19238.         TABLE_SCHEMA    = user_name(o.uid),
  19239.         TABLE_NAME    = o.name,
  19240.         TABLE_TYPE    = convert(varchar(30),
  19241.                     case o.type 
  19242.                     when 'U' then 'TABLE'
  19243.                     when 'V' then 'VIEW'
  19244.                     when 'S' then 'SYSTEM TABLE'
  19245.                     end),
  19246.         TABLE_GUID    = convert(binary(16), null),
  19247.         DESCRIPTION    = convert(varchar(1), null)
  19248.     from    sysusers u, sysobjects o
  19249.     where    o.type in ('U','V','S')
  19250.     and     o.name = @table_name
  19251.     and     (    @table_schema is null
  19252.         or    @table_schema = user_name(o.uid)
  19253.         )
  19254.     and     (
  19255.             @table_type is null
  19256.         or    @table_type = case o.type 
  19257.                     when 'U' then 'TABLE'
  19258.                     when 'V' then 'VIEW'
  19259.                     when 'S' then 'SYSTEM TABLE'
  19260.                     end
  19261.         )
  19262.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  19263.     and     (
  19264.         suser_id() = 1     /* User is the System Administrator */
  19265.         or o.uid = user_id()     /* User created the object */
  19266.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  19267.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  19268.             from sysprotects p
  19269.             /* join to correlate with all rows in sysobjects */
  19270.             where p.id = o.id
  19271.             /* get rows for public,current user,user's group */
  19272.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  19273.             /* check for SELECT,EXECUTE privilege */
  19274.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  19275.             ) = 1    /* final magic...compare Grants      */
  19276.         )        
  19277.     END
  19278.  
  19279. ELSE
  19280.     BEGIN
  19281.     select    TABLE_CATALOG    = db_name(),
  19282.         TABLE_SCHEMA    = user_name(o.uid),
  19283.         TABLE_NAME    = o.name,
  19284.         TABLE_TYPE    = convert(varchar(30),
  19285.                     case o.type 
  19286.                     when 'U' then 'TABLE'
  19287.                     when 'V' then 'VIEW'
  19288.                     when 'S' then 'SYSTEM TABLE'
  19289.                     end),
  19290.         TABLE_GUID    = convert(binary(16), null),
  19291.         DESCRIPTION    = convert(varchar(1), null)
  19292.     from    sysusers u, sysobjects o
  19293.     where    o.type in ('U','V','S')
  19294.     and     (    @table_schema is null
  19295.         or    @table_schema = user_name(o.uid)
  19296.         )
  19297.     and     (
  19298.             @table_type is null
  19299.         or    @table_type = case o.type 
  19300.                     when 'U' then 'TABLE'
  19301.                     when 'V' then 'VIEW'
  19302.                     when 'S' then 'SYSTEM TABLE'
  19303.                     end
  19304.         )
  19305.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  19306.     and     (
  19307.         suser_id() = 1     /* User is the System Administrator */
  19308.         or o.uid = user_id()     /* User created the object */
  19309.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  19310.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  19311.             from sysprotects p
  19312.             /* join to correlate with all rows in sysobjects */
  19313.             where p.id = o.id
  19314.             /* get rows for public,current user,user's group */
  19315.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  19316.             /* check for SELECT,EXECUTE privilege */
  19317.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  19318.             ) = 1    /* final magic...compare Grants      */
  19319.         )        
  19320.     END
  19321. go
  19322.  
  19323. if (charindex('7.00', @@version) > 0)
  19324.     drop procedure sp_tables_rowset
  19325. else
  19326. begin
  19327.     print ''
  19328.     print ''
  19329.     print 'Warning:'
  19330.     print 'you are installing the stored procedures '
  19331.     print 'on a pre 7.0 SQL Server.'
  19332.     print 'Ignore the following errors.'
  19333. end
  19334. go
  19335.  
  19336. /*    Procedure for 7.00 server */
  19337. create procedure sp_tables_rowset
  19338.     (
  19339.     @table_name    varchar(255), 
  19340.     @table_schema    varchar(255) = null,    
  19341.     @table_type    varchar(255) = null 
  19342.     )
  19343. as
  19344.     select    TABLE_CATALOG    = db_name(),
  19345.         TABLE_SCHEMA    = user_name(o.uid),
  19346.         TABLE_NAME    = o.name,
  19347.         TABLE_TYPE    = convert(varchar(30),
  19348.                     case o.type 
  19349.                     when 'U' then 'TABLE'
  19350.                     when 'V' then 'VIEW'
  19351.                     when 'S' then 'SYSTEM TABLE'
  19352.                     end),
  19353.         TABLE_GUID    = convert(binary(16), null),
  19354.         DESCRIPTION    = convert(varchar(1), null)
  19355.     from    sysobjects o
  19356.     where    o.name = @table_name
  19357.     and     o.type in ('U','V','S')
  19358.     and    permissions(o.id) <> 0
  19359.     and     (    @table_schema is null
  19360.         or    @table_schema = user_name(o.uid)
  19361.         )
  19362.     and     (
  19363.             @table_type is null
  19364.         or    @table_type = case o.type 
  19365.                     when 'U' then 'TABLE'
  19366.                     when 'V' then 'VIEW'
  19367.                     when 'S' then 'SYSTEM TABLE'
  19368.                     end
  19369.         )    
  19370.     order by 4, 2, 3
  19371. go
  19372. create procedure sp_tables_rowset;2
  19373.     (
  19374.     @table_schema    varchar(255) = null,    
  19375.     @table_type    varchar(255) = null 
  19376.     )
  19377. as
  19378.     select    TABLE_CATALOG    = db_name(),
  19379.         TABLE_SCHEMA    = user_name(o.uid),
  19380.         TABLE_NAME    = o.name,
  19381.         TABLE_TYPE    = convert(varchar(30),
  19382.                     case o.type 
  19383.                     when 'U' then 'TABLE'
  19384.                     when 'V' then 'VIEW'
  19385.                     when 'S' then 'SYSTEM TABLE'
  19386.                     end),
  19387.         TABLE_GUID    = convert(binary(16), null),
  19388.         DESCRIPTION    = convert(varchar(1), null)
  19389.     from    sysobjects o
  19390.     where    o.type in ('U','V','S')
  19391.     and    permissions(o.id) <> 0
  19392.     and     (    @table_schema is null
  19393.         or    @table_schema = user_name(o.uid)
  19394.         )
  19395.     and     (
  19396.             @table_type is null
  19397.         or    @table_type = case o.type 
  19398.                     when 'U' then 'TABLE'
  19399.                     when 'V' then 'VIEW'
  19400.                     when 'S' then 'SYSTEM TABLE'
  19401.                     end
  19402.         )    
  19403.     order by 4, 2, 3
  19404. go
  19405. create procedure sp_tables_rowset;3
  19406.     (
  19407.     @table_name    varchar(255) = null, 
  19408.     @table_schema    varchar(255) = null,    
  19409.     @table_type    varchar(255) = null 
  19410.     )
  19411. as
  19412. IF @table_name is not null
  19413.     BEGIN
  19414.     select    TABLE_CATALOG    = db_name(),
  19415.         TABLE_SCHEMA    = user_name(o.uid),
  19416.         TABLE_NAME    = o.name,
  19417.         TABLE_TYPE    = convert(varchar(30),
  19418.                     case o.type 
  19419.                     when 'U' then 'TABLE'
  19420.                     when 'V' then 'VIEW'
  19421.                     when 'S' then 'SYSTEM TABLE'
  19422.                     end),
  19423.         TABLE_GUID    = convert(binary(16), null),
  19424.         DESCRIPTION    = convert(varchar(1), null)
  19425.     from    sysobjects o
  19426.     where    o.name = @table_name
  19427.     and     o.type in ('U','V','S')
  19428.     and    permissions(o.id) <> 0
  19429.     and     (    @table_schema is null
  19430.         or    @table_schema = user_name(o.uid)
  19431.         )
  19432.     and     (
  19433.             @table_type is null
  19434.         or    @table_type = case o.type 
  19435.                     when 'U' then 'TABLE'
  19436.                     when 'V' then 'VIEW'
  19437.                     when 'S' then 'SYSTEM TABLE'
  19438.                     end
  19439.         )    
  19440.     END
  19441. ELSE
  19442.     BEGIN
  19443.     select    TABLE_CATALOG    = db_name(),
  19444.         TABLE_SCHEMA    = user_name(o.uid),
  19445.         TABLE_NAME    = o.name,
  19446.         TABLE_TYPE    = convert(varchar(30),
  19447.                     case o.type 
  19448.                     when 'U' then 'TABLE'
  19449.                     when 'V' then 'VIEW'
  19450.                     when 'S' then 'SYSTEM TABLE'
  19451.                     end),
  19452.         TABLE_GUID    = convert(binary(16), null),
  19453.         DESCRIPTION    = convert(varchar(1), null)
  19454.     from    sysobjects o
  19455.     where    o.type in ('U','V','S')
  19456.     and    permissions(o.id) <> 0
  19457.     and     (    @table_schema is null
  19458.         or    @table_schema = user_name(o.uid)
  19459.         )
  19460.     and     (
  19461.             @table_type is null
  19462.         or    @table_type = case o.type 
  19463.                     when 'U' then 'TABLE'
  19464.                     when 'V' then 'VIEW'
  19465.                     when 'S' then 'SYSTEM TABLE'
  19466.                     end
  19467.         )    
  19468.     END
  19469. go
  19470.  
  19471.  
  19472. create procedure sp_tables_rowset;4
  19473.     (
  19474.  
  19475.        @handle        int output,
  19476.        @scrollopt    int output,
  19477.     @ccopt        int output,
  19478.     @rows        int output,
  19479.     @cmd        varchar(255)
  19480.     )
  19481. as
  19482.     declare @ret int
  19483.     declare @database_name varchar(30)
  19484.  
  19485.     create table #sptablesrowset
  19486.         (
  19487.         TABLE_CATALOG        varchar(30) not null,
  19488.         TABLE_SCHEMA        varchar(30) not null,
  19489.         TABLE_NAME        varchar(30) not null,
  19490.         TABLE_TYPE        varchar(30) not null,
  19491.         TABLE_GUID        binary(16) null,
  19492.         DESCRIPTION        varchar(1) null
  19493.         )
  19494.  
  19495.     declare sysdatabases_cursor insensitive cursor for 
  19496.         select name from master.dbo.sysdatabases
  19497.   
  19498.     set nocount on
  19499.     open sysdatabases_cursor
  19500.       fetch next from sysdatabases_cursor into @database_name 
  19501.     while @@fetch_status = 0
  19502.         begin
  19503.         exec('use '+@database_name+' insert into #sptablesrowset '+@cmd)
  19504.         fetch next from sysdatabases_cursor into @database_name 
  19505.         end
  19506.       close sysdatabases_cursor
  19507.     deallocate sysdatabases_cursor
  19508.  
  19509.     if @scrollopt is null
  19510.         begin
  19511.         select * from #sptablesrowset order by 4,1,2,3
  19512.         end
  19513.     else
  19514.         begin
  19515.         exec @ret = sp_cursoropen @handle output,
  19516.             'select * from #sptablesrowset order by 4,1,2,3',
  19517.                 @scrollopt output, @ccopt output, @rows output
  19518.         end
  19519.     drop table #sptablesrowset
  19520.     return isnull(@ret,0)
  19521. go
  19522.  
  19523. grant execute on sp_tables_rowset to public
  19524. go
  19525.  
  19526. dump tran master with no_log
  19527. go
  19528.  
  19529.  
  19530.  
  19531.  
  19532. print 'creating sp_table_privileges_rowset'
  19533. go
  19534.  
  19535. /*    Procedure for 6.0 and 6.5 server */
  19536. CREATE PROCEDURE sp_table_privileges_rowset
  19537.     (
  19538.     @table_name    varchar(255) = null,
  19539.     @table_schema    varchar(255) = null,
  19540.     @grantor    varchar(255) = null,
  19541.     @grantee    varchar(255) = null
  19542.     )
  19543. as
  19544. IF @table_name is not null
  19545.     BEGIN
  19546.     select
  19547.         GRANTOR        = user_name(p.grantor),
  19548.         GRANTEE        = user_name(u.uid),
  19549.         TABLE_CATALOG    = db_name(),
  19550.         TABLE_SCHEMA    = user_name(o.uid),
  19551.         TABLE_NAME    = o.name,
  19552.         PRIVILEGE_TYPE    = convert(varchar(30),
  19553.                     case p.action
  19554.                     when 193 then 'SELECT'
  19555.                     when 195 then 'INSERT'
  19556.                     when 197 then 'UPDATE'
  19557.                     else 'REFERENCES'
  19558.                     end),
  19559.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  19560.     from 
  19561.         sysprotects p, sysobjects o, sysusers u
  19562.     where
  19563.         o.name = @table_name
  19564.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  19565.     and    (@grantee is null or @grantee = user_name(u.uid))
  19566.     and     o.type in ('U','V')
  19567.     and    p.id = o.id
  19568.     and     (@grantor is null or @grantor = user_name(p.grantor))
  19569.             /* expand groups */
  19570.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  19571.     and     p.protecttype <> 206    /* only grant rows */
  19572.     and     p.action in (26,193,195,196,197)
  19573.     and     o.uid <> u.uid            /* no rows for owner */
  19574.     and     not exists (            /* exclude revoke'd privileges */
  19575.             select     *
  19576.             from     sysprotects p1
  19577.             where    p1.protecttype = 206
  19578.             and     p1.action = p.action
  19579.             and     p1.id = p.id
  19580.             and     p1.uid = u.uid)
  19581.     union all
  19582.     select    /*    Add rows for table owner */
  19583.         GRANTOR        = user_name(u.uid),
  19584.         GRANTEE        = user_name(o.uid),
  19585.         TABLE_CATALOG    = db_name(),
  19586.         TABLE_SCHEMA    = user_name(o.uid),
  19587.         TABLE_NAME    = o.name,
  19588.         PRIVILEGE_TYPE    = convert(varchar(30),
  19589.                     case v.number
  19590.                     when 193 then 'SELECT'
  19591.                     when 195 then 'INSERT'
  19592.                     when 197 then 'UPDATE'
  19593.                     else 'REFERENCES'
  19594.                     end),
  19595.         IS_GRANTABLE    = convert(bit,1)    
  19596.     from 
  19597.         sysobjects o, master.dbo.spt_values v, sysusers u
  19598.     where
  19599.         o.name = @table_name
  19600.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  19601.     and    (@grantee is null or @grantee = user_name(u.uid))
  19602.     and     o.type in ('U','V')
  19603.     and     u.suid = 1        /* grantor is dbo of database */
  19604.     and    (@grantor is null or @grantor = user_name(u.uid))
  19605.     and     v.type = 'P'    /* cross product to get all exposed privileges */
  19606.     and     v.number in (26,193,195,196,197)
  19607.     and     not exists (    /* exclude revoke'd privileges */
  19608.             select     *
  19609.             from     sysprotects p1
  19610.             where    p1.protecttype = 206
  19611.             and     p1.action = v.number
  19612.             and     p1.id = o.id
  19613.             and     p1.uid = o.uid)
  19614.     order by 4,5,6,1,2
  19615.     END
  19616. ELSE
  19617.     BEGIN
  19618.     select
  19619.         GRANTOR        = user_name(p.grantor),
  19620.         GRANTEE        = user_name(u.uid),
  19621.         TABLE_CATALOG    = db_name(),
  19622.         TABLE_SCHEMA    = user_name(o.uid),
  19623.         TABLE_NAME    = o.name,
  19624.         PRIVILEGE_TYPE    = convert(varchar(30),
  19625.                     case p.action
  19626.                     when 193 then 'SELECT'
  19627.                     when 195 then 'INSERT'
  19628.                     when 197 then 'UPDATE'
  19629.                     else 'REFERENCES'
  19630.                     end),
  19631.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  19632.     from 
  19633.         sysprotects p, sysobjects o, sysusers u
  19634.     where
  19635.         o.type in ('U','V')
  19636.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  19637.     and    (@grantee is null or @grantee = user_name(u.uid))
  19638.     and    p.id = o.id
  19639.     and     (@grantor is null or @grantor = user_name(p.grantor))
  19640.             /* expand groups */
  19641.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  19642.     and     p.protecttype <> 206    /* only grant rows */
  19643.     and     p.action in (26,193,195,196,197)
  19644.     and     o.uid <> u.uid            /* no rows for owner */
  19645.     and     not exists (            /* exclude revoke'd privileges */
  19646.             select     *
  19647.             from     sysprotects p1
  19648.             where    p1.protecttype = 206
  19649.             and     p1.action = p.action
  19650.             and     p1.id = p.id
  19651.             and     p1.uid = u.uid)
  19652.     union all
  19653.     select    /*    Add rows for table owner */
  19654.         GRANTOR        = user_name(u.uid),
  19655.         GRANTEE        = user_name(o.uid),
  19656.         TABLE_CATALOG    = db_name(),
  19657.         TABLE_SCHEMA    = user_name(o.uid),
  19658.         TABLE_NAME    = o.name,
  19659.         PRIVILEGE_TYPE    = convert(varchar(30),
  19660.                     case v.number
  19661.                     when 193 then 'SELECT'
  19662.                     when 195 then 'INSERT'
  19663.                     when 197 then 'UPDATE'
  19664.                     else 'REFERENCES'
  19665.                     end),
  19666.         IS_GRANTABLE    = convert(bit,1)    
  19667.     from 
  19668.         sysobjects o, master.dbo.spt_values v, sysusers u
  19669.     where
  19670.         o.type in ('U','V')
  19671.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  19672.     and    (@grantee is null or @grantee = user_name(u.uid))
  19673.     and     u.suid = 1        /* grantor is dbo of database */
  19674.     and    (@grantor is null or @grantor = user_name(u.uid))
  19675.     and     v.type = 'P'    /* cross product to get all exposed privileges */
  19676.     and     v.number in (26,193,195,196,197)
  19677.     and     not exists (    /* exclude revoke'd privileges */
  19678.             select     *
  19679.             from     sysprotects p1
  19680.             where    p1.protecttype = 206
  19681.             and     p1.action = v.number
  19682.             and     p1.id = o.id
  19683.             and     p1.uid = o.uid)
  19684.     order by 4,5,6,1,2
  19685.     END
  19686. go
  19687. CREATE PROCEDURE sp_table_privileges_rowset;2
  19688.     (
  19689.        @handle        int output,
  19690.        @scrollopt    int output,
  19691.     @ccopt        int output,
  19692.     @rows        int output,
  19693.     @table_name     varchar(255) = null,
  19694.     @table_schema    varchar(255) = null,
  19695.     @grantor    varchar(255) = null,
  19696.     @grantee    varchar(255) = null
  19697.     )
  19698. as
  19699. declare @ret int
  19700. create table #sptprivsrowset1
  19701.     (
  19702.     GRANTOR        varchar(30) not null,
  19703.     GRANTEE        varchar(30) not null,
  19704.     TABLE_CATALOG    varchar(30) not null,
  19705.     TABLE_SCHEMA    varchar(30) not null,
  19706.     TABLE_NAME    varchar(30) not null,
  19707.     PRIVILEGE_TYPE    varchar(30) not null,
  19708.     IS_GRANTABLE    bit not null
  19709.     )
  19710.     
  19711. IF @table_name is not null
  19712.     BEGIN
  19713.     insert into #sptprivsrowset1
  19714.     select
  19715.         GRANTOR        = user_name(p.grantor),
  19716.         GRANTEE        = user_name(u.uid),
  19717.         TABLE_CATALOG    = db_name(),
  19718.         TABLE_SCHEMA    = user_name(o.uid),
  19719.         TABLE_NAME    = o.name,
  19720.         PRIVILEGE_TYPE    = convert(varchar(30),
  19721.                     case p.action
  19722.                     when 193 then 'SELECT'
  19723.                     when 195 then 'INSERT'
  19724.                     when 197 then 'UPDATE'
  19725.                     else 'REFERENCES'
  19726.                     end),
  19727.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  19728.     from 
  19729.         sysprotects p, sysobjects o, sysusers u
  19730.     where
  19731.         o.name = @table_name
  19732.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  19733.     and    (@grantee is null or @grantee = user_name(u.uid))
  19734.     and     o.type in ('U','V')
  19735.     and    p.id = o.id
  19736.     and     (@grantor is null or @grantor = user_name(p.grantor))
  19737.             /* expand groups */
  19738.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  19739.     and     p.protecttype <> 206    /* only grant rows */
  19740.     and     p.action in (26,193,195,196,197)
  19741.     and     o.uid <> u.uid            /* no rows for owner */
  19742.     and     not exists (            /* exclude revoke'd privileges */
  19743.             select     *
  19744.             from     sysprotects p1
  19745.             where    p1.protecttype = 206
  19746.             and     p1.action = p.action
  19747.             and     p1.id = p.id
  19748.             and     p1.uid = u.uid)
  19749.     union all
  19750.     select    /*    Add rows for table owner */
  19751.         GRANTOR        = user_name(u.uid),
  19752.         GRANTEE        = user_name(o.uid),
  19753.         TABLE_CATALOG    = db_name(),
  19754.         TABLE_SCHEMA    = user_name(o.uid),
  19755.         TABLE_NAME    = o.name,
  19756.         PRIVILEGE_TYPE    = convert(varchar(30),
  19757.                     case v.number
  19758.                     when 193 then 'SELECT'
  19759.                     when 195 then 'INSERT'
  19760.                     when 197 then 'UPDATE'
  19761.                     else 'REFERENCES'
  19762.                     end),
  19763.         IS_GRANTABLE    = convert(bit,1)    
  19764.     from 
  19765.         sysobjects o, master.dbo.spt_values v, sysusers u
  19766.     where
  19767.         o.name = @table_name
  19768.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  19769.     and    (@grantee is null or @grantee = user_name(u.uid))
  19770.     and     o.type in ('U','V')
  19771.     and     u.suid = 1        /* grantor is dbo of database */
  19772.     and    (@grantor is null or @grantor = user_name(u.uid))
  19773.     and     v.type = 'P'    /* cross product to get all exposed privileges */
  19774.     and     v.number in (26,193,195,196,197)
  19775.     and     not exists (    /* exclude revoke'd privileges */
  19776.             select     *
  19777.             from     sysprotects p1
  19778.             where    p1.protecttype = 206
  19779.             and     p1.action = v.number
  19780.             and     p1.id = o.id
  19781.             and     p1.uid = o.uid)
  19782.     order by 4,5,6,1,2
  19783.     END
  19784. ELSE
  19785.     BEGIN
  19786.     insert into #sptprivsrowset1
  19787.     select
  19788.         GRANTOR        = user_name(p.grantor),
  19789.         GRANTEE        = user_name(u.uid),
  19790.         TABLE_CATALOG    = db_name(),
  19791.         TABLE_SCHEMA    = user_name(o.uid),
  19792.         TABLE_NAME    = o.name,
  19793.         PRIVILEGE_TYPE    = convert(varchar(30),
  19794.                     case p.action
  19795.                     when 193 then 'SELECT'
  19796.                     when 195 then 'INSERT'
  19797.                     when 197 then 'UPDATE'
  19798.                     else 'REFERENCES'
  19799.                     end),
  19800.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  19801.     from 
  19802.         sysprotects p, sysobjects o, sysusers u
  19803.     where
  19804.         o.type in ('U','V')
  19805.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  19806.     and    (@grantee is null or @grantee = user_name(u.uid))
  19807.     and    p.id = o.id
  19808.     and     (@grantor is null or @grantor = user_name(p.grantor))
  19809.             /* expand groups */
  19810.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  19811.     and     p.protecttype <> 206    /* only grant rows */
  19812.     and     p.action in (26,193,195,196,197)
  19813.     and     o.uid <> u.uid            /* no rows for owner */
  19814.     and     not exists (            /* exclude revoke'd privileges */
  19815.             select     *
  19816.             from     sysprotects p1
  19817.             where    p1.protecttype = 206
  19818.             and     p1.action = p.action
  19819.             and     p1.id = p.id
  19820.             and     p1.uid = u.uid)
  19821.     union all
  19822.     select    /*    Add rows for table owner */
  19823.         GRANTOR        = user_name(u.uid),
  19824.         GRANTEE        = user_name(o.uid),
  19825.         TABLE_CATALOG    = db_name(),
  19826.         TABLE_SCHEMA    = user_name(o.uid),
  19827.         TABLE_NAME    = o.name,
  19828.         PRIVILEGE_TYPE    = convert(varchar(30),
  19829.                     case v.number
  19830.                     when 193 then 'SELECT'
  19831.                     when 195 then 'INSERT'
  19832.                     when 197 then 'UPDATE'
  19833.                     else 'REFERENCES'
  19834.                     end),
  19835.         IS_GRANTABLE    = convert(bit,1)    
  19836.     from 
  19837.         sysobjects o, master.dbo.spt_values v, sysusers u
  19838.     where
  19839.         o.type in ('U','V')
  19840.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  19841.     and    (@grantee is null or @grantee = user_name(u.uid))
  19842.     and     u.suid = 1        /* grantor is dbo of database */
  19843.     and    (@grantor is null or @grantor = user_name(u.uid))
  19844.     and     v.type = 'P'    /* cross product to get all exposed privileges */
  19845.     and     v.number in (26,193,195,196,197)
  19846.     and     not exists (    /* exclude revoke'd privileges */
  19847.             select     *
  19848.             from     sysprotects p1
  19849.             where    p1.protecttype = 206
  19850.             and     p1.action = v.number
  19851.             and     p1.id = o.id
  19852.             and     p1.uid = o.uid)
  19853.     order by 4,5,6,1,2
  19854.     END
  19855.  
  19856. exec @ret = sp_cursoropen @handle output, 'select * from #sptprivsrowset1',
  19857.     @scrollopt output, @ccopt output, @rows output
  19858.  
  19859. drop table #sptprivsrowset1
  19860. return isnull(@ret,0)
  19861. go
  19862.  
  19863.  
  19864. create procedure sp_table_privileges_rowset;4
  19865.     (
  19866.        @handle        int output,
  19867.        @scrollopt    int output,
  19868.     @ccopt        int output,
  19869.     @rows        int output,
  19870.     @cmd        varchar(255)
  19871.     )
  19872. as
  19873.     declare @ret int
  19874.     declare @database_name varchar(30)
  19875.  
  19876.     create table #sptprivsrowset2
  19877.         (
  19878.         GRANTOR        varchar(30) not null,
  19879.         GRANTEE        varchar(30) not null,
  19880.         TABLE_CATALOG    varchar(30) not null,
  19881.         TABLE_SCHEMA    varchar(30) not null,
  19882.         TABLE_NAME    varchar(30) not null,
  19883.         PRIVILEGE_TYPE    varchar(30) not null,
  19884.         IS_GRANTABLE    bit not null
  19885.         )
  19886.  
  19887.     declare sysdatabases_cursor insensitive cursor for 
  19888.         select name from master.dbo.sysdatabases order by name
  19889.   
  19890.     set nocount on
  19891.     open sysdatabases_cursor
  19892.       fetch next from sysdatabases_cursor into @database_name 
  19893.     while @@fetch_status = 0
  19894.         begin
  19895.         exec('use '+@database_name+' insert into #sptprivsrowset2 '+@cmd)
  19896.         fetch next from sysdatabases_cursor into @database_name 
  19897.         end
  19898.       close sysdatabases_cursor
  19899.     deallocate sysdatabases_cursor
  19900.  
  19901.     if @scrollopt is null
  19902.         begin
  19903.         select * from #sptprivsrowset2
  19904.         end
  19905.     else
  19906.         begin
  19907.         exec @ret = sp_cursoropen @handle output,
  19908.             'select * from #sptprivsrowset2',
  19909.                 @scrollopt output, @ccopt output, @rows output
  19910.         end
  19911.     drop table #sptprivsrowset2
  19912.     return isnull(@ret,0)
  19913. go
  19914.  
  19915.  
  19916.  
  19917.  
  19918. if (charindex('7.00', @@version) = 0)
  19919. begin
  19920.     print ''
  19921.     print ''
  19922.     print 'Warning:'
  19923.     print 'you are installing the stored procedures '
  19924.     print 'on a pre 7.0 SQL Server.'
  19925.     print 'Ignore the following errors.'
  19926. end
  19927. else
  19928.     drop proc sp_table_privileges_rowset
  19929. go
  19930.  
  19931.  
  19932. /*    Procedure for 7.0 server */
  19933. CREATE PROCEDURE sp_table_privileges_rowset
  19934.     (
  19935.     @table_name         varchar(255),
  19936.     @table_schema        varchar(255) = null,
  19937.     @grantor        varchar(255) = null,
  19938.     @grantee        varchar(255) = null
  19939.     )
  19940. as
  19941.     select
  19942.         GRANTOR        = user_name(p.grantor),
  19943.         GRANTEE        = user_name(u.uid),
  19944.         TABLE_CATALOG    = db_name(),
  19945.         TABLE_SCHEMA    = user_name(o.uid),
  19946.         TABLE_NAME    = o.name,
  19947.         PRIVILEGE_TYPE    = convert(varchar(30),
  19948.                     case p.action
  19949.                     when 193 then 'SELECT'
  19950.                     when 195 then 'INSERT'
  19951.                     when 197 then 'UPDATE'
  19952.                     else 'REFERENCES'
  19953.                     end),
  19954.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  19955.     from 
  19956.         sysprotects p, sysobjects o, sysusers u, sysmembers m
  19957.     where
  19958.         o.name = @table_name
  19959.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  19960.     and    (@grantee is null or @grantee = user_name(u.uid))
  19961.     and     o.type in ('U','V')
  19962.     and    p.id = o.id
  19963.     and     (@grantor is null or @grantor = user_name(p.grantor))
  19964.         /* expand groups - AKUNDONE: only 1 level of grp unrolling. */
  19965.     and     (u.uid > 0 and u.uid < 16384)
  19966.     and     ((p.uid = u.uid) or (p.uid = m.groupuid and u.uid = m.memberuid))
  19967.     and     p.protecttype <> 206    /* only grant rows */
  19968.     and     p.action in (26,193,195,196,197)
  19969.     and     o.uid <> u.uid            /* no rows for owner */
  19970.     and     not exists (            /* exclude revoke'd privileges */
  19971.             select     *
  19972.             from     sysprotects p1
  19973.             where    p1.protecttype = 206
  19974.             and     p1.action = p.action
  19975.             and     p1.id = p.id
  19976.             and     p1.uid = u.uid)
  19977.     union all
  19978.     select    /*    Add rows for table owner */
  19979.         GRANTOR        = user_name(u.uid),
  19980.         GRANTEE        = user_name(o.uid),
  19981.         TABLE_CATALOG    = db_name(),
  19982.         TABLE_SCHEMA    = user_name(o.uid),
  19983.         TABLE_NAME    = o.name,
  19984.         PRIVILEGE_TYPE    = convert(varchar(30),
  19985.                     case v.number
  19986.                     when 193 then 'SELECT'
  19987.                     when 195 then 'INSERT'
  19988.                     when 197 then 'UPDATE'
  19989.                     else 'REFERENCES'
  19990.                     end),
  19991.         IS_GRANTABLE    = convert(bit,1)    
  19992.     from 
  19993.         sysobjects o, master.dbo.spt_values v, sysusers u
  19994.     where
  19995.         o.name = @table_name
  19996.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  19997.     and    (@grantee is null or @grantee = user_name(u.uid))
  19998.     and     o.type in ('U','V')
  19999.     and     u.suid = 1        /* grantor is dbo of database */
  20000.     and    (@grantor is null or @grantor = user_name(u.uid))
  20001.     and     v.type = 'P'    /* cross product to get all exposed privileges */
  20002.     and     v.number in (26,193,195,196,197)
  20003.     and     not exists (    /* exclude revoke'd privileges */
  20004.             select     *
  20005.             from     sysprotects p1
  20006.             where    p1.protecttype = 206
  20007.             and     p1.action = v.number
  20008.             and     p1.id = o.id
  20009.             and     p1.uid = o.uid)
  20010.     order by 4,5,6,1,2
  20011. go
  20012. CREATE PROCEDURE sp_table_privileges_rowset;2
  20013.     (
  20014.     @table_schema        varchar(255) = null,
  20015.     @grantor        varchar(255) = null,
  20016.     @grantee        varchar(255) = null
  20017.     )
  20018. as
  20019.     select
  20020.         GRANTOR        = user_name(p.grantor),
  20021.         GRANTEE        = user_name(u.uid),
  20022.         TABLE_CATALOG    = db_name(),
  20023.         TABLE_SCHEMA    = user_name(o.uid),
  20024.         TABLE_NAME    = o.name,
  20025.         PRIVILEGE_TYPE    = convert(varchar(30),
  20026.                     case p.action
  20027.                     when 193 then 'SELECT'
  20028.                     when 195 then 'INSERT'
  20029.                     when 197 then 'UPDATE'
  20030.                     else 'REFERENCES'
  20031.                     end),
  20032.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  20033.     from 
  20034.         sysprotects p, sysobjects o, sysusers u, sysmembers m
  20035.     where
  20036.         o.type in ('U','V')
  20037.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  20038.     and    (@grantee is null or @grantee = user_name(u.uid))
  20039.     and    p.id = o.id
  20040.     and     (@grantor is null or @grantor = user_name(p.grantor))
  20041.         /* expand groups - AKUNDONE: only 1 level of grp unrolling. */
  20042.     and     (u.uid > 0 and u.uid < 16384)
  20043.     and     ((p.uid = u.uid) or (p.uid = m.groupuid and u.uid = m.memberuid))
  20044.     and     p.protecttype <> 206    /* only grant rows */
  20045.     and     p.action in (26,193,195,196,197)
  20046.     and     o.uid <> u.uid            /* no rows for owner */
  20047.     and     not exists (            /* exclude revoke'd privileges */
  20048.             select     *
  20049.             from     sysprotects p1
  20050.             where    p1.protecttype = 206
  20051.             and     p1.action = p.action
  20052.             and     p1.id = p.id
  20053.             and     p1.uid = u.uid)
  20054.     union all
  20055.     select    /*    Add rows for table owner */
  20056.         GRANTOR        = user_name(u.uid),
  20057.         GRANTEE        = user_name(o.uid),
  20058.         TABLE_CATALOG    = db_name(),
  20059.         TABLE_SCHEMA    = user_name(o.uid),
  20060.         TABLE_NAME    = o.name,
  20061.         PRIVILEGE_TYPE    = convert(varchar(30),
  20062.                     case v.number
  20063.                     when 193 then 'SELECT'
  20064.                     when 195 then 'INSERT'
  20065.                     when 197 then 'UPDATE'
  20066.                     else 'REFERENCES'
  20067.                     end),
  20068.         IS_GRANTABLE    = convert(bit,1)    
  20069.     from 
  20070.         sysobjects o, master.dbo.spt_values v, sysusers u
  20071.     where
  20072.         o.type in ('U','V')
  20073.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  20074.     and    (@grantee is null or @grantee = user_name(u.uid))
  20075.     and     u.suid = 1        /* grantor is dbo of database */
  20076.     and    (@grantor is null or @grantor = user_name(u.uid))
  20077.     and     v.type = 'P'    /* cross product to get all exposed privileges */
  20078.     and     v.number in (26,193,195,196,197)
  20079.     and     not exists (    /* exclude revoke'd privileges */
  20080.             select     *
  20081.             from     sysprotects p1
  20082.             where    p1.protecttype = 206
  20083.             and     p1.action = v.number
  20084.             and     p1.id = o.id
  20085.             and     p1.uid = o.uid)
  20086.     order by 4,5,6,1,2
  20087. go
  20088. CREATE PROCEDURE sp_table_privileges_rowset;3
  20089.     (
  20090.     @table_name         varchar(255) = null,
  20091.     @table_schema        varchar(255) = null,
  20092.     @grantor        varchar(255) = null,
  20093.     @grantee        varchar(255) = null
  20094.     )
  20095. as
  20096. IF @table_name is not null
  20097.     BEGIN
  20098.     select
  20099.         GRANTOR        = user_name(p.grantor),
  20100.         GRANTEE        = user_name(u.uid),
  20101.         TABLE_CATALOG    = db_name(),
  20102.         TABLE_SCHEMA    = user_name(o.uid),
  20103.         TABLE_NAME    = o.name,
  20104.         PRIVILEGE_TYPE    = convert(varchar(30),
  20105.                     case p.action
  20106.                     when 193 then 'SELECT'
  20107.                     when 195 then 'INSERT'
  20108.                     when 197 then 'UPDATE'
  20109.                     else 'REFERENCES'
  20110.                     end),
  20111.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  20112.     from 
  20113.         sysprotects p, sysobjects o, sysusers u, sysmembers m
  20114.     where
  20115.         o.name = @table_name
  20116.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  20117.     and    (@grantee is null or @grantee = user_name(u.uid))
  20118.     and     o.type in ('U','V')
  20119.     and    p.id = o.id
  20120.     and     (@grantor is null or @grantor = user_name(p.grantor))
  20121.         /* expand groups - AKUNDONE: only 1 level of grp unrolling. */
  20122.     and     (u.uid > 0 and u.uid < 16384)
  20123.     and     ((p.uid = u.uid) or (p.uid = m.groupuid and u.uid = m.memberuid))
  20124.     and     p.protecttype <> 206    /* only grant rows */
  20125.     and     p.action in (26,193,195,196,197)
  20126.     and     o.uid <> u.uid            /* no rows for owner */
  20127.     and     not exists (            /* exclude revoke'd privileges */
  20128.             select     *
  20129.             from     sysprotects p1
  20130.             where    p1.protecttype = 206
  20131.             and     p1.action = p.action
  20132.             and     p1.id = p.id
  20133.             and     p1.uid = u.uid)
  20134.     union all
  20135.     select    /*    Add rows for table owner */
  20136.         GRANTOR        = user_name(u.uid),
  20137.         GRANTEE        = user_name(o.uid),
  20138.         TABLE_CATALOG    = db_name(),
  20139.         TABLE_SCHEMA    = user_name(o.uid),
  20140.         TABLE_NAME    = o.name,
  20141.         PRIVILEGE_TYPE    = convert(varchar(30),
  20142.                     case v.number
  20143.                     when 193 then 'SELECT'
  20144.                     when 195 then 'INSERT'
  20145.                     when 197 then 'UPDATE'
  20146.                     else 'REFERENCES'
  20147.                     end),
  20148.         IS_GRANTABLE    = convert(bit,1)    
  20149.     from 
  20150.         sysobjects o, master.dbo.spt_values v, sysusers u
  20151.     where
  20152.         o.name = @table_name
  20153.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  20154.     and    (@grantee is null or @grantee = user_name(u.uid))
  20155.     and     o.type in ('U','V')
  20156.     and     u.suid = 1        /* grantor is dbo of database */
  20157.     and    (@grantor is null or @grantor = user_name(u.uid))
  20158.     and     v.type = 'P'    /* cross product to get all exposed privileges */
  20159.     and     v.number in (26,193,195,196,197)
  20160.     and     not exists (    /* exclude revoke'd privileges */
  20161.             select     *
  20162.             from     sysprotects p1
  20163.             where    p1.protecttype = 206
  20164.             and     p1.action = v.number
  20165.             and     p1.id = o.id
  20166.             and     p1.uid = o.uid)
  20167.     END
  20168. ELSE
  20169.     BEGIN
  20170.     select
  20171.         GRANTOR        = user_name(p.grantor),
  20172.         GRANTEE        = user_name(u.uid),
  20173.         TABLE_CATALOG    = db_name(),
  20174.         TABLE_SCHEMA    = user_name(o.uid),
  20175.         TABLE_NAME    = o.name,
  20176.         PRIVILEGE_TYPE    = convert(varchar(30),
  20177.                     case p.action
  20178.                     when 193 then 'SELECT'
  20179.                     when 195 then 'INSERT'
  20180.                     when 197 then 'UPDATE'
  20181.                     else 'REFERENCES'
  20182.                     end),
  20183.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  20184.     from 
  20185.         sysprotects p, sysobjects o, sysusers u, sysmembers m
  20186.     where
  20187.         o.type in ('U','V')
  20188.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  20189.     and    (@grantee is null or @grantee = user_name(u.uid))
  20190.     and    p.id = o.id
  20191.     and     (@grantor is null or @grantor = user_name(p.grantor))
  20192.         /* expand groups - AKUNDONE: only 1 level of grp unrolling. */
  20193.     and     (u.uid > 0 and u.uid < 16384)
  20194.     and     ((p.uid = u.uid) or (p.uid = m.groupuid and u.uid = m.memberuid))
  20195.     and     p.protecttype <> 206    /* only grant rows */
  20196.     and     p.action in (26,193,195,196,197)
  20197.     and     o.uid <> u.uid            /* no rows for owner */
  20198.     and     not exists (            /* exclude revoke'd privileges */
  20199.             select     *
  20200.             from     sysprotects p1
  20201.             where    p1.protecttype = 206
  20202.             and     p1.action = p.action
  20203.             and     p1.id = p.id
  20204.             and     p1.uid = u.uid)
  20205.     union all
  20206.     select    /*    Add rows for table owner */
  20207.         GRANTOR        = user_name(u.uid),
  20208.         GRANTEE        = user_name(o.uid),
  20209.         TABLE_CATALOG    = db_name(),
  20210.         TABLE_SCHEMA    = user_name(o.uid),
  20211.         TABLE_NAME    = o.name,
  20212.         PRIVILEGE_TYPE    = convert(varchar(30),
  20213.                     case v.number
  20214.                     when 193 then 'SELECT'
  20215.                     when 195 then 'INSERT'
  20216.                     when 197 then 'UPDATE'
  20217.                     else 'REFERENCES'
  20218.                     end),
  20219.         IS_GRANTABLE    = convert(bit,1)    
  20220.     from 
  20221.         sysobjects o, master.dbo.spt_values v, sysusers u
  20222.     where
  20223.         o.type in ('U','V')
  20224.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  20225.     and    (@grantee is null or @grantee = user_name(u.uid))
  20226.     and     u.suid = 1        /* grantor is dbo of database */
  20227.     and    (@grantor is null or @grantor = user_name(u.uid))
  20228.     and     v.type = 'P'    /* cross product to get all exposed privileges */
  20229.     and     v.number in (26,193,195,196,197)
  20230.     and     not exists (    /* exclude revoke'd privileges */
  20231.             select     *
  20232.             from     sysprotects p1
  20233.             where    p1.protecttype = 206
  20234.             and     p1.action = v.number
  20235.             and     p1.id = o.id
  20236.             and     p1.uid = o.uid)
  20237.     END
  20238. go
  20239.  
  20240.  
  20241. create procedure sp_table_privileges_rowset;4
  20242.     (
  20243.        @handle        int output,
  20244.        @scrollopt    int output,
  20245.     @ccopt        int output,
  20246.     @rows        int output,
  20247.     @cmd        varchar(255)
  20248.     )
  20249. as
  20250.     declare @ret int
  20251.     declare @database_name varchar(30)
  20252.  
  20253.     create table #sptprivsrowset
  20254.         (
  20255.         GRANTOR        varchar(30) not null,
  20256.         GRANTEE        varchar(30) not null,
  20257.         TABLE_CATALOG    varchar(30) not null,
  20258.         TABLE_SCHEMA    varchar(30) not null,
  20259.         TABLE_NAME    varchar(30) not null,
  20260.         PRIVILEGE_TYPE    varchar(30) not null,
  20261.         IS_GRANTABLE    bit not null
  20262.         )
  20263.  
  20264.     declare sysdatabases_cursor insensitive cursor for 
  20265.         select name from master.dbo.sysdatabases
  20266.   
  20267.     set nocount on
  20268.     open sysdatabases_cursor
  20269.       fetch next from sysdatabases_cursor into @database_name 
  20270.     while @@fetch_status = 0
  20271.         begin
  20272.         exec('use '+@database_name+' insert into #sptprivsrowset '+@cmd)
  20273.         fetch next from sysdatabases_cursor into @database_name 
  20274.         end
  20275.       close sysdatabases_cursor
  20276.     deallocate sysdatabases_cursor
  20277.  
  20278.     if @scrollopt is null
  20279.         begin
  20280.         select * from #sptprivsrowset order by 3,4,5,6,1,2
  20281.         end
  20282.     else
  20283.         begin
  20284.         exec @ret = sp_cursoropen @handle output,
  20285.             'select * from #sptprivsrowset order by 3,4,5,6,1,2',
  20286.                 @scrollopt output, @ccopt output, @rows output
  20287.         end
  20288.     drop table #sptprivsrowset
  20289.     return isnull(@ret,0)
  20290. go
  20291.  
  20292.  
  20293. grant execute on sp_table_privileges_rowset to public
  20294. go
  20295.  
  20296. -------------------------------------------------------------------------------
  20297. -------------- CATALOG STORED PROCEDURES FOR OLEDB SOURCES --------------------
  20298. -------------------------------------------------------------------------------
  20299.  
  20300. if object_id('sp_linkedservers', 'P') is not null
  20301.     drop proc sp_linkedservers
  20302. go
  20303. raiserror(15339,-1,-1,'sp_linkedservers')
  20304. go
  20305. create proc sp_linkedservers as
  20306.     select
  20307.         SRV_NAME = srvname,
  20308.         SRV_PROVIDER = convert(sysname, 'MSDASQL'),
  20309.         SRV_PRODUCTNAME = convert(sysname, 'SQL Server 7.0'),
  20310.         SRV_DATASOURCE = convert(nvarchar(255), srvnetname),
  20311.         SRV_LOCATION = convert(nvarchar(255), null)
  20312.     from sysservers
  20313. go
  20314. grant execute on sp_linkedservers to public
  20315. go
  20316.  
  20317.  
  20318. if object_id('sp_catalogs', 'P') is not null
  20319.     drop proc sp_catalogs
  20320. go
  20321. raiserror(15339,-1,-1,'sp_catalogs')
  20322. go
  20323. create procedure sp_catalogs(
  20324.     @server_name        sysname)
  20325. as
  20326.     select
  20327.         CATALOG_NAME,
  20328.         convert (nvarchar(255),DESCRIPTION)
  20329.     from master.dbo.SYSREMOTE_CATALOGS < @server_name >
  20330.     order by CATALOG_NAME
  20331. go
  20332. grant execute on sp_catalogs to public
  20333. go
  20334.  
  20335.  
  20336. if object_id('sp_tables_ex', 'P') is not null
  20337.     drop proc sp_tables_ex
  20338. go
  20339. raiserror(15339,-1,-1,'sp_tables_ex')
  20340. go
  20341. create procedure sp_tables_ex(
  20342.     @table_server        sysname,
  20343.     @table_name            sysname = null,
  20344.     @table_schema        sysname = null,
  20345.     @table_catalog        sysname = null,
  20346.     @table_type            sysname = null)
  20347. as
  20348.     select
  20349.         TABLE_CAT = TABLE_CATALOG,
  20350.         TABLE_SCHEM = TABLE_SCHEMA,
  20351.         TABLE_NAME = TABLE_NAME,
  20352.         TABLE_TYPE = TABLE_TYPE,
  20353.         REMARKS = convert(nvarchar(255),DESCRIPTION)
  20354.     from master.dbo.SYSREMOTE_TABLES <
  20355.                 @table_server,
  20356.                 @table_catalog,
  20357.                 @table_schema,
  20358.                 @table_name,
  20359.                 @table_type >
  20360.     order by TABLE_TYPE, TABLE_CAT, TABLE_SCHEM, TABLE_NAME
  20361. go
  20362. grant execute on sp_tables_ex to public
  20363. go
  20364.  
  20365.  
  20366. if object_id('sp_columns_ex', 'P') is not null
  20367.     drop proc sp_columns_ex
  20368. go
  20369. raiserror(15339,-1,-1,'sp_columns_ex')
  20370. go
  20371. create procedure sp_columns_ex(
  20372.     @table_server        sysname,
  20373.     @table_name            sysname = null,
  20374.     @table_schema        sysname = null,
  20375.     @table_catalog        sysname = null,
  20376.     @column_name        sysname = null)
  20377. as
  20378.     select
  20379.         TABLE_CAT = c.TABLE_CATALOG,
  20380.         TABLE_SCHEM = c.TABLE_SCHEMA,
  20381.         TABLE_NAME = c.TABLE_NAME,
  20382.         COLUMN_NAME = c.COLUMN_NAME,
  20383.         DATA_TYPE = c.DATA_TYPE,
  20384.         TYPE_NAME = p.TYPE_NAME,
  20385.         COLUMN_SIZE = c.NUMERIC_PRECISION,
  20386.         BUFFER_LENGTH = c.CHARACTER_OCTET_LENGTH,
  20387.         DECIMAL_DIGITS = c.NUMERIC_SCALE,
  20388.         NUM_PREC_RADIX = convert(smallint, 10),
  20389.         NULLABLE = convert(smallint, c.IS_NULLABLE),
  20390.         REMARKS = convert(nvarchar(255),c.DESCRIPTION),
  20391.         COLUMN_DEF = c.COLUMN_DEFAULT,
  20392.         SQL_DATA_TYPE = c.DATA_TYPE,
  20393.         SQL_DATETIME_SUB = convert(smallint, c.DATETIME_PRECISION),
  20394.         CHAR_OCTET_LENGTH = c.CHARACTER_OCTET_LENGTH,
  20395.         ORDINAL_POSITION = c.ORDINAL_POSITION,
  20396.         IS_NULLABLE = c.IS_NULLABLE,
  20397.         SS_DATA_TYPE = convert(tinyint, XTypeToTds(c.DATA_TYPE, 0))
  20398.     from master.dbo.SYSREMOTE_COLUMNS <
  20399.                 @table_server,
  20400.                 @table_catalog,
  20401.                 @table_schema,
  20402.                 @table_name,
  20403.                 @column_name > c
  20404.          ,master.dbo.SYSREMOTE_PROVIDER_TYPES <
  20405.                 @table_server,
  20406.                 NULL,           -- DATA_TYPE
  20407.                 NULL    -- 1> p            -- BEST_MATCH
  20408.                         -- UNDONE: BUGBUG: Kagera needs to fix BEST_MATCH so
  20409.                         --  we get only one PROVIDER_TYPES row for each column!
  20410.                 > p
  20411.     where c.DATA_TYPE = p.DATA_TYPE
  20412.     order by TABLE_CAT, TABLE_SCHEM, TABLE_NAME, ORDINAL_POSITION
  20413. go
  20414. grant execute on sp_columns_ex to public
  20415. go
  20416.  
  20417.  
  20418. if object_id('sp_table_privileges_ex', 'P') is not null
  20419.     drop proc sp_table_privileges_ex
  20420. go
  20421. raiserror(15339,-1,-1,'sp_table_privileges_ex')
  20422. go
  20423. create procedure sp_table_privileges_ex(
  20424.     @table_server        sysname,
  20425.     @table_name            sysname = null,
  20426.     @table_schema        sysname = null,
  20427.     @table_catalog        sysname = null)
  20428. as
  20429.     select
  20430.         TABLE_CAT = TABLE_CATALOG,
  20431.         TABLE_SCHEM = TABLE_SCHEMA,
  20432.         TABLE_NAME = TABLE_NAME,
  20433.         GRANTOR = GRANTOR,
  20434.         GRANTEE = GRANTEE,
  20435.         PRIVILEGE = PRIVILEGE_TYPE,
  20436.         IS_GRANTABLE = IS_GRANTABLE
  20437.     from master.dbo.SYSREMOTE_TABLE_PRIVILEGES <
  20438.                 @table_server,
  20439.                 @table_catalog,
  20440.                 @table_schema,
  20441.                 @table_name >
  20442.     order by TABLE_CAT, TABLE_SCHEM, TABLE_NAME, PRIVILEGE
  20443. go
  20444. grant execute on sp_table_privileges_ex to public
  20445. go
  20446.  
  20447.  
  20448. if object_id('sp_column_privileges_ex', 'P') is not null
  20449.     drop proc sp_column_privileges_ex
  20450. go
  20451. raiserror(15339,-1,-1,'sp_column_privileges_ex')
  20452. go
  20453. create procedure sp_column_privileges_ex(
  20454.     @table_server        sysname,
  20455.     @table_name            sysname = null,
  20456.     @table_schema        sysname = null,
  20457.     @table_catalog        sysname = null,
  20458.     @column_name        sysname = null)
  20459. as
  20460.     select
  20461.         TABLE_CAT = TABLE_CATALOG,
  20462.         TABLE_SCHEM = TABLE_SCHEMA,
  20463.         TABLE_NAME = TABLE_NAME,
  20464.         COLUMN_NAME = COLUMN_NAME,
  20465.         GRANTOR = GRANTOR,
  20466.         GRANTEE = GRANTEE,
  20467.         PRIVILEGE = PRIVILEGE_TYPE,
  20468.         IS_GRANTABLE = IS_GRANTABLE
  20469.     from master.dbo.SYSREMOTE_COLUMN_PRIVILEGES <
  20470.                 @table_server,
  20471.                 @table_catalog,
  20472.                 @table_schema,
  20473.                 @table_name,
  20474.                 @column_name >
  20475.     order by TABLE_CAT, TABLE_SCHEM, TABLE_NAME, COLUMN_NAME, PRIVILEGE
  20476. go
  20477. grant execute on sp_column_privileges_ex to public
  20478. go
  20479.  
  20480.  
  20481. if object_id('sp_indexes', 'P') is not null
  20482.     drop proc sp_indexes
  20483. go
  20484. raiserror(15339,-1,-1,'sp_indexes')
  20485. go
  20486. create procedure sp_indexes(
  20487.     @table_server        sysname,
  20488.     @table_name            sysname = null,
  20489.     @table_schema        sysname = null,
  20490.     @table_catalog        sysname = null,
  20491.     @index_name            sysname = null,
  20492.     @is_unique          bit = null)
  20493. as
  20494.     select
  20495.         TABLE_CAT = TABLE_CATALOG,
  20496.         TABLE_SCHEM = TABLE_SCHEMA,
  20497.         TABLE_NAME = TABLE_NAME,
  20498.         NON_UNIQUE = convert(smallint, 1 - [UNIQUE]),
  20499.         INDEX_NAME = INDEX_NAME,
  20500.         INDEX_QUALIFIER = INDEX_SCHEMA,
  20501.         TYPE = TYPE,
  20502.         ORDINAL_POSITION = ORDINAL_POSITION,
  20503.         COLUMN_NAME = COLUMN_NAME,
  20504.         ASC_OR_DESC = [COLLATION],
  20505.         CARDINALITY = CARDINALITY,
  20506.         PAGES = PAGES,
  20507.         FILTER_CONDITION = FILTER_CONDITION
  20508.     from master.dbo.SYSREMOTE_INDEXES <
  20509.                 @table_server,
  20510.                 @table_catalog,
  20511.                 @table_schema,
  20512.                 @index_name,
  20513.                 NULL,           -- TYPE (index type)
  20514.                 @table_name >
  20515.     where @is_unique is null or @is_unique = [UNIQUE]
  20516.     order by NON_UNIQUE, TYPE, INDEX_QUALIFIER, INDEX_NAME, ORDINAL_POSITION
  20517. go
  20518. grant execute on sp_indexes to public
  20519. go
  20520.  
  20521.  
  20522. if object_id('sp_foreignkeys', 'P') is not null
  20523.     drop proc sp_foreignkeys
  20524. go
  20525. raiserror(15339,-1,-1,'sp_foreignkeys')
  20526. go
  20527. create procedure sp_foreignkeys(
  20528.     @table_server        sysname,
  20529.     @pktab_name         sysname = null,
  20530.     @pktab_schema       sysname = null,
  20531.     @pktab_catalog      sysname = null,
  20532.     @fktab_name         sysname = null,
  20533.     @fktab_schema       sysname = null,
  20534.     @fktab_catalog      sysname = null)
  20535. as
  20536.     select
  20537.         PKTABLE_CAT = PK_TABLE_CATALOG,
  20538.         PKTABLE_SCHEM = PK_TABLE_SCHEMA,
  20539.         PKTABLE_NAME = PK_TABLE_NAME,
  20540.         PKCOLUMN_NAME = PK_COLUMN_NAME,
  20541.         FKTABLE_CAT = FK_TABLE_CATALOG,
  20542.         FKTABLE_SCHEM = FK_TABLE_SCHEMA,
  20543.         FKTABLE_NAME = FK_TABLE_NAME,
  20544.         FKCOLUMN_NAME = FK_COLUMN_NAME,
  20545.         KEY_SEQ = ORDINAL,
  20546.         UPDATE_RULE = UPDATE_RULE,
  20547.         DELETE_RULE = DELETE_RULE,
  20548.         FK_NAME = convert(sysname, NULL),
  20549.         PK_NAME = convert(sysname, NULL)
  20550.     from master.dbo.SYSREMOTE_FOREIGN_KEYS <
  20551.                 @table_server,
  20552.                 @pktab_catalog,
  20553.                 @pktab_schema,
  20554.                 @pktab_name,
  20555.                 @fktab_catalog,
  20556.                 @fktab_schema,
  20557.                 @fktab_name >
  20558.     order by FKTABLE_CAT, FKTABLE_SCHEM, FKTABLE_NAME, KEY_SEQ
  20559. go
  20560. grant execute on sp_foreignkeys to public
  20561. go
  20562.  
  20563.  
  20564. if object_id('sp_primarykeys', 'P') is not null
  20565.     drop proc sp_primarykeys
  20566. go
  20567. raiserror(15339,-1,-1,'sp_primarykeys')
  20568. go
  20569. create procedure sp_primarykeys(
  20570.     @table_server        sysname,
  20571.     @table_name         sysname = null,
  20572.     @table_schema       sysname = null,
  20573.     @table_catalog      sysname = null)
  20574. as
  20575.     select
  20576.         TABLE_CAT = TABLE_CATALOG,
  20577.         TABLE_SCHEM = TABLE_SCHEMA,
  20578.         TABLE_NAME = TABLE_NAME,
  20579.         COLUMN_NAME = COLUMN_NAME,
  20580.         KEY_SEQ = ORDINAL,
  20581.         PK_NAME = convert(sysname, NULL)
  20582.     from master.dbo.SYSREMOTE_PRIMARY_KEYS <
  20583.                 @table_server,
  20584.                 @table_catalog,
  20585.                 @table_schema,
  20586.                 @table_name >
  20587.     order by TABLE_CAT, TABLE_SCHEM, TABLE_NAME, KEY_SEQ
  20588. go
  20589. grant execute on sp_primarykeys to public
  20590. go
  20591.  
  20592. ---------------------------- END OLEDB CATALOG PROCS --------------------------
  20593.  
  20594.  
  20595. dump tran master with no_log
  20596. go
  20597.  
  20598.  
  20599.  
  20600.  
  20601. if (exists (select * from sysobjects
  20602.         where name = 'sp_configure' and type = 'P '))
  20603.     begin
  20604.         exec sp_configure 'allow updates',0
  20605.         reconfigure with override
  20606.     end
  20607. go
  20608.  
  20609. exec sp_MS_upd_sysobj_category 2 /* set category | 2 based on crdate. */
  20610.  
  20611. go
  20612.  
  20613. if exists (select * from sysobjects where name = 'sp_check_objects'
  20614.             and type = 'P ')
  20615.     begin
  20616.         /* Only supported on 6.0 servers */
  20617.         print ''
  20618.         print 'Checking objects created by instcat.sql.'
  20619.  
  20620.         exec sp_check_objects 'catalog'
  20621.     end
  20622. go
  20623.  
  20624. print ''
  20625. print 'instcat.sql completed successfully.'
  20626. go
  20627.  
  20628. set quoted_identifier off
  20629. go
  20630.  
  20631. dump tran master with no_log
  20632. go
  20633. checkpoint
  20634. go
  20635. /**/
  20636.