home *** CD-ROM | disk | FTP | other *** search
/ Delphi Anthology / aDELPHI.iso / install / mdac_typ.exe / RCDATA / CABINET / instcat.sql < prev    next >
Text File  |  1999-01-28  |  592KB  |  19,646 lines

  1.  
  2. /*
  3. **    INSTCAT.SQL
  4. **    Installs catalog stored procedures on the Microsoft SQL Server.
  5. **    Copyright 1992-1999, Microsoft Corp.    All rights reserved.
  6. */
  7.  
  8. /*
  9. NOTE:  you MUST change the last row inserted into spt_server_info
  10. to be version number of this file.    the convention is j.nn.bbb, where
  11. j is the major version number ('7' now), nn is the minor version number
  12. ('00' now), and bbb is the build number.
  13. insert into spt_server_info
  14.     values (500, 'SYS_SPROC_VERSION', '7.00.bbb')
  15. */
  16.  
  17. /****************************************************************************/
  18. /* This portion sets up the ability to perform all the functions in this    */
  19. /* script                                                                    */
  20. /****************************************************************************/
  21. go
  22. use master
  23. go
  24. dump tran master with no_log
  25. go
  26.  
  27. set quoted_identifier on
  28. go
  29.  
  30. if (exists (select * from sysobjects
  31.         where name = 'sp_configure' and type = 'P '))
  32. begin
  33.     execute sp_configure 'update',1
  34. end
  35. reconfigure with override
  36. go
  37.  
  38. exec sp_MS_upd_sysobj_category 1 /*Capture datetime for use below.*/
  39.  
  40. go
  41.  
  42. /*
  43. ** If old versions of tables exist, drop them.
  44. */
  45. if (exists (select * from sysobjects
  46.         where name = 'MSdatatype_info' and type = 'U '))
  47.     drop table MSdatatype_info
  48. go
  49. if (exists (select * from sysobjects
  50.         where name = 'MSdatatype_info_ext' and type = 'U '))
  51.     drop table MSdatatype_info_ext
  52. go
  53. if (exists (select * from sysobjects
  54.         where name = 'MStable_types' and type = 'U '))
  55.     drop table MStable_types
  56. go
  57. if (exists (select * from sysobjects
  58.         where name = 'MSserver_info' and type = 'U '))
  59.     drop table MSserver_info
  60. go
  61. if (exists (select * from sysobjects
  62.         where name = 'spt_table_types' and type = 'U '))
  63.     drop table spt_table_types
  64. go
  65. /*
  66. ** If tables or procs already exist, drop them.
  67. */
  68.  
  69. if (exists (select * from sysobjects
  70.         where name = 'spt_datatype_info' and type = 'U '))
  71.     drop table spt_datatype_info
  72. go
  73. if (exists (select * from sysobjects
  74.         where name = 'spt_datatype_info_ext' and type = 'U '))
  75.     drop table spt_datatype_info_ext
  76. go
  77. if (exists (select * from sysobjects
  78.         where name = 'sp_add_server_sortinfo' and type = 'P '))
  79.     drop proc sp_add_server_sortinfo
  80. go
  81. if (exists (select * from sysobjects
  82.         where name = 'spt_server_info' and type = 'U '))
  83.     drop table spt_server_info
  84. go
  85. if (exists (select * from sysobjects
  86.         where name = 'sp_tables' and type = 'P '))
  87.     drop proc sp_tables
  88. go
  89. if (exists (select * from sysobjects
  90.         where name = 'sp_statistics' and type = 'P '))
  91.     drop proc sp_statistics
  92. go
  93. if (exists (select * from sysobjects
  94.         where name = 'sp_columns' and type = 'P '))
  95.     drop proc sp_columns
  96. go
  97. if (exists (select * from sysobjects
  98.         where name = 'sp_fkeys' and type = 'P '))
  99.     drop proc sp_fkeys
  100. go
  101. if (exists (select * from sysobjects
  102.         where name = 'sp_pkeys' and type = 'P '))
  103.     drop proc sp_pkeys
  104. dump tran master with no_log
  105. go
  106.  
  107. go
  108. if (exists (select * from sysobjects
  109.         where name = 'sp_stored_procedures' and type = 'P '))
  110.     drop proc sp_stored_procedures
  111. go
  112. if (exists (select * from sysobjects
  113.         where name = 'sp_sproc_columns' and type = 'P '))
  114.     drop proc sp_sproc_columns
  115. go
  116. if (exists (select * from sysobjects
  117.         where name = 'sp_table_privileges' and type = 'P '))
  118.     drop proc sp_table_privileges
  119. go
  120. if (exists (select * from sysobjects
  121.         where name = 'sp_column_privileges' and type = 'P '))
  122.     drop proc sp_column_privileges
  123. go
  124. dump tran master with no_log
  125. go
  126. if (exists (select * from sysobjects
  127.         where name = 'sp_server_info' and type = 'P '))
  128.     drop proc sp_server_info
  129. go
  130. if (exists (select * from sysobjects
  131.         where name = 'sp_datatype_info' and type = 'P '))
  132.     drop proc sp_datatype_info
  133. go
  134. if (exists (select * from sysobjects
  135.         where name = 'sp_special_columns' and type = 'P '))
  136.     drop proc sp_special_columns
  137. go
  138. if (exists (select * from sysobjects
  139.         where name = 'sp_databases' and type = 'P '))
  140.     drop proc sp_databases
  141. go
  142. dump tran master with no_log
  143. go
  144. if (exists (select * from sysobjects
  145.         where name = 'sp_ddopen' and type = 'P '))
  146.     drop proc sp_ddopen
  147. go
  148. if (exists (select * from sysobjects
  149.         where name = 'sp_tableswc' and type = 'P '))
  150.     drop proc sp_tableswc
  151. go
  152.  
  153. dump tran master with no_log
  154. go
  155.  
  156.  
  157. if (exists (select * from sysobjects
  158.         where name = 'spt_provider_types' and type = 'U '))
  159.     begin
  160.     drop table spt_provider_types
  161.     dump tran master with no_log
  162.     end
  163. go
  164. if (exists (select * from sysobjects
  165.         where name = 'sp_catalogs_rowset' and type = 'P '))
  166.     begin
  167.     drop procedure sp_catalogs_rowset
  168.     dump tran master with no_log
  169.     end
  170. go
  171. if (exists (select * from sysobjects
  172.         where name = 'sp_column_privileges_rowset' and type = 'P '))
  173.     begin
  174.     drop procedure sp_column_privileges_rowset
  175.     dump tran master with no_log
  176.     end
  177. go
  178. if (exists (select * from sysobjects
  179.         where name = 'sp_columns_rowset' and type = 'P '))
  180.     begin
  181.     drop procedure sp_columns_rowset
  182.     dump tran master with no_log
  183.     end
  184. go
  185. if (exists (select * from sysobjects
  186.         where name = 'sp_foreign_keys_rowset' and type = 'P '))
  187.     begin
  188.     drop procedure sp_foreign_keys_rowset
  189.     dump tran master with no_log
  190.     end
  191. go
  192. if (exists (select * from sysobjects
  193.         where name = 'sp_indexes_rowset' and type = 'P '))
  194.     begin
  195.     drop procedure sp_indexes_rowset
  196.     dump tran master with no_log
  197.     end
  198. go
  199. if (exists (select * from sysobjects
  200.         where name = 'sp_primary_keys_rowset' and type = 'P '))
  201.     begin
  202.     drop procedure sp_primary_keys_rowset
  203.     dump tran master with no_log
  204.     end
  205. go
  206. if (exists (select * from sysobjects
  207.         where name = 'sp_procedure_params_rowset' and type = 'P '))
  208.     begin
  209.     drop procedure sp_procedure_params_rowset
  210.     dump tran master with no_log
  211.     end
  212. go
  213. if (exists (select * from sysobjects
  214.         where name = 'sp_procedures_rowset' and type = 'P '))
  215.     begin
  216.     drop procedure sp_procedures_rowset
  217.     dump tran master with no_log
  218.     end
  219. go
  220. if (exists (select * from sysobjects
  221.         where name = 'sp_provider_types_rowset' and type = 'P '))
  222.     begin
  223.     drop procedure sp_provider_types_rowset
  224.     dump tran master with no_log
  225.     end
  226. go
  227. if (exists (select * from sysobjects
  228.         where name = 'sp_schemata_rowset' and type = 'P '))
  229.     begin
  230.     drop procedure sp_schemata_rowset
  231.     dump tran master with no_log
  232.     end
  233. go
  234. if (exists (select * from sysobjects
  235.         where name = 'sp_statistics_rowset' and type = 'P '))
  236.     begin
  237.     drop procedure sp_statistics_rowset
  238.     dump tran master with no_log
  239.     end
  240. go
  241. if (exists (select * from sysobjects
  242.         where name = 'sp_tables_rowset' and type = 'P '))
  243.     begin
  244.     drop procedure sp_tables_rowset
  245.     dump tran master with no_log
  246.     end
  247. go
  248. if (exists (select * from sysobjects
  249.         where name = 'sp_tables_info_rowset' and type = 'P '))
  250.     begin
  251.     drop procedure sp_tables_info_rowset
  252.     dump tran master with no_log
  253.     end
  254. go
  255. if (exists (select * from sysobjects
  256.         where name = 'sp_table_constraints_rowset' and type = 'P '))
  257.     begin
  258.     drop proc sp_table_constraints_rowset
  259.     dump tran master with no_log
  260.     end
  261. go
  262. if (exists (select * from sysobjects
  263.         where name = 'sp_table_privileges_rowset' and type = 'P '))
  264.     begin
  265.     drop proc sp_table_privileges_rowset
  266.     dump tran master with no_log
  267.     end
  268. go
  269. if (exists (select * from sysobjects
  270.         where name = 'sp_linkedservers_rowset' and type = 'P '))
  271.     begin
  272.     drop proc sp_linkedservers_rowset
  273.     dump tran master with no_log
  274.     end
  275. go
  276.  
  277.  
  278. print 'creating table spt_datatype_info_ext'
  279. go
  280.  
  281. if (charindex('6.00', @@version) = 0 and
  282.     charindex('6.50', @@version) = 0 and
  283.     charindex('7.00', @@version) = 0)
  284. begin    /*    Pre 6.0 server */
  285.     print ''
  286.     print ''
  287.     print 'Warning:'
  288.     print 'you are installing the stored procedures '
  289.     print 'on a pre 6.0 SQL Server.'
  290.     print 'Ignore the following error.'
  291.     create table spt_datatype_info_ext (
  292.                 user_type        smallint    not null,
  293.                 CREATE_PARAMS    varchar(32) null,
  294.                 AUTO_INCREMENT    smallint null,
  295.                 typename        varchar(32))
  296. end
  297. go
  298. if (charindex('6.00', @@version) > 0 or
  299.     charindex('6.50', @@version) > 0 or
  300.     charindex('7.00', @@version) > 0)
  301. begin    /* 6.0 or later server */
  302.     create table spt_datatype_info_ext (
  303.                 user_type        smallint    not null,
  304.                 CREATE_PARAMS    varchar(32) null,
  305.                 AUTO_INCREMENT    smallint null,
  306.                 typename        sysname)    /* from systypes, to avoid xusertype hard-code */
  307. end
  308. go
  309.  
  310. grant select on spt_datatype_info_ext to public
  311. go
  312.  
  313.  
  314. insert into spt_datatype_info_ext
  315.     /* CHAR      user_type, create_params, auto_increment */
  316.     values             (1,    'length' ,0, 'char')
  317.  
  318. insert into spt_datatype_info_ext
  319.     /* VARCHAR     user_type, create_params, auto_increment */
  320.     values             (2,    'max length' ,0, 'varchar')
  321.  
  322. insert into spt_datatype_info_ext
  323.     /* BINARY     user_type, create_params, auto_increment */
  324.     values             (3,    'length' ,0, 'binary')
  325.  
  326. insert into spt_datatype_info_ext
  327.     /* VARBINARY user_type, create_params, auto_increment */
  328.     values             (4,    'max length' ,0, 'varbinary')
  329.  
  330. if    (charindex('6.00', @@version) > 0 or
  331.      charindex('6.50', @@version) > 0 or
  332.      charindex('7.00', @@version) > 0)
  333. begin    /*    Add 6.0 data types */
  334.     insert into spt_datatype_info_ext
  335.         /* DECIMAL user_type, create_params, auto_increment */
  336.         values             (26,    'precision,scale' ,0, 'decimal')
  337.  
  338.     insert into spt_datatype_info_ext
  339.         /* NUMERIC user_type, create_params, auto_increment */
  340.         values             (25,    'precision,scale' ,0, 'numeric')
  341.  
  342.     insert into spt_datatype_info_ext
  343.         /* DECIMAL IDENTITY user_type, create_params, auto_increment */
  344.         values             (26,    'precision' ,1, 'decimal')
  345.  
  346.     insert into spt_datatype_info_ext
  347.         /* NUMERIC IDENTITY user_type, create_params, auto_increment */
  348.         values             (25,    'precision' ,1, 'numeric')
  349.  
  350. end
  351. else    /*    Pre 6.0 server, add SYSNAME create param */
  352.     begin
  353.         insert into spt_datatype_info_ext
  354.             /* SYSNAME     user_type, create_param, auto_increments */
  355.             values             (18,    'max length' ,0, 'sysname')
  356.  
  357.     end
  358. go
  359.  
  360. if (charindex('7.00', @@version) = 0)
  361. begin
  362.     print ''
  363.     print ''
  364.     print 'Warning:'
  365.     print 'you are installing the stored procedures '
  366.     print 'on a pre 7.0 SQL Server.'
  367.     print 'Ignore the following errors.'
  368. end
  369. go
  370. if (charindex('7.00', @@version) > 0)
  371. begin    /*    Update usertypes for 7.00 server */
  372.     begin tran
  373.     insert into spt_datatype_info_ext
  374.         /* NCHAR     user_type, create_params, auto_increment */
  375.         values             (0,    'length' ,0, 'nchar')
  376.  
  377.     insert into spt_datatype_info_ext
  378.         /* NVARCHAR     user_type, create_params, auto_increment */
  379.         values             (0,    'max length' ,0, 'nvarchar')
  380.  
  381.     /* SET user_type TO SPHINX VALUES */
  382.     update spt_datatype_info_ext set user_type = xusertype
  383.         from spt_datatype_info_ext e, systypes t where t.name = e.typename
  384.  
  385.     commit tran
  386. end
  387. go
  388.  
  389. create unique clustered index datatypeinfoextclust on spt_datatype_info_ext(user_type,AUTO_INCREMENT)
  390. go
  391.  
  392. print 'creating table spt_datatype_info'
  393. go
  394. if (charindex('6.00', @@version) = 0 and
  395.     charindex('6.50', @@version) = 0 and
  396.     charindex('7.00', @@version) = 0)
  397. begin    /*    Pre 6.0 server */
  398.     print ''
  399.     print ''
  400.     print 'Warning:'
  401.     print 'you are installing the stored procedures '
  402.     print 'on a pre 6.0 SQL Server.'
  403.     print 'Ignore the following error.'
  404.     create table spt_datatype_info (
  405.         ss_dtype            tinyint     not null,
  406.         fixlen                int         null,        /* datatype len for variable, else null */
  407.         ODBCVer             tinyint     null,        /* version if needed, else null */
  408.         TYPE_NAME            varchar(32)    not null,
  409.         DATA_TYPE            smallint    not null,
  410.         data_precision        int         null,
  411.         numeric_scale        smallint    null,    /* min scale if 6.0 */
  412.         RADIX                smallint    null,
  413.         length                int         null,
  414.         LITERAL_PREFIX        varchar(32) null,
  415.         LITERAL_SUFFIX        varchar(32) null,
  416.         CREATE_PARAMS        varchar(32) null,
  417.         NULLABLE            smallint    not null,
  418.         CASE_SENSITIVE        smallint    not null,
  419.         SEARCHABLE            smallint    not null,
  420.         UNSIGNED_ATTRIBUTE    smallint    null,
  421.         MONEY                smallint    not null,
  422.         AUTO_INCREMENT        smallint    null,
  423.         LOCAL_TYPE_NAME     varchar(32)    null,
  424.         charbin             tinyint     null, /* 0 for char/binary types, NULL for all others */
  425.         SQL_DATA_TYPE        smallint    not null,
  426.         SQL_DATETIME_SUB    smallint    null)
  427. end
  428. go
  429. if (charindex('6.00', @@version) > 0 or
  430.     charindex('6.50', @@version) > 0 or
  431.     charindex('7.00', @@version) > 0)
  432. begin    /* 6.0 or later server */
  433.     create table spt_datatype_info (
  434.         ss_dtype            tinyint     not null,
  435.         fixlen                int         null,        /* datatype len for variable, else null */
  436.         ODBCVer             tinyint     null,        /* version if needed, else null */
  437.         TYPE_NAME            sysname     not null,
  438.         DATA_TYPE            smallint    not null,
  439.         data_precision        int         null,
  440.         numeric_scale        smallint    null,    /* min scale if 6.0 */
  441.         RADIX                smallint    null,
  442.         length                int         null,
  443.         LITERAL_PREFIX        varchar(32) null,
  444.         LITERAL_SUFFIX        varchar(32) null,
  445.         CREATE_PARAMS        varchar(32) null,
  446.         NULLABLE            smallint    not null,
  447.         CASE_SENSITIVE        smallint    not null,
  448.         SEARCHABLE            smallint    not null,
  449.         UNSIGNED_ATTRIBUTE    smallint    null,
  450.         MONEY                smallint    not null,
  451.         AUTO_INCREMENT        smallint    null,
  452.         LOCAL_TYPE_NAME     sysname     null,
  453.         charbin             tinyint     null, /* 0 for char/binary types, NULL for all others */
  454.         SQL_DATA_TYPE        smallint    not null,
  455.         SQL_DATETIME_SUB    smallint    null)
  456. end
  457. go
  458.  
  459. grant select on spt_datatype_info to public
  460. go
  461.  
  462. /* Get case sensitivity */
  463. if 'A' = 'A' /* create dummy begin block */
  464. begin
  465.     declare @case smallint
  466.  
  467.     begin tran
  468.     select @case = 0
  469.     select @case = 1 where 'a' <> 'A'
  470.  
  471.     /* Local Binary */
  472.     insert into spt_datatype_info values
  473.     (45,null,null,'binary',-2,null,null,null,null,'0x',null,'length',1,0,2,null,0,null,'binary',0,-2,null)
  474.  
  475.     /* Local Bit */
  476.     insert into spt_datatype_info values
  477.     (50,null,null,'bit',-7,1,0,null,1,null,null,null,0,0,2,null,0,null,'bit',null,-7,null)
  478.  
  479.     /* Local Char */
  480.     insert into spt_datatype_info values
  481.     (47,null,null,'char',1,null,null,null,null,'''','''','length',1,@case,3,null,0,null,'char',0,1,null)
  482.  
  483.     /* Local Datetime */
  484.     insert into spt_datatype_info values
  485.     (61,8,2,'datetime',11,23,3,NULL,16,'''','''',null,1,0,3,null,0,null,'datetime',null,9,3)
  486.     insert into spt_datatype_info values
  487.     (61,8,3,'datetime',93,23,3,NULL,16,'''','''',null,1,0,3,null,0,null,'datetime',null,9,3)
  488.  
  489.     /* Local Smalldatetime */
  490.     insert into spt_datatype_info values
  491.     (58,4,2,'smalldatetime',11,16,0,NULL,16,'''','''',null,1,0,3,null,0,null,'smalldatetime',null,9,3)
  492.     insert into spt_datatype_info values
  493.     (58,4,3,'smalldatetime',93,16,0,NULL,16,'''','''',null,1,0,3,null,0,null,'smalldatetime',null,9,3)
  494.  
  495.     /* Local Float */
  496.     insert into spt_datatype_info values
  497.     (62,8,2,'float',6,15,null,10,8,null,null,null,1,0,2,0,0,0,'float',null,6,null)
  498.     insert into spt_datatype_info values
  499.     (62,8,3,'float',6,53,null, 2,8,null,null,null,1,0,2,0,0,0,'float',null,6,null)
  500.  
  501.     /* Local Real */
  502.     insert into spt_datatype_info values
  503.     (59,4,2,'real',7, 7,null,10,4,null,null,null,1,0,2,0,0,0,'real',null,7,null)
  504.     insert into spt_datatype_info values
  505.     (59,4,3,'real',7,24,null, 2,4,null,null,null,1,0,2,0,0,0,'real',null,7,null)
  506.  
  507.     /* Local Smallmoney */
  508.     insert into spt_datatype_info values
  509.     (122,4,null,'smallmoney',3,10,4,10,12,'$',null,null,1,0,2,0,1,0,'smallmoney',null,3,null)
  510.  
  511.     /* Local Money */
  512.     insert into spt_datatype_info values
  513.     (60,8,null,'money',3,19,4,10,21,'$',null,null,1,0,2,0,1,0,'money',null,3,null)
  514.  
  515.     /* Local Int */
  516.     insert into spt_datatype_info values
  517.     (56,4,null,'int',4,10,0,10,4,null,null,null,1,0,2,0,0,0,'int',null,4,null)
  518.  
  519.     commit tran
  520. end
  521. go
  522.  
  523. if 'A' = 'A' /* create dummy begin block */
  524. begin
  525.     declare @case smallint
  526.  
  527.     begin tran
  528.     select @case = 0
  529.     select @case = 1 where 'a' <> 'A'
  530.  
  531.  
  532.     /* Local Smallint */
  533.     insert into spt_datatype_info values
  534.     (52,2,null,'smallint',5,5,0,10,2,null,null,null,1,0,2,0,0,0,'smallint',null,5,null)
  535.     insert into spt_datatype_info values
  536.     (52,2,1,'smallint',5,5,0,10,2,null,null,null,1,0,2,0,0,0,'smallint',null,5,null)
  537.  
  538.     /* Local Tinyint */
  539.     insert into spt_datatype_info values
  540.     (48,1,null,'tinyint',-6,3,0,10,1,null,null,null,1,0,2,1,0,0,'tinyint',null,-6,null)
  541.  
  542.     /* Local Text */
  543.     insert into spt_datatype_info values
  544.     (35,null,null,'text',-1,2147483647,null,null,2147483647,'''','''',null,1,@case,1,null,0,null,'text',0,-1,null)
  545.  
  546.     /* Local Varbinary */
  547.     insert into spt_datatype_info values
  548.     (37,null,null,'varbinary',-3,null,null,null,null,'0x',null,'max length',1,0,2,null,0,null,'varbinary',0,-3,null)
  549.  
  550.     /* Local Varchar */
  551.     insert into spt_datatype_info values
  552.     (39,null,null,'varchar',12,null,null,null,null,'''','''','max length',1,@case,3,null,0,null,'varchar',0,12,null)
  553.  
  554.     /* Local Image */
  555.     insert into spt_datatype_info values
  556.     (34,null,null,'image',-4,2147483647,null,null,2147483647,'0x',null,null,1,0,0,null,0,null,'image',0,-4,null)
  557.  
  558.     if (charindex('6.00', @@version) > 0 or
  559.         charindex('6.50', @@version) > 0 or
  560.         charindex('7.00', @@version) > 0)
  561.     begin    /*    Add 6.0 data types */
  562.         /* Local Decimal */
  563.         insert into spt_datatype_info values    /* sql server type is 'decimaln' */
  564.         (55,null,null,'decimal',3,38,0,10,null,null,null,'precision,scale',1,0,2,0,0,0,'decimal',null,3,null)
  565.  
  566.         /* Local Numeric */
  567.         insert into spt_datatype_info values    /* sql server type is 'numericn' */
  568.         (63,null,null,'numeric',2    ,38,0,10,null,null,null,'precision,scale',1,0,2,0,0,0,'numeric',null,2,null)
  569.  
  570.         /* Identity attribute data types */
  571.  
  572.         /* Identity Int */
  573.         insert into spt_datatype_info values
  574.         (56,null,null,'int identity',4,10,0,10,null,null,null,null,0,0,2,0,0,1,'int identity',null,4,null)
  575.  
  576.         /* Identity Smallint */
  577.         insert into spt_datatype_info values
  578.         (52,null,null,'smallint identity',5,5,0,10,null,null,null,null,0,0,2,0,0,1,'smallint identity',null,5,null)
  579.  
  580.         /* Identity Tinyint */
  581.         insert into spt_datatype_info values
  582.         (48,null,null,'tinyint identity',-6,3,0,10,null,null,null,null,0,0,2,1,0,1,'tinyint identity',null,-6,null)
  583.  
  584.         /* Identity Numeric */
  585.         insert into spt_datatype_info values    /* sql server type is 'decmaln' */
  586.         (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)
  587.  
  588.         /* Identity Numeric */
  589.         insert into spt_datatype_info values    /* sql server type is 'decmaln' */
  590.         (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)
  591.  
  592.     end
  593.  
  594.     if (charindex('7.00', @@version) = 0)
  595.     begin    /*    Add nullable type for non-Sphinx server */
  596.         /* Local Datetimn */
  597.         insert into spt_datatype_info values
  598.         (111,4,2,'smalldatetime',11,16,0,10,16,'''','''',null,1,0,3,null,0,null,'smalldatetime',null,9,3)
  599.         insert into spt_datatype_info values
  600.         (111,4,3,'smalldatetime',93,16,0,10,16,'''','''',null,1,0,3,null,0,null,'smalldatetime',null,9,3)
  601.         insert into spt_datatype_info values /* sql server type is 'datetimn' */
  602.         (111,8,2,'datetime',11,23,3,10,16,'''','''',null,1,0,3,null,0,null,'datetime',null,9,3)
  603.         insert into spt_datatype_info values
  604.         (111,8,3,'datetime',93,23,3,10,16,'''','''',null,1,0,3,null,0,null,'datetime',null,9,3)
  605.  
  606.         /* Local Floatn */
  607.         insert into spt_datatype_info values /* sql server type is 'floatn' */
  608.         (109,4,2,'real',7, 7,null,10,4,null,null,null,1,0,2,0,0,0,'real',null,7,null)
  609.         insert into spt_datatype_info values
  610.         (109,4,3,'real',7,24,null, 2,4,null,null,null,1,0,2,0,0,0,'real',null,7,null)
  611.         insert into spt_datatype_info values /* sql server type is 'floatn' */
  612.         (109,8,2,'float',6,15,null,10,8,null,null,null,1,0,2,0,0,0,'float',null,6,null)
  613.         insert into spt_datatype_info values
  614.         (109,8,3,'float',6,53,null, 2,8,null,null,null,1,0,2,0,0,0,'float',null,6,null)
  615.  
  616.         /* Local Moneyn */
  617.         insert into spt_datatype_info values    /* sql server type is 'moneyn' */
  618.         (110,4,null,'smallmoney',3,10,4,10,12,'$',null,null,1,0,2,0,1,0,'smallmoney',null,3,null)
  619.         insert into spt_datatype_info values    /* sql server type is 'moneyn' */
  620.         (110,8,null,'money',3,19,4,10,21,'$',null,null,1,0,2,0,1,0,'money',null,3,null)
  621.  
  622.         /* Local Intn */
  623.         insert into spt_datatype_info values /* sql server type is 'intn' */
  624.         (38,4,null,'int',4,10,0,10,4,null,null,null,1,0,2,0,0,0,'int',null,4,null)
  625.         insert into spt_datatype_info values /* sql server type is 'intn' */
  626.         (38,2,null,'smallint',5,5,0,10,2,null,null,null,1,0,2,0,0,0,'smallint',null,5,null)
  627.         insert into spt_datatype_info values
  628.         (38,1,null,'tinyint',-6,3,0,10,1,null,null,null,1,0,2,1,0,0,'tinyint',null,-6,null)
  629.  
  630.         if (charindex('6.00', @@version) > 0 or
  631.             charindex('6.50', @@version) > 0 or
  632.             charindex('7.00', @@version) > 0)
  633.         begin    /*    Add 6.0 data types */
  634.             /* Local Decimaln */
  635.             insert into spt_datatype_info values    /* sql server type is 'decimaln' */
  636.             (106,null,null,'decimal',3,38,0,10,null,null,null,'precision,scale',1,0,2,0,0,0,'decimal',null,3,null)
  637.             insert into spt_datatype_info values    /* sql server type is 'decmaln' */
  638.             (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)
  639.  
  640.             /* Local Numericn */
  641.             insert into spt_datatype_info values    /* sql server type is 'numericn' */
  642.             (108,null,null,'numeric',2,38,0,10,null,null,null,'precision,scale',1,0,2,0,0,0,'numeric',null,2,null)
  643.             insert into spt_datatype_info values    /* sql server type is 'decmaln' */
  644.             (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)
  645.         end
  646.     end
  647.  
  648.     commit tran
  649. end
  650. go
  651.  
  652. if (charindex('7.00', @@version) = 0)
  653. begin
  654.     print ''
  655.     print ''
  656.     print 'Warning:'
  657.     print 'you are installing the stored procedures '
  658.     print 'on a pre 7.0 SQL Server.'
  659.     print 'Ignore the following errors.'
  660. end
  661. go
  662. if (charindex('7.00', @@version) > 0)
  663.     begin
  664.         declare @ncase smallint
  665.  
  666.         select @ncase = 0
  667.         select @ncase = 1 where N'a' <> N'A'
  668.  
  669.         /* Local Timestamp */
  670.         insert into spt_datatype_info values
  671.         (0,null,null,'timestamp',-2,8,null,null,null,'0x',null,null,1,0,2,null,0,null,'timestamp',0,-2,null)
  672.  
  673.         /* Local GUID */
  674.         insert into spt_datatype_info values
  675.         (0,null,null,'uniqueidentifier',-11,36,null,null,null,'''','''',null,1,0,2,null,0,null,'uniqueidentifier',NULL,-11,null)
  676.  
  677.         /* Local NChar */
  678.         insert into spt_datatype_info values
  679.         (0,null,null,'nchar',-8,null,null,null,null,'N''','''','length',1,@ncase,3,null,0,null,'nchar',0,-8,null)
  680.  
  681.         /* Local NVarchar */
  682.         insert into spt_datatype_info values
  683.         (0,null,null,'nvarchar',-9,null,null,null,null,'N''','''','max length',1,@ncase,3,null,0,null,'nvarchar',0,-9,null)
  684.  
  685.         /* Local NText */
  686.         insert into spt_datatype_info values
  687.         (0,null,null,'ntext',-10,2147483646,null,null,2147483646,'N''','''',null,1,@ncase,1,null,0,null,'ntext',0,-10,null)
  688.  
  689.         update spt_datatype_info set NULLABLE = 1
  690.             where TYPE_NAME = 'bit'
  691.  
  692.         update spt_datatype_info set ss_dtype = (select xtype from systypes
  693.             where TYPE_NAME like name+'%')
  694.     end
  695. go
  696.  
  697. create unique clustered index datatypeinfoclust on spt_datatype_info(ss_dtype,fixlen,ODBCVer,AUTO_INCREMENT)
  698. go
  699.  
  700. dump tran master with no_log
  701. go
  702.  
  703. print 'creating table spt_server_info'
  704. go
  705. create table spt_server_info (
  706.               attribute_id        int NOT NULL,
  707.               attribute_name    varchar(60) NOT NULL,
  708.               attribute_value    varchar(255) NOT NULL)
  709. go
  710.  
  711. create unique clustered index serverinfoclust on spt_server_info(attribute_id)
  712. go
  713.  
  714. if (charindex('7.00', @@version) = 0)
  715.     begin
  716.         drop procedure sp_add_server_sortinfo    /* not used by older servers */
  717.         dump tran master with no_log
  718.     end
  719. go
  720.  
  721.  
  722. insert into spt_server_info
  723.     values (1,'DBMS_NAME','Microsoft SQL Server')
  724. insert into spt_server_info
  725.     values (2,'DBMS_VER',@@version)
  726. insert into spt_server_info
  727.     values (10,'OWNER_TERM','owner')
  728. insert into spt_server_info
  729.     values (11,'TABLE_TERM','table')
  730. insert into spt_server_info
  731.     values (12,'MAX_OWNER_NAME_LENGTH','30')
  732. insert into spt_server_info
  733.     values (13,'TABLE_LENGTH','30')
  734. insert into spt_server_info
  735.     values (14,'MAX_QUAL_LENGTH','30')
  736. insert into spt_server_info
  737.     values (15,'COLUMN_LENGTH','30')
  738. if 'A' = 'a' /* If not case sensitive server */
  739. begin
  740.     insert into spt_server_info
  741.         values (16,'IDENTIFIER_CASE','MIXED')
  742. end
  743. else
  744. begin
  745.     insert into spt_server_info
  746.         values (16,'IDENTIFIER_CASE','SENSITIVE')
  747. end
  748. insert into spt_server_info
  749.     values (17,'TX_ISOLATION','2')
  750. if (charindex('6.00', @@version) > 0 or
  751.     charindex('6.50', @@version) > 0 or
  752.     charindex('7.00', @@version) > 0)
  753. begin    /*    Add 6.0 collation sequence */
  754.     insert into spt_server_info
  755.         select 18,'COLLATION_SEQ',
  756.             'charset='+t2.name+' sort_order='+t1.name
  757.             +' charset_num='+rtrim(convert(char(4),t1.csid))+
  758.             ' sort_order_num='+rtrim(convert(char(4),t1.id))
  759.         from syscharsets t1, syscharsets t2, sysconfigures t3
  760.         where t1.csid=t2.id and t1.id=t3.value and t3.config=1123
  761. end
  762. else
  763. begin    /*    Add 4.2x collation sequence */
  764.     insert into spt_server_info
  765.         select 18,'COLLATION_SEQ',
  766.             'charset='+t2.name+' sort_order='+t1.name
  767.             +' charset_num='+rtrim(convert(char(4),t1.csid))+
  768.             ' sort_order_num='+rtrim(convert(char(4),t1.id))
  769.         from syscharsets t1, syscharsets t2, sysconfigures t3
  770.         where t1.csid=t2.id and t1.id=t3.value and t3.config=123
  771. end
  772. insert into spt_server_info
  773.     values (19,'SAVEPOINT_SUPPORT','Y')
  774. insert into spt_server_info
  775.     values (20,'MULTI_RESULT_SETS','Y')
  776. insert into spt_server_info
  777.     values (22,'ACCESSIBLE_TABLES','Y')
  778. go
  779.  
  780. insert into spt_server_info
  781.     values (100,'USERID_LENGTH','30')
  782. insert into spt_server_info
  783.     values (101,'QUALIFIER_TERM','database')
  784. insert into spt_server_info
  785.     values (102,'NAMED_TRANSACTIONS','Y')
  786. insert into spt_server_info
  787.     values (103,'SPROC_AS_LANGUAGE','Y')
  788. insert into spt_server_info
  789.     values (104,'ACCESSIBLE_SPROC','Y')
  790. insert into spt_server_info
  791.     values (105,'MAX_INDEX_COLS','16')
  792. insert into spt_server_info
  793.     values (106,'RENAME_TABLE','Y')
  794. insert into spt_server_info
  795.     values (107,'RENAME_COLUMN','Y')
  796. insert into spt_server_info
  797.     values (108,'DROP_COLUMN','N')
  798. insert into spt_server_info
  799.     values (109,'INCREASE_COLUMN_LENGTH','N')
  800. if (charindex('6.50', @@version) = 0 and
  801.     charindex('7.00', @@version) = 0)
  802. begin
  803.     insert into spt_server_info
  804.         values (110,'DDL_IN_TRANSACTION','N')
  805. end
  806. else
  807. begin
  808.     insert into spt_server_info
  809.         values (110,'DDL_IN_TRANSACTION','Y')
  810. end
  811. insert into spt_server_info
  812.     values (111,'DESCENDING_INDEXES','N')
  813. insert into spt_server_info
  814.     values (112,'SP_RENAME','Y')
  815. insert into spt_server_info
  816.     values (113,'REMOTE_SPROC','Y')
  817. insert into spt_server_info
  818.     values (500,'SYS_SPROC_VERSION','7.00.624')
  819. go
  820.  
  821. if (charindex('7.00', @@version) > 0)
  822. begin    /*    Update values for 7.00 server */
  823.     update spt_server_info set attribute_value = '128'
  824.         where attribute_id in (12,13,14,15,100)
  825. end
  826. go
  827.  
  828. grant select on spt_server_info to public
  829. go
  830.  
  831. print 'creating sp_column_privileges'
  832. go
  833.  
  834. /*    Procedure for pre 6.50 server */
  835. CREATE PROCEDURE sp_column_privileges (
  836.             @table_name         varchar(32),
  837.             @table_owner        varchar(32) = null,
  838.             @table_qualifier    varchar(32) = null,
  839.             @column_name        varchar(96) = null)    /* 3*32 */
  840. as
  841.  
  842.     set nocount on
  843.     declare @table_id     int
  844.     DECLARE @full_table_name    varchar(65)    /* 2*32 + 1 */
  845.     declare @low smallint                     /* range of userids to check */
  846.     declare @high smallint
  847.     declare @owner_uid smallint
  848.  
  849.     select @low = 0, @high = 32767
  850.  
  851.     if @column_name is null /*    If column name not supplied, match all */
  852.         select @column_name = '%'
  853.  
  854.     if @table_qualifier is not null
  855.     begin
  856.         if db_name() <> @table_qualifier
  857.         begin    /* If qualifier doesn't match current database */
  858.             raiserror 20001 '~~Rush_5~~'
  859.             return
  860.         end
  861.     end
  862.     if @table_owner is null
  863.     begin    /* If unqualified table name */
  864.         SELECT @full_table_name = @table_name
  865.     end
  866.     else
  867.     begin    /* Qualified table name */
  868.         SELECT @full_table_name = @table_owner + '.' + @table_name
  869.     end
  870.     /*    Get Object ID */
  871.     select @table_id = object_id(@full_table_name)
  872.  
  873.     if (@@trancount <> 0)
  874.     begin    /* If inside a transaction */
  875.         raiserror 20003 '~~Rush_6~~'
  876.         return
  877.     end
  878.  
  879.     /*
  880.     ** We need to create a table which will contain a row for every row to
  881.     ** be returned to the client.
  882.     */
  883.  
  884.     create table #column_priv1(
  885.         COLUMN_NAME             varchar(32) NOT NULL,
  886.         grantor                 smallint NOT NULL,
  887.         grantee                 smallint NOT NULL,
  888.         select_privilege        bit,
  889.         select_grantable        bit,
  890.         insert_privilege        bit,
  891.         insert_grantable        bit,
  892.         update_privilege        bit,
  893.         update_grantable        bit,
  894.         references_privilege    bit,
  895.         references_grantable    bit,
  896.         uid                     smallint NOT NULL,
  897.         gid                     smallint NOT NULL)
  898.  
  899. /*
  900. ** insert a row for the table owner (who has all permissions)
  901. */
  902.     select @owner_uid = (
  903.         select uid
  904.         from sysobjects
  905.         where id = @table_id)
  906.  
  907.     if (charindex('6.00', @@version) > 0)
  908.     begin
  909.         insert into #column_priv1
  910.             select
  911.                 c.name,
  912.                 u.uid,
  913.                 @owner_uid,
  914.                 0,
  915.                 1,
  916.                 0,
  917.                 1,
  918.                 0,
  919.                 1,
  920.                 0,
  921.                 1,
  922.                 @owner_uid,
  923.                 0
  924.             from syscolumns c, sysusers u
  925.             where id = @table_id
  926.                 and c.number = 0
  927.                 and u.suid = 1        /* grantor is dbo of database */
  928.     end
  929.     else
  930.     begin
  931.         insert into #column_priv1
  932.             select
  933.                 c.name,
  934.                 u.uid,
  935.                 @owner_uid,
  936.                 0,
  937.                 1,
  938.                 0,
  939.                 1,
  940.                 0,
  941.                 1,
  942.                 0,
  943.                 0,
  944.                 @owner_uid,
  945.                 0
  946.             from syscolumns c, sysusers u
  947.             where id = @table_id
  948.                 and c.number = 0
  949.                 and u.suid = 1        /* grantor is dbo of database */
  950.     end
  951. /*
  952. ** now stick in a row for every column for every user in the database
  953. ** we will need to weed out those who have no permissions later
  954. ** (and yes this is a cartesion product: the uid field in sysprotects
  955. ** can also have a group id, in which case we need to extend those
  956. ** privileges to all group members).
  957. */
  958.  
  959.     insert into #column_priv1
  960.         select distinct
  961.             c.name,
  962.             o.uid,
  963.             u.uid,
  964.             0,
  965.             0,
  966.             0,
  967.             0,
  968.             0,
  969.             0,
  970.             0,
  971.             0,
  972.             u.uid,
  973.             u.gid
  974.         from sysusers u, syscolumns c, sysobjects o
  975.         where o.id = @table_id
  976.             and c.id = o.id
  977.             and c.number = 0
  978.             and u.gid <> u.uid
  979.             and u.uid <> @owner_uid
  980.  
  981.     /*
  982.     ** we need to create another temporary table to contain all the various
  983.     ** protection information for the table in question
  984.     */
  985.     create table #protects (
  986.                 uid         smallint NOT NULL,
  987.                 grantor        smallint NOT NULL,
  988.                 action        tinyint NOT NULL,
  989.                 protecttype tinyint NOT NULL,
  990.                 name        varchar(32) NOT NULL)
  991.  
  992.     insert into #protects
  993.         select
  994.             p.uid,
  995.             p.uid,
  996.             p.action,
  997.             p.protecttype,
  998.             isnull(col_name(id, c.number), '~All')
  999.             from
  1000.                 sysprotects p,
  1001.                 master.dbo.spt_values c,
  1002.                 master.dbo.spt_values a,
  1003.                 master.dbo.spt_values b
  1004.             where
  1005.                 convert(tinyint, substring(isnull(p.columns, 0x1), c.low, 1))
  1006.                     & c.high <> 0
  1007.                     and c.number <= (
  1008.                         select count(*)
  1009.                         from syscolumns
  1010.                         where id = @table_id)
  1011.                 and c.type = 'P'
  1012.                 and a.type = 'T'
  1013.                 and a.number = p.action
  1014.                 and p.action in (193,195,197,26)
  1015.                 and b.type = 'T'
  1016.                 and b.number = p.protecttype
  1017.                 and p.id = @table_id
  1018.                 and p.uid between @low and @high
  1019.  
  1020.  
  1021.     update #column_priv1
  1022.     set select_privilege = 1
  1023.         from #protects p
  1024.     where
  1025.         p.protecttype = 205
  1026.         and p.action = 193
  1027.         and (p.name = #column_priv1.COLUMN_NAME
  1028.             or name = '~All')
  1029.         and (p.uid = 0
  1030.             or p.uid = #column_priv1.gid
  1031.             or p.uid = #column_priv1.uid)
  1032.         and not exists (
  1033.             select * from #protects
  1034.             where
  1035.                 protecttype = 206
  1036.                 and action = 193
  1037.                 and (name = #column_priv1.COLUMN_NAME
  1038.                     or name = '~All')
  1039.                 and ( uid = 0
  1040.                     or uid = #column_priv1.gid
  1041.                     or uid = #column_priv1.uid))
  1042.  
  1043.     update #column_priv1
  1044.     set insert_privilege = 1
  1045.         from #protects p
  1046.     where
  1047.         p.protecttype = 205
  1048.         and p.action = 195
  1049.         and (p.name = #column_priv1.COLUMN_NAME
  1050.             or name = '~All')
  1051.         and (p.uid = 0
  1052.             or p.uid = #column_priv1.gid
  1053.             or p.uid = #column_priv1.uid)
  1054.         and not exists (
  1055.             select * from #protects
  1056.             where
  1057.                 protecttype = 206
  1058.                 and action = 195
  1059.                 and (name = #column_priv1.COLUMN_NAME
  1060.                        or name = '~All')
  1061.                 and (uid = 0
  1062.                     or uid = #column_priv1.gid
  1063.                     or uid = #column_priv1.uid))
  1064.  
  1065.     update #column_priv1
  1066.     set update_privilege = 1
  1067.         from #protects p
  1068.     where
  1069.         p.protecttype = 205
  1070.         and p.action = 197
  1071.         and (p.name = #column_priv1.COLUMN_NAME
  1072.             or name = '~All')
  1073.         and (p.uid = 0
  1074.             or p.uid = #column_priv1.gid
  1075.             or p.uid = #column_priv1.uid)
  1076.         and not exists (
  1077.             select * from #protects
  1078.                 where protecttype = 206
  1079.                 and action = 197
  1080.                 and (name = #column_priv1.COLUMN_NAME
  1081.                     or name = '~All')
  1082.                 and (uid = 0
  1083.                     or uid = #column_priv1.gid
  1084.                     or uid = #column_priv1.uid))
  1085.  
  1086.     update #column_priv1
  1087.     set references_privilege = 1
  1088.         from #protects p
  1089.     where
  1090.         p.protecttype = 205
  1091.         and p.action = 26
  1092.         and (p.name = #column_priv1.COLUMN_NAME
  1093.             or name = '~All')
  1094.         and (p.uid = 0
  1095.             or p.uid = #column_priv1.gid
  1096.             or p.uid = #column_priv1.uid)
  1097.         and not exists (
  1098.             select * from #protects
  1099.                 where protecttype = 206
  1100.                 and action = 26
  1101.                 and (name = #column_priv1.COLUMN_NAME
  1102.                     or name = '~All')
  1103.                 and (uid = 0
  1104.                     or uid = #column_priv1.gid
  1105.                     or uid = #column_priv1.uid))
  1106.  
  1107.     update #column_priv1
  1108.     set select_grantable = 1
  1109.         from #protects p
  1110.     where
  1111.         p.protecttype = 204
  1112.         and p.action = 193
  1113.         and (p.name = #column_priv1.COLUMN_NAME
  1114.             or name = '~All')
  1115.         and (p.uid = 0
  1116.             or p.uid = #column_priv1.gid
  1117.             or p.uid = #column_priv1.uid)
  1118.         and not exists (
  1119.             select * from #protects
  1120.             where
  1121.                 protecttype = 206
  1122.                 and action = 193
  1123.                 and (name = #column_priv1.COLUMN_NAME
  1124.                     or name = '~All')
  1125.                 and ( uid = 0
  1126.                     or uid = #column_priv1.gid
  1127.                     or uid = #column_priv1.uid))
  1128.  
  1129.     update #column_priv1
  1130.     set insert_grantable = 1
  1131.         from #protects p
  1132.     where
  1133.         p.protecttype = 204
  1134.         and p.action = 195
  1135.         and (p.name = #column_priv1.COLUMN_NAME
  1136.             or name = '~All')
  1137.         and (p.uid = 0
  1138.             or p.uid = #column_priv1.gid
  1139.             or p.uid = #column_priv1.uid)
  1140.         and not exists (
  1141.             select * from #protects
  1142.             where
  1143.                 protecttype = 206
  1144.                 and action = 195
  1145.                 and (name = #column_priv1.COLUMN_NAME
  1146.                     or name = '~All')
  1147.                 and ( uid = 0
  1148.                     or uid = #column_priv1.gid
  1149.                     or uid = #column_priv1.uid))
  1150.  
  1151.     update #column_priv1
  1152.     set update_grantable = 1
  1153.         from #protects p
  1154.     where
  1155.         p.protecttype = 204
  1156.         and p.action = 197
  1157.         and (p.name = #column_priv1.COLUMN_NAME
  1158.             or name = '~All')
  1159.         and (p.uid = 0
  1160.             or p.uid = #column_priv1.gid
  1161.             or p.uid = #column_priv1.uid)
  1162.         and not exists (
  1163.             select * from #protects
  1164.             where
  1165.                 protecttype = 206
  1166.                 and action = 197
  1167.                 and (name = #column_priv1.COLUMN_NAME
  1168.                     or name = '~All')
  1169.                 and ( uid = 0
  1170.                     or uid = #column_priv1.gid
  1171.                     or uid = #column_priv1.uid))
  1172.  
  1173.     update #column_priv1
  1174.     set references_grantable = 1
  1175.         from #protects p
  1176.     where
  1177.         p.protecttype = 204
  1178.         and p.action = 26
  1179.         and (p.name = #column_priv1.COLUMN_NAME
  1180.             or name = '~All')
  1181.         and (p.uid = 0
  1182.             or p.uid = #column_priv1.gid
  1183.             or p.uid = #column_priv1.uid)
  1184.         and not exists (
  1185.             select * from #protects
  1186.             where
  1187.                 protecttype = 206
  1188.                 and action = 26
  1189.                 and (name = #column_priv1.COLUMN_NAME
  1190.                     or name = '~All')
  1191.                 and ( uid = 0
  1192.                     or uid = #column_priv1.gid
  1193.                     or uid = #column_priv1.uid))
  1194.  
  1195.     create table #column_priv2(
  1196.         COLUMN_NAME     varchar(32) NOT NULL,
  1197.         grantor         smallint NULL,
  1198.         grantee         smallint NOT NULL,
  1199.         PRIVILEGE        varchar(32) NOT NULL,
  1200.         IS_GRANTABLE    varchar(3) NULL)
  1201.  
  1202.     insert into #column_priv2
  1203.         select
  1204.             COLUMN_NAME,
  1205.             grantor,
  1206.             grantee,
  1207.             'SELECT',
  1208.             'NO'
  1209.         from #column_priv1
  1210.         where select_privilege = 1 and select_grantable    = 0
  1211.  
  1212.     insert into #column_priv2
  1213.         select
  1214.             COLUMN_NAME,
  1215.             grantor,
  1216.             grantee,
  1217.             'INSERT',
  1218.             'NO'
  1219.         from #column_priv1
  1220.         where insert_privilege = 1 and insert_grantable = 0
  1221.  
  1222.     insert into #column_priv2
  1223.         select
  1224.             COLUMN_NAME,
  1225.             grantor,
  1226.             grantee,
  1227.             'UPDATE',
  1228.             'NO'
  1229.         from #column_priv1
  1230.         where update_privilege = 1 and update_grantable = 0
  1231.  
  1232.     insert into #column_priv2
  1233.         select
  1234.             COLUMN_NAME,
  1235.             grantor,
  1236.             grantee,
  1237.             'REFERENCES',
  1238.             'NO'
  1239.         from #column_priv1
  1240.         where references_privilege = 1 and references_grantable = 0
  1241.  
  1242.     insert into #column_priv2
  1243.         select
  1244.             COLUMN_NAME,
  1245.             grantor,
  1246.             grantee,
  1247.             'SELECT',
  1248.             'YES'
  1249.         from #column_priv1
  1250.         where select_grantable = 1
  1251.  
  1252.     insert into #column_priv2
  1253.         select
  1254.             COLUMN_NAME,
  1255.             grantor,
  1256.             grantee,
  1257.             'INSERT',
  1258.             'YES'
  1259.         from #column_priv1
  1260.         where insert_grantable = 1
  1261.  
  1262.     insert into #column_priv2
  1263.         select
  1264.             COLUMN_NAME,
  1265.             grantor,
  1266.             grantee,
  1267.             'UPDATE',
  1268.             'YES'
  1269.         from #column_priv1
  1270.         where update_grantable = 1
  1271.  
  1272.     insert into #column_priv2
  1273.         select
  1274.             COLUMN_NAME,
  1275.             grantor,
  1276.             grantee,
  1277.             'REFERENCES',
  1278.             'YES'
  1279.         from #column_priv1
  1280.         where references_grantable = 1
  1281.  
  1282.     select
  1283.         convert(varchar(32),db_name()) TABLE_QUALIFIER,
  1284.         convert(varchar(32),user_name(@owner_uid)) TABLE_OWNER,
  1285.         @table_name TABLE_NAME,
  1286.         COLUMN_NAME,
  1287.         convert(varchar(32),user_name(grantor)) GRANTOR,
  1288.         convert(varchar(32),user_name(grantee)) GRANTEE,
  1289.         PRIVILEGE,
  1290.         IS_GRANTABLE
  1291.     from #column_priv2
  1292.     where COLUMN_NAME like @column_name
  1293.     order by 4, 7
  1294. go
  1295.  
  1296. if (charindex('6.50', @@version) = 0 and
  1297.     charindex('7.00', @@version) = 0)
  1298. begin
  1299.     print ''
  1300.     print ''
  1301.     print 'Warning:'
  1302.     print 'you are installing the stored procedures '
  1303.     print 'on a pre 6.50 SQL Server.'
  1304.     print 'Ignore the following errors.'
  1305. end
  1306. else
  1307.     drop proc sp_column_privileges
  1308. go
  1309.  
  1310. /*    Procedure for 6.50 server */
  1311. CREATE PROCEDURE sp_column_privileges (
  1312.             @table_name         varchar(32),
  1313.             @table_owner        varchar(32) = null,
  1314.             @table_qualifier    varchar(32) = null,
  1315.             @column_name        varchar(96) = null)    /* 3*32 */
  1316. as
  1317.  
  1318.     declare @table_id     int
  1319.  
  1320.     if @column_name is null /*    If column name not supplied, match all */
  1321.         select @column_name = '%'
  1322.  
  1323.     if @table_qualifier is not null
  1324.     begin
  1325.         if db_name() <> @table_qualifier
  1326.         begin    /* If qualifier doesn't match current database */
  1327.             raiserror (15250, -1,-1)
  1328.             return
  1329.         end
  1330.     end
  1331.     if @table_owner is null
  1332.     begin    /* If unqualified table name */
  1333.         select @table_id = object_id(@table_name)
  1334.     end
  1335.     else
  1336.     begin    /* Qualified table name */
  1337.         select @table_id = object_id(@table_owner + '.' + @table_name)
  1338.     end
  1339.  
  1340.     select
  1341.         convert(varchar(32),db_name()) TABLE_QUALIFIER,
  1342.         convert(varchar(32),user_name(o.uid)) TABLE_OWNER,
  1343.         @table_name TABLE_NAME,
  1344.         convert(varchar(32),c.name) COLUMN_NAME,
  1345.         convert(varchar(32),user_name(p.grantor)) GRANTOR,
  1346.         convert(varchar(32),user_name(u.uid)) GRANTEE,
  1347.         convert (varchar(32),case p.action
  1348.              when 193 then 'SELECT'
  1349.              when 195 then 'INSERT'
  1350.              when 197 then 'UPDATE'
  1351.              else 'REFERENCES'
  1352.         end) PRIVILEGE,
  1353.         convert (varchar(3),case when p.protecttype = 205 then 'NO'
  1354.             else 'YES'
  1355.         end) IS_GRANTABLE
  1356.     from sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c
  1357.     where
  1358.         c.id = @table_id
  1359.         and c.name like @column_name
  1360.         and c.id = p.id
  1361.         and c.id = o.id
  1362.         and case substring(p.columns, 1, 1) & 1
  1363.                 when NULL then 255    /* all columns have permission */
  1364.                 when 0 then convert(tinyint, substring(p.columns, v.low, 1))
  1365.                 else (~convert(tinyint, isnull(substring(p.columns, v.low, 1),0)))
  1366.             end
  1367.             & v.high <> 0            /* permission applies to this column */
  1368.         and v.number <= (select count(*) from syscolumns
  1369.             where id = @table_id)    /* ranges from 1 to # of columns in table */
  1370.         and v.type = 'P'
  1371.         and v.number = c.colid
  1372.             /* expand groups */
  1373.         and ((p.uid = u.uid and u.uid <> u.gid) or
  1374.              (p.uid = u.gid and u.uid <> u.gid))
  1375.         and p.protecttype <> 206    /* only grant rows */
  1376.         and p.action in (26,193,195,197)
  1377.         and o.uid <> u.uid            /* no rows for owner */
  1378.         and not exists (            /* exclude revoke'd privileges */
  1379.             select *
  1380.             from sysprotects p1
  1381.             where
  1382.                 p1.protecttype = 206
  1383.                 and p1.action = p.action
  1384.                 and p1.id = p.id
  1385.                 and p1.uid = u.uid
  1386.                 and case substring(p1.columns, 1, 1) & 1
  1387.                         when NULL then 255    /* all columns have permission */
  1388.                         when 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  1389.                                             else (~convert(tinyint,isnull(substring(p.columns, v.low, 1),0)))
  1390.                     end
  1391.                     & v.high <> 0)            /* permission applies to this column */
  1392.     union all
  1393.     select    /*    Add rows for table owner */
  1394.         convert(varchar(32),db_name()) TABLE_QUALIFIER,
  1395.         convert(varchar(32),user_name(o.uid)) TABLE_OWNER,
  1396.         @table_name TABLE_NAME,
  1397.         convert(varchar(32),col_name(@table_id, c.colid)) COLUMN_NAME,
  1398.         convert(varchar(32),user_name(u.uid)) grantor,
  1399.         convert(varchar(32),user_name(o.uid)) grantee,
  1400.         convert (varchar(32),case v.number
  1401.             when 193 then 'SELECT'
  1402.             when 195 then 'INSERT'
  1403.             when 197 then 'UPDATE'
  1404.             else 'REFERENCES'
  1405.         end) PRIVILEGE,
  1406.         convert(varchar(3),'YES') IS_GRANTABLE
  1407.     from sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  1408.     where
  1409.         c.id = @table_id
  1410.         and c.name like @column_name
  1411.         and c.id = o.id
  1412.         and u.suid = 1        /* grantor is dbo of database */
  1413.         and v.type = 'P'    /* cross product to get all exposed privileges */
  1414.         and v.number in (26,193,195,197)
  1415.         and not exists (    /* exclude revoke'd privileges */
  1416.             select *
  1417.             from sysprotects p1
  1418.             where
  1419.                 p1.protecttype = 206
  1420.                 and p1.action = v.number
  1421.                 and p1.id = o.id
  1422.                 and p1.uid = o.uid)
  1423.     order by 4, 7
  1424. go
  1425.  
  1426.  
  1427.  
  1428. if (charindex('7.00', @@version) = 0)
  1429. begin
  1430.     print ''
  1431.     print ''
  1432.     print 'Warning:'
  1433.     print 'you are installing the stored procedures '
  1434.     print 'on a pre 7.0 SQL Server.'
  1435.     print 'Ignore the following errors.'
  1436. end
  1437. else
  1438.     drop proc sp_column_privileges
  1439. go
  1440.  
  1441. /*    Procedure for 7.00 server */
  1442. CREATE PROCEDURE sp_column_privileges (
  1443.             @table_name         sysname,
  1444.             @table_owner        sysname = null,
  1445.             @table_qualifier    sysname = null,
  1446.             @column_name        nvarchar(384) = null)    /* 3*128 */
  1447. as
  1448.  
  1449.     declare @table_id     int
  1450.  
  1451.     if @column_name is null /*    If column name not supplied, match all */
  1452.         select @column_name = '%'
  1453.  
  1454.     if @table_qualifier is not null
  1455.     begin
  1456.         if db_name() <> @table_qualifier
  1457.         begin    /* If qualifier doesn't match current database */
  1458.             raiserror (15250, -1,-1)
  1459.             return
  1460.         end
  1461.     end
  1462.     if @table_owner is null
  1463.     begin    /* If unqualified table name */
  1464.         select @table_id = object_id(quotename(@table_name))
  1465.     end
  1466.     else
  1467.     begin    /* Qualified table name */
  1468.         if @table_owner = N''
  1469.         begin    /* If empty owner name */
  1470.             select @table_id = 0
  1471.         end
  1472.         else
  1473.         begin
  1474.             select @table_id = object_id(quotename(@table_owner) +
  1475.                 '.' + quotename(@table_name))
  1476.         end
  1477.     end
  1478.  
  1479.     select
  1480.         convert(sysname,db_name()) TABLE_QUALIFIER,
  1481.         convert(sysname,user_name(o.uid)) TABLE_OWNER,
  1482.         @table_name TABLE_NAME,
  1483.         convert(sysname,c.name) COLUMN_NAME,
  1484.         convert(sysname,user_name(p.grantor)) GRANTOR,
  1485.         convert(sysname,user_name(u.uid)) GRANTEE,
  1486.         convert (varchar(32),case p.action
  1487.              when 193 then 'SELECT'
  1488.              when 195 then 'INSERT'
  1489.              when 197 then 'UPDATE'
  1490.              else 'REFERENCES'
  1491.         end) PRIVILEGE,
  1492.         convert (varchar(3),case when p.protecttype = 205 then 'NO'
  1493.             else 'YES'
  1494.         end) IS_GRANTABLE
  1495.     from sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c, sysmembers m
  1496.     where
  1497.         c.id = @table_id
  1498.         and c.name like @column_name
  1499.         and c.id = p.id
  1500.         and c.id = o.id
  1501.         and case substring(p.columns, 1, 1) & 1
  1502.                 when NULL then 255    /* all columns have permission */
  1503.                 when 0 then convert(tinyint, substring(p.columns, v.low, 1))
  1504.                 else (~convert(tinyint, isnull(substring(p.columns, v.low, 1),0)))
  1505.             end
  1506.             & v.high <> 0            /* permission applies to this column */
  1507.         and v.number <= (select count(*) from syscolumns
  1508.             where id = @table_id)    /* ranges from 1 to # of columns in table */
  1509.         and v.type = 'P'
  1510.         and v.number = c.colid
  1511.             /* expand groups - AKUNDONE: only 1 level of group unrolling here. Need more?? */
  1512.         and (u.uid > 0 and u.uid < 16384)
  1513.         and ((p.uid = u.uid) or 
  1514.              (p.uid = m.groupuid and u.uid = m.memberuid))
  1515.         and p.protecttype <> 206    /* only grant rows */
  1516.         and p.action in (26,193,195,197)
  1517.         and o.uid <> u.uid            /* no rows for owner */
  1518.         and not exists (            /* exclude revoke'd privileges */
  1519.             select *
  1520.             from sysprotects p1
  1521.             where
  1522.                 p1.protecttype = 206
  1523.                 and p1.action = p.action
  1524.                 and p1.id = p.id
  1525.                 and p1.uid = u.uid
  1526.                 and case substring(p1.columns, 1, 1) & 1
  1527.                         when NULL then 255    /* all columns have permission */
  1528.                         when 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  1529.                                             else (~convert(tinyint,isnull(substring(p.columns, v.low, 1),0)))
  1530.                     end
  1531.                     & v.high <> 0)            /* permission applies to this column */
  1532.     union all
  1533.     select    /*    Add rows for table owner */
  1534.         convert(sysname,db_name()) TABLE_QUALIFIER,
  1535.         convert(sysname,user_name(o.uid)) TABLE_OWNER,
  1536.         @table_name TABLE_NAME,
  1537.         convert(sysname,col_name(@table_id, c.colid)) COLUMN_NAME,
  1538.         convert(sysname,user_name(u.uid)) grantor,
  1539.         convert(sysname,user_name(o.uid)) grantee,
  1540.         convert (varchar(32),case v.number
  1541.             when 193 then 'SELECT'
  1542.             when 195 then 'INSERT'
  1543.             when 197 then 'UPDATE'
  1544.             else 'REFERENCES'
  1545.         end) PRIVILEGE,
  1546.         convert(varchar(3),'YES') IS_GRANTABLE
  1547.     from sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  1548.     where
  1549.         c.id = @table_id
  1550.         and c.name like @column_name
  1551.         and c.id = o.id
  1552.         and u.uid = 1        /* grantor is 'dbo' of database */
  1553.         and v.type = 'P'    /* cross product to get all exposed privileges */
  1554.         and v.number in (26,193,195,197)
  1555.         and not exists (    /* exclude revoke'd privileges */
  1556.             select *
  1557.             from sysprotects p1
  1558.             where
  1559.                 p1.protecttype = 206
  1560.                 and p1.action = v.number
  1561.                 and p1.id = o.id
  1562.                 and p1.uid = o.uid)
  1563.     order by 4, 7
  1564. go
  1565.  
  1566.  
  1567. grant execute on sp_column_privileges to public
  1568. go
  1569.  
  1570. dump tran master with no_log
  1571. go
  1572.  
  1573. print 'creating sp_columns'
  1574. go
  1575.  
  1576. /*    Procedure for pre-6.0 server */
  1577. CREATE PROCEDURE sp_columns (
  1578.                  @table_name        varchar(96),
  1579.                  @table_owner        varchar(96) = null,
  1580.                  @table_qualifier    varchar(32) = null,
  1581.                  @column_name        varchar(96) = null,
  1582.                  @ODBCVer            int = 2)
  1583. AS
  1584.     DECLARE @full_table_name    varchar(193)
  1585.     DECLARE @table_id int
  1586.  
  1587.     if @ODBCVer <> 3
  1588.         select @ODBCVer = 2
  1589.     if @column_name is null /*    If column name not supplied, match all */
  1590.         select @column_name = '%'
  1591.     if @table_qualifier is not null
  1592.     begin
  1593.         if db_name() <> @table_qualifier
  1594.         begin    /* If qualifier doesn't match current database */
  1595.             raiserror 20001 '~~Rush_5~~'
  1596.             return
  1597.         end
  1598.     end
  1599.     if @table_name is null
  1600.     begin    /*    If table name not supplied, match all */
  1601.         select @table_name = '%'
  1602.     end
  1603.     if @table_owner is null
  1604.     begin    /* If unqualified table name */
  1605.         SELECT @full_table_name = @table_name
  1606.     end
  1607.     else
  1608.     begin    /* Qualified table name */
  1609.         SELECT @full_table_name = @table_owner + '.' + @table_name
  1610.     end
  1611.  
  1612.     /*    Get Object ID */
  1613.     SELECT @table_id = object_id(@full_table_name)
  1614.     if ((charindex('%',@full_table_name) = 0) and
  1615.         (charindex('_',@full_table_name) = 0) and
  1616.         @table_id <> 0)
  1617.     begin
  1618.         /* this block is for the case where there is no pattern
  1619.              matching required for the table name */
  1620.         SELECT
  1621.             TABLE_QUALIFIER = convert(varchar(32),DB_NAME()),
  1622.             TABLE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  1623.             TABLE_NAME = convert(varchar(32),o.name),
  1624.             COLUMN_NAME = convert(varchar(32),c.name),
  1625.             d.DATA_TYPE,
  1626.             TYPE_NAME = t.name,
  1627.             "PRECISION" = isnull(d.data_precision, convert(int,c.length)),
  1628.             LENGTH = isnull(d.length, convert(int,c.length)),
  1629.             SCALE = d.numeric_scale,
  1630.             d.RADIX,
  1631.             NULLABLE =    /* set nullability from status flag */
  1632.                 convert(smallint, convert(bit, c.status&8)),
  1633.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  1634.             COLUMN_DEF = text,
  1635.             d.SQL_DATA_TYPE,
  1636.             d.SQL_DATETIME_SUB,
  1637.             CHAR_OCTET_LENGTH = isnull(d.data_precision, convert(int,c.length))+d.charbin,
  1638.             ORDINAL_POSITION = convert(int,c.colid),
  1639.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO      YES',(c.status&8)+1,3))),
  1640.             SS_DATA_TYPE = c.type
  1641.         FROM
  1642.             syscolumns c,
  1643.             sysobjects o,
  1644.             syscomments m,
  1645.             master.dbo.spt_datatype_info d,
  1646.             systypes t
  1647.         WHERE
  1648.             o.id = @table_id
  1649.             AND c.id = o.id
  1650.             AND t.type = d.ss_dtype
  1651.             AND c.length = isnull(d.fixlen, c.length)
  1652.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  1653.             AND o.type <> 'P'
  1654.             AND c.usertype = t.usertype
  1655.             AND c.name like @column_name
  1656.             AND c.cdefault *= m.id
  1657.             AND m.colid = 1
  1658.         ORDER BY 17
  1659.     end
  1660.     else
  1661.     begin
  1662.         /* this block is for the case where there IS pattern
  1663.              matching done on the table name */
  1664.         if @table_owner is null /*    If owner not supplied, match all */
  1665.             select @table_owner = '%'
  1666.         SELECT
  1667.             TABLE_QUALIFIER = convert(varchar(32),DB_NAME()),
  1668.             TABLE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  1669.             TABLE_NAME = convert(varchar(32),o.name),
  1670.             COLUMN_NAME = convert(varchar(32),c.name),
  1671.             d.DATA_TYPE,
  1672.             TYPE_NAME = t.name,
  1673.             "PRECISION" = isnull(d.data_precision, convert(int,c.length)),
  1674.             LENGTH = isnull(d.length, convert(int,c.length)),
  1675.             SCALE = d.numeric_scale,
  1676.             d.RADIX,
  1677.             NULLABLE =    /* set nullability from status flag */
  1678.                 convert(smallint, convert(bit, c.status&8)),
  1679.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  1680.             COLUMN_DEF = text,
  1681.             d.SQL_DATA_TYPE,
  1682.             d.SQL_DATETIME_SUB,
  1683.             CHAR_OCTET_LENGTH = isnull(d.data_precision, convert(int,c.length))+d.charbin,
  1684.             ORDINAL_POSITION = convert(int,c.colid),
  1685.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO      YES',(c.status&8)+1,3))),
  1686.             SS_DATA_TYPE = c.type
  1687.         FROM
  1688.             syscolumns c,
  1689.             sysobjects o,
  1690.             syscomments m,
  1691.             master.dbo.spt_datatype_info d,
  1692.             systypes t
  1693.         WHERE
  1694.             o.name like @table_name
  1695.             AND user_name(o.uid) like @table_owner
  1696.             AND o.id = c.id
  1697.             AND t.type = d.ss_dtype
  1698.             AND c.length = isnull(d.fixlen, c.length)
  1699.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  1700.             AND o.type <> 'P'
  1701.             AND c.usertype = t.usertype
  1702.             AND c.name like @column_name
  1703.             AND c.cdefault *= m.id
  1704.             AND m.colid = 1
  1705.         ORDER BY 2, 3, 17
  1706.     end
  1707. go
  1708.  
  1709. if (charindex('6.00', @@version) = 0 and
  1710.     charindex('6.50', @@version) = 0 and
  1711.     charindex('7.00', @@version) = 0)
  1712. begin
  1713.     print ''
  1714.     print ''
  1715.     print 'Warning:'
  1716.     print 'you are installing the stored procedures '
  1717.     print 'on a pre 6.0 SQL Server.'
  1718.     print 'Ignore the following error.'
  1719. end
  1720. else
  1721.     drop proc sp_columns
  1722. go
  1723.  
  1724. /*    Procedure for 6.0 and 6.50 server */
  1725. CREATE PROCEDURE sp_columns (
  1726.                  @table_name        varchar(96),
  1727.                  @table_owner        varchar(96) = null,
  1728.                  @table_qualifier    varchar(32) = null,
  1729.                  @column_name        varchar(96) = null,
  1730.                  @ODBCVer            int = 2)
  1731. AS
  1732.     DECLARE @full_table_name    varchar(193)
  1733.     DECLARE @table_id int
  1734.  
  1735.     if @ODBCVer <> 3
  1736.         select @ODBCVer = 2
  1737.     if @column_name is null /*    If column name not supplied, match all */
  1738.         select @column_name = '%'
  1739.     if @table_qualifier is not null
  1740.     begin
  1741.         if db_name() <> @table_qualifier
  1742.         begin    /* If qualifier doesn't match current database */
  1743.             raiserror (15250, -1,-1)
  1744.             return
  1745.         end
  1746.     end
  1747.     if @table_name is null
  1748.     begin    /*    If table name not supplied, match all */
  1749.         select @table_name = '%'
  1750.     end
  1751.     if @table_owner is null
  1752.     begin    /* If unqualified table name */
  1753.         SELECT @full_table_name = @table_name
  1754.     end
  1755.     else
  1756.     begin    /* Qualified table name */
  1757.         SELECT @full_table_name = @table_owner + '.' + @table_name
  1758.     end
  1759.  
  1760.     /*    Get Object ID */
  1761.     SELECT @table_id = object_id(@full_table_name)
  1762.     if ((charindex('%',@full_table_name) = 0) and
  1763.         (charindex('[',@full_table_name) = 0) and
  1764.         (charindex('_',@full_table_name) = 0) and
  1765.         @table_id <> 0)
  1766.     begin
  1767.         /* this block is for the case where there is no pattern
  1768.             matching required for the table name */
  1769.         SELECT
  1770.             TABLE_QUALIFIER = convert(varchar(32),DB_NAME()),
  1771.             TABLE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  1772.             TABLE_NAME = convert(varchar(32),o.name),
  1773.             COLUMN_NAME = convert(varchar(32),c.name),
  1774.             d.DATA_TYPE,
  1775.             convert(varchar(32),case
  1776.                 when t.usertype > 100 or t.usertype in (18,80) then t.name
  1777.                 else d.TYPE_NAME
  1778.             end) TYPE_NAME,
  1779.             convert(int,case
  1780.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  1781.                 else isnull(convert(int,c.prec), 2147483647)
  1782.             end) "PRECISION",
  1783.             convert(int,case
  1784.                 when d.ss_dtype IN (106, 108, 55, 63) then    /* decimal/numeric types */
  1785.                     c.prec+2
  1786.                 else
  1787.                     isnull(d.length, c.length)
  1788.             end) LENGTH,
  1789.             SCALE = convert(smallint, c.scale),
  1790.             d.RADIX,
  1791.             NULLABLE =    /* set nullability from status flag */
  1792.                 convert(smallint, convert(bit, c.status&8)),
  1793.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  1794.             COLUMN_DEF = text,
  1795.             d.SQL_DATA_TYPE,
  1796.             d.SQL_DATETIME_SUB,
  1797.             CHAR_OCTET_LENGTH = isnull(convert(int,c.prec), 2147483647)+d.charbin,
  1798.             ORDINAL_POSITION = convert(int,c.colid),
  1799.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO      YES',(c.status&8)+1,3))),
  1800.             SS_DATA_TYPE = c.type
  1801.         FROM
  1802.             syscolumns c,
  1803.             sysobjects o,
  1804.             syscomments m,
  1805.             master.dbo.spt_datatype_info d,
  1806.             systypes t
  1807.         WHERE
  1808.             o.id = @table_id
  1809.             AND c.id = o.id
  1810.             AND t.type = d.ss_dtype
  1811.             AND c.length = isnull(d.fixlen, c.length)
  1812.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  1813.             AND o.type <> 'P'
  1814.             AND isnull(d.AUTO_INCREMENT,0) = (c.status&128)/128
  1815.             AND c.usertype = t.usertype
  1816.             AND c.name like @column_name
  1817.             AND c.cdefault *= m.id
  1818.             AND m.colid = 1
  1819.         ORDER BY 17
  1820.     end
  1821.     else
  1822.     begin
  1823.         /* this block is for the case where there IS pattern
  1824.             matching done on the table name */
  1825.         if @table_owner is null /*    If owner not supplied, match all */
  1826.             select @table_owner = '%'
  1827.         SELECT
  1828.             TABLE_QUALIFIER = convert(varchar(32),DB_NAME()),
  1829.             TABLE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  1830.             TABLE_NAME = convert(varchar(32),o.name),
  1831.             COLUMN_NAME = convert(varchar(32),c.name),
  1832.             d.DATA_TYPE,
  1833.             convert(varchar(32),case
  1834.                 when t.usertype > 100 or t.usertype in (18,80) then t.name
  1835.                 else d.TYPE_NAME
  1836.             end) TYPE_NAME,
  1837.             convert(int,case
  1838.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  1839.                 else isnull(convert(int,c.prec), 2147483647)
  1840.             end) "PRECISION",
  1841.             convert(int,case
  1842.                 when d.ss_dtype IN (106, 108, 55, 63) then    /* decimal/numeric types */
  1843.                     c.prec+2
  1844.                 else
  1845.                     isnull(d.length, c.length)
  1846.             end) LENGTH,
  1847.             SCALE = convert(smallint, c.scale),
  1848.             d.RADIX,
  1849.             NULLABLE =    /* set nullability from status flag */
  1850.                 convert(smallint, convert(bit, c.status&8)),
  1851.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  1852.             COLUMN_DEF = text,
  1853.             d.SQL_DATA_TYPE,
  1854.             d.SQL_DATETIME_SUB,
  1855.             CHAR_OCTET_LENGTH = isnull(convert(int,c.prec), 2147483647)+d.charbin,
  1856.             ORDINAL_POSITION = convert(int,c.colid),
  1857.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO      YES',(c.status&8)+1,3))),
  1858.             SS_DATA_TYPE = c.type
  1859.         FROM
  1860.             syscolumns c,
  1861.             sysobjects o,
  1862.             syscomments m,
  1863.             master.dbo.spt_datatype_info d,
  1864.             systypes t
  1865.         WHERE
  1866.             o.name like @table_name
  1867.             AND user_name(o.uid) like @table_owner
  1868.             AND o.id = c.id
  1869.             AND t.type = d.ss_dtype
  1870.             AND c.length = isnull(d.fixlen, c.length)
  1871.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  1872.             AND o.type <> 'P'
  1873.             AND isnull(d.AUTO_INCREMENT,0) = (c.status&128)/128
  1874.             AND c.usertype = t.usertype
  1875.             AND c.name like @column_name
  1876.             AND c.cdefault *= m.id
  1877.             AND m.colid = 1
  1878.         ORDER BY 2, 3, 17
  1879.     end
  1880. go
  1881.  
  1882. if (charindex('7.00', @@version) = 0)
  1883. begin
  1884.     print ''
  1885.     print ''
  1886.     print 'Warning:'
  1887.     print 'you are installing the stored procedures '
  1888.     print 'on a pre 7.0 SQL Server.'
  1889.     print 'Ignore the following errors.'
  1890. end
  1891. else
  1892.     drop proc sp_columns
  1893. go
  1894.  
  1895. /*    Procedure for 7.0 server */
  1896. CREATE PROCEDURE sp_columns (
  1897.                  @table_name        nvarchar(384),
  1898.                  @table_owner        nvarchar(384) = null,
  1899.                  @table_qualifier    sysname = null,
  1900.                  @column_name        nvarchar(384) = null,
  1901.                  @ODBCVer            int = 2)
  1902. AS
  1903.     DECLARE @full_table_name    nvarchar(769)
  1904.     DECLARE @table_id int
  1905.  
  1906.     if @ODBCVer <> 3
  1907.         select @ODBCVer = 2
  1908.     if @column_name is null /*    If column name not supplied, match all */
  1909.         select @column_name = '%'
  1910.     if @table_qualifier is not null
  1911.     begin
  1912.         if db_name() <> @table_qualifier
  1913.         begin    /* If qualifier doesn't match current database */
  1914.             raiserror (15250, -1,-1)
  1915.             return
  1916.         end
  1917.     end
  1918.     if @table_name is null
  1919.     begin    /*    If table name not supplied, match all */
  1920.         select @table_name = '%'
  1921.     end
  1922.     if @table_owner is null
  1923.     begin    /* If unqualified table name */
  1924.         SELECT @full_table_name = quotename(@table_name)
  1925.     end
  1926.     else
  1927.     begin    /* Qualified table name */
  1928.         if @table_owner = ''
  1929.         begin    /* If empty owner name */
  1930.             SELECT @full_table_name = quotename(@table_owner)
  1931.         end
  1932.         else
  1933.         begin
  1934.             SELECT @full_table_name = quotename(@table_owner) +
  1935.                 '.' + quotename(@table_name)
  1936.         end
  1937.     end
  1938.  
  1939.     /*    Get Object ID */
  1940.     SELECT @table_id = object_id(@full_table_name)
  1941.     if ((isnull(charindex('%', @full_table_name),0) = 0) and
  1942.         (isnull(charindex('[', @table_name),0) = 0) and
  1943.         (isnull(charindex('[', @table_owner),0) = 0) and
  1944.         (isnull(charindex('_', @full_table_name),0) = 0) and
  1945.         @table_id <> 0)
  1946.     begin
  1947.         /* this block is for the case where there is no pattern
  1948.             matching required for the table name */
  1949.         SELECT
  1950.             TABLE_QUALIFIER = convert(sysname,DB_NAME()),
  1951.             TABLE_OWNER = convert(sysname,USER_NAME(o.uid)),
  1952.             TABLE_NAME = convert(sysname,o.name),
  1953.             COLUMN_NAME = convert(sysname,c.name),
  1954.             d.DATA_TYPE,
  1955.             convert (sysname,case
  1956.                 when t.xusertype > 255 then t.name
  1957.                 else d.TYPE_NAME
  1958.             end) TYPE_NAME,
  1959.             convert(int,case
  1960.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  1961.                 else OdbcPrec(c.xtype,c.length,c.xprec)
  1962.             end) "PRECISION",
  1963.             convert(int,case
  1964.                 when type_name(d.ss_dtype) IN ('numeric','decimal') then    /* decimal/numeric types */
  1965.                     OdbcPrec(c.xtype,c.length,c.xprec)+2
  1966.                 else
  1967.                     isnull(d.length, c.length)
  1968.             end) LENGTH,
  1969.             SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
  1970.             d.RADIX,
  1971.             NULLABLE = convert(smallint, ColumnProperty (c.id, c.name, 'AllowsNull')),
  1972.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  1973.             COLUMN_DEF = text,
  1974.             d.SQL_DATA_TYPE,
  1975.             d.SQL_DATETIME_SUB,
  1976.             CHAR_OCTET_LENGTH = isnull(d.length, c.length)+d.charbin,
  1977.             ORDINAL_POSITION = convert(int,c.colid),
  1978.             IS_NULLABLE = convert(varchar(254),
  1979.                 substring('NO YES',(ColumnProperty (c.id, c.name, 'AllowsNull')*3)+1,3)),
  1980.             SS_DATA_TYPE = c.type
  1981.         FROM
  1982.             sysobjects o,
  1983.             master.dbo.spt_datatype_info d,
  1984.             systypes t,
  1985.             syscolumns c
  1986.             LEFT OUTER JOIN syscomments m on c.cdefault = m.id
  1987.                 AND m.colid = 1
  1988.         WHERE
  1989.             o.id = @table_id
  1990.             AND c.id = o.id
  1991.             AND t.xtype = d.ss_dtype
  1992.             AND c.length = isnull(d.fixlen, c.length)
  1993.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  1994.             AND o.type <> 'P'
  1995.             AND isnull(d.AUTO_INCREMENT,0) = isnull(ColumnProperty (c.id, c.name, 'IsIdentity'),0)
  1996.             AND c.xusertype = t.xusertype
  1997.             AND c.name like @column_name
  1998.         ORDER BY 17
  1999.     end
  2000.     else
  2001.     begin
  2002.         /* this block is for the case where there IS pattern
  2003.             matching done on the table name */
  2004.         if @table_owner is null /*    If owner not supplied, match all */
  2005.             select @table_owner = '%'
  2006.         SELECT
  2007.             TABLE_QUALIFIER = convert(sysname,DB_NAME()),
  2008.             TABLE_OWNER = convert(sysname,USER_NAME(o.uid)),
  2009.             TABLE_NAME = convert(sysname,o.name),
  2010.             COLUMN_NAME = convert(sysname,c.name),
  2011.             d.DATA_TYPE,
  2012.             convert (sysname,case
  2013.                 when t.xusertype > 255 then t.name
  2014.                 else d.TYPE_NAME
  2015.             end) TYPE_NAME,
  2016.             convert(int,case
  2017.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  2018.                 else OdbcPrec(c.xtype,c.length,c.xprec)
  2019.             end) "PRECISION",
  2020.             convert(int,case
  2021.                 when type_name(d.ss_dtype) IN ('numeric','decimal') then    /* decimal/numeric types */
  2022.                     OdbcPrec(c.xtype,c.length,c.xprec)+2
  2023.                 else
  2024.                     isnull(d.length, c.length)
  2025.             end) LENGTH,
  2026.             SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
  2027.             d.RADIX,
  2028.             NULLABLE = convert(smallint, ColumnProperty (c.id, c.name, 'AllowsNull')),
  2029.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  2030.             COLUMN_DEF = text,
  2031.             d.SQL_DATA_TYPE,
  2032.             d.SQL_DATETIME_SUB,
  2033.             CHAR_OCTET_LENGTH = isnull(d.length, c.length)+d.charbin,
  2034.             ORDINAL_POSITION = convert(int,c.colid),
  2035.             IS_NULLABLE = convert(varchar(254),
  2036.                 rtrim(substring('NO YES',(ColumnProperty (c.id, c.name, 'AllowsNull')*3)+1,3))),
  2037.             SS_DATA_TYPE = c.type
  2038.         FROM
  2039.             sysobjects o,
  2040.             master.dbo.spt_datatype_info d,
  2041.             systypes t,
  2042.             syscolumns c
  2043.             LEFT OUTER JOIN syscomments m on c.cdefault = m.id
  2044.                 AND m.colid = 1
  2045.         WHERE
  2046.             o.name like @table_name
  2047.             AND user_name(o.uid) like @table_owner
  2048.             AND o.id = c.id
  2049.             AND t.xtype = d.ss_dtype
  2050.             AND c.length = isnull(d.fixlen, c.length)
  2051.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2052.             AND o.type <> 'P'
  2053.             AND isnull(d.AUTO_INCREMENT,0) = isnull(ColumnProperty (c.id, c.name, 'IsIdentity'),0)
  2054.             AND c.xusertype = t.xusertype
  2055.             AND c.name like @column_name
  2056.         ORDER BY 2, 3, 17
  2057.     end
  2058. go
  2059.  
  2060. grant execute on sp_columns to public
  2061. go
  2062.  
  2063. dump tran master with no_log
  2064. go
  2065.  
  2066. print 'creating sp_databases'
  2067. go
  2068.  
  2069. /*    Procedure for pre-7.0 server */
  2070. create proc sp_databases
  2071. as
  2072.     set nocount on
  2073.     /* Use temporary table to sum up database size w/o using group by */
  2074.     create table #databases (
  2075.                   DATABASE_NAME varchar(32) NOT NULL,
  2076.                   size int NOT NULL)
  2077.  
  2078.     /* Insert row for each database */
  2079.     insert into #databases
  2080.         select
  2081.             name,
  2082.             (select sum(size) from master.dbo.sysusages
  2083.                 where dbid = d.dbid)
  2084.         from master.dbo.sysdatabases d
  2085.  
  2086.     select
  2087.          DATABASE_NAME,
  2088.          DATABASE_SIZE = size*2,    /* Convert from 2048 byte pages to K */
  2089.          REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  2090.     from #databases
  2091.     order by 1
  2092. go
  2093.  
  2094. if (charindex('7.00', @@version) = 0)
  2095. begin
  2096.     print ''
  2097.     print ''
  2098.     print 'Warning:'
  2099.     print 'you are installing the stored procedures '
  2100.     print 'on a pre 7.0 SQL Server.'
  2101.     print 'Ignore the following errors.'
  2102. end
  2103. else
  2104.     drop proc sp_databases
  2105. go
  2106.  
  2107. /*    Procedure for 7.0 server */
  2108. create proc sp_databases
  2109. as
  2110.     set nocount on
  2111.     declare @name sysname
  2112.     declare @SQL  nvarchar(600)
  2113.  
  2114.     /* Use temporary table to sum up database size w/o using group by */
  2115.     create table #databases (
  2116.                   DATABASE_NAME sysname NOT NULL,
  2117.                   size int NOT NULL)
  2118.  
  2119.     declare c1 cursor for 
  2120.         select name from master.dbo.sysdatabases
  2121.  
  2122.     open c1
  2123.     fetch c1 into @name
  2124.  
  2125.     while @@fetch_status >= 0
  2126.     begin
  2127.         select @SQL = 'insert into #databases
  2128.                 select N'''+ @name + ''', sum(size) from '
  2129.                 + QuoteName(@name) + '.dbo.sysfiles'
  2130.         /* Insert row for each database */
  2131.         execute (@SQL)
  2132.         fetch c1 into @name
  2133.     end
  2134.     deallocate c1
  2135.  
  2136.     select    
  2137.         DATABASE_NAME,
  2138.         DATABASE_SIZE = size*8,/* Convert from 8192 byte pages to K */
  2139.         REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  2140.     from #databases
  2141.     order by 1
  2142. go
  2143.  
  2144. grant execute on sp_databases to public
  2145. go
  2146.  
  2147. dump tran master with no_log
  2148. go
  2149.  
  2150. print 'creating sp_datatype_info'
  2151. go
  2152.  
  2153. /*    Procedure for pre-6.0 server */
  2154. create proc sp_datatype_info
  2155.     (@data_type int = 0, @ODBCVer tinyint = 2)
  2156. as
  2157.     if @ODBCVer <> 3
  2158.         select @ODBCVer = 2
  2159.     if @data_type = 0
  2160.         select
  2161.             TYPE_NAME = t.name,
  2162.             d.DATA_TYPE,
  2163.             "PRECISION" = isnull(d.data_precision, convert(int,t.length)),
  2164.             d.LITERAL_PREFIX,
  2165.             d.LITERAL_SUFFIX,
  2166.             e.CREATE_PARAMS,
  2167.             d.NULLABLE,
  2168.             d.CASE_SENSITIVE,
  2169.             d.SEARCHABLE,
  2170.             d.UNSIGNED_ATTRIBUTE,
  2171.             d.MONEY,
  2172.             d.AUTO_INCREMENT,
  2173.             LOCAL_TYPE_NAME = t.name,
  2174.             MINIMUM_SCALE = d.numeric_scale,
  2175.             MAXIMUM_SCALE = d.numeric_scale,
  2176.             d.SQL_DATA_TYPE,
  2177.             d.SQL_DATETIME_SUB,
  2178.             NUM_PREC_RADIX = convert(int,d.RADIX),
  2179.             INTERVAL_PRECISION = convert(smallint,NULL),
  2180.             USERTYPE = t.usertype
  2181.         from master.dbo.spt_datatype_info d, master.dbo.spt_datatype_info_ext e, systypes t
  2182.         where
  2183.             d.ss_dtype = t.type
  2184.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2185.             and t.usertype *= e.user_type
  2186.             and (t.type not in (111,109,38,110) or /* get rid of nullable types */
  2187.                 t.usertype > 100)
  2188.         order by 2, 12, 11, t.usertype
  2189.  
  2190.     else
  2191.         select
  2192.             TYPE_NAME = t.name,
  2193.             d.DATA_TYPE,
  2194.             "PRECISION" = isnull(d.data_precision, convert(int,t.length)),
  2195.             d.LITERAL_PREFIX,
  2196.             d.LITERAL_SUFFIX,
  2197.             e.CREATE_PARAMS,
  2198.             d.NULLABLE,
  2199.             d.CASE_SENSITIVE,
  2200.             d.SEARCHABLE,
  2201.             d.UNSIGNED_ATTRIBUTE,
  2202.             d.MONEY,
  2203.             d.AUTO_INCREMENT,
  2204.             LOCAL_TYPE_NAME = t.name,
  2205.             MINIMUM_SCALE = d.numeric_scale,
  2206.             MAXIMUM_SCALE = d.numeric_scale,
  2207.             d.SQL_DATA_TYPE,
  2208.             d.SQL_DATETIME_SUB,
  2209.             NUM_PREC_RADIX = convert(int,d.RADIX),
  2210.             INTERVAL_PRECISION = convert(smallint,NULL),
  2211.             USERTYPE = t.usertype
  2212.         from master.dbo.spt_datatype_info d, master.dbo.spt_datatype_info_ext e, systypes t
  2213.         where
  2214.             DATA_TYPE = @data_type
  2215.             and d.ss_dtype = t.type
  2216.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2217.             and t.usertype *= e.user_type
  2218.             and (t.type not in (111,109,38,110) or /* get rid of nullable types */
  2219.                 t.usertype > 100)
  2220.         order by 12, 11, t.usertype
  2221.  
  2222. go
  2223.  
  2224. if (charindex('6.00', @@version) = 0 and
  2225.     charindex('6.50', @@version) = 0 and
  2226.     charindex('7.00', @@version) = 0)
  2227. begin
  2228.     print ''
  2229.     print ''
  2230.     print 'Warning:'
  2231.     print 'you are installing the stored procedures '
  2232.     print 'on a pre 6.0 SQL Server.'
  2233.     print 'Ignore the following errors.'
  2234. end
  2235. else
  2236.     drop proc sp_datatype_info
  2237. go
  2238.  
  2239. /*    Procedure for 6.0 and 6.50 servers */
  2240. create proc sp_datatype_info
  2241.     (@data_type int = 0, @ODBCVer tinyint = 2)
  2242. as
  2243.     if @ODBCVer <> 3
  2244.         select @ODBCVer = 2
  2245.     if @data_type = 0
  2246.         select
  2247.             convert(varchar(32),case
  2248.                 when t.usertype > 100 or t.usertype in (18,80) then t.name
  2249.                 else d.TYPE_NAME
  2250.             end) TYPE_NAME,
  2251.             d.DATA_TYPE,
  2252.             convert(int,case
  2253.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  2254.                 when d.ss_dtype in (35,34) then 2147483647                /* TEXT/IMAGE */
  2255.                 when d.ss_dtype in (55,63,106,108) and t.usertype <= 100 then @@max_precision /* DECIMAL/NUMERIC */
  2256.                 else t.prec
  2257.             end) "PRECISION",
  2258.             d.LITERAL_PREFIX,
  2259.             d.LITERAL_SUFFIX,
  2260.             e.CREATE_PARAMS,
  2261.             convert(smallint,case
  2262.                 when d.AUTO_INCREMENT = 1 then 0 /* IDENTITY*/
  2263.                 else t.allownulls
  2264.             end) NULLABLE,
  2265.             d.CASE_SENSITIVE,
  2266.             d.SEARCHABLE,
  2267.             d.UNSIGNED_ATTRIBUTE,
  2268.             d.MONEY,
  2269.             d.AUTO_INCREMENT,
  2270.             convert(varchar(32),case
  2271.                 when t.usertype > 100 or t.usertype in (18,80) then t.name
  2272.                 else d.TYPE_NAME
  2273.             end) LOCAL_TYPE_NAME,
  2274.             convert(smallint,case
  2275.                 when d.ss_dtype in (55,63,106,108) and t.usertype > 100 then t.scale
  2276.                 else d.numeric_scale
  2277.             end) MINIMUM_SCALE,
  2278.             convert(smallint,case
  2279.                 when d.ss_dtype in (106,108) and d.AUTO_INCREMENT = 0 then @@max_precision /* DECIMAL/NUMERIC */
  2280.                 when d.ss_dtype in (106,108) and d.AUTO_INCREMENT = 1 then 0 /* DECIMAL/NUMERIC IDENTITY*/
  2281.                 else t.scale
  2282.             end) MAXIMUM_SCALE,
  2283.             d.SQL_DATA_TYPE,
  2284.             d.SQL_DATETIME_SUB,
  2285.             NUM_PREC_RADIX = convert(int,d.RADIX),
  2286.             INTERVAL_PRECISION = convert(smallint,NULL),
  2287.             USERTYPE = t.usertype
  2288.         from master.dbo.spt_datatype_info d, master.dbo.spt_datatype_info_ext e, systypes t
  2289.         where
  2290.             d.ss_dtype = t.type
  2291.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2292.             and t.usertype *= e.user_type
  2293.             and isnull(d.AUTO_INCREMENT,0) *= e.AUTO_INCREMENT
  2294.             and (t.type not in (111,109,38,110,55,63) or /* get rid of nullable types */
  2295.                 t.usertype > 100)
  2296.             and (t.usertype <= 100 or
  2297.                 isnull(d.AUTO_INCREMENT,0) = 0)
  2298.         order by 2, 12, 11, t.usertype
  2299.  
  2300.     else
  2301.         select
  2302.             convert(varchar(32),case
  2303.                 when t.usertype > 100 or t.usertype in (18,80) then t.name
  2304.                 else d.TYPE_NAME
  2305.             end) TYPE_NAME,
  2306.             d.DATA_TYPE,
  2307.             convert(int,case
  2308.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  2309.                 when d.ss_dtype in (35,34) then 2147483647                /* TEXT/IMAGE */
  2310.                 when d.ss_dtype in (55,63,106,108) and t.usertype <= 100 then @@max_precision /* DECIMAL/NUMERIC */
  2311.                 else t.prec
  2312.             end) "PRECISION",
  2313.             d.LITERAL_PREFIX,
  2314.             d.LITERAL_SUFFIX,
  2315.             e.CREATE_PARAMS,
  2316.             convert(smallint,case
  2317.                 when d.AUTO_INCREMENT = 1 then 0 /* IDENTITY*/
  2318.                 else t.allownulls
  2319.             end) NULLABLE,
  2320.             d.CASE_SENSITIVE,
  2321.             d.SEARCHABLE,
  2322.             d.UNSIGNED_ATTRIBUTE,
  2323.             d.MONEY,
  2324.             d.AUTO_INCREMENT,
  2325.             convert(varchar(32),case
  2326.                 when t.usertype > 100 or t.usertype in (18,80) then t.name
  2327.                 else d.TYPE_NAME
  2328.             end) LOCAL_TYPE_NAME,
  2329.             convert(smallint,case
  2330.                 when d.ss_dtype in (55,63,106,108) and t.usertype > 100 then t.scale
  2331.                 else d.numeric_scale
  2332.             end) MINIMUM_SCALE,
  2333.             convert(smallint,case
  2334.                 when d.ss_dtype in (106,108) and d.AUTO_INCREMENT = 0 then @@max_precision /* DECIMAL/NUMERIC */
  2335.                 when d.ss_dtype in (106,108) and d.AUTO_INCREMENT = 1 then 0 /* DECIMAL/NUMERIC IDENTITY*/
  2336.                 else t.scale
  2337.             end) MAXIMUM_SCALE,
  2338.             d.SQL_DATA_TYPE,
  2339.             d.SQL_DATETIME_SUB,
  2340.             NUM_PREC_RADIX = convert(int,d.RADIX),
  2341.             INTERVAL_PRECISION = convert(smallint,NULL),
  2342.             USERTYPE = t.usertype
  2343.         from master.dbo.spt_datatype_info d, master.dbo.spt_datatype_info_ext e, systypes t
  2344.         where
  2345.             d.DATA_TYPE = @data_type
  2346.             and d.ss_dtype = t.type
  2347.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2348.             and t.usertype *= e.user_type
  2349.             and isnull(d.AUTO_INCREMENT,0) *= e.AUTO_INCREMENT
  2350.             and (t.type not in (111,109,38,110,55,63) or /* get rid of nullable types */
  2351.                 t.usertype > 100)
  2352.             and (t.usertype <= 100 or
  2353.                 isnull(d.AUTO_INCREMENT,0) = 0)
  2354.         order by 12, 11, t.usertype
  2355. go
  2356.  
  2357. if (charindex('7.00', @@version) = 0)
  2358. begin
  2359.     print ''
  2360.     print ''
  2361.     print 'Warning:'
  2362.     print 'you are installing the stored procedures '
  2363.     print 'on a pre 7.0 SQL Server.'
  2364.     print 'Ignore the following errors.'
  2365. end
  2366. else
  2367.     drop proc sp_datatype_info
  2368. go
  2369.  
  2370. /*    Procedure for 7.0 server */
  2371. create proc sp_datatype_info
  2372.     (@data_type int = 0, @ODBCVer tinyint = 2)
  2373. as
  2374.     declare @mintype int
  2375.     declare @maxtype int
  2376.  
  2377.     if @ODBCVer <> 3
  2378.         select @ODBCVer = 2
  2379.     if @data_type = 0
  2380.     begin
  2381.         select @mintype = -32768
  2382.         select @maxtype = 32767
  2383.     end
  2384.     else
  2385.     begin
  2386.         select @mintype = @data_type
  2387.         select @maxtype = @data_type
  2388.     end
  2389.  
  2390.     select
  2391.         convert(sysname,case
  2392.             when t.xusertype > 255 then t.name
  2393.             else d.TYPE_NAME
  2394.         end) TYPE_NAME,
  2395.         d.DATA_TYPE,
  2396.         convert(int,case
  2397.             when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  2398.             when type_name(d.ss_dtype) IN ('numeric','decimal') and
  2399.                 t.xusertype <= 255 then @@max_precision /* DECIMAL/NUMERIC */
  2400.             else OdbcPrec(t.xtype, t.length, t.xprec)
  2401.         end) "PRECISION",
  2402.         d.LITERAL_PREFIX,
  2403.         d.LITERAL_SUFFIX,
  2404.         e.CREATE_PARAMS,
  2405.         convert(smallint,case
  2406.             when d.AUTO_INCREMENT = 1 then 0 /* IDENTITY*/
  2407.             else TypeProperty (t.name, 'AllowsNull')
  2408.         end) NULLABLE,
  2409.         d.CASE_SENSITIVE,
  2410.         d.SEARCHABLE,
  2411.         d.UNSIGNED_ATTRIBUTE,
  2412.         d.MONEY,
  2413.         d.AUTO_INCREMENT,
  2414.         convert(sysname,case
  2415.             when t.xusertype > 255 then t.name
  2416.             else d.LOCAL_TYPE_NAME
  2417.         end) LOCAL_TYPE_NAME,
  2418.         convert(smallint,case
  2419.             when type_name(d.ss_dtype) IN ('numeric','decimal') and t.xusertype > 255 then TypeProperty (t.name, 'Scale')
  2420.             else d.numeric_scale
  2421.         end) MINIMUM_SCALE,
  2422.         convert(smallint,case
  2423.             when type_name(d.ss_dtype) IN ('numeric','decimal') and d.AUTO_INCREMENT = 0 and t.xusertype <= 255 then @@max_precision /* DECIMAL/NUMERIC */
  2424.             when type_name(d.ss_dtype) IN ('numeric','decimal') and d.AUTO_INCREMENT = 1 then 0 /* DECIMAL/NUMERIC IDENTITY*/
  2425.             else TypeProperty (t.name, 'Scale')
  2426.         end) MAXIMUM_SCALE,
  2427.         d.SQL_DATA_TYPE,
  2428.         d.SQL_DATETIME_SUB,
  2429.         NUM_PREC_RADIX = convert(int,d.RADIX),
  2430.         INTERVAL_PRECISION = convert(smallint,NULL),
  2431.         USERTYPE = t.usertype
  2432.     from master.dbo.spt_datatype_info d
  2433.         INNER JOIN systypes t on d.ss_dtype = t.xtype
  2434.         LEFT OUTER JOIN master.dbo.spt_datatype_info_ext e on
  2435.             t.xusertype = e.user_type
  2436.             and isnull(d.AUTO_INCREMENT,0) = e.AUTO_INCREMENT
  2437.     where
  2438.         d.DATA_TYPE between @mintype and @maxtype
  2439.         and (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2440.         and (t.xusertype <= 255 or
  2441.             isnull(d.AUTO_INCREMENT,0) = 0)
  2442.     order by 2, 12, 11,
  2443.     case
  2444.         when t.usertype=18 then 255
  2445.         else t.usertype
  2446.     end
  2447.  
  2448. go
  2449.  
  2450. grant execute on sp_datatype_info to public
  2451. go
  2452.  
  2453. dump tran master with no_log
  2454. go
  2455.  
  2456. print 'creating sp_fkeys'
  2457. go
  2458.  
  2459. /*    Procedure for pre-6.0 server */
  2460. CREATE PROCEDURE sp_fkeys(
  2461.                @pktable_name        varchar(32) = null,
  2462.                @pktable_owner        varchar(32) = null,
  2463.                @pktable_qualifier    varchar(32) = null,
  2464.                @fktable_name        varchar(32) = null,
  2465.                @fktable_owner        varchar(32) = null,
  2466.                @fktable_qualifier    varchar(32) = null )
  2467. as
  2468.     set nocount on
  2469.     declare    @order_by_pk int
  2470.  
  2471.     select  @order_by_pk = 0
  2472.  
  2473.     if (@pktable_name is null) and (@fktable_name is null)
  2474.     begin    /* If neither primary key nor foreign key table names given */
  2475.         raiserror 20004 '~~Rush_25~~'
  2476.         return
  2477.     end
  2478.     if @fktable_qualifier is not null
  2479.     begin
  2480.         if db_name() <> @fktable_qualifier
  2481.         begin    /* If qualifier doesn't match current database */
  2482.             raiserror 20001 '~~Rush_26~~'
  2483.             return
  2484.         end
  2485.     end
  2486.     if @pktable_qualifier is not null
  2487.     begin
  2488.         if db_name() <> @pktable_qualifier
  2489.         begin    /* If qualifier doesn't match current database */
  2490.             raiserror 20001 '~~Rush_27~~'
  2491.             return
  2492.         end
  2493.     end
  2494.  
  2495.     if @pktable_name is null
  2496.     begin /*  If table name not supplied, match all */
  2497.         select @pktable_name = '%'
  2498.         select @order_by_pk = 1
  2499.     end
  2500.     if @pktable_owner is null    /*    If PK owner not supplied, match all */
  2501.         select @pktable_owner = '%'
  2502.     if @fktable_name is null    /*    If table name not supplied, match all */
  2503.         select @fktable_name = '%'
  2504.     if @fktable_owner is null    /*    If FK owner not supplied, match all */
  2505.         select @fktable_owner = '%'
  2506.  
  2507.     if @@trancount <> 0
  2508.     begin    /* If inside a transaction */
  2509.         raiserror 20003 '~~Rush_28~~'
  2510.         return
  2511.     end
  2512.     create table #fkeys(
  2513.              PKTABLE_QUALIFIER    varchar(32) NULL,
  2514.              PKTABLE_OWNER        varchar(32) NULL,
  2515.              PKTABLE_NAME        varchar(32) NOT NULL,
  2516.              PKCOLUMN_NAME        varchar(32) NOT NULL,
  2517.              FKTABLE_QUALIFIER    varchar(32) NULL,
  2518.              FKTABLE_OWNER        varchar(32) NULL,
  2519.              FKTABLE_NAME        varchar(32) NOT NULL,
  2520.              FKCOLUMN_NAME        varchar(32) NOT NULL,
  2521.              KEY_SEQ            smallint NOT NULL)
  2522.  
  2523.     /*    SQL Server supports upto 8 PK/FK relationships between 2 tables */
  2524.     /*    Process syskeys for each relationship */
  2525.     /*    The inserts below adds a row to the temp table for each of the
  2526.         8 possible relationships */
  2527.     insert into #fkeys
  2528.         select
  2529.             db_name(),
  2530.             user_name(o1.uid),
  2531.             object_name(k.depid),
  2532.             c2.name,
  2533.             db_name(),
  2534.             user_name(o2.uid),
  2535.             object_name(k.id),
  2536.             c1.name,
  2537.             1
  2538.         from
  2539.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  2540.         where
  2541.             c1.id = k.id
  2542.             and k.type = 2    /* Foreign type key */
  2543.             and c1.colid = k.key1
  2544.             and c2.id = k.depid
  2545.             and c2.colid = k.depkey1
  2546.             and o1.id = k.depid
  2547.             and o2.id = k.id
  2548.     union all
  2549.         select
  2550.             db_name(),
  2551.             user_name(o1.uid),
  2552.             object_name(k.depid),
  2553.             c2.name,
  2554.             db_name(),
  2555.             user_name(o2.uid),
  2556.             object_name(k.id),
  2557.             c1.name,
  2558.             2
  2559.         from
  2560.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  2561.         where
  2562.             c1.id = k.id
  2563.             and k.type = 2    /* Foreign type key */
  2564.             and c1.colid = k.key2
  2565.             and c2.id = k.depid
  2566.             and c2.colid = k.depkey2
  2567.             and o1.id = k.depid
  2568.             and o2.id = k.id
  2569.     union all
  2570.         select
  2571.             db_name(),
  2572.             user_name(o1.uid),
  2573.             object_name(k.depid),
  2574.             c2.name,
  2575.             db_name(),
  2576.             user_name(o2.uid),
  2577.             object_name(k.id),
  2578.             c1.name,
  2579.             3
  2580.         from
  2581.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  2582.         where
  2583.             c1.id = k.id
  2584.             and k.type = 2    /* Foreign type key */
  2585.             and c1.colid = k.key3
  2586.             and c2.id = k.depid
  2587.             and c2.colid = k.depkey3
  2588.             and o1.id = k.depid
  2589.             and o2.id = k.id
  2590.     union all
  2591.         select
  2592.             db_name(),
  2593.             user_name(o1.uid),
  2594.             object_name(k.depid),
  2595.             c2.name,
  2596.             db_name(),
  2597.             user_name(o2.uid),
  2598.             object_name(k.id),
  2599.             c1.name,
  2600.             4
  2601.         from
  2602.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  2603.         where
  2604.             c1.id = k.id
  2605.             and k.type = 2    /* Foreign type key */
  2606.             and c1.colid = k.key4
  2607.             and c2.id = k.depid
  2608.             and c2.colid = k.depkey4
  2609.             and o1.id = k.depid
  2610.             and o2.id = k.id
  2611.     union all
  2612.         select
  2613.             db_name(),
  2614.             user_name(o1.uid),
  2615.             object_name(k.depid),
  2616.             c2.name,
  2617.             db_name(),
  2618.             user_name(o2.uid),
  2619.             object_name(k.id),
  2620.             c1.name,
  2621.             5
  2622.         from
  2623.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  2624.         where
  2625.             c1.id = k.id
  2626.             and k.type = 2    /* Foreign type key */
  2627.             and c1.colid = k.key5
  2628.             and c2.id = k.depid
  2629.             and c2.colid = k.depkey5
  2630.             and o1.id = k.depid
  2631.             and o2.id = k.id
  2632.     union all
  2633.         select
  2634.             db_name(),
  2635.             user_name(o1.uid),
  2636.             object_name(k.depid),
  2637.             c2.name,
  2638.             db_name(),
  2639.             user_name(o2.uid),
  2640.             object_name(k.id),
  2641.             c1.name,
  2642.             6
  2643.         from
  2644.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  2645.         where
  2646.             c1.id = k.id
  2647.             and k.type = 2    /* Foreign type key */
  2648.             and c1.colid = k.key6
  2649.             and c2.id = k.depid
  2650.             and c2.colid = k.depkey6
  2651.             and o1.id = k.depid
  2652.             and o2.id = k.id
  2653.     union all
  2654.         select
  2655.             db_name(),
  2656.             user_name(o1.uid),
  2657.             object_name(k.depid),
  2658.             c2.name,
  2659.             db_name(),
  2660.             user_name(o2.uid),
  2661.             object_name(k.id),
  2662.             c1.name,
  2663.             7
  2664.         from
  2665.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  2666.         where
  2667.             c1.id = k.id
  2668.             and k.type = 2    /* Foreign type key */
  2669.             and c1.colid = k.key7
  2670.             and c2.id = k.depid
  2671.             and c2.colid = k.depkey7
  2672.             and o1.id = k.depid
  2673.             and o2.id = k.id
  2674.     union all
  2675.         select
  2676.             db_name(),
  2677.             user_name(o1.uid),
  2678.             object_name(k.depid),
  2679.             c2.name,
  2680.             db_name(),
  2681.             user_name(o2.uid),
  2682.             object_name(k.id),
  2683.             c1.name,
  2684.             8
  2685.         from
  2686.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  2687.         where
  2688.             c1.id = k.id
  2689.             and k.type = 2    /* Foreign type key */
  2690.             and c1.colid = k.key8
  2691.             and c2.id = k.depid
  2692.             and c2.colid = k.depkey8
  2693.             and o1.id = k.depid
  2694.             and o2.id = k.id
  2695.  
  2696.     if @order_by_pk = 1 /*    If order by PK fields */
  2697.         select
  2698.             PKTABLE_QUALIFIER,
  2699.             PKTABLE_OWNER,
  2700.             PKTABLE_NAME,
  2701.             PKCOLUMN_NAME,
  2702.             FKTABLE_QUALIFIER,
  2703.             FKTABLE_OWNER,
  2704.             FKTABLE_NAME,
  2705.             FKCOLUMN_NAME,
  2706.             KEY_SEQ,
  2707.             UPDATE_RULE = convert(smallint, null),
  2708.             DELETE_RULE = convert(smallint,null),
  2709.             FK_NAME = convert(varchar(32),null),
  2710.             PK_NAME = convert(varchar(32),null),
  2711.             DEFERRABILITY = convert(smallint,7)
  2712.         from #fkeys
  2713.         where FKTABLE_NAME like @fktable_name
  2714.             and FKTABLE_OWNER like @fktable_owner
  2715.             and PKTABLE_NAME  like @pktable_name
  2716.             and PKTABLE_OWNER like @pktable_owner
  2717.         order by 1, 2, 3, 9
  2718.     else        /*    Order by FK fields */
  2719.         select
  2720.             PKTABLE_QUALIFIER,
  2721.             PKTABLE_OWNER,
  2722.             PKTABLE_NAME,
  2723.             PKCOLUMN_NAME,
  2724.             FKTABLE_QUALIFIER,
  2725.             FKTABLE_OWNER,
  2726.             FKTABLE_NAME,
  2727.             FKCOLUMN_NAME,
  2728.             KEY_SEQ,
  2729.             UPDATE_RULE = convert(smallint,null),
  2730.             DELETE_RULE = convert(smallint,null),
  2731.             FK_NAME = convert(varchar(32),null),
  2732.             PK_NAME = convert(varchar(32),null),
  2733.             DEFERRABILITY = convert(smallint,7)
  2734.         from #fkeys
  2735.         where FKTABLE_NAME like @fktable_name
  2736.             and FKTABLE_OWNER like @fktable_owner
  2737.             and PKTABLE_NAME  like @pktable_name
  2738.             and PKTABLE_OWNER like @pktable_owner
  2739.         order by 5, 6, 7, 9
  2740. go
  2741.  
  2742. if (charindex('6.00', @@version) = 0 and
  2743.     charindex('6.50', @@version) = 0 and
  2744.     charindex('7.00', @@version) = 0)
  2745. begin
  2746.     print ''
  2747.     print ''
  2748.     print 'Warning:'
  2749.     print 'you are installing the stored procedures '
  2750.     print 'on a pre 6.0 SQL Server.'
  2751.     print 'Ignore the following errors.'
  2752. end
  2753. else
  2754.     drop proc sp_fkeys
  2755. go
  2756.  
  2757. /*    Procedure for 6.0 and 6.50 servers */
  2758. CREATE PROCEDURE sp_fkeys(
  2759.                @pktable_name        varchar(32) = null,
  2760.                @pktable_owner        varchar(32) = null,
  2761.                @pktable_qualifier    varchar(32) = null,
  2762.                @fktable_name        varchar(32) = null,
  2763.                @fktable_owner        varchar(32) = null,
  2764.                @fktable_qualifier    varchar(32) = null )
  2765. as
  2766.     set nocount on
  2767.     DECLARE @pktable_id            int
  2768.     DECLARE @pkfull_table_name    varchar(65) /* 2*32 +1 */
  2769.     DECLARE @fktable_id            int
  2770.     DECLARE @fkfull_table_name    varchar(65) /* 2*32 +1 */
  2771.     declare    @order_by_pk        int
  2772.  
  2773.     select  @order_by_pk = 0
  2774.  
  2775.     if (@pktable_name is null) and (@fktable_name is null)
  2776.     begin    /* If neither primary key nor foreign key table names given */
  2777.         raiserror (15252,-1,-1)
  2778.         return
  2779.     end
  2780.     if @fktable_qualifier is not null
  2781.     begin
  2782.         if db_name() <> @fktable_qualifier
  2783.         begin    /* If qualifier doesn't match current database */
  2784.             raiserror (15250, -1,-1)
  2785.             return
  2786.         end
  2787.     end
  2788.     if @pktable_qualifier is not null
  2789.     begin
  2790.         if db_name() <> @pktable_qualifier
  2791.         begin    /* If qualifier doesn't match current database */
  2792.             raiserror (15250, -1,-1)
  2793.             return
  2794.         end
  2795.     end
  2796.  
  2797.     if @pktable_owner is null
  2798.     begin    /* If unqualified primary key table name */
  2799.         SELECT @pkfull_table_name = @pktable_name
  2800.     end
  2801.     else
  2802.     begin    /* Qualified primary key table name */
  2803.         SELECT @pkfull_table_name = @pktable_owner + '.' + @pktable_name
  2804.     end
  2805.     /*    Get Object ID */
  2806.     SELECT @pktable_id = object_id(@pkfull_table_name)
  2807.  
  2808.     if @fktable_owner is null
  2809.     begin    /* If unqualified foreign key table name */
  2810.         SELECT @fkfull_table_name = @fktable_name
  2811.     end
  2812.     else
  2813.     begin    /* Qualified foreign key table name */
  2814.         SELECT @fkfull_table_name = @fktable_owner + '.' + @fktable_name
  2815.     end
  2816.     /*    Get Object ID */
  2817.     SELECT @fktable_id = object_id(@fkfull_table_name)
  2818.  
  2819.     if @fktable_name is not null
  2820.     begin
  2821.         if @fktable_id is null
  2822.             SELECT @fktable_id = 0    /* fk table not found, empty result */
  2823.     end
  2824.  
  2825.     if @pktable_name is null
  2826.     begin /*  If table name not supplied, match all */
  2827.         select @order_by_pk = 1
  2828.     end
  2829.     else
  2830.     begin
  2831.         if @pktable_id is null
  2832.         begin
  2833.             SELECT @pktable_id = 0    /* pk table not found, empty result */
  2834.         end
  2835.     end
  2836.  
  2837.     if (@@trancount <> 0 and
  2838.         charindex('6.50', @@version) = 0)
  2839.     begin    /* If inside a transaction */
  2840.         raiserror (15002,-1,-1,'sp_fkeys')
  2841.         return
  2842.     end
  2843.  
  2844.     create table #fkeys(
  2845.              pkdb_id        int NOT NULL,
  2846.              pktable_id     int NOT NULL,
  2847.              pkcolid        int NOT NULL,
  2848.              fkdb_id        int NOT NULL,
  2849.              fktable_id        int NOT NULL,
  2850.              fkcolid        int NOT NULL,
  2851.              KEY_SEQ        smallint NOT NULL,
  2852.              fk_id            int NOT NULL,
  2853.              pk_id            int NOT NULL)
  2854.  
  2855.     /*    SQL Server supports upto 16 PK/FK relationships between 2 tables */
  2856.     /*    Process syskeys for each relationship */
  2857.     /*    The inserts below adds a row to the temp table for each of the
  2858.         16 possible relationships */
  2859.     insert into #fkeys
  2860.         select
  2861.             r.rkeydbid,
  2862.             r.rkeyid,
  2863.             r.rkey1,
  2864.             r.fkeydbid,
  2865.             r.fkeyid,
  2866.             r.fkey1,
  2867.             1,
  2868.             r.constid,
  2869.             s.constid
  2870.         from
  2871.             sysreferences r, sysconstraints s
  2872.         where    r.rkeyid = s.id
  2873.             AND (s.status & 0xf) = 1
  2874.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  2875.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  2876.       union all
  2877.         select
  2878.             r.rkeydbid,
  2879.             r.rkeyid,
  2880.             r.rkey2,
  2881.             r.fkeydbid,
  2882.             r.fkeyid,
  2883.             r.fkey2,
  2884.             2,
  2885.             r.constid,
  2886.             s.constid
  2887.         from
  2888.             sysreferences r, sysconstraints s
  2889.         where    r.rkeyid = s.id
  2890.             AND (s.status & 0xf) = 1
  2891.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  2892.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  2893.       union all
  2894.         select
  2895.             r.rkeydbid,
  2896.             r.rkeyid,
  2897.             r.rkey3,
  2898.             r.fkeydbid,
  2899.             r.fkeyid,
  2900.             r.fkey3,
  2901.             3,
  2902.             r.constid,
  2903.             s.constid
  2904.         from
  2905.             sysreferences r, sysconstraints s
  2906.         where    r.rkeyid = s.id
  2907.             AND (s.status & 0xf) = 1
  2908.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  2909.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  2910.       union all
  2911.         select
  2912.             r.rkeydbid,
  2913.             r.rkeyid,
  2914.             r.rkey4,
  2915.             r.fkeydbid,
  2916.             r.fkeyid,
  2917.             r.fkey4,
  2918.             4,
  2919.             r.constid,
  2920.             s.constid
  2921.         from
  2922.             sysreferences r, sysconstraints s
  2923.         where    r.rkeyid = s.id
  2924.             AND (s.status & 0xf) = 1
  2925.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  2926.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  2927.       union all
  2928.         select
  2929.             r.rkeydbid,
  2930.             r.rkeyid,
  2931.             r.rkey5,
  2932.             r.fkeydbid,
  2933.             r.fkeyid,
  2934.             r.fkey5,
  2935.             5,
  2936.             r.constid,
  2937.             s.constid
  2938.         from
  2939.             sysreferences r, sysconstraints s
  2940.         where    r.rkeyid = s.id
  2941.             AND (s.status & 0xf) = 1
  2942.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  2943.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  2944.       union all
  2945.         select
  2946.             r.rkeydbid,
  2947.             r.rkeyid,
  2948.             r.rkey6,
  2949.             r.fkeydbid,
  2950.             r.fkeyid,
  2951.             r.fkey6,
  2952.             6,
  2953.             r.constid,
  2954.             s.constid
  2955.         from
  2956.             sysreferences r, sysconstraints s
  2957.         where    r.rkeyid = s.id
  2958.             AND (s.status & 0xf) = 1
  2959.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  2960.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  2961.       union all
  2962.         select
  2963.             r.rkeydbid,
  2964.             r.rkeyid,
  2965.             r.rkey7,
  2966.             r.fkeydbid,
  2967.             r.fkeyid,
  2968.             r.fkey7,
  2969.             7,
  2970.             r.constid,
  2971.             s.constid
  2972.         from
  2973.             sysreferences r, sysconstraints s
  2974.         where    r.rkeyid = s.id
  2975.             AND (s.status & 0xf) = 1
  2976.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  2977.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  2978.       union all
  2979.         select
  2980.             r.rkeydbid,
  2981.             r.rkeyid,
  2982.             r.rkey8,
  2983.             r.fkeydbid,
  2984.             r.fkeyid,
  2985.             r.fkey8,
  2986.             8,
  2987.             r.constid,
  2988.             s.constid
  2989.         from
  2990.             sysreferences r, sysconstraints s
  2991.         where    r.rkeyid = s.id
  2992.             AND (s.status & 0xf) = 1
  2993.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  2994.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  2995.       union all
  2996.         select
  2997.             r.rkeydbid,
  2998.             r.rkeyid,
  2999.             r.rkey9,
  3000.             r.fkeydbid,
  3001.             r.fkeyid,
  3002.             r.fkey9,
  3003.             9,
  3004.             r.constid,
  3005.             s.constid
  3006.         from
  3007.             sysreferences r, sysconstraints s
  3008.         where    r.rkeyid = s.id
  3009.             AND (s.status & 0xf) = 1
  3010.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3011.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3012.       union all
  3013.         select
  3014.             r.rkeydbid,
  3015.             r.rkeyid,
  3016.             r.rkey10,
  3017.             r.fkeydbid,
  3018.             r.fkeyid,
  3019.             r.fkey10,
  3020.             10,
  3021.             r.constid,
  3022.             s.constid
  3023.         from
  3024.             sysreferences r, sysconstraints s
  3025.         where    r.rkeyid = s.id
  3026.             AND (s.status & 0xf) = 1
  3027.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3028.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3029.       union all
  3030.         select
  3031.             r.rkeydbid,
  3032.             r.rkeyid,
  3033.             r.rkey11,
  3034.             r.fkeydbid,
  3035.             r.fkeyid,
  3036.             r.fkey11,
  3037.             11,
  3038.             r.constid,
  3039.             s.constid
  3040.         from
  3041.             sysreferences r, sysconstraints s
  3042.         where    r.rkeyid = s.id
  3043.             AND (s.status & 0xf) = 1
  3044.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3045.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3046.       union all
  3047.         select
  3048.             r.rkeydbid,
  3049.             r.rkeyid,
  3050.             r.rkey12,
  3051.             r.fkeydbid,
  3052.             r.fkeyid,
  3053.             r.fkey12,
  3054.             12,
  3055.             r.constid,
  3056.             s.constid
  3057.         from
  3058.             sysreferences r, sysconstraints s
  3059.         where    r.rkeyid = s.id
  3060.             AND (s.status & 0xf) = 1
  3061.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3062.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3063.       union all
  3064.         select
  3065.             r.rkeydbid,
  3066.             r.rkeyid,
  3067.             r.rkey13,
  3068.             r.fkeydbid,
  3069.             r.fkeyid,
  3070.             r.fkey13,
  3071.             13,
  3072.             r.constid,
  3073.             s.constid
  3074.         from
  3075.             sysreferences r, sysconstraints s
  3076.         where    r.rkeyid = s.id
  3077.             AND (s.status & 0xf) = 1
  3078.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3079.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3080.       union all
  3081.         select
  3082.             r.rkeydbid,
  3083.             r.rkeyid,
  3084.             r.rkey14,
  3085.             r.fkeydbid,
  3086.             r.fkeyid,
  3087.             r.fkey14,
  3088.             14,
  3089.             r.constid,
  3090.             s.constid
  3091.         from
  3092.             sysreferences r, sysconstraints s
  3093.         where    r.rkeyid = s.id
  3094.             AND (s.status & 0xf) = 1
  3095.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3096.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3097.       union all
  3098.         select
  3099.             r.rkeydbid,
  3100.             r.rkeyid,
  3101.             r.rkey15,
  3102.             r.fkeydbid,
  3103.             r.fkeyid,
  3104.             r.fkey15,
  3105.             15,
  3106.             r.constid,
  3107.             s.constid
  3108.         from
  3109.             sysreferences r, sysconstraints s
  3110.         where    r.rkeyid = s.id
  3111.             AND (s.status & 0xf) = 1
  3112.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3113.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3114.       union all
  3115.         select
  3116.             r.rkeydbid,
  3117.             r.rkeyid,
  3118.             r.rkey16,
  3119.             r.fkeydbid,
  3120.             r.fkeyid,
  3121.             r.fkey16,
  3122.             16,
  3123.             r.constid,
  3124.             s.constid
  3125.         from
  3126.             sysreferences r, sysconstraints s
  3127.         where    r.rkeyid = s.id
  3128.             AND (s.status & 0xf) = 1
  3129.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3130.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3131.  
  3132.     if @order_by_pk = 1 /*    If order by PK fields */
  3133.         select
  3134.             PKTABLE_QUALIFIER = convert(varchar(32),DB_NAME(f.pkdb_id)),
  3135.             PKTABLE_OWNER = convert(varchar(32),USER_NAME(o1.uid)),
  3136.             PKTABLE_NAME = convert(varchar(32),o1.name),
  3137.             PKCOLUMN_NAME = convert(varchar(32),c1.name),
  3138.             FKTABLE_QUALIFIER = convert(varchar(32),DB_NAME(f.fkdb_id)),
  3139.             FKTABLE_OWNER = convert(varchar(32),USER_NAME(o2.uid)),
  3140.             FKTABLE_NAME = convert(varchar(32),o2.name),
  3141.             FKCOLUMN_NAME = convert(varchar(32),c2.name),
  3142.             KEY_SEQ,
  3143.             UPDATE_RULE = convert(smallint,1),
  3144.             DELETE_RULE = convert(smallint,1),
  3145.             FK_NAME = convert(varchar(32),OBJECT_NAME(fk_id)),
  3146.             PK_NAME = convert(varchar(32),OBJECT_NAME(pk_id)),
  3147.             DEFERRABILITY = convert(smallint,7)
  3148.         from #fkeys f,
  3149.             sysobjects o1, sysobjects o2,
  3150.             syscolumns c1, syscolumns c2
  3151.         where    o1.id = f.pktable_id
  3152.             AND o2.id = f.fktable_id
  3153.             AND c1.id = f.pktable_id
  3154.             AND c2.id = f.fktable_id
  3155.             AND c1.colid = f.pkcolid
  3156.             AND c2.colid = f.fkcolid
  3157.         order by 1,2,3,9
  3158.     else        /*    Order by FK fields */
  3159.         select
  3160.             PKTABLE_QUALIFIER = convert(varchar(32),DB_NAME(f.pkdb_id)),
  3161.             PKTABLE_OWNER = convert(varchar(32),USER_NAME(o1.uid)),
  3162.             PKTABLE_NAME = convert(varchar(32),o1.name),
  3163.             PKCOLUMN_NAME = convert(varchar(32),c1.name),
  3164.             FKTABLE_QUALIFIER = convert(varchar(32),DB_NAME(f.fkdb_id)),
  3165.             FKTABLE_OWNER = convert(varchar(32),USER_NAME(o2.uid)),
  3166.             FKTABLE_NAME = convert(varchar(32),o2.name),
  3167.             FKCOLUMN_NAME = convert(varchar(32),c2.name),
  3168.             KEY_SEQ,
  3169.             UPDATE_RULE = convert(smallint,1),
  3170.             DELETE_RULE = convert(smallint,1),
  3171.             FK_NAME = convert(varchar(32),OBJECT_NAME(fk_id)),
  3172.             PK_NAME = convert(varchar(32),OBJECT_NAME(pk_id)),
  3173.             DEFERRABILITY = convert(smallint,7)
  3174.         from #fkeys f,
  3175.             sysobjects o1, sysobjects o2,
  3176.             syscolumns c1, syscolumns c2
  3177.         where    o1.id = f.pktable_id
  3178.             AND o2.id = f.fktable_id
  3179.             AND c1.id = f.pktable_id
  3180.             AND c2.id = f.fktable_id
  3181.             AND c1.colid = f.pkcolid
  3182.             AND c2.colid = f.fkcolid
  3183.         order by 5,6,7,9
  3184. go
  3185.  
  3186. if (charindex('7.00', @@version) = 0)
  3187. begin
  3188.     print ''
  3189.     print ''
  3190.     print 'Warning:'
  3191.     print 'you are installing the stored procedures '
  3192.     print 'on a pre 7.0 SQL Server.'
  3193.     print 'Ignore the following errors.'
  3194. end
  3195. else
  3196.     drop proc sp_fkeys
  3197. go
  3198.  
  3199. /*    Procedure for 7.0 and later servers */
  3200. CREATE PROCEDURE sp_fkeys(
  3201.                @pktable_name        sysname = null,
  3202.                @pktable_owner        sysname = null,
  3203.                @pktable_qualifier    sysname = null,
  3204.                @fktable_name        sysname = null,
  3205.                @fktable_owner        sysname = null,
  3206.                @fktable_qualifier    sysname = null )
  3207. as
  3208.     set nocount on
  3209.     DECLARE @pktable_id            int
  3210.     DECLARE @pkfull_table_name    nvarchar(257) /* 2*128 + 1 */
  3211.     DECLARE @fktable_id            int
  3212.     DECLARE @fkfull_table_name    nvarchar(257) /* 2*128 + 1 */
  3213.     declare    @order_by_pk        int
  3214.  
  3215.     /* select 'XXX starting table creation' */
  3216.  
  3217.     create table #fkeysall(
  3218.             rkeyid int NOT NULL,
  3219.             rkey1 int NOT NULL,
  3220.                 rkey2 int NOT NULL,
  3221.                 rkey3 int NOT NULL,
  3222.                 rkey4 int NOT NULL,
  3223.                 rkey5 int NOT NULL,
  3224.                 rkey6 int NOT NULL,
  3225.                 rkey7 int NOT NULL,
  3226.                 rkey8 int NOT NULL,
  3227.                 rkey9 int NOT NULL,
  3228.                 rkey10 int NOT NULL,
  3229.                 rkey11 int NOT NULL,
  3230.                 rkey12 int NOT NULL,
  3231.                 rkey13 int NOT NULL,
  3232.                 rkey14 int NOT NULL,
  3233.                 rkey15 int NOT NULL,
  3234.                 rkey16 int NOT NULL,
  3235.             fkeyid int NOT NULL,
  3236.             fkey1 int NOT NULL,
  3237.                 fkey2 int NOT NULL,
  3238.                 fkey3 int NOT NULL,
  3239.                 fkey4 int NOT NULL,
  3240.                 fkey5 int NOT NULL,
  3241.                 fkey6 int NOT NULL,
  3242.                 fkey7 int NOT NULL,
  3243.                 fkey8 int NOT NULL,
  3244.                 fkey9 int NOT NULL,
  3245.                 fkey10 int NOT NULL,
  3246.                 fkey11 int NOT NULL,
  3247.                 fkey12 int NOT NULL,
  3248.                 fkey13 int NOT NULL,
  3249.                 fkey14 int NOT NULL,
  3250.                 fkey15 int NOT NULL,
  3251.                 fkey16 int NOT NULL,
  3252.             constid int NOT NULL,
  3253.             name sysname NOT NULL)
  3254.  
  3255.     create table #fkeys(
  3256.             pktable_id        int NOT NULL,
  3257.             pkcolid         int NOT NULL,
  3258.             fktable_id        int NOT NULL,
  3259.             fkcolid         int NOT NULL,
  3260.             KEY_SEQ         smallint NOT NULL,
  3261.             fk_id            int NOT NULL,
  3262.             PK_NAME            sysname NOT NULL)
  3263.  
  3264.     create table #fkeysout(
  3265.             PKTABLE_QUALIFIER sysname NULL,
  3266.             PKTABLE_OWNER sysname NULL,
  3267.             PKTABLE_NAME sysname NOT NULL,
  3268.             PKCOLUMN_NAME sysname NOT NULL,
  3269.             FKTABLE_QUALIFIER sysname NULL,
  3270.             FKTABLE_OWNER sysname NULL,
  3271.             FKTABLE_NAME sysname NOT NULL,
  3272.             FKCOLUMN_NAME sysname NOT NULL,
  3273.             KEY_SEQ smallint NOT NULL,
  3274.             UPDATE_RULE smallint NULL,
  3275.             DELETE_RULE smallint NULL,
  3276.             FK_NAME sysname NULL,
  3277.             PK_NAME sysname NULL,
  3278.             DEFERRABILITY smallint null)
  3279.  
  3280.     /* select 'XXX starting parameter analysis' */
  3281.  
  3282.     select  @order_by_pk = 0
  3283.  
  3284.     if (@pktable_name is null) and (@fktable_name is null)
  3285.     begin    /* If neither primary key nor foreign key table names given */
  3286.         raiserror (15252,-1,-1)
  3287.         return
  3288.     end
  3289.     if @fktable_qualifier is not null
  3290.     begin
  3291.         if db_name() <> @fktable_qualifier
  3292.         begin    /* If qualifier doesn't match current database */
  3293.             raiserror (15250, -1,-1)
  3294.             return
  3295.         end
  3296.     end
  3297.     if @pktable_qualifier is not null
  3298.     begin
  3299.         if db_name() <> @pktable_qualifier
  3300.         begin    /* If qualifier doesn't match current database */
  3301.             raiserror (15250, -1,-1)
  3302.             return
  3303.         end
  3304.     end
  3305.  
  3306.     if @pktable_owner is null
  3307.     begin    /* If unqualified primary key table name */
  3308.         SELECT @pkfull_table_name = quotename(@pktable_name)
  3309.     end
  3310.     else
  3311.     begin    /* Qualified primary key table name */
  3312.         if @pktable_owner = ''
  3313.         begin    /* If empty owner name */
  3314.             SELECT @pkfull_table_name = quotename(@pktable_owner)
  3315.         end
  3316.         else
  3317.         begin
  3318.             SELECT @pkfull_table_name = quotename(@pktable_owner) +
  3319.                 '.' + quotename(@pktable_name)
  3320.         end
  3321.     end
  3322.     /*    Get Object ID */
  3323.     SELECT @pktable_id = object_id(@pkfull_table_name)
  3324.  
  3325.     if @fktable_owner is null
  3326.     begin    /* If unqualified foreign key table name */
  3327.         SELECT @fkfull_table_name = quotename(@fktable_name)
  3328.     end
  3329.     else
  3330.     begin    /* Qualified foreign key table name */
  3331.         if @fktable_owner = ''
  3332.         begin    /* If empty owner name */
  3333.             SELECT @fkfull_table_name = quotename(@fktable_owner)
  3334.         end
  3335.         else
  3336.         begin
  3337.             SELECT @fkfull_table_name = quotename(@fktable_owner) +
  3338.                 '.' + quotename(@fktable_name)
  3339.         end
  3340.     end
  3341.     /*    Get Object ID */
  3342.     SELECT @fktable_id = object_id(@fkfull_table_name)
  3343.  
  3344.     if @fktable_name is not null
  3345.     begin
  3346.         if @fktable_id is null
  3347.             SELECT @fktable_id = 0    /* fk table not found, empty result */
  3348.     end
  3349.  
  3350.     if @pktable_name is null
  3351.     begin /*  If table name not supplied, match all */
  3352.         select @order_by_pk = 1
  3353.     end
  3354.     else
  3355.     begin
  3356.         if @pktable_id is null
  3357.         begin
  3358.             SELECT @pktable_id = 0    /* pk table not found, empty result */
  3359.         end
  3360.     end
  3361.  
  3362.     /*    SQL Server supports upto 16 PK/FK relationships between 2 tables */
  3363.     /*    Process syskeys for each relationship */
  3364.     /*  First, attempt to get all 16 keys for each rel'ship, then sort
  3365.         them out with a 16-way "insert select ... union select ..." */
  3366.  
  3367.     /* select 'XXX starting data analysis' */
  3368.  
  3369.     insert into #fkeysall
  3370.         select
  3371.             r.rkeyid,
  3372.             r.rkey1, r.rkey2, r.rkey3, r.rkey4,
  3373.                 r.rkey5, r.rkey6, r.rkey7, r.rkey8,
  3374.                 r.rkey9, r.rkey10, r.rkey11, r.rkey12,
  3375.                 r.rkey13, r.rkey14, r.rkey15, r.rkey16,
  3376.             r.fkeyid,
  3377.             r.fkey1, r.fkey2, r.fkey3, r.fkey4,
  3378.                 r.fkey5, r.fkey6, r.fkey7, r.fkey8,
  3379.                 r.fkey9, r.fkey10, r.fkey11, r.fkey12,
  3380.                 r.fkey13, r.fkey14, r.fkey15, r.fkey16,
  3381.             r.constid,
  3382.             i.name
  3383.         from
  3384.             sysreferences r, sysobjects o, sysindexes i
  3385.         where    r.constid = o.id
  3386.             AND o.xtype = 'F'
  3387.             AND r.rkeyindid = i.indid
  3388.             AND r.rkeyid = i.id
  3389.             AND r.rkeyid between isnull(@pktable_id, 0)
  3390.                             and isnull(@pktable_id, 0x7fffffff)
  3391.             AND r.fkeyid between isnull(@fktable_id, 0)
  3392.                             and isnull(@fktable_id, 0x7fffffff)
  3393.  
  3394.     /* select count (*) as 'XXX countall' from #fkeysall */
  3395.  
  3396.     insert into #fkeys
  3397.             select rkeyid, rkey1, fkeyid, fkey1, 1, constid, name
  3398.             from #fkeysall
  3399.         union all
  3400.             select rkeyid, rkey2, fkeyid, fkey2, 2, constid, name
  3401.             from #fkeysall
  3402.         union all
  3403.             select rkeyid, rkey3, fkeyid, fkey3, 3, constid, name
  3404.             from #fkeysall
  3405.         union all
  3406.             select rkeyid, rkey4, fkeyid, fkey4, 4, constid, name
  3407.             from #fkeysall
  3408.         union all
  3409.             select rkeyid, rkey5, fkeyid, fkey5, 5, constid, name
  3410.             from #fkeysall
  3411.         union all
  3412.             select rkeyid, rkey6, fkeyid, fkey6, 6, constid, name
  3413.             from #fkeysall
  3414.         union all
  3415.             select rkeyid, rkey7, fkeyid, fkey7, 7, constid, name
  3416.             from #fkeysall
  3417.         union all
  3418.             select rkeyid, rkey8, fkeyid, fkey8, 8, constid, name
  3419.             from #fkeysall
  3420.         union all
  3421.             select rkeyid, rkey9, fkeyid, fkey9, 9, constid, name
  3422.             from #fkeysall
  3423.         union all
  3424.             select rkeyid, rkey10, fkeyid, fkey10, 10, constid, name
  3425.             from #fkeysall
  3426.         union all
  3427.             select rkeyid, rkey11, fkeyid, fkey11, 11, constid, name
  3428.             from #fkeysall
  3429.         union all
  3430.             select rkeyid, rkey12, fkeyid, fkey12, 12, constid, name
  3431.             from #fkeysall
  3432.         union all
  3433.             select rkeyid, rkey13, fkeyid, fkey13, 13, constid, name
  3434.             from #fkeysall
  3435.         union all
  3436.             select rkeyid, rkey14, fkeyid, fkey14, 14, constid, name
  3437.             from #fkeysall
  3438.         union all
  3439.             select rkeyid, rkey15, fkeyid, fkey15, 15, constid, name
  3440.             from #fkeysall
  3441.         union all
  3442.             select rkeyid, rkey16, fkeyid, fkey16, 16, constid, name
  3443.             from #fkeysall
  3444.  
  3445.     /* select count (*) as 'XXX count' from #fkeys */
  3446.  
  3447.     insert into #fkeysout
  3448.         select
  3449.             PKTABLE_QUALIFIER = convert(sysname,db_name()),
  3450.             PKTABLE_OWNER = convert(sysname,USER_NAME(o1.uid)),
  3451.             PKTABLE_NAME = convert(sysname,o1.name),
  3452.             PKCOLUMN_NAME = convert(sysname,c1.name),
  3453.             FKTABLE_QUALIFIER = convert(sysname,db_name()),
  3454.             FKTABLE_OWNER = convert(sysname,USER_NAME(o2.uid)),
  3455.             FKTABLE_NAME = convert(sysname,o2.name),
  3456.             FKCOLUMN_NAME = convert(sysname,c2.name),
  3457.             KEY_SEQ,
  3458.             UPDATE_RULE = convert(smallint,1),
  3459.             DELETE_RULE = convert(smallint,1),
  3460.             FK_NAME = convert(sysname,OBJECT_NAME(fk_id)),
  3461.             PK_NAME,
  3462.             DEFERRABILITY = 7    /* SQL_NOT_DEFERRABLE */
  3463.         from #fkeys f,
  3464.             sysobjects o1, sysobjects o2,
  3465.             syscolumns c1, syscolumns c2
  3466.         where    o1.id = f.pktable_id
  3467.             AND o2.id = f.fktable_id
  3468.             AND c1.id = f.pktable_id
  3469.             AND c2.id = f.fktable_id
  3470.             AND c1.colid = f.pkcolid
  3471.             AND c2.colid = f.fkcolid
  3472.  
  3473.     /* select count (*) as 'XXX countout' from #fkeysout */
  3474.  
  3475.     if @order_by_pk = 1 /*    If order by PK fields */
  3476.         select
  3477.             PKTABLE_QUALIFIER, PKTABLE_OWNER, PKTABLE_NAME, PKCOLUMN_NAME,
  3478.             FKTABLE_QUALIFIER, FKTABLE_OWNER, FKTABLE_NAME, FKCOLUMN_NAME,
  3479.             KEY_SEQ, UPDATE_RULE, DELETE_RULE, FK_NAME, PK_NAME, DEFERRABILITY
  3480.         from #fkeysout
  3481.         order by 1,2,3,9
  3482.     else        /*    Order by FK fields */
  3483.         select
  3484.             PKTABLE_QUALIFIER, PKTABLE_OWNER, PKTABLE_NAME, PKCOLUMN_NAME,
  3485.             FKTABLE_QUALIFIER, FKTABLE_OWNER, FKTABLE_NAME, FKCOLUMN_NAME,
  3486.             KEY_SEQ, UPDATE_RULE, DELETE_RULE, FK_NAME, PK_NAME, DEFERRABILITY
  3487.         from #fkeysout
  3488.         order by 5,6,7,9
  3489. go
  3490.  
  3491. grant execute on sp_fkeys to public
  3492. go
  3493.  
  3494. dump tran master with no_log
  3495. go
  3496.  
  3497. print 'creating sp_pkeys'
  3498. go
  3499.  
  3500. /*    Procedure for pre-6.0 server */
  3501. CREATE PROCEDURE sp_pkeys(
  3502.                @table_name        varchar(32),
  3503.                @table_owner     varchar(32) = null,
  3504.                @table_qualifier varchar(32) = null )
  3505. as
  3506.     set nocount on
  3507.     if @table_qualifier is not null
  3508.     begin
  3509.         if db_name() <> @table_qualifier
  3510.         begin    /* If qualifier doesn't match current database */
  3511.             raiserror 20001 '~~Rush_5~~'
  3512.             return
  3513.         end
  3514.     end
  3515.     if @table_owner is null /*    If owner not supplied, match all */
  3516.         select @table_owner = '%'
  3517.     if @@trancount <> 0
  3518.     begin    /* If inside a transaction */
  3519.         raiserror 20003 '~~Rush_35~~'
  3520.         return
  3521.     end
  3522.  
  3523.     create table #pkeys(
  3524.              TABLE_QUALIFIER varchar(32) NULL,
  3525.              TABLE_OWNER     varchar(32) NULL,
  3526.              TABLE_NAME      varchar(32) NOT NULL,
  3527.              COLUMN_NAME     varchar(32) NOT NULL,
  3528.              KEY_SEQ         smallint NOT NULL)
  3529.  
  3530.     /*    SQL Server supports upto 8 PK/FK relationships between 2 tables */
  3531.     /*    Process syskeys for each relationship */
  3532.     /*    The inserts below adds a row to the temp table for each of the
  3533.         8 possible relationships */
  3534.     insert into #pkeys
  3535.         select
  3536.             db_name(),
  3537.             (select user_name(uid) from sysobjects o where o.id = k.id),
  3538.             object_name(k.id),
  3539.             c.name,
  3540.             1
  3541.         from
  3542.             syskeys k, syscolumns c
  3543.         where
  3544.             c.id = k.id
  3545.             and k.type = 1    /* Primary type key */
  3546.             and c.colid = k.key1
  3547.     if (@@rowcount = 0)
  3548.         goto done
  3549.  
  3550.     insert into #pkeys
  3551.         select
  3552.             db_name(),
  3553.             (select user_name(uid) from sysobjects o where o.id = k.id),
  3554.             object_name(k.id),
  3555.             c.name,
  3556.             2
  3557.         from
  3558.             syskeys k, syscolumns c
  3559.         where
  3560.             c.id = k.id
  3561.             and k.type = 1    /* Primary type key */
  3562.             and c.colid = key2
  3563.     if (@@rowcount = 0)
  3564.         goto done
  3565.  
  3566.     insert into #pkeys
  3567.         select
  3568.             db_name(),
  3569.             (select user_name(uid) from sysobjects o where o.id = k.id),
  3570.             object_name(k.id),
  3571.             c.name,
  3572.             3
  3573.         from
  3574.             syskeys k, syscolumns c
  3575.         where
  3576.             c.id = k.id
  3577.             and k.type = 1    /* Primary type key */
  3578.             and c.colid = key3
  3579.     if (@@rowcount = 0)
  3580.         goto done
  3581.  
  3582.     insert into #pkeys
  3583.         select
  3584.             db_name(),
  3585.             (select user_name(uid) from sysobjects o where o.id = k.id),
  3586.             object_name(k.id),
  3587.             c.name,
  3588.             4
  3589.         from
  3590.             syskeys k, syscolumns c
  3591.         where
  3592.             c.id = k.id
  3593.             and k.type = 1    /* Primary type key */
  3594.             and c.colid = key4
  3595.     if (@@rowcount = 0)
  3596.         goto done
  3597.  
  3598.     insert into #pkeys
  3599.         select
  3600.             db_name(),
  3601.             (select user_name(uid) from sysobjects o where o.id = k.id),
  3602.             object_name(k.id),
  3603.             c.name,
  3604.             5
  3605.         from
  3606.             syskeys k, syscolumns c
  3607.         where
  3608.             c.id = k.id
  3609.             and k.type = 1    /* Primary type key */
  3610.             and c.colid = key5
  3611.     if (@@rowcount = 0)
  3612.         goto done
  3613.  
  3614.     insert into #pkeys
  3615.         select
  3616.             db_name(),
  3617.             (select user_name(uid) from sysobjects o where o.id = k.id),
  3618.             object_name(k.id),
  3619.             c.name,
  3620.             6
  3621.         from
  3622.             syskeys k, syscolumns c
  3623.         where
  3624.             c.id = k.id
  3625.             and k.type = 1    /* Primary type key */
  3626.             and c.colid = key6
  3627.     if (@@rowcount = 0)
  3628.         goto done
  3629.  
  3630.     insert into #pkeys
  3631.         select
  3632.             db_name(),
  3633.             (select user_name(uid) from sysobjects o where o.id = k.id),
  3634.             object_name(k.id),
  3635.             c.name,
  3636.             7
  3637.         from
  3638.             syskeys k, syscolumns c
  3639.         where
  3640.             c.id = k.id
  3641.             and k.type = 1    /* Primary type key */
  3642.             and c.colid = key7
  3643.     if (@@rowcount = 0)
  3644.         goto done
  3645.  
  3646.     insert into #pkeys
  3647.          select
  3648.              db_name(),
  3649.              (select user_name(uid) from sysobjects o where o.id = k.id),
  3650.              object_name(k.id),
  3651.              c.name,
  3652.              8
  3653.          from
  3654.              syskeys k, syscolumns c
  3655.          where
  3656.              c.id = k.id
  3657.              and k.type = 1 /* Primary type key */
  3658.              and c.colid = key8
  3659.  
  3660.     done:
  3661.     select
  3662.         TABLE_QUALIFIER,
  3663.         TABLE_OWNER,
  3664.         TABLE_NAME,
  3665.         COLUMN_NAME,
  3666.         KEY_SEQ,
  3667.         PK_NAME = convert(varchar(32),null)
  3668.     from #pkeys
  3669.     where TABLE_NAME = @table_name
  3670.         and TABLE_OWNER like @table_owner
  3671.     order by 1, 2, 3, 5
  3672. go
  3673.  
  3674. if (charindex('6.00', @@version) = 0 and
  3675.     charindex('6.50', @@version) = 0 and
  3676.     charindex('7.00', @@version) = 0)
  3677. begin
  3678.     print ''
  3679.     print ''
  3680.     print 'Warning:'
  3681.     print 'you are installing the stored procedures '
  3682.     print 'on a pre 6.0 SQL Server.'
  3683.     print 'Ignore the following error.'
  3684. end
  3685. else
  3686.     drop proc sp_pkeys
  3687. go
  3688.  
  3689. /*    Procedure for 6.0 and 6.50 servers */
  3690. CREATE PROCEDURE sp_pkeys(
  3691.                @table_name        sysname,
  3692.                @table_owner     sysname = null,
  3693.                @table_qualifier sysname = null )
  3694. as
  3695.     DECLARE @table_id            int
  3696.     DECLARE @full_table_name    varchar(255) /* 2*128 + 1 */
  3697.  
  3698.     if @table_qualifier is not null
  3699.     begin
  3700.         if db_name() <> @table_qualifier
  3701.         begin    /* If qualifier doesn't match current database */
  3702.             raiserror (15250, -1,-1)
  3703.             return
  3704.         end
  3705.     end
  3706.     if @table_owner is null
  3707.     begin    /* If unqualified table name */
  3708.         SELECT @full_table_name = @table_name
  3709.     end
  3710.     else
  3711.     begin    /* Qualified table name */
  3712.         if @table_owner = ''
  3713.         begin    /* If empty owner name */
  3714.             SELECT @full_table_name = @table_owner
  3715.         end
  3716.         else
  3717.         begin
  3718.             SELECT @full_table_name = @table_owner + '.' + @table_name
  3719.         end
  3720.     end
  3721.     /*    Get Object ID */
  3722.     SELECT @table_id = object_id(@full_table_name)
  3723.  
  3724.     select
  3725.         TABLE_QUALIFIER = convert(sysname,db_name()),
  3726.         TABLE_OWNER = convert(sysname,user_name(o.uid)),
  3727.         TABLE_NAME = convert(sysname,o.name),
  3728.         COLUMN_NAME = convert(sysname,c.name),
  3729.         KEY_SEQ = convert(smallint,c1.colid),
  3730.         PK_NAME = convert(sysname,i.name)
  3731.     from
  3732.         sysindexes i, syscolumns c, sysobjects o, syscolumns c1
  3733.     where
  3734.         o.id = @table_id
  3735.         and o.id = c.id
  3736.         and o.id = i.id
  3737.         and (i.status & 0x800) = 0x800
  3738.         and c.name = index_col (@full_table_name, i.indid, c1.colid)
  3739.         and c1.colid <= i.keycnt    /* create rows from 1 to keycnt */
  3740.         and c1.id = @table_id
  3741.     order by 1, 2, 3, 5
  3742. go
  3743.  
  3744. if (charindex('7.00', @@version) = 0)
  3745. begin
  3746.     print ''
  3747.     print ''
  3748.     print 'Warning:'
  3749.     print 'you are installing the stored procedures '
  3750.     print 'on a pre 7.0 SQL Server.'
  3751.     print 'Ignore the following errors.'
  3752. end
  3753. else
  3754.     drop proc sp_pkeys
  3755. go
  3756.  
  3757. /*    Procedure for 7.0 and later servers */
  3758. CREATE PROCEDURE sp_pkeys(
  3759.                @table_name        sysname,
  3760.                @table_owner     sysname = null,
  3761.                @table_qualifier sysname = null )
  3762. as
  3763.     DECLARE @table_id            int
  3764.     DECLARE @full_table_name    varchar(255) /* 2*128 + 1 */
  3765.  
  3766.     if @table_qualifier is not null
  3767.     begin
  3768.         if db_name() <> @table_qualifier
  3769.         begin    /* If qualifier doesn't match current database */
  3770.             raiserror (15250, -1,-1)
  3771.             return
  3772.         end
  3773.     end
  3774.     if @table_owner is null
  3775.     begin    /* If unqualified table name */
  3776.         SELECT @full_table_name = quotename(@table_name)
  3777.     end
  3778.     else
  3779.     begin    /* Qualified table name */
  3780.         if @table_owner = ''
  3781.         begin    /* If empty owner name */
  3782.             SELECT @full_table_name = quotename(@table_owner)
  3783.         end
  3784.         else
  3785.         begin
  3786.             SELECT @full_table_name = quotename(@table_owner) +
  3787.                 '.' + quotename(@table_name)
  3788.         end
  3789.     end
  3790.     /*    Get Object ID */
  3791.     SELECT @table_id = object_id(@full_table_name)
  3792.  
  3793.     select
  3794.         TABLE_QUALIFIER = convert(sysname,db_name()),
  3795.         TABLE_OWNER = convert(sysname,user_name(o.uid)),
  3796.         TABLE_NAME = convert(sysname,o.name),
  3797.         COLUMN_NAME = convert(sysname,c.name),
  3798.         KEY_SEQ = convert(smallint,c1.colid),
  3799.         PK_NAME = convert(sysname,i.name)
  3800.     from
  3801.         sysindexes i, syscolumns c, sysobjects o, syscolumns c1
  3802.     where
  3803.         o.id = @table_id
  3804.         and o.id = c.id
  3805.         and o.id = i.id
  3806.         and (i.status & 0x800) = 0x800
  3807.         and c.name = index_col (@full_table_name, i.indid, c1.colid)
  3808.         and c1.colid <= i.keycnt    /* create rows from 1 to keycnt */
  3809.         and c1.id = @table_id
  3810.     order by 1, 2, 3, 5
  3811. go
  3812.  
  3813. grant execute on sp_pkeys to public
  3814. go
  3815.  
  3816. dump tran master with no_log
  3817. go
  3818.  
  3819. print 'creating sp_server_info'
  3820. go
  3821.  
  3822. create proc sp_server_info (
  3823.             @attribute_id  int = null)
  3824. as
  3825.     if @attribute_id is not null
  3826.         select *
  3827.         from master.dbo.spt_server_info
  3828.         where attribute_id = @attribute_id
  3829.     else
  3830.         select *
  3831.         from master.dbo.spt_server_info
  3832.         order by attribute_id
  3833. go
  3834.  
  3835. grant execute on sp_server_info to public
  3836. go
  3837.  
  3838. dump tran master with no_log
  3839. go
  3840.  
  3841. print 'creating sp_special_columns'
  3842. go
  3843.  
  3844. /*    Procedure for pre-6.0 server */
  3845. CREATE PROCEDURE sp_special_columns (
  3846.                  @table_name        varchar(32),
  3847.                  @table_owner        varchar(32) = null,
  3848.                  @table_qualifier    varchar(32) = null,
  3849.                  @col_type            char(1) = 'R',
  3850.                  @scope                char(1) = 'T',
  3851.                  @nullable            char(1) = 'U',
  3852.                  @ODBCVer            int = 2)
  3853. AS
  3854.     DECLARE @indid                int
  3855.     DECLARE @table_id            int
  3856.     DECLARE @full_table_name    varchar(65) /* 2*32+1 */
  3857.     DECLARE @scopeout            smallint
  3858.  
  3859.     if @col_type not in ('R','V')
  3860.     begin
  3861.         raiserror 20002 '~~Rush_42~~'
  3862.         return
  3863.     end
  3864.  
  3865.     if @scope = 'C'
  3866.         select @scopeout = 0
  3867.     else if @scope = 'T'
  3868.         select @scopeout = 1
  3869.     else
  3870.     begin
  3871.         raiserror 20002 '~~Rush_43~~'
  3872.         return
  3873.     end
  3874.  
  3875.     if @nullable not in ('U','O')
  3876.     begin
  3877.         raiserror 20002 '~~Rush_44~~'
  3878.         return
  3879.     end
  3880.  
  3881.     if @table_qualifier is not null
  3882.     begin
  3883.         if db_name() <> @table_qualifier
  3884.         begin /* If qualifier doesn't match current database */
  3885.             raiserror 20001 '~~Rush_5~~'
  3886.             return
  3887.         end
  3888.     end
  3889.     if @table_owner is null
  3890.     begin     /* If unqualified table name */
  3891.         SELECT @full_table_name = @table_name
  3892.     end
  3893.     else
  3894.     begin    /* Qualified table name */
  3895.         SELECT @full_table_name = @table_owner + '.' + @table_name
  3896.     end
  3897.     /*    Get Object ID */
  3898.     SELECT @table_id = object_id(@full_table_name)
  3899.  
  3900.     if @col_type = 'V'
  3901.     BEGIN /* if ROWVER, just run that query */
  3902.         SELECT
  3903.             SCOPE = convert(smallint,NULL),
  3904.             COLUMN_NAME = convert(varchar(32),c.name),
  3905.             DATA_TYPE = convert(smallint, -3),
  3906.             TYPE_NAME = t.name,
  3907.             "PRECISION" = convert(int,8),
  3908.             LENGTH = convert(int,8),
  3909.             SCALE = convert(smallint, NULL),
  3910.             PSEUDO_COLUMN = convert(smallint,1)
  3911.         FROM
  3912.             systypes t, syscolumns c
  3913.         WHERE
  3914.             c.id = @table_id
  3915.             AND c.usertype = 80 /*    TIMESTAMP */
  3916.             AND t.usertype = 80 /*    TIMESTAMP */
  3917.         RETURN
  3918.     END
  3919.  
  3920.     /* ROWID, now find the id of the 'best' index for this table */
  3921.  
  3922.     IF @nullable = 'O'    /* Don't include any indexes that contain
  3923.                            nullable columns. */
  3924.  
  3925.             SELECT @indid = MIN(indid)
  3926.                 FROM sysindexes i,syscolumns c,syscolumns c2
  3927.                 WHERE
  3928.                     i.status&2 = 2        /*    If Unique Index */
  3929.                      AND c.id = i.id
  3930.                      AND c2.id = c.id
  3931.                      AND c2.colid < i.keycnt + (i.status&16)/16
  3932.                     AND i.id = @table_id
  3933.                     AND indid > 0        /*    Eliminate Table Row */
  3934.                     AND c.name = index_col(@table_name,i.indid,c2.colid)
  3935.                     GROUP BY indid HAVING SUM(c.status&8) = 0
  3936.  
  3937.     ELSE    /* Include indexes that are partially nullable. */
  3938.  
  3939.         SELECT @indid = MIN(indid)
  3940.             FROM sysindexes i
  3941.             WHERE
  3942.                 status&2 = 2        /*    If Unique Index */
  3943.                 AND id = @table_id
  3944.                 AND indid > 0        /*    Eliminate Table Row */
  3945.  
  3946.     SELECT
  3947.         SCOPE = @scopeout,
  3948.         COLUMN_NAME = convert(varchar(32),INDEX_COL(@full_table_name,indid,c2.colid)),
  3949.         d.DATA_TYPE,
  3950.         TYPE_NAME = t.name,
  3951.         "PRECISION" = isnull(d.data_precision, convert(int,c.length)),
  3952.         LENGTH = isnull(d.length, convert(int,c.length)),
  3953.         SCALE = d.numeric_scale,
  3954.         PSEUDO_COLUMN = convert(smallint,1)
  3955.     FROM
  3956.         sysindexes x,
  3957.         syscolumns c,
  3958.         master.dbo.spt_datatype_info d,
  3959.         systypes t,
  3960.         syscolumns c2    /* Self-join to generate list of index columns and */
  3961.                         /* to extract datatype names */
  3962.     WHERE
  3963.         x.id = @table_id
  3964.         AND c.name = INDEX_COL(@full_table_name,@indid,c2.colid)
  3965.         AND c.id = x.id
  3966.         AND c2.id = x.id
  3967.         AND c2.colid < keycnt+(x.status&16)/16
  3968.         AND x.indid = @indid
  3969.         AND t.type = d.ss_dtype
  3970.         AND c.length = d.fixlen
  3971.         AND c.usertype = t.usertype
  3972.  
  3973. go
  3974.  
  3975. if (charindex('6.00', @@version) = 0 and
  3976.     charindex('6.50', @@version) = 0 and
  3977.     charindex('7.00', @@version) = 0)
  3978. begin
  3979.     print ''
  3980.     print ''
  3981.     print 'Warning:'
  3982.     print 'you are installing the stored procedures '
  3983.     print 'on a pre 6.0 SQL Server.'
  3984.     print 'Ignore the following errors.'
  3985. end
  3986. else
  3987.     drop proc sp_special_columns
  3988. go
  3989.  
  3990. /*    Procedure for 6.0 and 6.50 servers */
  3991. CREATE PROCEDURE sp_special_columns (
  3992.                  @table_name        varchar(32),
  3993.                  @table_owner        varchar(32) = null,
  3994.                  @table_qualifier    varchar(32) = null,
  3995.                  @col_type            char(1) = 'R',
  3996.                  @scope                char(1) = 'T',
  3997.                  @nullable            char(1) = 'U',
  3998.                  @ODBCVer            int = 2)
  3999. AS
  4000.     DECLARE @indid                int
  4001.     DECLARE @table_id            int
  4002.     DECLARE @full_table_name    varchar(65) /* 2*32+1 */
  4003.     DECLARE @scopeout            smallint
  4004.  
  4005.     if @col_type not in ('R','V')
  4006.     begin
  4007.         raiserror (15251,-1,-1,'col_type','''R'' or ''V''')
  4008.         return
  4009.     end
  4010.  
  4011.     if @scope = 'C'
  4012.         select @scopeout = 0
  4013.     else if @scope = 'T'
  4014.         select @scopeout = 1
  4015.     else
  4016.     begin
  4017.         raiserror (15251,-1,-1,'scope','''C'' or ''T''')
  4018.         return
  4019.     end
  4020.  
  4021.     if @nullable not in ('U','O')
  4022.     begin
  4023.         raiserror (15251,-1,-1,'nullable','''U'' or ''O''')
  4024.         return
  4025.     end
  4026.  
  4027.     if @table_qualifier is not null
  4028.     begin
  4029.         if db_name() <> @table_qualifier
  4030.         begin /* If qualifier doesn't match current database */
  4031.             raiserror (15250, -1,-1)
  4032.             return
  4033.         end
  4034.     end
  4035.     if @table_owner is null
  4036.     begin     /* If unqualified table name */
  4037.         SELECT @full_table_name = @table_name
  4038.     end
  4039.     else
  4040.     begin     /* Qualified table name */
  4041.         SELECT @full_table_name = @table_owner + '.' + @table_name
  4042.     end
  4043.     /*    Get Object ID */
  4044.     SELECT @table_id = object_id(@full_table_name)
  4045.  
  4046.     if @col_type = 'V'
  4047.     BEGIN /* if ROWVER, just run that query */
  4048.         SELECT
  4049.             SCOPE = convert(smallint,NULL),
  4050.             COLUMN_NAME = convert(varchar(32),c.name),
  4051.             DATA_TYPE = convert(smallint, -2),
  4052.             TYPE_NAME = t.name,
  4053.             "PRECISION" = convert(int,8),
  4054.             LENGTH = convert(int,8),
  4055.             SCALE = convert(smallint, NULL),
  4056.             PSEUDO_COLUMN = convert(smallint,1)
  4057.         FROM
  4058.             systypes t, syscolumns c
  4059.         WHERE
  4060.             c.id = @table_id
  4061.             AND c.usertype = 80 /*    TIMESTAMP */
  4062.             AND t.usertype = 80 /*    TIMESTAMP */
  4063.         RETURN
  4064.     END
  4065.  
  4066.     /* ROWID, now find the id of the 'best' index for this table */
  4067.  
  4068.     IF @nullable = 'O'    /* Don't include any indexes that contain
  4069.                            nullable columns. */
  4070.  
  4071.         SELECT @indid = MIN(indid)
  4072.             FROM sysindexes i,syscolumns c,syscolumns c2
  4073.             WHERE
  4074.                 i.status&2 = 2        /*    If Unique Index */
  4075.                 AND c.id = i.id
  4076.                 AND c2.id = c.id
  4077.                 AND c2.colid < i.keycnt + (i.status&16)/16
  4078.                 AND i.id = @table_id
  4079.                 AND indid > 0        /*    Eliminate Table Row */
  4080.                 AND c.name = index_col(@table_name,i.indid,c2.colid)
  4081.                 GROUP BY indid HAVING SUM(c.status&8) = 0
  4082.  
  4083.     ELSE    /* Include indexes that are partially nullable. */
  4084.  
  4085.         SELECT @indid = MIN(indid)
  4086.             FROM sysindexes i
  4087.             WHERE
  4088.                 status&2 = 2        /*    If Unique Index */
  4089.                 AND id = @table_id
  4090.                 AND indid > 0        /*    Eliminate Table Row */
  4091.  
  4092.     SELECT
  4093.         SCOPE = @scopeout,
  4094.         COLUMN_NAME = convert(varchar(32),INDEX_COL(@full_table_name,indid,c2.colid)),
  4095.         d.DATA_TYPE,
  4096.         convert(varchar(32),case
  4097.             when (t.usertype > 100 or t.usertype in (18,80))
  4098.                 then t.name
  4099.             else d.TYPE_NAME
  4100.         end) TYPE_NAME,
  4101.         convert(int,case
  4102.             when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  4103.             else isnull(convert(int,c.prec), 2147483647)
  4104.         end) "PRECISION",
  4105.         convert(int,case
  4106.             when d.ss_dtype IN (106, 108, 55, 63) then    /* decimal/numeric types */
  4107.                 convert(int,c.prec+2)
  4108.             else
  4109.                 isnull(d.length, c.length)
  4110.         end) LENGTH,
  4111.         SCALE = convert(smallint, c.scale),
  4112.         PSEUDO_COLUMN = convert(smallint,1)
  4113.     FROM
  4114.         sysindexes x,
  4115.         syscolumns c,
  4116.         master.dbo.spt_datatype_info d,
  4117.         systypes t,
  4118.         syscolumns c2    /* Self-join to generate list of index columns and */
  4119.                         /* to extract datatype names */
  4120.     WHERE
  4121.         x.id = @table_id
  4122.         AND c.name = INDEX_COL(@full_table_name,@indid,c2.colid)
  4123.         AND c.id = x.id
  4124.         AND c2.id = x.id
  4125.         AND c2.colid < x.keycnt+(x.status&16)/16
  4126.         AND x.indid = @indid
  4127.         AND t.type = d.ss_dtype
  4128.         AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  4129.         AND isnull(d.AUTO_INCREMENT,0) = (c.status&128)/128
  4130.         AND c.usertype = t.usertype
  4131. go
  4132.  
  4133. if (charindex('7.00', @@version) = 0)
  4134. begin
  4135.     print ''
  4136.     print ''
  4137.     print 'Warning:'
  4138.     print 'you are installing the stored procedures '
  4139.     print 'on a pre 7.0 SQL Server.'
  4140.     print 'Ignore the following errors.'
  4141. end
  4142. else
  4143.     drop proc sp_special_columns
  4144. go
  4145.  
  4146. /*    Procedure for 7.0 and later servers */
  4147. CREATE PROCEDURE sp_special_columns (
  4148.                  @table_name        sysname,
  4149.                  @table_owner        sysname = null,
  4150.                  @table_qualifier    sysname = null,
  4151.                  @col_type            char(1) = 'R',
  4152.                  @scope                char(1) = 'T',
  4153.                  @nullable            char(1) = 'U',
  4154.                  @ODBCVer            int = 2)
  4155. AS
  4156.     DECLARE @indid                int
  4157.     DECLARE @table_id            int
  4158.     DECLARE @full_table_name    nvarchar(257)
  4159.     DECLARE @scopeout            smallint
  4160.  
  4161.     if @col_type not in ('R','V')
  4162.     begin
  4163.         raiserror (15251,-1,-1,'col_type','''R'' or ''V''')
  4164.         return
  4165.     end
  4166.  
  4167.     if @scope = 'C'
  4168.         select @scopeout = 0
  4169.     else if @scope = 'T'
  4170.         select @scopeout = 1
  4171.     else
  4172.     begin
  4173.         raiserror (15251,-1,-1,'scope','''C'' or ''T''')
  4174.         return
  4175.     end
  4176.  
  4177.     if @nullable not in ('U','O')
  4178.     begin
  4179.         raiserror (15251,-1,-1,'nullable','''U'' or ''O''')
  4180.         return
  4181.     end
  4182.  
  4183.     if @table_qualifier is not null
  4184.     begin
  4185.         if db_name() <> @table_qualifier
  4186.         begin /* If qualifier doesn't match current database */
  4187.             raiserror (15250, -1,-1)
  4188.             return
  4189.         end
  4190.     end
  4191.     if @table_owner is null
  4192.     begin     /* If unqualified table name */
  4193.         SELECT @full_table_name = quotename(@table_name)
  4194.     end
  4195.     else
  4196.     begin    /* Qualified table name */
  4197.         if @table_owner = ''
  4198.         begin    /* If empty owner name */
  4199.             SELECT @full_table_name = quotename(@table_owner)
  4200.         end
  4201.         else
  4202.         begin
  4203.             SELECT @full_table_name = quotename(@table_owner) +
  4204.                 '.' + quotename(@table_name)
  4205.         end
  4206.     end
  4207.     /*    Get Object ID */
  4208.     SELECT @table_id = object_id(@full_table_name)
  4209.  
  4210.     if @col_type = 'V'
  4211.     BEGIN /* if ROWVER, just run that query */
  4212.         SELECT
  4213.             SCOPE = convert(smallint,NULL),
  4214.             COLUMN_NAME = convert(sysname,c.name),
  4215.             DATA_TYPE = convert(smallint, -2),
  4216.             TYPE_NAME = t.name,
  4217.             "PRECISION" = convert(int,8),
  4218.             LENGTH = convert(int,8),
  4219.             SCALE = convert(smallint, NULL),
  4220.             PSEUDO_COLUMN = convert(smallint,1)
  4221.         FROM
  4222.             systypes t, syscolumns c
  4223.         WHERE
  4224.             not (@table_id is null)
  4225.             AND c.id = @table_id
  4226.             AND t.name = 'timestamp'    /*    TIMESTAMP  */
  4227.             AND t.xtype = c.xtype        
  4228.             AND t.xusertype = c.xusertype    
  4229.         RETURN
  4230.     END
  4231.  
  4232.     /* ROWID, now find the id of the 'best' index for this table */
  4233.  
  4234.     IF @nullable = 'O'    /* Don't include any indexes that contain
  4235.                            nullable columns. */
  4236.  
  4237.         SELECT @indid = MIN(indid)
  4238.             FROM sysindexes x, syscolumns c, syscolumns c2
  4239.             WHERE
  4240.                 not (@table_id is null)
  4241.                 AND x.status&2 = 2        /*    If Unique Index */
  4242.                 AND c.id = x.id
  4243.                 AND c2.id = c.id
  4244.                 AND c2.colid < x.keycnt + (x.status&16)/16
  4245.                 AND x.id = @table_id
  4246.                 AND indid > 0        /*    Eliminate Table Row */
  4247.                 AND c.name = index_col(@table_name,x.indid,c2.colid)
  4248.                 GROUP BY indid HAVING SUM(c.status&8) = 0
  4249.  
  4250.     ELSE    /* Include indexes that are partially nullable. */
  4251.  
  4252.         SELECT @indid = MIN(indid)
  4253.             FROM sysindexes x
  4254.             WHERE
  4255.                 not (@table_id is null)
  4256.                 AND status&2 = 2        /*    If Unique Index */
  4257.                 AND id = @table_id
  4258.                 AND indid > 0        /*    Eliminate Table Row */
  4259.  
  4260.     SELECT
  4261.         SCOPE = @scopeout,
  4262.         COLUMN_NAME = convert(sysname,INDEX_COL(@full_table_name,indid,c2.colid)),
  4263.         d.DATA_TYPE,
  4264.         convert(sysname,case
  4265.             when t.xusertype > 255 then t.name
  4266.             else d.TYPE_NAME
  4267.         end) TYPE_NAME,
  4268.         convert(int,case
  4269.             when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  4270.             else OdbcPrec(c.xtype,c.length,c.xprec)
  4271.         end) "PRECISION",
  4272.         convert(int,case
  4273.             when type_name(d.ss_dtype) IN ('numeric','decimal') then    /* decimal/numeric types */
  4274.                 OdbcPrec(c.xtype,c.length,c.xprec)+2
  4275.             else isnull(d.length, c.length)
  4276.         end) LENGTH,
  4277.         SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
  4278.         PSEUDO_COLUMN = convert(smallint,1)
  4279.     FROM
  4280.         sysindexes x,
  4281.         syscolumns c,
  4282.         master.dbo.spt_datatype_info d,
  4283.         systypes t,
  4284.         syscolumns c2    /* Self-join to generate list of index columns and */
  4285.                         /* to extract datatype names */
  4286.     WHERE
  4287.         not (@table_id is null)
  4288.         AND x.id = @table_id
  4289.         AND c.name = INDEX_COL(@full_table_name,@indid,c2.colid)
  4290.         AND c.id = x.id
  4291.         AND c2.id = x.id
  4292.         AND c2.colid < x.keycnt + (x.status&16)/16
  4293.         AND x.indid = @indid
  4294.         AND t.xtype = d.ss_dtype
  4295.         AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  4296.         AND isnull(d.AUTO_INCREMENT,0) = isnull(ColumnProperty (c.id, c.name, 'IsIdentity'),0)
  4297.         AND c.xusertype = t.xusertype
  4298. go
  4299.  
  4300. grant execute on sp_special_columns to public
  4301. go
  4302.  
  4303. dump tran master with no_log
  4304. go
  4305.  
  4306. print 'creating sp_sproc_columns'
  4307. go
  4308.  
  4309. /*    Procedure for pre-6.0 server */
  4310. CREATE PROCEDURE sp_sproc_columns (
  4311.                  @procedure_name        varchar(102) = '%', /* 3*32+5+1 */
  4312.                  @procedure_owner        varchar(96) = null,
  4313.                  @procedure_qualifier    varchar(32) = null,
  4314.                  @column_name            varchar(96) = null,
  4315.                  @ODBCVer                int = 2)
  4316. AS
  4317.     DECLARE @group_num_lower smallint
  4318.     DECLARE @group_num_upper smallint
  4319.     DECLARE @semi_position int
  4320.     DECLARE @full_procedure_name    varchar(205)/* 2*102+1 */
  4321.     DECLARE @procedure_id int
  4322.  
  4323.     if @column_name is null /*    If column name not supplied, match all */
  4324.         select @column_name = '%'
  4325.     if @procedure_qualifier is not null
  4326.     begin
  4327.         if db_name() <> @procedure_qualifier
  4328.         begin
  4329.             if @procedure_qualifier = ''
  4330.             begin
  4331.                 /* in this case, we need to return an empty result set */
  4332.                 /* because the user has requested a database with an empty name */
  4333.                 select @procedure_name = ''
  4334.                 select @procedure_owner = ''
  4335.             end
  4336.             else
  4337.             begin    /* If qualifier doesn't match current database */
  4338.                 raiserror 20001 '~~Rush_51~~'
  4339.                 return
  4340.             end
  4341.         end
  4342.     end
  4343.  
  4344.     if @procedure_name is null
  4345.     begin    /*    If procedure name not supplied, match all */
  4346.         select @procedure_name = '%'
  4347.     end
  4348.  
  4349.     /* first we need to extract the procedure group number, if one exists */
  4350.     select @semi_position = charindex(';',@procedure_name)
  4351.     if (@semi_position > 0)
  4352.     begin    /* If group number separator (;) found */
  4353.         select @group_num_lower = convert(int,substring(@procedure_name, @semi_position + 1, 2))
  4354.         select @group_num_upper = @group_num_lower
  4355.         select @procedure_name = substring(@procedure_name, 1, @semi_position -1)
  4356.     end
  4357.     else
  4358.     begin    /* No group separator, so default to group number of 1 */
  4359.         select @group_num_lower = 1
  4360.         select @group_num_upper = 32767            
  4361.     end
  4362.  
  4363.     if @procedure_owner is null
  4364.     begin    /* If unqualified procedure name */
  4365.         SELECT @full_procedure_name = @procedure_name
  4366.     end
  4367.     else
  4368.     begin    /* Qualified procedure name */
  4369.         SELECT @full_procedure_name = @procedure_owner + '.' + @procedure_name
  4370.     end
  4371.  
  4372.     /*    Get Object ID */
  4373.     SELECT @procedure_id = object_id(@full_procedure_name)
  4374.     if ((charindex('%',@full_procedure_name) = 0) and
  4375.         (charindex('_',@full_procedure_name) = 0) and
  4376.         @procedure_id <> 0)
  4377.     begin
  4378.         /* this block is for the case where there is no pattern
  4379.             matching required for the procedure name */
  4380.         SELECT
  4381.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  4382.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  4383.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ ltrim(str(c.number,5))),
  4384.             COLUMN_NAME = convert(varchar(32),c.name),
  4385.             COLUMN_TYPE = convert(smallint, 0),
  4386.             d.DATA_TYPE,
  4387.             TYPE_NAME = t.name,
  4388.             "PRECISION" = isnull(d.data_precision, convert(int,c.length)),
  4389.             LENGTH = isnull(d.length, convert(int,c.length)),
  4390.             SCALE = d.numeric_scale,
  4391.             d.RADIX,
  4392.             d.NULLABLE,
  4393.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  4394.             COLUMN_DEF = convert(varchar(255),null),
  4395.             d.SQL_DATA_TYPE,
  4396.             d.SQL_DATETIME_SUB,
  4397.             CHAR_OCTET_LENGTH = isnull(d.data_precision, convert(int,c.length))+d.charbin,
  4398.             ORDINAL_POSITION = convert(int,c.colid),
  4399.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  4400.             SS_DATA_TYPE = c.type
  4401.         FROM
  4402.             syscolumns c,
  4403.             sysobjects o,
  4404.             master.dbo.spt_datatype_info d,
  4405.             systypes t
  4406.         WHERE
  4407.             o.id = @procedure_id
  4408.             AND c.id = o.id
  4409.             AND t.type = d.ss_dtype
  4410.             AND c.length = isnull(d.fixlen, c.length)
  4411.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  4412.             AND c.usertype = t.usertype
  4413.             AND c.name like @column_name
  4414.             AND c.number between @group_num_lower and @group_num_upper
  4415.         UNION ALL
  4416.         SELECT           /* return value row*/
  4417.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  4418.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  4419.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ isnull(ltrim(str(c.number,5)),'1')),
  4420.             COLUMN_NAME = convert(varchar(32),'RETURN_VALUE'),
  4421.             COLUMN_TYPE = convert(smallint, 5),
  4422.             DATA_TYPE = convert(smallint, 4),
  4423.             TYPE_NAME = convert(varchar(32),'int'),
  4424.             "PRECISION" = convert(int,10),
  4425.             LENGTH = convert(int,4),
  4426.             SCALE = convert(smallint,0),
  4427.             RADIX = convert(smallint,10),
  4428.             NULLABLE = convert(smallint,0),
  4429.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  4430.             COLUMN_DEF = convert(varchar(255),NULL),
  4431.             SQL_DATA_TYPE = convert(smallint, 4),
  4432.             SQL_DATETIME_SUB = convert(smallint,null),
  4433.             CHAR_OCTET_LENGTH = convert(int,null),
  4434.             ORDINAL_POSITION = convert(int,0),
  4435.             IS_NULLABLE = convert(varchar(254),'NO'),
  4436.             SS_DATA_TYPE = convert(tinyint,56)
  4437.         FROM
  4438.             syscomments c, sysobjects o
  4439.         WHERE
  4440.             o.id = @procedure_id
  4441.             AND c.id = o.id
  4442.             AND c.colid = 1
  4443.             AND o.type = 'P'                        /* Just Procedures */
  4444.             AND 'RETURN_VALUE' like @column_name
  4445.             AND c.number between @group_num_lower and @group_num_upper        
  4446.         ORDER BY 1, 2, 3, 18
  4447.     end
  4448.     else
  4449.     begin
  4450.         /* this block is for the case where there IS pattern
  4451.             matching done on the procedure name */
  4452.         if @procedure_owner is null
  4453.             select @procedure_owner = '%'
  4454.         SELECT
  4455.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  4456.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  4457.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ ltrim(str(c.number,5))),
  4458.             COLUMN_NAME = convert(varchar(32),c.name),
  4459.             COLUMN_TYPE = convert(smallint, 0),
  4460.             d.DATA_TYPE,
  4461.             TYPE_NAME = t.name,
  4462.             "PRECISION" = isnull(d.data_precision, convert(int,c.length)),
  4463.             LENGTH = isnull(d.length, convert(int,c.length)),
  4464.             SCALE = d.numeric_scale,
  4465.             d.RADIX,
  4466.             d.NULLABLE,
  4467.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  4468.             COLUMN_DEF = convert(varchar(255),null),
  4469.             d.SQL_DATA_TYPE,
  4470.             d.SQL_DATETIME_SUB,
  4471.             CHAR_OCTET_LENGTH = isnull(d.data_precision, convert(int,c.length))+d.charbin,
  4472.             ORDINAL_POSITION = convert(int,c.colid),
  4473.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  4474.             SS_DATA_TYPE = c.type
  4475.         FROM
  4476.             syscolumns c,
  4477.             sysobjects o,
  4478.             master.dbo.spt_datatype_info d,
  4479.             systypes t
  4480.         WHERE
  4481.             o.name like @procedure_name
  4482.             AND user_name(o.uid) like @procedure_owner
  4483.             AND o.id = c.id
  4484.             AND t.type = d.ss_dtype
  4485.             AND c.length = isnull(d.fixlen, c.length)
  4486.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  4487.             AND c.usertype = t.usertype
  4488.             AND o.type = 'P'                            /* Just Procedures */
  4489.             AND c.name like @column_name
  4490.             AND c.number between @group_num_lower and @group_num_upper
  4491.         UNION ALL
  4492.         SELECT           /* return value row*/
  4493.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  4494.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  4495.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ isnull(ltrim(str(c.number,5)),'1')),
  4496.             COLUMN_NAME = convert(varchar(32),'RETURN_VALUE'),
  4497.             COLUMN_TYPE = convert(smallint, 5),
  4498.             DATA_TYPE = convert(smallint, 4),
  4499.             TYPE_NAME = convert(varchar(32),'int'),
  4500.             "PRECISION" = convert(int,10),
  4501.             LENGTH = convert(int,4),
  4502.             SCALE = convert(smallint,0),
  4503.             RADIX = convert(smallint,10),
  4504.             NULLABLE = convert(smallint,0),
  4505.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  4506.             COLUMN_DEF = convert(varchar(255),NULL),
  4507.             SQL_DATA_TYPE = convert(smallint, 4),
  4508.             SQL_DATETIME_SUB = convert(smallint,null),
  4509.             CHAR_OCTET_LENGTH = convert(int,null),
  4510.             ORDINAL_POSITION = convert(int,0),
  4511.             IS_NULLABLE = convert(varchar(254),'NO'),
  4512.             SS_DATA_TYPE = convert(tinyint,56)
  4513.         FROM
  4514.             syscomments c, sysobjects o
  4515.         WHERE
  4516.             o.name like @procedure_name
  4517.             AND user_name(o.uid) like @procedure_owner
  4518.             AND c.id = o.id
  4519.             AND c.colid = 1
  4520.             AND o.type = 'P'                        /* Just Procedures */
  4521.             AND 'RETURN_VALUE' like @column_name
  4522.             AND c.number between @group_num_lower and @group_num_upper
  4523.         ORDER BY 1, 2, 3, 18
  4524.     end
  4525. go
  4526.  
  4527. if (charindex('6.00', @@version) = 0 and
  4528.     charindex('6.50', @@version) = 0 and
  4529.     charindex('7.00', @@version) = 0)
  4530. begin
  4531.     print ''
  4532.     print ''
  4533.     print 'Warning:'
  4534.     print 'you are installing the stored procedures '
  4535.     print 'on a pre 6.0 SQL Server.'
  4536.     print 'Ignore the following error.'
  4537. end
  4538. else
  4539.     drop proc sp_sproc_columns
  4540. go
  4541.  
  4542. /*    Procedure for 6.0 and 6.50 servers */
  4543. CREATE PROCEDURE sp_sproc_columns (
  4544.                  @procedure_name        varchar(102) = '%', /* 3*36+5+1 */
  4545.                  @procedure_owner        varchar(96) = null,
  4546.                  @procedure_qualifier    varchar(32) = null,
  4547.                  @column_name            varchar(96) = null,
  4548.                  @ODBCVer                int = 2)
  4549. AS
  4550.     DECLARE @group_num_lower smallint
  4551.     DECLARE @group_num_upper smallint
  4552.     DECLARE @semi_position int
  4553.     DECLARE @full_procedure_name    varchar(205)
  4554.     DECLARE @procedure_id int
  4555.  
  4556.     if @column_name is null /*    If column name not supplied, match all */
  4557.         select @column_name = '%'
  4558.     if @procedure_qualifier is not null
  4559.     begin
  4560.         if db_name() <> @procedure_qualifier
  4561.         begin
  4562.             if @procedure_qualifier = ''
  4563.             begin
  4564.                 /* in this case, we need to return an empty result set */
  4565.                 /* because the user has requested a database with an empty name */
  4566.                 select @procedure_name = ''
  4567.                 select @procedure_owner = ''
  4568.             end
  4569.             else
  4570.             begin    /* If qualifier doesn't match current database */
  4571.                 raiserror (15250, -1,-1)
  4572.                 return
  4573.             end
  4574.         end
  4575.     end
  4576.  
  4577.     if @procedure_name is null
  4578.     begin    /*    If procedure name not supplied, match all */
  4579.         select @procedure_name = '%'
  4580.     end
  4581.  
  4582.     /* first we need to extract the procedure group number, if one exists */
  4583.     select @semi_position = charindex(';',@procedure_name)
  4584.     if (@semi_position > 0)
  4585.     begin    /* If group number separator (;) found */
  4586.         select @group_num_lower = convert(int,substring(@procedure_name, @semi_position + 1, 2))
  4587.         select @group_num_upper = @group_num_lower
  4588.         select @procedure_name = substring(@procedure_name, 1, @semi_position -1)
  4589.     end
  4590.     else
  4591.     begin    /* No group separator, so default to group number of 1 */
  4592.         select @group_num_lower = 1
  4593.         select @group_num_upper = 32767            
  4594.     end
  4595.  
  4596.     if @procedure_owner is null
  4597.     begin    /* If unqualified procedure name */
  4598.         SELECT @full_procedure_name = @procedure_name
  4599.     end
  4600.     else
  4601.     begin    /* Qualified procedure name */
  4602.         SELECT @full_procedure_name = @procedure_owner + '.' + @procedure_name
  4603.     end
  4604.  
  4605.     /*    Get Object ID */
  4606.     SELECT @procedure_id = object_id(@full_procedure_name)
  4607.     if ((charindex('%',@full_procedure_name) = 0) and
  4608.         (charindex('[',@full_procedure_name) = 0) and
  4609.         (charindex('_',@full_procedure_name) = 0) and
  4610.         @procedure_id <> 0)
  4611.     begin
  4612.         /* this block is for the case where there is no pattern
  4613.             matching required for the procedure name */
  4614.         SELECT
  4615.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  4616.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  4617.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ ltrim(str(c.number,5))),
  4618.             COLUMN_NAME = convert(varchar(32),c.name),
  4619.             COLUMN_TYPE = convert(smallint, 1+((c.status/64)&1)),
  4620.             d.DATA_TYPE,
  4621.             TYPE_NAME = t.name,
  4622.             convert(int,case
  4623.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  4624.                 else isnull(convert(int,c.prec), 2147483647)
  4625.             end) "PRECISION",
  4626.             convert(int,case
  4627.                 when d.ss_dtype IN (106, 108, 55, 63) then    /* decimal/numeric types */
  4628.                     c.prec+2
  4629.                 else
  4630.                     isnull(d.length, c.length)
  4631.             end) LENGTH,
  4632.             SCALE = convert(smallint, c.scale),
  4633.             d.RADIX,
  4634.             d.NULLABLE,
  4635.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  4636.             COLUMN_DEF = convert(varchar(255),NULL),
  4637.             d.SQL_DATA_TYPE,
  4638.             d.SQL_DATETIME_SUB,
  4639.             CHAR_OCTET_LENGTH = isnull(convert(int,c.prec), 2147483647)+d.charbin,
  4640.             ORDINAL_POSITION = convert(int,c.colid),
  4641.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  4642.             SS_DATA_TYPE = c.type
  4643.         FROM
  4644.             syscolumns c,
  4645.             sysobjects o,
  4646.             master.dbo.spt_datatype_info d,
  4647.             systypes t
  4648.         WHERE
  4649.             o.id = @procedure_id
  4650.             AND c.id = o.id
  4651.             AND c.type = d.ss_dtype
  4652.             AND c.length = isnull(d.fixlen, c.length)
  4653.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  4654.             AND isnull(d.AUTO_INCREMENT,0) = 0
  4655.             AND c.usertype = t.usertype
  4656.             AND c.name like @column_name
  4657.             AND c.number between @group_num_lower and @group_num_upper
  4658.         UNION ALL
  4659.         SELECT           /* return value row*/
  4660.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  4661.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  4662.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ isnull(ltrim(str(c.number,5)),'1')),
  4663.             COLUMN_NAME = convert(varchar(32),'RETURN_VALUE'),
  4664.             COLUMN_TYPE = convert(smallint, 5),
  4665.             DATA_TYPE = convert(smallint, 4),
  4666.             TYPE_NAME = convert(varchar(32),'int'),
  4667.             "PRECISION" = convert(int,10),
  4668.             LENGTH = convert(int,4),
  4669.             SCALE = convert(smallint,0),
  4670.             RADIX = convert(smallint,10),
  4671.             NULLABLE = convert(smallint,0),
  4672.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  4673.             COLUMN_DEF = convert(varchar(255),NULL),
  4674.             SQL_DATA_TYPE = convert(smallint, 4),
  4675.             SQL_DATETIME_SUB = convert(smallint,null),
  4676.             CHAR_OCTET_LENGTH = convert(int,null),
  4677.             ORDINAL_POSITION = convert(int,0),
  4678.             IS_NULLABLE = convert(varchar(254),'NO'),
  4679.             SS_DATA_TYPE = convert(tinyint,56)
  4680.         FROM
  4681.             syscomments c, sysobjects o
  4682.         WHERE
  4683.             o.id = @procedure_id
  4684.             AND c.id = o.id
  4685.             AND c.colid = 1
  4686.             AND o.type = 'P'                        /* Just Procedures */
  4687.             AND 'RETURN_VALUE' like @column_name
  4688.             AND c.number between @group_num_lower and @group_num_upper
  4689.         ORDER BY 1, 2, 3, 18
  4690.     end
  4691.     else
  4692.     begin
  4693.         /* this block is for the case where there IS pattern
  4694.             matching done on the procedure name */
  4695.         if @procedure_owner is null
  4696.             select @procedure_owner = '%'
  4697.         SELECT
  4698.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  4699.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  4700.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ ltrim(str(c.number,5))),
  4701.             COLUMN_NAME = convert(varchar(32),c.name),
  4702.             COLUMN_TYPE = convert(smallint, 1+((c.status/64)&1)),
  4703.             d.DATA_TYPE,
  4704.             TYPE_NAME = t.name,
  4705.             convert(int,case
  4706.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  4707.                 else isnull(convert(int,c.prec), 2147483647)
  4708.             end) "PRECISION",
  4709.             convert(int,case
  4710.                 when d.ss_dtype IN (106, 108, 55, 63) then    /* decimal/numeric types */
  4711.                     c.prec+2
  4712.                 else
  4713.                     isnull(d.length, c.length)
  4714.             end) LENGTH,
  4715.             SCALE = convert(smallint, c.scale),
  4716.             d.RADIX,
  4717.             d.NULLABLE,
  4718.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  4719.             COLUMN_DEF = convert(varchar(255),NULL),
  4720.             d.SQL_DATA_TYPE,
  4721.             d.SQL_DATETIME_SUB,
  4722.             CHAR_OCTET_LENGTH = isnull(convert(int,c.prec), 2147483647)+d.charbin,
  4723.             ORDINAL_POSITION = convert(int,c.colid),
  4724.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  4725.             SS_DATA_TYPE = c.type
  4726.         FROM
  4727.             syscolumns c,
  4728.             sysobjects o,
  4729.             master.dbo.spt_datatype_info d,
  4730.             systypes t
  4731.         WHERE
  4732.             o.name like @procedure_name
  4733.             AND user_name(o.uid) like @procedure_owner
  4734.             AND o.id = c.id
  4735.             AND c.type = d.ss_dtype
  4736.             AND c.length = isnull(d.fixlen, c.length)
  4737.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  4738.             AND isnull(d.AUTO_INCREMENT,0) = 0
  4739.             AND c.usertype = t.usertype
  4740.             AND o.type = 'P'                            /* Just Procedures */
  4741.             AND c.name like @column_name
  4742.             AND c.number between @group_num_lower and @group_num_upper
  4743.         UNION ALL
  4744.         SELECT           /* return value row*/
  4745.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  4746.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  4747.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ isnull(ltrim(str(c.number,5)),'1')),
  4748.             COLUMN_NAME = convert(varchar(32),'RETURN_VALUE'),
  4749.             COLUMN_TYPE = convert(smallint, 5),
  4750.             DATA_TYPE = convert(smallint, 4),
  4751.             TYPE_NAME = convert(varchar(32),'int'),
  4752.             "PRECISION" = convert(int,10),
  4753.             LENGTH = convert(int,4),
  4754.             SCALE = convert(smallint,0),
  4755.             RADIX = convert(smallint,10),
  4756.             NULLABLE = convert(smallint,0),
  4757.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  4758.             COLUMN_DEF = convert(varchar(255),NULL),
  4759.             SQL_DATA_TYPE = convert(smallint, 4),
  4760.             SQL_DATETIME_SUB = convert(smallint,null),
  4761.             CHAR_OCTET_LENGTH = convert(int,null),
  4762.             ORDINAL_POSITION = convert(int,0),
  4763.             IS_NULLABLE = convert(varchar(254),'NO'),
  4764.             SS_DATA_TYPE = convert(tinyint,56)
  4765.         FROM
  4766.             syscomments c, sysobjects o
  4767.         WHERE
  4768.             o.name like @procedure_name
  4769.             AND user_name(o.uid) like @procedure_owner
  4770.             AND c.id = o.id
  4771.             AND c.colid = 1
  4772.             AND o.type = 'P'                        /* Just Procedures */
  4773.             AND 'RETURN_VALUE' like @column_name
  4774.             AND c.number between @group_num_lower and @group_num_upper
  4775.         ORDER BY 1, 2, 3, 18
  4776.     end
  4777. go
  4778.  
  4779. if (charindex('7.00', @@version) = 0)
  4780. begin
  4781.     print ''
  4782.     print ''
  4783.     print 'Warning:'
  4784.     print 'you are installing the stored procedures '
  4785.     print 'on a pre 7.0 SQL Server.'
  4786.     print 'Ignore the following errors.'
  4787. end
  4788. else
  4789.     drop proc sp_sproc_columns
  4790. go
  4791.  
  4792. /*    Procedure for 7.0 server */
  4793. CREATE PROCEDURE sp_sproc_columns (
  4794.                  @procedure_name        nvarchar(390) = '%',
  4795.                  @procedure_owner        nvarchar(384) = null,
  4796.                  @procedure_qualifier    sysname = null,
  4797.                  @column_name            nvarchar(384) = null,
  4798.                  @ODBCVer                int = 2)
  4799. AS
  4800.     DECLARE @group_num_lower smallint
  4801.     DECLARE @group_num_upper smallint
  4802.     DECLARE @semi_position int
  4803.     DECLARE @full_procedure_name    nvarchar(774)
  4804.     DECLARE @procedure_id int
  4805.  
  4806.     if @column_name is null /*    If column name not supplied, match all */
  4807.         select @column_name = '%'
  4808.     if @procedure_qualifier is not null
  4809.     begin
  4810.         if db_name() <> @procedure_qualifier
  4811.         begin
  4812.             if @procedure_qualifier = ''
  4813.             begin
  4814.                 /* in this case, we need to return an empty result set */
  4815.                 /* because the user has requested a database with an empty name */
  4816.                 select @procedure_name = ''
  4817.                 select @procedure_owner = ''
  4818.             end
  4819.             else
  4820.             begin    /* If qualifier doesn't match current database */
  4821.                 raiserror (15250, -1,-1)
  4822.                 return
  4823.             end
  4824.         end
  4825.     end
  4826.  
  4827.     if @procedure_name is null
  4828.     begin    /*    If procedure name not supplied, match all */
  4829.         select @procedure_name = '%'
  4830.     end
  4831.  
  4832.     /* first we need to extract the procedure group number, if one exists */
  4833.     select @semi_position = charindex(';',@procedure_name)
  4834.     if (@semi_position > 0)
  4835.     begin    /* If group number separator (;) found */
  4836.         select @group_num_lower = convert(int,substring(@procedure_name, @semi_position + 1, 2))
  4837.         select @group_num_upper = @group_num_lower
  4838.         select @procedure_name = substring(@procedure_name, 1, @semi_position -1)
  4839.     end
  4840.     else
  4841.     begin    /* No group separator, so default to all groups */
  4842.         select @group_num_lower = 1
  4843.         select @group_num_upper = 32767            
  4844.     end
  4845.  
  4846.     if @procedure_owner is null
  4847.     begin    /* If unqualified procedure name */
  4848.         SELECT @full_procedure_name = quotename(@procedure_name)
  4849.     end
  4850.     else
  4851.     begin    /* Qualified procedure name */
  4852.         if @procedure_owner = ''
  4853.         begin    /* If empty owner name */
  4854.             SELECT @full_procedure_name = quotename(@procedure_owner)
  4855.         end
  4856.         else
  4857.         begin
  4858.             SELECT @full_procedure_name = quotename(@procedure_owner) +
  4859.                 '.' + quotename(@procedure_name)
  4860.         end
  4861.     end
  4862.  
  4863.     /*    Get Object ID */
  4864.     SELECT @procedure_id = object_id(@full_procedure_name)
  4865.     if ((isnull(charindex('%', @full_procedure_name),0) = 0) and
  4866.         (isnull(charindex('[', @procedure_name),0) = 0) and
  4867.         (isnull(charindex('[', @procedure_owner),0) = 0) and
  4868.         (isnull(charindex('_', @full_procedure_name),0) = 0) and
  4869.         not (@procedure_id is null))
  4870.     begin
  4871.         /* this block is for the case where there is no pattern
  4872.             matching required for the procedure name */
  4873.         SELECT
  4874.             PROCEDURE_QUALIFIER = convert(sysname,DB_NAME()),
  4875.             PROCEDURE_OWNER = convert(sysname,USER_NAME(o.uid)),
  4876.             PROCEDURE_NAME = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  4877.             COLUMN_NAME = convert(sysname,c.name),
  4878.             COLUMN_TYPE = convert(smallint, 1+c.isoutparam),
  4879.             d.DATA_TYPE,
  4880.             TYPE_NAME = t.name,
  4881.             convert(int,case
  4882.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  4883.                 else OdbcPrec(c.xtype,c.length,c.xprec)
  4884.             end) "PRECISION",
  4885.             convert(int,case
  4886.                 when type_name(d.ss_dtype) IN ('numeric','decimal') then    /* decimal/numeric types */
  4887.                     OdbcPrec(c.xtype,c.length,c.xprec)+2
  4888.                 else
  4889.                     isnull(d.length, c.length)
  4890.             end) LENGTH,
  4891.             SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
  4892.             d.RADIX,
  4893.             d.NULLABLE,
  4894.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  4895.             COLUMN_DEF = convert(nvarchar(4000),NULL),
  4896.             d.SQL_DATA_TYPE,
  4897.             d.SQL_DATETIME_SUB,
  4898.             CHAR_OCTET_LENGTH = isnull(d.length, c.length)+d.charbin,
  4899.             ORDINAL_POSITION = convert(int,c.colid),
  4900.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  4901.             SS_DATA_TYPE = c.type
  4902.         FROM
  4903.             syscolumns c,
  4904.             sysobjects o,
  4905.             master.dbo.spt_datatype_info d,
  4906.             systypes t
  4907.         WHERE
  4908.             o.id = @procedure_id
  4909.             AND c.id = o.id
  4910.             AND c.xtype = d.ss_dtype
  4911.             AND c.length = isnull(d.fixlen, c.length)
  4912.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  4913.             AND isnull(d.AUTO_INCREMENT,0) = 0
  4914.             AND c.xusertype = t.xusertype
  4915.             AND c.name like @column_name
  4916.             AND c.number between @group_num_lower and @group_num_upper
  4917.         UNION ALL
  4918.         SELECT           /* return value row*/
  4919.             PROCEDURE_QUALIFIER = convert(sysname,DB_NAME()),
  4920.             PROCEDURE_OWNER = convert(sysname,USER_NAME(o.uid)),
  4921.             PROCEDURE_NAME = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  4922.             COLUMN_NAME = convert(sysname,'RETURN_VALUE'),
  4923.             COLUMN_TYPE = convert(smallint, 5),
  4924.             DATA_TYPE = convert(smallint, 4),
  4925.             TYPE_NAME = convert(sysname,'int'),
  4926.             "PRECISION" = convert(int,10),
  4927.             LENGTH = convert(int,4),
  4928.             SCALE = convert(smallint,0),
  4929.             RADIX = convert(smallint,10),
  4930.             NULLABLE = convert(smallint,0),
  4931.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  4932.             COLUMN_DEF = convert(nvarchar(4000),NULL),
  4933.             SQL_DATA_TYPE = convert(smallint, 4),
  4934.             SQL_DATETIME_SUB = convert(smallint,null),
  4935.             CHAR_OCTET_LENGTH = convert(int,null),
  4936.             ORDINAL_POSITION = convert(int,0),
  4937.             IS_NULLABLE = convert(varchar(254),'NO'),
  4938.             SS_DATA_TYPE = convert(tinyint,56)
  4939.         FROM
  4940.             syscomments c, sysobjects o
  4941.         WHERE
  4942.             o.id = @procedure_id
  4943.             AND c.id = o.id
  4944.             AND c.colid = 1
  4945.             AND o.type = 'P'                        /* Just Procedures */
  4946.             AND 'RETURN_VALUE' like @column_name
  4947.             AND c.number between @group_num_lower and @group_num_upper        
  4948.         ORDER BY 1, 2, 3, 18
  4949.     end
  4950.     else
  4951.     begin
  4952.         /* this block is for the case where there IS pattern
  4953.             matching done on the procedure name */
  4954.         if @procedure_owner is null
  4955.             select @procedure_owner = '%'
  4956.         SELECT
  4957.             PROCEDURE_QUALIFIER = convert(sysname,DB_NAME()),
  4958.             PROCEDURE_OWNER = convert(sysname,USER_NAME(o.uid)),
  4959.             PROCEDURE_NAME = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  4960.             COLUMN_NAME = convert(sysname,c.name),
  4961.             COLUMN_TYPE = convert(smallint,    1+c.isoutparam),
  4962.             d.DATA_TYPE,
  4963.             TYPE_NAME = t.name,
  4964.             convert(int,case
  4965.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  4966.                 else OdbcPrec(c.xtype,c.length,c.xprec)
  4967.             end) "PRECISION",
  4968.             convert(int,case
  4969.                 when type_name(d.ss_dtype) IN ('numeric','decimal') then    /* decimal/numeric types */
  4970.                     OdbcPrec(c.xtype,c.length,c.xprec)+2
  4971.                 else
  4972.                     isnull(d.length, c.length)
  4973.             end) LENGTH,
  4974.             SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
  4975.             d.RADIX,
  4976.             d.NULLABLE,
  4977.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  4978.             COLUMN_DEF = convert(nvarchar(4000),NULL),
  4979.             d.SQL_DATA_TYPE,
  4980.             d.SQL_DATETIME_SUB,
  4981.             CHAR_OCTET_LENGTH = isnull(d.length, c.length)+d.charbin,
  4982.             ORDINAL_POSITION = convert(int,c.colid),
  4983.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  4984.             SS_DATA_TYPE = c.type
  4985.         FROM
  4986.             syscolumns c,
  4987.             sysobjects o,
  4988.             master.dbo.spt_datatype_info d,
  4989.             systypes t
  4990.         WHERE
  4991.             o.name like @procedure_name
  4992.             AND user_name(o.uid) like @procedure_owner
  4993.             AND o.id = c.id
  4994.             AND c.xtype = d.ss_dtype
  4995.             AND c.length = isnull(d.fixlen, c.length)
  4996.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  4997.             AND isnull(d.AUTO_INCREMENT,0) = 0
  4998.             AND c.xusertype = t.xusertype
  4999.             AND o.type = 'P'                            /* Just Procedures */
  5000.             AND c.name like @column_name
  5001.             AND c.number between @group_num_lower and @group_num_upper
  5002.         UNION ALL
  5003.         SELECT           /* return value row*/
  5004.             PROCEDURE_QUALIFIER = convert(sysname,DB_NAME()),
  5005.             PROCEDURE_OWNER = convert(sysname,USER_NAME(o.uid)),
  5006.             PROCEDURE_NAME = convert(nvarchar(134),o.name +';'+ isnull(ltrim(str(c.number,5)),'1')),
  5007.             COLUMN_NAME = convert(sysname,'RETURN_VALUE'),
  5008.             COLUMN_TYPE = convert(smallint, 5),
  5009.             DATA_TYPE = convert(smallint, 4),
  5010.             TYPE_NAME = convert(sysname,'int'),
  5011.             "PRECISION" = convert(int,10),
  5012.             LENGTH = convert(int,4),
  5013.             SCALE = convert(smallint,0),
  5014.             RADIX = convert(smallint,10),
  5015.             NULLABLE = convert(smallint,0),
  5016.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  5017.             COLUMN_DEF = convert(nvarchar(4000),NULL),
  5018.             SQL_DATA_TYPE = convert(smallint, 4),
  5019.             SQL_DATETIME_SUB = convert(smallint,null),
  5020.             CHAR_OCTET_LENGTH = convert(int,null),
  5021.             ORDINAL_POSITION = convert(int,0),
  5022.             IS_NULLABLE = convert(varchar(254),'NO'),
  5023.             SS_DATA_TYPE = convert(tinyint,56)
  5024.         FROM
  5025.             syscomments c, sysobjects o
  5026.         WHERE
  5027.             o.name like @procedure_name
  5028.             AND c.id = o.id
  5029.             AND user_name(o.uid) like @procedure_owner
  5030.             AND c.colid = 1
  5031.             AND o.type = 'P'                        /* Just Procedures */
  5032.             AND 'RETURN_VALUE' like @column_name
  5033.             AND c.number between @group_num_lower and @group_num_upper
  5034.         ORDER BY 1, 2, 3, 18
  5035.     end
  5036. go
  5037.  
  5038. grant execute on sp_sproc_columns to public
  5039. go
  5040.  
  5041. dump tran master with no_log
  5042. go
  5043.  
  5044. print 'creating sp_statistics'
  5045. go
  5046.  
  5047. /*    Procedure for pre-7.0 server */
  5048. CREATE PROCEDURE sp_statistics (
  5049.                  @table_name        varchar(32),
  5050.                  @table_owner        varchar(32) = null,
  5051.                  @table_qualifier    varchar(32) = null,
  5052.                  @index_name        varchar(32) = '%',
  5053.                  @is_unique         char(1) = 'N',
  5054.                  @accuracy            char(1) = 'Q')
  5055. AS
  5056.     set nocount on
  5057.     DECLARE @indid                int
  5058.     DECLARE @lastindid            int
  5059.     DECLARE @table_id            int
  5060.     DECLARE @full_table_name    varchar(65) /* 2*32+1 */
  5061.  
  5062.     create table #TmpIndex(
  5063.         TABLE_QUALIFIER varchar(32) NULL,
  5064.         TABLE_OWNER     varchar(32) NULL,
  5065.         TABLE_NAME        varchar(32) NOT NULL,
  5066.         INDEX_QUALIFIER varchar(32) null,
  5067.         INDEX_NAME        varchar(32) null,
  5068.         NON_UNIQUE        smallint null,
  5069.         TYPE            smallint NOT NULL,
  5070.         SEQ_IN_INDEX    smallint null,
  5071.         COLUMN_NAME     varchar(32) null,
  5072.         COLLATION        char(1) null,
  5073.         index_id        int null,
  5074.         CARDINALITY     int null,
  5075.         PAGES            int null,
  5076.         status            smallint NOT NULL)
  5077.  
  5078.     if @table_qualifier is not null
  5079.     begin
  5080.         if db_name() <> @table_qualifier
  5081.         begin    /* If qualifier doesn't match current database */
  5082.             raiserror 20001 '~~Rush_5~~'
  5083.             return
  5084.         end
  5085.     end
  5086.  
  5087.     if @accuracy not in ('Q','E')
  5088.         begin
  5089.             raiserror 20002 '~~Rush_58~~'
  5090.             return
  5091.         end
  5092.  
  5093.     if (@@trancount <> 0 and
  5094.         charindex('6.50', @@version) = 0 and
  5095.         charindex('7.00', @@version) = 0)
  5096.     begin    /* If inside a transaction */
  5097.         raiserror 20003 '~~Rush_59~~'
  5098.         return
  5099.     end
  5100.  
  5101.     if @table_owner is null
  5102.     begin    /* If unqualified table name */
  5103.         SELECT @full_table_name = @table_name
  5104.     end
  5105.     else
  5106.     begin    /* Qualified table name */
  5107.         if @table_owner = ''
  5108.         begin    /* If empty owner name */
  5109.             SELECT @full_table_name = @table_owner
  5110.         end
  5111.         else
  5112.         begin
  5113.             SELECT @full_table_name = @table_owner + '.' + @table_name
  5114.         end
  5115.     end
  5116.     /*    Get Object ID */
  5117.     SELECT @table_id = object_id(@full_table_name)
  5118.  
  5119.     /*    Start at lowest index id */
  5120.     SELECT @indid = min(indid)
  5121.     FROM sysindexes
  5122.     WHERE id = @table_id
  5123.         AND indid > 0
  5124.         AND indid < 255
  5125.  
  5126.     WHILE @indid is not NULL
  5127.     BEGIN
  5128.         INSERT #TmpIndex    /* Add all columns that are in index */
  5129.             SELECT
  5130.                 DB_NAME(),                                /* TABLE_QUALIFIER */
  5131.                 USER_NAME(o.uid),                        /* TABLE_OWNER       */
  5132.                 o.name,                                 /* TABLE_NAME       */
  5133.                 o.name,                                 /* INDEX_QUALIFIER */
  5134.                 x.name,                                 /* INDEX_NAME       */
  5135.                 0,                                        /* NON_UNIQUE       */
  5136.                 1,                                        /* SQL_INDEX_CLUSTERED */
  5137.                 colid,                                    /* SEQ_IN_INDEX    */
  5138.                 INDEX_COL(@full_table_name,indid,colid),/* COLUMN_NAME       */
  5139.                 'A',                                    /* COLLATION       */
  5140.                 @indid,                                 /* index_id        */
  5141.                 x.rows,                                 /* CARDINALITY       */
  5142.                 x.dpages,                                /* PAGES           */
  5143.                 x.status                                /* status            */
  5144.             FROM sysindexes x, syscolumns c, sysobjects o
  5145.             WHERE
  5146.                 x.id = @table_id
  5147.                 AND x.id = o.id
  5148.                 AND x.id = c.id
  5149.                 AND c.colid < keycnt+(x.status&16)/16    /* all but Unique Clust indices have an extra key */
  5150.                 AND x.indid = @indid
  5151.         /*
  5152.         **      Now move @indid to the next index.
  5153.         */
  5154.         SELECT @lastindid = @indid
  5155.         SELECT @indid = NULL
  5156.  
  5157.         SELECT @indid = min(indid)
  5158.         FROM sysindexes
  5159.         WHERE id = @table_id
  5160.             AND indid > @lastindid
  5161.             AND indid < 255
  5162.     END
  5163.  
  5164.     UPDATE #TmpIndex
  5165.         SET NON_UNIQUE = 1
  5166.         WHERE status&2 <> 2 /* If non-unique index */
  5167.     UPDATE #TmpIndex
  5168.         SET
  5169.             TYPE = 3,            /* SQL_INDEX_OTHER */
  5170.             CARDINALITY = NULL,
  5171.             PAGES = NULL
  5172.         WHERE index_id > 1    /* If non-clustered index */
  5173.  
  5174.     /* now add row for table statistics */
  5175.     INSERT #TmpIndex
  5176.         SELECT
  5177.             DB_NAME(),                /* TABLE_QUALIFIER */
  5178.             USER_NAME(o.uid),        /* TABLE_OWNER       */
  5179.             o.name,                 /* TABLE_NAME       */
  5180.             null,                    /* INDEX_QUALIFIER */
  5181.             null,                    /* INDEX_NAME       */
  5182.             null,                    /* NON_UNIQUE       */
  5183.             0,                        /* SQL_TABLE_STAT  */
  5184.             null,                    /* SEQ_IN_INDEX    */
  5185.             null,                    /* COLUMN_NAME       */
  5186.             null,                    /* COLLATION       */
  5187.             0,                        /* index_id        */
  5188.             x.rows,                 /* CARDINALITY       */
  5189.             x.dpages,                /* PAGES           */
  5190.             0                        /* status           */
  5191.         FROM sysindexes x, sysobjects o
  5192.         WHERE o.id = @table_id
  5193.             AND x.id = o.id
  5194.             AND (x.indid = 0 or x.indid = 1)    /*    If there are no indexes */
  5195.                                                 /*    then table stats are in */
  5196.                                                 /*    a row with indid =0        */
  5197.  
  5198.     if @is_unique <> 'Y'    /* If all indexes desired */
  5199.         SELECT
  5200.             TABLE_QUALIFIER,
  5201.             TABLE_OWNER,
  5202.             TABLE_NAME,
  5203.             NON_UNIQUE,
  5204.             INDEX_QUALIFIER,
  5205.             INDEX_NAME,
  5206.             TYPE,
  5207.             SEQ_IN_INDEX,
  5208.             COLUMN_NAME,
  5209.             COLLATION,
  5210.             CARDINALITY,
  5211.             PAGES,
  5212.             FILTER_CONDITION = convert(varchar(128),null)
  5213.         FROM #TmpIndex
  5214.         WHERE
  5215.             INDEX_NAME like @index_name /* If matching name */
  5216.             or INDEX_NAME is null        /* If SQL_TABLE_STAT row */
  5217.         ORDER BY 4, 7, 6, 8
  5218.     else                    /* If only unique indexes desired */
  5219.         SELECT
  5220.             TABLE_QUALIFIER,
  5221.             TABLE_OWNER,
  5222.             TABLE_NAME,
  5223.             NON_UNIQUE,
  5224.             INDEX_QUALIFIER,
  5225.             INDEX_NAME,
  5226.             TYPE,
  5227.             SEQ_IN_INDEX,
  5228.             COLUMN_NAME,
  5229.             COLLATION,
  5230.             CARDINALITY,
  5231.             PAGES,
  5232.             FILTER_CONDITION = convert(varchar(128),null)
  5233.         FROM #TmpIndex
  5234.         WHERE
  5235.             (NON_UNIQUE = 0             /* If unique */
  5236.                 or NON_UNIQUE is NULL)    /* If SQL_TABLE_STAT row */
  5237.             and (INDEX_NAME like @index_name    /* If matching name */
  5238.                 or INDEX_NAME is null)    /* If SQL_TABLE_STAT row */
  5239.         ORDER BY 4, 7, 6, 8
  5240.  
  5241.     DROP TABLE #TmpIndex
  5242. go
  5243.  
  5244. if (charindex('7.00', @@version) = 0)
  5245. begin
  5246.     print ''
  5247.     print ''
  5248.     print 'Warning:'
  5249.     print 'you are installing the stored procedures '
  5250.     print 'on a pre 7.0 SQL Server.'
  5251.     print 'Ignore the following errors.'
  5252. end
  5253. else
  5254.     drop proc sp_statistics
  5255. go
  5256.  
  5257. /*    Procedure for 7.0 server */
  5258. CREATE PROCEDURE sp_statistics (
  5259.                  @table_name        sysname,
  5260.                  @table_owner        sysname = null,
  5261.                  @table_qualifier    sysname = null,
  5262.                  @index_name        sysname = '%',
  5263.                  @is_unique         char(1) = 'N',
  5264.                  @accuracy            char(1) = 'Q')
  5265. AS
  5266.     set nocount on
  5267.     DECLARE @indid                int
  5268.     DECLARE @lastindid            int
  5269.     DECLARE @table_id            int
  5270.     DECLARE @full_table_name    nvarchar(257)
  5271.  
  5272.     create table #TmpIndex(
  5273.         TABLE_QUALIFIER sysname NULL,
  5274.         TABLE_OWNER     sysname NULL,
  5275.         TABLE_NAME        sysname NOT NULL,
  5276.         INDEX_QUALIFIER sysname null,
  5277.         INDEX_NAME        sysname null,
  5278.         NON_UNIQUE        smallint null,
  5279.         TYPE            smallint NOT NULL,
  5280.         SEQ_IN_INDEX    smallint null,
  5281.         COLUMN_NAME     sysname null,
  5282.         COLLATION        char(1) null,
  5283.         index_id        int null,
  5284.         CARDINALITY     int null,
  5285.         PAGES            int null,
  5286.         status            int NOT NULL)
  5287.  
  5288.     if @table_qualifier is not null
  5289.     begin
  5290.         if db_name() <> @table_qualifier
  5291.         begin    /* If qualifier doesn't match current database */
  5292.             raiserror (15250, -1,-1)
  5293.             return
  5294.         end
  5295.     end
  5296.  
  5297.     if @accuracy not in ('Q','E')
  5298.         begin
  5299.             raiserror (15251,-1,-1,'accuracy','''Q'' or ''E''')
  5300.             return
  5301.         end
  5302.  
  5303.     if @table_owner is null
  5304.     begin    /* If unqualified table name */
  5305.         SELECT @full_table_name = quotename(@table_name)
  5306.     end
  5307.     else
  5308.     begin    /* Qualified table name */
  5309.         if @table_owner = ''
  5310.         begin    /* If empty owner name */
  5311.             SELECT @full_table_name = quotename(@table_owner)
  5312.         end
  5313.         else
  5314.         begin
  5315.             SELECT @full_table_name = quotename(@table_owner) +
  5316.                 '.' + quotename(@table_name)
  5317.         end
  5318.     end
  5319.     /*    Get Object ID */
  5320.     SELECT @table_id = object_id(@full_table_name)
  5321.  
  5322.     /*    Start at lowest index id */
  5323.     SELECT @indid = min(indid)
  5324.     FROM sysindexes
  5325.     WHERE not (@table_id is null)
  5326.         AND id = @table_id
  5327.         AND indid > 0
  5328.         AND indid < 255
  5329.  
  5330.     WHILE @indid is not NULL
  5331.     BEGIN
  5332.         INSERT #TmpIndex    /* Add all columns that are in index */
  5333.             SELECT
  5334.                 DB_NAME(),                                /* TABLE_QUALIFIER */
  5335.                 USER_NAME(o.uid),                        /* TABLE_OWNER       */
  5336.                 o.name,                                 /* TABLE_NAME       */
  5337.                 o.name,                                 /* INDEX_QUALIFIER */
  5338.                 x.name,                                 /* INDEX_NAME       */
  5339.                 case                                    /* NON_UNIQUE       */
  5340.                     WHEN x.status&2 <> 2 then 1            /* Nonunique index */
  5341.                     else 0                                /* Unique index    */
  5342.                 end,
  5343.                 case                                    /* TYPE            */
  5344.                     when @indid > 1 then 3                /* Non-Clustered   */
  5345.                     else 1                                /* Clustered index */
  5346.                 end,
  5347.                 colid,                                    /* SEQ_IN_INDEX    */
  5348.                 INDEX_COL(@full_table_name,indid,colid),/* COLUMN_NAME       */
  5349.                 'A',                                    /* COLLATION       */
  5350.                 @indid,                                 /* index_id        */
  5351.                 case                                    /* CARDINALITY       */
  5352.                     when @indid > 1 then NULL            /* Non-Clustered   */
  5353.                     else x.rows                         /* Clustered index */
  5354.                 end,
  5355.                 case                                    /* PAGES           */
  5356.                     when @indid > 1 then NULL            /* Non-Clustered   */
  5357.                     else x.dpages                        /* Clustered index */
  5358.                 end,
  5359.                 x.status                                /* status            */
  5360.             FROM sysindexes x, syscolumns c, sysobjects o
  5361.             WHERE
  5362.                 not (@table_id is null)
  5363.                 AND x.id = @table_id
  5364.                 AND x.id = o.id
  5365.                 AND x.id = c.id
  5366.                 AND colid < keycnt+(x.status&18)/18    /* all but Unique Clust indices have an extra key */
  5367.                 AND INDEX_COL(@full_table_name,indid,colid) IS NOT NULL
  5368.                 AND indid = @indid
  5369.                 AND (x.status&2 = 2
  5370.                     OR @is_unique <> 'Y')
  5371.                 AND (x.status&32) = 0
  5372.         /*
  5373.         **      Now move @indid to the next index.
  5374.         */
  5375.         SELECT @lastindid = @indid
  5376.         SELECT @indid = NULL
  5377.  
  5378.         SELECT @indid = min(indid)
  5379.         FROM sysindexes
  5380.         WHERE not (@table_id is null)
  5381.             AND id = @table_id
  5382.             AND indid > @lastindid
  5383.             AND indid < 255
  5384.     END
  5385.  
  5386.     /* now add row for table statistics */
  5387.     INSERT #TmpIndex
  5388.         SELECT
  5389.             DB_NAME(),                /* TABLE_QUALIFIER */
  5390.             USER_NAME(o.uid),        /* TABLE_OWNER       */
  5391.             o.name,                 /* TABLE_NAME       */
  5392.             null,                    /* INDEX_QUALIFIER */
  5393.             null,                    /* INDEX_NAME       */
  5394.             null,                    /* NON_UNIQUE       */
  5395.             0,                        /* SQL_TABLE_STAT  */
  5396.             null,                    /* SEQ_IN_INDEX    */
  5397.             null,                    /* COLUMN_NAME       */
  5398.             null,                    /* COLLATION       */
  5399.             0,                        /* index_id        */
  5400.             x.rows,                 /* CARDINALITY       */
  5401.             x.dpages,                /* PAGES           */
  5402.             0                        /* status           */
  5403.         FROM sysindexes x, sysobjects o
  5404.         WHERE not (@table_id is null)
  5405.             AND o.id = @table_id
  5406.             AND x.id = o.id
  5407.             AND (x.indid = 0 or x.indid = 1)    /*    If there are no indexes */
  5408.                                                 /*    then table stats are in */
  5409.                                                 /*    a row with indid =0        */
  5410.  
  5411.     SELECT
  5412.         TABLE_QUALIFIER,
  5413.         TABLE_OWNER,
  5414.         TABLE_NAME,
  5415.         NON_UNIQUE,
  5416.         INDEX_QUALIFIER,
  5417.         INDEX_NAME,
  5418.         TYPE,
  5419.         SEQ_IN_INDEX,
  5420.         COLUMN_NAME,
  5421.         COLLATION,
  5422.         CARDINALITY,
  5423.         PAGES,
  5424.         FILTER_CONDITION = convert(varchar(128),null)
  5425.     FROM #TmpIndex
  5426.     WHERE
  5427.         INDEX_NAME like @index_name /* If matching name */
  5428.         or INDEX_NAME is null        /* If SQL_TABLE_STAT row */
  5429.     ORDER BY 4, 7, 6, 8
  5430.  
  5431.     DROP TABLE #TmpIndex
  5432. go
  5433.  
  5434. grant execute on sp_statistics to public
  5435. go
  5436.  
  5437. dump tran master with no_log
  5438. go
  5439.  
  5440. print 'creating sp_stored_procedures'
  5441. go
  5442.  
  5443.  
  5444. /* pre 7.00 version */
  5445. create procedure sp_stored_procedures(
  5446.                         @sp_name        varchar(102) = null,
  5447.                         @sp_owner        varchar(96) = null,
  5448.                         @sp_qualifier    varchar(32) = null)
  5449. as
  5450.     declare @proc_type smallint
  5451.  
  5452.     if @sp_qualifier is not null
  5453.     begin
  5454.         if db_name() <> @sp_qualifier
  5455.         begin
  5456.             if @sp_qualifier = ''
  5457.             begin
  5458.                 /* in this case, we need to return an empty result set */
  5459.                 /* because the user has requested a database with an empty name */
  5460.                 select @sp_name = ''
  5461.                 select @sp_owner = ''
  5462.             end else
  5463.             begin    /* If qualifier doesn't match current database */
  5464.                 raiserror 20001 '~~Rush_51~~'
  5465.                 return
  5466.             end
  5467.         end
  5468.     end
  5469.  
  5470.     if @sp_name is null
  5471.     begin  /*  If procedure name not supplied, match all */
  5472.         select @sp_name = '%'
  5473.     end
  5474.     else begin
  5475.         if (@sp_owner is null) and (charindex('%', @sp_name) = 0)
  5476.         begin
  5477.             if exists (select * from sysobjects
  5478.                 where uid = user_id()
  5479.                     and name = @sp_name
  5480.                     and type = 'P') /* Object type of Procedure */
  5481.             begin
  5482.                 select @sp_owner = user_name()
  5483.             end
  5484.         end
  5485.     end
  5486.     if @sp_owner is null    /*    If procedure owner not supplied, match all */
  5487.         select @sp_owner = '%'
  5488.  
  5489.     select @proc_type=2        /* Return 2 for 4.2 and later servers. */
  5490.  
  5491.     select
  5492.         PROCEDURE_QUALIFIER = convert(varchar(32),db_name()),
  5493.         PROCEDURE_OWNER = convert(varchar(32),user_name(o.uid)),
  5494.         PROCEDURE_NAME = convert(varchar(36),o.name +';'+ ltrim(str(c.number,5))),
  5495.         NUM_INPUT_PARAMS = -1,    /* Constant since value unknown */
  5496.         NUM_OUTPUT_PARAMS = -1, /* Constant since value unknown */
  5497.         NUM_RESULT_SETS = -1,    /* Constant since value unknown */
  5498.         REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  5499.         PROCEDURE_TYPE = @proc_type
  5500.     from
  5501.         sysobjects o,syscomments c,sysusers u
  5502.     where
  5503.         o.name like @sp_name
  5504.         and c.colid = 1
  5505.         and user_name(o.uid) like @sp_owner
  5506.         and o.type = 'P'        /* Object type of Procedure */
  5507.         and c.id = o.id
  5508.         and u.uid = user_id()    /* constrain sysusers uid for use in subquery */
  5509.         and (suser_id() = 1     /* User is the System Administrator */
  5510.             or o.uid = user_id()    /* User created the object */
  5511.             /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  5512.             or ((select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  5513.              from sysprotects p
  5514.              /* outer join to correlate with all rows in sysobjects */
  5515.              where p.id =* o.id
  5516.                  /*  get rows for public,current user,user's group */
  5517.                  and (p.uid = 0 or p.uid = user_id() or p.uid =* u.gid)
  5518.                  /* check for SELECT,EXECUTE privilege */
  5519.                  and (action in (193,224)))&1    /* more magic...normalize GRANT */
  5520.             ) = 1     /* final magic...compare Grants    */
  5521.         )
  5522.     order by 1, 2, 3
  5523. go
  5524.  
  5525. grant execute on sp_stored_procedures to public
  5526. go
  5527.  
  5528.  
  5529. if (charindex('7.00', @@version) > 0)
  5530.     drop procedure sp_stored_procedures
  5531. else
  5532. begin
  5533.     print ''
  5534.     print ''
  5535.     print 'Warning:'
  5536.     print 'you are installing the stored procedures '
  5537.     print 'on a pre 7.0 SQL Server.'
  5538.     print 'Ignore the following errors.'
  5539. end
  5540. go
  5541.  
  5542.  
  5543. /* 7.00 version */
  5544. create procedure sp_stored_procedures(
  5545.                         @sp_name        nvarchar(390) = null,
  5546.                         @sp_owner        nvarchar(384) = null,
  5547.                         @sp_qualifier    sysname = null)
  5548. as
  5549.     declare @proc_type smallint
  5550.  
  5551.     if @sp_qualifier is not null
  5552.     begin
  5553.         if db_name() <> @sp_qualifier
  5554.         begin
  5555.             if @sp_qualifier = ''
  5556.             begin
  5557.                 /* in this case, we need to return an empty result set */
  5558.                 /* because the user has requested a database with an empty name */
  5559.                 select @sp_name = ''
  5560.                 select @sp_owner = ''
  5561.             end else
  5562.             begin    /* If qualifier doesn't match current database */
  5563.                 raiserror (15250, -1,-1)
  5564.                 return
  5565.             end
  5566.         end
  5567.     end
  5568.  
  5569.     if @sp_name is null
  5570.     begin  /*  If procedure name not supplied, match all */
  5571.         select @sp_name = '%'
  5572.     end
  5573.     else begin
  5574.         if (@sp_owner is null) and (charindex('%', @sp_name) = 0)
  5575.         begin
  5576.             if exists (select * from sysobjects
  5577.                 where uid = user_id()
  5578.                     and name = @sp_name
  5579.                     and type = 'P') /* Object type of Procedure */
  5580.             begin
  5581.                 select @sp_owner = user_name()
  5582.             end
  5583.         end
  5584.     end
  5585.     if @sp_owner is null    /*    If procedure owner not supplied, match all */
  5586.         select @sp_owner = '%'
  5587.  
  5588.     select @proc_type=2        /* Return 2 for 4.2 and later servers. */
  5589.  
  5590.     select
  5591.         PROCEDURE_QUALIFIER = convert(sysname,db_name()),
  5592.         PROCEDURE_OWNER = convert(sysname,user_name(o.uid)),
  5593.         PROCEDURE_NAME = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  5594.         NUM_INPUT_PARAMS = -1,    /* Constant since value unknown */
  5595.         NUM_OUTPUT_PARAMS = -1, /* Constant since value unknown */
  5596.         NUM_RESULT_SETS = -1,    /* Constant since value unknown */
  5597.         REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  5598.         PROCEDURE_TYPE = @proc_type
  5599.     from
  5600.         sysobjects o,syscomments c
  5601.     where
  5602.         o.name like @sp_name
  5603.         and c.colid = 1
  5604.         and user_name(o.uid) like @sp_owner
  5605.         and o.type = 'P'        /* Object type of Procedure */
  5606.         and c.id = o.id
  5607.         and permissions (o.id)&32 <> 0
  5608.     order by 1, 2, 3
  5609. go
  5610.  
  5611. grant execute on sp_stored_procedures to public
  5612. go
  5613.  
  5614. dump tran master with no_log
  5615. go
  5616.  
  5617.  
  5618. print 'creating sp_table_privileges'
  5619. go
  5620.  
  5621. /*    Procedure for pre 6.50 server */
  5622. CREATE PROCEDURE sp_table_privileges (
  5623.             @table_name         varchar(90),
  5624.             @table_owner        varchar(90) = null,
  5625.             @table_qualifier    varchar(32) = null)
  5626. as
  5627.     set nocount on
  5628.  
  5629.     declare @table_id    int,
  5630.             @owner_id     int,
  5631.             @full_table_name char(181)
  5632.     declare @refconst int
  5633.  
  5634.     select @refconst = 1
  5635.     if    (charindex('6.00', @@version) = 0)
  5636.         select @refconst = NULL
  5637.  
  5638.     if @table_qualifier is not null
  5639.     begin
  5640.         if db_name() != @table_qualifier
  5641.         begin    /* If qualifier doesn't match current database */
  5642.             raiserror 20001 'Table qualifier must be name of current database'
  5643.             return
  5644.         end
  5645.     end
  5646.     if @table_owner is null
  5647.     begin    /* If unqualified table name */
  5648.         SELECT @full_table_name = @table_name
  5649.     end
  5650.     else
  5651.     begin    /* Qualified table name */
  5652.         SELECT @full_table_name = @table_owner + '.' + @table_name
  5653.     end
  5654.     /*    Get Object ID */
  5655.     SELECT @table_id = object_id(@full_table_name)
  5656.  
  5657.     if @@trancount != 0
  5658.     begin    /* If inside a transaction */
  5659.         raiserror 20003 'The procedure ''sp_table_privileges'' cannot be executed from within a transaction.'
  5660.         return
  5661.     end
  5662.     create table #table_priv1(
  5663.         table_qualifier            varchar(32) NOT NULL,
  5664.         table_owner             varchar(32) NOT NULL,
  5665.         table_name                varchar(32) NOT NULL,
  5666.         grantor                 varchar(32) NOT NULL,
  5667.         grantee                 varchar(32) NOT NULL,
  5668.         select_privilege        int NOT NULL,
  5669.         insert_privilege        int NOT NULL,
  5670.         update_privilege        int NOT NULL,
  5671.         delete_privilege        int NOT NULL,
  5672.         references_privilege    int NULL,
  5673.         is_grantable            varchar(3) NOT NULL,
  5674.         uid                     int NOT NULL,
  5675.         gid                     int NOT NULL)
  5676.  
  5677.     insert into #table_priv1
  5678.         select distinct
  5679.             db_name(),
  5680.             user_name(o.uid),
  5681.             o.name,
  5682.             user_name(o.uid),
  5683.             u.name,
  5684.             0,
  5685.             0,
  5686.             0,
  5687.             0,
  5688.             @refconst,
  5689.             'no',
  5690.             u.uid,
  5691.             u.gid
  5692.         from sysusers u, sysobjects o
  5693.         where o.id = @table_id and u.uid != u.gid
  5694.            and sysstat & 0xf in (1,2,3)    /* only valid for system tables,
  5695.                                            ** user tables, and views. */
  5696.  
  5697.     /*
  5698.     ** now add row for table owner
  5699.     */
  5700.     if exists (
  5701.         select *
  5702.             from #table_priv1
  5703.             where grantor = grantee)
  5704.     begin
  5705.         update #table_priv1
  5706.         set
  5707.             select_privilege = 1,
  5708.             update_privilege = 1,
  5709.             insert_privilege = 1,
  5710.             delete_privilege = 1,
  5711.             references_privilege = 1,
  5712.             is_grantable = 'yes'
  5713.         where grantor = grantee
  5714.     end
  5715.     else
  5716.     begin
  5717.         insert into #table_priv1
  5718.             select    db_name(),
  5719.                 user_name(o.uid),
  5720.                 o.name,
  5721.                 user_name(o.uid),
  5722.                 user_name(o.uid),
  5723.                 1,
  5724.                 1,
  5725.                 1,
  5726.                 1,
  5727.                 @refconst,
  5728.                 'yes',
  5729.                 o.uid,
  5730.                 u.gid
  5731.             from sysobjects o, sysusers u
  5732.             where o.id = @table_id and u.uid = o.uid
  5733.             and sysstat & 0xf in (1,2,3)    /* only valid for system tables,
  5734.                                            ** user tables, and views. */
  5735.  
  5736.     end
  5737.  
  5738.     update #table_priv1
  5739.     set select_privilege = 1
  5740.     where
  5741.         exists (
  5742.             select * from sysprotects
  5743.             where
  5744.                 id = @table_id
  5745.                 and (#table_priv1.uid = uid
  5746.                     or #table_priv1.gid = uid
  5747.                     or uid = 0)
  5748.                 and protecttype = 205
  5749.                 and action = 193)
  5750.         and not exists (
  5751.             select * from sysprotects
  5752.             where
  5753.                 id = @table_id
  5754.                 and (#table_priv1.uid = uid
  5755.                     or #table_priv1.gid = uid
  5756.                     or uid = 0)
  5757.                 and protecttype = 206
  5758.                 and action = 193)
  5759.  
  5760.     update #table_priv1
  5761.     set insert_privilege = 1
  5762.     where
  5763.         exists (
  5764.             select * from sysprotects
  5765.             where
  5766.                 id = @table_id
  5767.                 and (#table_priv1.uid = uid
  5768.                     or #table_priv1.gid = uid
  5769.                     or uid = 0)
  5770.                 and protecttype = 205
  5771.                 and action = 195)
  5772.         and not exists (
  5773.             select * from sysprotects
  5774.             where
  5775.                 id = @table_id
  5776.                 and (#table_priv1.uid = uid
  5777.                     or #table_priv1.gid = uid
  5778.                     or uid = 0)
  5779.                 and protecttype = 206
  5780.                 and action = 195)
  5781.  
  5782.     update #table_priv1
  5783.     set delete_privilege = 1
  5784.     where
  5785.         exists (
  5786.             select * from sysprotects
  5787.             where
  5788.                 id = @table_id
  5789.                 and (#table_priv1.uid = uid
  5790.                     or #table_priv1.gid = uid
  5791.                     or uid = 0)
  5792.                 and protecttype = 205
  5793.                 and action = 196)
  5794.         and not exists (select * from sysprotects
  5795.             where
  5796.                 id = @table_id
  5797.                 and (#table_priv1.uid = uid
  5798.                     or #table_priv1.gid = uid
  5799.                     or uid = 0)
  5800.                 and protecttype = 206
  5801.                 and action = 196)
  5802.  
  5803.     update #table_priv1
  5804.     set update_privilege = 1
  5805.     where
  5806.         exists (
  5807.             select * from sysprotects
  5808.             where
  5809.                 id = @table_id
  5810.                 and (#table_priv1.uid = uid
  5811.                     or #table_priv1.gid = uid
  5812.                     or uid = 0)
  5813.                 and protecttype = 205
  5814.                 and action = 197)
  5815.         and not exists (
  5816.             select * from sysprotects
  5817.             where
  5818.                 id = @table_id
  5819.                 and (#table_priv1.uid = uid
  5820.                     or #table_priv1.gid = uid
  5821.                     or uid = 0)
  5822.                 and protecttype = 206
  5823.                 and action = 197)
  5824.  
  5825.     update #table_priv1
  5826.     set references_privilege = 1
  5827.     where
  5828.         exists (
  5829.             select * from sysprotects
  5830.             where
  5831.                 id = @table_id
  5832.                 and (#table_priv1.uid = uid
  5833.                     or #table_priv1.gid = uid
  5834.                     or uid = 0)
  5835.                 and protecttype = 205
  5836.                 and action = 26)
  5837.         and not exists (
  5838.             select * from sysprotects
  5839.             where
  5840.                 id = @table_id
  5841.                 and (#table_priv1.uid = uid
  5842.                     or #table_priv1.gid = uid
  5843.                     or uid = 0)
  5844.                 and protecttype = 206
  5845.                 and action = 26)
  5846.  
  5847.     create table #table_priv2(
  5848.         table_qualifier varchar(32) NULL,
  5849.         table_owner     varchar(32) NULL,
  5850.         table_name        varchar(32) NOT NULL,
  5851.         grantor         varchar(32) NULL,
  5852.         grantee         varchar(32) NOT NULL,
  5853.         privilege        varchar(32) NOT NULL,
  5854.         is_grantable    varchar(3) NULL)
  5855.  
  5856.     insert into #table_priv2
  5857.         select
  5858.             table_qualifier,
  5859.             table_owner,
  5860.             table_name,
  5861.             grantor,
  5862.             grantee,
  5863.             'SELECT',
  5864.             is_grantable
  5865.         from #table_priv1
  5866.         where select_privilege = 1
  5867.  
  5868.  
  5869.     insert into #table_priv2
  5870.         select
  5871.             table_qualifier,
  5872.             table_owner,
  5873.             table_name,
  5874.             grantor,
  5875.             grantee,
  5876.             'INSERT',
  5877.             is_grantable
  5878.         from #table_priv1
  5879.         where insert_privilege = 1
  5880.  
  5881.  
  5882.     insert into #table_priv2
  5883.         select
  5884.             table_qualifier,
  5885.             table_owner,
  5886.             table_name,
  5887.             grantor,
  5888.             grantee,
  5889.             'DELETE',
  5890.             is_grantable
  5891.         from #table_priv1
  5892.         where delete_privilege = 1
  5893.  
  5894.  
  5895.     insert into #table_priv2
  5896.         select
  5897.             table_qualifier,
  5898.             table_owner,
  5899.             table_name,
  5900.             grantor,
  5901.             grantee,
  5902.             'UPDATE',
  5903.             is_grantable
  5904.         from #table_priv1
  5905.         where update_privilege = 1
  5906.  
  5907.     insert into #table_priv2
  5908.         select
  5909.             table_qualifier,
  5910.             table_owner,
  5911.             table_name,
  5912.             grantor,
  5913.             grantee,
  5914.             'REFERENCES',
  5915.             is_grantable
  5916.         from #table_priv1
  5917.         where references_privilege = 1
  5918.  
  5919.  
  5920.     select * from #table_priv2
  5921.     order by table_owner,table_name,privilege,grantee
  5922. /*    order by 2,3,6,5 Can't use since fails on 4.21a server */
  5923. go
  5924.  
  5925. if (charindex('6.50', @@version) = 0 and
  5926.     charindex('7.00', @@version) = 0)
  5927. begin
  5928.     print ''
  5929.     print ''
  5930.     print 'Warning:'
  5931.     print 'you are installing the stored procedures '
  5932.     print 'on a pre 6.50 SQL Server.'
  5933.     print 'Ignore the following errors.'
  5934. end
  5935. else
  5936.     drop proc sp_table_privileges
  5937. go
  5938.  
  5939.  
  5940. /*    Procedure for 6.50 server */
  5941. CREATE PROCEDURE sp_table_privileges (
  5942.             @table_name         varchar(96),
  5943.             @table_owner        varchar(96) = null,
  5944.             @table_qualifier    varchar(32) = null)
  5945. as
  5946.  
  5947.     if @table_qualifier is not null
  5948.     begin
  5949.         if db_name() <> @table_qualifier
  5950.         begin    /* If qualifier doesn't match current database */
  5951.             raiserror (15250, -1,-1)
  5952.             return
  5953.         end
  5954.     end
  5955.     if @table_name is null
  5956.         select @table_name = '%'
  5957.     if @table_owner is null /* If no owner supplied, force wildcard */
  5958.         select @table_owner = '%'
  5959.  
  5960.     select
  5961.         convert(varchar(32),db_name()) TABLE_QUALIFIER,
  5962.         convert(varchar(32),user_name(o.uid)) TABLE_OWNER,
  5963.         convert(varchar(32),object_name(o.id)) TABLE_NAME,
  5964.         convert(varchar(32),user_name(p.grantor)) GRANTOR,
  5965.         convert(varchar(32),user_name(u.uid)) GRANTEE,
  5966.         convert(varchar(32),case p.action
  5967.              when 193 then 'SELECT'
  5968.              when 195 then 'INSERT'
  5969.              when 196 then 'DELETE'
  5970.              when 197 then 'UPDATE'
  5971.              else 'REFERENCES'
  5972.         end) PRIVILEGE,
  5973.         convert(varchar(3),case when p.protecttype = 205 then 'NO'
  5974.             else 'YES'
  5975.         end) IS_GRANTABLE
  5976.     from sysprotects p, sysobjects o, sysusers u
  5977.     where
  5978.         p.id = o.id
  5979.         and o.type in ('U','V','S')
  5980.         and object_name(o.id) like @table_name
  5981.         and user_name(o.uid) like @table_owner
  5982.             /* expand groups */
  5983.         and ((p.uid = u.uid and u.uid <> u.gid) or
  5984.              (p.uid = u.gid and u.uid <> u.gid))
  5985.         and p.protecttype <> 206    /* only grant rows */
  5986.         and p.action in (26,193,195,196,197)
  5987.         and o.uid <> u.uid            /* no rows for owner */
  5988.         and not exists (            /* exclude revoke'd privileges */
  5989.             select *
  5990.             from sysprotects p1
  5991.             where
  5992.                 p1.protecttype = 206
  5993.                 and p1.action = p.action
  5994.                 and p1.id = p.id
  5995.                 and p1.uid = u.uid)
  5996.     union all
  5997.     select    /*    Add rows for table owner */
  5998.         convert(varchar(32),db_name()) TABLE_QUALIFIER,
  5999.         convert(varchar(32),user_name(o.uid)) TABLE_OWNER,
  6000.         convert(varchar(32),object_name(o.id)) TABLE_NAME,
  6001.         convert(varchar(32),user_name(u.uid)) GRANTOR,
  6002.         convert(varchar(32),user_name(o.uid)) GRANTEE,
  6003.         convert(varchar(32),case v.number
  6004.             when 193 then 'SELECT'
  6005.             when 195 then 'INSERT'
  6006.             when 196 then 'DELETE'
  6007.             when 197 then 'UPDATE'
  6008.             else 'REFERENCES'
  6009.         end) PRIVILEGE,
  6010.         convert(varchar(3),'YES') IS_GRANTABLE
  6011.     from sysobjects o, master.dbo.spt_values v, sysusers u
  6012.     where
  6013.         object_name(o.id) like @table_name
  6014.         and o.type in ('U','V','S')
  6015.         and user_name(o.uid) like @table_owner
  6016.         and u.suid = 1        /* grantor is dbo of database */
  6017.         and v.type = 'P'    /* cross product to get all exposed privileges */
  6018.         and v.number in (26,193,195,196,197)
  6019.         and not exists (    /* exclude revoke'd privileges */
  6020.             select *
  6021.             from sysprotects p1
  6022.             where
  6023.                 p1.protecttype = 206
  6024.                 and p1.action = v.number
  6025.                 and p1.id = o.id
  6026.                 and p1.uid = o.uid)
  6027.     order by 2,3,6,5
  6028. go
  6029.  
  6030.  
  6031. if (charindex('7.00', @@version) > 0)
  6032.     drop proc sp_table_privileges
  6033. else
  6034. begin
  6035.     print ''
  6036.     print ''
  6037.     print 'Warning:'
  6038.     print 'you are installing the stored procedures '
  6039.     print 'on a pre 7.0 SQL Server.'
  6040.     print 'Ignore the following errors.'
  6041. end
  6042. go
  6043.  
  6044. /*    Procedure for 7.00 server */
  6045. CREATE PROCEDURE sp_table_privileges (
  6046.             @table_name         nvarchar(384),
  6047.             @table_owner        nvarchar(384) = null,
  6048.             @table_qualifier    sysname = null)
  6049. as
  6050.  
  6051.     if @table_qualifier is not null
  6052.     begin
  6053.         if db_name() <> @table_qualifier
  6054.         begin    /* If qualifier doesn't match current database */
  6055.             raiserror (15250, -1,-1)
  6056.             return
  6057.         end
  6058.     end
  6059.     if @table_name is null
  6060.         select @table_name = '%'
  6061.     if @table_owner is null /* If no owner supplied, force wildcard */
  6062.         select @table_owner = '%'
  6063.  
  6064.     select
  6065.         convert(sysname,db_name()) TABLE_QUALIFIER,
  6066.         convert(sysname,user_name(o.uid)) TABLE_OWNER,
  6067.         convert(sysname,object_name(o.id)) TABLE_NAME,
  6068.         convert(sysname,user_name(p.grantor)) GRANTOR,
  6069.         convert(sysname,user_name(u.uid)) GRANTEE,
  6070.         convert(varchar(32),case p.action
  6071.              when 193 then 'SELECT'
  6072.              when 195 then 'INSERT'
  6073.              when 196 then 'DELETE'
  6074.              when 197 then 'UPDATE'
  6075.              else 'REFERENCES'
  6076.         end) PRIVILEGE,
  6077.         convert(varchar(3),case when p.protecttype = 205 then 'NO'
  6078.             else 'YES'
  6079.         end) IS_GRANTABLE
  6080.     from sysprotects p, sysobjects o, sysusers u, sysmembers m
  6081.     where
  6082.         p.id = o.id
  6083.         and o.type in ('U','V','S')
  6084.         and object_name(o.id) like @table_name
  6085.         and user_name(o.uid) like @table_owner
  6086.             /* expand groups - AKUNDONE: only 1 level of grp unrolling. */
  6087.         and (u.uid > 0 and u.uid < 16384)
  6088.         and ((p.uid = u.uid) or
  6089.              (p.uid = m.groupuid and u.uid = m.memberuid))
  6090.         and p.protecttype <> 206    /* only grant rows */
  6091.         and p.action in (26,193,195,196,197)
  6092.         and o.uid <> u.uid            /* no rows for owner */
  6093.         and not exists (            /* exclude revoke'd privileges */
  6094.             select *
  6095.             from sysprotects p1
  6096.             where
  6097.                 p1.protecttype = 206
  6098.                 and p1.action = p.action
  6099.                 and p1.id = p.id
  6100.                 and p1.uid = u.uid)
  6101.     union all
  6102.     select    /*    Add rows for table owner */
  6103.         convert(sysname,db_name()) TABLE_QUALIFIER,
  6104.         convert(sysname,user_name(o.uid)) TABLE_OWNER,
  6105.         convert(sysname,object_name(o.id)) TABLE_NAME,
  6106.         convert(sysname,user_name(u.uid)) GRANTOR,
  6107.         convert(sysname,user_name(o.uid)) GRANTEE,
  6108.         convert(varchar(32),case v.number
  6109.             when 193 then 'SELECT'
  6110.             when 195 then 'INSERT'
  6111.             when 196 then 'DELETE'
  6112.             when 197 then 'UPDATE'
  6113.             else 'REFERENCES'
  6114.         end) PRIVILEGE,
  6115.         convert(varchar(3),'YES') IS_GRANTABLE
  6116.     from sysobjects o, master.dbo.spt_values v, sysusers u
  6117.     where
  6118.         object_name(o.id) like @table_name
  6119.         and o.type in ('U','V','S')
  6120.         and user_name(o.uid) like @table_owner
  6121.         and u.uid = 1        /* grantor is 'dbo' of database */
  6122.         and v.type = 'P'    /* cross product to get all exposed privileges */
  6123.         and v.number in (26,193,195,196,197)
  6124.         and not exists (    /* exclude revoke'd privileges */
  6125.             select *
  6126.             from sysprotects p1
  6127.             where
  6128.                 p1.protecttype = 206
  6129.                 and p1.action = v.number
  6130.                 and p1.id = o.id
  6131.                 and p1.uid = o.uid)
  6132.     order by 2,3,6,5
  6133. go
  6134.  
  6135. grant execute on sp_table_privileges to public
  6136. go
  6137.  
  6138. dump tran master with no_log
  6139. go
  6140.  
  6141. print 'creating sp_tables'
  6142. go
  6143.  
  6144. /*    Procedure for 6.50 and earlier servers */
  6145. create procedure sp_tables(
  6146.                @table_name        varchar(96)    = null,
  6147.                @table_owner     varchar(96)    = null,
  6148.                @table_qualifier varchar(32)    = null,
  6149.                @table_type        varchar(100) = null)
  6150. as
  6151.     declare @type1 varchar(3)
  6152.     declare @tableindex int
  6153.  
  6154.  
  6155.     /* Special feature #1:    enumerate databases when owner and name
  6156.          are blank but qualifier is explicitly '%'.  */
  6157.     if @table_qualifier = '%' and
  6158.         @table_owner = '' and
  6159.         @table_name = ''
  6160.     begin    /* If enumerating databases */
  6161.         select
  6162.             TABLE_QUALIFIER = convert(varchar(32),d.name),
  6163.             TABLE_OWNER = convert(varchar(32),null),
  6164.             TABLE_NAME = convert(varchar(32),null),
  6165.             TABLE_TYPE = convert(varchar(32),null),
  6166.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  6167.         from master.dbo.sysdatabases d
  6168.         where d.name <> 'model'    /* eliminate MODEL database */
  6169.         order by 1
  6170.     end
  6171.  
  6172.     /* Special feature #2:    enumerate owners when qualifier and name
  6173.          are blank but owner is explicitly '%'.  */
  6174.     else if @table_qualifier = '' and
  6175.         @table_owner = '%' and
  6176.         @table_name = ''
  6177.     begin    /* If enumerating owners */
  6178.         select distinct
  6179.             TABLE_QUALIFIER = convert(varchar(32),null),
  6180.             TABLE_OWNER = convert(varchar(32),user_name(uid)),
  6181.             TABLE_NAME = convert(varchar(32),null),
  6182.             TABLE_TYPE = convert(varchar(32),null),
  6183.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  6184.         from sysobjects
  6185.         order by 2
  6186.     end
  6187.  
  6188.     /* Special feature #3:    enumerate table types when qualifier, owner and
  6189.          name are blank but table type is explicitly '%'.    */
  6190.     else if @table_qualifier = '' and
  6191.         @table_owner = '' and
  6192.         @table_name = '' and
  6193.         @table_type = '%'
  6194.     begin    /* If enumerating table types */
  6195.         select
  6196.             TABLE_QUALIFIER = convert(varchar(32),null),
  6197.             TABLE_OWNER = convert(varchar(32),null),
  6198.             TABLE_NAME = convert(varchar(32),null),
  6199.             TABLE_TYPE = convert(varchar(32),rtrim(substring('SYSTEM TABLETABLE       VIEW',(colid-1)*12+1,12))),
  6200.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  6201.         from sysobjects o, syscolumns c
  6202.         where o.id=c.id and o.name='sysusers' and colid<=3
  6203.     end
  6204.  
  6205.     else
  6206.     begin /* end of special features - do normal processing */
  6207.         if @table_qualifier is not null
  6208.         begin
  6209.             if db_name() <> @table_qualifier
  6210.             begin
  6211.                 if @table_qualifier = ''
  6212.                 begin  /* If empty qualifier supplied */
  6213.                     /* Force an empty result set */
  6214.                     select @table_name = ''
  6215.                     select @table_owner = ''
  6216.                 end
  6217.                 else
  6218.                 begin    /* If qualifier doesn't match current database */
  6219.                     raiserror 20001 '~~Rush_5~~'
  6220.                     return
  6221.                 end
  6222.             end
  6223.         end
  6224.         if @table_type is null
  6225.         begin    /* Select all ODBC supported table types */
  6226.             select @type1 = 'SUV'
  6227.         end
  6228.         else
  6229.         begin
  6230.             /*    TableType is case sensitive if CS server */
  6231.             select @type1 = null
  6232.             if (charindex('''SYSTEM TABLE''',@table_type) <> 0)
  6233.                 select @type1 = @type1 + 'S'    /* Add System Tables */
  6234.             if (charindex('''TABLE''',@table_type) <> 0)
  6235.                 select @type1 = @type1 + 'U'    /* Add User Tables */
  6236.             if (charindex('''VIEW''',@table_type) <> 0)
  6237.                 select @type1 = @type1 + 'V'    /* Add Views */
  6238.         end
  6239.         if @table_name is null
  6240.         begin    /*    If table name not supplied, match all */
  6241.             select @table_name = '%'
  6242.         end
  6243.         else
  6244.         begin
  6245.             if (@table_owner is null) and (charindex('%', @table_name) = 0)
  6246.             begin    /* If owner not specified and table is specified */
  6247.                 if exists (select * from sysobjects
  6248.                     where uid = user_id()
  6249.                     and name = @table_name
  6250.                     and (type = 'U' or type = 'V' or type = 'S'))
  6251.                 begin    /* Override supplied owner w/owner of table */
  6252.                     select @table_owner = user_name()
  6253.                 end
  6254.             end
  6255.         end
  6256.         if @table_owner is null /* If no owner supplied, force wildcard */
  6257.             select @table_owner = '%'
  6258.         select
  6259.             TABLE_QUALIFIER = convert(varchar(32),db_name()),
  6260.             TABLE_OWNER = convert(varchar(32),user_name(o.uid)),
  6261.             TABLE_NAME = convert(varchar(32),o.name),    /* make nullable */
  6262.             TABLE_TYPE = convert(varchar(32),rtrim(
  6263.                 substring('SYSTEM TABLE            TABLE       VIEW       ',
  6264.                     (ascii(o.type)-83)*12+1,12))),    /* 'S'=0,'U'=2,'V'=3 */
  6265.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  6266.         from sysusers u, sysobjects o
  6267.         where
  6268.             o.name like @table_name
  6269.             and user_name(o.uid) like @table_owner
  6270.             and charindex(substring(o.type,1,1),@type1) <> 0 /* Only desired types */
  6271.             and u.uid = user_id() /* constrain sysusers uid for use in subquery */
  6272.             and (
  6273.                 suser_id() = 1     /* User is the System Administrator */
  6274.                 or o.uid = user_id()     /* User created the object */
  6275.                 /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  6276.                 or ((select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  6277.                     from sysprotects p
  6278.                     /* outer join to correlate with all rows in sysobjects */
  6279.                     where p.id =* o.id
  6280.                         /* get rows for public,current user,user's group */
  6281.                         and (p.uid = 0 or p.uid = user_id() or p.uid =* u.gid)
  6282.                         /* check for SELECT,EXECUTE privilege */
  6283.                         and (action in (193,224)))&1     /* more magic...normalize GRANT */
  6284.                     ) = 1    /* final magic...compare Grants      */
  6285.             )
  6286.         order by 4, 1, 2, 3
  6287.     end
  6288. go
  6289.  
  6290. if (charindex('7.00', @@version) > 0)
  6291.     drop procedure sp_tables
  6292. else
  6293. begin
  6294.     print ''
  6295.     print ''
  6296.     print 'Warning:'
  6297.     print 'you are installing the stored procedures '
  6298.     print 'on a pre 7.0 SQL Server.'
  6299.     print 'Ignore the following errors.'
  6300. end
  6301. go
  6302.  
  6303. /*    Procedure for 7.00 server */
  6304. create procedure sp_tables(
  6305.                @table_name        nvarchar(384)    = null,
  6306.                @table_owner     nvarchar(384)    = null,
  6307.                @table_qualifier sysname    = null,
  6308.                @table_type        varchar(100) = null)
  6309. as
  6310.     declare @type1 varchar(3)
  6311.     declare @tableindex int
  6312.  
  6313.  
  6314.     /* Special feature #1:    enumerate databases when owner and name
  6315.          are blank but qualifier is explicitly '%'.  */
  6316.     if @table_qualifier = '%' and
  6317.         @table_owner = '' and
  6318.         @table_name = ''
  6319.     begin    /* If enumerating databases */
  6320.         select
  6321.             TABLE_QUALIFIER = convert(sysname,d.name),
  6322.             TABLE_OWNER = convert(sysname,null),
  6323.             TABLE_NAME = convert(sysname,null),
  6324.             TABLE_TYPE = convert(varchar(32),null),
  6325.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  6326.         from master.dbo.sysdatabases d
  6327.         where d.name <> 'model'    /* eliminate MODEL database */
  6328.         order by 1
  6329.     end
  6330.  
  6331.     /* Special feature #2:    enumerate owners when qualifier and name
  6332.          are blank but owner is explicitly '%'.  */
  6333.     else if @table_qualifier = '' and
  6334.         @table_owner = '%' and
  6335.         @table_name = ''
  6336.     begin    /* If enumerating owners */
  6337.         select distinct
  6338.             TABLE_QUALIFIER = convert(sysname,null),
  6339.             TABLE_OWNER = convert(sysname,user_name(uid)),
  6340.             TABLE_NAME = convert(sysname,null),
  6341.             TABLE_TYPE = convert(varchar(32),null),
  6342.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  6343.         from sysobjects
  6344.         order by 2
  6345.     end
  6346.  
  6347.     /* Special feature #3:    enumerate table types when qualifier, owner and
  6348.          name are blank but table type is explicitly '%'.    */
  6349.     else if @table_qualifier = '' and
  6350.         @table_owner = '' and
  6351.         @table_name = '' and
  6352.         @table_type = '%'
  6353.     begin    /* If enumerating table types */
  6354.         select
  6355.             TABLE_QUALIFIER = convert(sysname,null),
  6356.             TABLE_OWNER = convert(sysname,null),
  6357.             TABLE_NAME = convert(sysname,null),
  6358.             TABLE_TYPE = convert(varchar(32),rtrim(substring('SYSTEM TABLETABLE       VIEW',(colid-1)*12+1,12))),
  6359.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  6360.         from sysobjects o, syscolumns c
  6361.         where o.id=c.id and o.name='sysusers' and colid<=3
  6362.     end
  6363.  
  6364.     else
  6365.     begin /* end of special features - do normal processing */
  6366.         if @table_qualifier is not null
  6367.         begin
  6368.             if db_name() <> @table_qualifier
  6369.             begin
  6370.                 if @table_qualifier = ''
  6371.                 begin  /* If empty qualifier supplied */
  6372.                     /* Force an empty result set */
  6373.                     select @table_name = ''
  6374.                     select @table_owner = ''
  6375.                 end
  6376.                 else
  6377.                 begin    /* If qualifier doesn't match current database */
  6378.                     raiserror (15250, -1,-1)
  6379.                     return
  6380.                 end
  6381.             end
  6382.         end
  6383.         if @table_type is null
  6384.         begin    /* Select all ODBC supported table types */
  6385.             select @type1 = 'SUV'
  6386.         end
  6387.         else
  6388.         begin
  6389.             /*    TableType is case sensitive if CS server */
  6390.             select @type1 = ''
  6391.             if (charindex('''SYSTEM TABLE''',@table_type) <> 0)
  6392.                 select @type1 = @type1 + 'S'    /* Add System Tables */
  6393.             if (charindex('''TABLE''',@table_type) <> 0)
  6394.                 select @type1 = @type1 + 'U'    /* Add User Tables */
  6395.             if (charindex('''VIEW''',@table_type) <> 0)
  6396.                 select @type1 = @type1 + 'V'    /* Add Views */
  6397.         end
  6398.         if @table_name is null
  6399.         begin    /*    If table name not supplied, match all */
  6400.             select @table_name = '%'
  6401.         end
  6402.         else
  6403.         begin
  6404.             if (@table_owner is null) and (charindex('%', @table_name) = 0)
  6405.             begin    /* If owner not specified and table is specified */
  6406.                 if exists (select * from sysobjects
  6407.                     where uid = user_id()
  6408.                     and name = @table_name
  6409.                     and (type = 'U' or type = 'V' or type = 'S'))
  6410.                 begin    /* Override supplied owner w/owner of table */
  6411.                     select @table_owner = user_name()
  6412.                 end
  6413.             end
  6414.         end
  6415.         if @table_owner is null /* If no owner supplied, force wildcard */
  6416.             select @table_owner = '%'
  6417.         select
  6418.             TABLE_QUALIFIER = convert(sysname,db_name()),
  6419.             TABLE_OWNER = convert(sysname,user_name(o.uid)),
  6420.             TABLE_NAME = convert(sysname,o.name),    /* make nullable */
  6421.             TABLE_TYPE = convert(varchar(32),rtrim(
  6422.                 substring('SYSTEM TABLE            TABLE       VIEW       ',
  6423.                     (ascii(o.type)-83)*12+1,12))),    /* 'S'=0,'U'=2,'V'=3 */
  6424.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  6425.         from sysobjects o
  6426.         where
  6427.             o.name like @table_name
  6428.             and user_name(o.uid) like @table_owner
  6429.             and o.type in ('U','V','S')
  6430.             and charindex(substring(o.type,1,1),@type1) <> 0 /* Only desired types */
  6431.             and permissions (o.id)&4096 <> 0
  6432.         order by 4, 1, 2, 3
  6433.     end
  6434. go
  6435.  
  6436.  
  6437. grant execute on sp_tables to public
  6438. go
  6439.  
  6440. dump tran master with no_log
  6441. go
  6442.  
  6443. /*-----------------------------------------------------------------------------*/
  6444. /*-------------- CATALOG STORED PROCEDURES FOR OLEDB SOURCES ------------------*/
  6445. /*-----------------------------------------------------------------------------*/
  6446.  
  6447. if object_id('sp_linkedservers', 'P') is not null
  6448.     drop proc sp_linkedservers
  6449. go
  6450. raiserror(15339,-1,-1,'sp_linkedservers')
  6451. go
  6452. create proc sp_linkedservers as
  6453.     select
  6454.         SRV_NAME = srvname,
  6455.         SRV_PROVIDERNAME = providername,
  6456.         SRV_PRODUCT = srvproduct,
  6457.         SRV_DATASOURCE = datasource,
  6458.         SRV_PROVIDERSTRING = providerstring,
  6459.         SRV_LOCATION = location,
  6460.         SRV_CAT = catalog
  6461.     from master.dbo.sysservers
  6462.     order by 1
  6463. go
  6464. grant execute on sp_linkedservers to public
  6465. go
  6466.  
  6467.  
  6468. if object_id('sp_catalogs', 'P') is not null
  6469.     drop proc sp_catalogs
  6470. go
  6471. raiserror(15339,-1,-1,'sp_catalogs')
  6472. go
  6473. create procedure sp_catalogs(
  6474.     @server_name        sysname)
  6475. as
  6476.     select
  6477.         CATALOG_NAME,
  6478.         convert (nvarchar(255),DESCRIPTION)
  6479.     from master.dbo.SYSREMOTE_CATALOGS < @server_name >
  6480.     order by CATALOG_NAME
  6481. go
  6482. grant execute on sp_catalogs to public
  6483. go
  6484.  
  6485.  
  6486. if object_id('sp_tables_ex', 'P') is not null
  6487.     drop proc sp_tables_ex
  6488. go
  6489. raiserror(15339,-1,-1,'sp_tables_ex')
  6490. go
  6491. create procedure sp_tables_ex(
  6492.     @table_server        sysname,
  6493.     @table_name            sysname = null,
  6494.     @table_schema        sysname = null,
  6495.     @table_catalog        sysname = null,
  6496.     @table_type            sysname = null)
  6497. as
  6498.     declare @table_catalog_param        sysname
  6499.     if ((isnull(charindex('%', @table_catalog),0) = 0) and
  6500.         (isnull(charindex('[', @table_catalog),0) = 0) and
  6501.         (isnull(charindex('_', @table_catalog),0) = 0))
  6502.         select @table_catalog_param = @table_catalog
  6503.     else
  6504.         select @table_catalog_param = null
  6505.  
  6506.     if ((isnull(charindex('%', @table_name),0) = 0) and
  6507.         (isnull(charindex('[', @table_name),0) = 0) and
  6508.         (isnull(charindex('_', @table_name),0) = 0))
  6509.     begin    /*    If no wild carding */
  6510.         select
  6511.             TABLE_CAT = TABLE_CATALOG,
  6512.             TABLE_SCHEM = TABLE_SCHEMA,
  6513.             TABLE_NAME = TABLE_NAME,
  6514.             TABLE_TYPE = TABLE_TYPE,
  6515.             REMARKS = convert(nvarchar(255),DESCRIPTION)
  6516.         from master.dbo.SYSREMOTE_TABLES <
  6517.                     @table_server,
  6518.                     @table_catalog_param,
  6519.                     NULL,
  6520.                     @table_name,
  6521.                     NULL >
  6522.         where (TABLE_SCHEMA like @table_schema
  6523.             or @table_schema is NULL or (TABLE_SCHEMA is NULL and @table_schema = N'%'))
  6524.             and (TABLE_CATALOG like @table_catalog
  6525.             or     @table_catalog is NULL)
  6526.             and (charindex ('''' + TABLE_TYPE + '''', @table_type) <> 0
  6527.             or     @table_type is NULL)
  6528.         order by TABLE_TYPE, TABLE_CAT, TABLE_SCHEM, TABLE_NAME
  6529.     end
  6530.     else
  6531.     begin    /*    If wild carding */
  6532.         select
  6533.             TABLE_CAT = TABLE_CATALOG,
  6534.             TABLE_SCHEM = TABLE_SCHEMA,
  6535.             TABLE_NAME = TABLE_NAME,
  6536.             TABLE_TYPE = TABLE_TYPE,
  6537.             REMARKS = convert(nvarchar(255),DESCRIPTION)
  6538.         from master.dbo.SYSREMOTE_TABLES <
  6539.                     @table_server,
  6540.                     @table_catalog_param,
  6541.                     NULL,
  6542.                     NULL,
  6543.                     NULL >
  6544.         where (TABLE_SCHEMA like @table_schema
  6545.             or @table_schema is NULL or (TABLE_SCHEMA is NULL and @table_schema = N'%'))
  6546.             and (TABLE_CATALOG like @table_catalog
  6547.             or     @table_catalog is NULL)
  6548.             and (charindex ('''' + TABLE_TYPE + '''', @table_type) <> 0
  6549.             or     @table_type is NULL)
  6550.             and TABLE_NAME like @table_name
  6551.         order by TABLE_TYPE, TABLE_CAT, TABLE_SCHEM, TABLE_NAME
  6552.     end
  6553. go
  6554. grant execute on sp_tables_ex to public
  6555. go
  6556.  
  6557.  
  6558. if object_id('sp_columns_ex', 'P') is not null
  6559.     drop proc sp_columns_ex
  6560. go
  6561. raiserror(15339,-1,-1,'sp_columns_ex')
  6562. go
  6563. create procedure sp_columns_ex(
  6564.     @table_server        sysname,
  6565.     @table_name            sysname = null,
  6566.     @table_schema        sysname = null,
  6567.     @table_catalog        sysname = null,
  6568.     @column_name        sysname = null,
  6569.     @ODBCVer            int = 2)
  6570. as
  6571.  
  6572.     set nocount on
  6573.     declare
  6574.     @DBCOLUMNFLAGS_ISFIXEDLENGTH binary(1),    @DBCOLUMNFLAGS_ISLONG    binary(1),
  6575.     @DBCOLUMNFLAGS_ISROWVER    binary(2)
  6576.  
  6577.     select
  6578.     @DBCOLUMNFLAGS_ISFIXEDLENGTH    = 0x10,    @DBCOLUMNFLAGS_ISLONG    = 0x80,
  6579.     @DBCOLUMNFLAGS_ISROWVER    = 0x0200
  6580.  
  6581.     declare    
  6582.     @DBTYPE_I2 smallint,            @DBTYPE_I4 smallint,
  6583.     @DBTYPE_R4 smallint,            @DBTYPE_R8 smallint,
  6584.     @DBTYPE_CY smallint,            @DBTYPE_DATE smallint,
  6585.     @DBTYPE_BSTR smallint,            @DBTYPE_BOOL smallint,
  6586.     @DBTYPE_VARIANT smallint,        @DBTYPE_DECIMAL smallint,
  6587.     @DBTYPE_UI1 smallint,            @DBTYPE_BYREF smallint,
  6588.     @DBTYPE_I1 smallint,            @DBTYPE_UI2 smallint,
  6589.     @DBTYPE_UI4 smallint,            @DBTYPE_I8 smallint,
  6590.     @DBTYPE_UI8 smallint,            @DBTYPE_GUID smallint,
  6591.     @DBTYPE_BYTES smallint,            @DBTYPE_STR smallint,
  6592.     @DBTYPE_WSTR smallint,            @DBTYPE_NUMERIC smallint,
  6593.     @DBTYPE_DBDATE smallint,        @DBTYPE_DBTIME smallint,
  6594.     @DBTYPE_DBTIMESTAMP smallint
  6595.  
  6596.     select
  6597.     @DBTYPE_I2            = 2,    @DBTYPE_I4            = 3,
  6598.     @DBTYPE_R4            = 4,    @DBTYPE_R8            = 5,
  6599.     @DBTYPE_CY            = 6,    @DBTYPE_DATE        = 7,
  6600.     @DBTYPE_BSTR        = 8,    @DBTYPE_BOOL        = 11,
  6601.     @DBTYPE_VARIANT        = 12,    @DBTYPE_DECIMAL        = 14,
  6602.     @DBTYPE_UI1            = 17,    @DBTYPE_BYREF        = 16384,
  6603.     @DBTYPE_I1            = 16,    @DBTYPE_UI2            = 18,
  6604.     @DBTYPE_UI4            = 19,    @DBTYPE_I8            = 20,
  6605.     @DBTYPE_UI8            = 21,    @DBTYPE_GUID        = 72,
  6606.     @DBTYPE_BYTES        = 128,    @DBTYPE_STR            = 129,
  6607.     @DBTYPE_WSTR        = 130,    @DBTYPE_NUMERIC        = 131,
  6608.     @DBTYPE_DBDATE        = 133,    @DBTYPE_DBTIME        = 134,    
  6609.     @DBTYPE_DBTIMESTAMP    = 135
  6610.  
  6611.     declare    
  6612.     @ODS_SMALLINT_NOT_NULL tinyint,    @ODS_INT_NULL tinyint,
  6613.     @ODS_INT_NOT_NULL tinyint,        @ODS_TINYINT_NOT_NULL tinyint,
  6614.     @ODS_REAL_NOT_NULL tinyint,        @ODS_FLOAT_NULL tinyint,
  6615.     @ODS_FLOAT_NOT_NULL tinyint,    @ODS_MONEY_NULL tinyint,
  6616.     @ODS_MONEY_NOT_NULL tinyint,    @ODS_DATETIME_NULL tinyint,
  6617.     @ODS_DATETIME_NOT_NULL tinyint,    @ODS_TEXT tinyint,
  6618.     @ODS_BIT tinyint,                @ODS_DECIMAL tinyint,
  6619.     @ODS_NUMERIC tinyint,            @ODS_GUID tinyint,
  6620.     @ODS_IMAGE tinyint,                @ODS_TIMESTAMP tinyint,
  6621.     @ODS_BINARY_NULL tinyint,        @ODS_BINARY_NOT_NULL tinyint,
  6622.     @ODS_VARBINARY tinyint,            @ODS_CHAR_NULL tinyint,
  6623.     @ODS_CHAR_NOT_NULL tinyint,        @ODS_VARCHAR tinyint
  6624.  
  6625.     select     
  6626.     @ODS_IMAGE = 34,            @ODS_TEXT = 35,
  6627.     @ODS_GUID = 36,                @ODS_BINARY_NULL = 37,            
  6628.     @ODS_VARBINARY = 37,        @ODS_INT_NULL = 38,
  6629.     @ODS_CHAR_NULL = 39,        @ODS_VARCHAR = 39,
  6630.     @ODS_TIMESTAMP = 45,        @ODS_BINARY_NOT_NULL = 45,
  6631.     @ODS_CHAR_NOT_NULL = 47,    @ODS_TINYINT_NOT_NULL = 48,
  6632.     @ODS_BIT = 50,                @ODS_SMALLINT_NOT_NULL = 52,    
  6633.     @ODS_INT_NOT_NULL = 56,        @ODS_REAL_NOT_NULL = 59,        
  6634.     @ODS_MONEY_NOT_NULL = 60,    @ODS_DATETIME_NOT_NULL = 61,    
  6635.     @ODS_FLOAT_NOT_NULL = 62,    @ODS_DECIMAL = 106,
  6636.     @ODS_NUMERIC = 108,            @ODS_FLOAT_NULL = 109,
  6637.     @ODS_MONEY_NULL = 110,        @ODS_DATETIME_NULL = 111
  6638.  
  6639.     declare 
  6640.     @ODBC_GUID smallint,        @ODBC_NTEXT smallint,
  6641.     @ODBC_NVARCHAR smallint,    @ODBC_NCHAR smallint,
  6642.     @ODBC_BIT smallint,            @ODBC_TINYINT smallint,
  6643.     @ODBC_IMAGE smallint,        @ODBC_VARBINARY smallint,
  6644.     @ODBC_TIMESTAMP smallint,    @ODBC_BINARY smallint,
  6645.     @ODBC_TEXT smallint,        @ODBC_CHAR smallint,
  6646.     @ODBC_NUMERIC smallint,        @ODBC_DECIMAL smallint,
  6647.     @ODBC_MONEY smallint,        @ODBC_INT smallint,        
  6648.     @ODBC_SMALLINT smallint,    @ODBC_FLOAT smallint,    
  6649.     @ODBC_REAL smallint,        @ODBC_VARCHAR smallint,
  6650.     @ODBC_DATETIME smallint
  6651.  
  6652.     select
  6653.     @ODBC_GUID        = -11,    @ODBC_NTEXT        = -10,
  6654.     @ODBC_NVARCHAR    = -9,    @ODBC_NCHAR        = -8,
  6655.     @ODBC_BIT        = -7,    @ODBC_TINYINT    = -6,
  6656.     @ODBC_IMAGE        = -4,    @ODBC_VARBINARY    = -3,
  6657.     @ODBC_TIMESTAMP    = -2,    @ODBC_BINARY    = -2,
  6658.     @ODBC_TEXT        = -1,    @ODBC_CHAR        = 1,
  6659.     @ODBC_NUMERIC    = 2,    @ODBC_DECIMAL    = 3,
  6660.     @ODBC_MONEY        = 3,    @ODBC_INT        = 4,        
  6661.     @ODBC_SMALLINT    = 5,    @ODBC_FLOAT        = 6,    
  6662.     @ODBC_REAL        = 7,    @ODBC_VARCHAR    = 12,
  6663.     @ODBC_DATETIME    =    
  6664.     case @ODBCVer
  6665.         when 2 then 11
  6666.         else 93
  6667.     end
  6668.  
  6669.     create table #tmp_columns
  6670.     (    TABLE_CAT sysname NULL,
  6671.         TABLE_SCHEM sysname NULL,
  6672.         TABLE_NAME sysname    NOT NULL,
  6673.         COLUMN_NAME sysname NULL,
  6674.         DATA_TYPE smallint NOT NULL,
  6675.         TYPE_NAME sysname  NULL,
  6676.         COLUMN_SIZE int NULL,
  6677.         BUFFER_LENGTH int NULL,
  6678.         DECIMAL_DIGITS smallint NULL,
  6679.         NUM_PREC_RADIX smallint NULL,
  6680.         NULLABLE smallint NOT NULL,
  6681.         REMARKS nvarchar(255) NULL,
  6682.         COLUMN_DEF nvarchar(127) NULL,
  6683.         SQL_DATA_TYPE smallint null,
  6684.         SQL_DATETIME_SUB smallint NULL,
  6685.         CHAR_OCTET_LENGTH int NULL,
  6686.         ORDINAL_POSITION smallint,
  6687.         IS_NULLABLE varchar(254) NOT NULL,
  6688.         SS_DATA_TYPE tinyint null,
  6689.         COLUMN_FLAGS int NOT NULL)
  6690.  
  6691.     if ((isnull(charindex('%', @table_name),0) = 0) and
  6692.         (isnull(charindex('[', @table_name),0) = 0) and
  6693.         (isnull(charindex('_', @table_name),0) = 0))
  6694.     begin    /*    If no wild carding */
  6695.         insert into #tmp_columns
  6696.         select
  6697.             TABLE_CAT = c.TABLE_CATALOG,
  6698.             TABLE_SCHEM = c.TABLE_SCHEMA,
  6699.             TABLE_NAME = c.TABLE_NAME,
  6700.             COLUMN_NAME = c.COLUMN_NAME,
  6701.             DATA_TYPE =
  6702.             case c.DATA_TYPE & ~@DBTYPE_BYREF
  6703.                 when @DBTYPE_I2                then @ODBC_SMALLINT
  6704.                 when @DBTYPE_I4                then @ODBC_INT
  6705.                 when @DBTYPE_R4                then @ODBC_REAL
  6706.                 when @DBTYPE_R8                then @ODBC_FLOAT
  6707.                 when @DBTYPE_CY                then @ODBC_MONEY
  6708.                 when @DBTYPE_DATE            then @ODBC_DATETIME
  6709.                 when @DBTYPE_DBDATE            then @ODBC_DATETIME
  6710.                 when @DBTYPE_DBTIME            then @ODBC_DATETIME
  6711.                 when @DBTYPE_DBTIMESTAMP    then @ODBC_DATETIME
  6712.                 when @DBTYPE_BOOL            then @ODBC_BIT
  6713.                 when @DBTYPE_VARIANT        then @ODBC_NVARCHAR
  6714.                 when @DBTYPE_DECIMAL        then @ODBC_DECIMAL
  6715.                 when @DBTYPE_UI1            then @ODBC_TINYINT
  6716.                 when @DBTYPE_I1                then @ODBC_NUMERIC
  6717.                 when @DBTYPE_UI2            then @ODBC_NUMERIC
  6718.                 when @DBTYPE_UI4            then @ODBC_NUMERIC
  6719.                 when @DBTYPE_I8                then @ODBC_NUMERIC
  6720.                 when @DBTYPE_UI8            then @ODBC_NUMERIC
  6721.                 when @DBTYPE_NUMERIC        then @ODBC_NUMERIC
  6722.                 when @DBTYPE_GUID            then @ODBC_GUID
  6723.                 when @DBTYPE_BYTES then
  6724.                     case
  6725.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG ) or (p.COLUMN_SIZE > 8000) then @ODBC_IMAGE
  6726.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISROWVER) = @DBCOLUMNFLAGS_ISROWVER AND
  6727.                                 (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH) = @DBCOLUMNFLAGS_ISFIXEDLENGTH and
  6728.                                  p.COLUMN_SIZE = 8
  6729.                             then @ODBC_TIMESTAMP
  6730.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH)
  6731.                             then @ODBC_BINARY
  6732.                         else @ODBC_VARBINARY
  6733.                     end
  6734.                  when @DBTYPE_STR  then
  6735.                     case
  6736.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG ) or (p.COLUMN_SIZE > 8000) then @ODBC_TEXT
  6737.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_CHAR
  6738.                         else @ODBC_VARCHAR
  6739.                     end
  6740.                 when @DBTYPE_WSTR then
  6741.                     case
  6742.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG) or (p.COLUMN_SIZE > 4000) then @ODBC_NTEXT
  6743.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_NCHAR
  6744.                         else @ODBC_NVARCHAR
  6745.                     end
  6746.                 when @DBTYPE_BSTR then
  6747.                     case
  6748.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG) or (p.COLUMN_SIZE > 4000) then @ODBC_NTEXT
  6749.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_NCHAR
  6750.                         else @ODBC_NVARCHAR
  6751.                     end
  6752.             end,
  6753.             TYPE_NAME = p.TYPE_NAME,
  6754.             COLUMN_SIZE =
  6755.                 COALESCE(c.CHARACTER_MAXIMUM_LENGTH, c.NUMERIC_PRECISION, p.COLUMN_SIZE),
  6756.             BUFFER_LENGTH = c.CHARACTER_OCTET_LENGTH,
  6757.             DECIMAL_DIGITS = c.NUMERIC_SCALE,
  6758.             NUM_PREC_RADIX = NULL,
  6759.             NULLABLE = convert(smallint, c.IS_NULLABLE),
  6760.             REMARKS = convert(nvarchar(255),c.DESCRIPTION),
  6761.             COLUMN_DEF = c.COLUMN_DEFAULT,
  6762.             SQL_DATA_TYPE = NULL,
  6763.             SQL_DATETIME_SUB = convert(smallint, c.DATETIME_PRECISION),
  6764.             CHAR_OCTET_LENGTH = c.CHARACTER_OCTET_LENGTH,
  6765.             ORDINAL_POSITION = c.ORDINAL_POSITION,
  6766.             IS_NULLABLE =
  6767.             case c.IS_NULLABLE
  6768.                 when 1 then 'YES'
  6769.                 else 'NO'
  6770.             end,
  6771.             SS_DATA_TYPE = NULL,
  6772.             COLUMN_FLAGS = c.COLUMN_FLAGS
  6773.         from master.dbo.SYSREMOTE_COLUMNS <
  6774.                     @table_server,
  6775.                     @table_catalog,
  6776.                     NULL,
  6777.                     @table_name,
  6778.                     NULL > c
  6779.             LEFT OUTER JOIN
  6780.                 master.dbo.SYSREMOTE_PROVIDER_TYPES <
  6781.                     @table_server > p
  6782.                     /* LUXOR/KAGERA dont support restricting DATA_TYPE or BEST_MATCH */
  6783.             ON c.DATA_TYPE = p.DATA_TYPE AND p.BEST_MATCH = 1
  6784.             where (TABLE_SCHEMA like @table_schema
  6785.                 or @table_schema is NULL or (TABLE_SCHEMA is NULL and @table_schema = N'%'))
  6786.                 and (c.COLUMN_NAME like @column_name
  6787.                 or     @column_name is NULL)
  6788.     end
  6789.     else
  6790.     begin    /*    If wild carding */
  6791.         insert into #tmp_columns
  6792.         select
  6793.             TABLE_CAT = c.TABLE_CATALOG,
  6794.             TABLE_SCHEM = c.TABLE_SCHEMA,
  6795.             TABLE_NAME = c.TABLE_NAME,
  6796.             COLUMN_NAME = c.COLUMN_NAME,
  6797.             DATA_TYPE =
  6798.             case c.DATA_TYPE & ~@DBTYPE_BYREF
  6799.                 when @DBTYPE_I2                then @ODBC_SMALLINT
  6800.                 when @DBTYPE_I4                then @ODBC_INT
  6801.                 when @DBTYPE_R4                then @ODBC_REAL
  6802.                 when @DBTYPE_R8                then @ODBC_FLOAT
  6803.                 when @DBTYPE_CY                then @ODBC_MONEY
  6804.                 when @DBTYPE_DATE            then @ODBC_DATETIME
  6805.                 when @DBTYPE_DBDATE            then @ODBC_DATETIME
  6806.                 when @DBTYPE_DBTIME            then @ODBC_DATETIME
  6807.                 when @DBTYPE_DBTIMESTAMP    then @ODBC_DATETIME
  6808.                 when @DBTYPE_BOOL            then @ODBC_BIT
  6809.                 when @DBTYPE_VARIANT        then @ODBC_NVARCHAR
  6810.                 when @DBTYPE_DECIMAL        then @ODBC_DECIMAL
  6811.                 when @DBTYPE_UI1            then @ODBC_TINYINT
  6812.                 when @DBTYPE_I1                then @ODBC_NUMERIC
  6813.                 when @DBTYPE_UI2            then @ODBC_NUMERIC
  6814.                 when @DBTYPE_UI4            then @ODBC_NUMERIC
  6815.                 when @DBTYPE_I8                then @ODBC_NUMERIC
  6816.                 when @DBTYPE_UI8            then @ODBC_NUMERIC
  6817.                 when @DBTYPE_NUMERIC        then @ODBC_NUMERIC
  6818.                 when @DBTYPE_GUID            then @ODBC_GUID
  6819.                 when @DBTYPE_BYTES then
  6820.                     case
  6821.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG ) or (p.COLUMN_SIZE > 8000) then @ODBC_IMAGE
  6822.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISROWVER) = @DBCOLUMNFLAGS_ISROWVER AND
  6823.                                 (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH) = @DBCOLUMNFLAGS_ISFIXEDLENGTH and
  6824.                                  p.COLUMN_SIZE = 8
  6825.                             then @ODBC_TIMESTAMP
  6826.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH)
  6827.                             then @ODBC_BINARY
  6828.                         else @ODBC_VARBINARY
  6829.                     end
  6830.                  when @DBTYPE_STR  then
  6831.                     case
  6832.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG ) or (p.COLUMN_SIZE > 8000) then @ODBC_TEXT
  6833.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_CHAR
  6834.                         else @ODBC_VARCHAR
  6835.                     end
  6836.                 when @DBTYPE_WSTR then
  6837.                     case
  6838.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG) or (p.COLUMN_SIZE > 4000) then @ODBC_NTEXT
  6839.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_NCHAR
  6840.                         else @ODBC_NVARCHAR
  6841.                     end
  6842.                 when @DBTYPE_BSTR then
  6843.                     case
  6844.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG) or (p.COLUMN_SIZE > 4000) then @ODBC_NTEXT
  6845.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_NCHAR
  6846.                         else @ODBC_NVARCHAR
  6847.                     end
  6848.             end,
  6849.             TYPE_NAME = p.TYPE_NAME,
  6850.             COLUMN_SIZE =
  6851.                 COALESCE(c.CHARACTER_MAXIMUM_LENGTH, c.NUMERIC_PRECISION, p.COLUMN_SIZE),
  6852.             BUFFER_LENGTH = c.CHARACTER_OCTET_LENGTH,
  6853.             DECIMAL_DIGITS = c.NUMERIC_SCALE,
  6854.             NUM_PREC_RADIX = NULL,
  6855.             NULLABLE = convert(smallint, c.IS_NULLABLE),
  6856.             REMARKS = convert(nvarchar(255),c.DESCRIPTION),
  6857.             COLUMN_DEF = c.COLUMN_DEFAULT,
  6858.             SQL_DATA_TYPE = NULL,
  6859.             SQL_DATETIME_SUB = convert(smallint, c.DATETIME_PRECISION),
  6860.             CHAR_OCTET_LENGTH = c.CHARACTER_OCTET_LENGTH,
  6861.             ORDINAL_POSITION = c.ORDINAL_POSITION,
  6862.             IS_NULLABLE =
  6863.             case c.IS_NULLABLE
  6864.                 when 1 then 'YES'
  6865.                 else 'NO'
  6866.             end,
  6867.             SS_DATA_TYPE = NULL,
  6868.             COLUMN_FLAGS = c.COLUMN_FLAGS
  6869.         from master.dbo.SYSREMOTE_COLUMNS <
  6870.                     @table_server,
  6871.                     @table_catalog,
  6872.                     NULL,
  6873.                     NULL,
  6874.                     NULL > c
  6875.             LEFT OUTER JOIN
  6876.                 master.dbo.SYSREMOTE_PROVIDER_TYPES <
  6877.                     @table_server > p
  6878.                     /* LUXOR/KAGERA dont support restricting DATA_TYPE or BEST_MATCH */
  6879.             ON c.DATA_TYPE = p.DATA_TYPE AND p.BEST_MATCH = 1
  6880.             where (TABLE_SCHEMA like @table_schema
  6881.                 or @table_schema is NULL or (TABLE_SCHEMA is NULL and @table_schema = N'%'))
  6882.                 and (c.COLUMN_NAME like @column_name
  6883.                 or     @column_name is NULL)
  6884.                 and c.TABLE_NAME like @table_name
  6885.     end
  6886.  
  6887.     update #tmp_columns
  6888.     set SQL_DATA_TYPE = spt_dt.SQL_DATA_TYPE,
  6889.         SQL_DATETIME_SUB =
  6890.         case
  6891.             when #tmp_columns.DATA_TYPE = @ODBC_DATETIME then 3
  6892.             else #tmp_columns.SQL_DATETIME_SUB
  6893.         end,
  6894.         TYPE_NAME = 
  6895.         case 
  6896.             when #tmp_columns.DATA_TYPE = @ODBC_BINARY and (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISROWVER) = @DBCOLUMNFLAGS_ISROWVER AND
  6897.                     (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH) = @DBCOLUMNFLAGS_ISFIXEDLENGTH and
  6898.                     BUFFER_LENGTH = 8
  6899.                 then 'timestamp'
  6900.             when #tmp_columns.DATA_TYPE = @ODBC_DATETIME and
  6901.                     #tmp_columns.SQL_DATETIME_SUB = 0
  6902.                 then 'smalldatetime'
  6903.             when #tmp_columns.DATA_TYPE = @ODBC_MONEY and
  6904.                     #tmp_columns.COLUMN_SIZE = 10
  6905.                 then 'smallmoney'
  6906.             when (#tmp_columns.DATA_TYPE in (@ODBC_BINARY, @ODBC_CHAR) and
  6907.                     (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH) = @DBCOLUMNFLAGS_ISFIXEDLENGTH) or
  6908.                  #tmp_columns.DATA_TYPE in (@ODBC_TEXT, @ODBC_NTEXT, @ODBC_IMAGE)
  6909.                 then spt_dt.TYPE_NAME
  6910.             else isnull(#tmp_columns.TYPE_NAME, spt_dt.TYPE_NAME)
  6911.         end,
  6912.         COLUMN_SIZE =
  6913.         case
  6914.             when #tmp_columns.DATA_TYPE IN (@ODBC_REAL, @ODBC_FLOAT) then /* app. numeric types */
  6915.                 spt_dt.data_precision
  6916.             when #tmp_columns.DATA_TYPE = @ODBC_DATETIME and
  6917.                     #tmp_columns.SQL_DATETIME_SUB = 0
  6918.                 then 16
  6919.             else #tmp_columns.COLUMN_SIZE
  6920.         end,
  6921.         BUFFER_LENGTH =
  6922.         case
  6923.             when #tmp_columns.DATA_TYPE IN (@ODBC_NUMERIC, @ODBC_DECIMAL) then    /* decimal/numeric types */
  6924.                 COLUMN_SIZE+2
  6925.             when isnull (#tmp_columns.BUFFER_LENGTH,0) = 0 then spt_dt.length
  6926.             else #tmp_columns.BUFFER_LENGTH
  6927.         end,
  6928.         DECIMAL_DIGITS =
  6929.         case
  6930.             when #tmp_columns.DATA_TYPE = @ODBC_DATETIME
  6931.                 then #tmp_columns.SQL_DATETIME_SUB
  6932.             else COALESCE (DECIMAL_DIGITS, spt_dt.numeric_scale)
  6933.         end,
  6934.         NUM_PREC_RADIX = spt_dt.RADIX,
  6935.         SS_DATA_TYPE = 
  6936.         case
  6937.             when #tmp_columns.DATA_TYPE in ( @ODBC_TINYINT, @ODBC_SMALLINT, @ODBC_INT ) 
  6938.                 and #tmp_columns.NULLABLE = 1
  6939.                     then @ODS_INT_NULL
  6940.             when #tmp_columns.DATA_TYPE = @ODBC_TINYINT and #tmp_columns.NULLABLE = 0 
  6941.                 then @ODS_TINYINT_NOT_NULL
  6942.             when #tmp_columns.DATA_TYPE = @ODBC_SMALLINT    and #tmp_columns.NULLABLE = 0
  6943.                 then @ODS_SMALLINT_NOT_NULL
  6944.             when #tmp_columns.DATA_TYPE = @ODBC_INT  and #tmp_columns.NULLABLE = 0
  6945.                 then @ODS_INT_NOT_NULL
  6946.             when #tmp_columns.DATA_TYPE in (@ODBC_REAL, @ODBC_FLOAT) and #tmp_columns.NULLABLE = 1
  6947.                 then @ODS_FLOAT_NULL
  6948.             when #tmp_columns.DATA_TYPE = @ODBC_REAL and #tmp_columns.NULLABLE = 0
  6949.                 then @ODS_REAL_NOT_NULL
  6950.             when #tmp_columns.DATA_TYPE = @ODBC_FLOAT and #tmp_columns.NULLABLE = 0
  6951.                 then @ODS_FLOAT_NOT_NULL
  6952.             when #tmp_columns.DATA_TYPE = @ODBC_MONEY and #tmp_columns.NULLABLE = 1 
  6953.                 then @ODS_MONEY_NULL
  6954.             when #tmp_columns.DATA_TYPE = @ODBC_MONEY and #tmp_columns.NULLABLE = 0 
  6955.                 then @ODS_MONEY_NOT_NULL
  6956.             when #tmp_columns.DATA_TYPE = @ODBC_DATETIME and #tmp_columns.NULLABLE = 1
  6957.                 then @ODS_DATETIME_NULL
  6958.             when #tmp_columns.DATA_TYPE = @ODBC_DATETIME and #tmp_columns.NULLABLE = 0
  6959.                 then @ODS_DATETIME_NOT_NULL
  6960.             when #tmp_columns.DATA_TYPE in (@ODBC_NTEXT, @ODBC_TEXT) then @ODS_TEXT
  6961.             when #tmp_columns.DATA_TYPE = @ODBC_BIT then @ODS_BIT
  6962.             when #tmp_columns.DATA_TYPE = @ODBC_DECIMAL then  @ODS_DECIMAL    
  6963.             when #tmp_columns.DATA_TYPE = @ODBC_NUMERIC then  @ODS_NUMERIC
  6964.             when #tmp_columns.DATA_TYPE = @ODBC_GUID then @ODS_GUID
  6965.             when #tmp_columns.DATA_TYPE = @ODBC_IMAGE then  @ODS_IMAGE
  6966.             when #tmp_columns.DATA_TYPE = @ODBC_TIMESTAMP
  6967.                 and (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISROWVER) = @DBCOLUMNFLAGS_ISROWVER AND
  6968.                     (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH) = @DBCOLUMNFLAGS_ISFIXEDLENGTH and
  6969.                     BUFFER_LENGTH = 8
  6970.                 then @ODS_TIMESTAMP
  6971.             when #tmp_columns.DATA_TYPE = @ODBC_BINARY and #tmp_columns.NULLABLE =    1
  6972.                 and (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH)
  6973.                 then  @ODS_BINARY_NULL
  6974.             when #tmp_columns.DATA_TYPE = @ODBC_BINARY and #tmp_columns.NULLABLE =    0
  6975.                 and (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH)
  6976.                 then @ODS_BINARY_NOT_NULL
  6977.             when #tmp_columns.DATA_TYPE = @ODBC_VARBINARY
  6978.                 then @ODS_VARBINARY    
  6979.             when #tmp_columns.DATA_TYPE in (@ODBC_CHAR, @ODBC_NCHAR) and #tmp_columns.NULLABLE = 1
  6980.                 then @ODS_CHAR_NULL
  6981.             when #tmp_columns.DATA_TYPE in (@ODBC_CHAR, @ODBC_NCHAR) and #tmp_columns.NULLABLE = 0
  6982.                 then @ODS_CHAR_NOT_NULL
  6983.             when #tmp_columns.DATA_TYPE in (@ODBC_VARCHAR, @ODBC_NVARCHAR) then @ODS_VARCHAR
  6984.             else null
  6985.         end
  6986.         from master.dbo.spt_datatype_info spt_dt
  6987.         where spt_dt.DATA_TYPE = #tmp_columns.DATA_TYPE
  6988.             AND (spt_dt.ODBCVer is null or spt_dt.ODBCVer = @ODBCVer)
  6989.             and isnull(spt_dt.AUTO_INCREMENT,0) = 0
  6990.             and not spt_dt.TYPE_NAME = 'smalldatetime'
  6991.  
  6992.     select
  6993.             TABLE_CAT,            TABLE_SCHEM,        TABLE_NAME,
  6994.             COLUMN_NAME,        DATA_TYPE,            TYPE_NAME,
  6995.             COLUMN_SIZE,        BUFFER_LENGTH,        DECIMAL_DIGITS,
  6996.             NUM_PREC_RADIX,        NULLABLE,            REMARKS,
  6997.             COLUMN_DEF,            SQL_DATA_TYPE,        SQL_DATETIME_SUB,
  6998.             CHAR_OCTET_LENGTH,    ORDINAL_POSITION,    IS_NULLABLE,
  6999.             SS_DATA_TYPE
  7000.         from #tmp_columns
  7001.         order by TABLE_CAT, TABLE_SCHEM, TABLE_NAME, ORDINAL_POSITION
  7002.  
  7003.         drop table #tmp_columns
  7004.  
  7005. go
  7006. grant execute on sp_columns_ex to public
  7007. go
  7008.  
  7009.  
  7010. if object_id('sp_table_privileges_ex', 'P') is not null
  7011.     drop proc sp_table_privileges_ex
  7012. go
  7013. raiserror(15339,-1,-1,'sp_table_privileges_ex')
  7014. go
  7015. create procedure sp_table_privileges_ex(
  7016.     @table_server        sysname,
  7017.     @table_name            sysname = null,
  7018.     @table_schema        sysname = null,
  7019.     @table_catalog        sysname = null)
  7020. as
  7021.     select
  7022.         TABLE_CAT = TABLE_CATALOG,
  7023.         TABLE_SCHEM = TABLE_SCHEMA,
  7024.         TABLE_NAME = TABLE_NAME,
  7025.         GRANTOR = GRANTOR,
  7026.         GRANTEE = GRANTEE,
  7027.         PRIVILEGE = PRIVILEGE_TYPE,
  7028.         IS_GRANTABLE = 
  7029.         case IS_GRANTABLE
  7030.             when 1 then 'YES'
  7031.             when 0 then 'NO'
  7032.             else null
  7033.         end
  7034.     from master.dbo.SYSREMOTE_TABLE_PRIVILEGES <
  7035.                 @table_server,
  7036.                 @table_catalog,
  7037.                 NULL,
  7038.                 NULL >
  7039.     where (TABLE_SCHEMA like @table_schema
  7040.         or @table_schema is NULL or (TABLE_SCHEMA is NULL and @table_schema = N'%'))
  7041.         and (TABLE_NAME like @table_name
  7042.         or     @table_name is NULL)
  7043.     order by TABLE_CAT, TABLE_SCHEM, TABLE_NAME, PRIVILEGE
  7044. go
  7045. grant execute on sp_table_privileges_ex to public
  7046. go
  7047.  
  7048.  
  7049. if object_id('sp_column_privileges_ex', 'P') is not null
  7050.     drop proc sp_column_privileges_ex
  7051. go
  7052. raiserror(15339,-1,-1,'sp_column_privileges_ex')
  7053. go
  7054. create procedure sp_column_privileges_ex(
  7055.     @table_server        sysname,
  7056.     @table_name            sysname = null,
  7057.     @table_schema        sysname = null,
  7058.     @table_catalog        sysname = null,
  7059.     @column_name        sysname = null)
  7060. as
  7061.     select
  7062.         TABLE_CAT = TABLE_CATALOG,
  7063.         TABLE_SCHEM = TABLE_SCHEMA,
  7064.         TABLE_NAME = TABLE_NAME,
  7065.         COLUMN_NAME = COLUMN_NAME,
  7066.         GRANTOR = GRANTOR,
  7067.         GRANTEE = GRANTEE,
  7068.         PRIVILEGE = PRIVILEGE_TYPE,
  7069.         IS_GRANTABLE = 
  7070.         case IS_GRANTABLE
  7071.             when 1 then 'YES'
  7072.             when 0 then 'NO'
  7073.             else null
  7074.         end
  7075.     from master.dbo.SYSREMOTE_COLUMN_PRIVILEGES <
  7076.         @table_server,
  7077.         @table_catalog,
  7078.         @table_schema,
  7079.         @table_name,
  7080.         NULL >
  7081.     where (COLUMN_NAME like @column_name
  7082.         or @column_name is NULL)
  7083.     order by TABLE_CAT, TABLE_SCHEM, TABLE_NAME, COLUMN_NAME, PRIVILEGE
  7084. go
  7085. grant execute on sp_column_privileges_ex to public
  7086. go
  7087.  
  7088.  
  7089. if object_id('sp_indexes', 'P') is not null
  7090.     drop proc sp_indexes
  7091. go
  7092. raiserror(15339,-1,-1,'sp_indexes')
  7093. go
  7094. create procedure sp_indexes(
  7095.     @table_server        sysname,
  7096.     @table_name            sysname = null,
  7097.     @table_schema        sysname = null,
  7098.     @table_catalog        sysname = null,
  7099.     @index_name            sysname = null,
  7100.     @is_unique          bit = null)
  7101. as
  7102.     select
  7103.         TABLE_CAT = TABLE_CATALOG,
  7104.         TABLE_SCHEM = TABLE_SCHEMA,
  7105.         TABLE_NAME = TABLE_NAME,
  7106.         NON_UNIQUE = convert(smallint, 1 - [UNIQUE]),
  7107.         INDEX_QUALIFIER = TABLE_NAME,
  7108.         INDEX_NAME = INDEX_NAME,
  7109.         TYPE = 
  7110.         case [CLUSTERED]
  7111.             when 1 then 1
  7112.             else 3
  7113.         end,
  7114.         ORDINAL_POSITION = ORDINAL_POSITION,
  7115.         COLUMN_NAME = COLUMN_NAME,
  7116.         ASC_OR_DESC = 
  7117.         case [COLLATION]
  7118.             when 1 then 'A'
  7119.             when 2 then 'D'
  7120.             else null
  7121.         end,
  7122.         CARDINALITY = CARDINALITY,
  7123.         PAGES = 
  7124.         case [CLUSTERED]
  7125.             when 1 then PAGES
  7126.             else NULL
  7127.         end,
  7128.         FILTER_CONDITION = FILTER_CONDITION
  7129.     from master.dbo.SYSREMOTE_INDEXES <
  7130.                 @table_server,
  7131.                 @table_catalog,
  7132.                 @table_schema,
  7133.                 @index_name,
  7134.                 NULL,            /* TYPE (index type) */
  7135.                 @table_name >
  7136.     where @is_unique is null or @is_unique = [UNIQUE]
  7137.     order by NON_UNIQUE, TYPE, INDEX_QUALIFIER, INDEX_NAME, ORDINAL_POSITION
  7138. go
  7139. grant execute on sp_indexes to public
  7140. go
  7141.  
  7142.  
  7143. if object_id('sp_foreignkeys', 'P') is not null
  7144.     drop proc sp_foreignkeys
  7145. go
  7146. raiserror(15339,-1,-1,'sp_foreignkeys')
  7147. go
  7148. create procedure sp_foreignkeys(
  7149.     @table_server        sysname,
  7150.     @pktab_name         sysname = null,
  7151.     @pktab_schema       sysname = null,
  7152.     @pktab_catalog      sysname = null,
  7153.     @fktab_name         sysname = null,
  7154.     @fktab_schema       sysname = null,
  7155.     @fktab_catalog      sysname = null)
  7156. as
  7157.     select
  7158.         PKTABLE_CAT = PK_TABLE_CATALOG,
  7159.         PKTABLE_SCHEM = PK_TABLE_SCHEMA,
  7160.         PKTABLE_NAME = PK_TABLE_NAME,
  7161.         PKCOLUMN_NAME = PK_COLUMN_NAME,
  7162.         FKTABLE_CAT = FK_TABLE_CATALOG,
  7163.         FKTABLE_SCHEM = FK_TABLE_SCHEMA,
  7164.         FKTABLE_NAME = FK_TABLE_NAME,
  7165.         FKCOLUMN_NAME = FK_COLUMN_NAME,
  7166.         KEY_SEQ = ORDINAL,
  7167.         UPDATE_RULE = 
  7168.         case UPDATE_RULE
  7169.             when 'CASCADE' then 0
  7170.             when 'NO ACTION' then 1
  7171.             when 'SET NULL' then 2
  7172.             else null
  7173.         end,
  7174.         DELETE_RULE = 
  7175.         case DELETE_RULE
  7176.             when 'CASCADE' then 0
  7177.             when 'NO ACTION' then 1
  7178.             when 'SET NULL' then 2
  7179.             else null
  7180.         end,
  7181.         FK_NAME = convert(sysname, NULL),
  7182.         PK_NAME = convert(sysname, NULL),
  7183.         DEFERRABILITY = convert(smallint, null)
  7184.  
  7185.     from master.dbo.SYSREMOTE_FOREIGN_KEYS <
  7186.                 @table_server,
  7187.                 @pktab_catalog,
  7188.                 @pktab_schema,
  7189.                 @pktab_name,
  7190.                 @fktab_catalog,
  7191.                 @fktab_schema,
  7192.                 @fktab_name >
  7193.     order by FKTABLE_CAT, FKTABLE_SCHEM, FKTABLE_NAME, KEY_SEQ
  7194. go
  7195. grant execute on sp_foreignkeys to public
  7196. go
  7197.  
  7198.  
  7199. if object_id('sp_primarykeys', 'P') is not null
  7200.     drop proc sp_primarykeys
  7201. go
  7202. raiserror(15339,-1,-1,'sp_primarykeys')
  7203. go
  7204. create procedure sp_primarykeys(
  7205.     @table_server        sysname,
  7206.     @table_name         sysname = null,
  7207.     @table_schema       sysname = null,
  7208.     @table_catalog      sysname = null)
  7209. as
  7210.     select
  7211.         TABLE_CAT = TABLE_CATALOG,
  7212.         TABLE_SCHEM = TABLE_SCHEMA,
  7213.         TABLE_NAME = TABLE_NAME,
  7214.         COLUMN_NAME = COLUMN_NAME,
  7215.         KEY_SEQ = ORDINAL,
  7216.         PK_NAME = convert(sysname, NULL)
  7217.     from master.dbo.SYSREMOTE_PRIMARY_KEYS <
  7218.                 @table_server,
  7219.                 @table_catalog,
  7220.                 @table_schema,
  7221.                 @table_name >
  7222.     order by TABLE_CAT, TABLE_SCHEM, TABLE_NAME, KEY_SEQ
  7223. go
  7224. grant execute on sp_primarykeys to public
  7225. go
  7226.  
  7227. /*-------------- END CATALOG STORED PROCEDURES FOR OLEDB SOURCES ------------------*/
  7228.  
  7229.  
  7230. dump tran master with no_log
  7231. go
  7232.  
  7233. if (charindex('6.50', @@version) = 0 and
  7234.     charindex('7.00', @@version) = 0)
  7235. begin
  7236.     print ''
  7237.     print ''
  7238.     print 'Warning:'
  7239.     print 'you are installing the stored procedures '
  7240.     print 'on a pre 6.50 SQL Server.'
  7241.     print 'Ignore the following errors.'
  7242. end
  7243.  
  7244. print 'creating sp_ddopen'
  7245. go
  7246.  
  7247. /*    Procedure for pre-7.00 server */
  7248. create procedure sp_ddopen(
  7249.                @handle            int output,
  7250.                @procname        sysname,
  7251.                @scrollopt        int output,
  7252.                @ccopt            int output,
  7253.                @rows            int output,
  7254.                @p1                varchar(255) = null,
  7255.                @p2                varchar(255) = null,
  7256.                @p3                varchar(255) = null,
  7257.                @p4                varchar(255) = null,
  7258.                @p5                varchar(255) = null,
  7259.                @p6                varchar(255) = null,
  7260.                @p7                int = null,
  7261.                @ODBCVer         int = 2)
  7262. as
  7263.     set nocount on
  7264.     declare @ret int
  7265.  
  7266.     if @procname = 'sp_column_privileges'
  7267.     begin
  7268.         create table #spcolpriv (
  7269.             TABLE_QUALIFIER varchar(32) null,
  7270.             TABLE_OWNER varchar(32) null,
  7271.             TABLE_NAME varchar(32)    not null,
  7272.             COLUMN_NAME varchar(32) not null,
  7273.             GRANTOR varchar(32) null,
  7274.             GRANTEE varchar(32) not null,
  7275.             PRIVILEGE varchar(32) not null,
  7276.             IS_GRANTABLE varchar(3) null
  7277.             )
  7278.         insert into #spcolpriv exec sp_column_privileges @p1,@p2,@p3,@p4
  7279.         exec @ret = sp_cursoropen @handle output,
  7280.             'select * from #spcolpriv',
  7281.             @scrollopt output, @ccopt output, @rows output
  7282.         drop table #spcolpriv
  7283.     end
  7284.     else if @procname = 'sp_columns'
  7285.     begin
  7286.         create table #spcolumns (
  7287.             TABLE_QUALIFIER varchar(32) null,
  7288.             TABLE_OWNER varchar(32) null,
  7289.             TABLE_NAME varchar(32)    not null,
  7290.             COLUMN_NAME varchar(32) not null,
  7291.             DATA_TYPE smallint not null,
  7292.             TYPE_NAME varchar(32) not null,
  7293.             "PRECISION" int null,
  7294.             LENGTH int null,
  7295.             SCALE smallint null,
  7296.             RADIX smallint null,
  7297.             NULLABLE smallint not null,
  7298.             REMARKS varchar(254) null,
  7299.             COLUMN_DEF varchar(255) null,
  7300.             SQL_DATA_TYPE smallint not null,
  7301.             SQL_DATETIME_SUB smallint null,
  7302.             CHAR_OCTET_LENGTH int null,
  7303.             ORDINAL_POSITION int not null,
  7304.             IS_NULLABLE varchar(254) null,
  7305.             SS_DATA_TYPE tinyint null
  7306.             )
  7307.         insert into #spcolumns exec sp_columns @p1,@p2,@p3,@p4,@ODBCVer
  7308.         exec @ret = sp_cursoropen @handle output,
  7309.             'select * from #spcolumns',
  7310.             @scrollopt output, @ccopt output, @rows output
  7311.         drop table #spcolumns
  7312.     end
  7313.     else if @procname = 'sp_datatype_info'
  7314.     begin
  7315.         create table #spdatatypeinfo (
  7316.             TYPE_NAME            varchar(32)  not null,
  7317.             DATA_TYPE            smallint not null,
  7318.             "PRECISION"            int null,
  7319.             LITERAL_PREFIX        varchar(32)    null,
  7320.             LITERAL_SUFFIX        varchar(32)    null,
  7321.             CREATE_PARAMS        varchar(32)    null,
  7322.             NULLABLE            smallint   not null,
  7323.             CASE_SENSITIVE        smallint   not null,
  7324.             SEARCHABLE            smallint   not null,
  7325.             UNSIGNED_ATTRIBUTE    smallint   null,
  7326.             MONEY    smallint    not null,
  7327.             AUTO_INCREMENT        smallint    null,
  7328.             LOCAL_TYPE_NAME     varchar(32) null,
  7329.             MINIMUM_SCALE        smallint     null,
  7330.             MAXIMUM_SCALE        smallint   null,
  7331.             SQL_DATA_TYPE        smallint      not null,
  7332.             SQL_DATETIME_SUB    smallint   null,
  7333.             NUM_PREC_RADIX        int     null,
  7334.             INTERVAL_PRECISION    smallint    NULL,
  7335.             USERTYPE            smallint not null)
  7336.         insert into #spdatatypeinfo exec sp_datatype_info @p7,@ODBCVer
  7337.         exec @ret = sp_cursoropen @handle output,
  7338.             'select * from #spdatatypeinfo',
  7339.             @scrollopt output, @ccopt output, @rows output
  7340.         drop table #spdatatypeinfo
  7341.     end
  7342.     else if @procname = 'sp_fkeys'
  7343.     begin
  7344.         create table #spfkeys (
  7345.             PKTABLE_QUALIFIER varchar(32)     null,
  7346.             PKTABLE_OWNER varchar(32)    null,
  7347.             PKTABLE_NAME varchar(32)  not null,
  7348.             PKCOLUMN_NAME varchar(32)    not null,
  7349.             FKTABLE_QUALIFIER varchar(32)    null,
  7350.             FKTABLE_OWNER varchar(32)    null,
  7351.             FKTABLE_NAME varchar(32)  not null,
  7352.             FKCOLUMN_NAME varchar(32)    not null,
  7353.             KEY_SEQ smallint not null,
  7354.             UPDATE_RULE smallint null,
  7355.             DELETE_RULE smallint null,
  7356.             FK_NAME varchar(32) null,
  7357.             PK_NAME varchar(32) null,
  7358.             DEFERRABILITY smallint null
  7359.             )
  7360.         insert into #spfkeys exec sp_fkeys @p1,@p2,@p3,@p4,@p5,@p6
  7361.         exec @ret = sp_cursoropen @handle output,
  7362.             'select * from #spfkeys',
  7363.             @scrollopt output, @ccopt output, @rows output
  7364.         drop table #spfkeys
  7365.     end
  7366.     else if @procname = 'sp_pkeys'
  7367.     begin
  7368.         create table #sppkeys (
  7369.             TABLE_QUALIFIER varchar(32)   null,
  7370.             TABLE_OWNER varchar(32)   null,
  7371.             TABLE_NAME varchar(32)    not null,
  7372.             COLUMN_NAME varchar(32)  not null,
  7373.             KEY_SEQ smallint not null,
  7374.             PK_NAME varchar(32) null
  7375.             )
  7376.         insert into #sppkeys exec sp_pkeys @p1,@p2,@p3
  7377.         exec @ret = sp_cursoropen @handle output,
  7378.             'select * from #sppkeys',
  7379.             @scrollopt output, @ccopt output, @rows output
  7380.         drop table #sppkeys
  7381.     end
  7382.     else if @procname = 'sp_special_columns'
  7383.     begin
  7384.         create table #spspeccol (
  7385.             SCOPE smallint null,
  7386.             COLUMN_NAME varchar(32) not null,
  7387.             DATA_TYPE smallint not null,
  7388.             TYPE_NAME varchar(32) not null,
  7389.             "PRECISION" int null,
  7390.             LENGTH int null,
  7391.             SCALE smallint null,
  7392.             PSEUDO_COLUMN smallint null
  7393.             )
  7394.         insert into #spspeccol exec sp_special_columns @p1,@p2,@p3,@p4,@p5,@p6,@ODBCVer
  7395.         exec @ret = sp_cursoropen @handle output,
  7396.             'select * from #spspeccol',
  7397.             @scrollopt output, @ccopt output, @rows output
  7398.         drop table #spspeccol
  7399.     end
  7400.     else if @procname = 'sp_sproc_columns'
  7401.     begin
  7402.         create table #spproccol (
  7403.             PROCEDURE_QUALIFIER varchar(32)  null,
  7404.             PROCEDURE_OWNER varchar(32)  null,
  7405.             PROCEDURE_NAME varchar(32)    not null,
  7406.             COLUMN_NAME varchar(32) not null,
  7407.             COLUMN_TYPE smallint not null,
  7408.             DATA_TYPE smallint not null,
  7409.             TYPE_NAME varchar(32) not null,
  7410.             "PRECISION" int null,
  7411.             LENGTH int null,
  7412.             SCALE smallint null,
  7413.             RADIX smallint null,
  7414.             NULLABLE smallint not null,
  7415.             REMARKS varchar(254) null,
  7416.             COLUMN_DEF varchar(255) null,
  7417.             SQL_DATA_TYPE smallint not null,
  7418.             SQL_DATETIME_SUB smallint null,
  7419.             CHAR_OCTET_LENGTH int null,
  7420.             ORDINAL_POSITION int not null,
  7421.             IS_NULLABLE varchar(254) null,
  7422.             SS_DATA_TYPE tinyint null
  7423.             )
  7424.         insert into #spproccol exec sp_sproc_columns @p1,@p2,@p3,@p4,@ODBCVer
  7425.         exec @ret = sp_cursoropen @handle output,
  7426.             'select * from #spproccol',
  7427.             @scrollopt output, @ccopt output, @rows output
  7428.         drop table #spproccol
  7429.     end
  7430.     else if @procname = 'sp_statistics'
  7431.     begin
  7432.         create table #spstatistics (
  7433.             TABLE_QUALIFIER varchar(32)   null,
  7434.             TABLE_OWNER varchar(32)   null,
  7435.             TABLE_NAME varchar(32)    not null,
  7436.             NON_UNIQUE smallint null,
  7437.             INDEX_QUALIFIER varchar(32) null,
  7438.             INDEX_NAME varchar(32)    null,
  7439.             TYPE smallint not null,
  7440.             SEQ_IN_INDEX smallint null,
  7441.             COLUMN_NAME varchar(32) null,
  7442.             COLLATION char(1) null,
  7443.             CARDINALITY int null,
  7444.             PAGES int null,
  7445.             FILTER_CONDITION varchar(128) null
  7446.             )
  7447.         insert into #spstatistics exec sp_statistics @p1,@p2,@p3,@p4,@p5,@p6
  7448.         exec @ret = sp_cursoropen @handle output,
  7449.             'select * from #spstatistics',
  7450.             @scrollopt output, @ccopt output, @rows output
  7451.         drop table #spstatistics
  7452.     end
  7453.     else if @procname = 'sp_stored_procedures'
  7454.     begin
  7455.         create table #spprocedures (
  7456.             PROCEDURE_QUALIFIER varchar(32)  null,
  7457.             PROCEDURE_OWNER varchar(32)  null,
  7458.             PROCEDURE_NAME varchar(32)    not null,
  7459.             NUM_INPUT_PARAMS int null,
  7460.             NUM_OUTPUT_PARAMS int null,
  7461.             NUM_RESULT_SETS int null,
  7462.             REMARKS varchar(254) null,
  7463.             PROCEDURE_TYPE smallint null
  7464.             )
  7465.         insert into #spprocedures exec sp_stored_procedures @p1,@p2,@p3
  7466.         exec @ret = sp_cursoropen @handle output,
  7467.             'select * from #spprocedures',
  7468.             @scrollopt output, @ccopt output, @rows output
  7469.         drop table #spprocedures
  7470.     end
  7471.     else if @procname = 'sp_table_privileges'
  7472.     begin
  7473.         create table #sptabpriv (
  7474.             TABLE_QUALIFIER varchar(32) null,
  7475.             TABLE_OWNER varchar(32) null,
  7476.             TABLE_NAME varchar(32)    not null,
  7477.             GRANTOR varchar(32) null,
  7478.             GRANTEE varchar(32) not null,
  7479.             PRIVILEGE varchar(32) not null,
  7480.             IS_GRANTABLE varchar(3) null
  7481.             )
  7482.         insert into #sptabpriv exec sp_table_privileges @p1,@p2,@p3
  7483.         exec @ret = sp_cursoropen @handle output,
  7484.             'select * from #sptabpriv',
  7485.             @scrollopt output, @ccopt output, @rows output
  7486.         drop table #sptabpriv
  7487.     end
  7488.     else if @procname = 'sp_tables'
  7489.     begin
  7490.         create table #sptables (
  7491.             TABLE_QUALIFIER varchar(32) null,
  7492.             TABLE_OWNER varchar(32) null,
  7493.             TABLE_NAME varchar(32)    null,
  7494.             TABLE_TYPE     varchar(32) null,
  7495.             REMARKS varchar(254) null)
  7496.         insert into #sptables exec sp_tables @p1,@p2,@p3,@p4
  7497.         exec @ret = sp_cursoropen @handle output,
  7498.             'select * from #sptables',
  7499.             @scrollopt output, @ccopt output, @rows output
  7500.         drop table #sptables
  7501.     end
  7502.     select @ret = isnull(@ret,0)
  7503.     return isnull(@ret,0)
  7504. go
  7505.  
  7506. if (charindex('7.00', @@version) > 0)
  7507.     drop procedure sp_ddopen
  7508. else
  7509. begin
  7510.     print ''
  7511.     print ''
  7512.     print 'Warning:'
  7513.     print 'you are installing the stored procedures '
  7514.     print 'on a pre 7.0 SQL Server.'
  7515.     print 'Ignore the following errors.'
  7516. end
  7517. go
  7518.  
  7519. /*    Procedure for 7.00 server */
  7520. create procedure sp_ddopen; 1(
  7521.                @handle            int output,
  7522.                @procname        sysname,
  7523.                @scrollopt        int output,
  7524.                @ccopt            int output,
  7525.                @rows            int output,
  7526.                @p1                nvarchar(774) = null,
  7527.                @p2                nvarchar(774) = null,
  7528.                @p3                nvarchar(774) = null,
  7529.                @p4                nvarchar(774) = null,
  7530.                @p5                nvarchar(774) = null,
  7531.                @p6                nvarchar(774) = null,
  7532.                @p7                int = null,
  7533.                @ODBCVer         int = 2)
  7534. as
  7535.     set nocount on
  7536.     declare @ret int
  7537.  
  7538.     if @procname = 'sp_column_privileges'
  7539.     begin
  7540.         exec @ret = sp_ddopen;2 @handle output, 
  7541.                             @scrollopt output, 
  7542.                             @ccopt output, 
  7543.                             @rows output, 
  7544.                             @p1,
  7545.                             @p2,
  7546.                             @p3,
  7547.                             @p4
  7548.     end
  7549.     else if @procname = 'sp_columns' or @procname = 'sp_columns_ex'
  7550.     begin
  7551.         exec @ret = sp_ddopen;3 @handle output,
  7552.                             @procname,
  7553.                             @scrollopt output, 
  7554.                             @ccopt output, 
  7555.                             @rows output, 
  7556.                             @p1,
  7557.                             @p2,
  7558.                             @p3,
  7559.                             @p4,
  7560.                             @p5,
  7561.                             @ODBCVer
  7562.     end
  7563.     else if @procname = 'sp_datatype_info'
  7564.     begin
  7565.         exec @ret = sp_ddopen;4 @handle output,
  7566.                             @scrollopt output, 
  7567.                             @ccopt output, 
  7568.                             @rows output, 
  7569.                             @p7,
  7570.                             @ODBCVer
  7571.     end
  7572.     else if @procname = 'sp_fkeys'
  7573.     begin
  7574.         exec @ret = sp_ddopen;5 @handle output, 
  7575.                             @scrollopt output, 
  7576.                             @ccopt output, 
  7577.                             @rows output, 
  7578.                             @p1,
  7579.                             @p2,
  7580.                             @p3,
  7581.                             @p4,
  7582.                             @p5,
  7583.                             @p6
  7584.     end
  7585.     else if @procname = 'sp_pkeys'
  7586.     begin
  7587.         exec @ret = sp_ddopen;6 @handle output, 
  7588.                             @scrollopt output, 
  7589.                             @ccopt output, 
  7590.                             @rows output, 
  7591.                             @p1,
  7592.                             @p2,
  7593.                             @p3
  7594.     end
  7595.     else if @procname = 'sp_special_columns'
  7596.     begin
  7597.         exec @ret = sp_ddopen;7 @handle output, 
  7598.                             @scrollopt output, 
  7599.                             @ccopt output, 
  7600.                             @rows output, 
  7601.                             @p1,
  7602.                             @p2,
  7603.                             @p3,
  7604.                             @p4,
  7605.                             @p5,
  7606.                             @p6,
  7607.                             @ODBCVer
  7608.     end
  7609.     else if @procname = 'sp_sproc_columns'
  7610.     begin
  7611.         exec @ret = sp_ddopen;8 @handle output, 
  7612.                             @scrollopt output, 
  7613.                             @ccopt output, 
  7614.                             @rows output, 
  7615.                             @p1,
  7616.                             @p2,
  7617.                             @p3,
  7618.                             @p4,
  7619.                             @ODBCVer
  7620.     end
  7621.     else if @procname = 'sp_statistics'
  7622.     begin
  7623.         exec @ret = sp_ddopen;9 @handle output, 
  7624.                             @scrollopt output, 
  7625.                             @ccopt output, 
  7626.                             @rows output, 
  7627.                             @p1,
  7628.                             @p2,
  7629.                             @p3,
  7630.                             @p4,
  7631.                             @p5,
  7632.                             @p6
  7633.     end
  7634.     else if @procname = 'sp_stored_procedures'
  7635.     begin
  7636.         exec @ret = sp_ddopen;10 @handle output, 
  7637.                              @scrollopt output, 
  7638.                              @ccopt output, 
  7639.                              @rows output, 
  7640.                              @p1,
  7641.                              @p2,
  7642.                              @p3
  7643.     end
  7644.     else if @procname = 'sp_table_privileges'
  7645.     begin
  7646.         exec @ret = sp_ddopen;11 @handle output, 
  7647.                              @scrollopt output, 
  7648.                              @ccopt output, 
  7649.                              @rows output, 
  7650.                              @p1,
  7651.                              @p2,
  7652.                              @p3
  7653.     end
  7654.     else if @procname = 'sp_tables' or @procname = 'sp_tables_ex'
  7655.     begin
  7656.         exec @ret = sp_ddopen;12 @handle output,
  7657.                             @procname,
  7658.                             @scrollopt output, 
  7659.                             @ccopt output, 
  7660.                             @rows output, 
  7661.                             @p1,
  7662.                             @p2,
  7663.                             @p3,
  7664.                             @p4,
  7665.                             @p5
  7666.     end
  7667.     else
  7668.         print 'Unknown dd_open procedure'
  7669.     select @ret = isnull(@ret,0)
  7670.     return isnull(@ret,0)
  7671. go
  7672.  
  7673.  
  7674. if (charindex('7.00', @@version) = 0)
  7675. begin
  7676.     print ''
  7677.     print ''
  7678.     print 'Warning:'
  7679.     print 'you are installing the stored procedures '
  7680.     print 'on a pre 7.0 SQL Server.'
  7681.     print 'Ignore the following errors.'
  7682. end
  7683. go
  7684.  
  7685. create procedure sp_ddopen; 2(
  7686.                @handle            int output,
  7687.                @scrollopt        int output,
  7688.                @ccopt            int output,
  7689.                @rows            int output,
  7690.                @p1                nvarchar(774),
  7691.                @p2                nvarchar(774),
  7692.                @p3                nvarchar(774),
  7693.                @p4                nvarchar(774))
  7694. as
  7695.     set nocount on
  7696.     declare @ret int
  7697.  
  7698.     create table #spcolpriv (
  7699.         TABLE_QUALIFIER sysname null,
  7700.         TABLE_OWNER sysname null,
  7701.         TABLE_NAME sysname not null,
  7702.         COLUMN_NAME sysname not null,
  7703.         GRANTOR sysname null,
  7704.         GRANTEE sysname not null,
  7705.         PRIVILEGE varchar(32) not null,
  7706.         IS_GRANTABLE varchar(3) null
  7707.         )
  7708.     insert into #spcolpriv exec sp_column_privileges @p1,@p2,@p3,@p4
  7709.     exec @ret = sp_cursoropen @handle output,
  7710.         'select * from #spcolpriv',
  7711.         @scrollopt output, @ccopt output, @rows output
  7712.     drop table #spcolpriv
  7713.     return @ret
  7714. go
  7715.  
  7716.  
  7717. if (charindex('7.00', @@version) = 0)
  7718. begin
  7719.     print ''
  7720.     print ''
  7721.     print 'Warning:'
  7722.     print 'you are installing the stored procedures '
  7723.     print 'on a pre 7.0 SQL Server.'
  7724.     print 'Ignore the following errors.'
  7725. end
  7726. go
  7727.  
  7728. create procedure sp_ddopen; 3(
  7729.                @handle            int output,
  7730.                @procname        sysname,
  7731.                @scrollopt        int output,
  7732.                @ccopt            int output,
  7733.                @rows            int output,
  7734.                @p1                nvarchar(774),
  7735.                @p2                nvarchar(774),
  7736.                @p3                nvarchar(774),
  7737.                @p4                nvarchar(774),
  7738.                @p5                nvarchar(774),
  7739.                @ODBCVer         int)
  7740. as
  7741.     set nocount on
  7742.     declare @ret int
  7743.  
  7744.     create table #spcolumns (
  7745.         TABLE_QUALIFIER sysname null,
  7746.         TABLE_OWNER sysname null,
  7747.         TABLE_NAME sysname not null,
  7748.         COLUMN_NAME sysname not null,
  7749.         DATA_TYPE smallint not null,
  7750.         TYPE_NAME sysname not null,
  7751.         "PRECISION" int null,
  7752.         LENGTH int null,
  7753.         SCALE smallint null,
  7754.         RADIX smallint null,
  7755.         NULLABLE smallint not null,
  7756.         REMARKS varchar(254) null,
  7757.         COLUMN_DEF nvarchar(3000) null,
  7758.         SQL_DATA_TYPE smallint not null,
  7759.         SQL_DATETIME_SUB smallint null,
  7760.         CHAR_OCTET_LENGTH int null,
  7761.         ORDINAL_POSITION int not null,
  7762.         IS_NULLABLE varchar(254) null,
  7763.         SS_DATA_TYPE tinyint null
  7764.         )
  7765.  
  7766.     if @procname = 'sp_columns'
  7767.     begin
  7768.         insert into #spcolumns exec sp_columns @p1,@p2,@p3,@p4,@ODBCVer
  7769.     end
  7770.     else
  7771.     begin
  7772.         insert into #spcolumns exec sp_columns_ex @p1,@p2,@p3,@p4,@p5,@ODBCVer
  7773.     end
  7774.     exec @ret = sp_cursoropen @handle output,
  7775.         'select * from #spcolumns',
  7776.         @scrollopt output, @ccopt output, @rows output
  7777.     drop table #spcolumns
  7778.     return @ret
  7779. go
  7780.  
  7781.  
  7782.  
  7783. if (charindex('7.00', @@version) = 0)
  7784. begin
  7785.     print ''
  7786.     print ''
  7787.     print 'Warning:'
  7788.     print 'you are installing the stored procedures '
  7789.     print 'on a pre 7.0 SQL Server.'
  7790.     print 'Ignore the following errors.'
  7791. end
  7792. go
  7793.  
  7794. create procedure sp_ddopen; 4(
  7795.                @handle            int output,
  7796.                @scrollopt        int output,
  7797.                @ccopt            int output,
  7798.                @rows            int output,
  7799.                @p7                int,
  7800.                @ODBCVer         int)
  7801. as
  7802.     set nocount on
  7803.     declare @ret int
  7804.  
  7805.     create table #spdatatypeinfo (
  7806.         TYPE_NAME            sysname  not null,
  7807.         DATA_TYPE            smallint not null,
  7808.         "PRECISION"            int null,
  7809.         LITERAL_PREFIX        varchar(32)    null,
  7810.         LITERAL_SUFFIX        varchar(32)    null,
  7811.         CREATE_PARAMS        varchar(32)    null,
  7812.         NULLABLE            smallint   not null,
  7813.         CASE_SENSITIVE        smallint   not null,
  7814.         SEARCHABLE            smallint   not null,
  7815.         UNSIGNED_ATTRIBUTE    smallint   null,
  7816.         MONEY    smallint    not null,
  7817.         AUTO_INCREMENT        smallint    null,
  7818.         LOCAL_TYPE_NAME     sysname null,
  7819.         MINIMUM_SCALE        smallint     null,
  7820.         MAXIMUM_SCALE        smallint   null,
  7821.         SQL_DATA_TYPE        smallint      not null,
  7822.         SQL_DATETIME_SUB    smallint   null,
  7823.         NUM_PREC_RADIX        int     null,
  7824.         INTERVAL_PRECISION    smallint    NULL,
  7825.         USERTYPE            smallint not null)
  7826.  
  7827.     insert into #spdatatypeinfo exec sp_datatype_info @p7,@ODBCVer
  7828.     exec @ret = sp_cursoropen @handle output,
  7829.         'select * from #spdatatypeinfo',
  7830.         @scrollopt output, @ccopt output, @rows output
  7831.     drop table #spdatatypeinfo
  7832.     return @ret
  7833. go
  7834.  
  7835.  
  7836.  
  7837. if (charindex('7.00', @@version) = 0)
  7838. begin
  7839.     print ''
  7840.     print ''
  7841.     print 'Warning:'
  7842.     print 'you are installing the stored procedures '
  7843.     print 'on a pre 7.0 SQL Server.'
  7844.     print 'Ignore the following errors.'
  7845. end
  7846. go
  7847.  
  7848. create procedure sp_ddopen; 5(
  7849.                @handle            int output,
  7850.                @scrollopt        int output,
  7851.                @ccopt            int output,
  7852.                @rows            int output,
  7853.                @p1                nvarchar(774),
  7854.                @p2                nvarchar(774),
  7855.                @p3                nvarchar(774),
  7856.                @p4                nvarchar(774),
  7857.                @p5                nvarchar(774),
  7858.                @p6                nvarchar(774))
  7859. as
  7860.     set nocount on
  7861.     declare @ret int
  7862.  
  7863.     create table #spfkeys (
  7864.         PKTABLE_QUALIFIER sysname     null,
  7865.         PKTABLE_OWNER sysname    null,
  7866.         PKTABLE_NAME sysname  not null,
  7867.         PKCOLUMN_NAME sysname  not null,
  7868.         FKTABLE_QUALIFIER sysname    null,
  7869.         FKTABLE_OWNER sysname    null,
  7870.         FKTABLE_NAME sysname  not null,
  7871.         FKCOLUMN_NAME sysname  not null,
  7872.         KEY_SEQ smallint not null,
  7873.         UPDATE_RULE smallint null,
  7874.         DELETE_RULE smallint null,
  7875.         FK_NAME sysname null,
  7876.         PK_NAME sysname null,
  7877.         DEFERRABILITY smallint null
  7878.         )
  7879.     insert into #spfkeys exec sp_fkeys @p1,@p2,@p3,@p4,@p5,@p6
  7880.     exec @ret = sp_cursoropen @handle output,
  7881.         'select * from #spfkeys',
  7882.         @scrollopt output, @ccopt output, @rows output
  7883.     drop table #spfkeys
  7884.     return @ret
  7885. go
  7886.  
  7887.  
  7888.  
  7889. if (charindex('7.00', @@version) = 0)
  7890. begin
  7891.     print ''
  7892.     print ''
  7893.     print 'Warning:'
  7894.     print 'you are installing the stored procedures '
  7895.     print 'on a pre 7.0 SQL Server.'
  7896.     print 'Ignore the following errors.'
  7897. end
  7898. go
  7899.  
  7900. create procedure sp_ddopen; 6(
  7901.                @handle            int output,
  7902.                @scrollopt        int output,
  7903.                @ccopt            int output,
  7904.                @rows            int output,
  7905.                @p1                nvarchar(774),
  7906.                @p2                nvarchar(774),
  7907.                @p3                nvarchar(774))
  7908. as
  7909.     set nocount on
  7910.     declare @ret int
  7911.  
  7912.     create table #sppkeys (
  7913.         TABLE_QUALIFIER sysname   null,
  7914.         TABLE_OWNER sysname   null,
  7915.         TABLE_NAME sysname    not null,
  7916.         COLUMN_NAME sysname  not null,
  7917.         KEY_SEQ smallint not null,
  7918.         PK_NAME sysname null
  7919.         )
  7920.     insert into #sppkeys exec sp_pkeys @p1,@p2,@p3
  7921.     exec @ret = sp_cursoropen @handle output,
  7922.         'select * from #sppkeys',
  7923.         @scrollopt output, @ccopt output, @rows output
  7924.     drop table #sppkeys
  7925.     return @ret
  7926. go
  7927.  
  7928.  
  7929.  
  7930. if (charindex('7.00', @@version) = 0)
  7931. begin
  7932.     print ''
  7933.     print ''
  7934.     print 'Warning:'
  7935.     print 'you are installing the stored procedures '
  7936.     print 'on a pre 7.0 SQL Server.'
  7937.     print 'Ignore the following errors.'
  7938. end
  7939. go
  7940.  
  7941. create procedure sp_ddopen; 7(
  7942.                @handle            int output,
  7943.                @scrollopt        int output,
  7944.                @ccopt            int output,
  7945.                @rows            int output,
  7946.                @p1                nvarchar(774),
  7947.                @p2                nvarchar(774),
  7948.                @p3                nvarchar(774),
  7949.                @p4                nvarchar(774),
  7950.                @p5                nvarchar(774),
  7951.                @p6                nvarchar(774),
  7952.                @ODBCVer         int)
  7953. as
  7954.     set nocount on
  7955.     declare @ret int
  7956.  
  7957.     create table #spspeccol (
  7958.         SCOPE smallint null,
  7959.         COLUMN_NAME sysname not null,
  7960.         DATA_TYPE smallint not null,
  7961.         TYPE_NAME sysname not null,
  7962.         "PRECISION" int null,
  7963.         LENGTH int null,
  7964.         SCALE smallint null,
  7965.         PSEUDO_COLUMN smallint null
  7966.         )
  7967.     insert into #spspeccol exec sp_special_columns @p1,@p2,@p3,@p4,@p5,@p6,@ODBCVer
  7968.     exec @ret = sp_cursoropen @handle output,
  7969.         'select * from #spspeccol',
  7970.         @scrollopt output, @ccopt output, @rows output
  7971.     drop table #spspeccol
  7972.     return @ret
  7973. go
  7974.  
  7975.  
  7976.  
  7977. if (charindex('7.00', @@version) = 0)
  7978. begin
  7979.     print ''
  7980.     print ''
  7981.     print 'Warning:'
  7982.     print 'you are installing the stored procedures '
  7983.     print 'on a pre 7.0 SQL Server.'
  7984.     print 'Ignore the following errors.'
  7985. end
  7986. go
  7987.  
  7988. create procedure sp_ddopen; 8(
  7989.                @handle            int output,
  7990.                @scrollopt        int output,
  7991.                @ccopt            int output,
  7992.                @rows            int output,
  7993.                @p1                nvarchar(774),
  7994.                @p2                nvarchar(774),
  7995.                @p3                nvarchar(774),
  7996.                @p4                nvarchar(774),
  7997.                @ODBCVer         int)
  7998. as
  7999.     set nocount on
  8000.     declare @ret int
  8001.  
  8002.     create table #spproccol (
  8003.         PROCEDURE_QUALIFIER sysname  null,
  8004.         PROCEDURE_OWNER sysname  null,
  8005.         PROCEDURE_NAME sysname not null,
  8006.         COLUMN_NAME sysname not null,
  8007.         COLUMN_TYPE smallint not null,
  8008.         DATA_TYPE smallint not null,
  8009.         TYPE_NAME sysname not null,
  8010.         "PRECISION" int null,
  8011.         LENGTH int null,
  8012.         SCALE smallint null,
  8013.         RADIX smallint null,
  8014.         NULLABLE smallint not null,
  8015.         REMARKS varchar(254) null,
  8016.         COLUMN_DEF nvarchar(3000) null,
  8017.         SQL_DATA_TYPE smallint not null,
  8018.         SQL_DATETIME_SUB smallint null,
  8019.         CHAR_OCTET_LENGTH int null,
  8020.         ORDINAL_POSITION int not null,
  8021.         IS_NULLABLE varchar(254) null,
  8022.         SS_DATA_TYPE tinyint null
  8023.         )
  8024.     insert into #spproccol exec sp_sproc_columns @p1,@p2,@p3,@p4,@ODBCVer
  8025.     exec @ret = sp_cursoropen @handle output,
  8026.         'select * from #spproccol',
  8027.         @scrollopt output, @ccopt output, @rows output
  8028.     drop table #spproccol
  8029.     return @ret
  8030. go
  8031.  
  8032.  
  8033.  
  8034. if (charindex('7.00', @@version) = 0)
  8035. begin
  8036.     print ''
  8037.     print ''
  8038.     print 'Warning:'
  8039.     print 'you are installing the stored procedures '
  8040.     print 'on a pre 7.0 SQL Server.'
  8041.     print 'Ignore the following errors.'
  8042. end
  8043. go
  8044.  
  8045. create procedure sp_ddopen; 9(
  8046.                @handle            int output,
  8047.                @scrollopt        int output,
  8048.                @ccopt            int output,
  8049.                @rows            int output,
  8050.                @p1                nvarchar(774),
  8051.                @p2                nvarchar(774),
  8052.                @p3                nvarchar(774),
  8053.                @p4                nvarchar(774),
  8054.                @p5                nvarchar(774),
  8055.                @p6                nvarchar(774))
  8056. as
  8057.     set nocount on
  8058.     declare @ret int
  8059.  
  8060.     create table #spstatistics (
  8061.         TABLE_QUALIFIER sysname   null,
  8062.         TABLE_OWNER sysname   null,
  8063.         TABLE_NAME sysname    not null,
  8064.         NON_UNIQUE smallint null,
  8065.         INDEX_QUALIFIER sysname null,
  8066.         INDEX_NAME sysname null,
  8067.         TYPE smallint not null,
  8068.         SEQ_IN_INDEX smallint null,
  8069.         COLUMN_NAME sysname null,
  8070.         COLLATION char(1) null,
  8071.         CARDINALITY int null,
  8072.         PAGES int null,
  8073.         FILTER_CONDITION varchar(128) null
  8074.         )
  8075.     insert into #spstatistics exec sp_statistics @p1,@p2,@p3,@p4,@p5,@p6
  8076.     exec @ret = sp_cursoropen @handle output,
  8077.         'select * from #spstatistics',
  8078.         @scrollopt output, @ccopt output, @rows output
  8079.     drop table #spstatistics
  8080.     return @ret
  8081. go
  8082.                
  8083.  
  8084.  
  8085. if (charindex('7.00', @@version) = 0)
  8086. begin
  8087.     print ''
  8088.     print ''
  8089.     print 'Warning:'
  8090.     print 'you are installing the stored procedures '
  8091.     print 'on a pre 7.0 SQL Server.'
  8092.     print 'Ignore the following errors.'
  8093. end
  8094. go
  8095.  
  8096. create procedure sp_ddopen; 10(
  8097.                @handle            int output,
  8098.                @scrollopt        int output,
  8099.                @ccopt            int output,
  8100.                @rows            int output,
  8101.                @p1                nvarchar(774),
  8102.                @p2                nvarchar(774),
  8103.                @p3                nvarchar(774))
  8104. as
  8105.     set nocount on
  8106.     declare @ret int
  8107.  
  8108.     create table #spprocedures (
  8109.         PROCEDURE_QUALIFIER sysname  null,
  8110.         PROCEDURE_OWNER sysname  null,
  8111.         PROCEDURE_NAME sysname not null,
  8112.         NUM_INPUT_PARAMS int null,
  8113.         NUM_OUTPUT_PARAMS int null,
  8114.         NUM_RESULT_SETS int null,
  8115.         REMARKS varchar(254) null,
  8116.         PROCEDURE_TYPE smallint null
  8117.         )
  8118.     insert into #spprocedures exec sp_stored_procedures @p1,@p2,@p3
  8119.     exec @ret = sp_cursoropen @handle output,
  8120.         'select * from #spprocedures',
  8121.         @scrollopt output, @ccopt output, @rows output
  8122.     drop table #spprocedures
  8123.     return @ret
  8124. go
  8125.  
  8126.  
  8127.  
  8128. if (charindex('7.00', @@version) = 0)
  8129. begin
  8130.     print ''
  8131.     print ''
  8132.     print 'Warning:'
  8133.     print 'you are installing the stored procedures '
  8134.     print 'on a pre 7.0 SQL Server.'
  8135.     print 'Ignore the following errors.'
  8136. end
  8137. go
  8138.  
  8139. create procedure sp_ddopen; 11(
  8140.                @handle            int output,
  8141.                @scrollopt        int output,
  8142.                @ccopt            int output,
  8143.                @rows            int output,
  8144.                @p1                nvarchar(774),
  8145.                @p2                nvarchar(774),
  8146.                @p3                nvarchar(774))
  8147. as
  8148.     set nocount on
  8149.     declare @ret int
  8150.  
  8151.     create table #sptabpriv (
  8152.         TABLE_QUALIFIER sysname null,
  8153.         TABLE_OWNER sysname null,
  8154.         TABLE_NAME sysname not null,
  8155.         GRANTOR sysname null,
  8156.         GRANTEE sysname not null,
  8157.         PRIVILEGE varchar(32) not null,
  8158.         IS_GRANTABLE varchar(3) null
  8159.         )
  8160.     insert into #sptabpriv exec sp_table_privileges @p1,@p2,@p3
  8161.     exec @ret = sp_cursoropen @handle output,
  8162.         'select * from #sptabpriv',
  8163.         @scrollopt output, @ccopt output, @rows output
  8164.     drop table #sptabpriv
  8165.     return @ret
  8166. go
  8167.  
  8168.  
  8169.  
  8170. if (charindex('7.00', @@version) = 0)
  8171. begin
  8172.     print ''
  8173.     print ''
  8174.     print 'Warning:'
  8175.     print 'you are installing the stored procedures '
  8176.     print 'on a pre 7.0 SQL Server.'
  8177.     print 'Ignore the following errors.'
  8178. end
  8179. go
  8180.  
  8181. create procedure sp_ddopen; 12(
  8182.                @handle            int output,
  8183.                @procname        sysname,
  8184.                @scrollopt        int output,
  8185.                @ccopt            int output,
  8186.                @rows            int output,
  8187.                @p1                nvarchar(774),
  8188.                @p2                nvarchar(774),
  8189.                @p3                nvarchar(774),
  8190.                @p4                nvarchar(774),
  8191.                @p5                nvarchar(774))
  8192. as
  8193.     set nocount on
  8194.     declare @ret int
  8195.  
  8196.     create table #sptables (
  8197.         TABLE_QUALIFIER sysname null,
  8198.         TABLE_OWNER sysname null,
  8199.         TABLE_NAME sysname null,
  8200.         TABLE_TYPE    varchar(32) null,
  8201.         REMARKS varchar(254) null)
  8202.     if @procname = 'sp_tables'
  8203.     begin
  8204.         insert into #sptables exec sp_tables @p1,@p2,@p3,@p4
  8205.     end
  8206.     else
  8207.     begin
  8208.         insert into #sptables exec sp_tables_ex @p1,@p2,@p3,@p4,@p5
  8209.     end
  8210.     exec @ret = sp_cursoropen @handle output,
  8211.         'select * from #sptables',
  8212.         @scrollopt output, @ccopt output, @rows output
  8213.     drop table #sptables
  8214.     return @ret
  8215. go
  8216.  
  8217. grant execute on sp_ddopen to public
  8218. go
  8219.  
  8220. print 'creating sp_tableswc'
  8221. go
  8222.  
  8223. if (charindex('7.00', @@version) = 0)
  8224. begin
  8225.     print ''
  8226.     print ''
  8227.     print 'Warning:'
  8228.     print 'you are installing the stored procedures '
  8229.     print 'on a pre 7.0 SQL Server.'
  8230.     print 'Ignore the following errors.'
  8231. end
  8232. go
  8233.  
  8234. /*    Procedure for 7.00 server */
  8235. create procedure sp_tableswc(
  8236.                @table_name        nvarchar(384)    = null,
  8237.                @table_owner     nvarchar(384)    = null,
  8238.                @table_qualifier sysname    = null,
  8239.                @table_type        varchar(100) = null)
  8240. as
  8241.     declare @databasename    sysname
  8242.     declare @qualprocname    nvarchar(141) /* 128 + '..sp_tables' */
  8243.  
  8244.     create table #sptables (
  8245.         TABLE_QUALIFIER sysname null,
  8246.         TABLE_OWNER sysname null,
  8247.         TABLE_NAME sysname null,
  8248.         TABLE_TYPE    varchar(32) null,
  8249.         REMARKS varchar(254) null)
  8250.  
  8251.     declare databases CURSOR FOR
  8252.         select name from master..sysdatabases
  8253.         where name like @table_qualifier and name <> 'model' and has_dbaccess(name)=1
  8254.         for read only
  8255.  
  8256.     open databases
  8257.     fetch next from databases into @databasename
  8258.     while (@@FETCH_STATUS <> -1)
  8259.     begin
  8260.         if (charindex('%', @databasename) = 0 and
  8261.             charindex('_', @databasename) = 0)
  8262.         begin    /* skip dbnames w/wildcard characters to prevent loop */
  8263.             select @qualprocname = @databasename + '..sp_tables'
  8264.             insert into #sptables exec @qualprocname
  8265.                 @table_name, @table_owner, @databasename, @table_type
  8266.         end
  8267.         fetch next from databases into @databasename
  8268.     end
  8269.     deallocate databases
  8270.     select * from #sptables
  8271.         order by 4, 1, 2, 3
  8272. go
  8273.  
  8274. grant execute on sp_tableswc to public
  8275. go
  8276.  
  8277. dump tran master with no_log
  8278. go
  8279.  
  8280. /*-------------------------------------------------------------------------*/
  8281. /*-------------- CATALOG STORED PROCEDURES FOR SQLOLEDB  ------------------*/
  8282. /*-------------------------------------------------------------------------*/
  8283.  
  8284. print ''
  8285. print 'creating spt_provider_types'
  8286. go
  8287. if (charindex('7.00', @@version) = 0)
  8288. begin /* Pre 7.00 Server */
  8289. create table spt_provider_types
  8290.     (
  8291.     ss_dtype        tinyint     not null,
  8292.     fixlen            int         null,        /* datatype len for variable, else null */
  8293.     type_name        sysname        not null,
  8294.     oledb_data_type        smallint    not null,
  8295.     best_match        bit        not null,
  8296.     is_nullable        tinyint        null,
  8297.     case_sensitive        bit        not null,
  8298.     fixed_prec_scale    bit        not null,
  8299.     is_long            bit        not null,
  8300.     auto_unique_value    tinyint        not null,
  8301.     data_precision        int         null,
  8302.     numeric_scale        smallint    null,        /* min scale if 6.0 */
  8303.     column_size        int         null,
  8304.     literal_prefix        varchar(32)    null,
  8305.     literal_suffix        varchar(32)     null,
  8306.     searchable        int        not null,
  8307.     unsigned_attribute    tinyint        null,
  8308.     local_type_name     sysname        null
  8309.     )
  8310. print ''
  8311. print ''
  8312. print 'Warning:'
  8313. print 'you are installing the stored procedures '
  8314. print 'on a pre 7.0 SQL Server.'
  8315. print 'Ignore the following errors.'
  8316. end
  8317. go
  8318. if (charindex('7.00', @@version) > 0)
  8319. begin /* 7.00 server*/
  8320. create table spt_provider_types
  8321.     (
  8322.     ss_dtype        tinyint     not null,
  8323.     fixlen            int         null,        /* datatype len for variable, else null */
  8324.     type_name        sysname        not null,
  8325.     oledb_data_type        smallint    not null,
  8326.     best_match        bit        not null,
  8327.     is_nullable        bit        null,
  8328.     case_sensitive        bit        not null,
  8329.     fixed_prec_scale    bit        not null,
  8330.     is_long            bit        not null,
  8331.     auto_unique_value    tinyint        not null,
  8332.     data_precision        int         null,
  8333.     numeric_scale        smallint    null,        /* min scale if 6.0 */
  8334.     column_size        int         null,
  8335.     literal_prefix        nvarchar(32)    null,
  8336.     literal_suffix        nvarchar(32)     null,
  8337.     searchable        int        not null,
  8338.     unsigned_attribute    tinyint        null,
  8339.     local_type_name     sysname        null
  8340.     )
  8341. end
  8342. go
  8343.  
  8344. grant select on spt_provider_types to public
  8345. go
  8346. dump tran master with no_log
  8347. go
  8348.  
  8349. /*
  8350. ** Insert the spt_provider_types rows for DBTYTPE_STR data types
  8351. */
  8352. begin tran
  8353.  
  8354. /* Get case sensitivity */
  8355. declare @case_sensitive bit
  8356. select @case_sensitive = case when 'a' <> 'A' then 1 else 0 end
  8357. /* Local Char */
  8358. insert into spt_provider_types values
  8359.     (
  8360.     47 /*SQLCHARACTER*/,        /* ss_dtype */
  8361.     0,                /* fixlen */
  8362.     'char',                /* type_name */
  8363.     129 /*DBTYPE_STR*/,        /* oledb_data_type */
  8364.     0,                /* best_match */
  8365.     null,                /* is_nullable */
  8366.     @case_sensitive,        /* case_sensitive */
  8367.     0,                /* fixed_prec_scale */
  8368.     0,                /* is_long */
  8369.     0,                /* auto_unique_value */
  8370.     null,                /* data_precision */
  8371.     null,                /* numeric_scale */
  8372.     null,                /* column_size */
  8373.     '''',                /* literal_prefix */
  8374.     '''',                /* literal_suffix */
  8375.     4 /*DB_SEARCHABLE*/,        /* searchable */
  8376.     null,                /* unsigned_attribute */
  8377.     'char'                /* local_type_name */
  8378.     )
  8379.  
  8380. /* Local Varchar */
  8381. insert into spt_provider_types values
  8382.     (
  8383.     39 /*SQLVARCHAR*/,        /* ss_dtype */
  8384.     null,                /* fixlen */
  8385.     'varchar',            /* type_name */
  8386.     129 /*DBTYPE_STR*/,        /* oledb_data_type */
  8387.     1,                /* best_match */
  8388.     null,                /* is_nullable */
  8389.     @case_sensitive,        /* case_sensitive */
  8390.     0,                /* fixed_prec_scale */
  8391.     0,                /* is_long */
  8392.     0,                /* auto_unique_value */
  8393.     null,                /* data_precision */
  8394.     null,                /* numeric_scale */
  8395.     null,                /* column_size */
  8396.     '''',                /* literal_prefix */
  8397.     '''',                /* literal_suffix */
  8398.     4 /*DB_SEARCHABLE*/,        /* searchable */
  8399.     null,                /* unsigned_attribute */
  8400.     'varchar'            /* local_type_name */
  8401.     )
  8402.  
  8403. /* Local Text */
  8404. insert into spt_provider_types values
  8405.     (
  8406.     35  /*SQLTEXT*/,        /* ss_dtype */
  8407.     null,                /* fixlen */
  8408.     'text',                /* type_name */
  8409.     129 /*DBTYPE_STR*/,        /* oledb_data_type */
  8410.     0,                /* best_match */
  8411.     null,                /* is_nullable */
  8412.     @case_sensitive,        /* case_sensitive */
  8413.     0,                /* fixed_prec_scale */
  8414.     1,                /* is_long */
  8415.     0,                /* auto_unique_value */
  8416.     null,                /* data_precision */
  8417.     null,                /* numeric_scale */
  8418.     2147483647,            /* column_size */
  8419.     '''',                /* literal_prefix */
  8420.     '''',                /* literal_suffix */
  8421.     2 /*DB_LIKE_ONLY*/,        /* searchable */
  8422.     null,                /* unsigned_attribute */
  8423.     'text'                /* local_type_name */
  8424.     )
  8425. commit tran
  8426. go
  8427. dump tran master with no_log
  8428. go
  8429.  
  8430. /*
  8431. ** Insert the spt_provider_types rows for DBTYTPE_BYTES data types
  8432. */
  8433. begin tran
  8434. /* Local Binary */
  8435. insert into spt_provider_types values
  8436.     (
  8437.     45 /*SQLBINARY*/,        /* ss_dtype */
  8438.     0,                /* fixlen */
  8439.     'binary',            /* type_name */
  8440.     128 /*DBTYPE_BYTES*/,        /* oledb_data_type */
  8441.     0,                /* best_match */
  8442.     null,                /* is_nullable */
  8443.     0,                /* case_sensitive */
  8444.     0,                /* fixed_prec_scale */
  8445.     0,                /* is_long */
  8446.     0,                /* auto_unique_value */
  8447.     null,                /* data_precision */
  8448.     null,                /* numeric_scale */
  8449.     null,                /* column_size */
  8450.     '0x',                /* literal_prefix */
  8451.     null,                /* literal_suffix */
  8452.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  8453.     null,                /* unsigned_attribute */
  8454.     'binary'            /* local_type_name */
  8455.     )
  8456.  
  8457. /* Local Varbinary */
  8458. insert into spt_provider_types values
  8459.     (
  8460.     37 /*SQLVARBINARY*/,        /* ss_dtype */
  8461.     null,                /* fixlen */
  8462.     'varbinary',            /* type_name */
  8463.     128 /*DBTYPE_BYTES*/,        /* oledb_data_type */
  8464.     1,                /* best_match */
  8465.     null,                /* is_nullable */
  8466.     0,                /* case_sensitive */
  8467.     0,                /* fixed_prec_scale */
  8468.     0,                /* is_long */
  8469.     0,                /* auto_unique_value */
  8470.     null,                /* data_precision */
  8471.     null,                /* numeric_scale */
  8472.     null,                /* column_size */
  8473.     '0x',                /* literal_prefix */
  8474.     null,                /* literal_suffix */
  8475.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  8476.     null,                /* unsigned_attribute */
  8477.     'varbinary'            /* local_type_name */
  8478.     )
  8479.  
  8480. /* Local Image */
  8481. insert into spt_provider_types values
  8482.     (
  8483.     34 /*SQLIMAGE*/,        /* ss_dtype */
  8484.     null,                /* fixlen */
  8485.     'image',            /* type_name */
  8486.     128 /*DBTYPE_BYTES*/,        /* oledb_data_type */
  8487.     0,                /* best_match */
  8488.     null,                /* is_nullable */
  8489.     0,                /* case_sensitive */
  8490.     0,                /* fixed_prec_scale */
  8491.     1,                /* is_long */
  8492.     0,                /* auto_unique_value */
  8493.     null,                /* data_precision */
  8494.     null,                /* numeric_scale */
  8495.     2147483647,            /* column_size */
  8496.     '0x',                /* literal_prefix */
  8497.     null,                /* literal_suffix */
  8498.     1 /*DB_UNSEARCHABLE*/,        /* searchable */
  8499.     null,                /* unsigned_attribute */
  8500.     'image'                /* local_type_name */
  8501.     )
  8502.  
  8503. commit tran
  8504. go
  8505. dump tran master with no_log
  8506. go
  8507.  
  8508. /*
  8509. ** Insert the spt_provider_types rows for DBTYPE_DBTIMESTAMP data types
  8510. */
  8511. begin tran
  8512.  
  8513. /* Local Datetime */
  8514. insert into spt_provider_types values
  8515.     (
  8516.     61 /*SQLDATETIME*/,        /* ss_dtype */
  8517.     8,                /* fixlen */
  8518.     'datetime',            /* type_name */
  8519.     135 /*DBTYPE_DBTIMESTAMP*/,    /* oledb_data_type */
  8520.     1,                /* best_match */
  8521.     null,                /* is_nullable */
  8522.     0,                /* case_sensitive */
  8523.     0,                /* fixed_prec_scale */
  8524.     0,                /* is_long */
  8525.     0,                /* auto_unique_value */
  8526.     23,                /* data_precision */
  8527.     null,                /* numeric_scale */
  8528.     null,                /* column_size */
  8529.     '''',                /* literal_prefix */
  8530.     '''',                /* literal_suffix */
  8531.     4 /*DB_SEARCHABLE*/,        /* searchable */
  8532.     null,                /* unsigned_attribute */
  8533.     'datetime'            /* local_type_name */
  8534.     )
  8535.  
  8536. /* Local Smalldatetime */
  8537. insert into spt_provider_types values
  8538.     (
  8539.     58 /*SQLDATETIM4*/,        /* ss_dtype */
  8540.     4,                /* fixlen */
  8541.     'smalldatetime',        /* type_name */
  8542.     135 /*DBTYPE_DBTIMESTAMP*/,    /* oledb_data_type */
  8543.     0,                /* best_match */
  8544.     null,                /* is_nullable */
  8545.     0,                /* case_sensitive */
  8546.     0,                /* fixed_prec_scale */
  8547.     0,                /* is_long */
  8548.     0,                /* auto_unique_value */
  8549.     16,                /* data_precision */
  8550.     null,                /* numeric_scale */
  8551.     null,                /* column_size */
  8552.     '''',                /* literal_prefix */
  8553.     '''',                /* literal_suffix */
  8554.     4 /*DB_SEARCHABLE*/,        /* searchable */
  8555.     null,                /* unsigned_attribute */
  8556.     'smalldatetime'            /* local_type_name */
  8557.     )
  8558.  
  8559. if (charindex('7.00', @@version) = 0)
  8560.     begin    /*    Add nullable type for non-Sphinx server */
  8561.     
  8562.     /* Local Datetimn */
  8563.     insert into spt_provider_types values
  8564.         (
  8565.         111 /*SQLDATETIMN*/,        /* ss_dtype */
  8566.         4,                /* fixlen */
  8567.         'smalldatetime',        /* type_name */
  8568.         135 /*DBTYPE_DBTIMESTAMP*/,    /* oledb_data_type */
  8569.         0,                /* best_match */
  8570.         null,                /* is_nullable */
  8571.         0,                /* case_sensitive */
  8572.         0,                /* fixed_prec_scale */
  8573.         0,                /* is_long */
  8574.         0,                /* auto_unique_value */
  8575.         16,                /* data_precision */
  8576.         null,                /* numeric_scale */
  8577.         null,                /* column_size */
  8578.         '''',                /* literal_prefix */
  8579.         '''',                /* literal_suffix */
  8580.         4 /*DB_SEARCHABLE*/,        /* searchable */
  8581.         null,                /* unsigned_attribute */
  8582.         'smalldatetime'            /* local_type_name */
  8583.         )    
  8584.     insert into spt_provider_types values
  8585.         (
  8586.         111 /*SQLDATETIMN*/,        /* ss_dtype */
  8587.         8,                /* fixlen */
  8588.         'datetime',            /* type_name */
  8589.         135 /*DBTYPE_DBTIMESTAMP*/,    /* oledb_data_type */
  8590.         0,                /* best_match */
  8591.         null,                /* is_nullable */
  8592.         0,                /* case_sensitive */
  8593.         0,                /* fixed_prec_scale */
  8594.         0,                /* is_long */
  8595.         0,                /* auto_unique_value */
  8596.         23,                /* data_precision */
  8597.         null,                /* numeric_scale */
  8598.         null,                /* column_size */
  8599.         '''',                /* literal_prefix */
  8600.         '''',                /* literal_suffix */
  8601.         4 /*DB_SEARCHABLE*/,        /* searchable */
  8602.         null,                /* unsigned_attribute */
  8603.         'datetime'            /* local_type_name */
  8604.         )
  8605.     end
  8606.  
  8607. commit tran
  8608. go
  8609. dump tran master with no_log
  8610. go
  8611.  
  8612. /*
  8613. ** Insert the spt_provider_types rows for DBTYPE_CY data types
  8614. */
  8615. begin tran
  8616.  
  8617. /* Local Smallmoney */
  8618. insert into spt_provider_types values
  8619.     (
  8620.     122 /*SQLMONEY4*/,        /* ss_dtype */
  8621.     4,                /* fixlen */
  8622.     'smallmoney',            /* type_name */
  8623.     6 /*DBTYPE_CY*/,        /* oledb_data_type */
  8624.     0,                /* best_match */
  8625.     null,                /* is_nullable */
  8626.     0,                /* case_sensitive */
  8627.     1,                /* fixed_prec_scale */
  8628.     0,                /* is_long */
  8629.     0,                /* auto_unique_value */
  8630.     10,                /* data_precision */
  8631.     null,                /* numeric_scale */
  8632.     null,                /* column_size */
  8633.     '$',                /* literal_prefix */
  8634.     null,                /* literal_suffix */
  8635.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  8636.     0,                /* unsigned_attribute */
  8637.     'smallmoney'            /* local_type_name */
  8638.     )    
  8639.     
  8640. /* Local Money */
  8641. insert into spt_provider_types values
  8642.     (
  8643.     60 /*SQLMONEY*/,        /* ss_dtype */
  8644.     8,                /* fixlen */
  8645.     'money',            /* type_name */
  8646.     6 /*DBTYPE_CY*/,        /* oledb_data_type */
  8647.     1,                /* best_match */
  8648.     null,                /* is_nullable */
  8649.     0,                /* case_sensitive */
  8650.     1,                /* fixed_prec_scale */
  8651.     0,                /* is_long */
  8652.     0,                /* auto_unique_value */
  8653.     19,                /* data_precision */
  8654.     null,                /* numeric_scale */
  8655.     null,                /* column_size */
  8656.     '$',                /* literal_prefix */
  8657.     null,                /* literal_suffix */
  8658.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  8659.     0,                /* unsigned_attribute */
  8660.     'money'                /* local_type_name */
  8661.     )    
  8662.  
  8663. if (charindex('7.00', @@version) = 0)
  8664.     begin    /*    Add nullable type for non-Sphinx server */
  8665.  
  8666.     /* Local Moneyn */
  8667.     insert into spt_provider_types values
  8668.         (
  8669.         110 /*SQLMONEYN*/,    /* ss_dtype */
  8670.         4,            /* fixlen */
  8671.         'smallmoney',        /* type_name */
  8672.         6 /*DBTYPE_CY*/,    /* oledb_data_type */
  8673.         0,            /* best_match */
  8674.         null,            /* is_nullable */
  8675.         0,            /* case_sensitive */
  8676.         1,            /* fixed_prec_scale */
  8677.         0,            /* is_long */
  8678.         0,            /* auto_unique_value */
  8679.         10,            /* data_precision */
  8680.         null,            /* numeric_scale */
  8681.         null,            /* column_size */
  8682.         '$',            /* literal_prefix */
  8683.         null,            /* literal_suffix */
  8684.         3 /*DB_ALL_EXCEPT_LIKE*/, /* searchable */
  8685.         0,            /* unsigned_attribute */
  8686.         'smallmoney'        /* local_type_name */
  8687.         )    
  8688.     insert into spt_provider_types values
  8689.         (
  8690.         110 /*SQLMONEYN*/,    /* ss_dtype */
  8691.         8,            /* fixlen */
  8692.         'money',        /* type_name */
  8693.         6 /*DBTYPE_CY*/,    /* oledb_data_type */
  8694.         0,            /* best_match */
  8695.         null,            /* is_nullable */
  8696.         0,            /* case_sensitive */
  8697.         1,            /* fixed_prec_scale */
  8698.         0,            /* is_long */
  8699.         0,            /* auto_unique_value */
  8700.         19,            /* data_precision */
  8701.         null,            /* numeric_scale */
  8702.         null,            /* column_size */
  8703.         '$',            /* literal_prefix */
  8704.         null,            /* literal_suffix */
  8705.         3 /*DB_ALL_EXCEPT_LIKE*/, /* searchable */
  8706.         null,            /* unsigned_attribute */
  8707.         'money'            /* local_type_name */
  8708.         )    
  8709.     end
  8710.  
  8711. commit tran
  8712. go
  8713. dump tran master with no_log
  8714. go
  8715.  
  8716. /*
  8717. ** Insert the spt_provider_types rows for the numeric data types
  8718. */
  8719. begin tran
  8720.  
  8721. /* Local Float */
  8722. insert into spt_provider_types values
  8723.     (
  8724.     62 /*SQLFLT8*/,            /* ss_dtype */
  8725.     8,                /* fixlen */
  8726.     'float',            /* type_name */
  8727.     5 /*DBTYPE_R8*/,        /* oledb_data_type */
  8728.     1,                /* best_match */
  8729.     null,                /* is_nullable */
  8730.     0,                /* case_sensitive */
  8731.     0,                /* fixed_prec_scale */
  8732.     0,                /* is_long */
  8733.     0,                /* auto_unique_value */
  8734.     15,                /* data_precision */
  8735.     null,                /* numeric_scale */
  8736.     null,                /* column_size */
  8737.     null,                /* literal_prefix */
  8738.     null,                /* literal_suffix */
  8739.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  8740.     0,                /* unsigned_attribute */
  8741.     'float'                /* local_type_name */
  8742.     )    
  8743.  
  8744. /* Local Real */
  8745. insert into spt_provider_types values
  8746.     (
  8747.     59 /*SQLFLT4*/,            /* ss_dtype */
  8748.     4,                /* fixlen */
  8749.     'real',                /* type_name */
  8750.     4 /*DBTYPE_R4*/,        /* oledb_data_type */
  8751.     1,                /* best_match */
  8752.     null,                /* is_nullable */
  8753.     0,                /* case_sensitive */
  8754.     0,                /* fixed_prec_scale */
  8755.     0,                /* is_long */
  8756.     0,                /* auto_unique_value */
  8757.     7,                /* data_precision */
  8758.     null,                /* numeric_scale */
  8759.     null,                /* column_size */
  8760.     null,                /* literal_prefix */
  8761.     null,                /* literal_suffix */
  8762.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  8763.     0,                /* unsigned_attribute */
  8764.     'real'                /* local_type_name */
  8765.     )    
  8766.  
  8767. /* Local Int */
  8768. insert into spt_provider_types values
  8769.     (
  8770.     56 /*SQLINT4*/,            /* ss_dtype */
  8771.     4,                /* fixlen */
  8772.     'int',                /* type_name */
  8773.     3 /*DBTYPE_I4*/,        /* oledb_data_type */
  8774.     1,                /* best_match */
  8775.     null,                /* is_nullable */
  8776.     0,                /* case_sensitive */
  8777.     1,                /* fixed_prec_scale */
  8778.     0,                /* is_long */
  8779.     1,                /* auto_unique_value */
  8780.     10,                /* data_precision */
  8781.     null,                /* numeric_scale */
  8782.     null,                /* column_size */
  8783.     null,                /* literal_prefix */
  8784.     null,                /* literal_suffix */
  8785.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  8786.     0,                /* unsigned_attribute */
  8787.     'int'                /* local_type_name */
  8788.     )    
  8789.  
  8790. /* Local Smallint */
  8791. insert into spt_provider_types values
  8792.     (
  8793.     52 /*SQLINT2*/,            /* ss_dtype */
  8794.     2,                /* fixlen */
  8795.     'smallint',            /* type_name */
  8796.     2 /*DBTYPE_I2*/,        /* oledb_data_type */
  8797.     1,                /* best_match */
  8798.     null,                /* is_nullable */
  8799.     0,                /* case_sensitive */
  8800.     1,                /* fixed_prec_scale */
  8801.     0,                /* is_long */
  8802.     1,                /* auto_unique_value */
  8803.     5,                /* data_precision */
  8804.     null,                /* numeric_scale */
  8805.     null,                /* column_size */
  8806.     null,                /* literal_prefix */
  8807.     null,                /* literal_suffix */
  8808.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  8809.     0,                /* unsigned_attribute */
  8810.     'smallint'            /* local_type_name */
  8811.     )    
  8812.  
  8813. /* Local Tinyint */
  8814. insert into spt_provider_types values
  8815.     (
  8816.     48 /*SQLINT1*/,            /* ss_dtype */
  8817.     1,                /* fixlen */
  8818.     'tinyint',            /* type_name */
  8819.     17 /*DBTYPE_UI1*/,        /* oledb_data_type */
  8820.     1,                /* best_match */
  8821.     null,                /* is_nullable */
  8822.     0,                /* case_sensitive */
  8823.     1,                /* fixed_prec_scale */
  8824.     0,                /* is_long */
  8825.     1,                /* auto_unique_value */
  8826.     3,                /* data_precision */
  8827.     null,                /* numeric_scale */
  8828.     null,                /* column_size */
  8829.     null,                /* literal_prefix */
  8830.     null,                /* literal_suffix */
  8831.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  8832.     1,                /* unsigned_attribute */
  8833.     'tinyint'            /* local_type_name */
  8834.     )    
  8835. commit tran
  8836. go
  8837. dump tran master with no_log
  8838. go
  8839. begin tran    
  8840. if (charindex('6.00', @@version) > 0 or
  8841.     charindex('6.50', @@version) > 0 or
  8842.     charindex('7.00', @@version) > 0)
  8843.     begin    /*    Add 6.0 data types */
  8844.     
  8845.     /* Local Decimal */
  8846.     insert into spt_provider_types values
  8847.         (
  8848.         55 /*SQLDECIMAL*/,        /* ss_dtype */
  8849.         0,                /* fixlen */
  8850.         'decimal',            /* type_name */
  8851.         131 /*DBTYPE_NUMERIC*/,        /* oledb_data_type */
  8852.         0,                /* best_match */
  8853.         null,                /* is_nullable */
  8854.         0,                /* case_sensitive */
  8855.         1,                /* fixed_prec_scale */
  8856.         0,                /* is_long */
  8857.         1,                /* auto_unique_value */
  8858.         38,                /* data_precision */
  8859.         null,                /* numeric_scale */
  8860.         null,                /* column_size */
  8861.         null,                /* literal_prefix */
  8862.         null,                /* literal_suffix */
  8863.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  8864.         0,                /* unsigned_attribute */
  8865.         'decimal'            /* local_type_name */
  8866.         )    
  8867.  
  8868.     /* Local Numeric */
  8869.     insert into spt_provider_types values
  8870.         (
  8871.         63 /*SQLNUMERIC*/,        /* ss_dtype */
  8872.         0,                /* fixlen */
  8873.         'numeric',            /* type_name */
  8874.         131 /*DBTYPE_NUMERIC*/,        /* oledb_data_type */
  8875.         1,                /* best_match */
  8876.         null,                /* is_nullable */
  8877.         0,                /* case_sensitive */
  8878.         1,                /* fixed_prec_scale */
  8879.         0,                /* is_long */
  8880.         1,                /* auto_unique_value */
  8881.         38,                /* data_precision */
  8882.         null,                /* numeric_scale */
  8883.         null,                /* column_size */
  8884.         null,                /* literal_prefix */
  8885.         null,                /* literal_suffix */
  8886.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  8887.         0,                /* unsigned_attribute */
  8888.         'numeric'            /* local_type_name */
  8889.         )    
  8890.     end
  8891. commit tran
  8892. go
  8893. dump tran master with no_log
  8894. go
  8895. begin tran
  8896. if (charindex('7.00', @@version) = 0)
  8897.     begin    /*    Add nullable type for non-Sphinx server */
  8898.     
  8899.     /* Local Floatn */
  8900.     insert into spt_provider_types values
  8901.         (
  8902.         109 /*SQLFLTN*/,        /* ss_dtype */
  8903.         8,                /* fixlen */
  8904.         'float',            /* type_name */
  8905.         5 /*DBTYPE_R8*/,        /* oledb_data_type */
  8906.         0,                /* best_match */
  8907.         null,                /* is_nullable */
  8908.         0,                /* case_sensitive */
  8909.         0,                /* fixed_prec_scale */
  8910.         0,                /* is_long */
  8911.         0,                /* auto_unique_value */
  8912.         15,                /* data_precision */
  8913.         null,                /* numeric_scale */
  8914.         null,                /* column_size */
  8915.         null,                /* literal_prefix */
  8916.         null,                /* literal_suffix */
  8917.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  8918.         null,                /* unsigned_attribute */
  8919.         'float'                /* local_type_name */
  8920.         )    
  8921.     insert into spt_provider_types values
  8922.         (
  8923.         109 /*SQLFLT4*/,        /* ss_dtype */
  8924.         4,                /* fixlen */
  8925.         'real',                /* type_name */
  8926.         4 /*DBTYPE_R4*/,        /* oledb_data_type */
  8927.         0,                /* best_match */
  8928.         null,                /* is_nullable */
  8929.         0,                /* case_sensitive */
  8930.         0,                /* fixed_prec_scale */
  8931.         0,                /* is_long */
  8932.         0,                /* auto_unique_value */
  8933.         7,                /* data_precision */
  8934.         null,                /* numeric_scale */
  8935.         null,                /* column_size */
  8936.         null,                /* literal_prefix */
  8937.         null,                /* literal_suffix */
  8938.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  8939.         null,                /* unsigned_attribute */
  8940.         'real'                /* local_type_name */
  8941.         )    
  8942.  
  8943.     /* Local Intn */
  8944.     insert into spt_provider_types values
  8945.         (
  8946.         38 /*SQLINTN*/,            /* ss_dtype */
  8947.         4,                /* fixlen */
  8948.         'int',                /* type_name */
  8949.         3 /*DBTYPE_I4*/,        /* oledb_data_type */
  8950.         1,                /* best_match */
  8951.         null,                /* is_nullable */
  8952.         0,                /* case_sensitive */
  8953.         1,                /* fixed_prec_scale */
  8954.         0,                /* is_long */
  8955.         1,                /* auto_unique_value */
  8956.         10,                /* data_precision */
  8957.         null,                /* numeric_scale */
  8958.         null,                /* column_size */
  8959.         null,                /* literal_prefix */
  8960.         null,                /* literal_suffix */
  8961.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  8962.         0,                /* unsigned_attribute */
  8963.         'int'                /* local_type_name */
  8964.         )    
  8965.     insert into spt_provider_types values
  8966.         (
  8967.         38 /*SQLINTN*/,            /* ss_dtype */
  8968.         2,                /* fixlen */
  8969.         'smallint',            /* type_name */
  8970.         2 /*DBTYPE_I2*/,        /* oledb_data_type */
  8971.         0,                /* best_match */
  8972.         null,                /* is_nullable */
  8973.         0,                /* case_sensitive */
  8974.         1,                /* fixed_prec_scale */
  8975.         0,                /* is_long */
  8976.         1,                /* auto_unique_value */
  8977.         5,                /* data_precision */
  8978.         null,                /* numeric_scale */
  8979.         null,                /* column_size */
  8980.         null,                /* literal_prefix */
  8981.         null,                /* literal_suffix */
  8982.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  8983.         0,                /* unsigned_attribute */
  8984.         'smallint'            /* local_type_name */
  8985.         )    
  8986.     insert into spt_provider_types values
  8987.         (
  8988.         38 /*SQLINTN*/,            /* ss_dtype */
  8989.         1,                /* fixlen */
  8990.         'tinyint',            /* type_name */
  8991.         17 /*DBTYPE_UI1*/,        /* oledb_data_type */
  8992.         0,                /* best_match */
  8993.         null,                /* is_nullable */
  8994.         0,                /* case_sensitive */
  8995.         1,                /* fixed_prec_scale */
  8996.         0,                /* is_long */
  8997.         1,                /* auto_unique_value */
  8998.         3,                /* data_precision */
  8999.         null,                /* numeric_scale */
  9000.         null,                /* column_size */
  9001.         null,                /* literal_prefix */
  9002.         null,                /* literal_suffix */
  9003.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  9004.         1,                /* unsigned_attribute */
  9005.         'tinyint'            /* local_type_name */
  9006.         )    
  9007.  
  9008.     if (charindex('6.00', @@version) > 0 or    charindex('6.50', @@version) > 0)
  9009.         begin    /*    Add 6.0 data types */
  9010.         
  9011.         /* Local Decimaln */
  9012.         insert into spt_provider_types values
  9013.             (
  9014.             106 /*SQLDECIMALN*/,        /* ss_dtype */
  9015.             0,                /* fixlen */
  9016.             'decimal',            /* type_name */
  9017.             131 /*DBTYPE_NUMERIC*/,        /* oledb_data_type */
  9018.             0,                /* best_match */
  9019.             null,                /* is_nullable */
  9020.             0,                /* case_sensitive */
  9021.             1,                /* fixed_prec_scale */
  9022.             0,                /* is_long */
  9023.             1,                /* auto_unique_value */
  9024.             38,                /* data_precision */
  9025.             null,                /* numeric_scale */
  9026.             null,                /* column_size */
  9027.             null,                /* literal_prefix */
  9028.             null,                /* literal_suffix */
  9029.             3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  9030.             0,                /* unsigned_attribute */
  9031.             'decimal'            /* local_type_name */
  9032.             )    
  9033.  
  9034.         /* Local Numericn */
  9035.         insert into spt_provider_types values
  9036.             (
  9037.             108 /*SQLNUMERICN*/,        /* ss_dtype */
  9038.             0,                /* fixlen */
  9039.             'numeric',            /* type_name */
  9040.             131 /*DBTYPE_NUMERIC*/,        /* oledb_data_type */
  9041.             0,                /* best_match */
  9042.             null,                /* is_nullable */
  9043.             0,                /* case_sensitive */
  9044.             1,                /* fixed_prec_scale */
  9045.             0,                /* is_long */
  9046.             1,                /* auto_unique_value */
  9047.             38,                /* data_precision */
  9048.             null,                /* numeric_scale */
  9049.             null,                /* column_size */
  9050.             null,                /* literal_prefix */
  9051.             null,                /* literal_suffix */
  9052.             3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  9053.             0,                /* unsigned_attribute */
  9054.             'numeric'            /* local_type_name */
  9055.             )    
  9056.         end
  9057.     end
  9058. commit tran
  9059. go
  9060. dump tran master with no_log
  9061. go
  9062.  
  9063. /*
  9064. **    Remaining data types
  9065. */
  9066. if (charindex('7.00', @@version) = 0)
  9067. begin
  9068.     print ''
  9069.     print ''
  9070.     print 'Warning:'
  9071.     print 'you are installing the stored procedures '
  9072.     print 'on a pre 7.0 SQL Server.'
  9073.     print 'Ignore the following errors.'
  9074. end
  9075. go
  9076. begin tran
  9077.  
  9078. if (charindex('7.00', @@version) > 0)
  9079.     begin
  9080.     /* Local Bit */
  9081.     insert into spt_provider_types values
  9082.         (
  9083.         50 /*SQLBIT*/,            /* ss_dtype */
  9084.         0,                /* fixlen */
  9085.         'bit',                /* type_name */
  9086.         11 /*DBTYPE_BOOL*/,        /* oledb_data_type */
  9087.         1,                /* best_match */
  9088.         null,                /* is_nullable */
  9089.         0,                /* case_sensitive */
  9090.         0,                /* fixed_prec_scale */
  9091.         0,                /* is_long */
  9092.         0,                /* auto_unique_value */
  9093.         1,                /* data_precision */
  9094.         null,                /* numeric_scale */
  9095.         null,                /* column_size */
  9096.         null,                /* literal_prefix */
  9097.         null,                /* literal_suffix */
  9098.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  9099.         null,                /* unsigned_attribute */
  9100.         'bit'                /* local_type_name */
  9101.         )        
  9102.     
  9103.     /* Local Timestamp */
  9104.     insert into spt_provider_types values
  9105.         (
  9106.         0,                /* ss_dtype */
  9107.         8,                /* fixlen */
  9108.         'timestamp',            /* type_name */
  9109.         128 /*DBTYPE_BYTES*/,        /* oledb_data_type */
  9110.         0,                /* best_match */
  9111.         null,                /* is_nullable */
  9112.         0,                /* case_sensitive */
  9113.         0,                /* fixed_prec_scale */
  9114.         0,                /* is_long */
  9115.         0,                /* auto_unique_value */
  9116.         null,                /* data_precision */
  9117.         null,                /* numeric_scale */
  9118.         null,                /* column_size */
  9119.         '0x',                /* literal_prefix */
  9120.         null,                /* literal_suffix */
  9121.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  9122.         null,                /* unsigned_attribute */
  9123.         'timestamp'            /* local_type_name */
  9124.         )    
  9125.     /* Local GUID */
  9126.     insert into spt_provider_types values
  9127.         (
  9128.         0,                /* ss_dtype */
  9129.         16,                /* fixlen */
  9130.         'uniqueidentifier',        /* type_name */
  9131.         72 /*DBTYPE_GUID*/,        /* oledb_data_type */
  9132.         1,                /* best_match */
  9133.         null,                /* is_nullable */
  9134.         0,                /* case_sensitive */
  9135.         0,                /* fixed_prec_scale */
  9136.         0,                /* is_long */
  9137.         0,                /* auto_unique_value */
  9138.         null,                /* data_precision */
  9139.         null,                /* numeric_scale */
  9140.         null,                /* column_size */
  9141.         '''',                /* literal_prefix */
  9142.         '''',                /* literal_suffix */
  9143.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  9144.         null,                /* unsigned_attribute */
  9145.         'uniqueidentifier'        /* local_type_name */
  9146.         )    
  9147.     
  9148.     /* Get case sensitivity */
  9149.     declare @ncase_sensitive bit
  9150.     select @ncase_sensitive = case when N'a' <> N'A' then 1 else 0 end
  9151.  
  9152.     /* Local NChar */
  9153.     insert into spt_provider_types values
  9154.         (
  9155.         0,                /* ss_dtype */
  9156.         0,                /* fixlen */
  9157.         'nchar',            /* type_name */
  9158.         130 /*DBTYPE_WSTR*/,        /* oledb_data_type */
  9159.         0,                /* best_match */
  9160.         null,                /* is_nullable */
  9161.         @ncase_sensitive,        /* case_sensitive */
  9162.         0,                /* fixed_prec_scale */
  9163.         0,                /* is_long */
  9164.         0,                /* auto_unique_value */
  9165.         null,                /* data_precision */
  9166.         null,                /* numeric_scale */
  9167.         null,                /* column_size */
  9168.         'N''',                /* literal_prefix */
  9169.         '''',                /* literal_suffix */
  9170.         4 /*DB_SEARCHABLE*/,        /* searchable */
  9171.         null,                /* unsigned_attribute */
  9172.         'nchar'                /* local_type_name */
  9173.         )    
  9174.  
  9175.     /* Local NVarChar */
  9176.     insert into spt_provider_types values
  9177.         (
  9178.         0,                /* ss_dtype */
  9179.         null,                /* fixlen */
  9180.         'nvarchar',            /* type_name */
  9181.         130 /*DBTYPE_WSTR*/,        /* oledb_data_type */
  9182.         1,                /* best_match */
  9183.         null,                /* is_nullable */
  9184.         @ncase_sensitive,        /* case_sensitive */
  9185.         0,                /* fixed_prec_scale */
  9186.         0,                /* is_long */
  9187.         0,                /* auto_unique_value */
  9188.         null,                /* data_precision */
  9189.         null,                /* numeric_scale */
  9190.         null,                /* column_size */
  9191.         'N''',                /* literal_prefix */
  9192.         '''',                /* literal_suffix */
  9193.         4 /*DB_SEARCHABLE*/,        /* searchable */
  9194.         null,                /* unsigned_attribute */
  9195.         'nvarchar'            /* local_type_name */
  9196.         )    
  9197.  
  9198.     /* Local NText */
  9199.     insert into spt_provider_types values
  9200.         (
  9201.         0,                /* ss_dtype */
  9202.         null,                /* fixlen */
  9203.         'ntext',            /* type_name */
  9204.         130 /*DBTYPE_WSTR*/,        /* oledb_data_type */
  9205.         0,                /* best_match */
  9206.         null,                /* is_nullable */
  9207.         @ncase_sensitive,        /* case_sensitive */
  9208.         0,                /* fixed_prec_scale */
  9209.         1,                /* is_long */
  9210.         0,                /* auto_unique_value */
  9211.         null,                /* data_precision */
  9212.         null,                /* numeric_scale */
  9213.         1073741823,            /* column_size */
  9214.         'N''',                /* literal_prefix */
  9215.         '''',                /* literal_suffix */
  9216.         2 /*DB_LIKE_ONLY*/,        /* searchable */
  9217.         null,                /* unsigned_attribute */
  9218.         'ntext'                /* local_type_name */
  9219.         )    
  9220.  
  9221.     /* data types changed in Sphinx: DO AFTER ALL TYPES INSERTED! */    
  9222.     update spt_provider_types set ss_dtype = (select xtype from systypes    where type_name like name+'%')
  9223.  
  9224.     end
  9225. commit tran
  9226. go
  9227. dump tran master with no_log
  9228. go
  9229. begin tran
  9230. if (charindex('7.00', @@version) = 0)
  9231.     begin
  9232.     /* Local Bit */
  9233.     insert into spt_provider_types values
  9234.         (
  9235.         50 /*SQLBIT*/,            /* ss_dtype */
  9236.         0,                /* fixlen */
  9237.         'bit',                /* type_name */
  9238.         11 /*DBTYPE_BOOL*/,        /* oledb_data_type */
  9239.         1,                /* best_match */
  9240.         0,                /* is_nullable */
  9241.         0,                /* case_sensitive */
  9242.         0,                /* fixed_prec_scale */
  9243.         0,                /* is_long */
  9244.         0,                /* auto_unique_value */
  9245.         1,                /* data_precision */
  9246.         null,                /* numeric_scale */
  9247.         null,                /* column_size */
  9248.         null,                /* literal_prefix */
  9249.         null,                /* literal_suffix */
  9250.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  9251.         null,                /* unsigned_attribute */
  9252.         'bit'                /* local_type_name */
  9253.         )    
  9254.  
  9255.     /* Local Timestamp */
  9256.     insert into spt_provider_types values
  9257.         (
  9258.         45 /*SQLBINARY*/,        /* ss_dtype */
  9259.         8,                /* fixlen */
  9260.         'timestamp',            /* type_name */
  9261.         128 /*DBTYPE_BYTES*/,        /* oledb_data_type */
  9262.         0,                /* best_match */
  9263.         null,                /* is_nullable */
  9264.         0,                /* case_sensitive */
  9265.         0,                /* fixed_prec_scale */
  9266.         0,                /* is_long */
  9267.         0,                /* auto_unique_value */
  9268.         null,                /* data_precision */
  9269.         null,                /* numeric_scale */
  9270.         null,                /* column_size */
  9271.         '0x',                /* literal_prefix */
  9272.         null,                /* literal_suffix */
  9273.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  9274.         null,                /* unsigned_attribute */
  9275.         'timestamp'            /* local_type_name */
  9276.         )        
  9277. end
  9278.  
  9279. commit tran
  9280. go
  9281. dump tran master with no_log
  9282. go
  9283.  
  9284. create unique clustered index datatypeinfoclust on spt_provider_types(ss_dtype,fixlen)
  9285. go
  9286.  
  9287. dump tran master with no_log
  9288. go
  9289. if (charindex('6.00', @@version) > 0)
  9290.     begin
  9291.     if (exists (select * from sysobjects
  9292.         where name = 'spt_provider_types' and type = 'U '))
  9293.         begin
  9294.         drop table spt_provider_types
  9295.         dump tran master with no_log
  9296.         end
  9297.     end
  9298. go
  9299.  
  9300. if (charindex('7.00', @@version) = 0)
  9301. begin
  9302.     print ''
  9303.     print ''
  9304.     print 'Warning:'
  9305.     print 'you are installing the stored procedures '
  9306.     print 'on a pre 7.0 SQL Server.'
  9307.     print 'Ignore the following errors.'
  9308. end
  9309. go
  9310.  
  9311. /* 
  9312. ** (leih 2/11/98) 
  9313. ** The following SP is shared by instcat.sql and the server to add information
  9314. ** related to server language collations. The server calls it at the end of
  9315. ** collation change to update the catalog with the new language collations
  9316. ** Everything in this SP should NOT use tempdb.  
  9317. */
  9318. create procedure sp_add_server_sortinfo
  9319. as
  9320. -- spt_server_info has unigue clustered index on attribute_id
  9321. delete spt_server_info where attribute_id in (16,18)
  9322.  
  9323. --insert into spt_server_info
  9324. --    select 18,'COLLATION_SEQ',
  9325. --        'charset='+t2.name+' sort_order='+t1.name
  9326. --        +' charset_num='+rtrim(convert(char(4),t1.csid))+
  9327. --        ' sort_order_num='+rtrim(convert(char(4),t1.id))
  9328. --    from master.dbo.syscharsets t1, master.dbo.syscharsets t2, master.dbo.sysconfigures t3
  9329. --    where t1.csid=t2.id and t1.id=t3.value and t3.config in (123,1123)
  9330. declare @sortid int, @csid int, @sortname varchar(255), @attribute_value varchar(255)
  9331. declare @case_sensitive bit
  9332. declare @ncase_sensitive bit
  9333. select @case_sensitive = case when 'a' <> 'A' then 1 else 0 end
  9334. select @ncase_sensitive = case when N'a' <> N'A' then 1 else 0 end
  9335. select @sortid = value from sysconfigures where config = 1123
  9336. select @csid = csid, @sortname = name from master.dbo.syscharsets where id = @sortid
  9337. select @attribute_value = 'charset='+ name +' sort_order=' + @sortname
  9338.         + ' charset_num=' + rtrim(convert(char(4),@csid))+
  9339.         ' sort_order_num=' + rtrim(convert(char(4),@sortid))
  9340.         from syscharsets where id = @csid
  9341.  
  9342. insert into spt_server_info
  9343.         values (18,'COLLATION_SEQ', isnull(@attribute_value, ' '))
  9344.  
  9345. if @case_sensitive = 1 /* If case sensitive server */
  9346. begin
  9347.     insert into spt_server_info
  9348.         values (16,'IDENTIFIER_CASE','SENSITIVE')
  9349. end
  9350. else
  9351. begin
  9352.     insert into spt_server_info
  9353.         values (16,'IDENTIFIER_CASE','MIXED')
  9354. end
  9355.  
  9356.  
  9357. update spt_datatype_info set CASE_SENSITIVE = @case_sensitive 
  9358.     where DATA_TYPE in (-1, 1, 12) /* CHAR types */
  9359. update spt_datatype_info set CASE_SENSITIVE = @ncase_sensitive
  9360.     where DATA_TYPE in (-10, -9, -8) /* NCHAR types */
  9361. update spt_provider_types set case_sensitive = @case_sensitive 
  9362.     where oledb_data_type = 129 /*DBTYPE_STR*/
  9363. update spt_provider_types set case_sensitive = @ncase_sensitive
  9364.     where oledb_data_type = 130 /*DBTYPE_WSTR*/
  9365.  
  9366. go
  9367.  
  9368. if (charindex('7.00', @@version) <> 0)
  9369. begin
  9370.     exec sp_add_server_sortinfo
  9371. end
  9372. go
  9373.  
  9374. print ''
  9375. print 'creating sp_catalogs_rowset'
  9376. go
  9377.  
  9378. create procedure sp_catalogs_rowset
  9379.     (
  9380.     @catalog_name        varchar(255)
  9381.     )        
  9382. as
  9383.     select
  9384.         CATALOG_NAME    = name,
  9385.         DESCRIPTION    = convert(varchar(1),null)
  9386.     from     master.dbo.sysdatabases
  9387.     where    name = @catalog_name
  9388. go
  9389. dump tran master with no_log
  9390. go
  9391. create procedure sp_catalogs_rowset;2
  9392.     (
  9393.     @dummy        int /* remove when Hydra bug 17032 is fixed */
  9394.     )
  9395. as
  9396.     select
  9397.         CATALOG_NAME    = name,
  9398.         DESCRIPTION    = convert(varchar(1),null)
  9399.     from     master.dbo.sysdatabases
  9400.     order by 1
  9401. go
  9402. dump tran master with no_log
  9403. go
  9404.  
  9405. if (charindex('7.00', @@version) = 0)
  9406. begin
  9407.     print ''
  9408.     print ''
  9409.     print 'Warning:'
  9410.     print 'you are installing the stored procedures '
  9411.     print 'on a pre 7.0 SQL Server.'
  9412.     print 'Ignore the following errors.'
  9413. end
  9414. else
  9415.     drop proc sp_catalogs_rowset
  9416. go
  9417.  
  9418. /*    Procedure for 7.0 server */
  9419. create procedure sp_catalogs_rowset
  9420.     (
  9421.     @catalog_name        sysname
  9422.     )        
  9423. as
  9424.     select
  9425.         CATALOG_NAME    = name,
  9426.         DESCRIPTION    = convert(nvarchar(1),null)
  9427.     from     master.dbo.sysdatabases
  9428.     where    name = @catalog_name
  9429. go
  9430. dump tran master with no_log
  9431. go
  9432. create procedure sp_catalogs_rowset;2
  9433.     (
  9434.     @dummy        int /* remove when Hydra bug 17032 is fixed */
  9435.     )
  9436. as
  9437.     select
  9438.         CATALOG_NAME    = name,
  9439.         DESCRIPTION    = convert(nvarchar(1),null)
  9440.     from     master.dbo.sysdatabases
  9441.     order by 1
  9442. go
  9443. dump tran master with no_log
  9444. go
  9445. create procedure sp_catalogs_rowset;5
  9446.     (
  9447.     @server_name    sysname,
  9448.     @catalog_name    sysname = NULL
  9449.     )
  9450. as
  9451.     select    CATALOG_NAME,
  9452.         DESCRIPTION
  9453.     from master.dbo.SYSREMOTE_CATALOGS < @server_name, @catalog_name >
  9454.     order by 1
  9455. go
  9456.  
  9457. grant execute on sp_catalogs_rowset to public
  9458. go
  9459.  
  9460. dump tran master with no_log
  9461. go
  9462. if (charindex('6.00', @@version) > 0)
  9463.     begin
  9464.     if (exists (select * from sysobjects
  9465.             where name = 'sp_catalogs_rowset' and type = 'P '))
  9466.         begin
  9467.         drop procedure sp_catalogs_rowset
  9468.         dump tran master with no_log
  9469.         end
  9470.     end
  9471. go
  9472.  
  9473.  
  9474. print ''
  9475. print 'creating sp_column_privileges_rowset'
  9476. go
  9477.  
  9478. /*    Procedure for 6.0 and 6.5 server */
  9479. CREATE PROCEDURE sp_column_privileges_rowset
  9480.     (
  9481.     @table_name     varchar(255) = null,
  9482.     @table_schema    varchar(255) = null,
  9483.     @column_name    varchar(255) = null,
  9484.     @grantor    varchar(255) = null,
  9485.     @grantee    varchar(255) = null
  9486.     )
  9487. as
  9488. IF @table_name is not null
  9489.     BEGIN
  9490.     select
  9491.         GRANTOR        = user_name(p.grantor),
  9492.         GRANTEE        = user_name(u.uid),
  9493.         TABLE_CATALOG    = db_name(),
  9494.         TABLE_SCHEMA    = user_name(o.uid),
  9495.         TABLE_NAME    = o.name,
  9496.         COLUMN_NAME    = c.name,
  9497.         COLUMN_GUID    = convert(binary(16),null),
  9498.         COLUMN_PROPID    = convert(int,null),
  9499.         PRIVILEGE_TYPE    = convert(varchar(30),
  9500.                     case p.action
  9501.                     when 193 then 'SELECT'
  9502.                     when 195 then 'INSERT'
  9503.                     when 197 then 'UPDATE'
  9504.                     else 'REFERENCES'
  9505.                     end),
  9506.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  9507.     from 
  9508.         sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c
  9509.     where
  9510.         o.name = @table_name
  9511.     and     o.type in ('U','V','S')
  9512.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  9513.     and    c.id = o.id
  9514.     and     (@column_name is null or @column_name = c.name)
  9515.     and     c.id = p.id
  9516.     and    (@grantor is null or @grantor = user_name(p.grantor))
  9517.     and     case 
  9518.             when substring(p.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  9519.             when substring(p.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p.columns, v.low, 1))
  9520.             else (~convert(tinyint, coalesce(substring(p.columns, v.low, 1),0)))
  9521.             end & v.high <> 0            /* permission applies to this column */
  9522.     and     v.number <= (select count(*) from syscolumns where id = o.id)    /* ranges from 1 to # of columns in table */
  9523.     and     v.type = 'P'
  9524.     and     v.number = c.colid
  9525.     and    (@grantee is null or @grantee = user_name(u.uid))
  9526.             /* expand groups */
  9527.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  9528.     and     p.protecttype <> 206    /* only grant rows */
  9529.     and     p.action in (26,193,195,197)
  9530.     and     o.uid <> u.uid            /* no rows for owner */
  9531.     and     not exists (            /* exclude revoke'd privileges */
  9532.             select *
  9533.             from sysprotects p1
  9534.             where
  9535.                 p1.protecttype = 206
  9536.             and     p1.action = p.action
  9537.             and     p1.id = p.id
  9538.             and     p1.uid = u.uid
  9539.             and     case 
  9540.                 when substring(p1.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  9541.                 when substring(p1.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  9542.                                 else (~convert(tinyint,coalesce(substring(p.columns, v.low, 1),0)))
  9543.                 end & v.high <> 0)            /* permission applies to this column */
  9544.     union all
  9545.     select    /*    Add rows for table owner */
  9546.         GRANTOR        = user_name(u.uid),
  9547.         GRANTEE        = user_name(o.uid),
  9548.         TABLE_CATALOG    = db_name(),
  9549.         TABLE_SCHEMA    = user_name(o.uid),
  9550.         TABLE_NAME    = o.name,
  9551.         COLUMN_NAME    = c.name,
  9552.         COLUMN_GUID    = convert(binary(16),null),
  9553.         COLUMN_PROPID    = convert(int,null),
  9554.         PRIVILEGE_TYPE    = convert(varchar(30),
  9555.                     case v.number
  9556.                     when 193 then 'SELECT'
  9557.                     when 195 then 'INSERT'
  9558.                     when 197 then 'UPDATE'
  9559.                     else 'REFERENCES'
  9560.                     end),
  9561.         IS_GRANTABLE    = convert(bit,1)    
  9562.     from 
  9563.         sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  9564.     where
  9565.         o.name = @table_name
  9566.     and     o.type in ('U','V','S')
  9567.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  9568.     and    (@grantee is null or @grantee = user_name(o.uid))
  9569.     and    c.id = o.id
  9570.     and     (@column_name is null or @column_name = c.name)
  9571.     and     u.suid = 1        /* grantor is dbo of database */
  9572.     and    (@grantor is null or @grantor = user_name(u.uid))
  9573.     and     v.type = 'P'        /* cross product to get all exposed privileges */
  9574.     and     v.number in (26,193,195,197)
  9575.     and     not exists (        /* exclude revoke'd privileges */
  9576.             select *
  9577.             from     sysprotects p1
  9578.             where    p1.protecttype = 206
  9579.             and     p1.action = v.number
  9580.             and     p1.id = o.id
  9581.             and     p1.uid = o.uid)
  9582.     order by 4, 5, 6, 9, 1, 2
  9583.     END
  9584. ELSE
  9585.     BEGIN
  9586.     select
  9587.         GRANTOR        = user_name(p.grantor),
  9588.         GRANTEE        = user_name(u.uid),
  9589.         TABLE_CATALOG    = db_name(),
  9590.         TABLE_SCHEMA    = user_name(o.uid),
  9591.         TABLE_NAME    = o.name,
  9592.         COLUMN_NAME    = c.name,
  9593.         COLUMN_GUID    = convert(binary(16),null),
  9594.         COLUMN_PROPID    = convert(int,null),
  9595.         PRIVILEGE_TYPE    = convert(varchar(30),
  9596.                     case p.action
  9597.                     when 193 then 'SELECT'
  9598.                     when 195 then 'INSERT'
  9599.                     when 197 then 'UPDATE'
  9600.                     else 'REFERENCES'
  9601.                     end),
  9602.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  9603.     from 
  9604.         sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c
  9605.     where
  9606.         o.type in ('U','V','S')
  9607.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  9608.     and    c.id = o.id
  9609.     and     (@column_name is null or @column_name = c.name)
  9610.     and     c.id = p.id
  9611.     and    (@grantor is null or @grantor = user_name(p.grantor))
  9612.     and     case 
  9613.             when substring(p.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  9614.             when substring(p.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p.columns, v.low, 1))
  9615.             else (~convert(tinyint, coalesce(substring(p.columns, v.low, 1),0)))
  9616.             end & v.high <> 0            /* permission applies to this column */
  9617.     and     v.number <= (select count(*) from syscolumns where id = o.id)    /* ranges from 1 to # of columns in table */
  9618.     and     v.type = 'P'
  9619.     and     v.number = c.colid
  9620.     and    (@grantee is null or @grantee = user_name(u.uid))
  9621.             /* expand groups */
  9622.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  9623.     and     p.protecttype <> 206    /* only grant rows */
  9624.     and     p.action in (26,193,195,197)
  9625.     and     o.uid <> u.uid            /* no rows for owner */
  9626.     and     not exists (            /* exclude revoke'd privileges */
  9627.             select *
  9628.             from sysprotects p1
  9629.             where
  9630.                 p1.protecttype = 206
  9631.             and     p1.action = p.action
  9632.             and     p1.id = p.id
  9633.             and     p1.uid = u.uid
  9634.             and     case 
  9635.                 when substring(p1.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  9636.                 when substring(p1.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  9637.                                 else (~convert(tinyint,coalesce(substring(p.columns, v.low, 1),0)))
  9638.                 end & v.high <> 0)            /* permission applies to this column */
  9639.     union all
  9640.     select    /*    Add rows for table owner */
  9641.         GRANTOR        = user_name(u.uid),
  9642.         GRANTEE        = user_name(o.uid),
  9643.         TABLE_CATALOG    = db_name(),
  9644.         TABLE_SCHEMA    = user_name(o.uid),
  9645.         TABLE_NAME    = o.name,
  9646.         COLUMN_NAME    = c.name,
  9647.         COLUMN_GUID    = convert(binary(16),null),
  9648.         COLUMN_PROPID    = convert(int,null),
  9649.         PRIVILEGE_TYPE    = convert(varchar(30),
  9650.                     case v.number
  9651.                     when 193 then 'SELECT'
  9652.                     when 195 then 'INSERT'
  9653.                     when 197 then 'UPDATE'
  9654.                     else 'REFERENCES'
  9655.                     end),
  9656.         IS_GRANTABLE    = convert(bit,1)    
  9657.     from 
  9658.         sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  9659.     where
  9660.          o.type in ('U','V','S')
  9661.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  9662.     and    (@grantee is null or @grantee = user_name(o.uid))
  9663.     and    c.id = o.id
  9664.     and     (@column_name is null or @column_name = c.name)
  9665.     and     u.suid = 1        /* grantor is dbo of database */
  9666.     and    (@grantor is null or @grantor = user_name(u.uid))
  9667.     and     v.type = 'P'        /* cross product to get all exposed privileges */
  9668.     and     v.number in (26,193,195,197)
  9669.     and     not exists (        /* exclude revoke'd privileges */
  9670.             select *
  9671.             from     sysprotects p1
  9672.             where    p1.protecttype = 206
  9673.             and     p1.action = v.number
  9674.             and     p1.id = o.id
  9675.             and     p1.uid = o.uid)
  9676.     order by 4, 5, 6, 9, 1, 2
  9677.     END
  9678. go
  9679. dump tran master with no_log
  9680. go
  9681. CREATE PROCEDURE sp_column_privileges_rowset;2
  9682.     (
  9683.        @handle        int output,
  9684.        @scrollopt    int output,
  9685.     @ccopt        int output,
  9686.     @rows        int output,
  9687.     @table_name     varchar(255) = null,
  9688.     @table_schema    varchar(255) = null,
  9689.     @column_name    varchar(255) = null,
  9690.     @grantor    varchar(255) = null,
  9691.     @grantee    varchar(255) = null
  9692.     )
  9693. as
  9694.  
  9695. declare @ret int
  9696.  
  9697. SET NOCOUNT ON
  9698.     
  9699. create table #spcprivsrowset1
  9700.     (
  9701.     GRANTOR        sysname not null,
  9702.     GRANTEE        sysname not null,
  9703.     TABLE_CATALOG    sysname not null,
  9704.     TABLE_SCHEMA    sysname not null,
  9705.     TABLE_NAME    sysname not null,
  9706.     COLUMN_NAME    sysname not null,
  9707.     COLUMN_GUID    binary(16) null,
  9708.     COLUMN_PROPID    int null,
  9709.     PRIVILEGE_TYPE    sysname not null,
  9710.     IS_GRANTABLE    bit not null
  9711.     )
  9712.  
  9713. IF @table_name is not null
  9714.     BEGIN
  9715.     insert into #spcprivsrowset1
  9716.     select
  9717.         GRANTOR        = user_name(p.grantor),
  9718.         GRANTEE        = user_name(u.uid),
  9719.         TABLE_CATALOG    = db_name(),
  9720.         TABLE_SCHEMA    = user_name(o.uid),
  9721.         TABLE_NAME    = o.name,
  9722.         COLUMN_NAME    = c.name,
  9723.         COLUMN_GUID    = convert(binary(16),null),
  9724.         COLUMN_PROPID    = convert(int,null),
  9725.         PRIVILEGE_TYPE    = convert(varchar(30),
  9726.                     case p.action
  9727.                     when 193 then 'SELECT'
  9728.                     when 195 then 'INSERT'
  9729.                     when 197 then 'UPDATE'
  9730.                     else 'REFERENCES'
  9731.                     end),
  9732.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  9733.     from 
  9734.         sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c
  9735.     where
  9736.         o.name = @table_name
  9737.     and     o.type in ('U','V','S')
  9738.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  9739.     and    c.id = o.id
  9740.     and     (@column_name is null or @column_name = c.name)
  9741.     and     c.id = p.id
  9742.     and    (@grantor is null or @grantor = user_name(p.grantor))
  9743.     and     case 
  9744.             when substring(p.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  9745.             when substring(p.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p.columns, v.low, 1))
  9746.             else (~convert(tinyint, coalesce(substring(p.columns, v.low, 1),0)))
  9747.             end & v.high <> 0            /* permission applies to this column */
  9748.     and     v.number <= (select count(*) from syscolumns where id = o.id)    /* ranges from 1 to # of columns in table */
  9749.     and     v.type = 'P'
  9750.     and     v.number = c.colid
  9751.     and    (@grantee is null or @grantee = user_name(u.uid))
  9752.             /* expand groups */
  9753.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  9754.     and     p.protecttype <> 206    /* only grant rows */
  9755.     and     p.action in (26,193,195,197)
  9756.     and     o.uid <> u.uid            /* no rows for owner */
  9757.     and     not exists (            /* exclude revoke'd privileges */
  9758.             select *
  9759.             from sysprotects p1
  9760.             where
  9761.                 p1.protecttype = 206
  9762.             and     p1.action = p.action
  9763.             and     p1.id = p.id
  9764.             and     p1.uid = u.uid
  9765.             and     case 
  9766.                 when substring(p1.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  9767.                 when substring(p1.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  9768.                                 else (~convert(tinyint,coalesce(substring(p.columns, v.low, 1),0)))
  9769.                 end & v.high <> 0)            /* permission applies to this column */
  9770.     union all
  9771.     select    /*    Add rows for table owner */
  9772.         GRANTOR        = user_name(u.uid),
  9773.         GRANTEE        = user_name(o.uid),
  9774.         TABLE_CATALOG    = db_name(),
  9775.         TABLE_SCHEMA    = user_name(o.uid),
  9776.         TABLE_NAME    = o.name,
  9777.         COLUMN_NAME    = c.name,
  9778.         COLUMN_GUID    = convert(binary(16),null),
  9779.         COLUMN_PROPID    = convert(int,null),
  9780.         PRIVILEGE_TYPE    = convert(varchar(30),
  9781.                     case v.number
  9782.                     when 193 then 'SELECT'
  9783.                     when 195 then 'INSERT'
  9784.                     when 197 then 'UPDATE'
  9785.                     else 'REFERENCES'
  9786.                     end),
  9787.         IS_GRANTABLE    = convert(bit,1)    
  9788.     from 
  9789.         sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  9790.     where
  9791.         o.name = @table_name
  9792.     and     o.type in ('U','V','S')
  9793.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  9794.     and    (@grantee is null or @grantee = user_name(o.uid))
  9795.     and    c.id = o.id
  9796.     and     (@column_name is null or @column_name = c.name)
  9797.     and     u.suid = 1        /* grantor is dbo of database */
  9798.     and    (@grantor is null or @grantor = user_name(u.uid))
  9799.     and     v.type = 'P'        /* cross product to get all exposed privileges */
  9800.     and     v.number in (26,193,195,197)
  9801.     and     not exists (        /* exclude revoke'd privileges */
  9802.             select *
  9803.             from     sysprotects p1
  9804.             where    p1.protecttype = 206
  9805.             and     p1.action = v.number
  9806.             and     p1.id = o.id
  9807.             and     p1.uid = o.uid)
  9808.     order by 4, 5, 6, 9, 1, 2
  9809.     END
  9810. ELSE
  9811.     BEGIN
  9812.     insert into #spcprivsrowset1
  9813.     select
  9814.         GRANTOR        = user_name(p.grantor),
  9815.         GRANTEE        = user_name(u.uid),
  9816.         TABLE_CATALOG    = db_name(),
  9817.         TABLE_SCHEMA    = user_name(o.uid),
  9818.         TABLE_NAME    = o.name,
  9819.         COLUMN_NAME    = c.name,
  9820.         COLUMN_GUID    = convert(binary(16),null),
  9821.         COLUMN_PROPID    = convert(int,null),
  9822.         PRIVILEGE_TYPE    = convert(varchar(30),
  9823.                     case p.action
  9824.                     when 193 then 'SELECT'
  9825.                     when 195 then 'INSERT'
  9826.                     when 197 then 'UPDATE'
  9827.                     else 'REFERENCES'
  9828.                     end),
  9829.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  9830.     from 
  9831.         sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c
  9832.     where
  9833.         o.type in ('U','V','S')
  9834.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  9835.     and    c.id = o.id
  9836.     and     (@column_name is null or @column_name = c.name)
  9837.     and     c.id = p.id
  9838.     and    (@grantor is null or @grantor = user_name(p.grantor))
  9839.     and     case 
  9840.             when substring(p.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  9841.             when substring(p.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p.columns, v.low, 1))
  9842.             else (~convert(tinyint, coalesce(substring(p.columns, v.low, 1),0)))
  9843.             end & v.high <> 0            /* permission applies to this column */
  9844.     and     v.number <= (select count(*) from syscolumns where id = o.id)    /* ranges from 1 to # of columns in table */
  9845.     and     v.type = 'P'
  9846.     and     v.number = c.colid
  9847.     and    (@grantee is null or @grantee = user_name(u.uid))
  9848.             /* expand groups */
  9849.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  9850.     and     p.protecttype <> 206    /* only grant rows */
  9851.     and     p.action in (26,193,195,197)
  9852.     and     o.uid <> u.uid            /* no rows for owner */
  9853.     and     not exists (            /* exclude revoke'd privileges */
  9854.             select *
  9855.             from sysprotects p1
  9856.             where
  9857.                 p1.protecttype = 206
  9858.             and     p1.action = p.action
  9859.             and     p1.id = p.id
  9860.             and     p1.uid = u.uid
  9861.             and     case 
  9862.                 when substring(p1.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  9863.                 when substring(p1.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  9864.                                 else (~convert(tinyint,coalesce(substring(p.columns, v.low, 1),0)))
  9865.                 end & v.high <> 0)            /* permission applies to this column */
  9866.     union all
  9867.     select    /*    Add rows for table owner */
  9868.         GRANTOR        = user_name(u.uid),
  9869.         GRANTEE        = user_name(o.uid),
  9870.         TABLE_CATALOG    = db_name(),
  9871.         TABLE_SCHEMA    = user_name(o.uid),
  9872.         TABLE_NAME    = o.name,
  9873.         COLUMN_NAME    = c.name,
  9874.         COLUMN_GUID    = convert(binary(16),null),
  9875.         COLUMN_PROPID    = convert(int,null),
  9876.         PRIVILEGE_TYPE    = convert(varchar(30),
  9877.                     case v.number
  9878.                     when 193 then 'SELECT'
  9879.                     when 195 then 'INSERT'
  9880.                     when 197 then 'UPDATE'
  9881.                     else 'REFERENCES'
  9882.                     end),
  9883.         IS_GRANTABLE    = convert(bit,1)    
  9884.     from 
  9885.         sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  9886.     where
  9887.          o.type in ('U','V','S')
  9888.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  9889.     and    (@grantee is null or @grantee = user_name(o.uid))
  9890.     and    c.id = o.id
  9891.     and     (@column_name is null or @column_name = c.name)
  9892.     and     u.suid = 1        /* grantor is dbo of database */
  9893.     and    (@grantor is null or @grantor = user_name(u.uid))
  9894.     and     v.type = 'P'        /* cross product to get all exposed privileges */
  9895.     and     v.number in (26,193,195,197)
  9896.     and     not exists (        /* exclude revoke'd privileges */
  9897.             select *
  9898.             from     sysprotects p1
  9899.             where    p1.protecttype = 206
  9900.             and     p1.action = v.number
  9901.             and     p1.id = o.id
  9902.             and     p1.uid = o.uid)
  9903.     order by 4, 5, 6, 9, 1, 2
  9904.     END
  9905.  
  9906. exec @ret = sp_cursoropen @handle output, 'select * from #spcprivsrowset1',
  9907.     @scrollopt output, @ccopt output, @rows output
  9908.  
  9909. drop table #spcprivsrowset1
  9910.  
  9911. return isnull(@ret,0)
  9912. go
  9913. dump tran master with no_log
  9914. go
  9915. CREATE PROCEDURE sp_column_privileges_rowset;3
  9916. as
  9917.     select
  9918.         GRANTOR        = convert(sysname,' '),
  9919.         GRANTEE        = convert(sysname,' '),
  9920.         TABLE_CATALOG    = convert(sysname,' '),
  9921.         TABLE_SCHEMA    = convert(sysname,' '),
  9922.         TABLE_NAME    = convert(sysname,' '),
  9923.         COLUMN_NAME    = convert(sysname,' '),
  9924.         COLUMN_GUID    = convert(binary(16),null),
  9925.         COLUMN_PROPID    = convert(int,null),
  9926.         PRIVILEGE_TYPE    = convert(varchar(30),' '),
  9927.         IS_GRANTABLE    = convert(bit,1)
  9928.     where    1=0
  9929. go
  9930. dump tran master with no_log
  9931. go
  9932.  
  9933. if (charindex('7.00', @@version) = 0)
  9934. begin
  9935.     print ''
  9936.     print ''
  9937.     print 'Warning:'
  9938.     print 'you are installing the stored procedures '
  9939.     print 'on a pre 7.0 SQL Server.'
  9940.     print 'Ignore the following errors.'
  9941. end
  9942. else
  9943.     drop proc sp_column_privileges_rowset
  9944. go
  9945.  
  9946.  
  9947. /*    Procedure for 7.0 server */
  9948. CREATE PROCEDURE sp_column_privileges_rowset
  9949.     (
  9950.     @table_name         sysname,
  9951.     @table_schema        sysname = null,
  9952.     @column_name        sysname = null,
  9953.     @grantor        sysname = null,
  9954.     @grantee        sysname = null
  9955.     )
  9956. as
  9957.     select
  9958.         GRANTOR        = user_name(p.grantor),
  9959.         GRANTEE        = user_name(u.uid),
  9960.         TABLE_CATALOG    = db_name(),
  9961.         TABLE_SCHEMA    = user_name(o.uid),
  9962.         TABLE_NAME    = o.name,
  9963.         COLUMN_NAME    = c.name,
  9964.         COLUMN_GUID    = convert(uniqueidentifier,null),
  9965.         COLUMN_PROPID    = convert(int,null),
  9966.         PRIVILEGE_TYPE    = convert(nvarchar(30),
  9967.                     case p.action
  9968.                     when 193 then N'SELECT'
  9969.                     when 195 then N'INSERT'
  9970.                     when 197 then N'UPDATE'
  9971.                     else N'REFERENCES'
  9972.                     end),
  9973.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  9974.     from 
  9975.         sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c, sysmembers m
  9976.     where
  9977.         o.name = @table_name
  9978.     and     o.type in ('U','V','S')
  9979.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  9980.     and    c.id = o.id
  9981.     and     (@column_name is null or @column_name = c.name)
  9982.     and     c.id = p.id
  9983.     and    (@grantor is null or @grantor = user_name(p.grantor))
  9984.     and     case 
  9985.             when substring(p.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  9986.             when substring(p.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p.columns, v.low, 1))
  9987.             else (~convert(tinyint, coalesce(substring(p.columns, v.low, 1),0)))
  9988.             end & v.high <> 0            /* permission applies to this column */
  9989.     and     v.number <= (select count(*) from syscolumns where id = o.id)    /* ranges from 1 to # of columns in table */
  9990.     and     v.type = 'P'
  9991.     and     v.number = c.colid
  9992.     and    (@grantee is null or @grantee = user_name(u.uid))
  9993.         /* expand groups - AKUNDONE: only 1 level of group unrolling here. Need more?? */
  9994.     and     (u.uid > 0 and u.uid < 16384)
  9995.     and     ((p.uid = u.uid) or  (p.uid = m.groupuid and u.uid = m.memberuid))    
  9996.     and     p.protecttype <> 206    /* only grant rows */
  9997.     and     p.action in (26,193,195,197)
  9998.     and     o.uid <> u.uid            /* no rows for owner */
  9999.     and     not exists (            /* exclude revoke'd privileges */
  10000.             select *
  10001.             from sysprotects p1
  10002.             where
  10003.                 p1.protecttype = 206
  10004.             and     p1.action = p.action
  10005.             and     p1.id = p.id
  10006.             and     p1.uid = u.uid
  10007.             and     case 
  10008.                 when substring(p1.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  10009.                 when substring(p1.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  10010.                                 else (~convert(tinyint,coalesce(substring(p.columns, v.low, 1),0)))
  10011.                 end & v.high <> 0)            /* permission applies to this column */
  10012.     union all
  10013.     select    /*    Add rows for table owner */
  10014.         GRANTOR        = user_name(u.uid),
  10015.         GRANTEE        = user_name(o.uid),
  10016.         TABLE_CATALOG    = db_name(),
  10017.         TABLE_SCHEMA    = user_name(o.uid),
  10018.         TABLE_NAME    = o.name,
  10019.         COLUMN_NAME    = c.name,
  10020.         COLUMN_GUID    = convert(uniqueidentifier,null),
  10021.         COLUMN_PROPID    = convert(int,null),
  10022.         PRIVILEGE_TYPE    = convert(nvarchar(30),
  10023.                     case v.number
  10024.                     when 193 then N'SELECT'
  10025.                     when 195 then N'INSERT'
  10026.                     when 197 then N'UPDATE'
  10027.                     else N'REFERENCES'
  10028.                     end),
  10029.         IS_GRANTABLE    = convert(bit,1)    
  10030.     from 
  10031.         sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  10032.     where
  10033.         o.name = @table_name
  10034.     and     o.type in ('U','V','S')
  10035.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  10036.     and    (@grantee is null or @grantee = user_name(o.uid))
  10037.     and    c.id = o.id
  10038.     and     (@column_name is null or @column_name = c.name)
  10039.     and     u.suid = 1        /* grantor is dbo of database */
  10040.     and    (@grantor is null or @grantor = user_name(u.uid))
  10041.     and     v.type = 'P'        /* cross product to get all exposed privileges */
  10042.     and     v.number in (26,193,195,197)
  10043.     and     not exists (        /* exclude revoke'd privileges */
  10044.             select *
  10045.             from     sysprotects p1
  10046.             where    p1.protecttype = 206
  10047.             and     p1.action = v.number
  10048.             and     p1.id = o.id
  10049.             and     p1.uid = o.uid)
  10050.     order by 4, 5, 6, 9, 1, 2
  10051. go
  10052. dump tran master with no_log
  10053. go
  10054. CREATE PROCEDURE sp_column_privileges_rowset;2
  10055.     (
  10056.     @table_schema        sysname = null,
  10057.     @column_name        sysname = null,
  10058.     @grantor        sysname = null,
  10059.     @grantee        sysname = null
  10060.     )
  10061. as
  10062.     select
  10063.         GRANTOR        = user_name(p.grantor),
  10064.         GRANTEE        = user_name(u.uid),
  10065.         TABLE_CATALOG    = db_name(),
  10066.         TABLE_SCHEMA    = user_name(o.uid),
  10067.         TABLE_NAME    = o.name,
  10068.         COLUMN_NAME    = c.name,
  10069.         COLUMN_GUID    = convert(uniqueidentifier,null),
  10070.         COLUMN_PROPID    = convert(int,null),
  10071.         PRIVILEGE_TYPE    = convert(nvarchar(30),
  10072.                     case p.action
  10073.                     when 193 then N'SELECT'
  10074.                     when 195 then N'INSERT'
  10075.                     when 197 then N'UPDATE'
  10076.                     else N'REFERENCES'
  10077.                     end),
  10078.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  10079.     from 
  10080.         sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c, sysmembers m
  10081.     where
  10082.         o.type in ('U','V','S')
  10083.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  10084.     and    c.id = o.id
  10085.     and     (@column_name is null or @column_name = c.name)
  10086.     and     c.id = p.id
  10087.     and    (@grantor is null or @grantor = user_name(p.grantor))
  10088.     and     case 
  10089.             when substring(p.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  10090.             when substring(p.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p.columns, v.low, 1))
  10091.             else (~convert(tinyint, coalesce(substring(p.columns, v.low, 1),0)))
  10092.             end & v.high <> 0            /* permission applies to this column */
  10093.     and     v.number <= (select count(*) from syscolumns where id = o.id)    /* ranges from 1 to # of columns in table */
  10094.     and     v.type = 'P'
  10095.     and     v.number = c.colid
  10096.     and    (@grantee is null or @grantee = user_name(u.uid))
  10097.         /* expand groups - AKUNDONE: only 1 level of group unrolling here. Need more?? */
  10098.     and     (u.uid > 0 and u.uid < 16384)
  10099.     and     ((p.uid = u.uid) or  (p.uid = m.groupuid and u.uid = m.memberuid))    
  10100.     and     p.protecttype <> 206    /* only grant rows */
  10101.     and     p.action in (26,193,195,197)
  10102.     and     o.uid <> u.uid            /* no rows for owner */
  10103.     and     not exists (            /* exclude revoke'd privileges */
  10104.             select *
  10105.             from sysprotects p1
  10106.             where
  10107.                 p1.protecttype = 206
  10108.             and     p1.action = p.action
  10109.             and     p1.id = p.id
  10110.             and     p1.uid = u.uid
  10111.             and     case 
  10112.                 when substring(p1.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  10113.                 when substring(p1.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  10114.                                 else (~convert(tinyint,coalesce(substring(p.columns, v.low, 1),0)))
  10115.                 end & v.high <> 0)            /* permission applies to this column */
  10116.     union all
  10117.     select    /*    Add rows for table owner */
  10118.         GRANTOR        = user_name(u.uid),
  10119.         GRANTEE        = user_name(o.uid),
  10120.         TABLE_CATALOG    = db_name(),
  10121.         TABLE_SCHEMA    = user_name(o.uid),
  10122.         TABLE_NAME    = o.name,
  10123.         COLUMN_NAME    = c.name,
  10124.         COLUMN_GUID    = convert(uniqueidentifier,null),
  10125.         COLUMN_PROPID    = convert(int,null),
  10126.         PRIVILEGE_TYPE    = convert(nvarchar(30),
  10127.                     case v.number
  10128.                     when 193 then N'SELECT'
  10129.                     when 195 then N'INSERT'
  10130.                     when 197 then N'UPDATE'
  10131.                     else N'REFERENCES'
  10132.                     end),
  10133.         IS_GRANTABLE    = convert(bit,1)    
  10134.     from 
  10135.         sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  10136.     where
  10137.         o.type in ('U','V','S')
  10138.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  10139.     and    (@grantee is null or @grantee = user_name(o.uid))
  10140.     and    c.id = o.id
  10141.     and     (@column_name is null or @column_name = c.name)
  10142.     and     u.suid = 1        /* grantor is dbo of database */
  10143.     and    (@grantor is null or @grantor = user_name(u.uid))
  10144.     and     v.type = 'P'        /* cross product to get all exposed privileges */
  10145.     and     v.number in (26,193,195,197)
  10146.     and     not exists (        /* exclude revoke'd privileges */
  10147.             select *
  10148.             from     sysprotects p1
  10149.             where    p1.protecttype = 206
  10150.             and     p1.action = v.number
  10151.             and     p1.id = o.id
  10152.             and     p1.uid = o.uid)
  10153.     order by 4, 5, 6, 9, 1, 2
  10154. go
  10155. dump tran master with no_log
  10156. go
  10157. create procedure sp_column_privileges_rowset;5
  10158.     (
  10159.     @table_server        sysname,
  10160.     @table_catalog        sysname = null,
  10161.     @table_name        sysname = null,
  10162.     @table_schema        sysname = null,
  10163.     @column_name        sysname = null,
  10164.     @grantor        sysname = null,
  10165.     @grantee        sysname = null
  10166.     )
  10167. as
  10168.     select
  10169.         GRANTOR,
  10170.         GRANTEE,
  10171.         TABLE_CATALOG,
  10172.         TABLE_SCHEMA,
  10173.         TABLE_NAME,
  10174.         COLUMN_NAME,
  10175.         COLUMN_GUID,
  10176.         COLUMN_PROPID,    
  10177.         PRIVILEGE_TYPE,
  10178.         IS_GRANTABLE    
  10179.         from master.dbo.SYSREMOTE_COLUMN_PRIVILEGES <
  10180.                 @table_server,
  10181.                 @table_catalog,
  10182.                 @table_schema,
  10183.                 @table_name,
  10184.                 @column_name,
  10185.                 @grantor,
  10186.                 @grantee>
  10187.     order by 3, 4, 5, 6, 9, 1, 2
  10188. go
  10189.  
  10190. grant execute on sp_column_privileges_rowset to public
  10191. go
  10192.  
  10193. dump tran master with no_log
  10194. go
  10195. if (charindex('6.00', @@version) > 0)
  10196.     begin
  10197.     if (exists (select * from sysobjects
  10198.             where name = 'sp_column_privileges_rowset' and type = 'P '))
  10199.         begin
  10200.         drop procedure sp_column_privileges_rowset
  10201.         dump tran master with no_log
  10202.         end
  10203.     end
  10204. go
  10205.  
  10206.  
  10207. print ''
  10208. print 'creating sp_columns_rowset'
  10209. go
  10210.  
  10211. /*    Procedure for 6.0 and 6.50 server */
  10212. create procedure sp_columns_rowset
  10213.     (
  10214.        @table_name    varchar(255),
  10215.     @table_schema     varchar(255) = null,
  10216.     @column_name    varchar(255) = null
  10217.     )
  10218. as     
  10219.     select
  10220.         TABLE_CATALOG        = db_name(),
  10221.         TABLE_SCHEMA        = user_name(o.uid),
  10222.         TABLE_NAME        = o.name,
  10223.         COLUMN_NAME        = c.name,
  10224.         COLUMN_GUID        = convert(binary(16),null),
  10225.         COLUMN_PROPID        = convert(int,null),
  10226.         ORDINAL_POSITION    = convert(int,c.colid),
  10227.         COLUMN_HASDEFAULT    = convert(bit, case when m.text is null then 0 else 1 end),
  10228.         COLUMN_DEFAULT        = substring(m.text,2,datalength(m.text)-2),
  10229.         COLUMN_FLAGS        = convert(int,
  10230.                         case when d.is_long = 1 
  10231.                         then 0x82 /*DBCOLUMNFLAGS_ISLONG|DBCOLUMNFLAGS_MAYDEFER*/
  10232.                         else 0
  10233.                         end
  10234.                     |    case when d.fixlen is not null
  10235.                         then 0x10 /*DBCOLUMNFLAGS_ISFIXEDLENGTH*/
  10236.                         else 0
  10237.                         end
  10238.                     |    case when c.status&8 = 8
  10239.                         then 0x20 /*DBCOLUMNFLAGS_ISNULLABLE*/
  10240.                         else 0
  10241.                         end
  10242.                     |    case 
  10243.                         when d.type_name = 'timestamp'
  10244.                             then 0x200 /*DBCOLUMNFLAGS_ISROWVER*/
  10245.                         when (c.status&128) != 128
  10246.                             then 0x8 /*DBCOLUMNFLAGS_WRITEUNKNOWN*/ 
  10247.                         else 0 
  10248.                         end),
  10249.         IS_NULLABLE        = convert(bit,c.status&8),
  10250.         DATA_TYPE        = d.oledb_data_type,
  10251.         TYPE_GUID        = convert(binary(16),null),
  10252.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  10253.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  10254.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  10255.                         then coalesce(d.column_size,c.length)
  10256.                         else null 
  10257.                         end),
  10258.         CHARACTER_OCTET_LENGTH    = convert(int,
  10259.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  10260.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  10261.                         then coalesce(d.column_size,c.length)
  10262.                         else null 
  10263.                         end),
  10264.         NUMERIC_PRECISION    = convert(smallint,
  10265.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  10266.                             when d.fixed_prec_scale =1 then d.data_precision else null end),
  10267.         NUMERIC_SCALE        = convert(smallint, 
  10268.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  10269.         DATETIME_PRECISION    = convert(int,
  10270.                         case when d.oledb_data_type <> 135 /*DBTYPE_DBTIMESTAMP*/ then null
  10271.                             when data_precision = 23 then 3 else 0 end),
  10272.         CHARACTER_SET_CATALOG    = convert(sysname,
  10273.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  10274.                         then 'master' 
  10275.                         else null 
  10276.                         end),
  10277.         CHARACTER_SET_SCHEMA    = convert(sysname,
  10278.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  10279.                         then 'dbo' 
  10280.                         else null 
  10281.                         end),
  10282.         CHARACTER_SET_NAME    = convert(sysname,
  10283.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  10284.                         then a_cha.name 
  10285.                         else null 
  10286.                         end),
  10287.         COLLATION_CATALOG    = convert(sysname,
  10288.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  10289.                         then 'master' 
  10290.                         else null 
  10291.                         end),
  10292.         COLLATION_SCHEMA    = convert(sysname,
  10293.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  10294.                         then 'dbo' 
  10295.                         else null 
  10296.                         end),
  10297.         COLLATION_NAME        = convert(sysname,
  10298.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  10299.                         then b_cha.name 
  10300.                         else null 
  10301.                         end),
  10302.         DOMAIN_CATALOG        = case when t.usertype < 256 then null else db_name() end,
  10303.         DOMAIN_SCHEMA        = case when t.usertype < 256 then null else user_name(o.uid) end,
  10304.         DOMAIN_NAME        = case when t.usertype < 256 then null else t.name end,
  10305.         DESCRIPTION        = convert(varchar(1),null)    
  10306.     
  10307.     from
  10308.         syscolumns c,
  10309.         syscomments m,
  10310.         sysobjects o,
  10311.         master.dbo.spt_provider_types d,
  10312.         systypes t,
  10313.         sysusers u,
  10314.         master.dbo.sysconfigures    cfg,
  10315.         master.dbo.syscharsets        a_cha, /* charset/1001, not sortorder. */
  10316.         master.dbo.syscharsets        b_cha  /* sortorder/2001, not charset. */
  10317.     where
  10318.         o.name = @table_name
  10319.     and     o.type in ('U','V','S')
  10320.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  10321.     and     (
  10322.         suser_id() = 1     /* User is the System Administrator */
  10323.         or o.uid = user_id()     /* User created the object */
  10324.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  10325.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  10326.             from sysprotects p
  10327.             where p.id = o.id
  10328.             /* get rows for public,current user,user's group */
  10329.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  10330.             /* check for SELECT,EXECUTE privilege */
  10331.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  10332.             ) = 1    /* final magic...compare Grants      */
  10333.            )
  10334.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  10335.     and    (@column_name is null or @column_name = c.name)
  10336.     and     o.id = c.id
  10337.     and     t.type = d.ss_dtype
  10338.     and    (t.usertype != 80 or d.type_name='timestamp')
  10339.     and     c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  10340.     and     c.usertype = t.usertype
  10341.     and    c.cdefault *= m.id
  10342.     and    m.colid = 1
  10343.     and    cfg.comment = 'default sortorder ID' 
  10344.     and    a_cha.type = 1001 /* type is charset */
  10345.     and    b_cha.type = 2001 /* type is sortorder */
  10346.     and    a_cha.id = b_cha.csid
  10347.     and     b_cha.id = cfg.value
  10348.     order by 2, 3, 7
  10349. go
  10350. dump tran master with no_log
  10351. go
  10352. create procedure sp_columns_rowset;2
  10353.     (
  10354.     @table_schema     varchar(255) = null,
  10355.     @column_name    varchar(255) = null
  10356.     )
  10357. as     
  10358.     select
  10359.         TABLE_CATALOG        = db_name(),
  10360.         TABLE_SCHEMA        = user_name(o.uid),
  10361.         TABLE_NAME        = o.name,
  10362.         COLUMN_NAME        = c.name,
  10363.         COLUMN_GUID        = convert(binary(16),null),
  10364.         COLUMN_PROPID        = convert(int,null),
  10365.         ORDINAL_POSITION    = convert(int,c.colid),
  10366.         COLUMN_HASDEFAULT    = convert(bit, case when m.text is null then 0 else 1 end),
  10367.         COLUMN_DEFAULT        = substring(m.text,2,datalength(m.text)-2),
  10368.         COLUMN_FLAGS        = convert(int,
  10369.                         case when d.is_long = 1 
  10370.                         then 0x82 /*DBCOLUMNFLAGS_ISLONG|DBCOLUMNFLAGS_MAYDEFER*/
  10371.                         else 0
  10372.                         end
  10373.                     |    case when d.fixlen is not null
  10374.                         then 0x10 /*DBCOLUMNFLAGS_ISFIXEDLENGTH*/
  10375.                         else 0
  10376.                         end
  10377.                     |    case when c.status&8 = 8
  10378.                         then 0x20 /*DBCOLUMNFLAGS_ISNULLABLE*/
  10379.                         else 0
  10380.                         end
  10381.                     |    case 
  10382.                         when d.type_name = 'timestamp'
  10383.                             then 0x200 /*DBCOLUMNFLAGS_ISROWVER*/
  10384.                         when (c.status&128) != 128
  10385.                             then 0x8 /*DBCOLUMNFLAGS_WRITEUNKNOWN*/ 
  10386.                         else 0 
  10387.                         end),
  10388.         IS_NULLABLE        = convert(bit,c.status&8),
  10389.         DATA_TYPE        = d.oledb_data_type,
  10390.         TYPE_GUID        = convert(binary(16),null),
  10391.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  10392.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  10393.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  10394.                         then coalesce(d.column_size,c.length)
  10395.                         else null 
  10396.                         end),
  10397.         CHARACTER_OCTET_LENGTH    = convert(int,
  10398.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  10399.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  10400.                         then coalesce(d.column_size,c.length)
  10401.                         else null 
  10402.                         end),
  10403.         NUMERIC_PRECISION    = convert(smallint,
  10404.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  10405.                             when d.fixed_prec_scale =1 then d.data_precision else null end),
  10406.         NUMERIC_SCALE        = convert(smallint, 
  10407.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  10408.         DATETIME_PRECISION    = convert(int,
  10409.                         case when d.oledb_data_type <> 135 /*DBTYPE_DBTIMESTAMP*/ then null
  10410.                             when data_precision = 23 then 3 else 0 end),
  10411.         CHARACTER_SET_CATALOG    = convert(sysname,
  10412.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  10413.                         then 'master' 
  10414.                         else null 
  10415.                         end),
  10416.         CHARACTER_SET_SCHEMA    = convert(sysname,
  10417.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  10418.                         then 'dbo' 
  10419.                         else null 
  10420.                         end),
  10421.         CHARACTER_SET_NAME    = convert(sysname,
  10422.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  10423.                         then a_cha.name 
  10424.                         else null 
  10425.                         end),
  10426.         COLLATION_CATALOG    = convert(sysname,
  10427.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  10428.                         then 'master' 
  10429.                         else null 
  10430.                         end),
  10431.         COLLATION_SCHEMA    = convert(sysname,
  10432.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  10433.                         then 'dbo' 
  10434.                         else null 
  10435.                         end),
  10436.         COLLATION_NAME        = convert(sysname,
  10437.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  10438.                         then b_cha.name 
  10439.                         else null 
  10440.                         end),
  10441.         DOMAIN_CATALOG        = case when t.usertype < 256 then null else db_name() end,
  10442.         DOMAIN_SCHEMA        = case when t.usertype < 256 then null else user_name(o.uid) end,
  10443.         DOMAIN_NAME        = case when t.usertype < 256 then null else t.name end,
  10444.         DESCRIPTION        = convert(varchar(1),null)    
  10445.     
  10446.     from
  10447.         syscolumns c,
  10448.         syscomments m,
  10449.         sysobjects o,
  10450.         master.dbo.spt_provider_types d,
  10451.         systypes t,
  10452.         sysusers u,
  10453.         master.dbo.sysconfigures    cfg,
  10454.         master.dbo.syscharsets        a_cha, /* charset/1001, not sortorder. */
  10455.         master.dbo.syscharsets        b_cha /* sortorder/2001, not charset. */
  10456.     where
  10457.          o.type in ('U','V','S')
  10458.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  10459.     and     (
  10460.         suser_id() = 1     /* User is the System Administrator */
  10461.         or o.uid = user_id()     /* User created the object */
  10462.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  10463.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  10464.             from sysprotects p
  10465.             where p.id = o.id
  10466.             /* get rows for public,current user,user's group */
  10467.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  10468.             /* check for SELECT,EXECUTE privilege */
  10469.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  10470.             ) = 1    /* final magic...compare Grants      */
  10471.            )
  10472.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  10473.     and    (@column_name is null or @column_name = c.name)
  10474.     and     o.id = c.id
  10475.     and     t.type = d.ss_dtype
  10476.     and    (t.usertype != 80 or d.type_name='timestamp')
  10477.     and     c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  10478.     and     c.usertype = t.usertype
  10479.     and    c.cdefault *= m.id
  10480.     and     m.colid = 1
  10481.     and    cfg.comment = 'default sortorder ID' 
  10482.     and    a_cha.type = 1001 /* type is charset */
  10483.     and    b_cha.type = 2001 /* type is sortorder */
  10484.     and    a_cha.id = b_cha.csid
  10485.     and     b_cha.id = cfg.value
  10486.     order by 2, 3, 7
  10487. go
  10488. dump tran master with no_log
  10489. go
  10490.  
  10491. if (charindex('7.00', @@version) = 0)
  10492. begin
  10493.     print ''
  10494.     print ''
  10495.     print 'Warning:'
  10496.     print 'you are installing the stored procedures '
  10497.     print 'on a pre 7.0 SQL Server.'
  10498.     print 'Ignore the following errors.'
  10499. end
  10500. else
  10501.     drop proc sp_columns_rowset
  10502. go
  10503.  
  10504. /*    Procedure for 7.0 server */
  10505. create procedure sp_columns_rowset
  10506.     (
  10507.        @table_name    sysname,
  10508.     @table_schema     sysname = NULL,
  10509.     @column_name    sysname = NULL
  10510.     )
  10511. as     
  10512.     select
  10513.         TABLE_CATALOG        = db_name(),
  10514.         TABLE_SCHEMA        = user_name(o.uid),
  10515.         TABLE_NAME        = o.name,
  10516.         COLUMN_NAME        = c.name,
  10517.         COLUMN_GUID        = convert(uniqueidentifier,null),
  10518.         COLUMN_PROPID        = convert(int,null),
  10519.         ORDINAL_POSITION    = convert(int,c.colid),
  10520.         COLUMN_HASDEFAULT    = convert(bit, case when m.text is null then 0 else 1 end),
  10521.         COLUMN_DEFAULT        = convert(nvarchar(2000),
  10522.                         substring(m.text,2,datalength(m.text)/2-2)),
  10523.         COLUMN_FLAGS        = convert(int,
  10524.                         case when d.is_long = 1 
  10525.                         then 0x82 /*DBCOLUMNFLAGS_ISLONG|DBCOLUMNFLAGS_MAYDEFER*/
  10526.                         else 0
  10527.                         end
  10528.                     |    case when d.fixlen is not null
  10529.                         then 0x10 /*DBCOLUMNFLAGS_ISFIXEDLENGTH*/
  10530.                         else 0
  10531.                         end
  10532.                     |    case when ColumnProperty(c.id,c.name,'AllowsNull') = 1
  10533.                         then 0x20 /*DBCOLUMNFLAGS_ISNULLABLE*/
  10534.                         else 0
  10535.                         end
  10536.                     |    case 
  10537.                         when d.type_name = 'timestamp'
  10538.                             then 0x200 /*DBCOLUMNFLAGS_ISROWVER*/
  10539.                         when (c.status&128) != 128  and permissions(o.id,c.name)&2 = 2
  10540.                             then 0x4 /*DBCOLUMNFLAGS_WRITE*/ 
  10541.                         else 0 
  10542.                         end),
  10543.         IS_NULLABLE        = convert(bit,ColumnProperty(c.id,c.name,'AllowsNull')),
  10544.         DATA_TYPE        = d.oledb_data_type,
  10545.         TYPE_GUID        = convert(uniqueidentifier,null),
  10546.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  10547.                         case 
  10548.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  10549.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  10550.                         then coalesce(d.column_size,c.length)
  10551.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  10552.                         then coalesce(d.column_size,c.length/2)
  10553.                         else null 
  10554.                         end),
  10555.         CHARACTER_OCTET_LENGTH    = convert(int,
  10556.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  10557.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  10558.                         then coalesce(d.column_size,c.length)
  10559.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  10560.                         then coalesce(d.column_size*2,c.length)
  10561.                         else null 
  10562.                         end),
  10563.         NUMERIC_PRECISION    = convert(smallint,
  10564.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.xprec
  10565.                             when d.fixed_prec_scale =1 then d.data_precision else null end),
  10566.         NUMERIC_SCALE        = convert(smallint, 
  10567.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  10568.         DATETIME_PRECISION    = convert(int,
  10569.                         case when d.oledb_data_type <> 135 /*DBTYPE_DBTIMESTAMP*/ then null
  10570.                             when data_precision = 23 then 3 else 0 end),
  10571.         CHARACTER_SET_CATALOG    = convert(sysname,
  10572.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  10573.                         then N'master' 
  10574.                         else null 
  10575.                         end),
  10576.         CHARACTER_SET_SCHEMA    = convert(sysname,
  10577.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  10578.                         then N'dbo' 
  10579.                         else null 
  10580.                         end),
  10581.         CHARACTER_SET_NAME    = convert(sysname,
  10582.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  10583.                         then a_cha.name 
  10584.                         else null 
  10585.                         end),
  10586.         COLLATION_CATALOG    = convert(sysname,
  10587.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  10588.                         then N'master' 
  10589.                         else null 
  10590.                         end),
  10591.         COLLATION_SCHEMA    = convert(sysname,
  10592.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  10593.                         then N'dbo' 
  10594.                         else null 
  10595.                         end),
  10596.         COLLATION_NAME        = convert(sysname,
  10597.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  10598.                         then b_cha.name 
  10599.                         else null 
  10600.                         end),
  10601.         DOMAIN_CATALOG        = case when t.usertype < 256 then null else db_name() end,
  10602.         DOMAIN_SCHEMA        = case when t.usertype < 256 then null else user_name(o.uid) end,
  10603.         DOMAIN_NAME        = case when t.usertype < 256 then null else t.name end,
  10604.         DESCRIPTION        = convert(nvarchar(1),null)    
  10605.     
  10606.     from
  10607.         syscolumns c left join syscomments m on c.cdefault = m.id and m.colid = 1,
  10608.         sysobjects o,
  10609.         master.dbo.spt_provider_types d,
  10610.         systypes t,
  10611.         master.dbo.sysconfigures    cfg,
  10612.         master.dbo.syscharsets        a_cha, /* charset/1001, not sortorder. */
  10613.         master.dbo.syscharsets        b_cha /* sortorder/2001, not charset. */
  10614.     where
  10615.         permissions(o.id, c.name) <> 0
  10616.     and    o.name = @table_name
  10617.     and     o.type in ('U','V','S')
  10618.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  10619.     and    (@column_name is null or @column_name = c.name)
  10620.     and     o.id = c.id
  10621.     and     t.xtype = d.ss_dtype
  10622.     and     c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  10623.     and     c.xusertype = t.xusertype
  10624.     and    cfg.comment = 'default sortorder id' 
  10625.     and    a_cha.type = 1001 /* type is charset */
  10626.     and    b_cha.type = 2001 /* type is sortorder */
  10627.     and    a_cha.id = b_cha.csid
  10628.     and     b_cha.id = cfg.value
  10629.     order by 2, 3, 7
  10630. go
  10631. dump tran master with no_log
  10632. go
  10633. create procedure sp_columns_rowset;2
  10634.     (
  10635.     @table_schema     sysname = NULL,
  10636.     @column_name    sysname = NULL
  10637.     )
  10638. as     
  10639.     select
  10640.         TABLE_CATALOG        = db_name(),
  10641.         TABLE_SCHEMA        = user_name(o.uid),
  10642.         TABLE_NAME        = o.name,
  10643.         COLUMN_NAME        = c.name,
  10644.         COLUMN_GUID        = convert(uniqueidentifier,null),
  10645.         COLUMN_PROPID        = convert(int,null),
  10646.         ORDINAL_POSITION    = convert(int,c.colid),
  10647.         COLUMN_HASDEFAULT    = convert(bit, case when m.text is null then 0 else 1 end),
  10648.         COLUMN_DEFAULT        = convert(nvarchar(2000),
  10649.                         substring(m.text,2,datalength(m.text)/2-2)),
  10650.         COLUMN_FLAGS        = convert(int,
  10651.                         case when d.is_long = 1 
  10652.                         then 0x82 /*DBCOLUMNFLAGS_ISLONG|DBCOLUMNFLAGS_MAYDEFER*/
  10653.                         else 0
  10654.                         end
  10655.                     |    case when d.fixlen is not null
  10656.                         then 0x10 /*DBCOLUMNFLAGS_ISFIXEDLENGTH*/
  10657.                         else 0
  10658.                         end
  10659.                     |    case when ColumnProperty(c.id,c.name,'AllowsNull') = 1
  10660.                         then 0x20 /*DBCOLUMNFLAGS_ISNULLABLE*/
  10661.                         else 0
  10662.                         end
  10663.                     |    case 
  10664.                         when d.type_name = 'timestamp'
  10665.                             then 0x200 /*DBCOLUMNFLAGS_ISROWVER*/
  10666.                         when (c.status&128) != 128 and permissions(o.id,c.name)&2 = 2
  10667.                             then 0x4 /*DBCOLUMNFLAGS_WRITE*/ 
  10668.                         else 0 
  10669.                         end),
  10670.         IS_NULLABLE        = convert(bit,ColumnProperty(c.id,c.name,'AllowsNull')),
  10671.         DATA_TYPE        = d.oledb_data_type,
  10672.         TYPE_GUID        = convert(uniqueidentifier,null),
  10673.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  10674.                         case 
  10675.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  10676.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  10677.                         then coalesce(d.column_size,c.length)
  10678.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  10679.                         then coalesce(d.column_size,c.length/2)
  10680.                         else null 
  10681.                         end),
  10682.         CHARACTER_OCTET_LENGTH    = convert(int,
  10683.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  10684.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  10685.                         then coalesce(d.column_size,c.length)
  10686.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  10687.                         then coalesce(d.column_size*2,c.length)
  10688.                         else null 
  10689.                         end),
  10690.         NUMERIC_PRECISION    = convert(smallint,
  10691.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.xprec
  10692.                             when d.fixed_prec_scale =1 then d.data_precision else null end),
  10693.         NUMERIC_SCALE        = convert(smallint, 
  10694.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  10695.         DATETIME_PRECISION    = convert(int,
  10696.                         case when d.oledb_data_type <> 135 /*DBTYPE_DBTIMESTAMP*/ then null
  10697.                             when data_precision = 23 then 3 else 0 end),
  10698.         CHARACTER_SET_CATALOG    = convert(sysname,
  10699.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  10700.                         then N'master' 
  10701.                         else null 
  10702.                         end),
  10703.         CHARACTER_SET_SCHEMA    = convert(sysname,
  10704.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  10705.                         then N'dbo' 
  10706.                         else null 
  10707.                         end),
  10708.         CHARACTER_SET_NAME    = convert(sysname,
  10709.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  10710.                         then a_cha.name 
  10711.                         else null 
  10712.                         end),
  10713.         COLLATION_CATALOG    = convert(sysname,
  10714.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  10715.                         then N'master' 
  10716.                         else null 
  10717.                         end),
  10718.         COLLATION_SCHEMA    = convert(sysname,
  10719.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  10720.                         then N'dbo' 
  10721.                         else null 
  10722.                         end),
  10723.         COLLATION_NAME        = convert(sysname,
  10724.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  10725.                         then b_cha.name 
  10726.                         else null 
  10727.                         end),
  10728.         DOMAIN_CATALOG        = case when t.usertype < 256 then null else db_name() end,
  10729.         DOMAIN_SCHEMA        = case when t.usertype < 256 then null else user_name(o.uid) end,
  10730.         DOMAIN_NAME        = case when t.usertype < 256 then null else t.name end,
  10731.         DESCRIPTION        = convert(nvarchar(1),null)    
  10732.     
  10733.     from
  10734.         syscolumns c left join syscomments m on c.cdefault = m.id and m.colid = 1,
  10735.         sysobjects o,
  10736.         master.dbo.spt_provider_types d,
  10737.         systypes t,
  10738.         master.dbo.sysconfigures    cfg,
  10739.         master.dbo.syscharsets        a_cha, /* charset/1001, not sortorder.*/
  10740.         master.dbo.syscharsets        b_cha /* sortorder/2001, not charset.*/
  10741.     where
  10742.         permissions(o.id, c.name) <> 0
  10743.     and     o.type in ('U','V','S')
  10744.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  10745.     and    (@column_name is null or @column_name = c.name)
  10746.     and     o.id = c.id
  10747.     and     t.xtype = d.ss_dtype
  10748.     and     c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  10749.     and     c.xusertype = t.xusertype
  10750.     and    cfg.comment = 'default sortorder id' 
  10751.     and a_cha.type = 1001 /* type is charset */
  10752.     and b_cha.type = 2001 /* type is sortorder */
  10753.     and    a_cha.id = b_cha.csid
  10754.     and     b_cha.id = cfg.value
  10755.     order by 2, 3, 7
  10756. go
  10757. dump tran master with no_log
  10758. go
  10759. create procedure sp_columns_rowset;5
  10760.     (
  10761.     @table_server        sysname,
  10762.     @table_catalog        sysname = null,
  10763.     @table_name        sysname = null,
  10764.     @table_schema        sysname = null,
  10765.     @column_name        sysname = null
  10766.     )
  10767. as
  10768.     select
  10769.         TABLE_CATALOG,
  10770.         TABLE_SCHEMA,
  10771.         TABLE_NAME,
  10772.         COLUMN_NAME,
  10773.         COLUMN_GUID,
  10774.         COLUMN_PROPID,
  10775.         ORDINAL_POSITION,
  10776.         COLUMN_HASDEFAULT,
  10777.         COLUMN_DEFAULT,
  10778.         COLUMN_FLAGS,
  10779.         IS_NULLABLE,
  10780.         DATA_TYPE,
  10781.         TYPE_GUID,
  10782.         CHARACTER_MAXIMUM_LENGTH,
  10783.         CHARACTER_OCTET_LENGTH,
  10784.         NUMERIC_PRECISION,
  10785.         NUMERIC_SCALE,
  10786.         DATETIME_PRECISION,
  10787.         CHARACTER_SET_CATALOG,
  10788.         CHARACTER_SET_SCHEMA,
  10789.         CHARACTER_SET_NAME,
  10790.         COLLATION_CATALOG,
  10791.         COLLATION_SCHEMA,
  10792.         COLLATION_NAME,
  10793.         DOMAIN_CATALOG,
  10794.         DOMAIN_SCHEMA,
  10795.         DOMAIN_NAME,
  10796.         DESCRIPTION
  10797.     from master.dbo.SYSREMOTE_COLUMNS <
  10798.                 @table_server,
  10799.                 @table_catalog,
  10800.                 @table_schema,
  10801.                 @table_name,
  10802.                 @column_name > 
  10803.     order by 1, 2, 3, 7
  10804. go
  10805.  
  10806. grant execute on sp_columns_rowset to public
  10807. go
  10808.  
  10809. dump tran master with no_log
  10810. go
  10811. if (charindex('6.00', @@version) > 0)
  10812.     begin
  10813.     if (exists (select * from sysobjects
  10814.             where name = 'sp_columns_rowset' and type = 'P '))
  10815.         begin
  10816.         drop procedure sp_columns_rowset
  10817.         dump tran master with no_log
  10818.         end
  10819.     end
  10820. go
  10821.  
  10822.  
  10823. print ''
  10824. print 'creating sp_foreign_keys_rowset'
  10825. go
  10826.  
  10827. /*    Procedure for 6.0 and 6.5 server */
  10828. CREATE PROCEDURE sp_foreign_keys_rowset
  10829.     (
  10830.      @pk_table_name        varchar(255) = null,
  10831.     @pk_table_schema    varchar(255) = null,
  10832.     @pk_table_catalog    varchar(255) = null,
  10833.     @fk_table_name        varchar(255) = null,
  10834.     @fk_table_schema    varchar(255) = null,
  10835.     @fk_table_catalog    varchar(255) = null
  10836.     )
  10837. as
  10838. IF @pk_table_name is not null
  10839.     BEGIN
  10840.     select
  10841.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  10842.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  10843.         PK_TABLE_NAME         = o1.name,
  10844.         PK_COLUMN_NAME         = c1.name,
  10845.         PK_COLUMN_GUID        = convert(binary(16),null),
  10846.         PK_COLUMN_PROPID    = convert(int,null),
  10847.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  10848.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  10849.         FK_TABLE_NAME         = o2.name,
  10850.         FK_COLUMN_NAME         = c2.name,
  10851.         FK_COLUMN_GUID        = convert(binary(16),null),
  10852.         FK_COLUMN_PROPID    = convert(int,null),
  10853.         ORDINAL                = convert(int,1),
  10854.         UPDATE_RULE            = 'NO ACTION',
  10855.         DELETE_RULE         = 'NO ACTION',
  10856.         PK_NAME                = object_name(s.constid),
  10857.         FK_NAME                = object_name(r.constid),
  10858.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  10859.     from
  10860.         sysobjects o1, sysobjects o2,
  10861.         syscolumns c1, syscolumns c2,
  10862.         sysreferences r, sysconstraints s
  10863.     where    
  10864.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  10865.     and    o1.name = @pk_table_name
  10866.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10867.     and    o1.id = r.rkeyid
  10868.     and s.id = o1.id and (s.status & 0xf) = 1
  10869.     and    o1.id = c1.id
  10870.     and    c1.colid = r.rkey1
  10871.     and    r.fkeyid = o2.id
  10872.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  10873.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10874.     and    o2.id = c2.id    
  10875.     and    c2.colid = r.fkey1
  10876.     union all
  10877.     select
  10878.         db_name(r.rkeydbid),
  10879.         user_name(o1.uid),
  10880.         o1.name,
  10881.         c1.name,
  10882.         convert(binary(16),null),
  10883.         convert(int,null),
  10884.         db_name(r.fkeydbid),
  10885.         user_name(o2.uid),
  10886.         o2.name,
  10887.         c2.name,
  10888.         convert(binary(16),null),
  10889.         convert(int,null),
  10890.         convert(int,2),
  10891.         'NO ACTION',
  10892.         'NO ACTION',
  10893.         object_name(s.constid),
  10894.         object_name(r.constid),
  10895.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  10896.     from
  10897.         sysobjects o1, sysobjects o2,
  10898.         syscolumns c1, syscolumns c2,
  10899.         sysreferences r, sysconstraints s
  10900.     where    
  10901.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  10902.     and    o1.name = @pk_table_name
  10903.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10904.     and    o1.id = r.rkeyid
  10905.     and s.id = o1.id and (s.status & 0xf) = 1
  10906.      and    o1.id = c1.id
  10907.     and    c1.colid = r.rkey2
  10908.     and    r.keycnt >= 2
  10909.     and r.fkeyid = o2.id
  10910.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  10911.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10912.     and    o2.id = c2.id    
  10913.     and    c2.colid = r.fkey2
  10914.     union all
  10915.     select
  10916.         db_name(r.rkeydbid),
  10917.         user_name(o1.uid),
  10918.         o1.name,
  10919.         c1.name,
  10920.         convert(binary(16),null),
  10921.         convert(int,null),
  10922.         db_name(r.fkeydbid),
  10923.         user_name(o2.uid),
  10924.         o2.name,
  10925.         c2.name,
  10926.         convert(binary(16),null),
  10927.         convert(int,null),
  10928.         convert(int,3),
  10929.         'NO ACTION',
  10930.         'NO ACTION',
  10931.         object_name(s.constid),
  10932.         object_name(r.constid),
  10933.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/        
  10934.     from
  10935.         sysobjects o1, sysobjects o2,
  10936.         syscolumns c1, syscolumns c2,
  10937.         sysreferences r, sysconstraints s
  10938.     where    
  10939.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  10940.     and    o1.name = @pk_table_name
  10941.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10942.     and    o1.id = r.rkeyid
  10943.     and s.id = o1.id and (s.status & 0xf) = 1
  10944.     and    o1.id = c1.id
  10945.     and    c1.colid = r.rkey3
  10946.     and    r.keycnt >= 3
  10947.     and r.fkeyid = o2.id
  10948.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  10949.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10950.     and    o2.id = c2.id    
  10951.     and    c2.colid = r.fkey3
  10952.     union all
  10953.     select
  10954.         db_name(r.rkeydbid),
  10955.         user_name(o1.uid),
  10956.         o1.name,
  10957.         c1.name,
  10958.         convert(binary(16),null),
  10959.         convert(int,null),
  10960.         db_name(r.fkeydbid),
  10961.         user_name(o2.uid),
  10962.         o2.name,
  10963.         c2.name,
  10964.         convert(binary(16),null),
  10965.         convert(int,null),
  10966.         convert(int,4),
  10967.         'NO ACTION',
  10968.         'NO ACTION',
  10969.         object_name(s.constid),
  10970.         object_name(r.constid),
  10971.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  10972.     from
  10973.         sysobjects o1, sysobjects o2,
  10974.         syscolumns c1, syscolumns c2,
  10975.         sysreferences r, sysconstraints s
  10976.     where    
  10977.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  10978.     and    o1.name = @pk_table_name
  10979.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  10980.     and    o1.id = r.rkeyid
  10981.     and s.id = o1.id and (s.status & 0xf) = 1
  10982.     and    r.keycnt >= 4
  10983.     and    o1.id = c1.id
  10984.     and    c1.colid = r.rkey4
  10985.     and    r.fkeyid = o2.id
  10986.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  10987.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  10988.     and    o2.id = c2.id    
  10989.     and    c2.colid = r.fkey4
  10990.     union all
  10991.     select
  10992.         db_name(r.rkeydbid),
  10993.         user_name(o1.uid),
  10994.         o1.name,
  10995.         c1.name,
  10996.         convert(binary(16),null),
  10997.         convert(int,null),
  10998.         db_name(r.fkeydbid),
  10999.         user_name(o2.uid),
  11000.         o2.name,
  11001.         c2.name,
  11002.         convert(binary(16),null),
  11003.         convert(int,null),
  11004.         convert(int,5),
  11005.         'NO ACTION',
  11006.         'NO ACTION',
  11007.         object_name(s.constid),
  11008.         object_name(r.constid),
  11009.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  11010.     from
  11011.         sysobjects o1, sysobjects o2,
  11012.         syscolumns c1, syscolumns c2,
  11013.         sysreferences r, sysconstraints s
  11014.     where    
  11015.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11016.     and    o1.name = @pk_table_name
  11017.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11018.     and    o1.id = r.rkeyid
  11019.     and s.id = o1.id and (s.status & 0xf) = 1
  11020.     and    r.keycnt >= 5
  11021.     and    o1.id = c1.id
  11022.     and    c1.colid = r.rkey5
  11023.     and r.fkeyid = o2.id
  11024.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  11025.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11026.     and    o2.id = c2.id    
  11027.     and    c2.colid = r.fkey5
  11028.     union all
  11029.     select
  11030.         db_name(r.rkeydbid),
  11031.         user_name(o1.uid),
  11032.         o1.name,
  11033.         c1.name,
  11034.         convert(binary(16),null),
  11035.         convert(int,null),
  11036.         db_name(r.fkeydbid),
  11037.         user_name(o2.uid),
  11038.         o2.name,
  11039.         c2.name,
  11040.         convert(binary(16),null),
  11041.         convert(int,null),
  11042.         convert(int,6),
  11043.         'NO ACTION',
  11044.         'NO ACTION',
  11045.         object_name(s.constid),
  11046.         object_name(r.constid),
  11047.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  11048.     from
  11049.         sysobjects o1, sysobjects o2,
  11050.         syscolumns c1, syscolumns c2,
  11051.         sysreferences r, sysconstraints s
  11052.     where    
  11053.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11054.     and    o1.name = @pk_table_name
  11055.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11056.     and    o1.id = r.rkeyid
  11057.     and s.id = o1.id and (s.status & 0xf) = 1
  11058.     and    r.keycnt >= 6
  11059.     and    o1.id = c1.id
  11060.     and    c1.colid = r.rkey6
  11061.     and r.fkeyid = o2.id
  11062.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  11063.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11064.     and    o2.id = c2.id    
  11065.     and    c2.colid = r.fkey6
  11066.     union all
  11067.     select
  11068.         db_name(r.rkeydbid),
  11069.         user_name(o1.uid),
  11070.         o1.name,
  11071.         c1.name,
  11072.         convert(binary(16),null),
  11073.         convert(int,null),
  11074.         db_name(r.fkeydbid),
  11075.         user_name(o2.uid),
  11076.         o2.name,
  11077.         c2.name,
  11078.         convert(binary(16),null),
  11079.         convert(int,null),
  11080.         convert(int,7),
  11081.         'NO ACTION',
  11082.         'NO ACTION',
  11083.         object_name(s.constid),
  11084.         object_name(r.constid),
  11085.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  11086.     from
  11087.         sysobjects o1, sysobjects o2,
  11088.         syscolumns c1, syscolumns c2,
  11089.         sysreferences r, sysconstraints s
  11090.     where    
  11091.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11092.     and    o1.name = @pk_table_name
  11093.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11094.     and    o1.id = r.rkeyid
  11095.     and s.id = o1.id and (s.status & 0xf) = 1
  11096.     and    r.keycnt >= 7
  11097.     and    o1.id = c1.id
  11098.     and    c1.colid = r.rkey7
  11099.     and r.fkeyid = o2.id
  11100.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  11101.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11102.     and    o2.id = c2.id    
  11103.     and    c2.colid = r.fkey7
  11104.     union all
  11105.     select
  11106.         db_name(r.rkeydbid),
  11107.         user_name(o1.uid),
  11108.         o1.name,
  11109.         c1.name,
  11110.         convert(binary(16),null),
  11111.         convert(int,null),
  11112.         db_name(r.fkeydbid),
  11113.         user_name(o2.uid),
  11114.         o2.name,
  11115.         c2.name,
  11116.         convert(binary(16),null),
  11117.         convert(int,null),
  11118.         convert(int,8),
  11119.         'NO ACTION',
  11120.         'NO ACTION',
  11121.         object_name(s.constid),
  11122.         object_name(r.constid),
  11123.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  11124.     from
  11125.         sysobjects o1, sysobjects o2,
  11126.         syscolumns c1, syscolumns c2,
  11127.         sysreferences r, sysconstraints s
  11128.     where    
  11129.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11130.     and    o1.name = @pk_table_name
  11131.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11132.     and    o1.id = r.rkeyid
  11133.     and s.id = o1.id and (s.status & 0xf) = 1
  11134.     and    r.keycnt >= 8
  11135.     and    o1.id = c1.id
  11136.     and    c1.colid = r.rkey8
  11137.     and r.fkeyid = o2.id
  11138.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  11139.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11140.     and    o2.id = c2.id    
  11141.     and    c2.colid = r.fkey8
  11142.     union all
  11143.     select
  11144.         db_name(r.rkeydbid),
  11145.         user_name(o1.uid),
  11146.         o1.name,
  11147.         c1.name,
  11148.         convert(binary(16),null),
  11149.         convert(int,null),
  11150.         db_name(r.fkeydbid),
  11151.         user_name(o2.uid),
  11152.         o2.name,
  11153.         c2.name,
  11154.         convert(binary(16),null),
  11155.         convert(int,null),
  11156.         convert(int,9),
  11157.         'NO ACTION',
  11158.         'NO ACTION',
  11159.         object_name(s.constid),
  11160.         object_name(r.constid),
  11161.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  11162.     from
  11163.         sysobjects o1, sysobjects o2,
  11164.         syscolumns c1, syscolumns c2,
  11165.         sysreferences r, sysconstraints s
  11166.     where    
  11167.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11168.     and    o1.name = @pk_table_name
  11169.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11170.     and    o1.id = r.rkeyid
  11171.     and s.id = o1.id and (s.status & 0xf) = 1
  11172.     and    r.keycnt >= 9
  11173.     and    o1.id = c1.id
  11174.     and    c1.colid = r.rkey9
  11175.     and r.fkeyid = o2.id
  11176.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  11177.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11178.     and    o2.id = c2.id    
  11179.     and    c2.colid = r.fkey9
  11180.     union all
  11181.     select
  11182.         db_name(r.rkeydbid),
  11183.         user_name(o1.uid),
  11184.         o1.name,
  11185.         c1.name,
  11186.         convert(binary(16),null),
  11187.         convert(int,null),
  11188.         db_name(r.fkeydbid),
  11189.         user_name(o2.uid),
  11190.         o2.name,
  11191.         c2.name,
  11192.         convert(binary(16),null),
  11193.         convert(int,null),
  11194.         convert(int,10),
  11195.         'NO ACTION',
  11196.         'NO ACTION',
  11197.         object_name(s.constid),
  11198.         object_name(r.constid),
  11199.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/    
  11200.     from
  11201.         sysobjects o1, sysobjects o2,
  11202.         syscolumns c1, syscolumns c2,
  11203.         sysreferences r, sysconstraints s
  11204.     where    
  11205.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11206.     and    o1.name = @pk_table_name
  11207.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11208.     and    o1.id = r.rkeyid
  11209.     and s.id = o1.id and (s.status & 0xf) = 1
  11210.     and    r.keycnt >= 10
  11211.     and    o1.id = c1.id
  11212.     and    c1.colid = r.rkey10
  11213.     and r.fkeyid = o2.id
  11214.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  11215.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11216.     and    o2.id = c2.id    
  11217.     and    c2.colid = r.fkey10
  11218.     union all
  11219.     select
  11220.         db_name(r.rkeydbid),
  11221.         user_name(o1.uid),
  11222.         o1.name,
  11223.         c1.name,
  11224.         convert(binary(16),null),
  11225.         convert(int,null),
  11226.         db_name(r.fkeydbid),
  11227.         user_name(o2.uid),
  11228.         o2.name,
  11229.         c2.name,
  11230.         convert(binary(16),null),
  11231.         convert(int,null),
  11232.         convert(int,11),
  11233.         'NO ACTION',
  11234.         'NO ACTION',
  11235.         object_name(s.constid),
  11236.         object_name(r.constid),
  11237.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/        
  11238.     from
  11239.         sysobjects o1, sysobjects o2,
  11240.         syscolumns c1, syscolumns c2,
  11241.         sysreferences r, sysconstraints s
  11242.     where    
  11243.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11244.     and    o1.name = @pk_table_name
  11245.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11246.     and    o1.id = r.rkeyid
  11247.     and s.id = o1.id and (s.status & 0xf) = 1
  11248.     and    r.keycnt >= 11
  11249.     and    o1.id = c1.id
  11250.     and    c1.colid = r.rkey11
  11251.     and r.fkeyid = o2.id
  11252.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  11253.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11254.     and    o2.id = c2.id    
  11255.     and    c2.colid = r.fkey11
  11256.     union all
  11257.     select
  11258.         db_name(r.rkeydbid),
  11259.         user_name(o1.uid),
  11260.         o1.name,
  11261.         c1.name,
  11262.         convert(binary(16),null),
  11263.         convert(int,null),
  11264.         db_name(r.fkeydbid),
  11265.         user_name(o2.uid),
  11266.         o2.name,
  11267.         c2.name,
  11268.         convert(binary(16),null),
  11269.         convert(int,null),
  11270.         convert(int,12),
  11271.         'NO ACTION',
  11272.         'NO ACTION',
  11273.         object_name(s.constid),
  11274.         object_name(r.constid),
  11275.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  11276.     from
  11277.         sysobjects o1, sysobjects o2,
  11278.         syscolumns c1, syscolumns c2,
  11279.         sysreferences r, sysconstraints s
  11280.     where    
  11281.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11282.     and    o1.name = @pk_table_name
  11283.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11284.     and    o1.id = r.rkeyid
  11285.     and s.id = o1.id and (s.status & 0xf) = 1
  11286.     and    r.keycnt >= 12
  11287.     and    o1.id = c1.id
  11288.     and    c1.colid = r.rkey12
  11289.     and r.fkeyid = o2.id
  11290.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  11291.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11292.     and    o2.id = c2.id    
  11293.     and    c2.colid = r.fkey12
  11294.     union all
  11295.     select
  11296.         db_name(r.rkeydbid),
  11297.         user_name(o1.uid),
  11298.         o1.name,
  11299.         c1.name,
  11300.         convert(binary(16),null),
  11301.         convert(int,null),
  11302.         db_name(r.fkeydbid),
  11303.         user_name(o2.uid),
  11304.         o2.name,
  11305.         c2.name,
  11306.         convert(binary(16),null),
  11307.         convert(int,null),
  11308.         convert(int,13),
  11309.         'NO ACTION',
  11310.         'NO ACTION',
  11311.         object_name(s.constid),
  11312.         object_name(r.constid),
  11313.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  11314.     from
  11315.         sysobjects o1, sysobjects o2,
  11316.         syscolumns c1, syscolumns c2,
  11317.         sysreferences r, sysconstraints s
  11318.     where    
  11319.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11320.     and    o1.name = @pk_table_name
  11321.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11322.     and    o1.id = r.rkeyid
  11323.     and s.id = o1.id and (s.status & 0xf) = 1
  11324.     and    r.keycnt >= 13
  11325.     and    o1.id = c1.id
  11326.     and    c1.colid = r.rkey13
  11327.     and r.fkeyid = o2.id
  11328.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  11329.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11330.     and    o2.id = c2.id    
  11331.     and    c2.colid = r.fkey13
  11332.     union all
  11333.     select
  11334.         db_name(r.rkeydbid),
  11335.         user_name(o1.uid),
  11336.         o1.name,
  11337.         c1.name,
  11338.         convert(binary(16),null),
  11339.         convert(int,null),
  11340.         db_name(r.fkeydbid),
  11341.         user_name(o2.uid),
  11342.         o2.name,
  11343.         c2.name,
  11344.         convert(binary(16),null),
  11345.         convert(int,null),
  11346.         convert(int,14),
  11347.         'NO ACTION',
  11348.         'NO ACTION',
  11349.         object_name(s.constid),
  11350.         object_name(r.constid),
  11351.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  11352.     from
  11353.         sysobjects o1, sysobjects o2,
  11354.         syscolumns c1, syscolumns c2,
  11355.         sysreferences r, sysconstraints s
  11356.     where    
  11357.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11358.     and    o1.name = @pk_table_name
  11359.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11360.     and    o1.id = r.rkeyid
  11361.     and s.id = o1.id and (s.status & 0xf) = 1
  11362.     and    r.keycnt >= 14
  11363.     and    o1.id = c1.id
  11364.     and    c1.colid = r.rkey14
  11365.     and r.fkeyid = o2.id
  11366.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  11367.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11368.     and    o2.id = c2.id    
  11369.     and    c2.colid = r.fkey14
  11370.     union all
  11371.     select
  11372.         db_name(r.rkeydbid),
  11373.         user_name(o1.uid),
  11374.         o1.name,
  11375.         c1.name,
  11376.         convert(binary(16),null),
  11377.         convert(int,null),
  11378.         db_name(r.fkeydbid),
  11379.         user_name(o2.uid),
  11380.         o2.name,
  11381.         c2.name,
  11382.         convert(binary(16),null),
  11383.         convert(int,null),
  11384.         convert(int,15),
  11385.         'NO ACTION',
  11386.         'NO ACTION',
  11387.         object_name(s.constid),
  11388.         object_name(r.constid),
  11389.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  11390.     from
  11391.         sysobjects o1, sysobjects o2,
  11392.         syscolumns c1, syscolumns c2,
  11393.         sysreferences r, sysconstraints s
  11394.     where    
  11395.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11396.     and    o1.name = @pk_table_name
  11397.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11398.     and    o1.id = r.rkeyid
  11399.     and s.id = o1.id and (s.status & 0xf) = 1
  11400.     and    r.keycnt >= 15
  11401.     and    o1.id = c1.id
  11402.     and    c1.colid = r.rkey15
  11403.     and r.fkeyid = o2.id
  11404.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  11405.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11406.     and    o2.id = c2.id    
  11407.     and    c2.colid = r.fkey15
  11408.     union all
  11409.     select
  11410.         db_name(r.rkeydbid),
  11411.         user_name(o1.uid),
  11412.         o1.name,
  11413.         c1.name,
  11414.         convert(binary(16),null),
  11415.         convert(int,null),
  11416.         db_name(r.fkeydbid),
  11417.         user_name(o2.uid),
  11418.         o2.name,
  11419.         c2.name,
  11420.         convert(binary(16),null),
  11421.         convert(int,null),
  11422.         convert(int,16),
  11423.         'NO ACTION',
  11424.         'NO ACTION',
  11425.         object_name(s.constid),
  11426.         object_name(r.constid),
  11427.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  11428.     from
  11429.         sysobjects o1, sysobjects o2,
  11430.         syscolumns c1, syscolumns c2,
  11431.         sysreferences r, sysconstraints s
  11432.     where    
  11433.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  11434.     and    o1.name = @pk_table_name
  11435.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11436.     and    o1.id = r.rkeyid
  11437.     and s.id = o1.id and (s.status & 0xf) = 1
  11438.     and    r.keycnt >= 16
  11439.     and    o1.id = c1.id
  11440.     and    c1.colid = r.rkey16
  11441.     and r.fkeyid = o2.id
  11442.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  11443.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11444.     and    o2.id = c2.id    
  11445.     and    c2.colid = r.fkey16
  11446.     order by 8,9,2,3,13
  11447.     END
  11448. ELSE IF @fk_table_name is not null
  11449.     BEGIN
  11450.     select
  11451.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  11452.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  11453.         PK_TABLE_NAME         = o1.name,
  11454.         PK_COLUMN_NAME         = c1.name,
  11455.         PK_COLUMN_GUID        = convert(binary(16),null),
  11456.         PK_COLUMN_PROPID    = convert(int,null),
  11457.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  11458.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  11459.         FK_TABLE_NAME         = o2.name,
  11460.         FK_COLUMN_NAME         = c2.name,
  11461.         FK_COLUMN_GUID        = convert(binary(16),null),
  11462.         FK_COLUMN_PROPID    = convert(int,null),
  11463.         ORDINAL                = convert(int,1),
  11464.         UPDATE_RULE            = 'NO ACTION',
  11465.         DELETE_RULE         = 'NO ACTION',
  11466.         PK_NAME                = object_name(s.constid),
  11467.         FK_NAME                = object_name(r.constid),
  11468.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/        
  11469.     from
  11470.         sysobjects o1, sysobjects o2,
  11471.         syscolumns c1, syscolumns c2,
  11472.         sysreferences r, sysconstraints s
  11473.     where    
  11474.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  11475.     and    o2.name = @fk_table_name
  11476.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11477.     and    o2.id = r.fkeyid
  11478.     and    o2.id = c2.id
  11479.     and    c2.colid = r.fkey1
  11480.     and r.rkeyid = o1.id
  11481.     and s.id = o1.id and (s.status & 0xf) = 1
  11482.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  11483.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11484.     and    o1.id = c1.id    
  11485.     and    c1.colid = r.rkey1
  11486.     union    all
  11487.     select
  11488.         db_name(r.rkeydbid),
  11489.         user_name(o1.uid),
  11490.         o1.name,
  11491.         c1.name,
  11492.         convert(binary(16),null),
  11493.         convert(int,null),
  11494.         db_name(r.fkeydbid),
  11495.         user_name(o2.uid),
  11496.         o2.name,
  11497.         c2.name,
  11498.         convert(binary(16),null),
  11499.         convert(int,null),
  11500.         convert(int,2),
  11501.         'NO ACTION',
  11502.         'NO ACTION',
  11503.         object_name(s.constid),
  11504.         object_name(r.constid),
  11505.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/        
  11506.     from
  11507.         sysobjects o1, sysobjects o2,
  11508.         syscolumns c1, syscolumns c2,
  11509.         sysreferences r, sysconstraints s
  11510.     where    
  11511.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  11512.     and    o2.name = @fk_table_name
  11513.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11514.     and    o2.id = r.fkeyid
  11515.     and    r.keycnt >= 2
  11516.     and    o2.id = c2.id
  11517.     and    c2.colid = r.fkey2
  11518.     and r.rkeyid = o1.id
  11519.     and s.id = o1.id and (s.status & 0xf) = 1
  11520.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  11521.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11522.     and    o1.id = c1.id    
  11523.     and    c1.colid = r.rkey2
  11524.     union    all
  11525.     select
  11526.         db_name(r.rkeydbid),
  11527.         user_name(o1.uid),
  11528.         o1.name,
  11529.         c1.name,
  11530.         convert(binary(16),null),
  11531.         convert(int,null),
  11532.         db_name(r.fkeydbid),
  11533.         user_name(o2.uid),
  11534.         o2.name,
  11535.         c2.name,
  11536.         convert(binary(16),null),
  11537.         convert(int,null),
  11538.         convert(int,3),
  11539.         'NO ACTION',
  11540.         'NO ACTION',
  11541.         object_name(s.constid),
  11542.         object_name(r.constid),
  11543.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  11544.     from
  11545.         sysobjects o1, sysobjects o2,
  11546.         syscolumns c1, syscolumns c2,
  11547.         sysreferences r, sysconstraints s
  11548.     where    
  11549.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  11550.     and    o2.name = @fk_table_name
  11551.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11552.     and    o2.id = r.fkeyid
  11553.     and    r.keycnt >= 3
  11554.     and    o2.id = c2.id
  11555.     and    c2.colid = r.fkey3
  11556.     and r.rkeyid = o1.id
  11557.     and s.id = o1.id and (s.status & 0xf) = 1
  11558.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  11559.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11560.     and    o1.id = c1.id    
  11561.     and    c1.colid = r.rkey3
  11562.     union    all
  11563.     select
  11564.         db_name(r.rkeydbid),
  11565.         user_name(o1.uid),
  11566.         o1.name,
  11567.         c1.name,
  11568.         convert(binary(16),null),
  11569.         convert(int,null),
  11570.         db_name(r.fkeydbid),
  11571.         user_name(o2.uid),
  11572.         o2.name,
  11573.         c2.name,
  11574.         convert(binary(16),null),
  11575.         convert(int,null),
  11576.         convert(int,4),
  11577.         'NO ACTION',
  11578.         'NO ACTION',
  11579.         object_name(s.constid),
  11580.         object_name(r.constid),
  11581.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/        
  11582.     from
  11583.         sysobjects o1, sysobjects o2,
  11584.         syscolumns c1, syscolumns c2,
  11585.         sysreferences r, sysconstraints s
  11586.     where    
  11587.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  11588.     and    o2.name = @fk_table_name
  11589.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11590.     and    o2.id = r.fkeyid
  11591.     and    r.keycnt >= 4
  11592.     and    o2.id = c2.id
  11593.     and    c2.colid = r.fkey4
  11594.     and r.rkeyid = o1.id
  11595.     and s.id = o1.id and (s.status & 0xf) = 1
  11596.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  11597.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11598.     and    o1.id = c1.id    
  11599.     and    c1.colid = r.rkey4
  11600.     union    all
  11601.     select
  11602.         db_name(r.rkeydbid),
  11603.         user_name(o1.uid),
  11604.         o1.name,
  11605.         c1.name,
  11606.         convert(binary(16),null),
  11607.         convert(int,null),
  11608.         db_name(r.fkeydbid),
  11609.         user_name(o2.uid),
  11610.         o2.name,
  11611.         c2.name,
  11612.         convert(binary(16),null),
  11613.         convert(int,null),
  11614.         convert(int,5),
  11615.         'NO ACTION',
  11616.         'NO ACTION',
  11617.         object_name(s.constid),
  11618.         object_name(r.constid),
  11619.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  11620.     from
  11621.         sysobjects o1, sysobjects o2,
  11622.         syscolumns c1, syscolumns c2,
  11623.         sysreferences r, sysconstraints s
  11624.     where    
  11625.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  11626.     and    o2.name = @fk_table_name
  11627.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11628.     and    o2.id = r.fkeyid
  11629.     and    r.keycnt >= 5
  11630.     and    o2.id = c2.id
  11631.     and    c2.colid = r.fkey5
  11632.     and r.rkeyid = o1.id
  11633.     and s.id = o1.id and (s.status & 0xf) = 1
  11634.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  11635.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11636.     and    o1.id = c1.id    
  11637.     and    c1.colid = r.rkey5
  11638.     union    all
  11639.     select
  11640.         db_name(r.rkeydbid),
  11641.         user_name(o1.uid),
  11642.         o1.name,
  11643.         c1.name,
  11644.         convert(binary(16),null),
  11645.         convert(int,null),
  11646.         db_name(r.fkeydbid),
  11647.         user_name(o2.uid),
  11648.         o2.name,
  11649.         c2.name,
  11650.         convert(binary(16),null),
  11651.         convert(int,null),
  11652.         convert(int,6),
  11653.         'NO ACTION',
  11654.         'NO ACTION',
  11655.         object_name(s.constid),
  11656.         object_name(r.constid),
  11657.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/        
  11658.     from
  11659.         sysobjects o1, sysobjects o2,
  11660.         syscolumns c1, syscolumns c2,
  11661.         sysreferences r, sysconstraints s
  11662.     where    
  11663.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  11664.     and    o2.name = @fk_table_name
  11665.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11666.     and    o2.id = r.fkeyid
  11667.     and    r.keycnt >= 6
  11668.     and    o2.id = c2.id
  11669.     and    c2.colid = r.fkey6
  11670.     and r.rkeyid = o1.id
  11671.     and s.id = o1.id and (s.status & 0xf) = 1
  11672.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  11673.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11674.     and    o1.id = c1.id    
  11675.     and    c1.colid = r.rkey6
  11676.     union    all
  11677.     select
  11678.         db_name(r.rkeydbid),
  11679.         user_name(o1.uid),
  11680.         o1.name,
  11681.         c1.name,
  11682.         convert(binary(16),null),
  11683.         convert(int,null),
  11684.         db_name(r.fkeydbid),
  11685.         user_name(o2.uid),
  11686.         o2.name,
  11687.         c2.name,
  11688.         convert(binary(16),null),
  11689.         convert(int,null),
  11690.         convert(int,7),
  11691.         'NO ACTION',
  11692.         'NO ACTION',
  11693.         object_name(s.constid),
  11694.         object_name(r.constid),
  11695.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  11696.     from
  11697.         sysobjects o1, sysobjects o2,
  11698.         syscolumns c1, syscolumns c2,
  11699.         sysreferences r, sysconstraints s
  11700.     where    
  11701.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  11702.     and    o2.name = @fk_table_name
  11703.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11704.     and    o2.id = r.fkeyid
  11705.     and    r.keycnt >= 7
  11706.     and    o2.id = c2.id
  11707.     and    c2.colid = r.fkey7
  11708.     and    r.rkeyid = o1.id
  11709.     and s.id = o1.id and (s.status & 0xf) = 1
  11710.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  11711.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11712.     and    o1.id = c1.id    
  11713.     and    c1.colid = r.rkey7
  11714.     union    all
  11715.     select
  11716.         db_name(r.rkeydbid),
  11717.         user_name(o1.uid),
  11718.         o1.name,
  11719.         c1.name,
  11720.         convert(binary(16),null),
  11721.         convert(int,null),
  11722.         db_name(r.fkeydbid),
  11723.         user_name(o2.uid),
  11724.         o2.name,
  11725.         c2.name,
  11726.         convert(binary(16),null),
  11727.         convert(int,null),
  11728.         convert(int,8),
  11729.         'NO ACTION',
  11730.         'NO ACTION',
  11731.         object_name(s.constid),
  11732.         object_name(r.constid),
  11733.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  11734.     from
  11735.         sysobjects o1, sysobjects o2,
  11736.         syscolumns c1, syscolumns c2,
  11737.         sysreferences r, sysconstraints s
  11738.     where    
  11739.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  11740.     and    o2.name = @fk_table_name
  11741.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11742.     and    o2.id = r.fkeyid
  11743.     and    r.keycnt >= 8
  11744.     and    o2.id = c2.id
  11745.     and    c2.colid = r.fkey8
  11746.     and r.rkeyid = o1.id
  11747.     and s.id = o1.id and (s.status & 0xf) = 1
  11748.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  11749.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11750.     and    o1.id = c1.id    
  11751.     and    c1.colid = r.rkey8
  11752.     union    all
  11753.     select
  11754.         db_name(r.rkeydbid),
  11755.         user_name(o1.uid),
  11756.         o1.name,
  11757.         c1.name,
  11758.         convert(binary(16),null),
  11759.         convert(int,null),
  11760.         db_name(r.fkeydbid),
  11761.         user_name(o2.uid),
  11762.         o2.name,
  11763.         c2.name,
  11764.         convert(binary(16),null),
  11765.         convert(int,null),
  11766.         convert(int,9),
  11767.         'NO ACTION',
  11768.         'NO ACTION',
  11769.         object_name(s.constid),
  11770.         object_name(r.constid),
  11771.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  11772.     from
  11773.         sysobjects o1, sysobjects o2,
  11774.         syscolumns c1, syscolumns c2,
  11775.         sysreferences r, sysconstraints s
  11776.     where    
  11777.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  11778.     and    o2.name = @fk_table_name
  11779.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11780.     and    o2.id = r.fkeyid
  11781.     and    r.keycnt >= 9
  11782.     and    o2.id = c2.id
  11783.     and    c2.colid = r.fkey9
  11784.     and r.rkeyid = o1.id
  11785.     and s.id = o1.id and (s.status & 0xf) = 1
  11786.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  11787.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11788.     and    o1.id = c1.id    
  11789.     and    c1.colid = r.rkey9
  11790.     union    all
  11791.     select
  11792.         db_name(r.rkeydbid),
  11793.         user_name(o1.uid),
  11794.         o1.name,
  11795.         c1.name,
  11796.         convert(binary(16),null),
  11797.         convert(int,null),
  11798.         db_name(r.fkeydbid),
  11799.         user_name(o2.uid),
  11800.         o2.name,
  11801.         c2.name,
  11802.         convert(binary(16),null),
  11803.         convert(int,null),
  11804.         convert(int,10),
  11805.         'NO ACTION',
  11806.         'NO ACTION',
  11807.         object_name(s.constid),
  11808.         object_name(r.constid),
  11809.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  11810.     from
  11811.         sysobjects o1, sysobjects o2,
  11812.         syscolumns c1, syscolumns c2,
  11813.         sysreferences r, sysconstraints s
  11814.     where    
  11815.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  11816.     and    o2.name = @fk_table_name
  11817.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11818.     and    o2.id = r.fkeyid
  11819.     and    r.keycnt >= 10
  11820.     and    o2.id = c2.id
  11821.     and    c2.colid = r.fkey10
  11822.     and r.rkeyid = o1.id
  11823.     and s.id = o1.id and (s.status & 0xf) = 1
  11824.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  11825.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11826.     and    o1.id = c1.id    
  11827.     and    c1.colid = r.rkey10
  11828.     union    all
  11829.     select
  11830.         db_name(r.rkeydbid),
  11831.         user_name(o1.uid),
  11832.         o1.name,
  11833.         c1.name,
  11834.         convert(binary(16),null),
  11835.         convert(int,null),
  11836.         db_name(r.fkeydbid),
  11837.         user_name(o2.uid),
  11838.         o2.name,
  11839.         c2.name,
  11840.         convert(binary(16),null),
  11841.         convert(int,null),
  11842.         convert(int,11),
  11843.         'NO ACTION',
  11844.         'NO ACTION',
  11845.         object_name(s.constid),
  11846.         object_name(r.constid),
  11847.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  11848.     from
  11849.         sysobjects o1, sysobjects o2,
  11850.         syscolumns c1, syscolumns c2,
  11851.         sysreferences r, sysconstraints s
  11852.     where    
  11853.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  11854.     and    o2.name = @fk_table_name
  11855.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11856.     and    o2.id = r.fkeyid
  11857.     and    r.keycnt >= 11
  11858.     and    o2.id = c2.id
  11859.     and    c2.colid = r.fkey11
  11860.     and r.rkeyid = o1.id
  11861.     and s.id = o1.id and (s.status & 0xf) = 1
  11862.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  11863.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11864.     and    o1.id = c1.id    
  11865.     and    c1.colid = r.rkey11
  11866.     union    all
  11867.     select
  11868.         db_name(r.rkeydbid),
  11869.         user_name(o1.uid),
  11870.         o1.name,
  11871.         c1.name,
  11872.         convert(binary(16),null),
  11873.         convert(int,null),
  11874.         db_name(r.fkeydbid),
  11875.         user_name(o2.uid),
  11876.         o2.name,
  11877.         c2.name,
  11878.         convert(binary(16),null),
  11879.         convert(int,null),
  11880.         convert(int,12),
  11881.         'NO ACTION',
  11882.         'NO ACTION',
  11883.         object_name(s.constid),
  11884.         object_name(r.constid),
  11885.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  11886.     from
  11887.         sysobjects o1, sysobjects o2,
  11888.         syscolumns c1, syscolumns c2,
  11889.         sysreferences r, sysconstraints s
  11890.     where    
  11891.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  11892.     and    o2.name = @fk_table_name
  11893.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11894.     and    o2.id = r.fkeyid
  11895.     and    r.keycnt >= 12
  11896.     and    o2.id = c2.id
  11897.     and    c2.colid = r.fkey12
  11898.     and r.rkeyid = o1.id
  11899.     and s.id = o1.id and (s.status & 0xf) = 1
  11900.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  11901.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11902.     and    o1.id = c1.id    
  11903.     and    c1.colid = r.rkey12
  11904.     union    all
  11905.     select
  11906.         db_name(r.rkeydbid),
  11907.         user_name(o1.uid),
  11908.         o1.name,
  11909.         c1.name,
  11910.         convert(binary(16),null),
  11911.         convert(int,null),
  11912.         db_name(r.fkeydbid),
  11913.         user_name(o2.uid),
  11914.         o2.name,
  11915.         c2.name,
  11916.         convert(binary(16),null),
  11917.         convert(int,null),
  11918.         convert(int,13),
  11919.         'NO ACTION',
  11920.         'NO ACTION',
  11921.         object_name(s.constid),
  11922.         object_name(r.constid),
  11923.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  11924.     from
  11925.         sysobjects o1, sysobjects o2,
  11926.         syscolumns c1, syscolumns c2,
  11927.         sysreferences r, sysconstraints s
  11928.     where    
  11929.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  11930.     and    o2.name = @fk_table_name
  11931.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11932.     and    o2.id = r.fkeyid
  11933.     and    r.keycnt >= 13
  11934.     and    o2.id = c2.id
  11935.     and    c2.colid = r.fkey13
  11936.     and r.rkeyid = o1.id
  11937.     and s.id = o1.id and (s.status & 0xf) = 1
  11938.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  11939.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11940.     and    o1.id = c1.id    
  11941.     and    c1.colid = r.rkey13
  11942.     union    all
  11943.     select
  11944.         db_name(r.rkeydbid),
  11945.         user_name(o1.uid),
  11946.         o1.name,
  11947.         c1.name,
  11948.         convert(binary(16),null),
  11949.         convert(int,null),
  11950.         db_name(r.fkeydbid),
  11951.         user_name(o2.uid),
  11952.         o2.name,
  11953.         c2.name,
  11954.         convert(binary(16),null),
  11955.         convert(int,null),
  11956.         convert(int,14),
  11957.         'NO ACTION',
  11958.         'NO ACTION',
  11959.         object_name(s.constid),
  11960.         object_name(r.constid),
  11961.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  11962.     from
  11963.         sysobjects o1, sysobjects o2,
  11964.         syscolumns c1, syscolumns c2,
  11965.         sysreferences r, sysconstraints s
  11966.     where    
  11967.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  11968.     and    o2.name = @fk_table_name
  11969.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  11970.     and    o2.id = r.fkeyid
  11971.     and    r.keycnt >= 14
  11972.     and    o2.id = c2.id
  11973.     and    c2.colid = r.fkey14
  11974.     and    r.rkeyid = o1.id
  11975.     and s.id = o1.id and (s.status & 0xf) = 1
  11976.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  11977.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  11978.     and    o1.id = c1.id    
  11979.     and    c1.colid = r.rkey14
  11980.     union    all
  11981.     select
  11982.         db_name(r.rkeydbid),
  11983.         user_name(o1.uid),
  11984.         o1.name,
  11985.         c1.name,
  11986.         convert(binary(16),null),
  11987.         convert(int,null),
  11988.         db_name(r.fkeydbid),
  11989.         user_name(o2.uid),
  11990.         o2.name,
  11991.         c2.name,
  11992.         convert(binary(16),null),
  11993.         convert(int,null),
  11994.         convert(int,15),
  11995.         'NO ACTION',
  11996.         'NO ACTION',
  11997.         object_name(s.constid),
  11998.         object_name(r.constid),
  11999.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12000.     from
  12001.         sysobjects o1, sysobjects o2,
  12002.         syscolumns c1, syscolumns c2,
  12003.         sysreferences r, sysconstraints s
  12004.     where    
  12005.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12006.     and    o2.name = @fk_table_name
  12007.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12008.     and    o2.id = r.fkeyid
  12009.     and    r.keycnt >= 15
  12010.     and    o2.id = c2.id
  12011.     and    c2.colid = r.fkey15
  12012.     and r.rkeyid = o1.id
  12013.     and s.id = o1.id and (s.status & 0xf) = 1
  12014.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  12015.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12016.     and    o1.id = c1.id    
  12017.     and    c1.colid = r.rkey15
  12018.     union    all
  12019.     select
  12020.         db_name(r.rkeydbid),
  12021.         user_name(o1.uid),
  12022.         o1.name,
  12023.         c1.name,
  12024.         convert(binary(16),null),
  12025.         convert(int,null),
  12026.         db_name(r.fkeydbid),
  12027.         user_name(o2.uid),
  12028.         o2.name,
  12029.         c2.name,
  12030.         convert(binary(16),null),
  12031.         convert(int,null),
  12032.         convert(int,16),
  12033.         'NO ACTION',
  12034.         'NO ACTION',
  12035.         object_name(s.constid),
  12036.         object_name(r.constid),
  12037.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12038.     from
  12039.         sysobjects o1, sysobjects o2,
  12040.         syscolumns c1, syscolumns c2,
  12041.         sysreferences r, sysconstraints s
  12042.     where    
  12043.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12044.     and    o2.name = @fk_table_name
  12045.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12046.     and    o2.id = r.fkeyid
  12047.     and    r.keycnt >= 16
  12048.     and    o2.id = c2.id
  12049.     and    c2.colid = r.fkey16
  12050.     and r.rkeyid = o1.id
  12051.     and s.id = o1.id and (s.status & 0xf) = 1
  12052.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  12053.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12054.     and    o1.id = c1.id    
  12055.     and    c1.colid = r.rkey16
  12056.     order by 8,9,2,3,13
  12057.     END
  12058. ELSE
  12059.     BEGIN
  12060.     select
  12061.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  12062.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  12063.         PK_TABLE_NAME         = o1.name,
  12064.         PK_COLUMN_NAME         = c1.name,
  12065.         PK_COLUMN_GUID        = convert(binary(16),null),
  12066.         PK_COLUMN_PROPID    = convert(int,null),
  12067.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  12068.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  12069.         FK_TABLE_NAME         = o2.name,
  12070.         FK_COLUMN_NAME         = c2.name,
  12071.         FK_COLUMN_GUID        = convert(binary(16),null),
  12072.         FK_COLUMN_PROPID    = convert(int,null),
  12073.         ORDINAL                = convert(int,1),
  12074.         UPDATE_RULE            = 'NO ACTION',
  12075.         DELETE_RULE         = 'NO ACTION',
  12076.         PK_NAME                = object_name(s.constid),
  12077.         FK_NAME                = object_name(r.constid),
  12078.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12079.     from
  12080.         sysobjects o1, sysobjects o2,
  12081.         syscolumns c1, syscolumns c2,
  12082.         sysreferences r, sysconstraints s
  12083.     where    
  12084.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12085.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12086.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12087.     and    o1.id = r.rkeyid
  12088.     and s.id = o1.id and (s.status & 0xf) = 1
  12089.     and    o1.id = c1.id
  12090.     and    c1.colid = r.rkey1
  12091.     and r.fkeyid = o2.id
  12092.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12093.     and    o2.id = c2.id    
  12094.     and    c2.colid = r.fkey1
  12095.     union all
  12096.     select
  12097.         db_name(r.rkeydbid),
  12098.         user_name(o1.uid),
  12099.         o1.name,
  12100.         c1.name,
  12101.         convert(binary(16),null),
  12102.         convert(int,null),
  12103.         db_name(r.fkeydbid),
  12104.         user_name(o2.uid),
  12105.         o2.name,
  12106.         c2.name,
  12107.         convert(binary(16),null),
  12108.         convert(int,null),
  12109.         convert(int,2),
  12110.         'NO ACTION',
  12111.         'NO ACTION',
  12112.         object_name(s.constid),
  12113.         object_name(r.constid),
  12114.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12115.     from
  12116.         sysobjects o1, sysobjects o2,
  12117.         syscolumns c1, syscolumns c2,
  12118.         sysreferences r, sysconstraints s
  12119.     where    
  12120.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12121.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12122.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12123.     and    o1.id = r.rkeyid
  12124.     and s.id = o1.id and (s.status & 0xf) = 1
  12125.     and    r.keycnt >= 2
  12126.     and    o1.id = c1.id
  12127.     and    c1.colid = r.rkey2
  12128.     and r.fkeyid = o2.id
  12129.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12130.     and    o2.id = c2.id    
  12131.     and    c2.colid = r.fkey2
  12132.     union all
  12133.     select
  12134.         db_name(r.rkeydbid),
  12135.         user_name(o1.uid),
  12136.         o1.name,
  12137.         c1.name,
  12138.         convert(binary(16),null),
  12139.         convert(int,null),
  12140.         db_name(r.fkeydbid),
  12141.         user_name(o2.uid),
  12142.         o2.name,
  12143.         c2.name,
  12144.         convert(binary(16),null),
  12145.         convert(int,null),
  12146.         convert(int,3),
  12147.         'NO ACTION',
  12148.         'NO ACTION',
  12149.         object_name(s.constid),
  12150.         object_name(r.constid),
  12151.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12152.     from
  12153.         sysobjects o1, sysobjects o2,
  12154.         syscolumns c1, syscolumns c2,
  12155.         sysreferences r, sysconstraints s
  12156.     where    
  12157.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12158.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12159.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12160.     and    o1.id = r.rkeyid
  12161.     and s.id = o1.id and (s.status & 0xf) = 1
  12162.     and    r.keycnt >= 3
  12163.     and    o1.id = c1.id
  12164.     and    c1.colid = r.rkey3
  12165.     and r.fkeyid = o2.id
  12166.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12167.     and    o2.id = c2.id    
  12168.     and    c2.colid = r.fkey3
  12169.     union all
  12170.     select
  12171.         db_name(r.rkeydbid),
  12172.         user_name(o1.uid),
  12173.         o1.name,
  12174.         c1.name,
  12175.         convert(binary(16),null),
  12176.         convert(int,null),
  12177.         db_name(r.fkeydbid),
  12178.         user_name(o2.uid),
  12179.         o2.name,
  12180.         c2.name,
  12181.         convert(binary(16),null),
  12182.         convert(int,null),
  12183.         convert(int,4),
  12184.         'NO ACTION',
  12185.         'NO ACTION',
  12186.         object_name(s.constid),
  12187.         object_name(r.constid),
  12188.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12189.     from
  12190.         sysobjects o1, sysobjects o2,
  12191.         syscolumns c1, syscolumns c2,
  12192.         sysreferences r, sysconstraints s
  12193.     where    
  12194.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12195.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12196.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12197.     and    o1.id = r.rkeyid
  12198.     and s.id = o1.id and (s.status & 0xf) = 1
  12199.     and    r.keycnt >= 4
  12200.     and    o1.id = c1.id
  12201.     and    c1.colid = r.rkey4
  12202.     and r.fkeyid = o2.id
  12203.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12204.     and    o2.id = c2.id    
  12205.     and    c2.colid = r.fkey4
  12206.     union all
  12207.     select
  12208.         db_name(r.rkeydbid),
  12209.         user_name(o1.uid),
  12210.         o1.name,
  12211.         c1.name,
  12212.         convert(binary(16),null),
  12213.         convert(int,null),
  12214.         db_name(r.fkeydbid),
  12215.         user_name(o2.uid),
  12216.         o2.name,
  12217.         c2.name,
  12218.         convert(binary(16),null),
  12219.         convert(int,null),
  12220.         convert(int,5),
  12221.         'NO ACTION',
  12222.         'NO ACTION',
  12223.         object_name(s.constid),
  12224.         object_name(r.constid),
  12225.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12226.     from
  12227.         sysobjects o1, sysobjects o2,
  12228.         syscolumns c1, syscolumns c2,
  12229.         sysreferences r, sysconstraints s
  12230.     where    
  12231.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12232.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12233.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12234.     and    o1.id = r.rkeyid
  12235.     and s.id = o1.id and (s.status & 0xf) = 1
  12236.     and    r.keycnt >= 5
  12237.     and    o1.id = c1.id
  12238.     and    c1.colid = r.rkey5
  12239.     and r.fkeyid = o2.id
  12240.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12241.     and    o2.id = c2.id    
  12242.     and    c2.colid = r.fkey5
  12243.     union all
  12244.     select
  12245.         db_name(r.rkeydbid),
  12246.         user_name(o1.uid),
  12247.         o1.name,
  12248.         c1.name,
  12249.         convert(binary(16),null),
  12250.         convert(int,null),
  12251.         db_name(r.fkeydbid),
  12252.         user_name(o2.uid),
  12253.         o2.name,
  12254.         c2.name,
  12255.         convert(binary(16),null),
  12256.         convert(int,null),
  12257.         convert(int,6),
  12258.         'NO ACTION',
  12259.         'NO ACTION',
  12260.         object_name(s.constid),
  12261.         object_name(r.constid),
  12262.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12263.     from
  12264.         sysobjects o1, sysobjects o2,
  12265.         syscolumns c1, syscolumns c2,
  12266.         sysreferences r, sysconstraints s
  12267.     where    
  12268.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12269.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12270.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12271.     and    o1.id = r.rkeyid
  12272.     and s.id = o1.id and (s.status & 0xf) = 1
  12273.     and    r.keycnt >= 6
  12274.     and    o1.id = c1.id
  12275.     and    c1.colid = r.rkey6
  12276.     and r.fkeyid = o2.id
  12277.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12278.     and    o2.id = c2.id    
  12279.     and    c2.colid = r.fkey6
  12280.     union all
  12281.     select
  12282.         db_name(r.rkeydbid),
  12283.         user_name(o1.uid),
  12284.         o1.name,
  12285.         c1.name,
  12286.         convert(binary(16),null),
  12287.         convert(int,null),
  12288.         db_name(r.fkeydbid),
  12289.         user_name(o2.uid),
  12290.         o2.name,
  12291.         c2.name,
  12292.         convert(binary(16),null),
  12293.         convert(int,null),
  12294.         convert(int,7),
  12295.         'NO ACTION',
  12296.         'NO ACTION',
  12297.         object_name(s.constid),
  12298.         object_name(r.constid),
  12299.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12300.     from
  12301.         sysobjects o1, sysobjects o2,
  12302.         syscolumns c1, syscolumns c2,
  12303.         sysreferences r, sysconstraints s
  12304.     where    
  12305.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12306.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12307.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12308.     and    o1.id = r.rkeyid
  12309.     and s.id = o1.id and (s.status & 0xf) = 1
  12310.     and    r.keycnt >= 7
  12311.     and    o1.id = c1.id
  12312.     and    c1.colid = r.rkey7
  12313.     and r.fkeyid = o2.id
  12314.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12315.     and    o2.id = c2.id    
  12316.     and    c2.colid = r.fkey7
  12317.     union all
  12318.     select
  12319.         db_name(r.rkeydbid),
  12320.         user_name(o1.uid),
  12321.         o1.name,
  12322.         c1.name,
  12323.         convert(binary(16),null),
  12324.         convert(int,null),
  12325.         db_name(r.fkeydbid),
  12326.         user_name(o2.uid),
  12327.         o2.name,
  12328.         c2.name,
  12329.         convert(binary(16),null),
  12330.         convert(int,null),
  12331.         convert(int,8),
  12332.         'NO ACTION',
  12333.         'NO ACTION',
  12334.         object_name(s.constid),
  12335.         object_name(r.constid),
  12336.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12337.     from
  12338.         sysobjects o1, sysobjects o2,
  12339.         syscolumns c1, syscolumns c2,
  12340.         sysreferences r, sysconstraints s
  12341.     where    
  12342.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12343.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12344.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12345.     and    o1.id = r.rkeyid
  12346.     and s.id = o1.id and (s.status & 0xf) = 1
  12347.     and    r.keycnt >= 8
  12348.     and    o1.id = c1.id
  12349.     and    c1.colid = r.rkey8
  12350.     and r.fkeyid = o2.id
  12351.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12352.     and    o2.id = c2.id    
  12353.     and    c2.colid = r.fkey8
  12354.     union all
  12355.     select
  12356.         db_name(r.rkeydbid),
  12357.         user_name(o1.uid),
  12358.         o1.name,
  12359.         c1.name,
  12360.         convert(binary(16),null),
  12361.         convert(int,null),
  12362.         db_name(r.fkeydbid),
  12363.         user_name(o2.uid),
  12364.         o2.name,
  12365.         c2.name,
  12366.         convert(binary(16),null),
  12367.         convert(int,null),
  12368.         convert(int,9),
  12369.         'NO ACTION',
  12370.         'NO ACTION',
  12371.         object_name(s.constid),
  12372.         object_name(r.constid),
  12373.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12374.     from
  12375.         sysobjects o1, sysobjects o2,
  12376.         syscolumns c1, syscolumns c2,
  12377.         sysreferences r, sysconstraints s
  12378.     where    
  12379.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12380.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12381.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12382.     and    o1.id = r.rkeyid
  12383.     and s.id = o1.id and (s.status & 0xf) = 1
  12384.     and    r.keycnt >= 9
  12385.     and    o1.id = c1.id
  12386.     and    c1.colid = r.rkey9
  12387.     and r.fkeyid = o2.id
  12388.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12389.     and    o2.id = c2.id    
  12390.     and    c2.colid = r.fkey9
  12391.     union all
  12392.     select
  12393.         db_name(r.rkeydbid),
  12394.         user_name(o1.uid),
  12395.         o1.name,
  12396.         c1.name,
  12397.         convert(binary(16),null),
  12398.         convert(int,null),
  12399.         db_name(r.fkeydbid),
  12400.         user_name(o2.uid),
  12401.         o2.name,
  12402.         c2.name,
  12403.         convert(binary(16),null),
  12404.         convert(int,null),
  12405.         convert(int,10),
  12406.         'NO ACTION',
  12407.         'NO ACTION',
  12408.         object_name(s.constid),
  12409.         object_name(r.constid),
  12410.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12411.     from
  12412.         sysobjects o1, sysobjects o2,
  12413.         syscolumns c1, syscolumns c2,
  12414.         sysreferences r, sysconstraints s
  12415.     where    
  12416.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12417.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12418.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12419.     and    o1.id = r.rkeyid
  12420.     and s.id = o1.id and (s.status & 0xf) = 1
  12421.     and    r.keycnt >= 10
  12422.     and    o1.id = c1.id
  12423.     and    c1.colid = r.rkey10
  12424.     and r.fkeyid = o2.id
  12425.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12426.     and    o2.id = c2.id    
  12427.     and    c2.colid = r.fkey10
  12428.     union all
  12429.     select
  12430.         db_name(r.rkeydbid),
  12431.         user_name(o1.uid),
  12432.         o1.name,
  12433.         c1.name,
  12434.         convert(binary(16),null),
  12435.         convert(int,null),
  12436.         db_name(r.fkeydbid),
  12437.         user_name(o2.uid),
  12438.         o2.name,
  12439.         c2.name,
  12440.         convert(binary(16),null),
  12441.         convert(int,null),
  12442.         convert(int,11),
  12443.         'NO ACTION',
  12444.         'NO ACTION',
  12445.         object_name(s.constid),
  12446.         object_name(r.constid),
  12447.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12448.     from
  12449.         sysobjects o1, sysobjects o2,
  12450.         syscolumns c1, syscolumns c2,
  12451.         sysreferences r, sysconstraints s
  12452.     where    
  12453.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12454.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12455.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12456.     and    o1.id = r.rkeyid
  12457.     and s.id = o1.id and (s.status & 0xf) = 1
  12458.     and    r.keycnt >= 11
  12459.     and    o1.id = c1.id
  12460.     and    c1.colid = r.rkey11
  12461.     and r.fkeyid = o2.id
  12462.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12463.     and    o2.id = c2.id    
  12464.     and    c2.colid = r.fkey11
  12465.     union all
  12466.     select
  12467.         db_name(r.rkeydbid),
  12468.         user_name(o1.uid),
  12469.         o1.name,
  12470.         c1.name,
  12471.         convert(binary(16),null),
  12472.         convert(int,null),
  12473.         db_name(r.fkeydbid),
  12474.         user_name(o2.uid),
  12475.         o2.name,
  12476.         c2.name,
  12477.         convert(binary(16),null),
  12478.         convert(int,null),
  12479.         convert(int,12),
  12480.         'NO ACTION',
  12481.         'NO ACTION',
  12482.         object_name(s.constid),
  12483.         object_name(r.constid),
  12484.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12485.     from
  12486.         sysobjects o1, sysobjects o2,
  12487.         syscolumns c1, syscolumns c2,
  12488.         sysreferences r, sysconstraints s
  12489.     where    
  12490.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12491.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12492.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12493.     and    o1.id = r.rkeyid
  12494.     and s.id = o1.id and (s.status & 0xf) = 1
  12495.     and    r.keycnt >= 12
  12496.     and    o1.id = c1.id
  12497.     and    c1.colid = r.rkey12
  12498.     and r.fkeyid = o2.id
  12499.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12500.     and    o2.id = c2.id    
  12501.     and    c2.colid = r.fkey12
  12502.     union all
  12503.     select
  12504.         db_name(r.rkeydbid),
  12505.         user_name(o1.uid),
  12506.         o1.name,
  12507.         c1.name,
  12508.         convert(binary(16),null),
  12509.         convert(int,null),
  12510.         db_name(r.fkeydbid),
  12511.         user_name(o2.uid),
  12512.         o2.name,
  12513.         c2.name,
  12514.         convert(binary(16),null),
  12515.         convert(int,null),
  12516.         convert(int,13),
  12517.         'NO ACTION',
  12518.         'NO ACTION',
  12519.         object_name(s.constid),
  12520.         object_name(r.constid),
  12521.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12522.     from
  12523.         sysobjects o1, sysobjects o2,
  12524.         syscolumns c1, syscolumns c2,
  12525.         sysreferences r, sysconstraints s
  12526.     where    
  12527.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12528.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12529.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12530.     and    o1.id = r.rkeyid
  12531.     and s.id = o1.id and (s.status & 0xf) = 1
  12532.     and    r.keycnt >= 13
  12533.     and    o1.id = c1.id
  12534.     and    c1.colid = r.rkey13
  12535.     and r.fkeyid = o2.id
  12536.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12537.     and    o2.id = c2.id    
  12538.     and    c2.colid = r.fkey13
  12539.     union all
  12540.     select
  12541.         db_name(r.rkeydbid),
  12542.         user_name(o1.uid),
  12543.         o1.name,
  12544.         c1.name,
  12545.         convert(binary(16),null),
  12546.         convert(int,null),
  12547.         db_name(r.fkeydbid),
  12548.         user_name(o2.uid),
  12549.         o2.name,
  12550.         c2.name,
  12551.         convert(binary(16),null),
  12552.         convert(int,null),
  12553.         convert(int,14),
  12554.         'NO ACTION',
  12555.         'NO ACTION',
  12556.         object_name(s.constid),
  12557.         object_name(r.constid),
  12558.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12559.     from
  12560.         sysobjects o1, sysobjects o2,
  12561.         syscolumns c1, syscolumns c2,
  12562.         sysreferences r, sysconstraints s
  12563.     where    
  12564.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12565.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12566.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12567.     and    o1.id = r.rkeyid
  12568.     and s.id = o1.id and (s.status & 0xf) = 1
  12569.     and    r.keycnt >= 14
  12570.     and    o1.id = c1.id
  12571.     and    c1.colid = r.rkey14
  12572.     and r.fkeyid = o2.id
  12573.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12574.     and    o2.id = c2.id    
  12575.     and    c2.colid = r.fkey14
  12576.     union all
  12577.     select
  12578.         db_name(r.rkeydbid),
  12579.         user_name(o1.uid),
  12580.         o1.name,
  12581.         c1.name,
  12582.         convert(binary(16),null),
  12583.         convert(int,null),
  12584.         db_name(r.fkeydbid),
  12585.         user_name(o2.uid),
  12586.         o2.name,
  12587.         c2.name,
  12588.         convert(binary(16),null),
  12589.         convert(int,null),
  12590.         convert(int,15),
  12591.         'NO ACTION',
  12592.         'NO ACTION',
  12593.         object_name(s.constid),
  12594.         object_name(r.constid),
  12595.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12596.     from
  12597.         sysobjects o1, sysobjects o2,
  12598.         syscolumns c1, syscolumns c2,
  12599.         sysreferences r, sysconstraints s
  12600.     where    
  12601.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12602.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12603.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12604.     and    o1.id = r.rkeyid
  12605.     and s.id = o1.id and (s.status & 0xf) = 1
  12606.     and    r.keycnt >= 15
  12607.     and    o1.id = c1.id
  12608.     and    c1.colid = r.rkey15
  12609.     and r.fkeyid = o2.id
  12610.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12611.     and    o2.id = c2.id    
  12612.     and    c2.colid = r.fkey15
  12613.     union all
  12614.     select
  12615.         db_name(r.rkeydbid),
  12616.         user_name(o1.uid),
  12617.         o1.name,
  12618.         c1.name,
  12619.         convert(binary(16),null),
  12620.         convert(int,null),
  12621.         db_name(r.fkeydbid),
  12622.         user_name(o2.uid),
  12623.         o2.name,
  12624.         c2.name,
  12625.         convert(binary(16),null),
  12626.         convert(int,null),
  12627.         convert(int,16),
  12628.         'NO ACTION',
  12629.         'NO ACTION',
  12630.         object_name(s.constid),
  12631.         object_name(r.constid),
  12632.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12633.     from
  12634.         sysobjects o1, sysobjects o2,
  12635.         syscolumns c1, syscolumns c2,
  12636.         sysreferences r, sysconstraints s
  12637.     where    
  12638.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  12639.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12640.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12641.     and    o1.id = r.rkeyid
  12642.     and s.id = o1.id and (s.status & 0xf) = 1
  12643.     and    o1.id = c1.id
  12644.     and    r.keycnt >= 16
  12645.     and    c1.colid = r.rkey16
  12646.     and r.fkeyid = o2.id
  12647.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12648.     and    o2.id = c2.id    
  12649.     and    c2.colid = r.fkey16
  12650.     order by 8,9,2,3,13
  12651.     END
  12652. go
  12653. dump tran master with no_log
  12654. go
  12655. CREATE PROCEDURE sp_foreign_keys_rowset;2
  12656.     (
  12657.        @handle            int output,
  12658.        @scrollopt        int output,
  12659.     @ccopt            int output,
  12660.     @rows            int output,
  12661.      @pk_table_name        varchar(255) = null,
  12662.     @pk_table_schema    varchar(255) = null,
  12663.     @pk_table_catalog    varchar(255) = null,
  12664.     @fk_table_name        varchar(255) = null,
  12665.     @fk_table_schema    varchar(255) = null,
  12666.     @fk_table_catalog    varchar(255) = null
  12667.     )
  12668. as
  12669.     declare @ret int
  12670.     
  12671.     SET NOCOUNT ON
  12672.  
  12673.     create table #spfkeysrowset1
  12674.         (
  12675.         PK_TABLE_CATALOG    sysname not null,
  12676.         PK_TABLE_SCHEMA        sysname not null,
  12677.         PK_TABLE_NAME         sysname not null,
  12678.         PK_COLUMN_NAME         sysname not null,
  12679.         PK_COLUMN_GUID        binary(16) null,
  12680.         PK_COLUMN_PROPID    int null,
  12681.         FK_TABLE_CATALOG    sysname not null,
  12682.         FK_TABLE_SCHEMA        sysname not null,
  12683.         FK_TABLE_NAME         sysname not null,
  12684.         FK_COLUMN_NAME         sysname not null,
  12685.         FK_COLUMN_GUID        binary(16) null,
  12686.         FK_COLUMN_PROPID    int null,
  12687.         ORDINAL                int not null,
  12688.         UPDATE_RULE            sysname not null,
  12689.         DELETE_RULE         sysname not null,
  12690.         PK_NAME                sysname not null,
  12691.         FK_NAME                sysname not null,
  12692.         DEFERRABILITY        smallint not null        
  12693.         )
  12694.  
  12695. IF @pk_table_name is not null
  12696.     BEGIN
  12697.     insert into #spfkeysrowset1
  12698.     select
  12699.         db_name(r.rkeydbid),
  12700.         user_name(o1.uid),
  12701.         o1.name,
  12702.         c1.name,
  12703.         convert(binary(16),null),
  12704.         convert(int,null),
  12705.         db_name(r.fkeydbid),
  12706.         user_name(o2.uid),
  12707.         o2.name,
  12708.         c2.name,
  12709.         convert(binary(16),null),
  12710.         convert(int,null),
  12711.         convert(int,1),
  12712.         'NO ACTION',
  12713.         'NO ACTION',
  12714.         object_name(s.constid),
  12715.         object_name(r.constid),
  12716.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12717.     from
  12718.         sysobjects o1, sysobjects o2,
  12719.         syscolumns c1, syscolumns c2,
  12720.         sysreferences r, sysconstraints s
  12721.     where    
  12722.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12723.     and    o1.name = @pk_table_name
  12724.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12725.     and    o1.id = r.rkeyid
  12726.     and s.id = o1.id and (s.status & 0xf) = 1
  12727.     and    o1.id = c1.id
  12728.     and    c1.colid = r.rkey1
  12729.     and r.fkeyid = o2.id
  12730.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  12731.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12732.     and    o2.id = c2.id    
  12733.     and    c2.colid = r.fkey1
  12734.     union all
  12735.     select
  12736.         db_name(r.rkeydbid),
  12737.         user_name(o1.uid),
  12738.         o1.name,
  12739.         c1.name,
  12740.         convert(binary(16),null),
  12741.         convert(int,null),
  12742.         db_name(r.fkeydbid),
  12743.         user_name(o2.uid),
  12744.         o2.name,
  12745.         c2.name,
  12746.         convert(binary(16),null),
  12747.         convert(int,null),
  12748.         convert(int,2),
  12749.         'NO ACTION',
  12750.         'NO ACTION',
  12751.         object_name(s.constid),
  12752.         object_name(r.constid),
  12753.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12754.     from
  12755.         sysobjects o1, sysobjects o2,
  12756.         syscolumns c1, syscolumns c2,
  12757.         sysreferences r, sysconstraints s
  12758.     where    
  12759.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12760.     and    o1.name = @pk_table_name
  12761.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12762.     and    o1.id = r.rkeyid
  12763.     and s.id = o1.id and (s.status & 0xf) = 1
  12764.     and    o1.id = c1.id
  12765.     and    c1.colid = r.rkey2
  12766.     and    r.keycnt >= 2
  12767.     and r.fkeyid = o2.id
  12768.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  12769.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12770.     and    o2.id = c2.id    
  12771.     and    c2.colid = r.fkey2
  12772.     union all
  12773.     select
  12774.         db_name(r.rkeydbid),
  12775.         user_name(o1.uid),
  12776.         o1.name,
  12777.         c1.name,
  12778.         convert(binary(16),null),
  12779.         convert(int,null),
  12780.         db_name(r.fkeydbid),
  12781.         user_name(o2.uid),
  12782.         o2.name,
  12783.         c2.name,
  12784.         convert(binary(16),null),
  12785.         convert(int,null),
  12786.         convert(int,3),
  12787.         'NO ACTION',
  12788.         'NO ACTION',
  12789.         object_name(s.constid),
  12790.         object_name(r.constid),
  12791.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12792.     from
  12793.         sysobjects o1, sysobjects o2,
  12794.         syscolumns c1, syscolumns c2,
  12795.         sysreferences r, sysconstraints s
  12796.     where    
  12797.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12798.     and    o1.name = @pk_table_name
  12799.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12800.     and    o1.id = r.rkeyid
  12801.     and s.id = o1.id and (s.status & 0xf) = 1
  12802.     and    o1.id = c1.id
  12803.     and    c1.colid = r.rkey3
  12804.     and    r.keycnt >= 3
  12805.     and r.fkeyid = o2.id
  12806.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  12807.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12808.     and    o2.id = c2.id    
  12809.     and    c2.colid = r.fkey3
  12810.     union all
  12811.     select
  12812.         db_name(r.rkeydbid),
  12813.         user_name(o1.uid),
  12814.         o1.name,
  12815.         c1.name,
  12816.         convert(binary(16),null),
  12817.         convert(int,null),
  12818.         db_name(r.fkeydbid),
  12819.         user_name(o2.uid),
  12820.         o2.name,
  12821.         c2.name,
  12822.         convert(binary(16),null),
  12823.         convert(int,null),
  12824.         convert(int,4),
  12825.         'NO ACTION',
  12826.         'NO ACTION',
  12827.         object_name(s.constid),
  12828.         object_name(r.constid),
  12829.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12830.     from
  12831.         sysobjects o1, sysobjects o2,
  12832.         syscolumns c1, syscolumns c2,
  12833.         sysreferences r, sysconstraints s
  12834.     where    
  12835.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12836.     and    o1.name = @pk_table_name
  12837.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12838.     and    o1.id = r.rkeyid
  12839.     and s.id = o1.id and (s.status & 0xf) = 1
  12840.     and    r.keycnt >= 4
  12841.     and    o1.id = c1.id
  12842.     and    c1.colid = r.rkey4
  12843.     and r.fkeyid = o2.id
  12844.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  12845.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12846.     and    o2.id = c2.id    
  12847.     and    c2.colid = r.fkey4
  12848.     union all
  12849.     select
  12850.         db_name(r.rkeydbid),
  12851.         user_name(o1.uid),
  12852.         o1.name,
  12853.         c1.name,
  12854.         convert(binary(16),null),
  12855.         convert(int,null),
  12856.         db_name(r.fkeydbid),
  12857.         user_name(o2.uid),
  12858.         o2.name,
  12859.         c2.name,
  12860.         convert(binary(16),null),
  12861.         convert(int,null),
  12862.         convert(int,5),
  12863.         'NO ACTION',
  12864.         'NO ACTION',
  12865.         object_name(s.constid),
  12866.         object_name(r.constid),
  12867.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12868.     from
  12869.         sysobjects o1, sysobjects o2,
  12870.         syscolumns c1, syscolumns c2,
  12871.         sysreferences r, sysconstraints s
  12872.     where    
  12873.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12874.     and    o1.name = @pk_table_name
  12875.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12876.     and    o1.id = r.rkeyid
  12877.     and s.id = o1.id and (s.status & 0xf) = 1
  12878.     and    r.keycnt >= 5
  12879.     and    o1.id = c1.id
  12880.     and    c1.colid = r.rkey5
  12881.     and r.fkeyid = o2.id
  12882.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  12883.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12884.     and    o2.id = c2.id    
  12885.     and    c2.colid = r.fkey5
  12886.     union all
  12887.     select
  12888.         db_name(r.rkeydbid),
  12889.         user_name(o1.uid),
  12890.         o1.name,
  12891.         c1.name,
  12892.         convert(binary(16),null),
  12893.         convert(int,null),
  12894.         db_name(r.fkeydbid),
  12895.         user_name(o2.uid),
  12896.         o2.name,
  12897.         c2.name,
  12898.         convert(binary(16),null),
  12899.         convert(int,null),
  12900.         convert(int,6),
  12901.         'NO ACTION',
  12902.         'NO ACTION',
  12903.         object_name(s.constid),
  12904.         object_name(r.constid),
  12905.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12906.     from
  12907.         sysobjects o1, sysobjects o2,
  12908.         syscolumns c1, syscolumns c2,
  12909.         sysreferences r, sysconstraints s
  12910.     where    
  12911.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12912.     and    o1.name = @pk_table_name
  12913.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12914.     and    o1.id = r.rkeyid
  12915.     and s.id = o1.id and (s.status & 0xf) = 1
  12916.     and    r.keycnt >= 6
  12917.     and    o1.id = c1.id
  12918.     and    c1.colid = r.rkey6
  12919.     and r.fkeyid = o2.id
  12920.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  12921.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12922.     and    o2.id = c2.id    
  12923.     and    c2.colid = r.fkey6
  12924.     union all
  12925.     select
  12926.         db_name(r.rkeydbid),
  12927.         user_name(o1.uid),
  12928.         o1.name,
  12929.         c1.name,
  12930.         convert(binary(16),null),
  12931.         convert(int,null),
  12932.         db_name(r.fkeydbid),
  12933.         user_name(o2.uid),
  12934.         o2.name,
  12935.         c2.name,
  12936.         convert(binary(16),null),
  12937.         convert(int,null),
  12938.         convert(int,7),
  12939.         'NO ACTION',
  12940.         'NO ACTION',
  12941.         object_name(s.constid),
  12942.         object_name(r.constid),
  12943.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12944.     from
  12945.         sysobjects o1, sysobjects o2,
  12946.         syscolumns c1, syscolumns c2,
  12947.         sysreferences r, sysconstraints s
  12948.     where    
  12949.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12950.     and    o1.name = @pk_table_name
  12951.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12952.     and    o1.id = r.rkeyid
  12953.     and s.id = o1.id and (s.status & 0xf) = 1
  12954.     and    r.keycnt >= 7
  12955.     and    o1.id = c1.id
  12956.     and    c1.colid = r.rkey7
  12957.     and r.fkeyid = o2.id
  12958.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  12959.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12960.     and    o2.id = c2.id    
  12961.     and    c2.colid = r.fkey7
  12962.     union all
  12963.     select
  12964.         db_name(r.rkeydbid),
  12965.         user_name(o1.uid),
  12966.         o1.name,
  12967.         c1.name,
  12968.         convert(binary(16),null),
  12969.         convert(int,null),
  12970.         db_name(r.fkeydbid),
  12971.         user_name(o2.uid),
  12972.         o2.name,
  12973.         c2.name,
  12974.         convert(binary(16),null),
  12975.         convert(int,null),
  12976.         convert(int,8),
  12977.         'NO ACTION',
  12978.         'NO ACTION',
  12979.         object_name(s.constid),
  12980.         object_name(r.constid),
  12981.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  12982.     from
  12983.         sysobjects o1, sysobjects o2,
  12984.         syscolumns c1, syscolumns c2,
  12985.         sysreferences r, sysconstraints s
  12986.     where    
  12987.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  12988.     and    o1.name = @pk_table_name
  12989.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  12990.     and    o1.id = r.rkeyid
  12991.     and s.id = o1.id and (s.status & 0xf) = 1
  12992.     and    r.keycnt >= 8
  12993.     and    o1.id = c1.id
  12994.     and    c1.colid = r.rkey8
  12995.     and r.fkeyid = o2.id
  12996.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  12997.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  12998.     and    o2.id = c2.id    
  12999.     and    c2.colid = r.fkey8
  13000.     union all
  13001.     select
  13002.         db_name(r.rkeydbid),
  13003.         user_name(o1.uid),
  13004.         o1.name,
  13005.         c1.name,
  13006.         convert(binary(16),null),
  13007.         convert(int,null),
  13008.         db_name(r.fkeydbid),
  13009.         user_name(o2.uid),
  13010.         o2.name,
  13011.         c2.name,
  13012.         convert(binary(16),null),
  13013.         convert(int,null),
  13014.         convert(int,9),
  13015.         'NO ACTION',
  13016.         'NO ACTION',
  13017.         object_name(s.constid),
  13018.         object_name(r.constid),
  13019.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13020.     from
  13021.         sysobjects o1, sysobjects o2,
  13022.         syscolumns c1, syscolumns c2,
  13023.         sysreferences r, sysconstraints s
  13024.     where    
  13025.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13026.     and    o1.name = @pk_table_name
  13027.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13028.     and    o1.id = r.rkeyid
  13029.     and s.id = o1.id and (s.status & 0xf) = 1
  13030.     and    r.keycnt >= 9
  13031.     and    o1.id = c1.id
  13032.     and    c1.colid = r.rkey9
  13033.     and r.fkeyid = o2.id
  13034.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  13035.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13036.     and    o2.id = c2.id    
  13037.     and    c2.colid = r.fkey9
  13038.     union all
  13039.     select
  13040.         db_name(r.rkeydbid),
  13041.         user_name(o1.uid),
  13042.         o1.name,
  13043.         c1.name,
  13044.         convert(binary(16),null),
  13045.         convert(int,null),
  13046.         db_name(r.fkeydbid),
  13047.         user_name(o2.uid),
  13048.         o2.name,
  13049.         c2.name,
  13050.         convert(binary(16),null),
  13051.         convert(int,null),
  13052.         convert(int,10),
  13053.         'NO ACTION',
  13054.         'NO ACTION',
  13055.         object_name(s.constid),
  13056.         object_name(r.constid),
  13057.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13058.     from
  13059.         sysobjects o1, sysobjects o2,
  13060.         syscolumns c1, syscolumns c2,
  13061.         sysreferences r, sysconstraints s
  13062.     where    
  13063.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13064.     and    o1.name = @pk_table_name
  13065.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13066.     and    o1.id = r.rkeyid
  13067.     and s.id = o1.id and (s.status & 0xf) = 1
  13068.     and    r.keycnt >= 10
  13069.     and    o1.id = c1.id
  13070.     and    c1.colid = r.rkey10
  13071.     and r.fkeyid = o2.id
  13072.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  13073.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13074.     and    o2.id = c2.id    
  13075.     and    c2.colid = r.fkey10
  13076.     union all
  13077.     select
  13078.         db_name(r.rkeydbid),
  13079.         user_name(o1.uid),
  13080.         o1.name,
  13081.         c1.name,
  13082.         convert(binary(16),null),
  13083.         convert(int,null),
  13084.         db_name(r.fkeydbid),
  13085.         user_name(o2.uid),
  13086.         o2.name,
  13087.         c2.name,
  13088.         convert(binary(16),null),
  13089.         convert(int,null),
  13090.         convert(int,11),
  13091.         'NO ACTION',
  13092.         'NO ACTION',
  13093.         object_name(s.constid),
  13094.         object_name(r.constid),
  13095.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13096.     from
  13097.         sysobjects o1, sysobjects o2,
  13098.         syscolumns c1, syscolumns c2,
  13099.         sysreferences r, sysconstraints s
  13100.     where    
  13101.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13102.     and    o1.name = @pk_table_name
  13103.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13104.     and    o1.id = r.rkeyid
  13105.     and s.id = o1.id and (s.status & 0xf) = 1
  13106.     and    r.keycnt >= 11
  13107.     and    o1.id = c1.id
  13108.     and    c1.colid = r.rkey11
  13109.     and r.fkeyid = o2.id
  13110.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  13111.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13112.     and    o2.id = c2.id    
  13113.     and    c2.colid = r.fkey11
  13114.     union all
  13115.     select
  13116.         db_name(r.rkeydbid),
  13117.         user_name(o1.uid),
  13118.         o1.name,
  13119.         c1.name,
  13120.         convert(binary(16),null),
  13121.         convert(int,null),
  13122.         db_name(r.fkeydbid),
  13123.         user_name(o2.uid),
  13124.         o2.name,
  13125.         c2.name,
  13126.         convert(binary(16),null),
  13127.         convert(int,null),
  13128.         convert(int,12),
  13129.         'NO ACTION',
  13130.         'NO ACTION',
  13131.         object_name(s.constid),
  13132.         object_name(r.constid),
  13133.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13134.     from
  13135.         sysobjects o1, sysobjects o2,
  13136.         syscolumns c1, syscolumns c2,
  13137.         sysreferences r, sysconstraints s
  13138.     where    
  13139.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13140.     and    o1.name = @pk_table_name
  13141.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13142.     and    o1.id = r.rkeyid
  13143.     and s.id = o1.id and (s.status & 0xf) = 1
  13144.     and    r.keycnt >= 12
  13145.     and    o1.id = c1.id
  13146.     and    c1.colid = r.rkey12
  13147.     and r.fkeyid = o2.id
  13148.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  13149.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13150.     and    o2.id = c2.id    
  13151.     and    c2.colid = r.fkey12
  13152.     union all
  13153.     select
  13154.         db_name(r.rkeydbid),
  13155.         user_name(o1.uid),
  13156.         o1.name,
  13157.         c1.name,
  13158.         convert(binary(16),null),
  13159.         convert(int,null),
  13160.         db_name(r.fkeydbid),
  13161.         user_name(o2.uid),
  13162.         o2.name,
  13163.         c2.name,
  13164.         convert(binary(16),null),
  13165.         convert(int,null),
  13166.         convert(int,13),
  13167.         'NO ACTION',
  13168.         'NO ACTION',
  13169.         object_name(s.constid),
  13170.         object_name(r.constid),
  13171.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13172.     from
  13173.         sysobjects o1, sysobjects o2,
  13174.         syscolumns c1, syscolumns c2,
  13175.         sysreferences r, sysconstraints s
  13176.     where    
  13177.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13178.     and    o1.name = @pk_table_name
  13179.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13180.     and    o1.id = r.rkeyid
  13181.     and s.id = o1.id and (s.status & 0xf) = 1
  13182.     and    r.keycnt >= 13
  13183.     and    o1.id = c1.id
  13184.     and    c1.colid = r.rkey13
  13185.     and r.fkeyid = o2.id
  13186.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  13187.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13188.     and    o2.id = c2.id    
  13189.     and    c2.colid = r.fkey13
  13190.     union all
  13191.     select
  13192.         db_name(r.rkeydbid),
  13193.         user_name(o1.uid),
  13194.         o1.name,
  13195.         c1.name,
  13196.         convert(binary(16),null),
  13197.         convert(int,null),
  13198.         db_name(r.fkeydbid),
  13199.         user_name(o2.uid),
  13200.         o2.name,
  13201.         c2.name,
  13202.         convert(binary(16),null),
  13203.         convert(int,null),
  13204.         convert(int,14),
  13205.         'NO ACTION',
  13206.         'NO ACTION',
  13207.         object_name(s.constid),
  13208.         object_name(r.constid),
  13209.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13210.     from
  13211.         sysobjects o1, sysobjects o2,
  13212.         syscolumns c1, syscolumns c2,
  13213.         sysreferences r, sysconstraints s
  13214.     where    
  13215.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13216.     and    o1.name = @pk_table_name
  13217.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13218.     and    o1.id = r.rkeyid
  13219.     and s.id = o1.id and (s.status & 0xf) = 1
  13220.     and    r.keycnt >= 14
  13221.     and    o1.id = c1.id
  13222.     and    c1.colid = r.rkey14
  13223.     and r.fkeyid = o2.id
  13224.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  13225.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13226.     and    o2.id = c2.id    
  13227.     and    c2.colid = r.fkey14
  13228.     union all
  13229.     select
  13230.         db_name(r.rkeydbid),
  13231.         user_name(o1.uid),
  13232.         o1.name,
  13233.         c1.name,
  13234.         convert(binary(16),null),
  13235.         convert(int,null),
  13236.         db_name(r.fkeydbid),
  13237.         user_name(o2.uid),
  13238.         o2.name,
  13239.         c2.name,
  13240.         convert(binary(16),null),
  13241.         convert(int,null),
  13242.         convert(int,15),
  13243.         'NO ACTION',
  13244.         'NO ACTION',
  13245.         object_name(s.constid),
  13246.         object_name(r.constid),
  13247.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13248.     from
  13249.         sysobjects o1, sysobjects o2,
  13250.         syscolumns c1, syscolumns c2,
  13251.         sysreferences r, sysconstraints s
  13252.     where    
  13253.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13254.     and    o1.name = @pk_table_name
  13255.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13256.     and    o1.id = r.rkeyid
  13257.     and s.id = o1.id and (s.status & 0xf) = 1
  13258.     and    r.keycnt >= 15
  13259.     and    o1.id = c1.id
  13260.     and    c1.colid = r.rkey15
  13261.     and r.fkeyid = o2.id
  13262.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  13263.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13264.     and    o2.id = c2.id    
  13265.     and    c2.colid = r.fkey15
  13266.     union all
  13267.     select
  13268.         db_name(r.rkeydbid),
  13269.         user_name(o1.uid),
  13270.         o1.name,
  13271.         c1.name,
  13272.         convert(binary(16),null),
  13273.         convert(int,null),
  13274.         db_name(r.fkeydbid),
  13275.         user_name(o2.uid),
  13276.         o2.name,
  13277.         c2.name,
  13278.         convert(binary(16),null),
  13279.         convert(int,null),
  13280.         convert(int,16),
  13281.         'NO ACTION',
  13282.         'NO ACTION',
  13283.         object_name(s.constid),
  13284.         object_name(r.constid),
  13285.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13286.     from
  13287.         sysobjects o1, sysobjects o2,
  13288.         syscolumns c1, syscolumns c2,
  13289.         sysreferences r, sysconstraints s
  13290.     where    
  13291.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13292.     and    o1.name = @pk_table_name
  13293.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13294.     and    o1.id = r.rkeyid
  13295.     and s.id = o1.id and (s.status & 0xf) = 1
  13296.     and    r.keycnt >= 16
  13297.     and    o1.id = c1.id
  13298.     and    c1.colid = r.rkey16
  13299.     and r.fkeyid = o2.id
  13300.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  13301.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13302.     and    o2.id = c2.id    
  13303.     and    c2.colid = r.fkey16
  13304.     order by 8,9,2,3,13
  13305.     END
  13306. ELSE IF @fk_table_name is not null
  13307.     BEGIN
  13308.     insert into #spfkeysrowset1
  13309.     select
  13310.         db_name(r.rkeydbid),
  13311.         user_name(o1.uid),
  13312.         o1.name,
  13313.         c1.name,
  13314.         convert(binary(16),null),
  13315.         convert(int,null),
  13316.         db_name(r.fkeydbid),
  13317.         user_name(o2.uid),
  13318.         o2.name,
  13319.         c2.name,
  13320.         convert(binary(16),null),
  13321.         convert(int,null),
  13322.         convert(int,1),
  13323.         'NO ACTION',
  13324.         'NO ACTION',
  13325.         object_name(s.constid),
  13326.         object_name(r.constid),
  13327.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13328.     from
  13329.         sysobjects o1, sysobjects o2,
  13330.         syscolumns c1, syscolumns c2,
  13331.         sysreferences r, sysconstraints s
  13332.     where    
  13333.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13334.     and    o2.name = @fk_table_name
  13335.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13336.     and    o2.id = r.fkeyid
  13337.     and    o2.id = c2.id
  13338.     and    c2.colid = r.fkey1
  13339.     and r.rkeyid = o1.id
  13340.     and s.id = o1.id and (s.status & 0xf) = 1
  13341.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  13342.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13343.     and    o1.id = c1.id    
  13344.     and    c1.colid = r.rkey1
  13345.     union    all
  13346.     select
  13347.         db_name(r.rkeydbid),
  13348.         user_name(o1.uid),
  13349.         o1.name,
  13350.         c1.name,
  13351.         convert(binary(16),null),
  13352.         convert(int,null),
  13353.         db_name(r.fkeydbid),
  13354.         user_name(o2.uid),
  13355.         o2.name,
  13356.         c2.name,
  13357.         convert(binary(16),null),
  13358.         convert(int,null),
  13359.         convert(int,2),
  13360.         'NO ACTION',
  13361.         'NO ACTION',
  13362.         object_name(s.constid),
  13363.         object_name(r.constid),
  13364.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13365.     from
  13366.         sysobjects o1, sysobjects o2,
  13367.         syscolumns c1, syscolumns c2,
  13368.         sysreferences r, sysconstraints s
  13369.     where    
  13370.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13371.     and    o2.name = @fk_table_name
  13372.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13373.     and    o2.id = r.fkeyid
  13374.     and    r.keycnt >= 2
  13375.     and    o2.id = c2.id
  13376.     and    c2.colid = r.fkey2
  13377.     and r.rkeyid = o1.id
  13378.     and s.id = o1.id and (s.status & 0xf) = 1
  13379.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  13380.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13381.     and    o1.id = c1.id    
  13382.     and    c1.colid = r.rkey2
  13383.     union    all
  13384.     select
  13385.         db_name(r.rkeydbid),
  13386.         user_name(o1.uid),
  13387.         o1.name,
  13388.         c1.name,
  13389.         convert(binary(16),null),
  13390.         convert(int,null),
  13391.         db_name(r.fkeydbid),
  13392.         user_name(o2.uid),
  13393.         o2.name,
  13394.         c2.name,
  13395.         convert(binary(16),null),
  13396.         convert(int,null),
  13397.         convert(int,3),
  13398.         'NO ACTION',
  13399.         'NO ACTION',
  13400.         object_name(s.constid),
  13401.         object_name(r.constid),
  13402.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13403.     from
  13404.         sysobjects o1, sysobjects o2,
  13405.         syscolumns c1, syscolumns c2,
  13406.         sysreferences r, sysconstraints s
  13407.     where    
  13408.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13409.     and    o2.name = @fk_table_name
  13410.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13411.     and    o2.id = r.fkeyid
  13412.     and    r.keycnt >= 3
  13413.     and    o2.id = c2.id
  13414.     and    c2.colid = r.fkey3
  13415.     and    r.rkeyid = o1.id
  13416.     and s.id = o1.id and (s.status & 0xf) = 1
  13417.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  13418.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13419.     and    o1.id = c1.id    
  13420.     and    c1.colid = r.rkey3
  13421.     union    all
  13422.     select
  13423.         db_name(r.rkeydbid),
  13424.         user_name(o1.uid),
  13425.         o1.name,
  13426.         c1.name,
  13427.         convert(binary(16),null),
  13428.         convert(int,null),
  13429.         db_name(r.fkeydbid),
  13430.         user_name(o2.uid),
  13431.         o2.name,
  13432.         c2.name,
  13433.         convert(binary(16),null),
  13434.         convert(int,null),
  13435.         convert(int,4),
  13436.         'NO ACTION',
  13437.         'NO ACTION',
  13438.         object_name(s.constid),
  13439.         object_name(r.constid),
  13440.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13441.     from
  13442.         sysobjects o1, sysobjects o2,
  13443.         syscolumns c1, syscolumns c2,
  13444.         sysreferences r, sysconstraints s
  13445.     where    
  13446.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13447.     and    o2.name = @fk_table_name
  13448.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13449.     and    o2.id = r.fkeyid
  13450.     and    r.keycnt >= 4
  13451.     and    o2.id = c2.id
  13452.     and    c2.colid = r.fkey4
  13453.     and r.rkeyid = o1.id
  13454.     and s.id = o1.id and (s.status & 0xf) = 1
  13455.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  13456.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13457.     and    o1.id = c1.id    
  13458.     and    c1.colid = r.rkey4
  13459.     union    all
  13460.     select
  13461.         db_name(r.rkeydbid),
  13462.         user_name(o1.uid),
  13463.         o1.name,
  13464.         c1.name,
  13465.         convert(binary(16),null),
  13466.         convert(int,null),
  13467.         db_name(r.fkeydbid),
  13468.         user_name(o2.uid),
  13469.         o2.name,
  13470.         c2.name,
  13471.         convert(binary(16),null),
  13472.         convert(int,null),
  13473.         convert(int,5),
  13474.         'NO ACTION',
  13475.         'NO ACTION',
  13476.         object_name(s.constid),
  13477.         object_name(r.constid),
  13478.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13479.     from
  13480.         sysobjects o1, sysobjects o2,
  13481.         syscolumns c1, syscolumns c2,
  13482.         sysreferences r, sysconstraints s
  13483.     where    
  13484.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13485.     and    o2.name = @fk_table_name
  13486.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13487.     and    o2.id = r.fkeyid
  13488.     and    r.keycnt >= 5
  13489.     and    o2.id = c2.id
  13490.     and    c2.colid = r.fkey5
  13491.     and r.rkeyid = o1.id
  13492.     and s.id = o1.id and (s.status & 0xf) = 1
  13493.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  13494.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13495.     and    o1.id = c1.id    
  13496.     and    c1.colid = r.rkey5
  13497.     union    all
  13498.     select
  13499.         db_name(r.rkeydbid),
  13500.         user_name(o1.uid),
  13501.         o1.name,
  13502.         c1.name,
  13503.         convert(binary(16),null),
  13504.         convert(int,null),
  13505.         db_name(r.fkeydbid),
  13506.         user_name(o2.uid),
  13507.         o2.name,
  13508.         c2.name,
  13509.         convert(binary(16),null),
  13510.         convert(int,null),
  13511.         convert(int,6),
  13512.         'NO ACTION',
  13513.         'NO ACTION',
  13514.         object_name(s.constid),
  13515.         object_name(r.constid),
  13516.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13517.     from
  13518.         sysobjects o1, sysobjects o2,
  13519.         syscolumns c1, syscolumns c2,
  13520.         sysreferences r, sysconstraints s
  13521.     where    
  13522.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13523.     and    o2.name = @fk_table_name
  13524.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13525.     and    o2.id = r.fkeyid
  13526.     and    r.keycnt >= 6
  13527.     and    o2.id = c2.id
  13528.     and    c2.colid = r.fkey6
  13529.     and r.rkeyid = o1.id
  13530.     and s.id = o1.id and (s.status & 0xf) = 1
  13531.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  13532.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13533.     and    o1.id = c1.id    
  13534.     and    c1.colid = r.rkey6
  13535.     union    all
  13536.     select
  13537.         db_name(r.rkeydbid),
  13538.         user_name(o1.uid),
  13539.         o1.name,
  13540.         c1.name,
  13541.         convert(binary(16),null),
  13542.         convert(int,null),
  13543.         db_name(r.fkeydbid),
  13544.         user_name(o2.uid),
  13545.         o2.name,
  13546.         c2.name,
  13547.         convert(binary(16),null),
  13548.         convert(int,null),
  13549.         convert(int,7),
  13550.         'NO ACTION',
  13551.         'NO ACTION',
  13552.         object_name(s.constid),
  13553.         object_name(r.constid),
  13554.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13555.     from
  13556.         sysobjects o1, sysobjects o2,
  13557.         syscolumns c1, syscolumns c2,
  13558.         sysreferences r, sysconstraints s
  13559.     where    
  13560.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13561.     and    o2.name = @fk_table_name
  13562.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13563.     and    o2.id = r.fkeyid
  13564.     and    r.keycnt >= 7
  13565.     and    o2.id = c2.id
  13566.     and    c2.colid = r.fkey7
  13567.     and r.rkeyid = o1.id
  13568.     and s.id = o1.id and (s.status & 0xf) = 1
  13569.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  13570.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13571.     and    o1.id = c1.id    
  13572.     and    c1.colid = r.rkey7
  13573.     union    all
  13574.     select
  13575.         db_name(r.rkeydbid),
  13576.         user_name(o1.uid),
  13577.         o1.name,
  13578.         c1.name,
  13579.         convert(binary(16),null),
  13580.         convert(int,null),
  13581.         db_name(r.fkeydbid),
  13582.         user_name(o2.uid),
  13583.         o2.name,
  13584.         c2.name,
  13585.         convert(binary(16),null),
  13586.         convert(int,null),
  13587.         convert(int,8),
  13588.         'NO ACTION',
  13589.         'NO ACTION',
  13590.         object_name(s.constid),
  13591.         object_name(r.constid),
  13592.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13593.     from
  13594.         sysobjects o1, sysobjects o2,
  13595.         syscolumns c1, syscolumns c2,
  13596.         sysreferences r, sysconstraints s
  13597.     where    
  13598.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13599.     and    o2.name = @fk_table_name
  13600.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13601.     and    o2.id = r.fkeyid
  13602.     and    r.keycnt >= 8
  13603.     and    o2.id = c2.id
  13604.     and    c2.colid = r.fkey8
  13605.     and r.rkeyid = o1.id
  13606.     and s.id = o1.id and (s.status & 0xf) = 1
  13607.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  13608.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13609.     and    o1.id = c1.id    
  13610.     and    c1.colid = r.rkey8
  13611.     union    all
  13612.     select
  13613.         db_name(r.rkeydbid),
  13614.         user_name(o1.uid),
  13615.         o1.name,
  13616.         c1.name,
  13617.         convert(binary(16),null),
  13618.         convert(int,null),
  13619.         db_name(r.fkeydbid),
  13620.         user_name(o2.uid),
  13621.         o2.name,
  13622.         c2.name,
  13623.         convert(binary(16),null),
  13624.         convert(int,null),
  13625.         convert(int,9),
  13626.         'NO ACTION',
  13627.         'NO ACTION',
  13628.         object_name(s.constid),
  13629.         object_name(r.constid),
  13630.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13631.     from
  13632.         sysobjects o1, sysobjects o2,
  13633.         syscolumns c1, syscolumns c2,
  13634.         sysreferences r, sysconstraints s
  13635.     where    
  13636.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13637.     and    o2.name = @fk_table_name
  13638.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13639.     and    o2.id = r.fkeyid
  13640.     and    r.keycnt >= 9
  13641.     and    o2.id = c2.id
  13642.     and    c2.colid = r.fkey9
  13643.     and r.rkeyid = o1.id
  13644.     and s.id = o1.id and (s.status & 0xf) = 1
  13645.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  13646.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13647.     and    o1.id = c1.id    
  13648.     and    c1.colid = r.rkey9
  13649.     union    all
  13650.     select
  13651.         db_name(r.rkeydbid),
  13652.         user_name(o1.uid),
  13653.         o1.name,
  13654.         c1.name,
  13655.         convert(binary(16),null),
  13656.         convert(int,null),
  13657.         db_name(r.fkeydbid),
  13658.         user_name(o2.uid),
  13659.         o2.name,
  13660.         c2.name,
  13661.         convert(binary(16),null),
  13662.         convert(int,null),
  13663.         convert(int,10),
  13664.         'NO ACTION',
  13665.         'NO ACTION',
  13666.         object_name(s.constid),
  13667.         object_name(r.constid),
  13668.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13669.     from
  13670.         sysobjects o1, sysobjects o2,
  13671.         syscolumns c1, syscolumns c2,
  13672.         sysreferences r, sysconstraints s
  13673.     where    
  13674.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13675.     and    o2.name = @fk_table_name
  13676.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13677.     and    o2.id = r.fkeyid
  13678.     and    r.keycnt >= 10
  13679.     and    o2.id = c2.id
  13680.     and    c2.colid = r.fkey10
  13681.     and r.rkeyid = o1.id
  13682.     and s.id = o1.id and (s.status & 0xf) = 1
  13683.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  13684.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13685.     and    o1.id = c1.id    
  13686.     and    c1.colid = r.rkey10
  13687.     union    all
  13688.     select
  13689.         db_name(r.rkeydbid),
  13690.         user_name(o1.uid),
  13691.         o1.name,
  13692.         c1.name,
  13693.         convert(binary(16),null),
  13694.         convert(int,null),
  13695.         db_name(r.fkeydbid),
  13696.         user_name(o2.uid),
  13697.         o2.name,
  13698.         c2.name,
  13699.         convert(binary(16),null),
  13700.         convert(int,null),
  13701.         convert(int,11),
  13702.         'NO ACTION',
  13703.         'NO ACTION',
  13704.         object_name(s.constid),
  13705.         object_name(r.constid),
  13706.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13707.     from
  13708.         sysobjects o1, sysobjects o2,
  13709.         syscolumns c1, syscolumns c2,
  13710.         sysreferences r, sysconstraints s
  13711.     where    
  13712.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13713.     and    o2.name = @fk_table_name
  13714.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13715.     and    o2.id = r.fkeyid
  13716.     and    r.keycnt >= 11
  13717.     and    o2.id = c2.id
  13718.     and    c2.colid = r.fkey11
  13719.     and r.rkeyid = o1.id
  13720.     and s.id = o1.id and (s.status & 0xf) = 1
  13721.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  13722.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13723.     and    o1.id = c1.id    
  13724.     and    c1.colid = r.rkey11
  13725.     union    all
  13726.     select
  13727.         db_name(r.rkeydbid),
  13728.         user_name(o1.uid),
  13729.         o1.name,
  13730.         c1.name,
  13731.         convert(binary(16),null),
  13732.         convert(int,null),
  13733.         db_name(r.fkeydbid),
  13734.         user_name(o2.uid),
  13735.         o2.name,
  13736.         c2.name,
  13737.         convert(binary(16),null),
  13738.         convert(int,null),
  13739.         convert(int,12),
  13740.         'NO ACTION',
  13741.         'NO ACTION',
  13742.         object_name(s.constid),
  13743.         object_name(r.constid),
  13744.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/        
  13745.     from
  13746.         sysobjects o1, sysobjects o2,
  13747.         syscolumns c1, syscolumns c2,
  13748.         sysreferences r, sysconstraints s
  13749.     where    
  13750.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13751.     and    o2.name = @fk_table_name
  13752.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13753.     and    o2.id = r.fkeyid
  13754.     and    r.keycnt >= 12
  13755.     and    o2.id = c2.id
  13756.     and    c2.colid = r.fkey12
  13757.     and r.rkeyid = o1.id
  13758.     and s.id = o1.id and (s.status & 0xf) = 1
  13759.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  13760.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13761.     and    o1.id = c1.id    
  13762.     and    c1.colid = r.rkey12
  13763.     union    all
  13764.     select
  13765.         db_name(r.rkeydbid),
  13766.         user_name(o1.uid),
  13767.         o1.name,
  13768.         c1.name,
  13769.         convert(binary(16),null),
  13770.         convert(int,null),
  13771.         db_name(r.fkeydbid),
  13772.         user_name(o2.uid),
  13773.         o2.name,
  13774.         c2.name,
  13775.         convert(binary(16),null),
  13776.         convert(int,null),
  13777.         convert(int,13),
  13778.         'NO ACTION',
  13779.         'NO ACTION',
  13780.         object_name(s.constid),
  13781.         object_name(r.constid),
  13782.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13783.     from
  13784.         sysobjects o1, sysobjects o2,
  13785.         syscolumns c1, syscolumns c2,
  13786.         sysreferences r, sysconstraints s
  13787.     where    
  13788.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13789.     and    o2.name = @fk_table_name
  13790.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13791.     and    o2.id = r.fkeyid
  13792.     and    r.keycnt >= 13
  13793.     and    o2.id = c2.id
  13794.     and    c2.colid = r.fkey13
  13795.     and r.rkeyid = o1.id
  13796.     and s.id = o1.id and (s.status & 0xf) = 1
  13797.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  13798.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13799.     and    o1.id = c1.id    
  13800.     and    c1.colid = r.rkey13
  13801.     union    all
  13802.     select
  13803.         db_name(r.rkeydbid),
  13804.         user_name(o1.uid),
  13805.         o1.name,
  13806.         c1.name,
  13807.         convert(binary(16),null),
  13808.         convert(int,null),
  13809.         db_name(r.fkeydbid),
  13810.         user_name(o2.uid),
  13811.         o2.name,
  13812.         c2.name,
  13813.         convert(binary(16),null),
  13814.         convert(int,null),
  13815.         convert(int,14),
  13816.         'NO ACTION',
  13817.         'NO ACTION',
  13818.         object_name(s.constid),
  13819.         object_name(r.constid),
  13820.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13821.     from
  13822.         sysobjects o1, sysobjects o2,
  13823.         syscolumns c1, syscolumns c2,
  13824.         sysreferences r, sysconstraints s
  13825.     where    
  13826.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13827.     and    o2.name = @fk_table_name
  13828.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13829.     and    o2.id = r.fkeyid
  13830.     and    r.keycnt >= 14
  13831.     and    o2.id = c2.id
  13832.     and    c2.colid = r.fkey14
  13833.     and r.rkeyid = o1.id
  13834.     and s.id = o1.id and (s.status & 0xf) = 1
  13835.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  13836.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13837.     and    o1.id = c1.id    
  13838.     and    c1.colid = r.rkey14
  13839.     union    all
  13840.     select
  13841.         db_name(r.rkeydbid),
  13842.         user_name(o1.uid),
  13843.         o1.name,
  13844.         c1.name,
  13845.         convert(binary(16),null),
  13846.         convert(int,null),
  13847.         db_name(r.fkeydbid),
  13848.         user_name(o2.uid),
  13849.         o2.name,
  13850.         c2.name,
  13851.         convert(binary(16),null),
  13852.         convert(int,null),
  13853.         convert(int,15),
  13854.         'NO ACTION',
  13855.         'NO ACTION',
  13856.         object_name(s.constid),
  13857.         object_name(r.constid),
  13858.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13859.     from
  13860.         sysobjects o1, sysobjects o2,
  13861.         syscolumns c1, syscolumns c2,
  13862.         sysreferences r, sysconstraints s
  13863.     where    
  13864.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13865.     and    o2.name = @fk_table_name
  13866.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13867.     and    o2.id = r.fkeyid
  13868.     and    r.keycnt >= 15
  13869.     and    o2.id = c2.id
  13870.     and    c2.colid = r.fkey15
  13871.     and r.rkeyid = o1.id
  13872.     and s.id = o1.id and (s.status & 0xf) = 1
  13873.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  13874.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13875.     and    o1.id = c1.id    
  13876.     and    c1.colid = r.rkey15
  13877.     union    all
  13878.     select
  13879.         db_name(r.rkeydbid),
  13880.         user_name(o1.uid),
  13881.         o1.name,
  13882.         c1.name,
  13883.         convert(binary(16),null),
  13884.         convert(int,null),
  13885.         db_name(r.fkeydbid),
  13886.         user_name(o2.uid),
  13887.         o2.name,
  13888.         c2.name,
  13889.         convert(binary(16),null),
  13890.         convert(int,null),
  13891.         convert(int,16),
  13892.         'NO ACTION',
  13893.         'NO ACTION',
  13894.         object_name(s.constid),
  13895.         object_name(r.constid),
  13896.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13897.     from
  13898.         sysobjects o1, sysobjects o2,
  13899.         syscolumns c1, syscolumns c2,
  13900.         sysreferences r, sysconstraints s
  13901.     where    
  13902.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13903.     and    o2.name = @fk_table_name
  13904.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13905.     and    o2.id = r.fkeyid
  13906.     and    r.keycnt >= 16
  13907.     and    o2.id = c2.id
  13908.     and    c2.colid = r.fkey16
  13909.     and r.rkeyid = o1.id
  13910.     and s.id = o1.id and (s.status & 0xf) = 1
  13911.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  13912.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13913.     and    o1.id = c1.id    
  13914.     and    c1.colid = r.rkey16
  13915.     order by 8,9,2,3,13
  13916.     END
  13917. ELSE
  13918.     BEGIN
  13919.     insert into #spfkeysrowset1
  13920.     select
  13921.         db_name(r.rkeydbid),
  13922.         user_name(o1.uid),
  13923.         o1.name,
  13924.         c1.name,
  13925.         convert(binary(16),null),
  13926.         convert(int,null),
  13927.         db_name(r.fkeydbid),
  13928.         user_name(o2.uid),
  13929.         o2.name,
  13930.         c2.name,
  13931.         convert(binary(16),null),
  13932.         convert(int,null),
  13933.         convert(int,1),
  13934.         'NO ACTION',
  13935.         'NO ACTION',
  13936.         object_name(s.constid),
  13937.         object_name(r.constid),
  13938.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13939.     from
  13940.         sysobjects o1, sysobjects o2,
  13941.         syscolumns c1, syscolumns c2,
  13942.         sysreferences r, sysconstraints s
  13943.     where    
  13944.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13945.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13946.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13947.     and    o1.id = r.rkeyid
  13948.     and s.id = o1.id and (s.status & 0xf) = 1
  13949.     and    o1.id = c1.id
  13950.     and    c1.colid = r.rkey1
  13951.     and r.fkeyid = o2.id
  13952.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13953.     and    o2.id = c2.id    
  13954.     and    c2.colid = r.fkey1
  13955.     union all
  13956.     select
  13957.         db_name(r.rkeydbid),
  13958.         user_name(o1.uid),
  13959.         o1.name,
  13960.         c1.name,
  13961.         convert(binary(16),null),
  13962.         convert(int,null),
  13963.         db_name(r.fkeydbid),
  13964.         user_name(o2.uid),
  13965.         o2.name,
  13966.         c2.name,
  13967.         convert(binary(16),null),
  13968.         convert(int,null),
  13969.         convert(int,2),
  13970.         'NO ACTION',
  13971.         'NO ACTION',
  13972.         object_name(s.constid),
  13973.         object_name(r.constid),
  13974.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  13975.     from
  13976.         sysobjects o1, sysobjects o2,
  13977.         syscolumns c1, syscolumns c2,
  13978.         sysreferences r, sysconstraints s
  13979.     where    
  13980.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  13981.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  13982.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  13983.     and    o1.id = r.rkeyid
  13984.     and s.id = o1.id and (s.status & 0xf) = 1
  13985.     and    r.keycnt >= 2
  13986.     and    o1.id = c1.id
  13987.     and    c1.colid = r.rkey2
  13988.     and r.fkeyid = o2.id
  13989.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  13990.     and    o2.id = c2.id    
  13991.     and    c2.colid = r.fkey2
  13992.     union all
  13993.     select
  13994.         db_name(r.rkeydbid),
  13995.         user_name(o1.uid),
  13996.         o1.name,
  13997.         c1.name,
  13998.         convert(binary(16),null),
  13999.         convert(int,null),
  14000.         db_name(r.fkeydbid),
  14001.         user_name(o2.uid),
  14002.         o2.name,
  14003.         c2.name,
  14004.         convert(binary(16),null),
  14005.         convert(int,null),
  14006.         convert(int,3),
  14007.         'NO ACTION',
  14008.         'NO ACTION',
  14009.         object_name(s.constid),
  14010.         object_name(r.constid),
  14011.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14012.     from
  14013.         sysobjects o1, sysobjects o2,
  14014.         syscolumns c1, syscolumns c2,
  14015.         sysreferences r, sysconstraints s
  14016.     where    
  14017.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14018.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14019.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14020.     and    o1.id = r.rkeyid
  14021.     and s.id = o1.id and (s.status & 0xf) = 1
  14022.     and    r.keycnt >= 3
  14023.     and    o1.id = c1.id
  14024.     and    c1.colid = r.rkey3
  14025.     and r.fkeyid = o2.id
  14026.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14027.     and    o2.id = c2.id    
  14028.     and    c2.colid = r.fkey3
  14029.     union all
  14030.     select
  14031.         db_name(r.rkeydbid),
  14032.         user_name(o1.uid),
  14033.         o1.name,
  14034.         c1.name,
  14035.         convert(binary(16),null),
  14036.         convert(int,null),
  14037.         db_name(r.fkeydbid),
  14038.         user_name(o2.uid),
  14039.         o2.name,
  14040.         c2.name,
  14041.         convert(binary(16),null),
  14042.         convert(int,null),
  14043.         convert(int,4),
  14044.         'NO ACTION',
  14045.         'NO ACTION',
  14046.         object_name(s.constid),
  14047.         object_name(r.constid),
  14048.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14049.     from
  14050.         sysobjects o1, sysobjects o2,
  14051.         syscolumns c1, syscolumns c2,
  14052.         sysreferences r, sysconstraints s
  14053.     where    
  14054.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14055.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14056.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14057.     and    o1.id = r.rkeyid
  14058.     and s.id = o1.id and (s.status & 0xf) = 1
  14059.     and    r.keycnt >= 4
  14060.     and    o1.id = c1.id
  14061.     and    c1.colid = r.rkey4
  14062.     and r.fkeyid = o2.id
  14063.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14064.     and    o2.id = c2.id    
  14065.     and    c2.colid = r.fkey4
  14066.     union all
  14067.     select
  14068.         db_name(r.rkeydbid),
  14069.         user_name(o1.uid),
  14070.         o1.name,
  14071.         c1.name,
  14072.         convert(binary(16),null),
  14073.         convert(int,null),
  14074.         db_name(r.fkeydbid),
  14075.         user_name(o2.uid),
  14076.         o2.name,
  14077.         c2.name,
  14078.         convert(binary(16),null),
  14079.         convert(int,null),
  14080.         convert(int,5),
  14081.         'NO ACTION',
  14082.         'NO ACTION',
  14083.         object_name(s.constid),
  14084.         object_name(r.constid),
  14085.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14086.     from
  14087.         sysobjects o1, sysobjects o2,
  14088.         syscolumns c1, syscolumns c2,
  14089.         sysreferences r, sysconstraints s
  14090.     where    
  14091.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14092.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14093.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14094.     and    o1.id = r.rkeyid
  14095.     and s.id = o1.id and (s.status & 0xf) = 1
  14096.     and    r.keycnt >= 5
  14097.     and    o1.id = c1.id
  14098.     and    c1.colid = r.rkey5
  14099.     and r.fkeyid = o2.id
  14100.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14101.     and    o2.id = c2.id    
  14102.     and    c2.colid = r.fkey5
  14103.     union all
  14104.     select
  14105.         db_name(r.rkeydbid),
  14106.         user_name(o1.uid),
  14107.         o1.name,
  14108.         c1.name,
  14109.         convert(binary(16),null),
  14110.         convert(int,null),
  14111.         db_name(r.fkeydbid),
  14112.         user_name(o2.uid),
  14113.         o2.name,
  14114.         c2.name,
  14115.         convert(binary(16),null),
  14116.         convert(int,null),
  14117.         convert(int,6),
  14118.         'NO ACTION',
  14119.         'NO ACTION',
  14120.         object_name(s.constid),
  14121.         object_name(r.constid),
  14122.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14123.     from
  14124.         sysobjects o1, sysobjects o2,
  14125.         syscolumns c1, syscolumns c2,
  14126.         sysreferences r, sysconstraints s
  14127.     where    
  14128.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14129.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14130.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14131.     and    o1.id = r.rkeyid
  14132.     and s.id = o1.id and (s.status & 0xf) = 1
  14133.     and    r.keycnt >= 6
  14134.     and    o1.id = c1.id
  14135.     and    c1.colid = r.rkey6
  14136.     and r.fkeyid = o2.id
  14137.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14138.     and    o2.id = c2.id    
  14139.     and    c2.colid = r.fkey6
  14140.     union all
  14141.     select
  14142.         db_name(r.rkeydbid),
  14143.         user_name(o1.uid),
  14144.         o1.name,
  14145.         c1.name,
  14146.         convert(binary(16),null),
  14147.         convert(int,null),
  14148.         db_name(r.fkeydbid),
  14149.         user_name(o2.uid),
  14150.         o2.name,
  14151.         c2.name,
  14152.         convert(binary(16),null),
  14153.         convert(int,null),
  14154.         convert(int,7),
  14155.         'NO ACTION',
  14156.         'NO ACTION',
  14157.         object_name(s.constid),
  14158.         object_name(r.constid),
  14159.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14160.     from
  14161.         sysobjects o1, sysobjects o2,
  14162.         syscolumns c1, syscolumns c2,
  14163.         sysreferences r, sysconstraints s
  14164.     where    
  14165.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14166.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14167.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14168.     and    o1.id = r.rkeyid
  14169.     and s.id = o1.id and (s.status & 0xf) = 1
  14170.     and    r.keycnt >= 7
  14171.     and    o1.id = c1.id
  14172.     and    c1.colid = r.rkey7
  14173.     and r.fkeyid = o2.id
  14174.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14175.     and    o2.id = c2.id    
  14176.     and    c2.colid = r.fkey7
  14177.     union all
  14178.     select
  14179.         db_name(r.rkeydbid),
  14180.         user_name(o1.uid),
  14181.         o1.name,
  14182.         c1.name,
  14183.         convert(binary(16),null),
  14184.         convert(int,null),
  14185.         db_name(r.fkeydbid),
  14186.         user_name(o2.uid),
  14187.         o2.name,
  14188.         c2.name,
  14189.         convert(binary(16),null),
  14190.         convert(int,null),
  14191.         convert(int,8),
  14192.         'NO ACTION',
  14193.         'NO ACTION',
  14194.         object_name(s.constid),
  14195.         object_name(r.constid),
  14196.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14197.     from
  14198.         sysobjects o1, sysobjects o2,
  14199.         syscolumns c1, syscolumns c2,
  14200.         sysreferences r, sysconstraints s
  14201.     where    
  14202.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14203.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14204.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14205.     and    o1.id = r.rkeyid
  14206.     and s.id = o1.id and (s.status & 0xf) = 1
  14207.     and    r.keycnt >= 8
  14208.     and    o1.id = c1.id
  14209.     and    c1.colid = r.rkey8
  14210.     and r.fkeyid = o2.id
  14211.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14212.     and    o2.id = c2.id    
  14213.     and    c2.colid = r.fkey8
  14214.     union all
  14215.     select
  14216.         db_name(r.rkeydbid),
  14217.         user_name(o1.uid),
  14218.         o1.name,
  14219.         c1.name,
  14220.         convert(binary(16),null),
  14221.         convert(int,null),
  14222.         db_name(r.fkeydbid),
  14223.         user_name(o2.uid),
  14224.         o2.name,
  14225.         c2.name,
  14226.         convert(binary(16),null),
  14227.         convert(int,null),
  14228.         convert(int,9),
  14229.         'NO ACTION',
  14230.         'NO ACTION',
  14231.         object_name(s.constid),
  14232.         object_name(r.constid),
  14233.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14234.     from
  14235.         sysobjects o1, sysobjects o2,
  14236.         syscolumns c1, syscolumns c2,
  14237.         sysreferences r, sysconstraints s
  14238.     where    
  14239.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14240.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14241.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14242.     and    o1.id = r.rkeyid
  14243.     and s.id = o1.id and (s.status & 0xf) = 1
  14244.     and    r.keycnt >= 9
  14245.     and    o1.id = c1.id
  14246.     and    c1.colid = r.rkey9
  14247.     and r.fkeyid = o2.id
  14248.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14249.     and    o2.id = c2.id    
  14250.     and    c2.colid = r.fkey9
  14251.     union all
  14252.     select
  14253.         db_name(r.rkeydbid),
  14254.         user_name(o1.uid),
  14255.         o1.name,
  14256.         c1.name,
  14257.         convert(binary(16),null),
  14258.         convert(int,null),
  14259.         db_name(r.fkeydbid),
  14260.         user_name(o2.uid),
  14261.         o2.name,
  14262.         c2.name,
  14263.         convert(binary(16),null),
  14264.         convert(int,null),
  14265.         convert(int,10),
  14266.         'NO ACTION',
  14267.         'NO ACTION',
  14268.         object_name(s.constid),
  14269.         object_name(r.constid),
  14270.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14271.     from
  14272.         sysobjects o1, sysobjects o2,
  14273.         syscolumns c1, syscolumns c2,
  14274.         sysreferences r, sysconstraints s
  14275.     where    
  14276.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14277.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14278.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14279.     and    o1.id = r.rkeyid
  14280.     and s.id = o1.id and (s.status & 0xf) = 1
  14281.     and    r.keycnt >= 10
  14282.     and    o1.id = c1.id
  14283.     and    c1.colid = r.rkey10
  14284.     and r.fkeyid = o2.id
  14285.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14286.     and    o2.id = c2.id    
  14287.     and    c2.colid = r.fkey10
  14288.     union all
  14289.     select
  14290.         db_name(r.rkeydbid),
  14291.         user_name(o1.uid),
  14292.         o1.name,
  14293.         c1.name,
  14294.         convert(binary(16),null),
  14295.         convert(int,null),
  14296.         db_name(r.fkeydbid),
  14297.         user_name(o2.uid),
  14298.         o2.name,
  14299.         c2.name,
  14300.         convert(binary(16),null),
  14301.         convert(int,null),
  14302.         convert(int,11),
  14303.         'NO ACTION',
  14304.         'NO ACTION',
  14305.         object_name(s.constid),
  14306.         object_name(r.constid),
  14307.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14308.     from
  14309.         sysobjects o1, sysobjects o2,
  14310.         syscolumns c1, syscolumns c2,
  14311.         sysreferences r, sysconstraints s
  14312.     where    
  14313.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14314.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14315.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14316.     and    o1.id = r.rkeyid
  14317.     and s.id = o1.id and (s.status & 0xf) = 1
  14318.     and    r.keycnt >= 11
  14319.     and    o1.id = c1.id
  14320.     and    c1.colid = r.rkey11
  14321.     and r.fkeyid = o2.id
  14322.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14323.     and    o2.id = c2.id    
  14324.     and    c2.colid = r.fkey11
  14325.     union all
  14326.     select
  14327.         db_name(r.rkeydbid),
  14328.         user_name(o1.uid),
  14329.         o1.name,
  14330.         c1.name,
  14331.         convert(binary(16),null),
  14332.         convert(int,null),
  14333.         db_name(r.fkeydbid),
  14334.         user_name(o2.uid),
  14335.         o2.name,
  14336.         c2.name,
  14337.         convert(binary(16),null),
  14338.         convert(int,null),
  14339.         convert(int,12),
  14340.         'NO ACTION',
  14341.         'NO ACTION',
  14342.         object_name(s.constid),
  14343.         object_name(r.constid),
  14344.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14345.     from
  14346.         sysobjects o1, sysobjects o2,
  14347.         syscolumns c1, syscolumns c2,
  14348.         sysreferences r, sysconstraints s
  14349.     where    
  14350.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14351.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14352.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14353.     and    o1.id = r.rkeyid
  14354.     and s.id = o1.id and (s.status & 0xf) = 1
  14355.     and    r.keycnt >= 12
  14356.     and    o1.id = c1.id
  14357.     and    c1.colid = r.rkey12
  14358.     and r.fkeyid = o2.id
  14359.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14360.     and    o2.id = c2.id    
  14361.     and    c2.colid = r.fkey12
  14362.     union all
  14363.     select
  14364.         db_name(r.rkeydbid),
  14365.         user_name(o1.uid),
  14366.         o1.name,
  14367.         c1.name,
  14368.         convert(binary(16),null),
  14369.         convert(int,null),
  14370.         db_name(r.fkeydbid),
  14371.         user_name(o2.uid),
  14372.         o2.name,
  14373.         c2.name,
  14374.         convert(binary(16),null),
  14375.         convert(int,null),
  14376.         convert(int,13),
  14377.         'NO ACTION',
  14378.         'NO ACTION',
  14379.         object_name(s.constid),
  14380.         object_name(r.constid),
  14381.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14382.     from
  14383.         sysobjects o1, sysobjects o2,
  14384.         syscolumns c1, syscolumns c2,
  14385.         sysreferences r, sysconstraints s
  14386.     where    
  14387.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14388.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14389.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14390.     and    o1.id = r.rkeyid
  14391.     and s.id = o1.id and (s.status & 0xf) = 1
  14392.     and    r.keycnt >= 13
  14393.     and    o1.id = c1.id
  14394.     and    c1.colid = r.rkey13
  14395.     and r.fkeyid = o2.id
  14396.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14397.     and    o2.id = c2.id    
  14398.     and    c2.colid = r.fkey13
  14399.     union all
  14400.     select
  14401.         db_name(r.rkeydbid),
  14402.         user_name(o1.uid),
  14403.         o1.name,
  14404.         c1.name,
  14405.         convert(binary(16),null),
  14406.         convert(int,null),
  14407.         db_name(r.fkeydbid),
  14408.         user_name(o2.uid),
  14409.         o2.name,
  14410.         c2.name,
  14411.         convert(binary(16),null),
  14412.         convert(int,null),
  14413.         convert(int,14),
  14414.         'NO ACTION',
  14415.         'NO ACTION',
  14416.         object_name(s.constid),
  14417.         object_name(r.constid),
  14418.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14419.     from
  14420.         sysobjects o1, sysobjects o2,
  14421.         syscolumns c1, syscolumns c2,
  14422.         sysreferences r, sysconstraints s
  14423.     where    
  14424.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14425.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14426.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14427.     and    o1.id = r.rkeyid
  14428.     and s.id = o1.id and (s.status & 0xf) = 1
  14429.     and    r.keycnt >= 14
  14430.     and    o1.id = c1.id
  14431.     and    c1.colid = r.rkey14
  14432.     and r.fkeyid = o2.id
  14433.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14434.     and    o2.id = c2.id    
  14435.     and    c2.colid = r.fkey14
  14436.     union all
  14437.     select
  14438.         db_name(r.rkeydbid),
  14439.         user_name(o1.uid),
  14440.         o1.name,
  14441.         c1.name,
  14442.         convert(binary(16),null),
  14443.         convert(int,null),
  14444.         db_name(r.fkeydbid),
  14445.         user_name(o2.uid),
  14446.         o2.name,
  14447.         c2.name,
  14448.         convert(binary(16),null),
  14449.         convert(int,null),
  14450.         convert(int,15),
  14451.         'NO ACTION',
  14452.         'NO ACTION',
  14453.         object_name(s.constid),
  14454.         object_name(r.constid),
  14455.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14456.     from
  14457.         sysobjects o1, sysobjects o2,
  14458.         syscolumns c1, syscolumns c2,
  14459.         sysreferences r, sysconstraints s
  14460.     where    
  14461.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14462.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14463.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14464.     and    o1.id = r.rkeyid
  14465.     and s.id = o1.id and (s.status & 0xf) = 1
  14466.     and    r.keycnt >= 15
  14467.     and    o1.id = c1.id
  14468.     and    c1.colid = r.rkey15
  14469.     and r.fkeyid = o2.id
  14470.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14471.     and    o2.id = c2.id    
  14472.     and    c2.colid = r.fkey15
  14473.     union all
  14474.     select
  14475.         db_name(r.rkeydbid),
  14476.         user_name(o1.uid),
  14477.         o1.name,
  14478.         c1.name,
  14479.         convert(binary(16),null),
  14480.         convert(int,null),
  14481.         db_name(r.fkeydbid),
  14482.         user_name(o2.uid),
  14483.         o2.name,
  14484.         c2.name,
  14485.         convert(binary(16),null),
  14486.         convert(int,null),
  14487.         convert(int,16),
  14488.         'NO ACTION',
  14489.         'NO ACTION',
  14490.         object_name(s.constid),
  14491.         object_name(r.constid),
  14492.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14493.     from
  14494.         sysobjects o1, sysobjects o2,
  14495.         syscolumns c1, syscolumns c2,
  14496.         sysreferences r, sysconstraints s
  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 s.id = o1.id and (s.status & 0xf) = 1
  14503.     and    o1.id = c1.id
  14504.     and    r.keycnt >= 16
  14505.     and    c1.colid = r.rkey16
  14506.     and r.fkeyid = o2.id
  14507.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14508.     and    o2.id = c2.id    
  14509.     and    c2.colid = r.fkey16
  14510.     order by 8,9,2,3,13
  14511.     END
  14512.  
  14513.     exec @ret = sp_cursoropen @handle output, 'select * from #spfkeysrowset1',
  14514.         @scrollopt output, @ccopt output, @rows output
  14515.  
  14516.     drop table #spfkeysrowset1
  14517.        return isnull(@ret,0)
  14518. go
  14519. dump tran master with no_log
  14520. go
  14521. CREATE PROCEDURE sp_foreign_keys_rowset;3
  14522. as
  14523.     select
  14524.         PK_TABLE_CATALOG    = convert(sysname,' '),
  14525.         PK_TABLE_SCHEMA        = convert(sysname,' '),
  14526.         PK_TABLE_NAME         = convert(sysname,' '),
  14527.         PK_COLUMN_NAME         = convert(sysname,' '),
  14528.         PK_COLUMN_GUID        = convert(binary(16),null),
  14529.         PK_COLUMN_PROPID    = convert(int,null),
  14530.         FK_TABLE_CATALOG    = convert(sysname,' '),
  14531.         FK_TABLE_SCHEMA        = convert(sysname,' '),
  14532.         FK_TABLE_NAME         = convert(sysname,' '),
  14533.         FK_COLUMN_NAME         = convert(sysname,' '),
  14534.         FK_COLUMN_GUID        = convert(binary(16),null),
  14535.         FK_COLUMN_PROPID    = convert(int,null),
  14536.         ORDINAL                = convert(int,1),
  14537.         UPDATE_RULE            = 'NO ACTION',
  14538.         DELETE_RULE         = 'NO ACTION',
  14539.         PK_NAME                = convert(sysname, ' '),
  14540.         FK_NAME                = convert(sysname, ' '),
  14541.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14542.     where    1=0
  14543. go
  14544. dump tran master with no_log
  14545. go
  14546.  
  14547.  
  14548. if (charindex('7.00', @@version) = 0)
  14549. begin
  14550.     print ''
  14551.     print ''
  14552.     print 'Warning:'
  14553.     print 'you are installing the stored procedures '
  14554.     print 'on a pre 7.0 SQL Server.'
  14555.     print 'Ignore the following errors.'
  14556. end
  14557. else
  14558.     drop proc sp_foreign_keys_rowset
  14559. go
  14560.  
  14561.  
  14562. /*    Procedure for 7.0 server */
  14563. CREATE PROCEDURE sp_foreign_keys_rowset
  14564.     (
  14565.     @pk_table_name        sysname,
  14566.     @pk_table_schema    sysname = null,
  14567.     @fk_table_name        sysname = null,
  14568.     @fk_table_schema    sysname = null,
  14569.     @fk_table_catalog    sysname = null
  14570.     )
  14571. as
  14572.     select
  14573.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14574.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14575.         PK_TABLE_NAME         = o1.name,
  14576.         PK_COLUMN_NAME         = c1.name,
  14577.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  14578.         PK_COLUMN_PROPID    = convert(int,null),
  14579.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14580.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  14581.         FK_TABLE_NAME         = o2.name,
  14582.         FK_COLUMN_NAME         = c2.name,
  14583.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  14584.         FK_COLUMN_PROPID    = convert(int,null),
  14585.         ORDINAL                = convert(int,1),
  14586.         UPDATE_RULE            = N'NO ACTION',
  14587.         DELETE_RULE         = N'NO ACTION',
  14588.         PK_NAME                = object_name(s.constid),
  14589.         FK_NAME                = object_name(r.constid),
  14590.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14591.     from
  14592.         sysobjects o1, sysobjects o2,
  14593.         syscolumns c1, syscolumns c2,
  14594.         sysreferences r, sysconstraints s
  14595.     where    
  14596.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14597.     and    o1.name = @pk_table_name
  14598.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14599.     and    o1.id = r.rkeyid
  14600.     and s.id = o1.id and (s.status & 0xf) = 1
  14601.     and    o1.id = c1.id
  14602.     and    c1.colid = r.rkey1
  14603.     and r.fkeyid = o2.id
  14604.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  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.fkey1
  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(uniqueidentifier,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(uniqueidentifier,null),
  14621.         FK_COLUMN_PROPID    = convert(int,null),
  14622.         ORDINAL                = convert(int,2),
  14623.         UPDATE_RULE            = N'NO ACTION',
  14624.         DELETE_RULE         = N'NO ACTION',
  14625.         PK_NAME                = object_name(s.constid),
  14626.         FK_NAME                = object_name(r.constid),
  14627.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14628.     from
  14629.         sysobjects o1, sysobjects o2,
  14630.         syscolumns c1, syscolumns c2,
  14631.         sysreferences r, sysconstraints s
  14632.     where    
  14633.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14634.     and    o1.name = @pk_table_name
  14635.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14636.     and    o1.id = r.rkeyid
  14637.     and s.id = o1.id and (s.status & 0xf) = 1
  14638.     and    o1.id = c1.id
  14639.     and    c1.colid = r.rkey2
  14640.     and    r.keycnt >= 2
  14641.     and r.fkeyid = o2.id
  14642.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  14643.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14644.     and    o2.id = c2.id    
  14645.     and    c2.colid = r.fkey2
  14646.     union all
  14647.     select
  14648.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14649.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14650.         PK_TABLE_NAME         = o1.name,
  14651.         PK_COLUMN_NAME         = c1.name,
  14652.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  14653.         PK_COLUMN_PROPID    = convert(int,null),
  14654.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14655.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  14656.         FK_TABLE_NAME         = o2.name,
  14657.         FK_COLUMN_NAME         = c2.name,
  14658.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  14659.         FK_COLUMN_PROPID    = convert(int,null),
  14660.         ORDINAL                = convert(int,3),
  14661.         UPDATE_RULE            = N'NO ACTION',
  14662.         DELETE_RULE         = N'NO ACTION',
  14663.         PK_NAME                = object_name(s.constid),
  14664.         FK_NAME                = object_name(r.constid),
  14665.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14666.     from
  14667.         sysobjects o1, sysobjects o2,
  14668.         syscolumns c1, syscolumns c2,
  14669.         sysreferences r, sysconstraints s
  14670.     where    
  14671.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14672.     and    o1.name = @pk_table_name
  14673.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14674.     and    o1.id = r.rkeyid
  14675.     and s.id = o1.id and (s.status & 0xf) = 1
  14676.     and    o1.id = c1.id
  14677.     and    c1.colid = r.rkey3
  14678.     and    r.keycnt >= 3
  14679.     and r.fkeyid = o2.id
  14680.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  14681.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14682.     and    o2.id = c2.id    
  14683.     and    c2.colid = r.fkey3
  14684.     union all
  14685.     select
  14686.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14687.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14688.         PK_TABLE_NAME         = o1.name,
  14689.         PK_COLUMN_NAME         = c1.name,
  14690.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  14691.         PK_COLUMN_PROPID    = convert(int,null),
  14692.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14693.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  14694.         FK_TABLE_NAME         = o2.name,
  14695.         FK_COLUMN_NAME         = c2.name,
  14696.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  14697.         FK_COLUMN_PROPID    = convert(int,null),
  14698.         ORDINAL                = convert(int,4),
  14699.         UPDATE_RULE            = N'NO ACTION',
  14700.         DELETE_RULE         = N'NO ACTION',
  14701.         PK_NAME                = object_name(s.constid),
  14702.         FK_NAME                = object_name(r.constid),
  14703.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14704.     from
  14705.         sysobjects o1, sysobjects o2,
  14706.         syscolumns c1, syscolumns c2,
  14707.         sysreferences r, sysconstraints s
  14708.     where    
  14709.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14710.     and    o1.name = @pk_table_name
  14711.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14712.     and    o1.id = r.rkeyid
  14713.     and s.id = o1.id and (s.status & 0xf) = 1
  14714.     and    r.keycnt >= 4
  14715.     and    o1.id = c1.id
  14716.     and    c1.colid = r.rkey4
  14717.     and r.fkeyid = o2.id
  14718.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  14719.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14720.     and    o2.id = c2.id    
  14721.     and    c2.colid = r.fkey4
  14722.     union all
  14723.     select
  14724.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14725.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14726.         PK_TABLE_NAME         = o1.name,
  14727.         PK_COLUMN_NAME         = c1.name,
  14728.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  14729.         PK_COLUMN_PROPID    = convert(int,null),
  14730.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14731.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  14732.         FK_TABLE_NAME         = o2.name,
  14733.         FK_COLUMN_NAME         = c2.name,
  14734.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  14735.         FK_COLUMN_PROPID    = convert(int,null),
  14736.         ORDINAL                = convert(int,5),
  14737.         UPDATE_RULE            = N'NO ACTION',
  14738.         DELETE_RULE         = N'NO ACTION',
  14739.         PK_NAME                = object_name(s.constid),
  14740.         FK_NAME                = object_name(r.constid),
  14741.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14742.     from
  14743.         sysobjects o1, sysobjects o2,
  14744.         syscolumns c1, syscolumns c2,
  14745.         sysreferences r, sysconstraints s
  14746.     where    
  14747.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14748.     and    o1.name = @pk_table_name
  14749.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14750.     and    o1.id = r.rkeyid
  14751.     and s.id = o1.id and (s.status & 0xf) = 1
  14752.     and    r.keycnt >= 5
  14753.     and    o1.id = c1.id
  14754.     and    c1.colid = r.rkey5
  14755.     and r.fkeyid = o2.id
  14756.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  14757.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14758.     and    o2.id = c2.id    
  14759.     and    c2.colid = r.fkey5
  14760.     union all
  14761.     select
  14762.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14763.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14764.         PK_TABLE_NAME         = o1.name,
  14765.         PK_COLUMN_NAME         = c1.name,
  14766.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  14767.         PK_COLUMN_PROPID    = convert(int,null),
  14768.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14769.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  14770.         FK_TABLE_NAME         = o2.name,
  14771.         FK_COLUMN_NAME         = c2.name,
  14772.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  14773.         FK_COLUMN_PROPID    = convert(int,null),
  14774.         ORDINAL                = convert(int,6),
  14775.         UPDATE_RULE            = N'NO ACTION',
  14776.         DELETE_RULE         = N'NO ACTION',
  14777.         PK_NAME                = object_name(s.constid),
  14778.         FK_NAME                = object_name(r.constid),
  14779.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14780.     from
  14781.         sysobjects o1, sysobjects o2,
  14782.         syscolumns c1, syscolumns c2,
  14783.         sysreferences r, sysconstraints s
  14784.     where    
  14785.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14786.     and    o1.name = @pk_table_name
  14787.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14788.     and    o1.id = r.rkeyid
  14789.     and s.id = o1.id and (s.status & 0xf) = 1
  14790.     and    r.keycnt >= 6
  14791.     and    o1.id = c1.id
  14792.     and    c1.colid = r.rkey6
  14793.     and r.fkeyid = o2.id
  14794.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  14795.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14796.     and    o2.id = c2.id    
  14797.     and    c2.colid = r.fkey6
  14798.     union all
  14799.     select
  14800.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14801.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14802.         PK_TABLE_NAME         = o1.name,
  14803.         PK_COLUMN_NAME         = c1.name,
  14804.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  14805.         PK_COLUMN_PROPID    = convert(int,null),
  14806.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14807.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  14808.         FK_TABLE_NAME         = o2.name,
  14809.         FK_COLUMN_NAME         = c2.name,
  14810.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  14811.         FK_COLUMN_PROPID    = convert(int,null),
  14812.         ORDINAL                = convert(int,7),
  14813.         UPDATE_RULE            = N'NO ACTION',
  14814.         DELETE_RULE         = N'NO ACTION',
  14815.         PK_NAME                = object_name(s.constid),
  14816.         FK_NAME                = object_name(r.constid),
  14817.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14818.     from
  14819.         sysobjects o1, sysobjects o2,
  14820.         syscolumns c1, syscolumns c2,
  14821.         sysreferences r, sysconstraints s
  14822.     where    
  14823.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14824.     and    o1.name = @pk_table_name
  14825.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14826.     and    o1.id = r.rkeyid
  14827.     and s.id = o1.id and (s.status & 0xf) = 1
  14828.     and    r.keycnt >= 7
  14829.     and    o1.id = c1.id
  14830.     and    c1.colid = r.rkey7
  14831.     and r.fkeyid = o2.id
  14832.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  14833.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14834.     and    o2.id = c2.id    
  14835.     and    c2.colid = r.fkey7
  14836.     union all
  14837.     select
  14838.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14839.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14840.         PK_TABLE_NAME         = o1.name,
  14841.         PK_COLUMN_NAME         = c1.name,
  14842.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  14843.         PK_COLUMN_PROPID    = convert(int,null),
  14844.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14845.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  14846.         FK_TABLE_NAME         = o2.name,
  14847.         FK_COLUMN_NAME         = c2.name,
  14848.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  14849.         FK_COLUMN_PROPID    = convert(int,null),
  14850.         ORDINAL                = convert(int,8),
  14851.         UPDATE_RULE            = N'NO ACTION',
  14852.         DELETE_RULE         = N'NO ACTION',
  14853.         PK_NAME                = object_name(s.constid),
  14854.         FK_NAME                = object_name(r.constid),
  14855.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14856.     from
  14857.         sysobjects o1, sysobjects o2,
  14858.         syscolumns c1, syscolumns c2,
  14859.         sysreferences r, sysconstraints s
  14860.     where    
  14861.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14862.     and    o1.name = @pk_table_name
  14863.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14864.     and    o1.id = r.rkeyid
  14865.     and s.id = o1.id and (s.status & 0xf) = 1
  14866.     and    r.keycnt >= 8
  14867.     and    o1.id = c1.id
  14868.     and    c1.colid = r.rkey8
  14869.     and r.fkeyid = o2.id
  14870.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  14871.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14872.     and    o2.id = c2.id    
  14873.     and    c2.colid = r.fkey8
  14874.     union all
  14875.     select
  14876.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14877.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14878.         PK_TABLE_NAME         = o1.name,
  14879.         PK_COLUMN_NAME         = c1.name,
  14880.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  14881.         PK_COLUMN_PROPID    = convert(int,null),
  14882.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14883.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  14884.         FK_TABLE_NAME         = o2.name,
  14885.         FK_COLUMN_NAME         = c2.name,
  14886.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  14887.         FK_COLUMN_PROPID    = convert(int,null),
  14888.         ORDINAL                = convert(int,9),
  14889.         UPDATE_RULE            = N'NO ACTION',
  14890.         DELETE_RULE         = N'NO ACTION',
  14891.         PK_NAME                = object_name(s.constid),
  14892.         FK_NAME                = object_name(r.constid),
  14893.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14894.     from
  14895.         sysobjects o1, sysobjects o2,
  14896.         syscolumns c1, syscolumns c2,
  14897.         sysreferences r, sysconstraints s
  14898.     where    
  14899.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14900.     and    o1.name = @pk_table_name
  14901.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14902.     and    o1.id = r.rkeyid
  14903.     and s.id = o1.id and (s.status & 0xf) = 1
  14904.     and    r.keycnt >= 9
  14905.     and    o1.id = c1.id
  14906.     and    c1.colid = r.rkey9
  14907.     and r.fkeyid = o2.id
  14908.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  14909.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14910.     and    o2.id = c2.id    
  14911.     and    c2.colid = r.fkey9
  14912.     union all
  14913.     select
  14914.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14915.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14916.         PK_TABLE_NAME         = o1.name,
  14917.         PK_COLUMN_NAME         = c1.name,
  14918.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  14919.         PK_COLUMN_PROPID    = convert(int,null),
  14920.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14921.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  14922.         FK_TABLE_NAME         = o2.name,
  14923.         FK_COLUMN_NAME         = c2.name,
  14924.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  14925.         FK_COLUMN_PROPID    = convert(int,null),
  14926.         ORDINAL                = convert(int,10),
  14927.         UPDATE_RULE            = N'NO ACTION',
  14928.         DELETE_RULE         = N'NO ACTION',
  14929.         PK_NAME                = object_name(s.constid),
  14930.         FK_NAME                = object_name(r.constid),
  14931.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14932.     from
  14933.         sysobjects o1, sysobjects o2,
  14934.         syscolumns c1, syscolumns c2,
  14935.         sysreferences r, sysconstraints s
  14936.     where    
  14937.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14938.     and    o1.name = @pk_table_name
  14939.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14940.     and    o1.id = r.rkeyid
  14941.     and s.id = o1.id and (s.status & 0xf) = 1
  14942.     and    r.keycnt >= 10
  14943.     and    o1.id = c1.id
  14944.     and    c1.colid = r.rkey10
  14945.     and r.fkeyid = o2.id
  14946.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  14947.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14948.     and    o2.id = c2.id    
  14949.     and    c2.colid = r.fkey10
  14950.     union all
  14951.     select
  14952.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14953.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14954.         PK_TABLE_NAME         = o1.name,
  14955.         PK_COLUMN_NAME         = c1.name,
  14956.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  14957.         PK_COLUMN_PROPID    = convert(int,null),
  14958.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14959.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  14960.         FK_TABLE_NAME         = o2.name,
  14961.         FK_COLUMN_NAME         = c2.name,
  14962.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  14963.         FK_COLUMN_PROPID    = convert(int,null),
  14964.         ORDINAL                = convert(int,11),
  14965.         UPDATE_RULE            = N'NO ACTION',
  14966.         DELETE_RULE         = N'NO ACTION',
  14967.         PK_NAME                = object_name(s.constid),
  14968.         FK_NAME                = object_name(r.constid),
  14969.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14970.     from
  14971.         sysobjects o1, sysobjects o2,
  14972.         syscolumns c1, syscolumns c2,
  14973.         sysreferences r, sysconstraints s
  14974.     where    
  14975.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14976.     and    o1.name = @pk_table_name
  14977.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14978.     and    o1.id = r.rkeyid
  14979.     and s.id = o1.id and (s.status & 0xf) = 1
  14980.     and    r.keycnt >= 11
  14981.     and    o1.id = c1.id
  14982.     and    c1.colid = r.rkey11
  14983.     and r.fkeyid = o2.id
  14984.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  14985.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14986.     and    o2.id = c2.id    
  14987.     and    c2.colid = r.fkey11
  14988.     union all
  14989.     select
  14990.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14991.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14992.         PK_TABLE_NAME         = o1.name,
  14993.         PK_COLUMN_NAME         = c1.name,
  14994.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  14995.         PK_COLUMN_PROPID    = convert(int,null),
  14996.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14997.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  14998.         FK_TABLE_NAME         = o2.name,
  14999.         FK_COLUMN_NAME         = c2.name,
  15000.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15001.         FK_COLUMN_PROPID    = convert(int,null),
  15002.         ORDINAL                = convert(int,12),
  15003.         UPDATE_RULE            = N'NO ACTION',
  15004.         DELETE_RULE         = N'NO ACTION',
  15005.         PK_NAME                = object_name(s.constid),
  15006.         FK_NAME                = object_name(r.constid),
  15007.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15008.     from
  15009.         sysobjects o1, sysobjects o2,
  15010.         syscolumns c1, syscolumns c2,
  15011.         sysreferences r, sysconstraints s
  15012.     where    
  15013.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15014.     and    o1.name = @pk_table_name
  15015.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15016.     and    o1.id = r.rkeyid
  15017.     and s.id = o1.id and (s.status & 0xf) = 1
  15018.     and    r.keycnt >= 12
  15019.     and    o1.id = c1.id
  15020.     and    c1.colid = r.rkey12
  15021.     and r.fkeyid = o2.id
  15022.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  15023.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15024.     and    o2.id = c2.id    
  15025.     and    c2.colid = r.fkey12
  15026.     union all
  15027.     select
  15028.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15029.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15030.         PK_TABLE_NAME         = o1.name,
  15031.         PK_COLUMN_NAME         = c1.name,
  15032.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15033.         PK_COLUMN_PROPID    = convert(int,null),
  15034.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15035.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  15036.         FK_TABLE_NAME         = o2.name,
  15037.         FK_COLUMN_NAME         = c2.name,
  15038.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15039.         FK_COLUMN_PROPID    = convert(int,null),
  15040.         ORDINAL                = convert(int,13),
  15041.         UPDATE_RULE            = N'NO ACTION',
  15042.         DELETE_RULE         = N'NO ACTION',
  15043.         PK_NAME                = object_name(s.constid),
  15044.         FK_NAME                = object_name(r.constid),
  15045.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15046.     from
  15047.         sysobjects o1, sysobjects o2,
  15048.         syscolumns c1, syscolumns c2,
  15049.         sysreferences r, sysconstraints s
  15050.     where    
  15051.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15052.     and    o1.name = @pk_table_name
  15053.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15054.     and    o1.id = r.rkeyid
  15055.     and s.id = o1.id and (s.status & 0xf) = 1
  15056.     and    r.keycnt >= 13
  15057.     and    o1.id = c1.id
  15058.     and    c1.colid = r.rkey13
  15059.     and r.fkeyid = o2.id
  15060.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  15061.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15062.     and    o2.id = c2.id    
  15063.     and    c2.colid = r.fkey13
  15064.     union all
  15065.     select
  15066.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15067.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15068.         PK_TABLE_NAME         = o1.name,
  15069.         PK_COLUMN_NAME         = c1.name,
  15070.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15071.         PK_COLUMN_PROPID    = convert(int,null),
  15072.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15073.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  15074.         FK_TABLE_NAME         = o2.name,
  15075.         FK_COLUMN_NAME         = c2.name,
  15076.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15077.         FK_COLUMN_PROPID    = convert(int,null),
  15078.         ORDINAL                = convert(int,14),
  15079.         UPDATE_RULE            = N'NO ACTION',
  15080.         DELETE_RULE         = N'NO ACTION',
  15081.         PK_NAME                = object_name(s.constid),
  15082.         FK_NAME                = object_name(r.constid),
  15083.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15084.     from
  15085.         sysobjects o1, sysobjects o2,
  15086.         syscolumns c1, syscolumns c2,
  15087.         sysreferences r, sysconstraints s
  15088.     where    
  15089.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15090.     and    o1.name = @pk_table_name
  15091.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15092.     and    o1.id = r.rkeyid
  15093.     and s.id = o1.id and (s.status & 0xf) = 1
  15094.     and    r.keycnt >= 14
  15095.     and    o1.id = c1.id
  15096.     and    c1.colid = r.rkey14
  15097.     and r.fkeyid = o2.id
  15098.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  15099.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15100.     and    o2.id = c2.id    
  15101.     and    c2.colid = r.fkey14
  15102.     union all
  15103.     select
  15104.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15105.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15106.         PK_TABLE_NAME         = o1.name,
  15107.         PK_COLUMN_NAME         = c1.name,
  15108.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15109.         PK_COLUMN_PROPID    = convert(int,null),
  15110.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15111.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  15112.         FK_TABLE_NAME         = o2.name,
  15113.         FK_COLUMN_NAME         = c2.name,
  15114.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15115.         FK_COLUMN_PROPID    = convert(int,null),
  15116.         ORDINAL                = convert(int,15),
  15117.         UPDATE_RULE            = N'NO ACTION',
  15118.         DELETE_RULE         = N'NO ACTION',
  15119.         PK_NAME                = object_name(s.constid),
  15120.         FK_NAME                = object_name(r.constid),
  15121.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15122.     from
  15123.         sysobjects o1, sysobjects o2,
  15124.         syscolumns c1, syscolumns c2,
  15125.         sysreferences r, sysconstraints s
  15126.     where    
  15127.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15128.     and    o1.name = @pk_table_name
  15129.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15130.     and    o1.id = r.rkeyid
  15131.     and s.id = o1.id and (s.status & 0xf) = 1
  15132.     and    r.keycnt >= 15
  15133.     and    o1.id = c1.id
  15134.     and    c1.colid = r.rkey15
  15135.     and r.fkeyid = o2.id
  15136.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  15137.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15138.     and    o2.id = c2.id    
  15139.     and    c2.colid = r.fkey15
  15140.     union all
  15141.     select
  15142.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15143.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15144.         PK_TABLE_NAME         = o1.name,
  15145.         PK_COLUMN_NAME         = c1.name,
  15146.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15147.         PK_COLUMN_PROPID    = convert(int,null),
  15148.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15149.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  15150.         FK_TABLE_NAME         = o2.name,
  15151.         FK_COLUMN_NAME         = c2.name,
  15152.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15153.         FK_COLUMN_PROPID    = convert(int,null),
  15154.         ORDINAL                = convert(int,16),
  15155.         UPDATE_RULE            = N'NO ACTION',
  15156.         DELETE_RULE         = N'NO ACTION',
  15157.         PK_NAME                = object_name(s.constid),
  15158.         FK_NAME                = object_name(r.constid),
  15159.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15160.     from
  15161.         sysobjects o1, sysobjects o2,
  15162.         syscolumns c1, syscolumns c2,
  15163.         sysreferences r, sysconstraints s
  15164.     where    
  15165.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15166.     and    o1.name = @pk_table_name
  15167.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15168.     and    o1.id = r.rkeyid
  15169.     and s.id = o1.id and (s.status & 0xf) = 1
  15170.     and    r.keycnt >= 16
  15171.     and    o1.id = c1.id
  15172.     and    c1.colid = r.rkey16
  15173.     and r.fkeyid = o2.id
  15174.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  15175.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15176.     and    o2.id = c2.id    
  15177.     and    c2.colid = r.fkey16
  15178.     order by 8,9,2,3,13
  15179. go
  15180. dump tran master with no_log
  15181. go
  15182. CREATE PROCEDURE sp_foreign_keys_rowset;2
  15183.     (
  15184.     @fk_table_name        sysname,
  15185.     @fk_table_schema    sysname = null,
  15186.     @pk_table_name        sysname = null,
  15187.     @pk_table_schema    sysname = null,
  15188.     @pk_table_catalog    sysname = null
  15189.     )
  15190. as
  15191.     select
  15192.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15193.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15194.         PK_TABLE_NAME         = o1.name,
  15195.         PK_COLUMN_NAME         = c1.name,
  15196.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15197.         PK_COLUMN_PROPID    = convert(int,null),
  15198.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15199.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  15200.         FK_TABLE_NAME         = o2.name,
  15201.         FK_COLUMN_NAME         = c2.name,
  15202.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15203.         FK_COLUMN_PROPID    = convert(int,null),
  15204.         ORDINAL                = convert(int,1),
  15205.         UPDATE_RULE            = N'NO ACTION',
  15206.         DELETE_RULE         = N'NO ACTION',
  15207.         PK_NAME                = object_name(s.constid),
  15208.         FK_NAME                = object_name(r.constid),
  15209.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15210.     from
  15211.         sysobjects o1, sysobjects o2,
  15212.         syscolumns c1, syscolumns c2,
  15213.         sysreferences r, sysconstraints s
  15214.     where    
  15215.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15216.     and    o2.name = @fk_table_name
  15217.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15218.     and    o2.id = r.fkeyid
  15219.     and    o2.id = c2.id
  15220.     and    c2.colid = r.fkey1
  15221.     and r.rkeyid = o1.id
  15222.     and s.id = o1.id and (s.status & 0xf) = 1
  15223.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  15224.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15225.     and    o1.id = c1.id    
  15226.     and    c1.colid = r.rkey1
  15227.     union    all
  15228.     select
  15229.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15230.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15231.         PK_TABLE_NAME         = o1.name,
  15232.         PK_COLUMN_NAME         = c1.name,
  15233.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15234.         PK_COLUMN_PROPID    = convert(int,null),
  15235.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15236.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  15237.         FK_TABLE_NAME         = o2.name,
  15238.         FK_COLUMN_NAME         = c2.name,
  15239.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15240.         FK_COLUMN_PROPID    = convert(int,null),
  15241.         ORDINAL                = convert(int,2),
  15242.         UPDATE_RULE            = N'NO ACTION',
  15243.         DELETE_RULE         = N'NO ACTION',
  15244.         PK_NAME                = object_name(s.constid),
  15245.         FK_NAME                = object_name(r.constid),
  15246.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15247.     from
  15248.         sysobjects o1, sysobjects o2,
  15249.         syscolumns c1, syscolumns c2,
  15250.         sysreferences r, sysconstraints s
  15251.     where    
  15252.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15253.     and    o2.name = @fk_table_name
  15254.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15255.     and    o2.id = r.fkeyid
  15256.     and    r.keycnt >= 2
  15257.     and    o2.id = c2.id
  15258.     and    c2.colid = r.fkey2
  15259.     and    r.rkeyid = o1.id
  15260.     and s.id = o1.id and (s.status & 0xf) = 1
  15261.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  15262.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15263.     and    o1.id = c1.id    
  15264.     and    c1.colid = r.rkey2
  15265.     union    all
  15266.     select
  15267.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15268.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15269.         PK_TABLE_NAME         = o1.name,
  15270.         PK_COLUMN_NAME         = c1.name,
  15271.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15272.         PK_COLUMN_PROPID    = convert(int,null),
  15273.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15274.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  15275.         FK_TABLE_NAME         = o2.name,
  15276.         FK_COLUMN_NAME         = c2.name,
  15277.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15278.         FK_COLUMN_PROPID    = convert(int,null),
  15279.         ORDINAL                = convert(int,3),
  15280.         UPDATE_RULE            = N'NO ACTION',
  15281.         DELETE_RULE         = N'NO ACTION',
  15282.         PK_NAME                = object_name(s.constid),
  15283.         FK_NAME                = object_name(r.constid),
  15284.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15285.     from
  15286.         sysobjects o1, sysobjects o2,
  15287.         syscolumns c1, syscolumns c2,
  15288.         sysreferences r, sysconstraints s
  15289.     where    
  15290.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15291.     and    o2.name = @fk_table_name
  15292.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15293.     and    o2.id = r.fkeyid
  15294.     and    r.keycnt >= 3
  15295.     and    o2.id = c2.id
  15296.     and    c2.colid = r.fkey3
  15297.     and r.rkeyid = o1.id
  15298.     and s.id = o1.id and (s.status & 0xf) = 1
  15299.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  15300.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15301.     and    o1.id = c1.id    
  15302.     and    c1.colid = r.rkey3
  15303.     union    all
  15304.     select
  15305.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15306.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15307.         PK_TABLE_NAME         = o1.name,
  15308.         PK_COLUMN_NAME         = c1.name,
  15309.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15310.         PK_COLUMN_PROPID    = convert(int,null),
  15311.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15312.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  15313.         FK_TABLE_NAME         = o2.name,
  15314.         FK_COLUMN_NAME         = c2.name,
  15315.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15316.         FK_COLUMN_PROPID    = convert(int,null),
  15317.         ORDINAL                = convert(int,4),
  15318.         UPDATE_RULE            = N'NO ACTION',
  15319.         DELETE_RULE         = N'NO ACTION',
  15320.         PK_NAME                = object_name(s.constid),
  15321.         FK_NAME                = object_name(r.constid),
  15322.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15323.     from
  15324.         sysobjects o1, sysobjects o2,
  15325.         syscolumns c1, syscolumns c2,
  15326.         sysreferences r, sysconstraints s
  15327.     where    
  15328.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15329.     and    o2.name = @fk_table_name
  15330.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15331.     and    o2.id = r.fkeyid
  15332.     and    r.keycnt >= 4
  15333.     and    o2.id = c2.id
  15334.     and    c2.colid = r.fkey4
  15335.     and r.rkeyid = o1.id
  15336.     and s.id = o1.id and (s.status & 0xf) = 1
  15337.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  15338.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15339.     and    o1.id = c1.id    
  15340.     and    c1.colid = r.rkey4
  15341.     union    all
  15342.     select
  15343.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15344.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15345.         PK_TABLE_NAME         = o1.name,
  15346.         PK_COLUMN_NAME         = c1.name,
  15347.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15348.         PK_COLUMN_PROPID    = convert(int,null),
  15349.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15350.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  15351.         FK_TABLE_NAME         = o2.name,
  15352.         FK_COLUMN_NAME         = c2.name,
  15353.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15354.         FK_COLUMN_PROPID    = convert(int,null),
  15355.         ORDINAL                = convert(int,5),
  15356.         UPDATE_RULE            = N'NO ACTION',
  15357.         DELETE_RULE         = N'NO ACTION',
  15358.         PK_NAME                = object_name(s.constid),
  15359.         FK_NAME                = object_name(r.constid),
  15360.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15361.     from
  15362.         sysobjects o1, sysobjects o2,
  15363.         syscolumns c1, syscolumns c2,
  15364.         sysreferences r, sysconstraints s
  15365.     where    
  15366.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15367.     and    o2.name = @fk_table_name
  15368.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15369.     and    o2.id = r.fkeyid
  15370.     and    r.keycnt >= 5
  15371.     and    o2.id = c2.id
  15372.     and    c2.colid = r.fkey5
  15373.     and    r.rkeyid = o1.id
  15374.     and s.id = o1.id and (s.status & 0xf) = 1
  15375.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  15376.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15377.     and    o1.id = c1.id    
  15378.     and    c1.colid = r.rkey5
  15379.     union    all
  15380.     select
  15381.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15382.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15383.         PK_TABLE_NAME         = o1.name,
  15384.         PK_COLUMN_NAME         = c1.name,
  15385.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15386.         PK_COLUMN_PROPID    = convert(int,null),
  15387.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15388.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  15389.         FK_TABLE_NAME         = o2.name,
  15390.         FK_COLUMN_NAME         = c2.name,
  15391.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15392.         FK_COLUMN_PROPID    = convert(int,null),
  15393.         ORDINAL                = convert(int,6),
  15394.         UPDATE_RULE            = N'NO ACTION',
  15395.         DELETE_RULE         = N'NO ACTION',
  15396.         PK_NAME                = object_name(s.constid),
  15397.         FK_NAME                = object_name(r.constid),
  15398.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15399.     from
  15400.         sysobjects o1, sysobjects o2,
  15401.         syscolumns c1, syscolumns c2,
  15402.         sysreferences r, sysconstraints s
  15403.     where    
  15404.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15405.     and    o2.name = @fk_table_name
  15406.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15407.     and    o2.id = r.fkeyid
  15408.     and    r.keycnt >= 6
  15409.     and    o2.id = c2.id
  15410.     and    c2.colid = r.fkey6
  15411.     and r.rkeyid = o1.id
  15412.     and s.id = o1.id and (s.status & 0xf) = 1
  15413.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  15414.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15415.     and    o1.id = c1.id    
  15416.     and    c1.colid = r.rkey6
  15417.     union    all
  15418.     select
  15419.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15420.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15421.         PK_TABLE_NAME         = o1.name,
  15422.         PK_COLUMN_NAME         = c1.name,
  15423.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15424.         PK_COLUMN_PROPID    = convert(int,null),
  15425.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15426.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  15427.         FK_TABLE_NAME         = o2.name,
  15428.         FK_COLUMN_NAME         = c2.name,
  15429.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15430.         FK_COLUMN_PROPID    = convert(int,null),
  15431.         ORDINAL                = convert(int,7),
  15432.         UPDATE_RULE            = N'NO ACTION',
  15433.         DELETE_RULE         = N'NO ACTION',
  15434.         PK_NAME                = object_name(s.constid),
  15435.         FK_NAME                = object_name(r.constid),
  15436.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15437.     from
  15438.         sysobjects o1, sysobjects o2,
  15439.         syscolumns c1, syscolumns c2,
  15440.         sysreferences r, sysconstraints s
  15441.     where    
  15442.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15443.     and    o2.name = @fk_table_name
  15444.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15445.     and    o2.id = r.fkeyid
  15446.     and    r.keycnt >= 7
  15447.     and    o2.id = c2.id
  15448.     and    c2.colid = r.fkey7
  15449.     and    r.rkeyid = o1.id
  15450.     and s.id = o1.id and (s.status & 0xf) = 1
  15451.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  15452.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15453.     and    o1.id = c1.id    
  15454.     and    c1.colid = r.rkey7
  15455.     union    all
  15456.     select
  15457.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15458.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15459.         PK_TABLE_NAME         = o1.name,
  15460.         PK_COLUMN_NAME         = c1.name,
  15461.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15462.         PK_COLUMN_PROPID    = convert(int,null),
  15463.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15464.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  15465.         FK_TABLE_NAME         = o2.name,
  15466.         FK_COLUMN_NAME         = c2.name,
  15467.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15468.         FK_COLUMN_PROPID    = convert(int,null),
  15469.         ORDINAL                = convert(int,8),
  15470.         UPDATE_RULE            = N'NO ACTION',
  15471.         DELETE_RULE         = N'NO ACTION',
  15472.         PK_NAME                = object_name(s.constid),
  15473.         FK_NAME                = object_name(r.constid),
  15474.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15475.     from
  15476.         sysobjects o1, sysobjects o2,
  15477.         syscolumns c1, syscolumns c2,
  15478.         sysreferences r, sysconstraints s
  15479.     where    
  15480.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15481.     and    o2.name = @fk_table_name
  15482.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15483.     and    o2.id = r.fkeyid
  15484.     and    r.keycnt >= 8
  15485.     and    o2.id = c2.id
  15486.     and    c2.colid = r.fkey8
  15487.     and r.rkeyid = o1.id
  15488.     and s.id = o1.id and (s.status & 0xf) = 1
  15489.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  15490.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15491.     and    o1.id = c1.id    
  15492.     and    c1.colid = r.rkey8
  15493.     union    all
  15494.     select
  15495.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15496.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15497.         PK_TABLE_NAME         = o1.name,
  15498.         PK_COLUMN_NAME         = c1.name,
  15499.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15500.         PK_COLUMN_PROPID    = convert(int,null),
  15501.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15502.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  15503.         FK_TABLE_NAME         = o2.name,
  15504.         FK_COLUMN_NAME         = c2.name,
  15505.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15506.         FK_COLUMN_PROPID    = convert(int,null),
  15507.         ORDINAL                = convert(int,9),
  15508.         UPDATE_RULE            = N'NO ACTION',
  15509.         DELETE_RULE         = N'NO ACTION',
  15510.         PK_NAME                = object_name(s.constid),
  15511.         FK_NAME                = object_name(r.constid),
  15512.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15513.     from
  15514.         sysobjects o1, sysobjects o2,
  15515.         syscolumns c1, syscolumns c2,
  15516.         sysreferences r, sysconstraints s
  15517.     where    
  15518.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15519.     and    o2.name = @fk_table_name
  15520.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15521.     and    o2.id = r.fkeyid
  15522.     and    r.keycnt >= 9
  15523.     and    o2.id = c2.id
  15524.     and    c2.colid = r.fkey9
  15525.     and    r.rkeyid = o1.id
  15526.     and s.id = o1.id and (s.status & 0xf) = 1
  15527.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  15528.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15529.     and    o1.id = c1.id    
  15530.     and    c1.colid = r.rkey9
  15531.     union    all
  15532.     select
  15533.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15534.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15535.         PK_TABLE_NAME         = o1.name,
  15536.         PK_COLUMN_NAME         = c1.name,
  15537.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15538.         PK_COLUMN_PROPID    = convert(int,null),
  15539.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15540.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  15541.         FK_TABLE_NAME         = o2.name,
  15542.         FK_COLUMN_NAME         = c2.name,
  15543.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15544.         FK_COLUMN_PROPID    = convert(int,null),
  15545.         ORDINAL                = convert(int,10),
  15546.         UPDATE_RULE            = N'NO ACTION',
  15547.         DELETE_RULE         = N'NO ACTION',
  15548.         PK_NAME                = object_name(s.constid),
  15549.         FK_NAME                = object_name(r.constid),
  15550.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15551.     from
  15552.         sysobjects o1, sysobjects o2,
  15553.         syscolumns c1, syscolumns c2,
  15554.         sysreferences r, sysconstraints s
  15555.     where    
  15556.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15557.     and    o2.name = @fk_table_name
  15558.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15559.     and    o2.id = r.fkeyid
  15560.     and    r.keycnt >= 10
  15561.     and    o2.id = c2.id
  15562.     and    c2.colid = r.fkey10
  15563.     and r.rkeyid = o1.id
  15564.     and s.id = o1.id and (s.status & 0xf) = 1
  15565.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  15566.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15567.     and    o1.id = c1.id    
  15568.     and    c1.colid = r.rkey10
  15569.     union    all
  15570.     select
  15571.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15572.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15573.         PK_TABLE_NAME         = o1.name,
  15574.         PK_COLUMN_NAME         = c1.name,
  15575.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15576.         PK_COLUMN_PROPID    = convert(int,null),
  15577.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15578.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  15579.         FK_TABLE_NAME         = o2.name,
  15580.         FK_COLUMN_NAME         = c2.name,
  15581.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15582.         FK_COLUMN_PROPID    = convert(int,null),
  15583.         ORDINAL                = convert(int,11),
  15584.         UPDATE_RULE            = N'NO ACTION',
  15585.         DELETE_RULE         = N'NO ACTION',
  15586.         PK_NAME                = object_name(s.constid),
  15587.         FK_NAME                = object_name(r.constid),
  15588.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15589.     from
  15590.         sysobjects o1, sysobjects o2,
  15591.         syscolumns c1, syscolumns c2,
  15592.         sysreferences r, sysconstraints s
  15593.     where    
  15594.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15595.     and    o2.name = @fk_table_name
  15596.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15597.     and    o2.id = r.fkeyid
  15598.     and    r.keycnt >= 11
  15599.     and    o2.id = c2.id
  15600.     and    c2.colid = r.fkey11
  15601.     and r.rkeyid = o1.id
  15602.     and s.id = o1.id and (s.status & 0xf) = 1
  15603.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  15604.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15605.     and    o1.id = c1.id    
  15606.     and    c1.colid = r.rkey11
  15607.     union    all
  15608.     select
  15609.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15610.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15611.         PK_TABLE_NAME         = o1.name,
  15612.         PK_COLUMN_NAME         = c1.name,
  15613.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15614.         PK_COLUMN_PROPID    = convert(int,null),
  15615.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15616.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  15617.         FK_TABLE_NAME         = o2.name,
  15618.         FK_COLUMN_NAME         = c2.name,
  15619.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15620.         FK_COLUMN_PROPID    = convert(int,null),
  15621.         ORDINAL                = convert(int,12),
  15622.         UPDATE_RULE            = N'NO ACTION',
  15623.         DELETE_RULE         = N'NO ACTION',
  15624.         PK_NAME                = object_name(s.constid),
  15625.         FK_NAME                = object_name(r.constid),
  15626.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15627.     from
  15628.         sysobjects o1, sysobjects o2,
  15629.         syscolumns c1, syscolumns c2,
  15630.         sysreferences r, sysconstraints s
  15631.     where    
  15632.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15633.     and    o2.name = @fk_table_name
  15634.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15635.     and    o2.id = r.fkeyid
  15636.     and    r.keycnt >= 12
  15637.     and    o2.id = c2.id
  15638.     and    c2.colid = r.fkey12
  15639.     and r.rkeyid = o1.id
  15640.     and s.id = o1.id and (s.status & 0xf) = 1
  15641.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  15642.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15643.     and    o1.id = c1.id    
  15644.     and    c1.colid = r.rkey12
  15645.     union    all
  15646.     select
  15647.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15648.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15649.         PK_TABLE_NAME         = o1.name,
  15650.         PK_COLUMN_NAME         = c1.name,
  15651.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15652.         PK_COLUMN_PROPID    = convert(int,null),
  15653.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15654.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  15655.         FK_TABLE_NAME         = o2.name,
  15656.         FK_COLUMN_NAME         = c2.name,
  15657.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15658.         FK_COLUMN_PROPID    = convert(int,null),
  15659.         ORDINAL                = convert(int,13),
  15660.         UPDATE_RULE            = N'NO ACTION',
  15661.         DELETE_RULE         = N'NO ACTION',
  15662.         PK_NAME                = object_name(s.constid),
  15663.         FK_NAME                = object_name(r.constid),
  15664.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15665.     from
  15666.         sysobjects o1, sysobjects o2,
  15667.         syscolumns c1, syscolumns c2,
  15668.         sysreferences r, sysconstraints s
  15669.     where    
  15670.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15671.     and    o2.name = @fk_table_name
  15672.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15673.     and    o2.id = r.fkeyid
  15674.     and    r.keycnt >= 13
  15675.     and    o2.id = c2.id
  15676.     and    c2.colid = r.fkey13
  15677.     and r.rkeyid = o1.id
  15678.     and s.id = o1.id and (s.status & 0xf) = 1
  15679.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  15680.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15681.     and    o1.id = c1.id    
  15682.     and    c1.colid = r.rkey13
  15683.     union    all
  15684.     select
  15685.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15686.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15687.         PK_TABLE_NAME         = o1.name,
  15688.         PK_COLUMN_NAME         = c1.name,
  15689.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15690.         PK_COLUMN_PROPID    = convert(int,null),
  15691.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15692.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  15693.         FK_TABLE_NAME         = o2.name,
  15694.         FK_COLUMN_NAME         = c2.name,
  15695.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15696.         FK_COLUMN_PROPID    = convert(int,null),
  15697.         ORDINAL                = convert(int,14),
  15698.         UPDATE_RULE            = N'NO ACTION',
  15699.         DELETE_RULE         = N'NO ACTION',
  15700.         PK_NAME                = object_name(s.constid),
  15701.         FK_NAME                = object_name(r.constid),
  15702.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15703.     from
  15704.         sysobjects o1, sysobjects o2,
  15705.         syscolumns c1, syscolumns c2,
  15706.         sysreferences r, sysconstraints s
  15707.     where    
  15708.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15709.     and    o2.name = @fk_table_name
  15710.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15711.     and    o2.id = r.fkeyid
  15712.     and    r.keycnt >= 14
  15713.     and    o2.id = c2.id
  15714.     and    c2.colid = r.fkey14
  15715.     and r.rkeyid = o1.id
  15716.     and s.id = o1.id and (s.status & 0xf) = 1
  15717.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  15718.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15719.     and    o1.id = c1.id    
  15720.     and    c1.colid = r.rkey14
  15721.     union    all
  15722.     select
  15723.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15724.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15725.         PK_TABLE_NAME         = o1.name,
  15726.         PK_COLUMN_NAME         = c1.name,
  15727.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15728.         PK_COLUMN_PROPID    = convert(int,null),
  15729.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15730.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  15731.         FK_TABLE_NAME         = o2.name,
  15732.         FK_COLUMN_NAME         = c2.name,
  15733.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15734.         FK_COLUMN_PROPID    = convert(int,null),
  15735.         ORDINAL                = convert(int,15),
  15736.         UPDATE_RULE            = N'NO ACTION',
  15737.         DELETE_RULE         = N'NO ACTION',
  15738.         PK_NAME                = object_name(s.constid),
  15739.         FK_NAME                = object_name(r.constid),
  15740.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/        
  15741.     from
  15742.         sysobjects o1, sysobjects o2,
  15743.         syscolumns c1, syscolumns c2,
  15744.         sysreferences r, sysconstraints s
  15745.     where    
  15746.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15747.     and    o2.name = @fk_table_name
  15748.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15749.     and    o2.id = r.fkeyid
  15750.     and    r.keycnt >= 15
  15751.     and    o2.id = c2.id
  15752.     and    c2.colid = r.fkey15
  15753.     and r.rkeyid = o1.id
  15754.     and s.id = o1.id and (s.status & 0xf) = 1
  15755.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  15756.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15757.     and    o1.id = c1.id    
  15758.     and    c1.colid = r.rkey15
  15759.     union    all
  15760.     select
  15761.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15762.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15763.         PK_TABLE_NAME         = o1.name,
  15764.         PK_COLUMN_NAME         = c1.name,
  15765.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15766.         PK_COLUMN_PROPID    = convert(int,null),
  15767.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15768.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  15769.         FK_TABLE_NAME         = o2.name,
  15770.         FK_COLUMN_NAME         = c2.name,
  15771.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15772.         FK_COLUMN_PROPID    = convert(int,null),
  15773.         ORDINAL                = convert(int,16),
  15774.         UPDATE_RULE            = N'NO ACTION',
  15775.         DELETE_RULE         = N'NO ACTION',
  15776.         PK_NAME                = object_name(s.constid),
  15777.         FK_NAME                = object_name(r.constid),
  15778.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/        
  15779.     from
  15780.         sysobjects o1, sysobjects o2,
  15781.         syscolumns c1, syscolumns c2,
  15782.         sysreferences r, sysconstraints s
  15783.     where    
  15784.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15785.     and    o2.name = @fk_table_name
  15786.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15787.     and    o2.id = r.fkeyid
  15788.     and    r.keycnt >= 16
  15789.     and    o2.id = c2.id
  15790.     and    c2.colid = r.fkey16
  15791.     and r.rkeyid = o1.id
  15792.     and s.id = o1.id and (s.status & 0xf) = 1
  15793.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  15794.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15795.     and    o1.id = c1.id    
  15796.     and    c1.colid = r.rkey16
  15797.     order by 8,9,2,3,13
  15798. go
  15799. dump tran master with no_log
  15800. go
  15801. CREATE PROCEDURE sp_foreign_keys_rowset;3
  15802.     (
  15803.     @pk_table_schema    sysname = null,
  15804.     @pk_table_catalog    sysname = null,
  15805.     @fk_table_schema    sysname = null,
  15806.     @fk_table_catalog    sysname = null
  15807.     )
  15808. as
  15809.     select
  15810.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15811.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15812.         PK_TABLE_NAME         = o1.name,
  15813.         PK_COLUMN_NAME         = c1.name,
  15814.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15815.         PK_COLUMN_PROPID    = convert(int,null),
  15816.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15817.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  15818.         FK_TABLE_NAME         = o2.name,
  15819.         FK_COLUMN_NAME         = c2.name,
  15820.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15821.         FK_COLUMN_PROPID    = convert(int,null),
  15822.         ORDINAL                = convert(int,1),
  15823.         UPDATE_RULE            = N'NO ACTION',
  15824.         DELETE_RULE         = N'NO ACTION',
  15825.         PK_NAME                = object_name(s.constid),
  15826.         FK_NAME                = object_name(r.constid),
  15827.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/        
  15828.     from
  15829.         sysobjects o1, sysobjects o2,
  15830.         syscolumns c1, syscolumns c2,
  15831.         sysreferences r, sysconstraints s
  15832.     where    
  15833.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15834.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15835.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15836.     and    o1.id = r.rkeyid
  15837.     and s.id = o1.id and (s.status & 0xf) = 1
  15838.     and    o1.id = c1.id
  15839.     and    c1.colid = r.rkey1
  15840.     and r.fkeyid = o2.id
  15841.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15842.     and    o2.id = c2.id    
  15843.     and    c2.colid = r.fkey1
  15844.     union all
  15845.     select
  15846.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15847.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15848.         PK_TABLE_NAME         = o1.name,
  15849.         PK_COLUMN_NAME         = c1.name,
  15850.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15851.         PK_COLUMN_PROPID    = convert(int,null),
  15852.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15853.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  15854.         FK_TABLE_NAME         = o2.name,
  15855.         FK_COLUMN_NAME         = c2.name,
  15856.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15857.         FK_COLUMN_PROPID    = convert(int,null),
  15858.         ORDINAL                = convert(int,2),
  15859.         UPDATE_RULE            = N'NO ACTION',
  15860.         DELETE_RULE         = N'NO ACTION',
  15861.         PK_NAME                = object_name(s.constid),
  15862.         FK_NAME                = object_name(r.constid),
  15863.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15864.     from
  15865.         sysobjects o1, sysobjects o2,
  15866.         syscolumns c1, syscolumns c2,
  15867.         sysreferences r, sysconstraints s
  15868.     where    
  15869.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15870.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15871.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15872.     and    o1.id = r.rkeyid
  15873.     and s.id = o1.id and (s.status & 0xf) = 1
  15874.     and    r.keycnt >= 2
  15875.     and    o1.id = c1.id
  15876.     and    c1.colid = r.rkey2
  15877.     and r.fkeyid = o2.id
  15878.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15879.     and    o2.id = c2.id    
  15880.     and    c2.colid = r.fkey2
  15881.     union all
  15882.     select
  15883.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15884.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15885.         PK_TABLE_NAME         = o1.name,
  15886.         PK_COLUMN_NAME         = c1.name,
  15887.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15888.         PK_COLUMN_PROPID    = convert(int,null),
  15889.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15890.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  15891.         FK_TABLE_NAME         = o2.name,
  15892.         FK_COLUMN_NAME         = c2.name,
  15893.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15894.         FK_COLUMN_PROPID    = convert(int,null),
  15895.         ORDINAL                = convert(int,3),
  15896.         UPDATE_RULE            = N'NO ACTION',
  15897.         DELETE_RULE         = N'NO ACTION',
  15898.         PK_NAME                = object_name(s.constid),
  15899.         FK_NAME                = object_name(r.constid),
  15900.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15901.     from
  15902.         sysobjects o1, sysobjects o2,
  15903.         syscolumns c1, syscolumns c2,
  15904.         sysreferences r, sysconstraints s
  15905.     where    
  15906.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15907.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15908.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15909.     and    o1.id = r.rkeyid
  15910.     and s.id = o1.id and (s.status & 0xf) = 1
  15911.     and    r.keycnt >= 3
  15912.     and    o1.id = c1.id
  15913.     and    c1.colid = r.rkey3
  15914.     and r.fkeyid = o2.id
  15915.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15916.     and    o2.id = c2.id    
  15917.     and    c2.colid = r.fkey3
  15918.     union all
  15919.     select
  15920.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15921.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15922.         PK_TABLE_NAME         = o1.name,
  15923.         PK_COLUMN_NAME         = c1.name,
  15924.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15925.         PK_COLUMN_PROPID    = convert(int,null),
  15926.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15927.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  15928.         FK_TABLE_NAME         = o2.name,
  15929.         FK_COLUMN_NAME         = c2.name,
  15930.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15931.         FK_COLUMN_PROPID    = convert(int,null),
  15932.         ORDINAL                = convert(int,4),
  15933.         UPDATE_RULE            = N'NO ACTION',
  15934.         DELETE_RULE         = N'NO ACTION',
  15935.         PK_NAME                = object_name(s.constid),
  15936.         FK_NAME                = object_name(r.constid),
  15937.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15938.     from
  15939.         sysobjects o1, sysobjects o2,
  15940.         syscolumns c1, syscolumns c2,
  15941.         sysreferences r, sysconstraints s
  15942.     where    
  15943.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15944.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15945.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15946.     and    o1.id = r.rkeyid
  15947.     and s.id = o1.id and (s.status & 0xf) = 1
  15948.     and    r.keycnt >= 4
  15949.     and    o1.id = c1.id
  15950.     and    c1.colid = r.rkey4
  15951.     and r.fkeyid = o2.id
  15952.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15953.     and    o2.id = c2.id    
  15954.     and    c2.colid = r.fkey4
  15955.     union all
  15956.     select
  15957.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15958.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15959.         PK_TABLE_NAME         = o1.name,
  15960.         PK_COLUMN_NAME         = c1.name,
  15961.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15962.         PK_COLUMN_PROPID    = convert(int,null),
  15963.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  15964.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  15965.         FK_TABLE_NAME         = o2.name,
  15966.         FK_COLUMN_NAME         = c2.name,
  15967.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15968.         FK_COLUMN_PROPID    = convert(int,null),
  15969.         ORDINAL                = convert(int,5),
  15970.         UPDATE_RULE            = N'NO ACTION',
  15971.         DELETE_RULE         = N'NO ACTION',
  15972.         PK_NAME                = object_name(s.constid),
  15973.         FK_NAME                = object_name(r.constid),
  15974.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15975.     from
  15976.         sysobjects o1, sysobjects o2,
  15977.         syscolumns c1, syscolumns c2,
  15978.         sysreferences r, sysconstraints s
  15979.     where    
  15980.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15981.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15982.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15983.     and    o1.id = r.rkeyid
  15984.     and s.id = o1.id and (s.status & 0xf) = 1
  15985.     and    r.keycnt >= 5
  15986.     and    o1.id = c1.id
  15987.     and    c1.colid = r.rkey5
  15988.     and r.fkeyid = o2.id
  15989.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15990.     and    o2.id = c2.id    
  15991.     and    c2.colid = r.fkey5
  15992.     union all
  15993.     select
  15994.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  15995.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  15996.         PK_TABLE_NAME         = o1.name,
  15997.         PK_COLUMN_NAME         = c1.name,
  15998.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  15999.         PK_COLUMN_PROPID    = convert(int,null),
  16000.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16001.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  16002.         FK_TABLE_NAME         = o2.name,
  16003.         FK_COLUMN_NAME         = c2.name,
  16004.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16005.         FK_COLUMN_PROPID    = convert(int,null),
  16006.         ORDINAL                = convert(int,6),
  16007.         UPDATE_RULE            = N'NO ACTION',
  16008.         DELETE_RULE         = N'NO ACTION',
  16009.         PK_NAME                = object_name(s.constid),
  16010.         FK_NAME                = object_name(r.constid),
  16011.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16012.     from
  16013.         sysobjects o1, sysobjects o2,
  16014.         syscolumns c1, syscolumns c2,
  16015.         sysreferences r, sysconstraints s
  16016.     where    
  16017.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16018.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16019.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16020.     and    o1.id = r.rkeyid
  16021.     and s.id = o1.id and (s.status & 0xf) = 1
  16022.     and    r.keycnt >= 6
  16023.     and    o1.id = c1.id
  16024.     and    c1.colid = r.rkey6
  16025.     and r.fkeyid = o2.id
  16026.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16027.     and    o2.id = c2.id    
  16028.     and    c2.colid = r.fkey6
  16029.     union all
  16030.     select
  16031.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16032.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16033.         PK_TABLE_NAME         = o1.name,
  16034.         PK_COLUMN_NAME         = c1.name,
  16035.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16036.         PK_COLUMN_PROPID    = convert(int,null),
  16037.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16038.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  16039.         FK_TABLE_NAME         = o2.name,
  16040.         FK_COLUMN_NAME         = c2.name,
  16041.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16042.         FK_COLUMN_PROPID    = convert(int,null),
  16043.         ORDINAL                = convert(int,7),
  16044.         UPDATE_RULE            = N'NO ACTION',
  16045.         DELETE_RULE         = N'NO ACTION',
  16046.         PK_NAME                = object_name(s.constid),
  16047.         FK_NAME                = object_name(r.constid),
  16048.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16049.     from
  16050.         sysobjects o1, sysobjects o2,
  16051.         syscolumns c1, syscolumns c2,
  16052.         sysreferences r, sysconstraints s
  16053.     where    
  16054.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16055.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16056.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16057.     and    o1.id = r.rkeyid
  16058.     and s.id = o1.id and (s.status & 0xf) = 1
  16059.     and    r.keycnt >= 7
  16060.     and    o1.id = c1.id
  16061.     and    c1.colid = r.rkey7
  16062.     and r.fkeyid = o2.id
  16063.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16064.     and    o2.id = c2.id    
  16065.     and    c2.colid = r.fkey7
  16066.     union all
  16067.     select
  16068.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16069.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16070.         PK_TABLE_NAME         = o1.name,
  16071.         PK_COLUMN_NAME         = c1.name,
  16072.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16073.         PK_COLUMN_PROPID    = convert(int,null),
  16074.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16075.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  16076.         FK_TABLE_NAME         = o2.name,
  16077.         FK_COLUMN_NAME         = c2.name,
  16078.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16079.         FK_COLUMN_PROPID    = convert(int,null),
  16080.         ORDINAL                = convert(int,8),
  16081.         UPDATE_RULE            = N'NO ACTION',
  16082.         DELETE_RULE         = N'NO ACTION',
  16083.         PK_NAME                = object_name(s.constid),
  16084.         FK_NAME                = object_name(r.constid),
  16085.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16086.     from
  16087.         sysobjects o1, sysobjects o2,
  16088.         syscolumns c1, syscolumns c2,
  16089.         sysreferences r, sysconstraints s
  16090.     where    
  16091.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16092.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16093.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16094.     and    o1.id = r.rkeyid
  16095.     and s.id = o1.id and (s.status & 0xf) = 1
  16096.     and    r.keycnt >= 8
  16097.     and    o1.id = c1.id
  16098.     and    c1.colid = r.rkey8
  16099.     and r.fkeyid = o2.id
  16100.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16101.     and    o2.id = c2.id    
  16102.     and    c2.colid = r.fkey8
  16103.     union all
  16104.     select
  16105.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16106.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16107.         PK_TABLE_NAME         = o1.name,
  16108.         PK_COLUMN_NAME         = c1.name,
  16109.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16110.         PK_COLUMN_PROPID    = convert(int,null),
  16111.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16112.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  16113.         FK_TABLE_NAME         = o2.name,
  16114.         FK_COLUMN_NAME         = c2.name,
  16115.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16116.         FK_COLUMN_PROPID    = convert(int,null),
  16117.         ORDINAL                = convert(int,9),
  16118.         UPDATE_RULE            = N'NO ACTION',
  16119.         DELETE_RULE         = N'NO ACTION',
  16120.         PK_NAME                = object_name(s.constid),
  16121.         FK_NAME                = object_name(r.constid),
  16122.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16123.     from
  16124.         sysobjects o1, sysobjects o2,
  16125.         syscolumns c1, syscolumns c2,
  16126.         sysreferences r, sysconstraints s
  16127.     where    
  16128.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16129.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16130.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16131.     and    o1.id = r.rkeyid
  16132.     and s.id = o1.id and (s.status & 0xf) = 1
  16133.     and    r.keycnt >= 9
  16134.     and    o1.id = c1.id
  16135.     and    c1.colid = r.rkey9
  16136.     and r.fkeyid = o2.id
  16137.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16138.     and    o2.id = c2.id    
  16139.     and    c2.colid = r.fkey9
  16140.     union all
  16141.     select
  16142.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16143.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16144.         PK_TABLE_NAME         = o1.name,
  16145.         PK_COLUMN_NAME         = c1.name,
  16146.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16147.         PK_COLUMN_PROPID    = convert(int,null),
  16148.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16149.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  16150.         FK_TABLE_NAME         = o2.name,
  16151.         FK_COLUMN_NAME         = c2.name,
  16152.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16153.         FK_COLUMN_PROPID    = convert(int,null),
  16154.         ORDINAL                = convert(int,10),
  16155.         UPDATE_RULE            = N'NO ACTION',
  16156.         DELETE_RULE         = N'NO ACTION',
  16157.         PK_NAME                = object_name(s.constid),
  16158.         FK_NAME                = object_name(r.constid),
  16159.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16160.     from
  16161.         sysobjects o1, sysobjects o2,
  16162.         syscolumns c1, syscolumns c2,
  16163.         sysreferences r, sysconstraints s
  16164.     where    
  16165.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16166.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16167.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16168.     and    o1.id = r.rkeyid
  16169.     and s.id = o1.id and (s.status & 0xf) = 1
  16170.     and    r.keycnt >= 10
  16171.     and    o1.id = c1.id
  16172.     and    c1.colid = r.rkey10
  16173.     and r.fkeyid = o2.id
  16174.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16175.     and    o2.id = c2.id    
  16176.     and    c2.colid = r.fkey10
  16177.     union all
  16178.     select
  16179.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16180.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16181.         PK_TABLE_NAME         = o1.name,
  16182.         PK_COLUMN_NAME         = c1.name,
  16183.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16184.         PK_COLUMN_PROPID    = convert(int,null),
  16185.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16186.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  16187.         FK_TABLE_NAME         = o2.name,
  16188.         FK_COLUMN_NAME         = c2.name,
  16189.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16190.         FK_COLUMN_PROPID    = convert(int,null),
  16191.         ORDINAL                = convert(int,11),
  16192.         UPDATE_RULE            = N'NO ACTION',
  16193.         DELETE_RULE         = N'NO ACTION',
  16194.         PK_NAME                = object_name(s.constid),
  16195.         FK_NAME                = object_name(r.constid),
  16196.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16197.     from
  16198.         sysobjects o1, sysobjects o2,
  16199.         syscolumns c1, syscolumns c2,
  16200.         sysreferences r, sysconstraints s
  16201.     where    
  16202.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16203.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16204.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16205.     and    o1.id = r.rkeyid
  16206.     and s.id = o1.id and (s.status & 0xf) = 1
  16207.     and    r.keycnt >= 11
  16208.     and    o1.id = c1.id
  16209.     and    c1.colid = r.rkey11
  16210.     and r.fkeyid = o2.id
  16211.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16212.     and    o2.id = c2.id    
  16213.     and    c2.colid = r.fkey11
  16214.     union all
  16215.     select
  16216.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16217.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16218.         PK_TABLE_NAME         = o1.name,
  16219.         PK_COLUMN_NAME         = c1.name,
  16220.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16221.         PK_COLUMN_PROPID    = convert(int,null),
  16222.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16223.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  16224.         FK_TABLE_NAME         = o2.name,
  16225.         FK_COLUMN_NAME         = c2.name,
  16226.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16227.         FK_COLUMN_PROPID    = convert(int,null),
  16228.         ORDINAL                = convert(int,12),
  16229.         UPDATE_RULE            = N'NO ACTION',
  16230.         DELETE_RULE         = N'NO ACTION',
  16231.         PK_NAME                = object_name(s.constid),
  16232.         FK_NAME                = object_name(r.constid),
  16233.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16234.     from
  16235.         sysobjects o1, sysobjects o2,
  16236.         syscolumns c1, syscolumns c2,
  16237.         sysreferences r, sysconstraints s
  16238.     where    
  16239.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16240.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16241.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16242.     and    o1.id = r.rkeyid
  16243.     and s.id = o1.id and (s.status & 0xf) = 1
  16244.     and    r.keycnt >= 12
  16245.     and    o1.id = c1.id
  16246.     and    c1.colid = r.rkey12
  16247.     and r.fkeyid = o2.id
  16248.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16249.     and    o2.id = c2.id    
  16250.     and    c2.colid = r.fkey12
  16251.     union all
  16252.     select
  16253.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16254.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16255.         PK_TABLE_NAME         = o1.name,
  16256.         PK_COLUMN_NAME         = c1.name,
  16257.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16258.         PK_COLUMN_PROPID    = convert(int,null),
  16259.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16260.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  16261.         FK_TABLE_NAME         = o2.name,
  16262.         FK_COLUMN_NAME         = c2.name,
  16263.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16264.         FK_COLUMN_PROPID    = convert(int,null),
  16265.         ORDINAL                = convert(int,13),
  16266.         UPDATE_RULE            = N'NO ACTION',
  16267.         DELETE_RULE         = N'NO ACTION',
  16268.         PK_NAME                = object_name(s.constid),
  16269.         FK_NAME                = object_name(r.constid),
  16270.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16271.     from
  16272.         sysobjects o1, sysobjects o2,
  16273.         syscolumns c1, syscolumns c2,
  16274.         sysreferences r, sysconstraints s
  16275.     where    
  16276.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16277.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16278.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16279.     and    o1.id = r.rkeyid
  16280.     and s.id = o1.id and (s.status & 0xf) = 1
  16281.     and    r.keycnt >= 13
  16282.     and    o1.id = c1.id
  16283.     and    c1.colid = r.rkey13
  16284.     and r.fkeyid = o2.id
  16285.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16286.     and    o2.id = c2.id    
  16287.     and    c2.colid = r.fkey13
  16288.     union all
  16289.     select
  16290.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16291.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16292.         PK_TABLE_NAME         = o1.name,
  16293.         PK_COLUMN_NAME         = c1.name,
  16294.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16295.         PK_COLUMN_PROPID    = convert(int,null),
  16296.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16297.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  16298.         FK_TABLE_NAME         = o2.name,
  16299.         FK_COLUMN_NAME         = c2.name,
  16300.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16301.         FK_COLUMN_PROPID    = convert(int,null),
  16302.         ORDINAL                = convert(int,14),
  16303.         UPDATE_RULE            = N'NO ACTION',
  16304.         DELETE_RULE         = N'NO ACTION',
  16305.         PK_NAME                = object_name(s.constid),
  16306.         FK_NAME                = object_name(r.constid),
  16307.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16308.     from
  16309.         sysobjects o1, sysobjects o2,
  16310.         syscolumns c1, syscolumns c2,
  16311.         sysreferences r, sysconstraints s
  16312.     where    
  16313.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16314.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16315.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16316.     and    o1.id = r.rkeyid
  16317.     and s.id = o1.id and (s.status & 0xf) = 1
  16318.     and    r.keycnt >= 14
  16319.     and    o1.id = c1.id
  16320.     and    c1.colid = r.rkey14
  16321.     and r.fkeyid = o2.id
  16322.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16323.     and    o2.id = c2.id    
  16324.     and    c2.colid = r.fkey14
  16325.     union all
  16326.     select
  16327.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16328.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16329.         PK_TABLE_NAME         = o1.name,
  16330.         PK_COLUMN_NAME         = c1.name,
  16331.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16332.         PK_COLUMN_PROPID    = convert(int,null),
  16333.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16334.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  16335.         FK_TABLE_NAME         = o2.name,
  16336.         FK_COLUMN_NAME         = c2.name,
  16337.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16338.         FK_COLUMN_PROPID    = convert(int,null),
  16339.         ORDINAL                = convert(int,15),
  16340.         UPDATE_RULE            = N'NO ACTION',
  16341.         DELETE_RULE         = N'NO ACTION',
  16342.         PK_NAME                = object_name(s.constid),
  16343.         FK_NAME                = object_name(r.constid),
  16344.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16345.     from
  16346.         sysobjects o1, sysobjects o2,
  16347.         syscolumns c1, syscolumns c2,
  16348.         sysreferences r, sysconstraints s
  16349.     where    
  16350.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16351.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16352.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16353.     and    o1.id = r.rkeyid
  16354.     and s.id = o1.id and (s.status & 0xf) = 1
  16355.     and    r.keycnt >= 15
  16356.     and    o1.id = c1.id
  16357.     and    c1.colid = r.rkey15
  16358.     and r.fkeyid = o2.id
  16359.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16360.     and    o2.id = c2.id    
  16361.     and    c2.colid = r.fkey15
  16362.     union all
  16363.     select
  16364.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16365.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16366.         PK_TABLE_NAME         = o1.name,
  16367.         PK_COLUMN_NAME         = c1.name,
  16368.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16369.         PK_COLUMN_PROPID    = convert(int,null),
  16370.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16371.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  16372.         FK_TABLE_NAME         = o2.name,
  16373.         FK_COLUMN_NAME         = c2.name,
  16374.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16375.         FK_COLUMN_PROPID    = convert(int,null),
  16376.         ORDINAL                = convert(int,16),
  16377.         UPDATE_RULE            = N'NO ACTION',
  16378.         DELETE_RULE         = N'NO ACTION',
  16379.         PK_NAME                = object_name(s.constid),
  16380.         FK_NAME                = object_name(r.constid),
  16381.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16382.     from
  16383.         sysobjects o1, sysobjects o2,
  16384.         syscolumns c1, syscolumns c2,
  16385.         sysreferences r, sysconstraints s
  16386.     where    
  16387.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16388.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16389.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16390.     and    o1.id = r.rkeyid
  16391.     and s.id = o1.id and (s.status & 0xf) = 1
  16392.     and    o1.id = c1.id
  16393.     and    r.keycnt >= 16
  16394.     and    c1.colid = r.rkey16
  16395.     and r.fkeyid = o2.id
  16396.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16397.     and    o2.id = c2.id    
  16398.     and    c2.colid = r.fkey16
  16399.     order by 8,9,2,3,13
  16400. go
  16401. dump tran master with no_log
  16402. go
  16403. create procedure sp_foreign_keys_rowset;5
  16404.     (
  16405.     @server_name        sysname,
  16406.     @pk_catalog_name    sysname = null,
  16407.     @fk_catalog_name    sysname = null,
  16408.     @pk_table_name        sysname = null,
  16409.     @pk_table_schema    sysname = null,
  16410.     @fk_table_name        sysname = null,
  16411.     @fk_table_schema    sysname = null
  16412.     )
  16413. as
  16414.     select
  16415.         PK_TABLE_CATALOG,
  16416.         PK_TABLE_SCHEMA,    
  16417.         PK_TABLE_NAME,     
  16418.         PK_COLUMN_NAME,     
  16419.         PK_COLUMN_GUID,    
  16420.         PK_COLUMN_PROPID,
  16421.         FK_TABLE_CATALOG,
  16422.         FK_TABLE_SCHEMA, 
  16423.         FK_TABLE_NAME,     
  16424.         FK_COLUMN_NAME,     
  16425.         FK_COLUMN_GUID,    
  16426.         FK_COLUMN_PROPID,
  16427.         ORDINAL,        
  16428.         UPDATE_RULE,    
  16429.         DELETE_RULE
  16430.         --PK_NAME,        
  16431.         --FK_NAME,
  16432.         --DEFERRABILITY    
  16433.     from master.dbo.SYSREMOTE_FOREIGN_KEYS <
  16434.                 @server_name,
  16435.                 @pk_catalog_name,
  16436.                 @pk_table_schema,
  16437.                 @pk_table_name,
  16438.                 @fk_catalog_name,
  16439.                 @fk_table_schema,
  16440.                 @fk_table_name >
  16441.     order by 7,8,9,1,2,3,13
  16442. go
  16443.  
  16444. grant execute on sp_foreign_keys_rowset to public
  16445. go
  16446.  
  16447. dump tran master with no_log
  16448. go
  16449. if (charindex('6.00', @@version) > 0)
  16450.     begin
  16451.     if (exists (select * from sysobjects
  16452.             where name = 'sp_foreign_keys_rowset' and type = 'P '))
  16453.         begin
  16454.         drop procedure sp_foreign_keys_rowset
  16455.         dump tran master with no_log
  16456.         end
  16457.     end
  16458. go
  16459.  
  16460.  
  16461. print ''
  16462. print 'creating sp_indexes_rowset'
  16463. go
  16464.  
  16465. /*    6.0 and 6.5 version */
  16466. create procedure sp_indexes_rowset
  16467.     (
  16468.     @table_name    varchar(255), 
  16469.     @index_name    varchar(255) = null,
  16470.     @table_schema    varchar(255) = null     
  16471.     )
  16472. as
  16473.     select    TABLE_CATALOG        = db_name(),
  16474.         TABLE_SCHEMA        = user_name(o.uid),
  16475.         TABLE_NAME        = o.name,
  16476.         INDEX_CATALOG        = db_name(),        
  16477.         INDEX_SCHEMA        = user_name(o.uid),
  16478.         INDEX_NAME        = x.name,
  16479.         PRIMARY_KEY        = convert(bit,(x.status & 0x800)/0x800),
  16480.         "UNIQUE"        = convert(bit,(x.status & 2)/2),
  16481.         "CLUSTERED"        = convert(bit,(x.status & 16)/16),
  16482.         "TYPE"            = convert(smallint, 1 /*DBPROPVAL_IT_BTREE*/),
  16483.         FILL_FACTOR        = convert(int, x.OrigFillFactor),
  16484.         INITIAL_SIZE        = convert(int,null),
  16485.         NULLS            = convert(int,null),
  16486.         SORT_BOOKMARKS        = convert(bit,0),
  16487.         AUTO_UPDATE        = convert(bit,1),
  16488.         NULL_COLLATION        = convert(int,4 /*DBPROPVAL_NC_LOW*/),
  16489.         ORDINAL_POSITION    = convert(int,c.colid),
  16490.         COLUMN_NAME        = index_col(user_name(o.uid)+'.'+o.name, x.indid, c.colid),
  16491.         COLUMN_GUID        = convert(binary(16),null),
  16492.         COLUMN_PROPID        = convert(int,null),
  16493.         COLLATION        = convert(smallint, 1 /*DB_COLLATION_ASC*/),
  16494.         CARDINALITY        = case when (x.status & 2) = 2 then x.rows else null end,
  16495.         PAGES            = convert(int, x.dpages),
  16496.         FILTER_CONDITION    = convert(varchar(1),null),
  16497.         INTEGRATED        = convert(bit,(x.status & 16)/16)
  16498.         
  16499.     from    sysobjects o, sysindexes x, syscolumns c, sysusers u
  16500.     where    o.type in ('U')
  16501.     and     o.name = @table_name
  16502.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  16503.     and    (@index_name is null or @index_name = x.name)
  16504.     and     x.id = o.id
  16505.     and     x.id = c.id
  16506.     and      c.colid < x.keycnt+(x.status&16)/16
  16507.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  16508.     and     (
  16509.         suser_id() = 1     /* User is the System Administrator */
  16510.         or o.uid = user_id()     /* User created the object */
  16511.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16512.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16513.             from sysprotects p
  16514.             where p.id = o.id
  16515.             /* get rows for public,current user,user's group */
  16516.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16517.             /* check for SELECT,EXECUTE privilege */
  16518.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16519.             ) = 1    /* final magic...compare Grants      */
  16520.            )
  16521.     order by 8 desc, 4, 5, 6, 17
  16522. go
  16523. dump tran master with no_log
  16524. go
  16525. create procedure sp_indexes_rowset;2
  16526.     (
  16527.     @index_name    varchar(255) = null,
  16528.     @table_schema    varchar(255) = null     
  16529.     )
  16530. as
  16531.     select    TABLE_CATALOG        = db_name(),
  16532.         TABLE_SCHEMA        = user_name(o.uid),
  16533.         TABLE_NAME        = o.name,
  16534.         INDEX_CATALOG        = db_name(),        
  16535.         INDEX_SCHEMA        = user_name(o.uid),
  16536.         INDEX_NAME        = x.name,
  16537.         PRIMARY_KEY        = convert(bit,(x.status & 0x800)/0x800),
  16538.         "UNIQUE"        = convert(bit,(x.status & 2)/2),
  16539.         "CLUSTERED"        = convert(bit,(x.status & 16)/16),
  16540.         "TYPE"            = convert(smallint, 1 /*DBPROPVAL_IT_BTREE*/),
  16541.         FILL_FACTOR        = convert(int, x.OrigFillFactor),
  16542.         INITIAL_SIZE        = convert(int,null),
  16543.         NULLS            = convert(int,null),
  16544.         SORT_BOOKMARKS        = convert(bit,0),
  16545.         AUTO_UPDATE        = convert(bit,1),
  16546.         NULL_COLLATION        = convert(int,4 /*DBPROPVAL_NC_LOW*/),
  16547.         ORDINAL_POSITION    = convert(int,c.colid),
  16548.         COLUMN_NAME        = index_col(user_name(o.uid)+'.'+o.name, x.indid, c.colid),
  16549.         COLUMN_GUID        = convert(binary(16),null),
  16550.         COLUMN_PROPID        = convert(int,null),
  16551.         COLLATION        = convert(smallint, 1 /*DB_COLLATION_ASC*/),
  16552.         CARDINALITY        = case when (x.status & 2) = 2 then x.rows else null end,
  16553.         PAGES            = convert(int, x.dpages),
  16554.         FILTER_CONDITION    = convert(varchar(1),null),
  16555.         INTEGRATED        = convert(bit,(x.status & 16)/16) 
  16556.         
  16557.     from    sysobjects o, sysindexes x, syscolumns c, sysusers u
  16558.     where    o.type in ('U')
  16559.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  16560.     and    (@index_name is null or @index_name = x.name)
  16561.     and     x.id = o.id
  16562.     and     x.id = c.id
  16563.     and      c.colid < x.keycnt+(x.status&16)/16
  16564.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  16565.     and     (
  16566.         suser_id() = 1     /* User is the System Administrator */
  16567.         or o.uid = user_id()     /* User created the object */
  16568.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16569.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16570.             from sysprotects p
  16571.             where p.id = o.id
  16572.             /* get rows for public,current user,user's group */
  16573.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16574.             /* check for SELECT,EXECUTE privilege */
  16575.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16576.             ) = 1    /* final magic...compare Grants      */
  16577.            )
  16578.     order by 8 desc, 4, 5, 6, 17
  16579. go
  16580. dump tran master with no_log
  16581. go
  16582.  
  16583.  
  16584. if (charindex('7.00', @@version) > 0)
  16585.     drop procedure sp_indexes_rowset
  16586. else
  16587. begin
  16588.     print ''
  16589.     print ''
  16590.     print 'Warning:'
  16591.     print 'you are installing the stored procedures '
  16592.     print 'on a pre 7.0 SQL Server.'
  16593.     print 'Ignore the following errors.'
  16594. end
  16595. go
  16596.  
  16597. /*     7.0 version */
  16598. create procedure sp_indexes_rowset
  16599.     (
  16600.     @table_name    sysname, 
  16601.     @index_name    sysname = null,
  16602.     @table_schema    sysname = null     
  16603.     )
  16604. as
  16605.     select    TABLE_CATALOG        = db_name(),
  16606.         TABLE_SCHEMA        = user_name(o.uid),
  16607.         TABLE_NAME        = o.name,
  16608.         INDEX_CATALOG        = db_name(),        
  16609.         INDEX_SCHEMA        = user_name(o.uid),
  16610.         INDEX_NAME        = x.name,
  16611.         PRIMARY_KEY        = convert(bit,(x.status & 0x800)/0x800),
  16612.         "UNIQUE"        = convert(bit,(x.status & 2)/2),
  16613.         "CLUSTERED"        = convert(bit,(x.status & 16)/16),
  16614.         "TYPE"            = convert(smallint, 1 /*DBPROPVAL_IT_BTREE*/),
  16615.         FILL_FACTOR        = convert(int, x.OrigFillFactor),
  16616.         INITIAL_SIZE        = convert(int,null),
  16617.         NULLS            = convert(int,null),
  16618.         SORT_BOOKMARKS        = convert(bit,0),
  16619.         AUTO_UPDATE        = convert(bit,1),
  16620.         NULL_COLLATION        = convert(int,4 /*DBPROPVAL_NC_LOW*/),
  16621.         ORDINAL_POSITION    = convert(int,c.colid),
  16622.         COLUMN_NAME        = index_col(user_name(o.uid)+'.'+o.name, x.indid, c.colid),
  16623.         COLUMN_GUID        = convert(uniqueidentifier,null),
  16624.         COLUMN_PROPID        = convert(int,null),
  16625.         COLLATION        = convert(smallint, 1 /*DB_COLLATION_ASC*/),
  16626.         CARDINALITY        = case when (x.status & 2) = 2 then x.rows else null end,
  16627.         PAGES            = convert(int, x.dpages),
  16628.         FILTER_CONDITION    = convert(nvarchar(1),null),
  16629.         INTEGRATED        = convert(bit,(x.status & 16)/16) 
  16630.         
  16631.     from    sysobjects o, sysindexes x, syscolumns c
  16632.     where    o.type in ('U')
  16633.     and     o.name = @table_name
  16634.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  16635.     and    (@index_name is null or @index_name = x.name)
  16636.     and     x.id = o.id
  16637.     and     x.id = c.id
  16638.     and      c.colid < x.keycnt+(x.status&16)/16
  16639.     and    index_col(user_name(o.uid)+'.'+o.name, x.indid, c.colid) is not null
  16640.     and     permissions(o.id) <> 0
  16641.     and     (x.status&32) = 0  -- No hypothetical indexes
  16642.     order by 8 desc, 4, 5, 6, 17
  16643. go
  16644. dump tran master with no_log
  16645. go
  16646. create procedure sp_indexes_rowset;2
  16647.     (
  16648.     @index_name    sysname = null,
  16649.     @table_schema    sysname = null     
  16650.     )
  16651. as
  16652.     select    TABLE_CATALOG        = db_name(),
  16653.         TABLE_SCHEMA        = user_name(o.uid),
  16654.         TABLE_NAME        = o.name,
  16655.         INDEX_CATALOG        = db_name(),        
  16656.         INDEX_SCHEMA        = user_name(o.uid),
  16657.         INDEX_NAME        = x.name,
  16658.         PRIMARY_KEY        = convert(bit,(x.status & 0x800)/0x800),
  16659.         "UNIQUE"        = convert(bit,(x.status & 2)/2),
  16660.         "CLUSTERED"        = convert(bit,(x.status & 16)/16),
  16661.         "TYPE"            = convert(smallint, 1 /*DBPROPVAL_IT_BTREE*/),
  16662.         FILL_FACTOR        = convert(int, x.OrigFillFactor),
  16663.         INITIAL_SIZE        = convert(int,null),
  16664.         NULLS            = convert(int,null),
  16665.         SORT_BOOKMARKS        = convert(bit,0),
  16666.         AUTO_UPDATE        = convert(bit,1),
  16667.         NULL_COLLATION        = convert(int,4 /*DBPROPVAL_NC_LOW*/),
  16668.         ORDINAL_POSITION    = convert(int,c.colid),
  16669.         COLUMN_NAME        = index_col(user_name(o.uid)+'.'+o.name, x.indid, c.colid),
  16670.         COLUMN_GUID        = convert(uniqueidentifier,null),
  16671.         COLUMN_PROPID        = convert(int,null),
  16672.         COLLATION        = convert(smallint, 1 /*DB_COLLATION_ASC*/),
  16673.         CARDINALITY        = case when (x.status & 2) = 2 then x.rows else null end,
  16674.         PAGES            = convert(int, x.dpages),
  16675.         FILTER_CONDITION    = convert(nvarchar(1),null),
  16676.         INTEGRATED        = convert(bit,(x.status & 16)/16) 
  16677.         
  16678.     from    sysobjects o, sysindexes x, syscolumns c
  16679.     where    o.type in ('U')
  16680.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  16681.     and    (@index_name is null or @index_name = x.name)
  16682.     and     x.id = o.id
  16683.     and     x.id = c.id
  16684.     and      c.colid < x.keycnt+(x.status&16)/16
  16685.     and    index_col(user_name(o.uid)+'.'+o.name, x.indid, c.colid) is not null
  16686.     and     permissions(o.id) <> 0
  16687.     and     (x.status&32) = 0  -- No hypothetical indexes
  16688.     order by 8 desc, 4, 5, 6, 17
  16689. go
  16690. dump tran master with no_log
  16691. go
  16692. create procedure sp_indexes_rowset;5
  16693.     (
  16694.     @table_server        sysname,
  16695.     @table_catalog        sysname = null,
  16696.     @table_name        sysname = null,
  16697.     @index_name        sysname = null,
  16698.     @table_schema        sysname = null
  16699.     )
  16700. as
  16701.     select
  16702.         TABLE_CATALOG,    
  16703.         TABLE_SCHEMA,    
  16704.         TABLE_NAME,    
  16705.         INDEX_CATALOG,        
  16706.         INDEX_SCHEMA,    
  16707.         INDEX_NAME,    
  16708.         PRIMARY_KEY,    
  16709.         "UNIQUE",    
  16710.         "CLUSTERED",    
  16711.         "TYPE",        
  16712.         FILL_FACTOR,    
  16713.         INITIAL_SIZE,    
  16714.         NULLS,        
  16715.         SORT_BOOKMARKS,    
  16716.         AUTO_UPDATE,    
  16717.         NULL_COLLATION,    
  16718.         ORDINAL_POSITION,
  16719.         COLUMN_NAME,    
  16720.         COLUMN_GUID,    
  16721.         COLUMN_PROPID,    
  16722.         COLLATION,    
  16723.         CARDINALITY,    
  16724.         PAGES,        
  16725.         FILTER_CONDITION
  16726.     --    INTEGRATED
  16727.     from master.dbo.SYSREMOTE_INDEXES <
  16728.                 @table_server,
  16729.                 @table_catalog,
  16730.                 @table_schema,
  16731.                         @index_name,
  16732.                 NULL,            /* TYPE (index type) */
  16733.                 @table_name >
  16734.     order by 8 desc, 4, 5, 6, 17
  16735. go
  16736.  
  16737. grant execute on sp_indexes_rowset to public
  16738. go
  16739.  
  16740. dump tran master with no_log
  16741. go
  16742. if (charindex('6.00', @@version) > 0)
  16743.     begin
  16744.     if (exists (select * from sysobjects
  16745.             where name = 'sp_indexes_rowset' and type = 'P '))
  16746.         begin
  16747.         drop procedure sp_indexes_rowset
  16748.         dump tran master with no_log
  16749.         end
  16750.     end
  16751. go
  16752.  
  16753.  
  16754. print ''
  16755. print 'creating sp_primary_keys_rowset'
  16756. go
  16757.  
  16758. /*    Procedure for 6.0 and 6.5 servers */
  16759. create procedure sp_primary_keys_rowset
  16760.     (
  16761.        @table_name        varchar(255),
  16762.     @table_schema         varchar(244) = null
  16763.     )
  16764. as
  16765.     select    TABLE_CATALOG    = db_name(),
  16766.             TABLE_SCHEMA    = user_name(o.uid),
  16767.             TABLE_NAME        = o.name,    
  16768.             COLUMN_NAME        = c.name,
  16769.             COLUMN_GUID        = convert(binary(16),null),
  16770.             COLUMN_PROPID    = convert(int,null),
  16771.             ORDINAL            = convert(int,c1.colid),
  16772.             PK_NAME            = i.name
  16773.     from    sysindexes i, syscolumns c, sysobjects o, syscolumns c1, sysusers u
  16774.     where    o.type in ('U')
  16775.     and     o.name = @table_name
  16776.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  16777.     and     o.id = c.id
  16778.     and     o.id = i.id
  16779.     and     (i.status & 0x800) = 0x800
  16780.     and     c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, c1.colid)
  16781.     and     c1.id = c.id
  16782.     and     c1.colid <= i.keycnt    /* create rows from 1 to keycnt */
  16783.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  16784.     and     (
  16785.         suser_id() = 1     /* User is the System Administrator */
  16786.         or o.uid = user_id()     /* User created the object */
  16787.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16788.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16789.             from sysprotects p
  16790.             where p.id = o.id
  16791.             /* get rows for public,current user,user's group */
  16792.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16793.             /* check for SELECT,EXECUTE privilege */
  16794.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16795.             ) = 1    /* final magic...compare Grants      */
  16796.            )
  16797.     order by 2, 3
  16798. go
  16799. dump tran master with no_log
  16800. go
  16801. create procedure sp_primary_keys_rowset;2
  16802.     (
  16803.      @table_schema         varchar(244) = null
  16804.     )
  16805. as
  16806.     select    TABLE_CATALOG    = db_name(),
  16807.             TABLE_SCHEMA    = user_name(o.uid),
  16808.             TABLE_NAME        = o.name,    
  16809.             COLUMN_NAME        = c.name,
  16810.             COLUMN_GUID        = convert(binary(16),null),
  16811.             COLUMN_PROPID    = convert(int,null),
  16812.             ORDINAL            = convert(int,c1.colid),
  16813.             PK_NAME            = i.name
  16814.     from    sysindexes i, syscolumns c, sysobjects o, syscolumns c1, sysusers u
  16815.     where    o.type in ('U')
  16816.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  16817.     and     o.id = c.id
  16818.     and     o.id = i.id
  16819.     and     (i.status & 0x800) = 0x800
  16820.     and     c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, c1.colid)
  16821.     and     c1.id = c.id
  16822.     and     c1.colid <= i.keycnt    /* create rows from 1 to keycnt */
  16823.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  16824.     and     (
  16825.         suser_id() = 1     /* User is the System Administrator */
  16826.         or o.uid = user_id()     /* User created the object */
  16827.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16828.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16829.             from sysprotects p
  16830.             where p.id = o.id
  16831.             /* get rows for public,current user,user's group */
  16832.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16833.             /* check for SELECT,EXECUTE privilege */
  16834.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16835.             ) = 1    /* final magic...compare Grants      */
  16836.            )
  16837.     order by 2, 3
  16838. go
  16839.  
  16840. if (charindex('7.00', @@version) = 0)
  16841. begin
  16842.     print ''
  16843.     print ''
  16844.     print 'Warning:'
  16845.     print 'you are installing the stored procedures '
  16846.     print 'on a pre 7.0 SQL Server.'
  16847.     print 'Ignore the following errors.'
  16848. end
  16849. else
  16850.     drop proc sp_primary_keys_rowset
  16851. go
  16852.  
  16853. /*    Procedure for 7.0 servers */
  16854. create procedure sp_primary_keys_rowset
  16855.     (
  16856.        @table_name        sysname,
  16857.     @table_schema         sysname = null
  16858.     )
  16859. as
  16860.     select    TABLE_CATALOG    = db_name(),
  16861.             TABLE_SCHEMA    = user_name(o.uid),
  16862.             TABLE_NAME        = o.name,    
  16863.             COLUMN_NAME        = c.name,
  16864.             COLUMN_GUID        = convert(uniqueidentifier,null),
  16865.             COLUMN_PROPID    = convert(int,null),
  16866.             ORDINAL            = convert(int,c1.colid),
  16867.             PK_NAME            = i.name
  16868.     from    sysindexes i, syscolumns c, sysobjects o, syscolumns c1
  16869.     where    o.type in ('U')
  16870.     and     o.name = @table_name
  16871.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  16872.     and     o.id = c.id
  16873.     and     o.id = i.id
  16874.     and     (i.status & 0x800) = 0x800
  16875.     and     c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, c1.colid)
  16876.     and     c1.id = c.id
  16877.     and     c1.colid <= i.keycnt    /* create rows from 1 to keycnt */
  16878.     and    permissions(o.id) <> 0
  16879.     order by 2, 3
  16880. go
  16881. dump tran master with no_log
  16882. go
  16883. create procedure sp_primary_keys_rowset;2
  16884.     (
  16885.      @table_schema     sysname = null
  16886.     )
  16887. as
  16888.     select    TABLE_CATALOG    = db_name(),
  16889.             TABLE_SCHEMA    = user_name(o.uid),
  16890.             TABLE_NAME        = o.name,    
  16891.             COLUMN_NAME        = c.name,
  16892.             COLUMN_GUID        = convert(uniqueidentifier,null),
  16893.             COLUMN_PROPID    = convert(int,null),
  16894.             ORDINAL            = convert(int,c1.colid),
  16895.             PK_NAME            = i.name
  16896.     from    sysindexes i, syscolumns c, sysobjects o, syscolumns c1
  16897.     where    o.type in ('U')
  16898.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  16899.     and     o.id = c.id
  16900.     and     o.id = i.id
  16901.     and     (i.status & 0x800) = 0x800
  16902.     and     c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, c1.colid)
  16903.     and     c1.id = c.id
  16904.     and     c1.colid <= i.keycnt    /* create rows from 1 to keycnt */
  16905.     and    permissions(o.id) <> 0
  16906.     order by 2, 3
  16907. go
  16908. dump tran master with no_log
  16909. go
  16910. create procedure sp_primary_keys_rowset;3
  16911.     (
  16912.        @table_name        sysname = null,
  16913.     @table_schema         sysname = null
  16914.     )
  16915. as
  16916. IF @table_name is not NULL
  16917.     BEGIN
  16918.     select    TABLE_CATALOG    = db_name(),
  16919.             TABLE_SCHEMA    = user_name(o.uid),
  16920.             TABLE_NAME        = o.name,    
  16921.             COLUMN_NAME        = c.name,
  16922.             COLUMN_GUID        = convert(uniqueidentifier,null),
  16923.             COLUMN_PROPID    = convert(int,null),
  16924.             ORDINAL            = convert(int,c1.colid),
  16925.             PK_NAME            = i.name
  16926.     from    sysindexes i, syscolumns c, sysobjects o, syscolumns c1
  16927.     where    o.type in ('U')
  16928.     and     o.name = @table_name
  16929.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  16930.     and     o.id = c.id
  16931.     and     o.id = i.id
  16932.     and     (i.status & 0x800) = 0x800
  16933.     and     c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, c1.colid)
  16934.     and     c1.id = c.id
  16935.     and     c1.colid <= i.keycnt    /* create rows from 1 to keycnt */
  16936.     and    permissions(o.id) <> 0
  16937.     END
  16938. ELSE
  16939.     BEGIN
  16940.     select    TABLE_CATALOG    = db_name(),
  16941.             TABLE_SCHEMA    = user_name(o.uid),
  16942.             TABLE_NAME        = o.name,    
  16943.             COLUMN_NAME        = c.name,
  16944.             COLUMN_GUID        = convert(uniqueidentifier,null),
  16945.             COLUMN_PROPID    = convert(int,null),
  16946.             ORDINAL            = convert(int,c1.colid),
  16947.             PK_NAME            = i.name
  16948.     from    sysindexes i, syscolumns c, sysobjects o, syscolumns c1
  16949.     where    o.type in ('U')
  16950.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  16951.     and     o.id = c.id
  16952.     and     o.id = i.id
  16953.     and     (i.status & 0x800) = 0x800
  16954.     and     c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, c1.colid)
  16955.     and     c1.id = c.id
  16956.     and     c1.colid <= i.keycnt    /* create rows from 1 to keycnt */
  16957.     and    permissions(o.id) <> 0
  16958.     END
  16959. go
  16960. dump tran master with no_log
  16961. go
  16962. create procedure sp_primary_keys_rowset;5
  16963.     (
  16964.     @table_server        sysname,
  16965.     @table_catalog        sysname = null,
  16966.     @table_name            sysname = null,
  16967.     @table_schema        sysname = null
  16968.     )
  16969. as
  16970.     select
  16971.         TABLE_CATALOG,    
  16972.         TABLE_SCHEMA,    
  16973.         TABLE_NAME,    
  16974.         COLUMN_NAME,    
  16975.         COLUMN_GUID,    
  16976.         COLUMN_PROPID,    
  16977.         ORDINAL
  16978.         --PK_NAME        
  16979.     from master.dbo.SYSREMOTE_PRIMARY_KEYS <
  16980.                 @table_server,
  16981.                 @table_catalog,
  16982.                 @table_schema,
  16983.                 @table_name >
  16984.     order by 1,2,3
  16985. go
  16986.  
  16987. grant execute on sp_primary_keys_rowset to public
  16988. go
  16989.  
  16990. dump tran master with no_log
  16991. go
  16992. if (charindex('6.00', @@version) > 0)
  16993.     begin
  16994.     if (exists (select * from sysobjects
  16995.             where name = 'sp_primary_keys_rowset' and type = 'P '))
  16996.         begin
  16997.         drop procedure sp_primary_keys_rowset
  16998.         dump tran master with no_log
  16999.         end
  17000.     end
  17001. go
  17002.  
  17003.  
  17004. print ''
  17005. print 'creating sp_provider_types_rowset'
  17006. go
  17007.  
  17008. /*    Procedure for 6.0 and 6.50 servers */
  17009. create proc sp_provider_types_rowset
  17010.     (
  17011.     @data_type     smallint = null,
  17012.     @best_match    tinyint  = null
  17013.     )
  17014. as
  17015.     select
  17016.         TYPE_NAME         = case when t.usertype = 80 then t.name 
  17017.                         else d.type_name 
  17018.                         end,
  17019.         DATA_TYPE         = d.oledb_data_type,                            
  17020.         COLUMN_SIZE        = case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ 
  17021.                             then @@max_precision
  17022.                             else coalesce(d.column_size,d.data_precision,t.prec) end,
  17023.         LITERAL_PREFIX         = d.literal_prefix,
  17024.         LITERAL_SUFFIX         = d.literal_suffix,
  17025.         CREATE_PARAMS         = convert(varchar(32),e.CREATE_PARAMS),
  17026.         IS_NULLABLE        = t.allownulls,
  17027.         CASE_SENSITIVE        = d.case_sensitive,
  17028.         SEARCHABLE         = d.searchable,
  17029.         UNSIGNED_ATTRIBUTE    = d.unsigned_attribute,
  17030.         FIXED_PREC_SCALE    = d.fixed_prec_scale,
  17031.         AUTO_UNIQUE_VALUE    = d.auto_unique_value, 
  17032.         LOCAL_TYPE_NAME        = case    when t.usertype = 80 then t.name 
  17033.                         else d.local_type_name
  17034.                         end,
  17035.         MINIMUM_SCALE        = convert(smallint,
  17036.                         case 
  17037.                         when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then 0 
  17038.                         else null 
  17039.                         end),
  17040.         MAXIMUM_SCALE        = convert(smallint,
  17041.                         case 
  17042.                         when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ 
  17043.                         then @@max_precision
  17044.                         else null 
  17045.                         end),
  17046.         GUID            = convert(binary(16),null),
  17047.         TYPELIB            = convert(varchar(1),null),
  17048.         VERSION            = convert(varchar(1),null),
  17049.         IS_LONG            = d.is_long,
  17050.         BEST_MATCH        = case when t.usertype = 80
  17051.                         then convert(bit,0)
  17052.                         else d.best_match
  17053.                         end,
  17054.         IS_FIXEDLENGTH        = convert(bit, 
  17055.                         case when d.fixlen is null then 0 else 1 end)
  17056.     from     master.dbo.spt_provider_types d, master.dbo.spt_datatype_info_ext e, systypes t
  17057.     where    d.ss_dtype = t.type 
  17058.     and    t.usertype <= 100
  17059.     and    t.usertype <> 18 /* sysname */
  17060.     and    (case when t.usertype = 80 /* TIMESTAMP */ then 1 else 0 end 
  17061.             = case when d.type_name = 'timestamp' then 1 else 0 end)
  17062.     and     t.usertype *= e.user_type 
  17063.     and     e.AUTO_INCREMENT = 0
  17064.     and     t.type not in (111,109,38,110,55,63)    /* get rid of nullable types */
  17065.     and    (@data_type is null or d.oledb_data_type = @data_type)
  17066.     and    (@best_match is null or d.best_match = @best_match)
  17067.     order by 2
  17068. go
  17069. dump tran master with no_log
  17070. go
  17071.  
  17072. if (charindex('7.00', @@version) = 0)
  17073. begin
  17074.     print ''
  17075.     print ''
  17076.     print 'Warning:'
  17077.     print 'you are installing the stored procedures '
  17078.     print 'on a pre 7.0 SQL Server.'
  17079.     print 'Ignore the following errors.'
  17080. end
  17081. else
  17082.     drop proc sp_provider_types_rowset
  17083. go
  17084.  
  17085. /*    Procedure for 7.0 server */
  17086. create proc sp_provider_types_rowset
  17087.     (
  17088.     @data_type     smallint = null,
  17089.     @best_match    tinyint  = null
  17090.     )
  17091. as
  17092.     select
  17093.         TYPE_NAME         = case    when t.usertype = 80 then t.name 
  17094.                         else d.type_name 
  17095.                         end,
  17096.         DATA_TYPE         = d.oledb_data_type,                            
  17097.         COLUMN_SIZE        = case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ 
  17098.                             then @@max_precision
  17099.                             else coalesce(d.column_size,d.data_precision,t.prec) end,
  17100.         LITERAL_PREFIX         = d.literal_prefix,
  17101.         LITERAL_SUFFIX         = d.literal_suffix,
  17102.         CREATE_PARAMS         = convert(nvarchar(32),e.CREATE_PARAMS),
  17103.         IS_NULLABLE        = t.allownulls,
  17104.         CASE_SENSITIVE        = d.case_sensitive,
  17105.         SEARCHABLE         = d.searchable,
  17106.         UNSIGNED_ATTRIBUTE    = d.unsigned_attribute,
  17107.         FIXED_PREC_SCALE    = d.fixed_prec_scale,
  17108.         AUTO_UNIQUE_VALUE    = d.auto_unique_value, 
  17109.         LOCAL_TYPE_NAME        = case    when t.usertype = 80 then t.name 
  17110.                         else d.local_type_name
  17111.                         end,
  17112.         MINIMUM_SCALE        = convert(smallint,
  17113.                         case 
  17114.                         when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then 0 
  17115.                         else null 
  17116.                         end),
  17117.         MAXIMUM_SCALE        = convert(smallint,
  17118.                         case 
  17119.                         when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ 
  17120.                         then @@max_precision
  17121.                         else null 
  17122.                         end),
  17123.         GUID            = convert(uniqueidentifier,null),
  17124.         TYPELIB            = convert(nvarchar(1),null),
  17125.         VERSION            = convert(nvarchar(1),null),
  17126.         IS_LONG            = d.is_long,
  17127.         BEST_MATCH        = case    when t.usertype = 80
  17128.                         then convert(bit,0)
  17129.                         else d.best_match
  17130.                         end,
  17131.         IS_FIXEDLENGTH        = convert(bit, 
  17132.                         case when d.fixlen is null then 0 else 1 end)
  17133.  
  17134.     from master.dbo.spt_provider_types d
  17135.         INNER JOIN master.dbo.systypes t on d.ss_dtype = t.xtype
  17136.         LEFT OUTER JOIN master.dbo.spt_datatype_info_ext e on
  17137.             t.xusertype = e.user_type
  17138.             and e.AUTO_INCREMENT = 0
  17139.     where    (@data_type is null or d.oledb_data_type = @data_type)
  17140.     and    (@best_match is null or d.best_match = @best_match)    
  17141.     and    t.usertype <= 255
  17142.     and    t.usertype <> 18 /* sysname */
  17143.     order by 2
  17144. go
  17145.  
  17146. grant execute on sp_provider_types_rowset to public
  17147. go
  17148.  
  17149. dump tran master with no_log
  17150. go
  17151. if (charindex('6.00', @@version) > 0)
  17152.     begin
  17153.     if (exists (select * from sysobjects
  17154.         where name = 'sp_provider_types_rowset' and type = 'P '))
  17155.         begin
  17156.         drop procedure sp_provider_types_rowset
  17157.         dump tran master with no_log
  17158.         end
  17159.     end
  17160. go
  17161.  
  17162.  
  17163. print ''
  17164. print 'creating sp_procedure_params_rowset'
  17165. go
  17166.  
  17167. /*    Procedure for 6.0 and 6.50 servers */
  17168. create procedure sp_procedure_params_rowset
  17169.     (
  17170.        @procedure_name        varchar(255) = null,
  17171.     @group_number        int = null,
  17172.     @procedure_schema     varchar(255) = null,
  17173.     @parameter_name        varchar(255) = null
  17174.     )
  17175. as
  17176. IF @procedure_name is not null
  17177.     BEGIN
  17178.     select
  17179.         PROCEDURE_CATALOG     = db_name(),
  17180.         PROCEDURE_SCHEMA     = user_name(),
  17181.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  17182.         PARAMETER_NAME         = c.name,
  17183.         ORDINAL_POSITION     = convert(smallint,c.colid),
  17184.         PARAMETER_TYPE         = convert(smallint, 1+((c.status/64)&1)),
  17185.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  17186.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  17187.         IS_NULLABLE        = convert(bit,
  17188.                         case when d.oledb_data_type = 11 /*DBTYPE_BOOL*/ 
  17189.                         then 0 else 1
  17190.                         end),
  17191.         DATA_TYPE        = d.oledb_data_type,
  17192.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  17193.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  17194.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  17195.                         then coalesce(d.column_size,c.length)
  17196.                         else null 
  17197.                         end),
  17198.         CHARACTER_OCTET_LENGTH    = convert(int,
  17199.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  17200.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  17201.                         then coalesce(d.column_size,c.length)
  17202.                         else null 
  17203.                         end),
  17204.         NUMERIC_PRECISION    = convert(smallint,
  17205.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec 
  17206.                             when d.fixed_prec_scale =1 then d.data_precision else null end),
  17207.         NUMERIC_SCALE        = convert(smallint, 
  17208.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  17209.         DESCRIPTION        = convert(varchar(1),null),
  17210.         TYPE_NAME        = d.type_name,
  17211.         LOCAL_TYPE_NAME        = d.local_type_name
  17212.     
  17213.     from
  17214.         syscolumns c,
  17215.         sysobjects o,
  17216.         master.dbo.spt_provider_types d,
  17217.         systypes t
  17218.     where
  17219.         o.name = @procedure_name
  17220.     and    o.type = 'P'                            /* Just Procedures */
  17221.     and    (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  17222.     and    o.id = c.id
  17223.     and    c.number = @group_number
  17224.     and    c.type = d.ss_dtype
  17225.     and    c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  17226.     and    c.usertype = t.usertype
  17227.     and    (t.usertype != 80 or d.type_name='timestamp')
  17228.     and    (@parameter_name is null or @parameter_name = c.name)
  17229.     UNION ALL
  17230.     SELECT           /* return value row*/
  17231.         PROCEDURE_CATALOG     = db_name(),
  17232.         PROCEDURE_SCHEMA     = user_name(),
  17233.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  17234.         PARAMETER_NAME         = convert(sysname,'RETURN_VALUE'),
  17235.         ORDINAL_POSITION     = convert(smallint,0),
  17236.         PARAMETER_TYPE         = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  17237.         PARAMETER_HASDEFAULT    = convert(tinyint,0),
  17238.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  17239.         IS_NULLABLE        = convert(bit,0),
  17240.         DATA_TYPE        = convert(smallint, 3 /*DBTYPE_I4*/),
  17241.         CHARACTER_MAXIMUM_LENGTH= convert(int,null),
  17242.         CHARACTER_OCTET_LENGTH    = convert(int,null),
  17243.         NUMERIC_PRECISION    = convert(smallint,10),
  17244.         NUMERIC_SCALE        = convert(smallint,null),
  17245.         DESCRIPTION        = convert(varchar(1),null),
  17246.         TYPE_NAME        = convert(sysname,'int'),
  17247.         LOCAL_TYPE_NAME        = convert(sysname,'int')
  17248.     from
  17249.         syscomments c,
  17250.         sysobjects o
  17251.     where
  17252.         o.name = @procedure_name
  17253.     and    o.type = 'P'                        /* Just Procedures */
  17254.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  17255.     and    (@parameter_name is null or @parameter_name = 'RETURN_VALUE')
  17256.     and    c.id = o.id
  17257.     and    c.number = @group_number
  17258.     and c.colid = 1
  17259.     order by 2, 3, 5
  17260.     END
  17261. ELSE
  17262.     BEGIN
  17263.     select
  17264.         PROCEDURE_CATALOG     = db_name(),
  17265.         PROCEDURE_SCHEMA     = user_name(),
  17266.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  17267.         PARAMETER_NAME         = c.name,
  17268.         ORDINAL_POSITION     = convert(smallint,c.colid),
  17269.         PARAMETER_TYPE         = convert(smallint, 1+((c.status/64)&1)),
  17270.         PARAMETER_HASDEFAULT    = convert(tinyint,0),
  17271.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  17272.         IS_NULLABLE        = convert(bit,
  17273.                         case when d.oledb_data_type = 11 /*DBTYPE_BOOL*/ 
  17274.                         then 0 else 1
  17275.                         end),
  17276.         DATA_TYPE        = d.oledb_data_type,
  17277.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  17278.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  17279.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  17280.                         then coalesce(d.column_size,c.length)
  17281.                         else null 
  17282.                         end),
  17283.         CHARACTER_OCTET_LENGTH    = convert(int,
  17284.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  17285.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  17286.                         then coalesce(d.column_size,c.length)
  17287.                         else null 
  17288.                         end),
  17289.         NUMERIC_PRECISION    = convert(smallint,
  17290.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  17291.                             when d.fixed_prec_scale =1 then d.data_precision else null end),
  17292.         NUMERIC_SCALE        = convert(smallint, 
  17293.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  17294.         DESCRIPTION        = convert(varchar(1),null),
  17295.         TYPE_NAME        = d.type_name,
  17296.         LOCAL_TYPE_NAME        = d.local_type_name
  17297.     
  17298.     from
  17299.         syscolumns c,
  17300.         sysobjects o,
  17301.         master.dbo.spt_provider_types d,
  17302.         systypes t
  17303.     where
  17304.         o.type = 'P'                            /* Just Procedures */
  17305.     and    (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  17306.     and    o.id = c.id
  17307.     and    c.type = d.ss_dtype
  17308.     and    c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  17309.     and    c.usertype = t.usertype
  17310.     and    (t.usertype != 80 or d.type_name='timestamp')
  17311.     and    (@parameter_name is null or @parameter_name = c.name)
  17312.     UNION ALL
  17313.     SELECT           /* return value row*/
  17314.         PROCEDURE_CATALOG     = db_name(),
  17315.         PROCEDURE_SCHEMA     = user_name(),
  17316.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  17317.         PARAMETER_NAME         = convert(sysname,'RETURN_VALUE'),
  17318.         ORDINAL_POSITION     = convert(smallint,0),
  17319.         PARAMETER_TYPE         = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  17320.         PARAMETER_HASDEFAULT    = convert(tinyint,0),
  17321.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  17322.         IS_NULLABLE        = convert(bit,0),
  17323.         DATA_TYPE        = convert(smallint, 3 /*DBTYPE_I4*/),
  17324.         CHARACTER_MAXIMUM_LENGTH= convert(int,null),
  17325.         CHARACTER_OCTET_LENGTH    = convert(int,null),
  17326.         NUMERIC_PRECISION    = convert(smallint,10),
  17327.         NUMERIC_SCALE        = convert(smallint,null),
  17328.         DESCRIPTION        = convert(varchar(1),null),
  17329.         TYPE_NAME        = convert(sysname,'int'),
  17330.         LOCAL_TYPE_NAME        = convert(sysname,'int')
  17331.     from
  17332.         syscomments c,
  17333.         sysobjects o
  17334.     where
  17335.         o.type = 'P'                        /* Just Procedures */
  17336.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  17337.     and    (@parameter_name is null or @parameter_name = 'RETURN_VALUE')
  17338.     and    c.id = o.id
  17339.     and     c.colid = 1
  17340.     order by 2, 3, 5
  17341.     END
  17342. go
  17343. dump tran master with no_log
  17344. go
  17345. create procedure sp_procedure_params_rowset;2
  17346.     (
  17347.       @handle            int output,
  17348.        @scrollopt        int output,
  17349.     @ccopt            int output,
  17350.     @rows            int output,
  17351.        @procedure_name        varchar(255) = null,
  17352.     @group_number        int = null,
  17353.     @procedure_schema     varchar(255) = null,
  17354.     @parameter_name        varchar(255) = null
  17355.     )
  17356. as
  17357.     declare @ret int
  17358.  
  17359.  
  17360. SET NOCOUNT ON
  17361.  
  17362.     create table #spprocparamrowset1
  17363.         (
  17364.         PROCEDURE_CATALOG     sysname not null,
  17365.         PROCEDURE_SCHEMA     sysname not null,
  17366.         PROCEDURE_NAME         varchar(35) not null,
  17367.         PARAMETER_NAME         sysname not null,
  17368.         ORDINAL_POSITION     smallint not null,
  17369.         PARAMETER_TYPE         smallint null,
  17370.         PARAMETER_HASDEFAULT    tinyint null,
  17371.         PARAMETER_DEFAULT    varchar(255) null,
  17372.         IS_NULLABLE        bit not null,
  17373.         DATA_TYPE        smallint null,
  17374.         CHARACTER_MAXIMUM_LENGTH int null,
  17375.         CHARACTER_OCTET_LENGTH    int null,
  17376.         NUMERIC_PRECISION    smallint null,
  17377.         NUMERIC_SCALE        smallint null,
  17378.         DESCRIPTION        varchar(1) null,
  17379.         TYPE_NAME        sysname null,
  17380.         LOCAL_TYPE_NAME        sysname null,
  17381.         )
  17382.  
  17383. IF @procedure_name is not null
  17384.     BEGIN
  17385.     insert into #spprocparamrowset1
  17386.     select
  17387.         PROCEDURE_CATALOG     = db_name(),
  17388.         PROCEDURE_SCHEMA     = user_name(),
  17389.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  17390.         PARAMETER_NAME         = c.name,
  17391.         ORDINAL_POSITION     = convert(smallint,c.colid),
  17392.         PARAMETER_TYPE         = convert(smallint, 1+((c.status/64)&1)),
  17393.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  17394.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  17395.         IS_NULLABLE        = convert(bit,
  17396.                         case when d.oledb_data_type = 11 /*DBTYPE_BOOL*/ 
  17397.                         then 0 else 1
  17398.                         end),
  17399.         DATA_TYPE        = d.oledb_data_type,
  17400.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  17401.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  17402.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  17403.                         then coalesce(d.column_size,c.length)
  17404.                         else null 
  17405.                         end),
  17406.         CHARACTER_OCTET_LENGTH    = convert(int,
  17407.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  17408.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  17409.                         then coalesce(d.column_size,c.length)
  17410.                         else null 
  17411.                         end),
  17412.         NUMERIC_PRECISION    = convert(smallint,
  17413.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  17414.                             when d.fixed_prec_scale =1 then d.data_precision else null end),
  17415.         NUMERIC_SCALE        = convert(smallint, 
  17416.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  17417.         DESCRIPTION        = convert(varchar(1),null),
  17418.         TYPE_NAME        = d.type_name,
  17419.         LOCAL_TYPE_NAME        = d.local_type_name
  17420.     
  17421.     from
  17422.         syscolumns c,
  17423.         sysobjects o,
  17424.         master.dbo.spt_provider_types d,
  17425.         systypes t
  17426.     where
  17427.         o.name = @procedure_name
  17428.     and    o.type = 'P'                            /* Just Procedures */
  17429.     and    (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  17430.     and    o.id = c.id
  17431.     and    c.number = @group_number
  17432.     and    c.type = d.ss_dtype
  17433.     and    c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  17434.     and    c.usertype = t.usertype
  17435.     and    (t.usertype != 80 or d.type_name='timestamp')
  17436.     and    (@parameter_name is null or @parameter_name = c.name)
  17437.     UNION ALL
  17438.     SELECT           /* return value row*/
  17439.         PROCEDURE_CATALOG     = db_name(),
  17440.         PROCEDURE_SCHEMA     = user_name(),
  17441.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  17442.         PARAMETER_NAME         = convert(sysname,'RETURN_VALUE'),
  17443.         ORDINAL_POSITION     = convert(smallint,0),
  17444.         PARAMETER_TYPE         = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  17445.         PARAMETER_HASDEFAULT    = convert(tinyint,0),
  17446.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  17447.         IS_NULLABLE        = convert(bit,0),
  17448.         DATA_TYPE        = convert(smallint, 3 /*DBTYPE_I4*/),
  17449.         CHARACTER_MAXIMUM_LENGTH= convert(int,null),
  17450.         CHARACTER_OCTET_LENGTH    = convert(int,null),
  17451.         NUMERIC_PRECISION    = convert(smallint,10),
  17452.         NUMERIC_SCALE        = convert(smallint,null),
  17453.         DESCRIPTION        = convert(varchar(1),null),
  17454.         TYPE_NAME        = convert(sysname,'int'),
  17455.         LOCAL_TYPE_NAME        = convert(sysname,'int')
  17456.     from
  17457.         syscomments c,
  17458.         sysobjects o
  17459.     where
  17460.         o.name = @procedure_name
  17461.     and    o.type = 'P'                        /* Just Procedures */
  17462.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  17463.     and    (@parameter_name is null or @parameter_name = 'RETURN_VALUE')
  17464.     and    c.id = o.id
  17465.     and    c.number = @group_number
  17466.     and c.colid = 1
  17467.     order by 2, 3, 5
  17468.     END
  17469. ELSE
  17470.     BEGIN
  17471.     insert into #spprocparamrowset1
  17472.     select
  17473.         PROCEDURE_CATALOG     = db_name(),
  17474.         PROCEDURE_SCHEMA     = user_name(),
  17475.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  17476.         PARAMETER_NAME         = c.name,
  17477.         ORDINAL_POSITION     = convert(smallint,c.colid),
  17478.         PARAMETER_TYPE         = convert(smallint, 1+((c.status/64)&1)),
  17479.         PARAMETER_HASDEFAULT    = convert(tinyint,0),
  17480.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  17481.         IS_NULLABLE        = convert(bit,
  17482.                         case when d.oledb_data_type = 11 /*DBTYPE_BOOL*/ 
  17483.                         then 0 else 1
  17484.                         end),
  17485.         DATA_TYPE        = d.oledb_data_type,
  17486.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  17487.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  17488.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  17489.                         then coalesce(d.column_size,c.length)
  17490.                         else null 
  17491.                         end),
  17492.         CHARACTER_OCTET_LENGTH    = convert(int,
  17493.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  17494.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  17495.                         then coalesce(d.column_size,c.length)
  17496.                         else null 
  17497.                         end),
  17498.         NUMERIC_PRECISION    = convert(smallint,
  17499.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  17500.                             when d.fixed_prec_scale =1 then d.data_precision else null end),
  17501.         NUMERIC_SCALE        = convert(smallint, 
  17502.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  17503.         DESCRIPTION        = convert(varchar(1),null),
  17504.         TYPE_NAME        = d.type_name,
  17505.         LOCAL_TYPE_NAME        = d.local_type_name
  17506.     
  17507.     from
  17508.         syscolumns c,
  17509.         sysobjects o,
  17510.         master.dbo.spt_provider_types d,
  17511.         systypes t
  17512.     where
  17513.         o.type = 'P'                            /* Just Procedures */
  17514.     and    (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  17515.     and    o.id = c.id
  17516.     and    c.type = d.ss_dtype
  17517.     and    c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  17518.     and    c.usertype = t.usertype
  17519.     and    (t.usertype != 80 or d.type_name='timestamp')
  17520.     and    (@parameter_name is null or @parameter_name = c.name)
  17521.     UNION ALL
  17522.     SELECT           /* return value row*/
  17523.         PROCEDURE_CATALOG     = db_name(),
  17524.         PROCEDURE_SCHEMA     = user_name(),
  17525.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  17526.         PARAMETER_NAME         = convert(sysname,'RETURN_VALUE'),
  17527.         ORDINAL_POSITION     = convert(smallint,0),
  17528.         PARAMETER_TYPE         = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  17529.         PARAMETER_HASDEFAULT    = convert(tinyint,0),
  17530.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  17531.         IS_NULLABLE        = convert(bit,0),
  17532.         DATA_TYPE        = convert(smallint, 3 /*DBTYPE_I4*/),
  17533.         CHARACTER_MAXIMUM_LENGTH= convert(int,null),
  17534.         CHARACTER_OCTET_LENGTH    = convert(int,null),
  17535.         NUMERIC_PRECISION    = convert(smallint,10),
  17536.         NUMERIC_SCALE        = convert(smallint,null),
  17537.         DESCRIPTION        = convert(varchar(1),null),
  17538.         TYPE_NAME        = convert(sysname,'int'),
  17539.         LOCAL_TYPE_NAME        = convert(sysname,'int')
  17540.     from
  17541.         syscomments c,
  17542.         sysobjects o
  17543.     where
  17544.         o.type = 'P'                        /* Just Procedures */
  17545.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  17546.     and    (@parameter_name is null or @parameter_name = 'RETURN_VALUE')
  17547.     and    c.id = o.id
  17548.     and     c.colid = 1
  17549.     order by 2, 3, 5
  17550.     END
  17551.  
  17552.     exec @ret = sp_cursoropen @handle output, 'select * from #spprocparamrowset1',
  17553.         @scrollopt output, @ccopt output, @rows output
  17554.  
  17555.     drop table #spprocparamrowset1
  17556.        return isnull(@ret,0)
  17557. go
  17558. dump tran master with no_log
  17559. go
  17560. create procedure sp_procedure_params_rowset;3
  17561. as
  17562.     select
  17563.         PROCEDURE_CATALOG     = convert(sysname, ' '),
  17564.         PROCEDURE_SCHEMA     = convert(sysname, ' '),
  17565.         PROCEDURE_NAME         = convert(varchar(35), ' '),
  17566.         PARAMETER_NAME         = convert(sysname, ' '),
  17567.         ORDINAL_POSITION     = convert(smallint, 0),
  17568.         PARAMETER_TYPE         = convert(smallint, 0),
  17569.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  17570.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  17571.         IS_NULLABLE        = convert(bit, 0),
  17572.         DATA_TYPE        = convert(smallint, 0),
  17573.         CHARACTER_MAXIMUM_LENGTH= convert(int, 0),
  17574.         CHARACTER_OCTET_LENGTH    = convert(int, 0),
  17575.         NUMERIC_PRECISION    = convert(smallint, 0),
  17576.         NUMERIC_SCALE        = convert(smallint, 0),
  17577.         DESCRIPTION        = convert(varchar(1),null),
  17578.         TYPE_NAME        = convert(sysname,null),
  17579.         LOCAL_TYPE_NAME        = convert(sysname,null)
  17580.     where    1=0
  17581. go
  17582. dump tran master with no_log
  17583. go
  17584.  
  17585.  
  17586. if (charindex('7.00', @@version) > 0)
  17587.     drop procedure sp_procedure_params_rowset
  17588. else
  17589. begin
  17590.     print ''
  17591.     print ''
  17592.     print 'Warning:'
  17593.     print 'you are installing the stored procedures '
  17594.     print 'on a pre 7.0 SQL Server.'
  17595.     print 'Ignore the following errors.'
  17596. end
  17597. go
  17598.  
  17599. /*    Procedure for 7.0 servers */
  17600. create procedure sp_procedure_params_rowset
  17601.     (
  17602.        @procedure_name        sysname,
  17603.     @group_number        int = 1,
  17604.     @procedure_schema     sysname = null,
  17605.     @parameter_name        sysname = null
  17606.     )
  17607. as
  17608.     select
  17609.         PROCEDURE_CATALOG     = db_name(),
  17610.         PROCEDURE_SCHEMA     = user_name(),
  17611.         PROCEDURE_NAME         = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  17612.         PARAMETER_NAME         = c.name,
  17613.         ORDINAL_POSITION     = convert(smallint,c.colid),
  17614.         PARAMETER_TYPE         = convert(smallint, 1+c.isoutparam),
  17615.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  17616.         PARAMETER_DEFAULT    = convert(nvarchar(255),null),
  17617.         IS_NULLABLE        = convert(bit,ColumnProperty(c.id,c.name,'AllowsNull')),
  17618.         DATA_TYPE        = d.oledb_data_type,
  17619.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  17620.                         case 
  17621.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  17622.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  17623.                         then coalesce(d.column_size,c.length)
  17624.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  17625.                         then coalesce(d.column_size,c.length/2)
  17626.                         else null 
  17627.                         end),
  17628.         CHARACTER_OCTET_LENGTH    = convert(int,
  17629.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  17630.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  17631.                         then coalesce(d.column_size,c.length)
  17632.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  17633.                         then coalesce(d.column_size*2,c.length)
  17634.                         else null 
  17635.                         end),
  17636.         NUMERIC_PRECISION    = convert(smallint,
  17637.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  17638.                             when d.fixed_prec_scale =1 then d.data_precision else null end),
  17639.         NUMERIC_SCALE        = convert(smallint, 
  17640.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  17641.         DESCRIPTION        = convert(nvarchar(1),null),
  17642.         TYPE_NAME        = d.type_name,
  17643.         LOCAL_TYPE_NAME        = d.local_type_name
  17644.     
  17645.     from
  17646.         sysobjects o,
  17647.         syscolumns c,
  17648.         master.dbo.spt_provider_types d,
  17649.         systypes t
  17650.     where
  17651.         o.name = @procedure_name
  17652.     and    o.type = 'P'                            /* Just Procedures */
  17653.     and    (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  17654.     and    o.id = c.id
  17655.     and    c.number = @group_number
  17656.     and    c.xtype = d.ss_dtype
  17657.     and    c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  17658.     and    c.xusertype = t.xusertype
  17659.     and    (@parameter_name is null or @parameter_name = c.name)
  17660.     UNION ALL
  17661.     SELECT           /* return value row*/
  17662.         PROCEDURE_CATALOG     = db_name(),
  17663.         PROCEDURE_SCHEMA     = user_name(),
  17664.         PROCEDURE_NAME         = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  17665.         PARAMETER_NAME         = convert(sysname,'RETURN_VALUE'),
  17666.         ORDINAL_POSITION     = convert(smallint,0),
  17667.         PARAMETER_TYPE         = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  17668.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  17669.         PARAMETER_DEFAULT    = convert(nvarchar(255),null),
  17670.         IS_NULLABLE        = convert(bit,0),
  17671.         DATA_TYPE        = convert(smallint, 3 /*DBTYPE_I4*/),
  17672.         CHARACTER_MAXIMUM_LENGTH= convert(int,null),
  17673.         CHARACTER_OCTET_LENGTH    = convert(int,null),
  17674.         NUMERIC_PRECISION    = convert(smallint,10),
  17675.         NUMERIC_SCALE        = convert(smallint,null),
  17676.         DESCRIPTION        = convert(nvarchar(1),null),
  17677.         TYPE_NAME        = convert(sysname,N'int'),
  17678.         LOCAL_TYPE_NAME        = convert(sysname,N'int')
  17679.     from
  17680.         sysobjects o,
  17681.         syscomments c
  17682.     where
  17683.         o.name = @procedure_name
  17684.     and    o.id = c.id 
  17685.     and    c.number = @group_number
  17686.     and    c.colid = 1
  17687.     and    o.type = 'P'                        /* Just Procedures */
  17688.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  17689.     and    (@parameter_name is null or @parameter_name = 'RETURN_VALUE')
  17690.     order by 2, 3, 5
  17691. go
  17692. dump tran master with no_log
  17693. go
  17694. create procedure sp_procedure_params_rowset;2
  17695.     (
  17696.     @procedure_schema     sysname = null,
  17697.     @parameter_name        sysname = null
  17698.     )
  17699. as
  17700.     select
  17701.         PROCEDURE_CATALOG     = db_name(),
  17702.         PROCEDURE_SCHEMA     = user_name(),
  17703.         PROCEDURE_NAME         = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  17704.         PARAMETER_NAME         = c.name,
  17705.         ORDINAL_POSITION     = convert(smallint,c.colid),
  17706.         PARAMETER_TYPE         = convert(smallint, 1+c.isoutparam),
  17707.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  17708.         PARAMETER_DEFAULT    = convert(nvarchar(255),null),
  17709.         IS_NULLABLE        = convert(bit,ColumnProperty(c.id,c.name,'AllowsNull')),
  17710.         DATA_TYPE        = d.oledb_data_type,
  17711.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  17712.                         case 
  17713.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  17714.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  17715.                         then coalesce(d.column_size,c.length)
  17716.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  17717.                         then coalesce(d.column_size,c.length/2)
  17718.                         else null 
  17719.                         end),
  17720.         CHARACTER_OCTET_LENGTH    = convert(int,
  17721.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  17722.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  17723.                         then coalesce(d.column_size,c.length)
  17724.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  17725.                         then coalesce(d.column_size*2,c.length)
  17726.                         else null 
  17727.                         end),
  17728.         NUMERIC_PRECISION    = convert(smallint,
  17729.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  17730.                             when d.fixed_prec_scale =1 then d.data_precision else null end),
  17731.         NUMERIC_SCALE        = convert(smallint, 
  17732.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  17733.         DESCRIPTION        = convert(nvarchar(1),null),
  17734.         TYPE_NAME        = d.type_name,
  17735.         LOCAL_TYPE_NAME        = d.local_type_name
  17736.     
  17737.     from
  17738.         sysobjects o,
  17739.         syscolumns c,
  17740.         master.dbo.spt_provider_types d,
  17741.         systypes t
  17742.     where
  17743.         o.type = 'P'                            /* Just Procedures */
  17744.     and    (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  17745.     and    o.id = c.id
  17746.     and    c.xtype = d.ss_dtype
  17747.     and    c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  17748.     and    c.xusertype = t.xusertype
  17749.     and    (@parameter_name is null or @parameter_name = c.name)
  17750.     UNION ALL
  17751.     SELECT           /* return value row*/
  17752.         PROCEDURE_CATALOG     = db_name(),
  17753.         PROCEDURE_SCHEMA     = user_name(),
  17754.         PROCEDURE_NAME         = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  17755.         PARAMETER_NAME         = convert(sysname,'RETURN_VALUE'),
  17756.         ORDINAL_POSITION     = convert(smallint,0),
  17757.         PARAMETER_TYPE         = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  17758.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  17759.         PARAMETER_DEFAULT    = convert(nvarchar(255),null),
  17760.         IS_NULLABLE        = convert(bit,0),
  17761.         DATA_TYPE        = convert(smallint, 3 /*DBTYPE_I4*/),
  17762.         CHARACTER_MAXIMUM_LENGTH= convert(int,null),
  17763.         CHARACTER_OCTET_LENGTH    = convert(int,null),
  17764.         NUMERIC_PRECISION    = convert(smallint,10),
  17765.         NUMERIC_SCALE        = convert(smallint,null),
  17766.         DESCRIPTION        = convert(nvarchar(1),null),
  17767.         TYPE_NAME        = convert(sysname,N'int'),
  17768.         LOCAL_TYPE_NAME        = convert(sysname,N'int')
  17769.     from
  17770.         sysobjects o,
  17771.         syscomments c
  17772.     where
  17773.         o.type = 'P'        /* Just Procedures */
  17774.     and    o.id = c.id 
  17775.     and    c.colid = 1
  17776.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  17777.     and    (@parameter_name is null or @parameter_name = 'RETURN_VALUE')
  17778.     order by 2, 3, 5
  17779. go
  17780.  
  17781.  
  17782. grant execute on sp_procedure_params_rowset to public
  17783. go
  17784.  
  17785. dump tran master with no_log
  17786. go
  17787. if (charindex('6.00', @@version) > 0)
  17788.     begin
  17789.     if (exists (select * from sysobjects
  17790.             where name = 'sp_procedure_params_rowset' and type = 'P '))
  17791.         begin
  17792.         drop procedure sp_procedure_params_rowset
  17793.         dump tran master with no_log
  17794.         end
  17795.     end
  17796. go
  17797.  
  17798.  
  17799. print ''
  17800. print 'creating sp_procedures_rowset'
  17801. go
  17802.  
  17803.  
  17804. /* pre 7.00 version */
  17805. create procedure sp_procedures_rowset
  17806.     (
  17807.     @procedure_name        varchar(255), 
  17808.     @group_number        int = 1,
  17809.     @procedure_schema    varchar(255) = null
  17810.     )        
  17811. as
  17812.     select
  17813.         PROCEDURE_CATALOG    = db_name(),
  17814.         PROCEDURE_SCHEMA    = user_name(o.uid),
  17815.         PROCEDURE_NAME        = convert(varchar(35),o.name +';'+ ltrim(str(p.number,5))),
  17816.         PROCEDURE_TYPE        = convert(smallint, 3 /*DB_PT_FUNCTION*/),
  17817.         PROCEDURE_DEFINITION    = convert(varchar(1),null),
  17818.         DESCRIPTION        = convert(varchar(1),null),
  17819.         DATE_CREATED        = o.crdate,
  17820.         DATE_MODIFIED        = convert(datetime,null)
  17821.     from     
  17822.         sysobjects o, 
  17823.         syscomments p,
  17824.         sysusers u
  17825.     where
  17826.             o.name = @procedure_name
  17827.         and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  17828.     and     o.type = 'P'        /* Object type of Procedure */
  17829.     and     p.colid = 1
  17830.     and     p.id = o.id
  17831.     and    p.number = @group_number
  17832.     and     u.uid = user_id()    /* constrain sysusers uid for use in subquery */
  17833.     and     (    suser_id() = 1     /* User is the System Administrator */
  17834.         or     o.uid = user_id()    /* User created the object */
  17835.             /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  17836.         or     ((select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  17837.              from sysprotects p
  17838.              where p.id = o.id
  17839.                  /*  get rows for public,current user,user's group */
  17840.                  and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  17841.                  /* check for SELECT,EXECUTE privilege */
  17842.              and (action in (193,224)))&1    /* more magic...normalize GRANT */
  17843.             ) = 1     /* final magic...compare Grants    */
  17844.         )
  17845.     order by 2, 3
  17846. go
  17847. dump tran master with no_log
  17848. go
  17849. create procedure sp_procedures_rowset;2
  17850.     (
  17851.     @procedure_schema    varchar(255) = null
  17852.     )        
  17853. as
  17854.     select
  17855.         PROCEDURE_CATALOG    = db_name(),
  17856.         PROCEDURE_SCHEMA    = user_name(o.uid),
  17857.         PROCEDURE_NAME        = convert(varchar(35),o.name +';'+ ltrim(str(p.number,5))),
  17858.         PROCEDURE_TYPE        = convert(smallint, 3 /*DB_PT_FUNCTION*/),
  17859.         PROCEDURE_DEFINITION    = convert(varchar(1),null),
  17860.         DESCRIPTION        = convert(varchar(1),null),
  17861.         DATE_CREATED        = o.crdate,
  17862.         DATE_MODIFIED        = convert(datetime,null)
  17863.     from     
  17864.         sysobjects o, 
  17865.         syscomments p,
  17866.         sysusers u
  17867.     where
  17868.             (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  17869.     and     o.type = 'P'        /* Object type of Procedure */
  17870.     and     p.colid = 1
  17871.     and     p.id = o.id
  17872.     and     u.uid = user_id()    /* constrain sysusers uid for use in subquery */
  17873.     and     (    suser_id() = 1     /* User is the System Administrator */
  17874.         or     o.uid = user_id()    /* User created the object */
  17875.             /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  17876.         or     ((select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  17877.              from sysprotects p
  17878.              where p.id = o.id
  17879.                  /*  get rows for public,current user,user's group */
  17880.                  and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  17881.                  /* check for SELECT,EXECUTE privilege */
  17882.              and (action in (193,224)))&1    /* more magic...normalize GRANT */
  17883.             ) = 1     /* final magic...compare Grants    */
  17884.         )
  17885.     order by 2, 3
  17886. go
  17887. dump tran master with no_log
  17888. go
  17889.  
  17890. if (charindex('7.00', @@version) > 0)
  17891.     drop procedure sp_procedures_rowset
  17892. else
  17893. begin
  17894.     print ''
  17895.     print ''
  17896.     print 'Warning:'
  17897.     print 'you are installing the stored procedures '
  17898.     print 'on a pre 7.0 SQL Server.'
  17899.     print 'Ignore the following errors.'
  17900. end
  17901. go
  17902.  
  17903. /* 7.00 version */
  17904. create procedure sp_procedures_rowset
  17905.     (
  17906.     @procedure_name        sysname, 
  17907.     @group_number        int = 1,
  17908.     @procedure_schema    sysname = null
  17909.     )        
  17910. as
  17911.     select
  17912.         PROCEDURE_CATALOG    = db_name(),
  17913.         PROCEDURE_SCHEMA    = user_name(o.uid),
  17914.         PROCEDURE_NAME        = convert(nvarchar(134),o.name +';'+ ltrim(str(p.number,5))),
  17915.         PROCEDURE_TYPE        = convert(smallint, 3 /*DB_PT_FUNCTION*/),
  17916.         PROCEDURE_DEFINITION    = convert(nvarchar(1),null),
  17917.         DESCRIPTION        = convert(nvarchar(1),null),
  17918.         DATE_CREATED        = o.crdate,
  17919.         DATE_MODIFIED        = convert(datetime,null)
  17920.     from     
  17921.         sysobjects o, 
  17922.         syscomments p
  17923.     where
  17924.         permissions(o.id) <> 0
  17925.     and    o.name = @procedure_name
  17926.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  17927.     and     o.type = 'P'        /* Object type of Procedure */
  17928.     and     p.colid = 1
  17929.     and     p.id = o.id
  17930.     and    p.number = @group_number
  17931.     order by 2, 3
  17932. go
  17933. dump tran master with no_log
  17934. go
  17935. create procedure sp_procedures_rowset;2
  17936.     (
  17937.     @procedure_schema    sysname = null
  17938.     )        
  17939. as
  17940.     select
  17941.         PROCEDURE_CATALOG    = db_name(),
  17942.         PROCEDURE_SCHEMA    = user_name(o.uid),
  17943.         PROCEDURE_NAME        = convert(nvarchar(134),o.name +';'+ ltrim(str(p.number,5))),
  17944.         PROCEDURE_TYPE        = convert(smallint, 3 /*DB_PT_FUNCTION*/),
  17945.         PROCEDURE_DEFINITION    = convert(nvarchar(1),null),
  17946.         DESCRIPTION        = convert(nvarchar(1),null),
  17947.         DATE_CREATED        = o.crdate,
  17948.         DATE_MODIFIED        = convert(datetime,null)
  17949.     from     sysobjects o, syscomments p
  17950.     where
  17951.         permissions(o.id) <> 0
  17952.     and        (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  17953.     and     o.type = 'P'        /* Object type of Procedure */
  17954.     and p.colid = 1
  17955.     and p.id = o.id
  17956.     order by 2, 3
  17957. go
  17958.  
  17959. grant execute on sp_procedures_rowset to public
  17960. go
  17961.  
  17962. dump tran master with no_log
  17963. go
  17964. if (charindex('6.00', @@version) > 0)
  17965.     begin
  17966.     if (exists (select * from sysobjects
  17967.             where name = 'sp_procedures_rowset' and type = 'P '))
  17968.         begin
  17969.         drop procedure sp_procedures_rowset
  17970.         dump tran master with no_log
  17971.         end
  17972.     end
  17973. go
  17974.  
  17975.  
  17976. print ''
  17977. print 'creating sp_schemata_rowset'
  17978. go
  17979.  
  17980. /*    6.0 and 6.5 version */
  17981. create procedure sp_schemata_rowset
  17982.     (
  17983.     @schema_name    varchar(90) = null, 
  17984.     @schema_owner    varchar(90) = null
  17985.     )        
  17986. as
  17987.     select    distinct
  17988.         CATALOG_NAME            = db_name(),
  17989.         SCHEMA_NAME            = user_name(o.uid),    
  17990.         SCHEMA_OWNER            = user_name(o.uid),    
  17991.         DEFAULT_CHARACTER_SET_CATALOG    = convert(sysname,'master'),
  17992.         DEFAULT_CHARACTER_SET_SCHEMA    = convert(sysname,'dbo'),
  17993.         DEFAULT_CHARACTER_SET_NAME    = convert(sysname,a_cha.name)
  17994.     from    
  17995.         sysobjects o,
  17996.         master.dbo.sysconfigures    cfg,
  17997.         master.dbo.syscharsets        a_cha, /* charset/1001, not sortorder */
  17998.         master.dbo.syscharsets        b_cha  /* sortorder/2001, not charset */
  17999.     where   
  18000.         (@schema_name is null or @schema_name = user_name(o.uid))
  18001.     and     (@schema_owner is null or @schema_owner = user_name(o.uid))
  18002.     and    a_cha.type = 1001 /* type is charset */
  18003.     and     b_cha.type = 2001 /* type is sortorder */
  18004.     and     a_cha.id = b_cha.csid
  18005.     and     b_cha.id = cfg.value
  18006.     order by 2
  18007. go
  18008. dump tran master with no_log
  18009. go
  18010.  
  18011. if (charindex('7.00', @@version) > 0)
  18012.     drop procedure sp_schemata_rowset
  18013. else
  18014. begin
  18015.     print ''
  18016.     print ''
  18017.     print 'Warning:'
  18018.     print 'you are installing the stored procedures '
  18019.     print 'on a pre 7.0 SQL Server.'
  18020.     print 'Ignore the following errors.'
  18021. end
  18022. go
  18023.  
  18024. /* 7.00 version */
  18025. create procedure sp_schemata_rowset
  18026.     (
  18027.     @schema_name    sysname = null, 
  18028.     @schema_owner    sysname = null
  18029.     )        
  18030. as
  18031.     select    distinct
  18032.         CATALOG_NAME            = db_name(),
  18033.         SCHEMA_NAME            = user_name(o.uid),    
  18034.         SCHEMA_OWNER            = user_name(o.uid),    
  18035.         DEFAULT_CHARACTER_SET_CATALOG    = convert(sysname,N'master'),
  18036.         DEFAULT_CHARACTER_SET_SCHEMA    = convert(sysname,N'dbo'),
  18037.         DEFAULT_CHARACTER_SET_NAME    = convert(sysname,a_cha.name)
  18038.     from    
  18039.         sysobjects o,
  18040.         master.dbo.sysconfigures    cfg,
  18041.         master.dbo.syscharsets        a_cha, /* charset/1001, not sortorder */
  18042.         master.dbo.syscharsets        b_cha  /* sortorder/2001, not charset */
  18043.     where   
  18044.         (@schema_name is null or @schema_name = user_name(o.uid))
  18045.     and     (@schema_owner is null or @schema_owner = user_name(o.uid))
  18046.     and    a_cha.type = 1001 /* type is charset */
  18047.     and     b_cha.type = 2001 /* type is sortorder */
  18048.     and     a_cha.id = b_cha.csid
  18049.     and     b_cha.id = cfg.value
  18050.     order by 2
  18051. go
  18052. dump tran master with no_log
  18053. go
  18054. go
  18055. /* The following stored procedure is used for Sphinx and Hydra */
  18056. create procedure sp_schemata_rowset;3
  18057. as
  18058.     select
  18059.         CATALOG_NAME            = convert(sysname,' '),
  18060.         SCHEMA_NAME            = convert(sysname,' '),    
  18061.         SCHEMA_OWNER            = convert(sysname,' '),    
  18062.         DEFAULT_CHARACTER_SET_CATALOG    = convert(sysname,' '),
  18063.         DEFAULT_CHARACTER_SET_SCHEMA    = convert(sysname,' '),
  18064.         DEFAULT_CHARACTER_SET_NAME    = convert(sysname,' ')
  18065.     where    1=0
  18066. go
  18067. grant execute on sp_schemata_rowset to public
  18068. go
  18069.  
  18070. dump tran master with no_log
  18071. go
  18072. if (charindex('6.00', @@version) > 0)
  18073.     begin
  18074.     if (exists (select * from sysobjects
  18075.             where name = 'sp_schemata_rowset' and type = 'P '))
  18076.         begin
  18077.         drop procedure sp_schemata_rowset
  18078.         dump tran master with no_log
  18079.         end
  18080.     end
  18081. go
  18082.  
  18083.  
  18084. print ''
  18085. print 'creating sp_statistics_rowset'
  18086. go
  18087.  
  18088.  
  18089. /*    6.0 and 6.5 version */
  18090. create procedure sp_statistics_rowset
  18091.     (
  18092.     @table_name    varchar(255),
  18093.     @table_schema    varchar(255) = null     
  18094.     )
  18095. as
  18096.     select    db_name()                as TABLE_CATALOG,        
  18097.         user_name(o.uid)            as TABLE_SCHEMA,
  18098.         o.name                    as TABLE_NAME,
  18099.         x.rows                    as CARDINALITY
  18100.     from    sysobjects o, sysindexes x, sysusers u
  18101.     where    o.type in ('U')
  18102.     and     o.name = @table_name
  18103.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  18104.     and     x.id = o.id
  18105.     and     x.indid in (0,1)    /*If there are no indexes then table stats are in a row with indid =0 */
  18106.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  18107.     and     (
  18108.         suser_id() = 1     /* User is the System Administrator */
  18109.         or o.uid = user_id()     /* User created the object */
  18110.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  18111.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  18112.             from sysprotects p
  18113.             where p.id = o.id
  18114.             /* get rows for public,current user,user's group */
  18115.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  18116.             /* check for SELECT,EXECUTE privilege */
  18117.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  18118.             ) = 1    /* final magic...compare Grants      */
  18119.            )
  18120.     order by 2, 3
  18121. go
  18122. dump tran master with no_log
  18123. go
  18124. create procedure sp_statistics_rowset;2
  18125.     (
  18126.     @table_schema    varchar(255) = null     
  18127.     )
  18128. as
  18129.     select    db_name()                as TABLE_CATALOG,        
  18130.         user_name(o.uid)            as TABLE_SCHEMA,
  18131.         o.name                    as TABLE_NAME,
  18132.         x.rows                    as CARDINALITY
  18133.     from    sysobjects o, sysindexes x, sysusers u
  18134.     where    o.type in ('U')
  18135.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  18136.     and     x.id = o.id
  18137.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  18138.     and     (
  18139.         suser_id() = 1     /* User is the System Administrator */
  18140.         or o.uid = user_id()     /* User created the object */
  18141.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  18142.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  18143.             from sysprotects p
  18144.             where p.id = o.id
  18145.             /* get rows for public,current user,user's group */
  18146.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  18147.             /* check for SELECT,EXECUTE privilege */
  18148.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  18149.             ) = 1    /* final magic...compare Grants      */
  18150.            )        
  18151.     order by 2, 3
  18152. go
  18153. dump tran master with no_log
  18154. go
  18155.  
  18156. if (charindex('7.00', @@version) > 0)
  18157.     drop procedure sp_statistics_rowset
  18158. else
  18159. begin
  18160.     print ''
  18161.     print ''
  18162.     print 'Warning:'
  18163.     print 'you are installing the stored procedures '
  18164.     print 'on a pre 7.0 SQL Server.'
  18165.     print 'Ignore the following errors.'
  18166. end
  18167. go
  18168.  
  18169. /*     7.0 version */
  18170. create procedure sp_statistics_rowset
  18171.     (
  18172.     @table_name    sysname,
  18173.     @table_schema    sysname = null     
  18174.     )
  18175. as
  18176.     select    db_name()                as TABLE_CATALOG,        
  18177.         user_name(o.uid)            as TABLE_SCHEMA,
  18178.         o.name                    as TABLE_NAME,
  18179.         x.rows                    as CARDINALITY
  18180.     from    sysobjects o, sysindexes x
  18181.     where    o.type in ('U')
  18182.     and     o.name = @table_name
  18183.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  18184.     and     x.id = o.id
  18185.     and     x.indid in (0,1)    /*If there are no indexes then table stats are in a row with indid =0 */
  18186.     and     permissions(o.id) <> 0
  18187.     order by 2, 3
  18188. go
  18189. dump tran master with no_log
  18190. go
  18191. create procedure sp_statistics_rowset;2
  18192.     (
  18193.     @table_schema    sysname = null     
  18194.     )
  18195. as
  18196.     select    db_name()                as TABLE_CATALOG,        
  18197.         user_name(o.uid)            as TABLE_SCHEMA,
  18198.         o.name                    as TABLE_NAME,
  18199.         x.rows                    as CARDINALITY
  18200.     from    sysobjects o, sysindexes x
  18201.     where    o.type in ('U')
  18202.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  18203.     and     x.id = o.id
  18204.     and     x.indid in (0,1)    /*If there are no indexes then table stats are in a row with indid =0 */
  18205.     and     permissions(o.id) <> 0
  18206.     order by 2, 3
  18207. go
  18208.  
  18209. grant execute on sp_statistics_rowset to public
  18210. go
  18211.  
  18212. dump tran master with no_log
  18213. go
  18214. if (charindex('6.00', @@version) > 0)
  18215.     begin
  18216.     if (exists (select * from sysobjects
  18217.             where name = 'sp_statistics_rowset' and type = 'P '))
  18218.         begin
  18219.         drop procedure sp_statistics_rowset
  18220.         dump tran master with no_log
  18221.         end
  18222.     end
  18223. go
  18224.  
  18225.  
  18226. print ''
  18227. print 'creating sp_tables_rowset'
  18228. go
  18229.  
  18230.  
  18231. /*    Procedure for 6.50 and earlier servers */
  18232. create procedure sp_tables_rowset
  18233.     (
  18234.     @table_name    varchar(255), 
  18235.     @table_schema    varchar(255) = null,    
  18236.     @table_type    varchar(255) = null 
  18237.     )
  18238. as
  18239.     select    TABLE_CATALOG    = db_name(),
  18240.         TABLE_SCHEMA    = user_name(o.uid),
  18241.         TABLE_NAME    = o.name,
  18242.         TABLE_TYPE    = convert(varchar(30),
  18243.                     case o.type 
  18244.                     when 'U' then 'TABLE'
  18245.                     when 'V' then 'VIEW'
  18246.                     when 'S' then 'SYSTEM TABLE'
  18247.                     end),
  18248.         TABLE_GUID    = convert(binary(16), null),
  18249.         DESCRIPTION    = convert(varchar(1), null),
  18250.         TABLE_PROPID    = convert(int,null),
  18251.         DATE_CREATED    = o.crdate,
  18252.         DATE_MODIFIED    = convert(datetime,null)
  18253.     from    sysusers u, sysobjects o
  18254.     where    o.type in ('U','V','S')
  18255.     and     o.name = @table_name
  18256.     and     (    @table_schema is null
  18257.         or    @table_schema = user_name(o.uid)
  18258.         )
  18259.     and     (
  18260.             @table_type is null
  18261.         or    @table_type = case o.type 
  18262.                     when 'U' then 'TABLE'
  18263.                     when 'V' then 'VIEW'
  18264.                     when 'S' then 'SYSTEM TABLE'
  18265.                     end
  18266.         )
  18267.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  18268.     and     (
  18269.         suser_id() = 1     /* User is the System Administrator */
  18270.         or o.uid = user_id()     /* User created the object */
  18271.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  18272.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  18273.             from sysprotects p
  18274.             /* join to correlate with all rows in sysobjects */
  18275.             where p.id = o.id
  18276.             /* get rows for public,current user,user's group */
  18277.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  18278.             /* check for SELECT,EXECUTE privilege */
  18279.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  18280.             ) = 1    /* final magic...compare Grants      */
  18281.         )        
  18282.     order by 4, 2, 3
  18283. go
  18284. dump tran master with no_log
  18285. go
  18286. create procedure sp_tables_rowset;2
  18287.     (
  18288.     @table_schema    varchar(255) = null,    
  18289.     @table_type    varchar(255) = null 
  18290.     )
  18291. as
  18292.     select    TABLE_CATALOG    = db_name(),
  18293.         TABLE_SCHEMA    = user_name(o.uid),
  18294.         TABLE_NAME    = o.name,
  18295.         TABLE_TYPE    = convert(varchar(30),
  18296.                     case o.type 
  18297.                     when 'U' then 'TABLE'
  18298.                     when 'V' then 'VIEW'
  18299.                     when 'S' then 'SYSTEM TABLE'
  18300.                     end),
  18301.         TABLE_GUID    = convert(binary(16), null),
  18302.         DESCRIPTION    = convert(varchar(1), null),
  18303.         TABLE_PROPID    = convert(int,null),
  18304.         DATE_CREATED    = o.crdate,
  18305.         DATE_MODIFIED    = convert(datetime,null)
  18306.     from    sysusers u, sysobjects o
  18307.     where    o.type in ('U','V','S')
  18308.     and     (    @table_schema is null
  18309.         or    @table_schema = user_name(o.uid)
  18310.         )
  18311.     and     (
  18312.             @table_type is null
  18313.         or    @table_type = case o.type 
  18314.                     when 'U' then 'TABLE'
  18315.                     when 'V' then 'VIEW'
  18316.                     when 'S' then 'SYSTEM TABLE'
  18317.                     end
  18318.         )
  18319.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  18320.     and     (
  18321.         suser_id() = 1     /* User is the System Administrator */
  18322.         or o.uid = user_id()     /* User created the object */
  18323.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  18324.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  18325.             from sysprotects p
  18326.             /* join to correlate with all rows in sysobjects */
  18327.             where p.id = o.id
  18328.             /* get rows for public,current user,user's group */
  18329.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  18330.             /* check for SELECT,EXECUTE privilege */
  18331.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  18332.             ) = 1    /* final magic...compare Grants      */
  18333.         )        
  18334.     order by 4, 2, 3
  18335. go
  18336. dump tran master with no_log
  18337. go
  18338.  
  18339. if (charindex('7.00', @@version) > 0)
  18340.     drop procedure sp_tables_rowset
  18341. else
  18342. begin
  18343.     print ''
  18344.     print ''
  18345.     print 'Warning:'
  18346.     print 'you are installing the stored procedures '
  18347.     print 'on a pre 7.0 SQL Server.'
  18348.     print 'Ignore the following errors.'
  18349. end
  18350. go
  18351.  
  18352. /*    Procedure for 7.00 server */
  18353. create procedure sp_tables_rowset
  18354.     (
  18355.     @table_name    sysname, 
  18356.     @table_schema    sysname = null,    
  18357.     @table_type    nvarchar(255) = null 
  18358.     )
  18359. as
  18360.     select    *
  18361.     from    (select    TABLE_CATALOG    = db_name(),
  18362.             TABLE_SCHEMA    = user_name(uid),
  18363.             TABLE_NAME    = name,
  18364.             TABLE_TYPE    = convert(nvarchar(30),
  18365.                         case type 
  18366.                         when 'U' then 
  18367.                             case when ObjectProperty(id, 'IsMSShipped') = 0
  18368.                             then N'TABLE' else N'SYSTEM TABLE' end
  18369.                         when 'S' then N'SYSTEM TABLE'
  18370.                         when 'V' then 
  18371.                             case when ObjectProperty(id, 'IsMSShipped') = 0
  18372.                             then N'VIEW' else N'SYSTEM VIEW' end
  18373.                         end),
  18374.             TABLE_GUID    = convert(uniqueidentifier, null),
  18375.             DESCRIPTION    = convert(nvarchar(1), null),
  18376.             TABLE_PROPID    = convert(int,null),
  18377.             DATE_CREATED    = crdate,
  18378.             DATE_MODIFIED    = convert(datetime,null)
  18379.         from    sysobjects
  18380.         where    name = @table_name
  18381.         and    type in ('U','V','S')
  18382.         and    permissions(id) <> 0
  18383.         ) as o
  18384.  
  18385.     where     (@table_schema is null or @table_schema = TABLE_SCHEMA)
  18386.     and     (@table_type is null or @table_type = TABLE_TYPE)    
  18387.     order by 4, 2, 3
  18388. go
  18389. dump tran master with no_log
  18390. go
  18391. create procedure sp_tables_rowset;2
  18392.     (
  18393.     @table_schema    sysname = null,    
  18394.     @table_type    nvarchar(255) = null 
  18395.     )
  18396. as
  18397.     select    *
  18398.     from    (select    TABLE_CATALOG    = db_name(),
  18399.             TABLE_SCHEMA    = user_name(uid),
  18400.             TABLE_NAME    = name,
  18401.             TABLE_TYPE    = convert(nvarchar(30),
  18402.                         case type 
  18403.                         when 'U' then 
  18404.                             case when ObjectProperty(id, 'IsMSShipped') = 0
  18405.                             then N'TABLE' else N'SYSTEM TABLE' end
  18406.                         when 'S' then N'SYSTEM TABLE'
  18407.                         when 'V' then 
  18408.                             case when ObjectProperty(id, 'IsMSShipped') = 0
  18409.                             then N'VIEW' else N'SYSTEM VIEW' end
  18410.                         end),
  18411.             TABLE_GUID    = convert(uniqueidentifier, null),
  18412.             DESCRIPTION    = convert(nvarchar(1), null),
  18413.             TABLE_PROPID    = convert(int,null),
  18414.             DATE_CREATED    = crdate,
  18415.             DATE_MODIFIED    = convert(datetime,null)
  18416.         from    sysobjects
  18417.         where    type in ('U','V','S')
  18418.         and    permissions(id) <> 0
  18419.         ) as o
  18420.  
  18421.     where     (@table_schema is null or @table_schema = TABLE_SCHEMA)
  18422.     and     (@table_type is null or @table_type = TABLE_TYPE)    
  18423.     order by 4, 2, 3
  18424. go
  18425. dump tran master with no_log
  18426. go
  18427. create procedure sp_tables_rowset;5
  18428.     (
  18429.     @table_server        sysname,
  18430.     @table_catalog        sysname = null,
  18431.     @table_name        sysname = null,
  18432.     @table_schema        sysname = null,
  18433.     @table_type        sysname = null
  18434.     )
  18435. as
  18436.     select
  18437.         TABLE_CATALOG,
  18438.         TABLE_SCHEMA,
  18439.         TABLE_NAME,
  18440.         TABLE_TYPE,
  18441.         TABLE_GUID,
  18442.         DESCRIPTION
  18443.     --    TABLE_PROPID,
  18444.     --    DATE_CREATED,
  18445.     --    DATE_MODIFIED
  18446.     from master.dbo.SYSREMOTE_TABLES <
  18447.                 @table_server,
  18448.                 @table_catalog,
  18449.                 @table_schema,
  18450.                 @table_name,
  18451.                 @table_type >
  18452.     order by 4,1,2,3
  18453. go
  18454.  
  18455. grant execute on sp_tables_rowset to public
  18456. go
  18457.  
  18458. dump tran master with no_log
  18459. go
  18460. if (charindex('6.00', @@version) > 0)
  18461.     begin
  18462.     if (exists (select * from sysobjects
  18463.             where name = 'sp_tables_rowset' and type = 'P '))
  18464.         begin
  18465.         drop procedure sp_tables_rowset
  18466.         dump tran master with no_log
  18467.         end
  18468.     end
  18469. go
  18470.  
  18471.  
  18472.  
  18473. print ''
  18474. print 'creating sp_tables_info_rowset'
  18475. go
  18476.  
  18477.  
  18478. /*    Procedure for 6.50 and earlier servers */
  18479. create procedure sp_tables_info_rowset
  18480.     (
  18481.     @table_name    varchar(255), 
  18482.     @table_schema    varchar(255) = null,    
  18483.     @table_type    varchar(255) = null 
  18484.     )
  18485. as
  18486.     select    TABLE_CATALOG        = db_name(),
  18487.         TABLE_SCHEMA        = user_name(o.uid),
  18488.         TABLE_NAME        = o.name,
  18489.         TABLE_TYPE        = convert(varchar(30),
  18490.                         case o.type 
  18491.                         when 'U' then 'TABLE'
  18492.                         when 'V' then 'VIEW'
  18493.                         when 'S' then 'SYSTEM TABLE'
  18494.                         end),
  18495.         TABLE_GUID        = convert(binary(16), null),
  18496.         BOOKMARKS        = convert(bit, 1),
  18497.         BOOKMARK_TYPE        = convert(int, 1 /*DBPROPVAL_BMK_NUMERIC*/),
  18498.         BOOKMARK_DATATYPE    = convert(smallint, 19 /*DBTYPE_UI4 */),
  18499.         BOOKMARK_MAXIMUM_LENGTH = convert(int, 4),
  18500.         BOOKMARK_INFORMATION    = convert(int, 0),
  18501.         TABLE_VERSION        = convert(int, o.schema_ver),
  18502.         CARDINALITY        = x.rows,
  18503.         DESCRIPTION        = convert(varchar(1), null),
  18504.         TABLE_PROPID        = convert(int, null)
  18505.  
  18506.     from    sysusers u, 
  18507.         sysobjects o,
  18508.         sysindexes x
  18509.  
  18510.     where    o.type in ('U','V','S')
  18511.     and     o.name = @table_name
  18512.     and     (    @table_schema is null
  18513.         or    @table_schema = user_name(o.uid)
  18514.         )
  18515.     and     (
  18516.             @table_type is null
  18517.         or    @table_type = case o.type 
  18518.                     when 'U' then 'TABLE'
  18519.                     when 'V' then 'VIEW'
  18520.                     when 'S' then 'SYSTEM TABLE'
  18521.                     end
  18522.         )
  18523.     and    o.id *= x.id
  18524.     and    x.indid in (0,1)
  18525.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  18526.     and     (
  18527.         suser_id() = 1     /* User is the System Administrator */
  18528.         or o.uid = user_id()     /* User created the object */
  18529.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  18530.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  18531.             from sysprotects p
  18532.             /* join to correlate with all rows in sysobjects */
  18533.             where p.id = o.id
  18534.             /* get rows for public,current user,user's group */
  18535.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  18536.             /* check for SELECT,EXECUTE privilege */
  18537.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  18538.             ) = 1    /* final magic...compare Grants      */
  18539.         )        
  18540.     order by 4, 2, 3
  18541. go
  18542. dump tran master with no_log
  18543. go
  18544. create procedure sp_tables_info_rowset;2
  18545.     (
  18546.     @table_schema    varchar(255) = null,    
  18547.     @table_type    varchar(255) = null 
  18548.     )
  18549. as
  18550.     select    TABLE_CATALOG        = db_name(),
  18551.         TABLE_SCHEMA        = user_name(o.uid),
  18552.         TABLE_NAME        = o.name,
  18553.         TABLE_TYPE        = convert(varchar(30),
  18554.                         case o.type 
  18555.                         when 'U' then 'TABLE'
  18556.                         when 'V' then 'VIEW'
  18557.                         when 'S' then 'SYSTEM TABLE'
  18558.                         end),
  18559.         TABLE_GUID        = convert(binary(16), null),
  18560.         BOOKMARKS        = convert(bit, 1),
  18561.         BOOKMARK_TYPE        = convert(int, 1 /*DBPROPVAL_BMK_NUMERIC*/),
  18562.         BOOKMARK_DATATYPE    = convert(smallint, 19 /*DBTYPE_UI4 */),
  18563.         BOOKMARK_MAXIMUM_LENGTH = convert(int, 4),
  18564.         BOOKMARK_INFORMATION    = convert(int, 0),
  18565.         TABLE_VERSION        = convert(int, o.schema_ver),
  18566.         CARDINALITY        = x.rows,
  18567.         DESCRIPTION        = convert(varchar(1), null),
  18568.         TABLE_PROPID        = convert(int, null)
  18569.  
  18570.     from    sysusers u, 
  18571.         sysobjects o,
  18572.         sysindexes x
  18573.  
  18574.     where    o.type in ('U','V','S')
  18575.     and     (    @table_schema is null
  18576.         or    @table_schema = user_name(o.uid)
  18577.         )
  18578.     and     (
  18579.             @table_type is null
  18580.         or    @table_type = case o.type 
  18581.                     when 'U' then 'TABLE'
  18582.                     when 'V' then 'VIEW'
  18583.                     when 'S' then 'SYSTEM TABLE'
  18584.                     end
  18585.         )
  18586.     and    o.id *= x.id
  18587.     and    x.indid in (0,1)
  18588.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  18589.     and     (
  18590.         suser_id() = 1     /* User is the System Administrator */
  18591.         or o.uid = user_id()     /* User created the object */
  18592.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  18593.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  18594.             from sysprotects p
  18595.             /* join to correlate with all rows in sysobjects */
  18596.             where p.id = o.id
  18597.             /* get rows for public,current user,user's group */
  18598.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  18599.             /* check for SELECT,EXECUTE privilege */
  18600.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  18601.             ) = 1    /* final magic...compare Grants      */
  18602.         )        
  18603.     order by 4, 2, 3
  18604. go
  18605. dump tran master with no_log
  18606. go
  18607.  
  18608. if (charindex('7.00', @@version) > 0)
  18609.     drop procedure sp_tables_info_rowset
  18610. else
  18611. begin
  18612.     print ''
  18613.     print ''
  18614.     print 'Warning:'
  18615.     print 'you are installing the stored procedures '
  18616.     print 'on a pre 7.0 SQL Server.'
  18617.     print 'Ignore the following errors.'
  18618. end
  18619. go
  18620.  
  18621. /*    Procedure for 7.00 server */
  18622. create procedure sp_tables_info_rowset
  18623.     (
  18624.     @table_name    sysname, 
  18625.     @table_schema    sysname = null,    
  18626.     @table_type    nvarchar(255) = null 
  18627.     )
  18628. as
  18629.     select    *
  18630.     from    (select    TABLE_CATALOG        = db_name(),
  18631.             TABLE_SCHEMA        = user_name(o.uid),
  18632.             TABLE_NAME        = o.name,
  18633.             TABLE_TYPE        = convert(nvarchar(30),
  18634.                             case o.type 
  18635.                             when 'U' then 
  18636.                                 case when ObjectProperty(o.id, 'IsMSShipped') = 0
  18637.                                 then N'TABLE' else N'SYSTEM TABLE' end
  18638.                             when 'S' then N'SYSTEM TABLE'
  18639.                             when 'V' then 
  18640.                                 case when ObjectProperty(o.id, 'IsMSShipped') = 0
  18641.                                 then N'VIEW' else N'SYSTEM VIEW' end
  18642.                             end),
  18643.             TABLE_GUID        = convert(uniqueidentifier, null),
  18644.             BOOKMARKS        = convert(bit, 1),
  18645.             BOOKMARK_TYPE        = convert(int, 1 /*DBPROPVAL_BMK_NUMERIC*/),
  18646.             BOOKMARK_DATATYPE    = convert(smallint, 19 /*DBTYPE_UI4 */),
  18647.             BOOKMARK_MAXIMUM_LENGTH = convert(int, 4),
  18648.             BOOKMARK_INFORMATION    = convert(int, 0),
  18649.             TABLE_VERSION        = convert(int, o.schema_ver),
  18650.             CARDINALITY        = x.rows,
  18651.             DESCRIPTION        = convert(nvarchar(1), null),
  18652.             TABLE_PROPID        = convert(int, null) 
  18653.     
  18654.         from    sysobjects o left join sysindexes x on o.id = x.id and x.indid in (0,1)
  18655.         where    o.name = @table_name
  18656.         and     o.type in ('U','V','S')
  18657.         and    permissions(o.id) <> 0) as t
  18658.  
  18659.     where     (@table_schema is null or @table_schema = TABLE_SCHEMA)
  18660.     and     (@table_type is null or    @table_type = TABLE_TYPE)
  18661.     order by 4, 2, 3
  18662. go
  18663. dump tran master with no_log
  18664. go
  18665. create procedure sp_tables_info_rowset;2
  18666.     (
  18667.     @table_schema    sysname = null,    
  18668.     @table_type    nvarchar(255) = null 
  18669.     )
  18670. as
  18671.     select    *
  18672.     from    (select    TABLE_CATALOG        = db_name(),
  18673.             TABLE_SCHEMA        = user_name(o.uid),
  18674.             TABLE_NAME        = o.name,
  18675.             TABLE_TYPE        = convert(nvarchar(30),
  18676.                             case o.type 
  18677.                             when 'U' then 
  18678.                                 case when ObjectProperty(o.id, 'IsMSShipped') = 0
  18679.                                 then N'TABLE' else N'SYSTEM TABLE' end
  18680.                             when 'S' then N'SYSTEM TABLE'
  18681.                             when 'V' then 
  18682.                                 case when ObjectProperty(o.id, 'IsMSShipped') = 0
  18683.                                 then N'VIEW' else N'SYSTEM VIEW' end
  18684.                             end),
  18685.             TABLE_GUID        = convert(uniqueidentifier, null),
  18686.             BOOKMARKS        = convert(bit, 1),
  18687.             BOOKMARK_TYPE        = convert(int, 1 /*DBPROPVAL_BMK_NUMERIC*/),
  18688.             BOOKMARK_DATATYPE    = convert(smallint, 19 /*DBTYPE_UI4 */),
  18689.             BOOKMARK_MAXIMUM_LENGTH = convert(int, 4),
  18690.             BOOKMARK_INFORMATION    = convert(int, 0),
  18691.             TABLE_VERSION        = convert(int, o.schema_ver),
  18692.             CARDINALITY        = x.rows,
  18693.             DESCRIPTION        = convert(nvarchar(1), null),
  18694.             TABLE_PROPID        = convert(int, null) 
  18695.     
  18696.         from    sysobjects o left join sysindexes x on o.id = x.id and x.indid in (0,1)
  18697.         where     o.type in ('U','V','S')
  18698.         and    permissions(o.id) <> 0) as t
  18699.  
  18700.     where     (@table_schema is null or @table_schema = TABLE_SCHEMA)
  18701.     and     (@table_type is null or    @table_type = TABLE_TYPE)
  18702.     order by 4, 2, 3
  18703. go
  18704.  
  18705. grant execute on sp_tables_info_rowset to public
  18706. go
  18707.  
  18708. dump tran master with no_log
  18709. go
  18710. if (charindex('6.00', @@version) > 0)
  18711.     begin
  18712.     if (exists (select * from sysobjects
  18713.         where name = 'sp_tables_info_rowset' and type = 'P '))
  18714.         begin
  18715.         drop procedure sp_tables_info_rowset
  18716.         dump tran master with no_log
  18717.         end
  18718.     end
  18719. go
  18720.  
  18721. print ''
  18722. print 'creating sp_table_constraints_rowset'
  18723. go
  18724.  
  18725.  
  18726. /*    Procedure for 6.50 and earlier servers */
  18727. create procedure sp_table_constraints_rowset
  18728.     (
  18729.     @table_name            varchar(255), 
  18730.     @table_schema        varchar(255) = null,
  18731.     @table_catalog        varchar(255) = null,
  18732.     @constraint_name    varchar(255) = null,
  18733.     @constraint_schema    varchar(255) = null,
  18734.     @constraint_catalog    varchar(255) = null,
  18735.     @constraint_type    varchar(255) = null 
  18736.     )
  18737. as
  18738.     select
  18739.         CONSTRAINT_CATALOG    = db_name(),
  18740.         CONSTRAINT_SCHEMA    = user_name(c_obj.uid),
  18741.         CONSTRAINT_NAME        = c_obj.name,
  18742.         TABLE_CATALOG        = db_name(),
  18743.         TABLE_SCHEMA        = user_name(t_obj.uid),
  18744.         TABLE_NAME            = t_obj.name,
  18745.         CONSTRAINT_TYPE        = case (c.status & 0xf)
  18746.                                 when 1 then 'PRIMARY KEY'
  18747.                                 when 2 then    'UNIQUE'
  18748.                                 when 3 then    'FOREIGN KEY'
  18749.                                 when 4 then    'CHECK'
  18750.                                  end, 
  18751.         IS_DEFERRABLE        = convert(bit, 0),
  18752.         INITIALLY_DEFERRED    = convert(bit, 0),
  18753.         DESCRIPTION            = convert(varchar(1), null)
  18754.  
  18755.     from 
  18756.         sysobjects c_obj, sysobjects t_obj, sysconstraints c
  18757.     where
  18758.             t_obj.name    = @table_name
  18759.         and t_obj.type in ('U','S')
  18760.         and (@table_catalog is null or @table_catalog = db_name())
  18761.         and    (@table_schema is null or @table_schema = user_name(t_obj.uid))
  18762.         and c.id = t_obj.id
  18763.         and (c.status & 0xf) between 1 and 4
  18764.         and c_obj.id    = c.constid
  18765.         and    c_obj.uid    = user_id()
  18766.         and (@constraint_name is null or c_obj.name    = @constraint_name)
  18767.         and (@constraint_catalog is null or @constraint_catalog = db_name())
  18768.         and    (@constraint_schema is null or @constraint_schema = user_name(c_obj.uid))
  18769.         and (@constraint_type is null
  18770.             or (c.status & 0xf)
  18771.                 = case @constraint_type
  18772.                     when 'PRIMARY KEY' then 1
  18773.                     when 'UNIQUE' then 2
  18774.                     when 'FOREIGN KEY' then 3
  18775.                     when 'CHECK' then 4
  18776.                     end)
  18777. order by 2,3,5,6,7 
  18778. go
  18779. dump tran master with no_log
  18780. go
  18781. create procedure sp_table_constraints_rowset;2
  18782.     (
  18783.     @table_schema        varchar(255) = null,
  18784.     @table_catalog        varchar(255) = null,
  18785.     @constraint_name    varchar(255) = null,
  18786.     @constraint_schema    varchar(255) = null,
  18787.     @constraint_catalog    varchar(255) = null,
  18788.     @constraint_type    varchar(255) = null 
  18789.     )
  18790. as
  18791.     select
  18792.         CONSTRAINT_CATALOG    = db_name(),
  18793.         CONSTRAINT_SCHEMA    = user_name(c_obj.uid),
  18794.         CONSTRAINT_NAME        = c_obj.name,
  18795.         TABLE_CATALOG        = db_name(),
  18796.         TABLE_SCHEMA        = user_name(t_obj.uid),
  18797.         TABLE_NAME            = t_obj.name,
  18798.         CONSTRAINT_TYPE        = case (c.status & 0xf)
  18799.                                 when 1 then 'PRIMARY KEY'
  18800.                                 when 2 then    'UNIQUE'
  18801.                                 when 3 then    'FOREIGN KEY'
  18802.                                 when 4 then    'CHECK'
  18803.                                  end, 
  18804.         IS_DEFERRABLE        = convert(bit, 0),
  18805.         INITIALLY_DEFERRED    = convert(bit, 0),
  18806.         DESCRIPTION            = convert(varchar(1), null)
  18807.  
  18808.     from 
  18809.         sysobjects c_obj, sysobjects t_obj, sysconstraints c
  18810.     where
  18811.             t_obj.type in ('U','S')
  18812.         and (@table_catalog is null or @table_catalog = db_name())
  18813.         and    (@table_schema is null or @table_schema = user_name(t_obj.uid))
  18814.         and c.id = t_obj.id
  18815.         and (c.status & 0xf) between 1 and 4
  18816.         and c_obj.id    = c.constid
  18817.         and    c_obj.uid    = user_id()
  18818.         and (@constraint_name is null or c_obj.name    = @constraint_name)
  18819.         and (@constraint_catalog is null or @constraint_catalog = db_name())
  18820.         and    (@constraint_schema is null or @constraint_schema = user_name(c_obj.uid))
  18821.         and (@constraint_type is null
  18822.             or (c.status & 0xf)
  18823.                 = case @constraint_type
  18824.                     when 'PRIMARY KEY' then 1
  18825.                     when 'UNIQUE' then 2
  18826.                     when 'FOREIGN KEY' then 3
  18827.                     when 'CHECK' then 4
  18828.                     end)
  18829. order by 2,3,5,6,7 
  18830. go
  18831. dump tran master with no_log
  18832. go
  18833.  
  18834.  
  18835. if (charindex('7.00', @@version) > 0)
  18836.     drop procedure sp_table_constraints_rowset
  18837. else
  18838. begin
  18839.     print ''
  18840.     print ''
  18841.     print 'Warning:'
  18842.     print 'you are installing the stored procedures '
  18843.     print 'on a pre 7.0 SQL Server.'
  18844.     print 'Ignore the following errors.'
  18845. end
  18846. go
  18847.  
  18848. /*    Procedure for 7.00 server */
  18849. create procedure sp_table_constraints_rowset
  18850.     (
  18851.     @table_name            sysname, 
  18852.     @table_schema        sysname = null,
  18853.     @table_catalog        sysname = null,
  18854.     @constraint_name    sysname = null,
  18855.     @constraint_schema    sysname = null,
  18856.     @constraint_catalog    sysname = null,
  18857.     @constraint_type    nvarchar(255) = null 
  18858.     )
  18859. as
  18860.     select
  18861.         CONSTRAINT_CATALOG    = db_name(),
  18862.         CONSTRAINT_SCHEMA    = user_name(c_obj.uid),
  18863.         CONSTRAINT_NAME        = c_obj.name,
  18864.         TABLE_CATALOG        = db_name(),
  18865.         TABLE_SCHEMA        = user_name(t_obj.uid),
  18866.         TABLE_NAME            = t_obj.name,
  18867.         CONSTRAINT_TYPE        = case (c.status & 0xf)
  18868.                                 when 1 then N'PRIMARY KEY'
  18869.                                 when 2 then    N'UNIQUE'
  18870.                                 when 3 then    N'FOREIGN KEY'
  18871.                                 when 4 then    N'CHECK'
  18872.                                  end, 
  18873.         IS_DEFERRABLE        = convert(bit, 0),
  18874.         INITIALLY_DEFERRED    = convert(bit, 0),
  18875.         DESCRIPTION            = convert(nvarchar(1), null)
  18876.  
  18877.     from 
  18878.         sysobjects c_obj, sysobjects t_obj, sysconstraints c
  18879.     where
  18880.             t_obj.name    = @table_name
  18881.         and t_obj.type in ('U','S')
  18882.         and (@table_catalog is null or @table_catalog = db_name())
  18883.         and    (@table_schema is null or @table_schema = user_name(t_obj.uid))
  18884.         and c.id = t_obj.id
  18885.         and (c.status & 0xf) between 1 and 4
  18886.         and c_obj.id    = c.constid
  18887.         and    c_obj.uid    = user_id()
  18888.         and (@constraint_name is null or c_obj.name    = @constraint_name)
  18889.         and (@constraint_catalog is null or @constraint_catalog = db_name())
  18890.         and    (@constraint_schema is null or @constraint_schema = user_name(c_obj.uid))
  18891.         and (@constraint_type is null
  18892.             or (c.status & 0xf)
  18893.                 = case @constraint_type
  18894.                     when N'PRIMARY KEY' then 1
  18895.                     when N'UNIQUE' then 2
  18896.                     when N'FOREIGN KEY' then 3
  18897.                     when N'CHECK' then 4
  18898.                     end)
  18899. order by 2,3,5,6,7 
  18900. go
  18901. dump tran master with no_log
  18902. go
  18903. create procedure sp_table_constraints_rowset;2
  18904.     (
  18905.     @table_schema        sysname = null,
  18906.     @table_catalog        sysname = null,
  18907.     @constraint_name    sysname = null,
  18908.     @constraint_schema    sysname = null,
  18909.     @constraint_catalog    sysname = null,
  18910.     @constraint_type    nvarchar(255) = null 
  18911.     )
  18912. as
  18913.     select
  18914.         CONSTRAINT_CATALOG    = db_name(),
  18915.         CONSTRAINT_SCHEMA    = user_name(c_obj.uid),
  18916.         CONSTRAINT_NAME        = c_obj.name,
  18917.         TABLE_CATALOG        = db_name(),
  18918.         TABLE_SCHEMA        = user_name(t_obj.uid),
  18919.         TABLE_NAME            = t_obj.name,
  18920.         CONSTRAINT_TYPE        = case (c.status & 0xf)
  18921.                                 when 1 then N'PRIMARY KEY'
  18922.                                 when 2 then    N'UNIQUE'
  18923.                                 when 3 then    N'FOREIGN KEY'
  18924.                                 when 4 then    N'CHECK'
  18925.                                  end, 
  18926.         IS_DEFERRABLE        = convert(bit, 0),
  18927.         INITIALLY_DEFERRED    = convert(bit, 0),
  18928.         DESCRIPTION            = convert(nvarchar(1), null)
  18929.  
  18930.     from 
  18931.         sysobjects c_obj, sysobjects t_obj, sysconstraints c
  18932.     where
  18933.             t_obj.type in ('U','S')
  18934.         and (@table_catalog is null or @table_catalog = db_name())
  18935.         and    (@table_schema is null or @table_schema = user_name(t_obj.uid))
  18936.         and c.id = t_obj.id
  18937.         and (c.status & 0xf) between 1 and 4
  18938.         and c_obj.id    = c.constid
  18939.         and    c_obj.uid    = user_id()
  18940.         and (@constraint_name is null or c_obj.name    = @constraint_name)
  18941.         and (@constraint_catalog is null or @constraint_catalog = db_name())
  18942.         and    (@constraint_schema is null or @constraint_schema = user_name(c_obj.uid))
  18943.         and (@constraint_type is null
  18944.             or (c.status & 0xf)
  18945.                 = case @constraint_type
  18946.                     when N'PRIMARY KEY' then 1
  18947.                     when N'UNIQUE' then 2
  18948.                     when N'FOREIGN KEY' then 3
  18949.                     when N'CHECK' then 4
  18950.                     end)
  18951. order by 2,3,5,6,7 
  18952. go
  18953. dump tran master with no_log
  18954.  
  18955.  
  18956. grant execute on sp_table_constraints_rowset to public
  18957. go
  18958.  
  18959. dump tran master with no_log
  18960. go
  18961. if (charindex('6.00', @@version) > 0)
  18962.     begin
  18963.     if (exists (select * from sysobjects
  18964.             where name = 'sp_table_constraints_rowset' and type = 'P '))
  18965.         begin
  18966.         drop procedure sp_table_constraints_rowset
  18967.         dump tran master with no_log
  18968.         end
  18969.     end
  18970. go
  18971.  
  18972. print ''
  18973. print 'creating sp_table_privileges_rowset'
  18974. go
  18975.  
  18976. /*    Procedure for 6.0 and 6.5 server */
  18977. CREATE PROCEDURE sp_table_privileges_rowset
  18978.     (
  18979.     @table_name    varchar(255) = null,
  18980.     @table_schema    varchar(255) = null,
  18981.     @grantor    varchar(255) = null,
  18982.     @grantee    varchar(255) = null
  18983.     )
  18984. as
  18985. IF @table_name is not null
  18986.     BEGIN
  18987.     select
  18988.         GRANTOR        = user_name(p.grantor),
  18989.         GRANTEE        = user_name(u.uid),
  18990.         TABLE_CATALOG    = db_name(),
  18991.         TABLE_SCHEMA    = user_name(o.uid),
  18992.         TABLE_NAME    = o.name,
  18993.         PRIVILEGE_TYPE    = convert(varchar(30),
  18994.                     case p.action
  18995.                     when 193 then 'SELECT'
  18996.                     when 195 then 'INSERT'
  18997.                     when 196 then 'DELETE'
  18998.                     when 197 then 'UPDATE'
  18999.                     else 'REFERENCES'
  19000.                     end),
  19001.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  19002.     from 
  19003.         sysprotects p, sysobjects o, sysusers u
  19004.     where
  19005.         o.name = @table_name
  19006.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  19007.     and    (@grantee is null or @grantee = user_name(u.uid))
  19008.     and     o.type in ('U','V','S')
  19009.     and    p.id = o.id
  19010.     and     (@grantor is null or @grantor = user_name(p.grantor))
  19011.             /* expand groups */
  19012.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  19013.     and     p.protecttype <> 206    /* only grant rows */
  19014.     and     p.action in (26,193,195,196,197)
  19015.     and     o.uid <> u.uid            /* no rows for owner */
  19016.     and     not exists (            /* exclude revoke'd privileges */
  19017.             select     *
  19018.             from     sysprotects p1
  19019.             where    p1.protecttype = 206
  19020.             and     p1.action = p.action
  19021.             and     p1.id = p.id
  19022.             and     p1.uid = u.uid)
  19023.     union all
  19024.     select    /*    Add rows for table owner */
  19025.         GRANTOR        = user_name(u.uid),
  19026.         GRANTEE        = user_name(o.uid),
  19027.         TABLE_CATALOG    = db_name(),
  19028.         TABLE_SCHEMA    = user_name(o.uid),
  19029.         TABLE_NAME    = o.name,
  19030.         PRIVILEGE_TYPE    = convert(varchar(30),
  19031.                     case v.number
  19032.                     when 193 then 'SELECT'
  19033.                     when 195 then 'INSERT'
  19034.                     when 196 then 'DELETE'
  19035.                     when 197 then 'UPDATE'
  19036.                     else 'REFERENCES'
  19037.                     end),
  19038.         IS_GRANTABLE    = convert(bit,1)    
  19039.     from 
  19040.         sysobjects o, master.dbo.spt_values v, sysusers u
  19041.     where
  19042.         o.name = @table_name
  19043.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  19044.     and    (@grantee is null or @grantee = user_name(u.uid))
  19045.     and     o.type in ('U','V','S')
  19046.     and     u.suid = 1        /* grantor is dbo of database */
  19047.     and    (@grantor is null or @grantor = user_name(u.uid))
  19048.     and     v.type = 'P'    /* cross product to get all exposed privileges */
  19049.     and     v.number in (26,193,195,196,197)
  19050.     and     not exists (    /* exclude revoke'd privileges */
  19051.             select     *
  19052.             from     sysprotects p1
  19053.             where    p1.protecttype = 206
  19054.             and     p1.action = v.number
  19055.             and     p1.id = o.id
  19056.             and     p1.uid = o.uid)
  19057.     order by 4,5,6,1,2
  19058.     END
  19059. ELSE
  19060.     BEGIN
  19061.     select
  19062.         GRANTOR        = user_name(p.grantor),
  19063.         GRANTEE        = user_name(u.uid),
  19064.         TABLE_CATALOG    = db_name(),
  19065.         TABLE_SCHEMA    = user_name(o.uid),
  19066.         TABLE_NAME    = o.name,
  19067.         PRIVILEGE_TYPE    = convert(varchar(30),
  19068.                     case p.action
  19069.                     when 193 then 'SELECT'
  19070.                     when 195 then 'INSERT'
  19071.                     when 196 then 'DELETE'
  19072.                     when 197 then 'UPDATE'
  19073.                     else 'REFERENCES'
  19074.                     end),
  19075.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  19076.     from 
  19077.         sysprotects p, sysobjects o, sysusers u
  19078.     where
  19079.         o.type in ('U','V','S')
  19080.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  19081.     and    (@grantee is null or @grantee = user_name(u.uid))
  19082.     and    p.id = o.id
  19083.     and     (@grantor is null or @grantor = user_name(p.grantor))
  19084.             /* expand groups */
  19085.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  19086.     and     p.protecttype <> 206    /* only grant rows */
  19087.     and     p.action in (26,193,195,196,197)
  19088.     and     o.uid <> u.uid            /* no rows for owner */
  19089.     and     not exists (            /* exclude revoke'd privileges */
  19090.             select     *
  19091.             from     sysprotects p1
  19092.             where    p1.protecttype = 206
  19093.             and     p1.action = p.action
  19094.             and     p1.id = p.id
  19095.             and     p1.uid = u.uid)
  19096.     union all
  19097.     select    /*    Add rows for table owner */
  19098.         GRANTOR        = user_name(u.uid),
  19099.         GRANTEE        = user_name(o.uid),
  19100.         TABLE_CATALOG    = db_name(),
  19101.         TABLE_SCHEMA    = user_name(o.uid),
  19102.         TABLE_NAME    = o.name,
  19103.         PRIVILEGE_TYPE    = convert(varchar(30),
  19104.                     case v.number
  19105.                     when 193 then 'SELECT'
  19106.                     when 195 then 'INSERT'
  19107.                     when 196 then 'DELETE'
  19108.                     when 197 then 'UPDATE'
  19109.                     else 'REFERENCES'
  19110.                     end),
  19111.         IS_GRANTABLE    = convert(bit,1)    
  19112.     from 
  19113.         sysobjects o, master.dbo.spt_values v, sysusers u
  19114.     where
  19115.         o.type in ('U','V','S')
  19116.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  19117.     and    (@grantee is null or @grantee = user_name(u.uid))
  19118.     and     u.suid = 1        /* grantor is dbo of database */
  19119.     and    (@grantor is null or @grantor = user_name(u.uid))
  19120.     and     v.type = 'P'    /* cross product to get all exposed privileges */
  19121.     and     v.number in (26,193,195,196,197)
  19122.     and     not exists (    /* exclude revoke'd privileges */
  19123.             select     *
  19124.             from     sysprotects p1
  19125.             where    p1.protecttype = 206
  19126.             and     p1.action = v.number
  19127.             and     p1.id = o.id
  19128.             and     p1.uid = o.uid)
  19129.     order by 4,5,6,1,2
  19130.     END
  19131. go
  19132. dump tran master with no_log
  19133. go
  19134. CREATE PROCEDURE sp_table_privileges_rowset;2
  19135.     (
  19136.        @handle        int output,
  19137.        @scrollopt    int output,
  19138.     @ccopt        int output,
  19139.     @rows        int output,
  19140.     @table_name     varchar(255) = null,
  19141.     @table_schema    varchar(255) = null,
  19142.     @grantor    varchar(255) = null,
  19143.     @grantee    varchar(255) = null
  19144.     )
  19145. as
  19146. declare @ret int
  19147. SET NOCOUNT ON
  19148. create table #sptprivsrowset1
  19149.     (
  19150.     GRANTOR        sysname not null,
  19151.     GRANTEE        sysname not null,
  19152.     TABLE_CATALOG    sysname not null,
  19153.     TABLE_SCHEMA    sysname not null,
  19154.     TABLE_NAME    sysname not null,
  19155.     PRIVILEGE_TYPE    sysname not null,
  19156.     IS_GRANTABLE    bit not null
  19157.     )
  19158.     
  19159. IF @table_name is not null
  19160.     BEGIN
  19161.     insert into #sptprivsrowset1
  19162.     select
  19163.         GRANTOR        = user_name(p.grantor),
  19164.         GRANTEE        = user_name(u.uid),
  19165.         TABLE_CATALOG    = db_name(),
  19166.         TABLE_SCHEMA    = user_name(o.uid),
  19167.         TABLE_NAME    = o.name,
  19168.         PRIVILEGE_TYPE    = convert(varchar(30),
  19169.                     case p.action
  19170.                     when 193 then 'SELECT'
  19171.                     when 195 then 'INSERT'
  19172.                     when 196 then 'DELETE'
  19173.                     when 197 then 'UPDATE'
  19174.                     else 'REFERENCES'
  19175.                     end),
  19176.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  19177.     from 
  19178.         sysprotects p, sysobjects o, sysusers u
  19179.     where
  19180.         o.name = @table_name
  19181.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  19182.     and    (@grantee is null or @grantee = user_name(u.uid))
  19183.     and     o.type in ('U','V','S')
  19184.     and    p.id = o.id
  19185.     and     (@grantor is null or @grantor = user_name(p.grantor))
  19186.             /* expand groups */
  19187.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  19188.     and     p.protecttype <> 206    /* only grant rows */
  19189.     and     p.action in (26,193,195,196,197)
  19190.     and     o.uid <> u.uid            /* no rows for owner */
  19191.     and     not exists (            /* exclude revoke'd privileges */
  19192.             select     *
  19193.             from     sysprotects p1
  19194.             where    p1.protecttype = 206
  19195.             and     p1.action = p.action
  19196.             and     p1.id = p.id
  19197.             and     p1.uid = u.uid)
  19198.     union all
  19199.     select    /*    Add rows for table owner */
  19200.         GRANTOR        = user_name(u.uid),
  19201.         GRANTEE        = user_name(o.uid),
  19202.         TABLE_CATALOG    = db_name(),
  19203.         TABLE_SCHEMA    = user_name(o.uid),
  19204.         TABLE_NAME    = o.name,
  19205.         PRIVILEGE_TYPE    = convert(varchar(30),
  19206.                     case v.number
  19207.                     when 193 then 'SELECT'
  19208.                     when 195 then 'INSERT'
  19209.                     when 196 then 'DELETE'
  19210.                     when 197 then 'UPDATE'
  19211.                     else 'REFERENCES'
  19212.                     end),
  19213.         IS_GRANTABLE    = convert(bit,1)    
  19214.     from 
  19215.         sysobjects o, master.dbo.spt_values v, sysusers u
  19216.     where
  19217.         o.name = @table_name
  19218.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  19219.     and    (@grantee is null or @grantee = user_name(u.uid))
  19220.     and     o.type in ('U','V','S')
  19221.     and     u.suid = 1        /* grantor is dbo of database */
  19222.     and    (@grantor is null or @grantor = user_name(u.uid))
  19223.     and     v.type = 'P'    /* cross product to get all exposed privileges */
  19224.     and     v.number in (26,193,195,196,197)
  19225.     and     not exists (    /* exclude revoke'd privileges */
  19226.             select     *
  19227.             from     sysprotects p1
  19228.             where    p1.protecttype = 206
  19229.             and     p1.action = v.number
  19230.             and     p1.id = o.id
  19231.             and     p1.uid = o.uid)
  19232.     order by 4,5,6,1,2
  19233.     END
  19234. ELSE
  19235.     BEGIN
  19236.     insert into #sptprivsrowset1
  19237.     select
  19238.         GRANTOR        = user_name(p.grantor),
  19239.         GRANTEE        = user_name(u.uid),
  19240.         TABLE_CATALOG    = db_name(),
  19241.         TABLE_SCHEMA    = user_name(o.uid),
  19242.         TABLE_NAME    = o.name,
  19243.         PRIVILEGE_TYPE    = convert(varchar(30),
  19244.                     case p.action
  19245.                     when 193 then 'SELECT'
  19246.                     when 195 then 'INSERT'
  19247.                     when 196 then 'DELETE'
  19248.                     when 197 then 'UPDATE'
  19249.                     else 'REFERENCES'
  19250.                     end),
  19251.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  19252.     from 
  19253.         sysprotects p, sysobjects o, sysusers u
  19254.     where
  19255.         o.type in ('U','V','S')
  19256.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  19257.     and    (@grantee is null or @grantee = user_name(u.uid))
  19258.     and    p.id = o.id
  19259.     and     (@grantor is null or @grantor = user_name(p.grantor))
  19260.             /* expand groups */
  19261.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  19262.     and     p.protecttype <> 206    /* only grant rows */
  19263.     and     p.action in (26,193,195,196,197)
  19264.     and     o.uid <> u.uid            /* no rows for owner */
  19265.     and     not exists (            /* exclude revoke'd privileges */
  19266.             select     *
  19267.             from     sysprotects p1
  19268.             where    p1.protecttype = 206
  19269.             and     p1.action = p.action
  19270.             and     p1.id = p.id
  19271.             and     p1.uid = u.uid)
  19272.     union all
  19273.     select    /*    Add rows for table owner */
  19274.         GRANTOR        = user_name(u.uid),
  19275.         GRANTEE        = user_name(o.uid),
  19276.         TABLE_CATALOG    = db_name(),
  19277.         TABLE_SCHEMA    = user_name(o.uid),
  19278.         TABLE_NAME    = o.name,
  19279.         PRIVILEGE_TYPE    = convert(varchar(30),
  19280.                     case v.number
  19281.                     when 193 then 'SELECT'
  19282.                     when 195 then 'INSERT'
  19283.                     when 196 then 'DELETE'
  19284.                     when 197 then 'UPDATE'
  19285.                     else 'REFERENCES'
  19286.                     end),
  19287.         IS_GRANTABLE    = convert(bit,1)    
  19288.     from 
  19289.         sysobjects o, master.dbo.spt_values v, sysusers u
  19290.     where
  19291.         o.type in ('U','V','S')
  19292.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  19293.     and    (@grantee is null or @grantee = user_name(u.uid))
  19294.     and     u.suid = 1        /* grantor is dbo of database */
  19295.     and    (@grantor is null or @grantor = user_name(u.uid))
  19296.     and     v.type = 'P'    /* cross product to get all exposed privileges */
  19297.     and     v.number in (26,193,195,196,197)
  19298.     and     not exists (    /* exclude revoke'd privileges */
  19299.             select     *
  19300.             from     sysprotects p1
  19301.             where    p1.protecttype = 206
  19302.             and     p1.action = v.number
  19303.             and     p1.id = o.id
  19304.             and     p1.uid = o.uid)
  19305.     order by 4,5,6,1,2
  19306.     END
  19307.  
  19308. exec @ret = sp_cursoropen @handle output, 'select * from #sptprivsrowset1',
  19309.     @scrollopt output, @ccopt output, @rows output
  19310.  
  19311. drop table #sptprivsrowset1
  19312. return isnull(@ret,0)
  19313. go
  19314. dump tran master with no_log
  19315. go
  19316. CREATE PROCEDURE sp_table_privileges_rowset;3
  19317. as
  19318.     select
  19319.         GRANTOR        = convert(sysname, ' '),
  19320.         GRANTEE        = convert(sysname, ' '),
  19321.         TABLE_CATALOG    = convert(sysname, ' '),
  19322.         TABLE_SCHEMA    = convert(sysname, ' '),
  19323.         TABLE_NAME    = convert(sysname, ' '),
  19324.         PRIVILEGE_TYPE    = convert(varchar(30), ' '),
  19325.         IS_GRANTABLE    = convert(bit, 0)
  19326.     where    1=0
  19327. go
  19328.  
  19329. if (charindex('7.00', @@version) = 0)
  19330. begin
  19331.     print ''
  19332.     print ''
  19333.     print 'Warning:'
  19334.     print 'you are installing the stored procedures '
  19335.     print 'on a pre 7.0 SQL Server.'
  19336.     print 'Ignore the following errors.'
  19337. end
  19338. else
  19339.     drop proc sp_table_privileges_rowset
  19340. go
  19341.  
  19342.  
  19343. /*    Procedure for 7.0 server */
  19344. CREATE PROCEDURE sp_table_privileges_rowset
  19345.     (
  19346.     @table_name         sysname,
  19347.     @table_schema        sysname = null,
  19348.     @grantor        sysname = null,
  19349.     @grantee        sysname = null
  19350.     )
  19351. as
  19352.     select
  19353.         GRANTOR        = user_name(p.grantor),
  19354.         GRANTEE        = user_name(u.uid),
  19355.         TABLE_CATALOG    = db_name(),
  19356.         TABLE_SCHEMA    = user_name(o.uid),
  19357.         TABLE_NAME    = o.name,
  19358.         PRIVILEGE_TYPE    = convert(nvarchar(30),
  19359.                     case p.action
  19360.                     when 193 then N'SELECT'
  19361.                     when 195 then N'INSERT'
  19362.                     when 196 then N'DELETE'
  19363.                     when 197 then N'UPDATE'
  19364.                     else N'REFERENCES'
  19365.                     end),
  19366.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  19367.     from 
  19368.         sysprotects p, sysobjects o, sysusers u, sysmembers m
  19369.     where
  19370.         o.name = @table_name
  19371.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  19372.     and    (@grantee is null or @grantee = user_name(u.uid))
  19373.     and     o.type in ('U','V','S')
  19374.     and    p.id = o.id
  19375.     and     (@grantor is null or @grantor = user_name(p.grantor))
  19376.         /* expand groups - AKUNDONE: only 1 level of grp unrolling. */
  19377.     and     (u.uid > 0 and u.uid < 16384)
  19378.     and     ((p.uid = u.uid) or (p.uid = m.groupuid and u.uid = m.memberuid))
  19379.     and     p.protecttype <> 206    /* only grant rows */
  19380.     and     p.action in (26,193,195,196,197)
  19381.     and     o.uid <> u.uid            /* no rows for owner */
  19382.     and     not exists (            /* exclude revoke'd privileges */
  19383.             select     *
  19384.             from     sysprotects p1
  19385.             where    p1.protecttype = 206
  19386.             and     p1.action = p.action
  19387.             and     p1.id = p.id
  19388.             and     p1.uid = u.uid)
  19389.     union all
  19390.     select    /*    Add rows for table owner */
  19391.         GRANTOR        = user_name(u.uid),
  19392.         GRANTEE        = user_name(o.uid),
  19393.         TABLE_CATALOG    = db_name(),
  19394.         TABLE_SCHEMA    = user_name(o.uid),
  19395.         TABLE_NAME    = o.name,
  19396.         PRIVILEGE_TYPE    = convert(nvarchar(30),
  19397.                     case v.number
  19398.                     when 193 then N'SELECT'
  19399.                     when 195 then N'INSERT'
  19400.                     when 196 then N'DELETE'
  19401.                     when 197 then N'UPDATE'
  19402.                     else N'REFERENCES'
  19403.                     end),
  19404.         IS_GRANTABLE    = convert(bit,1)    
  19405.     from 
  19406.         sysobjects o, master.dbo.spt_values v, sysusers u
  19407.     where
  19408.         o.name = @table_name
  19409.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  19410.     and    (@grantee is null or @grantee = user_name(u.uid))
  19411.     and     o.type in ('U','V','S')
  19412.     and     u.suid = 1        /* grantor is dbo of database */
  19413.     and    (@grantor is null or @grantor = user_name(u.uid))
  19414.     and     v.type = 'P'    /* cross product to get all exposed privileges */
  19415.     and     v.number in (26,193,195,196,197)
  19416.     and     not exists (    /* exclude revoke'd privileges */
  19417.             select     *
  19418.             from     sysprotects p1
  19419.             where    p1.protecttype = 206
  19420.             and     p1.action = v.number
  19421.             and     p1.id = o.id
  19422.             and     p1.uid = o.uid)
  19423.     order by 4,5,6,1,2
  19424. go
  19425. dump tran master with no_log
  19426. go
  19427. CREATE PROCEDURE sp_table_privileges_rowset;2
  19428.     (
  19429.     @table_schema        sysname = null,
  19430.     @grantor        sysname = null,
  19431.     @grantee        sysname = null
  19432.     )
  19433. as
  19434.     select
  19435.         GRANTOR        = user_name(p.grantor),
  19436.         GRANTEE        = user_name(u.uid),
  19437.         TABLE_CATALOG    = db_name(),
  19438.         TABLE_SCHEMA    = user_name(o.uid),
  19439.         TABLE_NAME    = o.name,
  19440.         PRIVILEGE_TYPE    = convert(nvarchar(30),
  19441.                     case p.action
  19442.                     when 193 then N'SELECT'
  19443.                     when 195 then N'INSERT'
  19444.                     when 196 then N'DELETE'
  19445.                     when 197 then N'UPDATE'
  19446.                     else N'REFERENCES'
  19447.                     end),
  19448.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  19449.     from 
  19450.         sysprotects p, sysobjects o, sysusers u, sysmembers m
  19451.     where
  19452.         o.type in ('U','V','S')
  19453.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  19454.     and    (@grantee is null or @grantee = user_name(u.uid))
  19455.     and    p.id = o.id
  19456.     and     (@grantor is null or @grantor = user_name(p.grantor))
  19457.         /* expand groups - AKUNDONE: only 1 level of grp unrolling. */
  19458.     and     (u.uid > 0 and u.uid < 16384)
  19459.     and     ((p.uid = u.uid) or (p.uid = m.groupuid and u.uid = m.memberuid))
  19460.     and     p.protecttype <> 206    /* only grant rows */
  19461.     and     p.action in (26,193,195,196,197)
  19462.     and     o.uid <> u.uid            /* no rows for owner */
  19463.     and     not exists (            /* exclude revoke'd privileges */
  19464.             select     *
  19465.             from     sysprotects p1
  19466.             where    p1.protecttype = 206
  19467.             and     p1.action = p.action
  19468.             and     p1.id = p.id
  19469.             and     p1.uid = u.uid)
  19470.     union all
  19471.     select    /*    Add rows for table owner */
  19472.         GRANTOR        = user_name(u.uid),
  19473.         GRANTEE        = user_name(o.uid),
  19474.         TABLE_CATALOG    = db_name(),
  19475.         TABLE_SCHEMA    = user_name(o.uid),
  19476.         TABLE_NAME    = o.name,
  19477.         PRIVILEGE_TYPE    = convert(nvarchar(30),
  19478.                     case v.number
  19479.                     when 193 then N'SELECT'
  19480.                     when 195 then N'INSERT'
  19481.                     when 196 then N'DELETE'
  19482.                     when 197 then N'UPDATE'
  19483.                     else N'REFERENCES'
  19484.                     end),
  19485.         IS_GRANTABLE    = convert(bit,1)    
  19486.     from 
  19487.         sysobjects o, master.dbo.spt_values v, sysusers u
  19488.     where
  19489.         o.type in ('U','V','S')
  19490.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  19491.     and    (@grantee is null or @grantee = user_name(u.uid))
  19492.     and     u.suid = 1        /* grantor is dbo of database */
  19493.     and    (@grantor is null or @grantor = user_name(u.uid))
  19494.     and     v.type = 'P'    /* cross product to get all exposed privileges */
  19495.     and     v.number in (26,193,195,196,197)
  19496.     and     not exists (    /* exclude revoke'd privileges */
  19497.             select     *
  19498.             from     sysprotects p1
  19499.             where    p1.protecttype = 206
  19500.             and     p1.action = v.number
  19501.             and     p1.id = o.id
  19502.             and     p1.uid = o.uid)
  19503.     order by 4,5,6,1,2
  19504. go
  19505. dump tran master with no_log
  19506. go
  19507. create procedure sp_table_privileges_rowset;5
  19508.     (
  19509.     @table_server        sysname,
  19510.     @table_catalog        sysname = null,
  19511.     @table_name        sysname = null,
  19512.     @table_schema        sysname = null,
  19513.     @grantor        sysname = null,
  19514.     @grantee        sysname = null
  19515.     )
  19516. as
  19517.     select
  19518.         GRANTOR,        
  19519.         GRANTEE,        
  19520.         TABLE_CATALOG,    
  19521.         TABLE_SCHEMA,    
  19522.         TABLE_NAME,    
  19523.         PRIVILEGE_TYPE,    
  19524.         IS_GRANTABLE    
  19525.     from master.dbo.SYSREMOTE_TABLE_PRIVILEGES <
  19526.                 @table_server,
  19527.                 @table_catalog,
  19528.                 @table_schema,
  19529.                 @table_name,
  19530.                 @grantor,
  19531.                 @grantee >
  19532.     order by 3,4,5,6,1,2
  19533. go
  19534.  
  19535. grant execute on sp_table_privileges_rowset to public
  19536. go
  19537. dump tran master with no_log
  19538. go
  19539. if (charindex('6.00', @@version) > 0)
  19540.     begin
  19541.     if (exists (select * from sysobjects
  19542.         where name = 'sp_table_privileges_rowset' and type = 'P '))
  19543.         begin
  19544.         drop proc sp_table_privileges_rowset
  19545.         dump tran master with no_log
  19546.         end
  19547.     end
  19548. go
  19549.  
  19550.  
  19551. print ''
  19552. print 'creating sp_linkedservers_rowset'
  19553. go
  19554. if (charindex('7.00', @@version) = 0)
  19555. begin
  19556.     print ''
  19557.     print ''
  19558.     print 'Warning:'
  19559.     print 'you are installing the stored procedures '
  19560.     print 'on a pre 7.0 SQL Server.'
  19561.     print 'Ignore the following errors.'
  19562. end
  19563. go
  19564. /*    Procedure for 7.0 server */
  19565. create proc sp_linkedservers_rowset
  19566.     (
  19567.     @srvname     sysname
  19568.     )
  19569. as
  19570.     select
  19571.         SVR_NAME         = srvname,
  19572.         SVR_PRODUCT        = srvproduct,
  19573.         SVR_PROVIDERNAME    = providername,
  19574.         SVR_DATASOURCE        = datasource,
  19575.         SVR_PROVIDERSTRING    = providerstring,
  19576.         SVR_LOCATION        = location,
  19577.         SVR_CATALOG        = catalog
  19578.     from master.dbo.sysservers
  19579.     where srvname = @srvname and (srvstatus & 128) = 128
  19580.     order by 1
  19581. go
  19582. dump tran master with no_log
  19583. go
  19584. create proc sp_linkedservers_rowset;2
  19585. as
  19586.     select
  19587.         SVR_NAME         = srvname,
  19588.         SVR_PRODUCT        = srvproduct,
  19589.         SVR_PROVIDERNAME    = providername,
  19590.         SVR_DATASOURCE        = datasource,
  19591.         SVR_PROVIDERSTRING    = providerstring,
  19592.         SVR_LOCATION        = location,
  19593.         SVR_CATALOG        = catalog
  19594.     from master.dbo.sysservers
  19595.     where (srvstatus & 128) = 128
  19596.     order by 1
  19597. go
  19598.  
  19599. grant execute on sp_linkedservers_rowset to public
  19600. go
  19601.  
  19602. /*---------------------------- END OLEDB CATALOG PROCS ------------------------*/
  19603.  
  19604.  
  19605. dump tran master with no_log
  19606. go
  19607.  
  19608.  
  19609.  
  19610.  
  19611. if (exists (select * from sysobjects
  19612.         where name = 'sp_configure' and type = 'P '))
  19613.     begin
  19614.         exec sp_configure 'allow updates',0
  19615.         reconfigure with override
  19616.     end
  19617. go
  19618.  
  19619. exec sp_MS_upd_sysobj_category 2 /* set category | 2 based on crdate. */
  19620.  
  19621. go
  19622.  
  19623. if exists (select * from sysobjects where name = 'sp_check_objects'
  19624.             and type = 'P ')
  19625.     begin
  19626.         /* Only supported on 6.0 servers */
  19627.         print ''
  19628.         print 'Checking objects created by instcat.sql.'
  19629.  
  19630.         exec sp_check_objects 'catalog'
  19631.     end
  19632. go
  19633.  
  19634. print ''
  19635. print 'instcat.sql completed successfully.'
  19636. go
  19637.  
  19638. set quoted_identifier off
  19639. go
  19640.  
  19641. dump tran master with no_log
  19642. go
  19643. checkpoint
  19644. go
  19645. /**/
  19646.