home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD 64 / supercd64_1.iso / Toma nota / MicroArea / Demo WinLab / WINLAB 6.msi / Binary.New_Binary3 / sqlnet.cab / instcat.sql < prev    next >
Encoding:
Text File  |  2000-07-14  |  732.8 KB  |  23,849 lines

  1.  
  2. /*
  3. **    INSTCAT.SQL
  4. **    Installs catalog stored procedures on the Microsoft SQL Server.
  5. ** Copyright Microsoft, Inc. 1994 - 2000
  6. ** All Rights Reserved.
  7. */
  8.  
  9. /*
  10. INTERNAL NOTE:
  11. When this file is updated, you MUST change the last row inserted into
  12. spt_server_info to be version number of this file.    the convention is
  13. j.nn.bbb, where j is the major version number ('7' now), nn is the minor
  14. version number ('10' now), and bbb is the build number.
  15. insert into spt_server_info
  16.     values (500, 'SYS_SPROC_VERSION', '8.00.bbb')
  17. */
  18.  
  19. /****************************************************************************/
  20. /* This portion sets up the ability to perform all the functions in this    */
  21. /* script                                                                    */
  22. /****************************************************************************/
  23. go
  24. use master
  25. go
  26. dump tran master with no_log
  27. go
  28.  
  29. set quoted_identifier on
  30. go
  31.  
  32. if (exists (select * from sysobjects
  33.         where name = 'sp_configure' and type = 'P '))
  34. begin
  35.     execute sp_configure 'update',1
  36. end
  37. reconfigure with override
  38. go
  39.  
  40. exec sp_MS_upd_sysobj_category 1 /*Capture datetime for use below.*/
  41.  
  42. go
  43.  
  44. /*
  45. ** If old versions of tables exist, drop them.
  46. */
  47. if (exists (select * from sysobjects
  48.         where name = 'MSdatatype_info' and type = 'U '))
  49.     drop table MSdatatype_info
  50. go
  51. if (exists (select * from sysobjects
  52.         where name = 'MSdatatype_info_ext' and type = 'U '))
  53.     drop table MSdatatype_info_ext
  54. go
  55. if (exists (select * from sysobjects
  56.         where name = 'MStable_types' and type = 'U '))
  57.     drop table MStable_types
  58. go
  59. if (exists (select * from sysobjects
  60.         where name = 'MSserver_info' and type = 'U '))
  61.     drop table MSserver_info
  62. go
  63. if (exists (select * from sysobjects
  64.         where name = 'spt_table_types' and type = 'U '))
  65.     drop table spt_table_types
  66. go
  67. /*
  68. ** If tables or procs already exist, drop them.
  69. */
  70.  
  71. if (exists (select * from sysobjects
  72.         where name = 'spt_datatype_info' and type = 'U '))
  73.     drop table spt_datatype_info
  74. go
  75. if (exists (select * from sysobjects
  76.         where name = 'spt_datatype_info_ext' and type = 'U '))
  77.     drop table spt_datatype_info_ext
  78. go
  79. if (exists (select * from sysobjects
  80.         where name = 'sp_add_server_sortinfo' and type = 'P '))
  81.     drop proc sp_add_server_sortinfo
  82. go
  83. if (exists (select * from sysobjects
  84.         where name = 'sp_add_server_sortinfo75' and type = 'P '))
  85.     drop proc sp_add_server_sortinfo75
  86. go
  87. if (exists (select * from sysobjects
  88.         where name = 'spt_server_info' and type = 'U '))
  89.     drop table spt_server_info
  90. go
  91. if (exists (select * from sysobjects
  92.         where name = 'sp_tables' and type = 'P '))
  93.     drop proc sp_tables
  94. go
  95. if (exists (select * from sysobjects
  96.         where name = 'sp_statistics' and type = 'P '))
  97.     drop proc sp_statistics
  98. go
  99. if (exists (select * from sysobjects
  100.         where name = 'sp_columns' and type = 'P '))
  101.     drop proc sp_columns
  102. go
  103. if (exists (select * from sysobjects
  104.         where name = 'sp_fkeys' and type = 'P '))
  105.     drop proc sp_fkeys
  106. go
  107. if (exists (select * from sysobjects
  108.         where name = 'sp_pkeys' and type = 'P '))
  109.     drop proc sp_pkeys
  110. dump tran master with no_log
  111. go
  112.  
  113. go
  114. if (exists (select * from sysobjects
  115.         where name = 'sp_stored_procedures' and type = 'P '))
  116.     drop proc sp_stored_procedures
  117. go
  118. if (exists (select * from sysobjects
  119.         where name = 'sp_sproc_columns' and type = 'P '))
  120.     drop proc sp_sproc_columns
  121. go
  122. if (exists (select * from sysobjects
  123.         where name = 'sp_table_privileges' and type = 'P '))
  124.     drop proc sp_table_privileges
  125. go
  126. if (exists (select * from sysobjects
  127.         where name = 'sp_column_privileges' and type = 'P '))
  128.     drop proc sp_column_privileges
  129. go
  130. dump tran master with no_log
  131. go
  132. if (exists (select * from sysobjects
  133.         where name = 'sp_server_info' and type = 'P '))
  134.     drop proc sp_server_info
  135. go
  136. if (exists (select * from sysobjects
  137.         where name = 'sp_datatype_info' and type = 'P '))
  138.     drop proc sp_datatype_info
  139. go
  140. if (exists (select * from sysobjects
  141.         where name = 'sp_special_columns' and type = 'P '))
  142.     drop proc sp_special_columns
  143. go
  144. if (exists (select * from sysobjects
  145.         where name = 'sp_databases' and type = 'P '))
  146.     drop proc sp_databases
  147. go
  148. dump tran master with no_log
  149. go
  150. if (exists (select * from sysobjects
  151.         where name = 'sp_ddopen' and type = 'P '))
  152.     drop proc sp_ddopen
  153. go
  154. if (exists (select * from sysobjects
  155.         where name = 'sp_tableswc' and type = 'P '))
  156.     drop proc sp_tableswc
  157. go
  158.  
  159. if (exists (select * from sysobjects
  160.         where name = 'sp_tablecollations' and type = 'P'))
  161.     drop proc sp_tablecollations
  162. go
  163.  
  164. if (exists (select * from sysobjects
  165.         where name = 'sp_bcp_dbcmptlevel' and type = 'P'))
  166.     drop proc sp_bcp_dbcmptlevel
  167. go
  168.  
  169. dump tran master with no_log
  170. go
  171.  
  172.  
  173. if (exists (select * from sysobjects
  174.         where name = 'spt_provider_types' and type = 'U '))
  175.     begin
  176.     drop table spt_provider_types
  177.     dump tran master with no_log
  178.     end
  179. go
  180. if (exists (select * from sysobjects
  181.         where name = 'sp_catalogs_rowset' and type = 'P '))
  182.     begin
  183.     drop procedure sp_catalogs_rowset
  184.     dump tran master with no_log
  185.     end
  186. go
  187. if (exists (select * from sysobjects
  188.         where name = 'sp_column_privileges_rowset' and type = 'P '))
  189.     begin
  190.     drop procedure sp_column_privileges_rowset
  191.     dump tran master with no_log
  192.     end
  193. go
  194. if (exists (select * from sysobjects
  195.         where name = 'sp_columns_rowset' and type = 'P '))
  196.     begin
  197.     drop procedure sp_columns_rowset
  198.     dump tran master with no_log
  199.     end
  200. go
  201. if (exists (select * from sysobjects
  202.         where name = 'sp_check_constraints_rowset' and type = 'P '))
  203.     begin
  204.     drop procedure sp_check_constraints_rowset
  205.     dump tran master with no_log
  206.     end
  207. go
  208. if (exists (select * from sysobjects
  209.         where name = 'sp_check_constbytable_rowset' and type = 'P '))
  210.     begin
  211.     drop procedure sp_check_constbytable_rowset
  212.     dump tran master with no_log
  213.     end
  214. go
  215. if (exists (select * from sysobjects
  216.         where name = 'sp_foreign_keys_rowset' and type = 'P '))
  217.     begin
  218.     drop procedure sp_foreign_keys_rowset
  219.     dump tran master with no_log
  220.     end
  221. go
  222. if (exists (select * from sysobjects
  223.         where name = 'sp_indexes_rowset' and type = 'P '))
  224.     begin
  225.     drop procedure sp_indexes_rowset
  226.     dump tran master with no_log
  227.     end
  228. go
  229. if (exists (select * from sysobjects
  230.         where name = 'sp_primary_keys_rowset' and type = 'P '))
  231.     begin
  232.     drop procedure sp_primary_keys_rowset
  233.     dump tran master with no_log
  234.     end
  235. go
  236. if (exists (select * from sysobjects
  237.         where name = 'sp_procedure_params_rowset' and type = 'P '))
  238.     begin
  239.     drop procedure sp_procedure_params_rowset
  240.     dump tran master with no_log
  241.     end
  242. go
  243. if (exists (select * from sysobjects
  244.         where name = 'sp_procedures_rowset' and type = 'P '))
  245.     begin
  246.     drop procedure sp_procedures_rowset
  247.     dump tran master with no_log
  248.     end
  249. go
  250. if (exists (select * from sysobjects
  251.         where name = 'sp_provider_types_rowset' and type = 'P '))
  252.     begin
  253.     drop procedure sp_provider_types_rowset
  254.     dump tran master with no_log
  255.     end
  256. go
  257. if (exists (select * from sysobjects
  258.         where name = 'sp_schemata_rowset' and type = 'P '))
  259.     begin
  260.     drop procedure sp_schemata_rowset
  261.     dump tran master with no_log
  262.     end
  263. go
  264. if (exists (select * from sysobjects
  265.         where name = 'sp_statistics_rowset' and type = 'P '))
  266.     begin
  267.     drop procedure sp_statistics_rowset
  268.     dump tran master with no_log
  269.     end
  270. go
  271. if (exists (select * from sysobjects
  272.         where name = 'sp_tables_rowset' and type = 'P '))
  273.     begin
  274.     drop procedure sp_tables_rowset
  275.     dump tran master with no_log
  276.     end
  277. go
  278. if (exists (select * from sysobjects
  279.         where name = 'sp_tables_info_rowset' and type = 'P '))
  280.     begin
  281.     drop procedure sp_tables_info_rowset
  282.     dump tran master with no_log
  283.     end
  284. go
  285. if (exists (select * from sysobjects
  286.         where name = 'sp_table_constraints_rowset' and type = 'P '))
  287.     begin
  288.     drop proc sp_table_constraints_rowset
  289.     dump tran master with no_log
  290.     end
  291. go
  292. if (exists (select * from sysobjects
  293.         where name = 'sp_table_privileges_rowset' and type = 'P '))
  294.     begin
  295.     drop proc sp_table_privileges_rowset
  296.     dump tran master with no_log
  297.     end
  298. go
  299. if (exists (select * from sysobjects
  300.         where name = 'sp_linkedservers_rowset' and type = 'P '))
  301.     begin
  302.     drop proc sp_linkedservers_rowset
  303.     dump tran master with no_log
  304.     end
  305. go
  306.  
  307. if (exists (select * from sysobjects
  308.         where name = 'sp_table_statistics_rowset' and type = 'P '))
  309.     begin
  310.     drop proc sp_table_statistics_rowset
  311.     dump tran master with no_log
  312.     end
  313. go
  314.  
  315. if (exists (select * from sysobjects
  316.         where name = 'sp_oledb_column_constraints' and type = 'P '))
  317.     begin
  318.     drop proc sp_oledb_column_constraints
  319.     dump tran master with no_log
  320.     end
  321. go
  322.  
  323. if (exists (select * from sysobjects
  324.         where name = 'sp_oledb_indexinfo' and type = 'P '))
  325.     begin
  326.     drop proc sp_oledb_indexinfo
  327.     dump tran master with no_log
  328.     end
  329. go
  330.  
  331. if (exists (select * from sysobjects
  332.         where name = 'sp_oledb_ro_usrname' and type = 'P '))
  333.     begin
  334.     drop proc sp_oledb_ro_usrname
  335.     dump tran master with no_log
  336.     end
  337. go
  338.  
  339. if (exists (select * from sysobjects
  340.         where name = 'sp_oledb_deflang' and type = 'P '))
  341.     begin
  342.     drop proc sp_oledb_deflang
  343.     dump tran master with no_log
  344.     end
  345. go
  346.  
  347. if (exists (select * from sysobjects
  348.         where name = 'sp_oledb_defdb' and type = 'P '))
  349.     begin
  350.     drop proc sp_oledb_defdb
  351.     dump tran master with no_log
  352.     end
  353. go
  354.  
  355. if (exists (select * from sysobjects
  356.         where name = 'sp_oledb_database' and type = 'P '))
  357.     begin
  358.     drop proc sp_oledb_database
  359.     dump tran master with no_log
  360.     end
  361. go
  362.  
  363. if (exists (select * from sysobjects
  364.         where name = 'sp_oledb_language' and type = 'P '))
  365.     begin
  366.     drop proc sp_oledb_language
  367.     dump tran master with no_log
  368.     end
  369. go
  370.  
  371. print 'creating table spt_datatype_info_ext'
  372. go
  373.  
  374. if (charindex('6.00', @@version) = 0 and
  375.     charindex('6.50', @@version) = 0 and
  376.     charindex('7.00', @@version) = 0 and
  377.     charindex('8.00', @@version) = 0)
  378. begin    /*    Pre 6.0 server */
  379.     print ''
  380.     print ''
  381.     print 'Warning:'
  382.     print 'you are installing the stored procedures '
  383.     print 'on a pre 6.0 SQL Server.'
  384.     print 'Ignore the following error.'
  385.     create table spt_datatype_info_ext (
  386.                 user_type        smallint    not null,
  387.                 CREATE_PARAMS    varchar(32) null,
  388.                 AUTO_INCREMENT    smallint null,
  389.                 typename        varchar(32))
  390. end
  391. go
  392. if (charindex('6.00', @@version) > 0 or
  393.     charindex('6.50', @@version) > 0 or
  394.     charindex('7.00', @@version) > 0 or
  395.     charindex('8.00', @@version) > 0)
  396. begin    /* 6.0 or later server */
  397.     create table spt_datatype_info_ext (
  398.                 user_type        smallint    not null,
  399.                 CREATE_PARAMS    varchar(32) null,
  400.                 AUTO_INCREMENT    smallint null,
  401.                 typename        sysname)    /* from systypes, to avoid xusertype hard-code */
  402. end
  403. go
  404.  
  405. grant select on spt_datatype_info_ext to public
  406. go
  407.  
  408.  
  409. insert into spt_datatype_info_ext
  410.     /* CHAR      user_type, create_params, auto_increment */
  411.     values             (1,    'length' ,0, 'char')
  412.  
  413. insert into spt_datatype_info_ext
  414.     /* VARCHAR     user_type, create_params, auto_increment */
  415.     values             (2,    'max length' ,0, 'varchar')
  416.  
  417. insert into spt_datatype_info_ext
  418.     /* BINARY     user_type, create_params, auto_increment */
  419.     values             (3,    'length' ,0, 'binary')
  420.  
  421. insert into spt_datatype_info_ext
  422.     /* VARBINARY user_type, create_params, auto_increment */
  423.     values             (4,    'max length' ,0, 'varbinary')
  424.  
  425. if    (charindex('6.00', @@version) > 0 or
  426.      charindex('6.50', @@version) > 0 or
  427.      charindex('7.00', @@version) > 0 or
  428.      charindex('8.00', @@version) > 0)
  429. begin    /*    Add 6.0 data types */
  430.     insert into spt_datatype_info_ext
  431.         /* DECIMAL user_type, create_params, auto_increment */
  432.         values             (26,    'precision,scale' ,0, 'decimal')
  433.  
  434.     insert into spt_datatype_info_ext
  435.         /* NUMERIC user_type, create_params, auto_increment */
  436.         values             (25,    'precision,scale' ,0, 'numeric')
  437.  
  438.     insert into spt_datatype_info_ext
  439.         /* DECIMAL IDENTITY user_type, create_params, auto_increment */
  440.         values             (26,    'precision' ,1, 'decimal')
  441.  
  442.     insert into spt_datatype_info_ext
  443.         /* NUMERIC IDENTITY user_type, create_params, auto_increment */
  444.         values             (25,    'precision' ,1, 'numeric')
  445.  
  446. end
  447. else    /*    Pre 6.0 server, add SYSNAME create param */
  448.     begin
  449.         insert into spt_datatype_info_ext
  450.             /* SYSNAME     user_type, create_param, auto_increments */
  451.             values             (18,    'max length' ,0, 'sysname')
  452.  
  453.     end
  454. go
  455.  
  456. if (charindex('7.00', @@version) = 0 and
  457.     charindex('8.00', @@version) = 0)
  458. begin
  459.     print ''
  460.     print ''
  461.     print 'Warning:'
  462.     print 'you are installing the stored procedures '
  463.     print 'on a pre 8.0 SQL Server.'
  464.     print 'Ignore the following errors.'
  465. end
  466. go
  467. if (charindex('7.00', @@version) > 0 or
  468.     charindex('8.00', @@version) > 0)
  469. begin    /*    Update usertypes for 8.0 server */
  470.     begin tran
  471.     insert into spt_datatype_info_ext
  472.         /* NCHAR     user_type, create_params, auto_increment */
  473.         values             (0,    'length' ,0, 'nchar')
  474.  
  475.     insert into spt_datatype_info_ext
  476.         /* NVARCHAR     user_type, create_params, auto_increment */
  477.         values             (0,    'max length' ,0, 'nvarchar')
  478.  
  479.     /* SET user_type TO SPHINX VALUES */
  480.     update spt_datatype_info_ext set user_type = xusertype
  481.         from spt_datatype_info_ext e, systypes t where t.name = e.typename
  482.  
  483.     commit tran
  484. end
  485. go
  486.  
  487. create unique clustered index datatypeinfoextclust on spt_datatype_info_ext(user_type,AUTO_INCREMENT)
  488. go
  489.  
  490. print 'creating table spt_datatype_info'
  491. go
  492. if (charindex('6.00', @@version) = 0 and
  493.     charindex('6.50', @@version) = 0 and
  494.     charindex('7.00', @@version) = 0 and
  495.     charindex('8.00', @@version) = 0)
  496. begin    /*    Pre 6.0 server */
  497.     print ''
  498.     print ''
  499.     print 'Warning:'
  500.     print 'you are installing the stored procedures '
  501.     print 'on a pre 6.0 SQL Server.'
  502.     print 'Ignore the following error.'
  503.     create table spt_datatype_info (
  504.         ss_dtype            tinyint     not null,
  505.         fixlen                int         null,        /* datatype len for variable, else null */
  506.         ODBCVer             tinyint     null,        /* version if needed, else null */
  507.         TYPE_NAME            varchar(32)    not null,
  508.         DATA_TYPE            smallint    not null,
  509.         data_precision        int         null,
  510.         numeric_scale        smallint    null,    /* min scale if 6.0 */
  511.         RADIX                smallint    null,
  512.         length                int         null,
  513.         LITERAL_PREFIX        varchar(32) null,
  514.         LITERAL_SUFFIX        varchar(32) null,
  515.         CREATE_PARAMS        varchar(32) null,
  516.         NULLABLE            smallint    not null,
  517.         CASE_SENSITIVE        smallint    not null,
  518.         SEARCHABLE            smallint    not null,
  519.         UNSIGNED_ATTRIBUTE    smallint    null,
  520.         MONEY                smallint    not null,
  521.         AUTO_INCREMENT        smallint    null,
  522.         LOCAL_TYPE_NAME     varchar(32)    null,
  523.         charbin             tinyint     null, /* 0 for char/binary types, NULL for all others */
  524.         SQL_DATA_TYPE        smallint    not null,
  525.         SQL_DATETIME_SUB    smallint    null)
  526. end
  527. go
  528. if (charindex('6.00', @@version) > 0 or
  529.     charindex('6.50', @@version) > 0 or
  530.     charindex('7.00', @@version) > 0 or
  531.     charindex('8.00', @@version) > 0)
  532. begin    /* 6.0 or later server */
  533.     create table spt_datatype_info (
  534.         ss_dtype            tinyint     not null,
  535.         fixlen                int         null,        /* datatype len for variable, else null */
  536.         ODBCVer             tinyint     null,        /* version if needed, else null */
  537.         TYPE_NAME            sysname     not null,
  538.         DATA_TYPE            smallint    not null,
  539.         data_precision        int         null,
  540.         numeric_scale        smallint    null,    /* min scale if 6.0 */
  541.         RADIX                smallint    null,
  542.         length                int         null,
  543.         LITERAL_PREFIX        varchar(32) null,
  544.         LITERAL_SUFFIX        varchar(32) null,
  545.         CREATE_PARAMS        varchar(32) null,
  546.         NULLABLE            smallint    not null,
  547.         CASE_SENSITIVE        smallint    not null,
  548.         SEARCHABLE            smallint    not null,
  549.         UNSIGNED_ATTRIBUTE    smallint    null,
  550.         MONEY                smallint    not null,
  551.         AUTO_INCREMENT        smallint    null,
  552.         LOCAL_TYPE_NAME     sysname     null,
  553.         charbin             tinyint     null, /* 0 for char/binary types, NULL for all others */
  554.         SQL_DATA_TYPE        smallint    not null,
  555.         SQL_DATETIME_SUB    smallint    null)
  556. end
  557. go
  558.  
  559. grant select on spt_datatype_info to public
  560. go
  561.  
  562. /* Get case sensitivity */
  563. if 'A' = 'A' /* create dummy begin block */
  564. begin
  565.     declare @case smallint
  566.  
  567.     begin tran
  568.     select @case = 0
  569.     select @case = 1 where 'a' <> 'A'
  570.  
  571.     /* Local Binary */
  572.     insert into spt_datatype_info values
  573.     (45,null,null,'binary',-2,null,null,null,null,'0x',null,'length',1,0,2,null,0,null,'binary',0,-2,null)
  574.  
  575.     /* Local Bit */
  576.     insert into spt_datatype_info values
  577.     (50,null,null,'bit',-7,1,0,null,1,null,null,null,0,0,2,null,0,null,'bit',null,-7,null)
  578.  
  579.     /* Local Char */
  580.     insert into spt_datatype_info values
  581.     (47,null,null,'char',1,null,null,null,null,'''','''','length',1,@case,3,null,0,null,'char',0,1,null)
  582.  
  583.     /* Local Datetime */
  584.     insert into spt_datatype_info values
  585.     (61,8,2,'datetime',11,23,3,NULL,16,'''','''',null,1,0,3,null,0,null,'datetime',null,9,3)
  586.     insert into spt_datatype_info values
  587.     (61,8,3,'datetime',93,23,3,NULL,16,'''','''',null,1,0,3,null,0,null,'datetime',null,9,3)
  588.  
  589.     /* Local Smalldatetime */
  590.     insert into spt_datatype_info values
  591.     (58,4,2,'smalldatetime',11,16,0,NULL,16,'''','''',null,1,0,3,null,0,null,'smalldatetime',null,9,3)
  592.     insert into spt_datatype_info values
  593.     (58,4,3,'smalldatetime',93,16,0,NULL,16,'''','''',null,1,0,3,null,0,null,'smalldatetime',null,9,3)
  594.  
  595.     /* Local Float */
  596.     insert into spt_datatype_info values
  597.     (62,8,2,'float',6,15,null,10,8,null,null,null,1,0,2,0,0,0,'float',null,6,null)
  598.     insert into spt_datatype_info values
  599.     (62,8,3,'float',6,53,null, 2,8,null,null,null,1,0,2,0,0,0,'float',null,6,null)
  600.  
  601.     /* Local Real */
  602.     insert into spt_datatype_info values
  603.     (59,4,2,'real',7, 7,null,10,4,null,null,null,1,0,2,0,0,0,'real',null,7,null)
  604.     insert into spt_datatype_info values
  605.     (59,4,3,'real',7,24,null, 2,4,null,null,null,1,0,2,0,0,0,'real',null,7,null)
  606.  
  607.     /* Local Smallmoney */
  608.     insert into spt_datatype_info values
  609.     (122,4,null,'smallmoney',3,10,4,10,12,'$',null,null,1,0,2,0,1,0,'smallmoney',null,3,null)
  610.  
  611.     /* Local Money */
  612.     insert into spt_datatype_info values
  613.     (60,8,null,'money',3,19,4,10,21,'$',null,null,1,0,2,0,1,0,'money',null,3,null)
  614.  
  615.     /* Local Int */
  616.     insert into spt_datatype_info values
  617.     (56,4,null,'int',4,10,0,10,4,null,null,null,1,0,2,0,0,0,'int',null,4,null)
  618.  
  619.     commit tran
  620. end
  621. go
  622.  
  623. if 'A' = 'A' /* create dummy begin block */
  624. begin
  625.     declare @case smallint
  626.  
  627.     begin tran
  628.     select @case = 0
  629.     select @case = 1 where 'a' <> 'A'
  630.  
  631.  
  632.     /* Local Smallint */
  633.     insert into spt_datatype_info values
  634.     (52,2,null,'smallint',5,5,0,10,2,null,null,null,1,0,2,0,0,0,'smallint',null,5,null)
  635.     insert into spt_datatype_info values
  636.     (52,2,1,'smallint',5,5,0,10,2,null,null,null,1,0,2,0,0,0,'smallint',null,5,null)
  637.  
  638.     /* Local Tinyint */
  639.     insert into spt_datatype_info values
  640.     (48,1,null,'tinyint',-6,3,0,10,1,null,null,null,1,0,2,1,0,0,'tinyint',null,-6,null)
  641.  
  642.     /* Local Text */
  643.     insert into spt_datatype_info values
  644.     (35,null,null,'text',-1,2147483647,null,null,2147483647,'''','''',null,1,@case,1,null,0,null,'text',0,-1,null)
  645.  
  646.     /* Local Varbinary */
  647.     insert into spt_datatype_info values
  648.     (37,null,null,'varbinary',-3,null,null,null,null,'0x',null,'max length',1,0,2,null,0,null,'varbinary',0,-3,null)
  649.  
  650.     /* Local Varchar */
  651.     insert into spt_datatype_info values
  652.     (39,null,null,'varchar',12,null,null,null,null,'''','''','max length',1,@case,3,null,0,null,'varchar',0,12,null)
  653.  
  654.     /* Local Image */
  655.     insert into spt_datatype_info values
  656.     (34,null,null,'image',-4,2147483647,null,null,2147483647,'0x',null,null,1,0,0,null,0,null,'image',0,-4,null)
  657.  
  658.     if (charindex('6.00', @@version) > 0 or
  659.         charindex('6.50', @@version) > 0 or
  660.         charindex('7.00', @@version) > 0 or
  661.         charindex('8.00', @@version) > 0)
  662.     begin    /*    Add 6.0 data types */
  663.         /* Local Decimal */
  664.         insert into spt_datatype_info values    /* sql server type is 'decimaln' */
  665.         (55,null,null,'decimal',3,38,0,10,null,null,null,'precision,scale',1,0,2,0,0,0,'decimal',null,3,null)
  666.  
  667.         /* Local Numeric */
  668.         insert into spt_datatype_info values    /* sql server type is 'numericn' */
  669.         (63,null,null,'numeric',2    ,38,0,10,null,null,null,'precision,scale',1,0,2,0,0,0,'numeric',null,2,null)
  670.  
  671.         /* Identity attribute data types */
  672.  
  673.         /* Identity Int */
  674.         insert into spt_datatype_info values
  675.         (56,null,null,'int identity',4,10,0,10,null,null,null,null,0,0,2,0,0,1,'int identity',null,4,null)
  676.  
  677.         /* Identity Smallint */
  678.         insert into spt_datatype_info values
  679.         (52,null,null,'smallint identity',5,5,0,10,null,null,null,null,0,0,2,0,0,1,'smallint identity',null,5,null)
  680.  
  681.         /* Identity Tinyint */
  682.         insert into spt_datatype_info values
  683.         (48,null,null,'tinyint identity',-6,3,0,10,null,null,null,null,0,0,2,1,0,1,'tinyint identity',null,-6,null)
  684.  
  685.         /* Identity Numeric */
  686.         insert into spt_datatype_info values    /* sql server type is 'decmaln' */
  687.         (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)
  688.  
  689.         /* Identity Numeric */
  690.         insert into spt_datatype_info values    /* sql server type is 'decmaln' */
  691.         (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)
  692.  
  693.     end
  694.  
  695.     if (charindex('7.00', @@version) = 0 and
  696.         charindex('8.00', @@version) = 0)
  697.     begin    /*    Add nullable type for non-8.0 server */
  698.         /* Local Datetimn */
  699.         insert into spt_datatype_info values
  700.         (111,4,2,'smalldatetime',11,16,0,10,16,'''','''',null,1,0,3,null,0,null,'smalldatetime',null,9,3)
  701.         insert into spt_datatype_info values
  702.         (111,4,3,'smalldatetime',93,16,0,10,16,'''','''',null,1,0,3,null,0,null,'smalldatetime',null,9,3)
  703.         insert into spt_datatype_info values /* sql server type is 'datetimn' */
  704.         (111,8,2,'datetime',11,23,3,10,16,'''','''',null,1,0,3,null,0,null,'datetime',null,9,3)
  705.         insert into spt_datatype_info values
  706.         (111,8,3,'datetime',93,23,3,10,16,'''','''',null,1,0,3,null,0,null,'datetime',null,9,3)
  707.  
  708.         /* Local Floatn */
  709.         insert into spt_datatype_info values /* sql server type is 'floatn' */
  710.         (109,4,2,'real',7, 7,null,10,4,null,null,null,1,0,2,0,0,0,'real',null,7,null)
  711.         insert into spt_datatype_info values
  712.         (109,4,3,'real',7,24,null, 2,4,null,null,null,1,0,2,0,0,0,'real',null,7,null)
  713.         insert into spt_datatype_info values /* sql server type is 'floatn' */
  714.         (109,8,2,'float',6,15,null,10,8,null,null,null,1,0,2,0,0,0,'float',null,6,null)
  715.         insert into spt_datatype_info values
  716.         (109,8,3,'float',6,53,null, 2,8,null,null,null,1,0,2,0,0,0,'float',null,6,null)
  717.  
  718.         /* Local Moneyn */
  719.         insert into spt_datatype_info values    /* sql server type is 'moneyn' */
  720.         (110,4,null,'smallmoney',3,10,4,10,12,'$',null,null,1,0,2,0,1,0,'smallmoney',null,3,null)
  721.         insert into spt_datatype_info values    /* sql server type is 'moneyn' */
  722.         (110,8,null,'money',3,19,4,10,21,'$',null,null,1,0,2,0,1,0,'money',null,3,null)
  723.  
  724.         /* Local Intn */
  725.         insert into spt_datatype_info values /* sql server type is 'intn' */
  726.         (38,4,null,'int',4,10,0,10,4,null,null,null,1,0,2,0,0,0,'int',null,4,null)
  727.         insert into spt_datatype_info values /* sql server type is 'intn' */
  728.         (38,2,null,'smallint',5,5,0,10,2,null,null,null,1,0,2,0,0,0,'smallint',null,5,null)
  729.         insert into spt_datatype_info values
  730.         (38,1,null,'tinyint',-6,3,0,10,1,null,null,null,1,0,2,1,0,0,'tinyint',null,-6,null)
  731.  
  732.         if (charindex('6.00', @@version) > 0 or
  733.             charindex('6.50', @@version) > 0 or
  734.             charindex('7.00', @@version) > 0 or
  735.             charindex('8.00', @@version) > 0)
  736.         begin    /*    Add 6.0 data types */
  737.             /* Local Decimaln */
  738.             insert into spt_datatype_info values    /* sql server type is 'decimaln' */
  739.             (106,null,null,'decimal',3,38,0,10,null,null,null,'precision,scale',1,0,2,0,0,0,'decimal',null,3,null)
  740.             insert into spt_datatype_info values    /* sql server type is 'decmaln' */
  741.             (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)
  742.  
  743.             /* Local Numericn */
  744.             insert into spt_datatype_info values    /* sql server type is 'numericn' */
  745.             (108,null,null,'numeric',2,38,0,10,null,null,null,'precision,scale',1,0,2,0,0,0,'numeric',null,2,null)
  746.             insert into spt_datatype_info values    /* sql server type is 'decmaln' */
  747.             (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)
  748.         end
  749.     end
  750.  
  751.     commit tran
  752. end
  753. go
  754.  
  755. if (charindex('7.00', @@version) = 0 and
  756.     charindex('8.00', @@version) = 0)
  757. begin
  758.     print ''
  759.     print ''
  760.     print 'Warning:'
  761.     print 'you are installing the stored procedures '
  762.     print 'on a pre 8.0 SQL Server.'
  763.     print 'Ignore the following errors.'
  764. end
  765. go
  766. if (charindex('7.00', @@version) > 0 or
  767.     charindex('8.00', @@version) > 0)
  768.     begin
  769.         declare @ncase smallint
  770.  
  771.         select @ncase = 0
  772.         select @ncase = 1 where N'a' <> N'A'
  773.  
  774.         /* Local Timestamp */
  775.         insert into spt_datatype_info values
  776.         (0,null,null,'timestamp',-2,8,null,null,null,'0x',null,null,1,0,2,null,0,null,'timestamp',0,-2,null)
  777.  
  778.         /* Local GUID */
  779.         insert into spt_datatype_info values
  780.         (0,null,null,'uniqueidentifier',-11,36,null,null,null,'''','''',null,1,0,2,null,0,null,'uniqueidentifier',NULL,-11,null)
  781.  
  782.         /* Local NChar */
  783.         insert into spt_datatype_info values
  784.         (0,null,null,'nchar',-8,null,null,null,null,'N''','''','length',1,@ncase,3,null,0,null,'nchar',0,-8,null)
  785.  
  786.         /* Local NVarchar */
  787.         insert into spt_datatype_info values
  788.         (0,null,null,'nvarchar',-9,null,null,null,null,'N''','''','max length',1,@ncase,3,null,0,null,'nvarchar',0,-9,null)
  789.  
  790.         /* Local NText */
  791.         insert into spt_datatype_info values
  792.         (0,null,null,'ntext',-10,2147483646,null,null,2147483646,'N''','''',null,1,@ncase,1,null,0,null,'ntext',0,-10,null)
  793.  
  794.         if (charindex('8.00', @@version) > 0)
  795.         begin
  796. --            /* Local BIGINT */
  797.             insert into spt_datatype_info values
  798.             (127,8,null,'bigint',-5,19,0,10,null,null,null,null,1,0,2,0,0,0,'bigint',null,-5,null)
  799. --            /* Identity BIGINT */
  800.             insert into spt_datatype_info values
  801.             (127,8,null,'bigint identity',-5,19,0,10,null,null,null,null,0,0,2,0,0,1,'bigint identity',null,-5,null)
  802.  
  803.  
  804. --            /* sql_variant */
  805.             insert into spt_datatype_info values
  806.             (
  807.             98,                    --ss_dtype        
  808.             null,                --fixlen
  809.             null,                --ODBCVer
  810.             'sql_variant',        --TYPE_NAME
  811.             -150,                --SQL DATA TYPE
  812.             8000,                --data_precision
  813.             0,                    --numeric_scale
  814.             10,                    --RADIX
  815.             8000,                --length
  816.             null,                --PREFIX
  817.             null,                --SUFFIX
  818.             null,                --Create Params
  819.             1,                    --Nullable
  820.             0,                --Case sensitive
  821.             2,                    --Searchable
  822.             null,                --UNSIGNED_ATTRIBUTE
  823.             0,                --MONEY
  824.             null,                --AUTO_INCREMENT
  825.             'sql_variant',        --LOCAL TYPE NAME
  826.             0,                    --CHARBIN
  827.             -150,                --SQL_DATA_TYPE
  828.             null                --SQL_DATETIME_SUB
  829.             )
  830.         
  831.         end
  832.  
  833.  
  834.  
  835.         update spt_datatype_info set NULLABLE = 1
  836.             where TYPE_NAME = 'bit'
  837.  
  838.         update spt_datatype_info set ss_dtype = isnull((select distinct xtype from systypes
  839.             where TYPE_NAME like name+'%'),0)
  840.     end
  841. go
  842.  
  843. create unique clustered index datatypeinfoclust on spt_datatype_info(ss_dtype,fixlen,ODBCVer,AUTO_INCREMENT)
  844. go
  845.  
  846. dump tran master with no_log
  847. go
  848.  
  849. print 'creating table spt_server_info'
  850. go
  851. create table spt_server_info (
  852.               attribute_id        int NOT NULL,
  853.               attribute_name    varchar(60) NOT NULL,
  854.               attribute_value    varchar(255) NOT NULL)
  855. go
  856.  
  857. create unique clustered index serverinfoclust on spt_server_info(attribute_id)
  858. go
  859.  
  860. if (charindex('7.00', @@version) = 0 and charindex('8.00', @@version) = 0)
  861.     begin
  862.         drop procedure sp_add_server_sortinfo    /* not used by other servers */
  863.         drop procedure sp_add_server_sortinfo75    /* not used by older servers */
  864.         dump tran master with no_log
  865.     end
  866. go
  867.  
  868.  
  869. insert into spt_server_info
  870.     values (1,'DBMS_NAME','Microsoft SQL Server')
  871. insert into spt_server_info
  872.     values (2,'DBMS_VER',@@version)
  873. insert into spt_server_info
  874.     values (10,'OWNER_TERM','owner')
  875. insert into spt_server_info
  876.     values (11,'TABLE_TERM','table')
  877. insert into spt_server_info
  878.     values (12,'MAX_OWNER_NAME_LENGTH','30')
  879. insert into spt_server_info
  880.     values (13,'TABLE_LENGTH','30')
  881. insert into spt_server_info
  882.     values (14,'MAX_QUAL_LENGTH','30')
  883. insert into spt_server_info
  884.     values (15,'COLUMN_LENGTH','30')
  885. if 'A' = 'a' /* If not case sensitive server */
  886. begin
  887.     insert into spt_server_info
  888.         values (16,'IDENTIFIER_CASE','MIXED')
  889. end
  890. else
  891. begin
  892.     insert into spt_server_info
  893.         values (16,'IDENTIFIER_CASE','SENSITIVE')
  894. end
  895. insert into spt_server_info
  896.     values (17,'TX_ISOLATION','2')
  897. if (charindex('6.00', @@version) > 0 or
  898.     charindex('6.50', @@version) > 0 or
  899.     charindex('7.00', @@version) > 0) 
  900. begin    /*    Add 6.0 collation sequence */
  901.     insert into spt_server_info
  902.         select 18,'COLLATION_SEQ',
  903.             'charset='+t2.name+' sort_order='+t1.name
  904.             +' charset_num='+rtrim(convert(char(4),t1.csid))+
  905.             ' sort_order_num='+rtrim(convert(char(4),t1.id))
  906.         from syscharsets t1, syscharsets t2, sysconfigures t3
  907.         where t1.csid=t2.id and t1.id=t3.value and t3.config=1123
  908. end
  909. else 
  910. begin    /*    Add 4.2x collation sequence */
  911.     insert into spt_server_info
  912.         select 18,'COLLATION_SEQ',
  913.             'charset='+t2.name+' sort_order='+t1.name
  914.             +' charset_num='+rtrim(convert(char(4),t1.csid))+
  915.             ' sort_order_num='+rtrim(convert(char(4),t1.id))
  916.         from syscharsets t1, syscharsets t2, sysconfigures t3
  917.         where t1.csid=t2.id and t1.id=t3.value and t3.config=123
  918. end
  919. insert into spt_server_info
  920.     values (19,'SAVEPOINT_SUPPORT','Y')
  921. insert into spt_server_info
  922.     values (20,'MULTI_RESULT_SETS','Y')
  923. insert into spt_server_info
  924.     values (22,'ACCESSIBLE_TABLES','Y')
  925. go
  926.  
  927. insert into spt_server_info
  928.     values (100,'USERID_LENGTH','30')
  929. insert into spt_server_info
  930.     values (101,'QUALIFIER_TERM','database')
  931. insert into spt_server_info
  932.     values (102,'NAMED_TRANSACTIONS','Y')
  933. insert into spt_server_info
  934.     values (103,'SPROC_AS_LANGUAGE','Y')
  935. insert into spt_server_info
  936.     values (104,'ACCESSIBLE_SPROC','Y')
  937. insert into spt_server_info
  938.     values (105,'MAX_INDEX_COLS','16')
  939. insert into spt_server_info
  940.     values (106,'RENAME_TABLE','Y')
  941. insert into spt_server_info
  942.     values (107,'RENAME_COLUMN','Y')
  943.  
  944. if (charindex('8.00', @@version) > 0)
  945. begin
  946.     /* Columns may be dropped on 8.0 or later */
  947.     insert into spt_server_info
  948.         values (108,'DROP_COLUMN','Y')
  949. end
  950. else
  951. begin
  952.     insert into spt_server_info
  953.         values (108,'DROP_COLUMN','N')
  954. end
  955.  
  956. if (charindex('8.00', @@version) > 0)
  957. begin
  958.     /* Columns size may be changed on 8.0 or later */
  959.     insert into spt_server_info
  960.         values (109,'INCREASE_COLUMN_LENGTH','Y')
  961. end
  962. else
  963. begin
  964.     insert into spt_server_info
  965.         values (109,'INCREASE_COLUMN_LENGTH','N')
  966. end
  967.  
  968. if (charindex('6.50', @@version) = 0 and
  969.     charindex('7.00', @@version) = 0 and
  970.     charindex('8.00', @@version) = 0)
  971. begin
  972.     insert into spt_server_info
  973.         values (110,'DDL_IN_TRANSACTION','N')
  974. end
  975. else
  976. begin
  977.     insert into spt_server_info
  978.         values (110,'DDL_IN_TRANSACTION','Y')
  979. end
  980.  
  981.  
  982. if (charindex('8.00', @@version) > 0)
  983. begin
  984.     /* Descending indexes allowed on 8.0 or later */
  985.     insert into spt_server_info
  986.         values (111,'DESCENDING_INDEXES','Y')
  987. end
  988. else
  989. begin
  990.     insert into spt_server_info
  991.         values (111,'DESCENDING_INDEXES','N')
  992. end
  993.  
  994. insert into spt_server_info
  995.     values (112,'SP_RENAME','Y')
  996. insert into spt_server_info
  997.     values (113,'REMOTE_SPROC','Y')
  998. insert into spt_server_info
  999.     values (500,'SYS_SPROC_VERSION','8.00.178')
  1000. go
  1001.  
  1002. if (charindex('7.00', @@version) > 0 or
  1003.     charindex('8.00', @@version) > 0)
  1004. begin    /*    Update values for 8.0 server */
  1005.     update spt_server_info set attribute_value = '128'
  1006.         where attribute_id in (12,13,14,15,100)
  1007. end
  1008. go
  1009.  
  1010. grant select on spt_server_info to public
  1011. go
  1012.  
  1013. print 'creating sp_column_privileges'
  1014. go
  1015.  
  1016. /*    Procedure for pre 6.50 server */
  1017. CREATE PROCEDURE sp_column_privileges (
  1018.             @table_name         varchar(32),
  1019.             @table_owner        varchar(32) = null,
  1020.             @table_qualifier    varchar(32) = null,
  1021.             @column_name        varchar(96) = null)    /* 3*32 */
  1022. as
  1023.  
  1024.     set nocount on
  1025.     declare @table_id     int
  1026.     DECLARE @full_table_name    varchar(65)    /* 2*32 + 1 */
  1027.     declare @low smallint                     /* range of userids to check */
  1028.     declare @high smallint
  1029.     declare @owner_uid smallint
  1030.  
  1031.     select @low = 0, @high = 32767
  1032.  
  1033.     if @column_name is null /*    If column name not supplied, match all */
  1034.         select @column_name = '%'
  1035.  
  1036.     if @table_qualifier is not null
  1037.     begin
  1038.         if db_name() <> @table_qualifier
  1039.         begin    /* If qualifier doesn't match current database */
  1040.             raiserror 20001 '~~Rush_5~~'
  1041.             return
  1042.         end
  1043.     end
  1044.     if @table_owner is null
  1045.     begin    /* If unqualified table name */
  1046.         SELECT @full_table_name = @table_name
  1047.     end
  1048.     else
  1049.     begin    /* Qualified table name */
  1050.         SELECT @full_table_name = @table_owner + '.' + @table_name
  1051.     end
  1052.     /*    Get Object ID */
  1053.     select @table_id = object_id(@full_table_name)
  1054.  
  1055.     if (@@trancount <> 0)
  1056.     begin    /* If inside a transaction */
  1057.         raiserror 20003 '~~Rush_6~~'
  1058.         return
  1059.     end
  1060.  
  1061.     /*
  1062.     ** We need to create a table which will contain a row for every row to
  1063.     ** be returned to the client.
  1064.     */
  1065.  
  1066.     create table #column_priv1(
  1067.         COLUMN_NAME             varchar(32) NOT NULL,
  1068.         grantor                 smallint NOT NULL,
  1069.         grantee                 smallint NOT NULL,
  1070.         select_privilege        bit,
  1071.         select_grantable        bit,
  1072.         insert_privilege        bit,
  1073.         insert_grantable        bit,
  1074.         update_privilege        bit,
  1075.         update_grantable        bit,
  1076.         references_privilege    bit,
  1077.         references_grantable    bit,
  1078.         uid                     smallint NOT NULL,
  1079.         gid                     smallint NOT NULL)
  1080.  
  1081. /*
  1082. ** insert a row for the table owner (who has all permissions)
  1083. */
  1084.     select @owner_uid = (
  1085.         select uid
  1086.         from sysobjects
  1087.         where id = @table_id)
  1088.  
  1089.     if (charindex('6.00', @@version) > 0)
  1090.     begin
  1091.         insert into #column_priv1
  1092.             select
  1093.                 c.name,
  1094.                 u.uid,
  1095.                 @owner_uid,
  1096.                 0,
  1097.                 1,
  1098.                 0,
  1099.                 1,
  1100.                 0,
  1101.                 1,
  1102.                 0,
  1103.                 1,
  1104.                 @owner_uid,
  1105.                 0
  1106.             from syscolumns c, sysusers u
  1107.             where id = @table_id
  1108.                 and c.number = 0
  1109.                 and u.uid = 1        /* grantor is dbo of database */
  1110.     end
  1111.     else
  1112.     begin
  1113.         insert into #column_priv1
  1114.             select
  1115.                 c.name,
  1116.                 u.uid,
  1117.                 @owner_uid,
  1118.                 0,
  1119.                 1,
  1120.                 0,
  1121.                 1,
  1122.                 0,
  1123.                 1,
  1124.                 0,
  1125.                 0,
  1126.                 @owner_uid,
  1127.                 0
  1128.             from syscolumns c, sysusers u
  1129.             where id = @table_id
  1130.                 and c.number = 0
  1131.                 and u.uid = 1        /* grantor is dbo of database */
  1132.     end
  1133. /*
  1134. ** now stick in a row for every column for every user in the database
  1135. ** we will need to weed out those who have no permissions later
  1136. ** (and yes this is a cartesion product: the uid field in sysprotects
  1137. ** can also have a group id, in which case we need to extend those
  1138. ** privileges to all group members).
  1139. */
  1140.  
  1141.     insert into #column_priv1
  1142.         select distinct
  1143.             c.name,
  1144.             o.uid,
  1145.             u.uid,
  1146.             0,
  1147.             0,
  1148.             0,
  1149.             0,
  1150.             0,
  1151.             0,
  1152.             0,
  1153.             0,
  1154.             u.uid,
  1155.             u.gid
  1156.         from sysusers u, syscolumns c, sysobjects o
  1157.         where o.id = @table_id
  1158.             and c.id = o.id
  1159.             and c.number = 0
  1160.             and u.gid <> u.uid
  1161.             and u.uid <> @owner_uid
  1162.  
  1163.     /*
  1164.     ** we need to create another temporary table to contain all the various
  1165.     ** protection information for the table in question
  1166.     */
  1167.     create table #protects (
  1168.                 uid         smallint NOT NULL,
  1169.                 grantor        smallint NOT NULL,
  1170.                 action        tinyint NOT NULL,
  1171.                 protecttype tinyint NOT NULL,
  1172.                 name        varchar(32) NOT NULL)
  1173.  
  1174.     insert into #protects
  1175.         select
  1176.             p.uid,
  1177.             p.uid,
  1178.             p.action,
  1179.             p.protecttype,
  1180.             isnull(col_name(id, c.number), '~All')
  1181.             from
  1182.                 sysprotects p,
  1183.                 master.dbo.spt_values c,
  1184.                 master.dbo.spt_values a,
  1185.                 master.dbo.spt_values b
  1186.             where
  1187.                 convert(tinyint, substring(isnull(p.columns, 0x1), c.low, 1))
  1188.                     & c.high <> 0
  1189.                     and c.number <= (
  1190.                         select count(*)
  1191.                         from syscolumns
  1192.                         where id = @table_id)
  1193.                 and c.type = 'P'
  1194.                 and a.type = 'T'
  1195.                 and a.number = p.action
  1196.                 and p.action in (193,195,197,26)
  1197.                 and b.type = 'T'
  1198.                 and b.number = p.protecttype
  1199.                 and p.id = @table_id
  1200.                 and p.uid between @low and @high
  1201.  
  1202.  
  1203.     update #column_priv1
  1204.     set select_privilege = 1
  1205.         from #protects p
  1206.     where
  1207.         p.protecttype = 205
  1208.         and p.action = 193
  1209.         and (p.name = #column_priv1.COLUMN_NAME
  1210.             or name = '~All')
  1211.         and (p.uid = 0
  1212.             or p.uid = #column_priv1.gid
  1213.             or p.uid = #column_priv1.uid)
  1214.         and not exists (
  1215.             select * from #protects
  1216.             where
  1217.                 protecttype = 206
  1218.                 and action = 193
  1219.                 and (name = #column_priv1.COLUMN_NAME
  1220.                     or name = '~All')
  1221.                 and ( uid = 0
  1222.                     or uid = #column_priv1.gid
  1223.                     or uid = #column_priv1.uid))
  1224.  
  1225.     update #column_priv1
  1226.     set insert_privilege = 1
  1227.         from #protects p
  1228.     where
  1229.         p.protecttype = 205
  1230.         and p.action = 195
  1231.         and (p.name = #column_priv1.COLUMN_NAME
  1232.             or name = '~All')
  1233.         and (p.uid = 0
  1234.             or p.uid = #column_priv1.gid
  1235.             or p.uid = #column_priv1.uid)
  1236.         and not exists (
  1237.             select * from #protects
  1238.             where
  1239.                 protecttype = 206
  1240.                 and action = 195
  1241.                 and (name = #column_priv1.COLUMN_NAME
  1242.                        or name = '~All')
  1243.                 and (uid = 0
  1244.                     or uid = #column_priv1.gid
  1245.                     or uid = #column_priv1.uid))
  1246.  
  1247.     update #column_priv1
  1248.     set update_privilege = 1
  1249.         from #protects p
  1250.     where
  1251.         p.protecttype = 205
  1252.         and p.action = 197
  1253.         and (p.name = #column_priv1.COLUMN_NAME
  1254.             or name = '~All')
  1255.         and (p.uid = 0
  1256.             or p.uid = #column_priv1.gid
  1257.             or p.uid = #column_priv1.uid)
  1258.         and not exists (
  1259.             select * from #protects
  1260.                 where protecttype = 206
  1261.                 and action = 197
  1262.                 and (name = #column_priv1.COLUMN_NAME
  1263.                     or name = '~All')
  1264.                 and (uid = 0
  1265.                     or uid = #column_priv1.gid
  1266.                     or uid = #column_priv1.uid))
  1267.  
  1268.     update #column_priv1
  1269.     set references_privilege = 1
  1270.         from #protects p
  1271.     where
  1272.         p.protecttype = 205
  1273.         and p.action = 26
  1274.         and (p.name = #column_priv1.COLUMN_NAME
  1275.             or name = '~All')
  1276.         and (p.uid = 0
  1277.             or p.uid = #column_priv1.gid
  1278.             or p.uid = #column_priv1.uid)
  1279.         and not exists (
  1280.             select * from #protects
  1281.                 where protecttype = 206
  1282.                 and action = 26
  1283.                 and (name = #column_priv1.COLUMN_NAME
  1284.                     or name = '~All')
  1285.                 and (uid = 0
  1286.                     or uid = #column_priv1.gid
  1287.                     or uid = #column_priv1.uid))
  1288.  
  1289.     update #column_priv1
  1290.     set select_grantable = 1
  1291.         from #protects p
  1292.     where
  1293.         p.protecttype = 204
  1294.         and p.action = 193
  1295.         and (p.name = #column_priv1.COLUMN_NAME
  1296.             or name = '~All')
  1297.         and (p.uid = 0
  1298.             or p.uid = #column_priv1.gid
  1299.             or p.uid = #column_priv1.uid)
  1300.         and not exists (
  1301.             select * from #protects
  1302.             where
  1303.                 protecttype = 206
  1304.                 and action = 193
  1305.                 and (name = #column_priv1.COLUMN_NAME
  1306.                     or name = '~All')
  1307.                 and ( uid = 0
  1308.                     or uid = #column_priv1.gid
  1309.                     or uid = #column_priv1.uid))
  1310.  
  1311.     update #column_priv1
  1312.     set insert_grantable = 1
  1313.         from #protects p
  1314.     where
  1315.         p.protecttype = 204
  1316.         and p.action = 195
  1317.         and (p.name = #column_priv1.COLUMN_NAME
  1318.             or name = '~All')
  1319.         and (p.uid = 0
  1320.             or p.uid = #column_priv1.gid
  1321.             or p.uid = #column_priv1.uid)
  1322.         and not exists (
  1323.             select * from #protects
  1324.             where
  1325.                 protecttype = 206
  1326.                 and action = 195
  1327.                 and (name = #column_priv1.COLUMN_NAME
  1328.                     or name = '~All')
  1329.                 and ( uid = 0
  1330.                     or uid = #column_priv1.gid
  1331.                     or uid = #column_priv1.uid))
  1332.  
  1333.     update #column_priv1
  1334.     set update_grantable = 1
  1335.         from #protects p
  1336.     where
  1337.         p.protecttype = 204
  1338.         and p.action = 197
  1339.         and (p.name = #column_priv1.COLUMN_NAME
  1340.             or name = '~All')
  1341.         and (p.uid = 0
  1342.             or p.uid = #column_priv1.gid
  1343.             or p.uid = #column_priv1.uid)
  1344.         and not exists (
  1345.             select * from #protects
  1346.             where
  1347.                 protecttype = 206
  1348.                 and action = 197
  1349.                 and (name = #column_priv1.COLUMN_NAME
  1350.                     or name = '~All')
  1351.                 and ( uid = 0
  1352.                     or uid = #column_priv1.gid
  1353.                     or uid = #column_priv1.uid))
  1354.  
  1355.     update #column_priv1
  1356.     set references_grantable = 1
  1357.         from #protects p
  1358.     where
  1359.         p.protecttype = 204
  1360.         and p.action = 26
  1361.         and (p.name = #column_priv1.COLUMN_NAME
  1362.             or name = '~All')
  1363.         and (p.uid = 0
  1364.             or p.uid = #column_priv1.gid
  1365.             or p.uid = #column_priv1.uid)
  1366.         and not exists (
  1367.             select * from #protects
  1368.             where
  1369.                 protecttype = 206
  1370.                 and action = 26
  1371.                 and (name = #column_priv1.COLUMN_NAME
  1372.                     or name = '~All')
  1373.                 and ( uid = 0
  1374.                     or uid = #column_priv1.gid
  1375.                     or uid = #column_priv1.uid))
  1376.  
  1377.     create table #column_priv2(
  1378.         COLUMN_NAME     varchar(32) NOT NULL,
  1379.         grantor         smallint NULL,
  1380.         grantee         smallint NOT NULL,
  1381.         PRIVILEGE        varchar(32) NOT NULL,
  1382.         IS_GRANTABLE    varchar(3) NULL)
  1383.  
  1384.     insert into #column_priv2
  1385.         select
  1386.             COLUMN_NAME,
  1387.             grantor,
  1388.             grantee,
  1389.             'SELECT',
  1390.             'NO'
  1391.         from #column_priv1
  1392.         where select_privilege = 1 and select_grantable    = 0
  1393.  
  1394.     insert into #column_priv2
  1395.         select
  1396.             COLUMN_NAME,
  1397.             grantor,
  1398.             grantee,
  1399.             'INSERT',
  1400.             'NO'
  1401.         from #column_priv1
  1402.         where insert_privilege = 1 and insert_grantable = 0
  1403.  
  1404.     insert into #column_priv2
  1405.         select
  1406.             COLUMN_NAME,
  1407.             grantor,
  1408.             grantee,
  1409.             'UPDATE',
  1410.             'NO'
  1411.         from #column_priv1
  1412.         where update_privilege = 1 and update_grantable = 0
  1413.  
  1414.     insert into #column_priv2
  1415.         select
  1416.             COLUMN_NAME,
  1417.             grantor,
  1418.             grantee,
  1419.             'REFERENCES',
  1420.             'NO'
  1421.         from #column_priv1
  1422.         where references_privilege = 1 and references_grantable = 0
  1423.  
  1424.     insert into #column_priv2
  1425.         select
  1426.             COLUMN_NAME,
  1427.             grantor,
  1428.             grantee,
  1429.             'SELECT',
  1430.             'YES'
  1431.         from #column_priv1
  1432.         where select_grantable = 1
  1433.  
  1434.     insert into #column_priv2
  1435.         select
  1436.             COLUMN_NAME,
  1437.             grantor,
  1438.             grantee,
  1439.             'INSERT',
  1440.             'YES'
  1441.         from #column_priv1
  1442.         where insert_grantable = 1
  1443.  
  1444.     insert into #column_priv2
  1445.         select
  1446.             COLUMN_NAME,
  1447.             grantor,
  1448.             grantee,
  1449.             'UPDATE',
  1450.             'YES'
  1451.         from #column_priv1
  1452.         where update_grantable = 1
  1453.  
  1454.     insert into #column_priv2
  1455.         select
  1456.             COLUMN_NAME,
  1457.             grantor,
  1458.             grantee,
  1459.             'REFERENCES',
  1460.             'YES'
  1461.         from #column_priv1
  1462.         where references_grantable = 1
  1463.  
  1464.     select
  1465.         convert(varchar(32),db_name()) TABLE_QUALIFIER,
  1466.         convert(varchar(32),user_name(@owner_uid)) TABLE_OWNER,
  1467.         @table_name TABLE_NAME,
  1468.         COLUMN_NAME,
  1469.         convert(varchar(32),user_name(grantor)) GRANTOR,
  1470.         convert(varchar(32),user_name(grantee)) GRANTEE,
  1471.         PRIVILEGE,
  1472.         IS_GRANTABLE
  1473.     from #column_priv2
  1474.     where COLUMN_NAME like @column_name
  1475.     order by 4, 7
  1476. go
  1477.  
  1478. if (charindex('6.50', @@version) = 0 and
  1479.     charindex('7.00', @@version) = 0 and
  1480.     charindex('8.00', @@version) = 0)
  1481. begin
  1482.     print ''
  1483.     print ''
  1484.     print 'Warning:'
  1485.     print 'you are installing the stored procedures '
  1486.     print 'on a pre 6.50 SQL Server.'
  1487.     print 'Ignore the following errors.'
  1488. end
  1489. else
  1490.     drop proc sp_column_privileges
  1491. go
  1492.  
  1493. /*    Procedure for 6.50 server */
  1494. CREATE PROCEDURE sp_column_privileges (
  1495.             @table_name         varchar(32),
  1496.             @table_owner        varchar(32) = null,
  1497.             @table_qualifier    varchar(32) = null,
  1498.             @column_name        varchar(96) = null)    /* 3*32 */
  1499. as
  1500.  
  1501.     declare @table_id     int
  1502.  
  1503.     if @column_name is null /*    If column name not supplied, match all */
  1504.         select @column_name = '%'
  1505.  
  1506.     if @table_qualifier is not null
  1507.     begin
  1508.         if db_name() <> @table_qualifier
  1509.         begin    /* If qualifier doesn't match current database */
  1510.             raiserror (15250, -1,-1)
  1511.             return
  1512.         end
  1513.     end
  1514.     if @table_owner is null
  1515.     begin    /* If unqualified table name */
  1516.         select @table_id = object_id(@table_name)
  1517.     end
  1518.     else
  1519.     begin    /* Qualified table name */
  1520.         select @table_id = object_id(@table_owner + '.' + @table_name)
  1521.     end
  1522.  
  1523.     select
  1524.         convert(varchar(32),db_name()) TABLE_QUALIFIER,
  1525.         convert(varchar(32),user_name(o.uid)) TABLE_OWNER,
  1526.         @table_name TABLE_NAME,
  1527.         convert(varchar(32),c.name) COLUMN_NAME,
  1528.         convert(varchar(32),user_name(p.grantor)) GRANTOR,
  1529.         convert(varchar(32),user_name(u.uid)) GRANTEE,
  1530.         convert (varchar(32),case p.action
  1531.              when 193 then 'SELECT'
  1532.              when 195 then 'INSERT'
  1533.              when 197 then 'UPDATE'
  1534.              else 'REFERENCES'
  1535.         end) PRIVILEGE,
  1536.         convert (varchar(3),case when p.protecttype = 205 then 'NO'
  1537.             else 'YES'
  1538.         end) IS_GRANTABLE
  1539.     from sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c
  1540.     where
  1541.         c.id = @table_id
  1542.         and c.name like @column_name
  1543.         and c.id = p.id
  1544.         and c.id = o.id
  1545.         and case substring(p.columns, 1, 1) & 1
  1546.                 when NULL then 255    /* all columns have permission */
  1547.                 when 0 then convert(tinyint, substring(p.columns, v.low, 1))
  1548.                 else (~convert(tinyint, isnull(substring(p.columns, v.low, 1),0)))
  1549.             end
  1550.             & v.high <> 0            /* permission applies to this column */
  1551.         and v.number <= (select count(*) from syscolumns
  1552.             where id = @table_id)    /* ranges from 1 to # of columns in table */
  1553.         and v.type = 'P'
  1554.         and v.number = c.colid
  1555.             /* expand groups */
  1556.         and ((p.uid = u.uid and u.uid <> u.gid) or
  1557.              (p.uid = u.gid and u.uid <> u.gid))
  1558.         and p.protecttype <> 206    /* only grant rows */
  1559.         and p.action in (26,193,195,197)
  1560.         and o.uid <> u.uid            /* no rows for owner */
  1561.         and not exists (            /* exclude revoke'd privileges */
  1562.             select *
  1563.             from sysprotects p1
  1564.             where
  1565.                 p1.protecttype = 206
  1566.                 and p1.action = p.action
  1567.                 and p1.id = p.id
  1568.                 and p1.uid = u.uid
  1569.                 and case substring(p1.columns, 1, 1) & 1
  1570.                         when NULL then 255    /* all columns have permission */
  1571.                         when 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  1572.                                             else (~convert(tinyint,isnull(substring(p.columns, v.low, 1),0)))
  1573.                     end
  1574.                     & v.high <> 0)            /* permission applies to this column */
  1575.     union
  1576.     select    /*    Add rows for table owner */
  1577.         convert(varchar(32),db_name()) TABLE_QUALIFIER,
  1578.         convert(varchar(32),user_name(o.uid)) TABLE_OWNER,
  1579.         @table_name TABLE_NAME,
  1580.         convert(varchar(32),col_name(@table_id, c.colid)) COLUMN_NAME,
  1581.         convert(varchar(32),user_name(u.uid)) grantor,
  1582.         convert(varchar(32),user_name(o.uid)) grantee,
  1583.         convert (varchar(32),case v.number
  1584.             when 193 then 'SELECT'
  1585.             when 195 then 'INSERT'
  1586.             when 197 then 'UPDATE'
  1587.             else 'REFERENCES'
  1588.         end) PRIVILEGE,
  1589.         convert(varchar(3),'YES') IS_GRANTABLE
  1590.     from sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  1591.     where
  1592.         c.id = @table_id
  1593.         and c.name like @column_name
  1594.         and c.id = o.id
  1595.         and u.uid = 1        /* grantor is dbo of database */
  1596.         and v.type = 'P'    /* cross product to get all exposed privileges */
  1597.         and v.number in (26,193,195,197)
  1598.         and not exists (    /* exclude revoke'd privileges */
  1599.             select *
  1600.             from sysprotects p1
  1601.             where
  1602.                 p1.protecttype = 206
  1603.                 and p1.action = v.number
  1604.                 and p1.id = o.id
  1605.                 and p1.uid = o.uid)
  1606.     order by 4, 7
  1607. go
  1608.  
  1609.  
  1610.  
  1611. if (charindex('7.00', @@version) = 0 and
  1612.     charindex('8.00', @@version) = 0)
  1613. begin
  1614.     print ''
  1615.     print ''
  1616.     print 'Warning:'
  1617.     print 'you are installing the stored procedures '
  1618.     print 'on a pre 8.0 SQL Server.'
  1619.     print 'Ignore the following errors.'
  1620. end
  1621. else
  1622.     drop proc sp_column_privileges
  1623. go
  1624.  
  1625. /*    Procedure for 8.0 server */
  1626. CREATE PROCEDURE sp_column_privileges (
  1627.             @table_name         sysname,
  1628.             @table_owner        sysname = null,
  1629.             @table_qualifier    sysname = null,
  1630.             @column_name        nvarchar(384) = null)    /* 3*128 */
  1631. as
  1632.  
  1633.     declare @table_id     int
  1634.  
  1635.     if @column_name is null /*    If column name not supplied, match all */
  1636.         select @column_name = '%'
  1637.  
  1638.     if @table_qualifier is not null
  1639.     begin
  1640.         if db_name() <> @table_qualifier
  1641.         begin    /* If qualifier doesn't match current database */
  1642.             raiserror (15250, -1,-1)
  1643.             return
  1644.         end
  1645.     end
  1646.     if @table_owner is null
  1647.     begin    /* If unqualified table name */
  1648.         select @table_id = object_id(quotename(@table_name))
  1649.     end
  1650.     else
  1651.     begin    /* Qualified table name */
  1652.         if @table_owner = N''
  1653.         begin    /* If empty owner name */
  1654.             select @table_id = 0
  1655.         end
  1656.         else
  1657.         begin
  1658.             select @table_id = object_id(quotename(@table_owner) +
  1659.                 '.' + quotename(@table_name))
  1660.         end
  1661.     end
  1662.  
  1663.     select
  1664.         convert(sysname,db_name()) TABLE_QUALIFIER,
  1665.         convert(sysname,user_name(o.uid)) TABLE_OWNER,
  1666.         @table_name TABLE_NAME,
  1667.         convert(sysname,c.name) COLUMN_NAME,
  1668.         convert(sysname,user_name(p.grantor)) GRANTOR,
  1669.         convert(sysname,user_name(u.uid)) GRANTEE,
  1670.         convert (varchar(32),case p.action
  1671.              when 193 then 'SELECT'
  1672.              when 195 then 'INSERT'
  1673.              when 197 then 'UPDATE'
  1674.              else 'REFERENCES'
  1675.         end) PRIVILEGE,
  1676.         convert (varchar(3),case when p.protecttype = 205 then 'NO'
  1677.             else 'YES'
  1678.         end) IS_GRANTABLE
  1679.     from sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c, sysmembers m
  1680.     where
  1681.         c.id = @table_id
  1682.         and c.name like @column_name
  1683.         and c.id = p.id
  1684.         and c.id = o.id
  1685.         and case substring(p.columns, 1, 1) & 1
  1686.                 when NULL then 255    /* all columns have permission */
  1687.                 when 0 then convert(tinyint, substring(p.columns, v.low, 1))
  1688.                 else (~convert(tinyint, isnull(substring(p.columns, v.low, 1),0)))
  1689.             end
  1690.             & v.high <> 0            /* permission applies to this column */
  1691.         and v.number <= (select count(*) from syscolumns
  1692.             where id = @table_id)    /* ranges from 1 to # of columns in table */
  1693.         and v.type = N'P'
  1694.         and v.number = c.colid
  1695.             /* expand groups - AKUNDONE: only 1 level of group unrolling here. Need more?? */
  1696.         and (u.uid > 0 and u.uid < 16384)
  1697.         and ((p.uid = u.uid) or 
  1698.              (p.uid = m.groupuid and u.uid = m.memberuid))
  1699.         and p.protecttype <> 206    /* only grant rows */
  1700.         and p.action in (26,193,195,197)
  1701.         and o.uid <> u.uid            /* no rows for owner */
  1702.         and not exists (            /* exclude revoke'd privileges */
  1703.             select *
  1704.             from sysprotects p1
  1705.             where
  1706.                 p1.protecttype = 206
  1707.                 and p1.action = p.action
  1708.                 and p1.id = p.id
  1709.                 and p1.uid = u.uid
  1710.                 and case substring(p1.columns, 1, 1) & 1
  1711.                         when NULL then 255    /* all columns have permission */
  1712.                         when 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  1713.                                             else (~convert(tinyint,isnull(substring(p.columns, v.low, 1),0)))
  1714.                     end
  1715.                     & v.high <> 0)            /* permission applies to this column */
  1716.     union
  1717.     select    /*    Add rows for table owner */
  1718.         convert(sysname,db_name()) TABLE_QUALIFIER,
  1719.         convert(sysname,user_name(o.uid)) TABLE_OWNER,
  1720.         @table_name TABLE_NAME,
  1721.         convert(sysname,col_name(@table_id, c.colid)) COLUMN_NAME,
  1722.         convert(sysname,user_name(u.uid)) grantor,
  1723.         convert(sysname,user_name(o.uid)) grantee,
  1724.         convert (varchar(32),case v.number
  1725.             when 193 then 'SELECT'
  1726.             when 195 then 'INSERT'
  1727.             when 197 then 'UPDATE'
  1728.             else 'REFERENCES'
  1729.         end) PRIVILEGE,
  1730.         convert(varchar(3),'YES') IS_GRANTABLE
  1731.     from sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  1732.     where
  1733.         c.id = @table_id
  1734.         and c.name like @column_name
  1735.         and c.id = o.id
  1736.         and u.uid = 1        /* grantor is 'dbo' of database */
  1737.         and v.type = N'P'    /* cross product to get all exposed privileges */
  1738.         and v.number in (26,193,195,197)
  1739.         and not exists (    /* exclude revoke'd privileges */
  1740.             select *
  1741.             from sysprotects p1
  1742.             where
  1743.                 p1.protecttype = 206
  1744.                 and p1.action = v.number
  1745.                 and p1.id = o.id
  1746.                 and p1.uid = o.uid)
  1747.     order by 4, 7
  1748. go
  1749.  
  1750.  
  1751. grant execute on sp_column_privileges to public
  1752. go
  1753.  
  1754. dump tran master with no_log
  1755. go
  1756.  
  1757. print 'creating sp_columns'
  1758. go
  1759.  
  1760. /*    Procedure for pre-6.0 server */
  1761. CREATE PROCEDURE sp_columns (
  1762.                  @table_name        varchar(96),
  1763.                  @table_owner        varchar(96) = null,
  1764.                  @table_qualifier    varchar(32) = null,
  1765.                  @column_name        varchar(96) = null,
  1766.                  @ODBCVer            int = 2)
  1767. AS
  1768.     DECLARE @full_table_name varchar(193)
  1769.     DECLARE @table_id        int
  1770.  
  1771.     if @ODBCVer <> 3
  1772.         select @ODBCVer = 2
  1773.     if @column_name is null /*    If column name not supplied, match all */
  1774.         select @column_name = '%'
  1775.     if @table_qualifier is not null
  1776.     begin
  1777.         if db_name() <> @table_qualifier
  1778.         begin    /* If qualifier doesn't match current database */
  1779.             raiserror 20001 '~~Rush_5~~'
  1780.             return
  1781.         end
  1782.     end
  1783.     if @table_name is null
  1784.     begin    /*    If table name not supplied, match all */
  1785.         select @table_name = '%'
  1786.     end
  1787.     if @table_owner is null
  1788.     begin    /* If unqualified table name */
  1789.         SELECT @full_table_name = @table_name
  1790.     end
  1791.     else
  1792.     begin    /* Qualified table name */
  1793.         SELECT @full_table_name = @table_owner + '.' + @table_name
  1794.     end
  1795.  
  1796.     /*    Get Object ID */
  1797.     SELECT @table_id = object_id(@full_table_name)
  1798.     if ((charindex('%',@full_table_name) = 0) and
  1799.         (charindex('_',@full_table_name) = 0) and
  1800.         @table_id <> 0)
  1801.     begin
  1802.         /* this block is for the case where there is no pattern
  1803.              matching required for the table name */
  1804.  
  1805.         SELECT
  1806.             TABLE_QUALIFIER = convert(varchar(32),DB_NAME()),
  1807.             TABLE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  1808.             TABLE_NAME = convert(varchar(32),o.name),
  1809.             COLUMN_NAME = convert(varchar(32),c.name),
  1810.             d.DATA_TYPE,
  1811.             TYPE_NAME = t.name,
  1812.             "PRECISION" = isnull(d.data_precision, convert(int,c.length)),
  1813.             LENGTH = isnull(d.length, convert(int,c.length)),
  1814.             SCALE = d.numeric_scale,
  1815.             d.RADIX,
  1816.             NULLABLE =    /* set nullability from status flag */
  1817.                 convert(smallint, convert(bit, c.status&8)),
  1818.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  1819.             COLUMN_DEF = text,
  1820.             d.SQL_DATA_TYPE,
  1821.             d.SQL_DATETIME_SUB,
  1822.             CHAR_OCTET_LENGTH = isnull(d.data_precision, convert(int,c.length))+d.charbin,
  1823.             ORDINAL_POSITION = convert(int,
  1824.                        (
  1825.                         select count(*)
  1826.                         from syscolumns sc
  1827.                         where sc.id     =  c.id
  1828.                           AND sc.number =  c.number
  1829.                           AND sc.colid  <= c.colid
  1830.                         )),
  1831.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO      YES',(c.status&8)+1,3))),
  1832.             SS_DATA_TYPE = c.type
  1833.         FROM
  1834.             syscolumns c,
  1835.             sysobjects o,
  1836.             syscomments m,
  1837.             master.dbo.spt_datatype_info d,
  1838.             systypes t
  1839.         WHERE
  1840.             o.id = @table_id
  1841.             AND c.id = o.id
  1842.             AND t.type = d.ss_dtype
  1843.             AND c.length = isnull(d.fixlen, c.length)
  1844.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  1845.             AND o.type <> 'P'
  1846.             AND c.usertype = t.usertype
  1847.             AND c.name like @column_name
  1848.             AND c.cdefault *= m.id
  1849.             AND m.colid = 1
  1850.         ORDER BY 17
  1851.     end
  1852.     else
  1853.     begin
  1854.         /* this block is for the case where there IS pattern
  1855.              matching done on the table name */
  1856.         if @table_owner is null /*    If owner not supplied, match all */
  1857.             select @table_owner = '%'
  1858.  
  1859.         SELECT
  1860.             TABLE_QUALIFIER = convert(varchar(32),DB_NAME()),
  1861.             TABLE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  1862.             TABLE_NAME = convert(varchar(32),o.name),
  1863.             COLUMN_NAME = convert(varchar(32),c.name),
  1864.             d.DATA_TYPE,
  1865.             TYPE_NAME = t.name,
  1866.             "PRECISION" = isnull(d.data_precision, convert(int,c.length)),
  1867.             LENGTH = isnull(d.length, convert(int,c.length)),
  1868.             SCALE = d.numeric_scale,
  1869.             d.RADIX,
  1870.             NULLABLE =    /* set nullability from status flag */
  1871.                 convert(smallint, convert(bit, c.status&8)),
  1872.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  1873.             COLUMN_DEF = text,
  1874.             d.SQL_DATA_TYPE,
  1875.             d.SQL_DATETIME_SUB,
  1876.             CHAR_OCTET_LENGTH = isnull(d.data_precision, convert(int,c.length))+d.charbin,
  1877.             ORDINAL_POSITION = convert(int,
  1878.                        (
  1879.                         select count(*)
  1880.                         from syscolumns sc
  1881.                         where sc.id     =  c.id
  1882.                           AND sc.number =  c.number
  1883.                           AND sc.colid  <= c.colid
  1884.                         )),
  1885.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO      YES',(c.status&8)+1,3))),
  1886.             SS_DATA_TYPE = c.type
  1887.         FROM
  1888.             syscolumns c,
  1889.             sysobjects o,
  1890.             syscomments m,
  1891.             master.dbo.spt_datatype_info d,
  1892.             systypes t
  1893.         WHERE
  1894.             o.name like @table_name
  1895.             AND user_name(o.uid) like @table_owner
  1896.             AND o.id = c.id
  1897.             AND t.type = d.ss_dtype
  1898.             AND c.length = isnull(d.fixlen, c.length)
  1899.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  1900.             AND o.type <> 'P'
  1901.             AND c.usertype = t.usertype
  1902.             AND c.name like @column_name
  1903.             AND c.cdefault *= m.id
  1904.             AND m.colid = 1
  1905.         ORDER BY 2, 3, 17
  1906.     end
  1907. go
  1908.  
  1909. if (charindex('6.00', @@version) = 0 and
  1910.     charindex('6.50', @@version) = 0 and
  1911.     charindex('7.00', @@version) = 0 and
  1912.     charindex('8.00', @@version) = 0)
  1913. begin
  1914.     print ''
  1915.     print ''
  1916.     print 'Warning:'
  1917.     print 'you are installing the stored procedures '
  1918.     print 'on a pre 6.0 SQL Server.'
  1919.     print 'Ignore the following error.'
  1920. end
  1921. else
  1922.     drop proc sp_columns
  1923. go
  1924.  
  1925. /*    Procedure for 6.0 and 6.50 server */
  1926. CREATE PROCEDURE sp_columns (
  1927.                  @table_name        varchar(96),
  1928.                  @table_owner        varchar(96) = null,
  1929.                  @table_qualifier    varchar(32) = null,
  1930.                  @column_name        varchar(96) = null,
  1931.                  @ODBCVer            int = 2)
  1932. AS
  1933.     DECLARE @full_table_name    varchar(193)
  1934.     DECLARE @table_id int
  1935.  
  1936.     if @ODBCVer <> 3
  1937.         select @ODBCVer = 2
  1938.     if @column_name is null /*    If column name not supplied, match all */
  1939.         select @column_name = '%'
  1940.     if @table_qualifier is not null
  1941.     begin
  1942.         if db_name() <> @table_qualifier
  1943.         begin    /* If qualifier doesn't match current database */
  1944.             raiserror (15250, -1,-1)
  1945.             return
  1946.         end
  1947.     end
  1948.     if @table_name is null
  1949.     begin
  1950.         /* If table name not supplied, match all */
  1951.         select @table_name = '%'
  1952.     end
  1953.     if @table_owner is null
  1954.     begin
  1955.         /* If unqualified table name */
  1956.         SELECT @full_table_name = @table_name
  1957.     end
  1958.     else
  1959.     begin
  1960.         /* Qualified table name */
  1961.         SELECT @full_table_name = @table_owner + '.' + @table_name
  1962.     end
  1963.  
  1964.     /* Get Object ID */
  1965.     SELECT @table_id = object_id(@full_table_name)
  1966.     if ((charindex('%',@full_table_name) = 0) and
  1967.         (charindex('[',@full_table_name) = 0) and
  1968.         (charindex('_',@full_table_name) = 0) and
  1969.         @table_id <> 0)
  1970.     begin
  1971.         /* this block is for the case where there is no pattern
  1972.             matching required for the table name */
  1973.  
  1974.         SELECT
  1975.             TABLE_QUALIFIER = convert(varchar(32),DB_NAME()),
  1976.             TABLE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  1977.             TABLE_NAME = convert(varchar(32),o.name),
  1978.             COLUMN_NAME = convert(varchar(32),c.name),
  1979.             DATA_TYPE = d.DATA_TYPE,
  1980.             TYPE_NAME = convert(varchar(32),
  1981.                     case
  1982.                         when t.usertype > 100 or t.usertype in (18,80) then t.name
  1983.                         else d.TYPE_NAME
  1984.                     end),
  1985.             "PRECISION" = convert(int,
  1986.                     case
  1987.                         when d.DATA_TYPE in (6,7) then d.data_precision    /* FLOAT/REAL */
  1988.                         else isnull(convert(int,c.prec), 2147483647)
  1989.                     end),
  1990.             LENGTH = convert(int,
  1991.                     case
  1992.                         when d.ss_dtype IN (106, 108, 55, 63) then    /* decimal/numeric types */
  1993.                         c.prec+2
  1994.                         else
  1995.                             isnull(d.length, c.length)
  1996.                     end),
  1997.             SCALE = convert(smallint, c.scale),
  1998.             RADIX = d.RADIX,
  1999.             NULLABLE =    /* set nullability from status flag */
  2000.                 convert(smallint, convert(bit, c.status&8)),
  2001.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  2002.             COLUMN_DEF = text,
  2003.             SQL_DATA_TYPE = d.SQL_DATA_TYPE,
  2004.             SQL_DATETIME_SUB = d.SQL_DATETIME_SUB,
  2005.             CHAR_OCTET_LENGTH = isnull(convert(int,c.prec), 2147483647)+d.charbin,
  2006.             ORDINAL_POSITION = convert(int,
  2007.                        (
  2008.                         select count(*)
  2009.                         from syscolumns sc
  2010.                         where sc.id     =  c.id
  2011.                           AND sc.number =  c.number
  2012.                           AND sc.colid  <= c.colid
  2013.                         )),
  2014.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO      YES',(c.status&8)+1,3))),
  2015.             SS_DATA_TYPE = c.type
  2016.         FROM
  2017.             syscolumns c,
  2018.             sysobjects o,
  2019.             syscomments m,
  2020.             master.dbo.spt_datatype_info d,
  2021.             systypes t
  2022.         WHERE
  2023.             o.id = @table_id
  2024.             AND c.id = o.id
  2025.             AND t.type = d.ss_dtype
  2026.             AND c.length = isnull(d.fixlen, c.length)
  2027.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2028.             AND o.type <> 'P'
  2029.             AND isnull(d.AUTO_INCREMENT,0) = (c.status&128)/128
  2030.             AND c.usertype = t.usertype
  2031.             AND c.name like @column_name
  2032.             AND c.cdefault *= m.id
  2033.             AND m.colid = 1
  2034.         ORDER BY 17
  2035.     end
  2036.     else
  2037.     begin
  2038.         /* this block is for the case where there IS pattern
  2039.             matching done on the table name */
  2040.  
  2041.         if @table_owner is null /*    If owner not supplied, match all */
  2042.             select @table_owner = '%'
  2043.  
  2044.         SELECT
  2045.             TABLE_QUALIFIER = convert(varchar(32),DB_NAME()),
  2046.             TABLE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  2047.             TABLE_NAME = convert(varchar(32),o.name),
  2048.             COLUMN_NAME = convert(varchar(32),c.name),
  2049.             DATA_TYPE = d.DATA_TYPE,
  2050.             TYPE_NAME = convert(varchar(32), 
  2051.                 case 
  2052.                     when t.usertype > 100 or t.usertype in (18,80) then t.name
  2053.                     else d.TYPE_NAME
  2054.                 end),
  2055.             "PRECISION" = convert(int,
  2056.                     case
  2057.                         when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  2058.                         else isnull(convert(int,c.prec), 2147483647)
  2059.                     end),
  2060.             LENGTH = convert(int,
  2061.                     case
  2062.                         when d.ss_dtype IN (106, 108, 55, 63) then    /* decimal/numeric types */
  2063.                             c.prec+2
  2064.                         else
  2065.                             isnull(d.length, c.length)
  2066.                     end),
  2067.             SCALE = convert(smallint, c.scale),
  2068.             RADIX = d.RADIX,
  2069.             NULLABLE =    /* set nullability from status flag */
  2070.                 convert(smallint, convert(bit, c.status&8)),
  2071.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  2072.             COLUMN_DEF = text,
  2073.             SQL_DATA_TYPE = d.SQL_DATA_TYPE,
  2074.             SQL_DATETIME_SUB = d.SQL_DATETIME_SUB,
  2075.             CHAR_OCTET_LENGTH = isnull(convert(int,c.prec), 2147483647) + d.charbin,
  2076.             ORDINAL_POSITION = convert(int,
  2077.                        (
  2078.                         select count(*)
  2079.                         from syscolumns sc
  2080.                         where sc.id     =  c.id
  2081.                           AND sc.number =  c.number
  2082.                           AND sc.colid  <= c.colid
  2083.                         )),
  2084.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO      YES',(c.status&8)+1,3))),
  2085.             SS_DATA_TYPE = c.type
  2086.         FROM
  2087.             syscolumns c,
  2088.             sysobjects o,
  2089.             syscomments m,
  2090.             master.dbo.spt_datatype_info d,
  2091.             systypes t
  2092.         WHERE
  2093.             o.name like @table_name
  2094.             AND user_name(o.uid) like @table_owner
  2095.             AND o.id = c.id
  2096.             AND t.type = d.ss_dtype
  2097.             AND c.length = isnull(d.fixlen, c.length)
  2098.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2099.             AND o.type <> 'P'
  2100.             AND isnull(d.AUTO_INCREMENT,0) = (c.status&128)/128
  2101.             AND c.usertype = t.usertype
  2102.             AND c.name like @column_name
  2103.             AND c.cdefault *= m.id
  2104.             AND m.colid = 1
  2105.         ORDER BY 2, 3, 17
  2106.     end
  2107. go
  2108.  
  2109. if (charindex('7.00', @@version) = 0 and
  2110.     charindex('8.00', @@version) = 0)
  2111. begin
  2112.     print ''
  2113.     print ''
  2114.     print 'Warning:'
  2115.     print 'you are installing the stored procedures '
  2116.     print 'on a pre 7.0 SQL Server.'
  2117.     print 'Ignore the following errors.'
  2118. end
  2119. else
  2120.     drop proc sp_columns
  2121. go
  2122.  
  2123. /*    Procedure for 7.0 server */
  2124. CREATE PROCEDURE sp_columns (
  2125.                  @table_name        nvarchar(384),
  2126.                  @table_owner        nvarchar(384) = null,
  2127.                  @table_qualifier    sysname = null,
  2128.                  @column_name        nvarchar(384) = null,
  2129.                  @ODBCVer            int = 2)
  2130. AS
  2131.     DECLARE @full_table_name    nvarchar(769)
  2132.     DECLARE @table_id int
  2133.  
  2134.     if @ODBCVer <> 3
  2135.         select @ODBCVer = 2
  2136.     if @column_name is null /*    If column name not supplied, match all */
  2137.         select @column_name = '%'
  2138.     if @table_qualifier is not null
  2139.     begin
  2140.         if db_name() <> @table_qualifier
  2141.         begin    /* If qualifier doesn't match current database */
  2142.             raiserror (15250, -1,-1)
  2143.             return
  2144.         end
  2145.     end
  2146.     if @table_name is null
  2147.     begin    /*    If table name not supplied, match all */
  2148.         select @table_name = '%'
  2149.     end
  2150.     if @table_owner is null
  2151.     begin    /* If unqualified table name */
  2152.         SELECT @full_table_name = quotename(@table_name)
  2153.     end
  2154.     else
  2155.     begin    /* Qualified table name */
  2156.         if @table_owner = ''
  2157.         begin    /* If empty owner name */
  2158.             SELECT @full_table_name = quotename(@table_owner)
  2159.         end
  2160.         else
  2161.         begin
  2162.             SELECT @full_table_name = quotename(@table_owner) +
  2163.                 '.' + quotename(@table_name)
  2164.         end
  2165.     end
  2166.  
  2167.     /*    Get Object ID */
  2168.     SELECT @table_id = object_id(@full_table_name)
  2169.     if ((isnull(charindex('%', @full_table_name),0) = 0) and
  2170.         (isnull(charindex('[', @table_name),0) = 0) and
  2171.         (isnull(charindex('[', @table_owner),0) = 0) and
  2172.         (isnull(charindex('_', @full_table_name),0) = 0) and
  2173.         @table_id <> 0)
  2174.     begin
  2175.         /* this block is for the case where there is no pattern
  2176.             matching required for the table name */
  2177.         
  2178.         SELECT
  2179.             TABLE_QUALIFIER = convert(sysname,DB_NAME()),
  2180.             TABLE_OWNER = convert(sysname,USER_NAME(o.uid)),
  2181.             TABLE_NAME = convert(sysname,o.name),
  2182.             COLUMN_NAME = convert(sysname,c.name),
  2183.             d.DATA_TYPE,
  2184.             convert (sysname,case
  2185.                 when t.xusertype > 255 then t.name
  2186.                 else d.TYPE_NAME
  2187.             end) TYPE_NAME,
  2188.             convert(int,case
  2189.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  2190.                 else OdbcPrec(c.xtype,c.length,c.xprec)
  2191.             end) "PRECISION",
  2192.             convert(int,case
  2193.                 when type_name(d.ss_dtype) IN ('numeric','decimal') then    /* decimal/numeric types */
  2194.                     OdbcPrec(c.xtype,c.length,c.xprec)+2
  2195.                 else
  2196.                     isnull(d.length, c.length)
  2197.             end) LENGTH,
  2198.             SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
  2199.             d.RADIX,
  2200.             NULLABLE = convert(smallint, ColumnProperty (c.id, c.name, 'AllowsNull')),
  2201.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  2202.             COLUMN_DEF = text,
  2203.             d.SQL_DATA_TYPE,
  2204.             d.SQL_DATETIME_SUB,
  2205.             CHAR_OCTET_LENGTH = isnull(d.length, c.length)+d.charbin,
  2206.             ORDINAL_POSITION = convert(int,
  2207.                        (
  2208.                         select count(*)
  2209.                         from syscolumns sc
  2210.                         where sc.id     =  c.id
  2211.                           AND sc.number =  c.number
  2212.                           AND sc.colid  <= c.colid
  2213.                         )),
  2214.             IS_NULLABLE = convert(varchar(254),
  2215.                 substring('NO YES',(ColumnProperty (c.id, c.name, 'AllowsNull')*3)+1,3)),
  2216.             SS_DATA_TYPE = c.type
  2217.         FROM
  2218.             sysobjects o,
  2219.             master.dbo.spt_datatype_info d,
  2220.             systypes t,
  2221.             syscolumns c
  2222.             LEFT OUTER JOIN syscomments m on c.cdefault = m.id
  2223.                 AND m.colid = 1
  2224.         WHERE
  2225.             o.id = @table_id
  2226.             AND c.id = o.id
  2227.             AND t.xtype = d.ss_dtype
  2228.             AND c.length = isnull(d.fixlen, c.length)
  2229.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2230.             AND (o.type not in ('P', 'FN', 'TF', 'IF') OR (o.type in ('TF', 'IF') and c.number = 0))
  2231.             AND isnull(d.AUTO_INCREMENT,0) = isnull(ColumnProperty (c.id, c.name, 'IsIdentity'),0)
  2232.             AND c.xusertype = t.xusertype
  2233.             AND c.name like @column_name
  2234.         ORDER BY 17
  2235.     end
  2236.     else
  2237.     begin
  2238.         /* this block is for the case where there IS pattern
  2239.             matching done on the table name */
  2240.  
  2241.         if @table_owner is null /*    If owner not supplied, match all */
  2242.             select @table_owner = '%'
  2243.  
  2244.         SELECT
  2245.             TABLE_QUALIFIER = convert(sysname,DB_NAME()),
  2246.             TABLE_OWNER = convert(sysname,USER_NAME(o.uid)),
  2247.             TABLE_NAME = convert(sysname,o.name),
  2248.             COLUMN_NAME = convert(sysname,c.name),
  2249.             d.DATA_TYPE,
  2250.             convert (sysname,case
  2251.                 when t.xusertype > 255 then t.name
  2252.                 else d.TYPE_NAME
  2253.             end) TYPE_NAME,
  2254.             convert(int,case
  2255.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  2256.                 else OdbcPrec(c.xtype,c.length,c.xprec)
  2257.             end) "PRECISION",
  2258.             convert(int,case
  2259.                 when type_name(d.ss_dtype) IN ('numeric','decimal') then    /* decimal/numeric types */
  2260.                     OdbcPrec(c.xtype,c.length,c.xprec)+2
  2261.                 else
  2262.                     isnull(d.length, c.length)
  2263.             end) LENGTH,
  2264.             SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
  2265.             d.RADIX,
  2266.             NULLABLE = convert(smallint, ColumnProperty (c.id, c.name, 'AllowsNull')),
  2267.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  2268.             COLUMN_DEF = text,
  2269.             d.SQL_DATA_TYPE,
  2270.             d.SQL_DATETIME_SUB,
  2271.             CHAR_OCTET_LENGTH = isnull(d.length, c.length)+d.charbin,
  2272.             ORDINAL_POSITION = convert(int,
  2273.                        (
  2274.                         select count(*)
  2275.                         from syscolumns sc
  2276.                         where sc.id     =  c.id
  2277.                           AND sc.number =  c.number
  2278.                           AND sc.colid  <= c.colid
  2279.                         )),
  2280.             IS_NULLABLE = convert(varchar(254),
  2281.                 rtrim(substring('NO YES',(ColumnProperty (c.id, c.name, 'AllowsNull')*3)+1,3))),
  2282.             SS_DATA_TYPE = c.type
  2283.         FROM
  2284.             sysobjects o,
  2285.             master.dbo.spt_datatype_info d,
  2286.             systypes t,
  2287.             syscolumns c
  2288.             LEFT OUTER JOIN syscomments m on c.cdefault = m.id
  2289.                 AND m.colid = 1
  2290.         WHERE
  2291.             o.name like @table_name
  2292.             AND user_name(o.uid) like @table_owner
  2293.             AND o.id = c.id
  2294.             AND t.xtype = d.ss_dtype
  2295.             AND c.length = isnull(d.fixlen, c.length)
  2296.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2297.             AND (o.type not in ('P', 'FN', 'TF', 'IF') OR (o.type in ('TF', 'IF') and c.number = 0))
  2298.             AND isnull(d.AUTO_INCREMENT,0) = isnull(ColumnProperty (c.id, c.name, 'IsIdentity'),0)
  2299.             AND c.xusertype = t.xusertype
  2300.             AND c.name like @column_name
  2301.         ORDER BY 2, 3, 17
  2302.     end
  2303. go
  2304.  
  2305. if (charindex('8.00', @@version) = 0)
  2306. begin
  2307.     print ''
  2308.     print ''
  2309.     print 'Warning:'
  2310.     print 'you are installing the stored procedures '
  2311.     print 'on a pre 8.0 SQL Server.'
  2312.     print 'Ignore the following errors.'
  2313. end
  2314. else
  2315.     drop proc sp_columns
  2316. go
  2317.  
  2318. /*    Procedure for 8.0 server */
  2319. CREATE PROCEDURE sp_columns (
  2320.                  @table_name        nvarchar(384),
  2321.                  @table_owner        nvarchar(384) = null,
  2322.                  @table_qualifier    sysname = null,
  2323.                  @column_name        nvarchar(384) = null,
  2324.                  @ODBCVer            int = 2)
  2325. AS
  2326.     DECLARE @full_table_name    nvarchar(769)
  2327.     DECLARE @table_id int
  2328.  
  2329.     if @ODBCVer <> 3
  2330.         select @ODBCVer = 2
  2331.     if @column_name is null /*    If column name not supplied, match all */
  2332.         select @column_name = '%'
  2333.     if @table_qualifier is not null
  2334.     begin
  2335.         if db_name() <> @table_qualifier
  2336.         begin    /* If qualifier doesn't match current database */
  2337.             raiserror (15250, -1,-1)
  2338.             return
  2339.         end
  2340.     end
  2341.     if @table_name is null
  2342.     begin    /*    If table name not supplied, match all */
  2343.         select @table_name = '%'
  2344.     end
  2345.     if @table_owner is null
  2346.     begin    /* If unqualified table name */
  2347.         SELECT @full_table_name = quotename(@table_name)
  2348.     end
  2349.     else
  2350.     begin    /* Qualified table name */
  2351.         if @table_owner = ''
  2352.         begin    /* If empty owner name */
  2353.             SELECT @full_table_name = quotename(@table_owner)
  2354.         end
  2355.         else
  2356.         begin
  2357.             SELECT @full_table_name = quotename(@table_owner) +
  2358.                 '.' + quotename(@table_name)
  2359.         end
  2360.     end
  2361.  
  2362.     /*    Get Object ID */
  2363.     SELECT @table_id = object_id(@full_table_name)
  2364.     if ((isnull(charindex('%', @full_table_name),0) = 0) and
  2365.         (isnull(charindex('[', @table_name),0) = 0) and
  2366.         (isnull(charindex('[', @table_owner),0) = 0) and
  2367.         (isnull(charindex('_', @full_table_name),0) = 0) and
  2368.         @table_id <> 0)
  2369.     begin
  2370.         /* this block is for the case where there is no pattern
  2371.             matching required for the table name */
  2372.         
  2373.         SELECT
  2374.             TABLE_QUALIFIER = convert(sysname,DB_NAME()),
  2375.             TABLE_OWNER = convert(sysname,USER_NAME(o.uid)),
  2376.             TABLE_NAME = convert(sysname,o.name),
  2377.             COLUMN_NAME = convert(sysname,c.name),
  2378.             d.DATA_TYPE,
  2379.             convert (sysname,case
  2380.                 when t.xusertype > 255 then t.name
  2381.                 else d.TYPE_NAME collate database_default
  2382.             end) TYPE_NAME,
  2383.             convert(int,case
  2384.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  2385.                 else OdbcPrec(c.xtype,c.length,c.xprec)
  2386.             end) "PRECISION",
  2387.             convert(int,case
  2388.                 when type_name(d.ss_dtype) IN ('numeric','decimal') then    /* decimal/numeric types */
  2389.                     OdbcPrec(c.xtype,c.length,c.xprec)+2
  2390.                 else
  2391.                     isnull(d.length, c.length)
  2392.             end) LENGTH,
  2393.             SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
  2394.             d.RADIX,
  2395.             NULLABLE = convert(smallint, ColumnProperty (c.id, c.name, 'AllowsNull')),
  2396.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  2397.             COLUMN_DEF = text,
  2398.             d.SQL_DATA_TYPE,
  2399.             d.SQL_DATETIME_SUB,
  2400.             CHAR_OCTET_LENGTH = isnull(d.length, c.length)+d.charbin,
  2401.             ORDINAL_POSITION = convert(int,
  2402.                        (
  2403.                         select count(*)
  2404.                         from syscolumns sc
  2405.                         where sc.id     =  c.id
  2406.                           AND sc.number =  c.number
  2407.                           AND sc.colid  <= c.colid
  2408.                         )),
  2409.             IS_NULLABLE = convert(varchar(254),
  2410.                 substring('NO YES',(ColumnProperty (c.id, c.name, 'AllowsNull')*3)+1,3)),
  2411.             SS_DATA_TYPE = c.type
  2412.         FROM
  2413.             sysobjects o,
  2414.             master.dbo.spt_datatype_info d,
  2415.             systypes t,
  2416.             syscolumns c
  2417.             LEFT OUTER JOIN syscomments m on c.cdefault = m.id
  2418.                 AND m.colid = 1
  2419.         WHERE
  2420.             o.id = @table_id
  2421.             AND c.id = o.id
  2422.             AND t.xtype = d.ss_dtype
  2423.             AND c.length = isnull(d.fixlen, c.length)
  2424.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2425.             AND (o.type not in ('P', 'FN', 'TF', 'IF') OR (o.type in ('TF', 'IF') and c.number = 0))
  2426.             AND isnull(d.AUTO_INCREMENT,0) = isnull(ColumnProperty (c.id, c.name, 'IsIdentity'),0)
  2427.             AND c.xusertype = t.xusertype
  2428.             AND c.name like @column_name
  2429.         ORDER BY 17
  2430.     end
  2431.     else
  2432.     begin
  2433.         /* this block is for the case where there IS pattern
  2434.             matching done on the table name */
  2435.  
  2436.         if @table_owner is null /*    If owner not supplied, match all */
  2437.             select @table_owner = '%'
  2438.  
  2439.         SELECT
  2440.             TABLE_QUALIFIER = convert(sysname,DB_NAME()),
  2441.             TABLE_OWNER = convert(sysname,USER_NAME(o.uid)),
  2442.             TABLE_NAME = convert(sysname,o.name),
  2443.             COLUMN_NAME = convert(sysname,c.name),
  2444.             d.DATA_TYPE,
  2445.             convert (sysname,case
  2446.                 when t.xusertype > 255 then t.name
  2447.                 else d.TYPE_NAME collate database_default
  2448.             end) TYPE_NAME,
  2449.             convert(int,case
  2450.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  2451.                 else OdbcPrec(c.xtype,c.length,c.xprec)
  2452.             end) "PRECISION",
  2453.             convert(int,case
  2454.                 when type_name(d.ss_dtype) IN ('numeric','decimal') then    /* decimal/numeric types */
  2455.                     OdbcPrec(c.xtype,c.length,c.xprec)+2
  2456.                 else
  2457.                     isnull(d.length, c.length)
  2458.             end) LENGTH,
  2459.             SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
  2460.             d.RADIX,
  2461.             NULLABLE = convert(smallint, ColumnProperty (c.id, c.name, 'AllowsNull')),
  2462.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  2463.             COLUMN_DEF = text,
  2464.             d.SQL_DATA_TYPE,
  2465.             d.SQL_DATETIME_SUB,
  2466.             CHAR_OCTET_LENGTH = isnull(d.length, c.length)+d.charbin,
  2467.             ORDINAL_POSITION = convert(int,
  2468.                        (
  2469.                         select count(*)
  2470.                         from syscolumns sc
  2471.                         where sc.id     =  c.id
  2472.                           AND sc.number =  c.number
  2473.                           AND sc.colid  <= c.colid
  2474.                         )),
  2475.             IS_NULLABLE = convert(varchar(254),
  2476.                 rtrim(substring('NO YES',(ColumnProperty (c.id, c.name, 'AllowsNull')*3)+1,3))),
  2477.             SS_DATA_TYPE = c.type
  2478.         FROM
  2479.             sysobjects o,
  2480.             master.dbo.spt_datatype_info d,
  2481.             systypes t,
  2482.             syscolumns c
  2483.             LEFT OUTER JOIN syscomments m on c.cdefault = m.id
  2484.                 AND m.colid = 1
  2485.         WHERE
  2486.             o.name like @table_name
  2487.             AND user_name(o.uid) like @table_owner
  2488.             AND o.id = c.id
  2489.             AND t.xtype = d.ss_dtype
  2490.             AND c.length = isnull(d.fixlen, c.length)
  2491.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2492.             AND (o.type not in ('P', 'FN', 'TF', 'IF') OR (o.type in ('TF', 'IF') and c.number = 0))
  2493.             AND isnull(d.AUTO_INCREMENT,0) = isnull(ColumnProperty (c.id, c.name, 'IsIdentity'),0)
  2494.             AND c.xusertype = t.xusertype
  2495.             AND c.name like @column_name
  2496.         ORDER BY 2, 3, 17
  2497.     end
  2498. go
  2499.  
  2500. grant execute on sp_columns to public
  2501. go
  2502.  
  2503. dump tran master with no_log
  2504. go
  2505.  
  2506. print 'creating sp_databases'
  2507. go
  2508.  
  2509. /*    Procedure for pre-7.0 server */
  2510. create proc sp_databases
  2511. as
  2512.     set nocount on
  2513.     /* Use temporary table to sum up database size w/o using group by */
  2514.     create table #databases (
  2515.                   DATABASE_NAME varchar(32) NOT NULL,
  2516.                   size int NOT NULL)
  2517.  
  2518.     /* Insert row for each database */
  2519.     insert into #databases
  2520.         select
  2521.             name,
  2522.             (select sum(size) from master.dbo.sysusages
  2523.                 where dbid = d.dbid)
  2524.         from master.dbo.sysdatabases d
  2525.  
  2526.     select
  2527.          DATABASE_NAME,
  2528.          DATABASE_SIZE = size*2,    /* Convert from 2048 byte pages to K */
  2529.          REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  2530.     from #databases
  2531.     order by 1
  2532. go
  2533.  
  2534. if (charindex('7.00', @@version) = 0 and
  2535.     charindex('8.00', @@version) = 0)
  2536. begin
  2537.     print ''
  2538.     print ''
  2539.     print 'Warning:'
  2540.     print 'you are installing the stored procedures '
  2541.     print 'on a pre 8.0 SQL Server.'
  2542.     print 'Ignore the following errors.'
  2543. end
  2544. else
  2545.     drop proc sp_databases
  2546. go
  2547.  
  2548. /*    Procedure for 8.0 server */
  2549. create proc sp_databases
  2550. as
  2551.     set nocount on
  2552.     declare @name sysname
  2553.     declare @SQL  nvarchar(600)
  2554.  
  2555.     /* Use temporary table to sum up database size w/o using group by */
  2556.     create table #databases (
  2557.                   DATABASE_NAME sysname NOT NULL,
  2558.                   size int NOT NULL)
  2559.  
  2560.     declare c1 cursor for 
  2561.         select name from master.dbo.sysdatabases
  2562.             where has_dbaccess(name) = 1 -- Only look at databases to which we have access
  2563.  
  2564.     open c1
  2565.     fetch c1 into @name
  2566.  
  2567.     while @@fetch_status >= 0
  2568.     begin
  2569.         select @SQL = 'insert into #databases
  2570.                 select N'''+ @name + ''', sum(size) from '
  2571.                 + QuoteName(@name) + '.dbo.sysfiles'
  2572.         /* Insert row for each database */
  2573.         execute (@SQL)
  2574.         fetch c1 into @name
  2575.     end
  2576.     deallocate c1
  2577.  
  2578.     select    
  2579.         DATABASE_NAME,
  2580.         DATABASE_SIZE = size*8,/* Convert from 8192 byte pages to K */
  2581.         REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  2582.     from #databases
  2583.     order by 1
  2584. go
  2585.  
  2586. grant execute on sp_databases to public
  2587. go
  2588.  
  2589. dump tran master with no_log
  2590. go
  2591.  
  2592. print 'creating sp_datatype_info'
  2593. go
  2594.  
  2595. /*    Procedure for pre-6.0 server */
  2596. create proc sp_datatype_info
  2597.     (@data_type int = 0, @ODBCVer tinyint = 2)
  2598. as
  2599.     if @ODBCVer <> 3
  2600.         select @ODBCVer = 2
  2601.     if @data_type = 0
  2602.         select
  2603.             TYPE_NAME = t.name,
  2604.             d.DATA_TYPE,
  2605.             "PRECISION" = isnull(d.data_precision, convert(int,t.length)),
  2606.             d.LITERAL_PREFIX,
  2607.             d.LITERAL_SUFFIX,
  2608.             e.CREATE_PARAMS,
  2609.             d.NULLABLE,
  2610.             d.CASE_SENSITIVE,
  2611.             d.SEARCHABLE,
  2612.             d.UNSIGNED_ATTRIBUTE,
  2613.             d.MONEY,
  2614.             d.AUTO_INCREMENT,
  2615.             LOCAL_TYPE_NAME = t.name,
  2616.             MINIMUM_SCALE = d.numeric_scale,
  2617.             MAXIMUM_SCALE = d.numeric_scale,
  2618.             d.SQL_DATA_TYPE,
  2619.             d.SQL_DATETIME_SUB,
  2620.             NUM_PREC_RADIX = convert(int,d.RADIX),
  2621.             INTERVAL_PRECISION = convert(smallint,NULL),
  2622.             USERTYPE = t.usertype
  2623.         from master.dbo.spt_datatype_info d, master.dbo.spt_datatype_info_ext e, systypes t
  2624.         where
  2625.             d.ss_dtype = t.type
  2626.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2627.             and t.usertype *= e.user_type
  2628.             and (t.type not in (111,109,38,110) or /* get rid of nullable types */
  2629.                 t.usertype > 100)
  2630.         order by 2, 12, 11, t.usertype
  2631.  
  2632.     else
  2633.         select
  2634.             TYPE_NAME = t.name,
  2635.             d.DATA_TYPE,
  2636.             "PRECISION" = isnull(d.data_precision, convert(int,t.length)),
  2637.             d.LITERAL_PREFIX,
  2638.             d.LITERAL_SUFFIX,
  2639.             e.CREATE_PARAMS,
  2640.             d.NULLABLE,
  2641.             d.CASE_SENSITIVE,
  2642.             d.SEARCHABLE,
  2643.             d.UNSIGNED_ATTRIBUTE,
  2644.             d.MONEY,
  2645.             d.AUTO_INCREMENT,
  2646.             LOCAL_TYPE_NAME = t.name,
  2647.             MINIMUM_SCALE = d.numeric_scale,
  2648.             MAXIMUM_SCALE = d.numeric_scale,
  2649.             d.SQL_DATA_TYPE,
  2650.             d.SQL_DATETIME_SUB,
  2651.             NUM_PREC_RADIX = convert(int,d.RADIX),
  2652.             INTERVAL_PRECISION = convert(smallint,NULL),
  2653.             USERTYPE = t.usertype
  2654.         from master.dbo.spt_datatype_info d, master.dbo.spt_datatype_info_ext e, systypes t
  2655.         where
  2656.             DATA_TYPE = @data_type
  2657.             and d.ss_dtype = t.type
  2658.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2659.             and t.usertype *= e.user_type
  2660.             and (t.type not in (111,109,38,110) or /* get rid of nullable types */
  2661.                 t.usertype > 100)
  2662.         order by 12, 11, t.usertype
  2663.  
  2664. go
  2665.  
  2666. if (charindex('6.00', @@version) = 0 and
  2667.     charindex('6.50', @@version) = 0 and
  2668.     charindex('7.00', @@version) = 0 and
  2669.     charindex('8.00', @@version) = 0)
  2670. begin
  2671.     print ''
  2672.     print ''
  2673.     print 'Warning:'
  2674.     print 'you are installing the stored procedures '
  2675.     print 'on a pre 6.0 SQL Server.'
  2676.     print 'Ignore the following errors.'
  2677. end
  2678. else
  2679.     drop proc sp_datatype_info
  2680. go
  2681.  
  2682. /*    Procedure for 6.0 and 6.50 servers */
  2683. create proc sp_datatype_info
  2684.     (@data_type int = 0, @ODBCVer tinyint = 2)
  2685. as
  2686.     if @ODBCVer <> 3
  2687.         select @ODBCVer = 2
  2688.     if @data_type = 0
  2689.         select
  2690.             convert(varchar(32),case
  2691.                 when t.usertype > 100 or t.usertype in (18,80) then t.name
  2692.                 else d.TYPE_NAME
  2693.             end) TYPE_NAME,
  2694.             d.DATA_TYPE,
  2695.             convert(int,case
  2696.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  2697.                 when d.ss_dtype in (35,34) then 2147483647                /* TEXT/IMAGE */
  2698.                 when d.ss_dtype in (55,63,106,108) and t.usertype <= 100 then @@max_precision /* DECIMAL/NUMERIC */
  2699.                 else t.prec
  2700.             end) "PRECISION",
  2701.             d.LITERAL_PREFIX,
  2702.             d.LITERAL_SUFFIX,
  2703.             e.CREATE_PARAMS,
  2704.             convert(smallint,case
  2705.                 when d.AUTO_INCREMENT = 1 then 0 /* IDENTITY*/
  2706.                 else t.allownulls
  2707.             end) NULLABLE,
  2708.             d.CASE_SENSITIVE,
  2709.             d.SEARCHABLE,
  2710.             d.UNSIGNED_ATTRIBUTE,
  2711.             d.MONEY,
  2712.             d.AUTO_INCREMENT,
  2713.             convert(varchar(32),case
  2714.                 when t.usertype > 100 or t.usertype in (18,80) then t.name
  2715.                 else d.TYPE_NAME
  2716.             end) LOCAL_TYPE_NAME,
  2717.             convert(smallint,case
  2718.                 when d.ss_dtype in (55,63,106,108) and t.usertype > 100 then t.scale
  2719.                 else d.numeric_scale
  2720.             end) MINIMUM_SCALE,
  2721.             convert(smallint,case
  2722.                 when d.ss_dtype in (106,108) and d.AUTO_INCREMENT = 0 then @@max_precision /* DECIMAL/NUMERIC */
  2723.                 when d.ss_dtype in (106,108) and d.AUTO_INCREMENT = 1 then 0 /* DECIMAL/NUMERIC IDENTITY*/
  2724.                 else t.scale
  2725.             end) MAXIMUM_SCALE,
  2726.             d.SQL_DATA_TYPE,
  2727.             d.SQL_DATETIME_SUB,
  2728.             NUM_PREC_RADIX = convert(int,d.RADIX),
  2729.             INTERVAL_PRECISION = convert(smallint,NULL),
  2730.             USERTYPE = t.usertype
  2731.         from master.dbo.spt_datatype_info d, master.dbo.spt_datatype_info_ext e, systypes t
  2732.         where
  2733.             d.ss_dtype = t.type
  2734.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2735.             and t.usertype *= e.user_type
  2736.             and isnull(d.AUTO_INCREMENT,0) *= e.AUTO_INCREMENT
  2737.             and (t.type not in (111,109,38,110,55,63) or /* get rid of nullable types */
  2738.                 t.usertype > 100)
  2739.             and (t.usertype <= 100 or
  2740.                 isnull(d.AUTO_INCREMENT,0) = 0)
  2741.         order by 2, 12, 11, t.usertype
  2742.  
  2743.     else
  2744.         select
  2745.             convert(varchar(32),case
  2746.                 when t.usertype > 100 or t.usertype in (18,80) then t.name
  2747.                 else d.TYPE_NAME
  2748.             end) TYPE_NAME,
  2749.             d.DATA_TYPE,
  2750.             convert(int,case
  2751.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  2752.                 when d.ss_dtype in (35,34) then 2147483647                /* TEXT/IMAGE */
  2753.                 when d.ss_dtype in (55,63,106,108) and t.usertype <= 100 then @@max_precision /* DECIMAL/NUMERIC */
  2754.                 else t.prec
  2755.             end) "PRECISION",
  2756.             d.LITERAL_PREFIX,
  2757.             d.LITERAL_SUFFIX,
  2758.             e.CREATE_PARAMS,
  2759.             convert(smallint,case
  2760.                 when d.AUTO_INCREMENT = 1 then 0 /* IDENTITY*/
  2761.                 else t.allownulls
  2762.             end) NULLABLE,
  2763.             d.CASE_SENSITIVE,
  2764.             d.SEARCHABLE,
  2765.             d.UNSIGNED_ATTRIBUTE,
  2766.             d.MONEY,
  2767.             d.AUTO_INCREMENT,
  2768.             convert(varchar(32),case
  2769.                 when t.usertype > 100 or t.usertype in (18,80) then t.name
  2770.                 else d.TYPE_NAME
  2771.             end) LOCAL_TYPE_NAME,
  2772.             convert(smallint,case
  2773.                 when d.ss_dtype in (55,63,106,108) and t.usertype > 100 then t.scale
  2774.                 else d.numeric_scale
  2775.             end) MINIMUM_SCALE,
  2776.             convert(smallint,case
  2777.                 when d.ss_dtype in (106,108) and d.AUTO_INCREMENT = 0 then @@max_precision /* DECIMAL/NUMERIC */
  2778.                 when d.ss_dtype in (106,108) and d.AUTO_INCREMENT = 1 then 0 /* DECIMAL/NUMERIC IDENTITY*/
  2779.                 else t.scale
  2780.             end) MAXIMUM_SCALE,
  2781.             d.SQL_DATA_TYPE,
  2782.             d.SQL_DATETIME_SUB,
  2783.             NUM_PREC_RADIX = convert(int,d.RADIX),
  2784.             INTERVAL_PRECISION = convert(smallint,NULL),
  2785.             USERTYPE = t.usertype
  2786.         from master.dbo.spt_datatype_info d, master.dbo.spt_datatype_info_ext e, systypes t
  2787.         where
  2788.             d.DATA_TYPE = @data_type
  2789.             and d.ss_dtype = t.type
  2790.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2791.             and t.usertype *= e.user_type
  2792.             and isnull(d.AUTO_INCREMENT,0) *= e.AUTO_INCREMENT
  2793.             and (t.type not in (111,109,38,110,55,63) or /* get rid of nullable types */
  2794.                 t.usertype > 100)
  2795.             and (t.usertype <= 100 or
  2796.                 isnull(d.AUTO_INCREMENT,0) = 0)
  2797.         order by 12, 11, t.usertype
  2798. go
  2799.  
  2800. if (charindex('7.00', @@version) = 0 and
  2801.     charindex('8.00', @@version) = 0)
  2802. begin
  2803.     print ''
  2804.     print ''
  2805.     print 'Warning:'
  2806.     print 'you are installing the stored procedures '
  2807.     print 'on a pre 7.0 SQL Server.'
  2808.     print 'Ignore the following errors.'
  2809. end
  2810. else
  2811.     drop proc sp_datatype_info
  2812. go
  2813.  
  2814. /*    Procedure for 7.0 server */
  2815. create proc sp_datatype_info
  2816.     (@data_type int = 0, @ODBCVer tinyint = 2)
  2817. as
  2818.     declare @mintype int
  2819.     declare @maxtype int
  2820.  
  2821.     if @ODBCVer <> 3
  2822.         select @ODBCVer = 2
  2823.     if @data_type = 0
  2824.     begin
  2825.         select @mintype = -32768
  2826.         select @maxtype = 32767
  2827.     end
  2828.     else
  2829.     begin
  2830.         select @mintype = @data_type
  2831.         select @maxtype = @data_type
  2832.     end
  2833.  
  2834.     select
  2835.         convert(sysname,case
  2836.             when t.xusertype > 255 then t.name
  2837.             else d.TYPE_NAME
  2838.         end) TYPE_NAME,
  2839.         d.DATA_TYPE,
  2840.         convert(int,case
  2841.             when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  2842.             when type_name(d.ss_dtype) IN ('numeric','decimal') and
  2843.                 t.xusertype <= 255 then @@max_precision /* DECIMAL/NUMERIC */
  2844.             else OdbcPrec(t.xtype, t.length, t.xprec)
  2845.         end) "PRECISION",
  2846.         d.LITERAL_PREFIX,
  2847.         d.LITERAL_SUFFIX,
  2848.         e.CREATE_PARAMS,
  2849.         convert(smallint,case
  2850.             when d.AUTO_INCREMENT = 1 then 0 /* IDENTITY*/
  2851.             else TypeProperty (t.name, 'AllowsNull')
  2852.         end) NULLABLE,
  2853.         d.CASE_SENSITIVE,
  2854.         d.SEARCHABLE,
  2855.         d.UNSIGNED_ATTRIBUTE,
  2856.         d.MONEY,
  2857.         d.AUTO_INCREMENT,
  2858.         convert(sysname,case
  2859.             when t.xusertype > 255 then t.name
  2860.             else d.LOCAL_TYPE_NAME
  2861.         end) LOCAL_TYPE_NAME,
  2862.         MINIMUM_SCALE = d.numeric_scale,
  2863.         convert(smallint,case
  2864.             when type_name(d.ss_dtype) IN ('numeric','decimal') and d.AUTO_INCREMENT = 0 and t.xusertype <= 255 then @@max_precision /* DECIMAL/NUMERIC */
  2865.             when type_name(d.ss_dtype) IN ('numeric','decimal') and d.AUTO_INCREMENT = 1 then 0 /* DECIMAL/NUMERIC IDENTITY*/
  2866.             else TypeProperty (t.name, 'Scale')
  2867.         end) MAXIMUM_SCALE,
  2868.         d.SQL_DATA_TYPE,
  2869.         d.SQL_DATETIME_SUB,
  2870.         NUM_PREC_RADIX = convert(int,d.RADIX),
  2871.         INTERVAL_PRECISION = convert(smallint,NULL),
  2872.         USERTYPE = t.usertype
  2873.     from master.dbo.spt_datatype_info d
  2874.         INNER JOIN systypes t on d.ss_dtype = t.xtype
  2875.         LEFT OUTER JOIN master.dbo.spt_datatype_info_ext e on
  2876.             t.xusertype = e.user_type
  2877.             and isnull(d.AUTO_INCREMENT,0) = e.AUTO_INCREMENT
  2878.     where
  2879.         d.DATA_TYPE between @mintype and @maxtype
  2880.         and (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2881.         and (t.xusertype <= 255 or
  2882.             isnull(d.AUTO_INCREMENT,0) = 0)
  2883.     order by 2, 12, 11,
  2884.     case
  2885.         when t.usertype=18 then 255
  2886.         else t.usertype
  2887.     end
  2888.  
  2889. go
  2890.  
  2891. if (charindex('8.00', @@version) = 0)
  2892. begin
  2893.     print ''
  2894.     print ''
  2895.     print 'Warning:'
  2896.     print 'you are installing the stored procedures '
  2897.     print 'on a pre 8.0 SQL Server.'
  2898.     print 'Ignore the following errors.'
  2899. end
  2900. else
  2901.     drop proc sp_datatype_info
  2902. go
  2903.  
  2904. /*    Procedure for 8.0 server */
  2905. create proc sp_datatype_info
  2906.     (@data_type int = 0, @ODBCVer tinyint = 2)
  2907. as
  2908.     declare @mintype int
  2909.     declare @maxtype int
  2910.  
  2911.     if @ODBCVer <> 3
  2912.         select @ODBCVer = 2
  2913.     if @data_type = 0
  2914.     begin
  2915.         select @mintype = -32768
  2916.         select @maxtype = 32767
  2917.     end
  2918.     else
  2919.     begin
  2920.         select @mintype = @data_type
  2921.         select @maxtype = @data_type
  2922.     end
  2923.  
  2924.     select
  2925.         convert(sysname,case
  2926.             when t.xusertype > 255 then t.name
  2927.             else d.TYPE_NAME collate database_default
  2928.         end) TYPE_NAME,
  2929.         d.DATA_TYPE,
  2930.         convert(int,case
  2931.             when d.DATA_TYPE in (6,7,-150) then d.data_precision         /* FLOAT/REAL/sql_variant*/
  2932.             when d.ss_dtype in (55,63,106,108) and
  2933.                 t.xusertype <= 255 then @@max_precision /* DECIMAL/NUMERIC */
  2934.             else OdbcPrec(t.xtype, t.length, t.xprec)
  2935.         end) "PRECISION",
  2936.         d.LITERAL_PREFIX,
  2937.         d.LITERAL_SUFFIX,
  2938.         e.CREATE_PARAMS,
  2939.         convert(smallint,case
  2940.             when d.AUTO_INCREMENT = 1 then 0 /* IDENTITY*/
  2941.             else TypeProperty (t.name, 'AllowsNull')
  2942.         end) NULLABLE,
  2943.         d.CASE_SENSITIVE,
  2944.         d.SEARCHABLE,
  2945.         d.UNSIGNED_ATTRIBUTE,
  2946.         d.MONEY,
  2947.         d.AUTO_INCREMENT,
  2948.         convert(sysname,case
  2949.             when t.xusertype > 255 then t.name
  2950.             else d.LOCAL_TYPE_NAME collate database_default
  2951.         end) LOCAL_TYPE_NAME,
  2952.         convert(smallint,case
  2953.             when d.ss_dtype in (55,63,106,108) and t.xusertype > 255 then TypeProperty (t.name, 'Scale')
  2954.             else d.numeric_scale
  2955.         end) MINIMUM_SCALE,
  2956.         convert(smallint,case
  2957.             when d.ss_dtype in (55,63,106,108) and d.AUTO_INCREMENT = 0 and t.xusertype <= 255 then @@max_precision /* DECIMAL/NUMERIC */
  2958.             when d.ss_dtype in (55,63,106,108) and d.AUTO_INCREMENT = 1 then 0 /* DECIMAL/NUMERIC IDENTITY*/
  2959.             else TypeProperty (t.name, 'Scale')
  2960.         end) MAXIMUM_SCALE,
  2961.         d.SQL_DATA_TYPE,
  2962.         d.SQL_DATETIME_SUB,
  2963.         NUM_PREC_RADIX = convert(int,d.RADIX),
  2964.         INTERVAL_PRECISION = convert(smallint,NULL),
  2965.         USERTYPE = t.usertype
  2966.     from master.dbo.spt_datatype_info d
  2967.         INNER JOIN systypes t on d.ss_dtype = t.xtype
  2968.         LEFT OUTER JOIN master.dbo.spt_datatype_info_ext e on
  2969.             t.xusertype = e.user_type
  2970.             and isnull(d.AUTO_INCREMENT,0) = e.AUTO_INCREMENT
  2971.     where
  2972.         d.DATA_TYPE between @mintype and @maxtype
  2973.         and (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2974.         and (t.xusertype <= 255 or
  2975.             isnull(d.AUTO_INCREMENT,0) = 0)
  2976.     order by 2, 12, 11,
  2977.     case
  2978.         when t.usertype=18 then 255
  2979.         else t.usertype
  2980.     end
  2981.  
  2982. go
  2983.  
  2984. grant execute on sp_datatype_info to public
  2985. go
  2986.  
  2987. dump tran master with no_log
  2988. go
  2989.  
  2990. print 'creating sp_fkeys'
  2991. go
  2992.  
  2993. /*    Procedure for pre-6.0 server */
  2994. CREATE PROCEDURE sp_fkeys(
  2995.                @pktable_name        varchar(32) = null,
  2996.                @pktable_owner        varchar(32) = null,
  2997.                @pktable_qualifier    varchar(32) = null,
  2998.                @fktable_name        varchar(32) = null,
  2999.                @fktable_owner        varchar(32) = null,
  3000.                @fktable_qualifier    varchar(32) = null )
  3001. as
  3002.     set nocount on
  3003.     declare    @order_by_pk int
  3004.  
  3005.     select  @order_by_pk = 0
  3006.  
  3007.     if (@pktable_name is null) and (@fktable_name is null)
  3008.     begin    /* If neither primary key nor foreign key table names given */
  3009.         raiserror 20004 '~~Rush_25~~'
  3010.         return
  3011.     end
  3012.     if @fktable_qualifier is not null
  3013.     begin
  3014.         if db_name() <> @fktable_qualifier
  3015.         begin    /* If qualifier doesn't match current database */
  3016.             raiserror 20001 '~~Rush_26~~'
  3017.             return
  3018.         end
  3019.     end
  3020.     if @pktable_qualifier is not null
  3021.     begin
  3022.         if db_name() <> @pktable_qualifier
  3023.         begin    /* If qualifier doesn't match current database */
  3024.             raiserror 20001 '~~Rush_27~~'
  3025.             return
  3026.         end
  3027.     end
  3028.  
  3029.     if @pktable_name is null
  3030.     begin /*  If table name not supplied, match all */
  3031.         select @pktable_name = '%'
  3032.         select @order_by_pk = 1
  3033.     end
  3034.     if @pktable_owner is null    /*    If PK owner not supplied, match all */
  3035.         select @pktable_owner = '%'
  3036.     if @fktable_name is null    /*    If table name not supplied, match all */
  3037.         select @fktable_name = '%'
  3038.     if @fktable_owner is null    /*    If FK owner not supplied, match all */
  3039.         select @fktable_owner = '%'
  3040.  
  3041.     if @@trancount <> 0
  3042.     begin    /* If inside a transaction */
  3043.         raiserror 20003 '~~Rush_28~~'
  3044.         return
  3045.     end
  3046.     create table #fkeys(
  3047.              PKTABLE_QUALIFIER    varchar(32) NULL,
  3048.              PKTABLE_OWNER        varchar(32) NULL,
  3049.              PKTABLE_NAME        varchar(32) NOT NULL,
  3050.              PKCOLUMN_NAME        varchar(32) NOT NULL,
  3051.              FKTABLE_QUALIFIER    varchar(32) NULL,
  3052.              FKTABLE_OWNER        varchar(32) NULL,
  3053.              FKTABLE_NAME        varchar(32) NOT NULL,
  3054.              FKCOLUMN_NAME        varchar(32) NOT NULL,
  3055.              KEY_SEQ            smallint NOT NULL)
  3056.  
  3057.     /*    SQL Server supports upto 8 PK/FK relationships between 2 tables */
  3058.     /*    Process syskeys for each relationship */
  3059.     /*    The inserts below adds a row to the temp table for each of the
  3060.         8 possible relationships */
  3061.     insert into #fkeys
  3062.         select
  3063.             db_name(),
  3064.             user_name(o1.uid),
  3065.             object_name(k.depid),
  3066.             c2.name,
  3067.             db_name(),
  3068.             user_name(o2.uid),
  3069.             object_name(k.id),
  3070.             c1.name,
  3071.             1
  3072.         from
  3073.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  3074.         where
  3075.             c1.id = k.id
  3076.             and k.type = 2    /* Foreign type key */
  3077.             and c1.colid = k.key1
  3078.             and c2.id = k.depid
  3079.             and c2.colid = k.depkey1
  3080.             and o1.id = k.depid
  3081.             and o2.id = k.id
  3082.     union all
  3083.         select
  3084.             db_name(),
  3085.             user_name(o1.uid),
  3086.             object_name(k.depid),
  3087.             c2.name,
  3088.             db_name(),
  3089.             user_name(o2.uid),
  3090.             object_name(k.id),
  3091.             c1.name,
  3092.             2
  3093.         from
  3094.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  3095.         where
  3096.             c1.id = k.id
  3097.             and k.type = 2    /* Foreign type key */
  3098.             and c1.colid = k.key2
  3099.             and c2.id = k.depid
  3100.             and c2.colid = k.depkey2
  3101.             and o1.id = k.depid
  3102.             and o2.id = k.id
  3103.     union all
  3104.         select
  3105.             db_name(),
  3106.             user_name(o1.uid),
  3107.             object_name(k.depid),
  3108.             c2.name,
  3109.             db_name(),
  3110.             user_name(o2.uid),
  3111.             object_name(k.id),
  3112.             c1.name,
  3113.             3
  3114.         from
  3115.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  3116.         where
  3117.             c1.id = k.id
  3118.             and k.type = 2    /* Foreign type key */
  3119.             and c1.colid = k.key3
  3120.             and c2.id = k.depid
  3121.             and c2.colid = k.depkey3
  3122.             and o1.id = k.depid
  3123.             and o2.id = k.id
  3124.     union all
  3125.         select
  3126.             db_name(),
  3127.             user_name(o1.uid),
  3128.             object_name(k.depid),
  3129.             c2.name,
  3130.             db_name(),
  3131.             user_name(o2.uid),
  3132.             object_name(k.id),
  3133.             c1.name,
  3134.             4
  3135.         from
  3136.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  3137.         where
  3138.             c1.id = k.id
  3139.             and k.type = 2    /* Foreign type key */
  3140.             and c1.colid = k.key4
  3141.             and c2.id = k.depid
  3142.             and c2.colid = k.depkey4
  3143.             and o1.id = k.depid
  3144.             and o2.id = k.id
  3145.     union all
  3146.         select
  3147.             db_name(),
  3148.             user_name(o1.uid),
  3149.             object_name(k.depid),
  3150.             c2.name,
  3151.             db_name(),
  3152.             user_name(o2.uid),
  3153.             object_name(k.id),
  3154.             c1.name,
  3155.             5
  3156.         from
  3157.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  3158.         where
  3159.             c1.id = k.id
  3160.             and k.type = 2    /* Foreign type key */
  3161.             and c1.colid = k.key5
  3162.             and c2.id = k.depid
  3163.             and c2.colid = k.depkey5
  3164.             and o1.id = k.depid
  3165.             and o2.id = k.id
  3166.     union all
  3167.         select
  3168.             db_name(),
  3169.             user_name(o1.uid),
  3170.             object_name(k.depid),
  3171.             c2.name,
  3172.             db_name(),
  3173.             user_name(o2.uid),
  3174.             object_name(k.id),
  3175.             c1.name,
  3176.             6
  3177.         from
  3178.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  3179.         where
  3180.             c1.id = k.id
  3181.             and k.type = 2    /* Foreign type key */
  3182.             and c1.colid = k.key6
  3183.             and c2.id = k.depid
  3184.             and c2.colid = k.depkey6
  3185.             and o1.id = k.depid
  3186.             and o2.id = k.id
  3187.     union all
  3188.         select
  3189.             db_name(),
  3190.             user_name(o1.uid),
  3191.             object_name(k.depid),
  3192.             c2.name,
  3193.             db_name(),
  3194.             user_name(o2.uid),
  3195.             object_name(k.id),
  3196.             c1.name,
  3197.             7
  3198.         from
  3199.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  3200.         where
  3201.             c1.id = k.id
  3202.             and k.type = 2    /* Foreign type key */
  3203.             and c1.colid = k.key7
  3204.             and c2.id = k.depid
  3205.             and c2.colid = k.depkey7
  3206.             and o1.id = k.depid
  3207.             and o2.id = k.id
  3208.     union all
  3209.         select
  3210.             db_name(),
  3211.             user_name(o1.uid),
  3212.             object_name(k.depid),
  3213.             c2.name,
  3214.             db_name(),
  3215.             user_name(o2.uid),
  3216.             object_name(k.id),
  3217.             c1.name,
  3218.             8
  3219.         from
  3220.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  3221.         where
  3222.             c1.id = k.id
  3223.             and k.type = 2    /* Foreign type key */
  3224.             and c1.colid = k.key8
  3225.             and c2.id = k.depid
  3226.             and c2.colid = k.depkey8
  3227.             and o1.id = k.depid
  3228.             and o2.id = k.id
  3229.  
  3230.     if @order_by_pk = 1 /*    If order by PK fields */
  3231.         select
  3232.             PKTABLE_QUALIFIER,
  3233.             PKTABLE_OWNER,
  3234.             PKTABLE_NAME,
  3235.             PKCOLUMN_NAME,
  3236.             FKTABLE_QUALIFIER,
  3237.             FKTABLE_OWNER,
  3238.             FKTABLE_NAME,
  3239.             FKCOLUMN_NAME,
  3240.             KEY_SEQ,
  3241.             UPDATE_RULE = convert(smallint, null),
  3242.             DELETE_RULE = convert(smallint,null),
  3243.             FK_NAME = convert(varchar(32),null),
  3244.             PK_NAME = convert(varchar(32),null),
  3245.             DEFERRABILITY = convert(smallint,7)
  3246.         from #fkeys
  3247.         where FKTABLE_NAME like @fktable_name
  3248.             and FKTABLE_OWNER like @fktable_owner
  3249.             and PKTABLE_NAME  like @pktable_name
  3250.             and PKTABLE_OWNER like @pktable_owner
  3251.         order by 1, 2, 3, 9, 4
  3252.     else        /*    Order by FK fields */
  3253.         select
  3254.             PKTABLE_QUALIFIER,
  3255.             PKTABLE_OWNER,
  3256.             PKTABLE_NAME,
  3257.             PKCOLUMN_NAME,
  3258.             FKTABLE_QUALIFIER,
  3259.             FKTABLE_OWNER,
  3260.             FKTABLE_NAME,
  3261.             FKCOLUMN_NAME,
  3262.             KEY_SEQ,
  3263.             UPDATE_RULE = convert(smallint,null),
  3264.             DELETE_RULE = convert(smallint,null),
  3265.             FK_NAME = convert(varchar(32),null),
  3266.             PK_NAME = convert(varchar(32),null),
  3267.             DEFERRABILITY = convert(smallint,7)
  3268.         from #fkeys
  3269.         where FKTABLE_NAME like @fktable_name
  3270.             and FKTABLE_OWNER like @fktable_owner
  3271.             and PKTABLE_NAME  like @pktable_name
  3272.             and PKTABLE_OWNER like @pktable_owner
  3273.         order by 5, 6, 7, 9, 8
  3274. go
  3275.  
  3276. if (charindex('6.00', @@version) = 0 and
  3277.     charindex('6.50', @@version) = 0 and
  3278.     charindex('7.00', @@version) = 0 and
  3279.     charindex('8.00', @@version) = 0)
  3280. begin
  3281.     print ''
  3282.     print ''
  3283.     print 'Warning:'
  3284.     print 'you are installing the stored procedures '
  3285.     print 'on a pre 6.0 SQL Server.'
  3286.     print 'Ignore the following errors.'
  3287. end
  3288. else
  3289.     drop proc sp_fkeys
  3290. go
  3291.  
  3292. /*    Procedure for 6.0 and 6.50 servers */
  3293. CREATE PROCEDURE sp_fkeys(
  3294.                @pktable_name        varchar(32) = null,
  3295.                @pktable_owner        varchar(32) = null,
  3296.                @pktable_qualifier    varchar(32) = null,
  3297.                @fktable_name        varchar(32) = null,
  3298.                @fktable_owner        varchar(32) = null,
  3299.                @fktable_qualifier    varchar(32) = null )
  3300. as
  3301.     set nocount on
  3302.     DECLARE @pktable_id            int
  3303.     DECLARE @pkfull_table_name    varchar(65) /* 2*32 +1 */
  3304.     DECLARE @fktable_id            int
  3305.     DECLARE @fkfull_table_name    varchar(65) /* 2*32 +1 */
  3306.     declare    @order_by_pk        int
  3307.  
  3308.     select  @order_by_pk = 0
  3309.  
  3310.     if (@pktable_name is null) and (@fktable_name is null)
  3311.     begin    /* If neither primary key nor foreign key table names given */
  3312.         raiserror (15252,-1,-1)
  3313.         return
  3314.     end
  3315.     if @fktable_qualifier is not null
  3316.     begin
  3317.         if db_name() <> @fktable_qualifier
  3318.         begin    /* If qualifier doesn't match current database */
  3319.             raiserror (15250, -1,-1)
  3320.             return
  3321.         end
  3322.     end
  3323.     if @pktable_qualifier is not null
  3324.     begin
  3325.         if db_name() <> @pktable_qualifier
  3326.         begin    /* If qualifier doesn't match current database */
  3327.             raiserror (15250, -1,-1)
  3328.             return
  3329.         end
  3330.     end
  3331.  
  3332.     if @pktable_owner is null
  3333.     begin    /* If unqualified primary key table name */
  3334.         SELECT @pkfull_table_name = @pktable_name
  3335.     end
  3336.     else
  3337.     begin    /* Qualified primary key table name */
  3338.         SELECT @pkfull_table_name = @pktable_owner + '.' + @pktable_name
  3339.     end
  3340.     /*    Get Object ID */
  3341.     SELECT @pktable_id = object_id(@pkfull_table_name)
  3342.  
  3343.     if @fktable_owner is null
  3344.     begin    /* If unqualified foreign key table name */
  3345.         SELECT @fkfull_table_name = @fktable_name
  3346.     end
  3347.     else
  3348.     begin    /* Qualified foreign key table name */
  3349.         SELECT @fkfull_table_name = @fktable_owner + '.' + @fktable_name
  3350.     end
  3351.     /*    Get Object ID */
  3352.     SELECT @fktable_id = object_id(@fkfull_table_name)
  3353.  
  3354.     if @fktable_name is not null
  3355.     begin
  3356.         if @fktable_id is null
  3357.             SELECT @fktable_id = 0    /* fk table not found, empty result */
  3358.     end
  3359.  
  3360.     if @pktable_name is null
  3361.     begin /*  If table name not supplied, match all */
  3362.         select @order_by_pk = 1
  3363.     end
  3364.     else
  3365.     begin
  3366.         if @pktable_id is null
  3367.         begin
  3368.             SELECT @pktable_id = 0    /* pk table not found, empty result */
  3369.         end
  3370.     end
  3371.  
  3372.     if (@@trancount <> 0 and
  3373.         charindex('6.50', @@version) = 0)
  3374.     begin    /* If inside a transaction */
  3375.         raiserror (15002,-1,-1,'sp_fkeys')
  3376.         return
  3377.     end
  3378.  
  3379.     create table #fkeys(
  3380.              pkdb_id        int NOT NULL,
  3381.              pktable_id     int NOT NULL,
  3382.              pkcolid        int NOT NULL,
  3383.              fkdb_id        int NOT NULL,
  3384.              fktable_id        int NOT NULL,
  3385.              fkcolid        int NOT NULL,
  3386.              KEY_SEQ        smallint NOT NULL,
  3387.              fk_id            int NOT NULL,
  3388.              pk_id            int NOT NULL)
  3389.  
  3390.     /*    SQL Server supports upto 16 PK/FK relationships between 2 tables */
  3391.     /*    Process syskeys for each relationship */
  3392.     /*    The inserts below adds a row to the temp table for each of the
  3393.         16 possible relationships */
  3394.     insert into #fkeys
  3395.         select
  3396.             r.rkeydbid,
  3397.             r.rkeyid,
  3398.             r.rkey1,
  3399.             r.fkeydbid,
  3400.             r.fkeyid,
  3401.             r.fkey1,
  3402.             1,
  3403.             r.constid,
  3404.             s.constid
  3405.         from
  3406.             sysreferences r, sysconstraints s
  3407.         where    r.rkeyid = s.id
  3408.             AND (s.status & 0xf) = 1
  3409.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3410.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3411.       union all
  3412.         select
  3413.             r.rkeydbid,
  3414.             r.rkeyid,
  3415.             r.rkey2,
  3416.             r.fkeydbid,
  3417.             r.fkeyid,
  3418.             r.fkey2,
  3419.             2,
  3420.             r.constid,
  3421.             s.constid
  3422.         from
  3423.             sysreferences r, sysconstraints s
  3424.         where    r.rkeyid = s.id
  3425.             AND (s.status & 0xf) = 1
  3426.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3427.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3428.       union all
  3429.         select
  3430.             r.rkeydbid,
  3431.             r.rkeyid,
  3432.             r.rkey3,
  3433.             r.fkeydbid,
  3434.             r.fkeyid,
  3435.             r.fkey3,
  3436.             3,
  3437.             r.constid,
  3438.             s.constid
  3439.         from
  3440.             sysreferences r, sysconstraints s
  3441.         where    r.rkeyid = s.id
  3442.             AND (s.status & 0xf) = 1
  3443.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3444.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3445.       union all
  3446.         select
  3447.             r.rkeydbid,
  3448.             r.rkeyid,
  3449.             r.rkey4,
  3450.             r.fkeydbid,
  3451.             r.fkeyid,
  3452.             r.fkey4,
  3453.             4,
  3454.             r.constid,
  3455.             s.constid
  3456.         from
  3457.             sysreferences r, sysconstraints s
  3458.         where    r.rkeyid = s.id
  3459.             AND (s.status & 0xf) = 1
  3460.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3461.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3462.       union all
  3463.         select
  3464.             r.rkeydbid,
  3465.             r.rkeyid,
  3466.             r.rkey5,
  3467.             r.fkeydbid,
  3468.             r.fkeyid,
  3469.             r.fkey5,
  3470.             5,
  3471.             r.constid,
  3472.             s.constid
  3473.         from
  3474.             sysreferences r, sysconstraints s
  3475.         where    r.rkeyid = s.id
  3476.             AND (s.status & 0xf) = 1
  3477.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3478.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3479.       union all
  3480.         select
  3481.             r.rkeydbid,
  3482.             r.rkeyid,
  3483.             r.rkey6,
  3484.             r.fkeydbid,
  3485.             r.fkeyid,
  3486.             r.fkey6,
  3487.             6,
  3488.             r.constid,
  3489.             s.constid
  3490.         from
  3491.             sysreferences r, sysconstraints s
  3492.         where    r.rkeyid = s.id
  3493.             AND (s.status & 0xf) = 1
  3494.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3495.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3496.       union all
  3497.         select
  3498.             r.rkeydbid,
  3499.             r.rkeyid,
  3500.             r.rkey7,
  3501.             r.fkeydbid,
  3502.             r.fkeyid,
  3503.             r.fkey7,
  3504.             7,
  3505.             r.constid,
  3506.             s.constid
  3507.         from
  3508.             sysreferences r, sysconstraints s
  3509.         where    r.rkeyid = s.id
  3510.             AND (s.status & 0xf) = 1
  3511.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3512.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3513.       union all
  3514.         select
  3515.             r.rkeydbid,
  3516.             r.rkeyid,
  3517.             r.rkey8,
  3518.             r.fkeydbid,
  3519.             r.fkeyid,
  3520.             r.fkey8,
  3521.             8,
  3522.             r.constid,
  3523.             s.constid
  3524.         from
  3525.             sysreferences r, sysconstraints s
  3526.         where    r.rkeyid = s.id
  3527.             AND (s.status & 0xf) = 1
  3528.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3529.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3530.       union all
  3531.         select
  3532.             r.rkeydbid,
  3533.             r.rkeyid,
  3534.             r.rkey9,
  3535.             r.fkeydbid,
  3536.             r.fkeyid,
  3537.             r.fkey9,
  3538.             9,
  3539.             r.constid,
  3540.             s.constid
  3541.         from
  3542.             sysreferences r, sysconstraints s
  3543.         where    r.rkeyid = s.id
  3544.             AND (s.status & 0xf) = 1
  3545.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3546.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3547.       union all
  3548.         select
  3549.             r.rkeydbid,
  3550.             r.rkeyid,
  3551.             r.rkey10,
  3552.             r.fkeydbid,
  3553.             r.fkeyid,
  3554.             r.fkey10,
  3555.             10,
  3556.             r.constid,
  3557.             s.constid
  3558.         from
  3559.             sysreferences r, sysconstraints s
  3560.         where    r.rkeyid = s.id
  3561.             AND (s.status & 0xf) = 1
  3562.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3563.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3564.       union all
  3565.         select
  3566.             r.rkeydbid,
  3567.             r.rkeyid,
  3568.             r.rkey11,
  3569.             r.fkeydbid,
  3570.             r.fkeyid,
  3571.             r.fkey11,
  3572.             11,
  3573.             r.constid,
  3574.             s.constid
  3575.         from
  3576.             sysreferences r, sysconstraints s
  3577.         where    r.rkeyid = s.id
  3578.             AND (s.status & 0xf) = 1
  3579.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3580.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3581.       union all
  3582.         select
  3583.             r.rkeydbid,
  3584.             r.rkeyid,
  3585.             r.rkey12,
  3586.             r.fkeydbid,
  3587.             r.fkeyid,
  3588.             r.fkey12,
  3589.             12,
  3590.             r.constid,
  3591.             s.constid
  3592.         from
  3593.             sysreferences r, sysconstraints s
  3594.         where    r.rkeyid = s.id
  3595.             AND (s.status & 0xf) = 1
  3596.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3597.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3598.       union all
  3599.         select
  3600.             r.rkeydbid,
  3601.             r.rkeyid,
  3602.             r.rkey13,
  3603.             r.fkeydbid,
  3604.             r.fkeyid,
  3605.             r.fkey13,
  3606.             13,
  3607.             r.constid,
  3608.             s.constid
  3609.         from
  3610.             sysreferences r, sysconstraints s
  3611.         where    r.rkeyid = s.id
  3612.             AND (s.status & 0xf) = 1
  3613.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3614.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3615.       union all
  3616.         select
  3617.             r.rkeydbid,
  3618.             r.rkeyid,
  3619.             r.rkey14,
  3620.             r.fkeydbid,
  3621.             r.fkeyid,
  3622.             r.fkey14,
  3623.             14,
  3624.             r.constid,
  3625.             s.constid
  3626.         from
  3627.             sysreferences r, sysconstraints s
  3628.         where    r.rkeyid = s.id
  3629.             AND (s.status & 0xf) = 1
  3630.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3631.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3632.       union all
  3633.         select
  3634.             r.rkeydbid,
  3635.             r.rkeyid,
  3636.             r.rkey15,
  3637.             r.fkeydbid,
  3638.             r.fkeyid,
  3639.             r.fkey15,
  3640.             15,
  3641.             r.constid,
  3642.             s.constid
  3643.         from
  3644.             sysreferences r, sysconstraints s
  3645.         where    r.rkeyid = s.id
  3646.             AND (s.status & 0xf) = 1
  3647.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3648.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3649.       union all
  3650.         select
  3651.             r.rkeydbid,
  3652.             r.rkeyid,
  3653.             r.rkey16,
  3654.             r.fkeydbid,
  3655.             r.fkeyid,
  3656.             r.fkey16,
  3657.             16,
  3658.             r.constid,
  3659.             s.constid
  3660.         from
  3661.             sysreferences r, sysconstraints s
  3662.         where    r.rkeyid = s.id
  3663.             AND (s.status & 0xf) = 1
  3664.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3665.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3666.  
  3667.     if @order_by_pk = 1 /*    If order by PK fields */
  3668.         select
  3669.             PKTABLE_QUALIFIER = convert(varchar(32),DB_NAME(f.pkdb_id)),
  3670.             PKTABLE_OWNER = convert(varchar(32),USER_NAME(o1.uid)),
  3671.             PKTABLE_NAME = convert(varchar(32),o1.name),
  3672.             PKCOLUMN_NAME = convert(varchar(32),c1.name),
  3673.             FKTABLE_QUALIFIER = convert(varchar(32),DB_NAME(f.fkdb_id)),
  3674.             FKTABLE_OWNER = convert(varchar(32),USER_NAME(o2.uid)),
  3675.             FKTABLE_NAME = convert(varchar(32),o2.name),
  3676.             FKCOLUMN_NAME = convert(varchar(32),c2.name),
  3677.             KEY_SEQ,
  3678.             UPDATE_RULE = convert(smallint,1),
  3679.             DELETE_RULE = convert(smallint,1),
  3680.             FK_NAME = convert(varchar(32),OBJECT_NAME(fk_id)),
  3681.             PK_NAME = convert(varchar(32),OBJECT_NAME(pk_id)),
  3682.             DEFERRABILITY = convert(smallint,7)
  3683.         from #fkeys f,
  3684.             sysobjects o1, sysobjects o2,
  3685.             syscolumns c1, syscolumns c2
  3686.         where    o1.id = f.pktable_id
  3687.             AND o2.id = f.fktable_id
  3688.             AND c1.id = f.pktable_id
  3689.             AND c2.id = f.fktable_id
  3690.             AND c1.colid = f.pkcolid
  3691.             AND c2.colid = f.fkcolid
  3692.         order by 1,2,3,9,4
  3693.     else        /*    Order by FK fields */
  3694.         select
  3695.             PKTABLE_QUALIFIER = convert(varchar(32),DB_NAME(f.pkdb_id)),
  3696.             PKTABLE_OWNER = convert(varchar(32),USER_NAME(o1.uid)),
  3697.             PKTABLE_NAME = convert(varchar(32),o1.name),
  3698.             PKCOLUMN_NAME = convert(varchar(32),c1.name),
  3699.             FKTABLE_QUALIFIER = convert(varchar(32),DB_NAME(f.fkdb_id)),
  3700.             FKTABLE_OWNER = convert(varchar(32),USER_NAME(o2.uid)),
  3701.             FKTABLE_NAME = convert(varchar(32),o2.name),
  3702.             FKCOLUMN_NAME = convert(varchar(32),c2.name),
  3703.             KEY_SEQ,
  3704.             UPDATE_RULE = convert(smallint,1),
  3705.             DELETE_RULE = convert(smallint,1),
  3706.             FK_NAME = convert(varchar(32),OBJECT_NAME(fk_id)),
  3707.             PK_NAME = convert(varchar(32),OBJECT_NAME(pk_id)),
  3708.             DEFERRABILITY = convert(smallint,7)
  3709.         from #fkeys f,
  3710.             sysobjects o1, sysobjects o2,
  3711.             syscolumns c1, syscolumns c2
  3712.         where    o1.id = f.pktable_id
  3713.             AND o2.id = f.fktable_id
  3714.             AND c1.id = f.pktable_id
  3715.             AND c2.id = f.fktable_id
  3716.             AND c1.colid = f.pkcolid
  3717.             AND c2.colid = f.fkcolid
  3718.         order by 5,6,7,9,8
  3719. go
  3720.  
  3721. if (charindex('7.00', @@version) = 0 and
  3722.     charindex('8.00', @@version) = 0)
  3723. begin
  3724.     print ''
  3725.     print ''
  3726.     print 'Warning:'
  3727.     print 'you are installing the stored procedures '
  3728.     print 'on a pre 7.0 SQL Server.'
  3729.     print 'Ignore the following errors.'
  3730. end
  3731. else
  3732.     drop proc sp_fkeys
  3733. go
  3734.  
  3735. /*    Procedure for 7.0 server */
  3736. CREATE PROCEDURE sp_fkeys(
  3737.                @pktable_name        sysname = null,
  3738.                @pktable_owner        sysname = null,
  3739.                @pktable_qualifier    sysname = null,
  3740.                @fktable_name        sysname = null,
  3741.                @fktable_owner        sysname = null,
  3742.                @fktable_qualifier    sysname = null )
  3743. as
  3744.     set nocount on
  3745.     DECLARE @pktable_id            int
  3746.     DECLARE @pkfull_table_name    nvarchar(257) /* 2*128 + 1 */
  3747.     DECLARE @fktable_id            int
  3748.     DECLARE @fkfull_table_name    nvarchar(257) /* 2*128 + 1 */
  3749.     declare    @order_by_pk        int
  3750.  
  3751.     /* select 'XXX starting table creation' */
  3752.  
  3753.     create table #fkeysall(
  3754.             rkeyid int NOT NULL,
  3755.             rkey1 int NOT NULL,
  3756.                 rkey2 int NOT NULL,
  3757.                 rkey3 int NOT NULL,
  3758.                 rkey4 int NOT NULL,
  3759.                 rkey5 int NOT NULL,
  3760.                 rkey6 int NOT NULL,
  3761.                 rkey7 int NOT NULL,
  3762.                 rkey8 int NOT NULL,
  3763.                 rkey9 int NOT NULL,
  3764.                 rkey10 int NOT NULL,
  3765.                 rkey11 int NOT NULL,
  3766.                 rkey12 int NOT NULL,
  3767.                 rkey13 int NOT NULL,
  3768.                 rkey14 int NOT NULL,
  3769.                 rkey15 int NOT NULL,
  3770.                 rkey16 int NOT NULL,
  3771.             fkeyid int NOT NULL,
  3772.             fkey1 int NOT NULL,
  3773.                 fkey2 int NOT NULL,
  3774.                 fkey3 int NOT NULL,
  3775.                 fkey4 int NOT NULL,
  3776.                 fkey5 int NOT NULL,
  3777.                 fkey6 int NOT NULL,
  3778.                 fkey7 int NOT NULL,
  3779.                 fkey8 int NOT NULL,
  3780.                 fkey9 int NOT NULL,
  3781.                 fkey10 int NOT NULL,
  3782.                 fkey11 int NOT NULL,
  3783.                 fkey12 int NOT NULL,
  3784.                 fkey13 int NOT NULL,
  3785.                 fkey14 int NOT NULL,
  3786.                 fkey15 int NOT NULL,
  3787.                 fkey16 int NOT NULL,
  3788.             constid int NOT NULL,
  3789.             name sysname NOT NULL)
  3790.  
  3791.     create table #fkeys(
  3792.             pktable_id        int NOT NULL,
  3793.             pkcolid         int NOT NULL,
  3794.             fktable_id        int NOT NULL,
  3795.             fkcolid         int NOT NULL,
  3796.             KEY_SEQ         smallint NOT NULL,
  3797.             fk_id            int NOT NULL,
  3798.             PK_NAME            sysname NOT NULL)
  3799.  
  3800.     create table #fkeysout(
  3801.             PKTABLE_QUALIFIER sysname NULL,
  3802.             PKTABLE_OWNER sysname NULL,
  3803.             PKTABLE_NAME sysname NOT NULL,
  3804.             PKCOLUMN_NAME sysname NOT NULL,
  3805.             FKTABLE_QUALIFIER sysname NULL,
  3806.             FKTABLE_OWNER sysname NULL,
  3807.             FKTABLE_NAME sysname NOT NULL,
  3808.             FKCOLUMN_NAME sysname NOT NULL,
  3809.             KEY_SEQ smallint NOT NULL,
  3810.             UPDATE_RULE smallint NULL,
  3811.             DELETE_RULE smallint NULL,
  3812.             FK_NAME sysname NULL,
  3813.             PK_NAME sysname NULL,
  3814.             DEFERRABILITY smallint null)
  3815.  
  3816.     /* select 'XXX starting parameter analysis' */
  3817.  
  3818.     select  @order_by_pk = 0
  3819.  
  3820.     if (@pktable_name is null) and (@fktable_name is null)
  3821.     begin    /* If neither primary key nor foreign key table names given */
  3822.         raiserror (15252,-1,-1)
  3823.         return
  3824.     end
  3825.     if @fktable_qualifier is not null
  3826.     begin
  3827.         if db_name() <> @fktable_qualifier
  3828.         begin    /* If qualifier doesn't match current database */
  3829.             raiserror (15250, -1,-1)
  3830.             return
  3831.         end
  3832.     end
  3833.     if @pktable_qualifier is not null
  3834.     begin
  3835.         if db_name() <> @pktable_qualifier
  3836.         begin    /* If qualifier doesn't match current database */
  3837.             raiserror (15250, -1,-1)
  3838.             return
  3839.         end
  3840.     end
  3841.  
  3842.     if @pktable_owner is null
  3843.     begin    /* If unqualified primary key table name */
  3844.         SELECT @pkfull_table_name = quotename(@pktable_name)
  3845.     end
  3846.     else
  3847.     begin    /* Qualified primary key table name */
  3848.         if @pktable_owner = ''
  3849.         begin    /* If empty owner name */
  3850.             SELECT @pkfull_table_name = quotename(@pktable_owner)
  3851.         end
  3852.         else
  3853.         begin
  3854.             SELECT @pkfull_table_name = quotename(@pktable_owner) +
  3855.                 '.' + quotename(@pktable_name)
  3856.         end
  3857.     end
  3858.     /*    Get Object ID */
  3859.     SELECT @pktable_id = object_id(@pkfull_table_name)
  3860.  
  3861.     if @fktable_owner is null
  3862.     begin    /* If unqualified foreign key table name */
  3863.         SELECT @fkfull_table_name = quotename(@fktable_name)
  3864.     end
  3865.     else
  3866.     begin    /* Qualified foreign key table name */
  3867.         if @fktable_owner = ''
  3868.         begin    /* If empty owner name */
  3869.             SELECT @fkfull_table_name = quotename(@fktable_owner)
  3870.         end
  3871.         else
  3872.         begin
  3873.             SELECT @fkfull_table_name = quotename(@fktable_owner) +
  3874.                 '.' + quotename(@fktable_name)
  3875.         end
  3876.     end
  3877.     /*    Get Object ID */
  3878.     SELECT @fktable_id = object_id(@fkfull_table_name)
  3879.  
  3880.     if @fktable_name is not null
  3881.     begin
  3882.         if @fktable_id is null
  3883.             SELECT @fktable_id = 0    /* fk table not found, empty result */
  3884.     end
  3885.  
  3886.     if @pktable_name is null
  3887.     begin /*  If table name not supplied, match all */
  3888.         select @order_by_pk = 1
  3889.     end
  3890.     else
  3891.     begin
  3892.         if @pktable_id is null
  3893.         begin
  3894.             SELECT @pktable_id = 0    /* pk table not found, empty result */
  3895.         end
  3896.     end
  3897.  
  3898.     /*    SQL Server supports upto 16 PK/FK relationships between 2 tables */
  3899.     /*    Process syskeys for each relationship */
  3900.     /*  First, attempt to get all 16 keys for each rel'ship, then sort
  3901.         them out with a 16-way "insert select ... union select ..." */
  3902.  
  3903.     /* select 'XXX starting data analysis' */
  3904.  
  3905.     insert into #fkeysall
  3906.         select
  3907.             r.rkeyid,
  3908.             r.rkey1, r.rkey2, r.rkey3, r.rkey4,
  3909.                 r.rkey5, r.rkey6, r.rkey7, r.rkey8,
  3910.                 r.rkey9, r.rkey10, r.rkey11, r.rkey12,
  3911.                 r.rkey13, r.rkey14, r.rkey15, r.rkey16,
  3912.             r.fkeyid,
  3913.             r.fkey1, r.fkey2, r.fkey3, r.fkey4,
  3914.                 r.fkey5, r.fkey6, r.fkey7, r.fkey8,
  3915.                 r.fkey9, r.fkey10, r.fkey11, r.fkey12,
  3916.                 r.fkey13, r.fkey14, r.fkey15, r.fkey16,
  3917.             r.constid,
  3918.             i.name
  3919.         from
  3920.             sysreferences r, sysobjects o, sysindexes i
  3921.         where    r.constid = o.id
  3922.             AND o.xtype = 'F'
  3923.             AND r.rkeyindid = i.indid
  3924.             AND r.rkeyid = i.id
  3925.             AND r.rkeyid between isnull(@pktable_id, 0)
  3926.                             and isnull(@pktable_id, 0x7fffffff)
  3927.             AND r.fkeyid between isnull(@fktable_id, 0)
  3928.                             and isnull(@fktable_id, 0x7fffffff)
  3929.  
  3930.     /* select count (*) as 'XXX countall' from #fkeysall */
  3931.  
  3932.     insert into #fkeys
  3933.             select rkeyid, rkey1, fkeyid, fkey1, 1, constid, name
  3934.             from #fkeysall
  3935.         union all
  3936.             select rkeyid, rkey2, fkeyid, fkey2, 2, constid, name
  3937.             from #fkeysall
  3938.         union all
  3939.             select rkeyid, rkey3, fkeyid, fkey3, 3, constid, name
  3940.             from #fkeysall
  3941.         union all
  3942.             select rkeyid, rkey4, fkeyid, fkey4, 4, constid, name
  3943.             from #fkeysall
  3944.         union all
  3945.             select rkeyid, rkey5, fkeyid, fkey5, 5, constid, name
  3946.             from #fkeysall
  3947.         union all
  3948.             select rkeyid, rkey6, fkeyid, fkey6, 6, constid, name
  3949.             from #fkeysall
  3950.         union all
  3951.             select rkeyid, rkey7, fkeyid, fkey7, 7, constid, name
  3952.             from #fkeysall
  3953.         union all
  3954.             select rkeyid, rkey8, fkeyid, fkey8, 8, constid, name
  3955.             from #fkeysall
  3956.         union all
  3957.             select rkeyid, rkey9, fkeyid, fkey9, 9, constid, name
  3958.             from #fkeysall
  3959.         union all
  3960.             select rkeyid, rkey10, fkeyid, fkey10, 10, constid, name
  3961.             from #fkeysall
  3962.         union all
  3963.             select rkeyid, rkey11, fkeyid, fkey11, 11, constid, name
  3964.             from #fkeysall
  3965.         union all
  3966.             select rkeyid, rkey12, fkeyid, fkey12, 12, constid, name
  3967.             from #fkeysall
  3968.         union all
  3969.             select rkeyid, rkey13, fkeyid, fkey13, 13, constid, name
  3970.             from #fkeysall
  3971.         union all
  3972.             select rkeyid, rkey14, fkeyid, fkey14, 14, constid, name
  3973.             from #fkeysall
  3974.         union all
  3975.             select rkeyid, rkey15, fkeyid, fkey15, 15, constid, name
  3976.             from #fkeysall
  3977.         union all
  3978.             select rkeyid, rkey16, fkeyid, fkey16, 16, constid, name
  3979.             from #fkeysall
  3980.  
  3981.     /* select count (*) as 'XXX count' from #fkeys */
  3982.  
  3983.     insert into #fkeysout
  3984.         select
  3985.             PKTABLE_QUALIFIER = convert(sysname,db_name()),
  3986.             PKTABLE_OWNER = convert(sysname,USER_NAME(o1.uid)),
  3987.             PKTABLE_NAME = convert(sysname,o1.name),
  3988.             PKCOLUMN_NAME = convert(sysname,c1.name),
  3989.             FKTABLE_QUALIFIER = convert(sysname,db_name()),
  3990.             FKTABLE_OWNER = convert(sysname,USER_NAME(o2.uid)),
  3991.             FKTABLE_NAME = convert(sysname,o2.name),
  3992.             FKCOLUMN_NAME = convert(sysname,c2.name),
  3993.             KEY_SEQ,
  3994.             UPDATE_RULE = CASE WHEN (ObjectProperty(fk_id, 'CnstIsUpdateCascade')=1) THEN 
  3995.                 convert(smallint,0) ELSE convert(smallint,1) END,
  3996.             DELETE_RULE = CASE WHEN (ObjectProperty(fk_id, 'CnstIsDeleteCascade')=1) THEN 
  3997.                 convert(smallint,0) ELSE convert(smallint,1) END,
  3998.             FK_NAME = convert(sysname,OBJECT_NAME(fk_id)),
  3999.             PK_NAME,
  4000.             DEFERRABILITY = 7    /* SQL_NOT_DEFERRABLE */
  4001.         from #fkeys f,
  4002.             sysobjects o1, sysobjects o2,
  4003.             syscolumns c1, syscolumns c2
  4004.         where    o1.id = f.pktable_id
  4005.             AND o2.id = f.fktable_id
  4006.             AND c1.id = f.pktable_id
  4007.             AND c2.id = f.fktable_id
  4008.             AND c1.colid = f.pkcolid
  4009.             AND c2.colid = f.fkcolid
  4010.     /* select count (*) as 'XXX countout' from #fkeysout */
  4011.  
  4012.     if @order_by_pk = 1 /*    If order by PK fields */
  4013.         select
  4014.             PKTABLE_QUALIFIER, PKTABLE_OWNER, PKTABLE_NAME, PKCOLUMN_NAME,
  4015.             FKTABLE_QUALIFIER, FKTABLE_OWNER, FKTABLE_NAME, FKCOLUMN_NAME,
  4016.             KEY_SEQ, UPDATE_RULE, DELETE_RULE, FK_NAME, PK_NAME, DEFERRABILITY
  4017.         from #fkeysout
  4018.         order by 1,2,3,9,4
  4019.     else        /*    Order by FK fields */
  4020.         select
  4021.             PKTABLE_QUALIFIER, PKTABLE_OWNER, PKTABLE_NAME, PKCOLUMN_NAME,
  4022.             FKTABLE_QUALIFIER, FKTABLE_OWNER, FKTABLE_NAME, FKCOLUMN_NAME,
  4023.             KEY_SEQ, UPDATE_RULE, DELETE_RULE, FK_NAME, PK_NAME, DEFERRABILITY
  4024.         from #fkeysout
  4025.         order by 5,6,7,9,8
  4026. go
  4027.  
  4028. if (charindex('8.00', @@version) = 0)
  4029. begin
  4030.     print ''
  4031.     print ''
  4032.     print 'Warning:'
  4033.     print 'you are installing the stored procedures '
  4034.     print 'on a pre 8.0 SQL Server.'
  4035.     print 'Ignore the following errors.'
  4036. end
  4037. else
  4038.     drop proc sp_fkeys
  4039. go
  4040.  
  4041. /*    Procedure for 8.0 server */
  4042. CREATE PROCEDURE sp_fkeys(
  4043.                @pktable_name        sysname = null,
  4044.                @pktable_owner        sysname = null,
  4045.                @pktable_qualifier    sysname = null,
  4046.                @fktable_name        sysname = null,
  4047.                @fktable_owner        sysname = null,
  4048.                @fktable_qualifier    sysname = null )
  4049. as
  4050.     set nocount on
  4051.     DECLARE @pktable_id            int
  4052.     DECLARE @pkfull_table_name    nvarchar(257) /* 2*128 + 1 */
  4053.     DECLARE @fktable_id            int
  4054.     DECLARE @fkfull_table_name    nvarchar(257) /* 2*128 + 1 */
  4055.     declare    @order_by_pk        int
  4056.  
  4057.     /* select 'XXX starting table creation' */
  4058.  
  4059.     create table #fkeysall(
  4060.             rkeyid int NOT NULL,
  4061.             rkey1 int NOT NULL,
  4062.                 rkey2 int NOT NULL,
  4063.                 rkey3 int NOT NULL,
  4064.                 rkey4 int NOT NULL,
  4065.                 rkey5 int NOT NULL,
  4066.                 rkey6 int NOT NULL,
  4067.                 rkey7 int NOT NULL,
  4068.                 rkey8 int NOT NULL,
  4069.                 rkey9 int NOT NULL,
  4070.                 rkey10 int NOT NULL,
  4071.                 rkey11 int NOT NULL,
  4072.                 rkey12 int NOT NULL,
  4073.                 rkey13 int NOT NULL,
  4074.                 rkey14 int NOT NULL,
  4075.                 rkey15 int NOT NULL,
  4076.                 rkey16 int NOT NULL,
  4077.             fkeyid int NOT NULL,
  4078.             fkey1 int NOT NULL,
  4079.                 fkey2 int NOT NULL,
  4080.                 fkey3 int NOT NULL,
  4081.                 fkey4 int NOT NULL,
  4082.                 fkey5 int NOT NULL,
  4083.                 fkey6 int NOT NULL,
  4084.                 fkey7 int NOT NULL,
  4085.                 fkey8 int NOT NULL,
  4086.                 fkey9 int NOT NULL,
  4087.                 fkey10 int NOT NULL,
  4088.                 fkey11 int NOT NULL,
  4089.                 fkey12 int NOT NULL,
  4090.                 fkey13 int NOT NULL,
  4091.                 fkey14 int NOT NULL,
  4092.                 fkey15 int NOT NULL,
  4093.                 fkey16 int NOT NULL,
  4094.             constid int NOT NULL,
  4095.             name sysname collate database_default NOT NULL)
  4096.  
  4097.     create table #fkeys(
  4098.             pktable_id        int NOT NULL,
  4099.             pkcolid         int NOT NULL,
  4100.             fktable_id        int NOT NULL,
  4101.             fkcolid         int NOT NULL,
  4102.             KEY_SEQ         smallint NOT NULL,
  4103.             fk_id            int NOT NULL,
  4104.             PK_NAME            sysname collate database_default NOT NULL)
  4105.  
  4106.     create table #fkeysout(
  4107.             PKTABLE_QUALIFIER sysname collate database_default NULL,
  4108.             PKTABLE_OWNER sysname collate database_default NULL,
  4109.             PKTABLE_NAME sysname collate database_default NOT NULL,
  4110.             PKCOLUMN_NAME sysname collate database_default NOT NULL,
  4111.             FKTABLE_QUALIFIER sysname collate database_default NULL,
  4112.             FKTABLE_OWNER sysname collate database_default NULL,
  4113.             FKTABLE_NAME sysname collate database_default NOT NULL,
  4114.             FKCOLUMN_NAME sysname collate database_default NOT NULL,
  4115.             KEY_SEQ smallint NOT NULL,
  4116.             UPDATE_RULE smallint NULL,
  4117.             DELETE_RULE smallint NULL,
  4118.             FK_NAME sysname collate database_default NULL,
  4119.             PK_NAME sysname collate database_default NULL,
  4120.             DEFERRABILITY smallint null)
  4121.  
  4122.     /* select 'XXX starting parameter analysis' */
  4123.  
  4124.     select  @order_by_pk = 0
  4125.  
  4126.     if (@pktable_name is null) and (@fktable_name is null)
  4127.     begin    /* If neither primary key nor foreign key table names given */
  4128.         raiserror (15252,-1,-1)
  4129.         return
  4130.     end
  4131.     if @fktable_qualifier is not null
  4132.     begin
  4133.         if db_name() <> @fktable_qualifier
  4134.         begin    /* If qualifier doesn't match current database */
  4135.             raiserror (15250, -1,-1)
  4136.             return
  4137.         end
  4138.     end
  4139.     if @pktable_qualifier is not null
  4140.     begin
  4141.         if db_name() <> @pktable_qualifier
  4142.         begin    /* If qualifier doesn't match current database */
  4143.             raiserror (15250, -1,-1)
  4144.             return
  4145.         end
  4146.     end
  4147.  
  4148.     if @pktable_owner is null
  4149.     begin    /* If unqualified primary key table name */
  4150.         SELECT @pkfull_table_name = quotename(@pktable_name)
  4151.     end
  4152.     else
  4153.     begin    /* Qualified primary key table name */
  4154.         if @pktable_owner = ''
  4155.         begin    /* If empty owner name */
  4156.             SELECT @pkfull_table_name = quotename(@pktable_owner)
  4157.         end
  4158.         else
  4159.         begin
  4160.             SELECT @pkfull_table_name = quotename(@pktable_owner) +
  4161.                 '.' + quotename(@pktable_name)
  4162.         end
  4163.     end
  4164.     /*    Get Object ID */
  4165.     SELECT @pktable_id = object_id(@pkfull_table_name)
  4166.  
  4167.     if @fktable_owner is null
  4168.     begin    /* If unqualified foreign key table name */
  4169.         SELECT @fkfull_table_name = quotename(@fktable_name)
  4170.     end
  4171.     else
  4172.     begin    /* Qualified foreign key table name */
  4173.         if @fktable_owner = ''
  4174.         begin    /* If empty owner name */
  4175.             SELECT @fkfull_table_name = quotename(@fktable_owner)
  4176.         end
  4177.         else
  4178.         begin
  4179.             SELECT @fkfull_table_name = quotename(@fktable_owner) +
  4180.                 '.' + quotename(@fktable_name)
  4181.         end
  4182.     end
  4183.     /*    Get Object ID */
  4184.     SELECT @fktable_id = object_id(@fkfull_table_name)
  4185.  
  4186.     if @fktable_name is not null
  4187.     begin
  4188.         if @fktable_id is null
  4189.             SELECT @fktable_id = 0    /* fk table not found, empty result */
  4190.     end
  4191.  
  4192.     if @pktable_name is null
  4193.     begin /*  If table name not supplied, match all */
  4194.         select @order_by_pk = 1
  4195.     end
  4196.     else
  4197.     begin
  4198.         if @pktable_id is null
  4199.         begin
  4200.             SELECT @pktable_id = 0    /* pk table not found, empty result */
  4201.         end
  4202.     end
  4203.  
  4204.     /*    SQL Server supports upto 16 PK/FK relationships between 2 tables */
  4205.     /*    Process syskeys for each relationship */
  4206.     /*  First, attempt to get all 16 keys for each rel'ship, then sort
  4207.         them out with a 16-way "insert select ... union select ..." */
  4208.  
  4209.     /* select 'XXX starting data analysis' */
  4210.  
  4211.     insert into #fkeysall
  4212.         select
  4213.             r.rkeyid,
  4214.             r.rkey1, r.rkey2, r.rkey3, r.rkey4,
  4215.                 r.rkey5, r.rkey6, r.rkey7, r.rkey8,
  4216.                 r.rkey9, r.rkey10, r.rkey11, r.rkey12,
  4217.                 r.rkey13, r.rkey14, r.rkey15, r.rkey16,
  4218.             r.fkeyid,
  4219.             r.fkey1, r.fkey2, r.fkey3, r.fkey4,
  4220.                 r.fkey5, r.fkey6, r.fkey7, r.fkey8,
  4221.                 r.fkey9, r.fkey10, r.fkey11, r.fkey12,
  4222.                 r.fkey13, r.fkey14, r.fkey15, r.fkey16,
  4223.             r.constid,
  4224.             i.name
  4225.         from
  4226.             sysreferences r, sysobjects o, sysindexes i
  4227.         where    r.constid = o.id
  4228.             AND o.xtype = 'F'
  4229.             AND r.rkeyindid = i.indid
  4230.             AND r.rkeyid = i.id
  4231.             AND r.rkeyid between isnull(@pktable_id, 0)
  4232.                             and isnull(@pktable_id, 0x7fffffff)
  4233.             AND r.fkeyid between isnull(@fktable_id, 0)
  4234.                             and isnull(@fktable_id, 0x7fffffff)
  4235.  
  4236.     /* select count (*) as 'XXX countall' from #fkeysall */
  4237.  
  4238.     insert into #fkeys
  4239.             select rkeyid, rkey1, fkeyid, fkey1, 1, constid, name
  4240.             from #fkeysall
  4241.         union all
  4242.             select rkeyid, rkey2, fkeyid, fkey2, 2, constid, name
  4243.             from #fkeysall
  4244.         union all
  4245.             select rkeyid, rkey3, fkeyid, fkey3, 3, constid, name
  4246.             from #fkeysall
  4247.         union all
  4248.             select rkeyid, rkey4, fkeyid, fkey4, 4, constid, name
  4249.             from #fkeysall
  4250.         union all
  4251.             select rkeyid, rkey5, fkeyid, fkey5, 5, constid, name
  4252.             from #fkeysall
  4253.         union all
  4254.             select rkeyid, rkey6, fkeyid, fkey6, 6, constid, name
  4255.             from #fkeysall
  4256.         union all
  4257.             select rkeyid, rkey7, fkeyid, fkey7, 7, constid, name
  4258.             from #fkeysall
  4259.         union all
  4260.             select rkeyid, rkey8, fkeyid, fkey8, 8, constid, name
  4261.             from #fkeysall
  4262.         union all
  4263.             select rkeyid, rkey9, fkeyid, fkey9, 9, constid, name
  4264.             from #fkeysall
  4265.         union all
  4266.             select rkeyid, rkey10, fkeyid, fkey10, 10, constid, name
  4267.             from #fkeysall
  4268.         union all
  4269.             select rkeyid, rkey11, fkeyid, fkey11, 11, constid, name
  4270.             from #fkeysall
  4271.         union all
  4272.             select rkeyid, rkey12, fkeyid, fkey12, 12, constid, name
  4273.             from #fkeysall
  4274.         union all
  4275.             select rkeyid, rkey13, fkeyid, fkey13, 13, constid, name
  4276.             from #fkeysall
  4277.         union all
  4278.             select rkeyid, rkey14, fkeyid, fkey14, 14, constid, name
  4279.             from #fkeysall
  4280.         union all
  4281.             select rkeyid, rkey15, fkeyid, fkey15, 15, constid, name
  4282.             from #fkeysall
  4283.         union all
  4284.             select rkeyid, rkey16, fkeyid, fkey16, 16, constid, name
  4285.             from #fkeysall
  4286.  
  4287.     /* select count (*) as 'XXX count' from #fkeys */
  4288.  
  4289.     insert into #fkeysout
  4290.         select
  4291.             PKTABLE_QUALIFIER = convert(sysname,db_name()),
  4292.             PKTABLE_OWNER = convert(sysname,USER_NAME(o1.uid)),
  4293.             PKTABLE_NAME = convert(sysname,o1.name),
  4294.             PKCOLUMN_NAME = convert(sysname,c1.name),
  4295.             FKTABLE_QUALIFIER = convert(sysname,db_name()),
  4296.             FKTABLE_OWNER = convert(sysname,USER_NAME(o2.uid)),
  4297.             FKTABLE_NAME = convert(sysname,o2.name),
  4298.             FKCOLUMN_NAME = convert(sysname,c2.name),
  4299.             KEY_SEQ,
  4300.             UPDATE_RULE = CASE WHEN (ObjectProperty(fk_id, 'CnstIsUpdateCascade')=1) THEN 
  4301.                 convert(smallint,0) ELSE convert(smallint,1) END,
  4302.             DELETE_RULE = CASE WHEN (ObjectProperty(fk_id, 'CnstIsDeleteCascade')=1) THEN 
  4303.                 convert(smallint,0) ELSE convert(smallint,1) END,
  4304.             FK_NAME = convert(sysname,OBJECT_NAME(fk_id)),
  4305.             PK_NAME,
  4306.             DEFERRABILITY = 7    /* SQL_NOT_DEFERRABLE */
  4307.         from #fkeys f,
  4308.             sysobjects o1, sysobjects o2,
  4309.             syscolumns c1, syscolumns c2
  4310.         where    o1.id = f.pktable_id
  4311.             AND o2.id = f.fktable_id
  4312.             AND c1.id = f.pktable_id
  4313.             AND c2.id = f.fktable_id
  4314.             AND c1.colid = f.pkcolid
  4315.             AND c2.colid = f.fkcolid
  4316.     /* select count (*) as 'XXX countout' from #fkeysout */
  4317.  
  4318.     if @order_by_pk = 1 /*    If order by PK fields */
  4319.         select
  4320.             PKTABLE_QUALIFIER, PKTABLE_OWNER, PKTABLE_NAME, PKCOLUMN_NAME,
  4321.             FKTABLE_QUALIFIER, FKTABLE_OWNER, FKTABLE_NAME, FKCOLUMN_NAME,
  4322.             KEY_SEQ, UPDATE_RULE, DELETE_RULE, FK_NAME, PK_NAME, DEFERRABILITY
  4323.         from #fkeysout
  4324.         order by 1,2,3,9,4
  4325.     else        /*    Order by FK fields */
  4326.         select
  4327.             PKTABLE_QUALIFIER, PKTABLE_OWNER, PKTABLE_NAME, PKCOLUMN_NAME,
  4328.             FKTABLE_QUALIFIER, FKTABLE_OWNER, FKTABLE_NAME, FKCOLUMN_NAME,
  4329.             KEY_SEQ, UPDATE_RULE, DELETE_RULE, FK_NAME, PK_NAME, DEFERRABILITY
  4330.         from #fkeysout
  4331.         order by 5,6,7,9,8
  4332. go
  4333. grant execute on sp_fkeys to public
  4334. go
  4335.  
  4336. dump tran master with no_log
  4337. go
  4338.  
  4339. print 'creating sp_pkeys'
  4340. go
  4341.  
  4342. /*    Procedure for pre-6.0 server */
  4343. CREATE PROCEDURE sp_pkeys(
  4344.                @table_name        varchar(32),
  4345.                @table_owner     varchar(32) = null,
  4346.                @table_qualifier varchar(32) = null )
  4347. as
  4348.     set nocount on
  4349.     if @table_qualifier is not null
  4350.     begin
  4351.         if db_name() <> @table_qualifier
  4352.         begin    /* If qualifier doesn't match current database */
  4353.             raiserror 20001 '~~Rush_5~~'
  4354.             return
  4355.         end
  4356.     end
  4357.     if @table_owner is null /*    If owner not supplied, match all */
  4358.         select @table_owner = '%'
  4359.     if @@trancount <> 0
  4360.     begin    /* If inside a transaction */
  4361.         raiserror 20003 '~~Rush_35~~'
  4362.         return
  4363.     end
  4364.  
  4365.     create table #pkeys(
  4366.              TABLE_QUALIFIER varchar(32) NULL,
  4367.              TABLE_OWNER     varchar(32) NULL,
  4368.              TABLE_NAME      varchar(32) NOT NULL,
  4369.              COLUMN_NAME     varchar(32) NOT NULL,
  4370.              KEY_SEQ         smallint NOT NULL)
  4371.  
  4372.     /*    SQL Server supports upto 8 PK/FK relationships between 2 tables */
  4373.     /*    Process syskeys for each relationship */
  4374.     /*    The inserts below adds a row to the temp table for each of the
  4375.         8 possible relationships */
  4376.     insert into #pkeys
  4377.         select
  4378.             db_name(),
  4379.             (select user_name(uid) from sysobjects o where o.id = k.id),
  4380.             object_name(k.id),
  4381.             c.name,
  4382.             1
  4383.         from
  4384.             syskeys k, syscolumns c
  4385.         where
  4386.             c.id = k.id
  4387.             and k.type = 1    /* Primary type key */
  4388.             and c.colid = k.key1
  4389.     if (@@rowcount = 0)
  4390.         goto done
  4391.  
  4392.     insert into #pkeys
  4393.         select
  4394.             db_name(),
  4395.             (select user_name(uid) from sysobjects o where o.id = k.id),
  4396.             object_name(k.id),
  4397.             c.name,
  4398.             2
  4399.         from
  4400.             syskeys k, syscolumns c
  4401.         where
  4402.             c.id = k.id
  4403.             and k.type = 1    /* Primary type key */
  4404.             and c.colid = key2
  4405.     if (@@rowcount = 0)
  4406.         goto done
  4407.  
  4408.     insert into #pkeys
  4409.         select
  4410.             db_name(),
  4411.             (select user_name(uid) from sysobjects o where o.id = k.id),
  4412.             object_name(k.id),
  4413.             c.name,
  4414.             3
  4415.         from
  4416.             syskeys k, syscolumns c
  4417.         where
  4418.             c.id = k.id
  4419.             and k.type = 1    /* Primary type key */
  4420.             and c.colid = key3
  4421.     if (@@rowcount = 0)
  4422.         goto done
  4423.  
  4424.     insert into #pkeys
  4425.         select
  4426.             db_name(),
  4427.             (select user_name(uid) from sysobjects o where o.id = k.id),
  4428.             object_name(k.id),
  4429.             c.name,
  4430.             4
  4431.         from
  4432.             syskeys k, syscolumns c
  4433.         where
  4434.             c.id = k.id
  4435.             and k.type = 1    /* Primary type key */
  4436.             and c.colid = key4
  4437.     if (@@rowcount = 0)
  4438.         goto done
  4439.  
  4440.     insert into #pkeys
  4441.         select
  4442.             db_name(),
  4443.             (select user_name(uid) from sysobjects o where o.id = k.id),
  4444.             object_name(k.id),
  4445.             c.name,
  4446.             5
  4447.         from
  4448.             syskeys k, syscolumns c
  4449.         where
  4450.             c.id = k.id
  4451.             and k.type = 1    /* Primary type key */
  4452.             and c.colid = key5
  4453.     if (@@rowcount = 0)
  4454.         goto done
  4455.  
  4456.     insert into #pkeys
  4457.         select
  4458.             db_name(),
  4459.             (select user_name(uid) from sysobjects o where o.id = k.id),
  4460.             object_name(k.id),
  4461.             c.name,
  4462.             6
  4463.         from
  4464.             syskeys k, syscolumns c
  4465.         where
  4466.             c.id = k.id
  4467.             and k.type = 1    /* Primary type key */
  4468.             and c.colid = key6
  4469.     if (@@rowcount = 0)
  4470.         goto done
  4471.  
  4472.     insert into #pkeys
  4473.         select
  4474.             db_name(),
  4475.             (select user_name(uid) from sysobjects o where o.id = k.id),
  4476.             object_name(k.id),
  4477.             c.name,
  4478.             7
  4479.         from
  4480.             syskeys k, syscolumns c
  4481.         where
  4482.             c.id = k.id
  4483.             and k.type = 1    /* Primary type key */
  4484.             and c.colid = key7
  4485.     if (@@rowcount = 0)
  4486.         goto done
  4487.  
  4488.     insert into #pkeys
  4489.          select
  4490.              db_name(),
  4491.              (select user_name(uid) from sysobjects o where o.id = k.id),
  4492.              object_name(k.id),
  4493.              c.name,
  4494.              8
  4495.          from
  4496.              syskeys k, syscolumns c
  4497.          where
  4498.              c.id = k.id
  4499.              and k.type = 1 /* Primary type key */
  4500.              and c.colid = key8
  4501.  
  4502.     done:
  4503.     select
  4504.         TABLE_QUALIFIER,
  4505.         TABLE_OWNER,
  4506.         TABLE_NAME,
  4507.         COLUMN_NAME,
  4508.         KEY_SEQ,
  4509.         PK_NAME = convert(varchar(32),null)
  4510.     from #pkeys
  4511.     where TABLE_NAME = @table_name
  4512.         and TABLE_OWNER like @table_owner
  4513.     order by 1, 2, 3, 5
  4514. go
  4515.  
  4516. if (charindex('6.00', @@version) = 0 and
  4517.     charindex('6.50', @@version) = 0 and
  4518.     charindex('7.00', @@version) = 0 and
  4519.     charindex('8.00', @@version) = 0)
  4520. begin
  4521.     print ''
  4522.     print ''
  4523.     print 'Warning:'
  4524.     print 'you are installing the stored procedures '
  4525.     print 'on a pre 6.0 SQL Server.'
  4526.     print 'Ignore the following error.'
  4527. end
  4528. else
  4529.     drop proc sp_pkeys
  4530. go
  4531.  
  4532. /*    Procedure for 6.0 and 6.50 servers */
  4533. CREATE PROCEDURE sp_pkeys(
  4534.                @table_name        sysname,
  4535.                @table_owner     sysname = null,
  4536.                @table_qualifier sysname = null )
  4537. as
  4538.     DECLARE @table_id            int
  4539.     DECLARE @full_table_name    varchar(255) /* 2*128 + 1 */
  4540.  
  4541.     if @table_qualifier is not null
  4542.     begin
  4543.         if db_name() <> @table_qualifier
  4544.         begin    /* If qualifier doesn't match current database */
  4545.             raiserror (15250, -1,-1)
  4546.             return
  4547.         end
  4548.     end
  4549.     if @table_owner is null
  4550.     begin    /* If unqualified table name */
  4551.         SELECT @full_table_name = @table_name
  4552.     end
  4553.     else
  4554.     begin    /* Qualified table name */
  4555.         if @table_owner = ''
  4556.         begin    /* If empty owner name */
  4557.             SELECT @full_table_name = @table_owner
  4558.         end
  4559.         else
  4560.         begin
  4561.             SELECT @full_table_name = @table_owner + '.' + @table_name
  4562.         end
  4563.     end
  4564.     /*    Get Object ID */
  4565.     SELECT @table_id = object_id(@full_table_name)
  4566.  
  4567.     select
  4568.         TABLE_QUALIFIER = convert(sysname,db_name()),
  4569.         TABLE_OWNER = convert(sysname,user_name(o.uid)),
  4570.         TABLE_NAME = convert(sysname,o.name),
  4571.         COLUMN_NAME = convert(sysname,c.name),
  4572.         KEY_SEQ = convert(smallint,c1.colid),
  4573.         PK_NAME = convert(sysname,i.name)
  4574.     from
  4575.         sysindexes i, syscolumns c, sysobjects o, syscolumns c1
  4576.     where
  4577.         o.id = @table_id
  4578.         and o.id = c.id
  4579.         and o.id = i.id
  4580.         and (i.status & 0x800) = 0x800
  4581.         and c.name = index_col (@full_table_name, i.indid, c1.colid)
  4582.         and c1.colid <= i.keycnt    /* create rows from 1 to keycnt */
  4583.         and c1.id = @table_id
  4584.     order by 1, 2, 3, 5
  4585. go
  4586.  
  4587. if (charindex('7.00', @@version) = 0 and
  4588.     charindex('8.00', @@version) = 0)
  4589. begin
  4590.     print ''
  4591.     print ''
  4592.     print 'Warning:'
  4593.     print 'you are installing the stored procedures '
  4594.     print 'on a pre 7.0 SQL Server.'
  4595.     print 'Ignore the following errors.'
  4596. end
  4597. else
  4598.     drop proc sp_pkeys
  4599. go
  4600.  
  4601. /*    Procedure for 8.0 server */
  4602. CREATE PROCEDURE sp_pkeys(
  4603.                @table_name        sysname,
  4604.                @table_owner     sysname = null,
  4605.                @table_qualifier sysname = null )
  4606. as
  4607.     DECLARE @table_id        int
  4608.     DECLARE @full_table_name    nvarchar(255)
  4609.  
  4610.     if @table_qualifier is not null
  4611.     begin
  4612.         if db_name() <> @table_qualifier
  4613.         begin    /* If qualifier doesn't match current database */
  4614.             raiserror (15250, -1,-1)
  4615.             return
  4616.         end
  4617.     end
  4618.     if @table_owner is null
  4619.     begin    /* If unqualified table name */
  4620.         SELECT @full_table_name = quotename(@table_name)
  4621.     end
  4622.     else
  4623.     begin    /* Qualified table name */
  4624.         if @table_owner = ''
  4625.         begin    /* If empty owner name */
  4626.             SELECT @full_table_name = quotename(@table_owner)
  4627.         end
  4628.         else
  4629.         begin
  4630.             SELECT @full_table_name = quotename(@table_owner) +
  4631.                 '.' + quotename(@table_name)
  4632.         end
  4633.     end
  4634.     /*    Get Object ID */
  4635.     SELECT @table_id = object_id(@full_table_name)
  4636.  
  4637.     select
  4638.         TABLE_QUALIFIER = convert(sysname,db_name()),
  4639.         TABLE_OWNER = convert(sysname,user_name(o.uid)),
  4640.         TABLE_NAME = convert(sysname,o.name),
  4641.         COLUMN_NAME = convert(sysname,c.name),
  4642.         --KEY_SEQ = convert(smallint,c.colid),
  4643.         KEY_SEQ =
  4644.             case
  4645.                 when c.name = index_col(@full_table_name, i.indid,  1) then convert (smallint,1)
  4646.                 when c.name = index_col(@full_table_name, i.indid,  2) then convert (smallint,2)
  4647.                 when c.name = index_col(@full_table_name, i.indid,  3) then convert (smallint,3)
  4648.                 when c.name = index_col(@full_table_name, i.indid,  4) then convert (smallint,4)
  4649.                 when c.name = index_col(@full_table_name, i.indid,  5) then convert (smallint,5)
  4650.                 when c.name = index_col(@full_table_name, i.indid,  6) then convert (smallint,6)
  4651.                 when c.name = index_col(@full_table_name, i.indid,  7) then convert (smallint,7)
  4652.                 when c.name = index_col(@full_table_name, i.indid,  8) then convert (smallint,8)
  4653.                 when c.name = index_col(@full_table_name, i.indid,  9) then convert (smallint,9)
  4654.                 when c.name = index_col(@full_table_name, i.indid, 10) then convert (smallint,10)
  4655.                 when c.name = index_col(@full_table_name, i.indid, 11) then convert (smallint,11)
  4656.                 when c.name = index_col(@full_table_name, i.indid, 12) then convert (smallint,12)
  4657.                 when c.name = index_col(@full_table_name, i.indid, 13) then convert (smallint,13)
  4658.                 when c.name = index_col(@full_table_name, i.indid, 14) then convert (smallint,14)
  4659.                 when c.name = index_col(@full_table_name, i.indid, 15) then convert (smallint,15)
  4660.                 when c.name = index_col(@full_table_name, i.indid, 16) then convert (smallint,16)
  4661.             end,
  4662.         PK_NAME = convert(sysname,i.name)
  4663.     from
  4664.         sysindexes i, syscolumns c, sysobjects o --, syscolumns c1
  4665.     where
  4666.         o.id = @table_id
  4667.         and o.id = c.id
  4668.         and o.id = i.id
  4669.         and (i.status & 0x800) = 0x800
  4670.         --and c.name = index_col (@full_table_name, i.indid, c1.colid)
  4671.         and (c.name = index_col (@full_table_name, i.indid,  1) or
  4672.              c.name = index_col (@full_table_name, i.indid,  2) or
  4673.              c.name = index_col (@full_table_name, i.indid,  3) or
  4674.              c.name = index_col (@full_table_name, i.indid,  4) or
  4675.              c.name = index_col (@full_table_name, i.indid,  5) or
  4676.              c.name = index_col (@full_table_name, i.indid,  6) or
  4677.              c.name = index_col (@full_table_name, i.indid,  7) or
  4678.              c.name = index_col (@full_table_name, i.indid,  8) or
  4679.              c.name = index_col (@full_table_name, i.indid,  9) or
  4680.              c.name = index_col (@full_table_name, i.indid, 10) or
  4681.              c.name = index_col (@full_table_name, i.indid, 11) or
  4682.              c.name = index_col (@full_table_name, i.indid, 12) or
  4683.              c.name = index_col (@full_table_name, i.indid, 13) or
  4684.              c.name = index_col (@full_table_name, i.indid, 14) or
  4685.              c.name = index_col (@full_table_name, i.indid, 15) or
  4686.              c.name = index_col (@full_table_name, i.indid, 16)
  4687.             )
  4688.         --and c1.colid <= i.keycnt    /* create rows from 1 to keycnt */
  4689.         --and c1.id = @table_id
  4690.     order by 1, 2, 3, 5
  4691. go
  4692.  
  4693.  
  4694. grant execute on sp_pkeys to public
  4695. go
  4696.  
  4697. dump tran master with no_log
  4698. go
  4699.  
  4700. print 'creating sp_server_info'
  4701. go
  4702.  
  4703. create proc sp_server_info (
  4704.             @attribute_id  int = null)
  4705. as
  4706.     if @attribute_id is not null
  4707.         select *
  4708.         from master.dbo.spt_server_info
  4709.         where attribute_id = @attribute_id
  4710.     else
  4711.         select *
  4712.         from master.dbo.spt_server_info
  4713.         order by attribute_id
  4714. go
  4715.  
  4716. grant execute on sp_server_info to public
  4717. go
  4718.  
  4719. dump tran master with no_log
  4720. go
  4721.  
  4722. print 'creating sp_special_columns'
  4723. go
  4724.  
  4725. /*    Procedure for pre-6.0 server */
  4726. CREATE PROCEDURE sp_special_columns (
  4727.                  @table_name        varchar(32),
  4728.                  @table_owner        varchar(32) = null,
  4729.                  @table_qualifier    varchar(32) = null,
  4730.                  @col_type            char(1) = 'R',
  4731.                  @scope                char(1) = 'T',
  4732.                  @nullable            char(1) = 'U',
  4733.                  @ODBCVer            int = 2)
  4734. AS
  4735.     DECLARE @indid                int
  4736.     DECLARE @table_id            int
  4737.     DECLARE @full_table_name    varchar(65) /* 2*32+1 */
  4738.     DECLARE @scopeout            smallint
  4739.  
  4740.     if @col_type not in ('R','V') or @col_type is null
  4741.     begin
  4742.         raiserror 20002 '~~Rush_42~~'
  4743.         return
  4744.     end
  4745.  
  4746.     if @scope = 'C'
  4747.         select @scopeout = 0
  4748.     else if @scope = 'T'
  4749.         select @scopeout = 1
  4750.     else
  4751.     begin
  4752.         raiserror 20002 '~~Rush_43~~'
  4753.         return
  4754.     end
  4755.  
  4756.     if @nullable not in ('U','O') or @nullable is null
  4757.     begin
  4758.         raiserror 20002 '~~Rush_44~~'
  4759.         return
  4760.     end
  4761.  
  4762.     if @table_qualifier is not null
  4763.     begin
  4764.         if db_name() <> @table_qualifier
  4765.         begin /* If qualifier doesn't match current database */
  4766.             raiserror 20001 '~~Rush_5~~'
  4767.             return
  4768.         end
  4769.     end
  4770.     if @table_owner is null
  4771.     begin     /* If unqualified table name */
  4772.         SELECT @full_table_name = @table_name
  4773.     end
  4774.     else
  4775.     begin    /* Qualified table name */
  4776.         SELECT @full_table_name = @table_owner + '.' + @table_name
  4777.     end
  4778.     /*    Get Object ID */
  4779.     SELECT @table_id = object_id(@full_table_name)
  4780.  
  4781.     if @col_type = 'V'
  4782.     BEGIN /* if ROWVER, just run that query */
  4783.         SELECT
  4784.             SCOPE = convert(smallint,NULL),
  4785.             COLUMN_NAME = convert(varchar(32),c.name),
  4786.             DATA_TYPE = convert(smallint, -3),
  4787.             TYPE_NAME = t.name,
  4788.             "PRECISION" = convert(int,8),
  4789.             LENGTH = convert(int,8),
  4790.             SCALE = convert(smallint, NULL),
  4791.             PSEUDO_COLUMN = convert(smallint,1)
  4792.         FROM
  4793.             systypes t, syscolumns c
  4794.         WHERE
  4795.             c.id = @table_id
  4796.             AND c.usertype = 80 /*    TIMESTAMP */
  4797.             AND t.usertype = 80 /*    TIMESTAMP */
  4798.         RETURN
  4799.     END
  4800.  
  4801.     /* ROWID, now find the id of the 'best' index for this table */
  4802.  
  4803.     IF @nullable = 'O'    /* Don't include any indexes that contain
  4804.                            nullable columns. */
  4805.  
  4806.             SELECT @indid = MIN(indid)
  4807.                 FROM sysindexes i,syscolumns c,syscolumns c2
  4808.                 WHERE
  4809.                     i.status&2 = 2        /*    If Unique Index */
  4810.                      AND c.id = i.id
  4811.                      AND c2.id = c.id
  4812.                      AND c2.colid < i.keycnt + (i.status&16)/16
  4813.                     AND i.id = @table_id
  4814.                     AND indid > 0        /*    Eliminate Table Row */
  4815.                     AND c.name = index_col(@table_name,i.indid,c2.colid)
  4816.                     GROUP BY indid HAVING SUM(c.status&8) = 0
  4817.  
  4818.     ELSE    /* Include indexes that are partially nullable. */
  4819.  
  4820.         SELECT @indid = MIN(indid)
  4821.             FROM sysindexes i
  4822.             WHERE
  4823.                 status&2 = 2        /*    If Unique Index */
  4824.                 AND id = @table_id
  4825.                 AND indid > 0        /*    Eliminate Table Row */
  4826.  
  4827.     SELECT
  4828.         SCOPE = @scopeout,
  4829.         COLUMN_NAME = convert(varchar(32),INDEX_COL(@full_table_name,indid,c2.colid)),
  4830.         d.DATA_TYPE,
  4831.         TYPE_NAME = t.name,
  4832.         "PRECISION" = isnull(d.data_precision, convert(int,c.length)),
  4833.         LENGTH = isnull(d.length, convert(int,c.length)),
  4834.         SCALE = d.numeric_scale,
  4835.         PSEUDO_COLUMN = convert(smallint,1)
  4836.     FROM
  4837.         sysindexes x,
  4838.         syscolumns c,
  4839.         master.dbo.spt_datatype_info d,
  4840.         systypes t,
  4841.         syscolumns c2    /* Self-join to generate list of index columns and */
  4842.                         /* to extract datatype names */
  4843.     WHERE
  4844.         x.id = @table_id
  4845.         AND c.name = INDEX_COL(@full_table_name,@indid,c2.colid)
  4846.         AND c.id = x.id
  4847.         AND c2.id = x.id
  4848.         AND c2.colid < keycnt+(x.status&16)/16
  4849.         AND x.indid = @indid
  4850.         AND t.type = d.ss_dtype
  4851.         AND c.length = d.fixlen
  4852.         AND c.usertype = t.usertype
  4853.  
  4854. go
  4855.  
  4856. if (charindex('6.00', @@version) = 0 and
  4857.     charindex('6.50', @@version) = 0 and
  4858.     charindex('7.00', @@version) = 0 and
  4859.     charindex('8.00', @@version) = 0)
  4860. begin
  4861.     print ''
  4862.     print ''
  4863.     print 'Warning:'
  4864.     print 'you are installing the stored procedures '
  4865.     print 'on a pre 6.0 SQL Server.'
  4866.     print 'Ignore the following errors.'
  4867. end
  4868. else
  4869.     drop proc sp_special_columns
  4870. go
  4871.  
  4872. /*    Procedure for 6.0 and 6.50 servers */
  4873. CREATE PROCEDURE sp_special_columns (
  4874.                  @table_name        varchar(32),
  4875.                  @table_owner        varchar(32) = null,
  4876.                  @table_qualifier    varchar(32) = null,
  4877.                  @col_type            char(1) = 'R',
  4878.                  @scope                char(1) = 'T',
  4879.                  @nullable            char(1) = 'U',
  4880.                  @ODBCVer            int = 2)
  4881. AS
  4882.     DECLARE @indid                int
  4883.     DECLARE @table_id            int
  4884.     DECLARE @full_table_name    varchar(65) /* 2*32+1 */
  4885.     DECLARE @scopeout            smallint
  4886.  
  4887.     if @col_type not in ('R','V') or @col_type is null
  4888.     begin
  4889.         raiserror (15251,-1,-1,'col_type','''R'' or ''V''')
  4890.         return
  4891.     end
  4892.  
  4893.     if @scope = 'C'
  4894.         select @scopeout = 0
  4895.     else if @scope = 'T'
  4896.         select @scopeout = 1
  4897.     else
  4898.     begin
  4899.         raiserror (15251,-1,-1,'scope','''C'' or ''T''')
  4900.         return
  4901.     end
  4902.  
  4903.     if @nullable not in ('U','O') or @nullable is null
  4904.     begin
  4905.         raiserror (15251,-1,-1,'nullable','''U'' or ''O''')
  4906.         return
  4907.     end
  4908.  
  4909.     if @table_qualifier is not null
  4910.     begin
  4911.         if db_name() <> @table_qualifier
  4912.         begin /* If qualifier doesn't match current database */
  4913.             raiserror (15250, -1,-1)
  4914.             return
  4915.         end
  4916.     end
  4917.     if @table_owner is null
  4918.     begin     /* If unqualified table name */
  4919.         SELECT @full_table_name = @table_name
  4920.     end
  4921.     else
  4922.     begin     /* Qualified table name */
  4923.         SELECT @full_table_name = @table_owner + '.' + @table_name
  4924.     end
  4925.     /*    Get Object ID */
  4926.     SELECT @table_id = object_id(@full_table_name)
  4927.  
  4928.     if @col_type = 'V'
  4929.     BEGIN /* if ROWVER, just run that query */
  4930.         SELECT
  4931.             SCOPE = convert(smallint,NULL),
  4932.             COLUMN_NAME = convert(varchar(32),c.name),
  4933.             DATA_TYPE = convert(smallint, -2),
  4934.             TYPE_NAME = t.name,
  4935.             "PRECISION" = convert(int,8),
  4936.             LENGTH = convert(int,8),
  4937.             SCALE = convert(smallint, NULL),
  4938.             PSEUDO_COLUMN = convert(smallint,1)
  4939.         FROM
  4940.             systypes t, syscolumns c
  4941.         WHERE
  4942.             c.id = @table_id
  4943.             AND c.usertype = 80 /*    TIMESTAMP */
  4944.             AND t.usertype = 80 /*    TIMESTAMP */
  4945.         RETURN
  4946.     END
  4947.  
  4948.     /* ROWID, now find the id of the 'best' index for this table */
  4949.  
  4950.     IF @nullable = 'O'    /* Don't include any indexes that contain
  4951.                            nullable columns. */
  4952.  
  4953.         SELECT @indid = MIN(indid)
  4954.             FROM sysindexes i,syscolumns c,syscolumns c2
  4955.             WHERE
  4956.                 i.status&2 = 2        /*    If Unique Index */
  4957.                 AND c.id = i.id
  4958.                 AND c2.id = c.id
  4959.                 AND c2.colid < i.keycnt + (i.status&16)/16
  4960.                 AND i.id = @table_id
  4961.                 AND indid > 0        /*    Eliminate Table Row */
  4962.                 AND c.name = index_col(@table_name,i.indid,c2.colid)
  4963.                 GROUP BY indid HAVING SUM(c.status&8) = 0
  4964.  
  4965.     ELSE    /* Include indexes that are partially nullable. */
  4966.  
  4967.         SELECT @indid = MIN(indid)
  4968.             FROM sysindexes i
  4969.             WHERE
  4970.                 status&2 = 2        /*    If Unique Index */
  4971.                 AND id = @table_id
  4972.                 AND indid > 0        /*    Eliminate Table Row */
  4973.  
  4974.     SELECT
  4975.         SCOPE = @scopeout,
  4976.         COLUMN_NAME = convert(varchar(32),INDEX_COL(@full_table_name,indid,c2.colid)),
  4977.         d.DATA_TYPE,
  4978.         convert(varchar(32),case
  4979.             when (t.usertype > 100 or t.usertype in (18,80))
  4980.                 then t.name
  4981.             else d.TYPE_NAME
  4982.         end) TYPE_NAME,
  4983.         convert(int,case
  4984.             when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  4985.             else isnull(convert(int,c.prec), 2147483647)
  4986.         end) "PRECISION",
  4987.         convert(int,case
  4988.             when d.ss_dtype IN (106, 108, 55, 63) then    /* decimal/numeric types */
  4989.                 convert(int,c.prec+2)
  4990.             else
  4991.                 isnull(d.length, c.length)
  4992.         end) LENGTH,
  4993.         SCALE = convert(smallint, c.scale),
  4994.         PSEUDO_COLUMN = convert(smallint,1)
  4995.     FROM
  4996.         sysindexes x,
  4997.         syscolumns c,
  4998.         master.dbo.spt_datatype_info d,
  4999.         systypes t,
  5000.         syscolumns c2    /* Self-join to generate list of index columns and */
  5001.                         /* to extract datatype names */
  5002.     WHERE
  5003.         x.id = @table_id
  5004.         AND c.name = INDEX_COL(@full_table_name,@indid,c2.colid)
  5005.         AND c.id = x.id
  5006.         AND c2.id = x.id
  5007.         AND c2.colid < x.keycnt+(x.status&16)/16
  5008.         AND x.indid = @indid
  5009.         AND t.type = d.ss_dtype
  5010.         AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  5011.         AND isnull(d.AUTO_INCREMENT,0) = (c.status&128)/128
  5012.         AND c.usertype = t.usertype
  5013. go
  5014.  
  5015. if (charindex('7.00', @@version) = 0 and
  5016.     charindex('8.00', @@version) = 0)
  5017. begin
  5018.     print ''
  5019.     print ''
  5020.     print 'Warning:'
  5021.     print 'you are installing the stored procedures '
  5022.     print 'on a pre 7.0 SQL Server.'
  5023.     print 'Ignore the following errors.'
  5024. end
  5025. else
  5026.     drop proc sp_special_columns
  5027. go
  5028.  
  5029. /*    Procedure for 7.0 servers */
  5030. CREATE PROCEDURE sp_special_columns (
  5031.                  @table_name        sysname,
  5032.                  @table_owner        sysname = null,
  5033.                  @table_qualifier    sysname = null,
  5034.                  @col_type            char(1) = 'R',
  5035.                  @scope                char(1) = 'T',
  5036.                  @nullable            char(1) = 'U',
  5037.                  @ODBCVer            int = 2)
  5038. AS
  5039.     DECLARE @indid                int
  5040.     DECLARE @table_id            int
  5041.     DECLARE @full_table_name    nvarchar(257)
  5042.     DECLARE @scopeout            smallint
  5043.  
  5044.     if @col_type not in ('R','V') or @col_type is null
  5045.     begin
  5046.         raiserror (15251,-1,-1,'col_type','''R'' or ''V''')
  5047.         return
  5048.     end
  5049.  
  5050.     if @scope = 'C'
  5051.         select @scopeout = 0
  5052.     else if @scope = 'T'
  5053.         select @scopeout = 1
  5054.     else
  5055.     begin
  5056.         raiserror (15251,-1,-1,'scope','''C'' or ''T''')
  5057.         return
  5058.     end
  5059.  
  5060.     if @nullable not in ('U','O') or @nullable is null
  5061.     begin
  5062.         raiserror (15251,-1,-1,'nullable','''U'' or ''O''')
  5063.         return
  5064.     end
  5065.  
  5066.     if @table_qualifier is not null
  5067.     begin
  5068.         if db_name() <> @table_qualifier
  5069.         begin /* If qualifier doesn't match current database */
  5070.             raiserror (15250, -1,-1)
  5071.             return
  5072.         end
  5073.     end
  5074.     if @table_owner is null
  5075.     begin     /* If unqualified table name */
  5076.         SELECT @full_table_name = quotename(@table_name)
  5077.     end
  5078.     else
  5079.     begin    /* Qualified table name */
  5080.         if @table_owner = ''
  5081.         begin    /* If empty owner name */
  5082.             SELECT @full_table_name = quotename(@table_owner)
  5083.         end
  5084.         else
  5085.         begin
  5086.             SELECT @full_table_name = quotename(@table_owner) +
  5087.                 '.' + quotename(@table_name)
  5088.         end
  5089.     end
  5090.     /*    Get Object ID */
  5091.     SELECT @table_id = object_id(@full_table_name)
  5092.  
  5093.     if @col_type = 'V'
  5094.     BEGIN /* if ROWVER, just run that query */
  5095.         SELECT
  5096.             SCOPE = convert(smallint,NULL),
  5097.             COLUMN_NAME = convert(sysname,c.name),
  5098.             DATA_TYPE = convert(smallint, -2),
  5099.             TYPE_NAME = t.name,
  5100.             "PRECISION" = convert(int,8),
  5101.             LENGTH = convert(int,8),
  5102.             SCALE = convert(smallint, NULL),
  5103.             PSEUDO_COLUMN = convert(smallint,1)
  5104.         FROM
  5105.             systypes t, syscolumns c
  5106.         WHERE
  5107.             not (@table_id is null)
  5108.             AND c.id = @table_id
  5109.             AND t.name = 'timestamp'    /*    TIMESTAMP  */
  5110.             AND t.xtype = c.xtype        
  5111.             AND t.xusertype = c.xusertype    
  5112.         RETURN
  5113.     END
  5114.  
  5115.     /* ROWID, now find the id of the 'best' index for this table */
  5116.  
  5117.     IF @nullable = 'O'    /* Don't include any indexes that contain
  5118.                            nullable columns. */
  5119.  
  5120.         SELECT @indid = MIN(indid)
  5121.             FROM sysindexes x, syscolumns c, syscolumns c2
  5122.             WHERE
  5123.                 not (@table_id is null)
  5124.                 AND x.status&2 = 2        /*    If Unique Index */
  5125.                 AND c.id = x.id
  5126.                 AND c2.id = c.id
  5127.                 AND c2.colid < x.keycnt + (x.status&16)/16
  5128.                 AND x.id = @table_id
  5129.                 AND indid > 0        /*    Eliminate Table Row */
  5130.                 AND c.name = index_col(@table_name,x.indid,c2.colid)
  5131.                 GROUP BY indid HAVING SUM(c.status&8) = 0
  5132.  
  5133.     ELSE    /* Include indexes that are partially nullable. */
  5134.  
  5135.         SELECT @indid = MIN(indid)
  5136.             FROM sysindexes x
  5137.             WHERE
  5138.                 not (@table_id is null)
  5139.                 AND status&2 = 2        /*    If Unique Index */
  5140.                 AND id = @table_id
  5141.                 AND indid > 0        /*    Eliminate Table Row */
  5142.  
  5143.     SELECT
  5144.         SCOPE = @scopeout,
  5145.         COLUMN_NAME = convert(sysname,INDEX_COL(@full_table_name,indid,c2.colid)),
  5146.         d.DATA_TYPE,
  5147.         convert(sysname,case
  5148.             when t.xusertype > 255 then t.name
  5149.             else d.TYPE_NAME
  5150.         end) TYPE_NAME,
  5151.         convert(int,case
  5152.             when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  5153.             else OdbcPrec(c.xtype,c.length,c.xprec)
  5154.         end) "PRECISION",
  5155.         convert(int,case
  5156.             when type_name(d.ss_dtype) IN ('numeric','decimal') then    /* decimal/numeric types */
  5157.                 OdbcPrec(c.xtype,c.length,c.xprec)+2
  5158.             else isnull(d.length, c.length)
  5159.         end) LENGTH,
  5160.         SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
  5161.         PSEUDO_COLUMN = convert(smallint,1)
  5162.     FROM
  5163.         sysindexes x,
  5164.         syscolumns c,
  5165.         master.dbo.spt_datatype_info d,
  5166.         systypes t,
  5167.         syscolumns c2    /* Self-join to generate list of index columns and */
  5168.                         /* to extract datatype names */
  5169.     WHERE
  5170.         not (@table_id is null)
  5171.         AND x.id = @table_id
  5172.         AND c.name = INDEX_COL(@full_table_name,@indid,c2.colid)
  5173.         AND c.id = x.id
  5174.         AND c2.id = x.id
  5175.         AND c2.colid < x.keycnt + (x.status&16)/16
  5176.         AND x.indid = @indid
  5177.         AND t.xtype = d.ss_dtype
  5178.         AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  5179.         AND isnull(d.AUTO_INCREMENT,0) = isnull(ColumnProperty (c.id, c.name, 'IsIdentity'),0)
  5180.         AND c.xusertype = t.xusertype
  5181. go
  5182.  
  5183. if (charindex('8.00', @@version) = 0)
  5184. begin
  5185.     print ''
  5186.     print ''
  5187.     print 'Warning:'
  5188.     print 'you are installing the stored procedures '
  5189.     print 'on a pre 8.0 SQL Server.'
  5190.     print 'Ignore the following errors.'
  5191. end
  5192. else
  5193.     drop proc sp_special_columns
  5194. go
  5195.  
  5196. /*    Procedure for 8.0 and later servers */
  5197. CREATE PROCEDURE sp_special_columns (
  5198.                  @table_name        sysname,
  5199.                  @table_owner        sysname = null,
  5200.                  @table_qualifier    sysname = null,
  5201.                  @col_type            char(1) = 'R',
  5202.                  @scope                char(1) = 'T',
  5203.                  @nullable            char(1) = 'U',
  5204.                  @ODBCVer            int = 2)
  5205. AS
  5206.     DECLARE @indid                int
  5207.     DECLARE @table_id            int
  5208.     DECLARE @full_table_name    nvarchar(257)
  5209.     DECLARE @scopeout            smallint
  5210.  
  5211.     if @col_type not in ('R','V') or @col_type is null
  5212.     begin
  5213.         raiserror (15251,-1,-1,'col_type','''R'' or ''V''')
  5214.         return
  5215.     end
  5216.  
  5217.     if @scope = 'C'
  5218.         select @scopeout = 0
  5219.     else if @scope = 'T'
  5220.         select @scopeout = 1
  5221.     else
  5222.     begin
  5223.         raiserror (15251,-1,-1,'scope','''C'' or ''T''')
  5224.         return
  5225.     end
  5226.  
  5227.     if @nullable not in ('U','O') or @nullable is null
  5228.     begin
  5229.         raiserror (15251,-1,-1,'nullable','''U'' or ''O''')
  5230.         return
  5231.     end
  5232.  
  5233.     if @table_qualifier is not null
  5234.     begin
  5235.         if db_name() <> @table_qualifier
  5236.         begin /* If qualifier doesn't match current database */
  5237.             raiserror (15250, -1,-1)
  5238.             return
  5239.         end
  5240.     end
  5241.     if @table_owner is null
  5242.     begin     /* If unqualified table name */
  5243.         SELECT @full_table_name = quotename(@table_name)
  5244.     end
  5245.     else
  5246.     begin    /* Qualified table name */
  5247.         if @table_owner = ''
  5248.         begin    /* If empty owner name */
  5249.             SELECT @full_table_name = quotename(@table_owner)
  5250.         end
  5251.         else
  5252.         begin
  5253.             SELECT @full_table_name = quotename(@table_owner) +
  5254.                 '.' + quotename(@table_name)
  5255.         end
  5256.     end
  5257.     /*    Get Object ID */
  5258.     SELECT @table_id = object_id(@full_table_name)
  5259.  
  5260.     if @col_type = 'V'
  5261.     BEGIN /* if ROWVER, just run that query */
  5262.         SELECT
  5263.             SCOPE = convert(smallint,NULL),
  5264.             COLUMN_NAME = convert(sysname,c.name),
  5265.             DATA_TYPE = convert(smallint, -2),
  5266.             TYPE_NAME = t.name,
  5267.             "PRECISION" = convert(int,8),
  5268.             LENGTH = convert(int,8),
  5269.             SCALE = convert(smallint, NULL),
  5270.             PSEUDO_COLUMN = convert(smallint,1)
  5271.         FROM
  5272.             systypes t, syscolumns c
  5273.         WHERE
  5274.             not (@table_id is null)
  5275.             AND c.id = @table_id
  5276.             AND t.name = 'timestamp'    /*    TIMESTAMP  */
  5277.             AND t.xtype = c.xtype        
  5278.             AND t.xusertype = c.xusertype    
  5279.         RETURN
  5280.     END
  5281.  
  5282.     /* ROWID, now find the id of the 'best' index for this table */
  5283.  
  5284.     IF @nullable = 'O'    /* Don't include any indexes that contain
  5285.                            nullable columns. */
  5286.  
  5287.         SELECT @indid = MIN(indid)
  5288.             FROM sysindexes x, syscolumns c, syscolumns c2
  5289.             WHERE
  5290.                 not (@table_id is null)
  5291.                 AND x.status&2 = 2        /*    If Unique Index */
  5292.                 AND c.id = x.id
  5293.                 AND c2.id = c.id
  5294.                 AND c2.colid < x.keycnt + (x.status&18)/18
  5295.                 AND x.id = @table_id
  5296.                 AND indid > 0        /*    Eliminate Table Row */
  5297.                 AND c.name = index_col(@table_name,x.indid,c2.colid)
  5298.                 GROUP BY indid HAVING SUM(c.status&8) = 0
  5299.  
  5300.     ELSE    /* Include indexes that are partially nullable. */
  5301.  
  5302.         SELECT @indid = MIN(indid)
  5303.             FROM sysindexes x
  5304.             WHERE
  5305.                 not (@table_id is null)
  5306.                 AND status&2 = 2        /*    If Unique Index */
  5307.                 AND id = @table_id
  5308.                 AND indid > 0        /*    Eliminate Table Row */
  5309.  
  5310.     SELECT
  5311.         SCOPE = @scopeout,
  5312.         COLUMN_NAME = convert(sysname,INDEX_COL(@full_table_name,indid,c2.colid)),
  5313.         d.DATA_TYPE,
  5314.         convert(sysname,case
  5315.             when t.xusertype > 255 then t.name
  5316.             else d.TYPE_NAME collate database_default
  5317.         end) TYPE_NAME,
  5318.         convert(int,case
  5319.             when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  5320.             else OdbcPrec(c.xtype,c.length,c.xprec)
  5321.         end) "PRECISION",
  5322.         convert(int,case
  5323.             when type_name(d.ss_dtype) IN ('numeric','decimal') then    /* decimal/numeric types */
  5324.                 OdbcPrec(c.xtype,c.length,c.xprec)+2
  5325.             else isnull(d.length, c.length)
  5326.         end) LENGTH,
  5327.         SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
  5328.         PSEUDO_COLUMN = convert(smallint,1)
  5329.     FROM
  5330.         sysindexes x,
  5331.         syscolumns c,
  5332.         master.dbo.spt_datatype_info d,
  5333.         systypes t,
  5334.         syscolumns c2    /* Self-join to generate list of index columns and */
  5335.                         /* to extract datatype names */
  5336.     WHERE
  5337.         not (@table_id is null)
  5338.         AND x.id = @table_id
  5339.         AND c.name = INDEX_COL(@full_table_name,@indid,c2.colid)
  5340.         AND c.id = x.id
  5341.         AND c2.id = x.id
  5342.         AND c2.colid < x.keycnt + (x.status&16)/16
  5343.         AND x.indid = @indid
  5344.         AND t.xtype = d.ss_dtype
  5345.         AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  5346.         AND isnull(d.AUTO_INCREMENT,0) = isnull(ColumnProperty (c.id, c.name, 'IsIdentity'),0)
  5347.         AND c.xusertype = t.xusertype
  5348. go
  5349.  
  5350. grant execute on sp_special_columns to public
  5351. go
  5352.  
  5353. dump tran master with no_log
  5354. go
  5355.  
  5356. print 'creating sp_sproc_columns'
  5357. go
  5358.  
  5359. /*    Procedure for pre-6.0 server */
  5360. CREATE PROCEDURE sp_sproc_columns (
  5361.                  @procedure_name        varchar(102) = '%', /* 3*32+5+1 */
  5362.                  @procedure_owner        varchar(96) = null,
  5363.                  @procedure_qualifier    varchar(32) = null,
  5364.                  @column_name            varchar(96) = null,
  5365.                  @ODBCVer                int = 2)
  5366. AS
  5367.     DECLARE @group_num_lower smallint
  5368.     DECLARE @group_num_upper smallint
  5369.     DECLARE @semi_position int
  5370.     DECLARE @full_procedure_name    varchar(205)/* 2*102+1 */
  5371.     DECLARE @procedure_id int
  5372.  
  5373.     if @column_name is null /*    If column name not supplied, match all */
  5374.         select @column_name = '%'
  5375.     if @procedure_qualifier is not null
  5376.     begin
  5377.         if db_name() <> @procedure_qualifier
  5378.         begin
  5379.             if @procedure_qualifier = ''
  5380.             begin
  5381.                 /* in this case, we need to return an empty result set */
  5382.                 /* because the user has requested a database with an empty name */
  5383.                 select @procedure_name = ''
  5384.                 select @procedure_owner = ''
  5385.             end
  5386.             else
  5387.             begin    /* If qualifier doesn't match current database */
  5388.                 raiserror 20001 '~~Rush_51~~'
  5389.                 return
  5390.             end
  5391.         end
  5392.     end
  5393.  
  5394.     if @procedure_name is null
  5395.     begin    /*    If procedure name not supplied, match all */
  5396.         select @procedure_name = '%'
  5397.     end
  5398.  
  5399.     /* first we need to extract the procedure group number, if one exists */
  5400.     select @semi_position = charindex(';',@procedure_name)
  5401.     if (@semi_position > 0)
  5402.     begin    /* If group number separator (;) found */
  5403.         select @group_num_lower = convert(int,substring(@procedure_name, @semi_position + 1, 2))
  5404.         select @group_num_upper = @group_num_lower
  5405.         select @procedure_name = substring(@procedure_name, 1, @semi_position -1)
  5406.     end
  5407.     else
  5408.     begin    /* No group separator, so default to group number of 1 */
  5409.         select @group_num_lower = 1
  5410.         select @group_num_upper = 32767            
  5411.     end
  5412.  
  5413.     if @procedure_owner is null
  5414.     begin    /* If unqualified procedure name */
  5415.         SELECT @full_procedure_name = @procedure_name
  5416.     end
  5417.     else
  5418.     begin    /* Qualified procedure name */
  5419.         SELECT @full_procedure_name = @procedure_owner + '.' + @procedure_name
  5420.     end
  5421.  
  5422.     /*    Get Object ID */
  5423.     SELECT @procedure_id = object_id(@full_procedure_name)
  5424.     if ((charindex('%',@full_procedure_name) = 0) and
  5425.         (charindex('_',@full_procedure_name) = 0) and
  5426.         @procedure_id <> 0)
  5427.     begin
  5428.         /* this block is for the case where there is no pattern
  5429.             matching required for the procedure name */
  5430.         SELECT
  5431.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  5432.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  5433.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ ltrim(str(c.number,5))),
  5434.             COLUMN_NAME = convert(varchar(32),c.name),
  5435.             COLUMN_TYPE = convert(smallint, 0),
  5436.             d.DATA_TYPE,
  5437.             TYPE_NAME = t.name,
  5438.             "PRECISION" = isnull(d.data_precision, convert(int,c.length)),
  5439.             LENGTH = isnull(d.length, convert(int,c.length)),
  5440.             SCALE = d.numeric_scale,
  5441.             d.RADIX,
  5442.             d.NULLABLE,
  5443.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  5444.             COLUMN_DEF = convert(varchar(255),null),
  5445.             d.SQL_DATA_TYPE,
  5446.             d.SQL_DATETIME_SUB,
  5447.             CHAR_OCTET_LENGTH = isnull(d.data_precision, convert(int,c.length))+d.charbin,
  5448.             ORDINAL_POSITION = convert(int, c.colid),
  5449.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  5450.             SS_DATA_TYPE = c.type
  5451.         FROM
  5452.             syscolumns c,
  5453.             sysobjects o,
  5454.             master.dbo.spt_datatype_info d,
  5455.             systypes t
  5456.         WHERE
  5457.             o.id = @procedure_id
  5458.             AND c.id = o.id
  5459.             AND t.type = d.ss_dtype
  5460.             AND c.length = isnull(d.fixlen, c.length)
  5461.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  5462.             AND c.usertype = t.usertype
  5463.             AND c.name like @column_name
  5464.             AND c.number between @group_num_lower and @group_num_upper
  5465.         UNION ALL
  5466.         SELECT           /* return value row*/
  5467.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  5468.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  5469.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ isnull(ltrim(str(c.number,5)),'1')),
  5470.             COLUMN_NAME = convert(varchar(32),'@RETURN_VALUE'),
  5471.             COLUMN_TYPE = convert(smallint, 5),
  5472.             DATA_TYPE = convert(smallint, 4),
  5473.             TYPE_NAME = convert(varchar(32),'int'),
  5474.             "PRECISION" = convert(int,10),
  5475.             LENGTH = convert(int,4),
  5476.             SCALE = convert(smallint,0),
  5477.             RADIX = convert(smallint,10),
  5478.             NULLABLE = convert(smallint,0),
  5479.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  5480.             COLUMN_DEF = convert(varchar(255),NULL),
  5481.             SQL_DATA_TYPE = convert(smallint, 4),
  5482.             SQL_DATETIME_SUB = convert(smallint,null),
  5483.             CHAR_OCTET_LENGTH = convert(int,null),
  5484.             ORDINAL_POSITION = convert(int,0),
  5485.             IS_NULLABLE = convert(varchar(254),'NO'),
  5486.             SS_DATA_TYPE = convert(tinyint,56)
  5487.         FROM
  5488.             syscomments c, sysobjects o
  5489.         WHERE
  5490.             o.id = @procedure_id
  5491.             AND c.id = o.id
  5492.             AND c.colid = 1
  5493.             AND o.type = 'P'                        /* Just Procedures */
  5494.             AND '@RETURN_VALUE' like @column_name
  5495.             AND c.number between @group_num_lower and @group_num_upper        
  5496.         ORDER BY 1, 2, 3, 18
  5497.     end
  5498.     else
  5499.     begin
  5500.         /* this block is for the case where there IS pattern
  5501.             matching done on the procedure name */
  5502.         if @procedure_owner is null
  5503.             select @procedure_owner = '%'
  5504.         SELECT
  5505.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  5506.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  5507.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ ltrim(str(c.number,5))),
  5508.             COLUMN_NAME = convert(varchar(32),c.name),
  5509.             COLUMN_TYPE = convert(smallint, 0),
  5510.             d.DATA_TYPE,
  5511.             TYPE_NAME = t.name,
  5512.             "PRECISION" = isnull(d.data_precision, convert(int,c.length)),
  5513.             LENGTH = isnull(d.length, convert(int,c.length)),
  5514.             SCALE = d.numeric_scale,
  5515.             d.RADIX,
  5516.             d.NULLABLE,
  5517.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  5518.             COLUMN_DEF = convert(varchar(255),null),
  5519.             d.SQL_DATA_TYPE,
  5520.             d.SQL_DATETIME_SUB,
  5521.             CHAR_OCTET_LENGTH = isnull(d.data_precision, convert(int,c.length))+d.charbin,
  5522.             ORDINAL_POSITION = convert(int, c.colid),
  5523.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  5524.             SS_DATA_TYPE = c.type
  5525.         FROM
  5526.             syscolumns c,
  5527.             sysobjects o,
  5528.             master.dbo.spt_datatype_info d,
  5529.             systypes t
  5530.         WHERE
  5531.             o.name like @procedure_name
  5532.             AND user_name(o.uid) like @procedure_owner
  5533.             AND o.id = c.id
  5534.             AND t.type = d.ss_dtype
  5535.             AND c.length = isnull(d.fixlen, c.length)
  5536.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  5537.             AND c.usertype = t.usertype
  5538.             AND o.type = 'P'                            /* Just Procedures */
  5539.             AND c.name like @column_name
  5540.             AND c.number between @group_num_lower and @group_num_upper
  5541.         UNION ALL
  5542.         SELECT           /* return value row*/
  5543.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  5544.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  5545.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ isnull(ltrim(str(c.number,5)),'1')),
  5546.             COLUMN_NAME = convert(varchar(32),'@RETURN_VALUE'),
  5547.             COLUMN_TYPE = convert(smallint, 5),
  5548.             DATA_TYPE = convert(smallint, 4),
  5549.             TYPE_NAME = convert(varchar(32),'int'),
  5550.             "PRECISION" = convert(int,10),
  5551.             LENGTH = convert(int,4),
  5552.             SCALE = convert(smallint,0),
  5553.             RADIX = convert(smallint,10),
  5554.             NULLABLE = convert(smallint,0),
  5555.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  5556.             COLUMN_DEF = convert(varchar(255),NULL),
  5557.             SQL_DATA_TYPE = convert(smallint, 4),
  5558.             SQL_DATETIME_SUB = convert(smallint,null),
  5559.             CHAR_OCTET_LENGTH = convert(int,null),
  5560.             ORDINAL_POSITION = convert(int,0),
  5561.             IS_NULLABLE = convert(varchar(254),'NO'),
  5562.             SS_DATA_TYPE = convert(tinyint,56)
  5563.         FROM
  5564.             syscomments c, sysobjects o
  5565.         WHERE
  5566.             o.name like @procedure_name
  5567.             AND user_name(o.uid) like @procedure_owner
  5568.             AND c.id = o.id
  5569.             AND c.colid = 1
  5570.             AND o.type = 'P'                        /* Just Procedures */
  5571.             AND '@RETURN_VALUE' like @column_name
  5572.             AND c.number between @group_num_lower and @group_num_upper
  5573.         ORDER BY 1, 2, 3, 18
  5574.     end
  5575. go
  5576.  
  5577. if (charindex('6.00', @@version) = 0 and
  5578.     charindex('6.50', @@version) = 0 and
  5579.     charindex('7.00', @@version) = 0 and
  5580.     charindex('8.00', @@version) = 0)
  5581. begin
  5582.     print ''
  5583.     print ''
  5584.     print 'Warning:'
  5585.     print 'you are installing the stored procedures '
  5586.     print 'on a pre 6.0 SQL Server.'
  5587.     print 'Ignore the following error.'
  5588. end
  5589. else
  5590.     drop proc sp_sproc_columns
  5591. go
  5592.  
  5593. /*    Procedure for 6.0 and 6.50 servers */
  5594. CREATE PROCEDURE sp_sproc_columns (
  5595.                  @procedure_name        varchar(102) = '%', /* 3*36+5+1 */
  5596.                  @procedure_owner        varchar(96) = null,
  5597.                  @procedure_qualifier    varchar(32) = null,
  5598.                  @column_name            varchar(96) = null,
  5599.                  @ODBCVer                int = 2)
  5600. AS
  5601.     DECLARE @group_num_lower smallint
  5602.     DECLARE @group_num_upper smallint
  5603.     DECLARE @semi_position int
  5604.     DECLARE @full_procedure_name    varchar(205)
  5605.     DECLARE @procedure_id int
  5606.  
  5607.     if @column_name is null /*    If column name not supplied, match all */
  5608.         select @column_name = '%'
  5609.     if @procedure_qualifier is not null
  5610.     begin
  5611.         if db_name() <> @procedure_qualifier
  5612.         begin
  5613.             if @procedure_qualifier = ''
  5614.             begin
  5615.                 /* in this case, we need to return an empty result set */
  5616.                 /* because the user has requested a database with an empty name */
  5617.                 select @procedure_name = ''
  5618.                 select @procedure_owner = ''
  5619.             end
  5620.             else
  5621.             begin    /* If qualifier doesn't match current database */
  5622.                 raiserror (15250, -1,-1)
  5623.                 return
  5624.             end
  5625.         end
  5626.     end
  5627.  
  5628.     if @procedure_name is null
  5629.     begin    /*    If procedure name not supplied, match all */
  5630.         select @procedure_name = '%'
  5631.     end
  5632.  
  5633.     /* first we need to extract the procedure group number, if one exists */
  5634.     select @semi_position = charindex(';',@procedure_name)
  5635.     if (@semi_position > 0)
  5636.     begin    /* If group number separator (;) found */
  5637.         select @group_num_lower = convert(int,substring(@procedure_name, @semi_position + 1, 2))
  5638.         select @group_num_upper = @group_num_lower
  5639.         select @procedure_name = substring(@procedure_name, 1, @semi_position -1)
  5640.     end
  5641.     else
  5642.     begin    /* No group separator, so default to group number of 1 */
  5643.         select @group_num_lower = 1
  5644.         select @group_num_upper = 32767            
  5645.     end
  5646.  
  5647.     if @procedure_owner is null
  5648.     begin    /* If unqualified procedure name */
  5649.         SELECT @full_procedure_name = @procedure_name
  5650.     end
  5651.     else
  5652.     begin    /* Qualified procedure name */
  5653.         SELECT @full_procedure_name = @procedure_owner + '.' + @procedure_name
  5654.     end
  5655.  
  5656.     /*    Get Object ID */
  5657.     SELECT @procedure_id = object_id(@full_procedure_name)
  5658.     if ((charindex('%',@full_procedure_name) = 0) and
  5659.         (charindex('[',@full_procedure_name) = 0) and
  5660.         (charindex('_',@full_procedure_name) = 0) and
  5661.         @procedure_id <> 0)
  5662.     begin
  5663.         /* this block is for the case where there is no pattern
  5664.             matching required for the procedure name */
  5665.         SELECT
  5666.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  5667.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  5668.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ ltrim(str(c.number,5))),
  5669.             COLUMN_NAME = convert(varchar(32),c.name),
  5670.             COLUMN_TYPE = convert(smallint, 1+((c.status/64)&1)),
  5671.             d.DATA_TYPE,
  5672.             TYPE_NAME = t.name,
  5673.             convert(int,case
  5674.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  5675.                 else isnull(convert(int,c.prec), 2147483647)
  5676.             end) "PRECISION",
  5677.             convert(int,case
  5678.                 when d.ss_dtype IN (106, 108, 55, 63) then    /* decimal/numeric types */
  5679.                     c.prec+2
  5680.                 else
  5681.                     isnull(d.length, c.length)
  5682.             end) LENGTH,
  5683.             SCALE = convert(smallint, c.scale),
  5684.             d.RADIX,
  5685.             d.NULLABLE,
  5686.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  5687.             COLUMN_DEF = convert(varchar(255),NULL),
  5688.             d.SQL_DATA_TYPE,
  5689.             d.SQL_DATETIME_SUB,
  5690.             CHAR_OCTET_LENGTH = isnull(convert(int,c.prec), 2147483647)+d.charbin,
  5691.             ORDINAL_POSITION = convert(int, c.colid),
  5692.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  5693.             SS_DATA_TYPE = c.type
  5694.         FROM
  5695.             syscolumns c,
  5696.             sysobjects o,
  5697.             master.dbo.spt_datatype_info d,
  5698.             systypes t
  5699.         WHERE
  5700.             o.id = @procedure_id
  5701.             AND c.id = o.id
  5702.             AND c.type = d.ss_dtype
  5703.             AND c.length = isnull(d.fixlen, c.length)
  5704.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  5705.             AND isnull(d.AUTO_INCREMENT,0) = 0
  5706.             AND c.usertype = t.usertype
  5707.             AND c.name like @column_name
  5708.             AND c.number between @group_num_lower and @group_num_upper
  5709.         UNION ALL
  5710.         SELECT           /* return value row*/
  5711.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  5712.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  5713.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ isnull(ltrim(str(c.number,5)),'1')),
  5714.             COLUMN_NAME = convert(varchar(32),'@RETURN_VALUE'),
  5715.             COLUMN_TYPE = convert(smallint, 5),
  5716.             DATA_TYPE = convert(smallint, 4),
  5717.             TYPE_NAME = convert(varchar(32),'int'),
  5718.             "PRECISION" = convert(int,10),
  5719.             LENGTH = convert(int,4),
  5720.             SCALE = convert(smallint,0),
  5721.             RADIX = convert(smallint,10),
  5722.             NULLABLE = convert(smallint,0),
  5723.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  5724.             COLUMN_DEF = convert(varchar(255),NULL),
  5725.             SQL_DATA_TYPE = convert(smallint, 4),
  5726.             SQL_DATETIME_SUB = convert(smallint,null),
  5727.             CHAR_OCTET_LENGTH = convert(int,null),
  5728.             ORDINAL_POSITION = convert(int,0),
  5729.             IS_NULLABLE = convert(varchar(254),'NO'),
  5730.             SS_DATA_TYPE = convert(tinyint,56)
  5731.         FROM
  5732.             syscomments c, sysobjects o
  5733.         WHERE
  5734.             o.id = @procedure_id
  5735.             AND c.id = o.id
  5736.             AND c.colid = 1
  5737.             AND o.type = 'P'                        /* Just Procedures */
  5738.             AND '@RETURN_VALUE' like @column_name
  5739.             AND c.number between @group_num_lower and @group_num_upper
  5740.         ORDER BY 1, 2, 3, 18
  5741.     end
  5742.     else
  5743.     begin
  5744.         /* this block is for the case where there IS pattern
  5745.             matching done on the procedure name */
  5746.         if @procedure_owner is null
  5747.             select @procedure_owner = '%'
  5748.         SELECT
  5749.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  5750.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  5751.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ ltrim(str(c.number,5))),
  5752.             COLUMN_NAME = convert(varchar(32),c.name),
  5753.             COLUMN_TYPE = convert(smallint, 1+((c.status/64)&1)),
  5754.             d.DATA_TYPE,
  5755.             TYPE_NAME = t.name,
  5756.             convert(int,case
  5757.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  5758.                 else isnull(convert(int,c.prec), 2147483647)
  5759.             end) "PRECISION",
  5760.             convert(int,case
  5761.                 when d.ss_dtype IN (106, 108, 55, 63) then    /* decimal/numeric types */
  5762.                     c.prec+2
  5763.                 else
  5764.                     isnull(d.length, c.length)
  5765.             end) LENGTH,
  5766.             SCALE = convert(smallint, c.scale),
  5767.             d.RADIX,
  5768.             d.NULLABLE,
  5769.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  5770.             COLUMN_DEF = convert(varchar(255),NULL),
  5771.             d.SQL_DATA_TYPE,
  5772.             d.SQL_DATETIME_SUB,
  5773.             CHAR_OCTET_LENGTH = isnull(convert(int,c.prec), 2147483647)+d.charbin,
  5774.             ORDINAL_POSITION = convert(int, c.colid),
  5775.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  5776.             SS_DATA_TYPE = c.type
  5777.         FROM
  5778.             syscolumns c,
  5779.             sysobjects o,
  5780.             master.dbo.spt_datatype_info d,
  5781.             systypes t
  5782.         WHERE
  5783.             o.name like @procedure_name
  5784.             AND user_name(o.uid) like @procedure_owner
  5785.             AND o.id = c.id
  5786.             AND c.type = d.ss_dtype
  5787.             AND c.length = isnull(d.fixlen, c.length)
  5788.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  5789.             AND isnull(d.AUTO_INCREMENT,0) = 0
  5790.             AND c.usertype = t.usertype
  5791.             AND o.type = 'P'                            /* Just Procedures */
  5792.             AND c.name like @column_name
  5793.             AND c.number between @group_num_lower and @group_num_upper
  5794.         UNION ALL
  5795.         SELECT           /* return value row*/
  5796.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  5797.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  5798.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ isnull(ltrim(str(c.number,5)),'1')),
  5799.             COLUMN_NAME = convert(varchar(32),'@RETURN_VALUE'),
  5800.             COLUMN_TYPE = convert(smallint, 5),
  5801.             DATA_TYPE = convert(smallint, 4),
  5802.             TYPE_NAME = convert(varchar(32),'int'),
  5803.             "PRECISION" = convert(int,10),
  5804.             LENGTH = convert(int,4),
  5805.             SCALE = convert(smallint,0),
  5806.             RADIX = convert(smallint,10),
  5807.             NULLABLE = convert(smallint,0),
  5808.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  5809.             COLUMN_DEF = convert(varchar(255),NULL),
  5810.             SQL_DATA_TYPE = convert(smallint, 4),
  5811.             SQL_DATETIME_SUB = convert(smallint,null),
  5812.             CHAR_OCTET_LENGTH = convert(int,null),
  5813.             ORDINAL_POSITION = convert(int,0),
  5814.             IS_NULLABLE = convert(varchar(254),'NO'),
  5815.             SS_DATA_TYPE = convert(tinyint,56)
  5816.         FROM
  5817.             syscomments c, sysobjects o
  5818.         WHERE
  5819.             o.name like @procedure_name
  5820.             AND user_name(o.uid) like @procedure_owner
  5821.             AND c.id = o.id
  5822.             AND c.colid = 1
  5823.             AND o.type = 'P'                        /* Just Procedures */
  5824.             AND '@RETURN_VALUE' like @column_name
  5825.             AND c.number between @group_num_lower and @group_num_upper
  5826.         ORDER BY 1, 2, 3, 18
  5827.     end
  5828. go
  5829.  
  5830. if (charindex('7.00', @@version) = 0 and
  5831.     charindex('8.00', @@version) = 0)
  5832. begin
  5833.     print ''
  5834.     print ''
  5835.     print 'Warning:'
  5836.     print 'you are installing the stored procedures '
  5837.     print 'on a pre 7.0 SQL Server.'
  5838.     print 'Ignore the following errors.'
  5839. end
  5840. else
  5841.     drop proc sp_sproc_columns
  5842. go
  5843.  
  5844. /*    Procedure for 7.0 server */
  5845. CREATE PROCEDURE sp_sproc_columns (
  5846.                  @procedure_name        nvarchar(390) = '%',
  5847.                  @procedure_owner        nvarchar(384) = null,
  5848.                  @procedure_qualifier    sysname = null,
  5849.                  @column_name            nvarchar(384) = null,
  5850.                  @ODBCVer                int = 2)
  5851. AS
  5852.     DECLARE @group_num_lower smallint
  5853.     DECLARE @group_num_upper smallint
  5854.     DECLARE @semi_position int
  5855.     DECLARE @full_procedure_name    nvarchar(774)
  5856.     DECLARE @procedure_id int
  5857.  
  5858.     if @column_name is null /*    If column name not supplied, match all */
  5859.         select @column_name = '%'
  5860.     if @procedure_qualifier is not null
  5861.     begin
  5862.         if db_name() <> @procedure_qualifier
  5863.         begin
  5864.             if @procedure_qualifier = ''
  5865.             begin
  5866.                 /* in this case, we need to return an empty result set */
  5867.                 /* because the user has requested a database with an empty name */
  5868.                 select @procedure_name = ''
  5869.                 select @procedure_owner = ''
  5870.             end
  5871.             else
  5872.             begin    /* If qualifier doesn't match current database */
  5873.                 raiserror (15250, -1,-1)
  5874.                 return
  5875.             end
  5876.         end
  5877.     end
  5878.  
  5879.     if @procedure_name is null
  5880.     begin    /*    If procedure name not supplied, match all */
  5881.         select @procedure_name = '%'
  5882.     end
  5883.  
  5884.     /* first we need to extract the procedure group number, if one exists */
  5885.     select @semi_position = charindex(';',@procedure_name)
  5886.     if (@semi_position > 0)
  5887.     begin    /* If group number separator (;) found */
  5888.         select @group_num_lower = convert(int,substring(@procedure_name, @semi_position + 1, 2))
  5889.         select @group_num_upper = @group_num_lower
  5890.         select @procedure_name = substring(@procedure_name, 1, @semi_position -1)
  5891.     end
  5892.     else
  5893.     begin    /* No group separator, so default to all groups */
  5894.         select @group_num_lower = 1
  5895.         select @group_num_upper = 32767            
  5896.     end
  5897.  
  5898.     if @procedure_owner is null
  5899.     begin    /* If unqualified procedure name */
  5900.         SELECT @full_procedure_name = quotename(@procedure_name)
  5901.     end
  5902.     else
  5903.     begin    /* Qualified procedure name */
  5904.         if @procedure_owner = ''
  5905.         begin    /* If empty owner name */
  5906.             SELECT @full_procedure_name = quotename(@procedure_owner)
  5907.         end
  5908.         else
  5909.         begin
  5910.             SELECT @full_procedure_name = quotename(@procedure_owner) +
  5911.                 '.' + quotename(@procedure_name)
  5912.         end
  5913.     end
  5914.  
  5915.     /*    Get Object ID */
  5916.     SELECT @procedure_id = object_id(@full_procedure_name)
  5917.     if ((isnull(charindex('%', @full_procedure_name),0) = 0) and
  5918.         (isnull(charindex('[', @procedure_name),0) = 0) and
  5919.         (isnull(charindex('[', @procedure_owner),0) = 0) and
  5920.         (isnull(charindex('_', @full_procedure_name),0) = 0) and
  5921.         not (@procedure_id is null))
  5922.     begin
  5923.         /* this block is for the case where there is no pattern
  5924.             matching required for the procedure name */
  5925.         SELECT
  5926.             PROCEDURE_QUALIFIER = convert(sysname,DB_NAME()),
  5927.             PROCEDURE_OWNER = convert(sysname,USER_NAME(o.uid)),
  5928.             PROCEDURE_NAME = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  5929.             COLUMN_NAME = convert(sysname,c.name),
  5930.             COLUMN_TYPE = convert(smallint, 1+c.isoutparam),
  5931.             d.DATA_TYPE,
  5932.             TYPE_NAME = t.name,
  5933.             convert(int,case
  5934.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  5935.                 else OdbcPrec(c.xtype,c.length,c.xprec)
  5936.             end) "PRECISION",
  5937.             convert(int,case
  5938.                 when type_name(d.ss_dtype) IN ('numeric','decimal') then    /* decimal/numeric types */
  5939.                     OdbcPrec(c.xtype,c.length,c.xprec)+2
  5940.                 else
  5941.                     isnull(d.length, c.length)
  5942.             end) LENGTH,
  5943.             SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
  5944.             d.RADIX,
  5945.             d.NULLABLE,
  5946.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  5947.             COLUMN_DEF = convert(nvarchar(4000),NULL),
  5948.             d.SQL_DATA_TYPE,
  5949.             d.SQL_DATETIME_SUB,
  5950.             CHAR_OCTET_LENGTH = isnull(d.length, c.length)+d.charbin,
  5951.             ORDINAL_POSITION = convert(int, c.colid),
  5952.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  5953.             SS_DATA_TYPE = c.type
  5954.         FROM
  5955.             syscolumns c,
  5956.             sysobjects o,
  5957.             master.dbo.spt_datatype_info d,
  5958.             systypes t
  5959.         WHERE
  5960.             o.id = @procedure_id
  5961.             AND c.id = o.id
  5962.             AND c.xtype = d.ss_dtype
  5963.             AND c.length = isnull(d.fixlen, c.length)
  5964.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  5965.             AND isnull(d.AUTO_INCREMENT,0) = 0
  5966.             AND c.xusertype = t.xusertype
  5967.             AND c.name like @column_name
  5968.             AND c.number between @group_num_lower and @group_num_upper
  5969.         UNION ALL
  5970.         SELECT           /* return value row*/
  5971.             PROCEDURE_QUALIFIER = convert(sysname,DB_NAME()),
  5972.             PROCEDURE_OWNER = convert(sysname,USER_NAME(o.uid)),
  5973.             PROCEDURE_NAME = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  5974.             COLUMN_NAME = convert(sysname,'@RETURN_VALUE'),
  5975.             COLUMN_TYPE = convert(smallint, 5),
  5976.             DATA_TYPE = convert(smallint, 4),
  5977.             TYPE_NAME = convert(sysname,'int'),
  5978.             "PRECISION" = convert(int,10),
  5979.             LENGTH = convert(int,4),
  5980.             SCALE = convert(smallint,0),
  5981.             RADIX = convert(smallint,10),
  5982.             NULLABLE = convert(smallint,0),
  5983.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  5984.             COLUMN_DEF = convert(nvarchar(4000),NULL),
  5985.             SQL_DATA_TYPE = convert(smallint, 4),
  5986.             SQL_DATETIME_SUB = convert(smallint,null),
  5987.             CHAR_OCTET_LENGTH = convert(int,null),
  5988.             ORDINAL_POSITION = convert(int,0),
  5989.             IS_NULLABLE = convert(varchar(254),'NO'),
  5990.             SS_DATA_TYPE = convert(tinyint,56)
  5991.         FROM
  5992.             syscomments c, sysobjects o
  5993.         WHERE
  5994.             o.id = @procedure_id
  5995.             AND c.id = o.id
  5996.             AND c.colid = 1
  5997.             AND o.type = 'P'                        /* Just Procedures */
  5998.             AND '@RETURN_VALUE' like @column_name
  5999.             AND c.number between @group_num_lower and @group_num_upper        
  6000.         ORDER BY 1, 2, 3, 18
  6001.     end
  6002.     else
  6003.     begin
  6004.         /* this block is for the case where there IS pattern
  6005.             matching done on the procedure name */
  6006.         if @procedure_owner is null
  6007.             select @procedure_owner = '%'
  6008.         SELECT
  6009.             PROCEDURE_QUALIFIER = convert(sysname,DB_NAME()),
  6010.             PROCEDURE_OWNER = convert(sysname,USER_NAME(o.uid)),
  6011.             PROCEDURE_NAME = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  6012.             COLUMN_NAME = convert(sysname,c.name),
  6013.             COLUMN_TYPE = convert(smallint,    1+c.isoutparam),
  6014.             d.DATA_TYPE,
  6015.             TYPE_NAME = t.name,
  6016.             convert(int,case
  6017.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  6018.                 else OdbcPrec(c.xtype,c.length,c.xprec)
  6019.             end) "PRECISION",
  6020.             convert(int,case
  6021.                 when type_name(d.ss_dtype) IN ('numeric','decimal') then    /* decimal/numeric types */
  6022.                     OdbcPrec(c.xtype,c.length,c.xprec)+2
  6023.                 else
  6024.                     isnull(d.length, c.length)
  6025.             end) LENGTH,
  6026.             SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
  6027.             d.RADIX,
  6028.             d.NULLABLE,
  6029.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  6030.             COLUMN_DEF = convert(nvarchar(4000),NULL),
  6031.             d.SQL_DATA_TYPE,
  6032.             d.SQL_DATETIME_SUB,
  6033.             CHAR_OCTET_LENGTH = isnull(d.length, c.length)+d.charbin,
  6034.             ORDINAL_POSITION = convert(int, c.colid),
  6035.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  6036.             SS_DATA_TYPE = c.type
  6037.         FROM
  6038.             syscolumns c,
  6039.             sysobjects o,
  6040.             master.dbo.spt_datatype_info d,
  6041.             systypes t
  6042.         WHERE
  6043.             o.name like @procedure_name
  6044.             AND user_name(o.uid) like @procedure_owner
  6045.             AND o.id = c.id
  6046.             AND c.xtype = d.ss_dtype
  6047.             AND c.length = isnull(d.fixlen, c.length)
  6048.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  6049.             AND isnull(d.AUTO_INCREMENT,0) = 0
  6050.             AND c.xusertype = t.xusertype
  6051.             AND o.type = 'P'                            /* Just Procedures */
  6052.             AND c.name like @column_name
  6053.             AND c.number between @group_num_lower and @group_num_upper
  6054.         UNION ALL
  6055.         SELECT           /* return value row*/
  6056.             PROCEDURE_QUALIFIER = convert(sysname,DB_NAME()),
  6057.             PROCEDURE_OWNER = convert(sysname,USER_NAME(o.uid)),
  6058.             PROCEDURE_NAME = convert(nvarchar(134),o.name +';'+ isnull(ltrim(str(c.number,5)),'1')),
  6059.             COLUMN_NAME = convert(sysname,'@RETURN_VALUE'),
  6060.             COLUMN_TYPE = convert(smallint, 5),
  6061.             DATA_TYPE = convert(smallint, 4),
  6062.             TYPE_NAME = convert(sysname,'int'),
  6063.             "PRECISION" = convert(int,10),
  6064.             LENGTH = convert(int,4),
  6065.             SCALE = convert(smallint,0),
  6066.             RADIX = convert(smallint,10),
  6067.             NULLABLE = convert(smallint,0),
  6068.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  6069.             COLUMN_DEF = convert(nvarchar(4000),NULL),
  6070.             SQL_DATA_TYPE = convert(smallint, 4),
  6071.             SQL_DATETIME_SUB = convert(smallint,null),
  6072.             CHAR_OCTET_LENGTH = convert(int,null),
  6073.             ORDINAL_POSITION = convert(int,0),
  6074.             IS_NULLABLE = convert(varchar(254),'NO'),
  6075.             SS_DATA_TYPE = convert(tinyint,56)
  6076.         FROM
  6077.             syscomments c, sysobjects o
  6078.         WHERE
  6079.             o.name like @procedure_name
  6080.             AND c.id = o.id
  6081.             AND user_name(o.uid) like @procedure_owner
  6082.             AND c.colid = 1
  6083.             AND o.type = 'P'                        /* Just Procedures */
  6084.             AND '@RETURN_VALUE' like @column_name
  6085.             AND c.number between @group_num_lower and @group_num_upper
  6086.         ORDER BY 1, 2, 3, 18
  6087.     end
  6088. go
  6089.  
  6090. if (charindex('8.00', @@version) = 0)
  6091. begin
  6092.     print ''
  6093.     print ''
  6094.     print 'Warning:'
  6095.     print 'you are installing the stored procedures '
  6096.     print 'on a pre 8.0 SQL Server.'
  6097.     print 'Ignore the following errors.'
  6098. end
  6099. else
  6100.     drop proc sp_sproc_columns
  6101. go
  6102.  
  6103. /*    Procedure for 8.0 server */
  6104. CREATE PROCEDURE sp_sproc_columns (
  6105.                  @procedure_name        nvarchar(390) = '%',
  6106.                  @procedure_owner        nvarchar(384) = null,
  6107.                  @procedure_qualifier        sysname = null,
  6108.                  @column_name            nvarchar(384) = null,
  6109.                  @ODBCVer                int = 2)
  6110. AS
  6111.     DECLARE @group_num_lower smallint
  6112.     DECLARE @group_num_upper smallint
  6113.     DECLARE @semi_position int
  6114.     DECLARE @full_procedure_name nvarchar(774)
  6115.     DECLARE @procedure_id int
  6116.  
  6117.     if @column_name is null /*    If column name not supplied, match all */
  6118.         select @column_name = '%'
  6119.     if @procedure_qualifier is not null
  6120.     begin
  6121.         if db_name() <> @procedure_qualifier
  6122.         begin
  6123.             if @procedure_qualifier = ''
  6124.             begin
  6125.                 /* in this case, we need to return an empty result set */
  6126.                 /* because the user has requested a database with an empty name */
  6127.                 select @procedure_name = ''
  6128.                 select @procedure_owner = ''
  6129.             end
  6130.             else
  6131.             begin    /* If qualifier doesn't match current database */
  6132.                 raiserror (15250, -1,-1)
  6133.                 return
  6134.             end
  6135.         end
  6136.     end
  6137.  
  6138.     if @procedure_name is null
  6139.     begin    /*    If procedure name not supplied, match all */
  6140.         select @procedure_name = '%'
  6141.     end
  6142.  
  6143.     /* first we need to extract the procedure group number, if one exists */
  6144.     select @semi_position = charindex(';',@procedure_name)
  6145.     if (@semi_position > 0)
  6146.     begin    /* If group number separator (;) found */
  6147.         select @group_num_lower = convert(int,substring(@procedure_name, @semi_position + 1, 2))
  6148.         select @group_num_upper = @group_num_lower
  6149.         select @procedure_name = substring(@procedure_name, 1, @semi_position -1)
  6150.     end
  6151.     else
  6152.     begin    /* No group separator, so default to all groups */
  6153.         select @group_num_lower = 1
  6154.         select @group_num_upper = 32767            
  6155.     end
  6156.  
  6157.     if @procedure_owner is null
  6158.     begin    /* If unqualified procedure name */
  6159.         SELECT @full_procedure_name = quotename(@procedure_name)
  6160.     end
  6161.     else
  6162.     begin    /* Qualified procedure name */
  6163.         if @procedure_owner = ''
  6164.         begin    /* If empty owner name */
  6165.             SELECT @full_procedure_name = quotename(@procedure_owner)
  6166.         end
  6167.         else
  6168.         begin
  6169.             SELECT @full_procedure_name = quotename(@procedure_owner) +
  6170.                 '.' + quotename(@procedure_name)
  6171.         end
  6172.     end
  6173.  
  6174.     /*    Get Object ID */
  6175.     SELECT @procedure_id = object_id(@full_procedure_name)
  6176.     if ((isnull(charindex('%', @full_procedure_name),0) = 0) and
  6177.         (isnull(charindex('[', @procedure_name),0) = 0) and
  6178.         (isnull(charindex('[', @procedure_owner),0) = 0) and
  6179.         (isnull(charindex('_', @full_procedure_name),0) = 0) and
  6180.         not (@procedure_id is null))
  6181.     begin
  6182.         /* this block is for the case where there is no pattern
  6183.             matching required for the procedure name */
  6184.         SELECT
  6185.             PROCEDURE_QUALIFIER = convert(sysname,DB_NAME()),
  6186.             PROCEDURE_OWNER = convert(sysname,USER_NAME(o.uid)),
  6187.             PROCEDURE_NAME = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  6188.             COLUMN_NAME = convert(sysname,c.name),
  6189.             COLUMN_TYPE = convert(smallint, 1+c.isoutparam),
  6190.             d.DATA_TYPE,
  6191.             TYPE_NAME = t.name,
  6192.             convert(int,case
  6193.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  6194.                 else OdbcPrec(c.xtype,c.length,c.xprec)
  6195.             end) "PRECISION",
  6196.             convert(int,case
  6197.                 when type_name(d.ss_dtype) IN ('numeric','decimal') then    /* decimal/numeric types */
  6198.                     OdbcPrec(c.xtype,c.length,c.xprec)+2
  6199.                 else
  6200.                     isnull(d.length, c.length)
  6201.             end) LENGTH,
  6202.             SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
  6203.             d.RADIX,
  6204.             d.NULLABLE,
  6205.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  6206.             COLUMN_DEF = convert(nvarchar(4000),NULL),
  6207.             d.SQL_DATA_TYPE,
  6208.             d.SQL_DATETIME_SUB,
  6209.             CHAR_OCTET_LENGTH = isnull(d.length, c.length)+d.charbin,
  6210.             ORDINAL_POSITION = convert(int, c.colid),
  6211.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  6212.             SS_DATA_TYPE = c.type
  6213.         FROM
  6214.             syscolumns c,
  6215.             sysobjects o,
  6216.             master.dbo.spt_datatype_info d,
  6217.             systypes t
  6218.         WHERE
  6219.             o.id = @procedure_id
  6220.             AND c.id = o.id
  6221.             AND c.xtype = d.ss_dtype
  6222.             AND c.length = isnull(d.fixlen, c.length)
  6223.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  6224.             AND isnull(d.AUTO_INCREMENT,0) = 0
  6225.             AND c.xusertype = t.xusertype
  6226.             AND c.name like @column_name
  6227.             AND (o.type in ('P', 'TF', 'IF') OR (len(c.name) > 0 and o.type = 'FN'))
  6228.             AND ((c.number between @group_num_lower and @group_num_upper)
  6229.                  OR (c.number = 0 and o.type = 'FN'))
  6230.         UNION ALL
  6231.         SELECT           /* return value row*/
  6232.             PROCEDURE_QUALIFIER = convert(sysname,DB_NAME()),
  6233.             PROCEDURE_OWNER = convert(sysname,USER_NAME(o.uid)),
  6234.             PROCEDURE_NAME = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  6235.             COLUMN_NAME = convert(sysname,'@RETURN_VALUE'),
  6236.             COLUMN_TYPE = convert(smallint, 5),
  6237.             DATA_TYPE = convert(smallint, 4),
  6238.             TYPE_NAME = convert(sysname,'int'),
  6239.             "PRECISION" = convert(int,10),
  6240.             LENGTH = convert(int,4),
  6241.             SCALE = convert(smallint,0),
  6242.             RADIX = convert(smallint,10),
  6243.             NULLABLE = convert(smallint,0),
  6244.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  6245.             COLUMN_DEF = convert(nvarchar(4000),NULL),
  6246.             SQL_DATA_TYPE = convert(smallint, 4),
  6247.             SQL_DATETIME_SUB = convert(smallint,null),
  6248.             CHAR_OCTET_LENGTH = convert(int,null),
  6249.             ORDINAL_POSITION = convert(int,0),
  6250.             IS_NULLABLE = convert(varchar(254),'NO'),
  6251.             SS_DATA_TYPE = convert(tinyint,56)
  6252.         FROM
  6253.             syscomments c, sysobjects o
  6254.         WHERE
  6255.             o.id = @procedure_id
  6256.             AND c.id = o.id
  6257.             AND c.colid = 1
  6258.             AND o.type = 'P'            /* Procedures */
  6259.             AND '@RETURN_VALUE' like @column_name
  6260.             AND c.number between @group_num_lower and @group_num_upper
  6261.         UNION ALL
  6262.         SELECT        /* UDF return value */
  6263.             PROCEDURE_QUALIFIER = convert(sysname,DB_NAME()),
  6264.             PROCEDURE_OWNER = convert(sysname,USER_NAME(o.uid)),
  6265.             PROCEDURE_NAME = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  6266.             COLUMN_NAME = convert(sysname,'@RETURN_VALUE'),
  6267.             COLUMN_TYPE = convert(smallint, 5),    /* SQL_RETURN_VALUE */
  6268.             d.DATA_TYPE,
  6269.             TYPE_NAME = t.name,
  6270.             convert(int,case
  6271.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  6272.                 else OdbcPrec(c.xtype,c.length,c.xprec)
  6273.             end) "PRECISION",
  6274.             convert(int,case
  6275.                 when type_name(d.ss_dtype) IN ('numeric','decimal') then    /* decimal/numeric types */
  6276.                     OdbcPrec(c.xtype,c.length,c.xprec)+2
  6277.                 else
  6278.                     isnull(d.length, c.length)
  6279.             end) LENGTH,
  6280.             SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
  6281.             d.RADIX,
  6282.             NULLABLE = convert(smallint, c.isnullable),
  6283.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  6284.             COLUMN_DEF = convert(nvarchar(4000),NULL),
  6285.             d.SQL_DATA_TYPE,
  6286.             d.SQL_DATETIME_SUB,
  6287.             CHAR_OCTET_LENGTH = isnull(d.length, c.length)+d.charbin,
  6288.             ORDINAL_POSITION = convert(int,0),
  6289.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  6290.             SS_DATA_TYPE = c.type
  6291.         FROM
  6292.             syscolumns c,
  6293.             sysobjects o,
  6294.             master.dbo.spt_datatype_info d,
  6295.             systypes t
  6296.         WHERE
  6297.             o.name like @procedure_name
  6298.             AND o.id = c.id
  6299.             AND c.xtype = d.ss_dtype
  6300.             AND c.length = isnull(d.fixlen, c.length)
  6301.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  6302.             AND isnull(d.AUTO_INCREMENT,0) = 0
  6303.             AND c.xusertype = t.xusertype
  6304.             AND o.type = 'FN'            /* Scalar UDF */
  6305.             AND c.name like @column_name
  6306.             AND c.colid = 0
  6307.             AND c.number = 0
  6308.         UNION ALL
  6309.         SELECT        /* Table valued functions */
  6310.             PROCEDURE_QUALIFIER = convert(sysname,DB_NAME()),
  6311.             PROCEDURE_OWNER = convert(sysname,USER_NAME(o.uid)),
  6312.             PROCEDURE_NAME = convert(nvarchar(134),o.name +';1'),
  6313.             COLUMN_NAME = convert(sysname,'@TABLE_RETURN_VALUE'),
  6314.             COLUMN_TYPE = convert(smallint, 3),
  6315.             DATA_TYPE = convert(smallint, NULL),
  6316.             TYPE_NAME = convert(sysname, 'table'),
  6317.             "PRECISION" = convert(int,0),
  6318.             LENGTH = convert(int,0),
  6319.             SCALE = convert(smallint,0),
  6320.             RADIX = convert(smallint,0),
  6321.             NULLABLE = convert(smallint,0),
  6322.             REMARKS = convert(varchar(254), 'Result table returned by table valued function'),
  6323.             COLUMN_DEF = convert(nvarchar(4000),NULL),
  6324.             SQL_DATA_TYPE = convert(smallint, NULL),
  6325.             SQL_DATETIME_SUB = convert(smallint,null),
  6326.             CHAR_OCTET_LENGTH = convert(int,null),
  6327.             ORDINAL_POSITION = convert(int,0),
  6328.             IS_NULLABLE = convert(varchar(254),'NO'),
  6329.             SS_DATA_TYPE = convert(tinyint,0)
  6330.         FROM
  6331.             syscomments c, sysobjects o
  6332.         WHERE
  6333.             o.id = @procedure_id
  6334.             AND c.id = o.id
  6335.             AND c.colid = 1
  6336.             AND o.type IN ('TF', 'IF')
  6337.             AND '@TABLE_RETURN_VALUE' like @column_name
  6338.             AND c.number = 0
  6339.         ORDER BY 1, 2, 3, 18
  6340.     end
  6341.     else
  6342.     begin
  6343.         /* this block is for the case where there IS pattern
  6344.             matching done on the procedure name */
  6345.         if @procedure_owner is null
  6346.             select @procedure_owner = '%'
  6347.  
  6348.         SELECT
  6349.             PROCEDURE_QUALIFIER = convert(sysname,DB_NAME()),
  6350.             PROCEDURE_OWNER = convert(sysname,USER_NAME(o.uid)),
  6351.             PROCEDURE_NAME = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  6352.             COLUMN_NAME = convert(sysname,c.name),
  6353.             COLUMN_TYPE = convert(smallint,    1+c.isoutparam),
  6354.             d.DATA_TYPE,
  6355.             TYPE_NAME = t.name,
  6356.             convert(int,case
  6357.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  6358.                 else OdbcPrec(c.xtype,c.length,c.xprec)
  6359.             end) "PRECISION",
  6360.             convert(int,case
  6361.                 when type_name(d.ss_dtype) IN ('numeric','decimal') then    /* decimal/numeric types */
  6362.                     OdbcPrec(c.xtype,c.length,c.xprec)+2
  6363.                 else
  6364.                     isnull(d.length, c.length)
  6365.             end) LENGTH,
  6366.             SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
  6367.             d.RADIX,
  6368.             d.NULLABLE,
  6369.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  6370.             COLUMN_DEF = convert(nvarchar(4000),NULL),
  6371.             d.SQL_DATA_TYPE,
  6372.             d.SQL_DATETIME_SUB,
  6373.             CHAR_OCTET_LENGTH = isnull(d.length, c.length)+d.charbin,
  6374.             ORDINAL_POSITION = convert(int, c.colid),
  6375.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  6376.             SS_DATA_TYPE = c.type
  6377.         FROM
  6378.             syscolumns c,
  6379.             sysobjects o,
  6380.             master.dbo.spt_datatype_info d,
  6381.             systypes t
  6382.         WHERE
  6383.             o.name like @procedure_name
  6384.             AND user_name(o.uid) like @procedure_owner
  6385.             AND o.id = c.id
  6386.             AND c.xtype = d.ss_dtype
  6387.             AND c.length = isnull(d.fixlen, c.length)
  6388.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  6389.             AND isnull(d.AUTO_INCREMENT,0) = 0
  6390.             AND c.xusertype = t.xusertype
  6391.             AND c.name like @column_name
  6392.             AND (o.type in ('P', 'TF', 'IF') OR (len(c.name) > 0 and o.type = 'FN'))
  6393.             AND ((c.number between @group_num_lower and @group_num_upper)
  6394.                  OR (c.number = 0 and o.type = 'FN'))
  6395.         UNION ALL
  6396.         SELECT           /* return value row*/
  6397.             PROCEDURE_QUALIFIER = convert(sysname,DB_NAME()),
  6398.             PROCEDURE_OWNER = convert(sysname,USER_NAME(o.uid)),
  6399.             PROCEDURE_NAME = convert(nvarchar(134),o.name +';'+ isnull(ltrim(str(c.number,5)),'1')),
  6400.             COLUMN_NAME = convert(sysname,'@RETURN_VALUE'),
  6401.             COLUMN_TYPE = convert(smallint, 5),    /* SQL_RETURN_VALUE */
  6402.             DATA_TYPE = convert(smallint, 4),
  6403.             TYPE_NAME = convert(sysname,'int'),
  6404.             "PRECISION" = convert(int,10),
  6405.             LENGTH = convert(int,4),
  6406.             SCALE = convert(smallint,0),
  6407.             RADIX = convert(smallint,10),
  6408.             NULLABLE = convert(smallint,0),
  6409.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  6410.             COLUMN_DEF = convert(nvarchar(4000),NULL),
  6411.             SQL_DATA_TYPE = convert(smallint, 4),
  6412.             SQL_DATETIME_SUB = convert(smallint,null),
  6413.             CHAR_OCTET_LENGTH = convert(int,null),
  6414.             ORDINAL_POSITION = convert(int,0),
  6415.             IS_NULLABLE = convert(varchar(254),'NO'),
  6416.             SS_DATA_TYPE = convert(tinyint,56)
  6417.         FROM
  6418.             syscomments c, sysobjects o
  6419.         WHERE
  6420.             o.name like @procedure_name
  6421.             AND c.id = o.id
  6422.             AND user_name(o.uid) like @procedure_owner
  6423.             AND c.colid = 1
  6424.             AND o.type = 'P'                    /* Procedures */
  6425.             AND '@RETURN_VALUE' like @column_name
  6426.             AND c.number between @group_num_lower and @group_num_upper
  6427.         UNION ALL
  6428.         SELECT        /* UDF return value */
  6429.             PROCEDURE_QUALIFIER = convert(sysname,DB_NAME()),
  6430.             PROCEDURE_OWNER = convert(sysname,USER_NAME(o.uid)),
  6431.             PROCEDURE_NAME = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  6432.             COLUMN_NAME = convert(sysname,'@RETURN_VALUE'),
  6433.             COLUMN_TYPE = convert(smallint, 5),    /* SQL_RETURN_VALUE */
  6434.             d.DATA_TYPE,
  6435.             TYPE_NAME = t.name,
  6436.             convert(int,case
  6437.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  6438.                 else OdbcPrec(c.xtype,c.length,c.xprec)
  6439.             end) "PRECISION",
  6440.             convert(int,case
  6441.                 when type_name(d.ss_dtype) IN ('numeric','decimal') then    /* decimal/numeric types */
  6442.                     OdbcPrec(c.xtype,c.length,c.xprec)+2
  6443.                 else
  6444.                     isnull(d.length, c.length)
  6445.             end) LENGTH,
  6446.             SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
  6447.             d.RADIX,
  6448.             NULLABLE = convert(smallint, c.isnullable),
  6449.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  6450.             COLUMN_DEF = convert(nvarchar(4000),NULL),
  6451.             d.SQL_DATA_TYPE,
  6452.             d.SQL_DATETIME_SUB,
  6453.             CHAR_OCTET_LENGTH = isnull(d.length, c.length)+d.charbin,
  6454.             ORDINAL_POSITION = convert(int,0),
  6455.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  6456.             SS_DATA_TYPE = c.type
  6457.         FROM
  6458.             syscolumns c,
  6459.             sysobjects o,
  6460.             master.dbo.spt_datatype_info d,
  6461.             systypes t
  6462.         WHERE
  6463.             o.name like @procedure_name
  6464.             AND user_name(o.uid) like @procedure_owner
  6465.             AND o.id = c.id
  6466.             AND c.xtype = d.ss_dtype
  6467.             AND c.length = isnull(d.fixlen, c.length)
  6468.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  6469.             AND isnull(d.AUTO_INCREMENT,0) = 0
  6470.             AND c.xusertype = t.xusertype
  6471.             AND o.type = 'FN'            /* Scalar UDF */
  6472.             AND c.name like @column_name
  6473.             AND c.colid = 0
  6474.             AND c.number = 0
  6475.         UNION ALL
  6476.         SELECT        /* Table valued functions */
  6477.             PROCEDURE_QUALIFIER = convert(sysname,DB_NAME()),
  6478.             PROCEDURE_OWNER = convert(sysname,USER_NAME(o.uid)),
  6479.             PROCEDURE_NAME = convert(nvarchar(134),o.name +';1'),
  6480.             COLUMN_NAME = convert(sysname,'@TABLE_RETURN_VALUE'),
  6481.             COLUMN_TYPE = convert(smallint, 3),
  6482.             DATA_TYPE = convert(smallint, NULL),
  6483.             TYPE_NAME = convert(sysname, 'table'),
  6484.             "PRECISION" = convert(int,0),
  6485.             LENGTH = convert(int,0),
  6486.             SCALE = convert(smallint,0),
  6487.             RADIX = convert(smallint,0),
  6488.             NULLABLE = convert(smallint,0),
  6489.             REMARKS = convert(varchar(254), 'Result table returned by table valued function'),
  6490.             COLUMN_DEF = convert(nvarchar(4000),NULL),
  6491.             SQL_DATA_TYPE = convert(smallint, NULL),
  6492.             SQL_DATETIME_SUB = convert(smallint,null),
  6493.             CHAR_OCTET_LENGTH = convert(int,null),
  6494.             ORDINAL_POSITION = convert(int,0),
  6495.             IS_NULLABLE = convert(varchar(254),'NO'),
  6496.             SS_DATA_TYPE = convert(tinyint,0)
  6497.         FROM
  6498.             syscomments c, sysobjects o
  6499.         WHERE
  6500.             o.name like @procedure_name
  6501.             AND user_name(o.uid) like @procedure_owner
  6502.             AND c.id = o.id
  6503.             AND c.colid = 1
  6504.             AND o.type IN ('TF', 'IF')
  6505.             AND '@TABLE_RETURN_VALUE' like @column_name
  6506.             AND c.number = 0
  6507.         ORDER BY 1, 2, 3, 18
  6508.     end
  6509. go
  6510.  
  6511. grant execute on sp_sproc_columns to public
  6512. go
  6513.  
  6514. dump tran master with no_log
  6515. go
  6516.  
  6517. print 'creating sp_statistics'
  6518. go
  6519.  
  6520. /*    Procedure for pre-7.0 server */
  6521. CREATE PROCEDURE sp_statistics (
  6522.                  @table_name        varchar(32),
  6523.                  @table_owner        varchar(32) = null,
  6524.                  @table_qualifier    varchar(32) = null,
  6525.                  @index_name        varchar(32) = '%',
  6526.                  @is_unique         char(1) = 'N',
  6527.                  @accuracy            char(1) = 'Q')
  6528. AS
  6529.     set nocount on
  6530.     DECLARE @indid                int
  6531.     DECLARE @lastindid            int
  6532.     DECLARE @table_id            int
  6533.     DECLARE @full_table_name    varchar(65) /* 2*32+1 */
  6534.  
  6535.     create table #TmpIndex(
  6536.         TABLE_QUALIFIER varchar(32) NULL,
  6537.         TABLE_OWNER     varchar(32) NULL,
  6538.         TABLE_NAME        varchar(32) NOT NULL,
  6539.         INDEX_QUALIFIER varchar(32) null,
  6540.         INDEX_NAME        varchar(32) null,
  6541.         NON_UNIQUE        smallint null,
  6542.         TYPE            smallint NOT NULL,
  6543.         SEQ_IN_INDEX    smallint null,
  6544.         COLUMN_NAME     varchar(32) null,
  6545.         COLLATION        char(1) null,
  6546.         index_id        int null,
  6547.         CARDINALITY     int null,
  6548.         PAGES            int null,
  6549.         status            smallint NOT NULL)
  6550.  
  6551.     if @table_qualifier is not null
  6552.     begin
  6553.         if db_name() <> @table_qualifier
  6554.         begin    /* If qualifier doesn't match current database */
  6555.             raiserror 20001 '~~Rush_5~~'
  6556.             return
  6557.         end
  6558.     end
  6559.  
  6560.     if @accuracy not in ('Q','E')
  6561.         begin
  6562.             raiserror 20002 '~~Rush_58~~'
  6563.             return
  6564.         end
  6565.  
  6566.     if (@@trancount <> 0 and
  6567.         charindex('6.50', @@version) = 0 and
  6568.         charindex('7.00', @@version) = 0 and
  6569.         charindex('8.00', @@version) = 0)
  6570.     begin    /* If inside a transaction */
  6571.         raiserror 20003 '~~Rush_59~~'
  6572.         return
  6573.     end
  6574.  
  6575.     if @table_owner is null
  6576.     begin    /* If unqualified table name */
  6577.         SELECT @full_table_name = @table_name
  6578.     end
  6579.     else
  6580.     begin    /* Qualified table name */
  6581.         if @table_owner = ''
  6582.         begin    /* If empty owner name */
  6583.             SELECT @full_table_name = @table_owner
  6584.         end
  6585.         else
  6586.         begin
  6587.             SELECT @full_table_name = @table_owner + '.' + @table_name
  6588.         end
  6589.     end
  6590.     /*    Get Object ID */
  6591.     SELECT @table_id = object_id(@full_table_name)
  6592.  
  6593.     /*    Start at lowest index id */
  6594.     SELECT @indid = min(indid)
  6595.     FROM sysindexes
  6596.     WHERE id = @table_id
  6597.         AND indid > 0
  6598.         AND indid < 255
  6599.  
  6600.     WHILE @indid is not NULL
  6601.     BEGIN
  6602.         INSERT #TmpIndex    /* Add all columns that are in index */
  6603.             SELECT
  6604.                 DB_NAME(),                                /* TABLE_QUALIFIER */
  6605.                 USER_NAME(o.uid),                        /* TABLE_OWNER       */
  6606.                 o.name,                                 /* TABLE_NAME       */
  6607.                 o.name,                                 /* INDEX_QUALIFIER */
  6608.                 x.name,                                 /* INDEX_NAME       */
  6609.                 0,                                        /* NON_UNIQUE       */
  6610.                 1,                                        /* SQL_INDEX_CLUSTERED */
  6611.                 colid,                                    /* SEQ_IN_INDEX    */
  6612.                 INDEX_COL(@full_table_name,indid,colid),/* COLUMN_NAME       */
  6613.                 'A',                                    /* COLLATION       */
  6614.                 @indid,                                 /* index_id        */
  6615.                 x.rows,                                 /* CARDINALITY       */
  6616.                 x.dpages,                                /* PAGES           */
  6617.                 x.status                                /* status            */
  6618.             FROM sysindexes x, syscolumns c, sysobjects o
  6619.             WHERE
  6620.                 x.id = @table_id
  6621.                 AND x.id = o.id
  6622.                 AND x.id = c.id
  6623.                 AND c.colid < keycnt+(x.status&16)/16    /* all but Unique Clust indices have an extra key */
  6624.                 AND x.indid = @indid
  6625.         /*
  6626.         **      Now move @indid to the next index.
  6627.         */
  6628.         SELECT @lastindid = @indid
  6629.         SELECT @indid = NULL
  6630.  
  6631.         SELECT @indid = min(indid)
  6632.         FROM sysindexes
  6633.         WHERE id = @table_id
  6634.             AND indid > @lastindid
  6635.             AND indid < 255
  6636.     END
  6637.  
  6638.     UPDATE #TmpIndex
  6639.         SET NON_UNIQUE = 1
  6640.         WHERE status&2 <> 2 /* If non-unique index */
  6641.     UPDATE #TmpIndex
  6642.         SET
  6643.             TYPE = 3,            /* SQL_INDEX_OTHER */
  6644.             CARDINALITY = NULL,
  6645.             PAGES = NULL
  6646.         WHERE index_id > 1    /* If non-clustered index */
  6647.  
  6648.     /* now add row for table statistics */
  6649.     INSERT #TmpIndex
  6650.         SELECT
  6651.             DB_NAME(),                /* TABLE_QUALIFIER */
  6652.             USER_NAME(o.uid),        /* TABLE_OWNER       */
  6653.             o.name,                 /* TABLE_NAME       */
  6654.             null,                    /* INDEX_QUALIFIER */
  6655.             null,                    /* INDEX_NAME       */
  6656.             null,                    /* NON_UNIQUE       */
  6657.             0,                        /* SQL_TABLE_STAT  */
  6658.             null,                    /* SEQ_IN_INDEX    */
  6659.             null,                    /* COLUMN_NAME       */
  6660.             null,                    /* COLLATION       */
  6661.             0,                        /* index_id        */
  6662.             x.rows,                 /* CARDINALITY       */
  6663.             x.dpages,                /* PAGES           */
  6664.             0                        /* status           */
  6665.         FROM sysindexes x, sysobjects o
  6666.         WHERE o.id = @table_id
  6667.             AND x.id = o.id
  6668.             AND (x.indid = 0 or x.indid = 1)    /*    If there are no indexes */
  6669.                                                 /*    then table stats are in */
  6670.                                                 /*    a row with indid =0        */
  6671.  
  6672.     if @is_unique <> 'Y'    /* If all indexes desired */
  6673.         SELECT
  6674.             TABLE_QUALIFIER,
  6675.             TABLE_OWNER,
  6676.             TABLE_NAME,
  6677.             NON_UNIQUE,
  6678.             INDEX_QUALIFIER,
  6679.             INDEX_NAME,
  6680.             TYPE,
  6681.             SEQ_IN_INDEX,
  6682.             COLUMN_NAME,
  6683.             COLLATION,
  6684.             CARDINALITY,
  6685.             PAGES,
  6686.             FILTER_CONDITION = convert(varchar(128),null)
  6687.         FROM #TmpIndex
  6688.         WHERE
  6689.             INDEX_NAME like @index_name /* If matching name */
  6690.             or INDEX_NAME is null        /* If SQL_TABLE_STAT row */
  6691.         ORDER BY 4, 7, 6, 8
  6692.     else                    /* If only unique indexes desired */
  6693.         SELECT
  6694.             TABLE_QUALIFIER,
  6695.             TABLE_OWNER,
  6696.             TABLE_NAME,
  6697.             NON_UNIQUE,
  6698.             INDEX_QUALIFIER,
  6699.             INDEX_NAME,
  6700.             TYPE,
  6701.             SEQ_IN_INDEX,
  6702.             COLUMN_NAME,
  6703.             COLLATION,
  6704.             CARDINALITY,
  6705.             PAGES,
  6706.             FILTER_CONDITION = convert(varchar(128),null)
  6707.         FROM #TmpIndex
  6708.         WHERE
  6709.             (NON_UNIQUE = 0             /* If unique */
  6710.                 or NON_UNIQUE is NULL)    /* If SQL_TABLE_STAT row */
  6711.             and (INDEX_NAME like @index_name    /* If matching name */
  6712.                 or INDEX_NAME is null)    /* If SQL_TABLE_STAT row */
  6713.         ORDER BY 4, 7, 6, 8
  6714.  
  6715.     DROP TABLE #TmpIndex
  6716. go
  6717.  
  6718. if (charindex('7.00', @@version) = 0 and
  6719.     charindex('8.00', @@version) = 0)
  6720. begin
  6721.     print ''
  6722.     print ''
  6723.     print 'Warning:'
  6724.     print 'you are installing the stored procedures '
  6725.     print 'on a pre 7.0 SQL Server.'
  6726.     print 'Ignore the following errors.'
  6727. end
  6728. else
  6729.     drop proc sp_statistics
  6730. go
  6731.  
  6732. /*    Procedure for 7.0 server */
  6733. CREATE PROCEDURE sp_statistics (
  6734.                  @table_name        sysname,
  6735.                  @table_owner        sysname = null,
  6736.                  @table_qualifier    sysname = null,
  6737.                  @index_name        sysname = '%',
  6738.                  @is_unique         char(1) = 'N',
  6739.                  @accuracy            char(1) = 'Q')
  6740. AS
  6741.     set nocount on
  6742.     DECLARE @indid                int
  6743.     DECLARE @lastindid            int
  6744.     DECLARE @table_id            int
  6745.     DECLARE @full_table_name    nvarchar(257)
  6746.  
  6747.     create table #TmpIndex(
  6748.         TABLE_QUALIFIER sysname NULL,
  6749.         TABLE_OWNER     sysname NULL,
  6750.         TABLE_NAME        sysname NOT NULL,
  6751.         INDEX_QUALIFIER sysname null,
  6752.         INDEX_NAME        sysname null,
  6753.         NON_UNIQUE        smallint null,
  6754.         TYPE            smallint NOT NULL,
  6755.         SEQ_IN_INDEX    smallint null,
  6756.         COLUMN_NAME     sysname null,
  6757.         COLLATION        char(1) null,
  6758.         index_id        int null,
  6759.         CARDINALITY     int null,
  6760.         PAGES            int null,
  6761.         status            int NOT NULL)
  6762.  
  6763.     if @table_qualifier is not null
  6764.     begin
  6765.         if db_name() <> @table_qualifier
  6766.         begin    /* If qualifier doesn't match current database */
  6767.             raiserror (15250, -1,-1)
  6768.             return
  6769.         end
  6770.     end
  6771.  
  6772.     if @accuracy not in ('Q','E')
  6773.         begin
  6774.             raiserror (15251,-1,-1,'accuracy','''Q'' or ''E''')
  6775.             return
  6776.         end
  6777.  
  6778.     if @table_owner is null
  6779.     begin    /* If unqualified table name */
  6780.         SELECT @full_table_name = quotename(@table_name)
  6781.     end
  6782.     else
  6783.     begin    /* Qualified table name */
  6784.         if @table_owner = ''
  6785.         begin    /* If empty owner name */
  6786.             SELECT @full_table_name = quotename(@table_owner)
  6787.         end
  6788.         else
  6789.         begin
  6790.             SELECT @full_table_name = quotename(@table_owner) +
  6791.                 '.' + quotename(@table_name)
  6792.         end
  6793.     end
  6794.     /*    Get Object ID */
  6795.     SELECT @table_id = object_id(@full_table_name)
  6796.  
  6797.     /*    Start at lowest index id */
  6798.     SELECT @indid = min(indid)
  6799.     FROM sysindexes
  6800.     WHERE not (@table_id is null)
  6801.         AND id = @table_id
  6802.         AND indid > 0
  6803.         AND indid < 255
  6804.  
  6805.     /* Create a temp table to correct the ordinal position of the columns */
  6806.     create table #TmpColumns
  6807.     (ordinal int identity(1,1),
  6808.      colid   smallint not null)
  6809.  
  6810.     /* Load columns into the temp table */
  6811.     insert into #TmpColumns (colid)
  6812.     select c.colid
  6813.     from syscolumns c
  6814.     where c.id = @table_id
  6815.     order by c.colid
  6816.     
  6817.     WHILE @indid is not NULL
  6818.     BEGIN
  6819.         INSERT #TmpIndex    /* Add all columns that are in index */
  6820.             SELECT
  6821.                 DB_NAME(),                                /* TABLE_QUALIFIER */
  6822.                 USER_NAME(o.uid),                        /* TABLE_OWNER       */
  6823.                 o.name,                                 /* TABLE_NAME       */
  6824.                 o.name,                                 /* INDEX_QUALIFIER */
  6825.                 x.name,                                 /* INDEX_NAME       */
  6826.                 case                                    /* NON_UNIQUE       */
  6827.                     WHEN x.status&2 <> 2 then 1            /* Nonunique index */
  6828.                     else 0                                /* Unique index    */
  6829.                 end,
  6830.                 case                                    /* TYPE            */
  6831.                     when @indid > 1 then 3                /* Non-Clustered   */
  6832.                     else 1                                /* Clustered index */
  6833.                 end,
  6834.                 tc.ordinal,                                    /* SEQ_IN_INDEX    */
  6835.                 INDEX_COL(@full_table_name, indid, tc.ordinal),/* COLUMN_NAME       */
  6836.                 'A',                                    /* COLLATION       */
  6837.                 @indid,                                 /* index_id        */
  6838.                 case                                    /* CARDINALITY       */
  6839.                     when @indid > 1 then NULL            /* Non-Clustered   */
  6840.                     else x.rows                         /* Clustered index */
  6841.                 end,
  6842.                 case                                    /* PAGES           */
  6843.                     when @indid > 1 then NULL            /* Non-Clustered   */
  6844.                     else x.dpages                        /* Clustered index */
  6845.                 end,
  6846.                 x.status                                /* status            */
  6847.             FROM sysindexes x, syscolumns c, sysobjects o, #TmpColumns tc
  6848.             WHERE
  6849.                 not (@table_id is null)
  6850.                 AND x.id = @table_id
  6851.                 AND x.id = o.id
  6852.                 AND x.id = c.id
  6853.                 AND tc.colid = c.colid
  6854.                 AND tc.ordinal < keycnt+(x.status&18)/18    /* all but Unique Clust indices have an extra key */
  6855.                 AND INDEX_COL(@full_table_name, indid, tc.ordinal) IS NOT NULL
  6856.                 AND indid = @indid
  6857.                 AND (x.status&2 = 2
  6858.                     OR @is_unique <> 'Y')
  6859.                 AND (x.status&32) = 0
  6860.         /*
  6861.         **      Now move @indid to the next index.
  6862.         */
  6863.         SELECT @lastindid = @indid
  6864.         SELECT @indid = NULL
  6865.  
  6866.         SELECT @indid = min(indid)
  6867.         FROM sysindexes
  6868.         WHERE not (@table_id is null)
  6869.             AND id = @table_id
  6870.             AND indid > @lastindid
  6871.             AND indid < 255
  6872.     END
  6873.  
  6874.     /* now add row for table statistics */
  6875.     INSERT #TmpIndex
  6876.         SELECT
  6877.             DB_NAME(),                /* TABLE_QUALIFIER */
  6878.             USER_NAME(o.uid),        /* TABLE_OWNER       */
  6879.             o.name,                 /* TABLE_NAME       */
  6880.             null,                    /* INDEX_QUALIFIER */
  6881.             null,                    /* INDEX_NAME       */
  6882.             null,                    /* NON_UNIQUE       */
  6883.             0,                        /* SQL_TABLE_STAT  */
  6884.             null,                    /* SEQ_IN_INDEX    */
  6885.             null,                    /* COLUMN_NAME       */
  6886.             null,                    /* COLLATION       */
  6887.             0,                        /* index_id        */
  6888.             x.rows,                 /* CARDINALITY       */
  6889.             x.dpages,                /* PAGES           */
  6890.             0                        /* status           */
  6891.         FROM sysindexes x, sysobjects o
  6892.         WHERE not (@table_id is null)
  6893.             AND o.id = @table_id
  6894.             AND x.id = o.id
  6895.             AND (x.indid = 0 or x.indid = 1)    /*    If there are no indexes */
  6896.                                                 /*    then table stats are in */
  6897.                                                 /*    a row with indid =0        */
  6898.  
  6899.     SELECT
  6900.         TABLE_QUALIFIER,
  6901.         TABLE_OWNER,
  6902.         TABLE_NAME,
  6903.         NON_UNIQUE,
  6904.         INDEX_QUALIFIER,
  6905.         INDEX_NAME,
  6906.         TYPE,
  6907.         SEQ_IN_INDEX,
  6908.         COLUMN_NAME,
  6909.         COLLATION,
  6910.         CARDINALITY,
  6911.         PAGES,
  6912.         FILTER_CONDITION = convert(varchar(128),null)
  6913.     FROM #TmpIndex
  6914.     WHERE
  6915.         INDEX_NAME like @index_name /* If matching name */
  6916.         or INDEX_NAME is null        /* If SQL_TABLE_STAT row */
  6917.     ORDER BY 4, 7, 6, 8
  6918.  
  6919.     DROP TABLE #TmpIndex, #TmpColumns
  6920. go
  6921.  
  6922. if (charindex('8.00', @@version) = 0)
  6923. begin
  6924.     print ''
  6925.     print ''
  6926.     print 'Warning:'
  6927.     print 'you are installing the stored procedures '
  6928.     print 'on a pre 8.0 SQL Server.'
  6929.     print 'Ignore the following errors.'
  6930. end
  6931. else
  6932.     drop proc sp_statistics
  6933. go
  6934.  
  6935. /*    Procedure for 8.0 server */
  6936. CREATE PROCEDURE sp_statistics (
  6937.                  @table_name        sysname,
  6938.                  @table_owner        sysname = null,
  6939.                  @table_qualifier    sysname = null,
  6940.                  @index_name        sysname = '%',
  6941.                  @is_unique         char(1) = 'N',
  6942.                  @accuracy            char(1) = 'Q')
  6943. AS
  6944.     set nocount on
  6945.     DECLARE @indid                int
  6946.     DECLARE @lastindid            int
  6947.     DECLARE @table_id            int
  6948.     DECLARE @full_table_name    nvarchar(257)
  6949.  
  6950.     create table #TmpIndex(
  6951.         TABLE_QUALIFIER sysname collate database_default NULL,
  6952.         TABLE_OWNER     sysname collate database_default NULL,
  6953.         TABLE_NAME        sysname collate database_default NOT NULL,
  6954.         INDEX_QUALIFIER sysname collate database_default null,
  6955.         INDEX_NAME        sysname collate database_default null,
  6956.         NON_UNIQUE        smallint null,
  6957.         TYPE            smallint NOT NULL,
  6958.         SEQ_IN_INDEX    smallint null,
  6959.         COLUMN_NAME     sysname collate database_default null,
  6960.         COLLATION        char(1) collate database_default null,
  6961.         index_id        int null,
  6962.         CARDINALITY     int null,
  6963.         PAGES            int null,
  6964.         status            int NOT NULL)
  6965.  
  6966.     if @table_qualifier is not null
  6967.     begin
  6968.         if db_name() <> @table_qualifier
  6969.         begin    /* If qualifier doesn't match current database */
  6970.             raiserror (15250, -1,-1)
  6971.             return
  6972.         end
  6973.     end
  6974.  
  6975.     if @accuracy not in ('Q','E')
  6976.         begin
  6977.             raiserror (15251,-1,-1,'accuracy','''Q'' or ''E''')
  6978.             return
  6979.         end
  6980.  
  6981.     if @table_owner is null
  6982.     begin    /* If unqualified table name */
  6983.         SELECT @full_table_name = quotename(@table_name)
  6984.     end
  6985.     else
  6986.     begin    /* Qualified table name */
  6987.         if @table_owner = ''
  6988.         begin    /* If empty owner name */
  6989.             SELECT @full_table_name = quotename(@table_owner)
  6990.         end
  6991.         else
  6992.         begin
  6993.             SELECT @full_table_name = quotename(@table_owner) +
  6994.                 '.' + quotename(@table_name)
  6995.         end
  6996.     end
  6997.     /*    Get Object ID */
  6998.     SELECT @table_id = object_id(@full_table_name)
  6999.  
  7000.     /*    Start at lowest index id */
  7001.     SELECT @indid = min(indid)
  7002.     FROM sysindexes
  7003.     WHERE not (@table_id is null)
  7004.         AND id = @table_id
  7005.         AND indid > 0
  7006.         AND indid < 255
  7007.  
  7008.     /* Create a temp table to correct the ordinal position of the columns */
  7009.     create table #TmpColumns
  7010.     (ordinal int identity(1,1),
  7011.      colid   smallint not null)
  7012.  
  7013.     /* Load columns into the temp table */
  7014.     insert into #TmpColumns (colid)
  7015.     select c.colid
  7016.     from syscolumns c
  7017.     where c.id = @table_id
  7018.     order by c.colid
  7019.     
  7020.     WHILE @indid is not NULL
  7021.     BEGIN
  7022.         INSERT #TmpIndex    /* Add all columns that are in index */
  7023.             SELECT
  7024.                 DB_NAME(),                                /* TABLE_QUALIFIER */
  7025.                 USER_NAME(o.uid),                        /* TABLE_OWNER       */
  7026.                 o.name,                                 /* TABLE_NAME       */
  7027.                 o.name,                                 /* INDEX_QUALIFIER */
  7028.                 x.name,                                 /* INDEX_NAME       */
  7029.                 case                                    /* NON_UNIQUE       */
  7030.                     WHEN x.status&2 <> 2 then 1            /* Nonunique index */
  7031.                     else 0                                /* Unique index    */
  7032.                 end,
  7033.                 case                                    /* TYPE            */
  7034.                     when @indid > 1 then 3                /* Non-Clustered   */
  7035.                     else 1                                /* Clustered index */
  7036.                 end,
  7037.                 tc.ordinal,                                    /* SEQ_IN_INDEX    */
  7038.                 INDEX_COL(@full_table_name, indid, tc.ordinal),/* COLUMN_NAME       */
  7039.                 'A',                                    /* COLLATION       */
  7040.                 @indid,                                 /* index_id        */
  7041.                 case                                    /* CARDINALITY       */
  7042.                     when @indid > 1 then NULL            /* Non-Clustered   */
  7043.                     else x.rows                         /* Clustered index */
  7044.                 end,
  7045.                 case                                    /* PAGES           */
  7046.                     when @indid > 1 then NULL            /* Non-Clustered   */
  7047.                     else x.dpages                        /* Clustered index */
  7048.                 end,
  7049.                 x.status                                /* status            */
  7050.             FROM sysindexes x, syscolumns c, sysobjects o, #TmpColumns tc
  7051.             WHERE
  7052.                 not (@table_id is null)
  7053.                 AND x.id = @table_id
  7054.                 AND x.id = o.id
  7055.                 AND x.id = c.id
  7056.                 AND tc.colid = c.colid
  7057.                 AND tc.ordinal < keycnt+(x.status&18)/18    /* all but Unique Clust indices have an extra key */
  7058.                 AND INDEX_COL(@full_table_name, indid, tc.ordinal) IS NOT NULL
  7059.                 AND indid = @indid
  7060.                 AND (x.status&2 = 2
  7061.                     OR @is_unique <> 'Y')
  7062.                 AND (x.status&32) = 0
  7063.         /*
  7064.         **      Now move @indid to the next index.
  7065.         */
  7066.         SELECT @lastindid = @indid
  7067.         SELECT @indid = NULL
  7068.  
  7069.         SELECT @indid = min(indid)
  7070.         FROM sysindexes
  7071.         WHERE not (@table_id is null)
  7072.             AND id = @table_id
  7073.             AND indid > @lastindid
  7074.             AND indid < 255
  7075.     END
  7076.  
  7077.     /* now add row for table statistics */
  7078.     INSERT #TmpIndex
  7079.         SELECT
  7080.             DB_NAME(),                /* TABLE_QUALIFIER */
  7081.             USER_NAME(o.uid),        /* TABLE_OWNER       */
  7082.             o.name,                 /* TABLE_NAME       */
  7083.             null,                    /* INDEX_QUALIFIER */
  7084.             null,                    /* INDEX_NAME       */
  7085.             null,                    /* NON_UNIQUE       */
  7086.             0,                        /* SQL_TABLE_STAT  */
  7087.             null,                    /* SEQ_IN_INDEX    */
  7088.             null,                    /* COLUMN_NAME       */
  7089.             null,                    /* COLLATION       */
  7090.             0,                        /* index_id        */
  7091.             x.rows,                 /* CARDINALITY       */
  7092.             x.dpages,                /* PAGES           */
  7093.             0                        /* status           */
  7094.         FROM sysindexes x, sysobjects o
  7095.         WHERE not (@table_id is null)
  7096.             AND o.id = @table_id
  7097.             AND x.id = o.id
  7098.             AND (x.indid = 0 or x.indid = 1)    /*    If there are no indexes */
  7099.                                                 /*    then table stats are in */
  7100.                                                 /*    a row with indid =0        */
  7101.  
  7102.     SELECT
  7103.         TABLE_QUALIFIER,
  7104.         TABLE_OWNER,
  7105.         TABLE_NAME,
  7106.         NON_UNIQUE,
  7107.         INDEX_QUALIFIER,
  7108.         INDEX_NAME,
  7109.         TYPE,
  7110.         SEQ_IN_INDEX,
  7111.         COLUMN_NAME,
  7112.         COLLATION,
  7113.         CARDINALITY,
  7114.         PAGES,
  7115.         FILTER_CONDITION = convert(varchar(128),null)
  7116.     FROM #TmpIndex
  7117.     WHERE
  7118.         INDEX_NAME like @index_name /* If matching name */
  7119.         or INDEX_NAME is null        /* If SQL_TABLE_STAT row */
  7120.     ORDER BY 4, 7, 6, 8
  7121.  
  7122.     DROP TABLE #TmpIndex, #TmpColumns
  7123. go
  7124.  
  7125. grant execute on sp_statistics to public
  7126. go
  7127.  
  7128. dump tran master with no_log
  7129. go
  7130.  
  7131. print 'creating sp_stored_procedures'
  7132. go
  7133.  
  7134.  
  7135. /* pre 8.0 version */
  7136. create procedure sp_stored_procedures(
  7137.                         @sp_name        varchar(102) = null,
  7138.                         @sp_owner        varchar(96) = null,
  7139.                         @sp_qualifier    varchar(32) = null)
  7140. as
  7141.     declare @proc_type smallint
  7142.  
  7143.     if @sp_qualifier is not null
  7144.     begin
  7145.         if db_name() <> @sp_qualifier
  7146.         begin
  7147.             if @sp_qualifier = ''
  7148.             begin
  7149.                 /* in this case, we need to return an empty result set */
  7150.                 /* because the user has requested a database with an empty name */
  7151.                 select @sp_name = ''
  7152.                 select @sp_owner = ''
  7153.             end else
  7154.             begin    /* If qualifier doesn't match current database */
  7155.                 raiserror 20001 '~~Rush_51~~'
  7156.                 return
  7157.             end
  7158.         end
  7159.     end
  7160.  
  7161.     if @sp_name is null
  7162.     begin  /*  If procedure name not supplied, match all */
  7163.         select @sp_name = '%'
  7164.     end
  7165.     else begin
  7166.         if (@sp_owner is null) and (charindex('%', @sp_name) = 0)
  7167.         begin
  7168.             if exists (select * from sysobjects
  7169.                 where uid = user_id()
  7170.                     and name = @sp_name
  7171.                     and type = 'P') /* Object type of Procedure */
  7172.             begin
  7173.                 select @sp_owner = user_name()
  7174.             end
  7175.         end
  7176.     end
  7177.     if @sp_owner is null    /*    If procedure owner not supplied, match all */
  7178.         select @sp_owner = '%'
  7179.  
  7180.     select @proc_type=2        /* Return 2 for 4.2 and later servers. */
  7181.  
  7182.     select
  7183.         PROCEDURE_QUALIFIER = convert(varchar(32),db_name()),
  7184.         PROCEDURE_OWNER = convert(varchar(32),user_name(o.uid)),
  7185.         PROCEDURE_NAME = convert(varchar(36),o.name +';'+ ltrim(str(c.number,5))),
  7186.         NUM_INPUT_PARAMS = -1,    /* Constant since value unknown */
  7187.         NUM_OUTPUT_PARAMS = -1, /* Constant since value unknown */
  7188.         NUM_RESULT_SETS = -1,    /* Constant since value unknown */
  7189.         REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  7190.         PROCEDURE_TYPE = @proc_type
  7191.     from
  7192.         sysobjects o,syscomments c,sysusers u
  7193.     where
  7194.         o.name like @sp_name
  7195.         and c.colid = 1
  7196.         and user_name(o.uid) like @sp_owner
  7197.         and o.type = 'P'        /* Object type of Procedure */
  7198.         and c.id = o.id
  7199.         and u.uid = user_id()    /* constrain sysusers uid for use in subquery */
  7200.         and (suser_id() = 1     /* User is the System Administrator */
  7201.             or o.uid = user_id()    /* User created the object */
  7202.             /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  7203.             or ((select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  7204.              from sysprotects p
  7205.              /* outer join to correlate with all rows in sysobjects */
  7206.              where p.id =* o.id
  7207.                  /*  get rows for public,current user,user's group */
  7208.                  and (p.uid = 0 or p.uid = user_id() or p.uid =* u.gid)
  7209.                  /* check for SELECT,EXECUTE privilege */
  7210.                  and (action in (193,224)))&1    /* more magic...normalize GRANT */
  7211.             ) = 1     /* final magic...compare Grants    */
  7212.         )
  7213.     order by 1, 2, 3
  7214. go
  7215.  
  7216. grant execute on sp_stored_procedures to public
  7217. go
  7218.  
  7219.  
  7220. if (charindex('7.00', @@version) > 0 or
  7221.     charindex('8.00', @@version) > 0)
  7222.     drop procedure sp_stored_procedures
  7223. else
  7224. begin
  7225.     print ''
  7226.     print ''
  7227.     print 'Warning:'
  7228.     print 'you are installing the stored procedures '
  7229.     print 'on a pre 7.0 SQL Server.'
  7230.     print 'Ignore the following errors.'
  7231. end
  7232. go
  7233.  
  7234.  
  7235. /* 7.0 version */
  7236. create procedure sp_stored_procedures(
  7237.                         @sp_name        nvarchar(390) = null,
  7238.                         @sp_owner        nvarchar(384) = null,
  7239.                         @sp_qualifier    sysname = null)
  7240. as
  7241.     declare @proc_type smallint
  7242.  
  7243.     if @sp_qualifier is not null
  7244.     begin
  7245.         if db_name() <> @sp_qualifier
  7246.         begin
  7247.             if @sp_qualifier = ''
  7248.             begin
  7249.                 /* in this case, we need to return an empty result set */
  7250.                 /* because the user has requested a database with an empty name */
  7251.                 select @sp_name = ''
  7252.                 select @sp_owner = ''
  7253.             end else
  7254.             begin    /* If qualifier doesn't match current database */
  7255.                 raiserror (15250, -1,-1)
  7256.                 return
  7257.             end
  7258.         end
  7259.     end
  7260.  
  7261.     if @sp_name is null
  7262.     begin  /*  If procedure name not supplied, match all */
  7263.         select @sp_name = '%'
  7264.     end
  7265.     else begin
  7266.         if (@sp_owner is null) and (charindex('%', @sp_name) = 0)
  7267.         begin
  7268.             if exists (select * from sysobjects
  7269.                 where uid = user_id()
  7270.                     and name = @sp_name
  7271.                     and type = 'P') /* Object type of Procedure */
  7272.             begin
  7273.                 select @sp_owner = user_name()
  7274.             end
  7275.         end
  7276.     end
  7277.     if @sp_owner is null    /*    If procedure owner not supplied, match all */
  7278.         select @sp_owner = '%'
  7279.  
  7280.     select @proc_type=2        /* Return 2 for 4.2 and later servers. */
  7281.  
  7282.     select
  7283.         PROCEDURE_QUALIFIER = convert(sysname,db_name()),
  7284.         PROCEDURE_OWNER = convert(sysname,user_name(o.uid)),
  7285.         PROCEDURE_NAME = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  7286.         NUM_INPUT_PARAMS = -1,    /* Constant since value unknown */
  7287.         NUM_OUTPUT_PARAMS = -1, /* Constant since value unknown */
  7288.         NUM_RESULT_SETS = -1,    /* Constant since value unknown */
  7289.         REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  7290.         PROCEDURE_TYPE = @proc_type
  7291.     from
  7292.         sysobjects o,syscomments c
  7293.     where
  7294.         o.name like @sp_name
  7295.         and c.colid = 1
  7296.         and user_name(o.uid) like @sp_owner
  7297.         and o.type = 'P'        /* Object type of Procedure */
  7298.         and c.id = o.id
  7299.         and permissions (o.id)&32 <> 0
  7300.     order by 1, 2, 3
  7301. go
  7302.  
  7303. grant execute on sp_stored_procedures to public
  7304. go
  7305.  
  7306. if (charindex('8.00', @@version) > 0)
  7307.     drop procedure sp_stored_procedures
  7308. else
  7309. begin
  7310.     print ''
  7311.     print ''
  7312.     print 'Warning:'
  7313.     print 'you are installing the stored procedures '
  7314.     print 'on a pre 8.0 SQL Server.'
  7315.     print 'Ignore the following errors.'
  7316. end
  7317. go
  7318.  
  7319.  
  7320. /* 8.0 version */
  7321. create procedure sp_stored_procedures(
  7322.                         @sp_name    nvarchar(390) = null,
  7323.                         @sp_owner    nvarchar(384) = null,
  7324.                         @sp_qualifier    sysname = null)
  7325. as
  7326.     declare @proc_type smallint
  7327.  
  7328.     if @sp_qualifier is not null
  7329.     begin
  7330.         if db_name() <> @sp_qualifier
  7331.         begin
  7332.             if @sp_qualifier = ''
  7333.             begin
  7334.                 /* in this case, we need to return an empty result set */
  7335.                 /* because the user has requested a database with an empty name */
  7336.                 select @sp_name = ''
  7337.                 select @sp_owner = ''
  7338.             end else
  7339.             begin    /* If qualifier doesn't match current database */
  7340.                 raiserror (15250, -1,-1)
  7341.                 return
  7342.             end
  7343.         end
  7344.     end
  7345.  
  7346.     if @sp_name is null
  7347.     begin  /*  If procedure name not supplied, match all */
  7348.         select @sp_name = '%'
  7349.     end
  7350.     else
  7351.     begin
  7352.     if (@sp_owner is null) and (charindex('%', @sp_name) = 0)
  7353.     begin
  7354.         if exists (select * from sysobjects
  7355.             where uid = user_id()
  7356.                 and name = @sp_name
  7357.                 and type in ('P', 'FN', 'TF', 'IF'))        /* Procedures, scalar UDF, table UDF */
  7358.         begin
  7359.             select @sp_owner = user_name()
  7360.         end
  7361.     end
  7362.     end
  7363.     
  7364.     if @sp_owner is null        /* If procedure owner not supplied, match all */
  7365.     select @sp_owner = '%'
  7366.  
  7367.     select @proc_type=2            /* Return 2 for 4.2 and later servers. */
  7368.  
  7369.     select
  7370.         PROCEDURE_QUALIFIER = convert(sysname,db_name()),
  7371.         PROCEDURE_OWNER = convert(sysname,user_name(o.uid)),
  7372.         PROCEDURE_NAME = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  7373.         NUM_INPUT_PARAMS = -1,    /* Constant since value unknown */
  7374.         NUM_OUTPUT_PARAMS = -1, /* Constant since value unknown */
  7375.         NUM_RESULT_SETS = -1,    /* Constant since value unknown */
  7376.         REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  7377.         PROCEDURE_TYPE = @proc_type
  7378.     from
  7379.         sysobjects o,syscomments c
  7380.     where
  7381.         o.name like @sp_name
  7382.         and c.colid = 1
  7383.         and user_name(o.uid) like @sp_owner
  7384.         and o.type in ('P', 'FN', 'TF', 'IF')        /* Object type of Procedure, scalar UDF, table UDF */
  7385.         and c.id = o.id
  7386.         and permissions (o.id)&32 <> 0
  7387.     order by 1, 2, 3
  7388. go
  7389.  
  7390. grant execute on sp_stored_procedures to public
  7391. go
  7392.  
  7393. dump tran master with no_log
  7394. go
  7395.  
  7396.  
  7397. print 'creating sp_table_privileges'
  7398. go
  7399.  
  7400. /*    Procedure for pre 6.50 server */
  7401. CREATE PROCEDURE sp_table_privileges (
  7402.             @table_name         varchar(90),
  7403.             @table_owner        varchar(90) = null,
  7404.             @table_qualifier    varchar(32) = null)
  7405. as
  7406.     set nocount on
  7407.  
  7408.     declare @table_id    int,
  7409.             @owner_id     int,
  7410.             @full_table_name char(181)
  7411.     declare @refconst int
  7412.  
  7413.     select @refconst = 1
  7414.     if    (charindex('6.00', @@version) = 0)
  7415.         select @refconst = NULL
  7416.  
  7417.     if @table_qualifier is not null
  7418.     begin
  7419.         if db_name() != @table_qualifier
  7420.         begin    /* If qualifier doesn't match current database */
  7421.             raiserror 20001 'Table qualifier must be name of current database'
  7422.             return
  7423.         end
  7424.     end
  7425.     if @table_owner is null
  7426.     begin    /* If unqualified table name */
  7427.         SELECT @full_table_name = @table_name
  7428.     end
  7429.     else
  7430.     begin    /* Qualified table name */
  7431.         SELECT @full_table_name = @table_owner + '.' + @table_name
  7432.     end
  7433.     /*    Get Object ID */
  7434.     SELECT @table_id = object_id(@full_table_name)
  7435.  
  7436.     if @@trancount != 0
  7437.     begin    /* If inside a transaction */
  7438.         raiserror 20003 'The procedure ''sp_table_privileges'' cannot be executed from within a transaction.'
  7439.         return
  7440.     end
  7441.     create table #table_priv1(
  7442.         table_qualifier            varchar(32) NOT NULL,
  7443.         table_owner             varchar(32) NOT NULL,
  7444.         table_name                varchar(32) NOT NULL,
  7445.         grantor                 varchar(32) NOT NULL,
  7446.         grantee                 varchar(32) NOT NULL,
  7447.         select_privilege        int NOT NULL,
  7448.         insert_privilege        int NOT NULL,
  7449.         update_privilege        int NOT NULL,
  7450.         delete_privilege        int NOT NULL,
  7451.         references_privilege    int NULL,
  7452.         is_grantable            varchar(3) NOT NULL,
  7453.         uid                     int NOT NULL,
  7454.         gid                     int NOT NULL)
  7455.  
  7456.     insert into #table_priv1
  7457.         select distinct
  7458.             db_name(),
  7459.             user_name(o.uid),
  7460.             o.name,
  7461.             user_name(o.uid),
  7462.             u.name,
  7463.             0,
  7464.             0,
  7465.             0,
  7466.             0,
  7467.             @refconst,
  7468.             'no',
  7469.             u.uid,
  7470.             u.gid
  7471.         from sysusers u, sysobjects o
  7472.         where o.id = @table_id and u.uid != u.gid
  7473.            and sysstat & 0xf in (1,2,3)    /* only valid for system tables,
  7474.                                            ** user tables, and views. */
  7475.  
  7476.     /*
  7477.     ** now add row for table owner
  7478.     */
  7479.     if exists (
  7480.         select *
  7481.             from #table_priv1
  7482.             where grantor = grantee)
  7483.     begin
  7484.         update #table_priv1
  7485.         set
  7486.             select_privilege = 1,
  7487.             update_privilege = 1,
  7488.             insert_privilege = 1,
  7489.             delete_privilege = 1,
  7490.             references_privilege = 1,
  7491.             is_grantable = 'yes'
  7492.         where grantor = grantee
  7493.     end
  7494.     else
  7495.     begin
  7496.         insert into #table_priv1
  7497.             select    db_name(),
  7498.                 user_name(o.uid),
  7499.                 o.name,
  7500.                 user_name(o.uid),
  7501.                 user_name(o.uid),
  7502.                 1,
  7503.                 1,
  7504.                 1,
  7505.                 1,
  7506.                 @refconst,
  7507.                 'yes',
  7508.                 o.uid,
  7509.                 u.gid
  7510.             from sysobjects o, sysusers u
  7511.             where o.id = @table_id and u.uid = o.uid
  7512.             and sysstat & 0xf in (1,2,3)    /* only valid for system tables,
  7513.                                            ** user tables, and views. */
  7514.  
  7515.     end
  7516.  
  7517.     update #table_priv1
  7518.     set select_privilege = 1
  7519.     where
  7520.         exists (
  7521.             select * from sysprotects
  7522.             where
  7523.                 id = @table_id
  7524.                 and (#table_priv1.uid = uid
  7525.                     or #table_priv1.gid = uid
  7526.                     or uid = 0)
  7527.                 and protecttype = 205
  7528.                 and action = 193)
  7529.         and not exists (
  7530.             select * from sysprotects
  7531.             where
  7532.                 id = @table_id
  7533.                 and (#table_priv1.uid = uid
  7534.                     or #table_priv1.gid = uid
  7535.                     or uid = 0)
  7536.                 and protecttype = 206
  7537.                 and action = 193)
  7538.  
  7539.     update #table_priv1
  7540.     set insert_privilege = 1
  7541.     where
  7542.         exists (
  7543.             select * from sysprotects
  7544.             where
  7545.                 id = @table_id
  7546.                 and (#table_priv1.uid = uid
  7547.                     or #table_priv1.gid = uid
  7548.                     or uid = 0)
  7549.                 and protecttype = 205
  7550.                 and action = 195)
  7551.         and not exists (
  7552.             select * from sysprotects
  7553.             where
  7554.                 id = @table_id
  7555.                 and (#table_priv1.uid = uid
  7556.                     or #table_priv1.gid = uid
  7557.                     or uid = 0)
  7558.                 and protecttype = 206
  7559.                 and action = 195)
  7560.  
  7561.     update #table_priv1
  7562.     set delete_privilege = 1
  7563.     where
  7564.         exists (
  7565.             select * from sysprotects
  7566.             where
  7567.                 id = @table_id
  7568.                 and (#table_priv1.uid = uid
  7569.                     or #table_priv1.gid = uid
  7570.                     or uid = 0)
  7571.                 and protecttype = 205
  7572.                 and action = 196)
  7573.         and not exists (select * from sysprotects
  7574.             where
  7575.                 id = @table_id
  7576.                 and (#table_priv1.uid = uid
  7577.                     or #table_priv1.gid = uid
  7578.                     or uid = 0)
  7579.                 and protecttype = 206
  7580.                 and action = 196)
  7581.  
  7582.     update #table_priv1
  7583.     set update_privilege = 1
  7584.     where
  7585.         exists (
  7586.             select * from sysprotects
  7587.             where
  7588.                 id = @table_id
  7589.                 and (#table_priv1.uid = uid
  7590.                     or #table_priv1.gid = uid
  7591.                     or uid = 0)
  7592.                 and protecttype = 205
  7593.                 and action = 197)
  7594.         and not exists (
  7595.             select * from sysprotects
  7596.             where
  7597.                 id = @table_id
  7598.                 and (#table_priv1.uid = uid
  7599.                     or #table_priv1.gid = uid
  7600.                     or uid = 0)
  7601.                 and protecttype = 206
  7602.                 and action = 197)
  7603.  
  7604.     update #table_priv1
  7605.     set references_privilege = 1
  7606.     where
  7607.         exists (
  7608.             select * from sysprotects
  7609.             where
  7610.                 id = @table_id
  7611.                 and (#table_priv1.uid = uid
  7612.                     or #table_priv1.gid = uid
  7613.                     or uid = 0)
  7614.                 and protecttype = 205
  7615.                 and action = 26)
  7616.         and not exists (
  7617.             select * from sysprotects
  7618.             where
  7619.                 id = @table_id
  7620.                 and (#table_priv1.uid = uid
  7621.                     or #table_priv1.gid = uid
  7622.                     or uid = 0)
  7623.                 and protecttype = 206
  7624.                 and action = 26)
  7625.  
  7626.     create table #table_priv2(
  7627.         table_qualifier varchar(32) NULL,
  7628.         table_owner     varchar(32) NULL,
  7629.         table_name        varchar(32) NOT NULL,
  7630.         grantor         varchar(32) NULL,
  7631.         grantee         varchar(32) NOT NULL,
  7632.         privilege        varchar(32) NOT NULL,
  7633.         is_grantable    varchar(3) NULL)
  7634.  
  7635.     insert into #table_priv2
  7636.         select
  7637.             table_qualifier,
  7638.             table_owner,
  7639.             table_name,
  7640.             grantor,
  7641.             grantee,
  7642.             'SELECT',
  7643.             is_grantable
  7644.         from #table_priv1
  7645.         where select_privilege = 1
  7646.  
  7647.  
  7648.     insert into #table_priv2
  7649.         select
  7650.             table_qualifier,
  7651.             table_owner,
  7652.             table_name,
  7653.             grantor,
  7654.             grantee,
  7655.             'INSERT',
  7656.             is_grantable
  7657.         from #table_priv1
  7658.         where insert_privilege = 1
  7659.  
  7660.  
  7661.     insert into #table_priv2
  7662.         select
  7663.             table_qualifier,
  7664.             table_owner,
  7665.             table_name,
  7666.             grantor,
  7667.             grantee,
  7668.             'DELETE',
  7669.             is_grantable
  7670.         from #table_priv1
  7671.         where delete_privilege = 1
  7672.  
  7673.  
  7674.     insert into #table_priv2
  7675.         select
  7676.             table_qualifier,
  7677.             table_owner,
  7678.             table_name,
  7679.             grantor,
  7680.             grantee,
  7681.             'UPDATE',
  7682.             is_grantable
  7683.         from #table_priv1
  7684.         where update_privilege = 1
  7685.  
  7686.     insert into #table_priv2
  7687.         select
  7688.             table_qualifier,
  7689.             table_owner,
  7690.             table_name,
  7691.             grantor,
  7692.             grantee,
  7693.             'REFERENCES',
  7694.             is_grantable
  7695.         from #table_priv1
  7696.         where references_privilege = 1
  7697.  
  7698.  
  7699.     select * from #table_priv2
  7700.     order by table_owner,table_name,privilege,grantee
  7701. /*    order by 2,3,6,5 Can't use since fails on 4.21a server */
  7702. go
  7703.  
  7704. if (charindex('6.50', @@version) = 0 and
  7705.     charindex('7.00', @@version) = 0 and
  7706.     charindex('8.00', @@version) = 0)
  7707. begin
  7708.     print ''
  7709.     print ''
  7710.     print 'Warning:'
  7711.     print 'you are installing the stored procedures '
  7712.     print 'on a pre 6.50 SQL Server.'
  7713.     print 'Ignore the following errors.'
  7714. end
  7715. else
  7716.     drop proc sp_table_privileges
  7717. go
  7718.  
  7719.  
  7720. /*    Procedure for 6.50 server */
  7721. CREATE PROCEDURE sp_table_privileges (
  7722.             @table_name         varchar(96),
  7723.             @table_owner        varchar(96) = null,
  7724.             @table_qualifier    varchar(32) = null)
  7725. as
  7726.  
  7727.     if @table_qualifier is not null
  7728.     begin
  7729.         if db_name() <> @table_qualifier
  7730.         begin    /* If qualifier doesn't match current database */
  7731.             raiserror (15250, -1,-1)
  7732.             return
  7733.         end
  7734.     end
  7735.     if @table_name is null
  7736.         select @table_name = '%'
  7737.     if @table_owner is null /* If no owner supplied, force wildcard */
  7738.         select @table_owner = '%'
  7739.  
  7740.     select
  7741.         convert(varchar(32),db_name()) TABLE_QUALIFIER,
  7742.         convert(varchar(32),user_name(o.uid)) TABLE_OWNER,
  7743.         convert(varchar(32),object_name(o.id)) TABLE_NAME,
  7744.         convert(varchar(32),user_name(p.grantor)) GRANTOR,
  7745.         convert(varchar(32),user_name(u.uid)) GRANTEE,
  7746.         convert(varchar(32),case p.action
  7747.              when 193 then 'SELECT'
  7748.              when 195 then 'INSERT'
  7749.              when 196 then 'DELETE'
  7750.              when 197 then 'UPDATE'
  7751.              else 'REFERENCES'
  7752.         end) PRIVILEGE,
  7753.         convert(varchar(3),case when p.protecttype = 205 then 'NO'
  7754.             else 'YES'
  7755.         end) IS_GRANTABLE
  7756.     from sysprotects p, sysobjects o, sysusers u
  7757.     where
  7758.         p.id = o.id
  7759.         and o.type in ('U','V','S')
  7760.         and object_name(o.id) like @table_name
  7761.         and user_name(o.uid) like @table_owner
  7762.             /* expand groups */
  7763.         and ((p.uid = u.uid and u.uid <> u.gid) or
  7764.              (p.uid = u.gid and u.uid <> u.gid))
  7765.         and p.protecttype <> 206    /* only grant rows */
  7766.         and p.action in (26,193,195,196,197)
  7767.         and o.uid <> u.uid            /* no rows for owner */
  7768.         and not exists (            /* exclude revoke'd privileges */
  7769.             select *
  7770.             from sysprotects p1
  7771.             where
  7772.                 p1.protecttype = 206
  7773.                 and p1.action = p.action
  7774.                 and p1.id = p.id
  7775.                 and p1.uid = u.uid)
  7776.     union
  7777.     select    /*    Add rows for table owner */
  7778.         convert(varchar(32),db_name()) TABLE_QUALIFIER,
  7779.         convert(varchar(32),user_name(o.uid)) TABLE_OWNER,
  7780.         convert(varchar(32),object_name(o.id)) TABLE_NAME,
  7781.         convert(varchar(32),user_name(u.uid)) GRANTOR,
  7782.         convert(varchar(32),user_name(o.uid)) GRANTEE,
  7783.         convert(varchar(32),case v.number
  7784.             when 193 then 'SELECT'
  7785.             when 195 then 'INSERT'
  7786.             when 196 then 'DELETE'
  7787.             when 197 then 'UPDATE'
  7788.             else 'REFERENCES'
  7789.         end) PRIVILEGE,
  7790.         convert(varchar(3),'YES') IS_GRANTABLE
  7791.     from sysobjects o, master.dbo.spt_values v, sysusers u
  7792.     where
  7793.         object_name(o.id) like @table_name
  7794.         and o.type in ('U','V','S')
  7795.         and user_name(o.uid) like @table_owner
  7796.         and u.uid = 1        /* grantor is dbo of database */
  7797.         and v.type = 'P'    /* cross product to get all exposed privileges */
  7798.         and v.number in (26,193,195,196,197)
  7799.         and not exists (    /* exclude revoke'd privileges */
  7800.             select *
  7801.             from sysprotects p1
  7802.             where
  7803.                 p1.protecttype = 206
  7804.                 and p1.action = v.number
  7805.                 and p1.id = o.id
  7806.                 and p1.uid = o.uid)
  7807.     order by 2,3,6,5
  7808. go
  7809.  
  7810.  
  7811. if (charindex('7.00', @@version) > 0 or
  7812.     charindex('8.00', @@version) > 0)
  7813.     drop proc sp_table_privileges
  7814. else
  7815. begin
  7816.     print ''
  7817.     print ''
  7818.     print 'Warning:'
  7819.     print 'you are installing the stored procedures '
  7820.     print 'on a pre 7.0 SQL Server.'
  7821.     print 'Ignore the following errors.'
  7822. end
  7823. go
  7824.  
  7825. /*    Procedure for 7.0 and later servers */
  7826. CREATE PROCEDURE sp_table_privileges (
  7827.             @table_name         nvarchar(384),
  7828.             @table_owner        nvarchar(384) = null,
  7829.             @table_qualifier    sysname = null)
  7830. as
  7831.  
  7832.     if @table_qualifier is not null
  7833.     begin
  7834.         if db_name() <> @table_qualifier
  7835.         begin    /* If qualifier doesn't match current database */
  7836.             raiserror (15250, -1,-1)
  7837.             return
  7838.         end
  7839.     end
  7840.     if @table_name is null
  7841.         select @table_name = '%'
  7842.     if @table_owner is null /* If no owner supplied, force wildcard */
  7843.         select @table_owner = '%'
  7844.  
  7845.     select
  7846.         convert(sysname,db_name()) TABLE_QUALIFIER,
  7847.         convert(sysname,user_name(o.uid)) TABLE_OWNER,
  7848.         convert(sysname,object_name(o.id)) TABLE_NAME,
  7849.         convert(sysname,user_name(p.grantor)) GRANTOR,
  7850.         convert(sysname,user_name(u.uid)) GRANTEE,
  7851.         convert(varchar(32),case p.action
  7852.              when 193 then 'SELECT'
  7853.              when 195 then 'INSERT'
  7854.              when 196 then 'DELETE'
  7855.              when 197 then 'UPDATE'
  7856.              else 'REFERENCES'
  7857.         end) PRIVILEGE,
  7858.         convert(varchar(3),case when p.protecttype = 205 then 'NO'
  7859.             else 'YES'
  7860.         end) IS_GRANTABLE
  7861.     from sysprotects p, sysobjects o, sysusers u, sysmembers m
  7862.     where
  7863.         p.id = o.id
  7864.         and o.type in ('U','V','S')
  7865.         and object_name(o.id) like @table_name
  7866.         and user_name(o.uid) like @table_owner
  7867.             /* expand groups - AKUNDONE: only 1 level of grp unrolling. */
  7868.         and (u.uid > 0 and u.uid < 16384)
  7869.         and ((p.uid = u.uid) or
  7870.              (p.uid = m.groupuid and u.uid = m.memberuid))
  7871.         and p.protecttype <> 206    /* only grant rows */
  7872.         and p.action in (26,193,195,196,197)
  7873.         and o.uid <> u.uid            /* no rows for owner */
  7874.         and not exists (            /* exclude revoke'd privileges */
  7875.             select *
  7876.             from sysprotects p1
  7877.             where
  7878.                 p1.protecttype = 206
  7879.                 and p1.action = p.action
  7880.                 and p1.id = p.id
  7881.                 and p1.uid = u.uid)
  7882.     union
  7883.     select    /*    Add rows for table owner */
  7884.         convert(sysname,db_name()) TABLE_QUALIFIER,
  7885.         convert(sysname,user_name(o.uid)) TABLE_OWNER,
  7886.         convert(sysname,object_name(o.id)) TABLE_NAME,
  7887.         convert(sysname,user_name(u.uid)) GRANTOR,
  7888.         convert(sysname,user_name(o.uid)) GRANTEE,
  7889.         convert(varchar(32),case v.number
  7890.             when 193 then 'SELECT'
  7891.             when 195 then 'INSERT'
  7892.             when 196 then 'DELETE'
  7893.             when 197 then 'UPDATE'
  7894.             else 'REFERENCES'
  7895.         end) PRIVILEGE,
  7896.         convert(varchar(3),'YES') IS_GRANTABLE
  7897.     from sysobjects o, master.dbo.spt_values v, sysusers u
  7898.     where
  7899.         object_name(o.id) like @table_name
  7900.         and o.type in ('U','V','S')
  7901.         and user_name(o.uid) like @table_owner
  7902.         and u.uid = 1        /* grantor is 'dbo' of database */
  7903.         and v.type = N'P'    /* cross product to get all exposed privileges */
  7904.         and v.number in (26,193,195,196,197)
  7905.         and not exists (    /* exclude revoke'd privileges */
  7906.             select *
  7907.             from sysprotects p1
  7908.             where
  7909.                 p1.protecttype = 206
  7910.                 and p1.action = v.number
  7911.                 and p1.id = o.id
  7912.                 and p1.uid = o.uid)
  7913.     order by 2,3,6,5
  7914. go
  7915.  
  7916. grant execute on sp_table_privileges to public
  7917. go
  7918.  
  7919. dump tran master with no_log
  7920. go
  7921.  
  7922. print 'creating sp_tables'
  7923. go
  7924.  
  7925. /*    Procedure for 6.50 and earlier servers */
  7926. create procedure sp_tables(
  7927.                @table_name        varchar(96)    = null,
  7928.                @table_owner     varchar(96)    = null,
  7929.                @table_qualifier varchar(32)    = null,
  7930.                @table_type        varchar(100) = null)
  7931. as
  7932.     declare @type1 varchar(3)
  7933.     declare @tableindex int
  7934.  
  7935.  
  7936.     /* Special feature #1:    enumerate databases when owner and name
  7937.          are blank but qualifier is explicitly '%'.  */
  7938.     if @table_qualifier = '%' and
  7939.         @table_owner = '' and
  7940.         @table_name = ''
  7941.     begin    /* If enumerating databases */
  7942.         select
  7943.             TABLE_QUALIFIER = convert(varchar(32),d.name),
  7944.             TABLE_OWNER = convert(varchar(32),null),
  7945.             TABLE_NAME = convert(varchar(32),null),
  7946.             TABLE_TYPE = convert(varchar(32),null),
  7947.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  7948.         from master.dbo.sysdatabases d
  7949.         where d.name <> 'model'    /* eliminate MODEL database */
  7950.         order by 1
  7951.     end
  7952.  
  7953.     /* Special feature #2:    enumerate owners when qualifier and name
  7954.          are blank but owner is explicitly '%'.  */
  7955.     else if @table_qualifier = '' and
  7956.         @table_owner = '%' and
  7957.         @table_name = ''
  7958.     begin    /* If enumerating owners */
  7959.         select distinct
  7960.             TABLE_QUALIFIER = convert(varchar(32),null),
  7961.             TABLE_OWNER = convert(varchar(32),user_name(uid)),
  7962.             TABLE_NAME = convert(varchar(32),null),
  7963.             TABLE_TYPE = convert(varchar(32),null),
  7964.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  7965.         from sysobjects
  7966.         order by 2
  7967.     end
  7968.  
  7969.     /* Special feature #3:    enumerate table types when qualifier, owner and
  7970.          name are blank but table type is explicitly '%'.    */
  7971.     else if @table_qualifier = '' and
  7972.         @table_owner = '' and
  7973.         @table_name = '' and
  7974.         @table_type = '%'
  7975.     begin    /* If enumerating table types */
  7976.         select
  7977.             TABLE_QUALIFIER = convert(varchar(32),null),
  7978.             TABLE_OWNER = convert(varchar(32),null),
  7979.             TABLE_NAME = convert(varchar(32),null),
  7980.             TABLE_TYPE = convert(varchar(32),rtrim(substring('SYSTEM TABLETABLE       VIEW',(colid-1)*12+1,12))),
  7981.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  7982.         from sysobjects o, syscolumns c
  7983.         where o.id=c.id and o.name='sysusers' and colid<=3
  7984.     end
  7985.  
  7986.     else
  7987.     begin /* end of special features - do normal processing */
  7988.         if @table_qualifier is not null
  7989.         begin
  7990.             if db_name() <> @table_qualifier
  7991.             begin
  7992.                 if @table_qualifier = ''
  7993.                 begin  /* If empty qualifier supplied */
  7994.                     /* Force an empty result set */
  7995.                     select @table_name = ''
  7996.                     select @table_owner = ''
  7997.                 end
  7998.                 else
  7999.                 begin    /* If qualifier doesn't match current database */
  8000.                     raiserror 20001 '~~Rush_5~~'
  8001.                     return
  8002.                 end
  8003.             end
  8004.         end
  8005.         if @table_type is null
  8006.         begin    /* Select all ODBC supported table types */
  8007.             select @type1 = 'SUV'
  8008.         end
  8009.         else
  8010.         begin
  8011.             /*    TableType is case sensitive if CS server */
  8012.             select @type1 = null
  8013.             if (charindex('''SYSTEM TABLE''',@table_type) <> 0)
  8014.                 select @type1 = @type1 + 'S'    /* Add System Tables */
  8015.             if (charindex('''TABLE''',@table_type) <> 0)
  8016.                 select @type1 = @type1 + 'U'    /* Add User Tables */
  8017.             if (charindex('''VIEW''',@table_type) <> 0)
  8018.                 select @type1 = @type1 + 'V'    /* Add Views */
  8019.         end
  8020.         if @table_name is null
  8021.         begin    /*    If table name not supplied, match all */
  8022.             select @table_name = '%'
  8023.         end
  8024.         else
  8025.         begin
  8026.             if (@table_owner is null) and (charindex('%', @table_name) = 0)
  8027.             begin    /* If owner not specified and table is specified */
  8028.                 if exists (select * from sysobjects
  8029.                     where uid = user_id()
  8030.                     and name = @table_name
  8031.                     and (type = 'U' or type = 'V' or type = 'S'))
  8032.                 begin    /* Override supplied owner w/owner of table */
  8033.                     select @table_owner = user_name()
  8034.                 end
  8035.             end
  8036.         end
  8037.         if @table_owner is null /* If no owner supplied, force wildcard */
  8038.             select @table_owner = '%'
  8039.         select
  8040.             TABLE_QUALIFIER = convert(varchar(32),db_name()),
  8041.             TABLE_OWNER = convert(varchar(32),user_name(o.uid)),
  8042.             TABLE_NAME = convert(varchar(32),o.name),    /* make nullable */
  8043.             TABLE_TYPE = convert(varchar(32),rtrim(
  8044.                 substring('SYSTEM TABLE            TABLE       VIEW       ',
  8045.                     (ascii(o.type)-83)*12+1,12))),    /* 'S'=0,'U'=2,'V'=3 */
  8046.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  8047.         from sysusers u, sysobjects o
  8048.         where
  8049.             o.name like @table_name
  8050.             and user_name(o.uid) like @table_owner
  8051.             and charindex(substring(o.type,1,1),@type1) <> 0 /* Only desired types */
  8052.             and u.uid = user_id() /* constrain sysusers uid for use in subquery */
  8053.             and (
  8054.                 suser_id() = 1     /* User is the System Administrator */
  8055.                 or o.uid = user_id()     /* User created the object */
  8056.                 /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  8057.                 or ((select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  8058.                     from sysprotects p
  8059.                     /* outer join to correlate with all rows in sysobjects */
  8060.                     where p.id =* o.id
  8061.                         /* get rows for public,current user,user's group */
  8062.                         and (p.uid = 0 or p.uid = user_id() or p.uid =* u.gid)
  8063.                         /* check for SELECT,EXECUTE privilege */
  8064.                         and (action in (193,224)))&1     /* more magic...normalize GRANT */
  8065.                     ) = 1    /* final magic...compare Grants      */
  8066.             )
  8067.         order by 4, 1, 2, 3
  8068.     end
  8069. go
  8070.  
  8071. if (charindex('7.00', @@version) > 0 or
  8072.     charindex('8.00', @@version) > 0)
  8073.     drop procedure sp_tables
  8074. else
  8075. begin
  8076.     print ''
  8077.     print ''
  8078.     print 'Warning:'
  8079.     print 'you are installing the stored procedures '
  8080.     print 'on a pre 8.0 SQL Server.'
  8081.     print 'Ignore the following errors.'
  8082. end
  8083. go
  8084.  
  8085. /*    Procedure for 8.0 server */
  8086. create procedure sp_tables(
  8087.                @table_name        nvarchar(384)    = null,
  8088.                @table_owner     nvarchar(384)    = null,
  8089.                @table_qualifier sysname    = null,
  8090.                @table_type        varchar(100) = null)
  8091. as
  8092.     declare @type1 varchar(3)
  8093.     declare @tableindex int
  8094.  
  8095.  
  8096.     /* Special feature #1:    enumerate databases when owner and name
  8097.          are blank but qualifier is explicitly '%'.  */
  8098.     if @table_qualifier = '%' and
  8099.         @table_owner = '' and
  8100.         @table_name = ''
  8101.     begin    /* If enumerating databases */
  8102.         select
  8103.             TABLE_QUALIFIER = convert(sysname,d.name),
  8104.             TABLE_OWNER = convert(sysname,null),
  8105.             TABLE_NAME = convert(sysname,null),
  8106.             TABLE_TYPE = convert(varchar(32),null),
  8107.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  8108.         from master.dbo.sysdatabases d
  8109.         where d.name <> 'model'    /* eliminate MODEL database */
  8110.         order by 1
  8111.     end
  8112.  
  8113.     /* Special feature #2:    enumerate owners when qualifier and name
  8114.          are blank but owner is explicitly '%'.  */
  8115.     else if @table_qualifier = '' and
  8116.         @table_owner = '%' and
  8117.         @table_name = ''
  8118.     begin    /* If enumerating owners */
  8119.         select distinct
  8120.             TABLE_QUALIFIER = convert(sysname,null),
  8121.             TABLE_OWNER = convert(sysname,user_name(uid)),
  8122.             TABLE_NAME = convert(sysname,null),
  8123.             TABLE_TYPE = convert(varchar(32),null),
  8124.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  8125.         from sysobjects
  8126.         order by 2
  8127.     end
  8128.  
  8129.     /* Special feature #3:    enumerate table types when qualifier, owner and
  8130.          name are blank but table type is explicitly '%'.    */
  8131.     else if @table_qualifier = '' and
  8132.         @table_owner = '' and
  8133.         @table_name = '' and
  8134.         @table_type = '%'
  8135.     begin    /* If enumerating table types */
  8136.         select
  8137.             TABLE_QUALIFIER = convert(sysname,null),
  8138.             TABLE_OWNER = convert(sysname,null),
  8139.             TABLE_NAME = convert(sysname,null),
  8140.             TABLE_TYPE = convert(varchar(32),rtrim(substring('SYSTEM TABLETABLE       VIEW',(colid-1)*12+1,12))),
  8141.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  8142.         from sysobjects o, syscolumns c
  8143.         where o.id=c.id and o.name='sysusers' and colid<=3
  8144.     end
  8145.  
  8146.     else
  8147.     begin /* end of special features - do normal processing */
  8148.         if @table_qualifier is not null
  8149.         begin
  8150.             if db_name() <> @table_qualifier
  8151.             begin
  8152.                 if @table_qualifier = ''
  8153.                 begin  /* If empty qualifier supplied */
  8154.                     /* Force an empty result set */
  8155.                     select @table_name = ''
  8156.                     select @table_owner = ''
  8157.                 end
  8158.                 else
  8159.                 begin    /* If qualifier doesn't match current database */
  8160.                     raiserror (15250, -1,-1)
  8161.                     return
  8162.                 end
  8163.             end
  8164.         end
  8165.         if @table_type is null
  8166.         begin    /* Select all ODBC supported table types */
  8167.             select @type1 = 'SUV'
  8168.         end
  8169.         else
  8170.         begin
  8171.             /*    TableType is case sensitive if CS server */
  8172.             select @type1 = ''
  8173.             if (charindex('''SYSTEM TABLE''',@table_type) <> 0)
  8174.                 select @type1 = @type1 + 'S'    /* Add System Tables */
  8175.             if (charindex('''TABLE''',@table_type) <> 0)
  8176.                 select @type1 = @type1 + 'U'    /* Add User Tables */
  8177.             if (charindex('''VIEW''',@table_type) <> 0)
  8178.                 select @type1 = @type1 + 'V'    /* Add Views */
  8179.         end
  8180.         if @table_name is null
  8181.         begin    /*    If table name not supplied, match all */
  8182.             select @table_name = '%'
  8183.         end
  8184.         else
  8185.         begin
  8186.             if (@table_owner is null) and (charindex('%', @table_name) = 0)
  8187.             begin    /* If owner not specified and table is specified */
  8188.                 if exists (select * from sysobjects
  8189.                     where uid = user_id()
  8190.                     and name = @table_name
  8191.                     and (type = 'U' or type = 'V' or type = 'S'))
  8192.                 begin    /* Override supplied owner w/owner of table */
  8193.                     select @table_owner = user_name()
  8194.                 end
  8195.             end
  8196.         end
  8197.         if @table_owner is null /* If no owner supplied, force wildcard */
  8198.             select @table_owner = '%'
  8199.         select
  8200.             TABLE_QUALIFIER = convert(sysname,db_name()),
  8201.             TABLE_OWNER = convert(sysname,user_name(o.uid)),
  8202.             TABLE_NAME = convert(sysname,o.name),    /* make nullable */
  8203.             TABLE_TYPE = convert(varchar(32),rtrim(
  8204.                 substring('SYSTEM TABLE            TABLE       VIEW       ',
  8205.                     (ascii(o.type)-83)*12+1,12))),    /* 'S'=0,'U'=2,'V'=3 */
  8206.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  8207.         from sysobjects o
  8208.         where
  8209.             o.name like @table_name
  8210.             and user_name(o.uid) like @table_owner
  8211.             and o.type in ('U','V','S')
  8212.             and charindex(substring(o.type,1,1),@type1) <> 0 /* Only desired types */
  8213.             and permissions (o.id)&4096 <> 0
  8214.         order by 4, 1, 2, 3
  8215.     end
  8216. go
  8217.  
  8218.  
  8219. grant execute on sp_tables to public
  8220. go
  8221.  
  8222. dump tran master with no_log
  8223. go
  8224.  
  8225. /*-----------------------------------------------------------------------------*/
  8226. /*-------------- CATALOG STORED PROCEDURES FOR OLEDB SOURCES ------------------*/
  8227. /*-----------------------------------------------------------------------------*/
  8228.  
  8229. if object_id('sp_linkedservers', 'P') is not null
  8230.     drop proc sp_linkedservers
  8231. go
  8232. raiserror(15339,-1,-1,'sp_linkedservers')
  8233. go
  8234. create proc sp_linkedservers as
  8235.     select
  8236.         SRV_NAME = srvname,
  8237.         SRV_PROVIDERNAME = providername,
  8238.         SRV_PRODUCT = srvproduct,
  8239.         SRV_DATASOURCE = datasource,
  8240.         SRV_PROVIDERSTRING = providerstring,
  8241.         SRV_LOCATION = location,
  8242.         SRV_CAT = catalog
  8243.     from master.dbo.sysservers
  8244.     order by 1
  8245. go
  8246. grant execute on sp_linkedservers to public
  8247. go
  8248.  
  8249.  
  8250. if object_id('sp_catalogs', 'P') is not null
  8251.     drop proc sp_catalogs
  8252. go
  8253. raiserror(15339,-1,-1,'sp_catalogs')
  8254. go
  8255. create procedure sp_catalogs(
  8256.     @server_name        sysname)
  8257. as
  8258.     select
  8259.         CATALOG_NAME,
  8260.         convert (nvarchar(255),DESCRIPTION)
  8261.     from master.dbo.SYSREMOTE_CATALOGS < @server_name >
  8262.     order by CATALOG_NAME
  8263. go
  8264. grant execute on sp_catalogs to public
  8265. go
  8266.  
  8267.  
  8268. if object_id('sp_tables_ex', 'P') is not null
  8269.     drop proc sp_tables_ex
  8270. go
  8271. raiserror(15339,-1,-1,'sp_tables_ex')
  8272. go
  8273. create procedure sp_tables_ex(
  8274.     @table_server        sysname,
  8275.     @table_name            sysname = null,
  8276.     @table_schema        sysname = null,
  8277.     @table_catalog        sysname = null,
  8278.     @table_type            sysname = null)
  8279. as
  8280.     declare @table_catalog_param        sysname
  8281.     if ((isnull(charindex('%', @table_catalog),0) = 0) and
  8282.         (isnull(charindex('[', @table_catalog),0) = 0) and
  8283.         (isnull(charindex('_', @table_catalog),0) = 0))
  8284.         select @table_catalog_param = @table_catalog
  8285.     else
  8286.         select @table_catalog_param = null
  8287.  
  8288.     if ((isnull(charindex('%', @table_name),0) = 0) and
  8289.         (isnull(charindex('[', @table_name),0) = 0) and
  8290.         (isnull(charindex('_', @table_name),0) = 0))
  8291.     begin    /*    If no wild carding */
  8292.         select
  8293.             TABLE_CAT = TABLE_CATALOG,
  8294.             TABLE_SCHEM = TABLE_SCHEMA,
  8295.             TABLE_NAME = TABLE_NAME,
  8296.             TABLE_TYPE = TABLE_TYPE,
  8297.             REMARKS = convert(nvarchar(255),DESCRIPTION)
  8298.         from master.dbo.SYSREMOTE_TABLES <
  8299.                     @table_server,
  8300.                     @table_catalog_param,
  8301.                     NULL,
  8302.                     @table_name,
  8303.                     NULL >
  8304.         where (TABLE_SCHEMA like @table_schema
  8305.             or @table_schema is NULL or (TABLE_SCHEMA is NULL and @table_schema = N'%'))
  8306.             and (TABLE_CATALOG like @table_catalog
  8307.             or     @table_catalog is NULL or (TABLE_CATALOG is NULL and @table_catalog = N'%'))
  8308.             and (charindex ('''' + TABLE_TYPE + '''', @table_type) <> 0
  8309.             or     @table_type is NULL)
  8310.         order by TABLE_TYPE, TABLE_CAT, TABLE_SCHEM, TABLE_NAME
  8311.     end
  8312.     else
  8313.     begin    /*    If wild carding */
  8314.         select
  8315.             TABLE_CAT = TABLE_CATALOG,
  8316.             TABLE_SCHEM = TABLE_SCHEMA,
  8317.             TABLE_NAME = TABLE_NAME,
  8318.             TABLE_TYPE = TABLE_TYPE,
  8319.             REMARKS = convert(nvarchar(255),DESCRIPTION)
  8320.         from master.dbo.SYSREMOTE_TABLES <
  8321.                     @table_server,
  8322.                     @table_catalog_param,
  8323.                     NULL,
  8324.                     NULL,
  8325.                     NULL >
  8326.         where (TABLE_SCHEMA like @table_schema
  8327.             or @table_schema is NULL or (TABLE_SCHEMA is NULL and @table_schema = N'%'))
  8328.             and (TABLE_CATALOG like @table_catalog
  8329.             or     @table_catalog is NULL or (TABLE_CATALOG is NULL and @table_catalog = N'%'))
  8330.             and (charindex ('''' + TABLE_TYPE + '''', @table_type) <> 0
  8331.             or     @table_type is NULL)
  8332.             and TABLE_NAME like @table_name
  8333.         order by TABLE_TYPE, TABLE_CAT, TABLE_SCHEM, TABLE_NAME
  8334.     end
  8335. go
  8336. grant execute on sp_tables_ex to public
  8337. go
  8338.  
  8339.  
  8340. if object_id('sp_columns_ex', 'P') is not null
  8341.     drop proc sp_columns_ex
  8342. go
  8343.  
  8344. /* 7.0 version */
  8345. raiserror(15339,-1,-1,'sp_columns_ex')
  8346. go
  8347.  
  8348. create procedure sp_columns_ex(
  8349.     @table_server        sysname,
  8350.     @table_name            sysname = null,
  8351.     @table_schema        sysname = null,
  8352.     @table_catalog        sysname = null,
  8353.     @column_name        sysname = null,
  8354.     @ODBCVer            int = 2)
  8355. as
  8356.  
  8357.     set nocount on
  8358.     declare
  8359.     @DBCOLUMNFLAGS_ISFIXEDLENGTH binary(1),    @DBCOLUMNFLAGS_ISLONG    binary(1),
  8360.     @DBCOLUMNFLAGS_ISROWVER    binary(2)
  8361.  
  8362.     select
  8363.     @DBCOLUMNFLAGS_ISFIXEDLENGTH    = 0x10,    @DBCOLUMNFLAGS_ISLONG    = 0x80,
  8364.     @DBCOLUMNFLAGS_ISROWVER    = 0x0200
  8365.  
  8366.     declare    
  8367.     @DBTYPE_I2 smallint,            @DBTYPE_I4 smallint,
  8368.     @DBTYPE_R4 smallint,            @DBTYPE_R8 smallint,
  8369.     @DBTYPE_CY smallint,            @DBTYPE_DATE smallint,
  8370.     @DBTYPE_BSTR smallint,            @DBTYPE_BOOL smallint,
  8371.     @DBTYPE_VARNUMERIC smallint,    @DBTYPE_DECIMAL smallint,
  8372.     @DBTYPE_UI1 smallint,            @DBTYPE_BYREF smallint,
  8373.     @DBTYPE_I1 smallint,            @DBTYPE_UI2 smallint,
  8374.     @DBTYPE_UI4 smallint,            @DBTYPE_I8 smallint,
  8375.     @DBTYPE_UI8 smallint,            @DBTYPE_GUID smallint,
  8376.     @DBTYPE_BYTES smallint,            @DBTYPE_STR smallint,
  8377.     @DBTYPE_WSTR smallint,            @DBTYPE_NUMERIC smallint,
  8378.     @DBTYPE_DBDATE smallint,        @DBTYPE_DBTIME smallint,
  8379.     @DBTYPE_DBTIMESTAMP smallint
  8380.  
  8381.     select
  8382.     @DBTYPE_I2            = 2,    @DBTYPE_I4            = 3,
  8383.     @DBTYPE_R4            = 4,    @DBTYPE_R8            = 5,
  8384.     @DBTYPE_CY            = 6,    @DBTYPE_DATE        = 7,
  8385.     @DBTYPE_BSTR        = 8,    @DBTYPE_BOOL        = 11,
  8386.     @DBTYPE_VARNUMERIC    = 139,    @DBTYPE_DECIMAL        = 14,
  8387.     @DBTYPE_UI1            = 17,    @DBTYPE_BYREF        = 16384,
  8388.     @DBTYPE_I1            = 16,    @DBTYPE_UI2            = 18,
  8389.     @DBTYPE_UI4            = 19,    @DBTYPE_I8            = 20,
  8390.     @DBTYPE_UI8            = 21,    @DBTYPE_GUID        = 72,
  8391.     @DBTYPE_BYTES        = 128,    @DBTYPE_STR            = 129,
  8392.     @DBTYPE_WSTR        = 130,    @DBTYPE_NUMERIC        = 131,
  8393.     @DBTYPE_DBDATE        = 133,    @DBTYPE_DBTIME        = 134,    
  8394.     @DBTYPE_DBTIMESTAMP    = 135
  8395.  
  8396.     declare    
  8397.     @ODS_SMALLINT_NOT_NULL tinyint,    @ODS_INT_NULL tinyint,
  8398.     @ODS_INT_NOT_NULL tinyint,        @ODS_TINYINT_NOT_NULL tinyint,
  8399.     @ODS_REAL_NOT_NULL tinyint,        @ODS_FLOAT_NULL tinyint,
  8400.     @ODS_FLOAT_NOT_NULL tinyint,    @ODS_MONEY_NULL tinyint,
  8401.     @ODS_MONEY_NOT_NULL tinyint,    @ODS_DATETIME_NULL tinyint,
  8402.     @ODS_DATETIME_NOT_NULL tinyint,    @ODS_TEXT tinyint,
  8403.     @ODS_BIT tinyint,                @ODS_DECIMAL tinyint,
  8404.     @ODS_NUMERIC tinyint,            @ODS_GUID tinyint,
  8405.     @ODS_IMAGE tinyint,                @ODS_TIMESTAMP tinyint,
  8406.     @ODS_BINARY_NULL tinyint,        @ODS_BINARY_NOT_NULL tinyint,
  8407.     @ODS_VARBINARY tinyint,            @ODS_CHAR_NULL tinyint,
  8408.     @ODS_CHAR_NOT_NULL tinyint,        @ODS_VARCHAR tinyint
  8409.  
  8410.     select     
  8411.     @ODS_IMAGE = 34,            @ODS_TEXT = 35,
  8412.     @ODS_GUID = 36,                @ODS_BINARY_NULL = 37,            
  8413.     @ODS_VARBINARY = 37,        @ODS_INT_NULL = 38,
  8414.     @ODS_CHAR_NULL = 39,        @ODS_VARCHAR = 39,
  8415.     @ODS_TIMESTAMP = 45,        @ODS_BINARY_NOT_NULL = 45,
  8416.     @ODS_CHAR_NOT_NULL = 47,    @ODS_TINYINT_NOT_NULL = 48,
  8417.     @ODS_BIT = 50,                @ODS_SMALLINT_NOT_NULL = 52,    
  8418.     @ODS_INT_NOT_NULL = 56,        @ODS_REAL_NOT_NULL = 59,        
  8419.     @ODS_MONEY_NOT_NULL = 60,    @ODS_DATETIME_NOT_NULL = 61,    
  8420.     @ODS_FLOAT_NOT_NULL = 62,    @ODS_DECIMAL = 106,
  8421.     @ODS_NUMERIC = 108,            @ODS_FLOAT_NULL = 109,
  8422.     @ODS_MONEY_NULL = 110,        @ODS_DATETIME_NULL = 111
  8423.  
  8424.     declare 
  8425.     @ODBC_GUID smallint,        @ODBC_NTEXT smallint,
  8426.     @ODBC_NVARCHAR smallint,    @ODBC_NCHAR smallint,
  8427.     @ODBC_BIT smallint,            @ODBC_TINYINT smallint,
  8428.     @ODBC_IMAGE smallint,        @ODBC_VARBINARY smallint,
  8429.     @ODBC_TIMESTAMP smallint,    @ODBC_BINARY smallint,
  8430.     @ODBC_TEXT smallint,        @ODBC_CHAR smallint,
  8431.     @ODBC_NUMERIC smallint,        @ODBC_DECIMAL smallint,
  8432.     @ODBC_MONEY smallint,        @ODBC_INT smallint,        
  8433.     @ODBC_SMALLINT smallint,    @ODBC_FLOAT smallint,    
  8434.     @ODBC_REAL smallint,        @ODBC_VARCHAR smallint,
  8435.     @ODBC_DATETIME smallint
  8436.  
  8437.     select
  8438.     @ODBC_GUID        = -11,    @ODBC_NTEXT        = -10,
  8439.     @ODBC_NVARCHAR    = -9,    @ODBC_NCHAR        = -8,
  8440.     @ODBC_BIT        = -7,    @ODBC_TINYINT    = -6,
  8441.     @ODBC_IMAGE        = -4,    @ODBC_VARBINARY    = -3,
  8442.     @ODBC_TIMESTAMP    = -2,    @ODBC_BINARY    = -2,
  8443.     @ODBC_TEXT        = -1,    @ODBC_CHAR        = 1,
  8444.     @ODBC_NUMERIC    = 2,    @ODBC_DECIMAL    = 3,
  8445.     @ODBC_MONEY        = 3,    @ODBC_INT        = 4,        
  8446.     @ODBC_SMALLINT    = 5,    @ODBC_FLOAT        = 6,    
  8447.     @ODBC_REAL        = 7,    @ODBC_VARCHAR    = 12,
  8448.     @ODBC_DATETIME    =    
  8449.     case @ODBCVer
  8450.         when 2 then 11
  8451.         else 93
  8452.     end
  8453.  
  8454.     create table #tmp_columns
  8455.     (    TABLE_CAT sysname  NULL,
  8456.         TABLE_SCHEM sysname  NULL,
  8457.         TABLE_NAME sysname NOT NULL,
  8458.         COLUMN_NAME sysname  NULL,
  8459.         DATA_TYPE smallint NOT NULL,
  8460.         TYPE_NAME sysname   NULL,
  8461.         COLUMN_SIZE int NULL,
  8462.         BUFFER_LENGTH int NULL,
  8463.         DECIMAL_DIGITS smallint NULL,
  8464.         NUM_PREC_RADIX smallint NULL,
  8465.         NULLABLE smallint NOT NULL,
  8466.         REMARKS nvarchar(255)  NULL,
  8467.             COLUMN_DEF nvarchar(128)  NULL,
  8468.             SQL_DATA_TYPE smallint null,
  8469.             SQL_DATETIME_SUB smallint NULL,
  8470.         CHAR_OCTET_LENGTH int NULL,
  8471.         ORDINAL_POSITION smallint,
  8472.         IS_NULLABLE varchar(254)  NOT NULL,
  8473.         SS_DATA_TYPE tinyint null,
  8474.         COLUMN_FLAGS int NOT NULL)
  8475.  
  8476.     if ((isnull(charindex('%', @table_name),0) = 0) and
  8477.         (isnull(charindex('[', @table_name),0) = 0) and
  8478.         (isnull(charindex('_', @table_name),0) = 0))
  8479.     begin    /*    If no wild carding */
  8480.         insert into #tmp_columns
  8481.         select
  8482.             TABLE_CAT = c.TABLE_CATALOG,
  8483.             TABLE_SCHEM = c.TABLE_SCHEMA,
  8484.             TABLE_NAME = c.TABLE_NAME,
  8485.             COLUMN_NAME = c.COLUMN_NAME,
  8486.             DATA_TYPE =
  8487.             case c.DATA_TYPE & ~@DBTYPE_BYREF
  8488.                 when @DBTYPE_I2                then @ODBC_SMALLINT
  8489.                 when @DBTYPE_I4                then @ODBC_INT
  8490.                 when @DBTYPE_R4                then @ODBC_REAL
  8491.                 when @DBTYPE_R8                then @ODBC_FLOAT
  8492.                 when @DBTYPE_CY                then @ODBC_MONEY
  8493.                 when @DBTYPE_DATE            then @ODBC_DATETIME
  8494.                 when @DBTYPE_DBDATE            then @ODBC_DATETIME
  8495.                 when @DBTYPE_DBTIME            then @ODBC_DATETIME
  8496.                 when @DBTYPE_DBTIMESTAMP    then @ODBC_DATETIME
  8497.                 when @DBTYPE_BOOL            then @ODBC_BIT
  8498.                 when @DBTYPE_DECIMAL        then @ODBC_DECIMAL
  8499.                 when @DBTYPE_UI1            then @ODBC_TINYINT
  8500.                 when @DBTYPE_I1                then @ODBC_NUMERIC
  8501.                 when @DBTYPE_UI2            then @ODBC_NUMERIC
  8502.                 when @DBTYPE_UI4            then @ODBC_NUMERIC
  8503.                 when @DBTYPE_I8                then @ODBC_NUMERIC
  8504.                 when @DBTYPE_UI8            then @ODBC_NUMERIC
  8505.                 when @DBTYPE_NUMERIC        then @ODBC_NUMERIC
  8506.                 when @DBTYPE_VARNUMERIC        then @ODBC_NUMERIC
  8507.                 when @DBTYPE_GUID            then @ODBC_GUID
  8508.                 when @DBTYPE_BYTES then
  8509.                     case
  8510.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG ) or (p.COLUMN_SIZE > 8000) then @ODBC_IMAGE
  8511.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISROWVER) = @DBCOLUMNFLAGS_ISROWVER AND
  8512.                                 (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH) = @DBCOLUMNFLAGS_ISFIXEDLENGTH and
  8513.                                  p.COLUMN_SIZE = 8
  8514.                             then @ODBC_TIMESTAMP
  8515.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH)
  8516.                             then @ODBC_BINARY
  8517.                         else @ODBC_VARBINARY
  8518.                     end
  8519.                 when @DBTYPE_STR then
  8520.                     case
  8521.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG ) or (p.COLUMN_SIZE > 8000) then @ODBC_TEXT
  8522.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_CHAR
  8523.                         else @ODBC_VARCHAR
  8524.                     end
  8525.                 when @DBTYPE_WSTR then
  8526.                     case
  8527.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG) or (p.COLUMN_SIZE > 4000) then @ODBC_NTEXT
  8528.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_NCHAR
  8529.                         else @ODBC_NVARCHAR
  8530.                     end
  8531.                 when @DBTYPE_BSTR then
  8532.                     case
  8533.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG) or (p.COLUMN_SIZE > 4000) then @ODBC_NTEXT
  8534.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_NCHAR
  8535.                         else @ODBC_NVARCHAR
  8536.                     end
  8537.                 else @ODBC_NVARCHAR     /*    Unknown OleDB datatype */
  8538.             end,
  8539.             TYPE_NAME = p.TYPE_NAME,
  8540.             COLUMN_SIZE =
  8541.                 COALESCE(c.CHARACTER_MAXIMUM_LENGTH, c.NUMERIC_PRECISION, p.COLUMN_SIZE),
  8542.             BUFFER_LENGTH = c.CHARACTER_OCTET_LENGTH,
  8543.             DECIMAL_DIGITS = c.NUMERIC_SCALE,
  8544.             NUM_PREC_RADIX = NULL,
  8545.             NULLABLE = convert(smallint, c.IS_NULLABLE),
  8546.             REMARKS = convert(nvarchar(255),c.DESCRIPTION),
  8547.             COLUMN_DEF = c.COLUMN_DEFAULT,
  8548.             SQL_DATA_TYPE = NULL,
  8549.             SQL_DATETIME_SUB = convert(smallint, c.DATETIME_PRECISION),
  8550.             CHAR_OCTET_LENGTH = c.CHARACTER_OCTET_LENGTH,
  8551.             ORDINAL_POSITION = c.ORDINAL_POSITION,
  8552.             IS_NULLABLE =
  8553.             case c.IS_NULLABLE
  8554.                 when 1 then 'YES'
  8555.                 else 'NO'
  8556.             end,
  8557.             SS_DATA_TYPE = NULL,
  8558.             COLUMN_FLAGS = c.COLUMN_FLAGS
  8559.         from master.dbo.SYSREMOTE_COLUMNS <
  8560.                     @table_server,
  8561.                     @table_catalog,
  8562.                     NULL,
  8563.                     @table_name,
  8564.                     NULL > c,
  8565.                 master.dbo.SYSREMOTE_PROVIDER_TYPES <
  8566.                     @table_server > p
  8567.                     /* LUXOR/KAGERA don't support restricting DATA_TYPE */
  8568.             where c.DATA_TYPE = p.DATA_TYPE AND p.BEST_MATCH = 1 and
  8569.                 (TABLE_SCHEMA like @table_schema
  8570.                 or @table_schema is NULL or (TABLE_SCHEMA is NULL and @table_schema = N'%'))
  8571.                 and (c.COLUMN_NAME like @column_name
  8572.                 or     @column_name is NULL)
  8573.     end
  8574.     else
  8575.     begin    /*    If wild carding */
  8576.         insert into #tmp_columns
  8577.         select
  8578.             TABLE_CAT = c.TABLE_CATALOG,
  8579.             TABLE_SCHEM = c.TABLE_SCHEMA,
  8580.             TABLE_NAME = c.TABLE_NAME,
  8581.             COLUMN_NAME = c.COLUMN_NAME,
  8582.             DATA_TYPE =
  8583.             case c.DATA_TYPE & ~@DBTYPE_BYREF
  8584.                 when @DBTYPE_I2                then @ODBC_SMALLINT
  8585.                 when @DBTYPE_I4                then @ODBC_INT
  8586.                 when @DBTYPE_R4                then @ODBC_REAL
  8587.                 when @DBTYPE_R8                then @ODBC_FLOAT
  8588.                 when @DBTYPE_CY                then @ODBC_MONEY
  8589.                 when @DBTYPE_DATE            then @ODBC_DATETIME
  8590.                 when @DBTYPE_DBDATE            then @ODBC_DATETIME
  8591.                 when @DBTYPE_DBTIME            then @ODBC_DATETIME
  8592.                 when @DBTYPE_DBTIMESTAMP    then @ODBC_DATETIME
  8593.                 when @DBTYPE_BOOL            then @ODBC_BIT
  8594.                 when @DBTYPE_DECIMAL        then @ODBC_DECIMAL
  8595.                 when @DBTYPE_UI1            then @ODBC_TINYINT
  8596.                 when @DBTYPE_I1                then @ODBC_NUMERIC
  8597.                 when @DBTYPE_UI2            then @ODBC_NUMERIC
  8598.                 when @DBTYPE_UI4            then @ODBC_NUMERIC
  8599.                 when @DBTYPE_I8                then @ODBC_NUMERIC
  8600.                 when @DBTYPE_UI8            then @ODBC_NUMERIC
  8601.                 when @DBTYPE_NUMERIC        then @ODBC_NUMERIC
  8602.                 when @DBTYPE_VARNUMERIC        then @ODBC_NUMERIC
  8603.                 when @DBTYPE_GUID            then @ODBC_GUID
  8604.                 when @DBTYPE_BYTES then
  8605.                     case
  8606.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG ) or (p.COLUMN_SIZE > 8000) then @ODBC_IMAGE
  8607.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISROWVER) = @DBCOLUMNFLAGS_ISROWVER AND
  8608.                                 (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH) = @DBCOLUMNFLAGS_ISFIXEDLENGTH and
  8609.                                  p.COLUMN_SIZE = 8
  8610.                             then @ODBC_TIMESTAMP
  8611.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH)
  8612.                             then @ODBC_BINARY
  8613.                         else @ODBC_VARBINARY
  8614.                     end
  8615.                 when @DBTYPE_STR then
  8616.                     case
  8617.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG ) or (p.COLUMN_SIZE > 8000) then @ODBC_TEXT
  8618.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_CHAR
  8619.                         else @ODBC_VARCHAR
  8620.                     end
  8621.                 when @DBTYPE_WSTR then
  8622.                     case
  8623.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG) or (p.COLUMN_SIZE > 4000) then @ODBC_NTEXT
  8624.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_NCHAR
  8625.                         else @ODBC_NVARCHAR
  8626.                     end
  8627.                 when @DBTYPE_BSTR then
  8628.                     case
  8629.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG) or (p.COLUMN_SIZE > 4000) then @ODBC_NTEXT
  8630.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_NCHAR
  8631.                         else @ODBC_NVARCHAR
  8632.                     end
  8633.                 else @ODBC_NVARCHAR     /*    Unknown OleDB datatype */
  8634.             end,
  8635.             TYPE_NAME = p.TYPE_NAME,
  8636.             COLUMN_SIZE =
  8637.                 COALESCE(c.CHARACTER_MAXIMUM_LENGTH, c.NUMERIC_PRECISION, p.COLUMN_SIZE),
  8638.             BUFFER_LENGTH = c.CHARACTER_OCTET_LENGTH,
  8639.             DECIMAL_DIGITS = c.NUMERIC_SCALE,
  8640.             NUM_PREC_RADIX = NULL,
  8641.             NULLABLE = convert(smallint, c.IS_NULLABLE),
  8642.             REMARKS = convert(nvarchar(255),c.DESCRIPTION),
  8643.             COLUMN_DEF = c.COLUMN_DEFAULT,
  8644.             SQL_DATA_TYPE = NULL,
  8645.             SQL_DATETIME_SUB = convert(smallint, c.DATETIME_PRECISION),
  8646.             CHAR_OCTET_LENGTH = c.CHARACTER_OCTET_LENGTH,
  8647.             ORDINAL_POSITION = c.ORDINAL_POSITION,
  8648.             IS_NULLABLE =
  8649.             case c.IS_NULLABLE
  8650.                 when 1 then 'YES'
  8651.                 else 'NO'
  8652.             end,
  8653.             SS_DATA_TYPE = NULL,
  8654.             COLUMN_FLAGS = c.COLUMN_FLAGS
  8655.         from master.dbo.SYSREMOTE_COLUMNS <
  8656.                     @table_server,
  8657.                     @table_catalog,
  8658.                     NULL,
  8659.                     NULL,
  8660.                     NULL > c,
  8661.                 master.dbo.SYSREMOTE_PROVIDER_TYPES <
  8662.                     @table_server > p
  8663.                     /* LUXOR/KAGERA don't support restricting DATA_TYPE */
  8664.             where c.DATA_TYPE = p.DATA_TYPE AND p.BEST_MATCH = 1 and
  8665.                 (TABLE_SCHEMA like @table_schema
  8666.                 or @table_schema is NULL or (TABLE_SCHEMA is NULL and @table_schema = N'%'))
  8667.                 and (c.COLUMN_NAME like @column_name
  8668.                 or     @column_name is NULL)
  8669.                 and c.TABLE_NAME like @table_name
  8670.     end
  8671.  
  8672.     update #tmp_columns
  8673.     set SQL_DATA_TYPE = spt_dt.SQL_DATA_TYPE,
  8674.         SQL_DATETIME_SUB =
  8675.         case
  8676.             when #tmp_columns.DATA_TYPE = @ODBC_DATETIME then 3
  8677.             else #tmp_columns.SQL_DATETIME_SUB
  8678.         end,
  8679.         TYPE_NAME = 
  8680.         case 
  8681.             when #tmp_columns.DATA_TYPE = @ODBC_BINARY and (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISROWVER) = @DBCOLUMNFLAGS_ISROWVER AND
  8682.                     (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH) = @DBCOLUMNFLAGS_ISFIXEDLENGTH and
  8683.                     BUFFER_LENGTH = 8
  8684.                 then 'timestamp'
  8685.             when #tmp_columns.DATA_TYPE = @ODBC_DATETIME and
  8686.                     #tmp_columns.SQL_DATETIME_SUB = 0
  8687.                 then 'smalldatetime'
  8688.             when #tmp_columns.DATA_TYPE = @ODBC_MONEY and
  8689.                     #tmp_columns.COLUMN_SIZE = 10
  8690.                 then 'smallmoney'
  8691.             when (#tmp_columns.DATA_TYPE in (@ODBC_BINARY, @ODBC_CHAR) and
  8692.                     (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH) = @DBCOLUMNFLAGS_ISFIXEDLENGTH) or
  8693.                  #tmp_columns.DATA_TYPE in (@ODBC_TEXT, @ODBC_NTEXT, @ODBC_IMAGE)
  8694.                 then spt_dt.TYPE_NAME
  8695.             else isnull(#tmp_columns.TYPE_NAME, spt_dt.TYPE_NAME)
  8696.         end,
  8697.         COLUMN_SIZE =
  8698.         case
  8699.             when #tmp_columns.DATA_TYPE IN (@ODBC_REAL, @ODBC_FLOAT) then /* app. numeric types */
  8700.                 spt_dt.data_precision
  8701.             when #tmp_columns.DATA_TYPE = @ODBC_DATETIME and
  8702.                     #tmp_columns.SQL_DATETIME_SUB = 0
  8703.                 then 16
  8704.             else #tmp_columns.COLUMN_SIZE
  8705.         end,
  8706.         BUFFER_LENGTH =
  8707.         case
  8708.             when #tmp_columns.DATA_TYPE IN (@ODBC_NUMERIC, @ODBC_DECIMAL) then    /* decimal/numeric types */
  8709.                 COLUMN_SIZE+2
  8710.             when isnull (#tmp_columns.BUFFER_LENGTH,0) = 0 then spt_dt.length
  8711.             else #tmp_columns.BUFFER_LENGTH
  8712.         end,
  8713.         DECIMAL_DIGITS =
  8714.         case
  8715.             when #tmp_columns.DATA_TYPE = @ODBC_DATETIME
  8716.                 then #tmp_columns.SQL_DATETIME_SUB
  8717.             else COALESCE (DECIMAL_DIGITS, spt_dt.numeric_scale)
  8718.         end,
  8719.         NUM_PREC_RADIX = spt_dt.RADIX,
  8720.         SS_DATA_TYPE = 
  8721.         case
  8722.             when #tmp_columns.DATA_TYPE in ( @ODBC_TINYINT, @ODBC_SMALLINT, @ODBC_INT ) 
  8723.                 and #tmp_columns.NULLABLE = 1
  8724.                     then @ODS_INT_NULL
  8725.             when #tmp_columns.DATA_TYPE = @ODBC_TINYINT and #tmp_columns.NULLABLE = 0 
  8726.                 then @ODS_TINYINT_NOT_NULL
  8727.             when #tmp_columns.DATA_TYPE = @ODBC_SMALLINT    and #tmp_columns.NULLABLE = 0
  8728.                 then @ODS_SMALLINT_NOT_NULL
  8729.             when #tmp_columns.DATA_TYPE = @ODBC_INT  and #tmp_columns.NULLABLE = 0
  8730.                 then @ODS_INT_NOT_NULL
  8731.             when #tmp_columns.DATA_TYPE in (@ODBC_REAL, @ODBC_FLOAT) and #tmp_columns.NULLABLE = 1
  8732.                 then @ODS_FLOAT_NULL
  8733.             when #tmp_columns.DATA_TYPE = @ODBC_REAL and #tmp_columns.NULLABLE = 0
  8734.                 then @ODS_REAL_NOT_NULL
  8735.             when #tmp_columns.DATA_TYPE = @ODBC_FLOAT and #tmp_columns.NULLABLE = 0
  8736.                 then @ODS_FLOAT_NOT_NULL
  8737.             when #tmp_columns.DATA_TYPE = @ODBC_MONEY and #tmp_columns.NULLABLE = 1 
  8738.                 then @ODS_MONEY_NULL
  8739.             when #tmp_columns.DATA_TYPE = @ODBC_MONEY and #tmp_columns.NULLABLE = 0 
  8740.                 then @ODS_MONEY_NOT_NULL
  8741.             when #tmp_columns.DATA_TYPE = @ODBC_DATETIME and #tmp_columns.NULLABLE = 1
  8742.                 then @ODS_DATETIME_NULL
  8743.             when #tmp_columns.DATA_TYPE = @ODBC_DATETIME and #tmp_columns.NULLABLE = 0
  8744.                 then @ODS_DATETIME_NOT_NULL
  8745.             when #tmp_columns.DATA_TYPE in (@ODBC_NTEXT, @ODBC_TEXT) then @ODS_TEXT
  8746.             when #tmp_columns.DATA_TYPE = @ODBC_BIT then @ODS_BIT
  8747.             when #tmp_columns.DATA_TYPE = @ODBC_DECIMAL then  @ODS_DECIMAL    
  8748.             when #tmp_columns.DATA_TYPE = @ODBC_NUMERIC then  @ODS_NUMERIC
  8749.             when #tmp_columns.DATA_TYPE = @ODBC_GUID then @ODS_GUID
  8750.             when #tmp_columns.DATA_TYPE = @ODBC_IMAGE then  @ODS_IMAGE
  8751.             when #tmp_columns.DATA_TYPE = @ODBC_TIMESTAMP
  8752.                 and (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISROWVER) = @DBCOLUMNFLAGS_ISROWVER AND
  8753.                     (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH) = @DBCOLUMNFLAGS_ISFIXEDLENGTH and
  8754.                     BUFFER_LENGTH = 8
  8755.                 then @ODS_TIMESTAMP
  8756.             when #tmp_columns.DATA_TYPE = @ODBC_BINARY and #tmp_columns.NULLABLE =    1
  8757.                 and (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH)
  8758.                 then  @ODS_BINARY_NULL
  8759.             when #tmp_columns.DATA_TYPE = @ODBC_BINARY and #tmp_columns.NULLABLE =    0
  8760.                 and (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH)
  8761.                 then @ODS_BINARY_NOT_NULL
  8762.             when #tmp_columns.DATA_TYPE = @ODBC_VARBINARY
  8763.                 then @ODS_VARBINARY    
  8764.             when #tmp_columns.DATA_TYPE in (@ODBC_CHAR, @ODBC_NCHAR) and #tmp_columns.NULLABLE = 1
  8765.                 then @ODS_CHAR_NULL
  8766.             when #tmp_columns.DATA_TYPE in (@ODBC_CHAR, @ODBC_NCHAR) and #tmp_columns.NULLABLE = 0
  8767.                 then @ODS_CHAR_NOT_NULL
  8768.             when #tmp_columns.DATA_TYPE in (@ODBC_VARCHAR, @ODBC_NVARCHAR) then @ODS_VARCHAR
  8769.             else null
  8770.         end
  8771.         from master.dbo.spt_datatype_info spt_dt
  8772.         where spt_dt.DATA_TYPE = #tmp_columns.DATA_TYPE
  8773.             AND (spt_dt.ODBCVer is null or spt_dt.ODBCVer = @ODBCVer)
  8774.             and isnull(spt_dt.AUTO_INCREMENT,0) = 0
  8775.             and not spt_dt.TYPE_NAME = 'smalldatetime'
  8776.  
  8777.     select
  8778.             TABLE_CAT,            TABLE_SCHEM,        TABLE_NAME,
  8779.             COLUMN_NAME,        DATA_TYPE,            TYPE_NAME,
  8780.             COLUMN_SIZE,        BUFFER_LENGTH,        DECIMAL_DIGITS,
  8781.             NUM_PREC_RADIX,        NULLABLE,            REMARKS,
  8782.             COLUMN_DEF,            SQL_DATA_TYPE,        SQL_DATETIME_SUB,
  8783.             CHAR_OCTET_LENGTH,    ORDINAL_POSITION,    IS_NULLABLE,
  8784.             SS_DATA_TYPE
  8785.         from #tmp_columns
  8786.         order by TABLE_CAT, TABLE_SCHEM, TABLE_NAME, ORDINAL_POSITION
  8787.  
  8788.         drop table #tmp_columns
  8789.  
  8790. go
  8791. if (charindex('8.00', @@version) > 0)
  8792.     drop procedure sp_columns_ex
  8793. else
  8794. begin
  8795.     print ''
  8796.     print ''
  8797.     print 'Warning:'
  8798.     print 'you are installing the stored procedures '
  8799.     print 'on a pre 8.0 SQL Server.'
  8800.     print 'Ignore the following errors.'
  8801. end
  8802. go
  8803.  
  8804. /* 8.0 version */
  8805. raiserror(15339,-1,-1,'sp_columns_ex')
  8806. go
  8807. create procedure sp_columns_ex(
  8808.     @table_server        sysname,
  8809.     @table_name            sysname = null,
  8810.     @table_schema        sysname = null,
  8811.     @table_catalog        sysname = null,
  8812.     @column_name        sysname = null,
  8813.     @ODBCVer            int = 2)
  8814. as
  8815.  
  8816.     set nocount on
  8817.     declare
  8818.     @DBCOLUMNFLAGS_ISFIXEDLENGTH binary(1),    @DBCOLUMNFLAGS_ISLONG    binary(1),
  8819.     @DBCOLUMNFLAGS_ISROWVER    binary(2)
  8820.  
  8821.     select
  8822.     @DBCOLUMNFLAGS_ISFIXEDLENGTH    = 0x10,    @DBCOLUMNFLAGS_ISLONG    = 0x80,
  8823.     @DBCOLUMNFLAGS_ISROWVER    = 0x0200
  8824.  
  8825.     declare    
  8826.     @DBTYPE_I2 smallint,            @DBTYPE_I4 smallint,
  8827.     @DBTYPE_R4 smallint,            @DBTYPE_R8 smallint,
  8828.     @DBTYPE_CY smallint,            @DBTYPE_DATE smallint,
  8829.     @DBTYPE_BSTR smallint,            @DBTYPE_BOOL smallint,
  8830.     @DBTYPE_VARNUMERIC smallint,    @DBTYPE_DECIMAL smallint,
  8831.     @DBTYPE_UI1 smallint,            @DBTYPE_BYREF smallint,
  8832.     @DBTYPE_I1 smallint,            @DBTYPE_UI2 smallint,
  8833.     @DBTYPE_UI4 smallint,            @DBTYPE_I8 smallint,
  8834.     @DBTYPE_UI8 smallint,            @DBTYPE_GUID smallint,
  8835.     @DBTYPE_BYTES smallint,            @DBTYPE_STR smallint,
  8836.     @DBTYPE_WSTR smallint,            @DBTYPE_NUMERIC smallint,
  8837.     @DBTYPE_DBDATE smallint,        @DBTYPE_DBTIME smallint,
  8838.     @DBTYPE_DBTIMESTAMP smallint
  8839.  
  8840.     select
  8841.     @DBTYPE_I2            = 2,    @DBTYPE_I4            = 3,
  8842.     @DBTYPE_R4            = 4,    @DBTYPE_R8            = 5,
  8843.     @DBTYPE_CY            = 6,    @DBTYPE_DATE        = 7,
  8844.     @DBTYPE_BSTR        = 8,    @DBTYPE_BOOL        = 11,
  8845.     @DBTYPE_VARNUMERIC    = 139,    @DBTYPE_DECIMAL        = 14,
  8846.     @DBTYPE_UI1            = 17,    @DBTYPE_BYREF        = 16384,
  8847.     @DBTYPE_I1            = 16,    @DBTYPE_UI2            = 18,
  8848.     @DBTYPE_UI4            = 19,    @DBTYPE_I8            = 20,
  8849.     @DBTYPE_UI8            = 21,    @DBTYPE_GUID        = 72,
  8850.     @DBTYPE_BYTES        = 128,    @DBTYPE_STR            = 129,
  8851.     @DBTYPE_WSTR        = 130,    @DBTYPE_NUMERIC        = 131,
  8852.     @DBTYPE_DBDATE        = 133,    @DBTYPE_DBTIME        = 134,    
  8853.     @DBTYPE_DBTIMESTAMP    = 135
  8854.  
  8855.     declare    
  8856.     @ODS_SMALLINT_NOT_NULL tinyint,    @ODS_INT_NULL tinyint,
  8857.     @ODS_INT_NOT_NULL tinyint,        @ODS_TINYINT_NOT_NULL tinyint,
  8858.     @ODS_REAL_NOT_NULL tinyint,        @ODS_FLOAT_NULL tinyint,
  8859.     @ODS_FLOAT_NOT_NULL tinyint,    @ODS_MONEY_NULL tinyint,
  8860.     @ODS_MONEY_NOT_NULL tinyint,    @ODS_DATETIME_NULL tinyint,
  8861.     @ODS_DATETIME_NOT_NULL tinyint,    @ODS_TEXT tinyint,
  8862.     @ODS_BIT tinyint,                @ODS_DECIMAL tinyint,
  8863.     @ODS_NUMERIC tinyint,            @ODS_GUID tinyint,
  8864.     @ODS_IMAGE tinyint,                @ODS_TIMESTAMP tinyint,
  8865.     @ODS_BINARY_NULL tinyint,        @ODS_BINARY_NOT_NULL tinyint,
  8866.     @ODS_VARBINARY tinyint,            @ODS_CHAR_NULL tinyint,
  8867.     @ODS_CHAR_NOT_NULL tinyint,        @ODS_VARCHAR tinyint
  8868.  
  8869.     select     
  8870.     @ODS_IMAGE = 34,            @ODS_TEXT = 35,
  8871.     @ODS_GUID = 36,                @ODS_BINARY_NULL = 37,            
  8872.     @ODS_VARBINARY = 37,        @ODS_INT_NULL = 38,
  8873.     @ODS_CHAR_NULL = 39,        @ODS_VARCHAR = 39,
  8874.     @ODS_TIMESTAMP = 45,        @ODS_BINARY_NOT_NULL = 45,
  8875.     @ODS_CHAR_NOT_NULL = 47,    @ODS_TINYINT_NOT_NULL = 48,
  8876.     @ODS_BIT = 50,                @ODS_SMALLINT_NOT_NULL = 52,    
  8877.     @ODS_INT_NOT_NULL = 56,        @ODS_REAL_NOT_NULL = 59,        
  8878.     @ODS_MONEY_NOT_NULL = 60,    @ODS_DATETIME_NOT_NULL = 61,    
  8879.     @ODS_FLOAT_NOT_NULL = 62,    @ODS_DECIMAL = 106,
  8880.     @ODS_NUMERIC = 108,            @ODS_FLOAT_NULL = 109,
  8881.     @ODS_MONEY_NULL = 110,        @ODS_DATETIME_NULL = 111
  8882.  
  8883.     declare 
  8884.     @ODBC_GUID smallint,        @ODBC_NTEXT smallint,
  8885.     @ODBC_NVARCHAR smallint,    @ODBC_NCHAR smallint,
  8886.     @ODBC_BIT smallint,            @ODBC_TINYINT smallint,
  8887.     @ODBC_IMAGE smallint,        @ODBC_VARBINARY smallint,
  8888.     @ODBC_TIMESTAMP smallint,    @ODBC_BINARY smallint,
  8889.     @ODBC_TEXT smallint,        @ODBC_CHAR smallint,
  8890.     @ODBC_NUMERIC smallint,        @ODBC_DECIMAL smallint,
  8891.     @ODBC_MONEY smallint,        @ODBC_INT smallint,        
  8892.     @ODBC_SMALLINT smallint,    @ODBC_FLOAT smallint,    
  8893.     @ODBC_REAL smallint,        @ODBC_VARCHAR smallint,
  8894.     @ODBC_DATETIME smallint
  8895.  
  8896.     select
  8897.     @ODBC_GUID        = -11,    @ODBC_NTEXT        = -10,
  8898.     @ODBC_NVARCHAR    = -9,    @ODBC_NCHAR        = -8,
  8899.     @ODBC_BIT        = -7,    @ODBC_TINYINT    = -6,
  8900.     @ODBC_IMAGE        = -4,    @ODBC_VARBINARY    = -3,
  8901.     @ODBC_TIMESTAMP    = -2,    @ODBC_BINARY    = -2,
  8902.     @ODBC_TEXT        = -1,    @ODBC_CHAR        = 1,
  8903.     @ODBC_NUMERIC    = 2,    @ODBC_DECIMAL    = 3,
  8904.     @ODBC_MONEY        = 3,    @ODBC_INT        = 4,        
  8905.     @ODBC_SMALLINT    = 5,    @ODBC_FLOAT        = 6,    
  8906.     @ODBC_REAL        = 7,    @ODBC_VARCHAR    = 12,
  8907.     @ODBC_DATETIME    =    
  8908.     case @ODBCVer
  8909.         when 2 then 11
  8910.         else 93
  8911.     end
  8912.  
  8913.     create table #tmp_columns
  8914.     (    TABLE_CAT sysname collate database_default NULL,
  8915.         TABLE_SCHEM sysname collate database_default NULL,
  8916.         TABLE_NAME sysname    collate database_default NOT NULL,
  8917.         COLUMN_NAME sysname collate database_default NULL,
  8918.         DATA_TYPE smallint NOT NULL,
  8919.         TYPE_NAME sysname  collate database_default NULL,
  8920.         COLUMN_SIZE int NULL,
  8921.         BUFFER_LENGTH int NULL,
  8922.         DECIMAL_DIGITS smallint NULL,
  8923.         NUM_PREC_RADIX smallint NULL,
  8924.         NULLABLE smallint NOT NULL,
  8925.         REMARKS nvarchar(255) collate database_default NULL,
  8926.             COLUMN_DEF nvarchar(128) collate database_default NULL,
  8927.             SQL_DATA_TYPE smallint null,
  8928.             SQL_DATETIME_SUB smallint NULL,
  8929.         CHAR_OCTET_LENGTH int NULL,
  8930.         ORDINAL_POSITION smallint,
  8931.         IS_NULLABLE varchar(254) collate database_default NOT NULL,
  8932.         SS_DATA_TYPE tinyint null,
  8933.         COLUMN_FLAGS int NOT NULL)
  8934.  
  8935.     if ((isnull(charindex('%', @table_name),0) = 0) and
  8936.         (isnull(charindex('[', @table_name),0) = 0) and
  8937.         (isnull(charindex('_', @table_name),0) = 0))
  8938.     begin    /*    If no wild carding */
  8939.         insert into #tmp_columns
  8940.         select
  8941.             TABLE_CAT = c.TABLE_CATALOG,
  8942.             TABLE_SCHEM = c.TABLE_SCHEMA,
  8943.             TABLE_NAME = c.TABLE_NAME,
  8944.             COLUMN_NAME = c.COLUMN_NAME,
  8945.             DATA_TYPE =
  8946.             case c.DATA_TYPE & ~@DBTYPE_BYREF
  8947.                 when @DBTYPE_I2                then @ODBC_SMALLINT
  8948.                 when @DBTYPE_I4                then @ODBC_INT
  8949.                 when @DBTYPE_R4                then @ODBC_REAL
  8950.                 when @DBTYPE_R8                then @ODBC_FLOAT
  8951.                 when @DBTYPE_CY                then @ODBC_MONEY
  8952.                 when @DBTYPE_DATE            then @ODBC_DATETIME
  8953.                 when @DBTYPE_DBDATE            then @ODBC_DATETIME
  8954.                 when @DBTYPE_DBTIME            then @ODBC_DATETIME
  8955.                 when @DBTYPE_DBTIMESTAMP    then @ODBC_DATETIME
  8956.                 when @DBTYPE_BOOL            then @ODBC_BIT
  8957.                 when @DBTYPE_DECIMAL        then @ODBC_DECIMAL
  8958.                 when @DBTYPE_UI1            then @ODBC_TINYINT
  8959.                 when @DBTYPE_I1                then @ODBC_NUMERIC
  8960.                 when @DBTYPE_UI2            then @ODBC_NUMERIC
  8961.                 when @DBTYPE_UI4            then @ODBC_NUMERIC
  8962.                 when @DBTYPE_I8                then @ODBC_NUMERIC
  8963.                 when @DBTYPE_UI8            then @ODBC_NUMERIC
  8964.                 when @DBTYPE_NUMERIC        then @ODBC_NUMERIC
  8965.                 when @DBTYPE_VARNUMERIC        then @ODBC_NUMERIC
  8966.                 when @DBTYPE_GUID            then @ODBC_GUID
  8967.                 when @DBTYPE_BYTES then
  8968.                     case
  8969.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG ) or (p.COLUMN_SIZE > 8000) then @ODBC_IMAGE
  8970.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISROWVER) = @DBCOLUMNFLAGS_ISROWVER AND
  8971.                                 (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH) = @DBCOLUMNFLAGS_ISFIXEDLENGTH and
  8972.                                  p.COLUMN_SIZE = 8
  8973.                             then @ODBC_TIMESTAMP
  8974.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH)
  8975.                             then @ODBC_BINARY
  8976.                         else @ODBC_VARBINARY
  8977.                     end
  8978.                 when @DBTYPE_STR then
  8979.                     case
  8980.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG ) or (p.COLUMN_SIZE > 8000) then @ODBC_TEXT
  8981.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_CHAR
  8982.                         else @ODBC_VARCHAR
  8983.                     end
  8984.                 when @DBTYPE_WSTR then
  8985.                     case
  8986.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG) or (p.COLUMN_SIZE > 4000) then @ODBC_NTEXT
  8987.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_NCHAR
  8988.                         else @ODBC_NVARCHAR
  8989.                     end
  8990.                 when @DBTYPE_BSTR then
  8991.                     case
  8992.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG) or (p.COLUMN_SIZE > 4000) then @ODBC_NTEXT
  8993.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_NCHAR
  8994.                         else @ODBC_NVARCHAR
  8995.                     end
  8996.                 else @ODBC_NVARCHAR     /*    Unknown OleDB datatype */
  8997.             end,
  8998.             TYPE_NAME = p.TYPE_NAME,
  8999.             COLUMN_SIZE =
  9000.                 COALESCE(c.CHARACTER_MAXIMUM_LENGTH, c.NUMERIC_PRECISION, p.COLUMN_SIZE),
  9001.             BUFFER_LENGTH = c.CHARACTER_OCTET_LENGTH,
  9002.             DECIMAL_DIGITS = c.NUMERIC_SCALE,
  9003.             NUM_PREC_RADIX = NULL,
  9004.             NULLABLE = convert(smallint, c.IS_NULLABLE),
  9005.             REMARKS = convert(nvarchar(255),c.DESCRIPTION),
  9006.             COLUMN_DEF = c.COLUMN_DEFAULT,
  9007.             SQL_DATA_TYPE = NULL,
  9008.             SQL_DATETIME_SUB = convert(smallint, c.DATETIME_PRECISION),
  9009.             CHAR_OCTET_LENGTH = c.CHARACTER_OCTET_LENGTH,
  9010.             ORDINAL_POSITION = c.ORDINAL_POSITION,
  9011.             IS_NULLABLE =
  9012.             case c.IS_NULLABLE
  9013.                 when 1 then 'YES'
  9014.                 else 'NO'
  9015.             end,
  9016.             SS_DATA_TYPE = NULL,
  9017.             COLUMN_FLAGS = c.COLUMN_FLAGS
  9018.         from master.dbo.SYSREMOTE_COLUMNS <
  9019.                     @table_server,
  9020.                     @table_catalog,
  9021.                     NULL,
  9022.                     @table_name,
  9023.                     NULL > c,
  9024.                 master.dbo.SYSREMOTE_PROVIDER_TYPES <
  9025.                     @table_server > p
  9026.                     /* LUXOR/KAGERA don't support restricting DATA_TYPE */
  9027.             where c.DATA_TYPE = p.DATA_TYPE AND p.BEST_MATCH = 1 and
  9028.                 (TABLE_SCHEMA like @table_schema
  9029.                 or @table_schema is NULL or (TABLE_SCHEMA is NULL and @table_schema = N'%'))
  9030.                 and (c.COLUMN_NAME like @column_name
  9031.                 or     @column_name is NULL)
  9032.     end
  9033.     else
  9034.     begin    /*    If wild carding */
  9035.         insert into #tmp_columns
  9036.         select
  9037.             TABLE_CAT = c.TABLE_CATALOG,
  9038.             TABLE_SCHEM = c.TABLE_SCHEMA,
  9039.             TABLE_NAME = c.TABLE_NAME,
  9040.             COLUMN_NAME = c.COLUMN_NAME,
  9041.             DATA_TYPE =
  9042.             case c.DATA_TYPE & ~@DBTYPE_BYREF
  9043.                 when @DBTYPE_I2                then @ODBC_SMALLINT
  9044.                 when @DBTYPE_I4                then @ODBC_INT
  9045.                 when @DBTYPE_R4                then @ODBC_REAL
  9046.                 when @DBTYPE_R8                then @ODBC_FLOAT
  9047.                 when @DBTYPE_CY                then @ODBC_MONEY
  9048.                 when @DBTYPE_DATE            then @ODBC_DATETIME
  9049.                 when @DBTYPE_DBDATE            then @ODBC_DATETIME
  9050.                 when @DBTYPE_DBTIME            then @ODBC_DATETIME
  9051.                 when @DBTYPE_DBTIMESTAMP    then @ODBC_DATETIME
  9052.                 when @DBTYPE_BOOL            then @ODBC_BIT
  9053.                 when @DBTYPE_DECIMAL        then @ODBC_DECIMAL
  9054.                 when @DBTYPE_UI1            then @ODBC_TINYINT
  9055.                 when @DBTYPE_I1                then @ODBC_NUMERIC
  9056.                 when @DBTYPE_UI2            then @ODBC_NUMERIC
  9057.                 when @DBTYPE_UI4            then @ODBC_NUMERIC
  9058.                 when @DBTYPE_I8                then @ODBC_NUMERIC
  9059.                 when @DBTYPE_UI8            then @ODBC_NUMERIC
  9060.                 when @DBTYPE_NUMERIC        then @ODBC_NUMERIC
  9061.                 when @DBTYPE_VARNUMERIC        then @ODBC_NUMERIC
  9062.                 when @DBTYPE_GUID            then @ODBC_GUID
  9063.                 when @DBTYPE_BYTES then
  9064.                     case
  9065.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG ) or (p.COLUMN_SIZE > 8000) then @ODBC_IMAGE
  9066.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISROWVER) = @DBCOLUMNFLAGS_ISROWVER AND
  9067.                                 (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH) = @DBCOLUMNFLAGS_ISFIXEDLENGTH and
  9068.                                  p.COLUMN_SIZE = 8
  9069.                             then @ODBC_TIMESTAMP
  9070.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH)
  9071.                             then @ODBC_BINARY
  9072.                         else @ODBC_VARBINARY
  9073.                     end
  9074.                 when @DBTYPE_STR then
  9075.                     case
  9076.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG ) or (p.COLUMN_SIZE > 8000) then @ODBC_TEXT
  9077.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_CHAR
  9078.                         else @ODBC_VARCHAR
  9079.                     end
  9080.                 when @DBTYPE_WSTR then
  9081.                     case
  9082.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG) or (p.COLUMN_SIZE > 4000) then @ODBC_NTEXT
  9083.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_NCHAR
  9084.                         else @ODBC_NVARCHAR
  9085.                     end
  9086.                 when @DBTYPE_BSTR then
  9087.                     case
  9088.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG) or (p.COLUMN_SIZE > 4000) then @ODBC_NTEXT
  9089.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_NCHAR
  9090.                         else @ODBC_NVARCHAR
  9091.                     end
  9092.                 else @ODBC_NVARCHAR     /*    Unknown OleDB datatype */
  9093.             end,
  9094.             TYPE_NAME = p.TYPE_NAME,
  9095.             COLUMN_SIZE =
  9096.                 COALESCE(c.CHARACTER_MAXIMUM_LENGTH, c.NUMERIC_PRECISION, p.COLUMN_SIZE),
  9097.             BUFFER_LENGTH = c.CHARACTER_OCTET_LENGTH,
  9098.             DECIMAL_DIGITS = c.NUMERIC_SCALE,
  9099.             NUM_PREC_RADIX = NULL,
  9100.             NULLABLE = convert(smallint, c.IS_NULLABLE),
  9101.             REMARKS = convert(nvarchar(255),c.DESCRIPTION),
  9102.             COLUMN_DEF = c.COLUMN_DEFAULT,
  9103.             SQL_DATA_TYPE = NULL,
  9104.             SQL_DATETIME_SUB = convert(smallint, c.DATETIME_PRECISION),
  9105.             CHAR_OCTET_LENGTH = c.CHARACTER_OCTET_LENGTH,
  9106.             ORDINAL_POSITION = c.ORDINAL_POSITION,
  9107.             IS_NULLABLE =
  9108.             case c.IS_NULLABLE
  9109.                 when 1 then 'YES'
  9110.                 else 'NO'
  9111.             end,
  9112.             SS_DATA_TYPE = NULL,
  9113.             COLUMN_FLAGS = c.COLUMN_FLAGS
  9114.         from master.dbo.SYSREMOTE_COLUMNS <
  9115.                     @table_server,
  9116.                     @table_catalog,
  9117.                     NULL,
  9118.                     NULL,
  9119.                     NULL > c,
  9120.                 master.dbo.SYSREMOTE_PROVIDER_TYPES <
  9121.                     @table_server > p
  9122.                     /* LUXOR/KAGERA don't support restricting DATA_TYPE */
  9123.             where c.DATA_TYPE = p.DATA_TYPE AND p.BEST_MATCH = 1 and
  9124.                 (TABLE_SCHEMA like @table_schema
  9125.                 or @table_schema is NULL or (TABLE_SCHEMA is NULL and @table_schema = N'%'))
  9126.                 and (c.COLUMN_NAME like @column_name
  9127.                 or     @column_name is NULL)
  9128.                 and c.TABLE_NAME like @table_name
  9129.     end
  9130.  
  9131.     update #tmp_columns
  9132.     set SQL_DATA_TYPE = spt_dt.SQL_DATA_TYPE,
  9133.         SQL_DATETIME_SUB =
  9134.         case
  9135.             when #tmp_columns.DATA_TYPE = @ODBC_DATETIME then 3
  9136.             else #tmp_columns.SQL_DATETIME_SUB
  9137.         end,
  9138.         TYPE_NAME = 
  9139.         case 
  9140.             when #tmp_columns.DATA_TYPE = @ODBC_BINARY and (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISROWVER) = @DBCOLUMNFLAGS_ISROWVER AND
  9141.                     (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH) = @DBCOLUMNFLAGS_ISFIXEDLENGTH and
  9142.                     BUFFER_LENGTH = 8
  9143.                 then 'timestamp'
  9144.             when #tmp_columns.DATA_TYPE = @ODBC_DATETIME and
  9145.                     #tmp_columns.SQL_DATETIME_SUB = 0
  9146.                 then 'smalldatetime'
  9147.             when #tmp_columns.DATA_TYPE = @ODBC_MONEY and
  9148.                     #tmp_columns.COLUMN_SIZE = 10
  9149.                 then 'smallmoney'
  9150.             when (#tmp_columns.DATA_TYPE in (@ODBC_BINARY, @ODBC_CHAR) and
  9151.                     (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH) = @DBCOLUMNFLAGS_ISFIXEDLENGTH) or
  9152.                  #tmp_columns.DATA_TYPE in (@ODBC_TEXT, @ODBC_NTEXT, @ODBC_IMAGE)
  9153.                 then spt_dt.TYPE_NAME collate database_default
  9154.             else isnull(#tmp_columns.TYPE_NAME, spt_dt.TYPE_NAME collate database_default)
  9155.         end,
  9156.         COLUMN_SIZE =
  9157.         case
  9158.             when #tmp_columns.DATA_TYPE IN (@ODBC_REAL, @ODBC_FLOAT) then /* app. numeric types */
  9159.                 spt_dt.data_precision
  9160.             when #tmp_columns.DATA_TYPE = @ODBC_DATETIME and
  9161.                     #tmp_columns.SQL_DATETIME_SUB = 0
  9162.                 then 16
  9163.             else #tmp_columns.COLUMN_SIZE
  9164.         end,
  9165.         BUFFER_LENGTH =
  9166.         case
  9167.             when #tmp_columns.DATA_TYPE IN (@ODBC_NUMERIC, @ODBC_DECIMAL) then    /* decimal/numeric types */
  9168.                 COLUMN_SIZE+2
  9169.             when isnull (#tmp_columns.BUFFER_LENGTH,0) = 0 then spt_dt.length
  9170.             else #tmp_columns.BUFFER_LENGTH
  9171.         end,
  9172.         DECIMAL_DIGITS =
  9173.         case
  9174.             when #tmp_columns.DATA_TYPE = @ODBC_DATETIME
  9175.                 then #tmp_columns.SQL_DATETIME_SUB
  9176.             else COALESCE (DECIMAL_DIGITS, spt_dt.numeric_scale)
  9177.         end,
  9178.         NUM_PREC_RADIX = spt_dt.RADIX,
  9179.         SS_DATA_TYPE = 
  9180.         case
  9181.             when #tmp_columns.DATA_TYPE in ( @ODBC_TINYINT, @ODBC_SMALLINT, @ODBC_INT ) 
  9182.                 and #tmp_columns.NULLABLE = 1
  9183.                     then @ODS_INT_NULL
  9184.             when #tmp_columns.DATA_TYPE = @ODBC_TINYINT and #tmp_columns.NULLABLE = 0 
  9185.                 then @ODS_TINYINT_NOT_NULL
  9186.             when #tmp_columns.DATA_TYPE = @ODBC_SMALLINT    and #tmp_columns.NULLABLE = 0
  9187.                 then @ODS_SMALLINT_NOT_NULL
  9188.             when #tmp_columns.DATA_TYPE = @ODBC_INT  and #tmp_columns.NULLABLE = 0
  9189.                 then @ODS_INT_NOT_NULL
  9190.             when #tmp_columns.DATA_TYPE in (@ODBC_REAL, @ODBC_FLOAT) and #tmp_columns.NULLABLE = 1
  9191.                 then @ODS_FLOAT_NULL
  9192.             when #tmp_columns.DATA_TYPE = @ODBC_REAL and #tmp_columns.NULLABLE = 0
  9193.                 then @ODS_REAL_NOT_NULL
  9194.             when #tmp_columns.DATA_TYPE = @ODBC_FLOAT and #tmp_columns.NULLABLE = 0
  9195.                 then @ODS_FLOAT_NOT_NULL
  9196.             when #tmp_columns.DATA_TYPE = @ODBC_MONEY and #tmp_columns.NULLABLE = 1 
  9197.                 then @ODS_MONEY_NULL
  9198.             when #tmp_columns.DATA_TYPE = @ODBC_MONEY and #tmp_columns.NULLABLE = 0 
  9199.                 then @ODS_MONEY_NOT_NULL
  9200.             when #tmp_columns.DATA_TYPE = @ODBC_DATETIME and #tmp_columns.NULLABLE = 1
  9201.                 then @ODS_DATETIME_NULL
  9202.             when #tmp_columns.DATA_TYPE = @ODBC_DATETIME and #tmp_columns.NULLABLE = 0
  9203.                 then @ODS_DATETIME_NOT_NULL
  9204.             when #tmp_columns.DATA_TYPE in (@ODBC_NTEXT, @ODBC_TEXT) then @ODS_TEXT
  9205.             when #tmp_columns.DATA_TYPE = @ODBC_BIT then @ODS_BIT
  9206.             when #tmp_columns.DATA_TYPE = @ODBC_DECIMAL then  @ODS_DECIMAL    
  9207.             when #tmp_columns.DATA_TYPE = @ODBC_NUMERIC then  @ODS_NUMERIC
  9208.             when #tmp_columns.DATA_TYPE = @ODBC_GUID then @ODS_GUID
  9209.             when #tmp_columns.DATA_TYPE = @ODBC_IMAGE then  @ODS_IMAGE
  9210.             when #tmp_columns.DATA_TYPE = @ODBC_TIMESTAMP
  9211.                 and (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISROWVER) = @DBCOLUMNFLAGS_ISROWVER AND
  9212.                     (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH) = @DBCOLUMNFLAGS_ISFIXEDLENGTH and
  9213.                     BUFFER_LENGTH = 8
  9214.                 then @ODS_TIMESTAMP
  9215.             when #tmp_columns.DATA_TYPE = @ODBC_BINARY and #tmp_columns.NULLABLE =    1
  9216.                 and (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH)
  9217.                 then  @ODS_BINARY_NULL
  9218.             when #tmp_columns.DATA_TYPE = @ODBC_BINARY and #tmp_columns.NULLABLE =    0
  9219.                 and (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH)
  9220.                 then @ODS_BINARY_NOT_NULL
  9221.             when #tmp_columns.DATA_TYPE = @ODBC_VARBINARY
  9222.                 then @ODS_VARBINARY    
  9223.             when #tmp_columns.DATA_TYPE in (@ODBC_CHAR, @ODBC_NCHAR) and #tmp_columns.NULLABLE = 1
  9224.                 then @ODS_CHAR_NULL
  9225.             when #tmp_columns.DATA_TYPE in (@ODBC_CHAR, @ODBC_NCHAR) and #tmp_columns.NULLABLE = 0
  9226.                 then @ODS_CHAR_NOT_NULL
  9227.             when #tmp_columns.DATA_TYPE in (@ODBC_VARCHAR, @ODBC_NVARCHAR) then @ODS_VARCHAR
  9228.             else null
  9229.         end
  9230.         from master.dbo.spt_datatype_info spt_dt
  9231.         where spt_dt.DATA_TYPE = #tmp_columns.DATA_TYPE
  9232.             AND (spt_dt.ODBCVer is null or spt_dt.ODBCVer = @ODBCVer)
  9233.             and isnull(spt_dt.AUTO_INCREMENT,0) = 0
  9234.             and not spt_dt.TYPE_NAME = 'smalldatetime'
  9235.  
  9236.     select
  9237.             TABLE_CAT,            TABLE_SCHEM,        TABLE_NAME,
  9238.             COLUMN_NAME,        DATA_TYPE,            TYPE_NAME,
  9239.             COLUMN_SIZE,        BUFFER_LENGTH,        DECIMAL_DIGITS,
  9240.             NUM_PREC_RADIX,        NULLABLE,            REMARKS,
  9241.             COLUMN_DEF,            SQL_DATA_TYPE,        SQL_DATETIME_SUB,
  9242.             CHAR_OCTET_LENGTH,    ORDINAL_POSITION,    IS_NULLABLE,
  9243.             SS_DATA_TYPE
  9244.         from #tmp_columns
  9245.         order by TABLE_CAT, TABLE_SCHEM, TABLE_NAME, ORDINAL_POSITION
  9246.  
  9247.         drop table #tmp_columns
  9248.  
  9249. go
  9250. grant execute on sp_columns_ex to public
  9251. go
  9252.  
  9253.  
  9254. if object_id('sp_table_privileges_ex', 'P') is not null
  9255.     drop proc sp_table_privileges_ex
  9256. go
  9257. raiserror(15339,-1,-1,'sp_table_privileges_ex')
  9258. go
  9259. create procedure sp_table_privileges_ex(
  9260.     @table_server        sysname,
  9261.     @table_name            sysname = null,
  9262.     @table_schema        sysname = null,
  9263.     @table_catalog        sysname = null)
  9264. as
  9265.     select
  9266.         TABLE_CAT = TABLE_CATALOG,
  9267.         TABLE_SCHEM = TABLE_SCHEMA,
  9268.         TABLE_NAME = TABLE_NAME,
  9269.         GRANTOR = GRANTOR,
  9270.         GRANTEE = GRANTEE,
  9271.         PRIVILEGE = PRIVILEGE_TYPE,
  9272.         IS_GRANTABLE = 
  9273.         case IS_GRANTABLE
  9274.             when 1 then 'YES'
  9275.             when 0 then 'NO'
  9276.             else null
  9277.         end
  9278.     from master.dbo.SYSREMOTE_TABLE_PRIVILEGES <
  9279.                 @table_server,
  9280.                 @table_catalog,
  9281.                 NULL,
  9282.                 NULL >
  9283.     where (TABLE_SCHEMA like @table_schema
  9284.         or @table_schema is NULL or (TABLE_SCHEMA is NULL and @table_schema = N'%'))
  9285.         and (TABLE_NAME like @table_name
  9286.         or     @table_name is NULL)
  9287.     order by TABLE_CAT, TABLE_SCHEM, TABLE_NAME, PRIVILEGE
  9288. go
  9289. grant execute on sp_table_privileges_ex to public
  9290. go
  9291.  
  9292.  
  9293. if object_id('sp_column_privileges_ex', 'P') is not null
  9294.     drop proc sp_column_privileges_ex
  9295. go
  9296. raiserror(15339,-1,-1,'sp_column_privileges_ex')
  9297. go
  9298. create procedure sp_column_privileges_ex(
  9299.     @table_server        sysname,
  9300.     @table_name            sysname = null,
  9301.     @table_schema        sysname = null,
  9302.     @table_catalog        sysname = null,
  9303.     @column_name        sysname = null)
  9304. as
  9305.     select
  9306.         TABLE_CAT = TABLE_CATALOG,
  9307.         TABLE_SCHEM = TABLE_SCHEMA,
  9308.         TABLE_NAME = TABLE_NAME,
  9309.         COLUMN_NAME = COLUMN_NAME,
  9310.         GRANTOR = GRANTOR,
  9311.         GRANTEE = GRANTEE,
  9312.         PRIVILEGE = PRIVILEGE_TYPE,
  9313.         IS_GRANTABLE = 
  9314.         case IS_GRANTABLE
  9315.             when 1 then 'YES'
  9316.             when 0 then 'NO'
  9317.             else null
  9318.         end
  9319.     from master.dbo.SYSREMOTE_COLUMN_PRIVILEGES <
  9320.         @table_server,
  9321.         @table_catalog,
  9322.         @table_schema,
  9323.         @table_name,
  9324.         NULL >
  9325.     where (COLUMN_NAME like @column_name
  9326.         or @column_name is NULL)
  9327.     order by TABLE_CAT, TABLE_SCHEM, TABLE_NAME, COLUMN_NAME, PRIVILEGE
  9328. go
  9329. grant execute on sp_column_privileges_ex to public
  9330. go
  9331.  
  9332.  
  9333. if object_id('sp_indexes', 'P') is not null
  9334.     drop proc sp_indexes
  9335. go
  9336. raiserror(15339,-1,-1,'sp_indexes')
  9337. go
  9338. create procedure sp_indexes(
  9339.     @table_server        sysname,
  9340.     @table_name            sysname = null,
  9341.     @table_schema        sysname = null,
  9342.     @table_catalog        sysname = null,
  9343.     @index_name            sysname = null,
  9344.     @is_unique          bit = null)
  9345. as
  9346.     select
  9347.         TABLE_CAT = TABLE_CATALOG,
  9348.         TABLE_SCHEM = TABLE_SCHEMA,
  9349.         TABLE_NAME = TABLE_NAME,
  9350.             NON_UNIQUE = convert(smallint, 1 - [UNIQUE]),
  9351.         INDEX_QUALIFIER = TABLE_NAME,
  9352.         INDEX_NAME = INDEX_NAME,
  9353.             TYPE =
  9354.         case [CLUSTERED]
  9355.             when 1 then 1
  9356.             else 3
  9357.         end,
  9358.         ORDINAL_POSITION = ORDINAL_POSITION,
  9359.             COLUMN_NAME = COLUMN_NAME,
  9360.         ASC_OR_DESC = 
  9361.         case [COLLATION]
  9362.             when 1 then 'A'
  9363.             when 2 then 'D'
  9364.             else null
  9365.         end,
  9366.             CARDINALITY = CARDINALITY,
  9367.         PAGES = 
  9368.         case [CLUSTERED]
  9369.             when 1 then PAGES
  9370.             else NULL
  9371.         end,
  9372.             FILTER_CONDITION = FILTER_CONDITION
  9373.     from master.dbo.SYSREMOTE_INDEXES <
  9374.                 @table_server,
  9375.                 @table_catalog,
  9376.                 @table_schema,
  9377.                         @index_name,
  9378.                 NULL,            /* TYPE (index type) */
  9379.                 @table_name >
  9380.     where @is_unique is null or @is_unique = [UNIQUE]
  9381.     order by NON_UNIQUE, TYPE, INDEX_QUALIFIER, INDEX_NAME, ORDINAL_POSITION
  9382. go
  9383. grant execute on sp_indexes to public
  9384. go
  9385.  
  9386.  
  9387. if object_id('sp_foreignkeys', 'P') is not null
  9388.     drop proc sp_foreignkeys
  9389. go
  9390. raiserror(15339,-1,-1,'sp_foreignkeys')
  9391. go
  9392. create procedure sp_foreignkeys(
  9393.     @table_server        sysname,
  9394.     @pktab_name         sysname = null,
  9395.     @pktab_schema       sysname = null,
  9396.     @pktab_catalog      sysname = null,
  9397.     @fktab_name         sysname = null,
  9398.     @fktab_schema       sysname = null,
  9399.     @fktab_catalog      sysname = null)
  9400. as
  9401.     select
  9402.         PKTABLE_CAT = PK_TABLE_CATALOG,
  9403.         PKTABLE_SCHEM = PK_TABLE_SCHEMA,
  9404.         PKTABLE_NAME = PK_TABLE_NAME,
  9405.         PKCOLUMN_NAME = PK_COLUMN_NAME,
  9406.         FKTABLE_CAT = FK_TABLE_CATALOG,
  9407.         FKTABLE_SCHEM = FK_TABLE_SCHEMA,
  9408.         FKTABLE_NAME = FK_TABLE_NAME,
  9409.         FKCOLUMN_NAME = FK_COLUMN_NAME,
  9410.         KEY_SEQ = ORDINAL,
  9411.         UPDATE_RULE = 
  9412.         case UPDATE_RULE
  9413.             when 'CASCADE' then 0
  9414.             when 'NO ACTION' then 1
  9415.             when 'SET NULL' then 2
  9416.             else null
  9417.         end,
  9418.         DELETE_RULE = 
  9419.         case DELETE_RULE
  9420.             when 'CASCADE' then 0
  9421.             when 'NO ACTION' then 1
  9422.             when 'SET NULL' then 2
  9423.             else null
  9424.         end,
  9425.         FK_NAME = convert(sysname, NULL),
  9426.         PK_NAME = convert(sysname, NULL),
  9427.         DEFERRABILITY = convert(smallint, null)
  9428.  
  9429.     from master.dbo.SYSREMOTE_FOREIGN_KEYS <
  9430.                 @table_server,
  9431.                 @pktab_catalog,
  9432.                 @pktab_schema,
  9433.                 @pktab_name,
  9434.                 @fktab_catalog,
  9435.                 @fktab_schema,
  9436.                 @fktab_name >
  9437.     order by FKTABLE_CAT, FKTABLE_SCHEM, FKTABLE_NAME, KEY_SEQ
  9438. go
  9439. grant execute on sp_foreignkeys to public
  9440. go
  9441.  
  9442.  
  9443. if object_id('sp_primarykeys', 'P') is not null
  9444.     drop proc sp_primarykeys
  9445. go
  9446. raiserror(15339,-1,-1,'sp_primarykeys')
  9447. go
  9448. create procedure sp_primarykeys(
  9449.     @table_server        sysname,
  9450.     @table_name         sysname = null,
  9451.     @table_schema       sysname = null,
  9452.     @table_catalog      sysname = null)
  9453. as
  9454.     select
  9455.         TABLE_CAT = TABLE_CATALOG,
  9456.         TABLE_SCHEM = TABLE_SCHEMA,
  9457.         TABLE_NAME = TABLE_NAME,
  9458.         COLUMN_NAME = COLUMN_NAME,
  9459.         KEY_SEQ = ORDINAL,
  9460.         PK_NAME = convert(sysname, NULL)
  9461.     from master.dbo.SYSREMOTE_PRIMARY_KEYS <
  9462.                 @table_server,
  9463.                 @table_catalog,
  9464.                 @table_schema,
  9465.                 @table_name >
  9466.     order by TABLE_CAT, TABLE_SCHEM, TABLE_NAME, KEY_SEQ
  9467. go
  9468. grant execute on sp_primarykeys to public
  9469. go
  9470.  
  9471. /*-------------- END CATALOG STORED PROCEDURES FOR OLEDB SOURCES ------------------*/
  9472.  
  9473.  
  9474. dump tran master with no_log
  9475. go
  9476.  
  9477. if (charindex('6.50', @@version) = 0 and
  9478.     charindex('7.00', @@version) = 0 and
  9479.     charindex('8.00', @@version) = 0)
  9480. begin
  9481.     print ''
  9482.     print ''
  9483.     print 'Warning:'
  9484.     print 'you are installing the stored procedures '
  9485.     print 'on a pre 6.50 SQL Server.'
  9486.     print 'Ignore the following errors.'
  9487. end
  9488.  
  9489. print 'creating sp_ddopen'
  9490. go
  9491.  
  9492. /*    Procedure for pre-6.50 server */
  9493. create procedure sp_ddopen(
  9494.                @handle            int output,
  9495.                @procname        sysname,
  9496.                @scrollopt        int output,
  9497.                @ccopt            int output,
  9498.                @rows            int output,
  9499.                @p1                varchar(255) = null,
  9500.                @p2                varchar(255) = null,
  9501.                @p3                varchar(255) = null,
  9502.                @p4                varchar(255) = null,
  9503.                @p5                varchar(255) = null,
  9504.                @p6                varchar(255) = null,
  9505.                @p7                int = null,
  9506.                @ODBCVer         int = 2)
  9507. as
  9508.     set nocount on
  9509.     declare @ret int
  9510.  
  9511.     if @procname = 'sp_column_privileges'
  9512.     begin
  9513.         create table #spcolpriv (
  9514.             TABLE_QUALIFIER varchar(32) null,
  9515.             TABLE_OWNER varchar(32) null,
  9516.             TABLE_NAME varchar(32)    not null,
  9517.             COLUMN_NAME varchar(32) not null,
  9518.             GRANTOR varchar(32) null,
  9519.             GRANTEE varchar(32) not null,
  9520.             PRIVILEGE varchar(32) not null,
  9521.             IS_GRANTABLE varchar(3) null
  9522.             )
  9523.         insert into #spcolpriv exec sp_column_privileges @p1,@p2,@p3,@p4
  9524.         exec @ret = sp_cursoropen @handle output,
  9525.             'select * from #spcolpriv',
  9526.             @scrollopt output, @ccopt output, @rows output
  9527.         drop table #spcolpriv
  9528.     end
  9529.     else if @procname = 'sp_columns'
  9530.     begin
  9531.         create table #spcolumns (
  9532.             TABLE_QUALIFIER varchar(32) null,
  9533.             TABLE_OWNER varchar(32) null,
  9534.             TABLE_NAME varchar(32)    not null,
  9535.             COLUMN_NAME varchar(32) not null,
  9536.             DATA_TYPE smallint not null,
  9537.             TYPE_NAME varchar(32) not null,
  9538.             "PRECISION" int null,
  9539.             LENGTH int null,
  9540.             SCALE smallint null,
  9541.             RADIX smallint null,
  9542.             NULLABLE smallint not null,
  9543.             REMARKS varchar(254) null,
  9544.             COLUMN_DEF varchar(255) null,
  9545.             SQL_DATA_TYPE smallint not null,
  9546.             SQL_DATETIME_SUB smallint null,
  9547.             CHAR_OCTET_LENGTH int null,
  9548.             ORDINAL_POSITION int not null,
  9549.             IS_NULLABLE varchar(254) null,
  9550.             SS_DATA_TYPE tinyint null
  9551.             )
  9552.         insert into #spcolumns exec sp_columns @p1,@p2,@p3,@p4,@ODBCVer
  9553.         exec @ret = sp_cursoropen @handle output,
  9554.             'select * from #spcolumns',
  9555.             @scrollopt output, @ccopt output, @rows output
  9556.         drop table #spcolumns
  9557.     end
  9558.     else if @procname = 'sp_datatype_info'
  9559.     begin
  9560.         create table #spdatatypeinfo (
  9561.             TYPE_NAME            varchar(32)  not null,
  9562.             DATA_TYPE            smallint not null,
  9563.             "PRECISION"            int null,
  9564.             LITERAL_PREFIX        varchar(32)    null,
  9565.             LITERAL_SUFFIX        varchar(32)    null,
  9566.             CREATE_PARAMS        varchar(32)    null,
  9567.             NULLABLE            smallint   not null,
  9568.             CASE_SENSITIVE        smallint   not null,
  9569.             SEARCHABLE            smallint   not null,
  9570.             UNSIGNED_ATTRIBUTE    smallint   null,
  9571.             MONEY    smallint    not null,
  9572.             AUTO_INCREMENT        smallint    null,
  9573.             LOCAL_TYPE_NAME     varchar(32) null,
  9574.             MINIMUM_SCALE        smallint     null,
  9575.             MAXIMUM_SCALE        smallint   null,
  9576.             SQL_DATA_TYPE        smallint      not null,
  9577.             SQL_DATETIME_SUB    smallint   null,
  9578.             NUM_PREC_RADIX        int     null,
  9579.             INTERVAL_PRECISION    smallint    NULL,
  9580.             USERTYPE            smallint not null)
  9581.         insert into #spdatatypeinfo exec sp_datatype_info @p7,@ODBCVer
  9582.         exec @ret = sp_cursoropen @handle output,
  9583.             'select * from #spdatatypeinfo',
  9584.             @scrollopt output, @ccopt output, @rows output
  9585.         drop table #spdatatypeinfo
  9586.     end
  9587.     else if @procname = 'sp_fkeys'
  9588.     begin
  9589.         create table #spfkeys (
  9590.             PKTABLE_QUALIFIER varchar(32)     null,
  9591.             PKTABLE_OWNER varchar(32)    null,
  9592.             PKTABLE_NAME varchar(32)  not null,
  9593.             PKCOLUMN_NAME varchar(32)    not null,
  9594.             FKTABLE_QUALIFIER varchar(32)    null,
  9595.             FKTABLE_OWNER varchar(32)    null,
  9596.             FKTABLE_NAME varchar(32)  not null,
  9597.             FKCOLUMN_NAME varchar(32)    not null,
  9598.             KEY_SEQ smallint not null,
  9599.             UPDATE_RULE smallint null,
  9600.             DELETE_RULE smallint null,
  9601.             FK_NAME varchar(32) null,
  9602.             PK_NAME varchar(32) null,
  9603.             DEFERRABILITY smallint null
  9604.             )
  9605.         insert into #spfkeys exec sp_fkeys @p1,@p2,@p3,@p4,@p5,@p6
  9606.         exec @ret = sp_cursoropen @handle output,
  9607.             'select * from #spfkeys',
  9608.             @scrollopt output, @ccopt output, @rows output
  9609.         drop table #spfkeys
  9610.     end
  9611.     else if @procname = 'sp_pkeys'
  9612.     begin
  9613.         create table #sppkeys (
  9614.             TABLE_QUALIFIER varchar(32)   null,
  9615.             TABLE_OWNER varchar(32)   null,
  9616.             TABLE_NAME varchar(32)    not null,
  9617.             COLUMN_NAME varchar(32)  not null,
  9618.             KEY_SEQ smallint not null,
  9619.             PK_NAME varchar(32) null
  9620.             )
  9621.         insert into #sppkeys exec sp_pkeys @p1,@p2,@p3
  9622.         exec @ret = sp_cursoropen @handle output,
  9623.             'select * from #sppkeys',
  9624.             @scrollopt output, @ccopt output, @rows output
  9625.         drop table #sppkeys
  9626.     end
  9627.     else if @procname = 'sp_special_columns'
  9628.     begin
  9629.         create table #spspeccol (
  9630.             SCOPE smallint null,
  9631.             COLUMN_NAME varchar(32) not null,
  9632.             DATA_TYPE smallint not null,
  9633.             TYPE_NAME varchar(32) not null,
  9634.             "PRECISION" int null,
  9635.             LENGTH int null,
  9636.             SCALE smallint null,
  9637.             PSEUDO_COLUMN smallint null
  9638.             )
  9639.         insert into #spspeccol exec sp_special_columns @p1,@p2,@p3,@p4,@p5,@p6,@ODBCVer
  9640.         exec @ret = sp_cursoropen @handle output,
  9641.             'select * from #spspeccol',
  9642.             @scrollopt output, @ccopt output, @rows output
  9643.         drop table #spspeccol
  9644.     end
  9645.     else if @procname = 'sp_sproc_columns'
  9646.     begin
  9647.         create table #spproccol (
  9648.             PROCEDURE_QUALIFIER varchar(32)  null,
  9649.             PROCEDURE_OWNER varchar(32)  null,
  9650.             PROCEDURE_NAME varchar(32)    not null,
  9651.             COLUMN_NAME varchar(32) not null,
  9652.             COLUMN_TYPE smallint not null,
  9653.             DATA_TYPE smallint not null,
  9654.             TYPE_NAME varchar(32) not null,
  9655.             "PRECISION" int null,
  9656.             LENGTH int null,
  9657.             SCALE smallint null,
  9658.             RADIX smallint null,
  9659.             NULLABLE smallint not null,
  9660.             REMARKS varchar(254) null,
  9661.             COLUMN_DEF varchar(255) null,
  9662.             SQL_DATA_TYPE smallint not null,
  9663.             SQL_DATETIME_SUB smallint null,
  9664.             CHAR_OCTET_LENGTH int null,
  9665.             ORDINAL_POSITION int not null,
  9666.             IS_NULLABLE varchar(254) null,
  9667.             SS_DATA_TYPE tinyint null
  9668.             )
  9669.         insert into #spproccol exec sp_sproc_columns @p1,@p2,@p3,@p4,@ODBCVer
  9670.         exec @ret = sp_cursoropen @handle output,
  9671.             'select * from #spproccol',
  9672.             @scrollopt output, @ccopt output, @rows output
  9673.         drop table #spproccol
  9674.     end
  9675.     else if @procname = 'sp_statistics'
  9676.     begin
  9677.         create table #spstatistics (
  9678.             TABLE_QUALIFIER varchar(32)   null,
  9679.             TABLE_OWNER varchar(32)   null,
  9680.             TABLE_NAME varchar(32)    not null,
  9681.             NON_UNIQUE smallint null,
  9682.             INDEX_QUALIFIER varchar(32) null,
  9683.             INDEX_NAME varchar(32)    null,
  9684.             TYPE smallint not null,
  9685.             SEQ_IN_INDEX smallint null,
  9686.             COLUMN_NAME varchar(32) null,
  9687.             COLLATION char(1) null,
  9688.             CARDINALITY int null,
  9689.             PAGES int null,
  9690.             FILTER_CONDITION varchar(128) null
  9691.             )
  9692.         insert into #spstatistics exec sp_statistics @p1,@p2,@p3,@p4,@p5,@p6
  9693.         exec @ret = sp_cursoropen @handle output,
  9694.             'select * from #spstatistics',
  9695.             @scrollopt output, @ccopt output, @rows output
  9696.         drop table #spstatistics
  9697.     end
  9698.     else if @procname = 'sp_stored_procedures'
  9699.     begin
  9700.         create table #spprocedures (
  9701.             PROCEDURE_QUALIFIER varchar(32)  null,
  9702.             PROCEDURE_OWNER varchar(32)  null,
  9703.             PROCEDURE_NAME varchar(32)    not null,
  9704.             NUM_INPUT_PARAMS int null,
  9705.             NUM_OUTPUT_PARAMS int null,
  9706.             NUM_RESULT_SETS int null,
  9707.             REMARKS varchar(254) null,
  9708.             PROCEDURE_TYPE smallint null
  9709.             )
  9710.         insert into #spprocedures exec sp_stored_procedures @p1,@p2,@p3
  9711.         exec @ret = sp_cursoropen @handle output,
  9712.             'select * from #spprocedures',
  9713.             @scrollopt output, @ccopt output, @rows output
  9714.         drop table #spprocedures
  9715.     end
  9716.     else if @procname = 'sp_table_privileges'
  9717.     begin
  9718.         create table #sptabpriv (
  9719.             TABLE_QUALIFIER varchar(32) null,
  9720.             TABLE_OWNER varchar(32) null,
  9721.             TABLE_NAME varchar(32)    not null,
  9722.             GRANTOR varchar(32) null,
  9723.             GRANTEE varchar(32) not null,
  9724.             PRIVILEGE varchar(32) not null,
  9725.             IS_GRANTABLE varchar(3) null
  9726.             )
  9727.         insert into #sptabpriv exec sp_table_privileges @p1,@p2,@p3
  9728.         exec @ret = sp_cursoropen @handle output,
  9729.             'select * from #sptabpriv',
  9730.             @scrollopt output, @ccopt output, @rows output
  9731.         drop table #sptabpriv
  9732.     end
  9733.     else if @procname = 'sp_tables'
  9734.     begin
  9735.         create table #sptables (
  9736.             TABLE_QUALIFIER varchar(32) null,
  9737.             TABLE_OWNER varchar(32) null,
  9738.             TABLE_NAME varchar(32)    null,
  9739.             TABLE_TYPE     varchar(32) null,
  9740.             REMARKS varchar(254) null)
  9741.         insert into #sptables exec sp_tables @p1,@p2,@p3,@p4
  9742.         exec @ret = sp_cursoropen @handle output,
  9743.             'select * from #sptables',
  9744.             @scrollopt output, @ccopt output, @rows output
  9745.         drop table #sptables
  9746.     end
  9747.     else
  9748.         print 'Unknown sp_ddopen procedure'
  9749.     select @ret = isnull(@ret,0)
  9750.     return isnull(@ret,0)
  9751. go
  9752.  
  9753. if (charindex('7.00', @@version) > 0 or
  9754.     charindex('8.00', @@version) > 0)
  9755.     drop procedure sp_ddopen
  9756. else
  9757. begin
  9758.     print ''
  9759.     print ''
  9760.     print 'Warning:'
  9761.     print 'you are installing the stored procedures '
  9762.     print 'on a pre 7.0 SQL Server.'
  9763.     print 'Ignore the following errors.'
  9764. end
  9765. go
  9766.  
  9767. /*    Procedure for 7.0 server */
  9768. create procedure sp_ddopen; 1(
  9769.                @handle            int output,
  9770.                @procname        sysname,
  9771.                @scrollopt        int output,
  9772.                @ccopt            int output,
  9773.                @rows            int output,
  9774.                @p1                nvarchar(774) = null,
  9775.                @p2                nvarchar(774) = null,
  9776.                @p3                nvarchar(774) = null,
  9777.                @p4                nvarchar(774) = null,
  9778.                @p5                nvarchar(774) = null,
  9779.                @p6                nvarchar(774) = null,
  9780.                @p7                int = null,
  9781.                @ODBCVer         int = 2)
  9782. as
  9783.     set nocount on
  9784.     declare @ret int
  9785.  
  9786.     if @procname = 'sp_column_privileges'
  9787.     begin
  9788.         exec @ret = sp_ddopen;2 @handle output, 
  9789.                             @scrollopt output, 
  9790.                             @ccopt output, 
  9791.                             @rows output, 
  9792.                             @p1,
  9793.                             @p2,
  9794.                             @p3,
  9795.                             @p4
  9796.     end
  9797.     else if @procname = 'sp_columns' or @procname = 'sp_columns_ex'
  9798.     begin
  9799.         exec @ret = sp_ddopen;3 @handle output,
  9800.                             @procname,
  9801.                             @scrollopt output, 
  9802.                             @ccopt output, 
  9803.                             @rows output, 
  9804.                             @p1,
  9805.                             @p2,
  9806.                             @p3,
  9807.                             @p4,
  9808.                             @p5,
  9809.                             @ODBCVer
  9810.     end
  9811.     else if @procname = 'sp_datatype_info'
  9812.     begin
  9813.         exec @ret = sp_ddopen;4 @handle output,
  9814.                             @scrollopt output, 
  9815.                             @ccopt output, 
  9816.                             @rows output, 
  9817.                             @p7,
  9818.                             @ODBCVer
  9819.     end
  9820.     else if @procname = 'sp_fkeys'
  9821.     begin
  9822.         exec @ret = sp_ddopen;5 @handle output, 
  9823.                             @scrollopt output, 
  9824.                             @ccopt output, 
  9825.                             @rows output, 
  9826.                             @p1,
  9827.                             @p2,
  9828.                             @p3,
  9829.                             @p4,
  9830.                             @p5,
  9831.                             @p6
  9832.     end
  9833.     else if @procname = 'sp_pkeys'
  9834.     begin
  9835.         exec @ret = sp_ddopen;6 @handle output, 
  9836.                             @scrollopt output, 
  9837.                             @ccopt output, 
  9838.                             @rows output, 
  9839.                             @p1,
  9840.                             @p2,
  9841.                             @p3
  9842.     end
  9843.     else if @procname = 'sp_special_columns'
  9844.     begin
  9845.         exec @ret = sp_ddopen;7 @handle output, 
  9846.                             @scrollopt output, 
  9847.                             @ccopt output, 
  9848.                             @rows output, 
  9849.                             @p1,
  9850.                             @p2,
  9851.                             @p3,
  9852.                             @p4,
  9853.                             @p5,
  9854.                             @p6,
  9855.                             @ODBCVer
  9856.     end
  9857.     else if @procname = 'sp_sproc_columns'
  9858.     begin
  9859.         exec @ret = sp_ddopen;8 @handle output, 
  9860.                             @scrollopt output, 
  9861.                             @ccopt output, 
  9862.                             @rows output, 
  9863.                             @p1,
  9864.                             @p2,
  9865.                             @p3,
  9866.                             @p4,
  9867.                             @ODBCVer
  9868.     end
  9869.     else if @procname = 'sp_statistics'
  9870.     begin
  9871.         exec @ret = sp_ddopen;9 @handle output, 
  9872.                             @scrollopt output, 
  9873.                             @ccopt output, 
  9874.                             @rows output, 
  9875.                             @p1,
  9876.                             @p2,
  9877.                             @p3,
  9878.                             @p4,
  9879.                             @p5,
  9880.                             @p6
  9881.     end
  9882.     else if @procname = 'sp_stored_procedures'
  9883.     begin
  9884.         exec @ret = sp_ddopen;10 @handle output, 
  9885.                              @scrollopt output, 
  9886.                              @ccopt output, 
  9887.                              @rows output, 
  9888.                              @p1,
  9889.                              @p2,
  9890.                              @p3
  9891.     end
  9892.     else if @procname = 'sp_table_privileges'
  9893.     begin
  9894.         exec @ret = sp_ddopen;11 @handle output, 
  9895.                              @scrollopt output, 
  9896.                              @ccopt output, 
  9897.                              @rows output, 
  9898.                              @p1,
  9899.                              @p2,
  9900.                              @p3
  9901.     end
  9902.     else if @procname = 'sp_tables' or @procname = 'sp_tables_ex'
  9903.     begin
  9904.         exec @ret = sp_ddopen;12 @handle output,
  9905.                             @procname,
  9906.                             @scrollopt output, 
  9907.                             @ccopt output, 
  9908.                             @rows output, 
  9909.                             @p1,
  9910.                             @p2,
  9911.                             @p3,
  9912.                             @p4,
  9913.                             @p5
  9914.     end
  9915.     else
  9916.         print 'Unknown sp_ddopen procedure'
  9917.     select @ret = isnull(@ret,0)
  9918.     return isnull(@ret,0)
  9919. go
  9920.  
  9921.  
  9922. if (charindex('7.00', @@version) = 0 and
  9923.     charindex('8.00', @@version) = 0)
  9924. begin
  9925.     print ''
  9926.     print ''
  9927.     print 'Warning:'
  9928.     print 'you are installing the stored procedures '
  9929.     print 'on a pre 7.0 SQL Server.'
  9930.     print 'Ignore the following errors.'
  9931. end
  9932. go
  9933.  
  9934. create procedure sp_ddopen; 2(
  9935.                @handle            int output,
  9936.                @scrollopt        int output,
  9937.                @ccopt            int output,
  9938.                @rows            int output,
  9939.                @p1                nvarchar(774),
  9940.                @p2                nvarchar(774),
  9941.                @p3                nvarchar(774),
  9942.                @p4                nvarchar(774))
  9943. as
  9944.     set nocount on
  9945.     declare @ret int
  9946.  
  9947.     create table #spcolpriv (
  9948.         TABLE_QUALIFIER sysname null,
  9949.         TABLE_OWNER sysname null,
  9950.         TABLE_NAME sysname not null,
  9951.         COLUMN_NAME sysname not null,
  9952.         GRANTOR sysname null,
  9953.         GRANTEE sysname not null,
  9954.         PRIVILEGE varchar(32) not null,
  9955.         IS_GRANTABLE varchar(3) null
  9956.         )
  9957.     insert into #spcolpriv exec sp_column_privileges @p1,@p2,@p3,@p4
  9958.     exec @ret = sp_cursoropen @handle output,
  9959.         'select * from #spcolpriv',
  9960.         @scrollopt output, @ccopt output, @rows output
  9961.     drop table #spcolpriv
  9962.     return @ret
  9963. go
  9964.  
  9965.  
  9966. if (charindex('7.00', @@version) = 0 and
  9967.     charindex('8.00', @@version) = 0)
  9968. begin
  9969.     print ''
  9970.     print ''
  9971.     print 'Warning:'
  9972.     print 'you are installing the stored procedures '
  9973.     print 'on a pre 7.0 SQL Server.'
  9974.     print 'Ignore the following errors.'
  9975. end
  9976. go
  9977.  
  9978. create procedure sp_ddopen; 3(
  9979.                @handle            int output,
  9980.                @procname        sysname,
  9981.                @scrollopt        int output,
  9982.                @ccopt            int output,
  9983.                @rows            int output,
  9984.                @p1                nvarchar(774),
  9985.                @p2                nvarchar(774),
  9986.                @p3                nvarchar(774),
  9987.                @p4                nvarchar(774),
  9988.                @p5                nvarchar(774),
  9989.                @ODBCVer         int)
  9990. as
  9991.     set nocount on
  9992.     declare @ret int
  9993.  
  9994.     create table #spcolumns (
  9995.         TABLE_QUALIFIER sysname null,
  9996.         TABLE_OWNER sysname null,
  9997.         TABLE_NAME sysname not null,
  9998.         COLUMN_NAME sysname not null,
  9999.         DATA_TYPE smallint not null,
  10000.         TYPE_NAME sysname not null,
  10001.         "PRECISION" int null,
  10002.         LENGTH int null,
  10003.         SCALE smallint null,
  10004.         RADIX smallint null,
  10005.         NULLABLE smallint not null,
  10006.         REMARKS varchar(254) null,
  10007.         COLUMN_DEF nvarchar(3000) null,
  10008.         SQL_DATA_TYPE smallint not null,
  10009.         SQL_DATETIME_SUB smallint null,
  10010.         CHAR_OCTET_LENGTH int null,
  10011.         ORDINAL_POSITION int not null,
  10012.         IS_NULLABLE varchar(254) null,
  10013.         SS_DATA_TYPE tinyint null
  10014.         )
  10015.  
  10016.     if @procname = 'sp_columns'
  10017.     begin
  10018.         insert into #spcolumns exec sp_columns @p1,@p2,@p3,@p4,@ODBCVer
  10019.     end
  10020.     else
  10021.     begin
  10022.         insert into #spcolumns exec sp_columns_ex @p1,@p2,@p3,@p4,@p5,@ODBCVer
  10023.     end
  10024.     exec @ret = sp_cursoropen @handle output,
  10025.         'select * from #spcolumns',
  10026.         @scrollopt output, @ccopt output, @rows output
  10027.     drop table #spcolumns
  10028.     return @ret
  10029. go
  10030.  
  10031.  
  10032.  
  10033. if (charindex('7.00', @@version) = 0 and
  10034.     charindex('8.00', @@version) = 0)
  10035. begin
  10036.     print ''
  10037.     print ''
  10038.     print 'Warning:'
  10039.     print 'you are installing the stored procedures '
  10040.     print 'on a pre 7.0 SQL Server.'
  10041.     print 'Ignore the following errors.'
  10042. end
  10043. go
  10044.  
  10045. create procedure sp_ddopen; 4(
  10046.                @handle            int output,
  10047.                @scrollopt        int output,
  10048.                @ccopt            int output,
  10049.                @rows            int output,
  10050.                @p7                int,
  10051.                @ODBCVer         int)
  10052. as
  10053.     set nocount on
  10054.     declare @ret int
  10055.  
  10056.     create table #spdatatypeinfo (
  10057.         TYPE_NAME            sysname  not null,
  10058.         DATA_TYPE            smallint not null,
  10059.         "PRECISION"            int null,
  10060.         LITERAL_PREFIX        varchar(32)    null,
  10061.         LITERAL_SUFFIX        varchar(32)    null,
  10062.         CREATE_PARAMS        varchar(32)    null,
  10063.         NULLABLE            smallint   not null,
  10064.         CASE_SENSITIVE        smallint   not null,
  10065.         SEARCHABLE            smallint   not null,
  10066.         UNSIGNED_ATTRIBUTE    smallint   null,
  10067.         MONEY    smallint    not null,
  10068.         AUTO_INCREMENT        smallint    null,
  10069.         LOCAL_TYPE_NAME     sysname null,
  10070.         MINIMUM_SCALE        smallint     null,
  10071.         MAXIMUM_SCALE        smallint   null,
  10072.         SQL_DATA_TYPE        smallint      not null,
  10073.         SQL_DATETIME_SUB    smallint   null,
  10074.         NUM_PREC_RADIX        int     null,
  10075.         INTERVAL_PRECISION    smallint    NULL,
  10076.         USERTYPE            smallint not null)
  10077.  
  10078.     insert into #spdatatypeinfo exec sp_datatype_info @p7,@ODBCVer
  10079.     exec @ret = sp_cursoropen @handle output,
  10080.         'select * from #spdatatypeinfo',
  10081.         @scrollopt output, @ccopt output, @rows output
  10082.     drop table #spdatatypeinfo
  10083.     return @ret
  10084. go
  10085.  
  10086.  
  10087.  
  10088. if (charindex('7.00', @@version) = 0 and
  10089.     charindex('8.00', @@version) = 0)
  10090. begin
  10091.     print ''
  10092.     print ''
  10093.     print 'Warning:'
  10094.     print 'you are installing the stored procedures '
  10095.     print 'on a pre 7.0 SQL Server.'
  10096.     print 'Ignore the following errors.'
  10097. end
  10098. go
  10099.  
  10100. create procedure sp_ddopen; 5(
  10101.                @handle            int output,
  10102.                @scrollopt        int output,
  10103.                @ccopt            int output,
  10104.                @rows            int output,
  10105.                @p1                nvarchar(774),
  10106.                @p2                nvarchar(774),
  10107.                @p3                nvarchar(774),
  10108.                @p4                nvarchar(774),
  10109.                @p5                nvarchar(774),
  10110.                @p6                nvarchar(774))
  10111. as
  10112.     set nocount on
  10113.     declare @ret int
  10114.  
  10115.     create table #spfkeys (
  10116.         PKTABLE_QUALIFIER sysname     null,
  10117.         PKTABLE_OWNER sysname    null,
  10118.         PKTABLE_NAME sysname  not null,
  10119.         PKCOLUMN_NAME sysname  not null,
  10120.         FKTABLE_QUALIFIER sysname    null,
  10121.         FKTABLE_OWNER sysname    null,
  10122.         FKTABLE_NAME sysname  not null,
  10123.         FKCOLUMN_NAME sysname  not null,
  10124.         KEY_SEQ smallint not null,
  10125.         UPDATE_RULE smallint null,
  10126.         DELETE_RULE smallint null,
  10127.         FK_NAME sysname null,
  10128.         PK_NAME sysname null,
  10129.         DEFERRABILITY smallint null
  10130.         )
  10131.     insert into #spfkeys exec sp_fkeys @p1,@p2,@p3,@p4,@p5,@p6
  10132.     exec @ret = sp_cursoropen @handle output,
  10133.         'select * from #spfkeys',
  10134.         @scrollopt output, @ccopt output, @rows output
  10135.     drop table #spfkeys
  10136.     return @ret
  10137. go
  10138.  
  10139.  
  10140.  
  10141. if (charindex('7.00', @@version) = 0 and
  10142.     charindex('8.00', @@version) = 0)
  10143. begin
  10144.     print ''
  10145.     print ''
  10146.     print 'Warning:'
  10147.     print 'you are installing the stored procedures '
  10148.     print 'on a pre 7.0 SQL Server.'
  10149.     print 'Ignore the following errors.'
  10150. end
  10151. go
  10152.  
  10153. create procedure sp_ddopen; 6(
  10154.                @handle            int output,
  10155.                @scrollopt        int output,
  10156.                @ccopt            int output,
  10157.                @rows            int output,
  10158.                @p1                nvarchar(774),
  10159.                @p2                nvarchar(774),
  10160.                @p3                nvarchar(774))
  10161. as
  10162.     set nocount on
  10163.     declare @ret int
  10164.  
  10165.     create table #sppkeys (
  10166.         TABLE_QUALIFIER sysname   null,
  10167.         TABLE_OWNER sysname   null,
  10168.         TABLE_NAME sysname    not null,
  10169.         COLUMN_NAME sysname  not null,
  10170.         KEY_SEQ smallint not null,
  10171.         PK_NAME sysname null
  10172.         )
  10173.     insert into #sppkeys exec sp_pkeys @p1,@p2,@p3
  10174.     exec @ret = sp_cursoropen @handle output,
  10175.         'select * from #sppkeys',
  10176.         @scrollopt output, @ccopt output, @rows output
  10177.     drop table #sppkeys
  10178.     return @ret
  10179. go
  10180.  
  10181.  
  10182.  
  10183. if (charindex('7.00', @@version) = 0 and
  10184.     charindex('8.00', @@version) = 0)
  10185. begin
  10186.     print ''
  10187.     print ''
  10188.     print 'Warning:'
  10189.     print 'you are installing the stored procedures '
  10190.     print 'on a pre 7.0 SQL Server.'
  10191.     print 'Ignore the following errors.'
  10192. end
  10193. go
  10194.  
  10195. create procedure sp_ddopen; 7(
  10196.                @handle            int output,
  10197.                @scrollopt        int output,
  10198.                @ccopt            int output,
  10199.                @rows            int output,
  10200.                @p1                nvarchar(774),
  10201.                @p2                nvarchar(774),
  10202.                @p3                nvarchar(774),
  10203.                @p4                nvarchar(774),
  10204.                @p5                nvarchar(774),
  10205.                @p6                nvarchar(774),
  10206.                @ODBCVer         int)
  10207. as
  10208.     set nocount on
  10209.     declare @ret int
  10210.  
  10211.     create table #spspeccol (
  10212.         SCOPE smallint null,
  10213.         COLUMN_NAME sysname not null,
  10214.         DATA_TYPE smallint not null,
  10215.         TYPE_NAME sysname not null,
  10216.         "PRECISION" int null,
  10217.         LENGTH int null,
  10218.         SCALE smallint null,
  10219.         PSEUDO_COLUMN smallint null
  10220.         )
  10221.     insert into #spspeccol exec sp_special_columns @p1,@p2,@p3,@p4,@p5,@p6,@ODBCVer
  10222.     exec @ret = sp_cursoropen @handle output,
  10223.         'select * from #spspeccol',
  10224.         @scrollopt output, @ccopt output, @rows output
  10225.     drop table #spspeccol
  10226.     return @ret
  10227. go
  10228.  
  10229.  
  10230.  
  10231. if (charindex('7.00', @@version) = 0 and
  10232.     charindex('8.00', @@version) = 0)
  10233. begin
  10234.     print ''
  10235.     print ''
  10236.     print 'Warning:'
  10237.     print 'you are installing the stored procedures '
  10238.     print 'on a pre 7.0 SQL Server.'
  10239.     print 'Ignore the following errors.'
  10240. end
  10241. go
  10242.  
  10243. create procedure sp_ddopen; 8(
  10244.                @handle            int output,
  10245.                @scrollopt        int output,
  10246.                @ccopt            int output,
  10247.                @rows            int output,
  10248.                @p1                nvarchar(774),
  10249.                @p2                nvarchar(774),
  10250.                @p3                nvarchar(774),
  10251.                @p4                nvarchar(774),
  10252.                @ODBCVer         int)
  10253. as
  10254.     set nocount on
  10255.     declare @ret int
  10256.  
  10257.     create table #spproccol (
  10258.         PROCEDURE_QUALIFIER sysname  null,
  10259.         PROCEDURE_OWNER sysname  null,
  10260.         PROCEDURE_NAME sysname not null,
  10261.         COLUMN_NAME sysname not null,
  10262.         COLUMN_TYPE smallint not null,
  10263.         DATA_TYPE smallint not null,
  10264.         TYPE_NAME sysname not null,
  10265.         "PRECISION" int null,
  10266.         LENGTH int null,
  10267.         SCALE smallint null,
  10268.         RADIX smallint null,
  10269.         NULLABLE smallint not null,
  10270.         REMARKS varchar(254) null,
  10271.         COLUMN_DEF nvarchar(3000) null,
  10272.         SQL_DATA_TYPE smallint not null,
  10273.         SQL_DATETIME_SUB smallint null,
  10274.         CHAR_OCTET_LENGTH int null,
  10275.         ORDINAL_POSITION int not null,
  10276.         IS_NULLABLE varchar(254) null,
  10277.         SS_DATA_TYPE tinyint null
  10278.         )
  10279.     insert into #spproccol exec sp_sproc_columns @p1,@p2,@p3,@p4,@ODBCVer
  10280.     exec @ret = sp_cursoropen @handle output,
  10281.         'select * from #spproccol',
  10282.         @scrollopt output, @ccopt output, @rows output
  10283.     drop table #spproccol
  10284.     return @ret
  10285. go
  10286.  
  10287.  
  10288.  
  10289. if (charindex('7.00', @@version) = 0 and
  10290.     charindex('8.00', @@version) = 0)
  10291. begin
  10292.     print ''
  10293.     print ''
  10294.     print 'Warning:'
  10295.     print 'you are installing the stored procedures '
  10296.     print 'on a pre 7.0 SQL Server.'
  10297.     print 'Ignore the following errors.'
  10298. end
  10299. go
  10300.  
  10301. create procedure sp_ddopen; 9(
  10302.                @handle            int output,
  10303.                @scrollopt        int output,
  10304.                @ccopt            int output,
  10305.                @rows            int output,
  10306.                @p1                nvarchar(774),
  10307.                @p2                nvarchar(774),
  10308.                @p3                nvarchar(774),
  10309.                @p4                nvarchar(774),
  10310.                @p5                nvarchar(774),
  10311.                @p6                nvarchar(774))
  10312. as
  10313.     set nocount on
  10314.     declare @ret int
  10315.  
  10316.     create table #spstatistics (
  10317.         TABLE_QUALIFIER sysname   null,
  10318.         TABLE_OWNER sysname   null,
  10319.         TABLE_NAME sysname    not null,
  10320.         NON_UNIQUE smallint null,
  10321.         INDEX_QUALIFIER sysname null,
  10322.         INDEX_NAME sysname null,
  10323.         TYPE smallint not null,
  10324.         SEQ_IN_INDEX smallint null,
  10325.         COLUMN_NAME sysname null,
  10326.         COLLATION char(1) null,
  10327.         CARDINALITY int null,
  10328.         PAGES int null,
  10329.         FILTER_CONDITION varchar(128) null
  10330.         )
  10331.     insert into #spstatistics exec sp_statistics @p1,@p2,@p3,@p4,@p5,@p6
  10332.     exec @ret = sp_cursoropen @handle output,
  10333.         'select * from #spstatistics',
  10334.         @scrollopt output, @ccopt output, @rows output
  10335.     drop table #spstatistics
  10336.     return @ret
  10337. go
  10338.                
  10339.  
  10340.  
  10341. if (charindex('7.00', @@version) = 0 and
  10342.     charindex('8.00', @@version) = 0)
  10343. begin
  10344.     print ''
  10345.     print ''
  10346.     print 'Warning:'
  10347.     print 'you are installing the stored procedures '
  10348.     print 'on a pre 7.0 SQL Server.'
  10349.     print 'Ignore the following errors.'
  10350. end
  10351. go
  10352.  
  10353. create procedure sp_ddopen; 10(
  10354.                @handle            int output,
  10355.                @scrollopt        int output,
  10356.                @ccopt            int output,
  10357.                @rows            int output,
  10358.                @p1                nvarchar(774),
  10359.                @p2                nvarchar(774),
  10360.                @p3                nvarchar(774))
  10361. as
  10362.     set nocount on
  10363.     declare @ret int
  10364.  
  10365.     create table #spprocedures (
  10366.         PROCEDURE_QUALIFIER sysname  null,
  10367.         PROCEDURE_OWNER sysname  null,
  10368.         PROCEDURE_NAME sysname not null,
  10369.         NUM_INPUT_PARAMS int null,
  10370.         NUM_OUTPUT_PARAMS int null,
  10371.         NUM_RESULT_SETS int null,
  10372.         REMARKS varchar(254) null,
  10373.         PROCEDURE_TYPE smallint null
  10374.         )
  10375.     insert into #spprocedures exec sp_stored_procedures @p1,@p2,@p3
  10376.     exec @ret = sp_cursoropen @handle output,
  10377.         'select * from #spprocedures',
  10378.         @scrollopt output, @ccopt output, @rows output
  10379.     drop table #spprocedures
  10380.     return @ret
  10381. go
  10382.  
  10383.  
  10384.  
  10385. if (charindex('7.00', @@version) = 0 and
  10386.     charindex('8.00', @@version) = 0)
  10387. begin
  10388.     print ''
  10389.     print ''
  10390.     print 'Warning:'
  10391.     print 'you are installing the stored procedures '
  10392.     print 'on a pre 7.0 SQL Server.'
  10393.     print 'Ignore the following errors.'
  10394. end
  10395. go
  10396.  
  10397. create procedure sp_ddopen; 11(
  10398.                @handle            int output,
  10399.                @scrollopt        int output,
  10400.                @ccopt            int output,
  10401.                @rows            int output,
  10402.                @p1                nvarchar(774),
  10403.                @p2                nvarchar(774),
  10404.                @p3                nvarchar(774))
  10405. as
  10406.     set nocount on
  10407.     declare @ret int
  10408.  
  10409.     create table #sptabpriv (
  10410.         TABLE_QUALIFIER sysname null,
  10411.         TABLE_OWNER sysname null,
  10412.         TABLE_NAME sysname not null,
  10413.         GRANTOR sysname null,
  10414.         GRANTEE sysname not null,
  10415.         PRIVILEGE varchar(32) not null,
  10416.         IS_GRANTABLE varchar(3) null
  10417.         )
  10418.     insert into #sptabpriv exec sp_table_privileges @p1,@p2,@p3
  10419.     exec @ret = sp_cursoropen @handle output,
  10420.         'select * from #sptabpriv',
  10421.         @scrollopt output, @ccopt output, @rows output
  10422.     drop table #sptabpriv
  10423.     return @ret
  10424. go
  10425.  
  10426.  
  10427.  
  10428. if (charindex('7.00', @@version) = 0 and
  10429.     charindex('8.00', @@version) = 0)
  10430. begin
  10431.     print ''
  10432.     print ''
  10433.     print 'Warning:'
  10434.     print 'you are installing the stored procedures '
  10435.     print 'on a pre 7.0 SQL Server.'
  10436.     print 'Ignore the following errors.'
  10437. end
  10438. go
  10439.  
  10440. create procedure sp_ddopen; 12(
  10441.                @handle            int output,
  10442.                @procname        sysname,
  10443.                @scrollopt        int output,
  10444.                @ccopt            int output,
  10445.                @rows            int output,
  10446.                @p1                nvarchar(774),
  10447.                @p2                nvarchar(774),
  10448.                @p3                nvarchar(774),
  10449.                @p4                nvarchar(774),
  10450.                @p5                nvarchar(774))
  10451. as
  10452.     set nocount on
  10453.     declare @ret int
  10454.  
  10455.     create table #sptables (
  10456.         TABLE_QUALIFIER sysname null,
  10457.         TABLE_OWNER sysname null,
  10458.         TABLE_NAME sysname null,
  10459.         TABLE_TYPE    varchar(32) null,
  10460.         REMARKS varchar(254) null)
  10461.     if @procname = 'sp_tables'
  10462.     begin
  10463.         insert into #sptables exec sp_tables @p1,@p2,@p3,@p4
  10464.     end
  10465.     else
  10466.     begin
  10467.         insert into #sptables exec sp_tables_ex @p1,@p2,@p3,@p4,@p5
  10468.     end
  10469.     exec @ret = sp_cursoropen @handle output,
  10470.         'select * from #sptables',
  10471.         @scrollopt output, @ccopt output, @rows output
  10472.     drop table #sptables
  10473.     return @ret
  10474. go
  10475.  
  10476. if (charindex('8.00', @@version) > 0)
  10477.     drop procedure sp_ddopen
  10478. else
  10479. begin
  10480.     print ''
  10481.     print ''
  10482.     print 'Warning:'
  10483.     print 'you are installing the stored procedures '
  10484.     print 'on a pre 8.0 SQL Server.'
  10485.     print 'Ignore the following errors.'
  10486. end
  10487. go
  10488.  
  10489. /*    Procedure for 8.0 server */
  10490. create procedure sp_ddopen; 1(
  10491.                @handle            int output,
  10492.                @procname        sysname,
  10493.                @scrollopt        int output,
  10494.                @ccopt            int output,
  10495.                @rows            int output,
  10496.                @p1                nvarchar(774) = null,
  10497.                @p2                nvarchar(774) = null,
  10498.                @p3                nvarchar(774) = null,
  10499.                @p4                nvarchar(774) = null,
  10500.                @p5                nvarchar(774) = null,
  10501.                @p6                nvarchar(774) = null,
  10502.                @p7                int = null,
  10503.                @ODBCVer         int = 2)
  10504. as
  10505.     set nocount on
  10506.     declare @ret int
  10507.  
  10508.     if @procname = 'sp_column_privileges'
  10509.     begin
  10510.         exec @ret = sp_ddopen;2 @handle output, 
  10511.                             @scrollopt output, 
  10512.                             @ccopt output, 
  10513.                             @rows output, 
  10514.                             @p1,
  10515.                             @p2,
  10516.                             @p3,
  10517.                             @p4
  10518.     end
  10519.     else if @procname = 'sp_columns' or @procname = 'sp_columns_ex'
  10520.     begin
  10521.         exec @ret = sp_ddopen;3 @handle output,
  10522.                             @procname,
  10523.                             @scrollopt output, 
  10524.                             @ccopt output, 
  10525.                             @rows output, 
  10526.                             @p1,
  10527.                             @p2,
  10528.                             @p3,
  10529.                             @p4,
  10530.                             @p5,
  10531.                             @ODBCVer
  10532.     end
  10533.     else if @procname = 'sp_datatype_info'
  10534.     begin
  10535.         exec @ret = sp_ddopen;4 @handle output,
  10536.                             @scrollopt output, 
  10537.                             @ccopt output, 
  10538.                             @rows output, 
  10539.                             @p7,
  10540.                             @ODBCVer
  10541.     end
  10542.     else if @procname = 'sp_fkeys'
  10543.     begin
  10544.         exec @ret = sp_ddopen;5 @handle output, 
  10545.                             @scrollopt output, 
  10546.                             @ccopt output, 
  10547.                             @rows output, 
  10548.                             @p1,
  10549.                             @p2,
  10550.                             @p3,
  10551.                             @p4,
  10552.                             @p5,
  10553.                             @p6
  10554.     end
  10555.     else if @procname = 'sp_pkeys'
  10556.     begin
  10557.         exec @ret = sp_ddopen;6 @handle output, 
  10558.                             @scrollopt output, 
  10559.                             @ccopt output, 
  10560.                             @rows output, 
  10561.                             @p1,
  10562.                             @p2,
  10563.                             @p3
  10564.     end
  10565.     else if @procname = 'sp_special_columns'
  10566.     begin
  10567.         exec @ret = sp_ddopen;7 @handle output, 
  10568.                             @scrollopt output, 
  10569.                             @ccopt output, 
  10570.                             @rows output, 
  10571.                             @p1,
  10572.                             @p2,
  10573.                             @p3,
  10574.                             @p4,
  10575.                             @p5,
  10576.                             @p6,
  10577.                             @ODBCVer
  10578.     end
  10579.     else if @procname = 'sp_sproc_columns'
  10580.     begin
  10581.         exec @ret = sp_ddopen;8 @handle output, 
  10582.                             @scrollopt output, 
  10583.                             @ccopt output, 
  10584.                             @rows output, 
  10585.                             @p1,
  10586.                             @p2,
  10587.                             @p3,
  10588.                             @p4,
  10589.                             @ODBCVer
  10590.     end
  10591.     else if @procname = 'sp_statistics'
  10592.     begin
  10593.         exec @ret = sp_ddopen;9 @handle output, 
  10594.                             @scrollopt output, 
  10595.                             @ccopt output, 
  10596.                             @rows output, 
  10597.                             @p1,
  10598.                             @p2,
  10599.                             @p3,
  10600.                             @p4,
  10601.                             @p5,
  10602.                             @p6
  10603.     end
  10604.     else if @procname = 'sp_stored_procedures'
  10605.     begin
  10606.         exec @ret = sp_ddopen;10 @handle output, 
  10607.                              @scrollopt output, 
  10608.                              @ccopt output, 
  10609.                              @rows output, 
  10610.                              @p1,
  10611.                              @p2,
  10612.                              @p3
  10613.     end
  10614.     else if @procname = 'sp_table_privileges'
  10615.     begin
  10616.         exec @ret = sp_ddopen;11 @handle output, 
  10617.                              @scrollopt output, 
  10618.                              @ccopt output, 
  10619.                              @rows output, 
  10620.                              @p1,
  10621.                              @p2,
  10622.                              @p3
  10623.     end
  10624.     else if @procname = 'sp_tables' or @procname = 'sp_tables_ex'
  10625.     begin
  10626.         exec @ret = sp_ddopen;12 @handle output,
  10627.                             @procname,
  10628.                             @scrollopt output, 
  10629.                             @ccopt output, 
  10630.                             @rows output, 
  10631.                             @p1,
  10632.                             @p2,
  10633.                             @p3,
  10634.                             @p4,
  10635.                             @p5
  10636.     end
  10637.     else
  10638.         print 'Unknown sp_ddopen procedure'
  10639.     select @ret = isnull(@ret,0)
  10640.     return isnull(@ret,0)
  10641. go
  10642.  
  10643.  
  10644. if (charindex('8.00', @@version) = 0)
  10645. begin
  10646.     print ''
  10647.     print ''
  10648.     print 'Warning:'
  10649.     print 'you are installing the stored procedures '
  10650.     print 'on a pre 8.0 SQL Server.'
  10651.     print 'Ignore the following errors.'
  10652. end
  10653. go
  10654.  
  10655. create procedure sp_ddopen; 2(
  10656.                @handle            int output,
  10657.                @scrollopt        int output,
  10658.                @ccopt            int output,
  10659.                @rows            int output,
  10660.                @p1                nvarchar(774),
  10661.                @p2                nvarchar(774),
  10662.                @p3                nvarchar(774),
  10663.                @p4                nvarchar(774))
  10664. as
  10665.     set nocount on
  10666.     declare @ret int
  10667.  
  10668.     create table #spcolpriv (
  10669.         TABLE_QUALIFIER sysname collate database_default null,
  10670.         TABLE_OWNER sysname collate database_default null,
  10671.         TABLE_NAME sysname collate database_default not null,
  10672.         COLUMN_NAME sysname collate database_default not null,
  10673.         GRANTOR sysname collate database_default null,
  10674.         GRANTEE sysname collate database_default not null,
  10675.         PRIVILEGE varchar(32) collate database_default not null,
  10676.         IS_GRANTABLE varchar(3) collate database_default null
  10677.         )
  10678.     insert into #spcolpriv exec sp_column_privileges @p1,@p2,@p3,@p4
  10679.     exec @ret = sp_cursoropen @handle output,
  10680.         'select * from #spcolpriv',
  10681.         @scrollopt output, @ccopt output, @rows output
  10682.     drop table #spcolpriv
  10683.     return @ret
  10684. go
  10685.  
  10686.  
  10687. if (charindex('8.00', @@version) = 0)
  10688. begin
  10689.     print ''
  10690.     print ''
  10691.     print 'Warning:'
  10692.     print 'you are installing the stored procedures '
  10693.     print 'on a pre 8.0 SQL Server.'
  10694.     print 'Ignore the following errors.'
  10695. end
  10696. go
  10697.  
  10698. create procedure sp_ddopen; 3(
  10699.                @handle            int output,
  10700.                @procname        sysname,
  10701.                @scrollopt        int output,
  10702.                @ccopt            int output,
  10703.                @rows            int output,
  10704.                @p1                nvarchar(774),
  10705.                @p2                nvarchar(774),
  10706.                @p3                nvarchar(774),
  10707.                @p4                nvarchar(774),
  10708.                @p5                nvarchar(774),
  10709.                @ODBCVer         int)
  10710. as
  10711.     set nocount on
  10712.     declare @ret int
  10713.  
  10714.     create table #spcolumns (
  10715.         TABLE_QUALIFIER sysname collate database_default null,
  10716.         TABLE_OWNER sysname collate database_default null,
  10717.         TABLE_NAME sysname collate database_default not null,
  10718.         COLUMN_NAME sysname collate database_default not null,
  10719.         DATA_TYPE smallint not null,
  10720.         TYPE_NAME sysname collate database_default not null,
  10721.         "PRECISION" int null,
  10722.         LENGTH int null,
  10723.         SCALE smallint null,
  10724.         RADIX smallint null,
  10725.         NULLABLE smallint not null,
  10726.         REMARKS varchar(254) collate database_default null,
  10727.         COLUMN_DEF nvarchar(3000) collate database_default null,
  10728.         SQL_DATA_TYPE smallint null,
  10729.         SQL_DATETIME_SUB smallint null,
  10730.         CHAR_OCTET_LENGTH int null,
  10731.         ORDINAL_POSITION int not null,
  10732.         IS_NULLABLE varchar(254) collate database_default null,
  10733.         SS_DATA_TYPE tinyint null
  10734.         )
  10735.  
  10736.     if @procname = 'sp_columns'
  10737.     begin
  10738.         insert into #spcolumns exec sp_columns @p1,@p2,@p3,@p4,@ODBCVer
  10739.     end
  10740.     else
  10741.     begin
  10742.         insert into #spcolumns exec sp_columns_ex @p1,@p2,@p3,@p4,@p5,@ODBCVer
  10743.     end
  10744.     exec @ret = sp_cursoropen @handle output,
  10745.         'select * from #spcolumns',
  10746.         @scrollopt output, @ccopt output, @rows output
  10747.     drop table #spcolumns
  10748.     return @ret
  10749. go
  10750.  
  10751.  
  10752.  
  10753. if (charindex('8.00', @@version) = 0)
  10754. begin
  10755.     print ''
  10756.     print ''
  10757.     print 'Warning:'
  10758.     print 'you are installing the stored procedures '
  10759.     print 'on a pre 8.0 SQL Server.'
  10760.     print 'Ignore the following errors.'
  10761. end
  10762. go
  10763.  
  10764. create procedure sp_ddopen; 4(
  10765.                @handle            int output,
  10766.                @scrollopt        int output,
  10767.                @ccopt            int output,
  10768.                @rows            int output,
  10769.                @p7                int,
  10770.                @ODBCVer         int)
  10771. as
  10772.     set nocount on
  10773.     declare @ret int
  10774.  
  10775.     create table #spdatatypeinfo (
  10776.         TYPE_NAME            sysname  collate database_default not null,
  10777.         DATA_TYPE            smallint not null,
  10778.         "PRECISION"            int null,
  10779.         LITERAL_PREFIX        varchar(32)    collate database_default null,
  10780.         LITERAL_SUFFIX        varchar(32)    collate database_default null,
  10781.         CREATE_PARAMS        varchar(32)    collate database_default null,
  10782.         NULLABLE            smallint   not null,
  10783.         CASE_SENSITIVE        smallint   not null,
  10784.         SEARCHABLE            smallint   not null,
  10785.         UNSIGNED_ATTRIBUTE    smallint   null,
  10786.         MONEY    smallint    not null,
  10787.         AUTO_INCREMENT        smallint    null,
  10788.         LOCAL_TYPE_NAME     sysname collate database_default null,
  10789.         MINIMUM_SCALE        smallint     null,
  10790.         MAXIMUM_SCALE        smallint   null,
  10791.         SQL_DATA_TYPE        smallint      not null,
  10792.         SQL_DATETIME_SUB    smallint   null,
  10793.         NUM_PREC_RADIX        int     null,
  10794.         INTERVAL_PRECISION    smallint    NULL,
  10795.         USERTYPE            smallint not null)
  10796.  
  10797.     insert into #spdatatypeinfo exec sp_datatype_info @p7,@ODBCVer
  10798.     exec @ret = sp_cursoropen @handle output,
  10799.         'select * from #spdatatypeinfo',
  10800.         @scrollopt output, @ccopt output, @rows output
  10801.     drop table #spdatatypeinfo
  10802.     return @ret
  10803. go
  10804.  
  10805.  
  10806.  
  10807. if (charindex('8.00', @@version) = 0)
  10808. begin
  10809.     print ''
  10810.     print ''
  10811.     print 'Warning:'
  10812.     print 'you are installing the stored procedures '
  10813.     print 'on a pre 8.0 SQL Server.'
  10814.     print 'Ignore the following errors.'
  10815. end
  10816. go
  10817.  
  10818. create procedure sp_ddopen; 5(
  10819.                @handle            int output,
  10820.                @scrollopt        int output,
  10821.                @ccopt            int output,
  10822.                @rows            int output,
  10823.                @p1                nvarchar(774),
  10824.                @p2                nvarchar(774),
  10825.                @p3                nvarchar(774),
  10826.                @p4                nvarchar(774),
  10827.                @p5                nvarchar(774),
  10828.                @p6                nvarchar(774))
  10829. as
  10830.     set nocount on
  10831.     declare @ret int
  10832.  
  10833.     create table #spfkeys (
  10834.         PKTABLE_QUALIFIER sysname     collate database_default null,
  10835.         PKTABLE_OWNER sysname    collate database_default null,
  10836.         PKTABLE_NAME sysname  collate database_default not null,
  10837.         PKCOLUMN_NAME sysname  collate database_default not null,
  10838.         FKTABLE_QUALIFIER sysname    collate database_default null,
  10839.         FKTABLE_OWNER sysname    collate database_default null,
  10840.         FKTABLE_NAME sysname  collate database_default not null,
  10841.         FKCOLUMN_NAME sysname  collate database_default not null,
  10842.         KEY_SEQ smallint not null,
  10843.         UPDATE_RULE smallint null,
  10844.         DELETE_RULE smallint null,
  10845.         FK_NAME sysname collate database_default null,
  10846.         PK_NAME sysname collate database_default null,
  10847.         DEFERRABILITY smallint null
  10848.         )
  10849.     insert into #spfkeys exec sp_fkeys @p1,@p2,@p3,@p4,@p5,@p6
  10850.     exec @ret = sp_cursoropen @handle output,
  10851.         'select * from #spfkeys',
  10852.         @scrollopt output, @ccopt output, @rows output
  10853.     drop table #spfkeys
  10854.     return @ret
  10855. go
  10856.  
  10857.  
  10858.  
  10859. if (charindex('8.00', @@version) = 0)
  10860. begin
  10861.     print ''
  10862.     print ''
  10863.     print 'Warning:'
  10864.     print 'you are installing the stored procedures '
  10865.     print 'on a pre 8.0 SQL Server.'
  10866.     print 'Ignore the following errors.'
  10867. end
  10868. go
  10869.  
  10870. create procedure sp_ddopen; 6(
  10871.                @handle            int output,
  10872.                @scrollopt        int output,
  10873.                @ccopt            int output,
  10874.                @rows            int output,
  10875.                @p1                nvarchar(774),
  10876.                @p2                nvarchar(774),
  10877.                @p3                nvarchar(774))
  10878. as
  10879.     set nocount on
  10880.     declare @ret int
  10881.  
  10882.     create table #sppkeys (
  10883.         TABLE_QUALIFIER sysname   collate database_default null,
  10884.         TABLE_OWNER sysname   collate database_default null,
  10885.         TABLE_NAME sysname    collate database_default not null,
  10886.         COLUMN_NAME sysname  collate database_default not null,
  10887.         KEY_SEQ smallint not null,
  10888.         PK_NAME sysname collate database_default null
  10889.         )
  10890.     insert into #sppkeys exec sp_pkeys @p1,@p2,@p3
  10891.     exec @ret = sp_cursoropen @handle output,
  10892.         'select * from #sppkeys',
  10893.         @scrollopt output, @ccopt output, @rows output
  10894.     drop table #sppkeys
  10895.     return @ret
  10896. go
  10897.  
  10898.  
  10899.  
  10900. if (charindex('8.00', @@version) = 0)
  10901. begin
  10902.     print ''
  10903.     print ''
  10904.     print 'Warning:'
  10905.     print 'you are installing the stored procedures '
  10906.     print 'on a pre 8.0 SQL Server.'
  10907.     print 'Ignore the following errors.'
  10908. end
  10909. go
  10910.  
  10911. create procedure sp_ddopen; 7(
  10912.                @handle            int output,
  10913.                @scrollopt        int output,
  10914.                @ccopt            int output,
  10915.                @rows            int output,
  10916.                @p1                nvarchar(774),
  10917.                @p2                nvarchar(774),
  10918.                @p3                nvarchar(774),
  10919.                @p4                nvarchar(774),
  10920.                @p5                nvarchar(774),
  10921.                @p6                nvarchar(774),
  10922.                @ODBCVer         int)
  10923. as
  10924.     set nocount on
  10925.     declare @ret int
  10926.  
  10927.     create table #spspeccol (
  10928.         SCOPE smallint null,
  10929.         COLUMN_NAME sysname collate database_default not null,
  10930.         DATA_TYPE smallint not null,
  10931.         TYPE_NAME sysname collate database_default not null,
  10932.         "PRECISION" int null,
  10933.         LENGTH int null,
  10934.         SCALE smallint null,
  10935.         PSEUDO_COLUMN smallint null
  10936.         )
  10937.     insert into #spspeccol exec sp_special_columns @p1,@p2,@p3,@p4,@p5,@p6,@ODBCVer
  10938.     exec @ret = sp_cursoropen @handle output,
  10939.         'select * from #spspeccol',
  10940.         @scrollopt output, @ccopt output, @rows output
  10941.     drop table #spspeccol
  10942.     return @ret
  10943. go
  10944.  
  10945.  
  10946.  
  10947. if (charindex('8.00', @@version) = 0)
  10948. begin
  10949.     print ''
  10950.     print ''
  10951.     print 'Warning:'
  10952.     print 'you are installing the stored procedures '
  10953.     print 'on a pre 8.0 SQL Server.'
  10954.     print 'Ignore the following errors.'
  10955. end
  10956. go
  10957.  
  10958. create procedure sp_ddopen; 8(
  10959.                @handle            int output,
  10960.                @scrollopt        int output,
  10961.                @ccopt            int output,
  10962.                @rows            int output,
  10963.                @p1                nvarchar(774),
  10964.                @p2                nvarchar(774),
  10965.                @p3                nvarchar(774),
  10966.                @p4                nvarchar(774),
  10967.                @ODBCVer         int)
  10968. as
  10969.     set nocount on
  10970.     declare @ret int
  10971.  
  10972.     create table #spproccol (
  10973.         PROCEDURE_QUALIFIER sysname  collate database_default null,
  10974.         PROCEDURE_OWNER sysname  collate database_default null,
  10975.         PROCEDURE_NAME sysname collate database_default not null,
  10976.         COLUMN_NAME sysname collate database_default not null,
  10977.         COLUMN_TYPE smallint not null,
  10978.         DATA_TYPE smallint null,
  10979.         TYPE_NAME sysname collate database_default not null,
  10980.         "PRECISION" int null,
  10981.         LENGTH int null,
  10982.         SCALE smallint null,
  10983.         RADIX smallint null,
  10984.         NULLABLE smallint not null,
  10985.         REMARKS varchar(254) collate database_default null,
  10986.         COLUMN_DEF nvarchar(3000) collate database_default null,
  10987.         SQL_DATA_TYPE smallint null,
  10988.         SQL_DATETIME_SUB smallint null,
  10989.         CHAR_OCTET_LENGTH int null,
  10990.         ORDINAL_POSITION int not null,
  10991.         IS_NULLABLE varchar(254) collate database_default null,
  10992.         SS_DATA_TYPE tinyint null
  10993.         )
  10994.     insert into #spproccol exec sp_sproc_columns @p1,@p2,@p3,@p4,@ODBCVer
  10995.     exec @ret = sp_cursoropen @handle output,
  10996.         'select * from #spproccol',
  10997.         @scrollopt output, @ccopt output, @rows output
  10998.     drop table #spproccol
  10999.     return @ret
  11000. go
  11001.  
  11002.  
  11003.  
  11004. if (charindex('8.00', @@version) = 0)
  11005. begin
  11006.     print ''
  11007.     print ''
  11008.     print 'Warning:'
  11009.     print 'you are installing the stored procedures '
  11010.     print 'on a pre 8.0 SQL Server.'
  11011.     print 'Ignore the following errors.'
  11012. end
  11013. go
  11014.  
  11015. create procedure sp_ddopen; 9(
  11016.                @handle            int output,
  11017.                @scrollopt        int output,
  11018.                @ccopt            int output,
  11019.                @rows            int output,
  11020.                @p1                nvarchar(774),
  11021.                @p2                nvarchar(774),
  11022.                @p3                nvarchar(774),
  11023.                @p4                nvarchar(774),
  11024.                @p5                nvarchar(774),
  11025.                @p6                nvarchar(774))
  11026. as
  11027.     set nocount on
  11028.     declare @ret int
  11029.  
  11030.     create table #spstatistics (
  11031.         TABLE_QUALIFIER sysname   collate database_default null,
  11032.         TABLE_OWNER sysname   collate database_default null,
  11033.         TABLE_NAME sysname    collate database_default not null,
  11034.         NON_UNIQUE smallint null,
  11035.         INDEX_QUALIFIER sysname collate database_default null,
  11036.         INDEX_NAME sysname collate database_default null,
  11037.         TYPE smallint not null,
  11038.         SEQ_IN_INDEX smallint null,
  11039.         COLUMN_NAME sysname collate database_default null,
  11040.         COLLATION char(1) collate database_default null,
  11041.         CARDINALITY int null,
  11042.         PAGES int null,
  11043.         FILTER_CONDITION varchar(128) collate database_default null
  11044.         )
  11045.     insert into #spstatistics exec sp_statistics @p1,@p2,@p3,@p4,@p5,@p6
  11046.     exec @ret = sp_cursoropen @handle output,
  11047.         'select * from #spstatistics',
  11048.         @scrollopt output, @ccopt output, @rows output
  11049.     drop table #spstatistics
  11050.     return @ret
  11051. go
  11052.                
  11053.  
  11054.  
  11055. if (charindex('8.00', @@version) = 0)
  11056. begin
  11057.     print ''
  11058.     print ''
  11059.     print 'Warning:'
  11060.     print 'you are installing the stored procedures '
  11061.     print 'on a pre 8.0 SQL Server.'
  11062.     print 'Ignore the following errors.'
  11063. end
  11064. go
  11065.  
  11066. create procedure sp_ddopen; 10(
  11067.                @handle            int output,
  11068.                @scrollopt        int output,
  11069.                @ccopt            int output,
  11070.                @rows            int output,
  11071.                @p1                nvarchar(774),
  11072.                @p2                nvarchar(774),
  11073.                @p3                nvarchar(774))
  11074. as
  11075.     set nocount on
  11076.     declare @ret int
  11077.  
  11078.     create table #spprocedures (
  11079.         PROCEDURE_QUALIFIER sysname  collate database_default null,
  11080.         PROCEDURE_OWNER sysname  collate database_default null,
  11081.         PROCEDURE_NAME sysname collate database_default not null,
  11082.         NUM_INPUT_PARAMS int null,
  11083.         NUM_OUTPUT_PARAMS int null,
  11084.         NUM_RESULT_SETS int null,
  11085.         REMARKS varchar(254) collate database_default null,
  11086.         PROCEDURE_TYPE smallint null
  11087.         )
  11088.     insert into #spprocedures exec sp_stored_procedures @p1,@p2,@p3
  11089.     exec @ret = sp_cursoropen @handle output,
  11090.         'select * from #spprocedures',
  11091.         @scrollopt output, @ccopt output, @rows output
  11092.     drop table #spprocedures
  11093.     return @ret
  11094. go
  11095.  
  11096.  
  11097.  
  11098. if (charindex('8.00', @@version) = 0)
  11099. begin
  11100.     print ''
  11101.     print ''
  11102.     print 'Warning:'
  11103.     print 'you are installing the stored procedures '
  11104.     print 'on a pre 8.0 SQL Server.'
  11105.     print 'Ignore the following errors.'
  11106. end
  11107. go
  11108.  
  11109. create procedure sp_ddopen; 11(
  11110.                @handle            int output,
  11111.                @scrollopt        int output,
  11112.                @ccopt            int output,
  11113.                @rows            int output,
  11114.                @p1                nvarchar(774),
  11115.                @p2                nvarchar(774),
  11116.                @p3                nvarchar(774))
  11117. as
  11118.     set nocount on
  11119.     declare @ret int
  11120.  
  11121.     create table #sptabpriv (
  11122.         TABLE_QUALIFIER sysname collate database_default null,
  11123.         TABLE_OWNER sysname collate database_default null,
  11124.         TABLE_NAME sysname collate database_default not null,
  11125.         GRANTOR sysname collate database_default null,
  11126.         GRANTEE sysname collate database_default not null,
  11127.         PRIVILEGE varchar(32) collate database_default not null,
  11128.         IS_GRANTABLE varchar(3) collate database_default null
  11129.         )
  11130.     insert into #sptabpriv exec sp_table_privileges @p1,@p2,@p3
  11131.     exec @ret = sp_cursoropen @handle output,
  11132.         'select * from #sptabpriv',
  11133.         @scrollopt output, @ccopt output, @rows output
  11134.     drop table #sptabpriv
  11135.     return @ret
  11136. go
  11137.  
  11138.  
  11139.  
  11140. if (charindex('8.00', @@version) = 0)
  11141. begin
  11142.     print ''
  11143.     print ''
  11144.     print 'Warning:'
  11145.     print 'you are installing the stored procedures '
  11146.     print 'on a pre 8.0 SQL Server.'
  11147.     print 'Ignore the following errors.'
  11148. end
  11149. go
  11150.  
  11151. create procedure sp_ddopen; 12(
  11152.                @handle            int output,
  11153.                @procname        sysname,
  11154.                @scrollopt        int output,
  11155.                @ccopt            int output,
  11156.                @rows            int output,
  11157.                @p1                nvarchar(774),
  11158.                @p2                nvarchar(774),
  11159.                @p3                nvarchar(774),
  11160.                @p4                nvarchar(774),
  11161.                @p5                nvarchar(774))
  11162. as
  11163.     set nocount on
  11164.     declare @ret int
  11165.  
  11166.     create table #sptables (
  11167.         TABLE_QUALIFIER sysname collate database_default null,
  11168.         TABLE_OWNER sysname collate database_default null,
  11169.         TABLE_NAME sysname collate database_default null,
  11170.         TABLE_TYPE    varchar(32) collate database_default null,
  11171.         REMARKS varchar(254) collate database_default null)
  11172.     if @procname = 'sp_tables'
  11173.     begin
  11174.         insert into #sptables exec sp_tables @p1,@p2,@p3,@p4
  11175.     end
  11176.     else
  11177.     begin
  11178.         insert into #sptables exec sp_tables_ex @p1,@p2,@p3,@p4,@p5
  11179.     end
  11180.     exec @ret = sp_cursoropen @handle output,
  11181.         'select * from #sptables',
  11182.         @scrollopt output, @ccopt output, @rows output
  11183.     drop table #sptables
  11184.     return @ret
  11185. go
  11186. grant execute on sp_ddopen to public
  11187. go
  11188.  
  11189. print 'creating sp_tableswc'
  11190. go
  11191.  
  11192. if (charindex('7.00', @@version) = 0 and
  11193.     charindex('8.00', @@version) = 0)
  11194. begin
  11195.     print ''
  11196.     print ''
  11197.     print 'Warning:'
  11198.     print 'you are installing the stored procedures '
  11199.     print 'on a pre 7.0 SQL Server.'
  11200.     print 'Ignore the following errors.'
  11201. end
  11202. go
  11203.  
  11204. /*    Procedure for 7.0 server */
  11205. create procedure sp_tableswc(
  11206.                @table_name        nvarchar(384)    = null,
  11207.                @table_owner     nvarchar(384)    = null,
  11208.                @table_qualifier sysname    = null,
  11209.                @table_type        varchar(100) = null)
  11210. as
  11211.     declare @databasename    sysname
  11212.     declare @qualprocname    nvarchar(141) /* 128 + '..sp_tables' */
  11213.  
  11214.     create table #sptables (
  11215.         TABLE_QUALIFIER sysname null,
  11216.         TABLE_OWNER sysname null,
  11217.         TABLE_NAME sysname null,
  11218.         TABLE_TYPE    varchar(32) null,
  11219.         REMARKS varchar(254) null)
  11220.  
  11221.     declare databases CURSOR FOR
  11222.         select name from master..sysdatabases
  11223.         where name like @table_qualifier and name <> 'model' and has_dbaccess(name)=1
  11224.         for read only
  11225.  
  11226.     open databases
  11227.     fetch next from databases into @databasename
  11228.     while (@@FETCH_STATUS <> -1)
  11229.     begin
  11230.         if (charindex('%', @databasename) = 0 and
  11231.             charindex('_', @databasename) = 0)
  11232.         begin    /* skip dbnames w/wildcard characters to prevent loop */
  11233.             select @qualprocname = @databasename + '..sp_tables'
  11234.             insert into #sptables exec @qualprocname
  11235.                 @table_name, @table_owner, @databasename, @table_type
  11236.         end
  11237.         fetch next from databases into @databasename
  11238.     end
  11239.     deallocate databases
  11240.     select * from #sptables
  11241.         order by 4, 1, 2, 3
  11242. go
  11243.  
  11244. if (charindex('8.00', @@version) = 0)
  11245. begin
  11246.     print ''
  11247.     print ''
  11248.     print 'Warning:'
  11249.     print 'you are installing the stored procedures '
  11250.     print 'on a pre 8.0 SQL Server.'
  11251.     print 'Ignore the following errors.'
  11252. end
  11253. else
  11254. drop procedure sp_tableswc
  11255. go
  11256.  
  11257. /*    Procedure for 8.0 server */
  11258. create procedure sp_tableswc(
  11259.                @table_name        nvarchar(384)    = null,
  11260.                @table_owner     nvarchar(384)    = null,
  11261.                @table_qualifier sysname    = null,
  11262.                @table_type        varchar(100) = null)
  11263. as
  11264.     declare @databasename    sysname
  11265.     declare @qualprocname    nvarchar(141) /* 128 + '..sp_tables' */
  11266.  
  11267.     create table #sptables (
  11268.         TABLE_QUALIFIER sysname collate database_default null,
  11269.         TABLE_OWNER sysname collate database_default null,
  11270.         TABLE_NAME sysname collate database_default null,
  11271.         TABLE_TYPE    varchar(32) collate database_default null,
  11272.         REMARKS varchar(254) collate database_default null)
  11273.  
  11274.     declare databases CURSOR FOR
  11275.         select name from master..sysdatabases
  11276.         where name like @table_qualifier and name <> 'model' and has_dbaccess(name)=1
  11277.         for read only
  11278.  
  11279.     open databases
  11280.     fetch next from databases into @databasename
  11281.     while (@@FETCH_STATUS <> -1)
  11282.     begin
  11283.         if (charindex('%', @databasename) = 0 and
  11284.             charindex('_', @databasename) = 0)
  11285.         begin    /* skip dbnames w/wildcard characters to prevent loop */
  11286.             select @qualprocname = @databasename + '..sp_tables'
  11287.             insert into #sptables exec @qualprocname
  11288.                 @table_name, @table_owner, @databasename, @table_type
  11289.         end
  11290.         fetch next from databases into @databasename
  11291.     end
  11292.     deallocate databases
  11293.     select * from #sptables
  11294.         order by 4, 1, 2, 3
  11295. go
  11296.  
  11297. grant execute on sp_tableswc to public
  11298. go
  11299.  
  11300. dump tran master with no_log
  11301. go
  11302.  
  11303. /*-------------------------------------------------------------------------*/
  11304. /*-------------- CATALOG STORED PROCEDURES FOR SQLOLEDB  ------------------*/
  11305. /*-------------------------------------------------------------------------*/
  11306.  
  11307. print ''
  11308. print 'creating spt_provider_types'
  11309. go
  11310. if (charindex('7.00', @@version) = 0 and
  11311.     charindex('8.00', @@version) = 0)
  11312. begin /* Pre 8.0 Server */
  11313. create table spt_provider_types
  11314.     (
  11315.     ss_dtype        tinyint     not null,
  11316.     fixlen            int         null,        /* datatype len for variable, else null */
  11317.     type_name        sysname        not null,
  11318.     oledb_data_type        smallint    not null,
  11319.     best_match        bit        not null,
  11320.     is_nullable        tinyint        null,
  11321.     case_sensitive        bit        not null,
  11322.     fixed_prec_scale    bit        not null,
  11323.     is_long            bit        not null,
  11324.     auto_unique_value    tinyint        not null,
  11325.     data_precision        int         null,
  11326.     numeric_scale        smallint    null,        /* min scale if 6.0 */
  11327.     column_size        int         null,
  11328.     literal_prefix        varchar(32)    null,
  11329.     literal_suffix        varchar(32)     null,
  11330.     searchable        int        not null,
  11331.     unsigned_attribute    tinyint        null,
  11332.     local_type_name     sysname        null
  11333.     )
  11334. print ''
  11335. print ''
  11336. print 'Warning:'
  11337. print 'you are installing the stored procedures '
  11338. print 'on a pre 8.0 SQL Server.'
  11339. print 'Ignore the following errors.'
  11340. end
  11341. go
  11342. if (charindex('7.00', @@version) > 0 or
  11343.     charindex('8.00', @@version) > 0)
  11344. begin /* 8.0 Server */
  11345. create table spt_provider_types
  11346.     (
  11347.     ss_dtype        tinyint     not null,
  11348.     fixlen            int         null,        /* datatype len for variable, else null */
  11349.     type_name        sysname        not null,
  11350.     oledb_data_type        smallint    not null,
  11351.     best_match        bit        not null,
  11352.     is_nullable        bit        null,
  11353.     case_sensitive        bit        not null,
  11354.     fixed_prec_scale    bit        not null,
  11355.     is_long            bit        not null,
  11356.     auto_unique_value    tinyint        not null,
  11357.     data_precision        int         null,
  11358.     numeric_scale        smallint    null,        /* min scale if 6.0 */
  11359.     column_size        int         null,
  11360.     literal_prefix        nvarchar(32)    null,
  11361.     literal_suffix        nvarchar(32)     null,
  11362.     searchable        int        not null,
  11363.     unsigned_attribute    tinyint        null,
  11364.     local_type_name     sysname        null
  11365.     )
  11366. end
  11367. go
  11368.  
  11369. grant select on spt_provider_types to public
  11370. go
  11371. dump tran master with no_log
  11372. go
  11373.  
  11374. /*
  11375. ** Insert the spt_provider_types rows for DBTYTPE_STR data types
  11376. */
  11377. begin tran
  11378.  
  11379. /* Get case sensitivity */
  11380. declare @case_sensitive bit
  11381. select @case_sensitive = case when 'a' <> 'A' then 1 else 0 end
  11382. /* Local Char */
  11383. insert into spt_provider_types values
  11384.     (
  11385.     47 /*SQLCHARACTER*/,        /* ss_dtype */
  11386.     0,                /* fixlen */
  11387.     'char',                /* type_name */
  11388.     129 /*DBTYPE_STR*/,        /* oledb_data_type */
  11389.     0,                /* best_match */
  11390.     null,                /* is_nullable */
  11391.     @case_sensitive,        /* case_sensitive */
  11392.     0,                /* fixed_prec_scale */
  11393.     0,                /* is_long */
  11394.     0,                /* auto_unique_value */
  11395.     null,                /* data_precision */
  11396.     null,                /* numeric_scale */
  11397.     null,                /* column_size */
  11398.     '''',                /* literal_prefix */
  11399.     '''',                /* literal_suffix */
  11400.     4 /*DB_SEARCHABLE*/,        /* searchable */
  11401.     null,                /* unsigned_attribute */
  11402.     'char'                /* local_type_name */
  11403.     )
  11404.  
  11405. /* Local Varchar */
  11406. insert into spt_provider_types values
  11407.     (
  11408.     39 /*SQLVARCHAR*/,        /* ss_dtype */
  11409.     null,                /* fixlen */
  11410.     'varchar',            /* type_name */
  11411.     129 /*DBTYPE_STR*/,        /* oledb_data_type */
  11412.     1,                /* best_match */
  11413.     null,                /* is_nullable */
  11414.     @case_sensitive,        /* case_sensitive */
  11415.     0,                /* fixed_prec_scale */
  11416.     0,                /* is_long */
  11417.     0,                /* auto_unique_value */
  11418.     null,                /* data_precision */
  11419.     null,                /* numeric_scale */
  11420.     null,                /* column_size */
  11421.     '''',                /* literal_prefix */
  11422.     '''',                /* literal_suffix */
  11423.     4 /*DB_SEARCHABLE*/,        /* searchable */
  11424.     null,                /* unsigned_attribute */
  11425.     'varchar'            /* local_type_name */
  11426.     )
  11427.  
  11428. /* Local Text */
  11429. insert into spt_provider_types values
  11430.     (
  11431.     35  /*SQLTEXT*/,        /* ss_dtype */
  11432.     null,                /* fixlen */
  11433.     'text',                /* type_name */
  11434.     129 /*DBTYPE_STR*/,        /* oledb_data_type */
  11435.     0,                /* best_match */
  11436.     null,                /* is_nullable */
  11437.     @case_sensitive,        /* case_sensitive */
  11438.     0,                /* fixed_prec_scale */
  11439.     1,                /* is_long */
  11440.     0,                /* auto_unique_value */
  11441.     null,                /* data_precision */
  11442.     null,                /* numeric_scale */
  11443.     2147483647,            /* column_size */
  11444.     '''',                /* literal_prefix */
  11445.     '''',                /* literal_suffix */
  11446.     2 /*DB_LIKE_ONLY*/,        /* searchable */
  11447.     null,                /* unsigned_attribute */
  11448.     'text'                /* local_type_name */
  11449.     )
  11450. commit tran
  11451. go
  11452. dump tran master with no_log
  11453. go
  11454.  
  11455. /*
  11456. ** Insert the spt_provider_types rows for DBTYTPE_BYTES data types
  11457. */
  11458. begin tran
  11459. /* Local Binary */
  11460. insert into spt_provider_types values
  11461.     (
  11462.     45 /*SQLBINARY*/,        /* ss_dtype */
  11463.     0,                /* fixlen */
  11464.     'binary',            /* type_name */
  11465.     128 /*DBTYPE_BYTES*/,        /* oledb_data_type */
  11466.     0,                /* best_match */
  11467.     null,                /* is_nullable */
  11468.     0,                /* case_sensitive */
  11469.     0,                /* fixed_prec_scale */
  11470.     0,                /* is_long */
  11471.     0,                /* auto_unique_value */
  11472.     null,                /* data_precision */
  11473.     null,                /* numeric_scale */
  11474.     null,                /* column_size */
  11475.     '0x',                /* literal_prefix */
  11476.     null,                /* literal_suffix */
  11477.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  11478.     null,                /* unsigned_attribute */
  11479.     'binary'            /* local_type_name */
  11480.     )
  11481.  
  11482. /* Local Varbinary */
  11483. insert into spt_provider_types values
  11484.     (
  11485.     37 /*SQLVARBINARY*/,        /* ss_dtype */
  11486.     null,                /* fixlen */
  11487.     'varbinary',            /* type_name */
  11488.     128 /*DBTYPE_BYTES*/,        /* oledb_data_type */
  11489.     1,                /* best_match */
  11490.     null,                /* is_nullable */
  11491.     0,                /* case_sensitive */
  11492.     0,                /* fixed_prec_scale */
  11493.     0,                /* is_long */
  11494.     0,                /* auto_unique_value */
  11495.     null,                /* data_precision */
  11496.     null,                /* numeric_scale */
  11497.     null,                /* column_size */
  11498.     '0x',                /* literal_prefix */
  11499.     null,                /* literal_suffix */
  11500.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  11501.     null,                /* unsigned_attribute */
  11502.     'varbinary'            /* local_type_name */
  11503.     )
  11504.  
  11505. /* Local Image */
  11506. insert into spt_provider_types values
  11507.     (
  11508.     34 /*SQLIMAGE*/,        /* ss_dtype */
  11509.     null,                /* fixlen */
  11510.     'image',            /* type_name */
  11511.     128 /*DBTYPE_BYTES*/,        /* oledb_data_type */
  11512.     0,                /* best_match */
  11513.     null,                /* is_nullable */
  11514.     0,                /* case_sensitive */
  11515.     0,                /* fixed_prec_scale */
  11516.     1,                /* is_long */
  11517.     0,                /* auto_unique_value */
  11518.     null,                /* data_precision */
  11519.     null,                /* numeric_scale */
  11520.     2147483647,            /* column_size */
  11521.     '0x',                /* literal_prefix */
  11522.     null,                /* literal_suffix */
  11523.     1 /*DB_UNSEARCHABLE*/,        /* searchable */
  11524.     null,                /* unsigned_attribute */
  11525.     'image'                /* local_type_name */
  11526.     )
  11527.  
  11528. commit tran
  11529. go
  11530. dump tran master with no_log
  11531. go
  11532.  
  11533. /*
  11534. ** Insert the spt_provider_types rows for DBTYPE_DBTIMESTAMP data types
  11535. */
  11536. begin tran
  11537.  
  11538. /* Local Datetime */
  11539. insert into spt_provider_types values
  11540.     (
  11541.     61 /*SQLDATETIME*/,        /* ss_dtype */
  11542.     8,                /* fixlen */
  11543.     'datetime',            /* type_name */
  11544.     135 /*DBTYPE_DBTIMESTAMP*/,    /* oledb_data_type */
  11545.     1,                /* best_match */
  11546.     null,                /* is_nullable */
  11547.     0,                /* case_sensitive */
  11548.     0,                /* fixed_prec_scale */
  11549.     0,                /* is_long */
  11550.     0,                /* auto_unique_value */
  11551.     23,                /* data_precision */
  11552.     null,                /* numeric_scale */
  11553.     null,                /* column_size */
  11554.     '''',                /* literal_prefix */
  11555.     '''',                /* literal_suffix */
  11556.     4 /*DB_SEARCHABLE*/,        /* searchable */
  11557.     null,                /* unsigned_attribute */
  11558.     'datetime'            /* local_type_name */
  11559.     )
  11560.  
  11561. /* Local Smalldatetime */
  11562. insert into spt_provider_types values
  11563.     (
  11564.     58 /*SQLDATETIM4*/,        /* ss_dtype */
  11565.     4,                /* fixlen */
  11566.     'smalldatetime',        /* type_name */
  11567.     135 /*DBTYPE_DBTIMESTAMP*/,    /* oledb_data_type */
  11568.     0,                /* best_match */
  11569.     null,                /* is_nullable */
  11570.     0,                /* case_sensitive */
  11571.     0,                /* fixed_prec_scale */
  11572.     0,                /* is_long */
  11573.     0,                /* auto_unique_value */
  11574.     16,                /* data_precision */
  11575.     null,                /* numeric_scale */
  11576.     null,                /* column_size */
  11577.     '''',                /* literal_prefix */
  11578.     '''',                /* literal_suffix */
  11579.     4 /*DB_SEARCHABLE*/,        /* searchable */
  11580.     null,                /* unsigned_attribute */
  11581.     'smalldatetime'            /* local_type_name */
  11582.     )
  11583.  
  11584. if (charindex('7.00', @@version) = 0 and
  11585.     charindex('8.00', @@version) = 0)
  11586.     begin    /*    Add nullable type for non-Sphinx server */
  11587.     
  11588.     /* Local Datetimn */
  11589.     insert into spt_provider_types values
  11590.         (
  11591.         111 /*SQLDATETIMN*/,        /* ss_dtype */
  11592.         4,                /* fixlen */
  11593.         'smalldatetime',        /* type_name */
  11594.         135 /*DBTYPE_DBTIMESTAMP*/,    /* oledb_data_type */
  11595.         0,                /* best_match */
  11596.         null,                /* is_nullable */
  11597.         0,                /* case_sensitive */
  11598.         0,                /* fixed_prec_scale */
  11599.         0,                /* is_long */
  11600.         0,                /* auto_unique_value */
  11601.         16,                /* data_precision */
  11602.         null,                /* numeric_scale */
  11603.         null,                /* column_size */
  11604.         '''',                /* literal_prefix */
  11605.         '''',                /* literal_suffix */
  11606.         4 /*DB_SEARCHABLE*/,        /* searchable */
  11607.         null,                /* unsigned_attribute */
  11608.         'smalldatetime'            /* local_type_name */
  11609.         )    
  11610.     insert into spt_provider_types values
  11611.         (
  11612.         111 /*SQLDATETIMN*/,        /* ss_dtype */
  11613.         8,                /* fixlen */
  11614.         'datetime',            /* type_name */
  11615.         135 /*DBTYPE_DBTIMESTAMP*/,    /* oledb_data_type */
  11616.         0,                /* best_match */
  11617.         null,                /* is_nullable */
  11618.         0,                /* case_sensitive */
  11619.         0,                /* fixed_prec_scale */
  11620.         0,                /* is_long */
  11621.         0,                /* auto_unique_value */
  11622.         23,                /* data_precision */
  11623.         null,                /* numeric_scale */
  11624.         null,                /* column_size */
  11625.         '''',                /* literal_prefix */
  11626.         '''',                /* literal_suffix */
  11627.         4 /*DB_SEARCHABLE*/,        /* searchable */
  11628.         null,                /* unsigned_attribute */
  11629.         'datetime'            /* local_type_name */
  11630.         )
  11631.     end
  11632.  
  11633. commit tran
  11634. go
  11635. dump tran master with no_log
  11636. go
  11637.  
  11638. /*
  11639. ** Insert the spt_provider_types rows for DBTYPE_CY data types
  11640. */
  11641. begin tran
  11642.  
  11643. /* Local Smallmoney */
  11644. insert into spt_provider_types values
  11645.     (
  11646.     122 /*SQLMONEY4*/,        /* ss_dtype */
  11647.     4,                /* fixlen */
  11648.     'smallmoney',            /* type_name */
  11649.     6 /*DBTYPE_CY*/,        /* oledb_data_type */
  11650.     0,                /* best_match */
  11651.     null,                /* is_nullable */
  11652.     0,                /* case_sensitive */
  11653.     1,                /* fixed_prec_scale */
  11654.     0,                /* is_long */
  11655.     0,                /* auto_unique_value */
  11656.     10,                /* data_precision */
  11657.     null,                /* numeric_scale */
  11658.     null,                /* column_size */
  11659.     '$',                /* literal_prefix */
  11660.     null,                /* literal_suffix */
  11661.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  11662.     0,                /* unsigned_attribute */
  11663.     'smallmoney'            /* local_type_name */
  11664.     )    
  11665.     
  11666. /* Local Money */
  11667. insert into spt_provider_types values
  11668.     (
  11669.     60 /*SQLMONEY*/,        /* ss_dtype */
  11670.     8,                /* fixlen */
  11671.     'money',            /* type_name */
  11672.     6 /*DBTYPE_CY*/,        /* oledb_data_type */
  11673.     1,                /* best_match */
  11674.     null,                /* is_nullable */
  11675.     0,                /* case_sensitive */
  11676.     1,                /* fixed_prec_scale */
  11677.     0,                /* is_long */
  11678.     0,                /* auto_unique_value */
  11679.     19,                /* data_precision */
  11680.     null,                /* numeric_scale */
  11681.     null,                /* column_size */
  11682.     '$',                /* literal_prefix */
  11683.     null,                /* literal_suffix */
  11684.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  11685.     0,                /* unsigned_attribute */
  11686.     'money'                /* local_type_name */
  11687.     )    
  11688.  
  11689. if (charindex('7.00', @@version) = 0 and
  11690.     charindex('8.00', @@version) = 0)
  11691.     begin    /*    Add nullable type for non-Sphinx server */
  11692.  
  11693.     /* Local Moneyn */
  11694.     insert into spt_provider_types values
  11695.         (
  11696.         110 /*SQLMONEYN*/,    /* ss_dtype */
  11697.         4,            /* fixlen */
  11698.         'smallmoney',        /* type_name */
  11699.         6 /*DBTYPE_CY*/,    /* oledb_data_type */
  11700.         0,            /* best_match */
  11701.         null,            /* is_nullable */
  11702.         0,            /* case_sensitive */
  11703.         1,            /* fixed_prec_scale */
  11704.         0,            /* is_long */
  11705.         0,            /* auto_unique_value */
  11706.         10,            /* data_precision */
  11707.         null,            /* numeric_scale */
  11708.         null,            /* column_size */
  11709.         '$',            /* literal_prefix */
  11710.         null,            /* literal_suffix */
  11711.         3 /*DB_ALL_EXCEPT_LIKE*/, /* searchable */
  11712.         0,            /* unsigned_attribute */
  11713.         'smallmoney'        /* local_type_name */
  11714.         )    
  11715.     insert into spt_provider_types values
  11716.         (
  11717.         110 /*SQLMONEYN*/,    /* ss_dtype */
  11718.         8,            /* fixlen */
  11719.         'money',        /* type_name */
  11720.         6 /*DBTYPE_CY*/,    /* oledb_data_type */
  11721.         0,            /* best_match */
  11722.         null,            /* is_nullable */
  11723.         0,            /* case_sensitive */
  11724.         1,            /* fixed_prec_scale */
  11725.         0,            /* is_long */
  11726.         0,            /* auto_unique_value */
  11727.         19,            /* data_precision */
  11728.         null,            /* numeric_scale */
  11729.         null,            /* column_size */
  11730.         '$',            /* literal_prefix */
  11731.         null,            /* literal_suffix */
  11732.         3 /*DB_ALL_EXCEPT_LIKE*/, /* searchable */
  11733.         null,            /* unsigned_attribute */
  11734.         'money'            /* local_type_name */
  11735.         )    
  11736.     end
  11737.  
  11738. commit tran
  11739. go
  11740. dump tran master with no_log
  11741. go
  11742.  
  11743. /*
  11744. ** Insert the spt_provider_types rows for the numeric data types
  11745. */
  11746. begin tran
  11747.  
  11748. /* Local Float */
  11749. insert into spt_provider_types values
  11750.     (
  11751.     62 /*SQLFLT8*/,            /* ss_dtype */
  11752.     8,                /* fixlen */
  11753.     'float',            /* type_name */
  11754.     5 /*DBTYPE_R8*/,        /* oledb_data_type */
  11755.     1,                /* best_match */
  11756.     null,                /* is_nullable */
  11757.     0,                /* case_sensitive */
  11758.     0,                /* fixed_prec_scale */
  11759.     0,                /* is_long */
  11760.     0,                /* auto_unique_value */
  11761.     15,                /* data_precision */
  11762.     null,                /* numeric_scale */
  11763.     null,                /* column_size */
  11764.     null,                /* literal_prefix */
  11765.     null,                /* literal_suffix */
  11766.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  11767.     0,                /* unsigned_attribute */
  11768.     'float'                /* local_type_name */
  11769.     )    
  11770.  
  11771. /* Local Real */
  11772. insert into spt_provider_types values
  11773.     (
  11774.     59 /*SQLFLT4*/,            /* ss_dtype */
  11775.     4,                /* fixlen */
  11776.     'real',                /* type_name */
  11777.     4 /*DBTYPE_R4*/,        /* oledb_data_type */
  11778.     1,                /* best_match */
  11779.     null,                /* is_nullable */
  11780.     0,                /* case_sensitive */
  11781.     0,                /* fixed_prec_scale */
  11782.     0,                /* is_long */
  11783.     0,                /* auto_unique_value */
  11784.     7,                /* data_precision */
  11785.     null,                /* numeric_scale */
  11786.     null,                /* column_size */
  11787.     null,                /* literal_prefix */
  11788.     null,                /* literal_suffix */
  11789.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  11790.     0,                /* unsigned_attribute */
  11791.     'real'                /* local_type_name */
  11792.     )    
  11793.  
  11794. /* Local Int */
  11795. insert into spt_provider_types values
  11796.     (
  11797.     56 /*SQLINT4*/,            /* ss_dtype */
  11798.     4,                /* fixlen */
  11799.     'int',                /* type_name */
  11800.     3 /*DBTYPE_I4*/,        /* oledb_data_type */
  11801.     1,                /* best_match */
  11802.     null,                /* is_nullable */
  11803.     0,                /* case_sensitive */
  11804.     1,                /* fixed_prec_scale */
  11805.     0,                /* is_long */
  11806.     1,                /* auto_unique_value */
  11807.     10,                /* data_precision */
  11808.     null,                /* numeric_scale */
  11809.     null,                /* column_size */
  11810.     null,                /* literal_prefix */
  11811.     null,                /* literal_suffix */
  11812.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  11813.     0,                /* unsigned_attribute */
  11814.     'int'                /* local_type_name */
  11815.     )    
  11816.  
  11817. /* Local Smallint */
  11818. insert into spt_provider_types values
  11819.     (
  11820.     52 /*SQLINT2*/,            /* ss_dtype */
  11821.     2,                /* fixlen */
  11822.     'smallint',            /* type_name */
  11823.     2 /*DBTYPE_I2*/,        /* oledb_data_type */
  11824.     1,                /* best_match */
  11825.     null,                /* is_nullable */
  11826.     0,                /* case_sensitive */
  11827.     1,                /* fixed_prec_scale */
  11828.     0,                /* is_long */
  11829.     1,                /* auto_unique_value */
  11830.     5,                /* data_precision */
  11831.     null,                /* numeric_scale */
  11832.     null,                /* column_size */
  11833.     null,                /* literal_prefix */
  11834.     null,                /* literal_suffix */
  11835.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  11836.     0,                /* unsigned_attribute */
  11837.     'smallint'            /* local_type_name */
  11838.     )    
  11839.  
  11840. /* Local Tinyint */
  11841. insert into spt_provider_types values
  11842.     (
  11843.     48 /*SQLINT1*/,            /* ss_dtype */
  11844.     1,                /* fixlen */
  11845.     'tinyint',            /* type_name */
  11846.     17 /*DBTYPE_UI1*/,        /* oledb_data_type */
  11847.     1,                /* best_match */
  11848.     null,                /* is_nullable */
  11849.     0,                /* case_sensitive */
  11850.     1,                /* fixed_prec_scale */
  11851.     0,                /* is_long */
  11852.     1,                /* auto_unique_value */
  11853.     3,                /* data_precision */
  11854.     null,                /* numeric_scale */
  11855.     null,                /* column_size */
  11856.     null,                /* literal_prefix */
  11857.     null,                /* literal_suffix */
  11858.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  11859.     1,                /* unsigned_attribute */
  11860.     'tinyint'            /* local_type_name */
  11861.     )    
  11862. commit tran
  11863. go
  11864. dump tran master with no_log
  11865. go
  11866. begin tran    
  11867. if (charindex('6.00', @@version) > 0 or
  11868.     charindex('6.50', @@version) > 0 or
  11869.     charindex('7.00', @@version) > 0 or
  11870.     charindex('8.00', @@version) > 0)
  11871.     begin    /*    Add 6.0 data types */
  11872.     
  11873.     /* Local Decimal */
  11874.     insert into spt_provider_types values
  11875.         (
  11876.         55 /*SQLDECIMAL*/,        /* ss_dtype */
  11877.         0,                /* fixlen */
  11878.         'decimal',            /* type_name */
  11879.         131 /*DBTYPE_NUMERIC*/,        /* oledb_data_type */
  11880.         0,                /* best_match */
  11881.         null,                /* is_nullable */
  11882.         0,                /* case_sensitive */
  11883.         1,                /* fixed_prec_scale */
  11884.         0,                /* is_long */
  11885.         1,                /* auto_unique_value */
  11886.         38,                /* data_precision */
  11887.         null,                /* numeric_scale */
  11888.         null,                /* column_size */
  11889.         null,                /* literal_prefix */
  11890.         null,                /* literal_suffix */
  11891.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  11892.         0,                /* unsigned_attribute */
  11893.         'decimal'            /* local_type_name */
  11894.         )    
  11895.  
  11896.     /* Local Numeric */
  11897.     insert into spt_provider_types values
  11898.         (
  11899.         63 /*SQLNUMERIC*/,        /* ss_dtype */
  11900.         0,                /* fixlen */
  11901.         'numeric',            /* type_name */
  11902.         131 /*DBTYPE_NUMERIC*/,        /* oledb_data_type */
  11903.         1,                /* best_match */
  11904.         null,                /* is_nullable */
  11905.         0,                /* case_sensitive */
  11906.         1,                /* fixed_prec_scale */
  11907.         0,                /* is_long */
  11908.         1,                /* auto_unique_value */
  11909.         38,                /* data_precision */
  11910.         null,                /* numeric_scale */
  11911.         null,                /* column_size */
  11912.         null,                /* literal_prefix */
  11913.         null,                /* literal_suffix */
  11914.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  11915.         0,                /* unsigned_attribute */
  11916.         'numeric'            /* local_type_name */
  11917.         )    
  11918.     end
  11919. commit tran
  11920. go
  11921. dump tran master with no_log
  11922. go
  11923. begin tran
  11924. if (charindex('7.00', @@version) = 0 and
  11925.     charindex('8.00', @@version) = 0)
  11926.     begin    /*    Add nullable type for non-Sphinx server */
  11927.     
  11928.     /* Local Floatn */
  11929.     insert into spt_provider_types values
  11930.         (
  11931.         109 /*SQLFLTN*/,        /* ss_dtype */
  11932.         8,                /* fixlen */
  11933.         'float',            /* type_name */
  11934.         5 /*DBTYPE_R8*/,        /* oledb_data_type */
  11935.         0,                /* best_match */
  11936.         null,                /* is_nullable */
  11937.         0,                /* case_sensitive */
  11938.         0,                /* fixed_prec_scale */
  11939.         0,                /* is_long */
  11940.         0,                /* auto_unique_value */
  11941.         15,                /* data_precision */
  11942.         null,                /* numeric_scale */
  11943.         null,                /* column_size */
  11944.         null,                /* literal_prefix */
  11945.         null,                /* literal_suffix */
  11946.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  11947.         null,                /* unsigned_attribute */
  11948.         'float'                /* local_type_name */
  11949.         )    
  11950.     insert into spt_provider_types values
  11951.         (
  11952.         109 /*SQLFLT4*/,        /* ss_dtype */
  11953.         4,                /* fixlen */
  11954.         'real',                /* type_name */
  11955.         4 /*DBTYPE_R4*/,        /* oledb_data_type */
  11956.         0,                /* best_match */
  11957.         null,                /* is_nullable */
  11958.         0,                /* case_sensitive */
  11959.         0,                /* fixed_prec_scale */
  11960.         0,                /* is_long */
  11961.         0,                /* auto_unique_value */
  11962.         7,                /* data_precision */
  11963.         null,                /* numeric_scale */
  11964.         null,                /* column_size */
  11965.         null,                /* literal_prefix */
  11966.         null,                /* literal_suffix */
  11967.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  11968.         null,                /* unsigned_attribute */
  11969.         'real'                /* local_type_name */
  11970.         )    
  11971.  
  11972.     /* Local Intn */
  11973.     insert into spt_provider_types values
  11974.         (
  11975.         38 /*SQLINTN*/,            /* ss_dtype */
  11976.         4,                /* fixlen */
  11977.         'int',                /* type_name */
  11978.         3 /*DBTYPE_I4*/,        /* oledb_data_type */
  11979.         1,                /* best_match */
  11980.         null,                /* is_nullable */
  11981.         0,                /* case_sensitive */
  11982.         1,                /* fixed_prec_scale */
  11983.         0,                /* is_long */
  11984.         1,                /* auto_unique_value */
  11985.         10,                /* data_precision */
  11986.         null,                /* numeric_scale */
  11987.         null,                /* column_size */
  11988.         null,                /* literal_prefix */
  11989.         null,                /* literal_suffix */
  11990.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  11991.         0,                /* unsigned_attribute */
  11992.         'int'                /* local_type_name */
  11993.         )    
  11994.     insert into spt_provider_types values
  11995.         (
  11996.         38 /*SQLINTN*/,            /* ss_dtype */
  11997.         2,                /* fixlen */
  11998.         'smallint',            /* type_name */
  11999.         2 /*DBTYPE_I2*/,        /* oledb_data_type */
  12000.         0,                /* best_match */
  12001.         null,                /* is_nullable */
  12002.         0,                /* case_sensitive */
  12003.         1,                /* fixed_prec_scale */
  12004.         0,                /* is_long */
  12005.         1,                /* auto_unique_value */
  12006.         5,                /* data_precision */
  12007.         null,                /* numeric_scale */
  12008.         null,                /* column_size */
  12009.         null,                /* literal_prefix */
  12010.         null,                /* literal_suffix */
  12011.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  12012.         0,                /* unsigned_attribute */
  12013.         'smallint'            /* local_type_name */
  12014.         )    
  12015.     insert into spt_provider_types values
  12016.         (
  12017.         38 /*SQLINTN*/,            /* ss_dtype */
  12018.         1,                /* fixlen */
  12019.         'tinyint',            /* type_name */
  12020.         17 /*DBTYPE_UI1*/,        /* oledb_data_type */
  12021.         0,                /* best_match */
  12022.         null,                /* is_nullable */
  12023.         0,                /* case_sensitive */
  12024.         1,                /* fixed_prec_scale */
  12025.         0,                /* is_long */
  12026.         1,                /* auto_unique_value */
  12027.         3,                /* data_precision */
  12028.         null,                /* numeric_scale */
  12029.         null,                /* column_size */
  12030.         null,                /* literal_prefix */
  12031.         null,                /* literal_suffix */
  12032.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  12033.         1,                /* unsigned_attribute */
  12034.         'tinyint'            /* local_type_name */
  12035.         )    
  12036.  
  12037.     if (charindex('6.00', @@version) > 0 or    charindex('6.50', @@version) > 0)
  12038.         begin    /*    Add 6.0 data types */
  12039.         
  12040.         /* Local Decimaln */
  12041.         insert into spt_provider_types values
  12042.             (
  12043.             106 /*SQLDECIMALN*/,        /* ss_dtype */
  12044.             0,                /* fixlen */
  12045.             'decimal',            /* type_name */
  12046.             131 /*DBTYPE_NUMERIC*/,        /* oledb_data_type */
  12047.             0,                /* best_match */
  12048.             null,                /* is_nullable */
  12049.             0,                /* case_sensitive */
  12050.             1,                /* fixed_prec_scale */
  12051.             0,                /* is_long */
  12052.             1,                /* auto_unique_value */
  12053.             38,                /* data_precision */
  12054.             null,                /* numeric_scale */
  12055.             null,                /* column_size */
  12056.             null,                /* literal_prefix */
  12057.             null,                /* literal_suffix */
  12058.             3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  12059.             0,                /* unsigned_attribute */
  12060.             'decimal'            /* local_type_name */
  12061.             )    
  12062.  
  12063.         /* Local Numericn */
  12064.         insert into spt_provider_types values
  12065.             (
  12066.             108 /*SQLNUMERICN*/,        /* ss_dtype */
  12067.             0,                /* fixlen */
  12068.             'numeric',            /* type_name */
  12069.             131 /*DBTYPE_NUMERIC*/,        /* oledb_data_type */
  12070.             0,                /* best_match */
  12071.             null,                /* is_nullable */
  12072.             0,                /* case_sensitive */
  12073.             1,                /* fixed_prec_scale */
  12074.             0,                /* is_long */
  12075.             1,                /* auto_unique_value */
  12076.             38,                /* data_precision */
  12077.             null,                /* numeric_scale */
  12078.             null,                /* column_size */
  12079.             null,                /* literal_prefix */
  12080.             null,                /* literal_suffix */
  12081.             3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  12082.             0,                /* unsigned_attribute */
  12083.             'numeric'            /* local_type_name */
  12084.             )    
  12085.         end
  12086.     end
  12087. commit tran
  12088. go
  12089. dump tran master with no_log
  12090. go
  12091.  
  12092. /*
  12093. **    Remaining data types
  12094. */
  12095. if (charindex('7.00', @@version) = 0 and
  12096.     charindex('8.00', @@version) = 0)
  12097. begin
  12098.     print ''
  12099.     print ''
  12100.     print 'Warning:'
  12101.     print 'you are installing the stored procedures '
  12102.     print 'on a pre 8.0 SQL Server.'
  12103.     print 'Ignore the following errors.'
  12104. end
  12105. go
  12106. begin tran
  12107.  
  12108. if (charindex('7.00', @@version) > 0 or
  12109.     charindex('8.00', @@version) > 0)
  12110.     begin
  12111.     /* Local Bit */
  12112.     insert into spt_provider_types values
  12113.         (
  12114.         50 /*SQLBIT*/,            /* ss_dtype */
  12115.         0,                /* fixlen */
  12116.         'bit',                /* type_name */
  12117.         11 /*DBTYPE_BOOL*/,        /* oledb_data_type */
  12118.         1,                /* best_match */
  12119.         null,                /* is_nullable */
  12120.         0,                /* case_sensitive */
  12121.         0,                /* fixed_prec_scale */
  12122.         0,                /* is_long */
  12123.         0,                /* auto_unique_value */
  12124.         1,                /* data_precision */
  12125.         null,                /* numeric_scale */
  12126.         null,                /* column_size */
  12127.         null,                /* literal_prefix */
  12128.         null,                /* literal_suffix */
  12129.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  12130.         null,                /* unsigned_attribute */
  12131.         'bit'                /* local_type_name */
  12132.         )        
  12133.     
  12134.     /* Local Timestamp */
  12135.     insert into spt_provider_types values
  12136.         (
  12137.         0,                /* ss_dtype */
  12138.         8,                /* fixlen */
  12139.         'timestamp',            /* type_name */
  12140.         128 /*DBTYPE_BYTES*/,        /* oledb_data_type */
  12141.         0,                /* best_match */
  12142.         null,                /* is_nullable */
  12143.         0,                /* case_sensitive */
  12144.         0,                /* fixed_prec_scale */
  12145.         0,                /* is_long */
  12146.         0,                /* auto_unique_value */
  12147.         null,                /* data_precision */
  12148.         null,                /* numeric_scale */
  12149.         null,                /* column_size */
  12150.         '0x',                /* literal_prefix */
  12151.         null,                /* literal_suffix */
  12152.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  12153.         null,                /* unsigned_attribute */
  12154.         'timestamp'            /* local_type_name */
  12155.         )    
  12156.     /* Local GUID */
  12157.     insert into spt_provider_types values
  12158.         (
  12159.         0,                /* ss_dtype */
  12160.         16,                /* fixlen */
  12161.         'uniqueidentifier',        /* type_name */
  12162.         72 /*DBTYPE_GUID*/,        /* oledb_data_type */
  12163.         1,                /* best_match */
  12164.         null,                /* is_nullable */
  12165.         0,                /* case_sensitive */
  12166.         0,                /* fixed_prec_scale */
  12167.         0,                /* is_long */
  12168.         0,                /* auto_unique_value */
  12169.         null,                /* data_precision */
  12170.         null,                /* numeric_scale */
  12171.         null,                /* column_size */
  12172.         '''',                /* literal_prefix */
  12173.         '''',                /* literal_suffix */
  12174.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  12175.         null,                /* unsigned_attribute */
  12176.         'uniqueidentifier'        /* local_type_name */
  12177.         )    
  12178.     
  12179.     /* Get case sensitivity */
  12180.     declare @ncase_sensitive bit
  12181.     select @ncase_sensitive = case when N'a' <> N'A' then 1 else 0 end
  12182.  
  12183.     /* Local NChar */
  12184.     insert into spt_provider_types values
  12185.         (
  12186.         0,                /* ss_dtype */
  12187.         0,                /* fixlen */
  12188.         'nchar',            /* type_name */
  12189.         130 /*DBTYPE_WSTR*/,        /* oledb_data_type */
  12190.         0,                /* best_match */
  12191.         null,                /* is_nullable */
  12192.         @ncase_sensitive,        /* case_sensitive */
  12193.         0,                /* fixed_prec_scale */
  12194.         0,                /* is_long */
  12195.         0,                /* auto_unique_value */
  12196.         null,                /* data_precision */
  12197.         null,                /* numeric_scale */
  12198.         null,                /* column_size */
  12199.         'N''',                /* literal_prefix */
  12200.         '''',                /* literal_suffix */
  12201.         4 /*DB_SEARCHABLE*/,        /* searchable */
  12202.         null,                /* unsigned_attribute */
  12203.         'nchar'                /* local_type_name */
  12204.         )    
  12205.  
  12206.     /* Local NVarChar */
  12207.     insert into spt_provider_types values
  12208.         (
  12209.         0,                /* ss_dtype */
  12210.         null,                /* fixlen */
  12211.         'nvarchar',            /* type_name */
  12212.         130 /*DBTYPE_WSTR*/,        /* oledb_data_type */
  12213.         1,                /* best_match */
  12214.         null,                /* is_nullable */
  12215.         @ncase_sensitive,        /* case_sensitive */
  12216.         0,                /* fixed_prec_scale */
  12217.         0,                /* is_long */
  12218.         0,                /* auto_unique_value */
  12219.         null,                /* data_precision */
  12220.         null,                /* numeric_scale */
  12221.         null,                /* column_size */
  12222.         'N''',                /* literal_prefix */
  12223.         '''',                /* literal_suffix */
  12224.         4 /*DB_SEARCHABLE*/,        /* searchable */
  12225.         null,                /* unsigned_attribute */
  12226.         'nvarchar'            /* local_type_name */
  12227.         )    
  12228.  
  12229.     /* Local NText */
  12230.     insert into spt_provider_types values
  12231.         (
  12232.         0,                /* ss_dtype */
  12233.         null,                /* fixlen */
  12234.         'ntext',            /* type_name */
  12235.         130 /*DBTYPE_WSTR*/,        /* oledb_data_type */
  12236.         0,                /* best_match */
  12237.         null,                /* is_nullable */
  12238.         @ncase_sensitive,        /* case_sensitive */
  12239.         0,                /* fixed_prec_scale */
  12240.         1,                /* is_long */
  12241.         0,                /* auto_unique_value */
  12242.         null,                /* data_precision */
  12243.         null,                /* numeric_scale */
  12244.         1073741823,            /* column_size */
  12245.         'N''',                /* literal_prefix */
  12246.         '''',                /* literal_suffix */
  12247.         2 /*DB_LIKE_ONLY*/,        /* searchable */
  12248.         null,                /* unsigned_attribute */
  12249.         'ntext'                /* local_type_name */
  12250.         )    
  12251.  
  12252.     if (charindex('8.00', @@version) > 0)
  12253.     begin
  12254.     /* Local BIGINT */
  12255.     insert into spt_provider_types values
  12256.         (
  12257.         127,                /* ss_dtype */
  12258.         8,                /* fixlen */
  12259.         'bigint',            /* type_name */
  12260.         20 /*DBTYPE_I8*/,        /* oledb_data_type */
  12261.         1,                /* best_match */
  12262.         null,                /* is_nullable */
  12263.         0,                /* case_sensitive */
  12264.         1,                /* fixed_prec_scale */
  12265.         0,                /* is_long */
  12266.         1,                /* auto_unique_value */
  12267.         19,                /* data_precision */
  12268.         null,            /* numeric_scale */
  12269.         null,            /* column_size */
  12270.         null,            /* literal_prefix */
  12271.         null,            /* literal_suffix */
  12272.         3 /*DB_ALL_EXCEPT_LIKE*/,        /* searchable */
  12273.         0,                /* unsigned_attribute */
  12274.         'bigint'        /* local_type_name */
  12275.         )
  12276.  
  12277.     /* sql_variant */
  12278.     insert into spt_provider_types values
  12279.         (
  12280.         98,                /* ss_dtype */
  12281.         null,            /* fixlen */
  12282.         'sql_variant',    /* type_name */
  12283.         12 /*DBTYPE_VARIANT*/,        /* oledb_data_type */
  12284.         1,                /* best_match */
  12285.         null,            /* is_nullable */
  12286.         0,                /* case_sensitive */
  12287.         0,                /* fixed_prec_scale */
  12288.         0,                /* is_long */
  12289.         0,                /* auto_unique_value */
  12290.         16,                /* data_precision sizeof(VARIANT)*/
  12291.         null,            /* numeric_scale */
  12292.         null,            /* column_size */
  12293.         null,            /* literal_prefix */
  12294.         null,            /* literal_suffix */
  12295.         3 ,                    /*DB_ALL_EXCEPT_LIKE*/
  12296.         null,            /* unsigned_attribute */
  12297.         'sql_variant'    /* local_type_name */
  12298.         )
  12299.     end
  12300.  
  12301.  
  12302.     /* data types changed in Sphinx: DO AFTER ALL TYPES INSERTED! */    
  12303.     update spt_provider_types set ss_dtype = (select xtype from systypes    where type_name like name+'%')
  12304.  
  12305.     end
  12306. commit tran
  12307. go
  12308. dump tran master with no_log
  12309. go
  12310. begin tran
  12311. if (charindex('7.00', @@version) = 0 and
  12312.     charindex('8.00', @@version) = 0)
  12313.     begin
  12314.     /* Local Bit */
  12315.     insert into spt_provider_types values
  12316.         (
  12317.         50 /*SQLBIT*/,            /* ss_dtype */
  12318.         0,                /* fixlen */
  12319.         'bit',                /* type_name */
  12320.         11 /*DBTYPE_BOOL*/,        /* oledb_data_type */
  12321.         1,                /* best_match */
  12322.         0,                /* is_nullable */
  12323.         0,                /* case_sensitive */
  12324.         0,                /* fixed_prec_scale */
  12325.         0,                /* is_long */
  12326.         0,                /* auto_unique_value */
  12327.         1,                /* data_precision */
  12328.         null,                /* numeric_scale */
  12329.         null,                /* column_size */
  12330.         null,                /* literal_prefix */
  12331.         null,                /* literal_suffix */
  12332.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  12333.         null,                /* unsigned_attribute */
  12334.         'bit'                /* local_type_name */
  12335.         )    
  12336.  
  12337.     /* Local Timestamp */
  12338.     insert into spt_provider_types values
  12339.         (
  12340.         45 /*SQLBINARY*/,        /* ss_dtype */
  12341.         8,                /* fixlen */
  12342.         'timestamp',            /* type_name */
  12343.         128 /*DBTYPE_BYTES*/,        /* oledb_data_type */
  12344.         0,                /* best_match */
  12345.         null,                /* is_nullable */
  12346.         0,                /* case_sensitive */
  12347.         0,                /* fixed_prec_scale */
  12348.         0,                /* is_long */
  12349.         0,                /* auto_unique_value */
  12350.         null,                /* data_precision */
  12351.         null,                /* numeric_scale */
  12352.         null,                /* column_size */
  12353.         '0x',                /* literal_prefix */
  12354.         null,                /* literal_suffix */
  12355.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  12356.         null,                /* unsigned_attribute */
  12357.         'timestamp'            /* local_type_name */
  12358.         )        
  12359. end
  12360.  
  12361. commit tran
  12362. go
  12363. dump tran master with no_log
  12364. go
  12365.  
  12366. create unique clustered index datatypeinfoclust on spt_provider_types(ss_dtype,fixlen)
  12367. go
  12368.  
  12369. dump tran master with no_log
  12370. go
  12371. if (charindex('6.00', @@version) > 0)
  12372.     begin
  12373.     if (exists (select * from sysobjects
  12374.         where name = 'spt_provider_types' and type = 'U '))
  12375.         begin
  12376.         drop table spt_provider_types
  12377.         dump tran master with no_log
  12378.         end
  12379.     end
  12380. go
  12381.  
  12382. if (charindex('7.00', @@version) = 0 and
  12383.     charindex('8.00', @@version) = 0)
  12384. begin
  12385.     print ''
  12386.     print ''
  12387.     print 'Warning:'
  12388.     print 'you are installing the stored procedures '
  12389.     print 'on a pre 8.0 SQL Server.'
  12390.     print 'Ignore the following errors.'
  12391. end
  12392. go
  12393.  
  12394.  
  12395. /* 
  12396. ** (2/11/98) 
  12397. ** The following SP is shared by instcat.sql and the server to add information
  12398. ** related to server language collations. The server calls it at the end of
  12399. ** collation change to update the catalog with the new language collations
  12400. ** Everything in this SP should NOT use tempdb.  
  12401. */
  12402.  
  12403. create procedure sp_add_server_sortinfo
  12404. as
  12405. -- spt_server_info has unigue clustered index on attribute_id
  12406. delete spt_server_info where attribute_id in (16,18)
  12407.  
  12408. --insert into spt_server_info
  12409. --    select 18,'COLLATION_SEQ',
  12410. --        'charset='+t2.name+' sort_order='+t1.name
  12411. --        +' charset_num='+rtrim(convert(char(4),t1.csid))+
  12412. --        ' sort_order_num='+rtrim(convert(char(4),t1.id))
  12413. --    from master.dbo.syscharsets t1, master.dbo.syscharsets t2, master.dbo.sysconfigures t3
  12414. --    where t1.csid=t2.id and t1.id=t3.value and t3.config in (123,1123)
  12415. declare @sortid int, @csid int, @sortname varchar(255), @attribute_value varchar(255)
  12416. declare @case_sensitive bit
  12417. declare @ncase_sensitive bit
  12418. select @case_sensitive = case when 'a' <> 'A' then 1 else 0 end
  12419. select @ncase_sensitive = case when N'a' <> N'A' then 1 else 0 end
  12420. select @sortid = value from sysconfigures where config = 1123
  12421. select @csid = csid, @sortname = name from master.dbo.syscharsets where id = @sortid
  12422. select @attribute_value = 'charset='+ name +' sort_order=' + @sortname
  12423.         + ' charset_num=' + rtrim(convert(char(4),@csid))+
  12424.         ' sort_order_num=' + rtrim(convert(char(4),@sortid))
  12425.         from syscharsets where id = @csid
  12426.  
  12427. insert into spt_server_info
  12428.         values (18,'COLLATION_SEQ', isnull(@attribute_value, ' '))
  12429.  
  12430. if @case_sensitive = 1 /* If case sensitive server */
  12431. begin
  12432.     insert into spt_server_info
  12433.         values (16,'IDENTIFIER_CASE','SENSITIVE')
  12434. end
  12435. else
  12436. begin
  12437.     insert into spt_server_info
  12438.         values (16,'IDENTIFIER_CASE','MIXED')
  12439. end
  12440.  
  12441.  
  12442. update spt_datatype_info set CASE_SENSITIVE = @case_sensitive 
  12443.     where DATA_TYPE in (-1, 1, 12) /* CHAR types */
  12444. update spt_datatype_info set CASE_SENSITIVE = @ncase_sensitive
  12445.     where DATA_TYPE in (-10, -9, -8) /* NCHAR types */
  12446. update spt_provider_types set case_sensitive = @case_sensitive 
  12447.     where oledb_data_type = 129 /*DBTYPE_STR*/
  12448. update spt_provider_types set case_sensitive = @ncase_sensitive
  12449.     where oledb_data_type = 130 /*DBTYPE_WSTR*/
  12450. /* sp_add_server_sortinfo */
  12451. go  
  12452.  
  12453.  
  12454. if (charindex('7.00', @@version) > 0)
  12455. begin
  12456.     exec sp_add_server_sortinfo
  12457. end
  12458. go
  12459.  
  12460. -- 8.0 version of sp_add_server_sortinfo
  12461. --
  12462. create procedure sp_add_server_sortinfo75
  12463. as
  12464. -- spt_server_info has unigue clustered index on attribute_id
  12465.     delete spt_server_info where attribute_id in (16, 18)
  12466.  
  12467.     declare @sortid int, @csid int, @sortname varchar(255), @attribute_value varchar(255)
  12468.     declare @case_sensitive bit
  12469.     declare @ncase_sensitive bit
  12470.     select @case_sensitive = case when 'a' <> 'A' then 1 else 0 end
  12471.     select @ncase_sensitive = case when N'a' <> N'A' then 1 else 0 end
  12472.  
  12473.     select @sortid = convert(int, ServerProperty('sqlsortorder')) 
  12474.     select @csid = convert(int, ServerProperty('sqlcharset')) 
  12475.     select @sortname = name from master.dbo.syscharsets where id = @sortid
  12476.  
  12477.     if @sortid = 0 -- Non-SQL Collations
  12478.         BEGIN
  12479.         select @attribute_value = 'charset=' 
  12480.             + name
  12481.             + ' collation=' + isnull(convert(sysname, ServerProperty('collation')), ' ')
  12482.             from syscharsets where id = @csid
  12483.         END
  12484.     else
  12485.         BEGIN
  12486.         select @attribute_value = 'charset='+ name +' sort_order=' + @sortname
  12487.             + ' charset_num=' + rtrim(convert(char(4),@csid))+
  12488.             ' sort_order_num=' + rtrim(convert(char(4),@sortid))
  12489.             from syscharsets where id = @csid
  12490.         END
  12491.  
  12492.     insert into spt_server_info
  12493.             values (18,'COLLATION_SEQ', isnull(@attribute_value, ' '))
  12494.  
  12495.     if @case_sensitive = 1 /* If case sensitive server */
  12496.     begin
  12497.         insert into spt_server_info
  12498.             values (16,'IDENTIFIER_CASE','SENSITIVE')
  12499.     end
  12500.     else
  12501.     begin
  12502.         insert into spt_server_info
  12503.             values (16,'IDENTIFIER_CASE','MIXED')
  12504.     end
  12505.  
  12506.  
  12507.     update spt_datatype_info set CASE_SENSITIVE = @case_sensitive 
  12508.         where DATA_TYPE in (-1, 1, 12) /* CHAR types */
  12509.     update spt_datatype_info set CASE_SENSITIVE = @ncase_sensitive
  12510.         where DATA_TYPE in (-10, -9, -8) /* NCHAR types */
  12511.     update spt_provider_types set case_sensitive = @case_sensitive 
  12512.         where oledb_data_type = 129 /*DBTYPE_STR*/
  12513.     update spt_provider_types set case_sensitive = @ncase_sensitive
  12514.         where oledb_data_type = 130 /*DBTYPE_WSTR*/
  12515. /* sp_add_server_info75 */
  12516. go 
  12517.  
  12518.  
  12519.  
  12520. if (charindex('8.00', @@version) > 0)
  12521. begin
  12522.     exec sp_add_server_sortinfo75
  12523. end
  12524. go
  12525.  
  12526. print ''
  12527. print 'creating sp_catalogs_rowset'
  12528. go
  12529.  
  12530. create procedure sp_catalogs_rowset
  12531.     (
  12532.     @catalog_name        varchar(255)
  12533.     )        
  12534. as
  12535.     select
  12536.         CATALOG_NAME    = name,
  12537.         DESCRIPTION    = convert(varchar(1),null)
  12538.     from     master.dbo.sysdatabases
  12539.     where    name = @catalog_name
  12540. go
  12541. dump tran master with no_log
  12542. go
  12543. create procedure sp_catalogs_rowset;2
  12544.     (
  12545.     @dummy        int /* remove when Hydra bug 17032 is fixed */
  12546.     )
  12547. as
  12548.     select
  12549.         CATALOG_NAME    = name,
  12550.         DESCRIPTION    = convert(varchar(1),null)
  12551.     from     master.dbo.sysdatabases
  12552.     order by 1
  12553. go
  12554. dump tran master with no_log
  12555. go
  12556.  
  12557. if (charindex('7.00', @@version) = 0 and
  12558.     charindex('8.00', @@version) = 0)
  12559. begin
  12560.     print ''
  12561.     print ''
  12562.     print 'Warning:'
  12563.     print 'you are installing the stored procedures '
  12564.     print 'on a pre 8.0 SQL Server.'
  12565.     print 'Ignore the following errors.'
  12566. end
  12567. else
  12568.     drop proc sp_catalogs_rowset
  12569. go
  12570.  
  12571. /*    Procedure for 8.0 server */
  12572. create procedure sp_catalogs_rowset
  12573.     (
  12574.     @catalog_name        sysname
  12575.     )        
  12576. as
  12577.     select
  12578.         CATALOG_NAME    = name,
  12579.         DESCRIPTION    = convert(nvarchar(1),null)
  12580.     from     master.dbo.sysdatabases
  12581.     where    name = @catalog_name
  12582. go
  12583. dump tran master with no_log
  12584. go
  12585. create procedure sp_catalogs_rowset;2
  12586.     (
  12587.     @dummy        int /* remove when Hydra bug 17032 is fixed */
  12588.     )
  12589. as
  12590.     select
  12591.         CATALOG_NAME    = name,
  12592.         DESCRIPTION    = convert(nvarchar(1),null)
  12593.     from     master.dbo.sysdatabases
  12594.     order by 1
  12595. go
  12596. dump tran master with no_log
  12597. go
  12598. create procedure sp_catalogs_rowset;5
  12599.     (
  12600.     @server_name    sysname,
  12601.     @catalog_name    sysname = NULL
  12602.     )
  12603. as
  12604.     select    CATALOG_NAME,
  12605.         DESCRIPTION
  12606.     from master.dbo.SYSREMOTE_CATALOGS < @server_name, @catalog_name >
  12607.     order by 1
  12608. go
  12609.  
  12610. grant execute on sp_catalogs_rowset to public
  12611. go
  12612.  
  12613. dump tran master with no_log
  12614. go
  12615. if (charindex('6.00', @@version) > 0)
  12616.     begin
  12617.     if (exists (select * from sysobjects
  12618.             where name = 'sp_catalogs_rowset' and type = 'P '))
  12619.         begin
  12620.         drop procedure sp_catalogs_rowset
  12621.         dump tran master with no_log
  12622.         end
  12623.     end
  12624. go
  12625.  
  12626.  
  12627. print ''
  12628. print 'creating sp_column_privileges_rowset'
  12629. go
  12630.  
  12631. /*    Procedure for 6.0 and 6.5 server */
  12632. CREATE PROCEDURE sp_column_privileges_rowset
  12633.     (
  12634.     @table_name     varchar(255) = null,
  12635.     @table_schema    varchar(255) = null,
  12636.     @column_name    varchar(255) = null,
  12637.     @grantor    varchar(255) = null,
  12638.     @grantee    varchar(255) = null
  12639.     )
  12640. as
  12641. IF @table_name is not null
  12642.     BEGIN
  12643.     select
  12644.         GRANTOR        = user_name(p.grantor),
  12645.         GRANTEE        = user_name(u.uid),
  12646.         TABLE_CATALOG    = db_name(),
  12647.         TABLE_SCHEMA    = user_name(o.uid),
  12648.         TABLE_NAME    = o.name,
  12649.         COLUMN_NAME    = c.name,
  12650.         COLUMN_GUID    = convert(binary(16),null),
  12651.         COLUMN_PROPID    = convert(int,null),
  12652.         PRIVILEGE_TYPE    = convert(varchar(30),
  12653.                     case p.action
  12654.                     when 193 then 'SELECT'
  12655.                     when 195 then 'INSERT'
  12656.                     when 197 then 'UPDATE'
  12657.                     else 'REFERENCES'
  12658.                     end),
  12659.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  12660.     from 
  12661.         sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c
  12662.     where
  12663.         o.name = @table_name
  12664.     and     o.type in ('U','V','S')
  12665.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  12666.     and    c.id = o.id
  12667.     and     (@column_name is null or @column_name = c.name)
  12668.     and     c.id = p.id
  12669.     and    (@grantor is null or @grantor = user_name(p.grantor))
  12670.     and     case 
  12671.             when substring(p.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  12672.             when substring(p.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p.columns, v.low, 1))
  12673.             else (~convert(tinyint, coalesce(substring(p.columns, v.low, 1),0)))
  12674.             end & v.high <> 0            /* permission applies to this column */
  12675.     and     v.number <= (select count(*) from syscolumns where id = o.id)    /* ranges from 1 to # of columns in table */
  12676.     and     v.type = 'P'
  12677.     and     v.number = c.colid
  12678.     and    (@grantee is null or @grantee = user_name(u.uid))
  12679.             /* expand groups */
  12680.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  12681.     and     p.protecttype <> 206    /* only grant rows */
  12682.     and     p.action in (26,193,195,197)
  12683.     and     o.uid <> u.uid            /* no rows for owner */
  12684.     and     not exists (            /* exclude revoke'd privileges */
  12685.             select *
  12686.             from sysprotects p1
  12687.             where
  12688.                 p1.protecttype = 206
  12689.             and     p1.action = p.action
  12690.             and     p1.id = p.id
  12691.             and     p1.uid = u.uid
  12692.             and     case 
  12693.                 when substring(p1.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  12694.                 when substring(p1.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  12695.                                 else (~convert(tinyint,coalesce(substring(p.columns, v.low, 1),0)))
  12696.                 end & v.high <> 0)            /* permission applies to this column */
  12697.     union
  12698.     select    /*    Add rows for table owner */
  12699.         GRANTOR        = user_name(u.uid),
  12700.         GRANTEE        = user_name(o.uid),
  12701.         TABLE_CATALOG    = db_name(),
  12702.         TABLE_SCHEMA    = user_name(o.uid),
  12703.         TABLE_NAME    = o.name,
  12704.         COLUMN_NAME    = c.name,
  12705.         COLUMN_GUID    = convert(binary(16),null),
  12706.         COLUMN_PROPID    = convert(int,null),
  12707.         PRIVILEGE_TYPE    = convert(varchar(30),
  12708.                     case v.number
  12709.                     when 193 then 'SELECT'
  12710.                     when 195 then 'INSERT'
  12711.                     when 197 then 'UPDATE'
  12712.                     else 'REFERENCES'
  12713.                     end),
  12714.         IS_GRANTABLE    = convert(bit,1)    
  12715.     from 
  12716.         sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  12717.     where
  12718.         o.name = @table_name
  12719.     and     o.type in ('U','V','S')
  12720.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  12721.     and    (@grantee is null or @grantee = user_name(o.uid))
  12722.     and    c.id = o.id
  12723.     and     (@column_name is null or @column_name = c.name)
  12724.     and     u.uid = 1        /* grantor is dbo of database */
  12725.     and    (@grantor is null or @grantor = user_name(u.uid))
  12726.     and     v.type = 'P'        /* cross product to get all exposed privileges */
  12727.     and     v.number in (26,193,195,197)
  12728.     and     not exists (        /* exclude revoke'd privileges */
  12729.             select *
  12730.             from     sysprotects p1
  12731.             where    p1.protecttype = 206
  12732.             and     p1.action = v.number
  12733.             and     p1.id = o.id
  12734.             and     p1.uid = o.uid)
  12735.     order by 4, 5, 6, 9, 1, 2
  12736.     END
  12737. ELSE
  12738.     BEGIN
  12739.     select
  12740.         GRANTOR        = user_name(p.grantor),
  12741.         GRANTEE        = user_name(u.uid),
  12742.         TABLE_CATALOG    = db_name(),
  12743.         TABLE_SCHEMA    = user_name(o.uid),
  12744.         TABLE_NAME    = o.name,
  12745.         COLUMN_NAME    = c.name,
  12746.         COLUMN_GUID    = convert(binary(16),null),
  12747.         COLUMN_PROPID    = convert(int,null),
  12748.         PRIVILEGE_TYPE    = convert(varchar(30),
  12749.                     case p.action
  12750.                     when 193 then 'SELECT'
  12751.                     when 195 then 'INSERT'
  12752.                     when 197 then 'UPDATE'
  12753.                     else 'REFERENCES'
  12754.                     end),
  12755.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  12756.     from 
  12757.         sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c
  12758.     where
  12759.         o.type in ('U','V','S')
  12760.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  12761.     and    c.id = o.id
  12762.     and     (@column_name is null or @column_name = c.name)
  12763.     and     c.id = p.id
  12764.     and    (@grantor is null or @grantor = user_name(p.grantor))
  12765.     and     case 
  12766.             when substring(p.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  12767.             when substring(p.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p.columns, v.low, 1))
  12768.             else (~convert(tinyint, coalesce(substring(p.columns, v.low, 1),0)))
  12769.             end & v.high <> 0            /* permission applies to this column */
  12770.     and     v.number <= (select count(*) from syscolumns where id = o.id)    /* ranges from 1 to # of columns in table */
  12771.     and     v.type = 'P'
  12772.     and     v.number = c.colid
  12773.     and    (@grantee is null or @grantee = user_name(u.uid))
  12774.             /* expand groups */
  12775.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  12776.     and     p.protecttype <> 206    /* only grant rows */
  12777.     and     p.action in (26,193,195,197)
  12778.     and     o.uid <> u.uid            /* no rows for owner */
  12779.     and     not exists (            /* exclude revoke'd privileges */
  12780.             select *
  12781.             from sysprotects p1
  12782.             where
  12783.                 p1.protecttype = 206
  12784.             and     p1.action = p.action
  12785.             and     p1.id = p.id
  12786.             and     p1.uid = u.uid
  12787.             and     case 
  12788.                 when substring(p1.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  12789.                 when substring(p1.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  12790.                                 else (~convert(tinyint,coalesce(substring(p.columns, v.low, 1),0)))
  12791.                 end & v.high <> 0)            /* permission applies to this column */
  12792.     union
  12793.     select    /*    Add rows for table owner */
  12794.         GRANTOR        = user_name(u.uid),
  12795.         GRANTEE        = user_name(o.uid),
  12796.         TABLE_CATALOG    = db_name(),
  12797.         TABLE_SCHEMA    = user_name(o.uid),
  12798.         TABLE_NAME    = o.name,
  12799.         COLUMN_NAME    = c.name,
  12800.         COLUMN_GUID    = convert(binary(16),null),
  12801.         COLUMN_PROPID    = convert(int,null),
  12802.         PRIVILEGE_TYPE    = convert(varchar(30),
  12803.                     case v.number
  12804.                     when 193 then 'SELECT'
  12805.                     when 195 then 'INSERT'
  12806.                     when 197 then 'UPDATE'
  12807.                     else 'REFERENCES'
  12808.                     end),
  12809.         IS_GRANTABLE    = convert(bit,1)    
  12810.     from 
  12811.         sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  12812.     where
  12813.          o.type in ('U','V','S')
  12814.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  12815.     and    (@grantee is null or @grantee = user_name(o.uid))
  12816.     and    c.id = o.id
  12817.     and     (@column_name is null or @column_name = c.name)
  12818.     and     u.uid = 1        /* grantor is dbo of database */
  12819.     and    (@grantor is null or @grantor = user_name(u.uid))
  12820.     and     v.type = 'P'        /* cross product to get all exposed privileges */
  12821.     and     v.number in (26,193,195,197)
  12822.     and     not exists (        /* exclude revoke'd privileges */
  12823.             select *
  12824.             from     sysprotects p1
  12825.             where    p1.protecttype = 206
  12826.             and     p1.action = v.number
  12827.             and     p1.id = o.id
  12828.             and     p1.uid = o.uid)
  12829.     order by 4, 5, 6, 9, 1, 2
  12830.     END
  12831. go
  12832. dump tran master with no_log
  12833. go
  12834. CREATE PROCEDURE sp_column_privileges_rowset;2
  12835.     (
  12836.        @handle        int output,
  12837.        @scrollopt    int output,
  12838.     @ccopt        int output,
  12839.     @rows        int output,
  12840.     @table_name     varchar(255) = null,
  12841.     @table_schema    varchar(255) = null,
  12842.     @column_name    varchar(255) = null,
  12843.     @grantor    varchar(255) = null,
  12844.     @grantee    varchar(255) = null
  12845.     )
  12846. as
  12847.  
  12848. declare @ret int
  12849.  
  12850. SET NOCOUNT ON
  12851.     
  12852. create table #spcprivsrowset1
  12853.     (
  12854.     GRANTOR        sysname not null,
  12855.     GRANTEE        sysname not null,
  12856.     TABLE_CATALOG    sysname not null,
  12857.     TABLE_SCHEMA    sysname not null,
  12858.     TABLE_NAME    sysname not null,
  12859.     COLUMN_NAME    sysname not null,
  12860.     COLUMN_GUID    binary(16) null,
  12861.     COLUMN_PROPID    int null,
  12862.     PRIVILEGE_TYPE    sysname not null,
  12863.     IS_GRANTABLE    bit not null
  12864.     )
  12865.  
  12866. IF @table_name is not null
  12867.     BEGIN
  12868.     insert into #spcprivsrowset1
  12869.     select
  12870.         GRANTOR        = user_name(p.grantor),
  12871.         GRANTEE        = user_name(u.uid),
  12872.         TABLE_CATALOG    = db_name(),
  12873.         TABLE_SCHEMA    = user_name(o.uid),
  12874.         TABLE_NAME    = o.name,
  12875.         COLUMN_NAME    = c.name,
  12876.         COLUMN_GUID    = convert(binary(16),null),
  12877.         COLUMN_PROPID    = convert(int,null),
  12878.         PRIVILEGE_TYPE    = convert(varchar(30),
  12879.                     case p.action
  12880.                     when 193 then 'SELECT'
  12881.                     when 195 then 'INSERT'
  12882.                     when 197 then 'UPDATE'
  12883.                     else 'REFERENCES'
  12884.                     end),
  12885.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  12886.     from 
  12887.         sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c
  12888.     where
  12889.         o.name = @table_name
  12890.     and     o.type in ('U','V','S')
  12891.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  12892.     and    c.id = o.id
  12893.     and     (@column_name is null or @column_name = c.name)
  12894.     and     c.id = p.id
  12895.     and    (@grantor is null or @grantor = user_name(p.grantor))
  12896.     and     case 
  12897.             when substring(p.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  12898.             when substring(p.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p.columns, v.low, 1))
  12899.             else (~convert(tinyint, coalesce(substring(p.columns, v.low, 1),0)))
  12900.             end & v.high <> 0            /* permission applies to this column */
  12901.     and     v.number <= (select count(*) from syscolumns where id = o.id)    /* ranges from 1 to # of columns in table */
  12902.     and     v.type = 'P'
  12903.     and     v.number = c.colid
  12904.     and    (@grantee is null or @grantee = user_name(u.uid))
  12905.             /* expand groups */
  12906.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  12907.     and     p.protecttype <> 206    /* only grant rows */
  12908.     and     p.action in (26,193,195,197)
  12909.     and     o.uid <> u.uid            /* no rows for owner */
  12910.     and     not exists (            /* exclude revoke'd privileges */
  12911.             select *
  12912.             from sysprotects p1
  12913.             where
  12914.                 p1.protecttype = 206
  12915.             and     p1.action = p.action
  12916.             and     p1.id = p.id
  12917.             and     p1.uid = u.uid
  12918.             and     case 
  12919.                 when substring(p1.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  12920.                 when substring(p1.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  12921.                                 else (~convert(tinyint,coalesce(substring(p.columns, v.low, 1),0)))
  12922.                 end & v.high <> 0)            /* permission applies to this column */
  12923.     union
  12924.     select    /*    Add rows for table owner */
  12925.         GRANTOR        = user_name(u.uid),
  12926.         GRANTEE        = user_name(o.uid),
  12927.         TABLE_CATALOG    = db_name(),
  12928.         TABLE_SCHEMA    = user_name(o.uid),
  12929.         TABLE_NAME    = o.name,
  12930.         COLUMN_NAME    = c.name,
  12931.         COLUMN_GUID    = convert(binary(16),null),
  12932.         COLUMN_PROPID    = convert(int,null),
  12933.         PRIVILEGE_TYPE    = convert(varchar(30),
  12934.                     case v.number
  12935.                     when 193 then 'SELECT'
  12936.                     when 195 then 'INSERT'
  12937.                     when 197 then 'UPDATE'
  12938.                     else 'REFERENCES'
  12939.                     end),
  12940.         IS_GRANTABLE    = convert(bit,1)    
  12941.     from 
  12942.         sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  12943.     where
  12944.         o.name = @table_name
  12945.     and     o.type in ('U','V','S')
  12946.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  12947.     and    (@grantee is null or @grantee = user_name(o.uid))
  12948.     and    c.id = o.id
  12949.     and     (@column_name is null or @column_name = c.name)
  12950.     and     u.uid = 1        /* grantor is dbo of database */
  12951.     and    (@grantor is null or @grantor = user_name(u.uid))
  12952.     and     v.type = 'P'        /* cross product to get all exposed privileges */
  12953.     and     v.number in (26,193,195,197)
  12954.     and     not exists (        /* exclude revoke'd privileges */
  12955.             select *
  12956.             from     sysprotects p1
  12957.             where    p1.protecttype = 206
  12958.             and     p1.action = v.number
  12959.             and     p1.id = o.id
  12960.             and     p1.uid = o.uid)
  12961.     order by 4, 5, 6, 9, 1, 2
  12962.     END
  12963. ELSE
  12964.     BEGIN
  12965.     insert into #spcprivsrowset1
  12966.     select
  12967.         GRANTOR        = user_name(p.grantor),
  12968.         GRANTEE        = user_name(u.uid),
  12969.         TABLE_CATALOG    = db_name(),
  12970.         TABLE_SCHEMA    = user_name(o.uid),
  12971.         TABLE_NAME    = o.name,
  12972.         COLUMN_NAME    = c.name,
  12973.         COLUMN_GUID    = convert(binary(16),null),
  12974.         COLUMN_PROPID    = convert(int,null),
  12975.         PRIVILEGE_TYPE    = convert(varchar(30),
  12976.                     case p.action
  12977.                     when 193 then 'SELECT'
  12978.                     when 195 then 'INSERT'
  12979.                     when 197 then 'UPDATE'
  12980.                     else 'REFERENCES'
  12981.                     end),
  12982.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  12983.     from 
  12984.         sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c
  12985.     where
  12986.         o.type in ('U','V','S')
  12987.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  12988.     and    c.id = o.id
  12989.     and     (@column_name is null or @column_name = c.name)
  12990.     and     c.id = p.id
  12991.     and    (@grantor is null or @grantor = user_name(p.grantor))
  12992.     and     case 
  12993.             when substring(p.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  12994.             when substring(p.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p.columns, v.low, 1))
  12995.             else (~convert(tinyint, coalesce(substring(p.columns, v.low, 1),0)))
  12996.             end & v.high <> 0            /* permission applies to this column */
  12997.     and     v.number <= (select count(*) from syscolumns where id = o.id)    /* ranges from 1 to # of columns in table */
  12998.     and     v.type = 'P'
  12999.     and     v.number = c.colid
  13000.     and    (@grantee is null or @grantee = user_name(u.uid))
  13001.             /* expand groups */
  13002.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  13003.     and     p.protecttype <> 206    /* only grant rows */
  13004.     and     p.action in (26,193,195,197)
  13005.     and     o.uid <> u.uid            /* no rows for owner */
  13006.     and     not exists (            /* exclude revoke'd privileges */
  13007.             select *
  13008.             from sysprotects p1
  13009.             where
  13010.                 p1.protecttype = 206
  13011.             and     p1.action = p.action
  13012.             and     p1.id = p.id
  13013.             and     p1.uid = u.uid
  13014.             and     case 
  13015.                 when substring(p1.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  13016.                 when substring(p1.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  13017.                                 else (~convert(tinyint,coalesce(substring(p.columns, v.low, 1),0)))
  13018.                 end & v.high <> 0)            /* permission applies to this column */
  13019.     union
  13020.     select    /*    Add rows for table owner */
  13021.         GRANTOR        = user_name(u.uid),
  13022.         GRANTEE        = user_name(o.uid),
  13023.         TABLE_CATALOG    = db_name(),
  13024.         TABLE_SCHEMA    = user_name(o.uid),
  13025.         TABLE_NAME    = o.name,
  13026.         COLUMN_NAME    = c.name,
  13027.         COLUMN_GUID    = convert(binary(16),null),
  13028.         COLUMN_PROPID    = convert(int,null),
  13029.         PRIVILEGE_TYPE    = convert(varchar(30),
  13030.                     case v.number
  13031.                     when 193 then 'SELECT'
  13032.                     when 195 then 'INSERT'
  13033.                     when 197 then 'UPDATE'
  13034.                     else 'REFERENCES'
  13035.                     end),
  13036.         IS_GRANTABLE    = convert(bit,1)    
  13037.     from 
  13038.         sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  13039.     where
  13040.          o.type in ('U','V','S')
  13041.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  13042.     and    (@grantee is null or @grantee = user_name(o.uid))
  13043.     and    c.id = o.id
  13044.     and     (@column_name is null or @column_name = c.name)
  13045.     and     u.uid = 1        /* grantor is dbo of database */
  13046.     and    (@grantor is null or @grantor = user_name(u.uid))
  13047.     and     v.type = 'P'        /* cross product to get all exposed privileges */
  13048.     and     v.number in (26,193,195,197)
  13049.     and     not exists (        /* exclude revoke'd privileges */
  13050.             select *
  13051.             from     sysprotects p1
  13052.             where    p1.protecttype = 206
  13053.             and     p1.action = v.number
  13054.             and     p1.id = o.id
  13055.             and     p1.uid = o.uid)
  13056.     order by 4, 5, 6, 9, 1, 2
  13057.     END
  13058.  
  13059. exec @ret = sp_cursoropen @handle output, 'select * from #spcprivsrowset1',
  13060.     @scrollopt output, @ccopt output, @rows output
  13061.  
  13062. drop table #spcprivsrowset1
  13063.  
  13064. return isnull(@ret,0)
  13065. go
  13066. dump tran master with no_log
  13067. go
  13068. CREATE PROCEDURE sp_column_privileges_rowset;3
  13069. as
  13070.     select
  13071.         GRANTOR        = convert(sysname,' '),
  13072.         GRANTEE        = convert(sysname,' '),
  13073.         TABLE_CATALOG    = convert(sysname,' '),
  13074.         TABLE_SCHEMA    = convert(sysname,' '),
  13075.         TABLE_NAME    = convert(sysname,' '),
  13076.         COLUMN_NAME    = convert(sysname,' '),
  13077.         COLUMN_GUID    = convert(binary(16),null),
  13078.         COLUMN_PROPID    = convert(int,null),
  13079.         PRIVILEGE_TYPE    = convert(varchar(30),' '),
  13080.         IS_GRANTABLE    = convert(bit,1)
  13081.     where    1=0
  13082. go
  13083. dump tran master with no_log
  13084. go
  13085.  
  13086. if (charindex('7.00', @@version) = 0 and
  13087.     charindex('8.00', @@version) = 0)
  13088. begin
  13089.     print ''
  13090.     print ''
  13091.     print 'Warning:'
  13092.     print 'you are installing the stored procedures '
  13093.     print 'on a pre 8.0 SQL Server.'
  13094.     print 'Ignore the following errors.'
  13095. end
  13096. else
  13097.     drop proc sp_column_privileges_rowset
  13098. go
  13099.  
  13100.  
  13101. /*    Procedure for 8.0 server */
  13102. CREATE PROCEDURE sp_column_privileges_rowset
  13103.     (
  13104.     @table_name         sysname,
  13105.     @table_schema        sysname = null,
  13106.     @column_name        sysname = null,
  13107.     @grantor        sysname = null,
  13108.     @grantee        sysname = null
  13109.     )
  13110. as
  13111.     select
  13112.         GRANTOR        = user_name(p.grantor),
  13113.         GRANTEE        = user_name(u.uid),
  13114.         TABLE_CATALOG    = db_name(),
  13115.         TABLE_SCHEMA    = user_name(o.uid),
  13116.         TABLE_NAME    = o.name,
  13117.         COLUMN_NAME    = c.name,
  13118.         COLUMN_GUID    = convert(uniqueidentifier,null),
  13119.         COLUMN_PROPID    = convert(int,null),
  13120.         PRIVILEGE_TYPE    = convert(nvarchar(30),
  13121.                     case p.action
  13122.                     when 193 then N'SELECT'
  13123.                     when 195 then N'INSERT'
  13124.                     when 197 then N'UPDATE'
  13125.                     else N'REFERENCES'
  13126.                     end),
  13127.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  13128.     from 
  13129.         sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c, sysmembers m
  13130.     where
  13131.         o.name = @table_name
  13132.     and     o.type in ('U','V','S')
  13133.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  13134.     and    c.id = o.id
  13135.     and     (@column_name is null or @column_name = c.name)
  13136.     and     c.id = p.id
  13137.     and    (@grantor is null or @grantor = user_name(p.grantor))
  13138.     and     case 
  13139.             when substring(p.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  13140.             when substring(p.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p.columns, v.low, 1))
  13141.             else (~convert(tinyint, coalesce(substring(p.columns, v.low, 1),0)))
  13142.             end & v.high <> 0            /* permission applies to this column */
  13143.     and     v.number <= (select count(*) from syscolumns where id = o.id)    /* ranges from 1 to # of columns in table */
  13144.     and     v.type = 'P'
  13145.     and     v.number = c.colid
  13146.     and    (@grantee is null or @grantee = user_name(u.uid))
  13147.         /* expand groups - AKUNDONE: only 1 level of group unrolling here. Need more?? */
  13148.     and     (u.uid > 0 and u.uid < 16384)
  13149.     and     ((p.uid = u.uid) or  (p.uid = m.groupuid and u.uid = m.memberuid))    
  13150.     and     p.protecttype <> 206    /* only grant rows */
  13151.     and     p.action in (26,193,195,197)
  13152.     and     o.uid <> u.uid            /* no rows for owner */
  13153.     and     not exists (            /* exclude revoke'd privileges */
  13154.             select *
  13155.             from sysprotects p1
  13156.             where
  13157.                 p1.protecttype = 206
  13158.             and     p1.action = p.action
  13159.             and     p1.id = p.id
  13160.             and     p1.uid = u.uid
  13161.             and     case 
  13162.                 when substring(p1.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  13163.                 when substring(p1.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  13164.                                 else (~convert(tinyint,coalesce(substring(p.columns, v.low, 1),0)))
  13165.                 end & v.high <> 0)            /* permission applies to this column */
  13166.     union
  13167.     select    /*    Add rows for table owner */
  13168.         GRANTOR        = user_name(u.uid),
  13169.         GRANTEE        = user_name(o.uid),
  13170.         TABLE_CATALOG    = db_name(),
  13171.         TABLE_SCHEMA    = user_name(o.uid),
  13172.         TABLE_NAME    = o.name,
  13173.         COLUMN_NAME    = c.name,
  13174.         COLUMN_GUID    = convert(uniqueidentifier,null),
  13175.         COLUMN_PROPID    = convert(int,null),
  13176.         PRIVILEGE_TYPE    = convert(nvarchar(30),
  13177.                     case v.number
  13178.                     when 193 then N'SELECT'
  13179.                     when 195 then N'INSERT'
  13180.                     when 197 then N'UPDATE'
  13181.                     else N'REFERENCES'
  13182.                     end),
  13183.         IS_GRANTABLE    = convert(bit,1)    
  13184.     from 
  13185.         sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  13186.     where
  13187.         o.name = @table_name
  13188.     and     o.type in ('U','V','S')
  13189.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  13190.     and    (@grantee is null or @grantee = user_name(o.uid))
  13191.     and    c.id = o.id
  13192.     and     (@column_name is null or @column_name = c.name)
  13193.     and     u.uid = 1        /* grantor is dbo of database */
  13194.     and    (@grantor is null or @grantor = user_name(u.uid))
  13195.     and     v.type = 'P'        /* cross product to get all exposed privileges */
  13196.     and     v.number in (26,193,195,197)
  13197.     and     not exists (        /* exclude revoke'd privileges */
  13198.             select *
  13199.             from     sysprotects p1
  13200.             where    p1.protecttype = 206
  13201.             and     p1.action = v.number
  13202.             and     p1.id = o.id
  13203.             and     p1.uid = o.uid)
  13204.     order by 4, 5, 6, 9, 1, 2
  13205. go
  13206. dump tran master with no_log
  13207. go
  13208. CREATE PROCEDURE sp_column_privileges_rowset;2
  13209.     (
  13210.     @table_schema        sysname = null,
  13211.     @column_name        sysname = null,
  13212.     @grantor        sysname = null,
  13213.     @grantee        sysname = null
  13214.     )
  13215. as
  13216.     select
  13217.         GRANTOR        = user_name(p.grantor),
  13218.         GRANTEE        = user_name(u.uid),
  13219.         TABLE_CATALOG    = db_name(),
  13220.         TABLE_SCHEMA    = user_name(o.uid),
  13221.         TABLE_NAME    = o.name,
  13222.         COLUMN_NAME    = c.name,
  13223.         COLUMN_GUID    = convert(uniqueidentifier,null),
  13224.         COLUMN_PROPID    = convert(int,null),
  13225.         PRIVILEGE_TYPE    = convert(nvarchar(30),
  13226.                     case p.action
  13227.                     when 193 then N'SELECT'
  13228.                     when 195 then N'INSERT'
  13229.                     when 197 then N'UPDATE'
  13230.                     else N'REFERENCES'
  13231.                     end),
  13232.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  13233.     from 
  13234.         sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c, sysmembers m
  13235.     where
  13236.         o.type in ('U','V','S')
  13237.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  13238.     and    c.id = o.id
  13239.     and     (@column_name is null or @column_name = c.name)
  13240.     and     c.id = p.id
  13241.     and    (@grantor is null or @grantor = user_name(p.grantor))
  13242.     and     case 
  13243.             when substring(p.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  13244.             when substring(p.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p.columns, v.low, 1))
  13245.             else (~convert(tinyint, coalesce(substring(p.columns, v.low, 1),0)))
  13246.             end & v.high <> 0            /* permission applies to this column */
  13247.     and     v.number <= (select count(*) from syscolumns where id = o.id)    /* ranges from 1 to # of columns in table */
  13248.     and     v.type = 'P'
  13249.     and     v.number = c.colid
  13250.     and    (@grantee is null or @grantee = user_name(u.uid))
  13251.         /* expand groups - AKUNDONE: only 1 level of group unrolling here. Need more?? */
  13252.     and     (u.uid > 0 and u.uid < 16384)
  13253.     and     ((p.uid = u.uid) or  (p.uid = m.groupuid and u.uid = m.memberuid))    
  13254.     and     p.protecttype <> 206    /* only grant rows */
  13255.     and     p.action in (26,193,195,197)
  13256.     and     o.uid <> u.uid            /* no rows for owner */
  13257.     and     not exists (            /* exclude revoke'd privileges */
  13258.             select *
  13259.             from sysprotects p1
  13260.             where
  13261.                 p1.protecttype = 206
  13262.             and     p1.action = p.action
  13263.             and     p1.id = p.id
  13264.             and     p1.uid = u.uid
  13265.             and     case 
  13266.                 when substring(p1.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  13267.                 when substring(p1.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  13268.                                 else (~convert(tinyint,coalesce(substring(p.columns, v.low, 1),0)))
  13269.                 end & v.high <> 0)            /* permission applies to this column */
  13270.     union
  13271.     select    /*    Add rows for table owner */
  13272.         GRANTOR        = user_name(u.uid),
  13273.         GRANTEE        = user_name(o.uid),
  13274.         TABLE_CATALOG    = db_name(),
  13275.         TABLE_SCHEMA    = user_name(o.uid),
  13276.         TABLE_NAME    = o.name,
  13277.         COLUMN_NAME    = c.name,
  13278.         COLUMN_GUID    = convert(uniqueidentifier,null),
  13279.         COLUMN_PROPID    = convert(int,null),
  13280.         PRIVILEGE_TYPE    = convert(nvarchar(30),
  13281.                     case v.number
  13282.                     when 193 then N'SELECT'
  13283.                     when 195 then N'INSERT'
  13284.                     when 197 then N'UPDATE'
  13285.                     else N'REFERENCES'
  13286.                     end),
  13287.         IS_GRANTABLE    = convert(bit,1)    
  13288.     from 
  13289.         sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  13290.     where
  13291.         o.type in ('U','V','S')
  13292.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  13293.     and    (@grantee is null or @grantee = user_name(o.uid))
  13294.     and    c.id = o.id
  13295.     and     (@column_name is null or @column_name = c.name)
  13296.     and     u.uid = 1        /* grantor is dbo of database */
  13297.     and    (@grantor is null or @grantor = user_name(u.uid))
  13298.     and     v.type = 'P'        /* cross product to get all exposed privileges */
  13299.     and     v.number in (26,193,195,197)
  13300.     and     not exists (        /* exclude revoke'd privileges */
  13301.             select *
  13302.             from     sysprotects p1
  13303.             where    p1.protecttype = 206
  13304.             and     p1.action = v.number
  13305.             and     p1.id = o.id
  13306.             and     p1.uid = o.uid)
  13307.     order by 4, 5, 6, 9, 1, 2
  13308. go
  13309. dump tran master with no_log
  13310. go
  13311. create procedure sp_column_privileges_rowset;5
  13312.     (
  13313.     @table_server        sysname,
  13314.     @table_catalog        sysname = null,
  13315.     @table_name        sysname = null,
  13316.     @table_schema        sysname = null,
  13317.     @column_name        sysname = null,
  13318.     @grantor        sysname = null,
  13319.     @grantee        sysname = null
  13320.     )
  13321. as
  13322.     select
  13323.         GRANTOR,
  13324.         GRANTEE,
  13325.         TABLE_CATALOG,
  13326.         TABLE_SCHEMA,
  13327.         TABLE_NAME,
  13328.         COLUMN_NAME,
  13329.         COLUMN_GUID,
  13330.         COLUMN_PROPID,    
  13331.         PRIVILEGE_TYPE,
  13332.         IS_GRANTABLE    
  13333.         from master.dbo.SYSREMOTE_COLUMN_PRIVILEGES <
  13334.                 @table_server,
  13335.                 @table_catalog,
  13336.                 @table_schema,
  13337.                 @table_name,
  13338.                 @column_name,
  13339.                 @grantor,
  13340.                 @grantee>
  13341.     order by 3, 4, 5, 6, 9, 1, 2
  13342. go
  13343.  
  13344. grant execute on sp_column_privileges_rowset to public
  13345. go
  13346.  
  13347. dump tran master with no_log
  13348. go
  13349. if (charindex('6.00', @@version) > 0)
  13350.     begin
  13351.     if (exists (select * from sysobjects
  13352.             where name = 'sp_column_privileges_rowset' and type = 'P '))
  13353.         begin
  13354.         drop procedure sp_column_privileges_rowset
  13355.         dump tran master with no_log
  13356.         end
  13357.     end
  13358. go
  13359.  
  13360.  
  13361. print ''
  13362. print 'creating sp_columns_rowset'
  13363. go
  13364.  
  13365. /*    Procedure for 6.0 and 6.50 server */
  13366. create procedure sp_columns_rowset
  13367.     (
  13368.        @table_name    varchar(255),
  13369.     @table_schema     varchar(255) = null,
  13370.     @column_name    varchar(255) = null
  13371.     )
  13372. as     
  13373.     select
  13374.         TABLE_CATALOG        = db_name(),
  13375.         TABLE_SCHEMA        = user_name(o.uid),
  13376.         TABLE_NAME        = o.name,
  13377.         COLUMN_NAME        = c.name,
  13378.         COLUMN_GUID        = convert(binary(16),null),
  13379.         COLUMN_PROPID        = convert(int,null),
  13380.         ORDINAL_POSITION     = convert(int,
  13381.                        (
  13382.                         select count(*)
  13383.                         from syscolumns sc
  13384.                         where sc.id     =  c.id
  13385.                           AND sc.number =  c.number
  13386.                           AND sc.colid  <= c.colid
  13387.                         )),
  13388.         COLUMN_HASDEFAULT    = convert(bit, case when m.text is null then 0 else 1 end),
  13389.  
  13390.         COLUMN_DEFAULT        = convert (varchar (255),
  13391.                                   case when substring(m.text,1,1) = '('
  13392.                                   then substring(m.text,2,datalength(m.text)-2)
  13393.                                   else substring(m.text,1,datalength(m.text)-2)
  13394.                               end), 
  13395.         COLUMN_FLAGS        = convert(int,
  13396.                         case when d.is_long = 1 
  13397.                         then 0x82 /*DBCOLUMNFLAGS_ISLONG|DBCOLUMNFLAGS_MAYDEFER*/
  13398.                         else 0
  13399.                         end
  13400.                     |    case when d.fixlen is not null
  13401.                         then 0x10 /*DBCOLUMNFLAGS_ISFIXEDLENGTH*/
  13402.                         else 0
  13403.                         end
  13404.                     |    case when c.status&8 = 8
  13405.                         then 0x60 /*DBCOLUMNFLAGS_ISNULLABLE|DBCOLUMNFLAGS_MAYBENULL*/
  13406.                         else 0
  13407.                         end
  13408.                     |    case 
  13409.                         when d.type_name = 'timestamp'
  13410.                             then 0x200 /*DBCOLUMNFLAGS_ISROWVER*/
  13411.                         when (c.status&128) != 128
  13412.                             then 0x8 /*DBCOLUMNFLAGS_WRITEUNKNOWN*/ 
  13413.                         else 0 
  13414.                         end),
  13415.         IS_NULLABLE        = convert(bit,c.status&8),
  13416.         DATA_TYPE        = d.oledb_data_type,
  13417.         TYPE_GUID        = convert(binary(16),null),
  13418.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  13419.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  13420.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  13421.                         then coalesce(d.column_size,c.length)
  13422.                         else null 
  13423.                         end),
  13424.         CHARACTER_OCTET_LENGTH    = convert(int,
  13425.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  13426.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  13427.                         then coalesce(d.column_size,c.length)
  13428.                         else null 
  13429.                         end),
  13430.         NUMERIC_PRECISION    = convert(smallint,
  13431.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  13432.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  13433.                                     then d.data_precision else null end),
  13434.         NUMERIC_SCALE        = convert(smallint, 
  13435.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  13436.         DATETIME_PRECISION    = convert(int,
  13437.                         case when d.oledb_data_type <> 135 /*DBTYPE_DBTIMESTAMP*/ then null
  13438.                             when data_precision = 23 then 3 else 0 end),
  13439.         CHARACTER_SET_CATALOG    = convert(sysname,
  13440.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13441.                         then 'master' 
  13442.                         else null 
  13443.                         end),
  13444.         CHARACTER_SET_SCHEMA    = convert(sysname,
  13445.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13446.                         then 'dbo' 
  13447.                         else null 
  13448.                         end),
  13449.         CHARACTER_SET_NAME    = convert(sysname,
  13450.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13451.                         then a_cha.name 
  13452.                         else null 
  13453.                         end),
  13454.         COLLATION_CATALOG    = convert(sysname,
  13455.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13456.                         then 'master' 
  13457.                         else null 
  13458.                         end),
  13459.         COLLATION_SCHEMA    = convert(sysname,
  13460.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13461.                         then 'dbo' 
  13462.                         else null 
  13463.                         end),
  13464.         COLLATION_NAME        = convert(sysname,
  13465.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13466.                         then b_cha.name 
  13467.                         else null 
  13468.                         end),
  13469.         DOMAIN_CATALOG        = case when t.usertype < 256 then null else db_name() end,
  13470.         DOMAIN_SCHEMA        = case when t.usertype < 256 then null else user_name(o.uid) end,
  13471.         DOMAIN_NAME        = case when t.usertype < 256 then null else t.name end,
  13472.         DESCRIPTION        = convert(varchar(1),null)    
  13473.     
  13474.     from
  13475.         syscolumns c,
  13476.         syscomments m,
  13477.         sysobjects o,
  13478.         master.dbo.spt_provider_types d,
  13479.         systypes t,
  13480.         sysusers u,
  13481.         master.dbo.sysconfigures    cfg,
  13482.         master.dbo.syscharsets        a_cha, /* charset/1001, not sortorder. */
  13483.         master.dbo.syscharsets        b_cha  /* sortorder/2001, not charset. */
  13484.     where
  13485.         o.name = @table_name
  13486.     and     o.type in ('U','V','S')
  13487.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  13488.     and     (
  13489.         suser_id() = 1     /* User is the System Administrator */
  13490.         or o.uid = user_id()     /* User created the object */
  13491.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  13492.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  13493.             from sysprotects p
  13494.             where p.id = o.id
  13495.             /* get rows for public,current user,user's group */
  13496.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  13497.             /* check for SELECT,EXECUTE privilege */
  13498.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  13499.             ) = 1    /* final magic...compare Grants      */
  13500.            )
  13501.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  13502.     and    (@column_name is null or @column_name = c.name)
  13503.     and     o.id = c.id
  13504.     and     t.type = d.ss_dtype
  13505.     and    (t.usertype != 80 or d.type_name='timestamp')
  13506.     and     c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  13507.     and     c.usertype = t.usertype
  13508.     and    c.cdefault *= m.id
  13509.     and    m.colid = 1
  13510.     and    cfg.comment = 'default sortorder ID' 
  13511.     and    a_cha.type = 1001 /* type is charset */
  13512.     and    b_cha.type = 2001 /* type is sortorder */
  13513.     and    a_cha.id = b_cha.csid
  13514.     and     b_cha.id = cfg.value
  13515.     order by 2, 3, c.colid
  13516. go
  13517.  
  13518. dump tran master with no_log
  13519. go
  13520.  
  13521. create procedure sp_columns_rowset;2
  13522.     (
  13523.     @table_schema     varchar(255) = null,
  13524.     @column_name    varchar(255) = null
  13525.     )
  13526. as     
  13527.     select
  13528.         TABLE_CATALOG        = db_name(),
  13529.         TABLE_SCHEMA        = user_name(o.uid),
  13530.         TABLE_NAME        = o.name,
  13531.         COLUMN_NAME        = c.name,
  13532.         COLUMN_GUID        = convert(binary(16),null),
  13533.         COLUMN_PROPID        = convert(int,null),
  13534.         ORDINAL_POSITION     = convert(int,
  13535.                        (
  13536.                         select count(*)
  13537.                         from syscolumns sc
  13538.                         where sc.id     =  c.id
  13539.                           AND sc.number =  c.number
  13540.                           AND sc.colid  <= c.colid
  13541.                         )),
  13542.         COLUMN_HASDEFAULT    = convert(bit, case when m.text is null then 0 else 1 end),
  13543.         COLUMN_DEFAULT        = convert (varchar (255),
  13544.                                   case when substring(m.text,1,1) = '('
  13545.                                   then substring(m.text,2,datalength(m.text)-2)
  13546.                                   else substring(m.text,1,datalength(m.text)-2)
  13547.                               end), 
  13548.         COLUMN_FLAGS        = convert(int,
  13549.                         case when d.is_long = 1 
  13550.                         then 0x82 /*DBCOLUMNFLAGS_ISLONG|DBCOLUMNFLAGS_MAYDEFER*/
  13551.                         else 0
  13552.                         end
  13553.                     |    case when d.fixlen is not null
  13554.                         then 0x10 /*DBCOLUMNFLAGS_ISFIXEDLENGTH*/
  13555.                         else 0
  13556.                         end
  13557.                     |    case when c.status&8 = 8
  13558.                         then 0x60 /*DBCOLUMNFLAGS_ISNULLABLE|DBCOLUMNFLAGS_MAYBENULL*/
  13559.                         else 0
  13560.                         end
  13561.                     |    case 
  13562.                         when d.type_name = 'timestamp'
  13563.                             then 0x200 /*DBCOLUMNFLAGS_ISROWVER*/
  13564.                         when (c.status&128) != 128
  13565.                             then 0x8 /*DBCOLUMNFLAGS_WRITEUNKNOWN*/ 
  13566.                         else 0 
  13567.                         end),
  13568.         IS_NULLABLE        = convert(bit,c.status&8),
  13569.         DATA_TYPE        = d.oledb_data_type,
  13570.         TYPE_GUID        = convert(binary(16),null),
  13571.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  13572.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  13573.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  13574.                         then coalesce(d.column_size,c.length)
  13575.                         else null 
  13576.                         end),
  13577.         CHARACTER_OCTET_LENGTH    = convert(int,
  13578.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  13579.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  13580.                         then coalesce(d.column_size,c.length)
  13581.                         else null 
  13582.                         end),
  13583.         NUMERIC_PRECISION    = convert(smallint,
  13584.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  13585.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  13586.                             then d.data_precision else null end),
  13587.         NUMERIC_SCALE        = convert(smallint, 
  13588.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  13589.         DATETIME_PRECISION    = convert(int,
  13590.                         case when d.oledb_data_type <> 135 /*DBTYPE_DBTIMESTAMP*/ then null
  13591.                             when data_precision = 23 then 3 else 0 end),
  13592.         CHARACTER_SET_CATALOG    = convert(sysname,
  13593.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13594.                         then 'master' 
  13595.                         else null 
  13596.                         end),
  13597.         CHARACTER_SET_SCHEMA    = convert(sysname,
  13598.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13599.                         then 'dbo' 
  13600.                         else null 
  13601.                         end),
  13602.         CHARACTER_SET_NAME    = convert(sysname,
  13603.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13604.                         then a_cha.name 
  13605.                         else null 
  13606.                         end),
  13607.         COLLATION_CATALOG    = convert(sysname,
  13608.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13609.                         then 'master' 
  13610.                         else null 
  13611.                         end),
  13612.         COLLATION_SCHEMA    = convert(sysname,
  13613.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13614.                         then 'dbo' 
  13615.                         else null 
  13616.                         end),
  13617.         COLLATION_NAME        = convert(sysname,
  13618.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13619.                         then b_cha.name 
  13620.                         else null 
  13621.                         end),
  13622.         DOMAIN_CATALOG        = case when t.usertype < 256 then null else db_name() end,
  13623.         DOMAIN_SCHEMA        = case when t.usertype < 256 then null else user_name(o.uid) end,
  13624.         DOMAIN_NAME        = case when t.usertype < 256 then null else t.name end,
  13625.         DESCRIPTION        = convert(varchar(1),null)    
  13626.     
  13627.     from
  13628.         syscolumns c,
  13629.         syscomments m,
  13630.         sysobjects o,
  13631.         master.dbo.spt_provider_types d,
  13632.         systypes t,
  13633.         sysusers u,
  13634.         master.dbo.sysconfigures    cfg,
  13635.         master.dbo.syscharsets        a_cha, /* charset/1001, not sortorder. */
  13636.         master.dbo.syscharsets        b_cha /* sortorder/2001, not charset. */
  13637.     where
  13638.          o.type in ('U','V','S')
  13639.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  13640.     and     (
  13641.         suser_id() = 1     /* User is the System Administrator */
  13642.         or o.uid = user_id()     /* User created the object */
  13643.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  13644.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  13645.             from sysprotects p
  13646.             where p.id = o.id
  13647.             /* get rows for public,current user,user's group */
  13648.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  13649.             /* check for SELECT,EXECUTE privilege */
  13650.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  13651.             ) = 1    /* final magic...compare Grants      */
  13652.            )
  13653.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  13654.     and    (@column_name is null or @column_name = c.name)
  13655.     and     o.id = c.id
  13656.     and     t.type = d.ss_dtype
  13657.     and    (t.usertype != 80 or d.type_name='timestamp')
  13658.     and     c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  13659.     and     c.usertype = t.usertype
  13660.     and    c.cdefault *= m.id
  13661.     and     m.colid = 1
  13662.     and    cfg.comment = 'default sortorder ID' 
  13663.     and    a_cha.type = 1001 /* type is charset */
  13664.     and    b_cha.type = 2001 /* type is sortorder */
  13665.     and    a_cha.id = b_cha.csid
  13666.     and     b_cha.id = cfg.value
  13667.     order by 2, 3, c.colid
  13668.     
  13669. go
  13670.  
  13671. dump tran master with no_log
  13672. go
  13673.  
  13674. if (charindex('7.00', @@version) = 0 and
  13675.     charindex('8.00', @@version) = 0)
  13676. begin
  13677.     print ''
  13678.     print ''
  13679.     print 'Warning:'
  13680.     print 'you are installing the stored procedures '
  13681.     print 'on a pre 8.0 SQL Server.'
  13682.     print 'Ignore the following errors.'
  13683. end
  13684. else
  13685.     drop proc sp_columns_rowset
  13686. go
  13687.  
  13688. /*    Procedure for 7.0 server */
  13689. create procedure sp_columns_rowset
  13690.     (
  13691.        @table_name    sysname,
  13692.     @table_schema     sysname = NULL,
  13693.     @column_name    sysname = NULL
  13694.     )
  13695. as
  13696.     select
  13697.         TABLE_CATALOG        = db_name(),
  13698.         TABLE_SCHEMA        = user_name(o.uid),
  13699.         TABLE_NAME        = o.name,
  13700.         COLUMN_NAME        = c.name,
  13701.         COLUMN_GUID        = convert(uniqueidentifier,null),
  13702.         COLUMN_PROPID        = convert(int,null),
  13703.         ORDINAL_POSITION     = convert(int,
  13704.                        (
  13705.                         select count(*)
  13706.                         from syscolumns sc
  13707.                         where sc.id     =  c.id
  13708.                           AND sc.number =  c.number
  13709.                           AND sc.colid  <= c.colid
  13710.                         )),
  13711.         COLUMN_HASDEFAULT    = convert(bit, case when m.text is null then 0 else 1 end),
  13712.         COLUMN_DEFAULT        =  convert(nvarchar(2000),
  13713.                                         case when substring(m.text,1,1) = '('
  13714.                                             then substring(m.text,2,datalength(m.text)/2-2)
  13715.                                             else substring(m.text,1,datalength(m.text)/2-2)
  13716.                                         end    ), 
  13717.         COLUMN_FLAGS        = convert(int,
  13718.                         case when d.is_long = 1 
  13719.                         then 0x82 /*DBCOLUMNFLAGS_ISLONG|DBCOLUMNFLAGS_MAYDEFER*/
  13720.                         else 0
  13721.                         end
  13722.                     |    case when d.fixlen is not null
  13723.                         then 0x10 /*DBCOLUMNFLAGS_ISFIXEDLENGTH*/
  13724.                         else 0
  13725.                         end
  13726.                     |    case when ColumnProperty(c.id,c.name,'AllowsNull') = 1
  13727.                         then 0x60 /*DBCOLUMNFLAGS_ISNULLABLE|DBCOLUMNFLAGS_MAYBENULL*/
  13728.                         else 0
  13729.                         end
  13730.                     |    case 
  13731.                         when d.type_name = 'timestamp'
  13732.                             then 0x200 /*DBCOLUMNFLAGS_ISROWVER*/
  13733.                         when (c.status&128) != 128  and permissions(o.id,c.name)&2 = 2
  13734.                             then 0x4 /*DBCOLUMNFLAGS_WRITE*/ 
  13735.                         else 0 
  13736.                         end),
  13737.         IS_NULLABLE        = convert(bit,ColumnProperty(c.id,c.name,'AllowsNull')),
  13738.         DATA_TYPE        = d.oledb_data_type,
  13739.         TYPE_GUID        = convert(uniqueidentifier,null),
  13740.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  13741.                         case 
  13742.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  13743.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  13744.                         then coalesce(d.column_size,c.length)
  13745.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  13746.                         then coalesce(d.column_size,c.length/2)
  13747.                         else null 
  13748.                         end),
  13749.         CHARACTER_OCTET_LENGTH    = convert(int,
  13750.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  13751.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  13752.                         then coalesce(d.column_size,c.length)
  13753.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  13754.                         then coalesce(d.column_size*2,c.length)
  13755.                         else null 
  13756.                         end),
  13757.         NUMERIC_PRECISION    = convert(smallint,
  13758.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.xprec
  13759.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  13760.                             then d.data_precision else null end),
  13761.         NUMERIC_SCALE        = convert(smallint, 
  13762.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  13763.         DATETIME_PRECISION    = convert(int,
  13764.                         case when d.oledb_data_type <> 135 /*DBTYPE_DBTIMESTAMP*/ then null
  13765.                             when data_precision = 23 then 3 else 0 end),
  13766.         CHARACTER_SET_CATALOG    = convert(sysname,
  13767.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13768.                         then N'master' 
  13769.                         else null 
  13770.                         end),
  13771.         CHARACTER_SET_SCHEMA    = convert(sysname,
  13772.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13773.                         then N'dbo' 
  13774.                         else null 
  13775.                         end),
  13776.         CHARACTER_SET_NAME    = convert(sysname,
  13777.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13778.                         then a_cha.name 
  13779.                         else null 
  13780.                         end),
  13781.         COLLATION_CATALOG    = convert(sysname,
  13782.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13783.                         then N'master' 
  13784.                         else null 
  13785.                         end),
  13786.         COLLATION_SCHEMA    = convert(sysname,
  13787.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13788.                         then N'dbo' 
  13789.                         else null 
  13790.                         end),
  13791.         COLLATION_NAME        = convert(sysname,
  13792.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13793.                         then b_cha.name 
  13794.                         else null 
  13795.                         end),
  13796.         DOMAIN_CATALOG        = case when t.usertype < 256 then null else db_name() end,
  13797.         DOMAIN_SCHEMA        = case when t.usertype < 256 then null else user_name(o.uid) end,
  13798.         DOMAIN_NAME        = case when t.usertype < 256 then null else t.name end,
  13799.         DESCRIPTION        = convert(nvarchar(1),null)    
  13800.  
  13801.     from
  13802.         syscolumns c left join syscomments m on c.cdefault = m.id and m.colid = 1,
  13803.         sysobjects o,
  13804.         master.dbo.spt_provider_types d,
  13805.         systypes t,
  13806.         master.dbo.sysconfigures    cfg,
  13807.         master.dbo.syscharsets        a_cha, /* charset/1001, not sortorder. */
  13808.         master.dbo.syscharsets        b_cha /* sortorder/2001, not charset. */
  13809.     where
  13810.         permissions(o.id, c.name) <> 0
  13811.     and    o.name = @table_name
  13812.     and     o.type in ('U','V','S')
  13813.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  13814.     and    (@column_name is null or @column_name = c.name)
  13815.     and     o.id = c.id
  13816.     and     t.xtype = d.ss_dtype
  13817.     and     c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  13818.     and     c.xusertype = t.xusertype
  13819.     and    cfg.comment = 'default sortorder id' 
  13820.     and    a_cha.type = 1001 /* type is charset */
  13821.     and    b_cha.type = 2001 /* type is sortorder */
  13822.     and    a_cha.id = b_cha.csid
  13823.     and     b_cha.id = cfg.value
  13824.     order by 2, 3, c.colorder
  13825. go
  13826.  
  13827. dump tran master with no_log
  13828. go
  13829.  
  13830. /*    Procedure for 7.0 server */
  13831. create procedure sp_columns_rowset;2
  13832.     (
  13833.     @table_schema     sysname = NULL,
  13834.     @column_name    sysname = NULL
  13835.     )
  13836. as     
  13837.     select
  13838.         TABLE_CATALOG        = db_name(),
  13839.         TABLE_SCHEMA        = user_name(o.uid),
  13840.         TABLE_NAME        = o.name,
  13841.         COLUMN_NAME        = c.name,
  13842.         COLUMN_GUID        = convert(uniqueidentifier,null),
  13843.         COLUMN_PROPID        = convert(int,null),
  13844.         ORDINAL_POSITION     = convert(int,
  13845.                        (
  13846.                         select count(*)
  13847.                         from syscolumns sc
  13848.                         where sc.id     =  c.id
  13849.                           AND sc.number =  c.number
  13850.                           AND sc.colid  <= c.colid
  13851.                         )),
  13852.         COLUMN_HASDEFAULT    = convert(bit, case when m.text is null then 0 else 1 end),
  13853.         COLUMN_DEFAULT        =  convert(nvarchar(2000),
  13854.                                         case when substring(m.text,1,1) = '('
  13855.                                             then substring(m.text,2,datalength(m.text)/2-2)
  13856.                                             else substring(m.text,1,datalength(m.text)/2-2)
  13857.                                         end    ), 
  13858.         COLUMN_FLAGS        = convert(int,
  13859.                         case when d.is_long = 1 
  13860.                         then 0x82 /*DBCOLUMNFLAGS_ISLONG|DBCOLUMNFLAGS_MAYDEFER*/
  13861.                         else 0
  13862.                         end
  13863.                     |    case when d.fixlen is not null
  13864.                         then 0x10 /*DBCOLUMNFLAGS_ISFIXEDLENGTH*/
  13865.                         else 0
  13866.                         end
  13867.                     |    case when ColumnProperty(c.id,c.name,'AllowsNull') = 1
  13868.                         then 0x60 /*DBCOLUMNFLAGS_ISNULLABLE|DBCOLUMNFLAGS_MAYBENULL*/
  13869.                         else 0
  13870.                         end
  13871.                     |    case 
  13872.                         when d.type_name = 'timestamp'
  13873.                             then 0x200 /*DBCOLUMNFLAGS_ISROWVER*/
  13874.                         when (c.status&128) != 128 and permissions(o.id,c.name)&2 = 2
  13875.                             then 0x4 /*DBCOLUMNFLAGS_WRITE*/ 
  13876.                         else 0 
  13877.                         end),
  13878.         IS_NULLABLE        = convert(bit,ColumnProperty(c.id,c.name,'AllowsNull')),
  13879.         DATA_TYPE        = d.oledb_data_type,
  13880.         TYPE_GUID        = convert(uniqueidentifier,null),
  13881.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  13882.                         case 
  13883.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  13884.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  13885.                         then coalesce(d.column_size,c.length)
  13886.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  13887.                         then coalesce(d.column_size,c.length/2)
  13888.                         else null 
  13889.                         end),
  13890.         CHARACTER_OCTET_LENGTH    = convert(int,
  13891.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  13892.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  13893.                         then coalesce(d.column_size,c.length)
  13894.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  13895.                         then coalesce(d.column_size*2,c.length)
  13896.                         else null 
  13897.                         end),
  13898.         NUMERIC_PRECISION    = convert(smallint,
  13899.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.xprec
  13900.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  13901.                             then d.data_precision else null end),
  13902.         NUMERIC_SCALE        = convert(smallint, 
  13903.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  13904.         DATETIME_PRECISION    = convert(int,
  13905.                         case when d.oledb_data_type <> 135 /*DBTYPE_DBTIMESTAMP*/ then null
  13906.                             when data_precision = 23 then 3 else 0 end),
  13907.         CHARACTER_SET_CATALOG    = convert(sysname,
  13908.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13909.                         then N'master' 
  13910.                         else null 
  13911.                         end),
  13912.         CHARACTER_SET_SCHEMA    = convert(sysname,
  13913.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13914.                         then N'dbo' 
  13915.                         else null 
  13916.                         end),
  13917.         CHARACTER_SET_NAME    = convert(sysname,
  13918.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13919.                         then a_cha.name 
  13920.                         else null 
  13921.                         end),
  13922.         COLLATION_CATALOG    = convert(sysname,
  13923.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13924.                         then N'master' 
  13925.                         else null 
  13926.                         end),
  13927.         COLLATION_SCHEMA    = convert(sysname,
  13928.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13929.                         then N'dbo' 
  13930.                         else null 
  13931.                         end),
  13932.         COLLATION_NAME        = convert(sysname,
  13933.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13934.                         then b_cha.name 
  13935.                         else null 
  13936.                         end),
  13937.         DOMAIN_CATALOG        = case when t.usertype < 256 then null else db_name() end,
  13938.         DOMAIN_SCHEMA        = case when t.usertype < 256 then null else user_name(o.uid) end,
  13939.         DOMAIN_NAME        = case when t.usertype < 256 then null else t.name end,
  13940.         DESCRIPTION        = convert(nvarchar(1),null)    
  13941.     
  13942.     from
  13943.         syscolumns c left join syscomments m on c.cdefault = m.id and m.colid = 1,
  13944.         sysobjects o,
  13945.         master.dbo.spt_provider_types d,
  13946.         systypes t,
  13947.         master.dbo.sysconfigures    cfg,
  13948.         master.dbo.syscharsets        a_cha, /* charset/1001, not sortorder.*/
  13949.         master.dbo.syscharsets        b_cha /* sortorder/2001, not charset.*/
  13950.     where
  13951.         permissions(o.id, c.name) <> 0
  13952.     and     o.type in ('U','V','S')
  13953.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  13954.     and    (@column_name is null or @column_name = c.name)
  13955.     and     o.id = c.id
  13956.     and     t.xtype = d.ss_dtype
  13957.     and     c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  13958.     and     c.xusertype = t.xusertype
  13959.     and    cfg.comment = 'default sortorder id' 
  13960.     and     a_cha.type = 1001 /* type is charset */
  13961.     and     b_cha.type = 2001 /* type is sortorder */
  13962.     and    a_cha.id = b_cha.csid
  13963.     and     b_cha.id = cfg.value
  13964.     order by 2, 3, c.colorder
  13965. go
  13966.  
  13967. dump tran master with no_log
  13968. go
  13969.  
  13970. /*    Procedure for 7.0 server */
  13971. create procedure sp_columns_rowset;5
  13972.     (
  13973.     @table_server        sysname,
  13974.     @table_catalog        sysname = null,
  13975.     @table_name        sysname = null,
  13976.     @table_schema        sysname = null,
  13977.     @column_name        sysname = null
  13978.     )
  13979. as
  13980.     select
  13981.         TABLE_CATALOG,
  13982.         TABLE_SCHEMA,
  13983.         TABLE_NAME,
  13984.         COLUMN_NAME,
  13985.         COLUMN_GUID,
  13986.         COLUMN_PROPID,
  13987.         ORDINAL_POSITION,
  13988.         COLUMN_HASDEFAULT,
  13989.         COLUMN_DEFAULT,
  13990.         COLUMN_FLAGS,
  13991.         IS_NULLABLE,
  13992.         DATA_TYPE,
  13993.         TYPE_GUID,
  13994.         CHARACTER_MAXIMUM_LENGTH,
  13995.         CHARACTER_OCTET_LENGTH,
  13996.         NUMERIC_PRECISION,
  13997.         NUMERIC_SCALE,
  13998.         DATETIME_PRECISION,
  13999.         CHARACTER_SET_CATALOG,
  14000.         CHARACTER_SET_SCHEMA,
  14001.         CHARACTER_SET_NAME,
  14002.         COLLATION_CATALOG,
  14003.         COLLATION_SCHEMA,
  14004.         COLLATION_NAME,
  14005.         DOMAIN_CATALOG,
  14006.         DOMAIN_SCHEMA,
  14007.         DOMAIN_NAME,
  14008.         DESCRIPTION
  14009.     from master.dbo.SYSREMOTE_COLUMNS <
  14010.                 @table_server,
  14011.                 @table_catalog,
  14012.                 @table_schema,
  14013.                 @table_name,
  14014.                 @column_name > 
  14015.     order by 1, 2, 3, 7
  14016. go
  14017.  
  14018.  
  14019. dump tran master with no_log
  14020. go
  14021.  
  14022. /*    Procedure for 8.00 server */
  14023. if (charindex('8.00', @@version) = 0)
  14024. begin
  14025.     print ''
  14026.     print ''
  14027.     print 'Warning:'
  14028.     print 'you are installing the stored procedures '
  14029.     print 'on a pre 8.0 SQL Server.'
  14030.     print 'Ignore the following errors.'
  14031. end
  14032. else
  14033.     drop proc sp_columns_rowset
  14034. go
  14035.  
  14036. /*    Procedure for 8.00 server */
  14037. create procedure sp_columns_rowset
  14038.     (
  14039.        @table_name    sysname,
  14040.     @table_schema     sysname = NULL,
  14041.     @column_name    sysname = NULL
  14042.     )
  14043. as     
  14044.     select
  14045.         TABLE_CATALOG        = db_name(),
  14046.         TABLE_SCHEMA        = user_name(o.uid),
  14047.         TABLE_NAME        = o.name,
  14048.         COLUMN_NAME        = c.name,
  14049.         COLUMN_GUID        = convert(uniqueidentifier,null),
  14050.         COLUMN_PROPID        = convert(int,null),
  14051.         ORDINAL_POSITION     = convert(int,
  14052.                        (
  14053.                         select count(*)
  14054.                         from syscolumns sc
  14055.                         where sc.id     =  c.id
  14056.                           AND sc.number =  c.number
  14057.                           AND sc.colid  <= c.colid
  14058.                         )),
  14059.         COLUMN_HASDEFAULT    = convert(bit, case when m.text is null then 0 else 1 end),
  14060.  
  14061.         /* Get rid of ( if it is first char otherwise display the whole thing */
  14062.         COLUMN_DEFAULT        = convert(nvarchar(2000),
  14063.                                         case when substring(m.text,1,1) = '('
  14064.                                             then substring(m.text,2,datalength(m.text)/2-2)
  14065.                                             else substring(m.text,1,datalength(m.text)/2-2)
  14066.                                         end    ), 
  14067.  
  14068.         COLUMN_FLAGS        = convert(int,
  14069.                         case when d.is_long = 1 
  14070.                         then 0x82 /*DBCOLUMNFLAGS_ISLONG|DBCOLUMNFLAGS_MAYDEFER*/
  14071.                         else 0
  14072.                         end
  14073.                     |    case when d.fixlen is not null
  14074.                         then 0x10 /*DBCOLUMNFLAGS_ISFIXEDLENGTH*/
  14075.                         else 0
  14076.                         end
  14077.                     |    case when ColumnProperty(c.id,c.name,'AllowsNull') = 1
  14078.                         then 0x60 /*DBCOLUMNFLAGS_ISNULLABLE|DBCOLUMNFLAGS_MAYBENULL*/
  14079.                         else 0
  14080.                         end
  14081.                     |    case 
  14082.                         when d.type_name = 'timestamp'
  14083.                             then 0x200 /*DBCOLUMNFLAGS_ISROWVER*/
  14084.                         when (c.status&128) != 128  and permissions(o.id,c.name)&2 = 2
  14085.                             then 0x4 /*DBCOLUMNFLAGS_WRITE*/ 
  14086.                         else 0 
  14087.                         end),
  14088.         IS_NULLABLE        = convert(bit,ColumnProperty(c.id,c.name,'AllowsNull')),
  14089.         DATA_TYPE        = d.oledb_data_type,
  14090.         TYPE_GUID        = convert(uniqueidentifier,null),
  14091.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  14092.                         case 
  14093.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  14094.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  14095.                         then coalesce(d.column_size,c.length)
  14096.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  14097.                         then coalesce(d.column_size,c.length/2)
  14098.                         else null 
  14099.                         end),
  14100.         CHARACTER_OCTET_LENGTH    = convert(int,
  14101.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  14102.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  14103.                         then coalesce(d.column_size,c.length)
  14104.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  14105.                         then coalesce(d.column_size*2,c.length)
  14106.                         else null 
  14107.                         end),
  14108.         NUMERIC_PRECISION    = convert(smallint,
  14109.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.xprec
  14110.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  14111.                             then d.data_precision else null end),
  14112.         NUMERIC_SCALE        = convert(smallint, 
  14113.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  14114.         DATETIME_PRECISION    = convert(int,
  14115.                         case when d.oledb_data_type <> 135 /*DBTYPE_DBTIMESTAMP*/ then null
  14116.                             when data_precision = 23 then 3 else 0 end),
  14117.         CHARACTER_SET_CATALOG    = convert(sysname,
  14118.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  14119.                         then N'master' 
  14120.                         else null 
  14121.                         end),
  14122.         CHARACTER_SET_SCHEMA    = convert(sysname,
  14123.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  14124.                         then N'dbo' 
  14125.                         else null 
  14126.                         end),
  14127.         CHARACTER_SET_NAME    = convert(sysname,
  14128.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  14129.                         then a_cha.name 
  14130.                         else null 
  14131.                         end),
  14132.         COLLATION_CATALOG    = convert(sysname,
  14133.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  14134.                         then N'master' 
  14135.                         else null 
  14136.                         end),
  14137.         COLLATION_SCHEMA    = convert(sysname,
  14138.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  14139.                         then N'dbo' 
  14140.                         else null 
  14141.                         end),
  14142.         COLLATION_NAME        = convert(sysname,
  14143.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  14144.                         then c.collation 
  14145.                         else null 
  14146.                         end),
  14147.         DOMAIN_CATALOG        = case when t.usertype < 256 then null else db_name() end,
  14148.         DOMAIN_SCHEMA        = case when t.usertype < 256 then null else user_name(o.uid) end,
  14149.         DOMAIN_NAME        = case when t.usertype < 256 then null else t.name end,
  14150.         DESCRIPTION        = convert(nvarchar(1),null),
  14151.         COLUMN_LCID     = convert(int, CollationPropertyFromID(c.collationid, 'lcid')),
  14152.         COLUMN_COMPFLAGS = convert(int, CollationPropertyFromID(c.collationid, 'oledbcompstyle')),
  14153.         COLUMN_SORTID = case /* hack to keep the old behavior: will be removed */
  14154.                                when ABS(c.collationid) > 0x1000000 
  14155.                             then convert(int, DATABASEPROPERTYEX(db_name(),'sqlsortorder'))
  14156.                             else null 
  14157.                         end,
  14158.         COLUMN_TDSCOLLATION = convert (binary(5),CollationPropertyFromID(c.collationid, 'TDSCollation')),
  14159.         IS_COMPUTED    = convert(bit, c.iscomputed)    
  14160.     from
  14161.         syscolumns c left join syscomments m on c.cdefault = m.id and m.colid = 1,
  14162.         sysobjects o,
  14163.         master.dbo.spt_provider_types d,
  14164.         systypes t,
  14165.         master.dbo.syscharsets        a_cha /* charset/1001, not sortorder. */
  14166.     where
  14167.         permissions(o.id, c.name) <> 0
  14168.     and    o.name = @table_name
  14169.     and     (o.type in ('U','V','S') OR (o.type in ('TF', 'IF') and c.number = 0))
  14170.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  14171.     and    (@column_name is null or @column_name = c.name)
  14172.     and     o.id = c.id
  14173.     and     t.xtype = d.ss_dtype
  14174.     and     c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  14175.     and     c.xusertype = t.xusertype
  14176.     and    a_cha.id = isnull(convert(tinyint, CollationPropertyFromID(c.collationid, 'sqlcharset')),
  14177.             convert(tinyint, ServerProperty('sqlcharset'))) -- make sure there's one and only one row selected for each column
  14178.     order by 2, 3, c.colorder
  14179. go
  14180.  
  14181.  
  14182. /*    Procedure for 8.0 server */
  14183. create procedure sp_columns_rowset;2
  14184.     (
  14185.     @table_schema     sysname = NULL,
  14186.     @column_name    sysname = NULL
  14187.     )
  14188. as     
  14189.     select
  14190.         TABLE_CATALOG        = db_name(),
  14191.         TABLE_SCHEMA        = user_name(o.uid),
  14192.         TABLE_NAME        = o.name,
  14193.         COLUMN_NAME        = c.name,
  14194.         COLUMN_GUID        = convert(uniqueidentifier,null),
  14195.         COLUMN_PROPID        = convert(int,null),
  14196.         ORDINAL_POSITION     = convert(int,
  14197.                        (
  14198.                         select count(*)
  14199.                         from syscolumns sc
  14200.                         where sc.id     =  c.id
  14201.                           AND sc.number =  c.number
  14202.                           AND sc.colid  <= c.colid
  14203.                         )),
  14204.         COLUMN_HASDEFAULT    = convert(bit, case when m.text is null then 0 else 1 end),
  14205.         COLUMN_DEFAULT        = convert(nvarchar(2000),
  14206.                                         case when substring(m.text,1,1) = '('
  14207.                                             then substring(m.text,2,datalength(m.text)/2-2)
  14208.                                             else substring(m.text,1,datalength(m.text)/2-2)
  14209.                                         end    ), 
  14210.         COLUMN_FLAGS        = convert(int,
  14211.                         case when d.is_long = 1 
  14212.                         then 0x82 /*DBCOLUMNFLAGS_ISLONG|DBCOLUMNFLAGS_MAYDEFER*/
  14213.                         else 0
  14214.                         end
  14215.                     |    case when d.fixlen is not null
  14216.                         then 0x10 /*DBCOLUMNFLAGS_ISFIXEDLENGTH*/
  14217.                         else 0
  14218.                         end
  14219.                     |    case when ColumnProperty(c.id,c.name,'AllowsNull') = 1
  14220.                         then 0x60 /*DBCOLUMNFLAGS_ISNULLABLE|DBCOLUMNFLAGS_MAYBENULL*/
  14221.                         else 0
  14222.                         end
  14223.                     |    case 
  14224.                         when d.type_name = 'timestamp'
  14225.                             then 0x200 /*DBCOLUMNFLAGS_ISROWVER*/
  14226.                         when (c.status&128) != 128 and permissions(o.id,c.name)&2 = 2
  14227.                             then 0x4 /*DBCOLUMNFLAGS_WRITE*/ 
  14228.                         else 0 
  14229.                         end),
  14230.         IS_NULLABLE        = convert(bit,ColumnProperty(c.id,c.name,'AllowsNull')),
  14231.         DATA_TYPE        = d.oledb_data_type,
  14232.         TYPE_GUID        = convert(uniqueidentifier,null),
  14233.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  14234.                         case 
  14235.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  14236.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  14237.                         then coalesce(d.column_size,c.length)
  14238.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  14239.                         then coalesce(d.column_size,c.length/2)
  14240.                         else null 
  14241.                         end),
  14242.         CHARACTER_OCTET_LENGTH    = convert(int,
  14243.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  14244.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  14245.                         then coalesce(d.column_size,c.length)
  14246.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  14247.                         then coalesce(d.column_size*2,c.length)
  14248.                         else null 
  14249.                         end),
  14250.         NUMERIC_PRECISION    = convert(smallint,
  14251.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.xprec
  14252.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  14253.                             then d.data_precision else null end),
  14254.         NUMERIC_SCALE        = convert(smallint, 
  14255.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  14256.         DATETIME_PRECISION    = convert(int,
  14257.                         case when d.oledb_data_type <> 135 /*DBTYPE_DBTIMESTAMP*/ then null
  14258.                             when data_precision = 23 then 3 else 0 end),
  14259.         CHARACTER_SET_CATALOG    = convert(sysname,
  14260.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  14261.                         then N'master' 
  14262.                         else null 
  14263.                         end),
  14264.         CHARACTER_SET_SCHEMA    = convert(sysname,
  14265.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  14266.                         then N'dbo' 
  14267.                         else null 
  14268.                         end),
  14269.         CHARACTER_SET_NAME    = convert(sysname,
  14270.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  14271.                         then a_cha.name 
  14272.                         else null 
  14273.                         end),
  14274.         COLLATION_CATALOG    = convert(sysname,
  14275.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  14276.                         then N'master' 
  14277.                         else null 
  14278.                         end),
  14279.         COLLATION_SCHEMA    = convert(sysname,
  14280.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  14281.                         then N'dbo' 
  14282.                         else null 
  14283.                         end),
  14284.         COLLATION_NAME        = convert(sysname,
  14285.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  14286.                         then c.collation 
  14287.                         else null 
  14288.                         end),
  14289.         DOMAIN_CATALOG        = case when t.usertype < 256 then null else db_name() end,
  14290.         DOMAIN_SCHEMA        = case when t.usertype < 256 then null else user_name(o.uid) end,
  14291.         DOMAIN_NAME        = case when t.usertype < 256 then null else t.name end,
  14292.         DESCRIPTION        = convert(nvarchar(1),null),    
  14293.         COLUMN_LCID     = convert(int, CollationPropertyFromID(c.collationid, 'lcid')),
  14294.         COLUMN_COMPFLAGS = convert(int, CollationPropertyFromID(c.collationid, 'oledbcompstyle')),
  14295.         COLUMN_SORTID = case /* hack to keep the old behavior: will be removed */
  14296.                                when ABS(c.collationid) > 0x1000000 
  14297.                             then convert(int, DATABASEPROPERTYEX(db_name(),'sqlsortorder'))
  14298.                             else null 
  14299.                         end,
  14300.         
  14301.         COLUMN_TDSCOLLATION = convert (binary(5),CollationPropertyFromID(c.collationid, 'TDSCollation')),
  14302.         IS_COMPUTED = convert(bit, c.iscomputed)
  14303.     
  14304.     from
  14305.         syscolumns c left join syscomments m on c.cdefault = m.id and m.colid = 1,
  14306.         sysobjects o,
  14307.         master.dbo.spt_provider_types d,
  14308.         systypes t,
  14309.         master.dbo.syscharsets        a_cha /* charset/1001, not sortorder.*/
  14310.     where
  14311.         permissions(o.id, c.name) <> 0
  14312.     and     (o.type in ('U','V','S') OR (o.type in ('TF', 'IF') and c.number = 0))
  14313.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  14314.     and    (@column_name is null or @column_name = c.name)
  14315.     and     o.id = c.id
  14316.     and     t.xtype = d.ss_dtype
  14317.     and     c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  14318.     and     c.xusertype = t.xusertype
  14319.     and    a_cha.type = 1001 /* type is charset */
  14320.     and    a_cha.id = isnull(convert(tinyint, CollationPropertyFromID(c.collationid, 'sqlcharset')),
  14321.             convert(tinyint, ServerProperty('sqlcharset'))) -- make sure there's one and only one row selected for each column
  14322.     order by 2, 3, c.colorder
  14323. go
  14324.  
  14325. dump tran master with no_log
  14326. go
  14327.  
  14328. /*    Procedure for 8.0 server */
  14329. create procedure sp_columns_rowset;5
  14330.     (
  14331.     @table_server        sysname,
  14332.     @table_catalog        sysname = null,
  14333.     @table_name        sysname = null,
  14334.     @table_schema        sysname = null,
  14335.     @column_name        sysname = null
  14336.     )
  14337. as
  14338.     select
  14339.         TABLE_CATALOG,
  14340.         TABLE_SCHEMA,
  14341.         TABLE_NAME,
  14342.         COLUMN_NAME,
  14343.         COLUMN_GUID,
  14344.         COLUMN_PROPID,
  14345.         ORDINAL_POSITION,
  14346.         COLUMN_HASDEFAULT,
  14347.         COLUMN_DEFAULT,
  14348.         COLUMN_FLAGS,
  14349.         IS_NULLABLE,
  14350.         DATA_TYPE,
  14351.         TYPE_GUID,
  14352.         CHARACTER_MAXIMUM_LENGTH,
  14353.         CHARACTER_OCTET_LENGTH,
  14354.         NUMERIC_PRECISION,
  14355.         NUMERIC_SCALE,
  14356.         DATETIME_PRECISION,
  14357.         CHARACTER_SET_CATALOG,
  14358.         CHARACTER_SET_SCHEMA,
  14359.         CHARACTER_SET_NAME,
  14360.         COLLATION_CATALOG,
  14361.         COLLATION_SCHEMA,
  14362.         COLLATION_NAME,
  14363.         DOMAIN_CATALOG,
  14364.         DOMAIN_SCHEMA,
  14365.         DOMAIN_NAME,
  14366.         DESCRIPTION
  14367.  
  14368.     from master.dbo.SYSREMOTE_COLUMNS <
  14369.                 @table_server,
  14370.                 @table_catalog,
  14371.                 @table_schema,
  14372.                 @table_name,
  14373.                 @column_name > 
  14374.     order by 1, 2, 3, 7
  14375. go
  14376.  
  14377.  
  14378. grant execute on sp_columns_rowset to public
  14379. go
  14380.  
  14381. dump tran master with no_log
  14382. go
  14383. if (charindex('6.00', @@version) > 0)
  14384.     begin
  14385.     if (exists (select * from sysobjects
  14386.             where name = 'sp_columns_rowset' and type = 'P '))
  14387.         begin
  14388.         drop procedure sp_columns_rowset
  14389.         dump tran master with no_log
  14390.         end
  14391.     end
  14392. go
  14393.  
  14394. print ''
  14395. print 'creating sp_check_constraints_rowset'
  14396. go
  14397.  
  14398. /*    Procedure for 6.50 and earlier servers */
  14399. create procedure sp_check_constraints_rowset
  14400.     (
  14401.     @constraint_name    varchar(255),
  14402.     @constraint_schema    varchar(255) = null
  14403.     )
  14404. as
  14405.     select
  14406.         CONSTRAINT_CATALOG    = db_name(),
  14407.         CONSTRAINT_SCHEMA    = user_name(c_obj.uid),
  14408.         CONSTRAINT_NAME        = c_obj.name,
  14409.         CHECK_CLAUSE        = m.text,    
  14410.         DESCRIPTION            = convert(varchar(1),null)
  14411.     from 
  14412.         sysobjects c_obj, syscomments m
  14413.     where
  14414.             c_obj.type = 'C '
  14415.         and c_obj.name    = @constraint_name
  14416.         and    (@constraint_schema is null or @constraint_schema = user_name(c_obj.uid))
  14417.         and c_obj.id = m.id
  14418.     order by 1,2,3 
  14419. go
  14420. dump tran master with no_log
  14421. go
  14422.  
  14423. create procedure sp_check_constraints_rowset;2
  14424.     (
  14425.     @constraint_schema    varchar(255) = null
  14426.     )
  14427. as
  14428.     select
  14429.         CONSTRAINT_CATALOG    = db_name(),
  14430.         CONSTRAINT_SCHEMA    = user_name(c_obj.uid),
  14431.         CONSTRAINT_NAME        = c_obj.name,
  14432.         CHECK_CLAUSE        = m.text,    
  14433.         DESCRIPTION            = convert(varchar(1),null)
  14434.     from 
  14435.         sysobjects c_obj, syscomments m
  14436.     where
  14437.             c_obj.type = 'C '
  14438.         and    (@constraint_schema is null or @constraint_schema = user_name(c_obj.uid))
  14439.         and c_obj.id = m.id
  14440.     order by 1,2,3 
  14441. go
  14442. dump tran master with no_log
  14443. go
  14444.  
  14445. if (charindex('7.00', @@version) > 0 or
  14446.     charindex('8.00', @@version) > 0)
  14447.     drop procedure sp_check_constraints_rowset
  14448. else
  14449. begin
  14450.     print ''
  14451.     print ''
  14452.     print 'Warning:'
  14453.     print 'you are installing the stored procedures '
  14454.     print 'on a pre 7.0 SQL Server.'
  14455.     print 'Ignore the following errors.'
  14456. end
  14457. go
  14458.  
  14459. /*    Procedure for 7.0 servers and 8.0 servers */
  14460. create procedure sp_check_constraints_rowset
  14461.     (
  14462.     @constraint_name    sysname,
  14463.     @constraint_schema    sysname = null
  14464.     )
  14465. as
  14466.     select
  14467.         CONSTRAINT_CATALOG    = db_name(),
  14468.         CONSTRAINT_SCHEMA    = user_name(c_obj.uid),
  14469.         CONSTRAINT_NAME        = c_obj.name,
  14470.         CHECK_CLAUSE        = m.text,    
  14471.         DESCRIPTION            = convert(nvarchar(1), null)
  14472.     from 
  14473.         sysobjects c_obj, syscomments m
  14474.     where
  14475.             c_obj.type = 'C '
  14476.         and c_obj.name    = @constraint_name
  14477.         and    (@constraint_schema is null or @constraint_schema = user_name(c_obj.uid))
  14478.         and m.id = c_obj.id
  14479.     order by 1,2,3 
  14480. go
  14481. dump tran master with no_log
  14482. go
  14483.  
  14484. create procedure sp_check_constraints_rowset;2
  14485.     (
  14486.     @constraint_schema    sysname = null
  14487.     )
  14488. as
  14489.     select
  14490.         CONSTRAINT_CATALOG    = db_name(),
  14491.         CONSTRAINT_SCHEMA    = user_name(c_obj.uid),
  14492.         CONSTRAINT_NAME        = c_obj.name,
  14493.         CHECK_CLAUSE        = m.text,    
  14494.         DESCRIPTION            = convert(nvarchar(1), null)
  14495.     from 
  14496.         sysobjects c_obj, syscomments m
  14497.     where
  14498.             c_obj.type = 'C '
  14499.         and    (@constraint_schema is null or @constraint_schema = user_name(c_obj.uid))
  14500.         and m.id = c_obj.id
  14501.     order by 1,2,3 
  14502. go
  14503.  
  14504. grant execute on sp_check_constraints_rowset to public
  14505. go
  14506.  
  14507. dump tran master with no_log
  14508. go
  14509.  
  14510. if (charindex('6.00', @@version) > 0)
  14511.     begin
  14512.     if (exists (select * from sysobjects
  14513.             where name = 'sp_check_constraints_rowset' and type = 'P '))
  14514.         begin
  14515.         drop procedure sp_check_constraints_rowset
  14516.         dump tran master with no_log
  14517.         end
  14518.     end
  14519. go
  14520.  
  14521. print ''
  14522. print 'creating sp_check_constbytable_rowset'
  14523. go
  14524.  
  14525. /*    Procedure for 6.50 and earlier servers */
  14526. create procedure sp_check_constbytable_rowset
  14527.     (
  14528.     @table_name            varchar(255), 
  14529.     @table_schema        varchar(255) = null,
  14530.     @constraint_name    varchar(255) = null,
  14531.     @constraint_schema    varchar(255) = null
  14532.     )
  14533. as
  14534.     select
  14535.         TABLE_CATALOG        = db_name(),
  14536.         TABLE_SCHEMA        = user_name(t_obj.uid),
  14537.         TABLE_NAME            = t_obj.name,
  14538.         CONSTRAINT_CATALOG    = db_name(),
  14539.         CONSTRAINT_SCHEMA    = user_name(c_obj.uid),
  14540.         CONSTRAINT_NAME        = c_obj.name,
  14541.         CHECK_CLAUSE        = m.text,    
  14542.         DESCRIPTION            = convert(varchar(1),null)
  14543.     from 
  14544.         sysobjects c_obj, sysobjects t_obj, sysconstraints c, syscomments m
  14545.     where
  14546.             t_obj.name    = @table_name
  14547.         and t_obj.type in ('U ','S ')
  14548.         and    (@table_schema is null or @table_schema = user_name(t_obj.uid))    
  14549.         and t_obj.id = c.id
  14550.         and c.constid = c_obj.id
  14551.         and c_obj.type = 'C '
  14552.         and (@constraint_name is null or c_obj.name    = @constraint_name)
  14553.         and    (@constraint_schema is null or @constraint_schema = user_name(c_obj.uid))
  14554.         and c_obj.id = m.id
  14555.     order by 1,2,3,4,5,6 
  14556. go
  14557. dump tran master with no_log
  14558. go
  14559.  
  14560. create procedure sp_check_constbytable_rowset;2
  14561.     (
  14562.     @table_schema        varchar(255) = null,
  14563.     @constraint_name    varchar(255) = null,
  14564.     @constraint_schema    varchar(255) = null
  14565.     )
  14566. as
  14567.     select
  14568.         TABLE_CATALOG        = db_name(),
  14569.         TABLE_SCHEMA        = user_name(t_obj.uid),
  14570.         TABLE_NAME            = t_obj.name,
  14571.         CONSTRAINT_CATALOG    = db_name(),
  14572.         CONSTRAINT_SCHEMA    = user_name(c_obj.uid),
  14573.         CONSTRAINT_NAME        = c_obj.name,
  14574.         CHECK_CLAUSE        = m.text,    
  14575.         DESCRIPTION            = convert(varchar(1),null)
  14576.     from 
  14577.         sysobjects c_obj, sysobjects t_obj, sysconstraints c, syscomments m
  14578.     where
  14579.             t_obj.type in ('U ','S ')
  14580.         and    (@table_schema is null or @table_schema = user_name(t_obj.uid))
  14581.         and c.id = t_obj.id
  14582.         and c.constid = c_obj.id
  14583.         and c_obj.type    = 'C '
  14584.         and (@constraint_name is null or c_obj.name    = @constraint_name)
  14585.         and    (@constraint_schema is null or @constraint_schema = user_name(c_obj.uid))
  14586.         and c_obj.id = m.id
  14587.     order by 1,2,3,4,5,6 
  14588. go
  14589. dump tran master with no_log
  14590. go
  14591.  
  14592. if (charindex('7.00', @@version) > 0 or
  14593.     charindex('8.00', @@version) > 0)
  14594.     drop procedure sp_check_constbytable_rowset
  14595. else
  14596. begin
  14597.     print ''
  14598.     print ''
  14599.     print 'Warning:'
  14600.     print 'you are installing the stored procedures '
  14601.     print 'on a pre 8.0 SQL Server.'
  14602.     print 'Ignore the following errors.'
  14603. end
  14604. go
  14605.  
  14606. /*    Procedure for 8.0 server */
  14607. create procedure sp_check_constbytable_rowset
  14608.     (
  14609.     @table_name            sysname, 
  14610.     @table_schema        sysname = null,
  14611.     @constraint_name    sysname = null,
  14612.     @constraint_schema    sysname = null
  14613.     )
  14614. as
  14615.     select
  14616.         TABLE_CATALOG        = db_name(),
  14617.         TABLE_SCHEMA        = user_name(t_obj.uid),
  14618.         TABLE_NAME            = t_obj.name,
  14619.         CONSTRAINT_CATALOG    = db_name(),
  14620.         CONSTRAINT_SCHEMA    = user_name(c_obj.uid),
  14621.         CONSTRAINT_NAME        = c_obj.name,
  14622.         CHECK_CLAUSE        = m.text,    
  14623.         DESCRIPTION            = convert(nvarchar(1), null)
  14624.  
  14625.     from 
  14626.         sysobjects c_obj, sysobjects t_obj, syscomments m
  14627.     where
  14628.             t_obj.name    = @table_name
  14629.         and t_obj.type in ('U ','S ')
  14630.         and    (@table_schema is null or @table_schema = user_name(t_obj.uid))
  14631.         and c_obj.parent_obj = t_obj.id
  14632.         and c_obj.type = 'C '
  14633.         and (@constraint_name is null or c_obj.name    = @constraint_name)
  14634.         and    (@constraint_schema is null or @constraint_schema = user_name(c_obj.uid))
  14635.         and m.id = c_obj.id
  14636. order by 1,2,3,4,5,6 
  14637. go
  14638. dump tran master with no_log
  14639. go
  14640. create procedure sp_check_constbytable_rowset;2
  14641.     (
  14642.     @table_schema        sysname = null,
  14643.     @constraint_name    sysname = null,
  14644.     @constraint_schema    sysname = null
  14645.     )
  14646. as
  14647.     select
  14648.         TABLE_CATALOG        = db_name(),
  14649.         TABLE_SCHEMA        = user_name(t_obj.uid),
  14650.         TABLE_NAME            = t_obj.name,
  14651.         CONSTRAINT_CATALOG    = db_name(),
  14652.         CONSTRAINT_SCHEMA    = user_name(c_obj.uid),
  14653.         CONSTRAINT_NAME        = c_obj.name,
  14654.         CHECK_CLAUSE        = m.text,    
  14655.         DESCRIPTION            = convert(nvarchar(1), null)
  14656.     from 
  14657.         sysobjects c_obj, sysobjects t_obj, syscomments m
  14658.     where
  14659.             t_obj.type in ('U ','S ')
  14660.         and    (@table_schema is null or @table_schema = user_name(t_obj.uid))
  14661.         and c_obj.parent_obj = t_obj.id
  14662.         and c_obj.type = 'C '
  14663.         and (@constraint_name is null or c_obj.name    = @constraint_name)
  14664.         and    (@constraint_schema is null or @constraint_schema = user_name(c_obj.uid))
  14665.         and m.id = c_obj.id
  14666.     order by 1,2,3,4,5,6 
  14667. go
  14668. dump tran master with no_log
  14669. go
  14670.  
  14671. grant execute on sp_check_constbytable_rowset to public
  14672. go
  14673.  
  14674. dump tran master with no_log
  14675. go
  14676. if (charindex('6.00', @@version) > 0)
  14677.     begin
  14678.     if (exists (select * from sysobjects
  14679.             where name = 'sp_check_constbytable_rowset' and type = 'P '))
  14680.         begin
  14681.         drop procedure sp_check_constbytable_rowset
  14682.         dump tran master with no_log
  14683.         end
  14684.     end
  14685. go
  14686.  
  14687.  
  14688. print ''
  14689. print 'creating sp_foreign_keys_rowset'
  14690. go
  14691.  
  14692. /*    Procedure for 6.0 and 6.5 server */
  14693. CREATE PROCEDURE sp_foreign_keys_rowset
  14694.     (
  14695.      @pk_table_name        varchar(255) = null,
  14696.     @pk_table_schema    varchar(255) = null,
  14697.     @pk_table_catalog    varchar(255) = null,
  14698.     @fk_table_name        varchar(255) = null,
  14699.     @fk_table_schema    varchar(255) = null,
  14700.     @fk_table_catalog    varchar(255) = null
  14701.     )
  14702. as
  14703.     BEGIN
  14704.     select
  14705.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14706.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14707.         PK_TABLE_NAME         = o1.name,
  14708.         PK_COLUMN_NAME         = c1.name,
  14709.         PK_COLUMN_GUID        = convert(binary(16),null),
  14710.         PK_COLUMN_PROPID    = convert(int,null),
  14711.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14712.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  14713.         FK_TABLE_NAME         = o2.name,
  14714.         FK_COLUMN_NAME         = c2.name,
  14715.         FK_COLUMN_GUID        = convert(binary(16),null),
  14716.         FK_COLUMN_PROPID    = convert(int,null),
  14717.         ORDINAL                = convert(int,1),
  14718.         UPDATE_RULE            = 'NO ACTION',
  14719.         DELETE_RULE         = 'NO ACTION',
  14720.         PK_NAME                = i.name,
  14721.         FK_NAME                = object_name(r.constid),
  14722.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14723.     from
  14724.         sysobjects o1, sysobjects o2,
  14725.         syscolumns c1, syscolumns c2,
  14726.         sysreferences r, sysindexes i, sysusers u
  14727.     where    
  14728.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14729.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14730.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14731.     and (@pk_table_name is null or @pk_table_name = o1.name)
  14732.     and (@fk_table_name is null or @fk_table_name = o2.name)
  14733.     and    o1.id = r.rkeyid
  14734.     and    o1.id = c1.id
  14735.     and    c1.colid = r.rkey1
  14736.     and r.fkeyid = o2.id
  14737.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14738.     and    o2.id = c2.id    
  14739.     and    c2.colid = r.fkey1
  14740.     and i.id = r.rkeyid
  14741.     and i.indid = r.rkeyindid
  14742.     and u.uid = user_id()
  14743.     and ( suser_id() = 1     /* User is the System Administrator */
  14744.     or (
  14745.         o1.uid = user_id()     /* User created the object */
  14746.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  14747.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  14748.             from sysprotects p
  14749.             where p.id = o1.id
  14750.             /* get rows for public,current user,user's group */
  14751.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  14752.             /* check for SELECT,EXECUTE privilege */
  14753.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  14754.             ) = 1    /* final magic...compare Grants      */
  14755.         )
  14756.     and (
  14757.         o2.uid = user_id()     /* User created the object */
  14758.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  14759.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  14760.             from sysprotects p
  14761.             where p.id = o2.id
  14762.             /* get rows for public,current user,user's group */
  14763.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  14764.             /* check for SELECT,EXECUTE privilege */
  14765.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  14766.             ) = 1    /* final magic...compare Grants      */
  14767.         )
  14768.     )
  14769.     union all
  14770.     select
  14771.         db_name(r.rkeydbid),
  14772.         user_name(o1.uid),
  14773.         o1.name,
  14774.         c1.name,
  14775.         convert(binary(16),null),
  14776.         convert(int,null),
  14777.         db_name(r.fkeydbid),
  14778.         user_name(o2.uid),
  14779.         o2.name,
  14780.         c2.name,
  14781.         convert(binary(16),null),
  14782.         convert(int,null),
  14783.         convert(int,2),
  14784.         'NO ACTION',
  14785.         'NO ACTION',
  14786.         i.name,
  14787.         object_name(r.constid),
  14788.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14789.     from
  14790.         sysobjects o1, sysobjects o2,
  14791.         syscolumns c1, syscolumns c2,
  14792.         sysreferences r, sysindexes i, sysusers u
  14793.     where    
  14794.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14795.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14796.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14797.     and (@pk_table_name is null or @pk_table_name = o1.name)
  14798.     and (@fk_table_name is null or @fk_table_name = o2.name)
  14799.     and    o1.id = r.rkeyid
  14800.     and    r.keycnt >= 2
  14801.     and    o1.id = c1.id
  14802.     and    c1.colid = r.rkey2
  14803.     and r.fkeyid = o2.id
  14804.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14805.     and    o2.id = c2.id    
  14806.     and    c2.colid = r.fkey2
  14807.     and i.id = r.rkeyid
  14808.     and i.indid = r.rkeyindid
  14809.     and u.uid = user_id()
  14810.     and ( suser_id() = 1     /* User is the System Administrator */
  14811.     or (
  14812.         o1.uid = user_id()     /* User created the object */
  14813.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  14814.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  14815.             from sysprotects p
  14816.             where p.id = o1.id
  14817.             /* get rows for public,current user,user's group */
  14818.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  14819.             /* check for SELECT,EXECUTE privilege */
  14820.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  14821.             ) = 1    /* final magic...compare Grants      */
  14822.         )
  14823.     and (
  14824.         o2.uid = user_id()     /* User created the object */
  14825.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  14826.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  14827.             from sysprotects p
  14828.             where p.id = o2.id
  14829.             /* get rows for public,current user,user's group */
  14830.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  14831.             /* check for SELECT,EXECUTE privilege */
  14832.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  14833.             ) = 1    /* final magic...compare Grants      */
  14834.         )
  14835.     )
  14836.     union all
  14837.     select
  14838.         db_name(r.rkeydbid),
  14839.         user_name(o1.uid),
  14840.         o1.name,
  14841.         c1.name,
  14842.         convert(binary(16),null),
  14843.         convert(int,null),
  14844.         db_name(r.fkeydbid),
  14845.         user_name(o2.uid),
  14846.         o2.name,
  14847.         c2.name,
  14848.         convert(binary(16),null),
  14849.         convert(int,null),
  14850.         convert(int,3),
  14851.         'NO ACTION',
  14852.         'NO ACTION',
  14853.         i.name,
  14854.         object_name(r.constid),
  14855.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14856.     from
  14857.         sysobjects o1, sysobjects o2,
  14858.         syscolumns c1, syscolumns c2,
  14859.         sysreferences r, sysindexes i, sysusers u
  14860.     where    
  14861.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14862.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14863.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14864.     and (@pk_table_name is null or @pk_table_name = o1.name)
  14865.     and (@fk_table_name is null or @fk_table_name = o2.name)
  14866.     and    o1.id = r.rkeyid
  14867.     and    r.keycnt >= 3
  14868.     and    o1.id = c1.id
  14869.     and    c1.colid = r.rkey3
  14870.     and r.fkeyid = o2.id
  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.fkey3
  14874.     and i.id = r.rkeyid
  14875.     and i.indid = r.rkeyindid
  14876.     and u.uid = user_id()
  14877.     and ( suser_id() = 1     /* User is the System Administrator */
  14878.     or (
  14879.         o1.uid = user_id()     /* User created the object */
  14880.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  14881.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  14882.             from sysprotects p
  14883.             where p.id = o1.id
  14884.             /* get rows for public,current user,user's group */
  14885.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  14886.             /* check for SELECT,EXECUTE privilege */
  14887.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  14888.             ) = 1    /* final magic...compare Grants      */
  14889.         )
  14890.     and (
  14891.         o2.uid = user_id()     /* User created the object */
  14892.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  14893.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  14894.             from sysprotects p
  14895.             where p.id = o2.id
  14896.             /* get rows for public,current user,user's group */
  14897.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  14898.             /* check for SELECT,EXECUTE privilege */
  14899.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  14900.             ) = 1    /* final magic...compare Grants      */
  14901.         )
  14902.     )
  14903.     union all
  14904.     select
  14905.         db_name(r.rkeydbid),
  14906.         user_name(o1.uid),
  14907.         o1.name,
  14908.         c1.name,
  14909.         convert(binary(16),null),
  14910.         convert(int,null),
  14911.         db_name(r.fkeydbid),
  14912.         user_name(o2.uid),
  14913.         o2.name,
  14914.         c2.name,
  14915.         convert(binary(16),null),
  14916.         convert(int,null),
  14917.         convert(int,4),
  14918.         'NO ACTION',
  14919.         'NO ACTION',
  14920.         i.name,
  14921.         object_name(r.constid),
  14922.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14923.     from
  14924.         sysobjects o1, sysobjects o2,
  14925.         syscolumns c1, syscolumns c2,
  14926.         sysreferences r, sysindexes i, sysusers u
  14927.     where    
  14928.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14929.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14930.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14931.     and (@pk_table_name is null or @pk_table_name = o1.name)
  14932.     and (@fk_table_name is null or @fk_table_name = o2.name)
  14933.     and    o1.id = r.rkeyid
  14934.     and    r.keycnt >= 4
  14935.     and    o1.id = c1.id
  14936.     and    c1.colid = r.rkey4
  14937.     and r.fkeyid = o2.id
  14938.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14939.     and    o2.id = c2.id    
  14940.     and    c2.colid = r.fkey4
  14941.     and i.id = r.rkeyid
  14942.     and i.indid = r.rkeyindid
  14943.     and u.uid = user_id()
  14944.     and ( suser_id() = 1     /* User is the System Administrator */
  14945.     or (
  14946.         o1.uid = user_id()     /* User created the object */
  14947.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  14948.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  14949.             from sysprotects p
  14950.             where p.id = o1.id
  14951.             /* get rows for public,current user,user's group */
  14952.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  14953.             /* check for SELECT,EXECUTE privilege */
  14954.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  14955.             ) = 1    /* final magic...compare Grants      */
  14956.         )
  14957.     and (
  14958.         o2.uid = user_id()     /* User created the object */
  14959.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  14960.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  14961.             from sysprotects p
  14962.             where p.id = o2.id
  14963.             /* get rows for public,current user,user's group */
  14964.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  14965.             /* check for SELECT,EXECUTE privilege */
  14966.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  14967.             ) = 1    /* final magic...compare Grants      */
  14968.         )
  14969.     )
  14970.     union all
  14971.     select
  14972.         db_name(r.rkeydbid),
  14973.         user_name(o1.uid),
  14974.         o1.name,
  14975.         c1.name,
  14976.         convert(binary(16),null),
  14977.         convert(int,null),
  14978.         db_name(r.fkeydbid),
  14979.         user_name(o2.uid),
  14980.         o2.name,
  14981.         c2.name,
  14982.         convert(binary(16),null),
  14983.         convert(int,null),
  14984.         convert(int,5),
  14985.         'NO ACTION',
  14986.         'NO ACTION',
  14987.         i.name,
  14988.         object_name(r.constid),
  14989.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14990.     from
  14991.         sysobjects o1, sysobjects o2,
  14992.         syscolumns c1, syscolumns c2,
  14993.         sysreferences r, sysindexes i, sysusers u
  14994.     where    
  14995.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14996.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14997.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14998.     and (@pk_table_name is null or @pk_table_name = o1.name)
  14999.     and (@fk_table_name is null or @fk_table_name = o2.name)
  15000.     and    o1.id = r.rkeyid
  15001.     and    r.keycnt >= 5
  15002.     and    o1.id = c1.id
  15003.     and    c1.colid = r.rkey5
  15004.     and r.fkeyid = o2.id
  15005.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15006.     and    o2.id = c2.id    
  15007.     and    c2.colid = r.fkey5
  15008.     and i.id = r.rkeyid
  15009.     and i.indid = r.rkeyindid
  15010.     and u.uid = user_id()
  15011.     and ( suser_id() = 1     /* User is the System Administrator */
  15012.     or (
  15013.         o1.uid = user_id()     /* User created the object */
  15014.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15015.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15016.             from sysprotects p
  15017.             where p.id = o1.id
  15018.             /* get rows for public,current user,user's group */
  15019.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15020.             /* check for SELECT,EXECUTE privilege */
  15021.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15022.             ) = 1    /* final magic...compare Grants      */
  15023.         )
  15024.     and (
  15025.         o2.uid = user_id()     /* User created the object */
  15026.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15027.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15028.             from sysprotects p
  15029.             where p.id = o2.id
  15030.             /* get rows for public,current user,user's group */
  15031.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15032.             /* check for SELECT,EXECUTE privilege */
  15033.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15034.             ) = 1    /* final magic...compare Grants      */
  15035.         )
  15036.     )
  15037.     union all
  15038.     select
  15039.         db_name(r.rkeydbid),
  15040.         user_name(o1.uid),
  15041.         o1.name,
  15042.         c1.name,
  15043.         convert(binary(16),null),
  15044.         convert(int,null),
  15045.         db_name(r.fkeydbid),
  15046.         user_name(o2.uid),
  15047.         o2.name,
  15048.         c2.name,
  15049.         convert(binary(16),null),
  15050.         convert(int,null),
  15051.         convert(int,6),
  15052.         'NO ACTION',
  15053.         'NO ACTION',
  15054.         i.name,
  15055.         object_name(r.constid),
  15056.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15057.     from
  15058.         sysobjects o1, sysobjects o2,
  15059.         syscolumns c1, syscolumns c2,
  15060.         sysreferences r, sysindexes i, sysusers u
  15061.     where    
  15062.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15063.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15064.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15065.     and (@pk_table_name is null or @pk_table_name = o1.name)
  15066.     and (@fk_table_name is null or @fk_table_name = o2.name)
  15067.     and    o1.id = r.rkeyid
  15068.     and    r.keycnt >= 6
  15069.     and    o1.id = c1.id
  15070.     and    c1.colid = r.rkey6
  15071.     and r.fkeyid = o2.id
  15072.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15073.     and    o2.id = c2.id    
  15074.     and    c2.colid = r.fkey6
  15075.     and i.id = r.rkeyid
  15076.     and i.indid = r.rkeyindid
  15077.     and u.uid = user_id()
  15078.     and ( suser_id() = 1     /* User is the System Administrator */
  15079.     or (
  15080.         o1.uid = user_id()     /* User created the object */
  15081.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15082.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15083.             from sysprotects p
  15084.             where p.id = o1.id
  15085.             /* get rows for public,current user,user's group */
  15086.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15087.             /* check for SELECT,EXECUTE privilege */
  15088.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15089.             ) = 1    /* final magic...compare Grants      */
  15090.         )
  15091.     and (
  15092.         o2.uid = user_id()     /* User created the object */
  15093.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15094.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15095.             from sysprotects p
  15096.             where p.id = o2.id
  15097.             /* get rows for public,current user,user's group */
  15098.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15099.             /* check for SELECT,EXECUTE privilege */
  15100.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15101.             ) = 1    /* final magic...compare Grants      */
  15102.         )
  15103.     )
  15104.     union all
  15105.     select
  15106.         db_name(r.rkeydbid),
  15107.         user_name(o1.uid),
  15108.         o1.name,
  15109.         c1.name,
  15110.         convert(binary(16),null),
  15111.         convert(int,null),
  15112.         db_name(r.fkeydbid),
  15113.         user_name(o2.uid),
  15114.         o2.name,
  15115.         c2.name,
  15116.         convert(binary(16),null),
  15117.         convert(int,null),
  15118.         convert(int,7),
  15119.         'NO ACTION',
  15120.         'NO ACTION',
  15121.         i.name,
  15122.         object_name(r.constid),
  15123.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15124.     from
  15125.         sysobjects o1, sysobjects o2,
  15126.         syscolumns c1, syscolumns c2,
  15127.         sysreferences r, sysindexes i, sysusers u
  15128.     where    
  15129.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15130.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15131.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15132.     and (@pk_table_name is null or @pk_table_name = o1.name)
  15133.     and (@fk_table_name is null or @fk_table_name = o2.name)
  15134.     and    o1.id = r.rkeyid
  15135.     and    r.keycnt >= 7
  15136.     and    o1.id = c1.id
  15137.     and    c1.colid = r.rkey7
  15138.     and r.fkeyid = o2.id
  15139.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15140.     and    o2.id = c2.id    
  15141.     and    c2.colid = r.fkey7
  15142.     and i.id = r.rkeyid
  15143.     and i.indid = r.rkeyindid
  15144.     and u.uid = user_id()
  15145.     and ( suser_id() = 1     /* User is the System Administrator */
  15146.     or (
  15147.         o1.uid = user_id()     /* User created the object */
  15148.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15149.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15150.             from sysprotects p
  15151.             where p.id = o1.id
  15152.             /* get rows for public,current user,user's group */
  15153.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15154.             /* check for SELECT,EXECUTE privilege */
  15155.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15156.             ) = 1    /* final magic...compare Grants      */
  15157.         )
  15158.     and (
  15159.         o2.uid = user_id()     /* User created the object */
  15160.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15161.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15162.             from sysprotects p
  15163.             where p.id = o2.id
  15164.             /* get rows for public,current user,user's group */
  15165.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15166.             /* check for SELECT,EXECUTE privilege */
  15167.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15168.             ) = 1    /* final magic...compare Grants      */
  15169.         )
  15170.     )
  15171.     union all
  15172.     select
  15173.         db_name(r.rkeydbid),
  15174.         user_name(o1.uid),
  15175.         o1.name,
  15176.         c1.name,
  15177.         convert(binary(16),null),
  15178.         convert(int,null),
  15179.         db_name(r.fkeydbid),
  15180.         user_name(o2.uid),
  15181.         o2.name,
  15182.         c2.name,
  15183.         convert(binary(16),null),
  15184.         convert(int,null),
  15185.         convert(int,8),
  15186.         'NO ACTION',
  15187.         'NO ACTION',
  15188.         i.name,
  15189.         object_name(r.constid),
  15190.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15191.     from
  15192.         sysobjects o1, sysobjects o2,
  15193.         syscolumns c1, syscolumns c2,
  15194.         sysreferences r, sysindexes i, sysusers u
  15195.     where    
  15196.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15197.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15198.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15199.     and (@pk_table_name is null or @pk_table_name = o1.name)
  15200.     and (@fk_table_name is null or @fk_table_name = o2.name)
  15201.     and    o1.id = r.rkeyid
  15202.     and    r.keycnt >= 8
  15203.     and    o1.id = c1.id
  15204.     and    c1.colid = r.rkey8
  15205.     and r.fkeyid = o2.id
  15206.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15207.     and    o2.id = c2.id    
  15208.     and    c2.colid = r.fkey8
  15209.     and i.id = r.rkeyid
  15210.     and i.indid = r.rkeyindid
  15211.     and u.uid = user_id()
  15212.     and ( suser_id() = 1     /* User is the System Administrator */
  15213.     or (
  15214.         o1.uid = user_id()     /* User created the object */
  15215.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15216.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15217.             from sysprotects p
  15218.             where p.id = o1.id
  15219.             /* get rows for public,current user,user's group */
  15220.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15221.             /* check for SELECT,EXECUTE privilege */
  15222.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15223.             ) = 1    /* final magic...compare Grants      */
  15224.         )
  15225.     and (
  15226.         o2.uid = user_id()     /* User created the object */
  15227.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15228.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15229.             from sysprotects p
  15230.             where p.id = o2.id
  15231.             /* get rows for public,current user,user's group */
  15232.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15233.             /* check for SELECT,EXECUTE privilege */
  15234.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15235.             ) = 1    /* final magic...compare Grants      */
  15236.         )
  15237.     )
  15238.     union all
  15239.     select
  15240.         db_name(r.rkeydbid),
  15241.         user_name(o1.uid),
  15242.         o1.name,
  15243.         c1.name,
  15244.         convert(binary(16),null),
  15245.         convert(int,null),
  15246.         db_name(r.fkeydbid),
  15247.         user_name(o2.uid),
  15248.         o2.name,
  15249.         c2.name,
  15250.         convert(binary(16),null),
  15251.         convert(int,null),
  15252.         convert(int,9),
  15253.         'NO ACTION',
  15254.         'NO ACTION',
  15255.         i.name,
  15256.         object_name(r.constid),
  15257.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15258.     from
  15259.         sysobjects o1, sysobjects o2,
  15260.         syscolumns c1, syscolumns c2,
  15261.         sysreferences r, sysindexes i, sysusers u
  15262.     where    
  15263.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15264.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15265.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15266.     and (@pk_table_name is null or @pk_table_name = o1.name)
  15267.     and (@fk_table_name is null or @fk_table_name = o2.name)
  15268.     and    o1.id = r.rkeyid
  15269.     and    r.keycnt >= 9
  15270.     and    o1.id = c1.id
  15271.     and    c1.colid = r.rkey9
  15272.     and r.fkeyid = o2.id
  15273.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15274.     and    o2.id = c2.id    
  15275.     and    c2.colid = r.fkey9
  15276.     and i.id = r.rkeyid
  15277.     and i.indid = r.rkeyindid
  15278.     and u.uid = user_id()
  15279.     and ( suser_id() = 1     /* User is the System Administrator */
  15280.     or (
  15281.         o1.uid = user_id()     /* User created the object */
  15282.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15283.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15284.             from sysprotects p
  15285.             where p.id = o1.id
  15286.             /* get rows for public,current user,user's group */
  15287.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15288.             /* check for SELECT,EXECUTE privilege */
  15289.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15290.             ) = 1    /* final magic...compare Grants      */
  15291.         )
  15292.     and (
  15293.         o2.uid = user_id()     /* User created the object */
  15294.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15295.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15296.             from sysprotects p
  15297.             where p.id = o2.id
  15298.             /* get rows for public,current user,user's group */
  15299.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15300.             /* check for SELECT,EXECUTE privilege */
  15301.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15302.             ) = 1    /* final magic...compare Grants      */
  15303.         )
  15304.     )
  15305.     union all
  15306.     select
  15307.         db_name(r.rkeydbid),
  15308.         user_name(o1.uid),
  15309.         o1.name,
  15310.         c1.name,
  15311.         convert(binary(16),null),
  15312.         convert(int,null),
  15313.         db_name(r.fkeydbid),
  15314.         user_name(o2.uid),
  15315.         o2.name,
  15316.         c2.name,
  15317.         convert(binary(16),null),
  15318.         convert(int,null),
  15319.         convert(int,10),
  15320.         'NO ACTION',
  15321.         'NO ACTION',
  15322.         i.name,
  15323.         object_name(r.constid),
  15324.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15325.     from
  15326.         sysobjects o1, sysobjects o2,
  15327.         syscolumns c1, syscolumns c2,
  15328.         sysreferences r, sysindexes i, sysusers u
  15329.     where    
  15330.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15331.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15332.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15333.     and (@pk_table_name is null or @pk_table_name = o1.name)
  15334.     and (@fk_table_name is null or @fk_table_name = o2.name)
  15335.     and    o1.id = r.rkeyid
  15336.     and    r.keycnt >= 10
  15337.     and    o1.id = c1.id
  15338.     and    c1.colid = r.rkey10
  15339.     and r.fkeyid = o2.id
  15340.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15341.     and    o2.id = c2.id    
  15342.     and    c2.colid = r.fkey10
  15343.     and i.id = r.rkeyid
  15344.     and i.indid = r.rkeyindid
  15345.     and u.uid = user_id()
  15346.     and ( suser_id() = 1     /* User is the System Administrator */
  15347.     or (
  15348.         o1.uid = user_id()     /* User created the object */
  15349.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15350.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15351.             from sysprotects p
  15352.             where p.id = o1.id
  15353.             /* get rows for public,current user,user's group */
  15354.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15355.             /* check for SELECT,EXECUTE privilege */
  15356.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15357.             ) = 1    /* final magic...compare Grants      */
  15358.         )
  15359.     and (
  15360.         o2.uid = user_id()     /* User created the object */
  15361.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15362.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15363.             from sysprotects p
  15364.             where p.id = o2.id
  15365.             /* get rows for public,current user,user's group */
  15366.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15367.             /* check for SELECT,EXECUTE privilege */
  15368.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15369.             ) = 1    /* final magic...compare Grants      */
  15370.         )
  15371.     )
  15372.     union all
  15373.     select
  15374.         db_name(r.rkeydbid),
  15375.         user_name(o1.uid),
  15376.         o1.name,
  15377.         c1.name,
  15378.         convert(binary(16),null),
  15379.         convert(int,null),
  15380.         db_name(r.fkeydbid),
  15381.         user_name(o2.uid),
  15382.         o2.name,
  15383.         c2.name,
  15384.         convert(binary(16),null),
  15385.         convert(int,null),
  15386.         convert(int,11),
  15387.         'NO ACTION',
  15388.         'NO ACTION',
  15389.         i.name,
  15390.         object_name(r.constid),
  15391.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15392.     from
  15393.         sysobjects o1, sysobjects o2,
  15394.         syscolumns c1, syscolumns c2,
  15395.         sysreferences r, sysindexes i, sysusers u
  15396.     where    
  15397.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15398.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15399.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15400.     and (@pk_table_name is null or @pk_table_name = o1.name)
  15401.     and (@fk_table_name is null or @fk_table_name = o2.name)
  15402.     and    o1.id = r.rkeyid
  15403.     and    r.keycnt >= 11
  15404.     and    o1.id = c1.id
  15405.     and    c1.colid = r.rkey11
  15406.     and r.fkeyid = o2.id
  15407.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15408.     and    o2.id = c2.id    
  15409.     and    c2.colid = r.fkey11
  15410.     and i.id = r.rkeyid
  15411.     and i.indid = r.rkeyindid
  15412.     and u.uid = user_id()
  15413.     and ( suser_id() = 1     /* User is the System Administrator */
  15414.     or (
  15415.         o1.uid = user_id()     /* User created the object */
  15416.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15417.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15418.             from sysprotects p
  15419.             where p.id = o1.id
  15420.             /* get rows for public,current user,user's group */
  15421.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15422.             /* check for SELECT,EXECUTE privilege */
  15423.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15424.             ) = 1    /* final magic...compare Grants      */
  15425.         )
  15426.     and (
  15427.         o2.uid = user_id()     /* User created the object */
  15428.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15429.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15430.             from sysprotects p
  15431.             where p.id = o2.id
  15432.             /* get rows for public,current user,user's group */
  15433.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15434.             /* check for SELECT,EXECUTE privilege */
  15435.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15436.             ) = 1    /* final magic...compare Grants      */
  15437.         )
  15438.     )
  15439.     union all
  15440.     select
  15441.         db_name(r.rkeydbid),
  15442.         user_name(o1.uid),
  15443.         o1.name,
  15444.         c1.name,
  15445.         convert(binary(16),null),
  15446.         convert(int,null),
  15447.         db_name(r.fkeydbid),
  15448.         user_name(o2.uid),
  15449.         o2.name,
  15450.         c2.name,
  15451.         convert(binary(16),null),
  15452.         convert(int,null),
  15453.         convert(int,12),
  15454.         'NO ACTION',
  15455.         'NO ACTION',
  15456.         i.name,
  15457.         object_name(r.constid),
  15458.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15459.     from
  15460.         sysobjects o1, sysobjects o2,
  15461.         syscolumns c1, syscolumns c2,
  15462.         sysreferences r, sysindexes i, sysusers u
  15463.     where    
  15464.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15465.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15466.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15467.     and (@pk_table_name is null or @pk_table_name = o1.name)
  15468.     and (@fk_table_name is null or @fk_table_name = o2.name)
  15469.     and    o1.id = r.rkeyid
  15470.     and    r.keycnt >= 12
  15471.     and    o1.id = c1.id
  15472.     and    c1.colid = r.rkey12
  15473.     and r.fkeyid = o2.id
  15474.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15475.     and    o2.id = c2.id    
  15476.     and    c2.colid = r.fkey12
  15477.     and i.id = r.rkeyid
  15478.     and i.indid = r.rkeyindid
  15479.     and u.uid = user_id()
  15480.     and ( suser_id() = 1     /* User is the System Administrator */
  15481.     or (
  15482.         o1.uid = user_id()     /* User created the object */
  15483.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15484.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15485.             from sysprotects p
  15486.             where p.id = o1.id
  15487.             /* get rows for public,current user,user's group */
  15488.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15489.             /* check for SELECT,EXECUTE privilege */
  15490.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15491.             ) = 1    /* final magic...compare Grants      */
  15492.         )
  15493.     and (
  15494.         o2.uid = user_id()     /* User created the object */
  15495.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15496.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15497.             from sysprotects p
  15498.             where p.id = o2.id
  15499.             /* get rows for public,current user,user's group */
  15500.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15501.             /* check for SELECT,EXECUTE privilege */
  15502.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15503.             ) = 1    /* final magic...compare Grants      */
  15504.         )
  15505.     )
  15506.     union all
  15507.     select
  15508.         db_name(r.rkeydbid),
  15509.         user_name(o1.uid),
  15510.         o1.name,
  15511.         c1.name,
  15512.         convert(binary(16),null),
  15513.         convert(int,null),
  15514.         db_name(r.fkeydbid),
  15515.         user_name(o2.uid),
  15516.         o2.name,
  15517.         c2.name,
  15518.         convert(binary(16),null),
  15519.         convert(int,null),
  15520.         convert(int,13),
  15521.         'NO ACTION',
  15522.         'NO ACTION',
  15523.         i.name,
  15524.         object_name(r.constid),
  15525.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15526.     from
  15527.         sysobjects o1, sysobjects o2,
  15528.         syscolumns c1, syscolumns c2,
  15529.         sysreferences r, sysindexes i, sysusers u
  15530.     where    
  15531.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15532.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15533.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15534.     and (@pk_table_name is null or @pk_table_name = o1.name)
  15535.     and (@fk_table_name is null or @fk_table_name = o2.name)
  15536.     and    o1.id = r.rkeyid
  15537.     and    r.keycnt >= 13
  15538.     and    o1.id = c1.id
  15539.     and    c1.colid = r.rkey13
  15540.     and r.fkeyid = o2.id
  15541.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15542.     and    o2.id = c2.id    
  15543.     and    c2.colid = r.fkey13
  15544.     and i.id = r.rkeyid
  15545.     and i.indid = r.rkeyindid
  15546.     and u.uid = user_id()
  15547.     and ( suser_id() = 1     /* User is the System Administrator */
  15548.     or (
  15549.         o1.uid = user_id()     /* User created the object */
  15550.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15551.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15552.             from sysprotects p
  15553.             where p.id = o1.id
  15554.             /* get rows for public,current user,user's group */
  15555.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15556.             /* check for SELECT,EXECUTE privilege */
  15557.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15558.             ) = 1    /* final magic...compare Grants      */
  15559.         )
  15560.     and (
  15561.         o2.uid = user_id()     /* User created the object */
  15562.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15563.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15564.             from sysprotects p
  15565.             where p.id = o2.id
  15566.             /* get rows for public,current user,user's group */
  15567.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15568.             /* check for SELECT,EXECUTE privilege */
  15569.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15570.             ) = 1    /* final magic...compare Grants      */
  15571.         )
  15572.     )
  15573.     union all
  15574.     select
  15575.         db_name(r.rkeydbid),
  15576.         user_name(o1.uid),
  15577.         o1.name,
  15578.         c1.name,
  15579.         convert(binary(16),null),
  15580.         convert(int,null),
  15581.         db_name(r.fkeydbid),
  15582.         user_name(o2.uid),
  15583.         o2.name,
  15584.         c2.name,
  15585.         convert(binary(16),null),
  15586.         convert(int,null),
  15587.         convert(int,14),
  15588.         'NO ACTION',
  15589.         'NO ACTION',
  15590.         i.name,
  15591.         object_name(r.constid),
  15592.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15593.     from
  15594.         sysobjects o1, sysobjects o2,
  15595.         syscolumns c1, syscolumns c2,
  15596.         sysreferences r, sysindexes i, sysusers u
  15597.     where    
  15598.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15599.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15600.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15601.     and (@pk_table_name is null or @pk_table_name = o1.name)
  15602.     and (@fk_table_name is null or @fk_table_name = o2.name)
  15603.     and    o1.id = r.rkeyid
  15604.     and    r.keycnt >= 14
  15605.     and    o1.id = c1.id
  15606.     and    c1.colid = r.rkey14
  15607.     and r.fkeyid = o2.id
  15608.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15609.     and    o2.id = c2.id    
  15610.     and    c2.colid = r.fkey14
  15611.     and i.id = r.rkeyid
  15612.     and i.indid = r.rkeyindid
  15613.     and u.uid = user_id()
  15614.     and ( suser_id() = 1     /* User is the System Administrator */
  15615.     or (
  15616.         o1.uid = user_id()     /* User created the object */
  15617.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15618.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15619.             from sysprotects p
  15620.             where p.id = o1.id
  15621.             /* get rows for public,current user,user's group */
  15622.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15623.             /* check for SELECT,EXECUTE privilege */
  15624.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15625.             ) = 1    /* final magic...compare Grants      */
  15626.         )
  15627.     and (
  15628.         o2.uid = user_id()     /* User created the object */
  15629.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15630.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15631.             from sysprotects p
  15632.             where p.id = o2.id
  15633.             /* get rows for public,current user,user's group */
  15634.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15635.             /* check for SELECT,EXECUTE privilege */
  15636.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15637.             ) = 1    /* final magic...compare Grants      */
  15638.         )
  15639.     )
  15640.     union all
  15641.     select
  15642.         db_name(r.rkeydbid),
  15643.         user_name(o1.uid),
  15644.         o1.name,
  15645.         c1.name,
  15646.         convert(binary(16),null),
  15647.         convert(int,null),
  15648.         db_name(r.fkeydbid),
  15649.         user_name(o2.uid),
  15650.         o2.name,
  15651.         c2.name,
  15652.         convert(binary(16),null),
  15653.         convert(int,null),
  15654.         convert(int,15),
  15655.         'NO ACTION',
  15656.         'NO ACTION',
  15657.         i.name,
  15658.         object_name(r.constid),
  15659.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15660.     from
  15661.         sysobjects o1, sysobjects o2,
  15662.         syscolumns c1, syscolumns c2,
  15663.         sysreferences r, sysindexes i, sysusers u
  15664.     where    
  15665.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15666.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15667.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15668.     and (@pk_table_name is null or @pk_table_name = o1.name)
  15669.     and (@fk_table_name is null or @fk_table_name = o2.name)
  15670.     and    o1.id = r.rkeyid
  15671.     and    r.keycnt >= 15
  15672.     and    o1.id = c1.id
  15673.     and    c1.colid = r.rkey15
  15674.     and r.fkeyid = o2.id
  15675.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15676.     and    o2.id = c2.id    
  15677.     and    c2.colid = r.fkey15
  15678.     and i.id = r.rkeyid
  15679.     and i.indid = r.rkeyindid
  15680.     and u.uid = user_id()
  15681.     and ( suser_id() = 1     /* User is the System Administrator */
  15682.     or (
  15683.         o1.uid = user_id()     /* User created the object */
  15684.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15685.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15686.             from sysprotects p
  15687.             where p.id = o1.id
  15688.             /* get rows for public,current user,user's group */
  15689.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15690.             /* check for SELECT,EXECUTE privilege */
  15691.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15692.             ) = 1    /* final magic...compare Grants      */
  15693.         )
  15694.     and (
  15695.         o2.uid = user_id()     /* User created the object */
  15696.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15697.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15698.             from sysprotects p
  15699.             where p.id = o2.id
  15700.             /* get rows for public,current user,user's group */
  15701.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15702.             /* check for SELECT,EXECUTE privilege */
  15703.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15704.             ) = 1    /* final magic...compare Grants      */
  15705.         )
  15706.     )
  15707.     union all
  15708.     select
  15709.         db_name(r.rkeydbid),
  15710.         user_name(o1.uid),
  15711.         o1.name,
  15712.         c1.name,
  15713.         convert(binary(16),null),
  15714.         convert(int,null),
  15715.         db_name(r.fkeydbid),
  15716.         user_name(o2.uid),
  15717.         o2.name,
  15718.         c2.name,
  15719.         convert(binary(16),null),
  15720.         convert(int,null),
  15721.         convert(int,16),
  15722.         'NO ACTION',
  15723.         'NO ACTION',
  15724.         i.name,
  15725.         object_name(r.constid),
  15726.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15727.     from
  15728.         sysobjects o1, sysobjects o2,
  15729.         syscolumns c1, syscolumns c2,
  15730.         sysreferences r, sysindexes i, sysusers u
  15731.     where    
  15732.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15733.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15734.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15735.     and (@pk_table_name is null or @pk_table_name = o1.name)
  15736.     and (@fk_table_name is null or @fk_table_name = o2.name)
  15737.     and    o1.id = r.rkeyid
  15738.     and    o1.id = c1.id
  15739.     and    r.keycnt >= 16
  15740.     and    c1.colid = r.rkey16
  15741.     and r.fkeyid = o2.id
  15742.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15743.     and    o2.id = c2.id    
  15744.     and    c2.colid = r.fkey16
  15745.     and i.id = r.rkeyid
  15746.     and i.indid = r.rkeyindid
  15747.     and u.uid = user_id()
  15748.     and ( suser_id() = 1     /* User is the System Administrator */
  15749.     or (
  15750.         o1.uid = user_id()     /* User created the object */
  15751.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15752.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15753.             from sysprotects p
  15754.             where p.id = o1.id
  15755.             /* get rows for public,current user,user's group */
  15756.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15757.             /* check for SELECT,EXECUTE privilege */
  15758.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15759.             ) = 1    /* final magic...compare Grants      */
  15760.         )
  15761.     and (
  15762.         o2.uid = user_id()     /* User created the object */
  15763.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15764.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15765.             from sysprotects p
  15766.             where p.id = o2.id
  15767.             /* get rows for public,current user,user's group */
  15768.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15769.             /* check for SELECT,EXECUTE privilege */
  15770.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15771.             ) = 1    /* final magic...compare Grants      */
  15772.         )
  15773.     )
  15774.     order by 8,9,2,3,13
  15775.     END
  15776. go
  15777. dump tran master with no_log
  15778. go
  15779. CREATE PROCEDURE sp_foreign_keys_rowset;2
  15780.     (
  15781.        @handle            int output,
  15782.        @scrollopt        int output,
  15783.     @ccopt            int output,
  15784.     @rows            int output,
  15785.      @pk_table_name        varchar(255) = null,
  15786.     @pk_table_schema    varchar(255) = null,
  15787.     @pk_table_catalog    varchar(255) = null,
  15788.     @fk_table_name        varchar(255) = null,
  15789.     @fk_table_schema    varchar(255) = null,
  15790.     @fk_table_catalog    varchar(255) = null
  15791.     )
  15792. as
  15793.     declare @ret int
  15794.     
  15795.     SET NOCOUNT ON
  15796.  
  15797.     create table #spfkeysrowset1
  15798.         (
  15799.         PK_TABLE_CATALOG    sysname not null,
  15800.         PK_TABLE_SCHEMA        sysname not null,
  15801.         PK_TABLE_NAME         sysname not null,
  15802.         PK_COLUMN_NAME         sysname not null,
  15803.         PK_COLUMN_GUID        binary(16) null,
  15804.         PK_COLUMN_PROPID    int null,
  15805.         FK_TABLE_CATALOG    sysname not null,
  15806.         FK_TABLE_SCHEMA        sysname not null,
  15807.         FK_TABLE_NAME         sysname not null,
  15808.         FK_COLUMN_NAME         sysname not null,
  15809.         FK_COLUMN_GUID        binary(16) null,
  15810.         FK_COLUMN_PROPID    int null,
  15811.         ORDINAL                int not null,
  15812.         UPDATE_RULE            sysname not null,
  15813.         DELETE_RULE         sysname not null,
  15814.         PK_NAME                sysname not null,
  15815.         FK_NAME                sysname not null,
  15816.         DEFERRABILITY        smallint not null        
  15817.         )
  15818.     BEGIN
  15819.     insert into #spfkeysrowset1
  15820.     select
  15821.         db_name(r.rkeydbid),
  15822.         user_name(o1.uid),
  15823.         o1.name,
  15824.         c1.name,
  15825.         convert(binary(16),null),
  15826.         convert(int,null),
  15827.         db_name(r.fkeydbid),
  15828.         user_name(o2.uid),
  15829.         o2.name,
  15830.         c2.name,
  15831.         convert(binary(16),null),
  15832.         convert(int,null),
  15833.         convert(int,1),
  15834.         'NO ACTION',
  15835.         'NO ACTION',
  15836.         i.name,
  15837.         object_name(r.constid),
  15838.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15839.     from
  15840.         sysobjects o1, sysobjects o2,
  15841.         syscolumns c1, syscolumns c2,
  15842.         sysreferences r, sysindexes i, sysusers u
  15843.     where    
  15844.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15845.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15846.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15847.     and (@pk_table_name is null or @pk_table_name = o1.name)
  15848.     and (@fk_table_name is null or @fk_table_name = o2.name)
  15849.     and    o1.id = r.rkeyid
  15850.     and    o1.id = c1.id
  15851.     and    c1.colid = r.rkey1
  15852.     and r.fkeyid = o2.id
  15853.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15854.     and    o2.id = c2.id    
  15855.     and    c2.colid = r.fkey1
  15856.     and i.id = r.rkeyid
  15857.     and i.indid = r.rkeyindid
  15858.     and u.uid = user_id()
  15859.     and ( suser_id() = 1     /* User is the System Administrator */
  15860.     or (
  15861.         o1.uid = user_id()     /* User created the object */
  15862.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15863.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15864.             from sysprotects p
  15865.             where p.id = o1.id
  15866.             /* get rows for public,current user,user's group */
  15867.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15868.             /* check for SELECT,EXECUTE privilege */
  15869.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15870.             ) = 1    /* final magic...compare Grants      */
  15871.         )
  15872.     and (
  15873.         o2.uid = user_id()     /* User created the object */
  15874.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15875.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15876.             from sysprotects p
  15877.             where p.id = o2.id
  15878.             /* get rows for public,current user,user's group */
  15879.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15880.             /* check for SELECT,EXECUTE privilege */
  15881.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15882.             ) = 1    /* final magic...compare Grants      */
  15883.         )
  15884.     )    
  15885.     union all
  15886.     select
  15887.         db_name(r.rkeydbid),
  15888.         user_name(o1.uid),
  15889.         o1.name,
  15890.         c1.name,
  15891.         convert(binary(16),null),
  15892.         convert(int,null),
  15893.         db_name(r.fkeydbid),
  15894.         user_name(o2.uid),
  15895.         o2.name,
  15896.         c2.name,
  15897.         convert(binary(16),null),
  15898.         convert(int,null),
  15899.         convert(int,2),
  15900.         'NO ACTION',
  15901.         'NO ACTION',
  15902.         i.name,
  15903.         object_name(r.constid),
  15904.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15905.     from
  15906.         sysobjects o1, sysobjects o2,
  15907.         syscolumns c1, syscolumns c2,
  15908.         sysreferences r, sysindexes i, sysusers u
  15909.     where    
  15910.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15911.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15912.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15913.     and (@pk_table_name is null or @pk_table_name = o1.name)
  15914.     and (@fk_table_name is null or @fk_table_name = o2.name)
  15915.     and    o1.id = r.rkeyid
  15916.     and    r.keycnt >= 2
  15917.     and    o1.id = c1.id
  15918.     and    c1.colid = r.rkey2
  15919.     and r.fkeyid = o2.id
  15920.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15921.     and    o2.id = c2.id    
  15922.     and    c2.colid = r.fkey2
  15923.     and i.id = r.rkeyid
  15924.     and i.indid = r.rkeyindid
  15925.     and u.uid = user_id()
  15926.     and ( suser_id() = 1     /* User is the System Administrator */
  15927.     or (
  15928.         o1.uid = user_id()     /* User created the object */
  15929.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15930.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15931.             from sysprotects p
  15932.             where p.id = o1.id
  15933.             /* get rows for public,current user,user's group */
  15934.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15935.             /* check for SELECT,EXECUTE privilege */
  15936.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15937.             ) = 1    /* final magic...compare Grants      */
  15938.         )
  15939.     and (
  15940.         o2.uid = user_id()     /* User created the object */
  15941.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15942.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15943.             from sysprotects p
  15944.             where p.id = o2.id
  15945.             /* get rows for public,current user,user's group */
  15946.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15947.             /* check for SELECT,EXECUTE privilege */
  15948.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15949.             ) = 1    /* final magic...compare Grants      */
  15950.         )
  15951.     )    
  15952.     union all
  15953.     select
  15954.         db_name(r.rkeydbid),
  15955.         user_name(o1.uid),
  15956.         o1.name,
  15957.         c1.name,
  15958.         convert(binary(16),null),
  15959.         convert(int,null),
  15960.         db_name(r.fkeydbid),
  15961.         user_name(o2.uid),
  15962.         o2.name,
  15963.         c2.name,
  15964.         convert(binary(16),null),
  15965.         convert(int,null),
  15966.         convert(int,3),
  15967.         'NO ACTION',
  15968.         'NO ACTION',
  15969.         i.name,
  15970.         object_name(r.constid),
  15971.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15972.     from
  15973.         sysobjects o1, sysobjects o2,
  15974.         syscolumns c1, syscolumns c2,
  15975.         sysreferences r, sysindexes i, sysusers u
  15976.     where    
  15977.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15978.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15979.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15980.     and (@pk_table_name is null or @pk_table_name = o1.name)
  15981.     and (@fk_table_name is null or @fk_table_name = o2.name)
  15982.     and    o1.id = r.rkeyid
  15983.     and    r.keycnt >= 3
  15984.     and    o1.id = c1.id
  15985.     and    c1.colid = r.rkey3
  15986.     and r.fkeyid = o2.id
  15987.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15988.     and    o2.id = c2.id    
  15989.     and    c2.colid = r.fkey3
  15990.     and i.id = r.rkeyid
  15991.     and i.indid = r.rkeyindid
  15992.     and u.uid = user_id()
  15993.     and ( suser_id() = 1     /* User is the System Administrator */
  15994.     or (
  15995.         o1.uid = user_id()     /* User created the object */
  15996.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15997.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15998.             from sysprotects p
  15999.             where p.id = o1.id
  16000.             /* get rows for public,current user,user's group */
  16001.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16002.             /* check for SELECT,EXECUTE privilege */
  16003.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16004.             ) = 1    /* final magic...compare Grants      */
  16005.         )
  16006.     and (
  16007.         o2.uid = user_id()     /* User created the object */
  16008.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16009.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16010.             from sysprotects p
  16011.             where p.id = o2.id
  16012.             /* get rows for public,current user,user's group */
  16013.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16014.             /* check for SELECT,EXECUTE privilege */
  16015.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16016.             ) = 1    /* final magic...compare Grants      */
  16017.         )
  16018.     )    
  16019.     union all
  16020.     select
  16021.         db_name(r.rkeydbid),
  16022.         user_name(o1.uid),
  16023.         o1.name,
  16024.         c1.name,
  16025.         convert(binary(16),null),
  16026.         convert(int,null),
  16027.         db_name(r.fkeydbid),
  16028.         user_name(o2.uid),
  16029.         o2.name,
  16030.         c2.name,
  16031.         convert(binary(16),null),
  16032.         convert(int,null),
  16033.         convert(int,4),
  16034.         'NO ACTION',
  16035.         'NO ACTION',
  16036.         i.name,
  16037.         object_name(r.constid),
  16038.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16039.     from
  16040.         sysobjects o1, sysobjects o2,
  16041.         syscolumns c1, syscolumns c2,
  16042.         sysreferences r, sysindexes i, sysusers u
  16043.     where    
  16044.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16045.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16046.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16047.     and (@pk_table_name is null or @pk_table_name = o1.name)
  16048.     and (@fk_table_name is null or @fk_table_name = o2.name)
  16049.     and    o1.id = r.rkeyid
  16050.     and    r.keycnt >= 4
  16051.     and    o1.id = c1.id
  16052.     and    c1.colid = r.rkey4
  16053.     and r.fkeyid = o2.id
  16054.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16055.     and    o2.id = c2.id    
  16056.     and    c2.colid = r.fkey4
  16057.     and i.id = r.rkeyid
  16058.     and i.indid = r.rkeyindid
  16059.     and u.uid = user_id()
  16060.     and ( suser_id() = 1     /* User is the System Administrator */
  16061.     or (
  16062.         o1.uid = user_id()     /* User created the object */
  16063.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16064.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16065.             from sysprotects p
  16066.             where p.id = o1.id
  16067.             /* get rows for public,current user,user's group */
  16068.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16069.             /* check for SELECT,EXECUTE privilege */
  16070.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16071.             ) = 1    /* final magic...compare Grants      */
  16072.         )
  16073.     and (
  16074.         o2.uid = user_id()     /* User created the object */
  16075.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16076.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16077.             from sysprotects p
  16078.             where p.id = o2.id
  16079.             /* get rows for public,current user,user's group */
  16080.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16081.             /* check for SELECT,EXECUTE privilege */
  16082.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16083.             ) = 1    /* final magic...compare Grants      */
  16084.         )
  16085.     )    
  16086.     union all
  16087.     select
  16088.         db_name(r.rkeydbid),
  16089.         user_name(o1.uid),
  16090.         o1.name,
  16091.         c1.name,
  16092.         convert(binary(16),null),
  16093.         convert(int,null),
  16094.         db_name(r.fkeydbid),
  16095.         user_name(o2.uid),
  16096.         o2.name,
  16097.         c2.name,
  16098.         convert(binary(16),null),
  16099.         convert(int,null),
  16100.         convert(int,5),
  16101.         'NO ACTION',
  16102.         'NO ACTION',
  16103.         i.name,
  16104.         object_name(r.constid),
  16105.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16106.     from
  16107.         sysobjects o1, sysobjects o2,
  16108.         syscolumns c1, syscolumns c2,
  16109.         sysreferences r, sysindexes i, sysusers u
  16110.     where    
  16111.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16112.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16113.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16114.     and (@pk_table_name is null or @pk_table_name = o1.name)
  16115.     and (@fk_table_name is null or @fk_table_name = o2.name)
  16116.     and    o1.id = r.rkeyid
  16117.     and    r.keycnt >= 5
  16118.     and    o1.id = c1.id
  16119.     and    c1.colid = r.rkey5
  16120.     and r.fkeyid = o2.id
  16121.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16122.     and    o2.id = c2.id    
  16123.     and    c2.colid = r.fkey5
  16124.     and i.id = r.rkeyid
  16125.     and i.indid = r.rkeyindid
  16126.     and u.uid = user_id()
  16127.     and ( suser_id() = 1     /* User is the System Administrator */
  16128.     or (
  16129.         o1.uid = user_id()     /* User created the object */
  16130.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16131.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16132.             from sysprotects p
  16133.             where p.id = o1.id
  16134.             /* get rows for public,current user,user's group */
  16135.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16136.             /* check for SELECT,EXECUTE privilege */
  16137.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16138.             ) = 1    /* final magic...compare Grants      */
  16139.         )
  16140.     and (
  16141.         o2.uid = user_id()     /* User created the object */
  16142.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16143.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16144.             from sysprotects p
  16145.             where p.id = o2.id
  16146.             /* get rows for public,current user,user's group */
  16147.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16148.             /* check for SELECT,EXECUTE privilege */
  16149.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16150.             ) = 1    /* final magic...compare Grants      */
  16151.         )
  16152.     )    
  16153.     union all
  16154.     select
  16155.         db_name(r.rkeydbid),
  16156.         user_name(o1.uid),
  16157.         o1.name,
  16158.         c1.name,
  16159.         convert(binary(16),null),
  16160.         convert(int,null),
  16161.         db_name(r.fkeydbid),
  16162.         user_name(o2.uid),
  16163.         o2.name,
  16164.         c2.name,
  16165.         convert(binary(16),null),
  16166.         convert(int,null),
  16167.         convert(int,6),
  16168.         'NO ACTION',
  16169.         'NO ACTION',
  16170.         i.name,
  16171.         object_name(r.constid),
  16172.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16173.     from
  16174.         sysobjects o1, sysobjects o2,
  16175.         syscolumns c1, syscolumns c2,
  16176.         sysreferences r, sysindexes i, sysusers u
  16177.     where    
  16178.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16179.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16180.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16181.     and (@pk_table_name is null or @pk_table_name = o1.name)
  16182.     and (@fk_table_name is null or @fk_table_name = o2.name)
  16183.     and    o1.id = r.rkeyid
  16184.     and    r.keycnt >= 6
  16185.     and    o1.id = c1.id
  16186.     and    c1.colid = r.rkey6
  16187.     and r.fkeyid = o2.id
  16188.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16189.     and    o2.id = c2.id    
  16190.     and    c2.colid = r.fkey6
  16191.     and i.id = r.rkeyid
  16192.     and i.indid = r.rkeyindid
  16193.     and u.uid = user_id()
  16194.     and ( suser_id() = 1     /* User is the System Administrator */
  16195.     or (
  16196.         o1.uid = user_id()     /* User created the object */
  16197.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16198.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16199.             from sysprotects p
  16200.             where p.id = o1.id
  16201.             /* get rows for public,current user,user's group */
  16202.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16203.             /* check for SELECT,EXECUTE privilege */
  16204.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16205.             ) = 1    /* final magic...compare Grants      */
  16206.         )
  16207.     and (
  16208.         o2.uid = user_id()     /* User created the object */
  16209.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16210.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16211.             from sysprotects p
  16212.             where p.id = o2.id
  16213.             /* get rows for public,current user,user's group */
  16214.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16215.             /* check for SELECT,EXECUTE privilege */
  16216.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16217.             ) = 1    /* final magic...compare Grants      */
  16218.         )
  16219.     )    
  16220.     union all
  16221.     select
  16222.         db_name(r.rkeydbid),
  16223.         user_name(o1.uid),
  16224.         o1.name,
  16225.         c1.name,
  16226.         convert(binary(16),null),
  16227.         convert(int,null),
  16228.         db_name(r.fkeydbid),
  16229.         user_name(o2.uid),
  16230.         o2.name,
  16231.         c2.name,
  16232.         convert(binary(16),null),
  16233.         convert(int,null),
  16234.         convert(int,7),
  16235.         'NO ACTION',
  16236.         'NO ACTION',
  16237.         i.name,
  16238.         object_name(r.constid),
  16239.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16240.     from
  16241.         sysobjects o1, sysobjects o2,
  16242.         syscolumns c1, syscolumns c2,
  16243.         sysreferences r, sysindexes i, sysusers u
  16244.     where    
  16245.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16246.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16247.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16248.     and (@pk_table_name is null or @pk_table_name = o1.name)
  16249.     and (@fk_table_name is null or @fk_table_name = o2.name)
  16250.     and    o1.id = r.rkeyid
  16251.     and    r.keycnt >= 7
  16252.     and    o1.id = c1.id
  16253.     and    c1.colid = r.rkey7
  16254.     and r.fkeyid = o2.id
  16255.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16256.     and    o2.id = c2.id    
  16257.     and    c2.colid = r.fkey7
  16258.     and i.id = r.rkeyid
  16259.     and i.indid = r.rkeyindid
  16260.     and u.uid = user_id()
  16261.     and ( suser_id() = 1     /* User is the System Administrator */
  16262.     or (
  16263.         o1.uid = user_id()     /* User created the object */
  16264.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16265.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16266.             from sysprotects p
  16267.             where p.id = o1.id
  16268.             /* get rows for public,current user,user's group */
  16269.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16270.             /* check for SELECT,EXECUTE privilege */
  16271.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16272.             ) = 1    /* final magic...compare Grants      */
  16273.         )
  16274.     and (
  16275.         o2.uid = user_id()     /* User created the object */
  16276.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16277.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16278.             from sysprotects p
  16279.             where p.id = o2.id
  16280.             /* get rows for public,current user,user's group */
  16281.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16282.             /* check for SELECT,EXECUTE privilege */
  16283.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16284.             ) = 1    /* final magic...compare Grants      */
  16285.         )
  16286.     )    
  16287.     union all
  16288.     select
  16289.         db_name(r.rkeydbid),
  16290.         user_name(o1.uid),
  16291.         o1.name,
  16292.         c1.name,
  16293.         convert(binary(16),null),
  16294.         convert(int,null),
  16295.         db_name(r.fkeydbid),
  16296.         user_name(o2.uid),
  16297.         o2.name,
  16298.         c2.name,
  16299.         convert(binary(16),null),
  16300.         convert(int,null),
  16301.         convert(int,8),
  16302.         'NO ACTION',
  16303.         'NO ACTION',
  16304.         i.name,
  16305.         object_name(r.constid),
  16306.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16307.     from
  16308.         sysobjects o1, sysobjects o2,
  16309.         syscolumns c1, syscolumns c2,
  16310.         sysreferences r, sysindexes i, sysusers u
  16311.     where    
  16312.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16313.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16314.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16315.     and (@pk_table_name is null or @pk_table_name = o1.name)
  16316.     and (@fk_table_name is null or @fk_table_name = o2.name)
  16317.     and    o1.id = r.rkeyid
  16318.     and    r.keycnt >= 8
  16319.     and    o1.id = c1.id
  16320.     and    c1.colid = r.rkey8
  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.fkey8
  16325.     and i.id = r.rkeyid
  16326.     and i.indid = r.rkeyindid
  16327.     and u.uid = user_id()
  16328.     and ( suser_id() = 1     /* User is the System Administrator */
  16329.     or (
  16330.         o1.uid = user_id()     /* User created the object */
  16331.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16332.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16333.             from sysprotects p
  16334.             where p.id = o1.id
  16335.             /* get rows for public,current user,user's group */
  16336.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16337.             /* check for SELECT,EXECUTE privilege */
  16338.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16339.             ) = 1    /* final magic...compare Grants      */
  16340.         )
  16341.     and (
  16342.         o2.uid = user_id()     /* User created the object */
  16343.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16344.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16345.             from sysprotects p
  16346.             where p.id = o2.id
  16347.             /* get rows for public,current user,user's group */
  16348.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16349.             /* check for SELECT,EXECUTE privilege */
  16350.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16351.             ) = 1    /* final magic...compare Grants      */
  16352.         )
  16353.     )    
  16354.     union all
  16355.     select
  16356.         db_name(r.rkeydbid),
  16357.         user_name(o1.uid),
  16358.         o1.name,
  16359.         c1.name,
  16360.         convert(binary(16),null),
  16361.         convert(int,null),
  16362.         db_name(r.fkeydbid),
  16363.         user_name(o2.uid),
  16364.         o2.name,
  16365.         c2.name,
  16366.         convert(binary(16),null),
  16367.         convert(int,null),
  16368.         convert(int,9),
  16369.         'NO ACTION',
  16370.         'NO ACTION',
  16371.         i.name,
  16372.         object_name(r.constid),
  16373.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16374.     from
  16375.         sysobjects o1, sysobjects o2,
  16376.         syscolumns c1, syscolumns c2,
  16377.         sysreferences r, sysindexes i, sysusers u
  16378.     where    
  16379.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16380.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16381.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16382.     and (@pk_table_name is null or @pk_table_name = o1.name)
  16383.     and (@fk_table_name is null or @fk_table_name = o2.name)
  16384.     and    o1.id = r.rkeyid
  16385.     and    r.keycnt >= 9
  16386.     and    o1.id = c1.id
  16387.     and    c1.colid = r.rkey9
  16388.     and r.fkeyid = o2.id
  16389.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16390.     and    o2.id = c2.id    
  16391.     and    c2.colid = r.fkey9
  16392.     and i.id = r.rkeyid
  16393.     and i.indid = r.rkeyindid
  16394.     and u.uid = user_id()
  16395.     and ( suser_id() = 1     /* User is the System Administrator */
  16396.     or (
  16397.         o1.uid = user_id()     /* User created the object */
  16398.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16399.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16400.             from sysprotects p
  16401.             where p.id = o1.id
  16402.             /* get rows for public,current user,user's group */
  16403.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16404.             /* check for SELECT,EXECUTE privilege */
  16405.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16406.             ) = 1    /* final magic...compare Grants      */
  16407.         )
  16408.     and (
  16409.         o2.uid = user_id()     /* User created the object */
  16410.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16411.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16412.             from sysprotects p
  16413.             where p.id = o2.id
  16414.             /* get rows for public,current user,user's group */
  16415.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16416.             /* check for SELECT,EXECUTE privilege */
  16417.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16418.             ) = 1    /* final magic...compare Grants      */
  16419.         )
  16420.     )    
  16421.     union all
  16422.     select
  16423.         db_name(r.rkeydbid),
  16424.         user_name(o1.uid),
  16425.         o1.name,
  16426.         c1.name,
  16427.         convert(binary(16),null),
  16428.         convert(int,null),
  16429.         db_name(r.fkeydbid),
  16430.         user_name(o2.uid),
  16431.         o2.name,
  16432.         c2.name,
  16433.         convert(binary(16),null),
  16434.         convert(int,null),
  16435.         convert(int,10),
  16436.         'NO ACTION',
  16437.         'NO ACTION',
  16438.         i.name,
  16439.         object_name(r.constid),
  16440.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16441.     from
  16442.         sysobjects o1, sysobjects o2,
  16443.         syscolumns c1, syscolumns c2,
  16444.         sysreferences r, sysindexes i, sysusers u
  16445.     where    
  16446.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16447.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16448.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16449.     and (@pk_table_name is null or @pk_table_name = o1.name)
  16450.     and (@fk_table_name is null or @fk_table_name = o2.name)
  16451.     and    o1.id = r.rkeyid
  16452.     and    r.keycnt >= 10
  16453.     and    o1.id = c1.id
  16454.     and    c1.colid = r.rkey10
  16455.     and r.fkeyid = o2.id
  16456.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16457.     and    o2.id = c2.id    
  16458.     and    c2.colid = r.fkey10
  16459.     and i.id = r.rkeyid
  16460.     and i.indid = r.rkeyindid
  16461.     and u.uid = user_id()
  16462.     and ( suser_id() = 1     /* User is the System Administrator */
  16463.     or (
  16464.         o1.uid = user_id()     /* User created the object */
  16465.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16466.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16467.             from sysprotects p
  16468.             where p.id = o1.id
  16469.             /* get rows for public,current user,user's group */
  16470.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16471.             /* check for SELECT,EXECUTE privilege */
  16472.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16473.             ) = 1    /* final magic...compare Grants      */
  16474.         )
  16475.     and (
  16476.         o2.uid = user_id()     /* User created the object */
  16477.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16478.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16479.             from sysprotects p
  16480.             where p.id = o2.id
  16481.             /* get rows for public,current user,user's group */
  16482.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16483.             /* check for SELECT,EXECUTE privilege */
  16484.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16485.             ) = 1    /* final magic...compare Grants      */
  16486.         )
  16487.     )    
  16488.     union all
  16489.     select
  16490.         db_name(r.rkeydbid),
  16491.         user_name(o1.uid),
  16492.         o1.name,
  16493.         c1.name,
  16494.         convert(binary(16),null),
  16495.         convert(int,null),
  16496.         db_name(r.fkeydbid),
  16497.         user_name(o2.uid),
  16498.         o2.name,
  16499.         c2.name,
  16500.         convert(binary(16),null),
  16501.         convert(int,null),
  16502.         convert(int,11),
  16503.         'NO ACTION',
  16504.         'NO ACTION',
  16505.         i.name,
  16506.         object_name(r.constid),
  16507.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16508.     from
  16509.         sysobjects o1, sysobjects o2,
  16510.         syscolumns c1, syscolumns c2,
  16511.         sysreferences r, sysindexes i, sysusers u
  16512.     where    
  16513.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16514.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16515.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16516.     and (@pk_table_name is null or @pk_table_name = o1.name)
  16517.     and (@fk_table_name is null or @fk_table_name = o2.name)
  16518.     and    o1.id = r.rkeyid
  16519.     and    r.keycnt >= 11
  16520.     and    o1.id = c1.id
  16521.     and    c1.colid = r.rkey11
  16522.     and r.fkeyid = o2.id
  16523.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16524.     and    o2.id = c2.id    
  16525.     and    c2.colid = r.fkey11
  16526.     and i.id = r.rkeyid
  16527.     and i.indid = r.rkeyindid
  16528.     and u.uid = user_id()
  16529.     and ( suser_id() = 1     /* User is the System Administrator */
  16530.     or (
  16531.         o1.uid = user_id()     /* User created the object */
  16532.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16533.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16534.             from sysprotects p
  16535.             where p.id = o1.id
  16536.             /* get rows for public,current user,user's group */
  16537.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16538.             /* check for SELECT,EXECUTE privilege */
  16539.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16540.             ) = 1    /* final magic...compare Grants      */
  16541.         )
  16542.     and (
  16543.         o2.uid = user_id()     /* User created the object */
  16544.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16545.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16546.             from sysprotects p
  16547.             where p.id = o2.id
  16548.             /* get rows for public,current user,user's group */
  16549.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16550.             /* check for SELECT,EXECUTE privilege */
  16551.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16552.             ) = 1    /* final magic...compare Grants      */
  16553.         )
  16554.     )    
  16555.     union all
  16556.     select
  16557.         db_name(r.rkeydbid),
  16558.         user_name(o1.uid),
  16559.         o1.name,
  16560.         c1.name,
  16561.         convert(binary(16),null),
  16562.         convert(int,null),
  16563.         db_name(r.fkeydbid),
  16564.         user_name(o2.uid),
  16565.         o2.name,
  16566.         c2.name,
  16567.         convert(binary(16),null),
  16568.         convert(int,null),
  16569.         convert(int,12),
  16570.         'NO ACTION',
  16571.         'NO ACTION',
  16572.         i.name,
  16573.         object_name(r.constid),
  16574.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16575.     from
  16576.         sysobjects o1, sysobjects o2,
  16577.         syscolumns c1, syscolumns c2,
  16578.         sysreferences r, sysindexes i, sysusers u
  16579.     where    
  16580.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16581.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16582.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16583.     and (@pk_table_name is null or @pk_table_name = o1.name)
  16584.     and (@fk_table_name is null or @fk_table_name = o2.name)
  16585.     and    o1.id = r.rkeyid
  16586.     and    r.keycnt >= 12
  16587.     and    o1.id = c1.id
  16588.     and    c1.colid = r.rkey12
  16589.     and r.fkeyid = o2.id
  16590.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16591.     and    o2.id = c2.id    
  16592.     and    c2.colid = r.fkey12
  16593.     and i.id = r.rkeyid
  16594.     and i.indid = r.rkeyindid
  16595.     and u.uid = user_id()
  16596.     and ( suser_id() = 1     /* User is the System Administrator */
  16597.     or (
  16598.         o1.uid = user_id()     /* User created the object */
  16599.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16600.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16601.             from sysprotects p
  16602.             where p.id = o1.id
  16603.             /* get rows for public,current user,user's group */
  16604.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16605.             /* check for SELECT,EXECUTE privilege */
  16606.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16607.             ) = 1    /* final magic...compare Grants      */
  16608.         )
  16609.     and (
  16610.         o2.uid = user_id()     /* User created the object */
  16611.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16612.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16613.             from sysprotects p
  16614.             where p.id = o2.id
  16615.             /* get rows for public,current user,user's group */
  16616.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16617.             /* check for SELECT,EXECUTE privilege */
  16618.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16619.             ) = 1    /* final magic...compare Grants      */
  16620.         )
  16621.     )    
  16622.     union all
  16623.     select
  16624.         db_name(r.rkeydbid),
  16625.         user_name(o1.uid),
  16626.         o1.name,
  16627.         c1.name,
  16628.         convert(binary(16),null),
  16629.         convert(int,null),
  16630.         db_name(r.fkeydbid),
  16631.         user_name(o2.uid),
  16632.         o2.name,
  16633.         c2.name,
  16634.         convert(binary(16),null),
  16635.         convert(int,null),
  16636.         convert(int,13),
  16637.         'NO ACTION',
  16638.         'NO ACTION',
  16639.         i.name,
  16640.         object_name(r.constid),
  16641.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16642.     from
  16643.         sysobjects o1, sysobjects o2,
  16644.         syscolumns c1, syscolumns c2,
  16645.         sysreferences r, sysindexes i, sysusers u
  16646.     where    
  16647.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16648.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16649.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16650.     and (@pk_table_name is null or @pk_table_name = o1.name)
  16651.     and (@fk_table_name is null or @fk_table_name = o2.name)
  16652.     and    o1.id = r.rkeyid
  16653.     and    r.keycnt >= 13
  16654.     and    o1.id = c1.id
  16655.     and    c1.colid = r.rkey13
  16656.     and r.fkeyid = o2.id
  16657.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16658.     and    o2.id = c2.id    
  16659.     and    c2.colid = r.fkey13
  16660.     and i.id = r.rkeyid
  16661.     and i.indid = r.rkeyindid
  16662.     and u.uid = user_id()
  16663.     and ( suser_id() = 1     /* User is the System Administrator */
  16664.     or (
  16665.         o1.uid = user_id()     /* User created the object */
  16666.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16667.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16668.             from sysprotects p
  16669.             where p.id = o1.id
  16670.             /* get rows for public,current user,user's group */
  16671.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16672.             /* check for SELECT,EXECUTE privilege */
  16673.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16674.             ) = 1    /* final magic...compare Grants      */
  16675.         )
  16676.     and (
  16677.         o2.uid = user_id()     /* User created the object */
  16678.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16679.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16680.             from sysprotects p
  16681.             where p.id = o2.id
  16682.             /* get rows for public,current user,user's group */
  16683.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16684.             /* check for SELECT,EXECUTE privilege */
  16685.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16686.             ) = 1    /* final magic...compare Grants      */
  16687.         )
  16688.     )    
  16689.     union all
  16690.     select
  16691.         db_name(r.rkeydbid),
  16692.         user_name(o1.uid),
  16693.         o1.name,
  16694.         c1.name,
  16695.         convert(binary(16),null),
  16696.         convert(int,null),
  16697.         db_name(r.fkeydbid),
  16698.         user_name(o2.uid),
  16699.         o2.name,
  16700.         c2.name,
  16701.         convert(binary(16),null),
  16702.         convert(int,null),
  16703.         convert(int,14),
  16704.         'NO ACTION',
  16705.         'NO ACTION',
  16706.         i.name,
  16707.         object_name(r.constid),
  16708.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16709.     from
  16710.         sysobjects o1, sysobjects o2,
  16711.         syscolumns c1, syscolumns c2,
  16712.         sysreferences r, sysindexes i, sysusers u
  16713.     where    
  16714.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16715.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16716.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16717.     and (@pk_table_name is null or @pk_table_name = o1.name)
  16718.     and (@fk_table_name is null or @fk_table_name = o2.name)
  16719.     and    o1.id = r.rkeyid
  16720.     and    r.keycnt >= 14
  16721.     and    o1.id = c1.id
  16722.     and    c1.colid = r.rkey14
  16723.     and r.fkeyid = o2.id
  16724.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16725.     and    o2.id = c2.id    
  16726.     and    c2.colid = r.fkey14
  16727.     and i.id = r.rkeyid
  16728.     and i.indid = r.rkeyindid
  16729.     and u.uid = user_id()
  16730.     and ( suser_id() = 1     /* User is the System Administrator */
  16731.     or (
  16732.         o1.uid = user_id()     /* User created the object */
  16733.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16734.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16735.             from sysprotects p
  16736.             where p.id = o1.id
  16737.             /* get rows for public,current user,user's group */
  16738.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16739.             /* check for SELECT,EXECUTE privilege */
  16740.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16741.             ) = 1    /* final magic...compare Grants      */
  16742.         )
  16743.     and (
  16744.         o2.uid = user_id()     /* User created the object */
  16745.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16746.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16747.             from sysprotects p
  16748.             where p.id = o2.id
  16749.             /* get rows for public,current user,user's group */
  16750.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16751.             /* check for SELECT,EXECUTE privilege */
  16752.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16753.             ) = 1    /* final magic...compare Grants      */
  16754.         )
  16755.     )    
  16756.     union all
  16757.     select
  16758.         db_name(r.rkeydbid),
  16759.         user_name(o1.uid),
  16760.         o1.name,
  16761.         c1.name,
  16762.         convert(binary(16),null),
  16763.         convert(int,null),
  16764.         db_name(r.fkeydbid),
  16765.         user_name(o2.uid),
  16766.         o2.name,
  16767.         c2.name,
  16768.         convert(binary(16),null),
  16769.         convert(int,null),
  16770.         convert(int,15),
  16771.         'NO ACTION',
  16772.         'NO ACTION',
  16773.         i.name,
  16774.         object_name(r.constid),
  16775.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16776.     from
  16777.         sysobjects o1, sysobjects o2,
  16778.         syscolumns c1, syscolumns c2,
  16779.         sysreferences r, sysindexes i, sysusers u
  16780.     where    
  16781.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16782.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16783.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16784.     and (@pk_table_name is null or @pk_table_name = o1.name)
  16785.     and (@fk_table_name is null or @fk_table_name = o2.name)
  16786.     and    o1.id = r.rkeyid
  16787.     and    r.keycnt >= 15
  16788.     and    o1.id = c1.id
  16789.     and    c1.colid = r.rkey15
  16790.     and r.fkeyid = o2.id
  16791.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16792.     and    o2.id = c2.id    
  16793.     and    c2.colid = r.fkey15
  16794.     and i.id = r.rkeyid
  16795.     and i.indid = r.rkeyindid
  16796.     and u.uid = user_id()
  16797.     and ( suser_id() = 1     /* User is the System Administrator */
  16798.     or (
  16799.         o1.uid = user_id()     /* User created the object */
  16800.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16801.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16802.             from sysprotects p
  16803.             where p.id = o1.id
  16804.             /* get rows for public,current user,user's group */
  16805.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16806.             /* check for SELECT,EXECUTE privilege */
  16807.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16808.             ) = 1    /* final magic...compare Grants      */
  16809.         )
  16810.     and (
  16811.         o2.uid = user_id()     /* User created the object */
  16812.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16813.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16814.             from sysprotects p
  16815.             where p.id = o2.id
  16816.             /* get rows for public,current user,user's group */
  16817.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16818.             /* check for SELECT,EXECUTE privilege */
  16819.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16820.             ) = 1    /* final magic...compare Grants      */
  16821.         )
  16822.     )    
  16823.     union all
  16824.     select
  16825.         db_name(r.rkeydbid),
  16826.         user_name(o1.uid),
  16827.         o1.name,
  16828.         c1.name,
  16829.         convert(binary(16),null),
  16830.         convert(int,null),
  16831.         db_name(r.fkeydbid),
  16832.         user_name(o2.uid),
  16833.         o2.name,
  16834.         c2.name,
  16835.         convert(binary(16),null),
  16836.         convert(int,null),
  16837.         convert(int,16),
  16838.         'NO ACTION',
  16839.         'NO ACTION',
  16840.         i.name,
  16841.         object_name(r.constid),
  16842.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16843.     from
  16844.         sysobjects o1, sysobjects o2,
  16845.         syscolumns c1, syscolumns c2,
  16846.         sysreferences r, sysindexes i, sysusers u
  16847.     where    
  16848.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16849.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16850.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16851.     and (@pk_table_name is null or @pk_table_name = o1.name)
  16852.     and (@fk_table_name is null or @fk_table_name = o2.name)
  16853.     and    o1.id = r.rkeyid
  16854.     and    o1.id = c1.id
  16855.     and    r.keycnt >= 16
  16856.     and    c1.colid = r.rkey16
  16857.     and r.fkeyid = o2.id
  16858.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16859.     and    o2.id = c2.id    
  16860.     and    c2.colid = r.fkey16
  16861.     and i.id = r.rkeyid
  16862.     and i.indid = r.rkeyindid
  16863.     and u.uid = user_id()
  16864.     and ( suser_id() = 1     /* User is the System Administrator */
  16865.     or (
  16866.         o1.uid = user_id()     /* User created the object */
  16867.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16868.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16869.             from sysprotects p
  16870.             where p.id = o1.id
  16871.             /* get rows for public,current user,user's group */
  16872.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16873.             /* check for SELECT,EXECUTE privilege */
  16874.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16875.             ) = 1    /* final magic...compare Grants      */
  16876.         )
  16877.     and (
  16878.         o2.uid = user_id()     /* User created the object */
  16879.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16880.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16881.             from sysprotects p
  16882.             where p.id = o2.id
  16883.             /* get rows for public,current user,user's group */
  16884.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16885.             /* check for SELECT,EXECUTE privilege */
  16886.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16887.             ) = 1    /* final magic...compare Grants      */
  16888.         )
  16889.     )
  16890.     order by 8,9,2,3,13
  16891.     END
  16892.  
  16893.     exec @ret = sp_cursoropen @handle output, 'select * from #spfkeysrowset1',
  16894.         @scrollopt output, @ccopt output, @rows output
  16895.  
  16896.     drop table #spfkeysrowset1
  16897.        return isnull(@ret,0)
  16898. go
  16899. dump tran master with no_log
  16900. go
  16901. CREATE PROCEDURE sp_foreign_keys_rowset;3
  16902. as
  16903.     select
  16904.         PK_TABLE_CATALOG    = convert(sysname,' '),
  16905.         PK_TABLE_SCHEMA        = convert(sysname,' '),
  16906.         PK_TABLE_NAME         = convert(sysname,' '),
  16907.         PK_COLUMN_NAME         = convert(sysname,' '),
  16908.         PK_COLUMN_GUID        = convert(binary(16),null),
  16909.         PK_COLUMN_PROPID    = convert(int,null),
  16910.         FK_TABLE_CATALOG    = convert(sysname,' '),
  16911.         FK_TABLE_SCHEMA        = convert(sysname,' '),
  16912.         FK_TABLE_NAME         = convert(sysname,' '),
  16913.         FK_COLUMN_NAME         = convert(sysname,' '),
  16914.         FK_COLUMN_GUID        = convert(binary(16),null),
  16915.         FK_COLUMN_PROPID    = convert(int,null),
  16916.         ORDINAL                = convert(int,1),
  16917.         UPDATE_RULE            = 'NO ACTION',
  16918.         DELETE_RULE         = 'NO ACTION',
  16919.         PK_NAME                = convert(sysname, ' '),
  16920.         FK_NAME                = convert(sysname, ' '),
  16921.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16922.     where    1=0
  16923. go
  16924. dump tran master with no_log
  16925. go
  16926.  
  16927.  
  16928. if (charindex('7.00', @@version) = 0 and
  16929.     charindex('8.00', @@version) = 0)
  16930. begin
  16931.     print ''
  16932.     print ''
  16933.     print 'Warning:'
  16934.     print 'you are installing the stored procedures '
  16935.     print 'on a pre 8.0 SQL Server.'
  16936.     print 'Ignore the following errors.'
  16937. end
  16938. else
  16939.     drop proc sp_foreign_keys_rowset
  16940. go
  16941.  
  16942.  
  16943. /*    Procedure for 8.0 server */
  16944. CREATE PROCEDURE sp_foreign_keys_rowset
  16945.     (
  16946.     @pk_table_name        sysname,
  16947.     @pk_table_schema    sysname = null,
  16948.     @fk_table_name        sysname = null,
  16949.     @fk_table_schema    sysname = null,
  16950.     @fk_table_catalog    sysname = null
  16951.     )
  16952. as
  16953.     select
  16954.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16955.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16956.         PK_TABLE_NAME         = o1.name,
  16957.         PK_COLUMN_NAME         = c1.name,
  16958.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16959.         PK_COLUMN_PROPID    = convert(int,null),
  16960.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  16961.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  16962.         FK_TABLE_NAME         = o2.name,
  16963.         FK_COLUMN_NAME         = c2.name,
  16964.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16965.         FK_COLUMN_PROPID    = convert(int,null),
  16966.         ORDINAL                = convert(int,1),
  16967.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  16968.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  16969.         PK_NAME                = i.name,
  16970.         FK_NAME                = object_name(r.constid),
  16971.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16972.     from
  16973.         sysobjects o1, sysobjects o2,
  16974.         syscolumns c1, syscolumns c2,
  16975.         sysreferences r, sysindexes i
  16976.     where    
  16977.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16978.     and    o1.name = @pk_table_name
  16979.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16980.     and    o1.id = r.rkeyid
  16981.     and    o1.id = c1.id
  16982.     and    c1.colid = r.rkey1
  16983.     and r.fkeyid = o2.id
  16984.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  16985.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16986.     and    o2.id = c2.id    
  16987.     and    c2.colid = r.fkey1
  16988.     and permissions(o1.id) <> 0
  16989.     and permissions(o2.id) <> 0
  16990.     and i.id = r.rkeyid
  16991.     and i.indid = r.rkeyindid
  16992.     union all
  16993.     select
  16994.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  16995.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  16996.         PK_TABLE_NAME         = o1.name,
  16997.         PK_COLUMN_NAME         = c1.name,
  16998.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  16999.         PK_COLUMN_PROPID    = convert(int,null),
  17000.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17001.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  17002.         FK_TABLE_NAME         = o2.name,
  17003.         FK_COLUMN_NAME         = c2.name,
  17004.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17005.         FK_COLUMN_PROPID    = convert(int,null),
  17006.         ORDINAL                = convert(int,2),
  17007.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17008.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17009.         PK_NAME                = i.name,
  17010.         FK_NAME                = object_name(r.constid),
  17011.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17012.     from
  17013.         sysobjects o1, sysobjects o2,
  17014.         syscolumns c1, syscolumns c2,
  17015.         sysreferences r, sysindexes i
  17016.     where    
  17017.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17018.     and    o1.name = @pk_table_name
  17019.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17020.     and    o1.id = r.rkeyid
  17021.     and    o1.id = c1.id
  17022.     and    c1.colid = r.rkey2
  17023.     and    r.keycnt >= 2
  17024.     and r.fkeyid = o2.id
  17025.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  17026.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17027.     and    o2.id = c2.id    
  17028.     and    c2.colid = r.fkey2
  17029.     and permissions(o1.id) <> 0
  17030.     and permissions(o2.id) <> 0
  17031.     and i.id = r.rkeyid
  17032.     and i.indid = r.rkeyindid
  17033.     union all
  17034.     select
  17035.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17036.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17037.         PK_TABLE_NAME         = o1.name,
  17038.         PK_COLUMN_NAME         = c1.name,
  17039.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17040.         PK_COLUMN_PROPID    = convert(int,null),
  17041.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17042.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  17043.         FK_TABLE_NAME         = o2.name,
  17044.         FK_COLUMN_NAME         = c2.name,
  17045.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17046.         FK_COLUMN_PROPID    = convert(int,null),
  17047.         ORDINAL                = convert(int,3),
  17048.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17049.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17050.         PK_NAME                = i.name,
  17051.         FK_NAME                = object_name(r.constid),
  17052.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17053.     from
  17054.         sysobjects o1, sysobjects o2,
  17055.         syscolumns c1, syscolumns c2,
  17056.         sysreferences r, sysindexes i
  17057.     where    
  17058.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17059.     and    o1.name = @pk_table_name
  17060.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17061.     and    o1.id = r.rkeyid
  17062.     and    o1.id = c1.id
  17063.     and    c1.colid = r.rkey3
  17064.     and    r.keycnt >= 3
  17065.     and r.fkeyid = o2.id
  17066.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  17067.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17068.     and    o2.id = c2.id    
  17069.     and    c2.colid = r.fkey3
  17070.     and permissions(o1.id) <> 0
  17071.     and permissions(o2.id) <> 0    
  17072.     and i.id = r.rkeyid
  17073.     and i.indid = r.rkeyindid
  17074.     union all
  17075.     select
  17076.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17077.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17078.         PK_TABLE_NAME         = o1.name,
  17079.         PK_COLUMN_NAME         = c1.name,
  17080.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17081.         PK_COLUMN_PROPID    = convert(int,null),
  17082.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17083.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  17084.         FK_TABLE_NAME         = o2.name,
  17085.         FK_COLUMN_NAME         = c2.name,
  17086.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17087.         FK_COLUMN_PROPID    = convert(int,null),
  17088.         ORDINAL                = convert(int,4),
  17089.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17090.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17091.         PK_NAME                = i.name,
  17092.         FK_NAME                = object_name(r.constid),
  17093.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17094.     from
  17095.         sysobjects o1, sysobjects o2,
  17096.         syscolumns c1, syscolumns c2,
  17097.         sysreferences r, sysindexes i
  17098.     where    
  17099.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17100.     and    o1.name = @pk_table_name
  17101.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17102.     and    o1.id = r.rkeyid
  17103.     and    r.keycnt >= 4
  17104.     and    o1.id = c1.id
  17105.     and    c1.colid = r.rkey4
  17106.     and r.fkeyid = o2.id
  17107.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  17108.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17109.     and    o2.id = c2.id    
  17110.     and    c2.colid = r.fkey4
  17111.     and permissions(o1.id) <> 0
  17112.     and permissions(o2.id) <> 0
  17113.     and i.id = r.rkeyid
  17114.     and i.indid = r.rkeyindid    
  17115.     union all
  17116.     select
  17117.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17118.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17119.         PK_TABLE_NAME         = o1.name,
  17120.         PK_COLUMN_NAME         = c1.name,
  17121.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17122.         PK_COLUMN_PROPID    = convert(int,null),
  17123.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17124.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  17125.         FK_TABLE_NAME         = o2.name,
  17126.         FK_COLUMN_NAME         = c2.name,
  17127.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17128.         FK_COLUMN_PROPID    = convert(int,null),
  17129.         ORDINAL                = convert(int,5),
  17130.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17131.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17132.         PK_NAME                = i.name,
  17133.         FK_NAME                = object_name(r.constid),
  17134.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17135.     from
  17136.         sysobjects o1, sysobjects o2,
  17137.         syscolumns c1, syscolumns c2,
  17138.         sysreferences r, sysindexes i
  17139.     where    
  17140.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17141.     and    o1.name = @pk_table_name
  17142.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17143.     and    o1.id = r.rkeyid
  17144.     and    r.keycnt >= 5
  17145.     and    o1.id = c1.id
  17146.     and    c1.colid = r.rkey5
  17147.     and r.fkeyid = o2.id
  17148.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  17149.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17150.     and    o2.id = c2.id    
  17151.     and    c2.colid = r.fkey5
  17152.     and permissions(o1.id) <> 0
  17153.     and permissions(o2.id) <> 0
  17154.     and i.id = r.rkeyid
  17155.     and i.indid = r.rkeyindid        
  17156.     union all
  17157.     select
  17158.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17159.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17160.         PK_TABLE_NAME         = o1.name,
  17161.         PK_COLUMN_NAME         = c1.name,
  17162.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17163.         PK_COLUMN_PROPID    = convert(int,null),
  17164.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17165.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  17166.         FK_TABLE_NAME         = o2.name,
  17167.         FK_COLUMN_NAME         = c2.name,
  17168.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17169.         FK_COLUMN_PROPID    = convert(int,null),
  17170.         ORDINAL                = convert(int,6),
  17171.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17172.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17173.         PK_NAME                = i.name,
  17174.         FK_NAME                = object_name(r.constid),
  17175.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17176.     from
  17177.         sysobjects o1, sysobjects o2,
  17178.         syscolumns c1, syscolumns c2,
  17179.         sysreferences r, sysindexes i
  17180.     where    
  17181.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17182.     and    o1.name = @pk_table_name
  17183.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17184.     and    o1.id = r.rkeyid
  17185.     and    r.keycnt >= 6
  17186.     and    o1.id = c1.id
  17187.     and    c1.colid = r.rkey6
  17188.     and r.fkeyid = o2.id
  17189.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  17190.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17191.     and    o2.id = c2.id    
  17192.     and    c2.colid = r.fkey6
  17193.     and permissions(o1.id) <> 0
  17194.     and permissions(o2.id) <> 0
  17195.     and i.id = r.rkeyid
  17196.     and i.indid = r.rkeyindid            
  17197.     union all
  17198.     select
  17199.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17200.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17201.         PK_TABLE_NAME         = o1.name,
  17202.         PK_COLUMN_NAME         = c1.name,
  17203.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17204.         PK_COLUMN_PROPID    = convert(int,null),
  17205.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17206.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  17207.         FK_TABLE_NAME         = o2.name,
  17208.         FK_COLUMN_NAME         = c2.name,
  17209.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17210.         FK_COLUMN_PROPID    = convert(int,null),
  17211.         ORDINAL                = convert(int,7),
  17212.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17213.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17214.         PK_NAME                = i.name,
  17215.         FK_NAME                = object_name(r.constid),
  17216.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17217.     from
  17218.         sysobjects o1, sysobjects o2,
  17219.         syscolumns c1, syscolumns c2,
  17220.         sysreferences r, sysindexes i
  17221.     where    
  17222.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17223.     and    o1.name = @pk_table_name
  17224.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17225.     and    o1.id = r.rkeyid
  17226.     and    r.keycnt >= 7
  17227.     and    o1.id = c1.id
  17228.     and    c1.colid = r.rkey7
  17229.     and r.fkeyid = o2.id
  17230.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  17231.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17232.     and    o2.id = c2.id    
  17233.     and    c2.colid = r.fkey7
  17234.     and permissions(o1.id) <> 0
  17235.     and permissions(o2.id) <> 0
  17236.     and i.id = r.rkeyid
  17237.     and i.indid = r.rkeyindid            
  17238.     union all
  17239.     select
  17240.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17241.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17242.         PK_TABLE_NAME         = o1.name,
  17243.         PK_COLUMN_NAME         = c1.name,
  17244.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17245.         PK_COLUMN_PROPID    = convert(int,null),
  17246.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17247.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  17248.         FK_TABLE_NAME         = o2.name,
  17249.         FK_COLUMN_NAME         = c2.name,
  17250.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17251.         FK_COLUMN_PROPID    = convert(int,null),
  17252.         ORDINAL                = convert(int,8),
  17253.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17254.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17255.         PK_NAME                = i.name,
  17256.         FK_NAME                = object_name(r.constid),
  17257.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17258.     from
  17259.         sysobjects o1, sysobjects o2,
  17260.         syscolumns c1, syscolumns c2,
  17261.         sysreferences r, sysindexes i
  17262.     where    
  17263.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17264.     and    o1.name = @pk_table_name
  17265.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17266.     and    o1.id = r.rkeyid
  17267.     and    r.keycnt >= 8
  17268.     and    o1.id = c1.id
  17269.     and    c1.colid = r.rkey8
  17270.     and r.fkeyid = o2.id
  17271.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  17272.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17273.     and    o2.id = c2.id    
  17274.     and    c2.colid = r.fkey8
  17275.     and permissions(o1.id) <> 0
  17276.     and permissions(o2.id) <> 0
  17277.     and i.id = r.rkeyid
  17278.     and i.indid = r.rkeyindid            
  17279.     union all
  17280.     select
  17281.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17282.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17283.         PK_TABLE_NAME         = o1.name,
  17284.         PK_COLUMN_NAME         = c1.name,
  17285.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17286.         PK_COLUMN_PROPID    = convert(int,null),
  17287.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17288.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  17289.         FK_TABLE_NAME         = o2.name,
  17290.         FK_COLUMN_NAME         = c2.name,
  17291.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17292.         FK_COLUMN_PROPID    = convert(int,null),
  17293.         ORDINAL                = convert(int,9),
  17294.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17295.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17296.         PK_NAME                = i.name,
  17297.         FK_NAME                = object_name(r.constid),
  17298.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17299.     from
  17300.         sysobjects o1, sysobjects o2,
  17301.         syscolumns c1, syscolumns c2,
  17302.         sysreferences r, sysindexes i
  17303.     where    
  17304.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17305.     and    o1.name = @pk_table_name
  17306.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17307.     and    o1.id = r.rkeyid
  17308.     and    r.keycnt >= 9
  17309.     and    o1.id = c1.id
  17310.     and    c1.colid = r.rkey9
  17311.     and r.fkeyid = o2.id
  17312.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  17313.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17314.     and    o2.id = c2.id    
  17315.     and    c2.colid = r.fkey9
  17316.     and permissions(o1.id) <> 0
  17317.     and permissions(o2.id) <> 0
  17318.     and i.id = r.rkeyid
  17319.     and i.indid = r.rkeyindid            
  17320.     union all
  17321.     select
  17322.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17323.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17324.         PK_TABLE_NAME         = o1.name,
  17325.         PK_COLUMN_NAME         = c1.name,
  17326.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17327.         PK_COLUMN_PROPID    = convert(int,null),
  17328.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17329.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  17330.         FK_TABLE_NAME         = o2.name,
  17331.         FK_COLUMN_NAME         = c2.name,
  17332.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17333.         FK_COLUMN_PROPID    = convert(int,null),
  17334.         ORDINAL                = convert(int,10),
  17335.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17336.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17337.         PK_NAME                = i.name,
  17338.         FK_NAME                = object_name(r.constid),
  17339.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17340.     from
  17341.         sysobjects o1, sysobjects o2,
  17342.         syscolumns c1, syscolumns c2,
  17343.         sysreferences r, sysindexes i
  17344.     where    
  17345.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17346.     and    o1.name = @pk_table_name
  17347.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17348.     and    o1.id = r.rkeyid
  17349.     and    r.keycnt >= 10
  17350.     and    o1.id = c1.id
  17351.     and    c1.colid = r.rkey10
  17352.     and r.fkeyid = o2.id
  17353.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  17354.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17355.     and    o2.id = c2.id    
  17356.     and    c2.colid = r.fkey10
  17357.     and permissions(o1.id) <> 0
  17358.     and permissions(o2.id) <> 0
  17359.     and i.id = r.rkeyid
  17360.     and i.indid = r.rkeyindid            
  17361.     union all
  17362.     select
  17363.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17364.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17365.         PK_TABLE_NAME         = o1.name,
  17366.         PK_COLUMN_NAME         = c1.name,
  17367.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17368.         PK_COLUMN_PROPID    = convert(int,null),
  17369.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17370.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  17371.         FK_TABLE_NAME         = o2.name,
  17372.         FK_COLUMN_NAME         = c2.name,
  17373.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17374.         FK_COLUMN_PROPID    = convert(int,null),
  17375.         ORDINAL                = convert(int,11),
  17376.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17377.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17378.         PK_NAME                = i.name,
  17379.         FK_NAME                = object_name(r.constid),
  17380.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17381.     from
  17382.         sysobjects o1, sysobjects o2,
  17383.         syscolumns c1, syscolumns c2,
  17384.         sysreferences r, sysindexes i
  17385.     where    
  17386.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17387.     and    o1.name = @pk_table_name
  17388.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17389.     and    o1.id = r.rkeyid
  17390.     and    r.keycnt >= 11
  17391.     and    o1.id = c1.id
  17392.     and    c1.colid = r.rkey11
  17393.     and r.fkeyid = o2.id
  17394.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  17395.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17396.     and    o2.id = c2.id    
  17397.     and    c2.colid = r.fkey11
  17398.     and permissions(o1.id) <> 0
  17399.     and permissions(o2.id) <> 0
  17400.     and i.id = r.rkeyid
  17401.     and i.indid = r.rkeyindid            
  17402.     union all
  17403.     select
  17404.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17405.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17406.         PK_TABLE_NAME         = o1.name,
  17407.         PK_COLUMN_NAME         = c1.name,
  17408.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17409.         PK_COLUMN_PROPID    = convert(int,null),
  17410.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17411.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  17412.         FK_TABLE_NAME         = o2.name,
  17413.         FK_COLUMN_NAME         = c2.name,
  17414.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17415.         FK_COLUMN_PROPID    = convert(int,null),
  17416.         ORDINAL                = convert(int,12),
  17417.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17418.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17419.         PK_NAME                = i.name,
  17420.         FK_NAME                = object_name(r.constid),
  17421.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17422.     from
  17423.         sysobjects o1, sysobjects o2,
  17424.         syscolumns c1, syscolumns c2,
  17425.         sysreferences r, sysindexes i
  17426.     where    
  17427.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17428.     and    o1.name = @pk_table_name
  17429.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17430.     and    o1.id = r.rkeyid
  17431.     and    r.keycnt >= 12
  17432.     and    o1.id = c1.id
  17433.     and    c1.colid = r.rkey12
  17434.     and r.fkeyid = o2.id
  17435.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  17436.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17437.     and    o2.id = c2.id    
  17438.     and    c2.colid = r.fkey12
  17439.     and permissions(o1.id) <> 0
  17440.     and permissions(o2.id) <> 0
  17441.     and i.id = r.rkeyid
  17442.     and i.indid = r.rkeyindid            
  17443.     union all
  17444.     select
  17445.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17446.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17447.         PK_TABLE_NAME         = o1.name,
  17448.         PK_COLUMN_NAME         = c1.name,
  17449.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17450.         PK_COLUMN_PROPID    = convert(int,null),
  17451.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17452.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  17453.         FK_TABLE_NAME         = o2.name,
  17454.         FK_COLUMN_NAME         = c2.name,
  17455.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17456.         FK_COLUMN_PROPID    = convert(int,null),
  17457.         ORDINAL                = convert(int,13),
  17458.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17459.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17460.         PK_NAME                = i.name,
  17461.         FK_NAME                = object_name(r.constid),
  17462.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17463.     from
  17464.         sysobjects o1, sysobjects o2,
  17465.         syscolumns c1, syscolumns c2,
  17466.         sysreferences r, sysindexes i
  17467.     where    
  17468.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17469.     and    o1.name = @pk_table_name
  17470.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17471.     and    o1.id = r.rkeyid
  17472.     and    r.keycnt >= 13
  17473.     and    o1.id = c1.id
  17474.     and    c1.colid = r.rkey13
  17475.     and r.fkeyid = o2.id
  17476.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  17477.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17478.     and    o2.id = c2.id    
  17479.     and    c2.colid = r.fkey13
  17480.     and permissions(o1.id) <> 0
  17481.     and permissions(o2.id) <> 0
  17482.     and i.id = r.rkeyid
  17483.     and i.indid = r.rkeyindid            
  17484.     union all
  17485.     select
  17486.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17487.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17488.         PK_TABLE_NAME         = o1.name,
  17489.         PK_COLUMN_NAME         = c1.name,
  17490.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17491.         PK_COLUMN_PROPID    = convert(int,null),
  17492.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17493.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  17494.         FK_TABLE_NAME         = o2.name,
  17495.         FK_COLUMN_NAME         = c2.name,
  17496.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17497.         FK_COLUMN_PROPID    = convert(int,null),
  17498.         ORDINAL                = convert(int,14),
  17499.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17500.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17501.         PK_NAME                = i.name,
  17502.         FK_NAME                = object_name(r.constid),
  17503.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17504.     from
  17505.         sysobjects o1, sysobjects o2,
  17506.         syscolumns c1, syscolumns c2,
  17507.         sysreferences r, sysindexes i
  17508.     where    
  17509.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17510.     and    o1.name = @pk_table_name
  17511.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17512.     and    o1.id = r.rkeyid
  17513.     and    r.keycnt >= 14
  17514.     and    o1.id = c1.id
  17515.     and    c1.colid = r.rkey14
  17516.     and r.fkeyid = o2.id
  17517.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  17518.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17519.     and    o2.id = c2.id    
  17520.     and    c2.colid = r.fkey14
  17521.     and permissions(o1.id) <> 0
  17522.     and permissions(o2.id) <> 0
  17523.     and i.id = r.rkeyid
  17524.     and i.indid = r.rkeyindid            
  17525.     union all
  17526.     select
  17527.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17528.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17529.         PK_TABLE_NAME         = o1.name,
  17530.         PK_COLUMN_NAME         = c1.name,
  17531.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17532.         PK_COLUMN_PROPID    = convert(int,null),
  17533.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17534.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  17535.         FK_TABLE_NAME         = o2.name,
  17536.         FK_COLUMN_NAME         = c2.name,
  17537.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17538.         FK_COLUMN_PROPID    = convert(int,null),
  17539.         ORDINAL                = convert(int,15),
  17540.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17541.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17542.         PK_NAME                = i.name,
  17543.         FK_NAME                = object_name(r.constid),
  17544.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17545.     from
  17546.         sysobjects o1, sysobjects o2,
  17547.         syscolumns c1, syscolumns c2,
  17548.         sysreferences r, sysindexes i
  17549.     where    
  17550.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17551.     and    o1.name = @pk_table_name
  17552.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17553.     and    o1.id = r.rkeyid
  17554.     and    r.keycnt >= 15
  17555.     and    o1.id = c1.id
  17556.     and    c1.colid = r.rkey15
  17557.     and r.fkeyid = o2.id
  17558.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  17559.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17560.     and    o2.id = c2.id    
  17561.     and    c2.colid = r.fkey15
  17562.     and permissions(o1.id) <> 0
  17563.     and permissions(o2.id) <> 0
  17564.     and i.id = r.rkeyid
  17565.     and i.indid = r.rkeyindid            
  17566.     union all
  17567.     select
  17568.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17569.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17570.         PK_TABLE_NAME         = o1.name,
  17571.         PK_COLUMN_NAME         = c1.name,
  17572.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17573.         PK_COLUMN_PROPID    = convert(int,null),
  17574.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17575.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  17576.         FK_TABLE_NAME         = o2.name,
  17577.         FK_COLUMN_NAME         = c2.name,
  17578.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17579.         FK_COLUMN_PROPID    = convert(int,null),
  17580.         ORDINAL                = convert(int,16),
  17581.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17582.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17583.         PK_NAME                = i.name,
  17584.         FK_NAME                = object_name(r.constid),
  17585.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17586.     from
  17587.         sysobjects o1, sysobjects o2,
  17588.         syscolumns c1, syscolumns c2,
  17589.         sysreferences r, sysindexes i
  17590.     where    
  17591.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17592.     and    o1.name = @pk_table_name
  17593.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17594.     and    o1.id = r.rkeyid
  17595.     and    r.keycnt >= 16
  17596.     and    o1.id = c1.id
  17597.     and    c1.colid = r.rkey16
  17598.     and r.fkeyid = o2.id
  17599.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  17600.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17601.     and    o2.id = c2.id    
  17602.     and    c2.colid = r.fkey16
  17603.     and permissions(o1.id) <> 0
  17604.     and permissions(o2.id) <> 0
  17605.     and i.id = r.rkeyid
  17606.     and i.indid = r.rkeyindid        
  17607.     order by 8,9,2,3,13
  17608. go
  17609. dump tran master with no_log
  17610. go
  17611. CREATE PROCEDURE sp_foreign_keys_rowset;2
  17612.     (
  17613.     @fk_table_name        sysname,
  17614.     @fk_table_schema    sysname = null,
  17615.     @pk_table_name        sysname = null,
  17616.     @pk_table_schema    sysname = null,
  17617.     @pk_table_catalog    sysname = null
  17618.     )
  17619. as
  17620.     select
  17621.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17622.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17623.         PK_TABLE_NAME         = o1.name,
  17624.         PK_COLUMN_NAME         = c1.name,
  17625.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17626.         PK_COLUMN_PROPID    = convert(int,null),
  17627.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17628.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  17629.         FK_TABLE_NAME         = o2.name,
  17630.         FK_COLUMN_NAME         = c2.name,
  17631.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17632.         FK_COLUMN_PROPID    = convert(int,null),
  17633.         ORDINAL                = convert(int,1),
  17634.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17635.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17636.         PK_NAME                = i.name,
  17637.         FK_NAME                = object_name(r.constid),
  17638.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17639.     from
  17640.         sysobjects o1, sysobjects o2,
  17641.         syscolumns c1, syscolumns c2,
  17642.         sysreferences r, sysindexes i
  17643.     where    
  17644.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  17645.     and    o2.name = @fk_table_name
  17646.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17647.     and    o2.id = r.fkeyid
  17648.     and    o2.id = c2.id
  17649.     and    c2.colid = r.fkey1
  17650.     and r.rkeyid = o1.id
  17651.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  17652.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17653.     and    o1.id = c1.id    
  17654.     and    c1.colid = r.rkey1
  17655.     and permissions(o1.id) <> 0
  17656.     and permissions(o2.id) <> 0
  17657.     and i.id = r.rkeyid
  17658.     and i.indid = r.rkeyindid        
  17659.     union    all
  17660.     select
  17661.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17662.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17663.         PK_TABLE_NAME         = o1.name,
  17664.         PK_COLUMN_NAME         = c1.name,
  17665.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17666.         PK_COLUMN_PROPID    = convert(int,null),
  17667.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17668.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  17669.         FK_TABLE_NAME         = o2.name,
  17670.         FK_COLUMN_NAME         = c2.name,
  17671.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17672.         FK_COLUMN_PROPID    = convert(int,null),
  17673.         ORDINAL                = convert(int,2),
  17674.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17675.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17676.         PK_NAME                = i.name,
  17677.         FK_NAME                = object_name(r.constid),
  17678.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17679.     from
  17680.         sysobjects o1, sysobjects o2,
  17681.         syscolumns c1, syscolumns c2,
  17682.         sysreferences r, sysindexes i
  17683.     where    
  17684.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  17685.     and    o2.name = @fk_table_name
  17686.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17687.     and    o2.id = r.fkeyid
  17688.     and    r.keycnt >= 2
  17689.     and    o2.id = c2.id
  17690.     and    c2.colid = r.fkey2
  17691.     and    r.rkeyid = o1.id
  17692.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  17693.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17694.     and    o1.id = c1.id    
  17695.     and    c1.colid = r.rkey2
  17696.     and permissions(o1.id) <> 0
  17697.     and permissions(o2.id) <> 0
  17698.     and i.id = r.rkeyid
  17699.     and i.indid = r.rkeyindid            
  17700.     union    all
  17701.     select
  17702.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17703.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17704.         PK_TABLE_NAME         = o1.name,
  17705.         PK_COLUMN_NAME         = c1.name,
  17706.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17707.         PK_COLUMN_PROPID    = convert(int,null),
  17708.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17709.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  17710.         FK_TABLE_NAME         = o2.name,
  17711.         FK_COLUMN_NAME         = c2.name,
  17712.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17713.         FK_COLUMN_PROPID    = convert(int,null),
  17714.         ORDINAL                = convert(int,3),
  17715.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17716.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17717.         PK_NAME                = i.name,
  17718.         FK_NAME                = object_name(r.constid),
  17719.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17720.     from
  17721.         sysobjects o1, sysobjects o2,
  17722.         syscolumns c1, syscolumns c2,
  17723.         sysreferences r, sysindexes i
  17724.     where    
  17725.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  17726.     and    o2.name = @fk_table_name
  17727.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17728.     and    o2.id = r.fkeyid
  17729.     and    r.keycnt >= 3
  17730.     and    o2.id = c2.id
  17731.     and    c2.colid = r.fkey3
  17732.     and r.rkeyid = o1.id
  17733.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  17734.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17735.     and    o1.id = c1.id    
  17736.     and    c1.colid = r.rkey3
  17737.     and permissions(o1.id) <> 0
  17738.     and permissions(o2.id) <> 0
  17739.     and i.id = r.rkeyid
  17740.     and i.indid = r.rkeyindid            
  17741.     union    all
  17742.     select
  17743.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17744.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17745.         PK_TABLE_NAME         = o1.name,
  17746.         PK_COLUMN_NAME         = c1.name,
  17747.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17748.         PK_COLUMN_PROPID    = convert(int,null),
  17749.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17750.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  17751.         FK_TABLE_NAME         = o2.name,
  17752.         FK_COLUMN_NAME         = c2.name,
  17753.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17754.         FK_COLUMN_PROPID    = convert(int,null),
  17755.         ORDINAL                = convert(int,4),
  17756.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17757.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17758.         PK_NAME                = i.name,
  17759.         FK_NAME                = object_name(r.constid),
  17760.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17761.     from
  17762.         sysobjects o1, sysobjects o2,
  17763.         syscolumns c1, syscolumns c2,
  17764.         sysreferences r, sysindexes i
  17765.     where    
  17766.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  17767.     and    o2.name = @fk_table_name
  17768.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17769.     and    o2.id = r.fkeyid
  17770.     and    r.keycnt >= 4
  17771.     and    o2.id = c2.id
  17772.     and    c2.colid = r.fkey4
  17773.     and r.rkeyid = o1.id
  17774.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  17775.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17776.     and    o1.id = c1.id    
  17777.     and    c1.colid = r.rkey4
  17778.     and permissions(o1.id) <> 0
  17779.     and permissions(o2.id) <> 0
  17780.     and i.id = r.rkeyid
  17781.     and i.indid = r.rkeyindid            
  17782.     union    all
  17783.     select
  17784.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17785.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17786.         PK_TABLE_NAME         = o1.name,
  17787.         PK_COLUMN_NAME         = c1.name,
  17788.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17789.         PK_COLUMN_PROPID    = convert(int,null),
  17790.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17791.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  17792.         FK_TABLE_NAME         = o2.name,
  17793.         FK_COLUMN_NAME         = c2.name,
  17794.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17795.         FK_COLUMN_PROPID    = convert(int,null),
  17796.         ORDINAL                = convert(int,5),
  17797.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17798.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17799.         PK_NAME                = i.name,
  17800.         FK_NAME                = object_name(r.constid),
  17801.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17802.     from
  17803.         sysobjects o1, sysobjects o2,
  17804.         syscolumns c1, syscolumns c2,
  17805.         sysreferences r, sysindexes i
  17806.     where    
  17807.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  17808.     and    o2.name = @fk_table_name
  17809.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17810.     and    o2.id = r.fkeyid
  17811.     and    r.keycnt >= 5
  17812.     and    o2.id = c2.id
  17813.     and    c2.colid = r.fkey5
  17814.     and    r.rkeyid = o1.id
  17815.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  17816.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17817.     and    o1.id = c1.id    
  17818.     and    c1.colid = r.rkey5
  17819.     and permissions(o1.id) <> 0
  17820.     and permissions(o2.id) <> 0
  17821.     and i.id = r.rkeyid
  17822.     and i.indid = r.rkeyindid            
  17823.     union    all
  17824.     select
  17825.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17826.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17827.         PK_TABLE_NAME         = o1.name,
  17828.         PK_COLUMN_NAME         = c1.name,
  17829.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17830.         PK_COLUMN_PROPID    = convert(int,null),
  17831.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17832.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  17833.         FK_TABLE_NAME         = o2.name,
  17834.         FK_COLUMN_NAME         = c2.name,
  17835.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17836.         FK_COLUMN_PROPID    = convert(int,null),
  17837.         ORDINAL                = convert(int,6),
  17838.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17839.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17840.         PK_NAME                = i.name,
  17841.         FK_NAME                = object_name(r.constid),
  17842.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17843.     from
  17844.         sysobjects o1, sysobjects o2,
  17845.         syscolumns c1, syscolumns c2,
  17846.         sysreferences r, sysindexes i
  17847.     where    
  17848.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  17849.     and    o2.name = @fk_table_name
  17850.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17851.     and    o2.id = r.fkeyid
  17852.     and    r.keycnt >= 6
  17853.     and    o2.id = c2.id
  17854.     and    c2.colid = r.fkey6
  17855.     and r.rkeyid = o1.id
  17856.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  17857.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17858.     and    o1.id = c1.id        
  17859.     and    c1.colid = r.rkey6
  17860.     and permissions(o1.id) <> 0
  17861.     and permissions(o2.id) <> 0
  17862.     and i.id = r.rkeyid
  17863.     and i.indid = r.rkeyindid        
  17864.     union    all
  17865.     select
  17866.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17867.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17868.         PK_TABLE_NAME         = o1.name,
  17869.         PK_COLUMN_NAME         = c1.name,
  17870.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17871.         PK_COLUMN_PROPID    = convert(int,null),
  17872.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17873.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  17874.         FK_TABLE_NAME         = o2.name,
  17875.         FK_COLUMN_NAME         = c2.name,
  17876.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17877.         FK_COLUMN_PROPID    = convert(int,null),
  17878.         ORDINAL                = convert(int,7),
  17879.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17880.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17881.         PK_NAME                = i.name,
  17882.         FK_NAME                = object_name(r.constid),
  17883.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17884.     from
  17885.         sysobjects o1, sysobjects o2,
  17886.         syscolumns c1, syscolumns c2,
  17887.         sysreferences r, sysindexes i
  17888.     where    
  17889.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  17890.     and    o2.name = @fk_table_name
  17891.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17892.     and    o2.id = r.fkeyid
  17893.     and    r.keycnt >= 7
  17894.     and    o2.id = c2.id
  17895.     and    c2.colid = r.fkey7
  17896.     and    r.rkeyid = o1.id
  17897.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  17898.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17899.     and    o1.id = c1.id    
  17900.     and    c1.colid = r.rkey7
  17901.     and permissions(o1.id) <> 0
  17902.     and permissions(o2.id) <> 0
  17903.     and i.id = r.rkeyid
  17904.     and i.indid = r.rkeyindid            
  17905.     union    all
  17906.     select
  17907.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17908.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17909.         PK_TABLE_NAME         = o1.name,
  17910.         PK_COLUMN_NAME         = c1.name,
  17911.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17912.         PK_COLUMN_PROPID    = convert(int,null),
  17913.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17914.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  17915.         FK_TABLE_NAME         = o2.name,
  17916.         FK_COLUMN_NAME         = c2.name,
  17917.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17918.         FK_COLUMN_PROPID    = convert(int,null),
  17919.         ORDINAL                = convert(int,8),
  17920.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17921.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17922.         PK_NAME                = i.name,
  17923.         FK_NAME                = object_name(r.constid),
  17924.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17925.     from
  17926.         sysobjects o1, sysobjects o2,
  17927.         syscolumns c1, syscolumns c2,
  17928.         sysreferences r, sysindexes i
  17929.     where    
  17930.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  17931.     and    o2.name = @fk_table_name
  17932.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17933.     and    o2.id = r.fkeyid
  17934.     and    r.keycnt >= 8
  17935.     and    o2.id = c2.id
  17936.     and    c2.colid = r.fkey8
  17937.     and r.rkeyid = o1.id
  17938.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  17939.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17940.     and    o1.id = c1.id    
  17941.     and    c1.colid = r.rkey8
  17942.     and permissions(o1.id) <> 0
  17943.     and permissions(o2.id) <> 0
  17944.     and i.id = r.rkeyid
  17945.     and i.indid = r.rkeyindid            
  17946.     union    all
  17947.     select
  17948.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17949.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17950.         PK_TABLE_NAME         = o1.name,
  17951.         PK_COLUMN_NAME         = c1.name,
  17952.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17953.         PK_COLUMN_PROPID    = convert(int,null),
  17954.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17955.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  17956.         FK_TABLE_NAME         = o2.name,
  17957.         FK_COLUMN_NAME         = c2.name,
  17958.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17959.         FK_COLUMN_PROPID    = convert(int,null),
  17960.         ORDINAL                = convert(int,9),
  17961.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17962.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17963.         PK_NAME                = i.name,
  17964.         FK_NAME                = object_name(r.constid),
  17965.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17966.     from
  17967.         sysobjects o1, sysobjects o2,
  17968.         syscolumns c1, syscolumns c2,
  17969.         sysreferences r, sysindexes i
  17970.     where    
  17971.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  17972.     and    o2.name = @fk_table_name
  17973.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17974.     and    o2.id = r.fkeyid
  17975.     and    r.keycnt >= 9
  17976.     and    o2.id = c2.id
  17977.     and    c2.colid = r.fkey9
  17978.     and    r.rkeyid = o1.id
  17979.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  17980.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17981.     and    o1.id = c1.id    
  17982.     and    c1.colid = r.rkey9
  17983.     and permissions(o1.id) <> 0
  17984.     and permissions(o2.id) <> 0
  17985.     and i.id = r.rkeyid
  17986.     and i.indid = r.rkeyindid            
  17987.     union    all
  17988.     select
  17989.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17990.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17991.         PK_TABLE_NAME         = o1.name,
  17992.         PK_COLUMN_NAME         = c1.name,
  17993.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17994.         PK_COLUMN_PROPID    = convert(int,null),
  17995.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17996.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  17997.         FK_TABLE_NAME         = o2.name,
  17998.         FK_COLUMN_NAME         = c2.name,
  17999.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18000.         FK_COLUMN_PROPID    = convert(int,null),
  18001.         ORDINAL                = convert(int,10),
  18002.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18003.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18004.         PK_NAME                = i.name,
  18005.         FK_NAME                = object_name(r.constid),
  18006.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18007.     from
  18008.         sysobjects o1, sysobjects o2,
  18009.         syscolumns c1, syscolumns c2,
  18010.         sysreferences r, sysindexes i
  18011.     where    
  18012.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18013.     and    o2.name = @fk_table_name
  18014.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18015.     and    o2.id = r.fkeyid
  18016.     and    r.keycnt >= 10
  18017.     and    o2.id = c2.id
  18018.     and    c2.colid = r.fkey10
  18019.     and r.rkeyid = o1.id
  18020.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  18021.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18022.     and    o1.id = c1.id    
  18023.     and    c1.colid = r.rkey10
  18024.     and permissions(o1.id) <> 0
  18025.     and permissions(o2.id) <> 0
  18026.     and i.id = r.rkeyid
  18027.     and i.indid = r.rkeyindid            
  18028.     union    all
  18029.     select
  18030.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18031.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  18032.         PK_TABLE_NAME         = o1.name,
  18033.         PK_COLUMN_NAME         = c1.name,
  18034.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18035.         PK_COLUMN_PROPID    = convert(int,null),
  18036.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18037.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  18038.         FK_TABLE_NAME         = o2.name,
  18039.         FK_COLUMN_NAME         = c2.name,
  18040.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18041.         FK_COLUMN_PROPID    = convert(int,null),
  18042.         ORDINAL                = convert(int,11),
  18043.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18044.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18045.         PK_NAME                = i.name,
  18046.         FK_NAME                = object_name(r.constid),
  18047.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18048.     from
  18049.         sysobjects o1, sysobjects o2,
  18050.         syscolumns c1, syscolumns c2,
  18051.         sysreferences r, sysindexes i
  18052.     where    
  18053.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18054.     and    o2.name = @fk_table_name
  18055.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18056.     and    o2.id = r.fkeyid
  18057.     and    r.keycnt >= 11
  18058.     and    o2.id = c2.id
  18059.     and    c2.colid = r.fkey11
  18060.     and r.rkeyid = o1.id
  18061.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  18062.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18063.     and    o1.id = c1.id    
  18064.     and    c1.colid = r.rkey11
  18065.     and permissions(o1.id) <> 0
  18066.     and permissions(o2.id) <> 0
  18067.     and i.id = r.rkeyid
  18068.     and i.indid = r.rkeyindid            
  18069.     union    all
  18070.     select
  18071.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18072.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  18073.         PK_TABLE_NAME         = o1.name,
  18074.         PK_COLUMN_NAME         = c1.name,
  18075.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18076.         PK_COLUMN_PROPID    = convert(int,null),
  18077.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18078.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  18079.         FK_TABLE_NAME         = o2.name,
  18080.         FK_COLUMN_NAME         = c2.name,
  18081.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18082.         FK_COLUMN_PROPID    = convert(int,null),
  18083.         ORDINAL                = convert(int,12),
  18084.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18085.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18086.         PK_NAME                = i.name,
  18087.         FK_NAME                = object_name(r.constid),
  18088.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18089.     from
  18090.         sysobjects o1, sysobjects o2,
  18091.         syscolumns c1, syscolumns c2,
  18092.         sysreferences r, sysindexes i
  18093.     where    
  18094.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18095.     and    o2.name = @fk_table_name
  18096.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18097.     and    o2.id = r.fkeyid
  18098.     and    r.keycnt >= 12
  18099.     and    o2.id = c2.id
  18100.     and    c2.colid = r.fkey12
  18101.     and r.rkeyid = o1.id
  18102.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  18103.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18104.     and    o1.id = c1.id    
  18105.     and    c1.colid = r.rkey12
  18106.     and permissions(o1.id) <> 0
  18107.     and permissions(o2.id) <> 0
  18108.     and i.id = r.rkeyid
  18109.     and i.indid = r.rkeyindid            
  18110.     union    all
  18111.     select
  18112.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18113.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  18114.         PK_TABLE_NAME         = o1.name,
  18115.         PK_COLUMN_NAME         = c1.name,
  18116.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18117.         PK_COLUMN_PROPID    = convert(int,null),
  18118.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18119.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  18120.         FK_TABLE_NAME         = o2.name,
  18121.         FK_COLUMN_NAME         = c2.name,
  18122.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18123.         FK_COLUMN_PROPID    = convert(int,null),
  18124.         ORDINAL                = convert(int,13),
  18125.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18126.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18127.         PK_NAME                = i.name,
  18128.         FK_NAME                = object_name(r.constid),
  18129.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18130.     from
  18131.         sysobjects o1, sysobjects o2,
  18132.         syscolumns c1, syscolumns c2,
  18133.         sysreferences r, sysindexes i
  18134.     where    
  18135.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18136.     and    o2.name = @fk_table_name
  18137.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18138.     and    o2.id = r.fkeyid
  18139.     and    r.keycnt >= 13
  18140.     and    o2.id = c2.id
  18141.     and    c2.colid = r.fkey13
  18142.     and r.rkeyid = o1.id
  18143.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  18144.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18145.     and    o1.id = c1.id    
  18146.     and    c1.colid = r.rkey13
  18147.     and permissions(o1.id) <> 0
  18148.     and permissions(o2.id) <> 0
  18149.     and i.id = r.rkeyid
  18150.     and i.indid = r.rkeyindid            
  18151.     union    all
  18152.     select
  18153.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18154.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  18155.         PK_TABLE_NAME         = o1.name,
  18156.         PK_COLUMN_NAME         = c1.name,
  18157.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18158.         PK_COLUMN_PROPID    = convert(int,null),
  18159.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18160.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  18161.         FK_TABLE_NAME         = o2.name,
  18162.         FK_COLUMN_NAME         = c2.name,
  18163.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18164.         FK_COLUMN_PROPID    = convert(int,null),
  18165.         ORDINAL                = convert(int,14),
  18166.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18167.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18168.         PK_NAME                = i.name,
  18169.         FK_NAME                = object_name(r.constid),
  18170.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18171.     from
  18172.         sysobjects o1, sysobjects o2,
  18173.         syscolumns c1, syscolumns c2,
  18174.         sysreferences r, sysindexes i
  18175.     where    
  18176.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18177.     and    o2.name = @fk_table_name
  18178.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18179.     and    o2.id = r.fkeyid
  18180.     and    r.keycnt >= 14
  18181.     and    o2.id = c2.id
  18182.     and    c2.colid = r.fkey14
  18183.     and r.rkeyid = o1.id
  18184.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  18185.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18186.     and    o1.id = c1.id    
  18187.     and    c1.colid = r.rkey14
  18188.     and permissions(o1.id) <> 0
  18189.     and permissions(o2.id) <> 0
  18190.     and i.id = r.rkeyid
  18191.     and i.indid = r.rkeyindid            
  18192.     union    all
  18193.     select
  18194.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18195.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  18196.         PK_TABLE_NAME         = o1.name,
  18197.         PK_COLUMN_NAME         = c1.name,
  18198.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18199.         PK_COLUMN_PROPID    = convert(int,null),
  18200.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18201.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  18202.         FK_TABLE_NAME         = o2.name,
  18203.         FK_COLUMN_NAME         = c2.name,
  18204.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18205.         FK_COLUMN_PROPID    = convert(int,null),
  18206.         ORDINAL                = convert(int,15),
  18207.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18208.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18209.         PK_NAME                = i.name,
  18210.         FK_NAME                = object_name(r.constid),
  18211.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/        
  18212.     from
  18213.         sysobjects o1, sysobjects o2,
  18214.         syscolumns c1, syscolumns c2,
  18215.         sysreferences r, sysindexes i
  18216.     where    
  18217.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18218.     and    o2.name = @fk_table_name
  18219.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18220.     and    o2.id = r.fkeyid
  18221.     and    r.keycnt >= 15
  18222.     and    o2.id = c2.id
  18223.     and    c2.colid = r.fkey15
  18224.     and r.rkeyid = o1.id
  18225.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  18226.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18227.     and    o1.id = c1.id    
  18228.     and    c1.colid = r.rkey15
  18229.     and permissions(o1.id) <> 0
  18230.     and permissions(o2.id) <> 0
  18231.     and i.id = r.rkeyid
  18232.     and i.indid = r.rkeyindid            
  18233.     union    all
  18234.     select
  18235.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18236.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  18237.         PK_TABLE_NAME         = o1.name,
  18238.         PK_COLUMN_NAME         = c1.name,
  18239.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18240.         PK_COLUMN_PROPID    = convert(int,null),
  18241.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18242.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  18243.         FK_TABLE_NAME         = o2.name,
  18244.         FK_COLUMN_NAME         = c2.name,
  18245.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18246.         FK_COLUMN_PROPID    = convert(int,null),
  18247.         ORDINAL                = convert(int,16),
  18248.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18249.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18250.         PK_NAME                = i.name,
  18251.         FK_NAME                = object_name(r.constid),
  18252.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/        
  18253.     from
  18254.         sysobjects o1, sysobjects o2,
  18255.         syscolumns c1, syscolumns c2,
  18256.         sysreferences r, sysindexes i
  18257.     where    
  18258.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18259.     and    o2.name = @fk_table_name
  18260.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18261.     and    o2.id = r.fkeyid
  18262.     and    r.keycnt >= 16
  18263.     and    o2.id = c2.id
  18264.     and    c2.colid = r.fkey16
  18265.     and r.rkeyid = o1.id
  18266.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  18267.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18268.     and    o1.id = c1.id    
  18269.     and    c1.colid = r.rkey16
  18270.     and permissions(o1.id) <> 0
  18271.     and permissions(o2.id) <> 0
  18272.     and i.id = r.rkeyid
  18273.     and i.indid = r.rkeyindid        
  18274.     order by 8,9,2,3,13
  18275. go
  18276. dump tran master with no_log
  18277. go
  18278. CREATE PROCEDURE sp_foreign_keys_rowset;3
  18279.     (
  18280.     @pk_table_schema    sysname = null,
  18281.     @pk_table_catalog    sysname = null,
  18282.     @fk_table_schema    sysname = null,
  18283.     @fk_table_catalog    sysname = null
  18284.     )
  18285. as
  18286.     select
  18287.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18288.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  18289.         PK_TABLE_NAME         = o1.name,
  18290.         PK_COLUMN_NAME         = c1.name,
  18291.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18292.         PK_COLUMN_PROPID    = convert(int,null),
  18293.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18294.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  18295.         FK_TABLE_NAME         = o2.name,
  18296.         FK_COLUMN_NAME         = c2.name,
  18297.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18298.         FK_COLUMN_PROPID    = convert(int,null),
  18299.         ORDINAL                = convert(int,1),
  18300.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18301.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18302.         PK_NAME                = i.name,
  18303.         FK_NAME                = object_name(r.constid),
  18304.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/        
  18305.     from
  18306.         sysobjects o1, sysobjects o2,
  18307.         syscolumns c1, syscolumns c2,
  18308.         sysreferences r, sysindexes i
  18309.     where    
  18310.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18311.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  18312.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18313.     and    o1.id = r.rkeyid
  18314.     and    o1.id = c1.id
  18315.     and    c1.colid = r.rkey1
  18316.     and r.fkeyid = o2.id
  18317.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18318.     and    o2.id = c2.id    
  18319.     and    c2.colid = r.fkey1
  18320.     and permissions(o1.id) <> 0
  18321.     and permissions(o2.id) <> 0
  18322.     and i.id = r.rkeyid
  18323.     and i.indid = r.rkeyindid        
  18324.     union all
  18325.     select
  18326.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18327.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  18328.         PK_TABLE_NAME         = o1.name,
  18329.         PK_COLUMN_NAME         = c1.name,
  18330.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18331.         PK_COLUMN_PROPID    = convert(int,null),
  18332.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18333.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  18334.         FK_TABLE_NAME         = o2.name,
  18335.         FK_COLUMN_NAME         = c2.name,
  18336.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18337.         FK_COLUMN_PROPID    = convert(int,null),
  18338.         ORDINAL                = convert(int,2),
  18339.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18340.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18341.         PK_NAME                = i.name,
  18342.         FK_NAME                = object_name(r.constid),
  18343.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18344.     from
  18345.         sysobjects o1, sysobjects o2,
  18346.         syscolumns c1, syscolumns c2,
  18347.         sysreferences r, sysindexes i
  18348.     where    
  18349.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18350.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  18351.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18352.     and    o1.id = r.rkeyid
  18353.     and    r.keycnt >= 2
  18354.     and    o1.id = c1.id
  18355.     and    c1.colid = r.rkey2
  18356.     and r.fkeyid = o2.id
  18357.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18358.     and    o2.id = c2.id    
  18359.     and    c2.colid = r.fkey2
  18360.     and permissions(o1.id) <> 0
  18361.     and permissions(o2.id) <> 0
  18362.     and i.id = r.rkeyid
  18363.     and i.indid = r.rkeyindid        
  18364.     union all
  18365.     select
  18366.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18367.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  18368.         PK_TABLE_NAME         = o1.name,
  18369.         PK_COLUMN_NAME         = c1.name,
  18370.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18371.         PK_COLUMN_PROPID    = convert(int,null),
  18372.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18373.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  18374.         FK_TABLE_NAME         = o2.name,
  18375.         FK_COLUMN_NAME         = c2.name,
  18376.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18377.         FK_COLUMN_PROPID    = convert(int,null),
  18378.         ORDINAL                = convert(int,3),
  18379.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18380.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18381.         PK_NAME                = i.name,
  18382.         FK_NAME                = object_name(r.constid),
  18383.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18384.     from
  18385.         sysobjects o1, sysobjects o2,
  18386.         syscolumns c1, syscolumns c2,
  18387.         sysreferences r, sysindexes i
  18388.     where    
  18389.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18390.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  18391.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18392.     and    o1.id = r.rkeyid
  18393.     and    r.keycnt >= 3
  18394.     and    o1.id = c1.id
  18395.     and    c1.colid = r.rkey3
  18396.     and r.fkeyid = o2.id
  18397.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18398.     and    o2.id = c2.id    
  18399.     and    c2.colid = r.fkey3
  18400.     and permissions(o1.id) <> 0
  18401.     and permissions(o2.id) <> 0
  18402.     and i.id = r.rkeyid
  18403.     and i.indid = r.rkeyindid        
  18404.     union all
  18405.     select
  18406.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18407.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  18408.         PK_TABLE_NAME         = o1.name,
  18409.         PK_COLUMN_NAME         = c1.name,
  18410.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18411.         PK_COLUMN_PROPID    = convert(int,null),
  18412.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18413.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  18414.         FK_TABLE_NAME         = o2.name,
  18415.         FK_COLUMN_NAME         = c2.name,
  18416.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18417.         FK_COLUMN_PROPID    = convert(int,null),
  18418.         ORDINAL                = convert(int,4),
  18419.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18420.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18421.         PK_NAME                = i.name,
  18422.         FK_NAME                = object_name(r.constid),
  18423.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18424.     from
  18425.         sysobjects o1, sysobjects o2,
  18426.         syscolumns c1, syscolumns c2,
  18427.         sysreferences r, sysindexes i
  18428.     where    
  18429.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18430.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  18431.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18432.     and    o1.id = r.rkeyid
  18433.     and    r.keycnt >= 4
  18434.     and    o1.id = c1.id
  18435.     and    c1.colid = r.rkey4
  18436.     and r.fkeyid = o2.id
  18437.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18438.     and    o2.id = c2.id    
  18439.     and    c2.colid = r.fkey4
  18440.     and permissions(o1.id) <> 0
  18441.     and permissions(o2.id) <> 0
  18442.     and i.id = r.rkeyid
  18443.     and i.indid = r.rkeyindid        
  18444.     union all
  18445.     select
  18446.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18447.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  18448.         PK_TABLE_NAME         = o1.name,
  18449.         PK_COLUMN_NAME         = c1.name,
  18450.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18451.         PK_COLUMN_PROPID    = convert(int,null),
  18452.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18453.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  18454.         FK_TABLE_NAME         = o2.name,
  18455.         FK_COLUMN_NAME         = c2.name,
  18456.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18457.         FK_COLUMN_PROPID    = convert(int,null),
  18458.         ORDINAL                = convert(int,5),
  18459.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18460.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18461.         PK_NAME                = i.name,
  18462.         FK_NAME                = object_name(r.constid),
  18463.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18464.     from
  18465.         sysobjects o1, sysobjects o2,
  18466.         syscolumns c1, syscolumns c2,
  18467.         sysreferences r, sysindexes i
  18468.     where    
  18469.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18470.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  18471.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18472.     and    o1.id = r.rkeyid
  18473.     and    r.keycnt >= 5
  18474.     and    o1.id = c1.id
  18475.     and    c1.colid = r.rkey5
  18476.     and r.fkeyid = o2.id
  18477.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18478.     and    o2.id = c2.id    
  18479.     and    c2.colid = r.fkey5
  18480.     and permissions(o1.id) <> 0
  18481.     and permissions(o2.id) <> 0
  18482.     and i.id = r.rkeyid
  18483.     and i.indid = r.rkeyindid        
  18484.     union all
  18485.     select
  18486.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18487.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  18488.         PK_TABLE_NAME         = o1.name,
  18489.         PK_COLUMN_NAME         = c1.name,
  18490.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18491.         PK_COLUMN_PROPID    = convert(int,null),
  18492.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18493.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  18494.         FK_TABLE_NAME         = o2.name,
  18495.         FK_COLUMN_NAME         = c2.name,
  18496.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18497.         FK_COLUMN_PROPID    = convert(int,null),
  18498.         ORDINAL                = convert(int,6),
  18499.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18500.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18501.         PK_NAME                = i.name,
  18502.         FK_NAME                = object_name(r.constid),
  18503.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18504.     from
  18505.         sysobjects o1, sysobjects o2,
  18506.         syscolumns c1, syscolumns c2,
  18507.         sysreferences r, sysindexes i
  18508.     where    
  18509.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18510.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  18511.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18512.     and    o1.id = r.rkeyid
  18513.     and    r.keycnt >= 6
  18514.     and    o1.id = c1.id
  18515.     and    c1.colid = r.rkey6
  18516.     and r.fkeyid = o2.id
  18517.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18518.     and    o2.id = c2.id    
  18519.     and    c2.colid = r.fkey6
  18520.     and permissions(o1.id) <> 0
  18521.     and permissions(o2.id) <> 0
  18522.     and i.id = r.rkeyid
  18523.     and i.indid = r.rkeyindid        
  18524.     union all
  18525.     select
  18526.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18527.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  18528.         PK_TABLE_NAME         = o1.name,
  18529.         PK_COLUMN_NAME         = c1.name,
  18530.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18531.         PK_COLUMN_PROPID    = convert(int,null),
  18532.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18533.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  18534.         FK_TABLE_NAME         = o2.name,
  18535.         FK_COLUMN_NAME         = c2.name,
  18536.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18537.         FK_COLUMN_PROPID    = convert(int,null),
  18538.         ORDINAL                = convert(int,7),
  18539.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18540.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18541.         PK_NAME                = i.name,
  18542.         FK_NAME                = object_name(r.constid),
  18543.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18544.     from
  18545.         sysobjects o1, sysobjects o2,
  18546.         syscolumns c1, syscolumns c2,
  18547.         sysreferences r, sysindexes i
  18548.     where    
  18549.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18550.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  18551.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18552.     and    o1.id = r.rkeyid
  18553.     and    r.keycnt >= 7
  18554.     and    o1.id = c1.id
  18555.     and    c1.colid = r.rkey7
  18556.     and r.fkeyid = o2.id
  18557.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18558.     and    o2.id = c2.id    
  18559.     and    c2.colid = r.fkey7
  18560.     and permissions(o1.id) <> 0
  18561.     and permissions(o2.id) <> 0
  18562.     and i.id = r.rkeyid
  18563.     and i.indid = r.rkeyindid        
  18564.     union all
  18565.     select
  18566.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18567.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  18568.         PK_TABLE_NAME         = o1.name,
  18569.         PK_COLUMN_NAME         = c1.name,
  18570.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18571.         PK_COLUMN_PROPID    = convert(int,null),
  18572.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18573.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  18574.         FK_TABLE_NAME         = o2.name,
  18575.         FK_COLUMN_NAME         = c2.name,
  18576.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18577.         FK_COLUMN_PROPID    = convert(int,null),
  18578.         ORDINAL                = convert(int,8),
  18579.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18580.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18581.         PK_NAME                = i.name,
  18582.         FK_NAME                = object_name(r.constid),
  18583.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18584.     from
  18585.         sysobjects o1, sysobjects o2,
  18586.         syscolumns c1, syscolumns c2,
  18587.         sysreferences r, sysindexes i
  18588.     where    
  18589.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18590.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  18591.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18592.     and    o1.id = r.rkeyid
  18593.     and    r.keycnt >= 8
  18594.     and    o1.id = c1.id
  18595.     and    c1.colid = r.rkey8
  18596.     and r.fkeyid = o2.id
  18597.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18598.     and    o2.id = c2.id    
  18599.     and    c2.colid = r.fkey8
  18600.     and permissions(o1.id) <> 0
  18601.     and permissions(o2.id) <> 0
  18602.     and i.id = r.rkeyid
  18603.     and i.indid = r.rkeyindid        
  18604.     union all
  18605.     select
  18606.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18607.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  18608.         PK_TABLE_NAME         = o1.name,
  18609.         PK_COLUMN_NAME         = c1.name,
  18610.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18611.         PK_COLUMN_PROPID    = convert(int,null),
  18612.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18613.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  18614.         FK_TABLE_NAME         = o2.name,
  18615.         FK_COLUMN_NAME         = c2.name,
  18616.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18617.         FK_COLUMN_PROPID    = convert(int,null),
  18618.         ORDINAL                = convert(int,9),
  18619.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18620.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18621.         PK_NAME                = i.name,
  18622.         FK_NAME                = object_name(r.constid),
  18623.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18624.     from
  18625.         sysobjects o1, sysobjects o2,
  18626.         syscolumns c1, syscolumns c2,
  18627.         sysreferences r, sysindexes i
  18628.     where    
  18629.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18630.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  18631.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18632.     and    o1.id = r.rkeyid
  18633.     and    r.keycnt >= 9
  18634.     and    o1.id = c1.id
  18635.     and    c1.colid = r.rkey9
  18636.     and r.fkeyid = o2.id
  18637.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18638.     and    o2.id = c2.id    
  18639.     and    c2.colid = r.fkey9
  18640.     and permissions(o1.id) <> 0
  18641.     and permissions(o2.id) <> 0
  18642.     and i.id = r.rkeyid
  18643.     and i.indid = r.rkeyindid        
  18644.     union all
  18645.     select
  18646.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18647.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  18648.         PK_TABLE_NAME         = o1.name,
  18649.         PK_COLUMN_NAME         = c1.name,
  18650.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18651.         PK_COLUMN_PROPID    = convert(int,null),
  18652.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18653.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  18654.         FK_TABLE_NAME         = o2.name,
  18655.         FK_COLUMN_NAME         = c2.name,
  18656.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18657.         FK_COLUMN_PROPID    = convert(int,null),
  18658.         ORDINAL                = convert(int,10),
  18659.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18660.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18661.         PK_NAME                = i.name,
  18662.         FK_NAME                = object_name(r.constid),
  18663.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18664.     from
  18665.         sysobjects o1, sysobjects o2,
  18666.         syscolumns c1, syscolumns c2,
  18667.         sysreferences r, sysindexes i
  18668.     where    
  18669.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18670.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  18671.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18672.     and    o1.id = r.rkeyid
  18673.     and    r.keycnt >= 10
  18674.     and    o1.id = c1.id
  18675.     and    c1.colid = r.rkey10
  18676.     and r.fkeyid = o2.id
  18677.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18678.     and    o2.id = c2.id    
  18679.     and    c2.colid = r.fkey10
  18680.     and permissions(o1.id) <> 0
  18681.     and permissions(o2.id) <> 0
  18682.     and i.id = r.rkeyid
  18683.     and i.indid = r.rkeyindid        
  18684.     union all
  18685.     select
  18686.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18687.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  18688.         PK_TABLE_NAME         = o1.name,
  18689.         PK_COLUMN_NAME         = c1.name,
  18690.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18691.         PK_COLUMN_PROPID    = convert(int,null),
  18692.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18693.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  18694.         FK_TABLE_NAME         = o2.name,
  18695.         FK_COLUMN_NAME         = c2.name,
  18696.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18697.         FK_COLUMN_PROPID    = convert(int,null),
  18698.         ORDINAL                = convert(int,11),
  18699.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18700.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18701.         PK_NAME                = i.name,
  18702.         FK_NAME                = object_name(r.constid),
  18703.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18704.     from
  18705.         sysobjects o1, sysobjects o2,
  18706.         syscolumns c1, syscolumns c2,
  18707.         sysreferences r, sysindexes i
  18708.     where    
  18709.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18710.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  18711.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18712.     and    o1.id = r.rkeyid
  18713.     and    r.keycnt >= 11
  18714.     and    o1.id = c1.id
  18715.     and    c1.colid = r.rkey11
  18716.     and r.fkeyid = o2.id
  18717.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18718.     and    o2.id = c2.id    
  18719.     and    c2.colid = r.fkey11
  18720.     and permissions(o1.id) <> 0
  18721.     and permissions(o2.id) <> 0
  18722.     and i.id = r.rkeyid
  18723.     and i.indid = r.rkeyindid        
  18724.     union all
  18725.     select
  18726.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18727.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  18728.         PK_TABLE_NAME         = o1.name,
  18729.         PK_COLUMN_NAME         = c1.name,
  18730.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18731.         PK_COLUMN_PROPID    = convert(int,null),
  18732.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18733.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  18734.         FK_TABLE_NAME         = o2.name,
  18735.         FK_COLUMN_NAME         = c2.name,
  18736.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18737.         FK_COLUMN_PROPID    = convert(int,null),
  18738.         ORDINAL                = convert(int,12),
  18739.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18740.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18741.         PK_NAME                = i.name,
  18742.         FK_NAME                = object_name(r.constid),
  18743.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18744.     from
  18745.         sysobjects o1, sysobjects o2,
  18746.         syscolumns c1, syscolumns c2,
  18747.         sysreferences r, sysindexes i
  18748.     where    
  18749.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18750.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  18751.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18752.     and    o1.id = r.rkeyid
  18753.     and    r.keycnt >= 12
  18754.     and    o1.id = c1.id
  18755.     and    c1.colid = r.rkey12
  18756.     and r.fkeyid = o2.id
  18757.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18758.     and    o2.id = c2.id    
  18759.     and    c2.colid = r.fkey12
  18760.     and permissions(o1.id) <> 0
  18761.     and permissions(o2.id) <> 0
  18762.     and i.id = r.rkeyid
  18763.     and i.indid = r.rkeyindid        
  18764.     union all
  18765.     select
  18766.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18767.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  18768.         PK_TABLE_NAME         = o1.name,
  18769.         PK_COLUMN_NAME         = c1.name,
  18770.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18771.         PK_COLUMN_PROPID    = convert(int,null),
  18772.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18773.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  18774.         FK_TABLE_NAME         = o2.name,
  18775.         FK_COLUMN_NAME         = c2.name,
  18776.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18777.         FK_COLUMN_PROPID    = convert(int,null),
  18778.         ORDINAL                = convert(int,13),
  18779.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18780.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18781.         PK_NAME                = i.name,
  18782.         FK_NAME                = object_name(r.constid),
  18783.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18784.     from
  18785.         sysobjects o1, sysobjects o2,
  18786.         syscolumns c1, syscolumns c2,
  18787.         sysreferences r, sysindexes i
  18788.     where    
  18789.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18790.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  18791.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18792.     and    o1.id = r.rkeyid
  18793.     and    r.keycnt >= 13
  18794.     and    o1.id = c1.id
  18795.     and    c1.colid = r.rkey13
  18796.     and r.fkeyid = o2.id
  18797.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18798.     and    o2.id = c2.id    
  18799.     and    c2.colid = r.fkey13
  18800.     and permissions(o1.id) <> 0
  18801.     and permissions(o2.id) <> 0
  18802.     and i.id = r.rkeyid
  18803.     and i.indid = r.rkeyindid        
  18804.     union all
  18805.     select
  18806.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18807.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  18808.         PK_TABLE_NAME         = o1.name,
  18809.         PK_COLUMN_NAME         = c1.name,
  18810.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18811.         PK_COLUMN_PROPID    = convert(int,null),
  18812.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18813.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  18814.         FK_TABLE_NAME         = o2.name,
  18815.         FK_COLUMN_NAME         = c2.name,
  18816.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18817.         FK_COLUMN_PROPID    = convert(int,null),
  18818.         ORDINAL                = convert(int,14),
  18819.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18820.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18821.         PK_NAME                = i.name,
  18822.         FK_NAME                = object_name(r.constid),
  18823.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18824.     from
  18825.         sysobjects o1, sysobjects o2,
  18826.         syscolumns c1, syscolumns c2,
  18827.         sysreferences r, sysindexes i
  18828.     where    
  18829.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18830.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  18831.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18832.     and    o1.id = r.rkeyid
  18833.     and    r.keycnt >= 14
  18834.     and    o1.id = c1.id
  18835.     and    c1.colid = r.rkey14
  18836.     and r.fkeyid = o2.id
  18837.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18838.     and    o2.id = c2.id    
  18839.     and    c2.colid = r.fkey14
  18840.     and permissions(o1.id) <> 0
  18841.     and permissions(o2.id) <> 0
  18842.     and i.id = r.rkeyid
  18843.     and i.indid = r.rkeyindid        
  18844.     union all
  18845.     select
  18846.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18847.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  18848.         PK_TABLE_NAME         = o1.name,
  18849.         PK_COLUMN_NAME         = c1.name,
  18850.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18851.         PK_COLUMN_PROPID    = convert(int,null),
  18852.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18853.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  18854.         FK_TABLE_NAME         = o2.name,
  18855.         FK_COLUMN_NAME         = c2.name,
  18856.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18857.         FK_COLUMN_PROPID    = convert(int,null),
  18858.         ORDINAL                = convert(int,15),
  18859.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18860.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18861.         PK_NAME                = i.name,
  18862.         FK_NAME                = object_name(r.constid),
  18863.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18864.     from
  18865.         sysobjects o1, sysobjects o2,
  18866.         syscolumns c1, syscolumns c2,
  18867.         sysreferences r, sysindexes i
  18868.     where    
  18869.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18870.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  18871.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18872.     and    o1.id = r.rkeyid
  18873.     and    r.keycnt >= 15
  18874.     and    o1.id = c1.id
  18875.     and    c1.colid = r.rkey15
  18876.     and r.fkeyid = o2.id
  18877.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18878.     and    o2.id = c2.id    
  18879.     and    c2.colid = r.fkey15
  18880.     and permissions(o1.id) <> 0
  18881.     and permissions(o2.id) <> 0
  18882.     and i.id = r.rkeyid
  18883.     and i.indid = r.rkeyindid        
  18884.     union all
  18885.     select
  18886.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18887.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  18888.         PK_TABLE_NAME         = o1.name,
  18889.         PK_COLUMN_NAME         = c1.name,
  18890.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18891.         PK_COLUMN_PROPID    = convert(int,null),
  18892.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18893.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  18894.         FK_TABLE_NAME         = o2.name,
  18895.         FK_COLUMN_NAME         = c2.name,
  18896.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18897.         FK_COLUMN_PROPID    = convert(int,null),
  18898.         ORDINAL                = convert(int,16),
  18899.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18900.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18901.         PK_NAME                = i.name,
  18902.         FK_NAME                = object_name(r.constid),
  18903.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18904.     from
  18905.         sysobjects o1, sysobjects o2,
  18906.         syscolumns c1, syscolumns c2,
  18907.         sysreferences r, sysindexes i
  18908.     where    
  18909.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18910.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  18911.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18912.     and    o1.id = r.rkeyid
  18913.     and    o1.id = c1.id
  18914.     and    r.keycnt >= 16
  18915.     and    c1.colid = r.rkey16
  18916.     and r.fkeyid = o2.id
  18917.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18918.     and    o2.id = c2.id    
  18919.     and    c2.colid = r.fkey16
  18920.     and permissions(o1.id) <> 0
  18921.     and permissions(o2.id) <> 0
  18922.     and i.id = r.rkeyid
  18923.     and i.indid = r.rkeyindid        
  18924.     order by 8,9,2,3,13
  18925. go
  18926. dump tran master with no_log
  18927. go
  18928. create procedure sp_foreign_keys_rowset;5
  18929.     (
  18930.     @server_name        sysname,
  18931.     @pk_catalog_name    sysname = null,
  18932.     @fk_catalog_name    sysname = null,
  18933.     @pk_table_name        sysname = null,
  18934.     @pk_table_schema    sysname = null,
  18935.     @fk_table_name        sysname = null,
  18936.     @fk_table_schema    sysname = null
  18937.     )
  18938. as
  18939.     select
  18940.         PK_TABLE_CATALOG,
  18941.         PK_TABLE_SCHEMA,    
  18942.         PK_TABLE_NAME,     
  18943.         PK_COLUMN_NAME,     
  18944.         PK_COLUMN_GUID,    
  18945.         PK_COLUMN_PROPID,
  18946.         FK_TABLE_CATALOG,
  18947.         FK_TABLE_SCHEMA, 
  18948.         FK_TABLE_NAME,     
  18949.         FK_COLUMN_NAME,     
  18950.         FK_COLUMN_GUID,    
  18951.         FK_COLUMN_PROPID,
  18952.         ORDINAL,        
  18953.         UPDATE_RULE,    
  18954.         DELETE_RULE
  18955.         --PK_NAME,        
  18956.         --FK_NAME,
  18957.         --DEFERRABILITY    
  18958.     from master.dbo.SYSREMOTE_FOREIGN_KEYS <
  18959.                 @server_name,
  18960.                 @pk_catalog_name,
  18961.                 @pk_table_schema,
  18962.                 @pk_table_name,
  18963.                 @fk_catalog_name,
  18964.                 @fk_table_schema,
  18965.                 @fk_table_name >
  18966.     order by 7,8,9,1,2,3,13
  18967. go
  18968.  
  18969. grant execute on sp_foreign_keys_rowset to public
  18970. go
  18971.  
  18972. dump tran master with no_log
  18973. go
  18974. if (charindex('6.00', @@version) > 0)
  18975.     begin
  18976.     if (exists (select * from sysobjects
  18977.             where name = 'sp_foreign_keys_rowset' and type = 'P '))
  18978.         begin
  18979.         drop procedure sp_foreign_keys_rowset
  18980.         dump tran master with no_log
  18981.         end
  18982.     end
  18983. go
  18984.  
  18985.  
  18986. print ''
  18987. print 'creating sp_indexes_rowset'
  18988. go
  18989.  
  18990. /*    6.0 and 6.5 version */
  18991. create procedure sp_indexes_rowset
  18992.     (
  18993.     @table_name    varchar(255), 
  18994.     @index_name    varchar(255) = null,
  18995.     @table_schema    varchar(255) = null     
  18996.     )
  18997. as
  18998.     select    TABLE_CATALOG        = db_name(),
  18999.         TABLE_SCHEMA        = user_name(o.uid),
  19000.         TABLE_NAME        = o.name,
  19001.         INDEX_CATALOG        = db_name(),        
  19002.         INDEX_SCHEMA        = user_name(o.uid),
  19003.         INDEX_NAME        = x.name,
  19004.         PRIMARY_KEY        = convert(bit,(x.status & 0x800)/0x800),
  19005.         "UNIQUE"        = convert(bit,(x.status & 2)/2),
  19006.         "CLUSTERED"        = convert(bit,(x.status & 16)/16),
  19007.         "TYPE"            = convert(smallint, 1 /*DBPROPVAL_IT_BTREE*/),
  19008.         FILL_FACTOR        = convert(int, x.OrigFillFactor),
  19009.         INITIAL_SIZE        = convert(int,null),
  19010.         NULLS            = convert(int,null),
  19011.         SORT_BOOKMARKS        = convert(bit,0),
  19012.         AUTO_UPDATE        = convert(bit,1),
  19013.         NULL_COLLATION        = convert(int,4 /*DBPROPVAL_NC_LOW*/),
  19014.         ORDINAL_POSITION     = convert(int,
  19015.                        (
  19016.                         select count(*)
  19017.                         from syscolumns sc
  19018.                         where sc.id     =  c.id
  19019.                           AND sc.number =  c.number
  19020.                           AND sc.colid  <= c.colid
  19021.                         )),
  19022.         COLUMN_NAME        = index_col(user_name(o.uid)+'.'+o.name, x.indid, c.colid),
  19023.         COLUMN_GUID        = convert(binary(16),null),
  19024.         COLUMN_PROPID        = convert(int,null),
  19025.         COLLATION    = convert(smallint, 1 /* DB_COLLATION_ASC */),
  19026.         CARDINALITY        = case when (x.status & 2) = 2 then x.rows else null end,
  19027.         PAGES            = convert(int, x.dpages),
  19028.         FILTER_CONDITION    = convert(varchar(1),null),
  19029.         INTEGRATED        = convert(bit,(x.status & 16)/16)
  19030.         
  19031.     from    sysobjects o, sysindexes x, syscolumns c, sysusers u
  19032.     where    o.type in ('U')
  19033.     and     o.name = @table_name
  19034.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  19035.     and    (@index_name is null or @index_name = x.name)
  19036.     and     x.id = o.id
  19037.     and     x.id = c.id
  19038.     and      c.colid < x.keycnt+(x.status&16)/16
  19039.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  19040.     and     (
  19041.         suser_id() = 1     /* User is the System Administrator */
  19042.         or o.uid = user_id()     /* User created the object */
  19043.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  19044.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  19045.             from sysprotects p
  19046.             where p.id = o.id
  19047.             /* get rows for public,current user,user's group */
  19048.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  19049.             /* check for SELECT,EXECUTE privilege */
  19050.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  19051.             ) = 1    /* final magic...compare Grants      */
  19052.            )
  19053.     order by 8 desc, 4, 5, 6, 17
  19054. go
  19055. dump tran master with no_log
  19056. go
  19057. create procedure sp_indexes_rowset;2
  19058.     (
  19059.     @index_name    varchar(255) = null,
  19060.     @table_schema    varchar(255) = null     
  19061.     )
  19062. as
  19063.     select    TABLE_CATALOG        = db_name(),
  19064.         TABLE_SCHEMA        = user_name(o.uid),
  19065.         TABLE_NAME        = o.name,
  19066.         INDEX_CATALOG        = db_name(),        
  19067.         INDEX_SCHEMA        = user_name(o.uid),
  19068.         INDEX_NAME        = x.name,
  19069.         PRIMARY_KEY        = convert(bit,(x.status & 0x800)/0x800),
  19070.         "UNIQUE"        = convert(bit,(x.status & 2)/2),
  19071.         "CLUSTERED"        = convert(bit,(x.status & 16)/16),
  19072.         "TYPE"            = convert(smallint, 1 /*DBPROPVAL_IT_BTREE*/),
  19073.         FILL_FACTOR        = convert(int, x.OrigFillFactor),
  19074.         INITIAL_SIZE        = convert(int,null),
  19075.         NULLS            = convert(int,null),
  19076.         SORT_BOOKMARKS        = convert(bit,0),
  19077.         AUTO_UPDATE        = convert(bit,1),
  19078.         NULL_COLLATION        = convert(int,4 /*DBPROPVAL_NC_LOW*/),
  19079.         ORDINAL_POSITION     = convert(int,
  19080.                        (
  19081.                         select count(*)
  19082.                         from syscolumns sc
  19083.                         where sc.id     =  c.id
  19084.                           AND sc.number =  c.number
  19085.                           AND sc.colid  <= c.colid
  19086.                         )),
  19087.         COLUMN_NAME        = index_col(user_name(o.uid)+'.'+o.name, x.indid, c.colid),
  19088.         COLUMN_GUID        = convert(binary(16),null),
  19089.         COLUMN_PROPID        = convert(int,null),
  19090.         COLLATION    = convert(smallint, 1 /* DB_COLLATION_ASC */),
  19091.         CARDINALITY        = case when (x.status & 2) = 2 then x.rows else null end,
  19092.         PAGES            = convert(int, x.dpages),
  19093.         FILTER_CONDITION    = convert(varchar(1),null),
  19094.         INTEGRATED        = convert(bit,(x.status & 16)/16) 
  19095.         
  19096.     from    sysobjects o, sysindexes x, syscolumns c, sysusers u
  19097.     where    o.type in ('U')
  19098.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  19099.     and    (@index_name is null or @index_name = x.name)
  19100.     and     x.id = o.id
  19101.     and     x.id = c.id
  19102.     and      c.colid < x.keycnt+(x.status&16)/16
  19103.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  19104.     and     (
  19105.         suser_id() = 1     /* User is the System Administrator */
  19106.         or o.uid = user_id()     /* User created the object */
  19107.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  19108.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  19109.             from sysprotects p
  19110.             where p.id = o.id
  19111.             /* get rows for public,current user,user's group */
  19112.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  19113.             /* check for SELECT,EXECUTE privilege */
  19114.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  19115.             ) = 1    /* final magic...compare Grants      */
  19116.            )
  19117.     order by 8 desc, 4, 5, 6, 17
  19118. go
  19119. dump tran master with no_log
  19120. go
  19121.  
  19122. if (charindex('7.00', @@version) > 0 or
  19123.     charindex('8.00', @@version) > 0)
  19124.     drop procedure sp_indexes_rowset
  19125. else
  19126. begin
  19127.     print ''
  19128.     print ''
  19129.     print 'Warning:'
  19130.     print 'you are installing the stored procedures '
  19131.     print 'on a pre 7.0 SQL Server.'
  19132.     print 'Ignore the following errors.'
  19133. end
  19134. go
  19135.  
  19136. /*    7.0 version */
  19137. create procedure sp_indexes_rowset
  19138.     (
  19139.     @table_name    sysname, 
  19140.     @index_name    sysname = null,
  19141.     @table_schema    sysname = null     
  19142.     )
  19143. as
  19144.     select    TABLE_CATALOG        = db_name(),
  19145.         TABLE_SCHEMA        = user_name(o.uid),
  19146.         TABLE_NAME        = o.name,
  19147.         INDEX_CATALOG        = db_name(),        
  19148.         INDEX_SCHEMA        = user_name(o.uid),
  19149.         INDEX_NAME        = x.name,
  19150.         PRIMARY_KEY        = convert(bit,(x.status & 0x800)/0x800),
  19151.         "UNIQUE"        = convert(bit,(x.status & 2)/2),
  19152.         "CLUSTERED"        = convert(bit,(x.status & 16)/16),
  19153.         "TYPE"            = convert(smallint, 1 /*DBPROPVAL_IT_BTREE*/),
  19154.         FILL_FACTOR        = convert(int, x.OrigFillFactor),
  19155.         INITIAL_SIZE        = convert(int,null),
  19156.         NULLS            = convert(int,null),
  19157.         SORT_BOOKMARKS        = convert(bit,0),
  19158.         AUTO_UPDATE        = convert(bit,1),
  19159.         NULL_COLLATION        = convert(int,4 /*DBPROPVAL_NC_LOW*/),
  19160.         ORDINAL_POSITION     = convert(int, xk.keyno),
  19161.         COLUMN_NAME        = c.name,
  19162.         COLUMN_GUID        = convert(uniqueidentifier,null),
  19163.         COLUMN_PROPID        = convert(int,null),
  19164.         COLLATION    = convert(smallint, 1 /* DB_COLLATION_ASC */),
  19165.         CARDINALITY        = case when (x.status & 2) = 2 then x.rows else null end,
  19166.         PAGES            = convert(int, x.dpages),
  19167.         FILTER_CONDITION    = convert(nvarchar(1),null),
  19168.         INTEGRATED        = convert(bit,(x.status & 16)/16) 
  19169.         
  19170.     from    sysobjects o, sysindexes x, syscolumns c, sysindexkeys xk
  19171.     where    o.type in ('U')
  19172.     and     o.name = @table_name
  19173.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  19174.     and    (@index_name is null or @index_name = x.name)
  19175.     and    x.id = o.id
  19176.     and    o.id = c.id
  19177.     and    o.id = xk.id
  19178.     and    x.indid = xk.indid
  19179.     and    c.colid = xk.colid
  19180.     and    xk.keyno <= x.keycnt
  19181.     and    permissions(o.id, c.name) <> 0
  19182.     and     (x.status&32) = 0  -- No hypothetical indexes
  19183.     order by 8 desc, 4, 5, 6, 17
  19184. go
  19185. dump tran master with no_log
  19186. go
  19187. create procedure sp_indexes_rowset;2
  19188.     (
  19189.     @index_name    sysname = null,
  19190.     @table_schema    sysname = null     
  19191.     )
  19192. as
  19193.     select    TABLE_CATALOG        = db_name(),
  19194.         TABLE_SCHEMA        = user_name(o.uid),
  19195.         TABLE_NAME        = o.name,
  19196.         INDEX_CATALOG        = db_name(),        
  19197.         INDEX_SCHEMA        = user_name(o.uid),
  19198.         INDEX_NAME        = x.name,
  19199.         PRIMARY_KEY        = convert(bit,(x.status & 0x800)/0x800),
  19200.         "UNIQUE"        = convert(bit,(x.status & 2)/2),
  19201.         "CLUSTERED"        = convert(bit,(x.status & 16)/16),
  19202.         "TYPE"            = convert(smallint, 1 /*DBPROPVAL_IT_BTREE*/),
  19203.         FILL_FACTOR        = convert(int, x.OrigFillFactor),
  19204.         INITIAL_SIZE        = convert(int,null),
  19205.         NULLS            = convert(int,null),
  19206.         SORT_BOOKMARKS        = convert(bit,0),
  19207.         AUTO_UPDATE        = convert(bit,1),
  19208.         NULL_COLLATION        = convert(int,4 /*DBPROPVAL_NC_LOW*/),
  19209.         ORDINAL_POSITION     = convert(int, xk.keyno),
  19210.         COLUMN_NAME        = c.name,
  19211.         COLUMN_GUID        = convert(uniqueidentifier,null),
  19212.         COLUMN_PROPID        = convert(int,null),
  19213.         COLLATION    = convert(smallint, 1 /* DB_COLLATION_ASC */),
  19214.         CARDINALITY        = case when (x.status & 2) = 2 then x.rows else null end,
  19215.         PAGES            = convert(int, x.dpages),
  19216.         FILTER_CONDITION    = convert(nvarchar(1),null),
  19217.         INTEGRATED        = convert(bit,(x.status & 16)/16) 
  19218.         
  19219.     from    sysobjects o, sysindexes x, syscolumns c, sysindexkeys xk
  19220.     where    o.type in ('U')
  19221.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  19222.     and    (@index_name is null or @index_name = x.name)
  19223.     and    x.id = o.id
  19224.     and    o.id = c.id
  19225.     and    o.id = xk.id
  19226.     and    x.indid = xk.indid
  19227.     and    c.colid = xk.colid
  19228.     and    xk.keyno <= x.keycnt
  19229.     and    permissions(o.id, c.name) <> 0
  19230.     and    (x.status&32) = 0  -- No hypothetical indexes
  19231.     order by 8 desc, 4, 5, 6, 17
  19232. go
  19233. dump tran master with no_log
  19234. go
  19235. create procedure sp_indexes_rowset;5
  19236.     (
  19237.     @table_server        sysname,
  19238.     @table_catalog        sysname = null,
  19239.     @table_name        sysname = null,
  19240.     @index_name        sysname = null,
  19241.     @table_schema        sysname = null
  19242.     )
  19243. as
  19244.     select
  19245.         TABLE_CATALOG,    
  19246.         TABLE_SCHEMA,    
  19247.         TABLE_NAME,    
  19248.         INDEX_CATALOG,        
  19249.         INDEX_SCHEMA,    
  19250.         INDEX_NAME,    
  19251.         PRIMARY_KEY,    
  19252.         "UNIQUE",    
  19253.         "CLUSTERED",    
  19254.         "TYPE",        
  19255.         FILL_FACTOR,    
  19256.         INITIAL_SIZE,    
  19257.         NULLS,        
  19258.         SORT_BOOKMARKS,    
  19259.         AUTO_UPDATE,    
  19260.         NULL_COLLATION,    
  19261.         ORDINAL_POSITION,
  19262.         COLUMN_NAME,    
  19263.         COLUMN_GUID,    
  19264.         COLUMN_PROPID,    
  19265.         COLLATION,    
  19266.         CARDINALITY,    
  19267.         PAGES,        
  19268.         FILTER_CONDITION
  19269.     --    INTEGRATED
  19270.     from master.dbo.SYSREMOTE_INDEXES <
  19271.                 @table_server,
  19272.                 @table_catalog,
  19273.                 @table_schema,
  19274.                 @index_name,
  19275.                 NULL,            /* TYPE (index type) */
  19276.                 @table_name >
  19277.     order by 8 desc, 4, 5, 6, 17
  19278. go
  19279.  
  19280. grant execute on sp_indexes_rowset to public
  19281. go
  19282.  
  19283. dump tran master with no_log
  19284. go
  19285.  
  19286. if (charindex('8.00', @@version) > 0)
  19287.     drop procedure sp_indexes_rowset
  19288. else
  19289. begin
  19290.     print ''
  19291.     print ''
  19292.     print 'Warning:'
  19293.     print 'you are installing the stored procedures '
  19294.     print 'on a pre 8.0 SQL Server.'
  19295.     print 'Ignore the following errors.'
  19296. end
  19297. go
  19298.  
  19299. /*    8.0 version */
  19300. create procedure sp_indexes_rowset
  19301.     (
  19302.     @table_name    sysname, 
  19303.     @index_name    sysname = null,
  19304.     @table_schema    sysname = null     
  19305.     )
  19306. as
  19307.     select    TABLE_CATALOG        = db_name(),
  19308.         TABLE_SCHEMA        = user_name(o.uid),
  19309.         TABLE_NAME        = o.name,
  19310.         INDEX_CATALOG        = db_name(),        
  19311.         INDEX_SCHEMA        = user_name(o.uid),
  19312.         INDEX_NAME        = x.name,
  19313.         PRIMARY_KEY        = convert(bit,(x.status & 0x800)/0x800),
  19314.         "UNIQUE"        = convert(bit,(x.status & 2)/2),
  19315.         "CLUSTERED"        = convert(bit,(x.status & 16)/16),
  19316.         "TYPE"            = convert(smallint, 1 /*DBPROPVAL_IT_BTREE*/),
  19317.         FILL_FACTOR        = convert(int, x.OrigFillFactor),
  19318.         INITIAL_SIZE        = convert(int,null),
  19319.         NULLS            = convert(int,null),
  19320.         SORT_BOOKMARKS        = convert(bit,0),
  19321.         AUTO_UPDATE        = convert(bit,1),
  19322.         NULL_COLLATION        = convert(int,4 /*DBPROPVAL_NC_LOW*/),
  19323.         ORDINAL_POSITION     = convert(int, xk.keyno),
  19324.         COLUMN_NAME        = c.name,
  19325.         COLUMN_GUID        = convert(uniqueidentifier,null),
  19326.         COLUMN_PROPID        = convert(int,null),
  19327.         COLLATION    = convert(smallint,
  19328.                         case when indexkey_property(o.id, x.indid, xk.keyno, 'IsDescending') =1
  19329.                         then 2        /* DB_COLLATION_DESC */ 
  19330.                         else 1        /* DB_COLLATION_ASC */ 
  19331.                         end),
  19332.         CARDINALITY        = case when (x.status & 2) = 2 then x.rows else null end,
  19333.         PAGES            = convert(int, x.dpages),
  19334.         FILTER_CONDITION    = convert(nvarchar(1),null),
  19335.         INTEGRATED        = convert(bit,(x.status & 16)/16) 
  19336.         
  19337.     from    sysobjects o, sysindexes x, syscolumns c, sysindexkeys xk
  19338.     where    o.type in ('U')
  19339.     and     o.name = @table_name
  19340.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  19341.     and    (@index_name is null or @index_name = x.name)
  19342.     and    x.id = o.id
  19343.     and    o.id = c.id
  19344.     and    o.id = xk.id
  19345.     and    x.indid = xk.indid
  19346.     and    c.colid = xk.colid
  19347.     and    xk.keyno <= x.keycnt
  19348.     and    permissions(o.id, c.name) <> 0
  19349.     and     (x.status&32) = 0  -- No hypothetical indexes
  19350.     order by 8 desc, 4, 5, 6, 17
  19351. go
  19352. dump tran master with no_log
  19353. go
  19354. create procedure sp_indexes_rowset;2
  19355.     (
  19356.     @index_name    sysname = null,
  19357.     @table_schema    sysname = null     
  19358.     )
  19359. as
  19360.     select    TABLE_CATALOG        = db_name(),
  19361.         TABLE_SCHEMA        = user_name(o.uid),
  19362.         TABLE_NAME        = o.name,
  19363.         INDEX_CATALOG        = db_name(),        
  19364.         INDEX_SCHEMA        = user_name(o.uid),
  19365.         INDEX_NAME        = x.name,
  19366.         PRIMARY_KEY        = convert(bit,(x.status & 0x800)/0x800),
  19367.         "UNIQUE"        = convert(bit,(x.status & 2)/2),
  19368.         "CLUSTERED"        = convert(bit,(x.status & 16)/16),
  19369.         "TYPE"            = convert(smallint, 1 /*DBPROPVAL_IT_BTREE*/),
  19370.         FILL_FACTOR        = convert(int, x.OrigFillFactor),
  19371.         INITIAL_SIZE        = convert(int,null),
  19372.         NULLS            = convert(int,null),
  19373.         SORT_BOOKMARKS        = convert(bit,0),
  19374.         AUTO_UPDATE        = convert(bit,1),
  19375.         NULL_COLLATION        = convert(int,4 /*DBPROPVAL_NC_LOW*/),
  19376.         ORDINAL_POSITION     = convert(int, xk.keyno),
  19377.         COLUMN_NAME        = c.name,
  19378.         COLUMN_GUID        = convert(uniqueidentifier,null),
  19379.         COLUMN_PROPID        = convert(int,null),
  19380.         COLLATION    = convert(smallint,
  19381.                         case when indexkey_property(o.id, x.indid, xk.keyno, 'IsDescending') =1
  19382.                         then 2        /* DB_COLLATION_DESC */ 
  19383.                         else 1        /* DB_COLLATION_ASC */ 
  19384.                         end),
  19385.         CARDINALITY        = case when (x.status & 2) = 2 then x.rows else null end,
  19386.         PAGES            = convert(int, x.dpages),
  19387.         FILTER_CONDITION    = convert(nvarchar(1),null),
  19388.         INTEGRATED        = convert(bit,(x.status & 16)/16) 
  19389.         
  19390.     from    sysobjects o, sysindexes x, syscolumns c, sysindexkeys xk
  19391.     where    o.type in ('U')
  19392.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  19393.     and    (@index_name is null or @index_name = x.name)
  19394.     and    x.id = o.id
  19395.     and    o.id = c.id
  19396.     and    o.id = xk.id
  19397.     and    x.indid = xk.indid
  19398.     and    c.colid = xk.colid
  19399.     and    xk.keyno <= x.keycnt
  19400.     and    permissions(o.id, c.name) <> 0
  19401.     and    (x.status&32) = 0  -- No hypothetical indexes
  19402.     order by 8 desc, 4, 5, 6, 17
  19403. go
  19404. dump tran master with no_log
  19405. go
  19406. create procedure sp_indexes_rowset;5
  19407.     (
  19408.     @table_server        sysname,
  19409.     @table_catalog        sysname = null,
  19410.     @table_name        sysname = null,
  19411.     @index_name        sysname = null,
  19412.     @table_schema        sysname = null
  19413.     )
  19414. as
  19415.     select
  19416.         TABLE_CATALOG,    
  19417.         TABLE_SCHEMA,    
  19418.         TABLE_NAME,    
  19419.         INDEX_CATALOG,        
  19420.         INDEX_SCHEMA,    
  19421.         INDEX_NAME,    
  19422.         PRIMARY_KEY,    
  19423.         "UNIQUE",    
  19424.         "CLUSTERED",    
  19425.         "TYPE",        
  19426.         FILL_FACTOR,    
  19427.         INITIAL_SIZE,    
  19428.         NULLS,        
  19429.         SORT_BOOKMARKS,    
  19430.         AUTO_UPDATE,    
  19431.         NULL_COLLATION,    
  19432.         ORDINAL_POSITION,
  19433.         COLUMN_NAME,    
  19434.         COLUMN_GUID,    
  19435.         COLUMN_PROPID,    
  19436.         COLLATION,    
  19437.         CARDINALITY,    
  19438.         PAGES,        
  19439.         FILTER_CONDITION
  19440.     --    INTEGRATED
  19441.     from master.dbo.SYSREMOTE_INDEXES <
  19442.                 @table_server,
  19443.                 @table_catalog,
  19444.                 @table_schema,
  19445.                 @index_name,
  19446.                 NULL,            /* TYPE (index type) */
  19447.                 @table_name >
  19448.     order by 8 desc, 4, 5, 6, 17
  19449. go
  19450.  
  19451. grant execute on sp_indexes_rowset to public
  19452. go
  19453.  
  19454. dump tran master with no_log
  19455. go
  19456. if (charindex('6.00', @@version) > 0)
  19457.     begin
  19458.     if (exists (select * from sysobjects
  19459.             where name = 'sp_indexes_rowset' and type = 'P '))
  19460.         begin
  19461.         drop procedure sp_indexes_rowset
  19462.         dump tran master with no_log
  19463.         end
  19464.     end
  19465. go
  19466.  
  19467.  
  19468. print ''
  19469. print 'creating sp_primary_keys_rowset'
  19470. go
  19471.  
  19472. /*    Procedure for 6.0 and 6.5 servers */
  19473. create procedure sp_primary_keys_rowset
  19474.     (
  19475.        @table_name        varchar(255),
  19476.     @table_schema         varchar(244) = null
  19477.     )
  19478. as
  19479.     select    TABLE_CATALOG    = db_name(),
  19480.             TABLE_SCHEMA    = user_name(o.uid),
  19481.             TABLE_NAME        = o.name,    
  19482.             COLUMN_NAME        = c.name,
  19483.             COLUMN_GUID        = convert(binary(16),null),
  19484.             COLUMN_PROPID    = convert(int,null),
  19485.             ORDINAL            = convert(int,c1.colid),
  19486.             PK_NAME            = i.name
  19487.     from    sysindexes i, syscolumns c, sysobjects o, syscolumns c1, sysusers u
  19488.     where    o.type in ('U')
  19489.     and     o.name = @table_name
  19490.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  19491.     and     o.id = c.id
  19492.     and     o.id = i.id
  19493.     and     (i.status & 0x800) = 0x800
  19494.     and     c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, c1.colid)
  19495.     and     c1.id = c.id
  19496.     and     c1.colid <= i.keycnt    /* create rows from 1 to keycnt */
  19497.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  19498.     and     (
  19499.         suser_id() = 1     /* User is the System Administrator */
  19500.         or o.uid = user_id()     /* User created the object */
  19501.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  19502.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  19503.             from sysprotects p
  19504.             where p.id = o.id
  19505.             /* get rows for public,current user,user's group */
  19506.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  19507.             /* check for SELECT,EXECUTE privilege */
  19508.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  19509.             ) = 1    /* final magic...compare Grants      */
  19510.            )
  19511.     order by 2, 3
  19512. go
  19513. dump tran master with no_log
  19514. go
  19515. create procedure sp_primary_keys_rowset;2
  19516.     (
  19517.      @table_schema         varchar(244) = null
  19518.     )
  19519. as
  19520.     select    TABLE_CATALOG    = db_name(),
  19521.             TABLE_SCHEMA    = user_name(o.uid),
  19522.             TABLE_NAME        = o.name,    
  19523.             COLUMN_NAME        = c.name,
  19524.             COLUMN_GUID        = convert(binary(16),null),
  19525.             COLUMN_PROPID    = convert(int,null),
  19526.             ORDINAL            = convert(int,c1.colid),
  19527.             PK_NAME            = i.name
  19528.     from    sysindexes i, syscolumns c, sysobjects o, syscolumns c1, sysusers u
  19529.     where    o.type in ('U')
  19530.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  19531.     and     o.id = c.id
  19532.     and     o.id = i.id
  19533.     and     (i.status & 0x800) = 0x800
  19534.     and     c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, c1.colid)
  19535.     and     c1.id = c.id
  19536.     and     c1.colid <= i.keycnt    /* create rows from 1 to keycnt */
  19537.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  19538.     and     (
  19539.         suser_id() = 1     /* User is the System Administrator */
  19540.         or o.uid = user_id()     /* User created the object */
  19541.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  19542.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  19543.             from sysprotects p
  19544.             where p.id = o.id
  19545.             /* get rows for public,current user,user's group */
  19546.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  19547.             /* check for SELECT,EXECUTE privilege */
  19548.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  19549.             ) = 1    /* final magic...compare Grants      */
  19550.            )
  19551.     order by 2, 3
  19552. go
  19553.  
  19554. if (charindex('7.00', @@version) = 0 and
  19555.     charindex('8.00', @@version) = 0)
  19556. begin
  19557.     print ''
  19558.     print ''
  19559.     print 'Warning:'
  19560.     print 'you are installing the stored procedures '
  19561.     print 'on a pre 8.0 SQL Server.'
  19562.     print 'Ignore the following errors.'
  19563. end
  19564. else
  19565.     drop proc sp_primary_keys_rowset
  19566. go
  19567.  
  19568. /*    Procedure for 8.0 servers */
  19569. create procedure sp_primary_keys_rowset
  19570.     (
  19571.        @table_name        sysname,
  19572.     @table_schema         sysname = null
  19573.     )
  19574. as
  19575.     select    TABLE_CATALOG    = db_name(),
  19576.             TABLE_SCHEMA    = user_name(o.uid),
  19577.             TABLE_NAME        = o.name,    
  19578.             COLUMN_NAME        = c.name,
  19579.             COLUMN_GUID        = convert(uniqueidentifier,null),
  19580.             COLUMN_PROPID    = convert(int,null),
  19581.             ORDINAL            = convert(int,c1.colid),
  19582.             PK_NAME            = i.name
  19583.     from    sysindexes i, syscolumns c, sysobjects o, syscolumns c1
  19584.     where    o.type in ('U')
  19585.     and     o.name = @table_name
  19586.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  19587.     and     o.id = c.id
  19588.     and     o.id = i.id
  19589.     and     (i.status & 0x800) = 0x800
  19590.     and     c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, c1.colid)
  19591.     and     c1.id = c.id
  19592.     and     c1.colid <= i.keycnt    /* create rows from 1 to keycnt */
  19593.     and    permissions(o.id) <> 0
  19594.     order by 2, 3
  19595. go
  19596. dump tran master with no_log
  19597. go
  19598. create procedure sp_primary_keys_rowset;2
  19599.     (
  19600.      @table_schema     sysname = null
  19601.     )
  19602. as
  19603.     select    TABLE_CATALOG    = db_name(),
  19604.             TABLE_SCHEMA    = user_name(o.uid),
  19605.             TABLE_NAME        = o.name,    
  19606.             COLUMN_NAME        = c.name,
  19607.             COLUMN_GUID        = convert(uniqueidentifier,null),
  19608.             COLUMN_PROPID    = convert(int,null),
  19609.             ORDINAL            = convert(int,c1.colid),
  19610.             PK_NAME            = i.name
  19611.     from    sysindexes i, syscolumns c, sysobjects o, syscolumns c1
  19612.     where    o.type in ('U')
  19613.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  19614.     and     o.id = c.id
  19615.     and     o.id = i.id
  19616.     and     (i.status & 0x800) = 0x800
  19617.     and     c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, c1.colid)
  19618.     and     c1.id = c.id
  19619.     and     c1.colid <= i.keycnt    /* create rows from 1 to keycnt */
  19620.     and    permissions(o.id) <> 0
  19621.     order by 2, 3
  19622. go
  19623. dump tran master with no_log
  19624. go
  19625. create procedure sp_primary_keys_rowset;3
  19626.     (
  19627.        @table_name        sysname = null,
  19628.     @table_schema         sysname = null
  19629.     )
  19630. as
  19631. IF @table_name is not NULL
  19632.     BEGIN
  19633.     select    TABLE_CATALOG    = db_name(),
  19634.             TABLE_SCHEMA    = user_name(o.uid),
  19635.             TABLE_NAME        = o.name,    
  19636.             COLUMN_NAME        = c.name,
  19637.             COLUMN_GUID        = convert(uniqueidentifier,null),
  19638.             COLUMN_PROPID    = convert(int,null),
  19639.             ORDINAL            = convert(int,c1.colid),
  19640.             PK_NAME            = i.name
  19641.     from    sysindexes i, syscolumns c, sysobjects o, syscolumns c1
  19642.     where    o.type in ('U')
  19643.     and     o.name = @table_name
  19644.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  19645.     and     o.id = c.id
  19646.     and     o.id = i.id
  19647.     and     (i.status & 0x800) = 0x800
  19648.     and     c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, c1.colid)
  19649.     and     c1.id = c.id
  19650.     and     c1.colid <= i.keycnt    /* create rows from 1 to keycnt */
  19651.     and    permissions(o.id) <> 0
  19652.     END
  19653. ELSE
  19654.     BEGIN
  19655.     select    TABLE_CATALOG    = db_name(),
  19656.             TABLE_SCHEMA    = user_name(o.uid),
  19657.             TABLE_NAME        = o.name,    
  19658.             COLUMN_NAME        = c.name,
  19659.             COLUMN_GUID        = convert(uniqueidentifier,null),
  19660.             COLUMN_PROPID    = convert(int,null),
  19661.             ORDINAL            = convert(int,c1.colid),
  19662.             PK_NAME            = i.name
  19663.     from    sysindexes i, syscolumns c, sysobjects o, syscolumns c1
  19664.     where    o.type in ('U')
  19665.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  19666.     and     o.id = c.id
  19667.     and     o.id = i.id
  19668.     and     (i.status & 0x800) = 0x800
  19669.     and     c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, c1.colid)
  19670.     and     c1.id = c.id
  19671.     and     c1.colid <= i.keycnt    /* create rows from 1 to keycnt */
  19672.     and    permissions(o.id) <> 0
  19673.     END
  19674. go
  19675. dump tran master with no_log
  19676. go
  19677. create procedure sp_primary_keys_rowset;5
  19678.     (
  19679.     @table_server        sysname,
  19680.     @table_catalog        sysname = null,
  19681.     @table_name            sysname = null,
  19682.     @table_schema        sysname = null
  19683.     )
  19684. as
  19685.     select
  19686.         TABLE_CATALOG,    
  19687.         TABLE_SCHEMA,    
  19688.         TABLE_NAME,    
  19689.         COLUMN_NAME,    
  19690.         COLUMN_GUID,    
  19691.         COLUMN_PROPID,    
  19692.         ORDINAL
  19693.         --PK_NAME        
  19694.     from master.dbo.SYSREMOTE_PRIMARY_KEYS <
  19695.                 @table_server,
  19696.                 @table_catalog,
  19697.                 @table_schema,
  19698.                 @table_name >
  19699.     order by 1,2,3
  19700. go
  19701.  
  19702. grant execute on sp_primary_keys_rowset to public
  19703. go
  19704.  
  19705. dump tran master with no_log
  19706. go
  19707. if (charindex('6.00', @@version) > 0)
  19708.     begin
  19709.     if (exists (select * from sysobjects
  19710.             where name = 'sp_primary_keys_rowset' and type = 'P '))
  19711.         begin
  19712.         drop procedure sp_primary_keys_rowset
  19713.         dump tran master with no_log
  19714.         end
  19715.     end
  19716. go
  19717.  
  19718.  
  19719. print ''
  19720. print 'creating sp_provider_types_rowset'
  19721. go
  19722.  
  19723. /*    Procedure for 6.0 and 6.50 servers */
  19724. create proc sp_provider_types_rowset
  19725.     (
  19726.     @data_type     smallint = null,
  19727.     @best_match    tinyint  = null
  19728.     )
  19729. as
  19730.     select
  19731.         TYPE_NAME         = case when t.usertype = 80 then t.name 
  19732.                         else d.type_name 
  19733.                         end,
  19734.         DATA_TYPE         = d.oledb_data_type,                            
  19735.         COLUMN_SIZE        = case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ 
  19736.                             then @@max_precision
  19737.                             else coalesce(d.column_size,d.data_precision,t.prec) end,
  19738.         LITERAL_PREFIX         = d.literal_prefix,
  19739.         LITERAL_SUFFIX         = d.literal_suffix,
  19740.         CREATE_PARAMS         = convert(varchar(32),e.CREATE_PARAMS),
  19741.         IS_NULLABLE        = t.allownulls,
  19742.         CASE_SENSITIVE        = d.case_sensitive,
  19743.         SEARCHABLE         = d.searchable,
  19744.         UNSIGNED_ATTRIBUTE    = d.unsigned_attribute,
  19745.         FIXED_PREC_SCALE    = d.fixed_prec_scale,
  19746.         AUTO_UNIQUE_VALUE    = d.auto_unique_value, 
  19747.         LOCAL_TYPE_NAME        = case    when t.usertype = 80 then t.name 
  19748.                         else d.local_type_name
  19749.                         end,
  19750.         MINIMUM_SCALE        = convert(smallint,
  19751.                         case 
  19752.                         when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then 0 
  19753.                         else null 
  19754.                         end),
  19755.         MAXIMUM_SCALE        = convert(smallint,
  19756.                         case 
  19757.                         when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ 
  19758.                         then @@max_precision
  19759.                         else null 
  19760.                         end),
  19761.         GUID            = convert(binary(16),null),
  19762.         TYPELIB            = convert(varchar(1),null),
  19763.         VERSION            = convert(varchar(1),null),
  19764.         IS_LONG            = d.is_long,
  19765.         BEST_MATCH        = case when t.usertype = 80
  19766.                         then convert(bit,0)
  19767.                         else d.best_match
  19768.                         end,
  19769.         IS_FIXEDLENGTH        = convert(bit, 
  19770.                         case when d.fixlen is null then 0 else 1 end)
  19771.     from     master.dbo.spt_provider_types d, master.dbo.spt_datatype_info_ext e, systypes t
  19772.     where    d.ss_dtype = t.type 
  19773.     and    t.usertype <= 100
  19774.     and    t.usertype <> 18 /* sysname */
  19775.     and    (case when t.usertype = 80 /* TIMESTAMP */ then 1 else 0 end 
  19776.             = case when d.type_name = 'timestamp' then 1 else 0 end)
  19777.     and     t.usertype *= e.user_type 
  19778.     and     e.AUTO_INCREMENT = 0
  19779.     and     t.type not in (111,109,38,110,55,63)    /* get rid of nullable types */
  19780.     and    (@data_type is null or d.oledb_data_type = @data_type)
  19781.     and    (@best_match is null or d.best_match = @best_match)
  19782.     order by 2
  19783. go
  19784. dump tran master with no_log
  19785. go
  19786.  
  19787. if (charindex('7.00', @@version) = 0 and
  19788.     charindex('8.00', @@version) = 0)
  19789. begin
  19790.     print ''
  19791.     print ''
  19792.     print 'Warning:'
  19793.     print 'you are installing the stored procedures '
  19794.     print 'on a pre 8.0 SQL Server.'
  19795.     print 'Ignore the following errors.'
  19796. end
  19797. else
  19798.     drop proc sp_provider_types_rowset
  19799. go
  19800.  
  19801. /*    Procedure for 8.0 server */
  19802. create proc sp_provider_types_rowset
  19803.     (
  19804.     @data_type     smallint = null,
  19805.     @best_match    tinyint  = null
  19806.     )
  19807. as
  19808.     select
  19809.         TYPE_NAME         = case    when t.usertype = 80 then t.name 
  19810.                         else d.type_name 
  19811.                         end,
  19812.         DATA_TYPE         = d.oledb_data_type,                            
  19813.  
  19814.  
  19815.         COLUMN_SIZE        = case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ 
  19816.                             then @@max_precision
  19817.                             else coalesce(d.column_size,d.data_precision,t.prec) end,
  19818.         LITERAL_PREFIX         = d.literal_prefix,
  19819.         LITERAL_SUFFIX         = d.literal_suffix,
  19820.         CREATE_PARAMS         = convert(nvarchar(32),e.CREATE_PARAMS),
  19821.         IS_NULLABLE        = t.allownulls,
  19822.         CASE_SENSITIVE        = d.case_sensitive,
  19823.         SEARCHABLE         = d.searchable,
  19824.         UNSIGNED_ATTRIBUTE    = d.unsigned_attribute,
  19825.         FIXED_PREC_SCALE    = d.fixed_prec_scale,
  19826.         AUTO_UNIQUE_VALUE    = d.auto_unique_value, 
  19827.         LOCAL_TYPE_NAME        = case    when t.usertype = 80 then t.name 
  19828.                         else d.local_type_name
  19829.                         end,
  19830.         MINIMUM_SCALE        = convert(smallint,
  19831.                         case 
  19832.                         when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then 0 
  19833.                         else null 
  19834.                         end),
  19835.         MAXIMUM_SCALE        = convert(smallint,
  19836.                         case 
  19837.                         when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ 
  19838.                         then @@max_precision
  19839.                         else null 
  19840.                         end),
  19841.         GUID            = convert(uniqueidentifier,null),
  19842.         TYPELIB            = convert(nvarchar(1),null),
  19843.         VERSION            = convert(nvarchar(1),null),
  19844.         IS_LONG            = d.is_long,
  19845.         BEST_MATCH        = case    when t.usertype = 80
  19846.                         then convert(bit,0)
  19847.                         else d.best_match
  19848.                         end,
  19849.         IS_FIXEDLENGTH        = convert(bit, 
  19850.                         case when d.fixlen is null then 0 else 1 end)
  19851.  
  19852.     from master.dbo.spt_provider_types d
  19853.         INNER JOIN master.dbo.systypes t on d.ss_dtype = t.xtype
  19854.         LEFT OUTER JOIN master.dbo.spt_datatype_info_ext e on
  19855.             t.xusertype = e.user_type
  19856.             and e.AUTO_INCREMENT = 0
  19857.     where    (@data_type is null or d.oledb_data_type = @data_type)
  19858.     and    (@best_match is null or d.best_match = @best_match)    
  19859.     and    t.usertype <= 255
  19860.     and    t.usertype <> 18 /* sysname */
  19861.     order by 2
  19862. go
  19863.  
  19864. grant execute on sp_provider_types_rowset to public
  19865. go
  19866.  
  19867. dump tran master with no_log
  19868. go
  19869. if (charindex('6.00', @@version) > 0)
  19870.     begin
  19871.     if (exists (select * from sysobjects
  19872.         where name = 'sp_provider_types_rowset' and type = 'P '))
  19873.         begin
  19874.         drop procedure sp_provider_types_rowset
  19875.         dump tran master with no_log
  19876.         end
  19877.     end
  19878. go
  19879.  
  19880.  
  19881. print ''
  19882. print 'creating sp_procedure_params_rowset'
  19883. go
  19884.  
  19885. /*    Procedure for 6.0 and 6.50 servers */
  19886. create procedure sp_procedure_params_rowset
  19887.     (
  19888.        @procedure_name        varchar(255) = null,
  19889.     @group_number        int = null,
  19890.     @procedure_schema     varchar(255) = null,
  19891.     @parameter_name        varchar(255) = null
  19892.     )
  19893. as
  19894. IF @procedure_name is not null
  19895.     BEGIN
  19896.     select
  19897.         PROCEDURE_CATALOG     = db_name(),
  19898.         PROCEDURE_SCHEMA     = user_name(o.uid),
  19899.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  19900.         PARAMETER_NAME         = c.name,
  19901.         ORDINAL_POSITION     = convert(smallint, c.colid),
  19902.         PARAMETER_TYPE         = convert(smallint, 1+((c.status/64)&1)),
  19903.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  19904.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  19905.         IS_NULLABLE        = convert(bit,
  19906.                         case when d.oledb_data_type = 11 /*DBTYPE_BOOL*/ 
  19907.                         then 0 else 1
  19908.                         end),
  19909.         DATA_TYPE        = d.oledb_data_type,
  19910.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  19911.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  19912.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  19913.                         then coalesce(d.column_size,c.length)
  19914.                         else null 
  19915.                         end),
  19916.         CHARACTER_OCTET_LENGTH    = convert(int,
  19917.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  19918.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  19919.                         then coalesce(d.column_size,c.length)
  19920.                         else null 
  19921.                         end),
  19922.         NUMERIC_PRECISION    = convert(smallint,
  19923.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec 
  19924.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  19925.                             then d.data_precision else null end),
  19926.         NUMERIC_SCALE        = convert(smallint, 
  19927.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  19928.         DESCRIPTION        = convert(varchar(1),null),
  19929.         TYPE_NAME        = d.type_name,
  19930.         LOCAL_TYPE_NAME        = d.local_type_name
  19931.     
  19932.     from
  19933.         syscolumns c,
  19934.         sysobjects o,
  19935.         master.dbo.spt_provider_types d,
  19936.         systypes t
  19937.     where
  19938.         o.name = @procedure_name
  19939.     and    o.type = 'P'                            /* Just Procedures */
  19940.     and    (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  19941.     and    o.id = c.id
  19942.     and    c.number = @group_number
  19943.     and    c.type = d.ss_dtype
  19944.     and    c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  19945.     and    c.usertype = t.usertype
  19946.     and    (t.usertype != 80 or d.type_name='timestamp')
  19947.     and    (@parameter_name is null or @parameter_name = c.name)
  19948.     UNION ALL
  19949.     SELECT           /* return value row*/
  19950.         PROCEDURE_CATALOG     = db_name(),
  19951.         PROCEDURE_SCHEMA     = user_name(o.uid),
  19952.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  19953.         PARAMETER_NAME         = convert(sysname,'@RETURN_VALUE'),
  19954.         ORDINAL_POSITION     = convert(smallint,0),
  19955.         PARAMETER_TYPE         = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  19956.         PARAMETER_HASDEFAULT    = convert(tinyint,0),
  19957.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  19958.         IS_NULLABLE        = convert(bit,0),
  19959.         DATA_TYPE        = convert(smallint, 3 /*DBTYPE_I4*/),
  19960.         CHARACTER_MAXIMUM_LENGTH= convert(int,null),
  19961.         CHARACTER_OCTET_LENGTH    = convert(int,null),
  19962.         NUMERIC_PRECISION    = convert(smallint,10),
  19963.         NUMERIC_SCALE        = convert(smallint,null),
  19964.         DESCRIPTION        = convert(varchar(1),null),
  19965.         TYPE_NAME        = convert(sysname,'int'),
  19966.         LOCAL_TYPE_NAME        = convert(sysname,'int')
  19967.     from
  19968.         syscomments c,
  19969.         sysobjects o
  19970.     where
  19971.         o.name = @procedure_name
  19972.     and    o.type = 'P'                        /* Just Procedures */
  19973.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  19974.     and    (@parameter_name is null or @parameter_name = '@RETURN_VALUE')
  19975.     and    c.id = o.id
  19976.     and    c.number = @group_number
  19977.     and c.colid = 1
  19978.     order by 2, 3, 5
  19979.     END
  19980. ELSE
  19981.     BEGIN
  19982.     select
  19983.         PROCEDURE_CATALOG     = db_name(),
  19984.         PROCEDURE_SCHEMA     = user_name(o.uid),
  19985.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  19986.         PARAMETER_NAME         = c.name,
  19987.         ORDINAL_POSITION     = convert(smallint, c.colid),
  19988.         PARAMETER_TYPE         = convert(smallint, 1+((c.status/64)&1)),
  19989.         PARAMETER_HASDEFAULT    = convert(tinyint,0),
  19990.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  19991.         IS_NULLABLE        = convert(bit,
  19992.                         case when d.oledb_data_type = 11 /*DBTYPE_BOOL*/ 
  19993.                         then 0 else 1
  19994.                         end),
  19995.         DATA_TYPE        = d.oledb_data_type,
  19996.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  19997.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  19998.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  19999.                         then coalesce(d.column_size,c.length)
  20000.                         else null 
  20001.                         end),
  20002.         CHARACTER_OCTET_LENGTH    = convert(int,
  20003.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  20004.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  20005.                         then coalesce(d.column_size,c.length)
  20006.                         else null 
  20007.                         end),
  20008.         NUMERIC_PRECISION    = convert(smallint,
  20009.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  20010.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  20011.                             then d.data_precision else null end),
  20012.         NUMERIC_SCALE        = convert(smallint, 
  20013.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  20014.         DESCRIPTION        = convert(varchar(1),null),
  20015.         TYPE_NAME        = d.type_name,
  20016.         LOCAL_TYPE_NAME        = d.local_type_name
  20017.     
  20018.     from
  20019.         syscolumns c,
  20020.         sysobjects o,
  20021.         master.dbo.spt_provider_types d,
  20022.         systypes t
  20023.     where
  20024.         o.type = 'P'                            /* Just Procedures */
  20025.     and    (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  20026.     and    o.id = c.id
  20027.     and    c.type = d.ss_dtype
  20028.     and    c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  20029.     and    c.usertype = t.usertype
  20030.     and    (t.usertype != 80 or d.type_name='timestamp')
  20031.     and    (@parameter_name is null or @parameter_name = c.name)
  20032.     UNION ALL
  20033.     SELECT           /* return value row*/
  20034.         PROCEDURE_CATALOG     = db_name(),
  20035.         PROCEDURE_SCHEMA     = user_name(o.uid),
  20036.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  20037.         PARAMETER_NAME         = convert(sysname,'@RETURN_VALUE'),
  20038.         ORDINAL_POSITION     = convert(smallint, c.colid),
  20039.         PARAMETER_TYPE         = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  20040.         PARAMETER_HASDEFAULT    = convert(tinyint,0),
  20041.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  20042.         IS_NULLABLE        = convert(bit,0),
  20043.         DATA_TYPE        = convert(smallint, 3 /*DBTYPE_I4*/),
  20044.         CHARACTER_MAXIMUM_LENGTH= convert(int,null),
  20045.         CHARACTER_OCTET_LENGTH    = convert(int,null),
  20046.         NUMERIC_PRECISION    = convert(smallint,10),
  20047.         NUMERIC_SCALE        = convert(smallint,null),
  20048.         DESCRIPTION        = convert(varchar(1),null),
  20049.         TYPE_NAME        = convert(sysname,'int'),
  20050.         LOCAL_TYPE_NAME        = convert(sysname,'int')
  20051.     from
  20052.         syscomments c,
  20053.         sysobjects o
  20054.     where
  20055.         o.type = 'P'                        /* Just Procedures */
  20056.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  20057.     and    (@parameter_name is null or @parameter_name = '@RETURN_VALUE')
  20058.     and    c.id = o.id
  20059.     and     c.colid = 1
  20060.     order by 2, 3, 5
  20061.     END
  20062. go
  20063. dump tran master with no_log
  20064. go
  20065. create procedure sp_procedure_params_rowset;2
  20066.     (
  20067.       @handle            int output,
  20068.        @scrollopt        int output,
  20069.     @ccopt            int output,
  20070.     @rows            int output,
  20071.        @procedure_name        varchar(255) = null,
  20072.     @group_number        int = null,
  20073.     @procedure_schema     varchar(255) = null,
  20074.     @parameter_name        varchar(255) = null
  20075.     )
  20076. as
  20077.     declare @ret int
  20078.  
  20079.  
  20080. SET NOCOUNT ON
  20081.  
  20082.     create table #spprocparamrowset1
  20083.         (
  20084.         PROCEDURE_CATALOG     sysname not null,
  20085.         PROCEDURE_SCHEMA     sysname not null,
  20086.         PROCEDURE_NAME         varchar(35) not null,
  20087.         PARAMETER_NAME         sysname not null,
  20088.         ORDINAL_POSITION     smallint not null,
  20089.         PARAMETER_TYPE         smallint null,
  20090.         PARAMETER_HASDEFAULT    tinyint null,
  20091.         PARAMETER_DEFAULT    varchar(255) null,
  20092.         IS_NULLABLE        bit not null,
  20093.         DATA_TYPE        smallint null,
  20094.         CHARACTER_MAXIMUM_LENGTH int null,
  20095.         CHARACTER_OCTET_LENGTH    int null,
  20096.         NUMERIC_PRECISION    smallint null,
  20097.         NUMERIC_SCALE        smallint null,
  20098.         DESCRIPTION        varchar(1) null,
  20099.         TYPE_NAME        sysname null,
  20100.         LOCAL_TYPE_NAME        sysname null,
  20101.         )
  20102.  
  20103. IF @procedure_name is not null
  20104.     BEGIN
  20105.     insert into #spprocparamrowset1
  20106.     select
  20107.         PROCEDURE_CATALOG     = db_name(),
  20108.         PROCEDURE_SCHEMA     = user_name(o.uid),
  20109.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  20110.         PARAMETER_NAME         = c.name,
  20111.         ORDINAL_POSITION     = convert(smallint, c.colid),
  20112.         PARAMETER_TYPE         = convert(smallint, 1+((c.status/64)&1)),
  20113.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  20114.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  20115.         IS_NULLABLE        = convert(bit,
  20116.                         case when d.oledb_data_type = 11 /*DBTYPE_BOOL*/ 
  20117.                         then 0 else 1
  20118.                         end),
  20119.         DATA_TYPE        = d.oledb_data_type,
  20120.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  20121.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  20122.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  20123.                         then coalesce(d.column_size,c.length)
  20124.                         else null 
  20125.                         end),
  20126.         CHARACTER_OCTET_LENGTH    = convert(int,
  20127.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  20128.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  20129.                         then coalesce(d.column_size,c.length)
  20130.                         else null 
  20131.                         end),
  20132.         NUMERIC_PRECISION    = convert(smallint,
  20133.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  20134.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  20135.                             then d.data_precision else null end),
  20136.         NUMERIC_SCALE        = convert(smallint, 
  20137.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  20138.         DESCRIPTION        = convert(varchar(1),null),
  20139.         TYPE_NAME        = d.type_name,
  20140.         LOCAL_TYPE_NAME        = d.local_type_name
  20141.     
  20142.     from
  20143.         syscolumns c,
  20144.         sysobjects o,
  20145.         master.dbo.spt_provider_types d,
  20146.         systypes t
  20147.     where
  20148.         o.name = @procedure_name
  20149.     and    o.type = 'P'                            /* Just Procedures */
  20150.     and    (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  20151.     and    o.id = c.id
  20152.     and    c.number = @group_number
  20153.     and    c.type = d.ss_dtype
  20154.     and    c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  20155.     and    c.usertype = t.usertype
  20156.     and    (t.usertype != 80 or d.type_name='timestamp')
  20157.     and    (@parameter_name is null or @parameter_name = c.name)
  20158.     UNION ALL
  20159.     SELECT           /* return value row*/
  20160.         PROCEDURE_CATALOG     = db_name(),
  20161.         PROCEDURE_SCHEMA     = user_name(o.uid),
  20162.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  20163.         PARAMETER_NAME         = convert(sysname,'@RETURN_VALUE'),
  20164.         ORDINAL_POSITION     = convert(smallint,0),
  20165.         PARAMETER_TYPE         = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  20166.         PARAMETER_HASDEFAULT    = convert(tinyint,0),
  20167.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  20168.         IS_NULLABLE        = convert(bit,0),
  20169.         DATA_TYPE        = convert(smallint, 3 /*DBTYPE_I4*/),
  20170.         CHARACTER_MAXIMUM_LENGTH= convert(int,null),
  20171.         CHARACTER_OCTET_LENGTH    = convert(int,null),
  20172.         NUMERIC_PRECISION    = convert(smallint,10),
  20173.         NUMERIC_SCALE        = convert(smallint,null),
  20174.         DESCRIPTION        = convert(varchar(1),null),
  20175.         TYPE_NAME        = convert(sysname,'int'),
  20176.         LOCAL_TYPE_NAME        = convert(sysname,'int')
  20177.     from
  20178.         syscomments c,
  20179.         sysobjects o
  20180.     where
  20181.         o.name = @procedure_name
  20182.     and    o.type = 'P'                        /* Just Procedures */
  20183.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  20184.     and    (@parameter_name is null or @parameter_name = '@RETURN_VALUE')
  20185.     and    c.id = o.id
  20186.     and    c.number = @group_number
  20187.     and c.colid = 1
  20188.     order by 2, 3, 5
  20189.     END
  20190. ELSE
  20191.     BEGIN
  20192.     insert into #spprocparamrowset1
  20193.     select
  20194.         PROCEDURE_CATALOG     = db_name(),
  20195.         PROCEDURE_SCHEMA     = user_name(o.uid),
  20196.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  20197.         PARAMETER_NAME         = c.name,
  20198.         ORDINAL_POSITION     = convert(smallint, c.colid),
  20199.         PARAMETER_TYPE         = convert(smallint, 1+((c.status/64)&1)),
  20200.         PARAMETER_HASDEFAULT    = convert(tinyint,0),
  20201.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  20202.         IS_NULLABLE        = convert(bit,
  20203.                         case when d.oledb_data_type = 11 /*DBTYPE_BOOL*/ 
  20204.                         then 0 else 1
  20205.                         end),
  20206.         DATA_TYPE        = d.oledb_data_type,
  20207.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  20208.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  20209.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  20210.                         then coalesce(d.column_size,c.length)
  20211.                         else null 
  20212.                         end),
  20213.         CHARACTER_OCTET_LENGTH    = convert(int,
  20214.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  20215.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  20216.                         then coalesce(d.column_size,c.length)
  20217.                         else null 
  20218.                         end),
  20219.         NUMERIC_PRECISION    = convert(smallint,
  20220.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  20221.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  20222.                             then d.data_precision else null end),
  20223.         NUMERIC_SCALE        = convert(smallint, 
  20224.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  20225.         DESCRIPTION        = convert(varchar(1),null),
  20226.         TYPE_NAME        = d.type_name,
  20227.         LOCAL_TYPE_NAME        = d.local_type_name
  20228.     
  20229.     from
  20230.         syscolumns c,
  20231.         sysobjects o,
  20232.         master.dbo.spt_provider_types d,
  20233.         systypes t
  20234.     where
  20235.         o.type = 'P'                            /* Just Procedures */
  20236.     and    (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  20237.     and    o.id = c.id
  20238.     and    c.type = d.ss_dtype
  20239.     and    c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  20240.     and    c.usertype = t.usertype
  20241.     and    (t.usertype != 80 or d.type_name='timestamp')
  20242.     and    (@parameter_name is null or @parameter_name = c.name)
  20243.     UNION ALL
  20244.     SELECT           /* return value row*/
  20245.         PROCEDURE_CATALOG     = db_name(),
  20246.         PROCEDURE_SCHEMA     = user_name(o.uid),
  20247.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  20248.         PARAMETER_NAME         = convert(sysname,'@RETURN_VALUE'),
  20249.         ORDINAL_POSITION     = convert(smallint,0),
  20250.         PARAMETER_TYPE         = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  20251.         PARAMETER_HASDEFAULT    = convert(tinyint,0),
  20252.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  20253.         IS_NULLABLE        = convert(bit,0),
  20254.         DATA_TYPE        = convert(smallint, 3 /*DBTYPE_I4*/),
  20255.         CHARACTER_MAXIMUM_LENGTH= convert(int,null),
  20256.         CHARACTER_OCTET_LENGTH    = convert(int,null),
  20257.         NUMERIC_PRECISION    = convert(smallint,10),
  20258.         NUMERIC_SCALE        = convert(smallint,null),
  20259.         DESCRIPTION        = convert(varchar(1),null),
  20260.         TYPE_NAME        = convert(sysname,'int'),
  20261.         LOCAL_TYPE_NAME        = convert(sysname,'int')
  20262.     from
  20263.         syscomments c,
  20264.         sysobjects o
  20265.     where
  20266.         o.type = 'P'                        /* Just Procedures */
  20267.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  20268.     and    (@parameter_name is null or @parameter_name = '@RETURN_VALUE')
  20269.     and    c.id = o.id
  20270.     and     c.colid = 1
  20271.     order by 2, 3, 5
  20272.     END
  20273.  
  20274.     exec @ret = sp_cursoropen @handle output, 'select * from #spprocparamrowset1',
  20275.         @scrollopt output, @ccopt output, @rows output
  20276.  
  20277.     drop table #spprocparamrowset1
  20278.        return isnull(@ret,0)
  20279. go
  20280. dump tran master with no_log
  20281. go
  20282. create procedure sp_procedure_params_rowset;3
  20283. as
  20284.     select
  20285.         PROCEDURE_CATALOG     = convert(sysname, ' '),
  20286.         PROCEDURE_SCHEMA     = convert(sysname, ' '),
  20287.         PROCEDURE_NAME         = convert(varchar(35), ' '),
  20288.         PARAMETER_NAME         = convert(sysname, ' '),
  20289.         ORDINAL_POSITION     = convert(smallint, 0),
  20290.         PARAMETER_TYPE         = convert(smallint, 0),
  20291.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  20292.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  20293.         IS_NULLABLE        = convert(bit, 0),
  20294.         DATA_TYPE        = convert(smallint, 0),
  20295.         CHARACTER_MAXIMUM_LENGTH= convert(int, 0),
  20296.         CHARACTER_OCTET_LENGTH    = convert(int, 0),
  20297.         NUMERIC_PRECISION    = convert(smallint, 0),
  20298.         NUMERIC_SCALE        = convert(smallint, 0),
  20299.         DESCRIPTION        = convert(varchar(1),null),
  20300.         TYPE_NAME        = convert(sysname,null),
  20301.         LOCAL_TYPE_NAME        = convert(sysname,null)
  20302.     where    1=0
  20303. go
  20304. dump tran master with no_log
  20305. go
  20306.  
  20307.  
  20308. if (charindex('7.00', @@version) > 0 or
  20309.     charindex('8.00', @@version) > 0)
  20310.     drop procedure sp_procedure_params_rowset
  20311. else
  20312. begin
  20313.     print ''
  20314.     print ''
  20315.     print 'Warning:'
  20316.     print 'you are installing the stored procedures '
  20317.     print 'on a pre 7.0 SQL Server.'
  20318.     print 'Ignore the following errors.'
  20319. end
  20320. go
  20321.  
  20322. /*    Procedure for 7.0 servers */
  20323. create procedure sp_procedure_params_rowset
  20324.     (
  20325.        @procedure_name        sysname,
  20326.     @group_number        int = 1,
  20327.     @procedure_schema     sysname = null,
  20328.     @parameter_name        sysname = null
  20329.     )
  20330. as
  20331.     select
  20332.         PROCEDURE_CATALOG     = db_name(),
  20333.         PROCEDURE_SCHEMA     = user_name(o.uid),
  20334.         PROCEDURE_NAME         = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  20335.         PARAMETER_NAME         = c.name,
  20336.         ORDINAL_POSITION     = convert(smallint, c.colid),
  20337.         PARAMETER_TYPE         = convert(smallint, 1+c.isoutparam),
  20338.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  20339.         PARAMETER_DEFAULT    = convert(nvarchar(255),null),
  20340.         IS_NULLABLE        = convert(bit,ColumnProperty(c.id,c.name,'AllowsNull')),
  20341.         DATA_TYPE        = d.oledb_data_type,
  20342.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  20343.                         case 
  20344.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  20345.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  20346.                         then coalesce(d.column_size,c.length)
  20347.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  20348.                         then coalesce(d.column_size,c.length/2)
  20349.                         else null 
  20350.                         end),
  20351.         CHARACTER_OCTET_LENGTH    = convert(int,
  20352.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  20353.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  20354.                         then coalesce(d.column_size,c.length)
  20355.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  20356.                         then coalesce(d.column_size*2,c.length)
  20357.                         else null 
  20358.                         end),
  20359.         NUMERIC_PRECISION    = convert(smallint,
  20360.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  20361.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  20362.                             then d.data_precision else null end),
  20363.         NUMERIC_SCALE        = convert(smallint, 
  20364.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  20365.         DESCRIPTION        = convert(nvarchar(1),null),
  20366.         TYPE_NAME        = d.type_name,
  20367.         LOCAL_TYPE_NAME        = d.local_type_name
  20368.     
  20369.     from
  20370.         sysobjects o,
  20371.         syscolumns c,
  20372.         master.dbo.spt_provider_types d,
  20373.         systypes t
  20374.     where
  20375.         o.name = @procedure_name
  20376.     and    o.type = 'P'                            /* Just Procedures */
  20377.     and    (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  20378.     and    o.id = c.id
  20379.     and    c.number = @group_number
  20380.     and    c.xtype = d.ss_dtype
  20381.     and    c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  20382.     and    c.xusertype = t.xusertype
  20383.     and    (@parameter_name is null or @parameter_name = c.name)
  20384.     UNION ALL
  20385.     SELECT           /* return value row*/
  20386.         PROCEDURE_CATALOG     = db_name(),
  20387.         PROCEDURE_SCHEMA     = user_name(o.uid),
  20388.         PROCEDURE_NAME         = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  20389.         PARAMETER_NAME         = convert(sysname,'@RETURN_VALUE'),
  20390.         ORDINAL_POSITION     = convert(smallint,0),
  20391.         PARAMETER_TYPE         = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  20392.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  20393.         PARAMETER_DEFAULT    = convert(nvarchar(255),null),
  20394.         IS_NULLABLE        = convert(bit,0),
  20395.         DATA_TYPE        = convert(smallint, 3 /*DBTYPE_I4*/),
  20396.         CHARACTER_MAXIMUM_LENGTH= convert(int,null),
  20397.         CHARACTER_OCTET_LENGTH    = convert(int,null),
  20398.         NUMERIC_PRECISION    = convert(smallint,10),
  20399.         NUMERIC_SCALE        = convert(smallint,null),
  20400.         DESCRIPTION        = convert(nvarchar(1),null),
  20401.         TYPE_NAME        = convert(sysname,N'int'),
  20402.         LOCAL_TYPE_NAME        = convert(sysname,N'int')
  20403.     from
  20404.         sysobjects o,
  20405.         syscomments c
  20406.     where
  20407.         o.name = @procedure_name
  20408.     and    o.id = c.id 
  20409.     and    c.number = @group_number
  20410.     and    c.colid = 1
  20411.     and    o.type = 'P'                        /* Just Procedures */
  20412.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  20413.     and    (@parameter_name is null or @parameter_name = '@RETURN_VALUE')
  20414.     order by 2, 3, 5
  20415. go
  20416. dump tran master with no_log
  20417. go
  20418. create procedure sp_procedure_params_rowset;2
  20419.     (
  20420.     @procedure_schema     sysname = null,
  20421.     @parameter_name        sysname = null
  20422.     )
  20423. as
  20424.     select
  20425.         PROCEDURE_CATALOG     = db_name(),
  20426.         PROCEDURE_SCHEMA     = user_name(o.uid),
  20427.         PROCEDURE_NAME         = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  20428.         PARAMETER_NAME         = c.name,
  20429.         ORDINAL_POSITION     = convert(smallint, c.colid),
  20430.         PARAMETER_TYPE         = convert(smallint, 1+c.isoutparam),
  20431.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  20432.         PARAMETER_DEFAULT    = convert(nvarchar(255),null),
  20433.         IS_NULLABLE        = convert(bit,ColumnProperty(c.id,c.name,'AllowsNull')),
  20434.         DATA_TYPE        = d.oledb_data_type,
  20435.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  20436.                         case 
  20437.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  20438.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  20439.                         then coalesce(d.column_size,c.length)
  20440.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  20441.                         then coalesce(d.column_size,c.length/2)
  20442.                         else null 
  20443.                         end),
  20444.         CHARACTER_OCTET_LENGTH    = convert(int,
  20445.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  20446.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  20447.                         then coalesce(d.column_size,c.length)
  20448.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  20449.                         then coalesce(d.column_size*2,c.length)
  20450.                         else null 
  20451.                         end),
  20452.         NUMERIC_PRECISION    = convert(smallint,
  20453.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  20454.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  20455.                             then d.data_precision else null end),
  20456.         NUMERIC_SCALE        = convert(smallint, 
  20457.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  20458.         DESCRIPTION        = convert(nvarchar(1),null),
  20459.         TYPE_NAME        = d.type_name,
  20460.         LOCAL_TYPE_NAME        = d.local_type_name
  20461.     
  20462.     from
  20463.         sysobjects o,
  20464.         syscolumns c,
  20465.         master.dbo.spt_provider_types d,
  20466.         systypes t
  20467.     where
  20468.         o.type = 'P'                            /* Just Procedures */
  20469.     and    (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  20470.     and    o.id = c.id
  20471.     and    c.xtype = d.ss_dtype
  20472.     and    c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  20473.     and    c.xusertype = t.xusertype
  20474.     and    (@parameter_name is null or @parameter_name = c.name)
  20475.     UNION ALL
  20476.     SELECT           /* return value row*/
  20477.         PROCEDURE_CATALOG     = db_name(),
  20478.         PROCEDURE_SCHEMA     = user_name(o.uid),
  20479.         PROCEDURE_NAME         = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  20480.         PARAMETER_NAME         = convert(sysname,'@RETURN_VALUE'),
  20481.         ORDINAL_POSITION     = convert(smallint,0),
  20482.         PARAMETER_TYPE         = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  20483.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  20484.         PARAMETER_DEFAULT    = convert(nvarchar(255),null),
  20485.         IS_NULLABLE        = convert(bit,0),
  20486.         DATA_TYPE        = convert(smallint, 3 /*DBTYPE_I4*/),
  20487.         CHARACTER_MAXIMUM_LENGTH= convert(int,null),
  20488.         CHARACTER_OCTET_LENGTH    = convert(int,null),
  20489.         NUMERIC_PRECISION    = convert(smallint,10),
  20490.         NUMERIC_SCALE        = convert(smallint,null),
  20491.         DESCRIPTION        = convert(nvarchar(1),null),
  20492.         TYPE_NAME        = convert(sysname,N'int'),
  20493.         LOCAL_TYPE_NAME        = convert(sysname,N'int')
  20494.     from
  20495.         sysobjects o,
  20496.         syscomments c
  20497.     where
  20498.         o.type = 'P'        /* Just Procedures */
  20499.     and    o.id = c.id 
  20500.     and    c.colid = 1
  20501.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  20502.     and    (@parameter_name is null or @parameter_name = '@RETURN_VALUE')
  20503.     order by 2, 3, 5
  20504. go
  20505.  
  20506. if (charindex('8.00', @@version) > 0)
  20507.     drop procedure sp_procedure_params_rowset
  20508. else
  20509. begin
  20510.     print ''
  20511.     print ''
  20512.     print 'Warning:'
  20513.     print 'you are installing the stored procedures '
  20514.     print 'on a pre 8.0 SQL Server.'
  20515.     print 'Ignore the following errors.'
  20516. end
  20517. go
  20518.  
  20519. /*    Procedure for 8.0 servers */
  20520. create procedure sp_procedure_params_rowset
  20521.     (
  20522.        @procedure_name        sysname,
  20523.     @group_number        int = 1,
  20524.     @procedure_schema     sysname = null,
  20525.     @parameter_name        sysname = null
  20526.     )
  20527. as
  20528.     select
  20529.         PROCEDURE_CATALOG     = db_name(),
  20530.         PROCEDURE_SCHEMA     = user_name(o.uid),
  20531.         PROCEDURE_NAME         = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  20532.         PARAMETER_NAME         = c.name,
  20533.         ORDINAL_POSITION     = convert(smallint, c.colid),
  20534.         PARAMETER_TYPE         = convert(smallint, 1+c.isoutparam),
  20535.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  20536.         PARAMETER_DEFAULT    = convert(nvarchar(255),null),
  20537.         IS_NULLABLE        = convert(bit,ColumnProperty(c.id,c.name,'AllowsNull')),
  20538.         DATA_TYPE        = d.oledb_data_type,
  20539.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  20540.                         case 
  20541.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  20542.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  20543.                         then coalesce(d.column_size,c.length)
  20544.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  20545.                         then coalesce(d.column_size,c.length/2)
  20546.                         else null 
  20547.                         end),
  20548.         CHARACTER_OCTET_LENGTH    = convert(int,
  20549.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  20550.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  20551.                         then coalesce(d.column_size,c.length)
  20552.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  20553.                         then coalesce(d.column_size*2,c.length)
  20554.                         else null 
  20555.                         end),
  20556.         NUMERIC_PRECISION    = convert(smallint,
  20557.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  20558.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  20559.                             then d.data_precision else null end),
  20560.         NUMERIC_SCALE        = convert(smallint, 
  20561.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  20562.         DESCRIPTION        = convert(nvarchar(1),null),
  20563.         TYPE_NAME        = d.type_name,
  20564.         LOCAL_TYPE_NAME        = d.local_type_name
  20565.     
  20566.     from
  20567.         sysobjects o,
  20568.         syscolumns c,
  20569.         master.dbo.spt_provider_types d,
  20570.         systypes t
  20571.     where
  20572.         o.name = @procedure_name
  20573.     and     (o.type in ('P', 'TF', 'IF') OR (len(c.name) > 0 and o.type = 'FN'))
  20574.     and    (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  20575.     and    o.id = c.id
  20576.     and    ((c.number = @group_number and o.type = 'P') 
  20577.             or (c.number = 0 and o.type = 'FN')
  20578.             or (c.number = 1 and o.type in ('TF', 'IF')))
  20579.     and    c.xtype = d.ss_dtype
  20580.     and    c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  20581.     and    c.xusertype = t.xusertype
  20582.     and    (@parameter_name is null or @parameter_name = c.name)
  20583.     UNION ALL
  20584.     SELECT           /* return value row*/
  20585.         PROCEDURE_CATALOG     = db_name(),
  20586.         PROCEDURE_SCHEMA     = user_name(o.uid),
  20587.         PROCEDURE_NAME         = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  20588.         PARAMETER_NAME         = convert(sysname,'@RETURN_VALUE'),
  20589.         ORDINAL_POSITION     = convert(smallint,0),
  20590.         PARAMETER_TYPE         = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  20591.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  20592.         PARAMETER_DEFAULT    = convert(nvarchar(255),null),
  20593.         IS_NULLABLE        = convert(bit,0),
  20594.         DATA_TYPE        = convert(smallint, 3 /*DBTYPE_I4*/),
  20595.         CHARACTER_MAXIMUM_LENGTH= convert(int,null),
  20596.         CHARACTER_OCTET_LENGTH    = convert(int,null),
  20597.         NUMERIC_PRECISION    = convert(smallint,10),
  20598.         NUMERIC_SCALE        = convert(smallint,null),
  20599.         DESCRIPTION        = convert(nvarchar(1),null),
  20600.         TYPE_NAME        = convert(sysname,N'int'),
  20601.         LOCAL_TYPE_NAME        = convert(sysname,N'int')
  20602.     from
  20603.         sysobjects o,
  20604.         syscomments c
  20605.     where
  20606.         o.name = @procedure_name
  20607.     and    o.id = c.id 
  20608.     and    c.number = @group_number
  20609.     and    c.colid = 1
  20610.     and    o.type = 'P'            /* Just Procedures */
  20611.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  20612.     and    (@parameter_name is null or @parameter_name = '@RETURN_VALUE')
  20613.     UNION ALL
  20614.     SELECT           /* UDF return value row*/
  20615.         PROCEDURE_CATALOG          = db_name(),
  20616.         PROCEDURE_SCHEMA          = user_name(o.uid),
  20617.         PROCEDURE_NAME              = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  20618.         PARAMETER_NAME              = convert(sysname,'@RETURN_VALUE'),
  20619.         ORDINAL_POSITION          = convert(smallint, 0),
  20620.         PARAMETER_TYPE              = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  20621.         PARAMETER_HASDEFAULT     = convert(tinyint, 0),
  20622.         PARAMETER_DEFAULT         = convert(nvarchar(255),null),
  20623.         IS_NULLABLE                 = convert(bit, c.isnullable),
  20624.         DATA_TYPE                 = d.oledb_data_type,
  20625.         CHARACTER_MAXIMUM_LENGTH = convert(int,
  20626.                         case 
  20627.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  20628.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  20629.                         then coalesce(d.column_size,c.length)
  20630.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  20631.                         then coalesce(d.column_size,c.length/2)
  20632.                         else null 
  20633.                         end),
  20634.         CHARACTER_OCTET_LENGTH     = convert(int,
  20635.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  20636.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  20637.                         then coalesce(d.column_size,c.length)
  20638.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  20639.                         then coalesce(d.column_size*2,c.length)
  20640.                         else null 
  20641.                         end),
  20642.         NUMERIC_PRECISION         = convert(smallint,
  20643.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  20644.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  20645.                             then d.data_precision else null end),
  20646.         NUMERIC_SCALE             = convert(smallint, 
  20647.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  20648.         DESCRIPTION                 = convert(nvarchar(1),null),
  20649.         TYPE_NAME                 = d.type_name,
  20650.         LOCAL_TYPE_NAME             = d.local_type_name
  20651.     FROM
  20652.         sysobjects o,
  20653.         syscolumns c,
  20654.         master.dbo.spt_provider_types d,
  20655.         systypes t
  20656.     WHERE
  20657.         o.name = @procedure_name
  20658.     and    o.id = c.id 
  20659.     and    c.number = 0
  20660.     and    c.colid = 0
  20661.     and    o.type = 'FN'            /* UDF scalar functions */
  20662.     and    c.xtype = d.ss_dtype
  20663.     and    c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  20664.     and    c.xusertype = t.xusertype
  20665.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  20666.     and    (@parameter_name is null or @parameter_name = '@RETURN_VALUE')
  20667.     UNION ALL
  20668.     SELECT           /* UDF table value row*/
  20669.         PROCEDURE_CATALOG     = db_name(),
  20670.         PROCEDURE_SCHEMA     = user_name(o.uid),
  20671.         PROCEDURE_NAME         = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  20672.         PARAMETER_NAME         = convert(sysname,'@TABLE_RETURN_VALUE'),
  20673.         ORDINAL_POSITION     = convert(smallint, 0),
  20674.         PARAMETER_TYPE         = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  20675.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  20676.         PARAMETER_DEFAULT    = convert(nvarchar(255),null),
  20677.         IS_NULLABLE        = convert(bit,0),
  20678.         DATA_TYPE        = convert(smallint, 0),                /*DBTYPE_EMPTY*/
  20679.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  20680.                         case 
  20681.                         when d.oledb_data_type = 129         /*DBTYPE_STR*/ 
  20682.                             or d.oledb_data_type = 128    /*DBTYPE_BYTES*/
  20683.                         then coalesce(d.column_size,c.length)
  20684.                         when d.oledb_data_type = 130        /*DBTYPE_WSTR*/
  20685.                         then coalesce(d.column_size,c.length/2)
  20686.                         else null 
  20687.                         end),
  20688.         CHARACTER_OCTET_LENGTH    = convert(int,
  20689.                         case when d.oledb_data_type = 129    /*DBTYPE_STR*/ 
  20690.                             or d.oledb_data_type = 128    /*DBTYPE_BYTES*/
  20691.                         then coalesce(d.column_size,c.length)
  20692.                         when d.oledb_data_type = 130        /*DBTYPE_WSTR*/
  20693.                         then coalesce(d.column_size*2,c.length)
  20694.                         else null 
  20695.                         end),
  20696.         NUMERIC_PRECISION    = convert(smallint,
  20697.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  20698.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  20699.                             then d.data_precision else null end),
  20700.         NUMERIC_SCALE        = convert(smallint, 
  20701.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  20702.         DESCRIPTION        = convert(nvarchar(50), N'Result table returned by table valued function'),
  20703.         TYPE_NAME        = N'table',
  20704.         LOCAL_TYPE_NAME        = N'table'
  20705.     
  20706.     from
  20707.         sysobjects o,
  20708.         syscolumns c,
  20709.         master.dbo.spt_provider_types d
  20710.     where
  20711.         o.name = @procedure_name
  20712.     and    o.id = c.id 
  20713.     and    c.number = 0
  20714.     and    c.colid = 1
  20715.     and    o.type in ('TF', 'IF')            /* UDF table functions */
  20716.     and    c.xtype = d.ss_dtype
  20717.     and    c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  20718.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  20719.     and    (@parameter_name is null or @parameter_name = '@TABLE_RETURN_VALUE')
  20720.     order by 2, 3, 5
  20721. go
  20722. dump tran master with no_log
  20723. go
  20724. create procedure sp_procedure_params_rowset;2
  20725.     (
  20726.     @procedure_schema     sysname = null,
  20727.     @parameter_name        sysname = null
  20728.     )
  20729. as
  20730.     select
  20731.         PROCEDURE_CATALOG     = db_name(),
  20732.         PROCEDURE_SCHEMA     = user_name(o.uid),
  20733.         PROCEDURE_NAME         = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  20734.         PARAMETER_NAME         = c.name,
  20735.         ORDINAL_POSITION     = convert(smallint, c.colid),
  20736.         PARAMETER_TYPE         = convert(smallint, 1+c.isoutparam),
  20737.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  20738.         PARAMETER_DEFAULT    = convert(nvarchar(255),null),
  20739.         IS_NULLABLE        = convert(bit,ColumnProperty(c.id,c.name,'AllowsNull')),
  20740.         DATA_TYPE        = d.oledb_data_type,
  20741.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  20742.                         case 
  20743.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  20744.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  20745.                         then coalesce(d.column_size,c.length)
  20746.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  20747.                         then coalesce(d.column_size,c.length/2)
  20748.                         else null 
  20749.                         end),
  20750.         CHARACTER_OCTET_LENGTH    = convert(int,
  20751.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  20752.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  20753.                         then coalesce(d.column_size,c.length)
  20754.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  20755.                         then coalesce(d.column_size*2,c.length)
  20756.                         else null 
  20757.                         end),
  20758.         NUMERIC_PRECISION    = convert(smallint,
  20759.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  20760.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  20761.                             then d.data_precision else null end),
  20762.         NUMERIC_SCALE        = convert(smallint, 
  20763.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  20764.         DESCRIPTION        = convert(nvarchar(1),null),
  20765.         TYPE_NAME        = d.type_name,
  20766.         LOCAL_TYPE_NAME        = d.local_type_name
  20767.     
  20768.     from
  20769.         sysobjects o,
  20770.         syscolumns c,
  20771.         master.dbo.spt_provider_types d,
  20772.         systypes t
  20773.     where
  20774.         (o.type in ('P', 'TF', 'IF') OR (len(c.name) > 0 and o.type = 'FN'))
  20775.     and    (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  20776.     and    o.id = c.id
  20777.     and    (o.type = 'P' or (c.number = 0 and o.type = 'FN') or (c.number = 1 and o.type in ('TF', 'IF')))
  20778.     and    c.xtype = d.ss_dtype
  20779.     and    c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  20780.     and    c.xusertype = t.xusertype
  20781.     and    (@parameter_name is null or @parameter_name = c.name)
  20782.     UNION ALL
  20783.     SELECT           /* return value row*/
  20784.         PROCEDURE_CATALOG     = db_name(),
  20785.         PROCEDURE_SCHEMA     = user_name(o.uid),
  20786.         PROCEDURE_NAME         = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  20787.         PARAMETER_NAME         = convert(sysname,'@RETURN_VALUE'),
  20788.         ORDINAL_POSITION     = convert(smallint,0),
  20789.         PARAMETER_TYPE         = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  20790.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  20791.         PARAMETER_DEFAULT    = convert(nvarchar(255),null),
  20792.         IS_NULLABLE        = convert(bit,0),
  20793.         DATA_TYPE        = convert(smallint, 3 /*DBTYPE_I4*/),
  20794.         CHARACTER_MAXIMUM_LENGTH= convert(int,null),
  20795.         CHARACTER_OCTET_LENGTH    = convert(int,null),
  20796.         NUMERIC_PRECISION    = convert(smallint,10),
  20797.         NUMERIC_SCALE        = convert(smallint,null),
  20798.         DESCRIPTION        = convert(nvarchar(1),null),
  20799.         TYPE_NAME        = convert(sysname,N'int'),
  20800.         LOCAL_TYPE_NAME        = convert(sysname,N'int')
  20801.     from
  20802.         sysobjects o,
  20803.         syscomments c
  20804.     where
  20805.         o.type = 'P'            /* Just Procedures */
  20806.     and    o.id = c.id 
  20807.     and    c.colid = 1
  20808.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  20809.     and    (@parameter_name is null or @parameter_name = '@RETURN_VALUE')
  20810.     UNION ALL
  20811.     SELECT           /* UDF return value row*/
  20812.         PROCEDURE_CATALOG     = db_name(),
  20813.         PROCEDURE_SCHEMA     = user_name(o.uid),
  20814.         PROCEDURE_NAME         = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  20815.         PARAMETER_NAME         = convert(sysname,'@RETURN_VALUE'),
  20816.         ORDINAL_POSITION     = convert(smallint, 0),
  20817.         PARAMETER_TYPE         = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  20818.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  20819.         PARAMETER_DEFAULT    = convert(nvarchar(255),null),
  20820.         IS_NULLABLE        = convert(bit, c.isnullable),
  20821.         DATA_TYPE        = d.oledb_data_type,
  20822.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  20823.                         case 
  20824.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  20825.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  20826.                         then coalesce(d.column_size,c.length)
  20827.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  20828.                         then coalesce(d.column_size,c.length/2)
  20829.                         else null 
  20830.                         end),
  20831.         CHARACTER_OCTET_LENGTH    = convert(int,
  20832.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  20833.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  20834.                         then coalesce(d.column_size,c.length)
  20835.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  20836.                         then coalesce(d.column_size*2,c.length)
  20837.                         else null 
  20838.                         end),
  20839.         NUMERIC_PRECISION    = convert(smallint,
  20840.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  20841.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  20842.                             then d.data_precision else null end),
  20843.         NUMERIC_SCALE        = convert(smallint, 
  20844.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  20845.         DESCRIPTION        = convert(nvarchar(1),null),
  20846.         TYPE_NAME        = d.type_name,
  20847.         LOCAL_TYPE_NAME        = d.local_type_name
  20848.     
  20849.     from
  20850.         sysobjects o,
  20851.         syscolumns c,
  20852.         master.dbo.spt_provider_types d,
  20853.         systypes t
  20854.     where
  20855.         o.id = c.id 
  20856.     and    c.number = 0
  20857.     and    c.colid = 0
  20858.     and    o.type = 'FN'            /* UDF scalar functions */
  20859.     and    c.xtype = d.ss_dtype
  20860.     and    c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  20861.     and    c.xusertype = t.xusertype
  20862.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  20863.     and    (@parameter_name is null or @parameter_name = '@RETURN_VALUE')
  20864.     UNION ALL
  20865.     SELECT           /* UDF table value row*/
  20866.         PROCEDURE_CATALOG     = db_name(),
  20867.         PROCEDURE_SCHEMA     = user_name(o.uid),
  20868.         PROCEDURE_NAME         = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  20869.         PARAMETER_NAME         = convert(sysname,'@TABLE_RETURN_VALUE'),
  20870.         ORDINAL_POSITION     = convert(smallint, 0),
  20871.         PARAMETER_TYPE         = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  20872.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  20873.         PARAMETER_DEFAULT    = convert(nvarchar(255),null),
  20874.         IS_NULLABLE        = convert(bit,0),
  20875.         DATA_TYPE        = convert(smallint, 0),            /*DBTYPE_EMPTY*/
  20876.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  20877.                         case 
  20878.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  20879.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  20880.                         then coalesce(d.column_size,c.length)
  20881.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  20882.                         then coalesce(d.column_size,c.length/2)
  20883.                         else null 
  20884.                         end),
  20885.         CHARACTER_OCTET_LENGTH    = convert(int,
  20886.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  20887.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  20888.                         then coalesce(d.column_size,c.length)
  20889.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  20890.                         then coalesce(d.column_size*2,c.length)
  20891.                         else null 
  20892.                         end),
  20893.         NUMERIC_PRECISION    = convert(smallint,
  20894.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  20895.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  20896.                             then d.data_precision else null end),
  20897.         NUMERIC_SCALE        = convert(smallint, 
  20898.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  20899.         DESCRIPTION        = convert(nvarchar(1),null),
  20900.         TYPE_NAME        = N'table',
  20901.         LOCAL_TYPE_NAME        = N'table'
  20902.     
  20903.     from
  20904.         sysobjects o,
  20905.         syscolumns c,
  20906.         master.dbo.spt_provider_types d
  20907.     where
  20908.         o.id = c.id 
  20909.     and    c.number = 0
  20910.     and    c.colid = 1
  20911.     and    o.type in ('TF', 'IF')            /* UDF table functions */
  20912.     and    c.xtype = d.ss_dtype
  20913.     and    c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  20914.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  20915.     and    (@parameter_name is null or @parameter_name = '@TABLE_RETURN_VALUE')
  20916.     order by 2, 3, 5
  20917. go
  20918.  
  20919. grant execute on sp_procedure_params_rowset to public
  20920. go
  20921.  
  20922. dump tran master with no_log
  20923. go
  20924. if (charindex('6.00', @@version) > 0)
  20925.     begin
  20926.     if (exists (select * from sysobjects
  20927.             where name = 'sp_procedure_params_rowset' and type = 'P '))
  20928.         begin
  20929.         drop procedure sp_procedure_params_rowset
  20930.         dump tran master with no_log
  20931.         end
  20932.     end
  20933. go
  20934.  
  20935.  
  20936. print ''
  20937. print 'creating sp_procedures_rowset'
  20938. go
  20939.  
  20940.  
  20941. /* pre 7.0 version */
  20942. create procedure sp_procedures_rowset
  20943.     (
  20944.     @procedure_name        varchar(255), 
  20945.     @group_number        int = 1,
  20946.     @procedure_schema    varchar(255) = null
  20947.     )        
  20948. as
  20949.     select
  20950.         PROCEDURE_CATALOG    = db_name(),
  20951.         PROCEDURE_SCHEMA    = user_name(o.uid),
  20952.         PROCEDURE_NAME        = convert(varchar(35),o.name +';'+ ltrim(str(p.number,5))),
  20953.         PROCEDURE_TYPE        = convert(smallint, 3 /*DB_PT_FUNCTION*/),
  20954.         PROCEDURE_DEFINITION    = convert(varchar(1),null),
  20955.         DESCRIPTION        = convert(varchar(1),null),
  20956.         DATE_CREATED        = o.crdate,
  20957.         DATE_MODIFIED        = convert(datetime,null)
  20958.     from     
  20959.         sysobjects o, 
  20960.         syscomments p,
  20961.         sysusers u
  20962.     where
  20963.             o.name = @procedure_name
  20964.         and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  20965.     and     o.type = 'P'        /* Object type of Procedure */
  20966.     and     p.colid = 1
  20967.     and     p.id = o.id
  20968.     and    p.number = @group_number
  20969.     and     u.uid = user_id()    /* constrain sysusers uid for use in subquery */
  20970.     and     (    suser_id() = 1     /* User is the System Administrator */
  20971.         or     o.uid = user_id()    /* User created the object */
  20972.             /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  20973.         or     ((select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  20974.              from sysprotects p
  20975.              where p.id = o.id
  20976.                  /*  get rows for public,current user,user's group */
  20977.                  and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  20978.                  /* check for SELECT,EXECUTE privilege */
  20979.              and (action in (193,224)))&1    /* more magic...normalize GRANT */
  20980.             ) = 1     /* final magic...compare Grants    */
  20981.         )
  20982.     order by 2, 3
  20983. go
  20984. dump tran master with no_log
  20985. go
  20986. create procedure sp_procedures_rowset;2
  20987.     (
  20988.     @procedure_schema    varchar(255) = null
  20989.     )        
  20990. as
  20991.     select
  20992.         PROCEDURE_CATALOG    = db_name(),
  20993.         PROCEDURE_SCHEMA    = user_name(o.uid),
  20994.         PROCEDURE_NAME        = convert(varchar(35),o.name +';'+ ltrim(str(p.number,5))),
  20995.         PROCEDURE_TYPE        = convert(smallint, 3 /*DB_PT_FUNCTION*/),
  20996.         PROCEDURE_DEFINITION    = convert(varchar(1),null),
  20997.         DESCRIPTION        = convert(varchar(1),null),
  20998.         DATE_CREATED        = o.crdate,
  20999.         DATE_MODIFIED        = convert(datetime,null)
  21000.     from     
  21001.         sysobjects o, 
  21002.         syscomments p,
  21003.         sysusers u
  21004.     where
  21005.             (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  21006.     and     o.type = 'P'        /* Object type of Procedure */
  21007.     and     p.colid = 1
  21008.     and     p.id = o.id
  21009.     and     u.uid = user_id()    /* constrain sysusers uid for use in subquery */
  21010.     and     (    suser_id() = 1     /* User is the System Administrator */
  21011.         or     o.uid = user_id()    /* User created the object */
  21012.             /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  21013.         or     ((select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  21014.              from sysprotects p
  21015.              where p.id = o.id
  21016.                  /*  get rows for public,current user,user's group */
  21017.                  and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  21018.                  /* check for SELECT,EXECUTE privilege */
  21019.              and (action in (193,224)))&1    /* more magic...normalize GRANT */
  21020.             ) = 1     /* final magic...compare Grants    */
  21021.         )
  21022.     order by 2, 3
  21023. go
  21024. dump tran master with no_log
  21025. go
  21026.  
  21027. if (charindex('7.00', @@version) > 0 or
  21028.     charindex('8.00', @@version) > 0)
  21029.     drop procedure sp_procedures_rowset
  21030. else
  21031. begin
  21032.     print ''
  21033.     print ''
  21034.     print 'Warning:'
  21035.     print 'you are installing the stored procedures '
  21036.     print 'on a pre 7.0 SQL Server.'
  21037.     print 'Ignore the following errors.'
  21038. end
  21039. go
  21040.  
  21041. /* 7.0 version */
  21042. create procedure sp_procedures_rowset
  21043.     (
  21044.     @procedure_name        sysname, 
  21045.     @group_number        int = 1,
  21046.     @procedure_schema    sysname = null
  21047.     )        
  21048. as
  21049.     select
  21050.         PROCEDURE_CATALOG    = db_name(),
  21051.         PROCEDURE_SCHEMA    = user_name(o.uid),
  21052.         PROCEDURE_NAME        = convert(nvarchar(134),o.name +';'+ ltrim(str(p.number,5))),
  21053.         PROCEDURE_TYPE        = convert(smallint, 3 /*DB_PT_FUNCTION*/),
  21054.         PROCEDURE_DEFINITION    = convert(nvarchar(1),null),
  21055.         DESCRIPTION        = convert(nvarchar(1),null),
  21056.         DATE_CREATED        = o.crdate,
  21057.         DATE_MODIFIED        = convert(datetime,null)
  21058.     from     
  21059.         sysobjects o, 
  21060.         syscomments p
  21061.     where
  21062.         permissions(o.id) <> 0
  21063.     and    o.name = @procedure_name
  21064.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  21065.     and     o.type = 'P'        /* Object type of Procedure */
  21066.     and     p.colid = 1
  21067.     and     p.id = o.id
  21068.     and    p.number = @group_number
  21069.     order by 2, 3
  21070. go
  21071. dump tran master with no_log
  21072. go
  21073. create procedure sp_procedures_rowset;2
  21074.     (
  21075.     @procedure_schema    sysname = null
  21076.     )        
  21077. as
  21078.     select
  21079.         PROCEDURE_CATALOG    = db_name(),
  21080.         PROCEDURE_SCHEMA    = user_name(o.uid),
  21081.         PROCEDURE_NAME        = convert(nvarchar(134),o.name +';'+ ltrim(str(p.number,5))),
  21082.         PROCEDURE_TYPE        = convert(smallint, 3 /*DB_PT_FUNCTION*/),
  21083.         PROCEDURE_DEFINITION    = convert(nvarchar(1),null),
  21084.         DESCRIPTION        = convert(nvarchar(1),null),
  21085.         DATE_CREATED        = o.crdate,
  21086.         DATE_MODIFIED        = convert(datetime,null)
  21087.     from     sysobjects o, syscomments p
  21088.     where
  21089.         permissions(o.id) <> 0
  21090.     and        (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  21091.     and     o.type = 'P'        /* Object type of Procedure */
  21092.     and p.colid = 1
  21093.     and p.id = o.id
  21094.     order by 2, 3
  21095. go
  21096.  
  21097. if (charindex('8.00', @@version) > 0)
  21098.     drop procedure sp_procedures_rowset
  21099. else
  21100. begin
  21101.     print ''
  21102.     print ''
  21103.     print 'Warning:'
  21104.     print 'you are installing the stored procedures '
  21105.     print 'on a pre 8.0 SQL Server.'
  21106.     print 'Ignore the following errors.'
  21107. end
  21108. go
  21109.  
  21110. /* 8.0 version */
  21111. create procedure sp_procedures_rowset
  21112.     (
  21113.     @procedure_name        sysname, 
  21114.     @group_number        int = 1,
  21115.     @procedure_schema    sysname = null
  21116.     )        
  21117. as
  21118.     select
  21119.         PROCEDURE_CATALOG    = db_name(),
  21120.         PROCEDURE_SCHEMA    = user_name(o.uid),
  21121.         PROCEDURE_NAME        = convert(nvarchar(134),o.name +';'+ ltrim(str(p.number,5))),
  21122.         PROCEDURE_TYPE        = convert(smallint, 3 /*DB_PT_FUNCTION*/),
  21123.         PROCEDURE_DEFINITION    = convert(nvarchar(1),null),
  21124.         DESCRIPTION        = convert(nvarchar(1),null),
  21125.         DATE_CREATED        = o.crdate,
  21126.         DATE_MODIFIED        = convert(datetime,null)
  21127.     from     
  21128.         sysobjects o, 
  21129.         syscomments p
  21130.     where
  21131.         permissions(o.id) <> 0
  21132.     and    o.name = @procedure_name
  21133.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  21134.     and     o.type in ('P', 'FN', 'TF', 'IF')    /* Object type of Procedure */
  21135.     and     p.colid = 1
  21136.     and     p.id = o.id
  21137.     and    p.number = @group_number
  21138.     order by 2, 3
  21139. go
  21140. dump tran master with no_log
  21141. go
  21142. create procedure sp_procedures_rowset;2
  21143.     (
  21144.     @procedure_schema    sysname = null
  21145.     )        
  21146. as
  21147.     select
  21148.         PROCEDURE_CATALOG    = db_name(),
  21149.         PROCEDURE_SCHEMA    = user_name(o.uid),
  21150.         PROCEDURE_NAME        = convert(nvarchar(134),o.name +';'+ ltrim(str(p.number,5))),
  21151.         PROCEDURE_TYPE        = convert(smallint, 3 /*DB_PT_FUNCTION*/),
  21152.         PROCEDURE_DEFINITION    = convert(nvarchar(1),null),
  21153.         DESCRIPTION        = convert(nvarchar(1),null),
  21154.         DATE_CREATED        = o.crdate,
  21155.         DATE_MODIFIED        = convert(datetime,null)
  21156.     from     sysobjects o, syscomments p
  21157.     where
  21158.         permissions(o.id) <> 0
  21159.     and        (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  21160.     and     o.type in ('P', 'FN', 'TF', 'IF')    /* Object type of Procedure */
  21161.     and p.colid = 1
  21162.     and p.id = o.id
  21163.     order by 2, 3
  21164. go
  21165.  
  21166. grant execute on sp_procedures_rowset to public
  21167. go
  21168.  
  21169. dump tran master with no_log
  21170. go
  21171.  
  21172. if (charindex('6.00', @@version) > 0)
  21173.     begin
  21174.     if (exists (select * from sysobjects
  21175.             where name = 'sp_procedures_rowset' and type = 'P '))
  21176.         begin
  21177.         drop procedure sp_procedures_rowset
  21178.         dump tran master with no_log
  21179.         end
  21180.     end
  21181. go
  21182.  
  21183.  
  21184. print ''
  21185. print 'creating sp_schemata_rowset'
  21186. go
  21187.  
  21188. /*    6.0 and 6.5 and 7.0 version */
  21189. create procedure sp_schemata_rowset
  21190.     (
  21191.     @schema_name    varchar(90) = null, 
  21192.     @schema_owner    varchar(90) = null
  21193.     )        
  21194. as
  21195.     select    distinct
  21196.         CATALOG_NAME            = db_name(),
  21197.         SCHEMA_NAME            = user_name(o.uid),    
  21198.         SCHEMA_OWNER            = user_name(o.uid),    
  21199.         DEFAULT_CHARACTER_SET_CATALOG    = convert(sysname,'master'),
  21200.         DEFAULT_CHARACTER_SET_SCHEMA    = convert(sysname,'dbo'),
  21201.         DEFAULT_CHARACTER_SET_NAME    = convert(sysname,a_cha.name)
  21202.     from    
  21203.         sysobjects o,
  21204.         master.dbo.sysconfigures    cfg,
  21205.         master.dbo.syscharsets        a_cha, /* charset/1001, not sortorder */
  21206.         master.dbo.syscharsets        b_cha  /* sortorder/2001, not charset */
  21207.     where   
  21208.         (@schema_name is null or @schema_name = user_name(o.uid))
  21209.     and     (@schema_owner is null or @schema_owner = user_name(o.uid))
  21210.     and    a_cha.type = 1001 /* type is charset */
  21211.     and     b_cha.type = 2001 /* type is sortorder */
  21212.     and     a_cha.id = b_cha.csid
  21213.     and     b_cha.id = cfg.value
  21214.     order by 2
  21215. go
  21216. dump tran master with no_log
  21217. go
  21218.  
  21219. if (charindex('8.00', @@version) > 0)
  21220.     drop procedure sp_schemata_rowset
  21221. else
  21222. begin
  21223.     print ''
  21224.     print ''
  21225.     print 'Warning:'
  21226.     print 'you are installing the stored procedures '
  21227.     print 'on a pre 8.0 SQL Server.'
  21228.     print 'Ignore the following errors.'
  21229. end
  21230. go
  21231.  
  21232. /* 8.0 version */
  21233. create procedure sp_schemata_rowset
  21234.     (
  21235.     @schema_name    sysname = null, 
  21236.     @schema_owner    sysname = null
  21237.     )        
  21238. as
  21239.     select    distinct
  21240.         CATALOG_NAME            = db_name(),
  21241.         SCHEMA_NAME            = user_name(o.uid),    
  21242.         SCHEMA_OWNER            = user_name(o.uid),    
  21243.         DEFAULT_CHARACTER_SET_CATALOG    = convert(sysname,N'master'),
  21244.         DEFAULT_CHARACTER_SET_SCHEMA    = convert(sysname,N'dbo'),
  21245.         DEFAULT_CHARACTER_SET_NAME    = convert(sysname,a_cha.name)
  21246.     from    
  21247.         sysobjects o,
  21248.         master.dbo.syscharsets        a_cha /* charset/1001, not sortorder */
  21249.     where   
  21250.         (@schema_name is null or @schema_name = user_name(o.uid))
  21251.     and     (@schema_owner is null or @schema_owner = user_name(o.uid))
  21252.     and    a_cha.type = 1001 /* type is charset */
  21253.     and     a_cha.id = convert(tinyint, DatabasePropertyEx(db_name(), 'sqlcharset')) /* what is charset of a table? */
  21254.     order by 2
  21255. go
  21256.  
  21257. dump tran master with no_log
  21258. go
  21259.  
  21260. /* The following stored procedure is used for Sphinx and Hydra */
  21261. create procedure sp_schemata_rowset;3
  21262. as
  21263.     select
  21264.         CATALOG_NAME            = convert(sysname,' '),
  21265.         SCHEMA_NAME            = convert(sysname,' '),    
  21266.         SCHEMA_OWNER            = convert(sysname,' '),    
  21267.         DEFAULT_CHARACTER_SET_CATALOG    = convert(sysname,' '),
  21268.         DEFAULT_CHARACTER_SET_SCHEMA    = convert(sysname,' '),
  21269.         DEFAULT_CHARACTER_SET_NAME    = convert(sysname,' ')
  21270.     where    1=0
  21271. go
  21272. grant execute on sp_schemata_rowset to public
  21273. go
  21274.  
  21275. dump tran master with no_log
  21276. go
  21277. if (charindex('6.00', @@version) > 0)
  21278.     begin
  21279.     if (exists (select * from sysobjects
  21280.             where name = 'sp_schemata_rowset' and type = 'P '))
  21281.         begin
  21282.         drop procedure sp_schemata_rowset
  21283.         dump tran master with no_log
  21284.         end
  21285.     end
  21286. go
  21287.  
  21288.  
  21289. print ''
  21290. print 'creating sp_statistics_rowset'
  21291. go
  21292.  
  21293.  
  21294. /*    6.0 and 6.5 version */
  21295. create procedure sp_statistics_rowset
  21296.     (
  21297.     @table_name    varchar(255),
  21298.     @table_schema    varchar(255) = null     
  21299.     )
  21300. as
  21301.     select    db_name()                as TABLE_CATALOG,        
  21302.         user_name(o.uid)            as TABLE_SCHEMA,
  21303.         o.name                    as TABLE_NAME,
  21304.         x.rows                    as CARDINALITY
  21305.     from    sysobjects o, sysindexes x, sysusers u
  21306.     where    o.type in ('U')
  21307.     and     o.name = @table_name
  21308.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  21309.     and     x.id = o.id
  21310.     and     x.indid in (0,1)    /*If there are no indexes then table stats are in a row with indid =0 */
  21311.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  21312.     and     (
  21313.         suser_id() = 1     /* User is the System Administrator */
  21314.         or o.uid = user_id()     /* User created the object */
  21315.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  21316.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  21317.             from sysprotects p
  21318.             where p.id = o.id
  21319.             /* get rows for public,current user,user's group */
  21320.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  21321.             /* check for SELECT,EXECUTE privilege */
  21322.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  21323.             ) = 1    /* final magic...compare Grants      */
  21324.            )
  21325.     order by 2, 3
  21326. go
  21327. dump tran master with no_log
  21328. go
  21329. create procedure sp_statistics_rowset;2
  21330.     (
  21331.     @table_schema    varchar(255) = null     
  21332.     )
  21333. as
  21334.     select    db_name()                as TABLE_CATALOG,        
  21335.         user_name(o.uid)            as TABLE_SCHEMA,
  21336.         o.name                    as TABLE_NAME,
  21337.         x.rows                    as CARDINALITY
  21338.     from    sysobjects o, sysindexes x, sysusers u
  21339.     where    o.type in ('U')
  21340.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  21341.     and     x.id = o.id
  21342.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  21343.     and     (
  21344.         suser_id() = 1     /* User is the System Administrator */
  21345.         or o.uid = user_id()     /* User created the object */
  21346.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  21347.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  21348.             from sysprotects p
  21349.             where p.id = o.id
  21350.             /* get rows for public,current user,user's group */
  21351.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  21352.             /* check for SELECT,EXECUTE privilege */
  21353.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  21354.             ) = 1    /* final magic...compare Grants      */
  21355.            )        
  21356.     order by 2, 3
  21357. go
  21358. dump tran master with no_log
  21359. go
  21360.  
  21361. if (charindex('7.00', @@version) > 0 or
  21362.     charindex('8.00', @@version) > 0)
  21363.     drop procedure sp_statistics_rowset
  21364. else
  21365. begin
  21366.     print ''
  21367.     print ''
  21368.     print 'Warning:'
  21369.     print 'you are installing the stored procedures '
  21370.     print 'on a pre 8.0 SQL Server.'
  21371.     print 'Ignore the following errors.'
  21372. end
  21373. go
  21374.  
  21375. /*    8.0 version */
  21376. create procedure sp_statistics_rowset
  21377.     (
  21378.     @table_name    sysname,
  21379.     @table_schema    sysname = null     
  21380.     )
  21381. as
  21382.     select    db_name()                as TABLE_CATALOG,        
  21383.         user_name(o.uid)            as TABLE_SCHEMA,
  21384.         o.name                    as TABLE_NAME,
  21385.         x.rows                    as CARDINALITY
  21386.     from    sysobjects o, sysindexes x
  21387.     where    o.type in ('U')
  21388.     and     o.name = @table_name
  21389.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  21390.     and     x.id = o.id
  21391.     and     x.indid in (0,1)    /*If there are no indexes then table stats are in a row with indid =0 */
  21392.     and     permissions(o.id) <> 0
  21393.     order by 2, 3
  21394. go
  21395. dump tran master with no_log
  21396. go
  21397. create procedure sp_statistics_rowset;2
  21398.     (
  21399.     @table_schema    sysname = null     
  21400.     )
  21401. as
  21402.     select    db_name()                as TABLE_CATALOG,        
  21403.         user_name(o.uid)            as TABLE_SCHEMA,
  21404.         o.name                    as TABLE_NAME,
  21405.         x.rows                    as CARDINALITY
  21406.     from    sysobjects o, sysindexes x
  21407.     where    o.type in ('U')
  21408.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  21409.     and     x.id = o.id
  21410.     and     x.indid in (0,1)    /*If there are no indexes then table stats are in a row with indid =0 */
  21411.     and     permissions(o.id) <> 0
  21412.     order by 2, 3
  21413. go
  21414.  
  21415. grant execute on sp_statistics_rowset to public
  21416. go
  21417.  
  21418. dump tran master with no_log
  21419. go
  21420. if (charindex('6.00', @@version) > 0)
  21421.     begin
  21422.     if (exists (select * from sysobjects
  21423.             where name = 'sp_statistics_rowset' and type = 'P '))
  21424.         begin
  21425.         drop procedure sp_statistics_rowset
  21426.         dump tran master with no_log
  21427.         end
  21428.     end
  21429. go
  21430.  
  21431.  
  21432. print ''
  21433. print 'creating sp_tables_rowset'
  21434. go
  21435.  
  21436.  
  21437. /*    Procedure for 6.50 and earlier servers */
  21438. create procedure sp_tables_rowset
  21439.     (
  21440.     @table_name    varchar(255), 
  21441.     @table_schema    varchar(255) = null,    
  21442.     @table_type    varchar(255) = null 
  21443.     )
  21444. as
  21445.     select    TABLE_CATALOG    = db_name(),
  21446.         TABLE_SCHEMA    = user_name(o.uid),
  21447.         TABLE_NAME    = o.name,
  21448.         TABLE_TYPE    = convert(varchar(30),
  21449.                     case o.type 
  21450.                     when 'U' then 'TABLE'
  21451.                     when 'V' then 'VIEW'
  21452.                     when 'S' then 'SYSTEM TABLE'
  21453.                     end),
  21454.         TABLE_GUID    = convert(binary(16), null),
  21455.         DESCRIPTION    = convert(varchar(1), null),
  21456.         TABLE_PROPID    = convert(int,null),
  21457.         DATE_CREATED    = o.crdate,
  21458.         DATE_MODIFIED    = convert(datetime,null)
  21459.     from    sysusers u, sysobjects o
  21460.     where    o.type in ('U','V','S')
  21461.     and     o.name = @table_name
  21462.     and     (    @table_schema is null
  21463.         or    @table_schema = user_name(o.uid)
  21464.         )
  21465.     and     (
  21466.             @table_type is null
  21467.         or    @table_type = case o.type 
  21468.                     when 'U' then 'TABLE'
  21469.                     when 'V' then 'VIEW'
  21470.                     when 'S' then 'SYSTEM TABLE'
  21471.                     end
  21472.         )
  21473.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  21474.     and     (
  21475.         suser_id() = 1     /* User is the System Administrator */
  21476.         or o.uid = user_id()     /* User created the object */
  21477.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  21478.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  21479.             from sysprotects p
  21480.             /* join to correlate with all rows in sysobjects */
  21481.             where p.id = o.id
  21482.             /* get rows for public,current user,user's group */
  21483.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  21484.             /* check for SELECT,EXECUTE privilege */
  21485.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  21486.             ) = 1    /* final magic...compare Grants      */
  21487.         )        
  21488.     order by 4, 2, 3
  21489. go
  21490. dump tran master with no_log
  21491. go
  21492. create procedure sp_tables_rowset;2
  21493.     (
  21494.     @table_schema    varchar(255) = null,    
  21495.     @table_type    varchar(255) = null 
  21496.     )
  21497. as
  21498.     select    TABLE_CATALOG    = db_name(),
  21499.         TABLE_SCHEMA    = user_name(o.uid),
  21500.         TABLE_NAME    = o.name,
  21501.         TABLE_TYPE    = convert(varchar(30),
  21502.                     case o.type 
  21503.                     when 'U' then 'TABLE'
  21504.                     when 'V' then 'VIEW'
  21505.                     when 'S' then 'SYSTEM TABLE'
  21506.                     end),
  21507.         TABLE_GUID    = convert(binary(16), null),
  21508.         DESCRIPTION    = convert(varchar(1), null),
  21509.         TABLE_PROPID    = convert(int,null),
  21510.         DATE_CREATED    = o.crdate,
  21511.         DATE_MODIFIED    = convert(datetime,null)
  21512.     from    sysusers u, sysobjects o
  21513.     where    o.type in ('U','V','S')
  21514.     and     (    @table_schema is null
  21515.         or    @table_schema = user_name(o.uid)
  21516.         )
  21517.     and     (
  21518.             @table_type is null
  21519.         or    @table_type = case o.type 
  21520.                     when 'U' then 'TABLE'
  21521.                     when 'V' then 'VIEW'
  21522.                     when 'S' then 'SYSTEM TABLE'
  21523.                     end
  21524.         )
  21525.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  21526.     and     (
  21527.         suser_id() = 1     /* User is the System Administrator */
  21528.         or o.uid = user_id()     /* User created the object */
  21529.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  21530.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  21531.             from sysprotects p
  21532.             /* join to correlate with all rows in sysobjects */
  21533.             where p.id = o.id
  21534.             /* get rows for public,current user,user's group */
  21535.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  21536.             /* check for SELECT,EXECUTE privilege */
  21537.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  21538.             ) = 1    /* final magic...compare Grants      */
  21539.         )        
  21540.     order by 4, 2, 3
  21541. go
  21542. dump tran master with no_log
  21543. go
  21544.  
  21545. if (charindex('7.00', @@version) > 0 or
  21546.     charindex('8.00', @@version) > 0)
  21547.     drop procedure sp_tables_rowset
  21548. else
  21549. begin
  21550.     print ''
  21551.     print ''
  21552.     print 'Warning:'
  21553.     print 'you are installing the stored procedures '
  21554.     print 'on a pre 8.0 SQL Server.'
  21555.     print 'Ignore the following errors.'
  21556. end
  21557. go
  21558.  
  21559. /*    Procedure for 8.0 server */
  21560. create procedure sp_tables_rowset
  21561.     (
  21562.     @table_name    sysname, 
  21563.     @table_schema    sysname = null,    
  21564.     @table_type    nvarchar(255) = null 
  21565.     )
  21566. as
  21567.     select    *
  21568.     from    (select    TABLE_CATALOG    = db_name(),
  21569.             TABLE_SCHEMA    = user_name(uid),
  21570.             TABLE_NAME    = name,
  21571.             TABLE_TYPE    = convert(nvarchar(30),
  21572.                         case type 
  21573.                         when 'U' then 
  21574.                             case when ObjectProperty(id, 'IsMSShipped') = 0
  21575.                             then N'TABLE' else N'SYSTEM TABLE' end
  21576.                         when 'S' then N'SYSTEM TABLE'
  21577.                         when 'V' then 
  21578.                             case when ObjectProperty(id, 'IsMSShipped') = 0
  21579.                             then N'VIEW' else N'SYSTEM VIEW' end
  21580.                         end),
  21581.             TABLE_GUID    = convert(uniqueidentifier, null),
  21582.             DESCRIPTION    = convert(nvarchar(1), null),
  21583.             TABLE_PROPID    = convert(int,null),
  21584.             DATE_CREATED    = crdate,
  21585.             DATE_MODIFIED    = convert(datetime,null)
  21586.         from    sysobjects
  21587.         where    name = @table_name
  21588.         and    type in ('U','V','S')
  21589.         and    permissions(id) <> 0
  21590.         ) as o
  21591.  
  21592.     where     (@table_schema is null or @table_schema = TABLE_SCHEMA)
  21593.     and     (@table_type is null or @table_type = TABLE_TYPE)    
  21594.     order by 4, 2, 3
  21595. go
  21596. dump tran master with no_log
  21597. go
  21598. create procedure sp_tables_rowset;2
  21599.     (
  21600.     @table_schema    sysname = null,    
  21601.     @table_type    nvarchar(255) = null 
  21602.     )
  21603. as
  21604.     select    *
  21605.     from    (select    TABLE_CATALOG    = db_name(),
  21606.             TABLE_SCHEMA    = user_name(uid),
  21607.             TABLE_NAME    = name,
  21608.             TABLE_TYPE    = convert(nvarchar(30),
  21609.                         case type 
  21610.                         when 'U' then 
  21611.                             case when ObjectProperty(id, 'IsMSShipped') = 0
  21612.                             then N'TABLE' else N'SYSTEM TABLE' end
  21613.                         when 'S' then N'SYSTEM TABLE'
  21614.                         when 'V' then 
  21615.                             case when ObjectProperty(id, 'IsMSShipped') = 0
  21616.                             then N'VIEW' else N'SYSTEM VIEW' end
  21617.                         end),
  21618.             TABLE_GUID    = convert(uniqueidentifier, null),
  21619.             DESCRIPTION    = convert(nvarchar(1), null),
  21620.             TABLE_PROPID    = convert(int,null),
  21621.             DATE_CREATED    = crdate,
  21622.             DATE_MODIFIED    = convert(datetime,null)
  21623.         from    sysobjects
  21624.         where    type in ('U','V','S')
  21625.         and    permissions(id) <> 0
  21626.         ) as o
  21627.  
  21628.     where     (@table_schema is null or @table_schema = TABLE_SCHEMA)
  21629.     and     (@table_type is null or @table_type = TABLE_TYPE)    
  21630.     order by 4, 2, 3
  21631. go
  21632. dump tran master with no_log
  21633. go
  21634. create procedure sp_tables_rowset;5
  21635.     (
  21636.     @table_server        sysname,
  21637.     @table_catalog        sysname = null,
  21638.     @table_name        sysname = null,
  21639.     @table_schema        sysname = null,
  21640.     @table_type        sysname = null
  21641.     )
  21642. as
  21643.     select
  21644.         TABLE_CATALOG,
  21645.         TABLE_SCHEMA,
  21646.         TABLE_NAME,
  21647.         TABLE_TYPE,
  21648.         TABLE_GUID,
  21649.         DESCRIPTION
  21650.     --    TABLE_PROPID,
  21651.     --    DATE_CREATED,
  21652.     --    DATE_MODIFIED
  21653.     from master.dbo.SYSREMOTE_TABLES <
  21654.                 @table_server,
  21655.                 @table_catalog,
  21656.                 @table_schema,
  21657.                 @table_name,
  21658.                 @table_type >
  21659.     order by 4,1,2,3
  21660. go
  21661.  
  21662. grant execute on sp_tables_rowset to public
  21663. go
  21664.  
  21665. dump tran master with no_log
  21666. go
  21667. if (charindex('6.00', @@version) > 0)
  21668.     begin
  21669.     if (exists (select * from sysobjects
  21670.             where name = 'sp_tables_rowset' and type = 'P '))
  21671.         begin
  21672.         drop procedure sp_tables_rowset
  21673.         dump tran master with no_log
  21674.         end
  21675.     end
  21676. go
  21677.  
  21678.  
  21679.  
  21680. print ''
  21681. print 'creating sp_tables_info_rowset'
  21682. go
  21683.  
  21684.  
  21685. /*    Procedure for 6.50 and earlier servers */
  21686. create procedure sp_tables_info_rowset
  21687.     (
  21688.     @table_name    varchar(255), 
  21689.     @table_schema    varchar(255) = null,    
  21690.     @table_type    varchar(255) = null 
  21691.     )
  21692. as
  21693.     select    TABLE_CATALOG        = db_name(),
  21694.         TABLE_SCHEMA        = user_name(o.uid),
  21695.         TABLE_NAME        = o.name,
  21696.         TABLE_TYPE        = convert(varchar(30),
  21697.                         case o.type 
  21698.                         when 'U' then 'TABLE'
  21699.                         when 'V' then 'VIEW'
  21700.                         when 'S' then 'SYSTEM TABLE'
  21701.                         end),
  21702.         TABLE_GUID        = convert(binary(16), null),
  21703.         BOOKMARKS        = convert(bit, 1),
  21704.         BOOKMARK_TYPE        = convert(int, 1 /*DBPROPVAL_BMK_NUMERIC*/),
  21705.         BOOKMARK_DATATYPE    = convert(smallint, 19 /*DBTYPE_UI4 */),
  21706.         BOOKMARK_MAXIMUM_LENGTH = convert(int, 4),
  21707.         BOOKMARK_INFORMATION    = convert(int, 0),
  21708.         TABLE_VERSION        = convert(int, o.schema_ver),
  21709.         CARDINALITY        = x.rows,
  21710.         DESCRIPTION        = convert(varchar(1), null),
  21711.         TABLE_PROPID        = convert(int, null)
  21712.  
  21713.     from    sysusers u, 
  21714.         sysobjects o,
  21715.         sysindexes x
  21716.  
  21717.     where    o.type in ('U','V','S')
  21718.     and     o.name = @table_name
  21719.     and     (    @table_schema is null
  21720.         or    @table_schema = user_name(o.uid)
  21721.         )
  21722.     and     (
  21723.             @table_type is null
  21724.         or    @table_type = case o.type 
  21725.                     when 'U' then 'TABLE'
  21726.                     when 'V' then 'VIEW'
  21727.                     when 'S' then 'SYSTEM TABLE'
  21728.                     end
  21729.         )
  21730.     and    o.id *= x.id
  21731.     and    x.indid in (0,1)
  21732.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  21733.     and     (
  21734.         suser_id() = 1     /* User is the System Administrator */
  21735.         or o.uid = user_id()     /* User created the object */
  21736.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  21737.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  21738.             from sysprotects p
  21739.             /* join to correlate with all rows in sysobjects */
  21740.             where p.id = o.id
  21741.             /* get rows for public,current user,user's group */
  21742.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  21743.             /* check for SELECT,EXECUTE privilege */
  21744.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  21745.             ) = 1    /* final magic...compare Grants      */
  21746.         )        
  21747.     order by 4, 2, 3
  21748. go
  21749. dump tran master with no_log
  21750. go
  21751. create procedure sp_tables_info_rowset;2
  21752.     (
  21753.     @table_schema    varchar(255) = null,    
  21754.     @table_type    varchar(255) = null 
  21755.     )
  21756. as
  21757.     select    TABLE_CATALOG        = db_name(),
  21758.         TABLE_SCHEMA        = user_name(o.uid),
  21759.         TABLE_NAME        = o.name,
  21760.         TABLE_TYPE        = convert(varchar(30),
  21761.                         case o.type 
  21762.                         when 'U' then 'TABLE'
  21763.                         when 'V' then 'VIEW'
  21764.                         when 'S' then 'SYSTEM TABLE'
  21765.                         end),
  21766.         TABLE_GUID        = convert(binary(16), null),
  21767.         BOOKMARKS        = convert(bit, 1),
  21768.         BOOKMARK_TYPE        = convert(int, 1 /*DBPROPVAL_BMK_NUMERIC*/),
  21769.         BOOKMARK_DATATYPE    = convert(smallint, 19 /*DBTYPE_UI4 */),
  21770.         BOOKMARK_MAXIMUM_LENGTH = convert(int, 4),
  21771.         BOOKMARK_INFORMATION    = convert(int, 0),
  21772.         TABLE_VERSION        = convert(int, o.schema_ver),
  21773.         CARDINALITY        = x.rows,
  21774.         DESCRIPTION        = convert(varchar(1), null),
  21775.         TABLE_PROPID        = convert(int, null)
  21776.  
  21777.     from    sysusers u, 
  21778.         sysobjects o,
  21779.         sysindexes x
  21780.  
  21781.     where    o.type in ('U','V','S')
  21782.     and     (    @table_schema is null
  21783.         or    @table_schema = user_name(o.uid)
  21784.         )
  21785.     and     (
  21786.             @table_type is null
  21787.         or    @table_type = case o.type 
  21788.                     when 'U' then 'TABLE'
  21789.                     when 'V' then 'VIEW'
  21790.                     when 'S' then 'SYSTEM TABLE'
  21791.                     end
  21792.         )
  21793.     and    o.id *= x.id
  21794.     and    x.indid in (0,1)
  21795.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  21796.     and     (
  21797.         suser_id() = 1     /* User is the System Administrator */
  21798.         or o.uid = user_id()     /* User created the object */
  21799.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  21800.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  21801.             from sysprotects p
  21802.             /* join to correlate with all rows in sysobjects */
  21803.             where p.id = o.id
  21804.             /* get rows for public,current user,user's group */
  21805.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  21806.             /* check for SELECT,EXECUTE privilege */
  21807.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  21808.             ) = 1    /* final magic...compare Grants      */
  21809.         )        
  21810.     order by 4, 2, 3
  21811. go
  21812. dump tran master with no_log
  21813. go
  21814.  
  21815. if (charindex('7.00', @@version) > 0 or
  21816.     charindex('8.00', @@version) > 0)
  21817.     drop procedure sp_tables_info_rowset
  21818. else
  21819. begin
  21820.     print ''
  21821.     print ''
  21822.     print 'Warning:'
  21823.     print 'you are installing the stored procedures '
  21824.     print 'on a pre 7.0 SQL Server.'
  21825.     print 'Ignore the following errors.'
  21826. end
  21827. go
  21828.  
  21829. /*    Procedure for 7.0 server */
  21830. create procedure sp_tables_info_rowset
  21831.     (
  21832.     @table_name    sysname, 
  21833.     @table_schema    sysname = null,    
  21834.     @table_type    nvarchar(255) = null 
  21835.     )
  21836. as
  21837.     select    *
  21838.     from    (select    TABLE_CATALOG        = db_name(),
  21839.             TABLE_SCHEMA        = user_name(o.uid),
  21840.             TABLE_NAME        = o.name,
  21841.             TABLE_TYPE        = convert(nvarchar(30),
  21842.                             case o.type 
  21843.                             when 'U' then 
  21844.                                 case when ObjectProperty(o.id, 'IsMSShipped') = 0
  21845.                                 then N'TABLE' else N'SYSTEM TABLE' end
  21846.                             when 'S' then N'SYSTEM TABLE'
  21847.                             when 'V' then 
  21848.                                 case when ObjectProperty(o.id, 'IsMSShipped') = 0
  21849.                                 then N'VIEW' else N'SYSTEM VIEW' end
  21850.                             end),
  21851.             TABLE_GUID        = convert(uniqueidentifier, null),
  21852.             BOOKMARKS        = convert(bit, 1),
  21853.             BOOKMARK_TYPE        = convert(int, 1 /*DBPROPVAL_BMK_NUMERIC*/),
  21854.             BOOKMARK_DATATYPE    = convert(smallint, 19 /*DBTYPE_UI4 */),
  21855.             BOOKMARK_MAXIMUM_LENGTH = convert(int, 4),
  21856.             BOOKMARK_INFORMATION    = convert(int, 0),
  21857.             TABLE_VERSION        = convert(int, o.schema_ver),
  21858.             CARDINALITY        = x.rows,
  21859.             DESCRIPTION        = convert(nvarchar(1), null),
  21860.             TABLE_PROPID        = convert(int, null) 
  21861.     
  21862.         from    sysobjects o left join sysindexes x on o.id = x.id and x.indid in (0,1)
  21863.         where    o.name = @table_name
  21864.         and     o.type in ('U','V','S')
  21865.         and    permissions(o.id) <> 0) as t
  21866.  
  21867.     where     (@table_schema is null or @table_schema = TABLE_SCHEMA)
  21868.     and     (@table_type is null or    @table_type = TABLE_TYPE)
  21869.     order by 4, 2, 3
  21870. go
  21871. dump tran master with no_log
  21872. go
  21873. create procedure sp_tables_info_rowset;2
  21874.     (
  21875.     @table_schema    sysname = null,    
  21876.     @table_type    nvarchar(255) = null 
  21877.     )
  21878. as
  21879.     select    *
  21880.     from    (select    TABLE_CATALOG        = db_name(),
  21881.             TABLE_SCHEMA        = user_name(o.uid),
  21882.             TABLE_NAME        = o.name,
  21883.             TABLE_TYPE        = convert(nvarchar(30),
  21884.                             case o.type 
  21885.                             when 'U' then 
  21886.                                 case when ObjectProperty(o.id, 'IsMSShipped') = 0
  21887.                                 then N'TABLE' else N'SYSTEM TABLE' end
  21888.                             when 'S' then N'SYSTEM TABLE'
  21889.                             when 'V' then 
  21890.                                 case when ObjectProperty(o.id, 'IsMSShipped') = 0
  21891.                                 then N'VIEW' else N'SYSTEM VIEW' end
  21892.                             end),
  21893.             TABLE_GUID        = convert(uniqueidentifier, null),
  21894.             BOOKMARKS        = convert(bit, 1),
  21895.             BOOKMARK_TYPE        = convert(int, 1 /*DBPROPVAL_BMK_NUMERIC*/),
  21896.             BOOKMARK_DATATYPE    = convert(smallint, 19 /*DBTYPE_UI4 */),
  21897.             BOOKMARK_MAXIMUM_LENGTH = convert(int, 4),
  21898.             BOOKMARK_INFORMATION    = convert(int, 0),
  21899.             TABLE_VERSION        = convert(int, o.schema_ver),
  21900.             CARDINALITY        = x.rows,
  21901.             DESCRIPTION        = convert(nvarchar(1), null),
  21902.             TABLE_PROPID        = convert(int, null) 
  21903.     
  21904.         from    sysobjects o left join sysindexes x on o.id = x.id and x.indid in (0,1)
  21905.         where     o.type in ('U','V','S')
  21906.         and    permissions(o.id) <> 0) as t
  21907.  
  21908.     where     (@table_schema is null or @table_schema = TABLE_SCHEMA)
  21909.     and     (@table_type is null or    @table_type = TABLE_TYPE)
  21910.     order by 4, 2, 3
  21911. go
  21912.  
  21913. dump tran master with no_log
  21914. go
  21915.  
  21916. if (charindex('8.00', @@version) > 0)
  21917.     drop procedure sp_tables_info_rowset
  21918. else
  21919. begin
  21920.     print ''
  21921.     print ''
  21922.     print 'Warning:'
  21923.     print 'you are installing the stored procedures '
  21924.     print 'on a pre 8.0 SQL Server.'
  21925.     print 'Ignore the following errors.'
  21926. end
  21927. go
  21928.  
  21929. /*    Procedure for 8.0 server */
  21930. create procedure sp_tables_info_rowset
  21931.     (
  21932.     @table_name    sysname, 
  21933.     @table_schema    sysname = null,    
  21934.     @table_type    nvarchar(255) = null 
  21935.     )
  21936. as
  21937.     select    *
  21938.     from    (select    TABLE_CATALOG        = db_name(),
  21939.             TABLE_SCHEMA        = user_name(o.uid),
  21940.             TABLE_NAME        = o.name,
  21941.             TABLE_TYPE        = convert(nvarchar(30),
  21942.                             case o.type 
  21943.                             when 'U' then 
  21944.                                 case when ObjectProperty(o.id, 'IsMSShipped') = 0
  21945.                                 then N'TABLE' else N'SYSTEM TABLE' end
  21946.                             when 'S' then N'SYSTEM TABLE'
  21947.                             when 'V' then 
  21948.                                 case when ObjectProperty(o.id, 'IsMSShipped') = 0
  21949.                                 then N'VIEW' else N'SYSTEM VIEW' end
  21950.                             end),
  21951.             TABLE_GUID        = convert(uniqueidentifier, null),
  21952.             BOOKMARKS        = convert(bit, 1),
  21953.             BOOKMARK_TYPE        = convert(int, 1 /*DBPROPVAL_BMK_NUMERIC*/),
  21954.             BOOKMARK_DATATYPE    = convert(smallint, 19 /*DBTYPE_UI4 */),
  21955.             BOOKMARK_MAXIMUM_LENGTH = convert(int, 4),
  21956.             BOOKMARK_INFORMATION    = convert(int, 0),
  21957.             TABLE_VERSION        = convert(bigint, ObjectPropertyEx(o.id, 'objectversion')),
  21958.             CARDINALITY        = x.rows,
  21959.             DESCRIPTION        = convert(nvarchar(1), null),
  21960.             TABLE_PROPID        = convert(int, null) 
  21961.     
  21962.         from    sysobjects o left join sysindexes x on o.id = x.id and x.indid in (0,1)
  21963.         where    o.name = @table_name
  21964.         and     o.type in ('U','V','S')
  21965.         and    permissions(o.id) <> 0) as t
  21966.  
  21967.     where     (@table_schema is null or @table_schema = TABLE_SCHEMA)
  21968.     and     (@table_type is null or    @table_type = TABLE_TYPE)
  21969.     order by 4, 2, 3
  21970. go
  21971. dump tran master with no_log
  21972. go
  21973. create procedure sp_tables_info_rowset;2
  21974.     (
  21975.     @table_schema    sysname = null,    
  21976.     @table_type    nvarchar(255) = null 
  21977.     )
  21978. as
  21979.     select    *
  21980.     from    (select    TABLE_CATALOG        = db_name(),
  21981.             TABLE_SCHEMA        = user_name(o.uid),
  21982.             TABLE_NAME        = o.name,
  21983.             TABLE_TYPE        = convert(nvarchar(30),
  21984.                             case o.type 
  21985.                             when 'U' then 
  21986.                                 case when ObjectProperty(o.id, 'IsMSShipped') = 0
  21987.                                 then N'TABLE' else N'SYSTEM TABLE' end
  21988.                             when 'S' then N'SYSTEM TABLE'
  21989.                             when 'V' then 
  21990.                                 case when ObjectProperty(o.id, 'IsMSShipped') = 0
  21991.                                 then N'VIEW' else N'SYSTEM VIEW' end
  21992.                             end),
  21993.             TABLE_GUID        = convert(uniqueidentifier, null),
  21994.             BOOKMARKS        = convert(bit, 1),
  21995.             BOOKMARK_TYPE        = convert(int, 1 /*DBPROPVAL_BMK_NUMERIC*/),
  21996.             BOOKMARK_DATATYPE    = convert(smallint, 19 /*DBTYPE_UI4 */),
  21997.             BOOKMARK_MAXIMUM_LENGTH = convert(int, 4),
  21998.             BOOKMARK_INFORMATION    = convert(int, 0),
  21999.             TABLE_VERSION        = convert(bigint, ObjectPropertyEx(o.id, 'objectversion')),
  22000.             CARDINALITY        = x.rows,
  22001.             DESCRIPTION        = convert(nvarchar(1), null),
  22002.             TABLE_PROPID        = convert(int, null) 
  22003.     
  22004.         from    sysobjects o left join sysindexes x on o.id = x.id and x.indid in (0,1)
  22005.         where     o.type in ('U','V','S')
  22006.         and    permissions(o.id) <> 0) as t
  22007.  
  22008.     where     (@table_schema is null or @table_schema = TABLE_SCHEMA)
  22009.     and     (@table_type is null or    @table_type = TABLE_TYPE)
  22010.     order by 4, 2, 3
  22011. go
  22012.  
  22013. grant execute on sp_tables_info_rowset to public
  22014. go
  22015.  
  22016. dump tran master with no_log
  22017. go
  22018.  
  22019. if (charindex('6.00', @@version) > 0)
  22020.     begin
  22021.     if (exists (select * from sysobjects
  22022.         where name = 'sp_tables_info_rowset' and type = 'P '))
  22023.         begin
  22024.         drop procedure sp_tables_info_rowset
  22025.         dump tran master with no_log
  22026.         end
  22027.     end
  22028. go
  22029.  
  22030. print ''
  22031. print 'creating sp_table_constraints_rowset'
  22032. go
  22033.  
  22034.  
  22035. /*    Procedure for 6.50 and earlier servers */
  22036. create procedure sp_table_constraints_rowset
  22037.     (
  22038.     @table_name            varchar(255), 
  22039.     @table_schema        varchar(255) = null,
  22040.     @table_catalog        varchar(255) = null,
  22041.     @constraint_name    varchar(255) = null,
  22042.     @constraint_schema    varchar(255) = null,
  22043.     @constraint_catalog    varchar(255) = null,
  22044.     @constraint_type    varchar(255) = null 
  22045.     )
  22046. as
  22047.     select
  22048.         CONSTRAINT_CATALOG    = db_name(),
  22049.         CONSTRAINT_SCHEMA    = user_name(c_obj.uid),
  22050.         CONSTRAINT_NAME        = c_obj.name,
  22051.         TABLE_CATALOG        = db_name(),
  22052.         TABLE_SCHEMA        = user_name(t_obj.uid),
  22053.         TABLE_NAME            = t_obj.name,
  22054.         CONSTRAINT_TYPE        = case (c.status & 0xf)
  22055.                                 when 1 then 'PRIMARY KEY'
  22056.                                 when 2 then    'UNIQUE'
  22057.                                 when 3 then    'FOREIGN KEY'
  22058.                                 when 4 then    'CHECK'
  22059.                                  end, 
  22060.         IS_DEFERRABLE        = convert(tinyint, 0),
  22061.         INITIALLY_DEFERRED    = convert(tinyint, 0),
  22062.         DESCRIPTION            = convert(varchar(1), null)
  22063.  
  22064.     from 
  22065.         sysobjects c_obj, sysobjects t_obj, sysconstraints c
  22066.     where
  22067.             t_obj.name    = @table_name
  22068.         and t_obj.type in ('U','S')
  22069.         and (@table_catalog is null or @table_catalog = db_name())
  22070.         and    (@table_schema is null or @table_schema = user_name(t_obj.uid))
  22071.         and c.id = t_obj.id
  22072.         and (c.status & 0xf) between 1 and 4
  22073.         and c_obj.id    = c.constid
  22074.         and    c_obj.uid    = user_id()
  22075.         and (@constraint_name is null or c_obj.name    = @constraint_name)
  22076.         and (@constraint_catalog is null or @constraint_catalog = db_name())
  22077.         and    (@constraint_schema is null or @constraint_schema = user_name(c_obj.uid))
  22078.         and (@constraint_type is null
  22079.             or (c.status & 0xf)
  22080.                 = case @constraint_type
  22081.                     when 'PRIMARY KEY' then 1
  22082.                     when 'UNIQUE' then 2
  22083.                     when 'FOREIGN KEY' then 3
  22084.                     when 'CHECK' then 4
  22085.                     end)
  22086. order by 2,3,5,6,7 
  22087. go
  22088. dump tran master with no_log
  22089. go
  22090. create procedure sp_table_constraints_rowset;2
  22091.     (
  22092.     @table_schema        varchar(255) = null,
  22093.     @table_catalog        varchar(255) = null,
  22094.     @constraint_name    varchar(255) = null,
  22095.     @constraint_schema    varchar(255) = null,
  22096.     @constraint_catalog    varchar(255) = null,
  22097.     @constraint_type    varchar(255) = null 
  22098.     )
  22099. as
  22100.     select
  22101.         CONSTRAINT_CATALOG    = db_name(),
  22102.         CONSTRAINT_SCHEMA    = user_name(c_obj.uid),
  22103.         CONSTRAINT_NAME        = c_obj.name,
  22104.         TABLE_CATALOG        = db_name(),
  22105.         TABLE_SCHEMA        = user_name(t_obj.uid),
  22106.         TABLE_NAME            = t_obj.name,
  22107.         CONSTRAINT_TYPE        = case (c.status & 0xf)
  22108.                                 when 1 then 'PRIMARY KEY'
  22109.                                 when 2 then    'UNIQUE'
  22110.                                 when 3 then    'FOREIGN KEY'
  22111.                                 when 4 then    'CHECK'
  22112.                                  end, 
  22113.         IS_DEFERRABLE        = convert(tinyint, 0),
  22114.         INITIALLY_DEFERRED    = convert(tinyint, 0),
  22115.         DESCRIPTION            = convert(varchar(1), null)
  22116.  
  22117.     from 
  22118.         sysobjects c_obj, sysobjects t_obj, sysconstraints c
  22119.     where
  22120.             t_obj.type in ('U','S')
  22121.         and (@table_catalog is null or @table_catalog = db_name())
  22122.         and    (@table_schema is null or @table_schema = user_name(t_obj.uid))
  22123.         and c.id = t_obj.id
  22124.         and (c.status & 0xf) between 1 and 4
  22125.         and c_obj.id    = c.constid
  22126.         and    c_obj.uid    = user_id()
  22127.         and (@constraint_name is null or c_obj.name    = @constraint_name)
  22128.         and (@constraint_catalog is null or @constraint_catalog = db_name())
  22129.         and    (@constraint_schema is null or @constraint_schema = user_name(c_obj.uid))
  22130.         and (@constraint_type is null
  22131.             or (c.status & 0xf)
  22132.                 = case @constraint_type
  22133.                     when 'PRIMARY KEY' then 1
  22134.                     when 'UNIQUE' then 2
  22135.                     when 'FOREIGN KEY' then 3
  22136.                     when 'CHECK' then 4
  22137.                     end)
  22138. order by 2,3,5,6,7 
  22139. go
  22140. dump tran master with no_log
  22141. go
  22142.  
  22143.  
  22144. if (charindex('7.00', @@version) > 0 or
  22145.     charindex('8.00', @@version) > 0)
  22146.     drop procedure sp_table_constraints_rowset
  22147. else
  22148. begin
  22149.     print ''
  22150.     print ''
  22151.     print 'Warning:'
  22152.     print 'you are installing the stored procedures '
  22153.     print 'on a pre 8.0 SQL Server.'
  22154.     print 'Ignore the following errors.'
  22155. end
  22156. go
  22157.  
  22158. /*    Procedure for 8.0 server */
  22159. create procedure sp_table_constraints_rowset
  22160.     (
  22161.     @table_name            sysname, 
  22162.     @table_schema        sysname = null,
  22163.     @table_catalog        sysname = null,
  22164.     @constraint_name    sysname = null,
  22165.     @constraint_schema    sysname = null,
  22166.     @constraint_catalog    sysname = null,
  22167.     @constraint_type    nvarchar(255) = null 
  22168.     )
  22169. as
  22170.     select
  22171.         CONSTRAINT_CATALOG    = db_name(),
  22172.         CONSTRAINT_SCHEMA    = user_name(c_obj.uid),
  22173.         CONSTRAINT_NAME        = c_obj.name,
  22174.         TABLE_CATALOG        = db_name(),
  22175.         TABLE_SCHEMA        = user_name(t_obj.uid),
  22176.         TABLE_NAME            = t_obj.name,
  22177.         CONSTRAINT_TYPE        = case (c.status & 0xf)
  22178.                                 when 1 then N'PRIMARY KEY'
  22179.                                 when 2 then    N'UNIQUE'
  22180.                                 when 3 then    N'FOREIGN KEY'
  22181.                                 when 4 then    N'CHECK'
  22182.                                  end, 
  22183.         IS_DEFERRABLE        = convert(bit, 0),
  22184.         INITIALLY_DEFERRED    = convert(bit, 0),
  22185.         DESCRIPTION            = convert(nvarchar(1), null)
  22186.  
  22187.     from 
  22188.         sysobjects c_obj, sysobjects t_obj, sysconstraints c
  22189.     where
  22190.             t_obj.name    = @table_name
  22191.         and t_obj.type in ('U','S')
  22192.         and (@table_catalog is null or @table_catalog = db_name())
  22193.         and    (@table_schema is null or @table_schema = user_name(t_obj.uid))
  22194.         and c.id = t_obj.id
  22195.         and (c.status & 0xf) between 1 and 4
  22196.         and c_obj.id    = c.constid
  22197.         and    c_obj.uid    = user_id()
  22198.         and (@constraint_name is null or c_obj.name    = @constraint_name)
  22199.         and (@constraint_catalog is null or @constraint_catalog = db_name())
  22200.         and    (@constraint_schema is null or @constraint_schema = user_name(c_obj.uid))
  22201.         and (@constraint_type is null
  22202.             or (c.status & 0xf)
  22203.                 = case @constraint_type
  22204.                     when N'PRIMARY KEY' then 1
  22205.                     when N'UNIQUE' then 2
  22206.                     when N'FOREIGN KEY' then 3
  22207.                     when N'CHECK' then 4
  22208.                     end)
  22209. order by 2,3,5,6,7 
  22210. go
  22211. dump tran master with no_log
  22212. go
  22213. create procedure sp_table_constraints_rowset;2
  22214.     (
  22215.     @table_schema        sysname = null,
  22216.     @table_catalog        sysname = null,
  22217.     @constraint_name    sysname = null,
  22218.     @constraint_schema    sysname = null,
  22219.     @constraint_catalog    sysname = null,
  22220.     @constraint_type    nvarchar(255) = null 
  22221.     )
  22222. as
  22223.     select
  22224.         CONSTRAINT_CATALOG    = db_name(),
  22225.         CONSTRAINT_SCHEMA    = user_name(c_obj.uid),
  22226.         CONSTRAINT_NAME        = c_obj.name,
  22227.         TABLE_CATALOG        = db_name(),
  22228.         TABLE_SCHEMA        = user_name(t_obj.uid),
  22229.         TABLE_NAME            = t_obj.name,
  22230.         CONSTRAINT_TYPE        = case (c.status & 0xf)
  22231.                                 when 1 then N'PRIMARY KEY'
  22232.                                 when 2 then    N'UNIQUE'
  22233.                                 when 3 then    N'FOREIGN KEY'
  22234.                                 when 4 then    N'CHECK'
  22235.                                  end, 
  22236.         IS_DEFERRABLE        = convert(bit, 0),
  22237.         INITIALLY_DEFERRED    = convert(bit, 0),
  22238.         DESCRIPTION            = convert(nvarchar(1), null)
  22239.  
  22240.     from 
  22241.         sysobjects c_obj, sysobjects t_obj, sysconstraints c
  22242.     where
  22243.             t_obj.type in ('U','S')
  22244.         and (@table_catalog is null or @table_catalog = db_name())
  22245.         and    (@table_schema is null or @table_schema = user_name(t_obj.uid))
  22246.         and c.id = t_obj.id
  22247.         and (c.status & 0xf) between 1 and 4
  22248.         and c_obj.id    = c.constid
  22249.         and    c_obj.uid    = user_id()
  22250.         and (@constraint_name is null or c_obj.name    = @constraint_name)
  22251.         and (@constraint_catalog is null or @constraint_catalog = db_name())
  22252.         and    (@constraint_schema is null or @constraint_schema = user_name(c_obj.uid))
  22253.         and (@constraint_type is null
  22254.             or (c.status & 0xf)
  22255.                 = case @constraint_type
  22256.                     when N'PRIMARY KEY' then 1
  22257.                     when N'UNIQUE' then 2
  22258.                     when N'FOREIGN KEY' then 3
  22259.                     when N'CHECK' then 4
  22260.                     end)
  22261. order by 2,3,5,6,7 
  22262. go
  22263. dump tran master with no_log
  22264.  
  22265.  
  22266. grant execute on sp_table_constraints_rowset to public
  22267. go
  22268.  
  22269. dump tran master with no_log
  22270. go
  22271. if (charindex('6.00', @@version) > 0)
  22272.     begin
  22273.     if (exists (select * from sysobjects
  22274.             where name = 'sp_table_constraints_rowset' and type = 'P '))
  22275.         begin
  22276.         drop procedure sp_table_constraints_rowset
  22277.         dump tran master with no_log
  22278.         end
  22279.     end
  22280. go
  22281.  
  22282. print ''
  22283. print 'creating sp_table_privileges_rowset'
  22284. go
  22285.  
  22286. /*    Procedure for 6.0 and 6.5 server */
  22287. CREATE PROCEDURE sp_table_privileges_rowset
  22288.     (
  22289.     @table_name    varchar(255) = null,
  22290.     @table_schema    varchar(255) = null,
  22291.     @grantor    varchar(255) = null,
  22292.     @grantee    varchar(255) = null
  22293.     )
  22294. as
  22295. IF @table_name is not null
  22296.     BEGIN
  22297.     select
  22298.         GRANTOR        = user_name(p.grantor),
  22299.         GRANTEE        = user_name(u.uid),
  22300.         TABLE_CATALOG    = db_name(),
  22301.         TABLE_SCHEMA    = user_name(o.uid),
  22302.         TABLE_NAME    = o.name,
  22303.         PRIVILEGE_TYPE    = convert(varchar(30),
  22304.                     case p.action
  22305.                     when 193 then 'SELECT'
  22306.                     when 195 then 'INSERT'
  22307.                     when 196 then 'DELETE'
  22308.                     when 197 then 'UPDATE'
  22309.                     else 'REFERENCES'
  22310.                     end),
  22311.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  22312.     from 
  22313.         sysprotects p, sysobjects o, sysusers u
  22314.     where
  22315.         o.name = @table_name
  22316.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  22317.     and    (@grantee is null or @grantee = user_name(u.uid))
  22318.     and     o.type in ('U','V','S')
  22319.     and    p.id = o.id
  22320.     and     (@grantor is null or @grantor = user_name(p.grantor))
  22321.             /* expand groups */
  22322.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  22323.     and     p.protecttype <> 206    /* only grant rows */
  22324.     and     p.action in (26,193,195,196,197)
  22325.     and     o.uid <> u.uid            /* no rows for owner */
  22326.     and     not exists (            /* exclude revoke'd privileges */
  22327.             select     *
  22328.             from     sysprotects p1
  22329.             where    p1.protecttype = 206
  22330.             and     p1.action = p.action
  22331.             and     p1.id = p.id
  22332.             and     p1.uid = u.uid)
  22333.     union
  22334.     select    /*    Add rows for table owner */
  22335.         GRANTOR        = user_name(u.uid),
  22336.         GRANTEE        = user_name(o.uid),
  22337.         TABLE_CATALOG    = db_name(),
  22338.         TABLE_SCHEMA    = user_name(o.uid),
  22339.         TABLE_NAME    = o.name,
  22340.         PRIVILEGE_TYPE    = convert(varchar(30),
  22341.                     case v.number
  22342.                     when 193 then 'SELECT'
  22343.                     when 195 then 'INSERT'
  22344.                     when 196 then 'DELETE'
  22345.                     when 197 then 'UPDATE'
  22346.                     else 'REFERENCES'
  22347.                     end),
  22348.         IS_GRANTABLE    = convert(bit,1)    
  22349.     from 
  22350.         sysobjects o, master.dbo.spt_values v, sysusers u
  22351.     where
  22352.         o.name = @table_name
  22353.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  22354.     and    (@grantee is null or @grantee = user_name(o.uid))
  22355.     and     o.type in ('U','V','S')
  22356.     and     u.uid = 1        /* grantor is dbo of database */
  22357.     and    (@grantor is null or @grantor = user_name(u.uid))
  22358.     and     v.type = 'P'    /* cross product to get all exposed privileges */
  22359.     and     v.number in (26,193,195,196,197)
  22360.     and     not exists (    /* exclude revoke'd privileges */
  22361.             select     *
  22362.             from     sysprotects p1
  22363.             where    p1.protecttype = 206
  22364.             and     p1.action = v.number
  22365.             and     p1.id = o.id
  22366.             and     p1.uid = o.uid)
  22367.     order by 4,5,6,1,2
  22368.     END
  22369. ELSE
  22370.     BEGIN
  22371.     select
  22372.         GRANTOR        = user_name(p.grantor),
  22373.         GRANTEE        = user_name(u.uid),
  22374.         TABLE_CATALOG    = db_name(),
  22375.         TABLE_SCHEMA    = user_name(o.uid),
  22376.         TABLE_NAME    = o.name,
  22377.         PRIVILEGE_TYPE    = convert(varchar(30),
  22378.                     case p.action
  22379.                     when 193 then 'SELECT'
  22380.                     when 195 then 'INSERT'
  22381.                     when 196 then 'DELETE'
  22382.                     when 197 then 'UPDATE'
  22383.                     else 'REFERENCES'
  22384.                     end),
  22385.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  22386.     from 
  22387.         sysprotects p, sysobjects o, sysusers u
  22388.     where
  22389.         o.type in ('U','V','S')
  22390.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  22391.     and    (@grantee is null or @grantee = user_name(u.uid))
  22392.     and    p.id = o.id
  22393.     and     (@grantor is null or @grantor = user_name(p.grantor))
  22394.             /* expand groups */
  22395.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  22396.     and     p.protecttype <> 206    /* only grant rows */
  22397.     and     p.action in (26,193,195,196,197)
  22398.     and     o.uid <> u.uid            /* no rows for owner */
  22399.     and     not exists (            /* exclude revoke'd privileges */
  22400.             select     *
  22401.             from     sysprotects p1
  22402.             where    p1.protecttype = 206
  22403.             and     p1.action = p.action
  22404.             and     p1.id = p.id
  22405.             and     p1.uid = u.uid)
  22406.     union
  22407.     select    /*    Add rows for table owner */
  22408.         GRANTOR        = user_name(u.uid),
  22409.         GRANTEE        = user_name(o.uid),
  22410.         TABLE_CATALOG    = db_name(),
  22411.         TABLE_SCHEMA    = user_name(o.uid),
  22412.         TABLE_NAME    = o.name,
  22413.         PRIVILEGE_TYPE    = convert(varchar(30),
  22414.                     case v.number
  22415.                     when 193 then 'SELECT'
  22416.                     when 195 then 'INSERT'
  22417.                     when 196 then 'DELETE'
  22418.                     when 197 then 'UPDATE'
  22419.                     else 'REFERENCES'
  22420.                     end),
  22421.         IS_GRANTABLE    = convert(bit,1)    
  22422.     from 
  22423.         sysobjects o, master.dbo.spt_values v, sysusers u
  22424.     where
  22425.         o.type in ('U','V','S')
  22426.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  22427.     and    (@grantee is null or @grantee = user_name(o.uid))
  22428.     and     u.uid = 1        /* grantor is dbo of database */
  22429.     and    (@grantor is null or @grantor = user_name(u.uid))
  22430.     and     v.type = 'P'    /* cross product to get all exposed privileges */
  22431.     and     v.number in (26,193,195,196,197)
  22432.     and     not exists (    /* exclude revoke'd privileges */
  22433.             select     *
  22434.             from     sysprotects p1
  22435.             where    p1.protecttype = 206
  22436.             and     p1.action = v.number
  22437.             and     p1.id = o.id
  22438.             and     p1.uid = o.uid)
  22439.     order by 4,5,6,1,2
  22440.     END
  22441. go
  22442. dump tran master with no_log
  22443. go
  22444. CREATE PROCEDURE sp_table_privileges_rowset;2
  22445.     (
  22446.        @handle        int output,
  22447.        @scrollopt    int output,
  22448.     @ccopt        int output,
  22449.     @rows        int output,
  22450.     @table_name     varchar(255) = null,
  22451.     @table_schema    varchar(255) = null,
  22452.     @grantor    varchar(255) = null,
  22453.     @grantee    varchar(255) = null
  22454.     )
  22455. as
  22456. declare @ret int
  22457. SET NOCOUNT ON
  22458. create table #sptprivsrowset1
  22459.     (
  22460.     GRANTOR        sysname not null,
  22461.     GRANTEE        sysname not null,
  22462.     TABLE_CATALOG    sysname not null,
  22463.     TABLE_SCHEMA    sysname not null,
  22464.     TABLE_NAME    sysname not null,
  22465.     PRIVILEGE_TYPE    sysname not null,
  22466.     IS_GRANTABLE    bit not null
  22467.     )
  22468.     
  22469. IF @table_name is not null
  22470.     BEGIN
  22471.     insert into #sptprivsrowset1
  22472.     select
  22473.         GRANTOR        = user_name(p.grantor),
  22474.         GRANTEE        = user_name(u.uid),
  22475.         TABLE_CATALOG    = db_name(),
  22476.         TABLE_SCHEMA    = user_name(o.uid),
  22477.         TABLE_NAME    = o.name,
  22478.         PRIVILEGE_TYPE    = convert(varchar(30),
  22479.                     case p.action
  22480.                     when 193 then 'SELECT'
  22481.                     when 195 then 'INSERT'
  22482.                     when 196 then 'DELETE'
  22483.                     when 197 then 'UPDATE'
  22484.                     else 'REFERENCES'
  22485.                     end),
  22486.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  22487.     from 
  22488.         sysprotects p, sysobjects o, sysusers u
  22489.     where
  22490.         o.name = @table_name
  22491.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  22492.     and    (@grantee is null or @grantee = user_name(u.uid))
  22493.     and     o.type in ('U','V','S')
  22494.     and    p.id = o.id
  22495.     and     (@grantor is null or @grantor = user_name(p.grantor))
  22496.             /* expand groups */
  22497.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  22498.     and     p.protecttype <> 206    /* only grant rows */
  22499.     and     p.action in (26,193,195,196,197)
  22500.     and     o.uid <> u.uid            /* no rows for owner */
  22501.     and     not exists (            /* exclude revoke'd privileges */
  22502.             select     *
  22503.             from     sysprotects p1
  22504.             where    p1.protecttype = 206
  22505.             and     p1.action = p.action
  22506.             and     p1.id = p.id
  22507.             and     p1.uid = u.uid)
  22508.     union
  22509.     select    /*    Add rows for table owner */
  22510.         GRANTOR        = user_name(u.uid),
  22511.         GRANTEE        = user_name(o.uid),
  22512.         TABLE_CATALOG    = db_name(),
  22513.         TABLE_SCHEMA    = user_name(o.uid),
  22514.         TABLE_NAME    = o.name,
  22515.         PRIVILEGE_TYPE    = convert(varchar(30),
  22516.                     case v.number
  22517.                     when 193 then 'SELECT'
  22518.                     when 195 then 'INSERT'
  22519.                     when 196 then 'DELETE'
  22520.                     when 197 then 'UPDATE'
  22521.                     else 'REFERENCES'
  22522.                     end),
  22523.         IS_GRANTABLE    = convert(bit,1)    
  22524.     from 
  22525.         sysobjects o, master.dbo.spt_values v, sysusers u
  22526.     where
  22527.         o.name = @table_name
  22528.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  22529.     and    (@grantee is null or @grantee = user_name(o.uid))
  22530.     and     o.type in ('U','V','S')
  22531.     and     u.uid = 1        /* grantor is dbo of database */
  22532.     and    (@grantor is null or @grantor = user_name(u.uid))
  22533.     and     v.type = 'P'    /* cross product to get all exposed privileges */
  22534.     and     v.number in (26,193,195,196,197)
  22535.     and     not exists (    /* exclude revoke'd privileges */
  22536.             select     *
  22537.             from     sysprotects p1
  22538.             where    p1.protecttype = 206
  22539.             and     p1.action = v.number
  22540.             and     p1.id = o.id
  22541.             and     p1.uid = o.uid)
  22542.     order by 4,5,6,1,2
  22543.     END
  22544. ELSE
  22545.     BEGIN
  22546.     insert into #sptprivsrowset1
  22547.     select
  22548.         GRANTOR        = user_name(p.grantor),
  22549.         GRANTEE        = user_name(u.uid),
  22550.         TABLE_CATALOG    = db_name(),
  22551.         TABLE_SCHEMA    = user_name(o.uid),
  22552.         TABLE_NAME    = o.name,
  22553.         PRIVILEGE_TYPE    = convert(varchar(30),
  22554.                     case p.action
  22555.                     when 193 then 'SELECT'
  22556.                     when 195 then 'INSERT'
  22557.                     when 196 then 'DELETE'
  22558.                     when 197 then 'UPDATE'
  22559.                     else 'REFERENCES'
  22560.                     end),
  22561.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  22562.     from 
  22563.         sysprotects p, sysobjects o, sysusers u
  22564.     where
  22565.         o.type in ('U','V','S')
  22566.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  22567.     and    (@grantee is null or @grantee = user_name(u.uid))
  22568.     and    p.id = o.id
  22569.     and     (@grantor is null or @grantor = user_name(p.grantor))
  22570.             /* expand groups */
  22571.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  22572.     and     p.protecttype <> 206    /* only grant rows */
  22573.     and     p.action in (26,193,195,196,197)
  22574.     and     o.uid <> u.uid            /* no rows for owner */
  22575.     and     not exists (            /* exclude revoke'd privileges */
  22576.             select     *
  22577.             from     sysprotects p1
  22578.             where    p1.protecttype = 206
  22579.             and     p1.action = p.action
  22580.             and     p1.id = p.id
  22581.             and     p1.uid = u.uid)
  22582.     union
  22583.     select    /*    Add rows for table owner */
  22584.         GRANTOR        = user_name(u.uid),
  22585.         GRANTEE        = user_name(o.uid),
  22586.         TABLE_CATALOG    = db_name(),
  22587.         TABLE_SCHEMA    = user_name(o.uid),
  22588.         TABLE_NAME    = o.name,
  22589.         PRIVILEGE_TYPE    = convert(varchar(30),
  22590.                     case v.number
  22591.                     when 193 then 'SELECT'
  22592.                     when 195 then 'INSERT'
  22593.                     when 196 then 'DELETE'
  22594.                     when 197 then 'UPDATE'
  22595.                     else 'REFERENCES'
  22596.                     end),
  22597.         IS_GRANTABLE    = convert(bit,1)    
  22598.     from 
  22599.         sysobjects o, master.dbo.spt_values v, sysusers u
  22600.     where
  22601.         o.type in ('U','V','S')
  22602.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  22603.     and    (@grantee is null or @grantee = user_name(o.uid))
  22604.     and     u.uid = 1        /* grantor is dbo of database */
  22605.     and    (@grantor is null or @grantor = user_name(u.uid))
  22606.     and     v.type = 'P'    /* cross product to get all exposed privileges */
  22607.     and     v.number in (26,193,195,196,197)
  22608.     and     not exists (    /* exclude revoke'd privileges */
  22609.             select     *
  22610.             from     sysprotects p1
  22611.             where    p1.protecttype = 206
  22612.             and     p1.action = v.number
  22613.             and     p1.id = o.id
  22614.             and     p1.uid = o.uid)
  22615.     order by 4,5,6,1,2
  22616.     END
  22617.  
  22618. exec @ret = sp_cursoropen @handle output, 'select * from #sptprivsrowset1',
  22619.     @scrollopt output, @ccopt output, @rows output
  22620.  
  22621. drop table #sptprivsrowset1
  22622. return isnull(@ret,0)
  22623. go
  22624. dump tran master with no_log
  22625. go
  22626. CREATE PROCEDURE sp_table_privileges_rowset;3
  22627. as
  22628.     select
  22629.         GRANTOR        = convert(sysname, ' '),
  22630.         GRANTEE        = convert(sysname, ' '),
  22631.         TABLE_CATALOG    = convert(sysname, ' '),
  22632.         TABLE_SCHEMA    = convert(sysname, ' '),
  22633.         TABLE_NAME    = convert(sysname, ' '),
  22634.         PRIVILEGE_TYPE    = convert(varchar(30), ' '),
  22635.         IS_GRANTABLE    = convert(bit, 0)
  22636.     where    1=0
  22637. go
  22638.  
  22639. if (charindex('7.00', @@version) = 0 and
  22640.     charindex('8.00', @@version) = 0)
  22641. begin
  22642.     print ''
  22643.     print ''
  22644.     print 'Warning:'
  22645.     print 'you are installing the stored procedures '
  22646.     print 'on a pre 8.0 SQL Server.'
  22647.     print 'Ignore the following errors.'
  22648. end
  22649. else
  22650.     drop proc sp_table_privileges_rowset
  22651. go
  22652.  
  22653.  
  22654. /*    Procedure for 8.0 server */
  22655. CREATE PROCEDURE sp_table_privileges_rowset
  22656.     (
  22657.     @table_name         sysname,
  22658.     @table_schema        sysname = null,
  22659.     @grantor        sysname = null,
  22660.     @grantee        sysname = null
  22661.     )
  22662. as
  22663.     select
  22664.         GRANTOR        = user_name(p.grantor),
  22665.         GRANTEE        = user_name(u.uid),
  22666.         TABLE_CATALOG    = db_name(),
  22667.         TABLE_SCHEMA    = user_name(o.uid),
  22668.         TABLE_NAME    = o.name,
  22669.         PRIVILEGE_TYPE    = convert(nvarchar(30),
  22670.                     case p.action
  22671.                     when 193 then N'SELECT'
  22672.                     when 195 then N'INSERT'
  22673.                     when 196 then N'DELETE'
  22674.                     when 197 then N'UPDATE'
  22675.                     else N'REFERENCES'
  22676.                     end),
  22677.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  22678.     from 
  22679.         sysprotects p, sysobjects o, sysusers u, sysmembers m
  22680.     where
  22681.         o.name = @table_name
  22682.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  22683.     and    (@grantee is null or @grantee = user_name(u.uid))
  22684.     and     o.type in ('U','V','S')
  22685.     and    p.id = o.id
  22686.     and     (@grantor is null or @grantor = user_name(p.grantor))
  22687.         /* expand groups - AKUNDONE: only 1 level of grp unrolling. */
  22688.     and     (u.uid > 0 and u.uid < 16384)
  22689.     and     ((p.uid = u.uid) or (p.uid = m.groupuid and u.uid = m.memberuid))
  22690.     and     p.protecttype <> 206    /* only grant rows */
  22691.     and     p.action in (26,193,195,196,197)
  22692.     and     o.uid <> u.uid            /* no rows for owner */
  22693.     and     not exists (            /* exclude revoke'd privileges */
  22694.             select     *
  22695.             from     sysprotects p1
  22696.             where    p1.protecttype = 206
  22697.             and     p1.action = p.action
  22698.             and     p1.id = p.id
  22699.             and     p1.uid = u.uid)
  22700.     union
  22701.     select    /*    Add rows for table owner */
  22702.         GRANTOR        = user_name(u.uid),
  22703.         GRANTEE        = user_name(o.uid),
  22704.         TABLE_CATALOG    = db_name(),
  22705.         TABLE_SCHEMA    = user_name(o.uid),
  22706.         TABLE_NAME    = o.name,
  22707.         PRIVILEGE_TYPE    = convert(nvarchar(30),
  22708.                     case v.number
  22709.                     when 193 then N'SELECT'
  22710.                     when 195 then N'INSERT'
  22711.                     when 196 then N'DELETE'
  22712.                     when 197 then N'UPDATE'
  22713.                     else N'REFERENCES'
  22714.                     end),
  22715.         IS_GRANTABLE    = convert(bit,1)    
  22716.     from 
  22717.         sysobjects o, master.dbo.spt_values v, sysusers u
  22718.     where
  22719.         o.name = @table_name
  22720.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  22721.     and    (@grantee is null or @grantee = user_name(o.uid))
  22722.     and     o.type in ('U','V','S')
  22723.     and     u.uid = 1        /* grantor is dbo of database */
  22724.     and    (@grantor is null or @grantor = user_name(u.uid))
  22725.     and     v.type = 'P'    /* cross product to get all exposed privileges */
  22726.     and     v.number in (26,193,195,196,197)
  22727.     and     not exists (    /* exclude revoke'd privileges */
  22728.             select     *
  22729.             from     sysprotects p1
  22730.             where    p1.protecttype = 206
  22731.             and     p1.action = v.number
  22732.             and     p1.id = o.id
  22733.             and     p1.uid = o.uid)
  22734.     order by 4,5,6,1,2
  22735. go
  22736. dump tran master with no_log
  22737. go
  22738. CREATE PROCEDURE sp_table_privileges_rowset;2
  22739.     (
  22740.     @table_schema        sysname = null,
  22741.     @grantor        sysname = null,
  22742.     @grantee        sysname = null
  22743.     )
  22744. as
  22745.     select
  22746.         GRANTOR        = user_name(p.grantor),
  22747.         GRANTEE        = user_name(u.uid),
  22748.         TABLE_CATALOG    = db_name(),
  22749.         TABLE_SCHEMA    = user_name(o.uid),
  22750.         TABLE_NAME    = o.name,
  22751.         PRIVILEGE_TYPE    = convert(nvarchar(30),
  22752.                     case p.action
  22753.                     when 193 then N'SELECT'
  22754.                     when 195 then N'INSERT'
  22755.                     when 196 then N'DELETE'
  22756.                     when 197 then N'UPDATE'
  22757.                     else N'REFERENCES'
  22758.                     end),
  22759.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  22760.     from 
  22761.         sysprotects p, sysobjects o, sysusers u, sysmembers m
  22762.     where
  22763.         o.type in ('U','V','S')
  22764.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  22765.     and    (@grantee is null or @grantee = user_name(u.uid))
  22766.     and    p.id = o.id
  22767.     and     (@grantor is null or @grantor = user_name(p.grantor))
  22768.         /* expand groups - AKUNDONE: only 1 level of grp unrolling. */
  22769.     and     (u.uid > 0 and u.uid < 16384)
  22770.     and     ((p.uid = u.uid) or (p.uid = m.groupuid and u.uid = m.memberuid))
  22771.     and     p.protecttype <> 206    /* only grant rows */
  22772.     and     p.action in (26,193,195,196,197)
  22773.     and     o.uid <> u.uid            /* no rows for owner */
  22774.     and     not exists (            /* exclude revoke'd privileges */
  22775.             select     *
  22776.             from     sysprotects p1
  22777.             where    p1.protecttype = 206
  22778.             and     p1.action = p.action
  22779.             and     p1.id = p.id
  22780.             and     p1.uid = u.uid)
  22781.     union
  22782.     select    /*    Add rows for table owner */
  22783.         GRANTOR        = user_name(u.uid),
  22784.         GRANTEE        = user_name(o.uid),
  22785.         TABLE_CATALOG    = db_name(),
  22786.         TABLE_SCHEMA    = user_name(o.uid),
  22787.         TABLE_NAME    = o.name,
  22788.         PRIVILEGE_TYPE    = convert(nvarchar(30),
  22789.                     case v.number
  22790.                     when 193 then N'SELECT'
  22791.                     when 195 then N'INSERT'
  22792.                     when 196 then N'DELETE'
  22793.                     when 197 then N'UPDATE'
  22794.                     else N'REFERENCES'
  22795.                     end),
  22796.         IS_GRANTABLE    = convert(bit,1)    
  22797.     from 
  22798.         sysobjects o, master.dbo.spt_values v, sysusers u
  22799.     where
  22800.         o.type in ('U','V','S')
  22801.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  22802.     and    (@grantee is null or @grantee = user_name(o.uid))
  22803.     and     u.uid = 1        /* grantor is dbo of database */
  22804.     and    (@grantor is null or @grantor = user_name(u.uid))
  22805.     and     v.type = 'P'    /* cross product to get all exposed privileges */
  22806.     and     v.number in (26,193,195,196,197)
  22807.     and     not exists (    /* exclude revoke'd privileges */
  22808.             select     *
  22809.             from     sysprotects p1
  22810.             where    p1.protecttype = 206
  22811.             and     p1.action = v.number
  22812.             and     p1.id = o.id
  22813.             and     p1.uid = o.uid)
  22814.     order by 4,5,6,1,2
  22815. go
  22816. dump tran master with no_log
  22817. go
  22818. create procedure sp_table_privileges_rowset;5
  22819.     (
  22820.     @table_server        sysname,
  22821.     @table_catalog        sysname = null,
  22822.     @table_name        sysname = null,
  22823.     @table_schema        sysname = null,
  22824.     @grantor        sysname = null,
  22825.     @grantee        sysname = null
  22826.     )
  22827. as
  22828.     select
  22829.         GRANTOR,        
  22830.         GRANTEE,        
  22831.         TABLE_CATALOG,    
  22832.         TABLE_SCHEMA,    
  22833.         TABLE_NAME,    
  22834.         PRIVILEGE_TYPE,    
  22835.         IS_GRANTABLE    
  22836.     from master.dbo.SYSREMOTE_TABLE_PRIVILEGES <
  22837.                 @table_server,
  22838.                 @table_catalog,
  22839.                 @table_schema,
  22840.                 @table_name,
  22841.                 @grantor,
  22842.                 @grantee >
  22843.     order by 3,4,5,6,1,2
  22844. go
  22845.  
  22846. grant execute on sp_table_privileges_rowset to public
  22847. go
  22848. dump tran master with no_log
  22849. go
  22850. if (charindex('6.00', @@version) > 0)
  22851.     begin
  22852.     if (exists (select * from sysobjects
  22853.         where name = 'sp_table_privileges_rowset' and type = 'P '))
  22854.         begin
  22855.         drop proc sp_table_privileges_rowset
  22856.         dump tran master with no_log
  22857.         end
  22858.     end
  22859. go
  22860.  
  22861.  
  22862. print ''
  22863. print 'creating sp_linkedservers_rowset'
  22864. go
  22865. if (charindex('7.00', @@version) = 0 and
  22866.     charindex('8.00', @@version) = 0)
  22867. begin
  22868.     print ''
  22869.     print ''
  22870.     print 'Warning:'
  22871.     print 'you are installing the stored procedures '
  22872.     print 'on a pre 8.0 SQL Server.'
  22873.     print 'Ignore the following errors.'
  22874. end
  22875. go
  22876. /*    Procedure for 8.0 server */
  22877. create proc sp_linkedservers_rowset
  22878.     (
  22879.     @srvname     sysname
  22880.     )
  22881. as
  22882.     select
  22883.         SVR_NAME         = srvname,
  22884.         SVR_PRODUCT        = srvproduct,
  22885.         SVR_PROVIDERNAME    = providername,
  22886.         SVR_DATASOURCE        = datasource,
  22887.         SVR_PROVIDERSTRING    = providerstring,
  22888.         SVR_LOCATION        = location,
  22889.         SVR_CATALOG        = catalog
  22890.     from master.dbo.sysservers
  22891.     where srvname = @srvname and (srvstatus & 128) = 128
  22892.     order by 1
  22893. go
  22894. dump tran master with no_log
  22895. go
  22896. create proc sp_linkedservers_rowset;2
  22897. as
  22898.     select
  22899.         SVR_NAME         = srvname,
  22900.         SVR_PRODUCT        = srvproduct,
  22901.         SVR_PROVIDERNAME    = providername,
  22902.         SVR_DATASOURCE        = datasource,
  22903.         SVR_PROVIDERSTRING    = providerstring,
  22904.         SVR_LOCATION        = location,
  22905.         SVR_CATALOG        = catalog
  22906.     from master.dbo.sysservers
  22907.     where (srvstatus & 128) = 128
  22908.     order by 1
  22909. go
  22910.  
  22911. grant execute on sp_linkedservers_rowset to public
  22912. go
  22913.  
  22914. dump tran master with no_log
  22915. go
  22916.  
  22917. print ''
  22918. print 'creating sp_table_statistics_rowset'
  22919. go
  22920. if (charindex('8.00', @@version) = 0)
  22921. begin
  22922.     print ''
  22923.     print ''
  22924.     print 'Warning:'
  22925.     print 'you are installing the stored procedures '
  22926.     print 'on a pre 8.00 SQL Server.'
  22927.     print 'Ignore the following errors.'
  22928. end
  22929. go
  22930. /*    Procedure for 8.00 server */
  22931. create proc sp_table_statistics_rowset
  22932. as
  22933.     select
  22934.         TABLE_CATALOG = convert(sysname, null),
  22935.         TABLE_SCHEMA = convert(sysname, null),
  22936.         TABLE_NAME = convert(sysname, null),
  22937.         STATISTICS_CATALOG = convert(sysname, null),
  22938.         STATISTICS_SCHEMA = convert(sysname, null),
  22939.         STATISTICS_NAME = convert(sysname, null),
  22940.         STATISTICS_TYPE = convert(smallint,0),
  22941.         COLUMN_NAME = convert(sysname, null),
  22942.         COLUMN_GUID = convert(uniqueidentifier, null),
  22943.         COLUMN_PROPID = convert(int, null),
  22944.         ORDINAL_POSITION = convert(int, null),
  22945.         SAMPLE_PCT = convert(smallint, null),
  22946.         LAST_UPDATE_TIME = convert(datetime, null),
  22947.         NO_OF_RANGES = convert(int, null),
  22948.         COLUMN_CARDINALITY = convert(bigint, null),
  22949.         TUPLE_CARDINALITY = convert(bigint, null),
  22950.         TABLE_CARDINALITY = convert(bigint, null),
  22951.         AVG_COLUMN_LENGTH = convert(int, null)
  22952.     where 1=0
  22953. go
  22954. dump tran master with no_log
  22955. go
  22956.  
  22957. create proc sp_table_statistics_rowset;2
  22958.                 (@table_catalog        sysname = null,
  22959.                 @table_schema        sysname = null,
  22960.                 @table_name        sysname = null,
  22961.                 @stat_catalog        sysname = null,
  22962.                 @stat_schema        sysname = null,
  22963.                 @stat_name        sysname = null)
  22964. as 
  22965. begin
  22966.     
  22967.     set nocount on
  22968.  
  22969.     -- check in parameters
  22970.     if ((@table_catalog is not null) and (db_name() <> @table_catalog)
  22971.         or (@stat_catalog is not null) and (db_name() <> @stat_catalog))
  22972.         begin    /* If qualifier doesn't match current database */
  22973.             raiserror 20001 '~~Rush_5~~'
  22974.             return
  22975.         end
  22976.  
  22977.     --- create temp table
  22978.     create table #spstattab(
  22979.         tblcatalog    sysname collate database_default not null,
  22980.         tblschema    sysname collate database_default not null,
  22981.         tblname        sysname collate database_default not null,
  22982.         statname    sysname collate database_default not null
  22983.         )
  22984.  
  22985.     insert into #spstattab
  22986.     select 
  22987.         db_name(),
  22988.         user_name(o.uid),
  22989.         o.name,
  22990.         x.name
  22991.     from sysobjects o, sysindexes x
  22992.     where o.type in ('U')
  22993.         and (@table_name is null or o.name = @table_name)
  22994.         and (@table_schema is null or @table_schema = user_name(o.uid))
  22995.         and (@stat_name is null or @stat_name = x.name)
  22996.         and (@stat_schema is null or @stat_schema = user_name(o.uid))
  22997.         and x.id = o.id
  22998.         and permissions(o.id) <> 0
  22999.         and x.statblob is not null
  23000.     order by 1,2,3,4
  23001.  
  23002.     -- fast forward CURSOR OVER THE temp TABLE
  23003.     declare ff_csr  cursor fast_forward for
  23004.     select tblcatalog, tblschema, tblname, statname from #spstattab
  23005.  
  23006.     declare @tblcatalog    sysname,
  23007.         @tblschema    sysname,
  23008.         @tblname    sysname,
  23009.         @statname    sysname,
  23010.         @qtbl        nvarchar(4000),
  23011.         @rowsetcount    int
  23012.  
  23013.     set @rowsetcount = 0
  23014.  
  23015.     open ff_csr
  23016.     fetch ff_csr into @tblcatalog, @tblschema, @tblname, @statname
  23017.     while @@fetch_status >= 0
  23018.     begin
  23019.         set @rowsetcount = @rowsetcount + 1
  23020.         set @qtbl = @tblcatalog + '.' + @tblschema + '.' + @tblname
  23021.         dbcc show_statistics(@qtbl, @statname) with stat_header join density_vector
  23022.         fetch ff_csr into @tblcatalog, @tblschema, @tblname, @statname
  23023.     end
  23024.     close ff_csr
  23025.     deallocate ff_csr
  23026.     
  23027.     --- drop temp table
  23028.     drop table #spstattab
  23029.  
  23030.     if @rowsetcount = 0
  23031.     begin
  23032.         select    
  23033.             TABLE_CATALOG = convert(sysname, null),
  23034.             TABLE_SCHEMA = convert(sysname, null),
  23035.             TABLE_NAME = convert(sysname, null),
  23036.             STATISTICS_CATALOG = convert(sysname, null),
  23037.             STATISTICS_SCHEMA = convert(sysname, null),
  23038.             STATISTICS_NAME = convert(sysname, null),
  23039.             STATISTICS_TYPE = convert(smallint,0),
  23040.             COLUMN_NAME = convert(sysname, null),
  23041.             COLUMN_GUID = convert(uniqueidentifier, null),
  23042.             COLUMN_PROPID = convert(int, null),
  23043.             ORDINAL_POSITION = convert(int, null),
  23044.             SAMPLE_PCT = convert(smallint, null),
  23045.             LAST_UPDATE_TIME = convert(datetime, null),
  23046.             NO_OF_RANGES = convert(int, null),
  23047.             COLUMN_CARDINALITY = convert(bigint, null),
  23048.             TUPLE_CARDINALITY = convert(bigint, null),
  23049.             TABLE_CARDINALITY = convert(bigint, null),
  23050.             AVG_COLUMN_LENGTH = convert(int, null)
  23051.         where 1=0
  23052.     end
  23053.  
  23054.     set nocount off
  23055. end
  23056. go
  23057.  
  23058. grant execute on sp_table_statistics_rowset to public
  23059. go
  23060.  
  23061. dump tran master with no_log
  23062. go
  23063.  
  23064. print ''
  23065. print 'creating sp_oledb_column_constraints'
  23066. go
  23067.  
  23068. /* Procedure for 6.5 server */
  23069. create procedure sp_oledb_column_constraints
  23070.     (@tblname varchar(225)        -- the table to check for constraints
  23071.     ,@colname varchar(225)        -- the column to check for constraints
  23072.     ,@dropcnst smallint)
  23073. as
  23074. begin
  23075.     -- PRELIM
  23076.     set nocount on
  23077.  
  23078.     declare    @tblid        int           -- the object id of the table
  23079.         ,@cnstname        varchar(225)   -- name of const. currently under consideration
  23080.         ,@cnstid        int
  23081.         ,@cnstatus        int
  23082.         ,@dbname        varchar(30)
  23083.  
  23084.     -- Create temp table
  23085.     create table #spcnsttab
  23086.     (cnst_name        varchar(225)        NOT NULL)
  23087.  
  23088.     -- Check to see that the object names are local to the current database.
  23089.     if @tblname like '%.%.%'
  23090.           and  substring(@tblname, 1, charindex('.', @tblname) - 1) <> db_name()
  23091.        begin
  23092.            raiserror(15250,-1,-1)
  23093.            return (1)
  23094.        end
  23095.     -- Check to see if the table exists and initialize @objid.
  23096.     select @tblid = object_id(@tblname)
  23097.     if @tblid is NULL
  23098.     begin
  23099.         select @dbname=db_name()
  23100.         raiserror(15009,-1,-1,@tblname,@dbname)
  23101.         return (1)
  23102.     end
  23103.  
  23104.     -- STATIC CURSOR OVER THE TABLE'S CONSTRAINTS
  23105.     declare cnst_csr insensitive cursor for
  23106.         select c.constid, c.status, o.name 
  23107.         from sysconstraints c, sysobjects o 
  23108.         where c.id = @tblid and o.id = c.constid
  23109.             and ((c.status & 0xf)=1 and (@dropcnst & 16)<> 0
  23110.             or (c.status & 0xf)=2 and (@dropcnst & 32)<> 0 
  23111.             or (c.status & 0xf)=5 and (@dropcnst & 2)<> 0)    
  23112.         -- ONLY 6.5 sysconstraints objects
  23113.         for read only
  23114.  
  23115.     -- Now check out each constraint, figure out its type and keys and
  23116.     -- save the info in a temporary table that we'll print out at the end.
  23117.     open cnst_csr
  23118.     fetch cnst_csr into @cnstid, @cnstatus, @cnstname
  23119.     while @@fetch_status >= 0
  23120.     begin
  23121.         if ((@cnstatus & 0xf) in (1, 2)) ---- primary key, unique
  23122.             begin
  23123.                 -- get indid
  23124.                 declare @indid smallint
  23125.                 select    @indid = indid
  23126.                 from    sysindexes
  23127.                 where    name = object_name(@cnstid) and id = @tblid
  23128.  
  23129.                 if (@colname = index_col(@tblname, @indid, 1)) 
  23130.                     and (index_col(@tblname, @indid, 2) is null)
  23131.                     begin
  23132.                         -- ADD TO TABLE
  23133.                         insert into #spcnsttab
  23134.                         values (@cnstname)
  23135.                     end
  23136.             end
  23137.         else
  23138.         if (@cnstatus & 0xf) = 5
  23139.             begin
  23140.                 if (select col_name(id, colid) from sysconstraints where colid > 0 and constid=@cnstid) = @colname
  23141.                     begin
  23142.                         insert into #spcnsttab
  23143.                         values (@cnstname)
  23144.                     end
  23145.             end
  23146.  
  23147.         fetch cnst_csr into @cnstid ,@cnstatus ,@cnstname
  23148.     end        --of major loop
  23149.     close cnst_csr
  23150.     deallocate cnst_csr
  23151.  
  23152.     -- Now print out the contents of the temporary index table.
  23153.     select 'constraint_name' = cnst_name
  23154.     from #spcnsttab 
  23155.     
  23156.     drop table #spcnsttab
  23157.  
  23158.     set nocount off
  23159.     return (0)
  23160. end    
  23161. go
  23162. dump tran master with no_log
  23163. go
  23164.  
  23165. if (charindex('7.00', @@version) = 0 and
  23166.     charindex('8.00', @@version) = 0)
  23167. begin
  23168.     print ''
  23169.     print ''
  23170.     print 'Warning:'
  23171.     print 'you are installing the stored procedures '
  23172.     print 'on a pre 7.0 SQL Server.'
  23173.     print 'Ignore the following errors.'
  23174. end
  23175. else
  23176.     drop proc sp_oledb_column_constraints
  23177. go
  23178.  
  23179. /* Procedure for 7.0 server */
  23180. create procedure sp_oledb_column_constraints
  23181.     (@tblname nvarchar(776)        -- the table to check for constraints
  23182.     ,@colname nvarchar(776)        -- the column to check for constraints
  23183.     ,@dropcnst smallint)
  23184. as
  23185. begin
  23186.     -- PRELIM
  23187.     set nocount on
  23188.  
  23189.     declare    @tblid        int           -- the object id of the table
  23190.         ,@cnstname        sysname       -- name of const. currently under consideration
  23191.         ,@cnstid        int
  23192.         ,@cnsttype        character(2)
  23193.         ,@dbname        sysname
  23194.  
  23195.     -- Create temp table
  23196.     create table #spcnsttab
  23197.     (cnst_name        sysname        NOT NULL)
  23198.  
  23199.     -- Check to see that the object names are local to the current database.
  23200.     select @dbname = parsename(@tblname,3) 
  23201.     if @dbname is not null and @dbname <> db_name()
  23202.     begin
  23203.         raiserror(15250,-1,-1)
  23204.         return (1)
  23205.     end
  23206.  
  23207.     -- Check to see if the table exists and initialize @objid.
  23208.     select @tblid = object_id(@tblname)
  23209.     if @tblid is NULL
  23210.     begin
  23211.         select @dbname=db_name()
  23212.         raiserror(15009,-1,-1,@tblname,@dbname)
  23213.         return (1)
  23214.     end
  23215.  
  23216.     -- STATIC CURSOR OVER THE TABLE'S CONSTRAINTS
  23217.     declare cnst_csr cursor fast_forward for
  23218.         select id, xtype, name 
  23219.         from sysobjects 
  23220.         where parent_obj = @tblid
  23221.             and ((xtype = 'PK' and (@dropcnst & 16)<> 0) 
  23222.             or (xtype = 'UQ' and (@dropcnst & 32)<> 0)
  23223.             or (xtype = 'D ' and (@dropcnst & 2)<> 0))    
  23224.         -- ONLY 6.5 sysconstraints objects
  23225.         for read only
  23226.  
  23227.     -- Now check out each constraint, figure out its type and keys and
  23228.     -- save the info in a temporary table that we'll print out at the end.
  23229.     open cnst_csr
  23230.     fetch cnst_csr into @cnstid, @cnsttype, @cnstname
  23231.     while @@fetch_status >= 0
  23232.     begin
  23233.         if @cnsttype in ('PK','UQ')
  23234.             begin
  23235.                 -- get indid
  23236.                 declare @indid smallint
  23237.                 select    @indid = indid
  23238.                 from    sysindexes
  23239.                 where    name = object_name(@cnstid) and id = @tblid
  23240.  
  23241.                 if (@colname = index_col(@tblname, @indid, 1)) 
  23242.                     and (index_col(@tblname, @indid, 2) is null)
  23243.                     begin
  23244.                         -- ADD TO TABLE
  23245.                         insert into #spcnsttab
  23246.                         values (@cnstname)
  23247.                     end
  23248.             end
  23249.         else
  23250.         if (@cnsttype = 'D ')
  23251.             begin
  23252.                 if (select col_name(@tblid, info) from sysobjects where id=@cnstid) = @colname
  23253.                     begin
  23254.                         insert into #spcnsttab
  23255.                         values (@cnstname)
  23256.                     end
  23257.             end
  23258.  
  23259.         fetch cnst_csr into @cnstid ,@cnsttype ,@cnstname
  23260.     end        --of major loop
  23261.     close cnst_csr
  23262.     deallocate cnst_csr
  23263.  
  23264.     -- Now print out the contents of the temporary index table.
  23265.     select 'constraint_name' = cnst_name
  23266.     from #spcnsttab 
  23267.  
  23268.     drop table #spcnsttab
  23269.     
  23270.     set nocount off
  23271.     return (0)
  23272. end
  23273. go
  23274.  
  23275. if (charindex('8.00', @@version) = 0)
  23276. begin
  23277.     print ''
  23278.     print ''
  23279.     print 'Warning:'
  23280.     print 'you are installing the stored procedures '
  23281.     print 'on a pre 8.0 SQL Server.'
  23282.     print 'Ignore the following errors.'
  23283. end
  23284. else
  23285.     drop proc sp_oledb_column_constraints
  23286. go
  23287.  
  23288. /* Procedure for 8.0 server */
  23289. create procedure sp_oledb_column_constraints
  23290.     (@tblname nvarchar(776)        -- the table to check for constraints
  23291.     ,@colname nvarchar(776)        -- the column to check for constraints
  23292.     ,@dropcnst smallint)
  23293. as
  23294. begin
  23295.     -- PRELIM
  23296.     set nocount on
  23297.  
  23298.     declare    @tblid        int           -- the object id of the table
  23299.         ,@cnstname        sysname       -- name of const. currently under consideration
  23300.         ,@cnstid        int
  23301.         ,@cnsttype        character(2)
  23302.         ,@dbname        sysname
  23303.  
  23304.     -- Create temp table
  23305.     create table #spcnsttab
  23306.     (cnst_name        sysname        collate database_default NOT NULL)
  23307.  
  23308.     -- Check to see that the object names are local to the current database.
  23309.     select @dbname = parsename(@tblname,3) 
  23310.     if @dbname is not null and @dbname <> db_name()
  23311.     begin
  23312.         raiserror(15250,-1,-1)
  23313.         return (1)
  23314.     end
  23315.  
  23316.     -- Check to see if the table exists and initialize @objid.
  23317.     select @tblid = object_id(@tblname)
  23318.     if @tblid is NULL
  23319.     begin
  23320.         select @dbname=db_name()
  23321.         raiserror(15009,-1,-1,@tblname,@dbname)
  23322.         return (1)
  23323.     end
  23324.  
  23325.     -- STATIC CURSOR OVER THE TABLE'S CONSTRAINTS
  23326.     declare cnst_csr cursor fast_forward for
  23327.         select id, xtype, name 
  23328.         from sysobjects 
  23329.         where parent_obj = @tblid
  23330.             and ((xtype = 'PK' and (@dropcnst & 16)<> 0) 
  23331.             or (xtype = 'UQ' and (@dropcnst & 32)<> 0)
  23332.             or (xtype = 'D ' and (@dropcnst & 2)<> 0))    
  23333.         -- ONLY 6.5 sysconstraints objects
  23334.         for read only
  23335.  
  23336.     -- Now check out each constraint, figure out its type and keys and
  23337.     -- save the info in a temporary table that we'll print out at the end.
  23338.     open cnst_csr
  23339.     fetch cnst_csr into @cnstid, @cnsttype, @cnstname
  23340.     while @@fetch_status >= 0
  23341.     begin
  23342.         if @cnsttype in ('PK','UQ')
  23343.             begin
  23344.                 -- get indid
  23345.                 declare @indid smallint
  23346.                 select    @indid = indid
  23347.                 from    sysindexes
  23348.                 where    name = object_name(@cnstid) and id = @tblid
  23349.  
  23350.                 if (@colname = index_col(@tblname, @indid, 1)) 
  23351.                     and (index_col(@tblname, @indid, 2) is null)
  23352.                     begin
  23353.                         -- ADD TO TABLE
  23354.                         insert into #spcnsttab
  23355.                         values (@cnstname)
  23356.                     end
  23357.             end
  23358.         else
  23359.         if (@cnsttype = 'D ')
  23360.             begin
  23361.                 if (select col_name(@tblid, info) from sysobjects where id=@cnstid) = @colname
  23362.                     begin
  23363.                         insert into #spcnsttab
  23364.                         values (@cnstname)
  23365.                     end
  23366.             end
  23367.  
  23368.         fetch cnst_csr into @cnstid ,@cnsttype ,@cnstname
  23369.     end        --of major loop
  23370.     close cnst_csr
  23371.     deallocate cnst_csr
  23372.  
  23373.     -- Now print out the contents of the temporary index table.
  23374.     select 'constraint_name' = cnst_name
  23375.     from #spcnsttab 
  23376.  
  23377.     drop table #spcnsttab
  23378.     
  23379.     set nocount off
  23380.     return (0)
  23381. end
  23382. go
  23383.  
  23384. grant execute on sp_oledb_column_constraints to public
  23385. go
  23386.  
  23387. dump tran master with no_log
  23388. go
  23389.  
  23390. print ''
  23391. print 'creating sp_oledb_indexinfo'
  23392. go
  23393.  
  23394. /* Procedure for 6.5 server */
  23395. create procedure sp_oledb_indexinfo
  23396.         @objname varchar(225)        -- the table to check for indexes
  23397.         ,@indname varchar(225)
  23398. as
  23399. begin
  23400.     -- PRELIM
  23401.     set nocount on
  23402.  
  23403.     declare @objid int,            -- the object id of the table
  23404.         @indid smallint,    -- the index id of an index
  23405.         @status int,
  23406.         @dbname    varchar(30),
  23407.         @OrigFillFactor tinyint,
  23408.         @i int, 
  23409.         @thiskey varchar(32),
  23410.         @tptr     varbinary(16) -- pointer for building text strings.
  23411.     ---- Check to see that the object names are local to the current database.
  23412.     if @objname like '%.%.%'
  23413.           and  substring(@objname, 1, charindex('.', @objname) - 1) <> db_name()
  23414.        begin
  23415.            raiserror(15250,-1,-1)
  23416.            return (1)
  23417.        end
  23418.     ---- Check to see if the table exists and initialize @objid.
  23419.     select @objid = object_id(@objname)
  23420.     ---- Table does not exist so return.
  23421.     if @objid is NULL
  23422.        begin
  23423.            select @dbname=db_name()
  23424.            raiserror(15009,-1,-1,@objname,@dbname)
  23425.            return (1)
  23426.        end
  23427.  
  23428.     select @indid = indid, @status = status, @OrigFillFactor = OrigFillFactor
  23429.     from sysindexes
  23430.     where id = @objid and name = @indname 
  23431.  
  23432.     -- IF NO INDEX, QUIT
  23433.     if @indid is NULL
  23434.     begin
  23435.         raiserror(15472,-1,-1) --'Object does not have any indexes.'
  23436.         return (1)
  23437.     end
  23438.     -- create temp table
  23439.     create table #spindtab
  23440.     (
  23441.         status                int,
  23442.         OrigFillFactor            tinyint,
  23443.         index_keys            text    NOT NULL,
  23444.     )
  23445.     -- Now check out each index, figure out its type and keys and
  23446.     -- First we'll figure out what the keys are.
  23447.     select @i = 1
  23448.     while (@i <= 16)
  23449.         begin
  23450.             select @thiskey = index_col(@objname, @indid, @i)
  23451.              if @thiskey is NULL
  23452.                     goto keysdone
  23453.              if @i=1
  23454.                 begin
  23455.                     insert into #spindtab
  23456.                     values (@status, @OrigFillFactor, @thiskey)
  23457.  
  23458.             select @tptr = textptr(index_keys) from #spindtab
  23459.                 end
  23460.              else
  23461.                 begin
  23462.                     select @thiskey = ', ' + @thiskey
  23463.  
  23464.             if @tptr is not null
  23465.                        updatetext #spindtab.index_keys @tptr null null @thiskey
  23466.                 end
  23467.  
  23468.         select @i = @i + 1
  23469.          end --loop 16
  23470.          ---- When we get here we now have all the keys.
  23471.  
  23472. keysdone:
  23473.     select * from #spindtab
  23474.     
  23475.     drop table #spindtab
  23476.     set nocount off
  23477.     return (0)
  23478. end
  23479. go
  23480. dump tran master with no_log
  23481. go
  23482.  
  23483. if (charindex('7.00', @@version) = 0 and
  23484.     charindex('8.00', @@version) = 0)
  23485. begin
  23486.     print ''
  23487.     print ''
  23488.     print 'Warning:'
  23489.     print 'you are installing the stored procedures '
  23490.     print 'on a pre 8.0 SQL Server.'
  23491.     print 'Ignore the following errors.'
  23492. end
  23493. else
  23494.     drop proc sp_oledb_indexinfo
  23495. go
  23496.  
  23497. /* Procedure for 8.0 server */
  23498. create proc sp_oledb_indexinfo
  23499.         @objname nvarchar(776)        -- the table to check for indexes
  23500.         ,@indname nvarchar(776)
  23501. as
  23502. begin
  23503.     -- PRELIM
  23504.     set nocount on
  23505.  
  23506.     declare @objid int,            -- the object id of the table
  23507.         @indid smallint,    -- the index id of an index
  23508.         @status int,
  23509.         @keys nvarchar(2078),-- string build index key list, length = (16*max_id_length)+(15*2)
  23510.         @dbname    sysname,
  23511.         @OrigFillFactor tinyint
  23512.  
  23513.     -- Check to see that the object names are local to the current database.
  23514.     select @dbname = parsename(@objname,3) 
  23515.  
  23516.     if @dbname is not null and @dbname <> db_name()
  23517.     begin
  23518.             raiserror(15250,-1,-1)
  23519.             return (1)
  23520.     end
  23521.  
  23522.     -- Check to see the the table exists and initialize @objid.
  23523.     select @objid = object_id(@objname)
  23524.     if @objid is NULL
  23525.     begin
  23526.         select @dbname=db_name()
  23527.         raiserror(15009,-1,-1,@objname,@dbname)
  23528.         return (1)
  23529.     end
  23530.  
  23531.     select @indid = indid, @status = status, @OrigFillFactor = OrigFillFactor
  23532.     from sysindexes
  23533.     where id = @objid and name = @indname 
  23534.  
  23535.     -- IF NO INDEX, QUIT
  23536.     if @indid is NULL
  23537.     begin
  23538.         raiserror(15472,-1,-1) --'Object does not have any indexes.'
  23539.         return (1)
  23540.     end
  23541.  
  23542.     -- First we'll figure out what the keys are.
  23543.     declare @i int, @thiskey sysname
  23544.     select @keys = index_col(@objname, @indid, 1),
  23545.         @i = 2, @thiskey = index_col(@objname, @indid, 2)
  23546.     while (@thiskey is not null )
  23547.     begin
  23548.         select @keys = @keys + ', ' + @thiskey, @i = @i + 1
  23549.         select @thiskey = index_col(@objname, @indid, @i)
  23550.     end
  23551.     -- DISPLAY THE RESULTS
  23552.     select 'Status'=@status, 'OrigFillFactor' =@OrigFillFactor, 'Index keys'=@keys
  23553.     set nocount off
  23554.     return (0) 
  23555. end
  23556. go
  23557.  
  23558. grant execute on sp_oledb_indexinfo to public
  23559. go
  23560.  
  23561. dump tran master with no_log
  23562. go
  23563.  
  23564. print ''
  23565. print 'creating sp_oledb_ro_usrname'
  23566. go
  23567.  
  23568. create procedure sp_oledb_ro_usrname
  23569. as
  23570. begin
  23571.     select substring('NY',status/1024&1+1,1),user_name() 
  23572.     from master..sysdatabases 
  23573.     where name=DB_NAME()
  23574. end
  23575. go
  23576.  
  23577. grant execute on sp_oledb_ro_usrname to public
  23578. go
  23579.  
  23580. dump tran master with no_log
  23581. go
  23582.  
  23583. if (charindex('6.00', @@version) > 0 or
  23584.     charindex('6.50', @@version) > 0 or
  23585.     charindex('7.00', @@version) > 0)
  23586.     begin
  23587.     if (exists (select * from sysobjects
  23588.         where name = 'sp_oledb_ro_usrname' and type = 'P '))
  23589.         begin
  23590.             drop proc sp_oledb_ro_usrname
  23591.             dump tran master with no_log
  23592.         end
  23593.     end
  23594. go
  23595.  
  23596. print ''
  23597. print 'creating sp_oledb_deflang'
  23598. go
  23599.  
  23600. if (charindex('8.00', @@version) = 0)
  23601. begin
  23602.     print ''
  23603.     print ''
  23604.     print 'Warning:'
  23605.     print 'you are installing the stored procedures '
  23606.     print 'on a pre 8.0 SQL Server.'
  23607.     print 'Ignore the following errors.'
  23608. end
  23609. go
  23610.  
  23611. create procedure sp_oledb_deflang
  23612. as
  23613.     begin
  23614.         select ISNULL(language,'us_english') 
  23615.         from master..syslogins 
  23616.         where sid=SUSER_SID()
  23617.     end
  23618. go
  23619.  
  23620. grant execute on sp_oledb_deflang to public
  23621. go
  23622.  
  23623. dump tran master with no_log
  23624. go
  23625.  
  23626. if (charindex('6.00', @@version) > 0 or
  23627.     charindex('6.50', @@version) > 0 or
  23628.     charindex('7.00', @@version) > 0)
  23629.     begin
  23630.     if (exists (select * from sysobjects
  23631.         where name = 'sp_oledb_deflang' and type = 'P '))
  23632.         begin
  23633.             drop proc sp_oledb_deflang
  23634.             dump tran master with no_log
  23635.         end
  23636.     end
  23637. go
  23638.  
  23639. print ''
  23640. print 'creating sp_oledb_defdb'
  23641. go
  23642.  
  23643. if (charindex('8.00', @@version) = 0)
  23644. begin
  23645.     print ''
  23646.     print ''
  23647.     print 'Warning:'
  23648.     print 'you are installing the stored procedures '
  23649.     print 'on a pre 8.0 SQL Server.'
  23650.     print 'Ignore the following errors.'
  23651. end
  23652. go
  23653.  
  23654. create procedure sp_oledb_defdb
  23655. as
  23656.     begin
  23657.         select dbname from master..syslogins where sid=SUSER_SID()
  23658.     end
  23659. go
  23660.  
  23661. grant execute on sp_oledb_defdb to public
  23662. go
  23663.  
  23664. dump tran master with no_log
  23665. go
  23666.  
  23667. if (charindex('6.00', @@version) > 0 or
  23668.     charindex('6.50', @@version) > 0 or
  23669.     charindex('7.00', @@version) > 0)
  23670.     begin
  23671.     if (exists (select * from sysobjects
  23672.         where name = 'sp_oledb_defdb' and type = 'P '))
  23673.         begin
  23674.             drop proc sp_oledb_defdb
  23675.             dump tran master with no_log
  23676.         end
  23677.     end
  23678. go
  23679.  
  23680. print ''
  23681. print 'creating sp_oledb_database'
  23682. go
  23683.  
  23684. create procedure sp_oledb_database
  23685. as
  23686.     begin
  23687.         select name from master..sysdatabases
  23688.     end
  23689. go
  23690.  
  23691. grant execute on sp_oledb_database to public
  23692. go
  23693.  
  23694. dump tran master with no_log
  23695. go
  23696.  
  23697. if (charindex('6.00', @@version) > 0 or
  23698.     charindex('6.50', @@version) > 0 or
  23699.     charindex('7.00', @@version) > 0)
  23700.     begin
  23701.     if (exists (select * from sysobjects
  23702.         where name = 'sp_oledb_database' and type = 'P '))
  23703.         begin
  23704.             drop proc sp_oledb_database
  23705.             dump tran master with no_log
  23706.         end
  23707.     end
  23708. go
  23709.  
  23710. print ''
  23711. print 'creating sp_oledb_language'
  23712. go
  23713.  
  23714. create procedure sp_oledb_language
  23715. as
  23716. begin
  23717.     select 'English','us_english' 
  23718.     union 
  23719.     select alias,name from master..syslanguages
  23720. end
  23721. go
  23722.  
  23723. grant execute on sp_oledb_language to public
  23724. go
  23725.  
  23726. dump tran master with no_log
  23727. go
  23728. if (charindex('6.00', @@version) > 0 or
  23729.     charindex('6.50', @@version) > 0 or
  23730.     charindex('7.00', @@version) > 0)
  23731.     begin
  23732.     if (exists (select * from sysobjects
  23733.         where name = 'sp_oledb_language' and type = 'P '))
  23734.         begin
  23735.             drop proc sp_oledb_language
  23736.             dump tran master with no_log
  23737.         end
  23738.     end
  23739. go
  23740.  
  23741. /*---------------------------- END OLEDB CATALOG PROCS ------------------------*/
  23742.  
  23743.  
  23744. /*---------------------------- BEGIN BCP CATALOG PROCS ------------------------*/
  23745.  
  23746. print 'creating sp_tablecollations'
  23747. go
  23748.  
  23749. /* Used by BCP to gather all the collation names for a table */
  23750. if (charindex('8.00', @@version) = 0)
  23751. begin
  23752.     print ''
  23753.     print ''
  23754.     print 'Warning:'
  23755.     print 'you are installing the stored procedures '
  23756.     print 'on a pre 8.0 SQL Server.'
  23757.     print 'Ignore the following errors.'
  23758. end
  23759. go
  23760.  
  23761. /* Procedure for 8.0 server */
  23762. create procedure sp_tablecollations (@object nvarchar(4000))
  23763. as
  23764. begin
  23765.     select c.colid,
  23766.            c.name, 
  23767.            tds_collation = c.tdscollation,
  23768.            collation_name = BCPCollationName(c.tdscollation, c.xtype)
  23769.     from syscolumns c inner join sysobjects t on c.id = t.id
  23770.     where t.id = object_id(@object, 'local')
  23771.     order by c.colid
  23772. end
  23773. go
  23774.  
  23775. grant execute on sp_tablecollations to public
  23776. go
  23777.  
  23778. print 'creating sp_bcp_dbcmptlevel'
  23779. go
  23780.  
  23781. /* Used by BCP to get current db compatibility level */
  23782. if (charindex('8.00', @@version) = 0)
  23783. begin
  23784.     print ''
  23785.     print ''
  23786.     print 'Warning:'
  23787.     print 'you are installing the stored procedures '
  23788.     print 'on a pre 8.0 SQL Server.'
  23789.     print 'Ignore the following errors.'
  23790. end
  23791. go
  23792.  
  23793. /* Procedure for 8.0 server */
  23794. create procedure sp_bcp_dbcmptlevel (@dbname sysname)
  23795. as
  23796. begin
  23797.     select cmptlevel
  23798.     from master.dbo.sysdatabases
  23799.     where name = @dbname
  23800. end
  23801. go
  23802.  
  23803. grant execute on sp_bcp_dbcmptlevel to public
  23804. go
  23805.  
  23806. /*---------------------------- END BCP CATALOG PROCS ------------------------*/
  23807.  
  23808. dump tran master with no_log
  23809. go
  23810.  
  23811.  
  23812.  
  23813.  
  23814. if (exists (select * from sysobjects
  23815.         where name = 'sp_configure' and type = 'P '))
  23816.     begin
  23817.         exec sp_configure 'allow updates',0
  23818.         reconfigure with override
  23819.     end
  23820. go
  23821.  
  23822. exec sp_MS_upd_sysobj_category 2 /* set category | 2 based on crdate. */
  23823.  
  23824. go
  23825.  
  23826. if exists (select * from sysobjects where name = 'sp_check_objects'
  23827.             and type = 'P ')
  23828.     begin
  23829.         /* Only supported on 6.0 servers */
  23830.         print ''
  23831.         print 'Checking objects created by instcat.sql.'
  23832.  
  23833.         exec sp_check_objects 'catalog'
  23834.     end
  23835. go
  23836.  
  23837. print ''
  23838. print 'instcat.sql completed successfully.'
  23839. go
  23840.  
  23841. set quoted_identifier off
  23842. go
  23843.  
  23844. dump tran master with no_log
  23845. go
  23846. checkpoint
  23847. go
  23848. /**/
  23849.